blob: 503f31d990f72ef5af6445c62f90eea8f446f28c [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 Geoffray818f2102014-02-18 16:43:35 +000021#include "utils/allocation.h"
Nicolas Geoffray0e336432014-02-26 18:24:38 +000022#include "utils/arena_bit_vector.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000023#include "utils/growable_array.h"
24
25namespace art {
26
27class HBasicBlock;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010028class HEnvironment;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000029class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000030class HIntConstant;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000031class HGraphVisitor;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010032class HPhi;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010033class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000034class LocationSummary;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000035
36static const int kDefaultNumberOfBlocks = 8;
37static const int kDefaultNumberOfSuccessors = 2;
38static const int kDefaultNumberOfPredecessors = 2;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000039static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000040
Dave Allison20dfc792014-06-16 20:44:29 -070041enum IfCondition {
42 kCondEQ,
43 kCondNE,
44 kCondLT,
45 kCondLE,
46 kCondGT,
47 kCondGE,
48};
49
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010050class HInstructionList {
51 public:
52 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
53
54 void AddInstruction(HInstruction* instruction);
55 void RemoveInstruction(HInstruction* instruction);
56
57 private:
58 HInstruction* first_instruction_;
59 HInstruction* last_instruction_;
60
61 friend class HBasicBlock;
62 friend class HInstructionIterator;
Nicolas Geoffray804d0932014-05-02 08:46:00 +010063 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010064
65 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
66};
67
Nicolas Geoffray818f2102014-02-18 16:43:35 +000068// Control-flow graph of a method. Contains a list of basic blocks.
69class HGraph : public ArenaObject {
70 public:
71 explicit HGraph(ArenaAllocator* arena)
72 : arena_(arena),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000073 blocks_(arena, kDefaultNumberOfBlocks),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +010074 reverse_post_order_(arena, kDefaultNumberOfBlocks),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +010075 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +010076 number_of_vregs_(0),
77 number_of_in_vregs_(0),
Dave Allison20dfc792014-06-16 20:44:29 -070078 current_instruction_id_(0) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +000079
Nicolas Geoffray787c3072014-03-17 10:20:19 +000080 ArenaAllocator* GetArena() const { return arena_; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +010081 const GrowableArray<HBasicBlock*>& GetBlocks() const { return blocks_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +000082
Nicolas Geoffray787c3072014-03-17 10:20:19 +000083 HBasicBlock* GetEntryBlock() const { return entry_block_; }
84 HBasicBlock* GetExitBlock() const { return exit_block_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000085
Nicolas Geoffray787c3072014-03-17 10:20:19 +000086 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
87 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000088
Nicolas Geoffray818f2102014-02-18 16:43:35 +000089 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010090
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000091 void BuildDominatorTree();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010092 void TransformToSSA();
93 void SimplifyCFG();
Nicolas Geoffray818f2102014-02-18 16:43:35 +000094
Nicolas Geoffray622d9c32014-05-12 16:11:02 +010095 // Find all natural loops in this graph. Aborts computation and returns false
Nicolas Geoffrayf635e632014-05-14 09:43:38 +010096 // if one loop is not natural, that is the header does not dominate the back
Nicolas Geoffray622d9c32014-05-12 16:11:02 +010097 // edge.
98 bool FindNaturalLoops() const;
99
100 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
101 void SimplifyLoop(HBasicBlock* header);
102
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000103 int GetNextInstructionId() {
104 return current_instruction_id_++;
105 }
106
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100107 uint16_t GetMaximumNumberOfOutVRegs() const {
108 return maximum_number_of_out_vregs_;
109 }
110
111 void UpdateMaximumNumberOfOutVRegs(uint16_t new_value) {
112 maximum_number_of_out_vregs_ = std::max(new_value, maximum_number_of_out_vregs_);
113 }
114
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100115 void SetNumberOfVRegs(uint16_t number_of_vregs) {
116 number_of_vregs_ = number_of_vregs;
117 }
118
119 uint16_t GetNumberOfVRegs() const {
120 return number_of_vregs_;
121 }
122
123 void SetNumberOfInVRegs(uint16_t value) {
124 number_of_in_vregs_ = value;
125 }
126
127 uint16_t GetNumberOfInVRegs() const {
128 return number_of_in_vregs_;
129 }
130
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100131 const GrowableArray<HBasicBlock*>& GetReversePostOrder() const {
132 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100133 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100134
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000135 private:
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000136 HBasicBlock* FindCommonDominator(HBasicBlock* first, HBasicBlock* second) const;
137 void VisitBlockForDominatorTree(HBasicBlock* block,
138 HBasicBlock* predecessor,
139 GrowableArray<size_t>* visits);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100140 void FindBackEdges(ArenaBitVector* visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000141 void VisitBlockForBackEdges(HBasicBlock* block,
142 ArenaBitVector* visited,
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100143 ArenaBitVector* visiting);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000144 void RemoveDeadBlocks(const ArenaBitVector& visited) const;
145
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000146 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000147
148 // List of blocks in insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000149 GrowableArray<HBasicBlock*> blocks_;
150
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100151 // List of blocks to perform a reverse post order tree traversal.
152 GrowableArray<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000153
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000154 HBasicBlock* entry_block_;
155 HBasicBlock* exit_block_;
156
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100157 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100158 uint16_t maximum_number_of_out_vregs_;
159
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100160 // The number of virtual registers in this method. Contains the parameters.
161 uint16_t number_of_vregs_;
162
163 // The number of virtual registers used by parameters of this method.
164 uint16_t number_of_in_vregs_;
165
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000166 // The current id to assign to a newly added instruction. See HInstruction.id_.
167 int current_instruction_id_;
168
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000169 DISALLOW_COPY_AND_ASSIGN(HGraph);
170};
171
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000172class HLoopInformation : public ArenaObject {
173 public:
174 HLoopInformation(HBasicBlock* header, HGraph* graph)
175 : header_(header),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100176 back_edges_(graph->GetArena(), kDefaultNumberOfBackEdges),
177 blocks_(graph->GetArena(), graph->GetBlocks().Size(), false) {}
178
179 HBasicBlock* GetHeader() const {
180 return header_;
181 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000182
183 void AddBackEdge(HBasicBlock* back_edge) {
184 back_edges_.Add(back_edge);
185 }
186
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100187 void RemoveBackEdge(HBasicBlock* back_edge) {
188 back_edges_.Delete(back_edge);
189 }
190
191 bool IsBackEdge(HBasicBlock* block) {
192 for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) {
193 if (back_edges_.Get(i) == block) return true;
194 }
195 return false;
196 }
197
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000198 int NumberOfBackEdges() const {
199 return back_edges_.Size();
200 }
201
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100202 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100203
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100204 const GrowableArray<HBasicBlock*>& GetBackEdges() const {
205 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100206 }
207
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100208 void ClearBackEdges() {
209 back_edges_.Reset();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100210 }
211
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100212 // Find blocks that are part of this loop. Returns whether the loop is a natural loop,
213 // that is the header dominates the back edge.
214 bool Populate();
215
216 // Returns whether this loop information contains `block`.
217 // Note that this loop information *must* be populated before entering this function.
218 bool Contains(const HBasicBlock& block) const;
219
220 // Returns whether this loop information is an inner loop of `other`.
221 // Note that `other` *must* be populated before entering this function.
222 bool IsIn(const HLoopInformation& other) const;
223
224 const ArenaBitVector& GetBlocks() const { return blocks_; }
225
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000226 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100227 // Internal recursive implementation of `Populate`.
228 void PopulateRecursive(HBasicBlock* block);
229
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000230 HBasicBlock* header_;
231 GrowableArray<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100232 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000233
234 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
235};
236
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100237static constexpr size_t kNoLifetime = -1;
238
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000239// A block in a method. Contains the list of instructions represented
240// as a double linked list. Each block knows its predecessors and
241// successors.
242class HBasicBlock : public ArenaObject {
243 public:
244 explicit HBasicBlock(HGraph* graph)
245 : graph_(graph),
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000246 predecessors_(graph->GetArena(), kDefaultNumberOfPredecessors),
247 successors_(graph->GetArena(), kDefaultNumberOfSuccessors),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000248 loop_information_(nullptr),
249 dominator_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100250 block_id_(-1),
251 lifetime_start_(kNoLifetime),
252 lifetime_end_(kNoLifetime) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000253
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100254 const GrowableArray<HBasicBlock*>& GetPredecessors() const {
255 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000256 }
257
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100258 const GrowableArray<HBasicBlock*>& GetSuccessors() const {
259 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000260 }
261
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000262 void AddBackEdge(HBasicBlock* back_edge) {
263 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000264 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000265 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100266 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000267 loop_information_->AddBackEdge(back_edge);
268 }
269
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000270 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000271
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000272 int GetBlockId() const { return block_id_; }
273 void SetBlockId(int id) { block_id_ = id; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000274
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000275 HBasicBlock* GetDominator() const { return dominator_; }
276 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000277
278 int NumberOfBackEdges() const {
279 return loop_information_ == nullptr
280 ? 0
281 : loop_information_->NumberOfBackEdges();
282 }
283
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100284 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
285 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100286 const HInstructionList& GetInstructions() const { return instructions_; }
287 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100288 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000289
290 void AddSuccessor(HBasicBlock* block) {
291 successors_.Add(block);
292 block->predecessors_.Add(this);
293 }
294
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100295 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
296 size_t successor_index = GetSuccessorIndexOf(existing);
297 DCHECK_NE(successor_index, static_cast<size_t>(-1));
298 existing->RemovePredecessor(this);
299 new_block->predecessors_.Add(this);
300 successors_.Put(successor_index, new_block);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000301 }
302
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100303 void RemovePredecessor(HBasicBlock* block) {
304 predecessors_.Delete(block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100305 }
306
307 void ClearAllPredecessors() {
308 predecessors_.Reset();
309 }
310
311 void AddPredecessor(HBasicBlock* block) {
312 predecessors_.Add(block);
313 block->successors_.Add(this);
314 }
315
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100316 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) {
317 for (size_t i = 0, e = predecessors_.Size(); i < e; ++i) {
318 if (predecessors_.Get(i) == predecessor) {
319 return i;
320 }
321 }
322 return -1;
323 }
324
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100325 size_t GetSuccessorIndexOf(HBasicBlock* successor) {
326 for (size_t i = 0, e = successors_.Size(); i < e; ++i) {
327 if (successors_.Get(i) == successor) {
328 return i;
329 }
330 }
331 return -1;
332 }
333
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000334 void AddInstruction(HInstruction* instruction);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100335 void RemoveInstruction(HInstruction* instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100336 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100337 void AddPhi(HPhi* phi);
338 void RemovePhi(HPhi* phi);
339
340 bool IsLoopHeader() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100341 return (loop_information_ != nullptr) && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100342 }
343
344 HLoopInformation* GetLoopInformation() const {
345 return loop_information_;
346 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000347
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100348 // Set the loop_information_ on this block. This method overrides the current
349 // loop_information if it is an outer loop of the passed loop information.
350 void SetInLoop(HLoopInformation* info) {
351 if (IsLoopHeader()) {
352 // Nothing to do. This just means `info` is an outer loop.
353 } else if (loop_information_ == nullptr) {
354 loop_information_ = info;
355 } else if (loop_information_->Contains(*info->GetHeader())) {
356 // Block is currently part of an outer loop. Make it part of this inner loop.
357 // Note that a non loop header having a loop information means this loop information
358 // has already been populated
359 loop_information_ = info;
360 } else {
361 // Block is part of an inner loop. Do not update the loop information.
362 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
363 // at this point, because this method is being called while populating `info`.
364 }
365 }
366
367 // Returns wheter this block dominates the blocked passed as parameter.
368 bool Dominates(HBasicBlock* block) const;
369
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100370 size_t GetLifetimeStart() const { return lifetime_start_; }
371 size_t GetLifetimeEnd() const { return lifetime_end_; }
372
373 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
374 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
375
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000376 private:
377 HGraph* const graph_;
378 GrowableArray<HBasicBlock*> predecessors_;
379 GrowableArray<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100380 HInstructionList instructions_;
381 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000382 HLoopInformation* loop_information_;
383 HBasicBlock* dominator_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000384 int block_id_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100385 size_t lifetime_start_;
386 size_t lifetime_end_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000387
388 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
389};
390
391#define FOR_EACH_INSTRUCTION(M) \
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000392 M(Add) \
Dave Allison20dfc792014-06-16 20:44:29 -0700393 M(Condition) \
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000394 M(Equal) \
Dave Allison20dfc792014-06-16 20:44:29 -0700395 M(NotEqual) \
396 M(LessThan) \
397 M(LessThanOrEqual) \
398 M(GreaterThan) \
399 M(GreaterThanOrEqual) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000400 M(Exit) \
401 M(Goto) \
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000402 M(If) \
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000403 M(IntConstant) \
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000404 M(InvokeStatic) \
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000405 M(LoadLocal) \
406 M(Local) \
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100407 M(LongConstant) \
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +0100408 M(NewInstance) \
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100409 M(Not) \
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100410 M(ParameterValue) \
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100411 M(ParallelMove) \
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100412 M(Phi) \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000413 M(Return) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000414 M(ReturnVoid) \
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000415 M(StoreLocal) \
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100416 M(Sub) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000417
Dave Allison20dfc792014-06-16 20:44:29 -0700418
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000419#define FORWARD_DECLARATION(type) class H##type;
420FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
421#undef FORWARD_DECLARATION
422
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000423#define DECLARE_INSTRUCTION(type) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000424 virtual const char* DebugName() const { return #type; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000425 virtual H##type* As##type() { return this; } \
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100426 virtual void Accept(HGraphVisitor* visitor) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000427
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100428template <typename T>
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000429class HUseListNode : public ArenaObject {
430 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100431 HUseListNode(T* user, size_t index, HUseListNode* tail)
Dave Allison20dfc792014-06-16 20:44:29 -0700432 : user_(user), index_(index), tail_(tail) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000433
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000434 HUseListNode* GetTail() const { return tail_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100435 T* GetUser() const { return user_; }
436 size_t GetIndex() const { return index_; }
437
438 void SetTail(HUseListNode<T>* node) { tail_ = node; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000439
440 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100441 T* const user_;
442 const size_t index_;
443 HUseListNode<T>* tail_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000444
445 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
446};
447
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000448class HInstruction : public ArenaObject {
449 public:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000450 HInstruction()
451 : previous_(nullptr),
452 next_(nullptr),
453 block_(nullptr),
454 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100455 ssa_index_(-1),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000456 uses_(nullptr),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100457 env_uses_(nullptr),
458 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100459 locations_(nullptr),
460 live_interval_(nullptr),
461 lifetime_position_(kNoLifetime) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000462
Dave Allison20dfc792014-06-16 20:44:29 -0700463 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000464
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000465 HInstruction* GetNext() const { return next_; }
466 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000467
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000468 HBasicBlock* GetBlock() const { return block_; }
469 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000470
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100471 virtual size_t InputCount() const = 0;
472 virtual HInstruction* InputAt(size_t i) const = 0;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000473
474 virtual void Accept(HGraphVisitor* visitor) = 0;
475 virtual const char* DebugName() const = 0;
476
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100477 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100478 virtual void SetRawInputAt(size_t index, HInstruction* input) = 0;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100479
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100480 virtual bool NeedsEnvironment() const { return false; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100481 virtual bool IsControlFlow() const { return false; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100482
483 void AddUseAt(HInstruction* user, size_t index) {
484 uses_ = new (block_->GetGraph()->GetArena()) HUseListNode<HInstruction>(user, index, uses_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000485 }
486
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100487 void AddEnvUseAt(HEnvironment* user, size_t index) {
488 env_uses_ = new (block_->GetGraph()->GetArena()) HUseListNode<HEnvironment>(
489 user, index, env_uses_);
490 }
491
492 void RemoveUser(HInstruction* user, size_t index);
493
494 HUseListNode<HInstruction>* GetUses() const { return uses_; }
495 HUseListNode<HEnvironment>* GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000496
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100497 bool HasUses() const { return uses_ != nullptr || env_uses_ != nullptr; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000498
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100499 size_t NumberOfUses() const {
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100500 // TODO: Optimize this method if it is used outside of the HGraphVisualizer.
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100501 size_t result = 0;
502 HUseListNode<HInstruction>* current = uses_;
503 while (current != nullptr) {
504 current = current->GetTail();
505 ++result;
506 }
507 return result;
508 }
509
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000510 int GetId() const { return id_; }
511 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000512
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100513 int GetSsaIndex() const { return ssa_index_; }
514 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
515 bool HasSsaIndex() const { return ssa_index_ != -1; }
516
517 bool HasEnvironment() const { return environment_ != nullptr; }
518 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100519 void SetEnvironment(HEnvironment* environment) { environment_ = environment; }
520
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000521 LocationSummary* GetLocations() const { return locations_; }
522 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000523
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100524 void ReplaceWith(HInstruction* instruction);
525
Dave Allison20dfc792014-06-16 20:44:29 -0700526 bool HasOnlyOneUse() const {
527 return uses_ != nullptr && uses_->GetTail() == nullptr;
528 }
529
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000530#define INSTRUCTION_TYPE_CHECK(type) \
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100531 bool Is##type() { return (As##type() != nullptr); } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000532 virtual H##type* As##type() { return nullptr; }
533
534 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
535#undef INSTRUCTION_TYPE_CHECK
536
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100537 size_t GetLifetimePosition() const { return lifetime_position_; }
538 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
539 LiveInterval* GetLiveInterval() const { return live_interval_; }
540 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
541 bool HasLiveInterval() const { return live_interval_ != nullptr; }
542
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000543 private:
544 HInstruction* previous_;
545 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000546 HBasicBlock* block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000547
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000548 // An instruction gets an id when it is added to the graph.
549 // It reflects creation order. A negative id means the instruction
550 // has not beed added to the graph.
551 int id_;
552
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100553 // When doing liveness analysis, instructions that have uses get an SSA index.
554 int ssa_index_;
555
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100556 // List of instructions that have this instruction as input.
557 HUseListNode<HInstruction>* uses_;
558
559 // List of environments that contain this instruction.
560 HUseListNode<HEnvironment>* env_uses_;
561
562 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000563
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000564 // Set by the code generator.
565 LocationSummary* locations_;
566
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100567 // Set by the liveness analysis.
568 LiveInterval* live_interval_;
569
570 // Set by the liveness analysis, this is the position in a linear
571 // order of blocks where this instruction's live interval start.
572 size_t lifetime_position_;
573
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000574 friend class HBasicBlock;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100575 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000576
577 DISALLOW_COPY_AND_ASSIGN(HInstruction);
578};
579
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100580template<typename T>
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000581class HUseIterator : public ValueObject {
582 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100583 explicit HUseIterator(HUseListNode<T>* uses) : current_(uses) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000584
585 bool Done() const { return current_ == nullptr; }
586
587 void Advance() {
588 DCHECK(!Done());
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000589 current_ = current_->GetTail();
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000590 }
591
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100592 HUseListNode<T>* Current() const {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000593 DCHECK(!Done());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100594 return current_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000595 }
596
597 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100598 HUseListNode<T>* current_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000599
600 friend class HValue;
601};
602
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100603// A HEnvironment object contains the values of virtual registers at a given location.
604class HEnvironment : public ArenaObject {
605 public:
606 HEnvironment(ArenaAllocator* arena, size_t number_of_vregs) : vregs_(arena, number_of_vregs) {
607 vregs_.SetSize(number_of_vregs);
608 for (size_t i = 0; i < number_of_vregs; i++) {
609 vregs_.Put(i, nullptr);
610 }
611 }
612
613 void Populate(const GrowableArray<HInstruction*>& env) {
614 for (size_t i = 0; i < env.Size(); i++) {
615 HInstruction* instruction = env.Get(i);
616 vregs_.Put(i, instruction);
617 if (instruction != nullptr) {
618 instruction->AddEnvUseAt(this, i);
619 }
620 }
621 }
622
623 void SetRawEnvAt(size_t index, HInstruction* instruction) {
624 vregs_.Put(index, instruction);
625 }
626
627 GrowableArray<HInstruction*>* GetVRegs() {
628 return &vregs_;
629 }
630
631 private:
632 GrowableArray<HInstruction*> vregs_;
633
634 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
635};
636
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000637class HInputIterator : public ValueObject {
638 public:
Dave Allison20dfc792014-06-16 20:44:29 -0700639 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000640
641 bool Done() const { return index_ == instruction_->InputCount(); }
642 HInstruction* Current() const { return instruction_->InputAt(index_); }
643 void Advance() { index_++; }
644
645 private:
646 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100647 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000648
649 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
650};
651
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000652class HInstructionIterator : public ValueObject {
653 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100654 explicit HInstructionIterator(const HInstructionList& instructions)
655 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000656 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000657 }
658
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000659 bool Done() const { return instruction_ == nullptr; }
660 HInstruction* Current() const { return instruction_; }
661 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000662 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000663 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000664 }
665
666 private:
667 HInstruction* instruction_;
668 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100669
670 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000671};
672
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100673class HBackwardInstructionIterator : public ValueObject {
674 public:
675 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
676 : instruction_(instructions.last_instruction_) {
677 next_ = Done() ? nullptr : instruction_->GetPrevious();
678 }
679
680 bool Done() const { return instruction_ == nullptr; }
681 HInstruction* Current() const { return instruction_; }
682 void Advance() {
683 instruction_ = next_;
684 next_ = Done() ? nullptr : instruction_->GetPrevious();
685 }
686
687 private:
688 HInstruction* instruction_;
689 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100690
691 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100692};
693
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000694// An embedded container with N elements of type T. Used (with partial
695// specialization for N=0) because embedded arrays cannot have size 0.
696template<typename T, intptr_t N>
697class EmbeddedArray {
698 public:
Dave Allison20dfc792014-06-16 20:44:29 -0700699 EmbeddedArray() : elements_() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000700
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000701 intptr_t GetLength() const { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000702
703 const T& operator[](intptr_t i) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000704 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000705 return elements_[i];
706 }
707
708 T& operator[](intptr_t i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000709 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000710 return elements_[i];
711 }
712
713 const T& At(intptr_t i) const {
714 return (*this)[i];
715 }
716
717 void SetAt(intptr_t i, const T& val) {
718 (*this)[i] = val;
719 }
720
721 private:
722 T elements_[N];
723};
724
725template<typename T>
726class EmbeddedArray<T, 0> {
727 public:
728 intptr_t length() const { return 0; }
729 const T& operator[](intptr_t i) const {
730 LOG(FATAL) << "Unreachable";
731 static T sentinel = 0;
732 return sentinel;
733 }
734 T& operator[](intptr_t i) {
735 LOG(FATAL) << "Unreachable";
736 static T sentinel = 0;
737 return sentinel;
738 }
739};
740
741template<intptr_t N>
742class HTemplateInstruction: public HInstruction {
743 public:
Dave Allison20dfc792014-06-16 20:44:29 -0700744 HTemplateInstruction<N>() : inputs_() {}
745 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000746
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100747 virtual size_t InputCount() const { return N; }
748 virtual HInstruction* InputAt(size_t i) const { return inputs_[i]; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000749
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000750 protected:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100751 virtual void SetRawInputAt(size_t i, HInstruction* instruction) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000752 inputs_[i] = instruction;
753 }
754
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000755 private:
756 EmbeddedArray<HInstruction*, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100757
758 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000759};
760
Dave Allison20dfc792014-06-16 20:44:29 -0700761template<intptr_t N>
762class HExpression: public HTemplateInstruction<N> {
763 public:
764 explicit HExpression<N>(Primitive::Type type) : type_(type) {}
765 virtual ~HExpression() {}
766
767 virtual Primitive::Type GetType() const { return type_; }
768
769 private:
770 const Primitive::Type type_;
771};
772
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000773// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
774// instruction that branches to the exit block.
775class HReturnVoid : public HTemplateInstruction<0> {
776 public:
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100777 HReturnVoid() {}
778
779 virtual bool IsControlFlow() const { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000780
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100781 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000782
783 private:
784 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
785};
786
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000787// Represents dex's RETURN opcodes. A HReturn is a control flow
788// instruction that branches to the exit block.
789class HReturn : public HTemplateInstruction<1> {
790 public:
791 explicit HReturn(HInstruction* value) {
792 SetRawInputAt(0, value);
793 }
794
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100795 virtual bool IsControlFlow() const { return true; }
796
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100797 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000798
799 private:
800 DISALLOW_COPY_AND_ASSIGN(HReturn);
801};
802
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000803// The exit instruction is the only instruction of the exit block.
804// Instructions aborting the method (HTrow and HReturn) must branch to the
805// exit block.
806class HExit : public HTemplateInstruction<0> {
807 public:
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100808 HExit() {}
809
810 virtual bool IsControlFlow() const { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000811
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100812 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000813
814 private:
815 DISALLOW_COPY_AND_ASSIGN(HExit);
816};
817
818// Jumps from one block to another.
819class HGoto : public HTemplateInstruction<0> {
820 public:
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100821 HGoto() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000822
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000823 HBasicBlock* GetSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100824 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000825 }
826
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100827 virtual bool IsControlFlow() const { return true; }
828
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100829 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000830
831 private:
832 DISALLOW_COPY_AND_ASSIGN(HGoto);
833};
834
Dave Allison20dfc792014-06-16 20:44:29 -0700835
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000836// Conditional branch. A block ending with an HIf instruction must have
837// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000838class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000839 public:
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000840 explicit HIf(HInstruction* input) {
841 SetRawInputAt(0, input);
842 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000843
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000844 HBasicBlock* IfTrueSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100845 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000846 }
847
848 HBasicBlock* IfFalseSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100849 return GetBlock()->GetSuccessors().Get(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000850 }
851
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100852 virtual bool IsControlFlow() const { return true; }
853
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100854 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000855
Dave Allison20dfc792014-06-16 20:44:29 -0700856 virtual bool IsIfInstruction() const { return true; }
857
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000858 private:
859 DISALLOW_COPY_AND_ASSIGN(HIf);
860};
861
Dave Allison20dfc792014-06-16 20:44:29 -0700862class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000863 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000864 HBinaryOperation(Primitive::Type result_type,
865 HInstruction* left,
Dave Allison20dfc792014-06-16 20:44:29 -0700866 HInstruction* right) : HExpression(result_type) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000867 SetRawInputAt(0, left);
868 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000869 }
870
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000871 HInstruction* GetLeft() const { return InputAt(0); }
872 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -0700873 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000874
875 virtual bool IsCommutative() { return false; }
876
877 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000878 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
879};
880
Dave Allison20dfc792014-06-16 20:44:29 -0700881class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000882 public:
Dave Allison20dfc792014-06-16 20:44:29 -0700883 HCondition(HInstruction* first, HInstruction* second)
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000884 : HBinaryOperation(Primitive::kPrimBoolean, first, second) {}
885
886 virtual bool IsCommutative() { return true; }
Dave Allison20dfc792014-06-16 20:44:29 -0700887 bool NeedsMaterialization() const;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000888
Dave Allison20dfc792014-06-16 20:44:29 -0700889 DECLARE_INSTRUCTION(Condition);
890
891 virtual IfCondition GetCondition() const = 0;
892
893 private:
894 DISALLOW_COPY_AND_ASSIGN(HCondition);
895};
896
897// Instruction to check if two inputs are equal to each other.
898class HEqual : public HCondition {
899 public:
900 HEqual(HInstruction* first, HInstruction* second)
901 : HCondition(first, second) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100902
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100903 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000904
Dave Allison20dfc792014-06-16 20:44:29 -0700905 virtual IfCondition GetCondition() const {
906 return kCondEQ;
907 }
908
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000909 private:
910 DISALLOW_COPY_AND_ASSIGN(HEqual);
911};
912
Dave Allison20dfc792014-06-16 20:44:29 -0700913class HNotEqual : public HCondition {
914 public:
915 HNotEqual(HInstruction* first, HInstruction* second)
916 : HCondition(first, second) {}
917
918 DECLARE_INSTRUCTION(NotEqual);
919
920 virtual IfCondition GetCondition() const {
921 return kCondNE;
922 }
923
924 private:
925 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
926};
927
928class HLessThan : public HCondition {
929 public:
930 HLessThan(HInstruction* first, HInstruction* second)
931 : HCondition(first, second) {}
932
933 DECLARE_INSTRUCTION(LessThan);
934
935 virtual IfCondition GetCondition() const {
936 return kCondLT;
937 }
938
939 private:
940 DISALLOW_COPY_AND_ASSIGN(HLessThan);
941};
942
943class HLessThanOrEqual : public HCondition {
944 public:
945 HLessThanOrEqual(HInstruction* first, HInstruction* second)
946 : HCondition(first, second) {}
947
948 DECLARE_INSTRUCTION(LessThanOrEqual);
949
950 virtual IfCondition GetCondition() const {
951 return kCondLE;
952 }
953
954 private:
955 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
956};
957
958class HGreaterThan : public HCondition {
959 public:
960 HGreaterThan(HInstruction* first, HInstruction* second)
961 : HCondition(first, second) {}
962
963 DECLARE_INSTRUCTION(GreaterThan);
964
965 virtual IfCondition GetCondition() const {
966 return kCondGT;
967 }
968
969 private:
970 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
971};
972
973class HGreaterThanOrEqual : public HCondition {
974 public:
975 HGreaterThanOrEqual(HInstruction* first, HInstruction* second)
976 : HCondition(first, second) {}
977
978 DECLARE_INSTRUCTION(GreaterThanOrEqual);
979
980 virtual IfCondition GetCondition() const {
981 return kCondGE;
982 }
983
984 private:
985 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
986};
987
988
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000989// A local in the graph. Corresponds to a Dex register.
990class HLocal : public HTemplateInstruction<0> {
991 public:
Dave Allison20dfc792014-06-16 20:44:29 -0700992 explicit HLocal(uint16_t reg_number) : reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000993
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100994 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000995
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000996 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000997
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000998 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000999 // The Dex register number.
1000 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001001
1002 DISALLOW_COPY_AND_ASSIGN(HLocal);
1003};
1004
1005// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07001006class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001007 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001008 explicit HLoadLocal(HLocal* local, Primitive::Type type) : HExpression(type) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001009 SetRawInputAt(0, local);
1010 }
1011
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001012 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1013
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001014 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001015
1016 private:
1017 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
1018};
1019
1020// Store a value in a given local. This instruction has two inputs: the value
1021// and the local.
1022class HStoreLocal : public HTemplateInstruction<2> {
1023 public:
1024 HStoreLocal(HLocal* local, HInstruction* value) {
1025 SetRawInputAt(0, local);
1026 SetRawInputAt(1, value);
1027 }
1028
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001029 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1030
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001031 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001032
1033 private:
1034 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
1035};
1036
1037// Constants of the type int. Those can be from Dex instructions, or
1038// synthesized (for example with the if-eqz instruction).
Dave Allison20dfc792014-06-16 20:44:29 -07001039class HIntConstant : public HExpression<0> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001040 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001041 explicit HIntConstant(int32_t value) : HExpression(Primitive::kPrimInt), value_(value) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001042
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001043 int32_t GetValue() const { return value_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001044
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001045 DECLARE_INSTRUCTION(IntConstant);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001046
1047 private:
1048 const int32_t value_;
1049
1050 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
1051};
1052
Dave Allison20dfc792014-06-16 20:44:29 -07001053class HLongConstant : public HExpression<0> {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001054 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001055 explicit HLongConstant(int64_t value) : HExpression(Primitive::kPrimLong), value_(value) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001056
1057 int64_t GetValue() const { return value_; }
1058
1059 virtual Primitive::Type GetType() const { return Primitive::kPrimLong; }
1060
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001061 DECLARE_INSTRUCTION(LongConstant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001062
1063 private:
1064 const int64_t value_;
1065
1066 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
1067};
1068
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001069class HInvoke : public HInstruction {
1070 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001071 HInvoke(ArenaAllocator* arena,
1072 uint32_t number_of_arguments,
1073 Primitive::Type return_type,
1074 uint32_t dex_pc)
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001075 : inputs_(arena, number_of_arguments),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001076 return_type_(return_type),
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001077 dex_pc_(dex_pc) {
1078 inputs_.SetSize(number_of_arguments);
1079 }
1080
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001081 virtual size_t InputCount() const { return inputs_.Size(); }
1082 virtual HInstruction* InputAt(size_t i) const { return inputs_.Get(i); }
1083
1084 // Runtime needs to walk the stack, so Dex -> Dex calls need to
1085 // know their environment.
1086 virtual bool NeedsEnvironment() const { return true; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001087
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001088 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001089 SetRawInputAt(index, argument);
1090 }
1091
1092 virtual void SetRawInputAt(size_t index, HInstruction* input) {
1093 inputs_.Put(index, input);
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001094 }
1095
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001096 virtual Primitive::Type GetType() const { return return_type_; }
1097
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001098 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001099
1100 protected:
1101 GrowableArray<HInstruction*> inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001102 const Primitive::Type return_type_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001103 const uint32_t dex_pc_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001104
1105 private:
1106 DISALLOW_COPY_AND_ASSIGN(HInvoke);
1107};
1108
1109class HInvokeStatic : public HInvoke {
1110 public:
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001111 HInvokeStatic(ArenaAllocator* arena,
1112 uint32_t number_of_arguments,
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001113 Primitive::Type return_type,
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001114 uint32_t dex_pc,
1115 uint32_t index_in_dex_cache)
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001116 : HInvoke(arena, number_of_arguments, return_type, dex_pc),
1117 index_in_dex_cache_(index_in_dex_cache) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001118
1119 uint32_t GetIndexInDexCache() const { return index_in_dex_cache_; }
1120
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001121 DECLARE_INSTRUCTION(InvokeStatic);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001122
1123 private:
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001124 const uint32_t index_in_dex_cache_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001125
1126 DISALLOW_COPY_AND_ASSIGN(HInvokeStatic);
1127};
1128
Dave Allison20dfc792014-06-16 20:44:29 -07001129class HNewInstance : public HExpression<0> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001130 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001131 HNewInstance(uint32_t dex_pc, uint16_t type_index) : HExpression(Primitive::kPrimNot),
1132 dex_pc_(dex_pc), type_index_(type_index) {}
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001133
1134 uint32_t GetDexPc() const { return dex_pc_; }
1135 uint16_t GetTypeIndex() const { return type_index_; }
1136
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001137 // Calls runtime so needs an environment.
1138 virtual bool NeedsEnvironment() const { return true; }
1139
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001140 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001141
1142 private:
1143 const uint32_t dex_pc_;
1144 const uint16_t type_index_;
1145
1146 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
1147};
1148
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001149class HAdd : public HBinaryOperation {
1150 public:
1151 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1152 : HBinaryOperation(result_type, left, right) {}
1153
1154 virtual bool IsCommutative() { return true; }
1155
1156 DECLARE_INSTRUCTION(Add);
1157
1158 private:
1159 DISALLOW_COPY_AND_ASSIGN(HAdd);
1160};
1161
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001162class HSub : public HBinaryOperation {
1163 public:
1164 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1165 : HBinaryOperation(result_type, left, right) {}
1166
1167 virtual bool IsCommutative() { return false; }
1168
1169 DECLARE_INSTRUCTION(Sub);
1170
1171 private:
1172 DISALLOW_COPY_AND_ASSIGN(HSub);
1173};
1174
1175// The value of a parameter in this method. Its location depends on
1176// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07001177class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001178 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001179 HParameterValue(uint8_t index, Primitive::Type parameter_type)
Dave Allison20dfc792014-06-16 20:44:29 -07001180 : HExpression(parameter_type), index_(index) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001181
1182 uint8_t GetIndex() const { return index_; }
1183
1184 DECLARE_INSTRUCTION(ParameterValue);
1185
1186 private:
1187 // The index of this parameter in the parameters list. Must be less
1188 // than HGraph::number_of_in_vregs_;
1189 const uint8_t index_;
1190
1191 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
1192};
1193
Dave Allison20dfc792014-06-16 20:44:29 -07001194class HNot : public HExpression<1> {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001195 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001196 explicit HNot(HInstruction* input) : HExpression(Primitive::kPrimBoolean) {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001197 SetRawInputAt(0, input);
1198 }
1199
1200 DECLARE_INSTRUCTION(Not);
1201
1202 private:
1203 DISALLOW_COPY_AND_ASSIGN(HNot);
1204};
1205
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001206class HPhi : public HInstruction {
1207 public:
1208 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
1209 : inputs_(arena, number_of_inputs),
1210 reg_number_(reg_number),
1211 type_(type) {
1212 inputs_.SetSize(number_of_inputs);
1213 }
1214
1215 virtual size_t InputCount() const { return inputs_.Size(); }
1216 virtual HInstruction* InputAt(size_t i) const { return inputs_.Get(i); }
1217
1218 virtual void SetRawInputAt(size_t index, HInstruction* input) {
1219 inputs_.Put(index, input);
1220 }
1221
1222 void AddInput(HInstruction* input);
1223
1224 virtual Primitive::Type GetType() const { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001225 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001226
1227 uint32_t GetRegNumber() const { return reg_number_; }
1228
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001229 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001230
1231 protected:
1232 GrowableArray<HInstruction*> inputs_;
1233 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001234 Primitive::Type type_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001235
1236 private:
1237 DISALLOW_COPY_AND_ASSIGN(HPhi);
1238};
1239
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001240class MoveOperands : public ArenaObject {
1241 public:
1242 MoveOperands(Location source, Location destination)
1243 : source_(source), destination_(destination) {}
1244
1245 Location GetSource() const { return source_; }
1246 Location GetDestination() const { return destination_; }
1247
1248 void SetSource(Location value) { source_ = value; }
1249 void SetDestination(Location value) { destination_ = value; }
1250
1251 // The parallel move resolver marks moves as "in-progress" by clearing the
1252 // destination (but not the source).
1253 Location MarkPending() {
1254 DCHECK(!IsPending());
1255 Location dest = destination_;
1256 destination_ = Location::NoLocation();
1257 return dest;
1258 }
1259
1260 void ClearPending(Location dest) {
1261 DCHECK(IsPending());
1262 destination_ = dest;
1263 }
1264
1265 bool IsPending() const {
1266 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
1267 return destination_.IsInvalid() && !source_.IsInvalid();
1268 }
1269
1270 // True if this blocks a move from the given location.
1271 bool Blocks(Location loc) const {
1272 return !IsEliminated() && source_.Equals(loc);
1273 }
1274
1275 // A move is redundant if it's been eliminated, if its source and
1276 // destination are the same, or if its destination is unneeded.
1277 bool IsRedundant() const {
1278 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
1279 }
1280
1281 // We clear both operands to indicate move that's been eliminated.
1282 void Eliminate() {
1283 source_ = destination_ = Location::NoLocation();
1284 }
1285
1286 bool IsEliminated() const {
1287 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
1288 return source_.IsInvalid();
1289 }
1290
1291 private:
1292 Location source_;
1293 Location destination_;
1294
1295 DISALLOW_COPY_AND_ASSIGN(MoveOperands);
1296};
1297
1298static constexpr size_t kDefaultNumberOfMoves = 4;
1299
1300class HParallelMove : public HTemplateInstruction<0> {
1301 public:
1302 explicit HParallelMove(ArenaAllocator* arena) : moves_(arena, kDefaultNumberOfMoves) {}
1303
1304 void AddMove(MoveOperands* move) {
1305 moves_.Add(move);
1306 }
1307
1308 MoveOperands* MoveOperandsAt(size_t index) const {
1309 return moves_.Get(index);
1310 }
1311
1312 size_t NumMoves() const { return moves_.Size(); }
1313
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001314 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001315
1316 private:
1317 GrowableArray<MoveOperands*> moves_;
1318
1319 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
1320};
1321
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001322class HGraphVisitor : public ValueObject {
1323 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001324 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
1325 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001326
Dave Allison20dfc792014-06-16 20:44:29 -07001327 virtual void VisitInstruction(HInstruction* instruction) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001328 virtual void VisitBasicBlock(HBasicBlock* block);
1329
1330 void VisitInsertionOrder();
1331
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001332 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001333
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001334 // Visit functions for instruction classes.
1335#define DECLARE_VISIT_INSTRUCTION(name) \
1336 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
1337
1338 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
1339
1340#undef DECLARE_VISIT_INSTRUCTION
1341
1342 private:
1343 HGraph* graph_;
1344
1345 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
1346};
1347
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001348class HInsertionOrderIterator : public ValueObject {
1349 public:
1350 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
1351
1352 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
1353 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
1354 void Advance() { ++index_; }
1355
1356 private:
1357 const HGraph& graph_;
1358 size_t index_;
1359
1360 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
1361};
1362
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001363class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001364 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001365 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001366
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001367 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
1368 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001369 void Advance() { ++index_; }
1370
1371 private:
1372 const HGraph& graph_;
1373 size_t index_;
1374
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001375 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001376};
1377
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001378class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001379 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001380 explicit HPostOrderIterator(const HGraph& graph)
1381 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001382
1383 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001384 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001385 void Advance() { --index_; }
1386
1387 private:
1388 const HGraph& graph_;
1389 size_t index_;
1390
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001391 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001392};
1393
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001394} // namespace art
1395
1396#endif // ART_COMPILER_OPTIMIZING_NODES_H_