blob: 142b517ea2d49449f53054678e9d715f6ab5ef57 [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"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000023#include "utils/allocation.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 Geoffrayddb311f2014-05-16 09:28:54 +010035class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000036class LocationSummary;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000037
38static const int kDefaultNumberOfBlocks = 8;
39static const int kDefaultNumberOfSuccessors = 2;
40static const int kDefaultNumberOfPredecessors = 2;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000041static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000042
Dave Allison20dfc792014-06-16 20:44:29 -070043enum IfCondition {
44 kCondEQ,
45 kCondNE,
46 kCondLT,
47 kCondLE,
48 kCondGT,
49 kCondGE,
50};
51
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010052class HInstructionList {
53 public:
54 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
55
56 void AddInstruction(HInstruction* instruction);
57 void RemoveInstruction(HInstruction* instruction);
58
59 private:
60 HInstruction* first_instruction_;
61 HInstruction* last_instruction_;
62
63 friend class HBasicBlock;
64 friend class HInstructionIterator;
Nicolas Geoffray804d0932014-05-02 08:46:00 +010065 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010066
67 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
68};
69
Nicolas Geoffray818f2102014-02-18 16:43:35 +000070// Control-flow graph of a method. Contains a list of basic blocks.
71class HGraph : public ArenaObject {
72 public:
73 explicit HGraph(ArenaAllocator* arena)
74 : arena_(arena),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000075 blocks_(arena, kDefaultNumberOfBlocks),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +010076 reverse_post_order_(arena, kDefaultNumberOfBlocks),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +010077 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +010078 number_of_vregs_(0),
79 number_of_in_vregs_(0),
Nicolas Geoffraye5038322014-07-04 09:41:32 +010080 number_of_temporaries_(0),
Dave Allison20dfc792014-06-16 20:44:29 -070081 current_instruction_id_(0) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +000082
Nicolas Geoffray787c3072014-03-17 10:20:19 +000083 ArenaAllocator* GetArena() const { return arena_; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +010084 const GrowableArray<HBasicBlock*>& GetBlocks() const { return blocks_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +000085
Nicolas Geoffray787c3072014-03-17 10:20:19 +000086 HBasicBlock* GetEntryBlock() const { return entry_block_; }
87 HBasicBlock* GetExitBlock() const { return exit_block_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000088
Nicolas Geoffray787c3072014-03-17 10:20:19 +000089 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
90 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000091
Nicolas Geoffray818f2102014-02-18 16:43:35 +000092 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010093
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000094 void BuildDominatorTree();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010095 void TransformToSSA();
96 void SimplifyCFG();
Nicolas Geoffray818f2102014-02-18 16:43:35 +000097
Nicolas Geoffray622d9c32014-05-12 16:11:02 +010098 // Find all natural loops in this graph. Aborts computation and returns false
Nicolas Geoffrayf635e632014-05-14 09:43:38 +010099 // if one loop is not natural, that is the header does not dominate the back
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100100 // edge.
101 bool FindNaturalLoops() const;
102
103 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
104 void SimplifyLoop(HBasicBlock* header);
105
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000106 int GetNextInstructionId() {
107 return current_instruction_id_++;
108 }
109
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100110 uint16_t GetMaximumNumberOfOutVRegs() const {
111 return maximum_number_of_out_vregs_;
112 }
113
114 void UpdateMaximumNumberOfOutVRegs(uint16_t new_value) {
115 maximum_number_of_out_vregs_ = std::max(new_value, maximum_number_of_out_vregs_);
116 }
117
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100118 void UpdateNumberOfTemporaries(size_t count) {
119 number_of_temporaries_ = std::max(count, number_of_temporaries_);
120 }
121
122 size_t GetNumberOfTemporaries() const {
123 return number_of_temporaries_;
124 }
125
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100126 void SetNumberOfVRegs(uint16_t number_of_vregs) {
127 number_of_vregs_ = number_of_vregs;
128 }
129
130 uint16_t GetNumberOfVRegs() const {
131 return number_of_vregs_;
132 }
133
134 void SetNumberOfInVRegs(uint16_t value) {
135 number_of_in_vregs_ = value;
136 }
137
138 uint16_t GetNumberOfInVRegs() const {
139 return number_of_in_vregs_;
140 }
141
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100142 uint16_t GetNumberOfLocalVRegs() const {
143 return number_of_vregs_ - number_of_in_vregs_;
144 }
145
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100146 const GrowableArray<HBasicBlock*>& GetReversePostOrder() const {
147 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100148 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100149
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000150 private:
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000151 HBasicBlock* FindCommonDominator(HBasicBlock* first, HBasicBlock* second) const;
152 void VisitBlockForDominatorTree(HBasicBlock* block,
153 HBasicBlock* predecessor,
154 GrowableArray<size_t>* visits);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100155 void FindBackEdges(ArenaBitVector* visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000156 void VisitBlockForBackEdges(HBasicBlock* block,
157 ArenaBitVector* visited,
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100158 ArenaBitVector* visiting);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000159 void RemoveDeadBlocks(const ArenaBitVector& visited) const;
160
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000161 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000162
163 // List of blocks in insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000164 GrowableArray<HBasicBlock*> blocks_;
165
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100166 // List of blocks to perform a reverse post order tree traversal.
167 GrowableArray<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000168
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000169 HBasicBlock* entry_block_;
170 HBasicBlock* exit_block_;
171
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100172 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100173 uint16_t maximum_number_of_out_vregs_;
174
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100175 // The number of virtual registers in this method. Contains the parameters.
176 uint16_t number_of_vregs_;
177
178 // The number of virtual registers used by parameters of this method.
179 uint16_t number_of_in_vregs_;
180
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100181 // The number of temporaries that will be needed for the baseline compiler.
182 size_t number_of_temporaries_;
183
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000184 // The current id to assign to a newly added instruction. See HInstruction.id_.
185 int current_instruction_id_;
186
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000187 DISALLOW_COPY_AND_ASSIGN(HGraph);
188};
189
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000190class HLoopInformation : public ArenaObject {
191 public:
192 HLoopInformation(HBasicBlock* header, HGraph* graph)
193 : header_(header),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100194 back_edges_(graph->GetArena(), kDefaultNumberOfBackEdges),
195 blocks_(graph->GetArena(), graph->GetBlocks().Size(), false) {}
196
197 HBasicBlock* GetHeader() const {
198 return header_;
199 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000200
201 void AddBackEdge(HBasicBlock* back_edge) {
202 back_edges_.Add(back_edge);
203 }
204
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100205 void RemoveBackEdge(HBasicBlock* back_edge) {
206 back_edges_.Delete(back_edge);
207 }
208
209 bool IsBackEdge(HBasicBlock* block) {
210 for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) {
211 if (back_edges_.Get(i) == block) return true;
212 }
213 return false;
214 }
215
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000216 int NumberOfBackEdges() const {
217 return back_edges_.Size();
218 }
219
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100220 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100221
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100222 const GrowableArray<HBasicBlock*>& GetBackEdges() const {
223 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100224 }
225
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100226 void ClearBackEdges() {
227 back_edges_.Reset();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100228 }
229
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100230 // Find blocks that are part of this loop. Returns whether the loop is a natural loop,
231 // that is the header dominates the back edge.
232 bool Populate();
233
234 // Returns whether this loop information contains `block`.
235 // Note that this loop information *must* be populated before entering this function.
236 bool Contains(const HBasicBlock& block) const;
237
238 // Returns whether this loop information is an inner loop of `other`.
239 // Note that `other` *must* be populated before entering this function.
240 bool IsIn(const HLoopInformation& other) const;
241
242 const ArenaBitVector& GetBlocks() const { return blocks_; }
243
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000244 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100245 // Internal recursive implementation of `Populate`.
246 void PopulateRecursive(HBasicBlock* block);
247
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000248 HBasicBlock* header_;
249 GrowableArray<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100250 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000251
252 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
253};
254
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100255static constexpr size_t kNoLifetime = -1;
256
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000257// A block in a method. Contains the list of instructions represented
258// as a double linked list. Each block knows its predecessors and
259// successors.
260class HBasicBlock : public ArenaObject {
261 public:
262 explicit HBasicBlock(HGraph* graph)
263 : graph_(graph),
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000264 predecessors_(graph->GetArena(), kDefaultNumberOfPredecessors),
265 successors_(graph->GetArena(), kDefaultNumberOfSuccessors),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000266 loop_information_(nullptr),
267 dominator_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100268 block_id_(-1),
269 lifetime_start_(kNoLifetime),
270 lifetime_end_(kNoLifetime) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000271
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100272 const GrowableArray<HBasicBlock*>& GetPredecessors() const {
273 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000274 }
275
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100276 const GrowableArray<HBasicBlock*>& GetSuccessors() const {
277 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000278 }
279
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000280 void AddBackEdge(HBasicBlock* back_edge) {
281 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000282 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000283 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100284 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000285 loop_information_->AddBackEdge(back_edge);
286 }
287
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000288 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000289
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000290 int GetBlockId() const { return block_id_; }
291 void SetBlockId(int id) { block_id_ = id; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000292
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000293 HBasicBlock* GetDominator() const { return dominator_; }
294 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000295
296 int NumberOfBackEdges() const {
297 return loop_information_ == nullptr
298 ? 0
299 : loop_information_->NumberOfBackEdges();
300 }
301
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100302 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
303 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100304 const HInstructionList& GetInstructions() const { return instructions_; }
305 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100306 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000307
308 void AddSuccessor(HBasicBlock* block) {
309 successors_.Add(block);
310 block->predecessors_.Add(this);
311 }
312
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100313 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
314 size_t successor_index = GetSuccessorIndexOf(existing);
315 DCHECK_NE(successor_index, static_cast<size_t>(-1));
316 existing->RemovePredecessor(this);
317 new_block->predecessors_.Add(this);
318 successors_.Put(successor_index, new_block);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000319 }
320
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100321 void RemovePredecessor(HBasicBlock* block) {
322 predecessors_.Delete(block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100323 }
324
325 void ClearAllPredecessors() {
326 predecessors_.Reset();
327 }
328
329 void AddPredecessor(HBasicBlock* block) {
330 predecessors_.Add(block);
331 block->successors_.Add(this);
332 }
333
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100334 void SwapPredecessors() {
335 DCHECK(predecessors_.Size() == 2);
336 HBasicBlock* temp = predecessors_.Get(0);
337 predecessors_.Put(0, predecessors_.Get(1));
338 predecessors_.Put(1, temp);
339 }
340
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100341 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) {
342 for (size_t i = 0, e = predecessors_.Size(); i < e; ++i) {
343 if (predecessors_.Get(i) == predecessor) {
344 return i;
345 }
346 }
347 return -1;
348 }
349
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100350 size_t GetSuccessorIndexOf(HBasicBlock* successor) {
351 for (size_t i = 0, e = successors_.Size(); i < e; ++i) {
352 if (successors_.Get(i) == successor) {
353 return i;
354 }
355 }
356 return -1;
357 }
358
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000359 void AddInstruction(HInstruction* instruction);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100360 void RemoveInstruction(HInstruction* instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100361 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100362 void AddPhi(HPhi* phi);
363 void RemovePhi(HPhi* phi);
364
365 bool IsLoopHeader() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100366 return (loop_information_ != nullptr) && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100367 }
368
369 HLoopInformation* GetLoopInformation() const {
370 return loop_information_;
371 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000372
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100373 // Set the loop_information_ on this block. This method overrides the current
374 // loop_information if it is an outer loop of the passed loop information.
375 void SetInLoop(HLoopInformation* info) {
376 if (IsLoopHeader()) {
377 // Nothing to do. This just means `info` is an outer loop.
378 } else if (loop_information_ == nullptr) {
379 loop_information_ = info;
380 } else if (loop_information_->Contains(*info->GetHeader())) {
381 // Block is currently part of an outer loop. Make it part of this inner loop.
382 // Note that a non loop header having a loop information means this loop information
383 // has already been populated
384 loop_information_ = info;
385 } else {
386 // Block is part of an inner loop. Do not update the loop information.
387 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
388 // at this point, because this method is being called while populating `info`.
389 }
390 }
391
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100392 bool IsInLoop() const { return loop_information_ != nullptr; }
393
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100394 // Returns wheter this block dominates the blocked passed as parameter.
395 bool Dominates(HBasicBlock* block) const;
396
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100397 size_t GetLifetimeStart() const { return lifetime_start_; }
398 size_t GetLifetimeEnd() const { return lifetime_end_; }
399
400 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
401 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
402
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000403 private:
404 HGraph* const graph_;
405 GrowableArray<HBasicBlock*> predecessors_;
406 GrowableArray<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100407 HInstructionList instructions_;
408 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000409 HLoopInformation* loop_information_;
410 HBasicBlock* dominator_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000411 int block_id_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100412 size_t lifetime_start_;
413 size_t lifetime_end_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000414
415 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
416};
417
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100418#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000419 M(Add) \
Dave Allison20dfc792014-06-16 20:44:29 -0700420 M(Condition) \
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000421 M(Equal) \
Dave Allison20dfc792014-06-16 20:44:29 -0700422 M(NotEqual) \
423 M(LessThan) \
424 M(LessThanOrEqual) \
425 M(GreaterThan) \
426 M(GreaterThanOrEqual) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000427 M(Exit) \
428 M(Goto) \
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000429 M(If) \
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000430 M(IntConstant) \
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000431 M(InvokeStatic) \
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100432 M(InvokeVirtual) \
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000433 M(LoadLocal) \
434 M(Local) \
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100435 M(LongConstant) \
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +0100436 M(NewInstance) \
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100437 M(Not) \
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100438 M(ParameterValue) \
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100439 M(ParallelMove) \
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100440 M(Phi) \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000441 M(Return) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000442 M(ReturnVoid) \
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000443 M(StoreLocal) \
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100444 M(Sub) \
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100445 M(Compare) \
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100446 M(InstanceFieldGet) \
447 M(InstanceFieldSet) \
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100448 M(ArrayGet) \
449 M(ArraySet) \
450 M(ArrayLength) \
451 M(BoundsCheck) \
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100452 M(NullCheck) \
453 M(Temporary) \
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000454 M(SuspendCheck) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000455
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100456#define FOR_EACH_INSTRUCTION(M) \
457 FOR_EACH_CONCRETE_INSTRUCTION(M) \
458 M(Constant)
Dave Allison20dfc792014-06-16 20:44:29 -0700459
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000460#define FORWARD_DECLARATION(type) class H##type;
461FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
462#undef FORWARD_DECLARATION
463
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000464#define DECLARE_INSTRUCTION(type) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000465 virtual const char* DebugName() const { return #type; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000466 virtual H##type* As##type() { return this; } \
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100467 virtual bool InstructionTypeEquals(HInstruction* other) const { \
468 return other->Is##type(); \
469 } \
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100470 virtual void Accept(HGraphVisitor* visitor) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000471
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100472template <typename T>
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000473class HUseListNode : public ArenaObject {
474 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100475 HUseListNode(T* user, size_t index, HUseListNode* tail)
Dave Allison20dfc792014-06-16 20:44:29 -0700476 : user_(user), index_(index), tail_(tail) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000477
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000478 HUseListNode* GetTail() const { return tail_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100479 T* GetUser() const { return user_; }
480 size_t GetIndex() const { return index_; }
481
482 void SetTail(HUseListNode<T>* node) { tail_ = node; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000483
484 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100485 T* const user_;
486 const size_t index_;
487 HUseListNode<T>* tail_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000488
489 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
490};
491
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100492// Represents the side effects an instruction may have.
493class SideEffects : public ValueObject {
494 public:
495 static SideEffects None() {
496 return SideEffects(0);
497 }
498
499 static SideEffects All() {
500 return SideEffects(ChangesSomething().flags_ | DependsOnSomething().flags_);
501 }
502
503 static SideEffects ChangesSomething() {
504 return SideEffects((1 << kFlagChangesCount) - 1);
505 }
506
507 static SideEffects DependsOnSomething() {
508 int count = kFlagDependsOnCount - kFlagChangesCount;
509 return SideEffects(((1 << count) - 1) << kFlagChangesCount);
510 }
511
512 private:
513 static constexpr int kFlagChangesSomething = 0;
514 static constexpr int kFlagChangesCount = kFlagChangesSomething + 1;
515
516 static constexpr int kFlagDependsOnSomething = kFlagChangesCount;
517 static constexpr int kFlagDependsOnCount = kFlagDependsOnSomething + 1;
518
519 private:
520 explicit SideEffects(size_t flags) : flags_(flags) {}
521
522 const size_t flags_;
523};
524
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000525class HInstruction : public ArenaObject {
526 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100527 explicit HInstruction(SideEffects side_effects)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000528 : previous_(nullptr),
529 next_(nullptr),
530 block_(nullptr),
531 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100532 ssa_index_(-1),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000533 uses_(nullptr),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100534 env_uses_(nullptr),
535 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100536 locations_(nullptr),
537 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100538 lifetime_position_(kNoLifetime),
539 side_effects_(side_effects) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000540
Dave Allison20dfc792014-06-16 20:44:29 -0700541 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000542
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000543 HInstruction* GetNext() const { return next_; }
544 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000545
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000546 HBasicBlock* GetBlock() const { return block_; }
547 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100548 bool IsInBlock() const { return block_ != nullptr; }
549 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +0100550 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000551
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100552 virtual size_t InputCount() const = 0;
553 virtual HInstruction* InputAt(size_t i) const = 0;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000554
555 virtual void Accept(HGraphVisitor* visitor) = 0;
556 virtual const char* DebugName() const = 0;
557
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100558 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100559 virtual void SetRawInputAt(size_t index, HInstruction* input) = 0;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100560
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100561 virtual bool NeedsEnvironment() const { return false; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100562 virtual bool IsControlFlow() const { return false; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100563
564 void AddUseAt(HInstruction* user, size_t index) {
565 uses_ = new (block_->GetGraph()->GetArena()) HUseListNode<HInstruction>(user, index, uses_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000566 }
567
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100568 void AddEnvUseAt(HEnvironment* user, size_t index) {
569 env_uses_ = new (block_->GetGraph()->GetArena()) HUseListNode<HEnvironment>(
570 user, index, env_uses_);
571 }
572
573 void RemoveUser(HInstruction* user, size_t index);
574
575 HUseListNode<HInstruction>* GetUses() const { return uses_; }
576 HUseListNode<HEnvironment>* GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000577
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100578 bool HasUses() const { return uses_ != nullptr || env_uses_ != nullptr; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100579 bool HasEnvironmentUses() const { return env_uses_ != nullptr; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000580
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100581 size_t NumberOfUses() const {
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100582 // TODO: Optimize this method if it is used outside of the HGraphVisualizer.
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100583 size_t result = 0;
584 HUseListNode<HInstruction>* current = uses_;
585 while (current != nullptr) {
586 current = current->GetTail();
587 ++result;
588 }
589 return result;
590 }
591
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000592 int GetId() const { return id_; }
593 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000594
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100595 int GetSsaIndex() const { return ssa_index_; }
596 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
597 bool HasSsaIndex() const { return ssa_index_ != -1; }
598
599 bool HasEnvironment() const { return environment_ != nullptr; }
600 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100601 void SetEnvironment(HEnvironment* environment) { environment_ = environment; }
602
Nicolas Geoffray39468442014-09-02 15:17:15 +0100603 // Returns the number of entries in the environment. Typically, that is the
604 // number of dex registers in a method. It could be more in case of inlining.
605 size_t EnvironmentSize() const;
606
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000607 LocationSummary* GetLocations() const { return locations_; }
608 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000609
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100610 void ReplaceWith(HInstruction* instruction);
611
Dave Allison20dfc792014-06-16 20:44:29 -0700612 bool HasOnlyOneUse() const {
613 return uses_ != nullptr && uses_->GetTail() == nullptr;
614 }
615
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000616#define INSTRUCTION_TYPE_CHECK(type) \
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100617 bool Is##type() { return (As##type() != nullptr); } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000618 virtual H##type* As##type() { return nullptr; }
619
620 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
621#undef INSTRUCTION_TYPE_CHECK
622
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100623 // Returns whether the instruction can be moved within the graph.
624 virtual bool CanBeMoved() const { return false; }
625
626 // Returns whether the two instructions are of the same kind.
627 virtual bool InstructionTypeEquals(HInstruction* other) const { return false; }
628
629 // Returns whether any data encoded in the two instructions is equal.
630 // This method does not look at the inputs. Both instructions must be
631 // of the same type, otherwise the method has undefined behavior.
632 virtual bool InstructionDataEquals(HInstruction* other) const { return false; }
633
634 // Returns whether two instructions are equal, that is:
635 // 1) They have the same type and contain the same data,
636 // 2) Their inputs are identical.
637 bool Equals(HInstruction* other) const;
638
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100639 size_t GetLifetimePosition() const { return lifetime_position_; }
640 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
641 LiveInterval* GetLiveInterval() const { return live_interval_; }
642 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
643 bool HasLiveInterval() const { return live_interval_ != nullptr; }
644
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000645 private:
646 HInstruction* previous_;
647 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000648 HBasicBlock* block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000649
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000650 // An instruction gets an id when it is added to the graph.
651 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +0100652 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000653 int id_;
654
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100655 // When doing liveness analysis, instructions that have uses get an SSA index.
656 int ssa_index_;
657
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100658 // List of instructions that have this instruction as input.
659 HUseListNode<HInstruction>* uses_;
660
661 // List of environments that contain this instruction.
662 HUseListNode<HEnvironment>* env_uses_;
663
Nicolas Geoffray39468442014-09-02 15:17:15 +0100664 // The environment associated with this instruction. Not null if the instruction
665 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100666 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000667
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000668 // Set by the code generator.
669 LocationSummary* locations_;
670
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100671 // Set by the liveness analysis.
672 LiveInterval* live_interval_;
673
674 // Set by the liveness analysis, this is the position in a linear
675 // order of blocks where this instruction's live interval start.
676 size_t lifetime_position_;
677
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100678 const SideEffects side_effects_;
679
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000680 friend class HBasicBlock;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100681 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000682
683 DISALLOW_COPY_AND_ASSIGN(HInstruction);
684};
685
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100686template<typename T>
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000687class HUseIterator : public ValueObject {
688 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100689 explicit HUseIterator(HUseListNode<T>* uses) : current_(uses) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000690
691 bool Done() const { return current_ == nullptr; }
692
693 void Advance() {
694 DCHECK(!Done());
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000695 current_ = current_->GetTail();
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000696 }
697
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100698 HUseListNode<T>* Current() const {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000699 DCHECK(!Done());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100700 return current_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000701 }
702
703 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100704 HUseListNode<T>* current_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000705
706 friend class HValue;
707};
708
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100709// A HEnvironment object contains the values of virtual registers at a given location.
710class HEnvironment : public ArenaObject {
711 public:
712 HEnvironment(ArenaAllocator* arena, size_t number_of_vregs) : vregs_(arena, number_of_vregs) {
713 vregs_.SetSize(number_of_vregs);
714 for (size_t i = 0; i < number_of_vregs; i++) {
715 vregs_.Put(i, nullptr);
716 }
717 }
718
719 void Populate(const GrowableArray<HInstruction*>& env) {
720 for (size_t i = 0; i < env.Size(); i++) {
721 HInstruction* instruction = env.Get(i);
722 vregs_.Put(i, instruction);
723 if (instruction != nullptr) {
724 instruction->AddEnvUseAt(this, i);
725 }
726 }
727 }
728
729 void SetRawEnvAt(size_t index, HInstruction* instruction) {
730 vregs_.Put(index, instruction);
731 }
732
Nicolas Geoffray39468442014-09-02 15:17:15 +0100733 HInstruction* GetInstructionAt(size_t index) const {
734 return vregs_.Get(index);
735 }
736
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100737 GrowableArray<HInstruction*>* GetVRegs() {
738 return &vregs_;
739 }
740
Nicolas Geoffray39468442014-09-02 15:17:15 +0100741 size_t Size() const { return vregs_.Size(); }
742
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100743 private:
744 GrowableArray<HInstruction*> vregs_;
745
746 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
747};
748
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000749class HInputIterator : public ValueObject {
750 public:
Dave Allison20dfc792014-06-16 20:44:29 -0700751 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000752
753 bool Done() const { return index_ == instruction_->InputCount(); }
754 HInstruction* Current() const { return instruction_->InputAt(index_); }
755 void Advance() { index_++; }
756
757 private:
758 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100759 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000760
761 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
762};
763
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000764class HInstructionIterator : public ValueObject {
765 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100766 explicit HInstructionIterator(const HInstructionList& instructions)
767 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000768 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000769 }
770
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000771 bool Done() const { return instruction_ == nullptr; }
772 HInstruction* Current() const { return instruction_; }
773 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000774 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000775 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000776 }
777
778 private:
779 HInstruction* instruction_;
780 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100781
782 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000783};
784
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100785class HBackwardInstructionIterator : public ValueObject {
786 public:
787 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
788 : instruction_(instructions.last_instruction_) {
789 next_ = Done() ? nullptr : instruction_->GetPrevious();
790 }
791
792 bool Done() const { return instruction_ == nullptr; }
793 HInstruction* Current() const { return instruction_; }
794 void Advance() {
795 instruction_ = next_;
796 next_ = Done() ? nullptr : instruction_->GetPrevious();
797 }
798
799 private:
800 HInstruction* instruction_;
801 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100802
803 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100804};
805
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000806// An embedded container with N elements of type T. Used (with partial
807// specialization for N=0) because embedded arrays cannot have size 0.
808template<typename T, intptr_t N>
809class EmbeddedArray {
810 public:
Dave Allison20dfc792014-06-16 20:44:29 -0700811 EmbeddedArray() : elements_() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000812
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000813 intptr_t GetLength() const { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000814
815 const T& operator[](intptr_t i) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000816 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000817 return elements_[i];
818 }
819
820 T& operator[](intptr_t i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000821 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000822 return elements_[i];
823 }
824
825 const T& At(intptr_t i) const {
826 return (*this)[i];
827 }
828
829 void SetAt(intptr_t i, const T& val) {
830 (*this)[i] = val;
831 }
832
833 private:
834 T elements_[N];
835};
836
837template<typename T>
838class EmbeddedArray<T, 0> {
839 public:
840 intptr_t length() const { return 0; }
841 const T& operator[](intptr_t i) const {
842 LOG(FATAL) << "Unreachable";
843 static T sentinel = 0;
844 return sentinel;
845 }
846 T& operator[](intptr_t i) {
847 LOG(FATAL) << "Unreachable";
848 static T sentinel = 0;
849 return sentinel;
850 }
851};
852
853template<intptr_t N>
854class HTemplateInstruction: public HInstruction {
855 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100856 HTemplateInstruction<N>(SideEffects side_effects)
857 : HInstruction(side_effects), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -0700858 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000859
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100860 virtual size_t InputCount() const { return N; }
861 virtual HInstruction* InputAt(size_t i) const { return inputs_[i]; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000862
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000863 protected:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100864 virtual void SetRawInputAt(size_t i, HInstruction* instruction) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000865 inputs_[i] = instruction;
866 }
867
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000868 private:
869 EmbeddedArray<HInstruction*, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100870
871 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000872};
873
Dave Allison20dfc792014-06-16 20:44:29 -0700874template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100875class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -0700876 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100877 HExpression<N>(Primitive::Type type, SideEffects side_effects)
878 : HTemplateInstruction<N>(side_effects), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -0700879 virtual ~HExpression() {}
880
881 virtual Primitive::Type GetType() const { return type_; }
882
883 private:
884 const Primitive::Type type_;
885};
886
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000887// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
888// instruction that branches to the exit block.
889class HReturnVoid : public HTemplateInstruction<0> {
890 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100891 HReturnVoid() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100892
893 virtual bool IsControlFlow() const { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000894
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100895 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000896
897 private:
898 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
899};
900
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000901// Represents dex's RETURN opcodes. A HReturn is a control flow
902// instruction that branches to the exit block.
903class HReturn : public HTemplateInstruction<1> {
904 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100905 explicit HReturn(HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000906 SetRawInputAt(0, value);
907 }
908
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100909 virtual bool IsControlFlow() const { return true; }
910
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100911 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000912
913 private:
914 DISALLOW_COPY_AND_ASSIGN(HReturn);
915};
916
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000917// The exit instruction is the only instruction of the exit block.
918// Instructions aborting the method (HTrow and HReturn) must branch to the
919// exit block.
920class HExit : public HTemplateInstruction<0> {
921 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100922 HExit() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100923
924 virtual bool IsControlFlow() const { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000925
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100926 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000927
928 private:
929 DISALLOW_COPY_AND_ASSIGN(HExit);
930};
931
932// Jumps from one block to another.
933class HGoto : public HTemplateInstruction<0> {
934 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100935 HGoto() : HTemplateInstruction(SideEffects::None()) {}
936
937 virtual bool IsControlFlow() const { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000938
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000939 HBasicBlock* GetSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100940 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000941 }
942
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100943 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000944
945 private:
946 DISALLOW_COPY_AND_ASSIGN(HGoto);
947};
948
Dave Allison20dfc792014-06-16 20:44:29 -0700949
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000950// Conditional branch. A block ending with an HIf instruction must have
951// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000952class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000953 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100954 explicit HIf(HInstruction* input) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000955 SetRawInputAt(0, input);
956 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000957
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100958 virtual bool IsControlFlow() const { return true; }
959
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000960 HBasicBlock* IfTrueSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100961 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000962 }
963
964 HBasicBlock* IfFalseSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100965 return GetBlock()->GetSuccessors().Get(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000966 }
967
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100968 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000969
Dave Allison20dfc792014-06-16 20:44:29 -0700970 virtual bool IsIfInstruction() const { return true; }
971
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000972 private:
973 DISALLOW_COPY_AND_ASSIGN(HIf);
974};
975
Dave Allison20dfc792014-06-16 20:44:29 -0700976class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000977 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000978 HBinaryOperation(Primitive::Type result_type,
979 HInstruction* left,
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100980 HInstruction* right) : HExpression(result_type, SideEffects::None()) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000981 SetRawInputAt(0, left);
982 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000983 }
984
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000985 HInstruction* GetLeft() const { return InputAt(0); }
986 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -0700987 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000988
989 virtual bool IsCommutative() { return false; }
990
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100991 virtual bool CanBeMoved() const { return true; }
992 virtual bool InstructionDataEquals(HInstruction* other) const { return true; }
993
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000994 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000995 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
996};
997
Dave Allison20dfc792014-06-16 20:44:29 -0700998class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000999 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001000 HCondition(HInstruction* first, HInstruction* second)
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001001 : HBinaryOperation(Primitive::kPrimBoolean, first, second) {}
1002
1003 virtual bool IsCommutative() { return true; }
Dave Allison20dfc792014-06-16 20:44:29 -07001004 bool NeedsMaterialization() const;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001005
Dave Allison20dfc792014-06-16 20:44:29 -07001006 DECLARE_INSTRUCTION(Condition);
1007
1008 virtual IfCondition GetCondition() const = 0;
1009
1010 private:
1011 DISALLOW_COPY_AND_ASSIGN(HCondition);
1012};
1013
1014// Instruction to check if two inputs are equal to each other.
1015class HEqual : public HCondition {
1016 public:
1017 HEqual(HInstruction* first, HInstruction* second)
1018 : HCondition(first, second) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001019
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001020 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001021
Dave Allison20dfc792014-06-16 20:44:29 -07001022 virtual IfCondition GetCondition() const {
1023 return kCondEQ;
1024 }
1025
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001026 private:
1027 DISALLOW_COPY_AND_ASSIGN(HEqual);
1028};
1029
Dave Allison20dfc792014-06-16 20:44:29 -07001030class HNotEqual : public HCondition {
1031 public:
1032 HNotEqual(HInstruction* first, HInstruction* second)
1033 : HCondition(first, second) {}
1034
1035 DECLARE_INSTRUCTION(NotEqual);
1036
1037 virtual IfCondition GetCondition() const {
1038 return kCondNE;
1039 }
1040
1041 private:
1042 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
1043};
1044
1045class HLessThan : public HCondition {
1046 public:
1047 HLessThan(HInstruction* first, HInstruction* second)
1048 : HCondition(first, second) {}
1049
1050 DECLARE_INSTRUCTION(LessThan);
1051
1052 virtual IfCondition GetCondition() const {
1053 return kCondLT;
1054 }
1055
1056 private:
1057 DISALLOW_COPY_AND_ASSIGN(HLessThan);
1058};
1059
1060class HLessThanOrEqual : public HCondition {
1061 public:
1062 HLessThanOrEqual(HInstruction* first, HInstruction* second)
1063 : HCondition(first, second) {}
1064
1065 DECLARE_INSTRUCTION(LessThanOrEqual);
1066
1067 virtual IfCondition GetCondition() const {
1068 return kCondLE;
1069 }
1070
1071 private:
1072 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
1073};
1074
1075class HGreaterThan : public HCondition {
1076 public:
1077 HGreaterThan(HInstruction* first, HInstruction* second)
1078 : HCondition(first, second) {}
1079
1080 DECLARE_INSTRUCTION(GreaterThan);
1081
1082 virtual IfCondition GetCondition() const {
1083 return kCondGT;
1084 }
1085
1086 private:
1087 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
1088};
1089
1090class HGreaterThanOrEqual : public HCondition {
1091 public:
1092 HGreaterThanOrEqual(HInstruction* first, HInstruction* second)
1093 : HCondition(first, second) {}
1094
1095 DECLARE_INSTRUCTION(GreaterThanOrEqual);
1096
1097 virtual IfCondition GetCondition() const {
1098 return kCondGE;
1099 }
1100
1101 private:
1102 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
1103};
1104
1105
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001106// Instruction to check how two inputs compare to each other.
1107// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
1108class HCompare : public HBinaryOperation {
1109 public:
1110 HCompare(Primitive::Type type, HInstruction* first, HInstruction* second)
1111 : HBinaryOperation(Primitive::kPrimInt, first, second) {
1112 DCHECK_EQ(type, first->GetType());
1113 DCHECK_EQ(type, second->GetType());
1114 }
1115
1116 DECLARE_INSTRUCTION(Compare);
1117
1118 private:
1119 DISALLOW_COPY_AND_ASSIGN(HCompare);
1120};
1121
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001122// A local in the graph. Corresponds to a Dex register.
1123class HLocal : public HTemplateInstruction<0> {
1124 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001125 explicit HLocal(uint16_t reg_number)
1126 : HTemplateInstruction(SideEffects::None()), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001127
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001128 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001129
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001130 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001131
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001132 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001133 // The Dex register number.
1134 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001135
1136 DISALLOW_COPY_AND_ASSIGN(HLocal);
1137};
1138
1139// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07001140class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001141 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001142 explicit HLoadLocal(HLocal* local, Primitive::Type type)
1143 : HExpression(type, SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001144 SetRawInputAt(0, local);
1145 }
1146
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001147 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1148
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001149 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001150
1151 private:
1152 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
1153};
1154
1155// Store a value in a given local. This instruction has two inputs: the value
1156// and the local.
1157class HStoreLocal : public HTemplateInstruction<2> {
1158 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001159 HStoreLocal(HLocal* local, HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001160 SetRawInputAt(0, local);
1161 SetRawInputAt(1, value);
1162 }
1163
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001164 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1165
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001166 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001167
1168 private:
1169 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
1170};
1171
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001172class HConstant : public HExpression<0> {
1173 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001174 explicit HConstant(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
1175
1176 virtual bool CanBeMoved() const { return true; }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001177
1178 DECLARE_INSTRUCTION(Constant);
1179
1180 private:
1181 DISALLOW_COPY_AND_ASSIGN(HConstant);
1182};
1183
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001184// Constants of the type int. Those can be from Dex instructions, or
1185// synthesized (for example with the if-eqz instruction).
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001186class HIntConstant : public HConstant {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001187 public:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001188 explicit HIntConstant(int32_t value) : HConstant(Primitive::kPrimInt), value_(value) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001189
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001190 int32_t GetValue() const { return value_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001191
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001192 virtual bool InstructionDataEquals(HInstruction* other) const {
1193 return other->AsIntConstant()->value_ == value_;
1194 }
1195
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001196 DECLARE_INSTRUCTION(IntConstant);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001197
1198 private:
1199 const int32_t value_;
1200
1201 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
1202};
1203
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001204class HLongConstant : public HConstant {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001205 public:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001206 explicit HLongConstant(int64_t value) : HConstant(Primitive::kPrimLong), value_(value) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001207
1208 int64_t GetValue() const { return value_; }
1209
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001210 virtual bool InstructionDataEquals(HInstruction* other) const {
1211 return other->AsLongConstant()->value_ == value_;
1212 }
1213
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001214 DECLARE_INSTRUCTION(LongConstant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001215
1216 private:
1217 const int64_t value_;
1218
1219 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
1220};
1221
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001222class HInvoke : public HInstruction {
1223 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001224 HInvoke(ArenaAllocator* arena,
1225 uint32_t number_of_arguments,
1226 Primitive::Type return_type,
1227 uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001228 : HInstruction(SideEffects::All()),
1229 inputs_(arena, number_of_arguments),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001230 return_type_(return_type),
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001231 dex_pc_(dex_pc) {
1232 inputs_.SetSize(number_of_arguments);
1233 }
1234
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001235 virtual size_t InputCount() const { return inputs_.Size(); }
1236 virtual HInstruction* InputAt(size_t i) const { return inputs_.Get(i); }
1237
1238 // Runtime needs to walk the stack, so Dex -> Dex calls need to
1239 // know their environment.
1240 virtual bool NeedsEnvironment() const { return true; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001241
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001242 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001243 SetRawInputAt(index, argument);
1244 }
1245
1246 virtual void SetRawInputAt(size_t index, HInstruction* input) {
1247 inputs_.Put(index, input);
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001248 }
1249
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001250 virtual Primitive::Type GetType() const { return return_type_; }
1251
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001252 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001253
1254 protected:
1255 GrowableArray<HInstruction*> inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001256 const Primitive::Type return_type_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001257 const uint32_t dex_pc_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001258
1259 private:
1260 DISALLOW_COPY_AND_ASSIGN(HInvoke);
1261};
1262
1263class HInvokeStatic : public HInvoke {
1264 public:
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001265 HInvokeStatic(ArenaAllocator* arena,
1266 uint32_t number_of_arguments,
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001267 Primitive::Type return_type,
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001268 uint32_t dex_pc,
1269 uint32_t index_in_dex_cache)
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001270 : HInvoke(arena, number_of_arguments, return_type, dex_pc),
1271 index_in_dex_cache_(index_in_dex_cache) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001272
1273 uint32_t GetIndexInDexCache() const { return index_in_dex_cache_; }
1274
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001275 DECLARE_INSTRUCTION(InvokeStatic);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001276
1277 private:
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001278 const uint32_t index_in_dex_cache_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001279
1280 DISALLOW_COPY_AND_ASSIGN(HInvokeStatic);
1281};
1282
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001283class HInvokeVirtual : public HInvoke {
1284 public:
1285 HInvokeVirtual(ArenaAllocator* arena,
1286 uint32_t number_of_arguments,
1287 Primitive::Type return_type,
1288 uint32_t dex_pc,
1289 uint32_t vtable_index)
1290 : HInvoke(arena, number_of_arguments, return_type, dex_pc),
1291 vtable_index_(vtable_index) {}
1292
1293 uint32_t GetVTableIndex() const { return vtable_index_; }
1294
1295 DECLARE_INSTRUCTION(InvokeVirtual);
1296
1297 private:
1298 const uint32_t vtable_index_;
1299
1300 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
1301};
1302
Dave Allison20dfc792014-06-16 20:44:29 -07001303class HNewInstance : public HExpression<0> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001304 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001305 HNewInstance(uint32_t dex_pc, uint16_t type_index)
1306 : HExpression(Primitive::kPrimNot, SideEffects::None()),
1307 dex_pc_(dex_pc),
1308 type_index_(type_index) {}
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001309
1310 uint32_t GetDexPc() const { return dex_pc_; }
1311 uint16_t GetTypeIndex() const { return type_index_; }
1312
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001313 // Calls runtime so needs an environment.
1314 virtual bool NeedsEnvironment() const { return true; }
1315
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001316 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001317
1318 private:
1319 const uint32_t dex_pc_;
1320 const uint16_t type_index_;
1321
1322 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
1323};
1324
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001325class HAdd : public HBinaryOperation {
1326 public:
1327 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1328 : HBinaryOperation(result_type, left, right) {}
1329
1330 virtual bool IsCommutative() { return true; }
1331
1332 DECLARE_INSTRUCTION(Add);
1333
1334 private:
1335 DISALLOW_COPY_AND_ASSIGN(HAdd);
1336};
1337
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001338class HSub : public HBinaryOperation {
1339 public:
1340 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1341 : HBinaryOperation(result_type, left, right) {}
1342
1343 virtual bool IsCommutative() { return false; }
1344
1345 DECLARE_INSTRUCTION(Sub);
1346
1347 private:
1348 DISALLOW_COPY_AND_ASSIGN(HSub);
1349};
1350
1351// The value of a parameter in this method. Its location depends on
1352// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07001353class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001354 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001355 HParameterValue(uint8_t index, Primitive::Type parameter_type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001356 : HExpression(parameter_type, SideEffects::None()), index_(index) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001357
1358 uint8_t GetIndex() const { return index_; }
1359
1360 DECLARE_INSTRUCTION(ParameterValue);
1361
1362 private:
1363 // The index of this parameter in the parameters list. Must be less
1364 // than HGraph::number_of_in_vregs_;
1365 const uint8_t index_;
1366
1367 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
1368};
1369
Dave Allison20dfc792014-06-16 20:44:29 -07001370class HNot : public HExpression<1> {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001371 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001372 explicit HNot(HInstruction* input) : HExpression(Primitive::kPrimBoolean, SideEffects::None()) {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001373 SetRawInputAt(0, input);
1374 }
1375
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001376 virtual bool CanBeMoved() const { return true; }
1377 virtual bool InstructionDataEquals(HInstruction* other) const { return true; }
1378
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001379 DECLARE_INSTRUCTION(Not);
1380
1381 private:
1382 DISALLOW_COPY_AND_ASSIGN(HNot);
1383};
1384
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001385class HPhi : public HInstruction {
1386 public:
1387 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001388 : HInstruction(SideEffects::None()),
1389 inputs_(arena, number_of_inputs),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001390 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001391 type_(type),
1392 is_live_(false) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001393 inputs_.SetSize(number_of_inputs);
1394 }
1395
1396 virtual size_t InputCount() const { return inputs_.Size(); }
1397 virtual HInstruction* InputAt(size_t i) const { return inputs_.Get(i); }
1398
1399 virtual void SetRawInputAt(size_t index, HInstruction* input) {
1400 inputs_.Put(index, input);
1401 }
1402
1403 void AddInput(HInstruction* input);
1404
1405 virtual Primitive::Type GetType() const { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001406 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001407
1408 uint32_t GetRegNumber() const { return reg_number_; }
1409
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001410 void SetDead() { is_live_ = false; }
1411 void SetLive() { is_live_ = true; }
1412 bool IsDead() const { return !is_live_; }
1413 bool IsLive() const { return is_live_; }
1414
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001415 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001416
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001417 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001418 GrowableArray<HInstruction*> inputs_;
1419 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001420 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001421 bool is_live_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001422
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001423 DISALLOW_COPY_AND_ASSIGN(HPhi);
1424};
1425
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001426class HNullCheck : public HExpression<1> {
1427 public:
1428 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001429 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001430 SetRawInputAt(0, value);
1431 }
1432
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001433 virtual bool CanBeMoved() const { return true; }
1434 virtual bool InstructionDataEquals(HInstruction* other) const { return true; }
1435
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001436 virtual bool NeedsEnvironment() const { return true; }
1437
1438 uint32_t GetDexPc() const { return dex_pc_; }
1439
1440 DECLARE_INSTRUCTION(NullCheck);
1441
1442 private:
1443 const uint32_t dex_pc_;
1444
1445 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
1446};
1447
1448class FieldInfo : public ValueObject {
1449 public:
Nicolas Geoffray39468442014-09-02 15:17:15 +01001450 explicit FieldInfo(MemberOffset field_offset, Primitive::Type field_type)
1451 : field_offset_(field_offset), field_type_(field_type) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001452
1453 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001454 Primitive::Type GetFieldType() const { return field_type_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001455
1456 private:
1457 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01001458 const Primitive::Type field_type_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001459};
1460
1461class HInstanceFieldGet : public HExpression<1> {
1462 public:
1463 HInstanceFieldGet(HInstruction* value,
1464 Primitive::Type field_type,
1465 MemberOffset field_offset)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001466 : HExpression(field_type, SideEffects::DependsOnSomething()),
1467 field_info_(field_offset, field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001468 SetRawInputAt(0, value);
1469 }
1470
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001471 virtual bool CanBeMoved() const { return true; }
1472 virtual bool InstructionDataEquals(HInstruction* other) const {
1473 size_t other_offset = other->AsInstanceFieldGet()->GetFieldOffset().SizeValue();
1474 return other_offset == GetFieldOffset().SizeValue();
1475 }
1476
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001477 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001478 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001479
1480 DECLARE_INSTRUCTION(InstanceFieldGet);
1481
1482 private:
1483 const FieldInfo field_info_;
1484
1485 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
1486};
1487
1488class HInstanceFieldSet : public HTemplateInstruction<2> {
1489 public:
1490 HInstanceFieldSet(HInstruction* object,
1491 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01001492 Primitive::Type field_type,
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001493 MemberOffset field_offset)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001494 : HTemplateInstruction(SideEffects::ChangesSomething()),
1495 field_info_(field_offset, field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001496 SetRawInputAt(0, object);
1497 SetRawInputAt(1, value);
1498 }
1499
1500 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001501 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001502
1503 DECLARE_INSTRUCTION(InstanceFieldSet);
1504
1505 private:
1506 const FieldInfo field_info_;
1507
1508 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
1509};
1510
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001511class HArrayGet : public HExpression<2> {
1512 public:
1513 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001514 : HExpression(type, SideEffects::DependsOnSomething()) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001515 SetRawInputAt(0, array);
1516 SetRawInputAt(1, index);
1517 }
1518
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001519 virtual bool CanBeMoved() const { return true; }
1520 virtual bool InstructionDataEquals(HInstruction* other) const { return true; }
1521
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001522 DECLARE_INSTRUCTION(ArrayGet);
1523
1524 private:
1525 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
1526};
1527
1528class HArraySet : public HTemplateInstruction<3> {
1529 public:
1530 HArraySet(HInstruction* array,
1531 HInstruction* index,
1532 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01001533 Primitive::Type component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001534 uint32_t dex_pc)
1535 : HTemplateInstruction(SideEffects::ChangesSomething()),
1536 dex_pc_(dex_pc),
1537 component_type_(component_type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001538 SetRawInputAt(0, array);
1539 SetRawInputAt(1, index);
1540 SetRawInputAt(2, value);
1541 }
1542
1543 virtual bool NeedsEnvironment() const {
1544 // We currently always call a runtime method to catch array store
1545 // exceptions.
1546 return InputAt(2)->GetType() == Primitive::kPrimNot;
1547 }
1548
1549 uint32_t GetDexPc() const { return dex_pc_; }
1550
Nicolas Geoffray39468442014-09-02 15:17:15 +01001551 Primitive::Type GetComponentType() const { return component_type_; }
1552
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001553 DECLARE_INSTRUCTION(ArraySet);
1554
1555 private:
1556 const uint32_t dex_pc_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01001557 const Primitive::Type component_type_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001558
1559 DISALLOW_COPY_AND_ASSIGN(HArraySet);
1560};
1561
1562class HArrayLength : public HExpression<1> {
1563 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001564 explicit HArrayLength(HInstruction* array)
1565 : HExpression(Primitive::kPrimInt, SideEffects::None()) {
1566 // Note that arrays do not change length, so the instruction does not
1567 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001568 SetRawInputAt(0, array);
1569 }
1570
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001571 virtual bool CanBeMoved() const { return true; }
1572 virtual bool InstructionDataEquals(HInstruction* other) const { return true; }
1573
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001574 DECLARE_INSTRUCTION(ArrayLength);
1575
1576 private:
1577 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
1578};
1579
1580class HBoundsCheck : public HExpression<2> {
1581 public:
1582 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001583 : HExpression(index->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001584 DCHECK(index->GetType() == Primitive::kPrimInt);
1585 SetRawInputAt(0, index);
1586 SetRawInputAt(1, length);
1587 }
1588
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001589 virtual bool CanBeMoved() const { return true; }
1590 virtual bool InstructionDataEquals(HInstruction* other) const { return true; }
1591
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001592 virtual bool NeedsEnvironment() const { return true; }
1593
1594 uint32_t GetDexPc() const { return dex_pc_; }
1595
1596 DECLARE_INSTRUCTION(BoundsCheck);
1597
1598 private:
1599 const uint32_t dex_pc_;
1600
1601 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
1602};
1603
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001604/**
1605 * Some DEX instructions are folded into multiple HInstructions that need
1606 * to stay live until the last HInstruction. This class
1607 * is used as a marker for the baseline compiler to ensure its preceding
1608 * HInstruction stays live. `index` is the temporary number that is used
1609 * for knowing the stack offset where to store the instruction.
1610 */
1611class HTemporary : public HTemplateInstruction<0> {
1612 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001613 explicit HTemporary(size_t index) : HTemplateInstruction(SideEffects::None()), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001614
1615 size_t GetIndex() const { return index_; }
1616
1617 DECLARE_INSTRUCTION(Temporary);
1618
1619 private:
1620 const size_t index_;
1621
1622 DISALLOW_COPY_AND_ASSIGN(HTemporary);
1623};
1624
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001625class HSuspendCheck : public HTemplateInstruction<0> {
1626 public:
1627 explicit HSuspendCheck(uint32_t dex_pc)
1628 : HTemplateInstruction(SideEffects::ChangesSomething()), dex_pc_(dex_pc) {}
1629
1630 virtual bool NeedsEnvironment() const {
1631 return true;
1632 }
1633
1634 uint32_t GetDexPc() const { return dex_pc_; }
1635
1636 DECLARE_INSTRUCTION(SuspendCheck);
1637
1638 private:
1639 const uint32_t dex_pc_;
1640
1641 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
1642};
1643
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001644class MoveOperands : public ArenaObject {
1645 public:
1646 MoveOperands(Location source, Location destination)
1647 : source_(source), destination_(destination) {}
1648
1649 Location GetSource() const { return source_; }
1650 Location GetDestination() const { return destination_; }
1651
1652 void SetSource(Location value) { source_ = value; }
1653 void SetDestination(Location value) { destination_ = value; }
1654
1655 // The parallel move resolver marks moves as "in-progress" by clearing the
1656 // destination (but not the source).
1657 Location MarkPending() {
1658 DCHECK(!IsPending());
1659 Location dest = destination_;
1660 destination_ = Location::NoLocation();
1661 return dest;
1662 }
1663
1664 void ClearPending(Location dest) {
1665 DCHECK(IsPending());
1666 destination_ = dest;
1667 }
1668
1669 bool IsPending() const {
1670 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
1671 return destination_.IsInvalid() && !source_.IsInvalid();
1672 }
1673
1674 // True if this blocks a move from the given location.
1675 bool Blocks(Location loc) const {
1676 return !IsEliminated() && source_.Equals(loc);
1677 }
1678
1679 // A move is redundant if it's been eliminated, if its source and
1680 // destination are the same, or if its destination is unneeded.
1681 bool IsRedundant() const {
1682 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
1683 }
1684
1685 // We clear both operands to indicate move that's been eliminated.
1686 void Eliminate() {
1687 source_ = destination_ = Location::NoLocation();
1688 }
1689
1690 bool IsEliminated() const {
1691 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
1692 return source_.IsInvalid();
1693 }
1694
1695 private:
1696 Location source_;
1697 Location destination_;
1698
1699 DISALLOW_COPY_AND_ASSIGN(MoveOperands);
1700};
1701
1702static constexpr size_t kDefaultNumberOfMoves = 4;
1703
1704class HParallelMove : public HTemplateInstruction<0> {
1705 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001706 explicit HParallelMove(ArenaAllocator* arena)
1707 : HTemplateInstruction(SideEffects::None()), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001708
1709 void AddMove(MoveOperands* move) {
1710 moves_.Add(move);
1711 }
1712
1713 MoveOperands* MoveOperandsAt(size_t index) const {
1714 return moves_.Get(index);
1715 }
1716
1717 size_t NumMoves() const { return moves_.Size(); }
1718
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001719 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001720
1721 private:
1722 GrowableArray<MoveOperands*> moves_;
1723
1724 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
1725};
1726
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001727class HGraphVisitor : public ValueObject {
1728 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001729 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
1730 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001731
Dave Allison20dfc792014-06-16 20:44:29 -07001732 virtual void VisitInstruction(HInstruction* instruction) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001733 virtual void VisitBasicBlock(HBasicBlock* block);
1734
1735 void VisitInsertionOrder();
1736
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001737 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001738
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001739 // Visit functions for instruction classes.
1740#define DECLARE_VISIT_INSTRUCTION(name) \
1741 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
1742
1743 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
1744
1745#undef DECLARE_VISIT_INSTRUCTION
1746
1747 private:
1748 HGraph* graph_;
1749
1750 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
1751};
1752
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001753class HInsertionOrderIterator : public ValueObject {
1754 public:
1755 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
1756
1757 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
1758 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
1759 void Advance() { ++index_; }
1760
1761 private:
1762 const HGraph& graph_;
1763 size_t index_;
1764
1765 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
1766};
1767
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001768class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001769 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001770 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001771
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001772 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
1773 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001774 void Advance() { ++index_; }
1775
1776 private:
1777 const HGraph& graph_;
1778 size_t index_;
1779
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001780 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001781};
1782
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001783class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001784 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001785 explicit HPostOrderIterator(const HGraph& graph)
1786 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001787
1788 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001789 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001790 void Advance() { --index_; }
1791
1792 private:
1793 const HGraph& graph_;
1794 size_t index_;
1795
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001796 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001797};
1798
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001799} // namespace art
1800
1801#endif // ART_COMPILER_OPTIMIZING_NODES_H_