blob: b012cef3da754da445eb08c01f7391487fe3c1ce [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
Roland Levillainccc07a92014-09-16 14:48:16 +010059 // Return true if `instruction1` is found before `instruction2` in
60 // this instruction list and false otherwise. Abort if none
61 // of these instructions is found.
62 bool FoundBefore(const HInstruction* instruction1,
63 const HInstruction* instruction2) const;
64
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010065 private:
66 HInstruction* first_instruction_;
67 HInstruction* last_instruction_;
68
69 friend class HBasicBlock;
70 friend class HInstructionIterator;
Nicolas Geoffray804d0932014-05-02 08:46:00 +010071 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010072
73 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
74};
75
Nicolas Geoffray818f2102014-02-18 16:43:35 +000076// Control-flow graph of a method. Contains a list of basic blocks.
77class HGraph : public ArenaObject {
78 public:
79 explicit HGraph(ArenaAllocator* arena)
80 : arena_(arena),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000081 blocks_(arena, kDefaultNumberOfBlocks),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +010082 reverse_post_order_(arena, kDefaultNumberOfBlocks),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +010083 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +010084 number_of_vregs_(0),
85 number_of_in_vregs_(0),
Nicolas Geoffraye5038322014-07-04 09:41:32 +010086 number_of_temporaries_(0),
Dave Allison20dfc792014-06-16 20:44:29 -070087 current_instruction_id_(0) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +000088
Nicolas Geoffray787c3072014-03-17 10:20:19 +000089 ArenaAllocator* GetArena() const { return arena_; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +010090 const GrowableArray<HBasicBlock*>& GetBlocks() const { return blocks_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +000091
Nicolas Geoffray787c3072014-03-17 10:20:19 +000092 HBasicBlock* GetEntryBlock() const { return entry_block_; }
93 HBasicBlock* GetExitBlock() const { return exit_block_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000094
Nicolas Geoffray787c3072014-03-17 10:20:19 +000095 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
96 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000097
Nicolas Geoffray818f2102014-02-18 16:43:35 +000098 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010099
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000100 void BuildDominatorTree();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100101 void TransformToSSA();
102 void SimplifyCFG();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000103
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100104 // Find all natural loops in this graph. Aborts computation and returns false
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100105 // if one loop is not natural, that is the header does not dominate the back
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100106 // edge.
107 bool FindNaturalLoops() const;
108
109 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
110 void SimplifyLoop(HBasicBlock* header);
111
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000112 int GetNextInstructionId() {
113 return current_instruction_id_++;
114 }
115
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100116 uint16_t GetMaximumNumberOfOutVRegs() const {
117 return maximum_number_of_out_vregs_;
118 }
119
120 void UpdateMaximumNumberOfOutVRegs(uint16_t new_value) {
121 maximum_number_of_out_vregs_ = std::max(new_value, maximum_number_of_out_vregs_);
122 }
123
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100124 void UpdateNumberOfTemporaries(size_t count) {
125 number_of_temporaries_ = std::max(count, number_of_temporaries_);
126 }
127
128 size_t GetNumberOfTemporaries() const {
129 return number_of_temporaries_;
130 }
131
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100132 void SetNumberOfVRegs(uint16_t number_of_vregs) {
133 number_of_vregs_ = number_of_vregs;
134 }
135
136 uint16_t GetNumberOfVRegs() const {
137 return number_of_vregs_;
138 }
139
140 void SetNumberOfInVRegs(uint16_t value) {
141 number_of_in_vregs_ = value;
142 }
143
144 uint16_t GetNumberOfInVRegs() const {
145 return number_of_in_vregs_;
146 }
147
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100148 uint16_t GetNumberOfLocalVRegs() const {
149 return number_of_vregs_ - number_of_in_vregs_;
150 }
151
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100152 const GrowableArray<HBasicBlock*>& GetReversePostOrder() const {
153 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100154 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100155
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000156 private:
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000157 HBasicBlock* FindCommonDominator(HBasicBlock* first, HBasicBlock* second) const;
158 void VisitBlockForDominatorTree(HBasicBlock* block,
159 HBasicBlock* predecessor,
160 GrowableArray<size_t>* visits);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100161 void FindBackEdges(ArenaBitVector* visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000162 void VisitBlockForBackEdges(HBasicBlock* block,
163 ArenaBitVector* visited,
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100164 ArenaBitVector* visiting);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000165 void RemoveDeadBlocks(const ArenaBitVector& visited) const;
166
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000167 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000168
169 // List of blocks in insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000170 GrowableArray<HBasicBlock*> blocks_;
171
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100172 // List of blocks to perform a reverse post order tree traversal.
173 GrowableArray<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000174
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000175 HBasicBlock* entry_block_;
176 HBasicBlock* exit_block_;
177
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100178 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100179 uint16_t maximum_number_of_out_vregs_;
180
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100181 // The number of virtual registers in this method. Contains the parameters.
182 uint16_t number_of_vregs_;
183
184 // The number of virtual registers used by parameters of this method.
185 uint16_t number_of_in_vregs_;
186
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100187 // The number of temporaries that will be needed for the baseline compiler.
188 size_t number_of_temporaries_;
189
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000190 // The current id to assign to a newly added instruction. See HInstruction.id_.
191 int current_instruction_id_;
192
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000193 DISALLOW_COPY_AND_ASSIGN(HGraph);
194};
195
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000196class HLoopInformation : public ArenaObject {
197 public:
198 HLoopInformation(HBasicBlock* header, HGraph* graph)
199 : header_(header),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100200 back_edges_(graph->GetArena(), kDefaultNumberOfBackEdges),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100201 // Make bit vector growable, as the number of blocks may change.
202 blocks_(graph->GetArena(), graph->GetBlocks().Size(), true) {}
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100203
204 HBasicBlock* GetHeader() const {
205 return header_;
206 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000207
208 void AddBackEdge(HBasicBlock* back_edge) {
209 back_edges_.Add(back_edge);
210 }
211
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100212 void RemoveBackEdge(HBasicBlock* back_edge) {
213 back_edges_.Delete(back_edge);
214 }
215
216 bool IsBackEdge(HBasicBlock* block) {
217 for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) {
218 if (back_edges_.Get(i) == block) return true;
219 }
220 return false;
221 }
222
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000223 int NumberOfBackEdges() const {
224 return back_edges_.Size();
225 }
226
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100227 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100228
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100229 const GrowableArray<HBasicBlock*>& GetBackEdges() const {
230 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100231 }
232
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100233 void ClearBackEdges() {
234 back_edges_.Reset();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100235 }
236
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100237 // Find blocks that are part of this loop. Returns whether the loop is a natural loop,
238 // that is the header dominates the back edge.
239 bool Populate();
240
241 // Returns whether this loop information contains `block`.
242 // Note that this loop information *must* be populated before entering this function.
243 bool Contains(const HBasicBlock& block) const;
244
245 // Returns whether this loop information is an inner loop of `other`.
246 // Note that `other` *must* be populated before entering this function.
247 bool IsIn(const HLoopInformation& other) const;
248
249 const ArenaBitVector& GetBlocks() const { return blocks_; }
250
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000251 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100252 // Internal recursive implementation of `Populate`.
253 void PopulateRecursive(HBasicBlock* block);
254
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000255 HBasicBlock* header_;
256 GrowableArray<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100257 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000258
259 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
260};
261
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100262static constexpr size_t kNoLifetime = -1;
263
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000264// A block in a method. Contains the list of instructions represented
265// as a double linked list. Each block knows its predecessors and
266// successors.
267class HBasicBlock : public ArenaObject {
268 public:
269 explicit HBasicBlock(HGraph* graph)
270 : graph_(graph),
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000271 predecessors_(graph->GetArena(), kDefaultNumberOfPredecessors),
272 successors_(graph->GetArena(), kDefaultNumberOfSuccessors),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000273 loop_information_(nullptr),
274 dominator_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100275 block_id_(-1),
276 lifetime_start_(kNoLifetime),
277 lifetime_end_(kNoLifetime) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000278
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100279 const GrowableArray<HBasicBlock*>& GetPredecessors() const {
280 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000281 }
282
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100283 const GrowableArray<HBasicBlock*>& GetSuccessors() const {
284 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000285 }
286
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000287 void AddBackEdge(HBasicBlock* back_edge) {
288 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000289 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000290 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100291 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000292 loop_information_->AddBackEdge(back_edge);
293 }
294
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000295 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000296
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000297 int GetBlockId() const { return block_id_; }
298 void SetBlockId(int id) { block_id_ = id; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000299
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000300 HBasicBlock* GetDominator() const { return dominator_; }
301 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000302
303 int NumberOfBackEdges() const {
304 return loop_information_ == nullptr
305 ? 0
306 : loop_information_->NumberOfBackEdges();
307 }
308
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100309 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
310 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100311 const HInstructionList& GetInstructions() const { return instructions_; }
312 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100313 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000314
315 void AddSuccessor(HBasicBlock* block) {
316 successors_.Add(block);
317 block->predecessors_.Add(this);
318 }
319
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100320 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
321 size_t successor_index = GetSuccessorIndexOf(existing);
322 DCHECK_NE(successor_index, static_cast<size_t>(-1));
323 existing->RemovePredecessor(this);
324 new_block->predecessors_.Add(this);
325 successors_.Put(successor_index, new_block);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000326 }
327
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100328 void RemovePredecessor(HBasicBlock* block) {
329 predecessors_.Delete(block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100330 }
331
332 void ClearAllPredecessors() {
333 predecessors_.Reset();
334 }
335
336 void AddPredecessor(HBasicBlock* block) {
337 predecessors_.Add(block);
338 block->successors_.Add(this);
339 }
340
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100341 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) {
342 for (size_t i = 0, e = predecessors_.Size(); i < e; ++i) {
343 if (predecessors_.Get(i) == predecessor) {
344 return i;
345 }
346 }
347 return -1;
348 }
349
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100350 size_t GetSuccessorIndexOf(HBasicBlock* successor) {
351 for (size_t i = 0, e = successors_.Size(); i < e; ++i) {
352 if (successors_.Get(i) == successor) {
353 return i;
354 }
355 }
356 return -1;
357 }
358
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000359 void AddInstruction(HInstruction* instruction);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100360 void RemoveInstruction(HInstruction* instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100361 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +0100362 // Replace instruction `initial` with `replacement` within this block.
363 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
364 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100365 void AddPhi(HPhi* phi);
366 void RemovePhi(HPhi* phi);
367
368 bool IsLoopHeader() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100369 return (loop_information_ != nullptr) && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100370 }
371
372 HLoopInformation* GetLoopInformation() const {
373 return loop_information_;
374 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000375
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100376 // Set the loop_information_ on this block. This method overrides the current
377 // loop_information if it is an outer loop of the passed loop information.
378 void SetInLoop(HLoopInformation* info) {
379 if (IsLoopHeader()) {
380 // Nothing to do. This just means `info` is an outer loop.
381 } else if (loop_information_ == nullptr) {
382 loop_information_ = info;
383 } else if (loop_information_->Contains(*info->GetHeader())) {
384 // Block is currently part of an outer loop. Make it part of this inner loop.
385 // Note that a non loop header having a loop information means this loop information
386 // has already been populated
387 loop_information_ = info;
388 } else {
389 // Block is part of an inner loop. Do not update the loop information.
390 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
391 // at this point, because this method is being called while populating `info`.
392 }
393 }
394
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100395 bool IsInLoop() const { return loop_information_ != nullptr; }
396
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100397 // Returns wheter this block dominates the blocked passed as parameter.
398 bool Dominates(HBasicBlock* block) const;
399
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100400 size_t GetLifetimeStart() const { return lifetime_start_; }
401 size_t GetLifetimeEnd() const { return lifetime_end_; }
402
403 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
404 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
405
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000406 private:
407 HGraph* const graph_;
408 GrowableArray<HBasicBlock*> predecessors_;
409 GrowableArray<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100410 HInstructionList instructions_;
411 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000412 HLoopInformation* loop_information_;
413 HBasicBlock* dominator_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000414 int block_id_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100415 size_t lifetime_start_;
416 size_t lifetime_end_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000417
418 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
419};
420
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100421#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000422 M(Add) \
Dave Allison20dfc792014-06-16 20:44:29 -0700423 M(Condition) \
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000424 M(Equal) \
Dave Allison20dfc792014-06-16 20:44:29 -0700425 M(NotEqual) \
426 M(LessThan) \
427 M(LessThanOrEqual) \
428 M(GreaterThan) \
429 M(GreaterThanOrEqual) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000430 M(Exit) \
431 M(Goto) \
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000432 M(If) \
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000433 M(IntConstant) \
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000434 M(InvokeStatic) \
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100435 M(InvokeVirtual) \
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000436 M(LoadLocal) \
437 M(Local) \
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100438 M(LongConstant) \
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +0100439 M(NewInstance) \
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100440 M(Not) \
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100441 M(ParameterValue) \
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100442 M(ParallelMove) \
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100443 M(Phi) \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000444 M(Return) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000445 M(ReturnVoid) \
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000446 M(StoreLocal) \
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100447 M(Sub) \
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100448 M(Compare) \
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100449 M(InstanceFieldGet) \
450 M(InstanceFieldSet) \
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100451 M(ArrayGet) \
452 M(ArraySet) \
453 M(ArrayLength) \
454 M(BoundsCheck) \
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100455 M(NullCheck) \
456 M(Temporary) \
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000457 M(SuspendCheck) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000458
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100459#define FOR_EACH_INSTRUCTION(M) \
460 FOR_EACH_CONCRETE_INSTRUCTION(M) \
Roland Levillainccc07a92014-09-16 14:48:16 +0100461 M(Constant) \
462 M(BinaryOperation)
Dave Allison20dfc792014-06-16 20:44:29 -0700463
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000464#define FORWARD_DECLARATION(type) class H##type;
465FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
466#undef FORWARD_DECLARATION
467
Roland Levillainccc07a92014-09-16 14:48:16 +0100468#define DECLARE_INSTRUCTION(type) \
469 virtual const char* DebugName() const { return #type; } \
470 virtual const H##type* As##type() const OVERRIDE { return this; } \
471 virtual H##type* As##type() OVERRIDE { return this; } \
472 virtual bool InstructionTypeEquals(HInstruction* other) const { \
473 return other->Is##type(); \
474 } \
475 virtual void Accept(HGraphVisitor* visitor)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000476
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100477template <typename T>
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000478class HUseListNode : public ArenaObject {
479 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100480 HUseListNode(T* user, size_t index, HUseListNode* tail)
Dave Allison20dfc792014-06-16 20:44:29 -0700481 : user_(user), index_(index), tail_(tail) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000482
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000483 HUseListNode* GetTail() const { return tail_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100484 T* GetUser() const { return user_; }
485 size_t GetIndex() const { return index_; }
486
487 void SetTail(HUseListNode<T>* node) { tail_ = node; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000488
489 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100490 T* const user_;
491 const size_t index_;
492 HUseListNode<T>* tail_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000493
494 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
495};
496
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100497// Represents the side effects an instruction may have.
498class SideEffects : public ValueObject {
499 public:
500 static SideEffects None() {
501 return SideEffects(0);
502 }
503
504 static SideEffects All() {
505 return SideEffects(ChangesSomething().flags_ | DependsOnSomething().flags_);
506 }
507
508 static SideEffects ChangesSomething() {
509 return SideEffects((1 << kFlagChangesCount) - 1);
510 }
511
512 static SideEffects DependsOnSomething() {
513 int count = kFlagDependsOnCount - kFlagChangesCount;
514 return SideEffects(((1 << count) - 1) << kFlagChangesCount);
515 }
516
Roland Levillain72bceff2014-09-15 18:29:00 +0100517 bool HasSideEffects() const {
518 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
519 return (flags_ & all_bits_set) != 0;
520 }
521
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100522 private:
523 static constexpr int kFlagChangesSomething = 0;
524 static constexpr int kFlagChangesCount = kFlagChangesSomething + 1;
525
526 static constexpr int kFlagDependsOnSomething = kFlagChangesCount;
527 static constexpr int kFlagDependsOnCount = kFlagDependsOnSomething + 1;
528
529 private:
530 explicit SideEffects(size_t flags) : flags_(flags) {}
531
532 const size_t flags_;
533};
534
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000535class HInstruction : public ArenaObject {
536 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100537 explicit HInstruction(SideEffects side_effects)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000538 : previous_(nullptr),
539 next_(nullptr),
540 block_(nullptr),
541 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100542 ssa_index_(-1),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000543 uses_(nullptr),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100544 env_uses_(nullptr),
545 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100546 locations_(nullptr),
547 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100548 lifetime_position_(kNoLifetime),
549 side_effects_(side_effects) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000550
Dave Allison20dfc792014-06-16 20:44:29 -0700551 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000552
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000553 HInstruction* GetNext() const { return next_; }
554 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000555
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000556 HBasicBlock* GetBlock() const { return block_; }
557 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100558 bool IsInBlock() const { return block_ != nullptr; }
559 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +0100560 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000561
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100562 virtual size_t InputCount() const = 0;
563 virtual HInstruction* InputAt(size_t i) const = 0;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000564
565 virtual void Accept(HGraphVisitor* visitor) = 0;
566 virtual const char* DebugName() const = 0;
567
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100568 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100569 virtual void SetRawInputAt(size_t index, HInstruction* input) = 0;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100570
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100571 virtual bool NeedsEnvironment() const { return false; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100572 virtual bool IsControlFlow() const { return false; }
Roland Levillain72bceff2014-09-15 18:29:00 +0100573 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100574
575 void AddUseAt(HInstruction* user, size_t index) {
576 uses_ = new (block_->GetGraph()->GetArena()) HUseListNode<HInstruction>(user, index, uses_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000577 }
578
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100579 void AddEnvUseAt(HEnvironment* user, size_t index) {
580 env_uses_ = new (block_->GetGraph()->GetArena()) HUseListNode<HEnvironment>(
581 user, index, env_uses_);
582 }
583
584 void RemoveUser(HInstruction* user, size_t index);
585
586 HUseListNode<HInstruction>* GetUses() const { return uses_; }
587 HUseListNode<HEnvironment>* GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000588
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100589 bool HasUses() const { return uses_ != nullptr || env_uses_ != nullptr; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100590 bool HasEnvironmentUses() const { return env_uses_ != nullptr; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000591
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100592 size_t NumberOfUses() const {
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100593 // TODO: Optimize this method if it is used outside of the HGraphVisualizer.
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100594 size_t result = 0;
595 HUseListNode<HInstruction>* current = uses_;
596 while (current != nullptr) {
597 current = current->GetTail();
598 ++result;
599 }
600 return result;
601 }
602
Roland Levillainccc07a92014-09-16 14:48:16 +0100603 // Does this instruction dominate `other_instruction`? Aborts if
604 // this instruction and `other_instruction` are both phis.
605 bool Dominates(HInstruction* other_instruction) const;
606
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000607 int GetId() const { return id_; }
608 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000609
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100610 int GetSsaIndex() const { return ssa_index_; }
611 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
612 bool HasSsaIndex() const { return ssa_index_ != -1; }
613
614 bool HasEnvironment() const { return environment_ != nullptr; }
615 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100616 void SetEnvironment(HEnvironment* environment) { environment_ = environment; }
617
Nicolas Geoffray39468442014-09-02 15:17:15 +0100618 // Returns the number of entries in the environment. Typically, that is the
619 // number of dex registers in a method. It could be more in case of inlining.
620 size_t EnvironmentSize() const;
621
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000622 LocationSummary* GetLocations() const { return locations_; }
623 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000624
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100625 void ReplaceWith(HInstruction* instruction);
626
Dave Allison20dfc792014-06-16 20:44:29 -0700627 bool HasOnlyOneUse() const {
628 return uses_ != nullptr && uses_->GetTail() == nullptr;
629 }
630
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000631#define INSTRUCTION_TYPE_CHECK(type) \
Roland Levillainccc07a92014-09-16 14:48:16 +0100632 bool Is##type() const { return (As##type() != nullptr); } \
633 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000634 virtual H##type* As##type() { return nullptr; }
635
636 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
637#undef INSTRUCTION_TYPE_CHECK
638
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100639 // Returns whether the instruction can be moved within the graph.
640 virtual bool CanBeMoved() const { return false; }
641
642 // Returns whether the two instructions are of the same kind.
643 virtual bool InstructionTypeEquals(HInstruction* other) const { return false; }
644
645 // Returns whether any data encoded in the two instructions is equal.
646 // This method does not look at the inputs. Both instructions must be
647 // of the same type, otherwise the method has undefined behavior.
648 virtual bool InstructionDataEquals(HInstruction* other) const { return false; }
649
650 // Returns whether two instructions are equal, that is:
651 // 1) They have the same type and contain the same data,
652 // 2) Their inputs are identical.
653 bool Equals(HInstruction* other) const;
654
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100655 size_t GetLifetimePosition() const { return lifetime_position_; }
656 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
657 LiveInterval* GetLiveInterval() const { return live_interval_; }
658 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
659 bool HasLiveInterval() const { return live_interval_ != nullptr; }
660
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000661 private:
662 HInstruction* previous_;
663 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000664 HBasicBlock* block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000665
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000666 // An instruction gets an id when it is added to the graph.
667 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +0100668 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000669 int id_;
670
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100671 // When doing liveness analysis, instructions that have uses get an SSA index.
672 int ssa_index_;
673
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100674 // List of instructions that have this instruction as input.
675 HUseListNode<HInstruction>* uses_;
676
677 // List of environments that contain this instruction.
678 HUseListNode<HEnvironment>* env_uses_;
679
Nicolas Geoffray39468442014-09-02 15:17:15 +0100680 // The environment associated with this instruction. Not null if the instruction
681 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100682 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000683
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000684 // Set by the code generator.
685 LocationSummary* locations_;
686
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100687 // Set by the liveness analysis.
688 LiveInterval* live_interval_;
689
690 // Set by the liveness analysis, this is the position in a linear
691 // order of blocks where this instruction's live interval start.
692 size_t lifetime_position_;
693
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100694 const SideEffects side_effects_;
695
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000696 friend class HBasicBlock;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100697 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000698
699 DISALLOW_COPY_AND_ASSIGN(HInstruction);
700};
701
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100702template<typename T>
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000703class HUseIterator : public ValueObject {
704 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100705 explicit HUseIterator(HUseListNode<T>* uses) : current_(uses) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000706
707 bool Done() const { return current_ == nullptr; }
708
709 void Advance() {
710 DCHECK(!Done());
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000711 current_ = current_->GetTail();
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000712 }
713
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100714 HUseListNode<T>* Current() const {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000715 DCHECK(!Done());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100716 return current_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000717 }
718
719 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100720 HUseListNode<T>* current_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000721
722 friend class HValue;
723};
724
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100725// A HEnvironment object contains the values of virtual registers at a given location.
726class HEnvironment : public ArenaObject {
727 public:
728 HEnvironment(ArenaAllocator* arena, size_t number_of_vregs) : vregs_(arena, number_of_vregs) {
729 vregs_.SetSize(number_of_vregs);
730 for (size_t i = 0; i < number_of_vregs; i++) {
731 vregs_.Put(i, nullptr);
732 }
733 }
734
735 void Populate(const GrowableArray<HInstruction*>& env) {
736 for (size_t i = 0; i < env.Size(); i++) {
737 HInstruction* instruction = env.Get(i);
738 vregs_.Put(i, instruction);
739 if (instruction != nullptr) {
740 instruction->AddEnvUseAt(this, i);
741 }
742 }
743 }
744
745 void SetRawEnvAt(size_t index, HInstruction* instruction) {
746 vregs_.Put(index, instruction);
747 }
748
Nicolas Geoffray39468442014-09-02 15:17:15 +0100749 HInstruction* GetInstructionAt(size_t index) const {
750 return vregs_.Get(index);
751 }
752
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100753 GrowableArray<HInstruction*>* GetVRegs() {
754 return &vregs_;
755 }
756
Nicolas Geoffray39468442014-09-02 15:17:15 +0100757 size_t Size() const { return vregs_.Size(); }
758
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100759 private:
760 GrowableArray<HInstruction*> vregs_;
761
762 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
763};
764
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000765class HInputIterator : public ValueObject {
766 public:
Dave Allison20dfc792014-06-16 20:44:29 -0700767 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000768
769 bool Done() const { return index_ == instruction_->InputCount(); }
770 HInstruction* Current() const { return instruction_->InputAt(index_); }
771 void Advance() { index_++; }
772
773 private:
774 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100775 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000776
777 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
778};
779
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000780class HInstructionIterator : public ValueObject {
781 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100782 explicit HInstructionIterator(const HInstructionList& instructions)
783 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000784 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000785 }
786
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000787 bool Done() const { return instruction_ == nullptr; }
788 HInstruction* Current() const { return instruction_; }
789 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000790 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000791 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000792 }
793
794 private:
795 HInstruction* instruction_;
796 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100797
798 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000799};
800
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100801class HBackwardInstructionIterator : public ValueObject {
802 public:
803 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
804 : instruction_(instructions.last_instruction_) {
805 next_ = Done() ? nullptr : instruction_->GetPrevious();
806 }
807
808 bool Done() const { return instruction_ == nullptr; }
809 HInstruction* Current() const { return instruction_; }
810 void Advance() {
811 instruction_ = next_;
812 next_ = Done() ? nullptr : instruction_->GetPrevious();
813 }
814
815 private:
816 HInstruction* instruction_;
817 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100818
819 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100820};
821
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000822// An embedded container with N elements of type T. Used (with partial
823// specialization for N=0) because embedded arrays cannot have size 0.
824template<typename T, intptr_t N>
825class EmbeddedArray {
826 public:
Dave Allison20dfc792014-06-16 20:44:29 -0700827 EmbeddedArray() : elements_() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000828
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000829 intptr_t GetLength() const { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000830
831 const T& operator[](intptr_t i) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000832 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000833 return elements_[i];
834 }
835
836 T& operator[](intptr_t i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000837 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000838 return elements_[i];
839 }
840
841 const T& At(intptr_t i) const {
842 return (*this)[i];
843 }
844
845 void SetAt(intptr_t i, const T& val) {
846 (*this)[i] = val;
847 }
848
849 private:
850 T elements_[N];
851};
852
853template<typename T>
854class EmbeddedArray<T, 0> {
855 public:
856 intptr_t length() const { return 0; }
857 const T& operator[](intptr_t i) const {
858 LOG(FATAL) << "Unreachable";
859 static T sentinel = 0;
860 return sentinel;
861 }
862 T& operator[](intptr_t i) {
863 LOG(FATAL) << "Unreachable";
864 static T sentinel = 0;
865 return sentinel;
866 }
867};
868
869template<intptr_t N>
870class HTemplateInstruction: public HInstruction {
871 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100872 HTemplateInstruction<N>(SideEffects side_effects)
873 : HInstruction(side_effects), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -0700874 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000875
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100876 virtual size_t InputCount() const { return N; }
877 virtual HInstruction* InputAt(size_t i) const { return inputs_[i]; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000878
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000879 protected:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100880 virtual void SetRawInputAt(size_t i, HInstruction* instruction) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000881 inputs_[i] = instruction;
882 }
883
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000884 private:
885 EmbeddedArray<HInstruction*, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100886
887 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000888};
889
Dave Allison20dfc792014-06-16 20:44:29 -0700890template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100891class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -0700892 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100893 HExpression<N>(Primitive::Type type, SideEffects side_effects)
894 : HTemplateInstruction<N>(side_effects), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -0700895 virtual ~HExpression() {}
896
897 virtual Primitive::Type GetType() const { return type_; }
898
899 private:
900 const Primitive::Type type_;
901};
902
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000903// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
904// instruction that branches to the exit block.
905class HReturnVoid : public HTemplateInstruction<0> {
906 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100907 HReturnVoid() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100908
909 virtual bool IsControlFlow() const { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000910
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100911 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000912
913 private:
914 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
915};
916
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000917// Represents dex's RETURN opcodes. A HReturn is a control flow
918// instruction that branches to the exit block.
919class HReturn : public HTemplateInstruction<1> {
920 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100921 explicit HReturn(HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000922 SetRawInputAt(0, value);
923 }
924
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100925 virtual bool IsControlFlow() const { return true; }
926
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100927 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000928
929 private:
930 DISALLOW_COPY_AND_ASSIGN(HReturn);
931};
932
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000933// The exit instruction is the only instruction of the exit block.
934// Instructions aborting the method (HTrow and HReturn) must branch to the
935// exit block.
936class HExit : public HTemplateInstruction<0> {
937 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100938 HExit() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100939
940 virtual bool IsControlFlow() const { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000941
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100942 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000943
944 private:
945 DISALLOW_COPY_AND_ASSIGN(HExit);
946};
947
948// Jumps from one block to another.
949class HGoto : public HTemplateInstruction<0> {
950 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100951 HGoto() : HTemplateInstruction(SideEffects::None()) {}
952
953 virtual bool IsControlFlow() const { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000954
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000955 HBasicBlock* GetSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100956 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000957 }
958
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100959 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000960
961 private:
962 DISALLOW_COPY_AND_ASSIGN(HGoto);
963};
964
Dave Allison20dfc792014-06-16 20:44:29 -0700965
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000966// Conditional branch. A block ending with an HIf instruction must have
967// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000968class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000969 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100970 explicit HIf(HInstruction* input) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000971 SetRawInputAt(0, input);
972 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000973
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100974 virtual bool IsControlFlow() const { return true; }
975
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000976 HBasicBlock* IfTrueSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100977 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000978 }
979
980 HBasicBlock* IfFalseSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100981 return GetBlock()->GetSuccessors().Get(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000982 }
983
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100984 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000985
Dave Allison20dfc792014-06-16 20:44:29 -0700986 virtual bool IsIfInstruction() const { return true; }
987
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000988 private:
989 DISALLOW_COPY_AND_ASSIGN(HIf);
990};
991
Dave Allison20dfc792014-06-16 20:44:29 -0700992class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000993 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000994 HBinaryOperation(Primitive::Type result_type,
995 HInstruction* left,
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100996 HInstruction* right) : HExpression(result_type, SideEffects::None()) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000997 SetRawInputAt(0, left);
998 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000999 }
1000
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001001 HInstruction* GetLeft() const { return InputAt(0); }
1002 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07001003 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001004
1005 virtual bool IsCommutative() { return false; }
1006
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001007 virtual bool CanBeMoved() const { return true; }
1008 virtual bool InstructionDataEquals(HInstruction* other) const { return true; }
1009
Roland Levillainccc07a92014-09-16 14:48:16 +01001010 DECLARE_INSTRUCTION(BinaryOperation);
1011
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001012 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001013 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
1014};
1015
Dave Allison20dfc792014-06-16 20:44:29 -07001016class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001017 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001018 HCondition(HInstruction* first, HInstruction* second)
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001019 : HBinaryOperation(Primitive::kPrimBoolean, first, second) {}
1020
1021 virtual bool IsCommutative() { return true; }
Dave Allison20dfc792014-06-16 20:44:29 -07001022 bool NeedsMaterialization() const;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001023
Dave Allison20dfc792014-06-16 20:44:29 -07001024 DECLARE_INSTRUCTION(Condition);
1025
1026 virtual IfCondition GetCondition() const = 0;
1027
1028 private:
1029 DISALLOW_COPY_AND_ASSIGN(HCondition);
1030};
1031
1032// Instruction to check if two inputs are equal to each other.
1033class HEqual : public HCondition {
1034 public:
1035 HEqual(HInstruction* first, HInstruction* second)
1036 : HCondition(first, second) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001037
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001038 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001039
Dave Allison20dfc792014-06-16 20:44:29 -07001040 virtual IfCondition GetCondition() const {
1041 return kCondEQ;
1042 }
1043
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001044 private:
1045 DISALLOW_COPY_AND_ASSIGN(HEqual);
1046};
1047
Dave Allison20dfc792014-06-16 20:44:29 -07001048class HNotEqual : public HCondition {
1049 public:
1050 HNotEqual(HInstruction* first, HInstruction* second)
1051 : HCondition(first, second) {}
1052
1053 DECLARE_INSTRUCTION(NotEqual);
1054
1055 virtual IfCondition GetCondition() const {
1056 return kCondNE;
1057 }
1058
1059 private:
1060 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
1061};
1062
1063class HLessThan : public HCondition {
1064 public:
1065 HLessThan(HInstruction* first, HInstruction* second)
1066 : HCondition(first, second) {}
1067
1068 DECLARE_INSTRUCTION(LessThan);
1069
1070 virtual IfCondition GetCondition() const {
1071 return kCondLT;
1072 }
1073
1074 private:
1075 DISALLOW_COPY_AND_ASSIGN(HLessThan);
1076};
1077
1078class HLessThanOrEqual : public HCondition {
1079 public:
1080 HLessThanOrEqual(HInstruction* first, HInstruction* second)
1081 : HCondition(first, second) {}
1082
1083 DECLARE_INSTRUCTION(LessThanOrEqual);
1084
1085 virtual IfCondition GetCondition() const {
1086 return kCondLE;
1087 }
1088
1089 private:
1090 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
1091};
1092
1093class HGreaterThan : public HCondition {
1094 public:
1095 HGreaterThan(HInstruction* first, HInstruction* second)
1096 : HCondition(first, second) {}
1097
1098 DECLARE_INSTRUCTION(GreaterThan);
1099
1100 virtual IfCondition GetCondition() const {
1101 return kCondGT;
1102 }
1103
1104 private:
1105 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
1106};
1107
1108class HGreaterThanOrEqual : public HCondition {
1109 public:
1110 HGreaterThanOrEqual(HInstruction* first, HInstruction* second)
1111 : HCondition(first, second) {}
1112
1113 DECLARE_INSTRUCTION(GreaterThanOrEqual);
1114
1115 virtual IfCondition GetCondition() const {
1116 return kCondGE;
1117 }
1118
1119 private:
1120 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
1121};
1122
1123
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001124// Instruction to check how two inputs compare to each other.
1125// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
1126class HCompare : public HBinaryOperation {
1127 public:
1128 HCompare(Primitive::Type type, HInstruction* first, HInstruction* second)
1129 : HBinaryOperation(Primitive::kPrimInt, first, second) {
1130 DCHECK_EQ(type, first->GetType());
1131 DCHECK_EQ(type, second->GetType());
1132 }
1133
1134 DECLARE_INSTRUCTION(Compare);
1135
1136 private:
1137 DISALLOW_COPY_AND_ASSIGN(HCompare);
1138};
1139
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001140// A local in the graph. Corresponds to a Dex register.
1141class HLocal : public HTemplateInstruction<0> {
1142 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001143 explicit HLocal(uint16_t reg_number)
1144 : HTemplateInstruction(SideEffects::None()), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001145
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001146 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001147
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001148 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001149
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001150 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001151 // The Dex register number.
1152 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001153
1154 DISALLOW_COPY_AND_ASSIGN(HLocal);
1155};
1156
1157// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07001158class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001159 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001160 explicit HLoadLocal(HLocal* local, Primitive::Type type)
1161 : HExpression(type, SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001162 SetRawInputAt(0, local);
1163 }
1164
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001165 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1166
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001167 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001168
1169 private:
1170 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
1171};
1172
1173// Store a value in a given local. This instruction has two inputs: the value
1174// and the local.
1175class HStoreLocal : public HTemplateInstruction<2> {
1176 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001177 HStoreLocal(HLocal* local, HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001178 SetRawInputAt(0, local);
1179 SetRawInputAt(1, value);
1180 }
1181
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001182 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1183
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001184 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001185
1186 private:
1187 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
1188};
1189
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001190class HConstant : public HExpression<0> {
1191 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001192 explicit HConstant(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
1193
1194 virtual bool CanBeMoved() const { return true; }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001195
1196 DECLARE_INSTRUCTION(Constant);
1197
1198 private:
1199 DISALLOW_COPY_AND_ASSIGN(HConstant);
1200};
1201
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001202// Constants of the type int. Those can be from Dex instructions, or
1203// synthesized (for example with the if-eqz instruction).
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001204class HIntConstant : public HConstant {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001205 public:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001206 explicit HIntConstant(int32_t value) : HConstant(Primitive::kPrimInt), value_(value) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001207
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001208 int32_t GetValue() const { return value_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001209
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001210 virtual bool InstructionDataEquals(HInstruction* other) const {
1211 return other->AsIntConstant()->value_ == value_;
1212 }
1213
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001214 DECLARE_INSTRUCTION(IntConstant);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001215
1216 private:
1217 const int32_t value_;
1218
1219 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
1220};
1221
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001222class HLongConstant : public HConstant {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001223 public:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001224 explicit HLongConstant(int64_t value) : HConstant(Primitive::kPrimLong), value_(value) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001225
1226 int64_t GetValue() const { return value_; }
1227
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001228 virtual bool InstructionDataEquals(HInstruction* other) const {
1229 return other->AsLongConstant()->value_ == value_;
1230 }
1231
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001232 DECLARE_INSTRUCTION(LongConstant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001233
1234 private:
1235 const int64_t value_;
1236
1237 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
1238};
1239
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001240class HInvoke : public HInstruction {
1241 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001242 HInvoke(ArenaAllocator* arena,
1243 uint32_t number_of_arguments,
1244 Primitive::Type return_type,
1245 uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001246 : HInstruction(SideEffects::All()),
1247 inputs_(arena, number_of_arguments),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001248 return_type_(return_type),
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001249 dex_pc_(dex_pc) {
1250 inputs_.SetSize(number_of_arguments);
1251 }
1252
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001253 virtual size_t InputCount() const { return inputs_.Size(); }
1254 virtual HInstruction* InputAt(size_t i) const { return inputs_.Get(i); }
1255
1256 // Runtime needs to walk the stack, so Dex -> Dex calls need to
1257 // know their environment.
1258 virtual bool NeedsEnvironment() const { return true; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001259
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001260 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001261 SetRawInputAt(index, argument);
1262 }
1263
1264 virtual void SetRawInputAt(size_t index, HInstruction* input) {
1265 inputs_.Put(index, input);
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001266 }
1267
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001268 virtual Primitive::Type GetType() const { return return_type_; }
1269
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001270 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001271
1272 protected:
1273 GrowableArray<HInstruction*> inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001274 const Primitive::Type return_type_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001275 const uint32_t dex_pc_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001276
1277 private:
1278 DISALLOW_COPY_AND_ASSIGN(HInvoke);
1279};
1280
1281class HInvokeStatic : public HInvoke {
1282 public:
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001283 HInvokeStatic(ArenaAllocator* arena,
1284 uint32_t number_of_arguments,
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001285 Primitive::Type return_type,
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001286 uint32_t dex_pc,
1287 uint32_t index_in_dex_cache)
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001288 : HInvoke(arena, number_of_arguments, return_type, dex_pc),
1289 index_in_dex_cache_(index_in_dex_cache) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001290
1291 uint32_t GetIndexInDexCache() const { return index_in_dex_cache_; }
1292
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001293 DECLARE_INSTRUCTION(InvokeStatic);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001294
1295 private:
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001296 const uint32_t index_in_dex_cache_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001297
1298 DISALLOW_COPY_AND_ASSIGN(HInvokeStatic);
1299};
1300
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001301class HInvokeVirtual : public HInvoke {
1302 public:
1303 HInvokeVirtual(ArenaAllocator* arena,
1304 uint32_t number_of_arguments,
1305 Primitive::Type return_type,
1306 uint32_t dex_pc,
1307 uint32_t vtable_index)
1308 : HInvoke(arena, number_of_arguments, return_type, dex_pc),
1309 vtable_index_(vtable_index) {}
1310
1311 uint32_t GetVTableIndex() const { return vtable_index_; }
1312
1313 DECLARE_INSTRUCTION(InvokeVirtual);
1314
1315 private:
1316 const uint32_t vtable_index_;
1317
1318 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
1319};
1320
Dave Allison20dfc792014-06-16 20:44:29 -07001321class HNewInstance : public HExpression<0> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001322 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001323 HNewInstance(uint32_t dex_pc, uint16_t type_index)
1324 : HExpression(Primitive::kPrimNot, SideEffects::None()),
1325 dex_pc_(dex_pc),
1326 type_index_(type_index) {}
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001327
1328 uint32_t GetDexPc() const { return dex_pc_; }
1329 uint16_t GetTypeIndex() const { return type_index_; }
1330
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001331 // Calls runtime so needs an environment.
1332 virtual bool NeedsEnvironment() const { return true; }
1333
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001334 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001335
1336 private:
1337 const uint32_t dex_pc_;
1338 const uint16_t type_index_;
1339
1340 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
1341};
1342
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001343class HAdd : public HBinaryOperation {
1344 public:
1345 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1346 : HBinaryOperation(result_type, left, right) {}
1347
1348 virtual bool IsCommutative() { return true; }
1349
1350 DECLARE_INSTRUCTION(Add);
1351
1352 private:
1353 DISALLOW_COPY_AND_ASSIGN(HAdd);
1354};
1355
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001356class HSub : public HBinaryOperation {
1357 public:
1358 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1359 : HBinaryOperation(result_type, left, right) {}
1360
1361 virtual bool IsCommutative() { return false; }
1362
1363 DECLARE_INSTRUCTION(Sub);
1364
1365 private:
1366 DISALLOW_COPY_AND_ASSIGN(HSub);
1367};
1368
1369// The value of a parameter in this method. Its location depends on
1370// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07001371class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001372 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001373 HParameterValue(uint8_t index, Primitive::Type parameter_type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001374 : HExpression(parameter_type, SideEffects::None()), index_(index) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001375
1376 uint8_t GetIndex() const { return index_; }
1377
1378 DECLARE_INSTRUCTION(ParameterValue);
1379
1380 private:
1381 // The index of this parameter in the parameters list. Must be less
1382 // than HGraph::number_of_in_vregs_;
1383 const uint8_t index_;
1384
1385 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
1386};
1387
Dave Allison20dfc792014-06-16 20:44:29 -07001388class HNot : public HExpression<1> {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001389 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001390 explicit HNot(HInstruction* input) : HExpression(Primitive::kPrimBoolean, SideEffects::None()) {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001391 SetRawInputAt(0, input);
1392 }
1393
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001394 virtual bool CanBeMoved() const { return true; }
1395 virtual bool InstructionDataEquals(HInstruction* other) const { return true; }
1396
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001397 DECLARE_INSTRUCTION(Not);
1398
1399 private:
1400 DISALLOW_COPY_AND_ASSIGN(HNot);
1401};
1402
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001403class HPhi : public HInstruction {
1404 public:
1405 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001406 : HInstruction(SideEffects::None()),
1407 inputs_(arena, number_of_inputs),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001408 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001409 type_(type),
1410 is_live_(false) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001411 inputs_.SetSize(number_of_inputs);
1412 }
1413
1414 virtual size_t InputCount() const { return inputs_.Size(); }
1415 virtual HInstruction* InputAt(size_t i) const { return inputs_.Get(i); }
1416
1417 virtual void SetRawInputAt(size_t index, HInstruction* input) {
1418 inputs_.Put(index, input);
1419 }
1420
1421 void AddInput(HInstruction* input);
1422
1423 virtual Primitive::Type GetType() const { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001424 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001425
1426 uint32_t GetRegNumber() const { return reg_number_; }
1427
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001428 void SetDead() { is_live_ = false; }
1429 void SetLive() { is_live_ = true; }
1430 bool IsDead() const { return !is_live_; }
1431 bool IsLive() const { return is_live_; }
1432
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001433 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001434
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001435 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001436 GrowableArray<HInstruction*> inputs_;
1437 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001438 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001439 bool is_live_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001440
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001441 DISALLOW_COPY_AND_ASSIGN(HPhi);
1442};
1443
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001444class HNullCheck : public HExpression<1> {
1445 public:
1446 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001447 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001448 SetRawInputAt(0, value);
1449 }
1450
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001451 virtual bool CanBeMoved() const { return true; }
1452 virtual bool InstructionDataEquals(HInstruction* other) const { return true; }
1453
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001454 virtual bool NeedsEnvironment() const { return true; }
1455
1456 uint32_t GetDexPc() const { return dex_pc_; }
1457
1458 DECLARE_INSTRUCTION(NullCheck);
1459
1460 private:
1461 const uint32_t dex_pc_;
1462
1463 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
1464};
1465
1466class FieldInfo : public ValueObject {
1467 public:
Nicolas Geoffray39468442014-09-02 15:17:15 +01001468 explicit FieldInfo(MemberOffset field_offset, Primitive::Type field_type)
1469 : field_offset_(field_offset), field_type_(field_type) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001470
1471 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001472 Primitive::Type GetFieldType() const { return field_type_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001473
1474 private:
1475 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01001476 const Primitive::Type field_type_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001477};
1478
1479class HInstanceFieldGet : public HExpression<1> {
1480 public:
1481 HInstanceFieldGet(HInstruction* value,
1482 Primitive::Type field_type,
1483 MemberOffset field_offset)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001484 : HExpression(field_type, SideEffects::DependsOnSomething()),
1485 field_info_(field_offset, field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001486 SetRawInputAt(0, value);
1487 }
1488
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001489 virtual bool CanBeMoved() const { return true; }
1490 virtual bool InstructionDataEquals(HInstruction* other) const {
1491 size_t other_offset = other->AsInstanceFieldGet()->GetFieldOffset().SizeValue();
1492 return other_offset == GetFieldOffset().SizeValue();
1493 }
1494
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001495 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001496 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001497
1498 DECLARE_INSTRUCTION(InstanceFieldGet);
1499
1500 private:
1501 const FieldInfo field_info_;
1502
1503 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
1504};
1505
1506class HInstanceFieldSet : public HTemplateInstruction<2> {
1507 public:
1508 HInstanceFieldSet(HInstruction* object,
1509 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01001510 Primitive::Type field_type,
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001511 MemberOffset field_offset)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001512 : HTemplateInstruction(SideEffects::ChangesSomething()),
1513 field_info_(field_offset, field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001514 SetRawInputAt(0, object);
1515 SetRawInputAt(1, value);
1516 }
1517
1518 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001519 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001520
1521 DECLARE_INSTRUCTION(InstanceFieldSet);
1522
1523 private:
1524 const FieldInfo field_info_;
1525
1526 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
1527};
1528
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001529class HArrayGet : public HExpression<2> {
1530 public:
1531 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001532 : HExpression(type, SideEffects::DependsOnSomething()) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001533 SetRawInputAt(0, array);
1534 SetRawInputAt(1, index);
1535 }
1536
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001537 virtual bool CanBeMoved() const { return true; }
1538 virtual bool InstructionDataEquals(HInstruction* other) const { return true; }
1539
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001540 DECLARE_INSTRUCTION(ArrayGet);
1541
1542 private:
1543 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
1544};
1545
1546class HArraySet : public HTemplateInstruction<3> {
1547 public:
1548 HArraySet(HInstruction* array,
1549 HInstruction* index,
1550 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01001551 Primitive::Type component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001552 uint32_t dex_pc)
1553 : HTemplateInstruction(SideEffects::ChangesSomething()),
1554 dex_pc_(dex_pc),
1555 component_type_(component_type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001556 SetRawInputAt(0, array);
1557 SetRawInputAt(1, index);
1558 SetRawInputAt(2, value);
1559 }
1560
1561 virtual bool NeedsEnvironment() const {
1562 // We currently always call a runtime method to catch array store
1563 // exceptions.
1564 return InputAt(2)->GetType() == Primitive::kPrimNot;
1565 }
1566
1567 uint32_t GetDexPc() const { return dex_pc_; }
1568
Nicolas Geoffray39468442014-09-02 15:17:15 +01001569 Primitive::Type GetComponentType() const { return component_type_; }
1570
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001571 DECLARE_INSTRUCTION(ArraySet);
1572
1573 private:
1574 const uint32_t dex_pc_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01001575 const Primitive::Type component_type_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001576
1577 DISALLOW_COPY_AND_ASSIGN(HArraySet);
1578};
1579
1580class HArrayLength : public HExpression<1> {
1581 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001582 explicit HArrayLength(HInstruction* array)
1583 : HExpression(Primitive::kPrimInt, SideEffects::None()) {
1584 // Note that arrays do not change length, so the instruction does not
1585 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001586 SetRawInputAt(0, array);
1587 }
1588
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001589 virtual bool CanBeMoved() const { return true; }
1590 virtual bool InstructionDataEquals(HInstruction* other) const { return true; }
1591
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001592 DECLARE_INSTRUCTION(ArrayLength);
1593
1594 private:
1595 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
1596};
1597
1598class HBoundsCheck : public HExpression<2> {
1599 public:
1600 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001601 : HExpression(index->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001602 DCHECK(index->GetType() == Primitive::kPrimInt);
1603 SetRawInputAt(0, index);
1604 SetRawInputAt(1, length);
1605 }
1606
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001607 virtual bool CanBeMoved() const { return true; }
1608 virtual bool InstructionDataEquals(HInstruction* other) const { return true; }
1609
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001610 virtual bool NeedsEnvironment() const { return true; }
1611
1612 uint32_t GetDexPc() const { return dex_pc_; }
1613
1614 DECLARE_INSTRUCTION(BoundsCheck);
1615
1616 private:
1617 const uint32_t dex_pc_;
1618
1619 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
1620};
1621
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001622/**
1623 * Some DEX instructions are folded into multiple HInstructions that need
1624 * to stay live until the last HInstruction. This class
1625 * is used as a marker for the baseline compiler to ensure its preceding
1626 * HInstruction stays live. `index` is the temporary number that is used
1627 * for knowing the stack offset where to store the instruction.
1628 */
1629class HTemporary : public HTemplateInstruction<0> {
1630 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001631 explicit HTemporary(size_t index) : HTemplateInstruction(SideEffects::None()), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001632
1633 size_t GetIndex() const { return index_; }
1634
1635 DECLARE_INSTRUCTION(Temporary);
1636
1637 private:
1638 const size_t index_;
1639
1640 DISALLOW_COPY_AND_ASSIGN(HTemporary);
1641};
1642
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001643class HSuspendCheck : public HTemplateInstruction<0> {
1644 public:
1645 explicit HSuspendCheck(uint32_t dex_pc)
1646 : HTemplateInstruction(SideEffects::ChangesSomething()), dex_pc_(dex_pc) {}
1647
1648 virtual bool NeedsEnvironment() const {
1649 return true;
1650 }
1651
1652 uint32_t GetDexPc() const { return dex_pc_; }
1653
1654 DECLARE_INSTRUCTION(SuspendCheck);
1655
1656 private:
1657 const uint32_t dex_pc_;
1658
1659 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
1660};
1661
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001662class MoveOperands : public ArenaObject {
1663 public:
1664 MoveOperands(Location source, Location destination)
1665 : source_(source), destination_(destination) {}
1666
1667 Location GetSource() const { return source_; }
1668 Location GetDestination() const { return destination_; }
1669
1670 void SetSource(Location value) { source_ = value; }
1671 void SetDestination(Location value) { destination_ = value; }
1672
1673 // The parallel move resolver marks moves as "in-progress" by clearing the
1674 // destination (but not the source).
1675 Location MarkPending() {
1676 DCHECK(!IsPending());
1677 Location dest = destination_;
1678 destination_ = Location::NoLocation();
1679 return dest;
1680 }
1681
1682 void ClearPending(Location dest) {
1683 DCHECK(IsPending());
1684 destination_ = dest;
1685 }
1686
1687 bool IsPending() const {
1688 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
1689 return destination_.IsInvalid() && !source_.IsInvalid();
1690 }
1691
1692 // True if this blocks a move from the given location.
1693 bool Blocks(Location loc) const {
1694 return !IsEliminated() && source_.Equals(loc);
1695 }
1696
1697 // A move is redundant if it's been eliminated, if its source and
1698 // destination are the same, or if its destination is unneeded.
1699 bool IsRedundant() const {
1700 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
1701 }
1702
1703 // We clear both operands to indicate move that's been eliminated.
1704 void Eliminate() {
1705 source_ = destination_ = Location::NoLocation();
1706 }
1707
1708 bool IsEliminated() const {
1709 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
1710 return source_.IsInvalid();
1711 }
1712
1713 private:
1714 Location source_;
1715 Location destination_;
1716
1717 DISALLOW_COPY_AND_ASSIGN(MoveOperands);
1718};
1719
1720static constexpr size_t kDefaultNumberOfMoves = 4;
1721
1722class HParallelMove : public HTemplateInstruction<0> {
1723 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001724 explicit HParallelMove(ArenaAllocator* arena)
1725 : HTemplateInstruction(SideEffects::None()), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001726
1727 void AddMove(MoveOperands* move) {
1728 moves_.Add(move);
1729 }
1730
1731 MoveOperands* MoveOperandsAt(size_t index) const {
1732 return moves_.Get(index);
1733 }
1734
1735 size_t NumMoves() const { return moves_.Size(); }
1736
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001737 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001738
1739 private:
1740 GrowableArray<MoveOperands*> moves_;
1741
1742 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
1743};
1744
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001745class HGraphVisitor : public ValueObject {
1746 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001747 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
1748 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001749
Dave Allison20dfc792014-06-16 20:44:29 -07001750 virtual void VisitInstruction(HInstruction* instruction) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001751 virtual void VisitBasicBlock(HBasicBlock* block);
1752
1753 void VisitInsertionOrder();
1754
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001755 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001756
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001757 // Visit functions for instruction classes.
1758#define DECLARE_VISIT_INSTRUCTION(name) \
1759 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
1760
1761 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
1762
1763#undef DECLARE_VISIT_INSTRUCTION
1764
1765 private:
1766 HGraph* graph_;
1767
1768 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
1769};
1770
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001771class HInsertionOrderIterator : public ValueObject {
1772 public:
1773 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
1774
1775 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
1776 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
1777 void Advance() { ++index_; }
1778
1779 private:
1780 const HGraph& graph_;
1781 size_t index_;
1782
1783 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
1784};
1785
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001786class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001787 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001788 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001789
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001790 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
1791 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001792 void Advance() { ++index_; }
1793
1794 private:
1795 const HGraph& graph_;
1796 size_t index_;
1797
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001798 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001799};
1800
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001801class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001802 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001803 explicit HPostOrderIterator(const HGraph& graph)
1804 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001805
1806 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001807 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001808 void Advance() { --index_; }
1809
1810 private:
1811 const HGraph& graph_;
1812 size_t index_;
1813
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001814 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001815};
1816
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001817} // namespace art
1818
1819#endif // ART_COMPILER_OPTIMIZING_NODES_H_