blob: ed6dd939de3bbd00f45e675031707ac2d676d096 [file] [log] [blame]
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ART_COMPILER_OPTIMIZING_NODES_H_
18#define ART_COMPILER_OPTIMIZING_NODES_H_
19
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010020#include "locations.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010021#include "offsets.h"
Ian Rogerse63db272014-07-15 15:36:11 -070022#include "primitive.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000023#include "utils/allocation.h"
Nicolas Geoffray0e336432014-02-26 18:24:38 +000024#include "utils/arena_bit_vector.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000025#include "utils/growable_array.h"
26
27namespace art {
28
29class HBasicBlock;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010030class HEnvironment;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000031class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000032class HIntConstant;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000033class HGraphVisitor;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010034class HPhi;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010035class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000036class LocationSummary;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000037
38static const int kDefaultNumberOfBlocks = 8;
39static const int kDefaultNumberOfSuccessors = 2;
40static const int kDefaultNumberOfPredecessors = 2;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000041static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000042
Dave Allison20dfc792014-06-16 20:44:29 -070043enum IfCondition {
44 kCondEQ,
45 kCondNE,
46 kCondLT,
47 kCondLE,
48 kCondGT,
49 kCondGE,
50};
51
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010052class HInstructionList {
53 public:
54 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
55
56 void AddInstruction(HInstruction* instruction);
57 void RemoveInstruction(HInstruction* instruction);
58
59 private:
60 HInstruction* first_instruction_;
61 HInstruction* last_instruction_;
62
63 friend class HBasicBlock;
64 friend class HInstructionIterator;
Nicolas Geoffray804d0932014-05-02 08:46:00 +010065 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010066
67 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
68};
69
Nicolas Geoffray818f2102014-02-18 16:43:35 +000070// Control-flow graph of a method. Contains a list of basic blocks.
71class HGraph : public ArenaObject {
72 public:
73 explicit HGraph(ArenaAllocator* arena)
74 : arena_(arena),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000075 blocks_(arena, kDefaultNumberOfBlocks),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +010076 reverse_post_order_(arena, kDefaultNumberOfBlocks),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +010077 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +010078 number_of_vregs_(0),
79 number_of_in_vregs_(0),
Nicolas Geoffraye5038322014-07-04 09:41:32 +010080 number_of_temporaries_(0),
Dave Allison20dfc792014-06-16 20:44:29 -070081 current_instruction_id_(0) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +000082
Nicolas Geoffray787c3072014-03-17 10:20:19 +000083 ArenaAllocator* GetArena() const { return arena_; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +010084 const GrowableArray<HBasicBlock*>& GetBlocks() const { return blocks_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +000085
Nicolas Geoffray787c3072014-03-17 10:20:19 +000086 HBasicBlock* GetEntryBlock() const { return entry_block_; }
87 HBasicBlock* GetExitBlock() const { return exit_block_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000088
Nicolas Geoffray787c3072014-03-17 10:20:19 +000089 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
90 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000091
Nicolas Geoffray818f2102014-02-18 16:43:35 +000092 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010093
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000094 void BuildDominatorTree();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010095 void TransformToSSA();
96 void SimplifyCFG();
Nicolas Geoffray818f2102014-02-18 16:43:35 +000097
Nicolas Geoffray622d9c32014-05-12 16:11:02 +010098 // Find all natural loops in this graph. Aborts computation and returns false
Nicolas Geoffrayf635e632014-05-14 09:43:38 +010099 // if one loop is not natural, that is the header does not dominate the back
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100100 // edge.
101 bool FindNaturalLoops() const;
102
103 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
104 void SimplifyLoop(HBasicBlock* header);
105
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000106 int GetNextInstructionId() {
107 return current_instruction_id_++;
108 }
109
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100110 uint16_t GetMaximumNumberOfOutVRegs() const {
111 return maximum_number_of_out_vregs_;
112 }
113
114 void UpdateMaximumNumberOfOutVRegs(uint16_t new_value) {
115 maximum_number_of_out_vregs_ = std::max(new_value, maximum_number_of_out_vregs_);
116 }
117
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100118 void UpdateNumberOfTemporaries(size_t count) {
119 number_of_temporaries_ = std::max(count, number_of_temporaries_);
120 }
121
122 size_t GetNumberOfTemporaries() const {
123 return number_of_temporaries_;
124 }
125
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100126 void SetNumberOfVRegs(uint16_t number_of_vregs) {
127 number_of_vregs_ = number_of_vregs;
128 }
129
130 uint16_t GetNumberOfVRegs() const {
131 return number_of_vregs_;
132 }
133
134 void SetNumberOfInVRegs(uint16_t value) {
135 number_of_in_vregs_ = value;
136 }
137
138 uint16_t GetNumberOfInVRegs() const {
139 return number_of_in_vregs_;
140 }
141
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100142 uint16_t GetNumberOfLocalVRegs() const {
143 return number_of_vregs_ - number_of_in_vregs_;
144 }
145
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100146 const GrowableArray<HBasicBlock*>& GetReversePostOrder() const {
147 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100148 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100149
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000150 private:
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000151 HBasicBlock* FindCommonDominator(HBasicBlock* first, HBasicBlock* second) const;
152 void VisitBlockForDominatorTree(HBasicBlock* block,
153 HBasicBlock* predecessor,
154 GrowableArray<size_t>* visits);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100155 void FindBackEdges(ArenaBitVector* visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000156 void VisitBlockForBackEdges(HBasicBlock* block,
157 ArenaBitVector* visited,
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100158 ArenaBitVector* visiting);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000159 void RemoveDeadBlocks(const ArenaBitVector& visited) const;
160
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000161 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000162
163 // List of blocks in insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000164 GrowableArray<HBasicBlock*> blocks_;
165
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100166 // List of blocks to perform a reverse post order tree traversal.
167 GrowableArray<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000168
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000169 HBasicBlock* entry_block_;
170 HBasicBlock* exit_block_;
171
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100172 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100173 uint16_t maximum_number_of_out_vregs_;
174
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100175 // The number of virtual registers in this method. Contains the parameters.
176 uint16_t number_of_vregs_;
177
178 // The number of virtual registers used by parameters of this method.
179 uint16_t number_of_in_vregs_;
180
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100181 // The number of temporaries that will be needed for the baseline compiler.
182 size_t number_of_temporaries_;
183
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000184 // The current id to assign to a newly added instruction. See HInstruction.id_.
185 int current_instruction_id_;
186
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000187 DISALLOW_COPY_AND_ASSIGN(HGraph);
188};
189
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000190class HLoopInformation : public ArenaObject {
191 public:
192 HLoopInformation(HBasicBlock* header, HGraph* graph)
193 : header_(header),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100194 back_edges_(graph->GetArena(), kDefaultNumberOfBackEdges),
195 blocks_(graph->GetArena(), graph->GetBlocks().Size(), false) {}
196
197 HBasicBlock* GetHeader() const {
198 return header_;
199 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000200
201 void AddBackEdge(HBasicBlock* back_edge) {
202 back_edges_.Add(back_edge);
203 }
204
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100205 void RemoveBackEdge(HBasicBlock* back_edge) {
206 back_edges_.Delete(back_edge);
207 }
208
209 bool IsBackEdge(HBasicBlock* block) {
210 for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) {
211 if (back_edges_.Get(i) == block) return true;
212 }
213 return false;
214 }
215
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000216 int NumberOfBackEdges() const {
217 return back_edges_.Size();
218 }
219
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100220 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100221
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100222 const GrowableArray<HBasicBlock*>& GetBackEdges() const {
223 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100224 }
225
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100226 void ClearBackEdges() {
227 back_edges_.Reset();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100228 }
229
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100230 // Find blocks that are part of this loop. Returns whether the loop is a natural loop,
231 // that is the header dominates the back edge.
232 bool Populate();
233
234 // Returns whether this loop information contains `block`.
235 // Note that this loop information *must* be populated before entering this function.
236 bool Contains(const HBasicBlock& block) const;
237
238 // Returns whether this loop information is an inner loop of `other`.
239 // Note that `other` *must* be populated before entering this function.
240 bool IsIn(const HLoopInformation& other) const;
241
242 const ArenaBitVector& GetBlocks() const { return blocks_; }
243
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000244 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100245 // Internal recursive implementation of `Populate`.
246 void PopulateRecursive(HBasicBlock* block);
247
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000248 HBasicBlock* header_;
249 GrowableArray<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100250 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000251
252 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
253};
254
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100255static constexpr size_t kNoLifetime = -1;
256
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000257// A block in a method. Contains the list of instructions represented
258// as a double linked list. Each block knows its predecessors and
259// successors.
260class HBasicBlock : public ArenaObject {
261 public:
262 explicit HBasicBlock(HGraph* graph)
263 : graph_(graph),
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000264 predecessors_(graph->GetArena(), kDefaultNumberOfPredecessors),
265 successors_(graph->GetArena(), kDefaultNumberOfSuccessors),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000266 loop_information_(nullptr),
267 dominator_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100268 block_id_(-1),
269 lifetime_start_(kNoLifetime),
270 lifetime_end_(kNoLifetime) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000271
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100272 const GrowableArray<HBasicBlock*>& GetPredecessors() const {
273 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000274 }
275
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100276 const GrowableArray<HBasicBlock*>& GetSuccessors() const {
277 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000278 }
279
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000280 void AddBackEdge(HBasicBlock* back_edge) {
281 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000282 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000283 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100284 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000285 loop_information_->AddBackEdge(back_edge);
286 }
287
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000288 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000289
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000290 int GetBlockId() const { return block_id_; }
291 void SetBlockId(int id) { block_id_ = id; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000292
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000293 HBasicBlock* GetDominator() const { return dominator_; }
294 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000295
296 int NumberOfBackEdges() const {
297 return loop_information_ == nullptr
298 ? 0
299 : loop_information_->NumberOfBackEdges();
300 }
301
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100302 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
303 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100304 const HInstructionList& GetInstructions() const { return instructions_; }
305 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100306 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000307
308 void AddSuccessor(HBasicBlock* block) {
309 successors_.Add(block);
310 block->predecessors_.Add(this);
311 }
312
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100313 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
314 size_t successor_index = GetSuccessorIndexOf(existing);
315 DCHECK_NE(successor_index, static_cast<size_t>(-1));
316 existing->RemovePredecessor(this);
317 new_block->predecessors_.Add(this);
318 successors_.Put(successor_index, new_block);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000319 }
320
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100321 void RemovePredecessor(HBasicBlock* block) {
322 predecessors_.Delete(block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100323 }
324
325 void ClearAllPredecessors() {
326 predecessors_.Reset();
327 }
328
329 void AddPredecessor(HBasicBlock* block) {
330 predecessors_.Add(block);
331 block->successors_.Add(this);
332 }
333
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100334 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) {
335 for (size_t i = 0, e = predecessors_.Size(); i < e; ++i) {
336 if (predecessors_.Get(i) == predecessor) {
337 return i;
338 }
339 }
340 return -1;
341 }
342
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100343 size_t GetSuccessorIndexOf(HBasicBlock* successor) {
344 for (size_t i = 0, e = successors_.Size(); i < e; ++i) {
345 if (successors_.Get(i) == successor) {
346 return i;
347 }
348 }
349 return -1;
350 }
351
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000352 void AddInstruction(HInstruction* instruction);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100353 void RemoveInstruction(HInstruction* instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100354 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100355 void AddPhi(HPhi* phi);
356 void RemovePhi(HPhi* phi);
357
358 bool IsLoopHeader() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100359 return (loop_information_ != nullptr) && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100360 }
361
362 HLoopInformation* GetLoopInformation() const {
363 return loop_information_;
364 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000365
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100366 // Set the loop_information_ on this block. This method overrides the current
367 // loop_information if it is an outer loop of the passed loop information.
368 void SetInLoop(HLoopInformation* info) {
369 if (IsLoopHeader()) {
370 // Nothing to do. This just means `info` is an outer loop.
371 } else if (loop_information_ == nullptr) {
372 loop_information_ = info;
373 } else if (loop_information_->Contains(*info->GetHeader())) {
374 // Block is currently part of an outer loop. Make it part of this inner loop.
375 // Note that a non loop header having a loop information means this loop information
376 // has already been populated
377 loop_information_ = info;
378 } else {
379 // Block is part of an inner loop. Do not update the loop information.
380 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
381 // at this point, because this method is being called while populating `info`.
382 }
383 }
384
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100385 bool IsInLoop() const { return loop_information_ != nullptr; }
386
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100387 // Returns wheter this block dominates the blocked passed as parameter.
388 bool Dominates(HBasicBlock* block) const;
389
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100390 size_t GetLifetimeStart() const { return lifetime_start_; }
391 size_t GetLifetimeEnd() const { return lifetime_end_; }
392
393 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
394 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
395
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000396 private:
397 HGraph* const graph_;
398 GrowableArray<HBasicBlock*> predecessors_;
399 GrowableArray<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100400 HInstructionList instructions_;
401 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000402 HLoopInformation* loop_information_;
403 HBasicBlock* dominator_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000404 int block_id_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100405 size_t lifetime_start_;
406 size_t lifetime_end_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000407
408 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
409};
410
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100411#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000412 M(Add) \
Dave Allison20dfc792014-06-16 20:44:29 -0700413 M(Condition) \
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000414 M(Equal) \
Dave Allison20dfc792014-06-16 20:44:29 -0700415 M(NotEqual) \
416 M(LessThan) \
417 M(LessThanOrEqual) \
418 M(GreaterThan) \
419 M(GreaterThanOrEqual) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000420 M(Exit) \
421 M(Goto) \
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000422 M(If) \
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000423 M(IntConstant) \
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000424 M(InvokeStatic) \
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000425 M(LoadLocal) \
426 M(Local) \
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100427 M(LongConstant) \
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +0100428 M(NewInstance) \
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100429 M(Not) \
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100430 M(ParameterValue) \
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100431 M(ParallelMove) \
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100432 M(Phi) \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000433 M(Return) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000434 M(ReturnVoid) \
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000435 M(StoreLocal) \
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100436 M(Sub) \
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100437 M(Compare) \
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100438 M(InstanceFieldGet) \
439 M(InstanceFieldSet) \
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100440 M(ArrayGet) \
441 M(ArraySet) \
442 M(ArrayLength) \
443 M(BoundsCheck) \
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100444 M(NullCheck) \
445 M(Temporary) \
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000446 M(SuspendCheck) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000447
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100448#define FOR_EACH_INSTRUCTION(M) \
449 FOR_EACH_CONCRETE_INSTRUCTION(M) \
450 M(Constant)
Dave Allison20dfc792014-06-16 20:44:29 -0700451
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000452#define FORWARD_DECLARATION(type) class H##type;
453FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
454#undef FORWARD_DECLARATION
455
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000456#define DECLARE_INSTRUCTION(type) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000457 virtual const char* DebugName() const { return #type; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000458 virtual H##type* As##type() { return this; } \
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100459 virtual bool InstructionTypeEquals(HInstruction* other) const { \
460 return other->Is##type(); \
461 } \
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100462 virtual void Accept(HGraphVisitor* visitor) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000463
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100464template <typename T>
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000465class HUseListNode : public ArenaObject {
466 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100467 HUseListNode(T* user, size_t index, HUseListNode* tail)
Dave Allison20dfc792014-06-16 20:44:29 -0700468 : user_(user), index_(index), tail_(tail) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000469
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000470 HUseListNode* GetTail() const { return tail_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100471 T* GetUser() const { return user_; }
472 size_t GetIndex() const { return index_; }
473
474 void SetTail(HUseListNode<T>* node) { tail_ = node; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000475
476 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100477 T* const user_;
478 const size_t index_;
479 HUseListNode<T>* tail_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000480
481 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
482};
483
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100484// Represents the side effects an instruction may have.
485class SideEffects : public ValueObject {
486 public:
487 static SideEffects None() {
488 return SideEffects(0);
489 }
490
491 static SideEffects All() {
492 return SideEffects(ChangesSomething().flags_ | DependsOnSomething().flags_);
493 }
494
495 static SideEffects ChangesSomething() {
496 return SideEffects((1 << kFlagChangesCount) - 1);
497 }
498
499 static SideEffects DependsOnSomething() {
500 int count = kFlagDependsOnCount - kFlagChangesCount;
501 return SideEffects(((1 << count) - 1) << kFlagChangesCount);
502 }
503
504 private:
505 static constexpr int kFlagChangesSomething = 0;
506 static constexpr int kFlagChangesCount = kFlagChangesSomething + 1;
507
508 static constexpr int kFlagDependsOnSomething = kFlagChangesCount;
509 static constexpr int kFlagDependsOnCount = kFlagDependsOnSomething + 1;
510
511 private:
512 explicit SideEffects(size_t flags) : flags_(flags) {}
513
514 const size_t flags_;
515};
516
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000517class HInstruction : public ArenaObject {
518 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100519 explicit HInstruction(SideEffects side_effects)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000520 : previous_(nullptr),
521 next_(nullptr),
522 block_(nullptr),
523 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100524 ssa_index_(-1),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000525 uses_(nullptr),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100526 env_uses_(nullptr),
527 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100528 locations_(nullptr),
529 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100530 lifetime_position_(kNoLifetime),
531 side_effects_(side_effects) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000532
Dave Allison20dfc792014-06-16 20:44:29 -0700533 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000534
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000535 HInstruction* GetNext() const { return next_; }
536 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000537
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000538 HBasicBlock* GetBlock() const { return block_; }
539 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100540 bool IsInBlock() const { return block_ != nullptr; }
541 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +0100542 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000543
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100544 virtual size_t InputCount() const = 0;
545 virtual HInstruction* InputAt(size_t i) const = 0;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000546
547 virtual void Accept(HGraphVisitor* visitor) = 0;
548 virtual const char* DebugName() const = 0;
549
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100550 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100551 virtual void SetRawInputAt(size_t index, HInstruction* input) = 0;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100552
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100553 virtual bool NeedsEnvironment() const { return false; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100554 virtual bool IsControlFlow() const { return false; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100555
556 void AddUseAt(HInstruction* user, size_t index) {
557 uses_ = new (block_->GetGraph()->GetArena()) HUseListNode<HInstruction>(user, index, uses_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000558 }
559
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100560 void AddEnvUseAt(HEnvironment* user, size_t index) {
561 env_uses_ = new (block_->GetGraph()->GetArena()) HUseListNode<HEnvironment>(
562 user, index, env_uses_);
563 }
564
565 void RemoveUser(HInstruction* user, size_t index);
566
567 HUseListNode<HInstruction>* GetUses() const { return uses_; }
568 HUseListNode<HEnvironment>* GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000569
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100570 bool HasUses() const { return uses_ != nullptr || env_uses_ != nullptr; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100571 bool HasEnvironmentUses() const { return env_uses_ != nullptr; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000572
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100573 size_t NumberOfUses() const {
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100574 // TODO: Optimize this method if it is used outside of the HGraphVisualizer.
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100575 size_t result = 0;
576 HUseListNode<HInstruction>* current = uses_;
577 while (current != nullptr) {
578 current = current->GetTail();
579 ++result;
580 }
581 return result;
582 }
583
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000584 int GetId() const { return id_; }
585 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000586
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100587 int GetSsaIndex() const { return ssa_index_; }
588 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
589 bool HasSsaIndex() const { return ssa_index_ != -1; }
590
591 bool HasEnvironment() const { return environment_ != nullptr; }
592 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100593 void SetEnvironment(HEnvironment* environment) { environment_ = environment; }
594
Nicolas Geoffray39468442014-09-02 15:17:15 +0100595 // Returns the number of entries in the environment. Typically, that is the
596 // number of dex registers in a method. It could be more in case of inlining.
597 size_t EnvironmentSize() const;
598
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000599 LocationSummary* GetLocations() const { return locations_; }
600 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000601
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100602 void ReplaceWith(HInstruction* instruction);
603
Dave Allison20dfc792014-06-16 20:44:29 -0700604 bool HasOnlyOneUse() const {
605 return uses_ != nullptr && uses_->GetTail() == nullptr;
606 }
607
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000608#define INSTRUCTION_TYPE_CHECK(type) \
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100609 bool Is##type() { return (As##type() != nullptr); } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000610 virtual H##type* As##type() { return nullptr; }
611
612 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
613#undef INSTRUCTION_TYPE_CHECK
614
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100615 // Returns whether the instruction can be moved within the graph.
616 virtual bool CanBeMoved() const { return false; }
617
618 // Returns whether the two instructions are of the same kind.
619 virtual bool InstructionTypeEquals(HInstruction* other) const { return false; }
620
621 // Returns whether any data encoded in the two instructions is equal.
622 // This method does not look at the inputs. Both instructions must be
623 // of the same type, otherwise the method has undefined behavior.
624 virtual bool InstructionDataEquals(HInstruction* other) const { return false; }
625
626 // Returns whether two instructions are equal, that is:
627 // 1) They have the same type and contain the same data,
628 // 2) Their inputs are identical.
629 bool Equals(HInstruction* other) const;
630
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100631 size_t GetLifetimePosition() const { return lifetime_position_; }
632 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
633 LiveInterval* GetLiveInterval() const { return live_interval_; }
634 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
635 bool HasLiveInterval() const { return live_interval_ != nullptr; }
636
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000637 private:
638 HInstruction* previous_;
639 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000640 HBasicBlock* block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000641
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000642 // An instruction gets an id when it is added to the graph.
643 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +0100644 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000645 int id_;
646
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100647 // When doing liveness analysis, instructions that have uses get an SSA index.
648 int ssa_index_;
649
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100650 // List of instructions that have this instruction as input.
651 HUseListNode<HInstruction>* uses_;
652
653 // List of environments that contain this instruction.
654 HUseListNode<HEnvironment>* env_uses_;
655
Nicolas Geoffray39468442014-09-02 15:17:15 +0100656 // The environment associated with this instruction. Not null if the instruction
657 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100658 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000659
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000660 // Set by the code generator.
661 LocationSummary* locations_;
662
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100663 // Set by the liveness analysis.
664 LiveInterval* live_interval_;
665
666 // Set by the liveness analysis, this is the position in a linear
667 // order of blocks where this instruction's live interval start.
668 size_t lifetime_position_;
669
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100670 const SideEffects side_effects_;
671
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000672 friend class HBasicBlock;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100673 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000674
675 DISALLOW_COPY_AND_ASSIGN(HInstruction);
676};
677
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100678template<typename T>
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000679class HUseIterator : public ValueObject {
680 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100681 explicit HUseIterator(HUseListNode<T>* uses) : current_(uses) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000682
683 bool Done() const { return current_ == nullptr; }
684
685 void Advance() {
686 DCHECK(!Done());
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000687 current_ = current_->GetTail();
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000688 }
689
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100690 HUseListNode<T>* Current() const {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000691 DCHECK(!Done());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100692 return current_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000693 }
694
695 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100696 HUseListNode<T>* current_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000697
698 friend class HValue;
699};
700
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100701// A HEnvironment object contains the values of virtual registers at a given location.
702class HEnvironment : public ArenaObject {
703 public:
704 HEnvironment(ArenaAllocator* arena, size_t number_of_vregs) : vregs_(arena, number_of_vregs) {
705 vregs_.SetSize(number_of_vregs);
706 for (size_t i = 0; i < number_of_vregs; i++) {
707 vregs_.Put(i, nullptr);
708 }
709 }
710
711 void Populate(const GrowableArray<HInstruction*>& env) {
712 for (size_t i = 0; i < env.Size(); i++) {
713 HInstruction* instruction = env.Get(i);
714 vregs_.Put(i, instruction);
715 if (instruction != nullptr) {
716 instruction->AddEnvUseAt(this, i);
717 }
718 }
719 }
720
721 void SetRawEnvAt(size_t index, HInstruction* instruction) {
722 vregs_.Put(index, instruction);
723 }
724
Nicolas Geoffray39468442014-09-02 15:17:15 +0100725 HInstruction* GetInstructionAt(size_t index) const {
726 return vregs_.Get(index);
727 }
728
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100729 GrowableArray<HInstruction*>* GetVRegs() {
730 return &vregs_;
731 }
732
Nicolas Geoffray39468442014-09-02 15:17:15 +0100733 size_t Size() const { return vregs_.Size(); }
734
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100735 private:
736 GrowableArray<HInstruction*> vregs_;
737
738 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
739};
740
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000741class HInputIterator : public ValueObject {
742 public:
Dave Allison20dfc792014-06-16 20:44:29 -0700743 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000744
745 bool Done() const { return index_ == instruction_->InputCount(); }
746 HInstruction* Current() const { return instruction_->InputAt(index_); }
747 void Advance() { index_++; }
748
749 private:
750 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100751 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000752
753 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
754};
755
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000756class HInstructionIterator : public ValueObject {
757 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100758 explicit HInstructionIterator(const HInstructionList& instructions)
759 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000760 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000761 }
762
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000763 bool Done() const { return instruction_ == nullptr; }
764 HInstruction* Current() const { return instruction_; }
765 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000766 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000767 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000768 }
769
770 private:
771 HInstruction* instruction_;
772 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100773
774 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000775};
776
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100777class HBackwardInstructionIterator : public ValueObject {
778 public:
779 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
780 : instruction_(instructions.last_instruction_) {
781 next_ = Done() ? nullptr : instruction_->GetPrevious();
782 }
783
784 bool Done() const { return instruction_ == nullptr; }
785 HInstruction* Current() const { return instruction_; }
786 void Advance() {
787 instruction_ = next_;
788 next_ = Done() ? nullptr : instruction_->GetPrevious();
789 }
790
791 private:
792 HInstruction* instruction_;
793 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100794
795 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100796};
797
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000798// An embedded container with N elements of type T. Used (with partial
799// specialization for N=0) because embedded arrays cannot have size 0.
800template<typename T, intptr_t N>
801class EmbeddedArray {
802 public:
Dave Allison20dfc792014-06-16 20:44:29 -0700803 EmbeddedArray() : elements_() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000804
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000805 intptr_t GetLength() const { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000806
807 const T& operator[](intptr_t i) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000808 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000809 return elements_[i];
810 }
811
812 T& operator[](intptr_t i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000813 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000814 return elements_[i];
815 }
816
817 const T& At(intptr_t i) const {
818 return (*this)[i];
819 }
820
821 void SetAt(intptr_t i, const T& val) {
822 (*this)[i] = val;
823 }
824
825 private:
826 T elements_[N];
827};
828
829template<typename T>
830class EmbeddedArray<T, 0> {
831 public:
832 intptr_t length() const { return 0; }
833 const T& operator[](intptr_t i) const {
834 LOG(FATAL) << "Unreachable";
835 static T sentinel = 0;
836 return sentinel;
837 }
838 T& operator[](intptr_t i) {
839 LOG(FATAL) << "Unreachable";
840 static T sentinel = 0;
841 return sentinel;
842 }
843};
844
845template<intptr_t N>
846class HTemplateInstruction: public HInstruction {
847 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100848 HTemplateInstruction<N>(SideEffects side_effects)
849 : HInstruction(side_effects), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -0700850 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000851
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100852 virtual size_t InputCount() const { return N; }
853 virtual HInstruction* InputAt(size_t i) const { return inputs_[i]; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000854
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000855 protected:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100856 virtual void SetRawInputAt(size_t i, HInstruction* instruction) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000857 inputs_[i] = instruction;
858 }
859
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000860 private:
861 EmbeddedArray<HInstruction*, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100862
863 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000864};
865
Dave Allison20dfc792014-06-16 20:44:29 -0700866template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100867class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -0700868 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100869 HExpression<N>(Primitive::Type type, SideEffects side_effects)
870 : HTemplateInstruction<N>(side_effects), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -0700871 virtual ~HExpression() {}
872
873 virtual Primitive::Type GetType() const { return type_; }
874
875 private:
876 const Primitive::Type type_;
877};
878
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000879// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
880// instruction that branches to the exit block.
881class HReturnVoid : public HTemplateInstruction<0> {
882 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100883 HReturnVoid() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100884
885 virtual bool IsControlFlow() const { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000886
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100887 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000888
889 private:
890 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
891};
892
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000893// Represents dex's RETURN opcodes. A HReturn is a control flow
894// instruction that branches to the exit block.
895class HReturn : public HTemplateInstruction<1> {
896 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100897 explicit HReturn(HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000898 SetRawInputAt(0, value);
899 }
900
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100901 virtual bool IsControlFlow() const { return true; }
902
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100903 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000904
905 private:
906 DISALLOW_COPY_AND_ASSIGN(HReturn);
907};
908
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000909// The exit instruction is the only instruction of the exit block.
910// Instructions aborting the method (HTrow and HReturn) must branch to the
911// exit block.
912class HExit : public HTemplateInstruction<0> {
913 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100914 HExit() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100915
916 virtual bool IsControlFlow() const { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000917
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100918 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000919
920 private:
921 DISALLOW_COPY_AND_ASSIGN(HExit);
922};
923
924// Jumps from one block to another.
925class HGoto : public HTemplateInstruction<0> {
926 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100927 HGoto() : HTemplateInstruction(SideEffects::None()) {}
928
929 virtual bool IsControlFlow() const { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000930
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000931 HBasicBlock* GetSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100932 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000933 }
934
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100935 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000936
937 private:
938 DISALLOW_COPY_AND_ASSIGN(HGoto);
939};
940
Dave Allison20dfc792014-06-16 20:44:29 -0700941
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000942// Conditional branch. A block ending with an HIf instruction must have
943// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000944class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000945 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100946 explicit HIf(HInstruction* input) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000947 SetRawInputAt(0, input);
948 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000949
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100950 virtual bool IsControlFlow() const { return true; }
951
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000952 HBasicBlock* IfTrueSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100953 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000954 }
955
956 HBasicBlock* IfFalseSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100957 return GetBlock()->GetSuccessors().Get(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000958 }
959
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100960 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000961
Dave Allison20dfc792014-06-16 20:44:29 -0700962 virtual bool IsIfInstruction() const { return true; }
963
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000964 private:
965 DISALLOW_COPY_AND_ASSIGN(HIf);
966};
967
Dave Allison20dfc792014-06-16 20:44:29 -0700968class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000969 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000970 HBinaryOperation(Primitive::Type result_type,
971 HInstruction* left,
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100972 HInstruction* right) : HExpression(result_type, SideEffects::None()) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000973 SetRawInputAt(0, left);
974 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000975 }
976
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000977 HInstruction* GetLeft() const { return InputAt(0); }
978 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -0700979 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000980
981 virtual bool IsCommutative() { return false; }
982
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100983 virtual bool CanBeMoved() const { return true; }
984 virtual bool InstructionDataEquals(HInstruction* other) const { return true; }
985
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000986 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000987 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
988};
989
Dave Allison20dfc792014-06-16 20:44:29 -0700990class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000991 public:
Dave Allison20dfc792014-06-16 20:44:29 -0700992 HCondition(HInstruction* first, HInstruction* second)
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000993 : HBinaryOperation(Primitive::kPrimBoolean, first, second) {}
994
995 virtual bool IsCommutative() { return true; }
Dave Allison20dfc792014-06-16 20:44:29 -0700996 bool NeedsMaterialization() const;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000997
Dave Allison20dfc792014-06-16 20:44:29 -0700998 DECLARE_INSTRUCTION(Condition);
999
1000 virtual IfCondition GetCondition() const = 0;
1001
1002 private:
1003 DISALLOW_COPY_AND_ASSIGN(HCondition);
1004};
1005
1006// Instruction to check if two inputs are equal to each other.
1007class HEqual : public HCondition {
1008 public:
1009 HEqual(HInstruction* first, HInstruction* second)
1010 : HCondition(first, second) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001011
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001012 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001013
Dave Allison20dfc792014-06-16 20:44:29 -07001014 virtual IfCondition GetCondition() const {
1015 return kCondEQ;
1016 }
1017
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001018 private:
1019 DISALLOW_COPY_AND_ASSIGN(HEqual);
1020};
1021
Dave Allison20dfc792014-06-16 20:44:29 -07001022class HNotEqual : public HCondition {
1023 public:
1024 HNotEqual(HInstruction* first, HInstruction* second)
1025 : HCondition(first, second) {}
1026
1027 DECLARE_INSTRUCTION(NotEqual);
1028
1029 virtual IfCondition GetCondition() const {
1030 return kCondNE;
1031 }
1032
1033 private:
1034 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
1035};
1036
1037class HLessThan : public HCondition {
1038 public:
1039 HLessThan(HInstruction* first, HInstruction* second)
1040 : HCondition(first, second) {}
1041
1042 DECLARE_INSTRUCTION(LessThan);
1043
1044 virtual IfCondition GetCondition() const {
1045 return kCondLT;
1046 }
1047
1048 private:
1049 DISALLOW_COPY_AND_ASSIGN(HLessThan);
1050};
1051
1052class HLessThanOrEqual : public HCondition {
1053 public:
1054 HLessThanOrEqual(HInstruction* first, HInstruction* second)
1055 : HCondition(first, second) {}
1056
1057 DECLARE_INSTRUCTION(LessThanOrEqual);
1058
1059 virtual IfCondition GetCondition() const {
1060 return kCondLE;
1061 }
1062
1063 private:
1064 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
1065};
1066
1067class HGreaterThan : public HCondition {
1068 public:
1069 HGreaterThan(HInstruction* first, HInstruction* second)
1070 : HCondition(first, second) {}
1071
1072 DECLARE_INSTRUCTION(GreaterThan);
1073
1074 virtual IfCondition GetCondition() const {
1075 return kCondGT;
1076 }
1077
1078 private:
1079 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
1080};
1081
1082class HGreaterThanOrEqual : public HCondition {
1083 public:
1084 HGreaterThanOrEqual(HInstruction* first, HInstruction* second)
1085 : HCondition(first, second) {}
1086
1087 DECLARE_INSTRUCTION(GreaterThanOrEqual);
1088
1089 virtual IfCondition GetCondition() const {
1090 return kCondGE;
1091 }
1092
1093 private:
1094 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
1095};
1096
1097
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001098// Instruction to check how two inputs compare to each other.
1099// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
1100class HCompare : public HBinaryOperation {
1101 public:
1102 HCompare(Primitive::Type type, HInstruction* first, HInstruction* second)
1103 : HBinaryOperation(Primitive::kPrimInt, first, second) {
1104 DCHECK_EQ(type, first->GetType());
1105 DCHECK_EQ(type, second->GetType());
1106 }
1107
1108 DECLARE_INSTRUCTION(Compare);
1109
1110 private:
1111 DISALLOW_COPY_AND_ASSIGN(HCompare);
1112};
1113
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001114// A local in the graph. Corresponds to a Dex register.
1115class HLocal : public HTemplateInstruction<0> {
1116 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001117 explicit HLocal(uint16_t reg_number)
1118 : HTemplateInstruction(SideEffects::None()), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001119
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001120 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001121
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001122 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001123
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001124 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001125 // The Dex register number.
1126 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001127
1128 DISALLOW_COPY_AND_ASSIGN(HLocal);
1129};
1130
1131// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07001132class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001133 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001134 explicit HLoadLocal(HLocal* local, Primitive::Type type)
1135 : HExpression(type, SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001136 SetRawInputAt(0, local);
1137 }
1138
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001139 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1140
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001141 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001142
1143 private:
1144 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
1145};
1146
1147// Store a value in a given local. This instruction has two inputs: the value
1148// and the local.
1149class HStoreLocal : public HTemplateInstruction<2> {
1150 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001151 HStoreLocal(HLocal* local, HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001152 SetRawInputAt(0, local);
1153 SetRawInputAt(1, value);
1154 }
1155
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001156 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1157
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001158 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001159
1160 private:
1161 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
1162};
1163
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001164class HConstant : public HExpression<0> {
1165 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001166 explicit HConstant(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
1167
1168 virtual bool CanBeMoved() const { return true; }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001169
1170 DECLARE_INSTRUCTION(Constant);
1171
1172 private:
1173 DISALLOW_COPY_AND_ASSIGN(HConstant);
1174};
1175
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001176// Constants of the type int. Those can be from Dex instructions, or
1177// synthesized (for example with the if-eqz instruction).
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001178class HIntConstant : public HConstant {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001179 public:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001180 explicit HIntConstant(int32_t value) : HConstant(Primitive::kPrimInt), value_(value) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001181
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001182 int32_t GetValue() const { return value_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001183
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001184 virtual bool InstructionDataEquals(HInstruction* other) const {
1185 return other->AsIntConstant()->value_ == value_;
1186 }
1187
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001188 DECLARE_INSTRUCTION(IntConstant);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001189
1190 private:
1191 const int32_t value_;
1192
1193 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
1194};
1195
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001196class HLongConstant : public HConstant {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001197 public:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001198 explicit HLongConstant(int64_t value) : HConstant(Primitive::kPrimLong), value_(value) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001199
1200 int64_t GetValue() const { return value_; }
1201
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001202 virtual bool InstructionDataEquals(HInstruction* other) const {
1203 return other->AsLongConstant()->value_ == value_;
1204 }
1205
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001206 DECLARE_INSTRUCTION(LongConstant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001207
1208 private:
1209 const int64_t value_;
1210
1211 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
1212};
1213
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001214class HInvoke : public HInstruction {
1215 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001216 HInvoke(ArenaAllocator* arena,
1217 uint32_t number_of_arguments,
1218 Primitive::Type return_type,
1219 uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001220 : HInstruction(SideEffects::All()),
1221 inputs_(arena, number_of_arguments),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001222 return_type_(return_type),
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001223 dex_pc_(dex_pc) {
1224 inputs_.SetSize(number_of_arguments);
1225 }
1226
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001227 virtual size_t InputCount() const { return inputs_.Size(); }
1228 virtual HInstruction* InputAt(size_t i) const { return inputs_.Get(i); }
1229
1230 // Runtime needs to walk the stack, so Dex -> Dex calls need to
1231 // know their environment.
1232 virtual bool NeedsEnvironment() const { return true; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001233
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001234 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001235 SetRawInputAt(index, argument);
1236 }
1237
1238 virtual void SetRawInputAt(size_t index, HInstruction* input) {
1239 inputs_.Put(index, input);
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001240 }
1241
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001242 virtual Primitive::Type GetType() const { return return_type_; }
1243
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001244 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001245
1246 protected:
1247 GrowableArray<HInstruction*> inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001248 const Primitive::Type return_type_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001249 const uint32_t dex_pc_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001250
1251 private:
1252 DISALLOW_COPY_AND_ASSIGN(HInvoke);
1253};
1254
1255class HInvokeStatic : public HInvoke {
1256 public:
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001257 HInvokeStatic(ArenaAllocator* arena,
1258 uint32_t number_of_arguments,
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001259 Primitive::Type return_type,
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001260 uint32_t dex_pc,
1261 uint32_t index_in_dex_cache)
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001262 : HInvoke(arena, number_of_arguments, return_type, dex_pc),
1263 index_in_dex_cache_(index_in_dex_cache) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001264
1265 uint32_t GetIndexInDexCache() const { return index_in_dex_cache_; }
1266
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001267 DECLARE_INSTRUCTION(InvokeStatic);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001268
1269 private:
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001270 const uint32_t index_in_dex_cache_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001271
1272 DISALLOW_COPY_AND_ASSIGN(HInvokeStatic);
1273};
1274
Dave Allison20dfc792014-06-16 20:44:29 -07001275class HNewInstance : public HExpression<0> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001276 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001277 HNewInstance(uint32_t dex_pc, uint16_t type_index)
1278 : HExpression(Primitive::kPrimNot, SideEffects::None()),
1279 dex_pc_(dex_pc),
1280 type_index_(type_index) {}
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001281
1282 uint32_t GetDexPc() const { return dex_pc_; }
1283 uint16_t GetTypeIndex() const { return type_index_; }
1284
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001285 // Calls runtime so needs an environment.
1286 virtual bool NeedsEnvironment() const { return true; }
1287
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001288 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001289
1290 private:
1291 const uint32_t dex_pc_;
1292 const uint16_t type_index_;
1293
1294 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
1295};
1296
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001297class HAdd : public HBinaryOperation {
1298 public:
1299 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1300 : HBinaryOperation(result_type, left, right) {}
1301
1302 virtual bool IsCommutative() { return true; }
1303
1304 DECLARE_INSTRUCTION(Add);
1305
1306 private:
1307 DISALLOW_COPY_AND_ASSIGN(HAdd);
1308};
1309
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001310class HSub : public HBinaryOperation {
1311 public:
1312 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1313 : HBinaryOperation(result_type, left, right) {}
1314
1315 virtual bool IsCommutative() { return false; }
1316
1317 DECLARE_INSTRUCTION(Sub);
1318
1319 private:
1320 DISALLOW_COPY_AND_ASSIGN(HSub);
1321};
1322
1323// The value of a parameter in this method. Its location depends on
1324// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07001325class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001326 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001327 HParameterValue(uint8_t index, Primitive::Type parameter_type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001328 : HExpression(parameter_type, SideEffects::None()), index_(index) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001329
1330 uint8_t GetIndex() const { return index_; }
1331
1332 DECLARE_INSTRUCTION(ParameterValue);
1333
1334 private:
1335 // The index of this parameter in the parameters list. Must be less
1336 // than HGraph::number_of_in_vregs_;
1337 const uint8_t index_;
1338
1339 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
1340};
1341
Dave Allison20dfc792014-06-16 20:44:29 -07001342class HNot : public HExpression<1> {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001343 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001344 explicit HNot(HInstruction* input) : HExpression(Primitive::kPrimBoolean, SideEffects::None()) {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001345 SetRawInputAt(0, input);
1346 }
1347
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001348 virtual bool CanBeMoved() const { return true; }
1349 virtual bool InstructionDataEquals(HInstruction* other) const { return true; }
1350
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001351 DECLARE_INSTRUCTION(Not);
1352
1353 private:
1354 DISALLOW_COPY_AND_ASSIGN(HNot);
1355};
1356
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001357class HPhi : public HInstruction {
1358 public:
1359 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001360 : HInstruction(SideEffects::None()),
1361 inputs_(arena, number_of_inputs),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001362 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001363 type_(type),
1364 is_live_(false) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001365 inputs_.SetSize(number_of_inputs);
1366 }
1367
1368 virtual size_t InputCount() const { return inputs_.Size(); }
1369 virtual HInstruction* InputAt(size_t i) const { return inputs_.Get(i); }
1370
1371 virtual void SetRawInputAt(size_t index, HInstruction* input) {
1372 inputs_.Put(index, input);
1373 }
1374
1375 void AddInput(HInstruction* input);
1376
1377 virtual Primitive::Type GetType() const { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001378 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001379
1380 uint32_t GetRegNumber() const { return reg_number_; }
1381
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001382 void SetDead() { is_live_ = false; }
1383 void SetLive() { is_live_ = true; }
1384 bool IsDead() const { return !is_live_; }
1385 bool IsLive() const { return is_live_; }
1386
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001387 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001388
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001389 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001390 GrowableArray<HInstruction*> inputs_;
1391 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001392 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001393 bool is_live_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001394
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001395 DISALLOW_COPY_AND_ASSIGN(HPhi);
1396};
1397
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001398class HNullCheck : public HExpression<1> {
1399 public:
1400 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001401 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001402 SetRawInputAt(0, value);
1403 }
1404
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001405 virtual bool CanBeMoved() const { return true; }
1406 virtual bool InstructionDataEquals(HInstruction* other) const { return true; }
1407
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001408 virtual bool NeedsEnvironment() const { return true; }
1409
1410 uint32_t GetDexPc() const { return dex_pc_; }
1411
1412 DECLARE_INSTRUCTION(NullCheck);
1413
1414 private:
1415 const uint32_t dex_pc_;
1416
1417 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
1418};
1419
1420class FieldInfo : public ValueObject {
1421 public:
Nicolas Geoffray39468442014-09-02 15:17:15 +01001422 explicit FieldInfo(MemberOffset field_offset, Primitive::Type field_type)
1423 : field_offset_(field_offset), field_type_(field_type) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001424
1425 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001426 Primitive::Type GetFieldType() const { return field_type_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001427
1428 private:
1429 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01001430 const Primitive::Type field_type_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001431};
1432
1433class HInstanceFieldGet : public HExpression<1> {
1434 public:
1435 HInstanceFieldGet(HInstruction* value,
1436 Primitive::Type field_type,
1437 MemberOffset field_offset)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001438 : HExpression(field_type, SideEffects::DependsOnSomething()),
1439 field_info_(field_offset, field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001440 SetRawInputAt(0, value);
1441 }
1442
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001443 virtual bool CanBeMoved() const { return true; }
1444 virtual bool InstructionDataEquals(HInstruction* other) const {
1445 size_t other_offset = other->AsInstanceFieldGet()->GetFieldOffset().SizeValue();
1446 return other_offset == GetFieldOffset().SizeValue();
1447 }
1448
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001449 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001450 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001451
1452 DECLARE_INSTRUCTION(InstanceFieldGet);
1453
1454 private:
1455 const FieldInfo field_info_;
1456
1457 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
1458};
1459
1460class HInstanceFieldSet : public HTemplateInstruction<2> {
1461 public:
1462 HInstanceFieldSet(HInstruction* object,
1463 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01001464 Primitive::Type field_type,
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001465 MemberOffset field_offset)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001466 : HTemplateInstruction(SideEffects::ChangesSomething()),
1467 field_info_(field_offset, field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001468 SetRawInputAt(0, object);
1469 SetRawInputAt(1, value);
1470 }
1471
1472 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001473 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001474
1475 DECLARE_INSTRUCTION(InstanceFieldSet);
1476
1477 private:
1478 const FieldInfo field_info_;
1479
1480 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
1481};
1482
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001483class HArrayGet : public HExpression<2> {
1484 public:
1485 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001486 : HExpression(type, SideEffects::DependsOnSomething()) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001487 SetRawInputAt(0, array);
1488 SetRawInputAt(1, index);
1489 }
1490
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001491 virtual bool CanBeMoved() const { return true; }
1492 virtual bool InstructionDataEquals(HInstruction* other) const { return true; }
1493
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001494 DECLARE_INSTRUCTION(ArrayGet);
1495
1496 private:
1497 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
1498};
1499
1500class HArraySet : public HTemplateInstruction<3> {
1501 public:
1502 HArraySet(HInstruction* array,
1503 HInstruction* index,
1504 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01001505 Primitive::Type component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001506 uint32_t dex_pc)
1507 : HTemplateInstruction(SideEffects::ChangesSomething()),
1508 dex_pc_(dex_pc),
1509 component_type_(component_type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001510 SetRawInputAt(0, array);
1511 SetRawInputAt(1, index);
1512 SetRawInputAt(2, value);
1513 }
1514
1515 virtual bool NeedsEnvironment() const {
1516 // We currently always call a runtime method to catch array store
1517 // exceptions.
1518 return InputAt(2)->GetType() == Primitive::kPrimNot;
1519 }
1520
1521 uint32_t GetDexPc() const { return dex_pc_; }
1522
Nicolas Geoffray39468442014-09-02 15:17:15 +01001523 Primitive::Type GetComponentType() const { return component_type_; }
1524
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001525 DECLARE_INSTRUCTION(ArraySet);
1526
1527 private:
1528 const uint32_t dex_pc_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01001529 const Primitive::Type component_type_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001530
1531 DISALLOW_COPY_AND_ASSIGN(HArraySet);
1532};
1533
1534class HArrayLength : public HExpression<1> {
1535 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001536 explicit HArrayLength(HInstruction* array)
1537 : HExpression(Primitive::kPrimInt, SideEffects::None()) {
1538 // Note that arrays do not change length, so the instruction does not
1539 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001540 SetRawInputAt(0, array);
1541 }
1542
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001543 virtual bool CanBeMoved() const { return true; }
1544 virtual bool InstructionDataEquals(HInstruction* other) const { return true; }
1545
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001546 DECLARE_INSTRUCTION(ArrayLength);
1547
1548 private:
1549 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
1550};
1551
1552class HBoundsCheck : public HExpression<2> {
1553 public:
1554 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001555 : HExpression(index->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001556 DCHECK(index->GetType() == Primitive::kPrimInt);
1557 SetRawInputAt(0, index);
1558 SetRawInputAt(1, length);
1559 }
1560
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001561 virtual bool CanBeMoved() const { return true; }
1562 virtual bool InstructionDataEquals(HInstruction* other) const { return true; }
1563
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001564 virtual bool NeedsEnvironment() const { return true; }
1565
1566 uint32_t GetDexPc() const { return dex_pc_; }
1567
1568 DECLARE_INSTRUCTION(BoundsCheck);
1569
1570 private:
1571 const uint32_t dex_pc_;
1572
1573 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
1574};
1575
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001576/**
1577 * Some DEX instructions are folded into multiple HInstructions that need
1578 * to stay live until the last HInstruction. This class
1579 * is used as a marker for the baseline compiler to ensure its preceding
1580 * HInstruction stays live. `index` is the temporary number that is used
1581 * for knowing the stack offset where to store the instruction.
1582 */
1583class HTemporary : public HTemplateInstruction<0> {
1584 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001585 explicit HTemporary(size_t index) : HTemplateInstruction(SideEffects::None()), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001586
1587 size_t GetIndex() const { return index_; }
1588
1589 DECLARE_INSTRUCTION(Temporary);
1590
1591 private:
1592 const size_t index_;
1593
1594 DISALLOW_COPY_AND_ASSIGN(HTemporary);
1595};
1596
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001597class HSuspendCheck : public HTemplateInstruction<0> {
1598 public:
1599 explicit HSuspendCheck(uint32_t dex_pc)
1600 : HTemplateInstruction(SideEffects::ChangesSomething()), dex_pc_(dex_pc) {}
1601
1602 virtual bool NeedsEnvironment() const {
1603 return true;
1604 }
1605
1606 uint32_t GetDexPc() const { return dex_pc_; }
1607
1608 DECLARE_INSTRUCTION(SuspendCheck);
1609
1610 private:
1611 const uint32_t dex_pc_;
1612
1613 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
1614};
1615
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001616class MoveOperands : public ArenaObject {
1617 public:
1618 MoveOperands(Location source, Location destination)
1619 : source_(source), destination_(destination) {}
1620
1621 Location GetSource() const { return source_; }
1622 Location GetDestination() const { return destination_; }
1623
1624 void SetSource(Location value) { source_ = value; }
1625 void SetDestination(Location value) { destination_ = value; }
1626
1627 // The parallel move resolver marks moves as "in-progress" by clearing the
1628 // destination (but not the source).
1629 Location MarkPending() {
1630 DCHECK(!IsPending());
1631 Location dest = destination_;
1632 destination_ = Location::NoLocation();
1633 return dest;
1634 }
1635
1636 void ClearPending(Location dest) {
1637 DCHECK(IsPending());
1638 destination_ = dest;
1639 }
1640
1641 bool IsPending() const {
1642 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
1643 return destination_.IsInvalid() && !source_.IsInvalid();
1644 }
1645
1646 // True if this blocks a move from the given location.
1647 bool Blocks(Location loc) const {
1648 return !IsEliminated() && source_.Equals(loc);
1649 }
1650
1651 // A move is redundant if it's been eliminated, if its source and
1652 // destination are the same, or if its destination is unneeded.
1653 bool IsRedundant() const {
1654 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
1655 }
1656
1657 // We clear both operands to indicate move that's been eliminated.
1658 void Eliminate() {
1659 source_ = destination_ = Location::NoLocation();
1660 }
1661
1662 bool IsEliminated() const {
1663 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
1664 return source_.IsInvalid();
1665 }
1666
1667 private:
1668 Location source_;
1669 Location destination_;
1670
1671 DISALLOW_COPY_AND_ASSIGN(MoveOperands);
1672};
1673
1674static constexpr size_t kDefaultNumberOfMoves = 4;
1675
1676class HParallelMove : public HTemplateInstruction<0> {
1677 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001678 explicit HParallelMove(ArenaAllocator* arena)
1679 : HTemplateInstruction(SideEffects::None()), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001680
1681 void AddMove(MoveOperands* move) {
1682 moves_.Add(move);
1683 }
1684
1685 MoveOperands* MoveOperandsAt(size_t index) const {
1686 return moves_.Get(index);
1687 }
1688
1689 size_t NumMoves() const { return moves_.Size(); }
1690
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001691 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001692
1693 private:
1694 GrowableArray<MoveOperands*> moves_;
1695
1696 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
1697};
1698
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001699class HGraphVisitor : public ValueObject {
1700 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001701 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
1702 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001703
Dave Allison20dfc792014-06-16 20:44:29 -07001704 virtual void VisitInstruction(HInstruction* instruction) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001705 virtual void VisitBasicBlock(HBasicBlock* block);
1706
1707 void VisitInsertionOrder();
1708
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001709 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001710
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001711 // Visit functions for instruction classes.
1712#define DECLARE_VISIT_INSTRUCTION(name) \
1713 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
1714
1715 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
1716
1717#undef DECLARE_VISIT_INSTRUCTION
1718
1719 private:
1720 HGraph* graph_;
1721
1722 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
1723};
1724
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001725class HInsertionOrderIterator : public ValueObject {
1726 public:
1727 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
1728
1729 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
1730 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
1731 void Advance() { ++index_; }
1732
1733 private:
1734 const HGraph& graph_;
1735 size_t index_;
1736
1737 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
1738};
1739
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001740class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001741 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001742 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001743
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001744 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
1745 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001746 void Advance() { ++index_; }
1747
1748 private:
1749 const HGraph& graph_;
1750 size_t index_;
1751
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001752 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001753};
1754
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001755class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001756 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001757 explicit HPostOrderIterator(const HGraph& graph)
1758 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001759
1760 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001761 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001762 void Advance() { --index_; }
1763
1764 private:
1765 const HGraph& graph_;
1766 size_t index_;
1767
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001768 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001769};
1770
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001771} // namespace art
1772
1773#endif // ART_COMPILER_OPTIMIZING_NODES_H_