blob: 9018fee0a581421de13b35182c7554533982f862 [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 Geoffray818f2102014-02-18 16:43:35 +0000446
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100447#define FOR_EACH_INSTRUCTION(M) \
448 FOR_EACH_CONCRETE_INSTRUCTION(M) \
449 M(Constant)
Dave Allison20dfc792014-06-16 20:44:29 -0700450
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000451#define FORWARD_DECLARATION(type) class H##type;
452FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
453#undef FORWARD_DECLARATION
454
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000455#define DECLARE_INSTRUCTION(type) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000456 virtual const char* DebugName() const { return #type; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000457 virtual H##type* As##type() { return this; } \
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100458 virtual bool InstructionTypeEquals(HInstruction* other) const { \
459 return other->Is##type(); \
460 } \
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100461 virtual void Accept(HGraphVisitor* visitor) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000462
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100463template <typename T>
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000464class HUseListNode : public ArenaObject {
465 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100466 HUseListNode(T* user, size_t index, HUseListNode* tail)
Dave Allison20dfc792014-06-16 20:44:29 -0700467 : user_(user), index_(index), tail_(tail) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000468
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000469 HUseListNode* GetTail() const { return tail_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100470 T* GetUser() const { return user_; }
471 size_t GetIndex() const { return index_; }
472
473 void SetTail(HUseListNode<T>* node) { tail_ = node; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000474
475 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100476 T* const user_;
477 const size_t index_;
478 HUseListNode<T>* tail_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000479
480 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
481};
482
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100483// Represents the side effects an instruction may have.
484class SideEffects : public ValueObject {
485 public:
486 static SideEffects None() {
487 return SideEffects(0);
488 }
489
490 static SideEffects All() {
491 return SideEffects(ChangesSomething().flags_ | DependsOnSomething().flags_);
492 }
493
494 static SideEffects ChangesSomething() {
495 return SideEffects((1 << kFlagChangesCount) - 1);
496 }
497
498 static SideEffects DependsOnSomething() {
499 int count = kFlagDependsOnCount - kFlagChangesCount;
500 return SideEffects(((1 << count) - 1) << kFlagChangesCount);
501 }
502
503 private:
504 static constexpr int kFlagChangesSomething = 0;
505 static constexpr int kFlagChangesCount = kFlagChangesSomething + 1;
506
507 static constexpr int kFlagDependsOnSomething = kFlagChangesCount;
508 static constexpr int kFlagDependsOnCount = kFlagDependsOnSomething + 1;
509
510 private:
511 explicit SideEffects(size_t flags) : flags_(flags) {}
512
513 const size_t flags_;
514};
515
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000516class HInstruction : public ArenaObject {
517 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100518 explicit HInstruction(SideEffects side_effects)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000519 : previous_(nullptr),
520 next_(nullptr),
521 block_(nullptr),
522 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100523 ssa_index_(-1),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000524 uses_(nullptr),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100525 env_uses_(nullptr),
526 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100527 locations_(nullptr),
528 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100529 lifetime_position_(kNoLifetime),
530 side_effects_(side_effects) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000531
Dave Allison20dfc792014-06-16 20:44:29 -0700532 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000533
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000534 HInstruction* GetNext() const { return next_; }
535 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000536
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000537 HBasicBlock* GetBlock() const { return block_; }
538 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100539 bool IsInBlock() const { return block_ != nullptr; }
540 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +0100541 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000542
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100543 virtual size_t InputCount() const = 0;
544 virtual HInstruction* InputAt(size_t i) const = 0;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000545
546 virtual void Accept(HGraphVisitor* visitor) = 0;
547 virtual const char* DebugName() const = 0;
548
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100549 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100550 virtual void SetRawInputAt(size_t index, HInstruction* input) = 0;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100551
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100552 virtual bool NeedsEnvironment() const { return false; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100553 virtual bool IsControlFlow() const { return false; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100554
555 void AddUseAt(HInstruction* user, size_t index) {
556 uses_ = new (block_->GetGraph()->GetArena()) HUseListNode<HInstruction>(user, index, uses_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000557 }
558
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100559 void AddEnvUseAt(HEnvironment* user, size_t index) {
560 env_uses_ = new (block_->GetGraph()->GetArena()) HUseListNode<HEnvironment>(
561 user, index, env_uses_);
562 }
563
564 void RemoveUser(HInstruction* user, size_t index);
565
566 HUseListNode<HInstruction>* GetUses() const { return uses_; }
567 HUseListNode<HEnvironment>* GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000568
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100569 bool HasUses() const { return uses_ != nullptr || env_uses_ != nullptr; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100570 bool HasEnvironmentUses() const { return env_uses_ != nullptr; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000571
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100572 size_t NumberOfUses() const {
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100573 // TODO: Optimize this method if it is used outside of the HGraphVisualizer.
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100574 size_t result = 0;
575 HUseListNode<HInstruction>* current = uses_;
576 while (current != nullptr) {
577 current = current->GetTail();
578 ++result;
579 }
580 return result;
581 }
582
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000583 int GetId() const { return id_; }
584 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000585
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100586 int GetSsaIndex() const { return ssa_index_; }
587 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
588 bool HasSsaIndex() const { return ssa_index_ != -1; }
589
590 bool HasEnvironment() const { return environment_ != nullptr; }
591 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100592 void SetEnvironment(HEnvironment* environment) { environment_ = environment; }
593
Nicolas Geoffray39468442014-09-02 15:17:15 +0100594 // Returns the number of entries in the environment. Typically, that is the
595 // number of dex registers in a method. It could be more in case of inlining.
596 size_t EnvironmentSize() const;
597
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000598 LocationSummary* GetLocations() const { return locations_; }
599 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000600
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100601 void ReplaceWith(HInstruction* instruction);
602
Dave Allison20dfc792014-06-16 20:44:29 -0700603 bool HasOnlyOneUse() const {
604 return uses_ != nullptr && uses_->GetTail() == nullptr;
605 }
606
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000607#define INSTRUCTION_TYPE_CHECK(type) \
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100608 bool Is##type() { return (As##type() != nullptr); } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000609 virtual H##type* As##type() { return nullptr; }
610
611 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
612#undef INSTRUCTION_TYPE_CHECK
613
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100614 // Returns whether the instruction can be moved within the graph.
615 virtual bool CanBeMoved() const { return false; }
616
617 // Returns whether the two instructions are of the same kind.
618 virtual bool InstructionTypeEquals(HInstruction* other) const { return false; }
619
620 // Returns whether any data encoded in the two instructions is equal.
621 // This method does not look at the inputs. Both instructions must be
622 // of the same type, otherwise the method has undefined behavior.
623 virtual bool InstructionDataEquals(HInstruction* other) const { return false; }
624
625 // Returns whether two instructions are equal, that is:
626 // 1) They have the same type and contain the same data,
627 // 2) Their inputs are identical.
628 bool Equals(HInstruction* other) const;
629
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100630 size_t GetLifetimePosition() const { return lifetime_position_; }
631 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
632 LiveInterval* GetLiveInterval() const { return live_interval_; }
633 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
634 bool HasLiveInterval() const { return live_interval_ != nullptr; }
635
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000636 private:
637 HInstruction* previous_;
638 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000639 HBasicBlock* block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000640
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000641 // An instruction gets an id when it is added to the graph.
642 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +0100643 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000644 int id_;
645
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100646 // When doing liveness analysis, instructions that have uses get an SSA index.
647 int ssa_index_;
648
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100649 // List of instructions that have this instruction as input.
650 HUseListNode<HInstruction>* uses_;
651
652 // List of environments that contain this instruction.
653 HUseListNode<HEnvironment>* env_uses_;
654
Nicolas Geoffray39468442014-09-02 15:17:15 +0100655 // The environment associated with this instruction. Not null if the instruction
656 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100657 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000658
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000659 // Set by the code generator.
660 LocationSummary* locations_;
661
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100662 // Set by the liveness analysis.
663 LiveInterval* live_interval_;
664
665 // Set by the liveness analysis, this is the position in a linear
666 // order of blocks where this instruction's live interval start.
667 size_t lifetime_position_;
668
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100669 const SideEffects side_effects_;
670
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000671 friend class HBasicBlock;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100672 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000673
674 DISALLOW_COPY_AND_ASSIGN(HInstruction);
675};
676
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100677template<typename T>
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000678class HUseIterator : public ValueObject {
679 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100680 explicit HUseIterator(HUseListNode<T>* uses) : current_(uses) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000681
682 bool Done() const { return current_ == nullptr; }
683
684 void Advance() {
685 DCHECK(!Done());
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000686 current_ = current_->GetTail();
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000687 }
688
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100689 HUseListNode<T>* Current() const {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000690 DCHECK(!Done());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100691 return current_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000692 }
693
694 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100695 HUseListNode<T>* current_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000696
697 friend class HValue;
698};
699
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100700// A HEnvironment object contains the values of virtual registers at a given location.
701class HEnvironment : public ArenaObject {
702 public:
703 HEnvironment(ArenaAllocator* arena, size_t number_of_vregs) : vregs_(arena, number_of_vregs) {
704 vregs_.SetSize(number_of_vregs);
705 for (size_t i = 0; i < number_of_vregs; i++) {
706 vregs_.Put(i, nullptr);
707 }
708 }
709
710 void Populate(const GrowableArray<HInstruction*>& env) {
711 for (size_t i = 0; i < env.Size(); i++) {
712 HInstruction* instruction = env.Get(i);
713 vregs_.Put(i, instruction);
714 if (instruction != nullptr) {
715 instruction->AddEnvUseAt(this, i);
716 }
717 }
718 }
719
720 void SetRawEnvAt(size_t index, HInstruction* instruction) {
721 vregs_.Put(index, instruction);
722 }
723
Nicolas Geoffray39468442014-09-02 15:17:15 +0100724 HInstruction* GetInstructionAt(size_t index) const {
725 return vregs_.Get(index);
726 }
727
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100728 GrowableArray<HInstruction*>* GetVRegs() {
729 return &vregs_;
730 }
731
Nicolas Geoffray39468442014-09-02 15:17:15 +0100732 size_t Size() const { return vregs_.Size(); }
733
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100734 private:
735 GrowableArray<HInstruction*> vregs_;
736
737 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
738};
739
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000740class HInputIterator : public ValueObject {
741 public:
Dave Allison20dfc792014-06-16 20:44:29 -0700742 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000743
744 bool Done() const { return index_ == instruction_->InputCount(); }
745 HInstruction* Current() const { return instruction_->InputAt(index_); }
746 void Advance() { index_++; }
747
748 private:
749 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100750 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000751
752 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
753};
754
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000755class HInstructionIterator : public ValueObject {
756 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100757 explicit HInstructionIterator(const HInstructionList& instructions)
758 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000759 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000760 }
761
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000762 bool Done() const { return instruction_ == nullptr; }
763 HInstruction* Current() const { return instruction_; }
764 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000765 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000766 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000767 }
768
769 private:
770 HInstruction* instruction_;
771 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100772
773 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000774};
775
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100776class HBackwardInstructionIterator : public ValueObject {
777 public:
778 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
779 : instruction_(instructions.last_instruction_) {
780 next_ = Done() ? nullptr : instruction_->GetPrevious();
781 }
782
783 bool Done() const { return instruction_ == nullptr; }
784 HInstruction* Current() const { return instruction_; }
785 void Advance() {
786 instruction_ = next_;
787 next_ = Done() ? nullptr : instruction_->GetPrevious();
788 }
789
790 private:
791 HInstruction* instruction_;
792 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100793
794 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100795};
796
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000797// An embedded container with N elements of type T. Used (with partial
798// specialization for N=0) because embedded arrays cannot have size 0.
799template<typename T, intptr_t N>
800class EmbeddedArray {
801 public:
Dave Allison20dfc792014-06-16 20:44:29 -0700802 EmbeddedArray() : elements_() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000803
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000804 intptr_t GetLength() const { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000805
806 const T& operator[](intptr_t i) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000807 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000808 return elements_[i];
809 }
810
811 T& operator[](intptr_t i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000812 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000813 return elements_[i];
814 }
815
816 const T& At(intptr_t i) const {
817 return (*this)[i];
818 }
819
820 void SetAt(intptr_t i, const T& val) {
821 (*this)[i] = val;
822 }
823
824 private:
825 T elements_[N];
826};
827
828template<typename T>
829class EmbeddedArray<T, 0> {
830 public:
831 intptr_t length() const { return 0; }
832 const T& operator[](intptr_t i) const {
833 LOG(FATAL) << "Unreachable";
834 static T sentinel = 0;
835 return sentinel;
836 }
837 T& operator[](intptr_t i) {
838 LOG(FATAL) << "Unreachable";
839 static T sentinel = 0;
840 return sentinel;
841 }
842};
843
844template<intptr_t N>
845class HTemplateInstruction: public HInstruction {
846 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100847 HTemplateInstruction<N>(SideEffects side_effects)
848 : HInstruction(side_effects), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -0700849 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000850
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100851 virtual size_t InputCount() const { return N; }
852 virtual HInstruction* InputAt(size_t i) const { return inputs_[i]; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000853
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000854 protected:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100855 virtual void SetRawInputAt(size_t i, HInstruction* instruction) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000856 inputs_[i] = instruction;
857 }
858
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000859 private:
860 EmbeddedArray<HInstruction*, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100861
862 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000863};
864
Dave Allison20dfc792014-06-16 20:44:29 -0700865template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100866class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -0700867 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100868 HExpression<N>(Primitive::Type type, SideEffects side_effects)
869 : HTemplateInstruction<N>(side_effects), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -0700870 virtual ~HExpression() {}
871
872 virtual Primitive::Type GetType() const { return type_; }
873
874 private:
875 const Primitive::Type type_;
876};
877
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000878// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
879// instruction that branches to the exit block.
880class HReturnVoid : public HTemplateInstruction<0> {
881 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100882 HReturnVoid() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100883
884 virtual bool IsControlFlow() const { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000885
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100886 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000887
888 private:
889 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
890};
891
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000892// Represents dex's RETURN opcodes. A HReturn is a control flow
893// instruction that branches to the exit block.
894class HReturn : public HTemplateInstruction<1> {
895 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100896 explicit HReturn(HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000897 SetRawInputAt(0, value);
898 }
899
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100900 virtual bool IsControlFlow() const { return true; }
901
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100902 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000903
904 private:
905 DISALLOW_COPY_AND_ASSIGN(HReturn);
906};
907
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000908// The exit instruction is the only instruction of the exit block.
909// Instructions aborting the method (HTrow and HReturn) must branch to the
910// exit block.
911class HExit : public HTemplateInstruction<0> {
912 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100913 HExit() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100914
915 virtual bool IsControlFlow() const { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000916
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100917 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000918
919 private:
920 DISALLOW_COPY_AND_ASSIGN(HExit);
921};
922
923// Jumps from one block to another.
924class HGoto : public HTemplateInstruction<0> {
925 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100926 HGoto() : HTemplateInstruction(SideEffects::None()) {}
927
928 virtual bool IsControlFlow() const { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000929
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000930 HBasicBlock* GetSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100931 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000932 }
933
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100934 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000935
936 private:
937 DISALLOW_COPY_AND_ASSIGN(HGoto);
938};
939
Dave Allison20dfc792014-06-16 20:44:29 -0700940
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000941// Conditional branch. A block ending with an HIf instruction must have
942// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000943class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000944 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100945 explicit HIf(HInstruction* input) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000946 SetRawInputAt(0, input);
947 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000948
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100949 virtual bool IsControlFlow() const { return true; }
950
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000951 HBasicBlock* IfTrueSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100952 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000953 }
954
955 HBasicBlock* IfFalseSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100956 return GetBlock()->GetSuccessors().Get(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000957 }
958
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100959 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000960
Dave Allison20dfc792014-06-16 20:44:29 -0700961 virtual bool IsIfInstruction() const { return true; }
962
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000963 private:
964 DISALLOW_COPY_AND_ASSIGN(HIf);
965};
966
Dave Allison20dfc792014-06-16 20:44:29 -0700967class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000968 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000969 HBinaryOperation(Primitive::Type result_type,
970 HInstruction* left,
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100971 HInstruction* right) : HExpression(result_type, SideEffects::None()) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000972 SetRawInputAt(0, left);
973 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000974 }
975
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000976 HInstruction* GetLeft() const { return InputAt(0); }
977 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -0700978 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000979
980 virtual bool IsCommutative() { return false; }
981
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100982 virtual bool CanBeMoved() const { return true; }
983 virtual bool InstructionDataEquals(HInstruction* other) const { return true; }
984
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000985 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000986 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
987};
988
Dave Allison20dfc792014-06-16 20:44:29 -0700989class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000990 public:
Dave Allison20dfc792014-06-16 20:44:29 -0700991 HCondition(HInstruction* first, HInstruction* second)
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000992 : HBinaryOperation(Primitive::kPrimBoolean, first, second) {}
993
994 virtual bool IsCommutative() { return true; }
Dave Allison20dfc792014-06-16 20:44:29 -0700995 bool NeedsMaterialization() const;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000996
Dave Allison20dfc792014-06-16 20:44:29 -0700997 DECLARE_INSTRUCTION(Condition);
998
999 virtual IfCondition GetCondition() const = 0;
1000
1001 private:
1002 DISALLOW_COPY_AND_ASSIGN(HCondition);
1003};
1004
1005// Instruction to check if two inputs are equal to each other.
1006class HEqual : public HCondition {
1007 public:
1008 HEqual(HInstruction* first, HInstruction* second)
1009 : HCondition(first, second) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001010
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001011 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001012
Dave Allison20dfc792014-06-16 20:44:29 -07001013 virtual IfCondition GetCondition() const {
1014 return kCondEQ;
1015 }
1016
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001017 private:
1018 DISALLOW_COPY_AND_ASSIGN(HEqual);
1019};
1020
Dave Allison20dfc792014-06-16 20:44:29 -07001021class HNotEqual : public HCondition {
1022 public:
1023 HNotEqual(HInstruction* first, HInstruction* second)
1024 : HCondition(first, second) {}
1025
1026 DECLARE_INSTRUCTION(NotEqual);
1027
1028 virtual IfCondition GetCondition() const {
1029 return kCondNE;
1030 }
1031
1032 private:
1033 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
1034};
1035
1036class HLessThan : public HCondition {
1037 public:
1038 HLessThan(HInstruction* first, HInstruction* second)
1039 : HCondition(first, second) {}
1040
1041 DECLARE_INSTRUCTION(LessThan);
1042
1043 virtual IfCondition GetCondition() const {
1044 return kCondLT;
1045 }
1046
1047 private:
1048 DISALLOW_COPY_AND_ASSIGN(HLessThan);
1049};
1050
1051class HLessThanOrEqual : public HCondition {
1052 public:
1053 HLessThanOrEqual(HInstruction* first, HInstruction* second)
1054 : HCondition(first, second) {}
1055
1056 DECLARE_INSTRUCTION(LessThanOrEqual);
1057
1058 virtual IfCondition GetCondition() const {
1059 return kCondLE;
1060 }
1061
1062 private:
1063 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
1064};
1065
1066class HGreaterThan : public HCondition {
1067 public:
1068 HGreaterThan(HInstruction* first, HInstruction* second)
1069 : HCondition(first, second) {}
1070
1071 DECLARE_INSTRUCTION(GreaterThan);
1072
1073 virtual IfCondition GetCondition() const {
1074 return kCondGT;
1075 }
1076
1077 private:
1078 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
1079};
1080
1081class HGreaterThanOrEqual : public HCondition {
1082 public:
1083 HGreaterThanOrEqual(HInstruction* first, HInstruction* second)
1084 : HCondition(first, second) {}
1085
1086 DECLARE_INSTRUCTION(GreaterThanOrEqual);
1087
1088 virtual IfCondition GetCondition() const {
1089 return kCondGE;
1090 }
1091
1092 private:
1093 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
1094};
1095
1096
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001097// Instruction to check how two inputs compare to each other.
1098// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
1099class HCompare : public HBinaryOperation {
1100 public:
1101 HCompare(Primitive::Type type, HInstruction* first, HInstruction* second)
1102 : HBinaryOperation(Primitive::kPrimInt, first, second) {
1103 DCHECK_EQ(type, first->GetType());
1104 DCHECK_EQ(type, second->GetType());
1105 }
1106
1107 DECLARE_INSTRUCTION(Compare);
1108
1109 private:
1110 DISALLOW_COPY_AND_ASSIGN(HCompare);
1111};
1112
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001113// A local in the graph. Corresponds to a Dex register.
1114class HLocal : public HTemplateInstruction<0> {
1115 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001116 explicit HLocal(uint16_t reg_number)
1117 : HTemplateInstruction(SideEffects::None()), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001118
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001119 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001120
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001121 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001122
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001123 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001124 // The Dex register number.
1125 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001126
1127 DISALLOW_COPY_AND_ASSIGN(HLocal);
1128};
1129
1130// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07001131class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001132 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001133 explicit HLoadLocal(HLocal* local, Primitive::Type type)
1134 : HExpression(type, SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001135 SetRawInputAt(0, local);
1136 }
1137
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001138 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1139
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001140 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001141
1142 private:
1143 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
1144};
1145
1146// Store a value in a given local. This instruction has two inputs: the value
1147// and the local.
1148class HStoreLocal : public HTemplateInstruction<2> {
1149 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001150 HStoreLocal(HLocal* local, HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001151 SetRawInputAt(0, local);
1152 SetRawInputAt(1, value);
1153 }
1154
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001155 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1156
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001157 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001158
1159 private:
1160 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
1161};
1162
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001163class HConstant : public HExpression<0> {
1164 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001165 explicit HConstant(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
1166
1167 virtual bool CanBeMoved() const { return true; }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001168
1169 DECLARE_INSTRUCTION(Constant);
1170
1171 private:
1172 DISALLOW_COPY_AND_ASSIGN(HConstant);
1173};
1174
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001175// Constants of the type int. Those can be from Dex instructions, or
1176// synthesized (for example with the if-eqz instruction).
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001177class HIntConstant : public HConstant {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001178 public:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001179 explicit HIntConstant(int32_t value) : HConstant(Primitive::kPrimInt), value_(value) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001180
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001181 int32_t GetValue() const { return value_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001182
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001183 virtual bool InstructionDataEquals(HInstruction* other) const {
1184 return other->AsIntConstant()->value_ == value_;
1185 }
1186
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001187 DECLARE_INSTRUCTION(IntConstant);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001188
1189 private:
1190 const int32_t value_;
1191
1192 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
1193};
1194
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001195class HLongConstant : public HConstant {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001196 public:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001197 explicit HLongConstant(int64_t value) : HConstant(Primitive::kPrimLong), value_(value) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001198
1199 int64_t GetValue() const { return value_; }
1200
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001201 virtual bool InstructionDataEquals(HInstruction* other) const {
1202 return other->AsLongConstant()->value_ == value_;
1203 }
1204
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001205 DECLARE_INSTRUCTION(LongConstant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001206
1207 private:
1208 const int64_t value_;
1209
1210 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
1211};
1212
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001213class HInvoke : public HInstruction {
1214 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001215 HInvoke(ArenaAllocator* arena,
1216 uint32_t number_of_arguments,
1217 Primitive::Type return_type,
1218 uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001219 : HInstruction(SideEffects::All()),
1220 inputs_(arena, number_of_arguments),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001221 return_type_(return_type),
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001222 dex_pc_(dex_pc) {
1223 inputs_.SetSize(number_of_arguments);
1224 }
1225
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001226 virtual size_t InputCount() const { return inputs_.Size(); }
1227 virtual HInstruction* InputAt(size_t i) const { return inputs_.Get(i); }
1228
1229 // Runtime needs to walk the stack, so Dex -> Dex calls need to
1230 // know their environment.
1231 virtual bool NeedsEnvironment() const { return true; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001232
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001233 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001234 SetRawInputAt(index, argument);
1235 }
1236
1237 virtual void SetRawInputAt(size_t index, HInstruction* input) {
1238 inputs_.Put(index, input);
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001239 }
1240
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001241 virtual Primitive::Type GetType() const { return return_type_; }
1242
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001243 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001244
1245 protected:
1246 GrowableArray<HInstruction*> inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001247 const Primitive::Type return_type_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001248 const uint32_t dex_pc_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001249
1250 private:
1251 DISALLOW_COPY_AND_ASSIGN(HInvoke);
1252};
1253
1254class HInvokeStatic : public HInvoke {
1255 public:
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001256 HInvokeStatic(ArenaAllocator* arena,
1257 uint32_t number_of_arguments,
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001258 Primitive::Type return_type,
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001259 uint32_t dex_pc,
1260 uint32_t index_in_dex_cache)
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001261 : HInvoke(arena, number_of_arguments, return_type, dex_pc),
1262 index_in_dex_cache_(index_in_dex_cache) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001263
1264 uint32_t GetIndexInDexCache() const { return index_in_dex_cache_; }
1265
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001266 DECLARE_INSTRUCTION(InvokeStatic);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001267
1268 private:
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001269 const uint32_t index_in_dex_cache_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001270
1271 DISALLOW_COPY_AND_ASSIGN(HInvokeStatic);
1272};
1273
Dave Allison20dfc792014-06-16 20:44:29 -07001274class HNewInstance : public HExpression<0> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001275 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001276 HNewInstance(uint32_t dex_pc, uint16_t type_index)
1277 : HExpression(Primitive::kPrimNot, SideEffects::None()),
1278 dex_pc_(dex_pc),
1279 type_index_(type_index) {}
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001280
1281 uint32_t GetDexPc() const { return dex_pc_; }
1282 uint16_t GetTypeIndex() const { return type_index_; }
1283
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001284 // Calls runtime so needs an environment.
1285 virtual bool NeedsEnvironment() const { return true; }
1286
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001287 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001288
1289 private:
1290 const uint32_t dex_pc_;
1291 const uint16_t type_index_;
1292
1293 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
1294};
1295
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001296class HAdd : public HBinaryOperation {
1297 public:
1298 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1299 : HBinaryOperation(result_type, left, right) {}
1300
1301 virtual bool IsCommutative() { return true; }
1302
1303 DECLARE_INSTRUCTION(Add);
1304
1305 private:
1306 DISALLOW_COPY_AND_ASSIGN(HAdd);
1307};
1308
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001309class HSub : public HBinaryOperation {
1310 public:
1311 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1312 : HBinaryOperation(result_type, left, right) {}
1313
1314 virtual bool IsCommutative() { return false; }
1315
1316 DECLARE_INSTRUCTION(Sub);
1317
1318 private:
1319 DISALLOW_COPY_AND_ASSIGN(HSub);
1320};
1321
1322// The value of a parameter in this method. Its location depends on
1323// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07001324class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001325 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001326 HParameterValue(uint8_t index, Primitive::Type parameter_type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001327 : HExpression(parameter_type, SideEffects::None()), index_(index) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001328
1329 uint8_t GetIndex() const { return index_; }
1330
1331 DECLARE_INSTRUCTION(ParameterValue);
1332
1333 private:
1334 // The index of this parameter in the parameters list. Must be less
1335 // than HGraph::number_of_in_vregs_;
1336 const uint8_t index_;
1337
1338 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
1339};
1340
Dave Allison20dfc792014-06-16 20:44:29 -07001341class HNot : public HExpression<1> {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001342 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001343 explicit HNot(HInstruction* input) : HExpression(Primitive::kPrimBoolean, SideEffects::None()) {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001344 SetRawInputAt(0, input);
1345 }
1346
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001347 virtual bool CanBeMoved() const { return true; }
1348 virtual bool InstructionDataEquals(HInstruction* other) const { return true; }
1349
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001350 DECLARE_INSTRUCTION(Not);
1351
1352 private:
1353 DISALLOW_COPY_AND_ASSIGN(HNot);
1354};
1355
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001356class HPhi : public HInstruction {
1357 public:
1358 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001359 : HInstruction(SideEffects::None()),
1360 inputs_(arena, number_of_inputs),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001361 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001362 type_(type),
1363 is_live_(false) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001364 inputs_.SetSize(number_of_inputs);
1365 }
1366
1367 virtual size_t InputCount() const { return inputs_.Size(); }
1368 virtual HInstruction* InputAt(size_t i) const { return inputs_.Get(i); }
1369
1370 virtual void SetRawInputAt(size_t index, HInstruction* input) {
1371 inputs_.Put(index, input);
1372 }
1373
1374 void AddInput(HInstruction* input);
1375
1376 virtual Primitive::Type GetType() const { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001377 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001378
1379 uint32_t GetRegNumber() const { return reg_number_; }
1380
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001381 void SetDead() { is_live_ = false; }
1382 void SetLive() { is_live_ = true; }
1383 bool IsDead() const { return !is_live_; }
1384 bool IsLive() const { return is_live_; }
1385
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001386 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001387
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001388 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001389 GrowableArray<HInstruction*> inputs_;
1390 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001391 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001392 bool is_live_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001393
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001394 DISALLOW_COPY_AND_ASSIGN(HPhi);
1395};
1396
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001397class HNullCheck : public HExpression<1> {
1398 public:
1399 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001400 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001401 SetRawInputAt(0, value);
1402 }
1403
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001404 virtual bool CanBeMoved() const { return true; }
1405 virtual bool InstructionDataEquals(HInstruction* other) const { return true; }
1406
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001407 virtual bool NeedsEnvironment() const { return true; }
1408
1409 uint32_t GetDexPc() const { return dex_pc_; }
1410
1411 DECLARE_INSTRUCTION(NullCheck);
1412
1413 private:
1414 const uint32_t dex_pc_;
1415
1416 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
1417};
1418
1419class FieldInfo : public ValueObject {
1420 public:
Nicolas Geoffray39468442014-09-02 15:17:15 +01001421 explicit FieldInfo(MemberOffset field_offset, Primitive::Type field_type)
1422 : field_offset_(field_offset), field_type_(field_type) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001423
1424 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001425 Primitive::Type GetFieldType() const { return field_type_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001426
1427 private:
1428 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01001429 const Primitive::Type field_type_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001430};
1431
1432class HInstanceFieldGet : public HExpression<1> {
1433 public:
1434 HInstanceFieldGet(HInstruction* value,
1435 Primitive::Type field_type,
1436 MemberOffset field_offset)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001437 : HExpression(field_type, SideEffects::DependsOnSomething()),
1438 field_info_(field_offset, field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001439 SetRawInputAt(0, value);
1440 }
1441
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001442 virtual bool CanBeMoved() const { return true; }
1443 virtual bool InstructionDataEquals(HInstruction* other) const {
1444 size_t other_offset = other->AsInstanceFieldGet()->GetFieldOffset().SizeValue();
1445 return other_offset == GetFieldOffset().SizeValue();
1446 }
1447
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001448 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001449 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001450
1451 DECLARE_INSTRUCTION(InstanceFieldGet);
1452
1453 private:
1454 const FieldInfo field_info_;
1455
1456 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
1457};
1458
1459class HInstanceFieldSet : public HTemplateInstruction<2> {
1460 public:
1461 HInstanceFieldSet(HInstruction* object,
1462 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01001463 Primitive::Type field_type,
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001464 MemberOffset field_offset)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001465 : HTemplateInstruction(SideEffects::ChangesSomething()),
1466 field_info_(field_offset, field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001467 SetRawInputAt(0, object);
1468 SetRawInputAt(1, value);
1469 }
1470
1471 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001472 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001473
1474 DECLARE_INSTRUCTION(InstanceFieldSet);
1475
1476 private:
1477 const FieldInfo field_info_;
1478
1479 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
1480};
1481
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001482class HArrayGet : public HExpression<2> {
1483 public:
1484 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001485 : HExpression(type, SideEffects::DependsOnSomething()) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001486 SetRawInputAt(0, array);
1487 SetRawInputAt(1, index);
1488 }
1489
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001490 virtual bool CanBeMoved() const { return true; }
1491 virtual bool InstructionDataEquals(HInstruction* other) const { return true; }
1492
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001493 DECLARE_INSTRUCTION(ArrayGet);
1494
1495 private:
1496 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
1497};
1498
1499class HArraySet : public HTemplateInstruction<3> {
1500 public:
1501 HArraySet(HInstruction* array,
1502 HInstruction* index,
1503 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01001504 Primitive::Type component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001505 uint32_t dex_pc)
1506 : HTemplateInstruction(SideEffects::ChangesSomething()),
1507 dex_pc_(dex_pc),
1508 component_type_(component_type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001509 SetRawInputAt(0, array);
1510 SetRawInputAt(1, index);
1511 SetRawInputAt(2, value);
1512 }
1513
1514 virtual bool NeedsEnvironment() const {
1515 // We currently always call a runtime method to catch array store
1516 // exceptions.
1517 return InputAt(2)->GetType() == Primitive::kPrimNot;
1518 }
1519
1520 uint32_t GetDexPc() const { return dex_pc_; }
1521
Nicolas Geoffray39468442014-09-02 15:17:15 +01001522 Primitive::Type GetComponentType() const { return component_type_; }
1523
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001524 DECLARE_INSTRUCTION(ArraySet);
1525
1526 private:
1527 const uint32_t dex_pc_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01001528 const Primitive::Type component_type_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001529
1530 DISALLOW_COPY_AND_ASSIGN(HArraySet);
1531};
1532
1533class HArrayLength : public HExpression<1> {
1534 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001535 explicit HArrayLength(HInstruction* array)
1536 : HExpression(Primitive::kPrimInt, SideEffects::None()) {
1537 // Note that arrays do not change length, so the instruction does not
1538 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001539 SetRawInputAt(0, array);
1540 }
1541
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001542 virtual bool CanBeMoved() const { return true; }
1543 virtual bool InstructionDataEquals(HInstruction* other) const { return true; }
1544
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001545 DECLARE_INSTRUCTION(ArrayLength);
1546
1547 private:
1548 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
1549};
1550
1551class HBoundsCheck : public HExpression<2> {
1552 public:
1553 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001554 : HExpression(index->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001555 DCHECK(index->GetType() == Primitive::kPrimInt);
1556 SetRawInputAt(0, index);
1557 SetRawInputAt(1, length);
1558 }
1559
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001560 virtual bool CanBeMoved() const { return true; }
1561 virtual bool InstructionDataEquals(HInstruction* other) const { return true; }
1562
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001563 virtual bool NeedsEnvironment() const { return true; }
1564
1565 uint32_t GetDexPc() const { return dex_pc_; }
1566
1567 DECLARE_INSTRUCTION(BoundsCheck);
1568
1569 private:
1570 const uint32_t dex_pc_;
1571
1572 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
1573};
1574
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001575/**
1576 * Some DEX instructions are folded into multiple HInstructions that need
1577 * to stay live until the last HInstruction. This class
1578 * is used as a marker for the baseline compiler to ensure its preceding
1579 * HInstruction stays live. `index` is the temporary number that is used
1580 * for knowing the stack offset where to store the instruction.
1581 */
1582class HTemporary : public HTemplateInstruction<0> {
1583 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001584 explicit HTemporary(size_t index) : HTemplateInstruction(SideEffects::None()), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001585
1586 size_t GetIndex() const { return index_; }
1587
1588 DECLARE_INSTRUCTION(Temporary);
1589
1590 private:
1591 const size_t index_;
1592
1593 DISALLOW_COPY_AND_ASSIGN(HTemporary);
1594};
1595
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001596class MoveOperands : public ArenaObject {
1597 public:
1598 MoveOperands(Location source, Location destination)
1599 : source_(source), destination_(destination) {}
1600
1601 Location GetSource() const { return source_; }
1602 Location GetDestination() const { return destination_; }
1603
1604 void SetSource(Location value) { source_ = value; }
1605 void SetDestination(Location value) { destination_ = value; }
1606
1607 // The parallel move resolver marks moves as "in-progress" by clearing the
1608 // destination (but not the source).
1609 Location MarkPending() {
1610 DCHECK(!IsPending());
1611 Location dest = destination_;
1612 destination_ = Location::NoLocation();
1613 return dest;
1614 }
1615
1616 void ClearPending(Location dest) {
1617 DCHECK(IsPending());
1618 destination_ = dest;
1619 }
1620
1621 bool IsPending() const {
1622 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
1623 return destination_.IsInvalid() && !source_.IsInvalid();
1624 }
1625
1626 // True if this blocks a move from the given location.
1627 bool Blocks(Location loc) const {
1628 return !IsEliminated() && source_.Equals(loc);
1629 }
1630
1631 // A move is redundant if it's been eliminated, if its source and
1632 // destination are the same, or if its destination is unneeded.
1633 bool IsRedundant() const {
1634 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
1635 }
1636
1637 // We clear both operands to indicate move that's been eliminated.
1638 void Eliminate() {
1639 source_ = destination_ = Location::NoLocation();
1640 }
1641
1642 bool IsEliminated() const {
1643 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
1644 return source_.IsInvalid();
1645 }
1646
1647 private:
1648 Location source_;
1649 Location destination_;
1650
1651 DISALLOW_COPY_AND_ASSIGN(MoveOperands);
1652};
1653
1654static constexpr size_t kDefaultNumberOfMoves = 4;
1655
1656class HParallelMove : public HTemplateInstruction<0> {
1657 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001658 explicit HParallelMove(ArenaAllocator* arena)
1659 : HTemplateInstruction(SideEffects::None()), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001660
1661 void AddMove(MoveOperands* move) {
1662 moves_.Add(move);
1663 }
1664
1665 MoveOperands* MoveOperandsAt(size_t index) const {
1666 return moves_.Get(index);
1667 }
1668
1669 size_t NumMoves() const { return moves_.Size(); }
1670
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001671 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001672
1673 private:
1674 GrowableArray<MoveOperands*> moves_;
1675
1676 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
1677};
1678
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001679class HGraphVisitor : public ValueObject {
1680 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001681 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
1682 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001683
Dave Allison20dfc792014-06-16 20:44:29 -07001684 virtual void VisitInstruction(HInstruction* instruction) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001685 virtual void VisitBasicBlock(HBasicBlock* block);
1686
1687 void VisitInsertionOrder();
1688
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001689 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001690
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001691 // Visit functions for instruction classes.
1692#define DECLARE_VISIT_INSTRUCTION(name) \
1693 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
1694
1695 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
1696
1697#undef DECLARE_VISIT_INSTRUCTION
1698
1699 private:
1700 HGraph* graph_;
1701
1702 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
1703};
1704
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001705class HInsertionOrderIterator : public ValueObject {
1706 public:
1707 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
1708
1709 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
1710 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
1711 void Advance() { ++index_; }
1712
1713 private:
1714 const HGraph& graph_;
1715 size_t index_;
1716
1717 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
1718};
1719
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001720class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001721 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001722 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001723
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001724 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
1725 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001726 void Advance() { ++index_; }
1727
1728 private:
1729 const HGraph& graph_;
1730 size_t index_;
1731
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001732 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001733};
1734
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001735class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001736 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001737 explicit HPostOrderIterator(const HGraph& graph)
1738 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001739
1740 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001741 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001742 void Advance() { --index_; }
1743
1744 private:
1745 const HGraph& graph_;
1746 size_t index_;
1747
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001748 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001749};
1750
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001751} // namespace art
1752
1753#endif // ART_COMPILER_OPTIMIZING_NODES_H_