blob: 1545b0a0268d10c536f25b415e3a1641b01acb91 [file] [log] [blame]
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ART_COMPILER_OPTIMIZING_NODES_H_
18#define ART_COMPILER_OPTIMIZING_NODES_H_
19
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010020#include "locations.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010021#include "offsets.h"
Ian Rogerse63db272014-07-15 15:36:11 -070022#include "primitive.h"
Ian Rogers0279ebb2014-10-08 17:27:48 -070023#include "utils/arena_object.h"
Nicolas Geoffray0e336432014-02-26 18:24:38 +000024#include "utils/arena_bit_vector.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000025#include "utils/growable_array.h"
26
27namespace art {
28
29class HBasicBlock;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010030class HEnvironment;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000031class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000032class HIntConstant;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000033class HGraphVisitor;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010034class HPhi;
Nicolas Geoffray3c049742014-09-24 18:10:46 +010035class HSuspendCheck;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010036class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000037class LocationSummary;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000038
39static const int kDefaultNumberOfBlocks = 8;
40static const int kDefaultNumberOfSuccessors = 2;
41static const int kDefaultNumberOfPredecessors = 2;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010042static const int kDefaultNumberOfDominatedBlocks = 1;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000043static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000044
Dave Allison20dfc792014-06-16 20:44:29 -070045enum IfCondition {
46 kCondEQ,
47 kCondNE,
48 kCondLT,
49 kCondLE,
50 kCondGT,
51 kCondGE,
52};
53
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010054class HInstructionList {
55 public:
56 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
57
58 void AddInstruction(HInstruction* instruction);
59 void RemoveInstruction(HInstruction* instruction);
60
Roland Levillain6b469232014-09-25 10:10:38 +010061 // Return true if this list contains `instruction`.
62 bool Contains(HInstruction* instruction) const;
63
Roland Levillainccc07a92014-09-16 14:48:16 +010064 // Return true if `instruction1` is found before `instruction2` in
65 // this instruction list and false otherwise. Abort if none
66 // of these instructions is found.
67 bool FoundBefore(const HInstruction* instruction1,
68 const HInstruction* instruction2) const;
69
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010070 private:
71 HInstruction* first_instruction_;
72 HInstruction* last_instruction_;
73
74 friend class HBasicBlock;
75 friend class HInstructionIterator;
Nicolas Geoffray804d0932014-05-02 08:46:00 +010076 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010077
78 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
79};
80
Nicolas Geoffray818f2102014-02-18 16:43:35 +000081// Control-flow graph of a method. Contains a list of basic blocks.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070082class HGraph : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +000083 public:
84 explicit HGraph(ArenaAllocator* arena)
85 : arena_(arena),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000086 blocks_(arena, kDefaultNumberOfBlocks),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +010087 reverse_post_order_(arena, kDefaultNumberOfBlocks),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070088 entry_block_(nullptr),
89 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +010090 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +010091 number_of_vregs_(0),
92 number_of_in_vregs_(0),
Nicolas Geoffraye5038322014-07-04 09:41:32 +010093 number_of_temporaries_(0),
Dave Allison20dfc792014-06-16 20:44:29 -070094 current_instruction_id_(0) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +000095
Nicolas Geoffray787c3072014-03-17 10:20:19 +000096 ArenaAllocator* GetArena() const { return arena_; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +010097 const GrowableArray<HBasicBlock*>& GetBlocks() const { return blocks_; }
Roland Levillain93445682014-10-06 19:24:02 +010098 HBasicBlock* GetBlock(size_t id) const { return blocks_.Get(id); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +000099
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000100 HBasicBlock* GetEntryBlock() const { return entry_block_; }
101 HBasicBlock* GetExitBlock() const { return exit_block_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000102
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000103 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
104 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000105
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000106 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100107
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000108 void BuildDominatorTree();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100109 void TransformToSSA();
110 void SimplifyCFG();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000111
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100112 // Find all natural loops in this graph. Aborts computation and returns false
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100113 // if one loop is not natural, that is the header does not dominate the back
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100114 // edge.
115 bool FindNaturalLoops() const;
116
117 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
118 void SimplifyLoop(HBasicBlock* header);
119
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000120 int GetNextInstructionId() {
121 return current_instruction_id_++;
122 }
123
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100124 uint16_t GetMaximumNumberOfOutVRegs() const {
125 return maximum_number_of_out_vregs_;
126 }
127
128 void UpdateMaximumNumberOfOutVRegs(uint16_t new_value) {
129 maximum_number_of_out_vregs_ = std::max(new_value, maximum_number_of_out_vregs_);
130 }
131
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100132 void UpdateNumberOfTemporaries(size_t count) {
133 number_of_temporaries_ = std::max(count, number_of_temporaries_);
134 }
135
136 size_t GetNumberOfTemporaries() const {
137 return number_of_temporaries_;
138 }
139
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100140 void SetNumberOfVRegs(uint16_t number_of_vregs) {
141 number_of_vregs_ = number_of_vregs;
142 }
143
144 uint16_t GetNumberOfVRegs() const {
145 return number_of_vregs_;
146 }
147
148 void SetNumberOfInVRegs(uint16_t value) {
149 number_of_in_vregs_ = value;
150 }
151
152 uint16_t GetNumberOfInVRegs() const {
153 return number_of_in_vregs_;
154 }
155
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100156 uint16_t GetNumberOfLocalVRegs() const {
157 return number_of_vregs_ - number_of_in_vregs_;
158 }
159
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100160 const GrowableArray<HBasicBlock*>& GetReversePostOrder() const {
161 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100162 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100163
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000164 private:
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000165 HBasicBlock* FindCommonDominator(HBasicBlock* first, HBasicBlock* second) const;
166 void VisitBlockForDominatorTree(HBasicBlock* block,
167 HBasicBlock* predecessor,
168 GrowableArray<size_t>* visits);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100169 void FindBackEdges(ArenaBitVector* visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000170 void VisitBlockForBackEdges(HBasicBlock* block,
171 ArenaBitVector* visited,
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100172 ArenaBitVector* visiting);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000173 void RemoveDeadBlocks(const ArenaBitVector& visited) const;
174
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000175 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000176
177 // List of blocks in insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000178 GrowableArray<HBasicBlock*> blocks_;
179
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100180 // List of blocks to perform a reverse post order tree traversal.
181 GrowableArray<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000182
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000183 HBasicBlock* entry_block_;
184 HBasicBlock* exit_block_;
185
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100186 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100187 uint16_t maximum_number_of_out_vregs_;
188
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100189 // The number of virtual registers in this method. Contains the parameters.
190 uint16_t number_of_vregs_;
191
192 // The number of virtual registers used by parameters of this method.
193 uint16_t number_of_in_vregs_;
194
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100195 // The number of temporaries that will be needed for the baseline compiler.
196 size_t number_of_temporaries_;
197
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000198 // The current id to assign to a newly added instruction. See HInstruction.id_.
199 int current_instruction_id_;
200
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000201 DISALLOW_COPY_AND_ASSIGN(HGraph);
202};
203
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700204class HLoopInformation : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000205 public:
206 HLoopInformation(HBasicBlock* header, HGraph* graph)
207 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100208 suspend_check_(nullptr),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100209 back_edges_(graph->GetArena(), kDefaultNumberOfBackEdges),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100210 // Make bit vector growable, as the number of blocks may change.
211 blocks_(graph->GetArena(), graph->GetBlocks().Size(), true) {}
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100212
213 HBasicBlock* GetHeader() const {
214 return header_;
215 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000216
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100217 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
218 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
219 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
220
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000221 void AddBackEdge(HBasicBlock* back_edge) {
222 back_edges_.Add(back_edge);
223 }
224
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100225 void RemoveBackEdge(HBasicBlock* back_edge) {
226 back_edges_.Delete(back_edge);
227 }
228
229 bool IsBackEdge(HBasicBlock* block) {
230 for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) {
231 if (back_edges_.Get(i) == block) return true;
232 }
233 return false;
234 }
235
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000236 int NumberOfBackEdges() const {
237 return back_edges_.Size();
238 }
239
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100240 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100241
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100242 const GrowableArray<HBasicBlock*>& GetBackEdges() const {
243 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100244 }
245
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100246 void ClearBackEdges() {
247 back_edges_.Reset();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100248 }
249
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100250 // Find blocks that are part of this loop. Returns whether the loop is a natural loop,
251 // that is the header dominates the back edge.
252 bool Populate();
253
254 // Returns whether this loop information contains `block`.
255 // Note that this loop information *must* be populated before entering this function.
256 bool Contains(const HBasicBlock& block) const;
257
258 // Returns whether this loop information is an inner loop of `other`.
259 // Note that `other` *must* be populated before entering this function.
260 bool IsIn(const HLoopInformation& other) const;
261
262 const ArenaBitVector& GetBlocks() const { return blocks_; }
263
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000264 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100265 // Internal recursive implementation of `Populate`.
266 void PopulateRecursive(HBasicBlock* block);
267
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000268 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100269 HSuspendCheck* suspend_check_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000270 GrowableArray<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100271 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000272
273 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
274};
275
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100276static constexpr size_t kNoLifetime = -1;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100277static constexpr uint32_t kNoDexPc = -1;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100278
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000279// A block in a method. Contains the list of instructions represented
280// as a double linked list. Each block knows its predecessors and
281// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100282
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700283class HBasicBlock : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000284 public:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100285 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000286 : graph_(graph),
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000287 predecessors_(graph->GetArena(), kDefaultNumberOfPredecessors),
288 successors_(graph->GetArena(), kDefaultNumberOfSuccessors),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000289 loop_information_(nullptr),
290 dominator_(nullptr),
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100291 dominated_blocks_(graph->GetArena(), kDefaultNumberOfDominatedBlocks),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100292 block_id_(-1),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100293 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100294 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000295 lifetime_end_(kNoLifetime),
296 is_catch_block_(false) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000297
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100298 const GrowableArray<HBasicBlock*>& GetPredecessors() const {
299 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000300 }
301
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100302 const GrowableArray<HBasicBlock*>& GetSuccessors() const {
303 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000304 }
305
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100306 const GrowableArray<HBasicBlock*>& GetDominatedBlocks() const {
307 return dominated_blocks_;
308 }
309
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100310 bool IsEntryBlock() const {
311 return graph_->GetEntryBlock() == this;
312 }
313
314 bool IsExitBlock() const {
315 return graph_->GetExitBlock() == this;
316 }
317
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000318 void AddBackEdge(HBasicBlock* back_edge) {
319 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000320 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000321 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100322 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000323 loop_information_->AddBackEdge(back_edge);
324 }
325
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000326 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000327
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000328 int GetBlockId() const { return block_id_; }
329 void SetBlockId(int id) { block_id_ = id; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000330
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000331 HBasicBlock* GetDominator() const { return dominator_; }
332 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100333 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.Add(block); }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000334
335 int NumberOfBackEdges() const {
336 return loop_information_ == nullptr
337 ? 0
338 : loop_information_->NumberOfBackEdges();
339 }
340
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100341 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
342 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100343 const HInstructionList& GetInstructions() const { return instructions_; }
344 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100345 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000346
347 void AddSuccessor(HBasicBlock* block) {
348 successors_.Add(block);
349 block->predecessors_.Add(this);
350 }
351
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100352 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
353 size_t successor_index = GetSuccessorIndexOf(existing);
354 DCHECK_NE(successor_index, static_cast<size_t>(-1));
355 existing->RemovePredecessor(this);
356 new_block->predecessors_.Add(this);
357 successors_.Put(successor_index, new_block);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000358 }
359
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100360 void RemovePredecessor(HBasicBlock* block) {
361 predecessors_.Delete(block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100362 }
363
364 void ClearAllPredecessors() {
365 predecessors_.Reset();
366 }
367
368 void AddPredecessor(HBasicBlock* block) {
369 predecessors_.Add(block);
370 block->successors_.Add(this);
371 }
372
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100373 void SwapPredecessors() {
Nicolas Geoffrayc83d4412014-09-18 16:46:20 +0100374 DCHECK_EQ(predecessors_.Size(), 2u);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100375 HBasicBlock* temp = predecessors_.Get(0);
376 predecessors_.Put(0, predecessors_.Get(1));
377 predecessors_.Put(1, temp);
378 }
379
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100380 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) {
381 for (size_t i = 0, e = predecessors_.Size(); i < e; ++i) {
382 if (predecessors_.Get(i) == predecessor) {
383 return i;
384 }
385 }
386 return -1;
387 }
388
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100389 size_t GetSuccessorIndexOf(HBasicBlock* successor) {
390 for (size_t i = 0, e = successors_.Size(); i < e; ++i) {
391 if (successors_.Get(i) == successor) {
392 return i;
393 }
394 }
395 return -1;
396 }
397
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000398 void AddInstruction(HInstruction* instruction);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100399 void RemoveInstruction(HInstruction* instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100400 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +0100401 // Replace instruction `initial` with `replacement` within this block.
402 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
403 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100404 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100405 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100406 void RemovePhi(HPhi* phi);
407
408 bool IsLoopHeader() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100409 return (loop_information_ != nullptr) && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100410 }
411
Roland Levillain6b879dd2014-09-22 17:13:44 +0100412 bool IsLoopPreHeaderFirstPredecessor() const {
413 DCHECK(IsLoopHeader());
414 DCHECK(!GetPredecessors().IsEmpty());
415 return GetPredecessors().Get(0) == GetLoopInformation()->GetPreHeader();
416 }
417
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100418 HLoopInformation* GetLoopInformation() const {
419 return loop_information_;
420 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000421
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100422 // Set the loop_information_ on this block. This method overrides the current
423 // loop_information if it is an outer loop of the passed loop information.
424 void SetInLoop(HLoopInformation* info) {
425 if (IsLoopHeader()) {
426 // Nothing to do. This just means `info` is an outer loop.
427 } else if (loop_information_ == nullptr) {
428 loop_information_ = info;
429 } else if (loop_information_->Contains(*info->GetHeader())) {
430 // Block is currently part of an outer loop. Make it part of this inner loop.
431 // Note that a non loop header having a loop information means this loop information
432 // has already been populated
433 loop_information_ = info;
434 } else {
435 // Block is part of an inner loop. Do not update the loop information.
436 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
437 // at this point, because this method is being called while populating `info`.
438 }
439 }
440
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100441 bool IsInLoop() const { return loop_information_ != nullptr; }
442
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100443 // Returns wheter this block dominates the blocked passed as parameter.
444 bool Dominates(HBasicBlock* block) const;
445
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100446 size_t GetLifetimeStart() const { return lifetime_start_; }
447 size_t GetLifetimeEnd() const { return lifetime_end_; }
448
449 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
450 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
451
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100452 uint32_t GetDexPc() const { return dex_pc_; }
453
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000454 bool IsCatchBlock() const { return is_catch_block_; }
455 void SetIsCatchBlock() { is_catch_block_ = true; }
456
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000457 private:
458 HGraph* const graph_;
459 GrowableArray<HBasicBlock*> predecessors_;
460 GrowableArray<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100461 HInstructionList instructions_;
462 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000463 HLoopInformation* loop_information_;
464 HBasicBlock* dominator_;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100465 GrowableArray<HBasicBlock*> dominated_blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000466 int block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100467 // The dex program counter of the first instruction of this block.
468 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100469 size_t lifetime_start_;
470 size_t lifetime_end_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000471 bool is_catch_block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000472
473 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
474};
475
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100476#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
477 M(Add, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000478 M(ArrayGet, Instruction) \
479 M(ArrayLength, Instruction) \
480 M(ArraySet, Instruction) \
481 M(BoundsCheck, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000482 M(CheckCast, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100483 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000484 M(Compare, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100485 M(Condition, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000486 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +0000487 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000488 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100489 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000490 M(Exit, Instruction) \
491 M(FloatConstant, Constant) \
492 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100493 M(GreaterThan, Condition) \
494 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100495 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000496 M(InstanceFieldGet, Instruction) \
497 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000498 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100499 M(IntConstant, Constant) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +0000500 M(InvokeInterface, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100501 M(InvokeStatic, Invoke) \
502 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000503 M(LessThan, Condition) \
504 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000505 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000506 M(LoadException, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100507 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000508 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100509 M(Local, Instruction) \
510 M(LongConstant, Constant) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +0000511 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000512 M(Mul, BinaryOperation) \
513 M(Neg, UnaryOperation) \
514 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100515 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +0100516 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000517 M(NotEqual, Condition) \
518 M(NullCheck, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100519 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000520 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100521 M(Phi, Instruction) \
522 M(Return, Instruction) \
523 M(ReturnVoid, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100524 M(StaticFieldGet, Instruction) \
525 M(StaticFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100526 M(StoreLocal, Instruction) \
527 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100528 M(SuspendCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000529 M(Temporary, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000530 M(Throw, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +0000531 M(TypeConversion, Instruction) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000532
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100533#define FOR_EACH_INSTRUCTION(M) \
534 FOR_EACH_CONCRETE_INSTRUCTION(M) \
535 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +0100536 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +0100537 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100538 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -0700539
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100540#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000541FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
542#undef FORWARD_DECLARATION
543
Roland Levillainccc07a92014-09-16 14:48:16 +0100544#define DECLARE_INSTRUCTION(type) \
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100545 virtual InstructionKind GetKind() const { return k##type; } \
Roland Levillainccc07a92014-09-16 14:48:16 +0100546 virtual const char* DebugName() const { return #type; } \
547 virtual const H##type* As##type() const OVERRIDE { return this; } \
548 virtual H##type* As##type() OVERRIDE { return this; } \
549 virtual bool InstructionTypeEquals(HInstruction* other) const { \
550 return other->Is##type(); \
551 } \
552 virtual void Accept(HGraphVisitor* visitor)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000553
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100554template <typename T>
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700555class HUseListNode : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000556 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100557 HUseListNode(T* user, size_t index, HUseListNode* tail)
Dave Allison20dfc792014-06-16 20:44:29 -0700558 : user_(user), index_(index), tail_(tail) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000559
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000560 HUseListNode* GetTail() const { return tail_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100561 T* GetUser() const { return user_; }
562 size_t GetIndex() const { return index_; }
563
564 void SetTail(HUseListNode<T>* node) { tail_ = node; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000565
566 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100567 T* const user_;
568 const size_t index_;
569 HUseListNode<T>* tail_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000570
571 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
572};
573
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100574// Represents the side effects an instruction may have.
575class SideEffects : public ValueObject {
576 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100577 SideEffects() : flags_(0) {}
578
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100579 static SideEffects None() {
580 return SideEffects(0);
581 }
582
583 static SideEffects All() {
584 return SideEffects(ChangesSomething().flags_ | DependsOnSomething().flags_);
585 }
586
587 static SideEffects ChangesSomething() {
588 return SideEffects((1 << kFlagChangesCount) - 1);
589 }
590
591 static SideEffects DependsOnSomething() {
592 int count = kFlagDependsOnCount - kFlagChangesCount;
593 return SideEffects(((1 << count) - 1) << kFlagChangesCount);
594 }
595
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100596 SideEffects Union(SideEffects other) const {
597 return SideEffects(flags_ | other.flags_);
598 }
599
Roland Levillain72bceff2014-09-15 18:29:00 +0100600 bool HasSideEffects() const {
601 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
602 return (flags_ & all_bits_set) != 0;
603 }
604
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100605 bool HasAllSideEffects() const {
606 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
607 return all_bits_set == (flags_ & all_bits_set);
608 }
609
610 bool DependsOn(SideEffects other) const {
611 size_t depends_flags = other.ComputeDependsFlags();
612 return (flags_ & depends_flags) != 0;
613 }
614
615 bool HasDependencies() const {
616 int count = kFlagDependsOnCount - kFlagChangesCount;
617 size_t all_bits_set = (1 << count) - 1;
618 return ((flags_ >> kFlagChangesCount) & all_bits_set) != 0;
619 }
620
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100621 private:
622 static constexpr int kFlagChangesSomething = 0;
623 static constexpr int kFlagChangesCount = kFlagChangesSomething + 1;
624
625 static constexpr int kFlagDependsOnSomething = kFlagChangesCount;
626 static constexpr int kFlagDependsOnCount = kFlagDependsOnSomething + 1;
627
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100628 explicit SideEffects(size_t flags) : flags_(flags) {}
629
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100630 size_t ComputeDependsFlags() const {
631 return flags_ << kFlagChangesCount;
632 }
633
634 size_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100635};
636
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700637class HInstruction : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000638 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100639 explicit HInstruction(SideEffects side_effects)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000640 : previous_(nullptr),
641 next_(nullptr),
642 block_(nullptr),
643 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100644 ssa_index_(-1),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000645 uses_(nullptr),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100646 env_uses_(nullptr),
647 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100648 locations_(nullptr),
649 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100650 lifetime_position_(kNoLifetime),
651 side_effects_(side_effects) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000652
Dave Allison20dfc792014-06-16 20:44:29 -0700653 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000654
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100655#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100656 enum InstructionKind {
657 FOR_EACH_INSTRUCTION(DECLARE_KIND)
658 };
659#undef DECLARE_KIND
660
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000661 HInstruction* GetNext() const { return next_; }
662 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000663
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000664 HBasicBlock* GetBlock() const { return block_; }
665 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100666 bool IsInBlock() const { return block_ != nullptr; }
667 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +0100668 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000669
Roland Levillain6b879dd2014-09-22 17:13:44 +0100670 virtual size_t InputCount() const = 0;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100671 virtual HInstruction* InputAt(size_t i) const = 0;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000672
673 virtual void Accept(HGraphVisitor* visitor) = 0;
674 virtual const char* DebugName() const = 0;
675
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100676 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100677 virtual void SetRawInputAt(size_t index, HInstruction* input) = 0;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100678
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100679 virtual bool NeedsEnvironment() const { return false; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100680 virtual bool IsControlFlow() const { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +0100681 virtual bool CanThrow() const { return false; }
Roland Levillain72bceff2014-09-15 18:29:00 +0100682 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100683
684 void AddUseAt(HInstruction* user, size_t index) {
685 uses_ = new (block_->GetGraph()->GetArena()) HUseListNode<HInstruction>(user, index, uses_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000686 }
687
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100688 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +0100689 DCHECK(user != nullptr);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100690 env_uses_ = new (block_->GetGraph()->GetArena()) HUseListNode<HEnvironment>(
691 user, index, env_uses_);
692 }
693
694 void RemoveUser(HInstruction* user, size_t index);
Nicolas Geoffray724c9632014-09-22 12:27:27 +0100695 void RemoveEnvironmentUser(HEnvironment* user, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100696
697 HUseListNode<HInstruction>* GetUses() const { return uses_; }
698 HUseListNode<HEnvironment>* GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000699
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100700 bool HasUses() const { return uses_ != nullptr || env_uses_ != nullptr; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100701 bool HasEnvironmentUses() const { return env_uses_ != nullptr; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000702
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100703 size_t NumberOfUses() const {
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100704 // TODO: Optimize this method if it is used outside of the HGraphVisualizer.
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100705 size_t result = 0;
706 HUseListNode<HInstruction>* current = uses_;
707 while (current != nullptr) {
708 current = current->GetTail();
709 ++result;
710 }
711 return result;
712 }
713
Roland Levillain6c82d402014-10-13 16:10:27 +0100714 // Does this instruction strictly dominate `other_instruction`?
715 // Returns false if this instruction and `other_instruction` are the same.
716 // Aborts if this instruction and `other_instruction` are both phis.
717 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +0100718
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000719 int GetId() const { return id_; }
720 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000721
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100722 int GetSsaIndex() const { return ssa_index_; }
723 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
724 bool HasSsaIndex() const { return ssa_index_ != -1; }
725
726 bool HasEnvironment() const { return environment_ != nullptr; }
727 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100728 void SetEnvironment(HEnvironment* environment) { environment_ = environment; }
729
Nicolas Geoffray39468442014-09-02 15:17:15 +0100730 // Returns the number of entries in the environment. Typically, that is the
731 // number of dex registers in a method. It could be more in case of inlining.
732 size_t EnvironmentSize() const;
733
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000734 LocationSummary* GetLocations() const { return locations_; }
735 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000736
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100737 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100738 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100739
Dave Allison20dfc792014-06-16 20:44:29 -0700740 bool HasOnlyOneUse() const {
741 return uses_ != nullptr && uses_->GetTail() == nullptr;
742 }
743
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100744#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +0100745 bool Is##type() const { return (As##type() != nullptr); } \
746 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000747 virtual H##type* As##type() { return nullptr; }
748
749 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
750#undef INSTRUCTION_TYPE_CHECK
751
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100752 // Returns whether the instruction can be moved within the graph.
753 virtual bool CanBeMoved() const { return false; }
754
755 // Returns whether the two instructions are of the same kind.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700756 virtual bool InstructionTypeEquals(HInstruction* other) const {
757 UNUSED(other);
758 return false;
759 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100760
761 // Returns whether any data encoded in the two instructions is equal.
762 // This method does not look at the inputs. Both instructions must be
763 // of the same type, otherwise the method has undefined behavior.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700764 virtual bool InstructionDataEquals(HInstruction* other) const {
765 UNUSED(other);
766 return false;
767 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100768
769 // Returns whether two instructions are equal, that is:
770 // 1) They have the same type and contain the same data,
771 // 2) Their inputs are identical.
772 bool Equals(HInstruction* other) const;
773
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100774 virtual InstructionKind GetKind() const = 0;
775
776 virtual size_t ComputeHashCode() const {
777 size_t result = GetKind();
778 for (size_t i = 0, e = InputCount(); i < e; ++i) {
779 result = (result * 31) + InputAt(i)->GetId();
780 }
781 return result;
782 }
783
784 SideEffects GetSideEffects() const { return side_effects_; }
785
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100786 size_t GetLifetimePosition() const { return lifetime_position_; }
787 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
788 LiveInterval* GetLiveInterval() const { return live_interval_; }
789 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
790 bool HasLiveInterval() const { return live_interval_ != nullptr; }
791
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000792 private:
793 HInstruction* previous_;
794 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000795 HBasicBlock* block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000796
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000797 // An instruction gets an id when it is added to the graph.
798 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +0100799 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000800 int id_;
801
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100802 // When doing liveness analysis, instructions that have uses get an SSA index.
803 int ssa_index_;
804
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100805 // List of instructions that have this instruction as input.
806 HUseListNode<HInstruction>* uses_;
807
808 // List of environments that contain this instruction.
809 HUseListNode<HEnvironment>* env_uses_;
810
Nicolas Geoffray39468442014-09-02 15:17:15 +0100811 // The environment associated with this instruction. Not null if the instruction
812 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100813 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000814
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000815 // Set by the code generator.
816 LocationSummary* locations_;
817
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100818 // Set by the liveness analysis.
819 LiveInterval* live_interval_;
820
821 // Set by the liveness analysis, this is the position in a linear
822 // order of blocks where this instruction's live interval start.
823 size_t lifetime_position_;
824
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100825 const SideEffects side_effects_;
826
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000827 friend class HBasicBlock;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100828 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000829
830 DISALLOW_COPY_AND_ASSIGN(HInstruction);
831};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700832std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000833
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100834template<typename T>
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000835class HUseIterator : public ValueObject {
836 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100837 explicit HUseIterator(HUseListNode<T>* uses) : current_(uses) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000838
839 bool Done() const { return current_ == nullptr; }
840
841 void Advance() {
842 DCHECK(!Done());
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000843 current_ = current_->GetTail();
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000844 }
845
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100846 HUseListNode<T>* Current() const {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000847 DCHECK(!Done());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100848 return current_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000849 }
850
851 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100852 HUseListNode<T>* current_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000853
854 friend class HValue;
855};
856
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100857// A HEnvironment object contains the values of virtual registers at a given location.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700858class HEnvironment : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100859 public:
860 HEnvironment(ArenaAllocator* arena, size_t number_of_vregs) : vregs_(arena, number_of_vregs) {
861 vregs_.SetSize(number_of_vregs);
862 for (size_t i = 0; i < number_of_vregs; i++) {
863 vregs_.Put(i, nullptr);
864 }
865 }
866
867 void Populate(const GrowableArray<HInstruction*>& env) {
868 for (size_t i = 0; i < env.Size(); i++) {
869 HInstruction* instruction = env.Get(i);
870 vregs_.Put(i, instruction);
871 if (instruction != nullptr) {
872 instruction->AddEnvUseAt(this, i);
873 }
874 }
875 }
876
877 void SetRawEnvAt(size_t index, HInstruction* instruction) {
878 vregs_.Put(index, instruction);
879 }
880
Nicolas Geoffray39468442014-09-02 15:17:15 +0100881 HInstruction* GetInstructionAt(size_t index) const {
882 return vregs_.Get(index);
883 }
884
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100885 GrowableArray<HInstruction*>* GetVRegs() {
886 return &vregs_;
887 }
888
Nicolas Geoffray39468442014-09-02 15:17:15 +0100889 size_t Size() const { return vregs_.Size(); }
890
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100891 private:
892 GrowableArray<HInstruction*> vregs_;
893
894 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
895};
896
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000897class HInputIterator : public ValueObject {
898 public:
Dave Allison20dfc792014-06-16 20:44:29 -0700899 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000900
901 bool Done() const { return index_ == instruction_->InputCount(); }
902 HInstruction* Current() const { return instruction_->InputAt(index_); }
903 void Advance() { index_++; }
904
905 private:
906 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100907 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000908
909 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
910};
911
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000912class HInstructionIterator : public ValueObject {
913 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100914 explicit HInstructionIterator(const HInstructionList& instructions)
915 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000916 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000917 }
918
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000919 bool Done() const { return instruction_ == nullptr; }
920 HInstruction* Current() const { return instruction_; }
921 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000922 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000923 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000924 }
925
926 private:
927 HInstruction* instruction_;
928 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100929
930 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000931};
932
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100933class HBackwardInstructionIterator : public ValueObject {
934 public:
935 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
936 : instruction_(instructions.last_instruction_) {
937 next_ = Done() ? nullptr : instruction_->GetPrevious();
938 }
939
940 bool Done() const { return instruction_ == nullptr; }
941 HInstruction* Current() const { return instruction_; }
942 void Advance() {
943 instruction_ = next_;
944 next_ = Done() ? nullptr : instruction_->GetPrevious();
945 }
946
947 private:
948 HInstruction* instruction_;
949 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100950
951 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100952};
953
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000954// An embedded container with N elements of type T. Used (with partial
955// specialization for N=0) because embedded arrays cannot have size 0.
956template<typename T, intptr_t N>
957class EmbeddedArray {
958 public:
Dave Allison20dfc792014-06-16 20:44:29 -0700959 EmbeddedArray() : elements_() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000960
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000961 intptr_t GetLength() const { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000962
963 const T& operator[](intptr_t i) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000964 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000965 return elements_[i];
966 }
967
968 T& operator[](intptr_t i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000969 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000970 return elements_[i];
971 }
972
973 const T& At(intptr_t i) const {
974 return (*this)[i];
975 }
976
977 void SetAt(intptr_t i, const T& val) {
978 (*this)[i] = val;
979 }
980
981 private:
982 T elements_[N];
983};
984
985template<typename T>
986class EmbeddedArray<T, 0> {
987 public:
988 intptr_t length() const { return 0; }
989 const T& operator[](intptr_t i) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700990 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000991 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700992 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000993 }
994 T& operator[](intptr_t i) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700995 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000996 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700997 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000998 }
999};
1000
1001template<intptr_t N>
1002class HTemplateInstruction: public HInstruction {
1003 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001004 HTemplateInstruction<N>(SideEffects side_effects)
1005 : HInstruction(side_effects), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07001006 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001007
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001008 virtual size_t InputCount() const { return N; }
1009 virtual HInstruction* InputAt(size_t i) const { return inputs_[i]; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001010
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001011 protected:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001012 virtual void SetRawInputAt(size_t i, HInstruction* instruction) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001013 inputs_[i] = instruction;
1014 }
1015
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001016 private:
1017 EmbeddedArray<HInstruction*, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001018
1019 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001020};
1021
Dave Allison20dfc792014-06-16 20:44:29 -07001022template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001023class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07001024 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001025 HExpression<N>(Primitive::Type type, SideEffects side_effects)
1026 : HTemplateInstruction<N>(side_effects), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07001027 virtual ~HExpression() {}
1028
1029 virtual Primitive::Type GetType() const { return type_; }
1030
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001031 protected:
1032 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07001033};
1034
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001035// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
1036// instruction that branches to the exit block.
1037class HReturnVoid : public HTemplateInstruction<0> {
1038 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001039 HReturnVoid() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001040
1041 virtual bool IsControlFlow() const { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001042
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001043 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001044
1045 private:
1046 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
1047};
1048
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001049// Represents dex's RETURN opcodes. A HReturn is a control flow
1050// instruction that branches to the exit block.
1051class HReturn : public HTemplateInstruction<1> {
1052 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001053 explicit HReturn(HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001054 SetRawInputAt(0, value);
1055 }
1056
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001057 virtual bool IsControlFlow() const { return true; }
1058
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001059 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001060
1061 private:
1062 DISALLOW_COPY_AND_ASSIGN(HReturn);
1063};
1064
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001065// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001066// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001067// exit block.
1068class HExit : public HTemplateInstruction<0> {
1069 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001070 HExit() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001071
1072 virtual bool IsControlFlow() const { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001073
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001074 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001075
1076 private:
1077 DISALLOW_COPY_AND_ASSIGN(HExit);
1078};
1079
1080// Jumps from one block to another.
1081class HGoto : public HTemplateInstruction<0> {
1082 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001083 HGoto() : HTemplateInstruction(SideEffects::None()) {}
1084
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001085 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001086
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001087 HBasicBlock* GetSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001088 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001089 }
1090
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001091 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001092
1093 private:
1094 DISALLOW_COPY_AND_ASSIGN(HGoto);
1095};
1096
Dave Allison20dfc792014-06-16 20:44:29 -07001097
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001098// Conditional branch. A block ending with an HIf instruction must have
1099// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001100class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001101 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001102 explicit HIf(HInstruction* input) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001103 SetRawInputAt(0, input);
1104 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001105
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001106 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001107
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001108 HBasicBlock* IfTrueSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001109 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001110 }
1111
1112 HBasicBlock* IfFalseSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001113 return GetBlock()->GetSuccessors().Get(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001114 }
1115
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001116 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001117
Dave Allison20dfc792014-06-16 20:44:29 -07001118 virtual bool IsIfInstruction() const { return true; }
1119
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001120 private:
1121 DISALLOW_COPY_AND_ASSIGN(HIf);
1122};
1123
Roland Levillain88cb1752014-10-20 16:36:47 +01001124class HUnaryOperation : public HExpression<1> {
1125 public:
1126 HUnaryOperation(Primitive::Type result_type, HInstruction* input)
1127 : HExpression(result_type, SideEffects::None()) {
1128 SetRawInputAt(0, input);
1129 }
1130
1131 HInstruction* GetInput() const { return InputAt(0); }
1132 Primitive::Type GetResultType() const { return GetType(); }
1133
1134 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001135 virtual bool InstructionDataEquals(HInstruction* other) const {
1136 UNUSED(other);
1137 return true;
1138 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001139
Roland Levillain9240d6a2014-10-20 16:47:04 +01001140 // Try to statically evaluate `operation` and return a HConstant
1141 // containing the result of this evaluation. If `operation` cannot
1142 // be evaluated as a constant, return nullptr.
1143 HConstant* TryStaticEvaluation() const;
1144
1145 // Apply this operation to `x`.
1146 virtual int32_t Evaluate(int32_t x) const = 0;
1147 virtual int64_t Evaluate(int64_t x) const = 0;
1148
Roland Levillain88cb1752014-10-20 16:36:47 +01001149 DECLARE_INSTRUCTION(UnaryOperation);
1150
1151 private:
1152 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
1153};
1154
Dave Allison20dfc792014-06-16 20:44:29 -07001155class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001156 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001157 HBinaryOperation(Primitive::Type result_type,
1158 HInstruction* left,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001159 HInstruction* right) : HExpression(result_type, SideEffects::None()) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001160 SetRawInputAt(0, left);
1161 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001162 }
1163
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001164 HInstruction* GetLeft() const { return InputAt(0); }
1165 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07001166 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001167
1168 virtual bool IsCommutative() { return false; }
1169
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001170 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001171 virtual bool InstructionDataEquals(HInstruction* other) const {
1172 UNUSED(other);
1173 return true;
1174 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001175
Roland Levillain9240d6a2014-10-20 16:47:04 +01001176 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01001177 // containing the result of this evaluation. If `operation` cannot
1178 // be evaluated as a constant, return nullptr.
Roland Levillain9240d6a2014-10-20 16:47:04 +01001179 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01001180
1181 // Apply this operation to `x` and `y`.
1182 virtual int32_t Evaluate(int32_t x, int32_t y) const = 0;
1183 virtual int64_t Evaluate(int64_t x, int64_t y) const = 0;
1184
Roland Levillainccc07a92014-09-16 14:48:16 +01001185 DECLARE_INSTRUCTION(BinaryOperation);
1186
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001187 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001188 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
1189};
1190
Dave Allison20dfc792014-06-16 20:44:29 -07001191class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001192 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001193 HCondition(HInstruction* first, HInstruction* second)
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001194 : HBinaryOperation(Primitive::kPrimBoolean, first, second),
1195 needs_materialization_(true) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001196
1197 virtual bool IsCommutative() { return true; }
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001198
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001199 bool NeedsMaterialization() const { return needs_materialization_; }
1200 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001201
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001202 // For code generation purposes, returns whether this instruction is just before
1203 // `if_`, and disregard moves in between.
1204 bool IsBeforeWhenDisregardMoves(HIf* if_) const;
1205
Dave Allison20dfc792014-06-16 20:44:29 -07001206 DECLARE_INSTRUCTION(Condition);
1207
1208 virtual IfCondition GetCondition() const = 0;
1209
1210 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001211 // For register allocation purposes, returns whether this instruction needs to be
1212 // materialized (that is, not just be in the processor flags).
1213 bool needs_materialization_;
1214
Dave Allison20dfc792014-06-16 20:44:29 -07001215 DISALLOW_COPY_AND_ASSIGN(HCondition);
1216};
1217
1218// Instruction to check if two inputs are equal to each other.
1219class HEqual : public HCondition {
1220 public:
1221 HEqual(HInstruction* first, HInstruction* second)
1222 : HCondition(first, second) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001223
Roland Levillain93445682014-10-06 19:24:02 +01001224 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1225 return x == y ? 1 : 0;
1226 }
1227 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1228 return x == y ? 1 : 0;
1229 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001230
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001231 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001232
Dave Allison20dfc792014-06-16 20:44:29 -07001233 virtual IfCondition GetCondition() const {
1234 return kCondEQ;
1235 }
1236
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001237 private:
1238 DISALLOW_COPY_AND_ASSIGN(HEqual);
1239};
1240
Dave Allison20dfc792014-06-16 20:44:29 -07001241class HNotEqual : public HCondition {
1242 public:
1243 HNotEqual(HInstruction* first, HInstruction* second)
1244 : HCondition(first, second) {}
1245
Roland Levillain93445682014-10-06 19:24:02 +01001246 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1247 return x != y ? 1 : 0;
1248 }
1249 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1250 return x != y ? 1 : 0;
1251 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001252
Dave Allison20dfc792014-06-16 20:44:29 -07001253 DECLARE_INSTRUCTION(NotEqual);
1254
1255 virtual IfCondition GetCondition() const {
1256 return kCondNE;
1257 }
1258
1259 private:
1260 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
1261};
1262
1263class HLessThan : public HCondition {
1264 public:
1265 HLessThan(HInstruction* first, HInstruction* second)
1266 : HCondition(first, second) {}
1267
Roland Levillain93445682014-10-06 19:24:02 +01001268 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1269 return x < y ? 1 : 0;
1270 }
1271 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1272 return x < y ? 1 : 0;
1273 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001274
Dave Allison20dfc792014-06-16 20:44:29 -07001275 DECLARE_INSTRUCTION(LessThan);
1276
1277 virtual IfCondition GetCondition() const {
1278 return kCondLT;
1279 }
1280
1281 private:
1282 DISALLOW_COPY_AND_ASSIGN(HLessThan);
1283};
1284
1285class HLessThanOrEqual : public HCondition {
1286 public:
1287 HLessThanOrEqual(HInstruction* first, HInstruction* second)
1288 : HCondition(first, second) {}
1289
Roland Levillain93445682014-10-06 19:24:02 +01001290 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1291 return x <= y ? 1 : 0;
1292 }
1293 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1294 return x <= y ? 1 : 0;
1295 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001296
Dave Allison20dfc792014-06-16 20:44:29 -07001297 DECLARE_INSTRUCTION(LessThanOrEqual);
1298
1299 virtual IfCondition GetCondition() const {
1300 return kCondLE;
1301 }
1302
1303 private:
1304 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
1305};
1306
1307class HGreaterThan : public HCondition {
1308 public:
1309 HGreaterThan(HInstruction* first, HInstruction* second)
1310 : HCondition(first, second) {}
1311
Roland Levillain93445682014-10-06 19:24:02 +01001312 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1313 return x > y ? 1 : 0;
1314 }
1315 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1316 return x > y ? 1 : 0;
1317 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001318
Dave Allison20dfc792014-06-16 20:44:29 -07001319 DECLARE_INSTRUCTION(GreaterThan);
1320
1321 virtual IfCondition GetCondition() const {
1322 return kCondGT;
1323 }
1324
1325 private:
1326 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
1327};
1328
1329class HGreaterThanOrEqual : public HCondition {
1330 public:
1331 HGreaterThanOrEqual(HInstruction* first, HInstruction* second)
1332 : HCondition(first, second) {}
1333
Roland Levillain93445682014-10-06 19:24:02 +01001334 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1335 return x >= y ? 1 : 0;
1336 }
1337 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1338 return x >= y ? 1 : 0;
1339 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001340
Dave Allison20dfc792014-06-16 20:44:29 -07001341 DECLARE_INSTRUCTION(GreaterThanOrEqual);
1342
1343 virtual IfCondition GetCondition() const {
1344 return kCondGE;
1345 }
1346
1347 private:
1348 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
1349};
1350
1351
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001352// Instruction to check how two inputs compare to each other.
1353// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
1354class HCompare : public HBinaryOperation {
1355 public:
1356 HCompare(Primitive::Type type, HInstruction* first, HInstruction* second)
1357 : HBinaryOperation(Primitive::kPrimInt, first, second) {
1358 DCHECK_EQ(type, first->GetType());
1359 DCHECK_EQ(type, second->GetType());
1360 }
1361
Roland Levillain93445682014-10-06 19:24:02 +01001362 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001363 return
1364 x == y ? 0 :
1365 x > y ? 1 :
1366 -1;
1367 }
Roland Levillain93445682014-10-06 19:24:02 +01001368 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001369 return
1370 x == y ? 0 :
1371 x > y ? 1 :
1372 -1;
1373 }
1374
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001375 DECLARE_INSTRUCTION(Compare);
1376
1377 private:
1378 DISALLOW_COPY_AND_ASSIGN(HCompare);
1379};
1380
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001381// A local in the graph. Corresponds to a Dex register.
1382class HLocal : public HTemplateInstruction<0> {
1383 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001384 explicit HLocal(uint16_t reg_number)
1385 : HTemplateInstruction(SideEffects::None()), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001386
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001387 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001388
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001389 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001390
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001391 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001392 // The Dex register number.
1393 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001394
1395 DISALLOW_COPY_AND_ASSIGN(HLocal);
1396};
1397
1398// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07001399class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001400 public:
Roland Levillain5799fc02014-09-25 12:15:20 +01001401 HLoadLocal(HLocal* local, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001402 : HExpression(type, SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001403 SetRawInputAt(0, local);
1404 }
1405
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001406 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1407
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001408 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001409
1410 private:
1411 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
1412};
1413
1414// Store a value in a given local. This instruction has two inputs: the value
1415// and the local.
1416class HStoreLocal : public HTemplateInstruction<2> {
1417 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001418 HStoreLocal(HLocal* local, HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001419 SetRawInputAt(0, local);
1420 SetRawInputAt(1, value);
1421 }
1422
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001423 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1424
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001425 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001426
1427 private:
1428 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
1429};
1430
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001431class HConstant : public HExpression<0> {
1432 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001433 explicit HConstant(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
1434
1435 virtual bool CanBeMoved() const { return true; }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001436
1437 DECLARE_INSTRUCTION(Constant);
1438
1439 private:
1440 DISALLOW_COPY_AND_ASSIGN(HConstant);
1441};
1442
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001443class HFloatConstant : public HConstant {
1444 public:
1445 explicit HFloatConstant(float value) : HConstant(Primitive::kPrimFloat), value_(value) {}
1446
1447 float GetValue() const { return value_; }
1448
1449 virtual bool InstructionDataEquals(HInstruction* other) const {
1450 return bit_cast<float, int32_t>(other->AsFloatConstant()->value_) ==
1451 bit_cast<float, int32_t>(value_);
1452 }
1453
1454 virtual size_t ComputeHashCode() const { return static_cast<size_t>(GetValue()); }
1455
1456 DECLARE_INSTRUCTION(FloatConstant);
1457
1458 private:
1459 const float value_;
1460
1461 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
1462};
1463
1464class HDoubleConstant : public HConstant {
1465 public:
1466 explicit HDoubleConstant(double value) : HConstant(Primitive::kPrimDouble), value_(value) {}
1467
1468 double GetValue() const { return value_; }
1469
1470 virtual bool InstructionDataEquals(HInstruction* other) const {
1471 return bit_cast<double, int64_t>(other->AsDoubleConstant()->value_) ==
1472 bit_cast<double, int64_t>(value_);
1473 }
1474
1475 virtual size_t ComputeHashCode() const { return static_cast<size_t>(GetValue()); }
1476
1477 DECLARE_INSTRUCTION(DoubleConstant);
1478
1479 private:
1480 const double value_;
1481
1482 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
1483};
1484
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001485// Constants of the type int. Those can be from Dex instructions, or
1486// synthesized (for example with the if-eqz instruction).
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001487class HIntConstant : public HConstant {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001488 public:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001489 explicit HIntConstant(int32_t value) : HConstant(Primitive::kPrimInt), value_(value) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001490
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001491 int32_t GetValue() const { return value_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001492
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001493 virtual bool InstructionDataEquals(HInstruction* other) const {
1494 return other->AsIntConstant()->value_ == value_;
1495 }
1496
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001497 virtual size_t ComputeHashCode() const { return GetValue(); }
1498
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001499 DECLARE_INSTRUCTION(IntConstant);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001500
1501 private:
1502 const int32_t value_;
1503
1504 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
1505};
1506
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001507class HLongConstant : public HConstant {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001508 public:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001509 explicit HLongConstant(int64_t value) : HConstant(Primitive::kPrimLong), value_(value) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001510
1511 int64_t GetValue() const { return value_; }
1512
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001513 virtual bool InstructionDataEquals(HInstruction* other) const {
1514 return other->AsLongConstant()->value_ == value_;
1515 }
1516
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001517 virtual size_t ComputeHashCode() const { return static_cast<size_t>(GetValue()); }
1518
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001519 DECLARE_INSTRUCTION(LongConstant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001520
1521 private:
1522 const int64_t value_;
1523
1524 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
1525};
1526
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001527class HInvoke : public HInstruction {
1528 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001529 HInvoke(ArenaAllocator* arena,
1530 uint32_t number_of_arguments,
1531 Primitive::Type return_type,
1532 uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001533 : HInstruction(SideEffects::All()),
1534 inputs_(arena, number_of_arguments),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001535 return_type_(return_type),
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001536 dex_pc_(dex_pc) {
1537 inputs_.SetSize(number_of_arguments);
1538 }
1539
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001540 virtual size_t InputCount() const { return inputs_.Size(); }
1541 virtual HInstruction* InputAt(size_t i) const { return inputs_.Get(i); }
1542
1543 // Runtime needs to walk the stack, so Dex -> Dex calls need to
1544 // know their environment.
1545 virtual bool NeedsEnvironment() const { return true; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001546
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001547 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001548 SetRawInputAt(index, argument);
1549 }
1550
1551 virtual void SetRawInputAt(size_t index, HInstruction* input) {
1552 inputs_.Put(index, input);
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001553 }
1554
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001555 virtual Primitive::Type GetType() const { return return_type_; }
1556
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001557 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001558
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001559 DECLARE_INSTRUCTION(Invoke);
1560
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001561 protected:
1562 GrowableArray<HInstruction*> inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001563 const Primitive::Type return_type_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001564 const uint32_t dex_pc_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001565
1566 private:
1567 DISALLOW_COPY_AND_ASSIGN(HInvoke);
1568};
1569
1570class HInvokeStatic : public HInvoke {
1571 public:
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001572 HInvokeStatic(ArenaAllocator* arena,
1573 uint32_t number_of_arguments,
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001574 Primitive::Type return_type,
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001575 uint32_t dex_pc,
1576 uint32_t index_in_dex_cache)
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001577 : HInvoke(arena, number_of_arguments, return_type, dex_pc),
1578 index_in_dex_cache_(index_in_dex_cache) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001579
1580 uint32_t GetIndexInDexCache() const { return index_in_dex_cache_; }
1581
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001582 DECLARE_INSTRUCTION(InvokeStatic);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001583
1584 private:
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001585 const uint32_t index_in_dex_cache_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001586
1587 DISALLOW_COPY_AND_ASSIGN(HInvokeStatic);
1588};
1589
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001590class HInvokeVirtual : public HInvoke {
1591 public:
1592 HInvokeVirtual(ArenaAllocator* arena,
1593 uint32_t number_of_arguments,
1594 Primitive::Type return_type,
1595 uint32_t dex_pc,
1596 uint32_t vtable_index)
1597 : HInvoke(arena, number_of_arguments, return_type, dex_pc),
1598 vtable_index_(vtable_index) {}
1599
1600 uint32_t GetVTableIndex() const { return vtable_index_; }
1601
1602 DECLARE_INSTRUCTION(InvokeVirtual);
1603
1604 private:
1605 const uint32_t vtable_index_;
1606
1607 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
1608};
1609
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001610class HInvokeInterface : public HInvoke {
1611 public:
1612 HInvokeInterface(ArenaAllocator* arena,
1613 uint32_t number_of_arguments,
1614 Primitive::Type return_type,
1615 uint32_t dex_pc,
1616 uint32_t dex_method_index,
1617 uint32_t imt_index)
1618 : HInvoke(arena, number_of_arguments, return_type, dex_pc),
1619 dex_method_index_(dex_method_index),
1620 imt_index_(imt_index) {}
1621
1622 uint32_t GetImtIndex() const { return imt_index_; }
1623 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
1624
1625 DECLARE_INSTRUCTION(InvokeInterface);
1626
1627 private:
1628 const uint32_t dex_method_index_;
1629 const uint32_t imt_index_;
1630
1631 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
1632};
1633
Dave Allison20dfc792014-06-16 20:44:29 -07001634class HNewInstance : public HExpression<0> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001635 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001636 HNewInstance(uint32_t dex_pc, uint16_t type_index)
1637 : HExpression(Primitive::kPrimNot, SideEffects::None()),
1638 dex_pc_(dex_pc),
1639 type_index_(type_index) {}
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001640
1641 uint32_t GetDexPc() const { return dex_pc_; }
1642 uint16_t GetTypeIndex() const { return type_index_; }
1643
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001644 // Calls runtime so needs an environment.
1645 virtual bool NeedsEnvironment() const { return true; }
1646
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001647 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001648
1649 private:
1650 const uint32_t dex_pc_;
1651 const uint16_t type_index_;
1652
1653 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
1654};
1655
Roland Levillain88cb1752014-10-20 16:36:47 +01001656class HNeg : public HUnaryOperation {
1657 public:
1658 explicit HNeg(Primitive::Type result_type, HInstruction* input)
1659 : HUnaryOperation(result_type, input) {}
1660
Roland Levillain9240d6a2014-10-20 16:47:04 +01001661 virtual int32_t Evaluate(int32_t x) const OVERRIDE { return -x; }
1662 virtual int64_t Evaluate(int64_t x) const OVERRIDE { return -x; }
1663
Roland Levillain88cb1752014-10-20 16:36:47 +01001664 DECLARE_INSTRUCTION(Neg);
1665
1666 private:
1667 DISALLOW_COPY_AND_ASSIGN(HNeg);
1668};
1669
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001670class HNewArray : public HExpression<1> {
1671 public:
1672 HNewArray(HInstruction* length, uint32_t dex_pc, uint16_t type_index)
1673 : HExpression(Primitive::kPrimNot, SideEffects::None()),
1674 dex_pc_(dex_pc),
1675 type_index_(type_index) {
1676 SetRawInputAt(0, length);
1677 }
1678
1679 uint32_t GetDexPc() const { return dex_pc_; }
1680 uint16_t GetTypeIndex() const { return type_index_; }
1681
1682 // Calls runtime so needs an environment.
1683 virtual bool NeedsEnvironment() const { return true; }
1684
1685 DECLARE_INSTRUCTION(NewArray);
1686
1687 private:
1688 const uint32_t dex_pc_;
1689 const uint16_t type_index_;
1690
1691 DISALLOW_COPY_AND_ASSIGN(HNewArray);
1692};
1693
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001694class HAdd : public HBinaryOperation {
1695 public:
1696 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1697 : HBinaryOperation(result_type, left, right) {}
1698
1699 virtual bool IsCommutative() { return true; }
1700
Roland Levillain93445682014-10-06 19:24:02 +01001701 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1702 return x + y;
1703 }
1704 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1705 return x + y;
1706 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001707
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001708 DECLARE_INSTRUCTION(Add);
1709
1710 private:
1711 DISALLOW_COPY_AND_ASSIGN(HAdd);
1712};
1713
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001714class HSub : public HBinaryOperation {
1715 public:
1716 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1717 : HBinaryOperation(result_type, left, right) {}
1718
Roland Levillain93445682014-10-06 19:24:02 +01001719 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1720 return x - y;
1721 }
1722 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1723 return x - y;
1724 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001725
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001726 DECLARE_INSTRUCTION(Sub);
1727
1728 private:
1729 DISALLOW_COPY_AND_ASSIGN(HSub);
1730};
1731
Calin Juravle34bacdf2014-10-07 20:23:36 +01001732class HMul : public HBinaryOperation {
1733 public:
1734 HMul(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1735 : HBinaryOperation(result_type, left, right) {}
1736
1737 virtual bool IsCommutative() { return true; }
1738
1739 virtual int32_t Evaluate(int32_t x, int32_t y) const { return x * y; }
1740 virtual int64_t Evaluate(int64_t x, int64_t y) const { return x * y; }
1741
1742 DECLARE_INSTRUCTION(Mul);
1743
1744 private:
1745 DISALLOW_COPY_AND_ASSIGN(HMul);
1746};
1747
Calin Juravle7c4954d2014-10-28 16:57:40 +00001748class HDiv : public HBinaryOperation {
1749 public:
1750 HDiv(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1751 : HBinaryOperation(result_type, left, right) {}
1752
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00001753 virtual int32_t Evaluate(int32_t x, int32_t y) const {
1754 // Our graph structure ensures we never have 0 for `y` during constant folding.
1755 DCHECK_NE(y, 0);
1756 // Special case -1 to avoid getting a SIGFPE on x86.
1757 return (y == -1) ? -x : x / y;
1758 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00001759 virtual int64_t Evaluate(int64_t x, int64_t y) const { return x / y; }
1760
1761 DECLARE_INSTRUCTION(Div);
1762
1763 private:
1764 DISALLOW_COPY_AND_ASSIGN(HDiv);
1765};
1766
Calin Juravled0d48522014-11-04 16:40:20 +00001767class HDivZeroCheck : public HExpression<1> {
1768 public:
1769 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
1770 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
1771 SetRawInputAt(0, value);
1772 }
1773
1774 bool CanBeMoved() const OVERRIDE { return true; }
1775
1776 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
1777 UNUSED(other);
1778 return true;
1779 }
1780
1781 bool NeedsEnvironment() const OVERRIDE { return true; }
1782 bool CanThrow() const OVERRIDE { return true; }
1783
1784 uint32_t GetDexPc() const { return dex_pc_; }
1785
1786 DECLARE_INSTRUCTION(DivZeroCheck);
1787
1788 private:
1789 const uint32_t dex_pc_;
1790
1791 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
1792};
1793
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001794// The value of a parameter in this method. Its location depends on
1795// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07001796class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001797 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001798 HParameterValue(uint8_t index, Primitive::Type parameter_type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001799 : HExpression(parameter_type, SideEffects::None()), index_(index) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001800
1801 uint8_t GetIndex() const { return index_; }
1802
1803 DECLARE_INSTRUCTION(ParameterValue);
1804
1805 private:
1806 // The index of this parameter in the parameters list. Must be less
1807 // than HGraph::number_of_in_vregs_;
1808 const uint8_t index_;
1809
1810 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
1811};
1812
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001813class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001814 public:
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001815 explicit HNot(Primitive::Type result_type, HInstruction* input)
1816 : HUnaryOperation(result_type, input) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001817
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001818 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001819 virtual bool InstructionDataEquals(HInstruction* other) const {
1820 UNUSED(other);
1821 return true;
1822 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001823
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001824 virtual int32_t Evaluate(int32_t x) const OVERRIDE { return ~x; }
1825 virtual int64_t Evaluate(int64_t x) const OVERRIDE { return ~x; }
1826
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001827 DECLARE_INSTRUCTION(Not);
1828
1829 private:
1830 DISALLOW_COPY_AND_ASSIGN(HNot);
1831};
1832
Roland Levillaindff1f282014-11-05 14:15:05 +00001833class HTypeConversion : public HExpression<1> {
1834 public:
1835 // Instantiate a type conversion of `input` to `result_type`.
1836 HTypeConversion(Primitive::Type result_type, HInstruction* input)
1837 : HExpression(result_type, SideEffects::None()) {
1838 SetRawInputAt(0, input);
1839 DCHECK_NE(input->GetType(), result_type);
1840 }
1841
1842 HInstruction* GetInput() const { return InputAt(0); }
1843 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
1844 Primitive::Type GetResultType() const { return GetType(); }
1845
1846 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00001847 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00001848
1849 DECLARE_INSTRUCTION(TypeConversion);
1850
1851 private:
1852 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
1853};
1854
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001855class HPhi : public HInstruction {
1856 public:
1857 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001858 : HInstruction(SideEffects::None()),
1859 inputs_(arena, number_of_inputs),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001860 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001861 type_(type),
1862 is_live_(false) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001863 inputs_.SetSize(number_of_inputs);
1864 }
1865
1866 virtual size_t InputCount() const { return inputs_.Size(); }
1867 virtual HInstruction* InputAt(size_t i) const { return inputs_.Get(i); }
1868
1869 virtual void SetRawInputAt(size_t index, HInstruction* input) {
1870 inputs_.Put(index, input);
1871 }
1872
1873 void AddInput(HInstruction* input);
1874
1875 virtual Primitive::Type GetType() const { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001876 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001877
1878 uint32_t GetRegNumber() const { return reg_number_; }
1879
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001880 void SetDead() { is_live_ = false; }
1881 void SetLive() { is_live_ = true; }
1882 bool IsDead() const { return !is_live_; }
1883 bool IsLive() const { return is_live_; }
1884
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001885 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001886
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001887 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001888 GrowableArray<HInstruction*> inputs_;
1889 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001890 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001891 bool is_live_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001892
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001893 DISALLOW_COPY_AND_ASSIGN(HPhi);
1894};
1895
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001896class HNullCheck : public HExpression<1> {
1897 public:
1898 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001899 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001900 SetRawInputAt(0, value);
1901 }
1902
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001903 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001904 virtual bool InstructionDataEquals(HInstruction* other) const {
1905 UNUSED(other);
1906 return true;
1907 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001908
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001909 virtual bool NeedsEnvironment() const { return true; }
1910
Roland Levillaine161a2a2014-10-03 12:45:18 +01001911 virtual bool CanThrow() const { return true; }
1912
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001913 uint32_t GetDexPc() const { return dex_pc_; }
1914
1915 DECLARE_INSTRUCTION(NullCheck);
1916
1917 private:
1918 const uint32_t dex_pc_;
1919
1920 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
1921};
1922
1923class FieldInfo : public ValueObject {
1924 public:
Roland Levillain5799fc02014-09-25 12:15:20 +01001925 FieldInfo(MemberOffset field_offset, Primitive::Type field_type)
Nicolas Geoffray39468442014-09-02 15:17:15 +01001926 : field_offset_(field_offset), field_type_(field_type) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001927
1928 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001929 Primitive::Type GetFieldType() const { return field_type_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001930
1931 private:
1932 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01001933 const Primitive::Type field_type_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001934};
1935
1936class HInstanceFieldGet : public HExpression<1> {
1937 public:
1938 HInstanceFieldGet(HInstruction* value,
1939 Primitive::Type field_type,
1940 MemberOffset field_offset)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001941 : HExpression(field_type, SideEffects::DependsOnSomething()),
1942 field_info_(field_offset, field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001943 SetRawInputAt(0, value);
1944 }
1945
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001946 virtual bool CanBeMoved() const { return true; }
1947 virtual bool InstructionDataEquals(HInstruction* other) const {
1948 size_t other_offset = other->AsInstanceFieldGet()->GetFieldOffset().SizeValue();
1949 return other_offset == GetFieldOffset().SizeValue();
1950 }
1951
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001952 virtual size_t ComputeHashCode() const {
1953 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
1954 }
1955
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001956 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001957 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001958
1959 DECLARE_INSTRUCTION(InstanceFieldGet);
1960
1961 private:
1962 const FieldInfo field_info_;
1963
1964 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
1965};
1966
1967class HInstanceFieldSet : public HTemplateInstruction<2> {
1968 public:
1969 HInstanceFieldSet(HInstruction* object,
1970 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01001971 Primitive::Type field_type,
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001972 MemberOffset field_offset)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001973 : HTemplateInstruction(SideEffects::ChangesSomething()),
1974 field_info_(field_offset, field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001975 SetRawInputAt(0, object);
1976 SetRawInputAt(1, value);
1977 }
1978
1979 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001980 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001981
1982 DECLARE_INSTRUCTION(InstanceFieldSet);
1983
1984 private:
1985 const FieldInfo field_info_;
1986
1987 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
1988};
1989
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001990class HArrayGet : public HExpression<2> {
1991 public:
1992 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001993 : HExpression(type, SideEffects::DependsOnSomething()) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001994 SetRawInputAt(0, array);
1995 SetRawInputAt(1, index);
1996 }
1997
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001998 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001999 virtual bool InstructionDataEquals(HInstruction* other) const {
2000 UNUSED(other);
2001 return true;
2002 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002003 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002004
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002005 DECLARE_INSTRUCTION(ArrayGet);
2006
2007 private:
2008 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
2009};
2010
2011class HArraySet : public HTemplateInstruction<3> {
2012 public:
2013 HArraySet(HInstruction* array,
2014 HInstruction* index,
2015 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002016 Primitive::Type expected_component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002017 uint32_t dex_pc)
2018 : HTemplateInstruction(SideEffects::ChangesSomething()),
2019 dex_pc_(dex_pc),
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002020 expected_component_type_(expected_component_type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002021 SetRawInputAt(0, array);
2022 SetRawInputAt(1, index);
2023 SetRawInputAt(2, value);
2024 }
2025
2026 virtual bool NeedsEnvironment() const {
2027 // We currently always call a runtime method to catch array store
2028 // exceptions.
2029 return InputAt(2)->GetType() == Primitive::kPrimNot;
2030 }
2031
2032 uint32_t GetDexPc() const { return dex_pc_; }
2033
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002034 HInstruction* GetValue() const { return InputAt(2); }
2035
2036 Primitive::Type GetComponentType() const {
2037 // The Dex format does not type floating point index operations. Since the
2038 // `expected_component_type_` is set during building and can therefore not
2039 // be correct, we also check what is the value type. If it is a floating
2040 // point type, we must use that type.
2041 Primitive::Type value_type = GetValue()->GetType();
2042 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
2043 ? value_type
2044 : expected_component_type_;
2045 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002046
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002047 DECLARE_INSTRUCTION(ArraySet);
2048
2049 private:
2050 const uint32_t dex_pc_;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002051 const Primitive::Type expected_component_type_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002052
2053 DISALLOW_COPY_AND_ASSIGN(HArraySet);
2054};
2055
2056class HArrayLength : public HExpression<1> {
2057 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002058 explicit HArrayLength(HInstruction* array)
2059 : HExpression(Primitive::kPrimInt, SideEffects::None()) {
2060 // Note that arrays do not change length, so the instruction does not
2061 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002062 SetRawInputAt(0, array);
2063 }
2064
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002065 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002066 virtual bool InstructionDataEquals(HInstruction* other) const {
2067 UNUSED(other);
2068 return true;
2069 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002070
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002071 DECLARE_INSTRUCTION(ArrayLength);
2072
2073 private:
2074 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
2075};
2076
2077class HBoundsCheck : public HExpression<2> {
2078 public:
2079 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002080 : HExpression(index->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002081 DCHECK(index->GetType() == Primitive::kPrimInt);
2082 SetRawInputAt(0, index);
2083 SetRawInputAt(1, length);
2084 }
2085
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002086 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002087 virtual bool InstructionDataEquals(HInstruction* other) const {
2088 UNUSED(other);
2089 return true;
2090 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002091
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002092 virtual bool NeedsEnvironment() const { return true; }
2093
Roland Levillaine161a2a2014-10-03 12:45:18 +01002094 virtual bool CanThrow() const { return true; }
2095
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002096 uint32_t GetDexPc() const { return dex_pc_; }
2097
2098 DECLARE_INSTRUCTION(BoundsCheck);
2099
2100 private:
2101 const uint32_t dex_pc_;
2102
2103 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
2104};
2105
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002106/**
2107 * Some DEX instructions are folded into multiple HInstructions that need
2108 * to stay live until the last HInstruction. This class
2109 * is used as a marker for the baseline compiler to ensure its preceding
2110 * HInstruction stays live. `index` is the temporary number that is used
2111 * for knowing the stack offset where to store the instruction.
2112 */
2113class HTemporary : public HTemplateInstruction<0> {
2114 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002115 explicit HTemporary(size_t index) : HTemplateInstruction(SideEffects::None()), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002116
2117 size_t GetIndex() const { return index_; }
2118
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00002119 Primitive::Type GetType() const OVERRIDE {
2120 // The previous instruction is the one that will be stored in the temporary location.
2121 DCHECK(GetPrevious() != nullptr);
2122 return GetPrevious()->GetType();
2123 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00002124
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002125 DECLARE_INSTRUCTION(Temporary);
2126
2127 private:
2128 const size_t index_;
2129
2130 DISALLOW_COPY_AND_ASSIGN(HTemporary);
2131};
2132
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002133class HSuspendCheck : public HTemplateInstruction<0> {
2134 public:
2135 explicit HSuspendCheck(uint32_t dex_pc)
Nicolas Geoffray9ebc72c2014-09-25 16:33:42 +01002136 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002137
2138 virtual bool NeedsEnvironment() const {
2139 return true;
2140 }
2141
2142 uint32_t GetDexPc() const { return dex_pc_; }
2143
2144 DECLARE_INSTRUCTION(SuspendCheck);
2145
2146 private:
2147 const uint32_t dex_pc_;
2148
2149 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
2150};
2151
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002152/**
2153 * Instruction to load a Class object.
2154 */
2155class HLoadClass : public HExpression<0> {
2156 public:
2157 HLoadClass(uint16_t type_index,
2158 bool is_referrers_class,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002159 uint32_t dex_pc)
2160 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2161 type_index_(type_index),
2162 is_referrers_class_(is_referrers_class),
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002163 dex_pc_(dex_pc),
2164 generate_clinit_check_(false) {}
2165
2166 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002167
2168 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2169 return other->AsLoadClass()->type_index_ == type_index_;
2170 }
2171
2172 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
2173
2174 uint32_t GetDexPc() const { return dex_pc_; }
2175 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002176 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002177
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002178 bool NeedsEnvironment() const OVERRIDE {
2179 // Will call runtime and load the class if the class is not loaded yet.
2180 // TODO: finer grain decision.
2181 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002182 }
2183
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002184 bool MustGenerateClinitCheck() const {
2185 return generate_clinit_check_;
2186 }
2187
2188 void SetMustGenerateClinitCheck() {
2189 generate_clinit_check_ = true;
2190 }
2191
2192 bool CanCallRuntime() const {
2193 return MustGenerateClinitCheck() || !is_referrers_class_;
2194 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002195
2196 DECLARE_INSTRUCTION(LoadClass);
2197
2198 private:
2199 const uint16_t type_index_;
2200 const bool is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002201 const uint32_t dex_pc_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002202 // Whether this instruction must generate the initialization check.
2203 // Used for code generation.
2204 bool generate_clinit_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002205
2206 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
2207};
2208
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002209class HLoadString : public HExpression<0> {
2210 public:
2211 HLoadString(uint32_t string_index, uint32_t dex_pc)
2212 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2213 string_index_(string_index),
2214 dex_pc_(dex_pc) {}
2215
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002216 bool CanBeMoved() const OVERRIDE { return true; }
2217
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002218 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2219 return other->AsLoadString()->string_index_ == string_index_;
2220 }
2221
2222 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
2223
2224 uint32_t GetDexPc() const { return dex_pc_; }
2225 uint32_t GetStringIndex() const { return string_index_; }
2226
2227 // TODO: Can we deopt or debug when we resolve a string?
2228 bool NeedsEnvironment() const OVERRIDE { return false; }
2229
2230 DECLARE_INSTRUCTION(LoadString);
2231
2232 private:
2233 const uint32_t string_index_;
2234 const uint32_t dex_pc_;
2235
2236 DISALLOW_COPY_AND_ASSIGN(HLoadString);
2237};
2238
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002239// TODO: Pass this check to HInvokeStatic nodes.
2240/**
2241 * Performs an initialization check on its Class object input.
2242 */
2243class HClinitCheck : public HExpression<1> {
2244 public:
2245 explicit HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
2246 : HExpression(Primitive::kPrimNot, SideEffects::All()),
2247 dex_pc_(dex_pc) {
2248 SetRawInputAt(0, constant);
2249 }
2250
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002251 bool CanBeMoved() const OVERRIDE { return true; }
2252 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2253 UNUSED(other);
2254 return true;
2255 }
2256
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002257 bool NeedsEnvironment() const OVERRIDE {
2258 // May call runtime to initialize the class.
2259 return true;
2260 }
2261
2262 uint32_t GetDexPc() const { return dex_pc_; }
2263
2264 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
2265
2266 DECLARE_INSTRUCTION(ClinitCheck);
2267
2268 private:
2269 const uint32_t dex_pc_;
2270
2271 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
2272};
2273
2274class HStaticFieldGet : public HExpression<1> {
2275 public:
2276 HStaticFieldGet(HInstruction* cls,
2277 Primitive::Type field_type,
2278 MemberOffset field_offset)
2279 : HExpression(field_type, SideEffects::DependsOnSomething()),
2280 field_info_(field_offset, field_type) {
2281 SetRawInputAt(0, cls);
2282 }
2283
2284 bool CanBeMoved() const OVERRIDE { return true; }
2285 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2286 size_t other_offset = other->AsStaticFieldGet()->GetFieldOffset().SizeValue();
2287 return other_offset == GetFieldOffset().SizeValue();
2288 }
2289
2290 size_t ComputeHashCode() const OVERRIDE {
2291 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
2292 }
2293
2294 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
2295 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
2296
2297 DECLARE_INSTRUCTION(StaticFieldGet);
2298
2299 private:
2300 const FieldInfo field_info_;
2301
2302 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
2303};
2304
2305class HStaticFieldSet : public HTemplateInstruction<2> {
2306 public:
2307 HStaticFieldSet(HInstruction* cls,
2308 HInstruction* value,
2309 Primitive::Type field_type,
2310 MemberOffset field_offset)
2311 : HTemplateInstruction(SideEffects::ChangesSomething()),
2312 field_info_(field_offset, field_type) {
2313 SetRawInputAt(0, cls);
2314 SetRawInputAt(1, value);
2315 }
2316
2317 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
2318 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
2319
2320 DECLARE_INSTRUCTION(StaticFieldSet);
2321
2322 private:
2323 const FieldInfo field_info_;
2324
2325 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
2326};
2327
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002328// Implement the move-exception DEX instruction.
2329class HLoadException : public HExpression<0> {
2330 public:
2331 HLoadException() : HExpression(Primitive::kPrimNot, SideEffects::None()) {}
2332
2333 DECLARE_INSTRUCTION(LoadException);
2334
2335 private:
2336 DISALLOW_COPY_AND_ASSIGN(HLoadException);
2337};
2338
2339class HThrow : public HTemplateInstruction<1> {
2340 public:
2341 HThrow(HInstruction* exception, uint32_t dex_pc)
2342 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {
2343 SetRawInputAt(0, exception);
2344 }
2345
2346 bool IsControlFlow() const OVERRIDE { return true; }
2347
2348 bool NeedsEnvironment() const OVERRIDE { return true; }
2349
2350 uint32_t GetDexPc() const { return dex_pc_; }
2351
2352 DECLARE_INSTRUCTION(Throw);
2353
2354 private:
2355 uint32_t dex_pc_;
2356
2357 DISALLOW_COPY_AND_ASSIGN(HThrow);
2358};
2359
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002360class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002361 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002362 HInstanceOf(HInstruction* object,
2363 HLoadClass* constant,
2364 bool class_is_final,
2365 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002366 : HExpression(Primitive::kPrimBoolean, SideEffects::None()),
2367 class_is_final_(class_is_final),
2368 dex_pc_(dex_pc) {
2369 SetRawInputAt(0, object);
2370 SetRawInputAt(1, constant);
2371 }
2372
2373 bool CanBeMoved() const OVERRIDE { return true; }
2374
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002375 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002376 return true;
2377 }
2378
2379 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002380 return false;
2381 }
2382
2383 uint32_t GetDexPc() const { return dex_pc_; }
2384
2385 bool IsClassFinal() const { return class_is_final_; }
2386
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002387 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002388
2389 private:
2390 const bool class_is_final_;
2391 const uint32_t dex_pc_;
2392
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002393 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
2394};
2395
2396class HCheckCast : public HTemplateInstruction<2> {
2397 public:
2398 HCheckCast(HInstruction* object,
2399 HLoadClass* constant,
2400 bool class_is_final,
2401 uint32_t dex_pc)
2402 : HTemplateInstruction(SideEffects::None()),
2403 class_is_final_(class_is_final),
2404 dex_pc_(dex_pc) {
2405 SetRawInputAt(0, object);
2406 SetRawInputAt(1, constant);
2407 }
2408
2409 bool CanBeMoved() const OVERRIDE { return true; }
2410
2411 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2412 return true;
2413 }
2414
2415 bool NeedsEnvironment() const OVERRIDE {
2416 // Instruction may throw a CheckCastError.
2417 return true;
2418 }
2419
2420 bool CanThrow() const OVERRIDE { return true; }
2421
2422 uint32_t GetDexPc() const { return dex_pc_; }
2423
2424 bool IsClassFinal() const { return class_is_final_; }
2425
2426 DECLARE_INSTRUCTION(CheckCast);
2427
2428 private:
2429 const bool class_is_final_;
2430 const uint32_t dex_pc_;
2431
2432 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002433};
2434
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00002435class HMonitorOperation : public HTemplateInstruction<1> {
2436 public:
2437 enum OperationKind {
2438 kEnter,
2439 kExit,
2440 };
2441
2442 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
2443 : HTemplateInstruction(SideEffects::None()), kind_(kind), dex_pc_(dex_pc) {
2444 SetRawInputAt(0, object);
2445 }
2446
2447 // Instruction may throw a Java exception, so we need an environment.
2448 bool NeedsEnvironment() const OVERRIDE { return true; }
2449 bool CanThrow() const OVERRIDE { return true; }
2450
2451 uint32_t GetDexPc() const { return dex_pc_; }
2452
2453 bool IsEnter() const { return kind_ == kEnter; }
2454
2455 DECLARE_INSTRUCTION(MonitorOperation);
2456
2457 protected:
2458 const OperationKind kind_;
2459 const uint32_t dex_pc_;
2460
2461 private:
2462 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
2463};
2464
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002465
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002466class MoveOperands : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002467 public:
Nicolas Geoffray740475d2014-09-29 10:33:25 +01002468 MoveOperands(Location source, Location destination, HInstruction* instruction)
2469 : source_(source), destination_(destination), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002470
2471 Location GetSource() const { return source_; }
2472 Location GetDestination() const { return destination_; }
2473
2474 void SetSource(Location value) { source_ = value; }
2475 void SetDestination(Location value) { destination_ = value; }
2476
2477 // The parallel move resolver marks moves as "in-progress" by clearing the
2478 // destination (but not the source).
2479 Location MarkPending() {
2480 DCHECK(!IsPending());
2481 Location dest = destination_;
2482 destination_ = Location::NoLocation();
2483 return dest;
2484 }
2485
2486 void ClearPending(Location dest) {
2487 DCHECK(IsPending());
2488 destination_ = dest;
2489 }
2490
2491 bool IsPending() const {
2492 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
2493 return destination_.IsInvalid() && !source_.IsInvalid();
2494 }
2495
2496 // True if this blocks a move from the given location.
2497 bool Blocks(Location loc) const {
2498 return !IsEliminated() && source_.Equals(loc);
2499 }
2500
2501 // A move is redundant if it's been eliminated, if its source and
2502 // destination are the same, or if its destination is unneeded.
2503 bool IsRedundant() const {
2504 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
2505 }
2506
2507 // We clear both operands to indicate move that's been eliminated.
2508 void Eliminate() {
2509 source_ = destination_ = Location::NoLocation();
2510 }
2511
2512 bool IsEliminated() const {
2513 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
2514 return source_.IsInvalid();
2515 }
2516
Nicolas Geoffray740475d2014-09-29 10:33:25 +01002517 HInstruction* GetInstruction() const { return instruction_; }
2518
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002519 private:
2520 Location source_;
2521 Location destination_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01002522 // The instruction this move is assocatied with. Null when this move is
2523 // for moving an input in the expected locations of user (including a phi user).
2524 // This is only used in debug mode, to ensure we do not connect interval siblings
2525 // in the same parallel move.
2526 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002527
2528 DISALLOW_COPY_AND_ASSIGN(MoveOperands);
2529};
2530
2531static constexpr size_t kDefaultNumberOfMoves = 4;
2532
2533class HParallelMove : public HTemplateInstruction<0> {
2534 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002535 explicit HParallelMove(ArenaAllocator* arena)
2536 : HTemplateInstruction(SideEffects::None()), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002537
2538 void AddMove(MoveOperands* move) {
Nicolas Geoffray740475d2014-09-29 10:33:25 +01002539 if (kIsDebugBuild && move->GetInstruction() != nullptr) {
2540 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
2541 DCHECK_NE(moves_.Get(i)->GetInstruction(), move->GetInstruction())
2542 << "Doing parallel moves for the same instruction.";
2543 }
2544 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002545 moves_.Add(move);
2546 }
2547
2548 MoveOperands* MoveOperandsAt(size_t index) const {
2549 return moves_.Get(index);
2550 }
2551
2552 size_t NumMoves() const { return moves_.Size(); }
2553
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002554 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002555
2556 private:
2557 GrowableArray<MoveOperands*> moves_;
2558
2559 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
2560};
2561
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002562class HGraphVisitor : public ValueObject {
2563 public:
Dave Allison20dfc792014-06-16 20:44:29 -07002564 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
2565 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002566
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002567 virtual void VisitInstruction(HInstruction* instruction) { UNUSED(instruction); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002568 virtual void VisitBasicBlock(HBasicBlock* block);
2569
Roland Levillain633021e2014-10-01 14:12:25 +01002570 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002571 void VisitInsertionOrder();
2572
Roland Levillain633021e2014-10-01 14:12:25 +01002573 // Visit the graph following dominator tree reverse post-order.
2574 void VisitReversePostOrder();
2575
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002576 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002577
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002578 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002579#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002580 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
2581
2582 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
2583
2584#undef DECLARE_VISIT_INSTRUCTION
2585
2586 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07002587 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002588
2589 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
2590};
2591
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002592class HGraphDelegateVisitor : public HGraphVisitor {
2593 public:
2594 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
2595 virtual ~HGraphDelegateVisitor() {}
2596
2597 // Visit functions that delegate to to super class.
2598#define DECLARE_VISIT_INSTRUCTION(name, super) \
2599 virtual void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
2600
2601 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
2602
2603#undef DECLARE_VISIT_INSTRUCTION
2604
2605 private:
2606 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
2607};
2608
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002609class HInsertionOrderIterator : public ValueObject {
2610 public:
2611 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
2612
2613 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
2614 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
2615 void Advance() { ++index_; }
2616
2617 private:
2618 const HGraph& graph_;
2619 size_t index_;
2620
2621 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
2622};
2623
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002624class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002625 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002626 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002627
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002628 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
2629 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002630 void Advance() { ++index_; }
2631
2632 private:
2633 const HGraph& graph_;
2634 size_t index_;
2635
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002636 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002637};
2638
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002639class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002640 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002641 explicit HPostOrderIterator(const HGraph& graph)
2642 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002643
2644 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002645 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002646 void Advance() { --index_; }
2647
2648 private:
2649 const HGraph& graph_;
2650 size_t index_;
2651
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002652 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002653};
2654
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002655} // namespace art
2656
2657#endif // ART_COMPILER_OPTIMIZING_NODES_H_