blob: 92920845c3175e3f425eb62b1618f5546c2eae82 [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 Geoffray412f10c2014-06-19 10:00:34 +0100417 M(Compare) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000418
Dave Allison20dfc792014-06-16 20:44:29 -0700419
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000420#define FORWARD_DECLARATION(type) class H##type;
421FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
422#undef FORWARD_DECLARATION
423
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000424#define DECLARE_INSTRUCTION(type) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000425 virtual const char* DebugName() const { return #type; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000426 virtual H##type* As##type() { return this; } \
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100427 virtual void Accept(HGraphVisitor* visitor) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000428
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100429template <typename T>
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000430class HUseListNode : public ArenaObject {
431 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100432 HUseListNode(T* user, size_t index, HUseListNode* tail)
Dave Allison20dfc792014-06-16 20:44:29 -0700433 : user_(user), index_(index), tail_(tail) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000434
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000435 HUseListNode* GetTail() const { return tail_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100436 T* GetUser() const { return user_; }
437 size_t GetIndex() const { return index_; }
438
439 void SetTail(HUseListNode<T>* node) { tail_ = node; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000440
441 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100442 T* const user_;
443 const size_t index_;
444 HUseListNode<T>* tail_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000445
446 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
447};
448
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000449class HInstruction : public ArenaObject {
450 public:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000451 HInstruction()
452 : previous_(nullptr),
453 next_(nullptr),
454 block_(nullptr),
455 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100456 ssa_index_(-1),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000457 uses_(nullptr),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100458 env_uses_(nullptr),
459 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100460 locations_(nullptr),
461 live_interval_(nullptr),
462 lifetime_position_(kNoLifetime) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000463
Dave Allison20dfc792014-06-16 20:44:29 -0700464 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000465
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000466 HInstruction* GetNext() const { return next_; }
467 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000468
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000469 HBasicBlock* GetBlock() const { return block_; }
470 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000471
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100472 virtual size_t InputCount() const = 0;
473 virtual HInstruction* InputAt(size_t i) const = 0;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000474
475 virtual void Accept(HGraphVisitor* visitor) = 0;
476 virtual const char* DebugName() const = 0;
477
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100478 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100479 virtual void SetRawInputAt(size_t index, HInstruction* input) = 0;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100480
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100481 virtual bool NeedsEnvironment() const { return false; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100482 virtual bool IsControlFlow() const { return false; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100483
484 void AddUseAt(HInstruction* user, size_t index) {
485 uses_ = new (block_->GetGraph()->GetArena()) HUseListNode<HInstruction>(user, index, uses_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000486 }
487
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100488 void AddEnvUseAt(HEnvironment* user, size_t index) {
489 env_uses_ = new (block_->GetGraph()->GetArena()) HUseListNode<HEnvironment>(
490 user, index, env_uses_);
491 }
492
493 void RemoveUser(HInstruction* user, size_t index);
494
495 HUseListNode<HInstruction>* GetUses() const { return uses_; }
496 HUseListNode<HEnvironment>* GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000497
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100498 bool HasUses() const { return uses_ != nullptr || env_uses_ != nullptr; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000499
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100500 size_t NumberOfUses() const {
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100501 // TODO: Optimize this method if it is used outside of the HGraphVisualizer.
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100502 size_t result = 0;
503 HUseListNode<HInstruction>* current = uses_;
504 while (current != nullptr) {
505 current = current->GetTail();
506 ++result;
507 }
508 return result;
509 }
510
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000511 int GetId() const { return id_; }
512 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000513
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100514 int GetSsaIndex() const { return ssa_index_; }
515 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
516 bool HasSsaIndex() const { return ssa_index_ != -1; }
517
518 bool HasEnvironment() const { return environment_ != nullptr; }
519 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100520 void SetEnvironment(HEnvironment* environment) { environment_ = environment; }
521
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000522 LocationSummary* GetLocations() const { return locations_; }
523 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000524
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100525 void ReplaceWith(HInstruction* instruction);
526
Dave Allison20dfc792014-06-16 20:44:29 -0700527 bool HasOnlyOneUse() const {
528 return uses_ != nullptr && uses_->GetTail() == nullptr;
529 }
530
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000531#define INSTRUCTION_TYPE_CHECK(type) \
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100532 bool Is##type() { return (As##type() != nullptr); } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000533 virtual H##type* As##type() { return nullptr; }
534
535 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
536#undef INSTRUCTION_TYPE_CHECK
537
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100538 size_t GetLifetimePosition() const { return lifetime_position_; }
539 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
540 LiveInterval* GetLiveInterval() const { return live_interval_; }
541 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
542 bool HasLiveInterval() const { return live_interval_ != nullptr; }
543
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000544 private:
545 HInstruction* previous_;
546 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000547 HBasicBlock* block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000548
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000549 // An instruction gets an id when it is added to the graph.
550 // It reflects creation order. A negative id means the instruction
551 // has not beed added to the graph.
552 int id_;
553
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100554 // When doing liveness analysis, instructions that have uses get an SSA index.
555 int ssa_index_;
556
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100557 // List of instructions that have this instruction as input.
558 HUseListNode<HInstruction>* uses_;
559
560 // List of environments that contain this instruction.
561 HUseListNode<HEnvironment>* env_uses_;
562
563 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000564
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000565 // Set by the code generator.
566 LocationSummary* locations_;
567
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100568 // Set by the liveness analysis.
569 LiveInterval* live_interval_;
570
571 // Set by the liveness analysis, this is the position in a linear
572 // order of blocks where this instruction's live interval start.
573 size_t lifetime_position_;
574
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000575 friend class HBasicBlock;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100576 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000577
578 DISALLOW_COPY_AND_ASSIGN(HInstruction);
579};
580
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100581template<typename T>
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000582class HUseIterator : public ValueObject {
583 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100584 explicit HUseIterator(HUseListNode<T>* uses) : current_(uses) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000585
586 bool Done() const { return current_ == nullptr; }
587
588 void Advance() {
589 DCHECK(!Done());
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000590 current_ = current_->GetTail();
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000591 }
592
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100593 HUseListNode<T>* Current() const {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000594 DCHECK(!Done());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100595 return current_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000596 }
597
598 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100599 HUseListNode<T>* current_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000600
601 friend class HValue;
602};
603
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100604// A HEnvironment object contains the values of virtual registers at a given location.
605class HEnvironment : public ArenaObject {
606 public:
607 HEnvironment(ArenaAllocator* arena, size_t number_of_vregs) : vregs_(arena, number_of_vregs) {
608 vregs_.SetSize(number_of_vregs);
609 for (size_t i = 0; i < number_of_vregs; i++) {
610 vregs_.Put(i, nullptr);
611 }
612 }
613
614 void Populate(const GrowableArray<HInstruction*>& env) {
615 for (size_t i = 0; i < env.Size(); i++) {
616 HInstruction* instruction = env.Get(i);
617 vregs_.Put(i, instruction);
618 if (instruction != nullptr) {
619 instruction->AddEnvUseAt(this, i);
620 }
621 }
622 }
623
624 void SetRawEnvAt(size_t index, HInstruction* instruction) {
625 vregs_.Put(index, instruction);
626 }
627
628 GrowableArray<HInstruction*>* GetVRegs() {
629 return &vregs_;
630 }
631
632 private:
633 GrowableArray<HInstruction*> vregs_;
634
635 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
636};
637
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000638class HInputIterator : public ValueObject {
639 public:
Dave Allison20dfc792014-06-16 20:44:29 -0700640 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000641
642 bool Done() const { return index_ == instruction_->InputCount(); }
643 HInstruction* Current() const { return instruction_->InputAt(index_); }
644 void Advance() { index_++; }
645
646 private:
647 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100648 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000649
650 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
651};
652
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000653class HInstructionIterator : public ValueObject {
654 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100655 explicit HInstructionIterator(const HInstructionList& instructions)
656 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000657 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000658 }
659
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000660 bool Done() const { return instruction_ == nullptr; }
661 HInstruction* Current() const { return instruction_; }
662 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000663 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000664 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000665 }
666
667 private:
668 HInstruction* instruction_;
669 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100670
671 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000672};
673
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100674class HBackwardInstructionIterator : public ValueObject {
675 public:
676 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
677 : instruction_(instructions.last_instruction_) {
678 next_ = Done() ? nullptr : instruction_->GetPrevious();
679 }
680
681 bool Done() const { return instruction_ == nullptr; }
682 HInstruction* Current() const { return instruction_; }
683 void Advance() {
684 instruction_ = next_;
685 next_ = Done() ? nullptr : instruction_->GetPrevious();
686 }
687
688 private:
689 HInstruction* instruction_;
690 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100691
692 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100693};
694
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000695// An embedded container with N elements of type T. Used (with partial
696// specialization for N=0) because embedded arrays cannot have size 0.
697template<typename T, intptr_t N>
698class EmbeddedArray {
699 public:
Dave Allison20dfc792014-06-16 20:44:29 -0700700 EmbeddedArray() : elements_() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000701
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000702 intptr_t GetLength() const { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000703
704 const T& operator[](intptr_t i) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000705 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000706 return elements_[i];
707 }
708
709 T& operator[](intptr_t i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000710 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000711 return elements_[i];
712 }
713
714 const T& At(intptr_t i) const {
715 return (*this)[i];
716 }
717
718 void SetAt(intptr_t i, const T& val) {
719 (*this)[i] = val;
720 }
721
722 private:
723 T elements_[N];
724};
725
726template<typename T>
727class EmbeddedArray<T, 0> {
728 public:
729 intptr_t length() const { return 0; }
730 const T& operator[](intptr_t i) const {
731 LOG(FATAL) << "Unreachable";
732 static T sentinel = 0;
733 return sentinel;
734 }
735 T& operator[](intptr_t i) {
736 LOG(FATAL) << "Unreachable";
737 static T sentinel = 0;
738 return sentinel;
739 }
740};
741
742template<intptr_t N>
743class HTemplateInstruction: public HInstruction {
744 public:
Dave Allison20dfc792014-06-16 20:44:29 -0700745 HTemplateInstruction<N>() : inputs_() {}
746 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000747
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100748 virtual size_t InputCount() const { return N; }
749 virtual HInstruction* InputAt(size_t i) const { return inputs_[i]; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000750
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000751 protected:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100752 virtual void SetRawInputAt(size_t i, HInstruction* instruction) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000753 inputs_[i] = instruction;
754 }
755
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000756 private:
757 EmbeddedArray<HInstruction*, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100758
759 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000760};
761
Dave Allison20dfc792014-06-16 20:44:29 -0700762template<intptr_t N>
763class HExpression: public HTemplateInstruction<N> {
764 public:
765 explicit HExpression<N>(Primitive::Type type) : type_(type) {}
766 virtual ~HExpression() {}
767
768 virtual Primitive::Type GetType() const { return type_; }
769
770 private:
771 const Primitive::Type type_;
772};
773
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000774// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
775// instruction that branches to the exit block.
776class HReturnVoid : public HTemplateInstruction<0> {
777 public:
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100778 HReturnVoid() {}
779
780 virtual bool IsControlFlow() const { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000781
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100782 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000783
784 private:
785 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
786};
787
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000788// Represents dex's RETURN opcodes. A HReturn is a control flow
789// instruction that branches to the exit block.
790class HReturn : public HTemplateInstruction<1> {
791 public:
792 explicit HReturn(HInstruction* value) {
793 SetRawInputAt(0, value);
794 }
795
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100796 virtual bool IsControlFlow() const { return true; }
797
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100798 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000799
800 private:
801 DISALLOW_COPY_AND_ASSIGN(HReturn);
802};
803
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000804// The exit instruction is the only instruction of the exit block.
805// Instructions aborting the method (HTrow and HReturn) must branch to the
806// exit block.
807class HExit : public HTemplateInstruction<0> {
808 public:
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100809 HExit() {}
810
811 virtual bool IsControlFlow() const { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000812
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100813 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000814
815 private:
816 DISALLOW_COPY_AND_ASSIGN(HExit);
817};
818
819// Jumps from one block to another.
820class HGoto : public HTemplateInstruction<0> {
821 public:
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100822 HGoto() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000823
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000824 HBasicBlock* GetSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100825 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000826 }
827
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100828 virtual bool IsControlFlow() const { return true; }
829
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100830 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000831
832 private:
833 DISALLOW_COPY_AND_ASSIGN(HGoto);
834};
835
Dave Allison20dfc792014-06-16 20:44:29 -0700836
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000837// Conditional branch. A block ending with an HIf instruction must have
838// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000839class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000840 public:
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000841 explicit HIf(HInstruction* input) {
842 SetRawInputAt(0, input);
843 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000844
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000845 HBasicBlock* IfTrueSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100846 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000847 }
848
849 HBasicBlock* IfFalseSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100850 return GetBlock()->GetSuccessors().Get(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000851 }
852
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100853 virtual bool IsControlFlow() const { return true; }
854
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100855 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000856
Dave Allison20dfc792014-06-16 20:44:29 -0700857 virtual bool IsIfInstruction() const { return true; }
858
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000859 private:
860 DISALLOW_COPY_AND_ASSIGN(HIf);
861};
862
Dave Allison20dfc792014-06-16 20:44:29 -0700863class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000864 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000865 HBinaryOperation(Primitive::Type result_type,
866 HInstruction* left,
Dave Allison20dfc792014-06-16 20:44:29 -0700867 HInstruction* right) : HExpression(result_type) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000868 SetRawInputAt(0, left);
869 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000870 }
871
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000872 HInstruction* GetLeft() const { return InputAt(0); }
873 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -0700874 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000875
876 virtual bool IsCommutative() { return false; }
877
878 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000879 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
880};
881
Dave Allison20dfc792014-06-16 20:44:29 -0700882class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000883 public:
Dave Allison20dfc792014-06-16 20:44:29 -0700884 HCondition(HInstruction* first, HInstruction* second)
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000885 : HBinaryOperation(Primitive::kPrimBoolean, first, second) {}
886
887 virtual bool IsCommutative() { return true; }
Dave Allison20dfc792014-06-16 20:44:29 -0700888 bool NeedsMaterialization() const;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000889
Dave Allison20dfc792014-06-16 20:44:29 -0700890 DECLARE_INSTRUCTION(Condition);
891
892 virtual IfCondition GetCondition() const = 0;
893
894 private:
895 DISALLOW_COPY_AND_ASSIGN(HCondition);
896};
897
898// Instruction to check if two inputs are equal to each other.
899class HEqual : public HCondition {
900 public:
901 HEqual(HInstruction* first, HInstruction* second)
902 : HCondition(first, second) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100903
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100904 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000905
Dave Allison20dfc792014-06-16 20:44:29 -0700906 virtual IfCondition GetCondition() const {
907 return kCondEQ;
908 }
909
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000910 private:
911 DISALLOW_COPY_AND_ASSIGN(HEqual);
912};
913
Dave Allison20dfc792014-06-16 20:44:29 -0700914class HNotEqual : public HCondition {
915 public:
916 HNotEqual(HInstruction* first, HInstruction* second)
917 : HCondition(first, second) {}
918
919 DECLARE_INSTRUCTION(NotEqual);
920
921 virtual IfCondition GetCondition() const {
922 return kCondNE;
923 }
924
925 private:
926 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
927};
928
929class HLessThan : public HCondition {
930 public:
931 HLessThan(HInstruction* first, HInstruction* second)
932 : HCondition(first, second) {}
933
934 DECLARE_INSTRUCTION(LessThan);
935
936 virtual IfCondition GetCondition() const {
937 return kCondLT;
938 }
939
940 private:
941 DISALLOW_COPY_AND_ASSIGN(HLessThan);
942};
943
944class HLessThanOrEqual : public HCondition {
945 public:
946 HLessThanOrEqual(HInstruction* first, HInstruction* second)
947 : HCondition(first, second) {}
948
949 DECLARE_INSTRUCTION(LessThanOrEqual);
950
951 virtual IfCondition GetCondition() const {
952 return kCondLE;
953 }
954
955 private:
956 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
957};
958
959class HGreaterThan : public HCondition {
960 public:
961 HGreaterThan(HInstruction* first, HInstruction* second)
962 : HCondition(first, second) {}
963
964 DECLARE_INSTRUCTION(GreaterThan);
965
966 virtual IfCondition GetCondition() const {
967 return kCondGT;
968 }
969
970 private:
971 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
972};
973
974class HGreaterThanOrEqual : public HCondition {
975 public:
976 HGreaterThanOrEqual(HInstruction* first, HInstruction* second)
977 : HCondition(first, second) {}
978
979 DECLARE_INSTRUCTION(GreaterThanOrEqual);
980
981 virtual IfCondition GetCondition() const {
982 return kCondGE;
983 }
984
985 private:
986 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
987};
988
989
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100990// Instruction to check how two inputs compare to each other.
991// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
992class HCompare : public HBinaryOperation {
993 public:
994 HCompare(Primitive::Type type, HInstruction* first, HInstruction* second)
995 : HBinaryOperation(Primitive::kPrimInt, first, second) {
996 DCHECK_EQ(type, first->GetType());
997 DCHECK_EQ(type, second->GetType());
998 }
999
1000 DECLARE_INSTRUCTION(Compare);
1001
1002 private:
1003 DISALLOW_COPY_AND_ASSIGN(HCompare);
1004};
1005
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001006// A local in the graph. Corresponds to a Dex register.
1007class HLocal : public HTemplateInstruction<0> {
1008 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001009 explicit HLocal(uint16_t reg_number) : reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001010
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001011 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001012
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001013 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001014
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001015 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001016 // The Dex register number.
1017 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001018
1019 DISALLOW_COPY_AND_ASSIGN(HLocal);
1020};
1021
1022// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07001023class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001024 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001025 explicit HLoadLocal(HLocal* local, Primitive::Type type) : HExpression(type) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001026 SetRawInputAt(0, local);
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(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001032
1033 private:
1034 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
1035};
1036
1037// Store a value in a given local. This instruction has two inputs: the value
1038// and the local.
1039class HStoreLocal : public HTemplateInstruction<2> {
1040 public:
1041 HStoreLocal(HLocal* local, HInstruction* value) {
1042 SetRawInputAt(0, local);
1043 SetRawInputAt(1, value);
1044 }
1045
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001046 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1047
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001048 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001049
1050 private:
1051 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
1052};
1053
1054// Constants of the type int. Those can be from Dex instructions, or
1055// synthesized (for example with the if-eqz instruction).
Dave Allison20dfc792014-06-16 20:44:29 -07001056class HIntConstant : public HExpression<0> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001057 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001058 explicit HIntConstant(int32_t value) : HExpression(Primitive::kPrimInt), value_(value) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001059
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001060 int32_t GetValue() const { return value_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001061
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001062 DECLARE_INSTRUCTION(IntConstant);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001063
1064 private:
1065 const int32_t value_;
1066
1067 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
1068};
1069
Dave Allison20dfc792014-06-16 20:44:29 -07001070class HLongConstant : public HExpression<0> {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001071 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001072 explicit HLongConstant(int64_t value) : HExpression(Primitive::kPrimLong), value_(value) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001073
1074 int64_t GetValue() const { return value_; }
1075
1076 virtual Primitive::Type GetType() const { return Primitive::kPrimLong; }
1077
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001078 DECLARE_INSTRUCTION(LongConstant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001079
1080 private:
1081 const int64_t value_;
1082
1083 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
1084};
1085
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001086class HInvoke : public HInstruction {
1087 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001088 HInvoke(ArenaAllocator* arena,
1089 uint32_t number_of_arguments,
1090 Primitive::Type return_type,
1091 uint32_t dex_pc)
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001092 : inputs_(arena, number_of_arguments),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001093 return_type_(return_type),
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001094 dex_pc_(dex_pc) {
1095 inputs_.SetSize(number_of_arguments);
1096 }
1097
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001098 virtual size_t InputCount() const { return inputs_.Size(); }
1099 virtual HInstruction* InputAt(size_t i) const { return inputs_.Get(i); }
1100
1101 // Runtime needs to walk the stack, so Dex -> Dex calls need to
1102 // know their environment.
1103 virtual bool NeedsEnvironment() const { return true; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001104
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001105 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001106 SetRawInputAt(index, argument);
1107 }
1108
1109 virtual void SetRawInputAt(size_t index, HInstruction* input) {
1110 inputs_.Put(index, input);
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001111 }
1112
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001113 virtual Primitive::Type GetType() const { return return_type_; }
1114
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001115 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001116
1117 protected:
1118 GrowableArray<HInstruction*> inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001119 const Primitive::Type return_type_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001120 const uint32_t dex_pc_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001121
1122 private:
1123 DISALLOW_COPY_AND_ASSIGN(HInvoke);
1124};
1125
1126class HInvokeStatic : public HInvoke {
1127 public:
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001128 HInvokeStatic(ArenaAllocator* arena,
1129 uint32_t number_of_arguments,
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001130 Primitive::Type return_type,
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001131 uint32_t dex_pc,
1132 uint32_t index_in_dex_cache)
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001133 : HInvoke(arena, number_of_arguments, return_type, dex_pc),
1134 index_in_dex_cache_(index_in_dex_cache) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001135
1136 uint32_t GetIndexInDexCache() const { return index_in_dex_cache_; }
1137
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001138 DECLARE_INSTRUCTION(InvokeStatic);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001139
1140 private:
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001141 const uint32_t index_in_dex_cache_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001142
1143 DISALLOW_COPY_AND_ASSIGN(HInvokeStatic);
1144};
1145
Dave Allison20dfc792014-06-16 20:44:29 -07001146class HNewInstance : public HExpression<0> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001147 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001148 HNewInstance(uint32_t dex_pc, uint16_t type_index) : HExpression(Primitive::kPrimNot),
1149 dex_pc_(dex_pc), type_index_(type_index) {}
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001150
1151 uint32_t GetDexPc() const { return dex_pc_; }
1152 uint16_t GetTypeIndex() const { return type_index_; }
1153
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001154 // Calls runtime so needs an environment.
1155 virtual bool NeedsEnvironment() const { return true; }
1156
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001157 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001158
1159 private:
1160 const uint32_t dex_pc_;
1161 const uint16_t type_index_;
1162
1163 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
1164};
1165
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001166class HAdd : public HBinaryOperation {
1167 public:
1168 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1169 : HBinaryOperation(result_type, left, right) {}
1170
1171 virtual bool IsCommutative() { return true; }
1172
1173 DECLARE_INSTRUCTION(Add);
1174
1175 private:
1176 DISALLOW_COPY_AND_ASSIGN(HAdd);
1177};
1178
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001179class HSub : public HBinaryOperation {
1180 public:
1181 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1182 : HBinaryOperation(result_type, left, right) {}
1183
1184 virtual bool IsCommutative() { return false; }
1185
1186 DECLARE_INSTRUCTION(Sub);
1187
1188 private:
1189 DISALLOW_COPY_AND_ASSIGN(HSub);
1190};
1191
1192// The value of a parameter in this method. Its location depends on
1193// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07001194class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001195 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001196 HParameterValue(uint8_t index, Primitive::Type parameter_type)
Dave Allison20dfc792014-06-16 20:44:29 -07001197 : HExpression(parameter_type), index_(index) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001198
1199 uint8_t GetIndex() const { return index_; }
1200
1201 DECLARE_INSTRUCTION(ParameterValue);
1202
1203 private:
1204 // The index of this parameter in the parameters list. Must be less
1205 // than HGraph::number_of_in_vregs_;
1206 const uint8_t index_;
1207
1208 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
1209};
1210
Dave Allison20dfc792014-06-16 20:44:29 -07001211class HNot : public HExpression<1> {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001212 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001213 explicit HNot(HInstruction* input) : HExpression(Primitive::kPrimBoolean) {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001214 SetRawInputAt(0, input);
1215 }
1216
1217 DECLARE_INSTRUCTION(Not);
1218
1219 private:
1220 DISALLOW_COPY_AND_ASSIGN(HNot);
1221};
1222
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001223class HPhi : public HInstruction {
1224 public:
1225 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
1226 : inputs_(arena, number_of_inputs),
1227 reg_number_(reg_number),
1228 type_(type) {
1229 inputs_.SetSize(number_of_inputs);
1230 }
1231
1232 virtual size_t InputCount() const { return inputs_.Size(); }
1233 virtual HInstruction* InputAt(size_t i) const { return inputs_.Get(i); }
1234
1235 virtual void SetRawInputAt(size_t index, HInstruction* input) {
1236 inputs_.Put(index, input);
1237 }
1238
1239 void AddInput(HInstruction* input);
1240
1241 virtual Primitive::Type GetType() const { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001242 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001243
1244 uint32_t GetRegNumber() const { return reg_number_; }
1245
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001246 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001247
1248 protected:
1249 GrowableArray<HInstruction*> inputs_;
1250 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001251 Primitive::Type type_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001252
1253 private:
1254 DISALLOW_COPY_AND_ASSIGN(HPhi);
1255};
1256
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001257class MoveOperands : public ArenaObject {
1258 public:
1259 MoveOperands(Location source, Location destination)
1260 : source_(source), destination_(destination) {}
1261
1262 Location GetSource() const { return source_; }
1263 Location GetDestination() const { return destination_; }
1264
1265 void SetSource(Location value) { source_ = value; }
1266 void SetDestination(Location value) { destination_ = value; }
1267
1268 // The parallel move resolver marks moves as "in-progress" by clearing the
1269 // destination (but not the source).
1270 Location MarkPending() {
1271 DCHECK(!IsPending());
1272 Location dest = destination_;
1273 destination_ = Location::NoLocation();
1274 return dest;
1275 }
1276
1277 void ClearPending(Location dest) {
1278 DCHECK(IsPending());
1279 destination_ = dest;
1280 }
1281
1282 bool IsPending() const {
1283 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
1284 return destination_.IsInvalid() && !source_.IsInvalid();
1285 }
1286
1287 // True if this blocks a move from the given location.
1288 bool Blocks(Location loc) const {
1289 return !IsEliminated() && source_.Equals(loc);
1290 }
1291
1292 // A move is redundant if it's been eliminated, if its source and
1293 // destination are the same, or if its destination is unneeded.
1294 bool IsRedundant() const {
1295 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
1296 }
1297
1298 // We clear both operands to indicate move that's been eliminated.
1299 void Eliminate() {
1300 source_ = destination_ = Location::NoLocation();
1301 }
1302
1303 bool IsEliminated() const {
1304 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
1305 return source_.IsInvalid();
1306 }
1307
1308 private:
1309 Location source_;
1310 Location destination_;
1311
1312 DISALLOW_COPY_AND_ASSIGN(MoveOperands);
1313};
1314
1315static constexpr size_t kDefaultNumberOfMoves = 4;
1316
1317class HParallelMove : public HTemplateInstruction<0> {
1318 public:
1319 explicit HParallelMove(ArenaAllocator* arena) : moves_(arena, kDefaultNumberOfMoves) {}
1320
1321 void AddMove(MoveOperands* move) {
1322 moves_.Add(move);
1323 }
1324
1325 MoveOperands* MoveOperandsAt(size_t index) const {
1326 return moves_.Get(index);
1327 }
1328
1329 size_t NumMoves() const { return moves_.Size(); }
1330
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001331 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001332
1333 private:
1334 GrowableArray<MoveOperands*> moves_;
1335
1336 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
1337};
1338
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001339class HGraphVisitor : public ValueObject {
1340 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001341 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
1342 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001343
Dave Allison20dfc792014-06-16 20:44:29 -07001344 virtual void VisitInstruction(HInstruction* instruction) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001345 virtual void VisitBasicBlock(HBasicBlock* block);
1346
1347 void VisitInsertionOrder();
1348
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001349 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001350
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001351 // Visit functions for instruction classes.
1352#define DECLARE_VISIT_INSTRUCTION(name) \
1353 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
1354
1355 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
1356
1357#undef DECLARE_VISIT_INSTRUCTION
1358
1359 private:
1360 HGraph* graph_;
1361
1362 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
1363};
1364
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001365class HInsertionOrderIterator : public ValueObject {
1366 public:
1367 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
1368
1369 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
1370 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
1371 void Advance() { ++index_; }
1372
1373 private:
1374 const HGraph& graph_;
1375 size_t index_;
1376
1377 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
1378};
1379
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001380class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001381 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001382 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001383
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001384 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
1385 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001386 void Advance() { ++index_; }
1387
1388 private:
1389 const HGraph& graph_;
1390 size_t index_;
1391
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001392 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001393};
1394
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001395class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001396 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001397 explicit HPostOrderIterator(const HGraph& graph)
1398 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001399
1400 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001401 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001402 void Advance() { --index_; }
1403
1404 private:
1405 const HGraph& graph_;
1406 size_t index_;
1407
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001408 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001409};
1410
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001411} // namespace art
1412
1413#endif // ART_COMPILER_OPTIMIZING_NODES_H_