blob: aae65ec9c0f3b35d1c2b1dfe5ffdb21aa7efb69e [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 Geoffray604c6e42014-09-17 12:08:44 +0100341 void SwapPredecessors() {
Nicolas Geoffrayc83d4412014-09-18 16:46:20 +0100342 DCHECK_EQ(predecessors_.Size(), 2u);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100343 HBasicBlock* temp = predecessors_.Get(0);
344 predecessors_.Put(0, predecessors_.Get(1));
345 predecessors_.Put(1, temp);
346 }
347
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100348 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) {
349 for (size_t i = 0, e = predecessors_.Size(); i < e; ++i) {
350 if (predecessors_.Get(i) == predecessor) {
351 return i;
352 }
353 }
354 return -1;
355 }
356
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100357 size_t GetSuccessorIndexOf(HBasicBlock* successor) {
358 for (size_t i = 0, e = successors_.Size(); i < e; ++i) {
359 if (successors_.Get(i) == successor) {
360 return i;
361 }
362 }
363 return -1;
364 }
365
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000366 void AddInstruction(HInstruction* instruction);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100367 void RemoveInstruction(HInstruction* instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100368 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +0100369 // Replace instruction `initial` with `replacement` within this block.
370 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
371 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100372 void AddPhi(HPhi* phi);
373 void RemovePhi(HPhi* phi);
374
375 bool IsLoopHeader() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100376 return (loop_information_ != nullptr) && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100377 }
378
379 HLoopInformation* GetLoopInformation() const {
380 return loop_information_;
381 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000382
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100383 // Set the loop_information_ on this block. This method overrides the current
384 // loop_information if it is an outer loop of the passed loop information.
385 void SetInLoop(HLoopInformation* info) {
386 if (IsLoopHeader()) {
387 // Nothing to do. This just means `info` is an outer loop.
388 } else if (loop_information_ == nullptr) {
389 loop_information_ = info;
390 } else if (loop_information_->Contains(*info->GetHeader())) {
391 // Block is currently part of an outer loop. Make it part of this inner loop.
392 // Note that a non loop header having a loop information means this loop information
393 // has already been populated
394 loop_information_ = info;
395 } else {
396 // Block is part of an inner loop. Do not update the loop information.
397 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
398 // at this point, because this method is being called while populating `info`.
399 }
400 }
401
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100402 bool IsInLoop() const { return loop_information_ != nullptr; }
403
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100404 // Returns wheter this block dominates the blocked passed as parameter.
405 bool Dominates(HBasicBlock* block) const;
406
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100407 size_t GetLifetimeStart() const { return lifetime_start_; }
408 size_t GetLifetimeEnd() const { return lifetime_end_; }
409
410 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
411 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
412
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000413 private:
414 HGraph* const graph_;
415 GrowableArray<HBasicBlock*> predecessors_;
416 GrowableArray<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100417 HInstructionList instructions_;
418 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000419 HLoopInformation* loop_information_;
420 HBasicBlock* dominator_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000421 int block_id_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100422 size_t lifetime_start_;
423 size_t lifetime_end_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000424
425 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
426};
427
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100428#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000429 M(Add) \
Dave Allison20dfc792014-06-16 20:44:29 -0700430 M(Condition) \
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000431 M(Equal) \
Dave Allison20dfc792014-06-16 20:44:29 -0700432 M(NotEqual) \
433 M(LessThan) \
434 M(LessThanOrEqual) \
435 M(GreaterThan) \
436 M(GreaterThanOrEqual) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000437 M(Exit) \
438 M(Goto) \
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000439 M(If) \
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000440 M(IntConstant) \
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000441 M(InvokeStatic) \
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100442 M(InvokeVirtual) \
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000443 M(LoadLocal) \
444 M(Local) \
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100445 M(LongConstant) \
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +0100446 M(NewInstance) \
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100447 M(Not) \
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100448 M(ParameterValue) \
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100449 M(ParallelMove) \
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100450 M(Phi) \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000451 M(Return) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000452 M(ReturnVoid) \
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000453 M(StoreLocal) \
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100454 M(Sub) \
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100455 M(Compare) \
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100456 M(InstanceFieldGet) \
457 M(InstanceFieldSet) \
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100458 M(ArrayGet) \
459 M(ArraySet) \
460 M(ArrayLength) \
461 M(BoundsCheck) \
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100462 M(NullCheck) \
463 M(Temporary) \
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000464 M(SuspendCheck) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000465
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100466#define FOR_EACH_INSTRUCTION(M) \
467 FOR_EACH_CONCRETE_INSTRUCTION(M) \
Roland Levillainccc07a92014-09-16 14:48:16 +0100468 M(Constant) \
469 M(BinaryOperation)
Dave Allison20dfc792014-06-16 20:44:29 -0700470
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000471#define FORWARD_DECLARATION(type) class H##type;
472FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
473#undef FORWARD_DECLARATION
474
Roland Levillainccc07a92014-09-16 14:48:16 +0100475#define DECLARE_INSTRUCTION(type) \
476 virtual const char* DebugName() const { return #type; } \
477 virtual const H##type* As##type() const OVERRIDE { return this; } \
478 virtual H##type* As##type() OVERRIDE { return this; } \
479 virtual bool InstructionTypeEquals(HInstruction* other) const { \
480 return other->Is##type(); \
481 } \
482 virtual void Accept(HGraphVisitor* visitor)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000483
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100484template <typename T>
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000485class HUseListNode : public ArenaObject {
486 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100487 HUseListNode(T* user, size_t index, HUseListNode* tail)
Dave Allison20dfc792014-06-16 20:44:29 -0700488 : user_(user), index_(index), tail_(tail) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000489
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000490 HUseListNode* GetTail() const { return tail_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100491 T* GetUser() const { return user_; }
492 size_t GetIndex() const { return index_; }
493
494 void SetTail(HUseListNode<T>* node) { tail_ = node; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000495
496 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100497 T* const user_;
498 const size_t index_;
499 HUseListNode<T>* tail_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000500
501 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
502};
503
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100504// Represents the side effects an instruction may have.
505class SideEffects : public ValueObject {
506 public:
507 static SideEffects None() {
508 return SideEffects(0);
509 }
510
511 static SideEffects All() {
512 return SideEffects(ChangesSomething().flags_ | DependsOnSomething().flags_);
513 }
514
515 static SideEffects ChangesSomething() {
516 return SideEffects((1 << kFlagChangesCount) - 1);
517 }
518
519 static SideEffects DependsOnSomething() {
520 int count = kFlagDependsOnCount - kFlagChangesCount;
521 return SideEffects(((1 << count) - 1) << kFlagChangesCount);
522 }
523
Roland Levillain72bceff2014-09-15 18:29:00 +0100524 bool HasSideEffects() const {
525 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
526 return (flags_ & all_bits_set) != 0;
527 }
528
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100529 private:
530 static constexpr int kFlagChangesSomething = 0;
531 static constexpr int kFlagChangesCount = kFlagChangesSomething + 1;
532
533 static constexpr int kFlagDependsOnSomething = kFlagChangesCount;
534 static constexpr int kFlagDependsOnCount = kFlagDependsOnSomething + 1;
535
536 private:
537 explicit SideEffects(size_t flags) : flags_(flags) {}
538
539 const size_t flags_;
540};
541
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000542class HInstruction : public ArenaObject {
543 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100544 explicit HInstruction(SideEffects side_effects)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000545 : previous_(nullptr),
546 next_(nullptr),
547 block_(nullptr),
548 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100549 ssa_index_(-1),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000550 uses_(nullptr),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100551 env_uses_(nullptr),
552 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100553 locations_(nullptr),
554 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100555 lifetime_position_(kNoLifetime),
556 side_effects_(side_effects) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000557
Dave Allison20dfc792014-06-16 20:44:29 -0700558 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000559
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000560 HInstruction* GetNext() const { return next_; }
561 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000562
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000563 HBasicBlock* GetBlock() const { return block_; }
564 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100565 bool IsInBlock() const { return block_ != nullptr; }
566 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +0100567 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000568
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100569 virtual size_t InputCount() const = 0;
570 virtual HInstruction* InputAt(size_t i) const = 0;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000571
572 virtual void Accept(HGraphVisitor* visitor) = 0;
573 virtual const char* DebugName() const = 0;
574
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100575 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100576 virtual void SetRawInputAt(size_t index, HInstruction* input) = 0;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100577
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100578 virtual bool NeedsEnvironment() const { return false; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100579 virtual bool IsControlFlow() const { return false; }
Roland Levillain72bceff2014-09-15 18:29:00 +0100580 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100581
582 void AddUseAt(HInstruction* user, size_t index) {
583 uses_ = new (block_->GetGraph()->GetArena()) HUseListNode<HInstruction>(user, index, uses_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000584 }
585
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100586 void AddEnvUseAt(HEnvironment* user, size_t index) {
587 env_uses_ = new (block_->GetGraph()->GetArena()) HUseListNode<HEnvironment>(
588 user, index, env_uses_);
589 }
590
591 void RemoveUser(HInstruction* user, size_t index);
592
593 HUseListNode<HInstruction>* GetUses() const { return uses_; }
594 HUseListNode<HEnvironment>* GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000595
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100596 bool HasUses() const { return uses_ != nullptr || env_uses_ != nullptr; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100597 bool HasEnvironmentUses() const { return env_uses_ != nullptr; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000598
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100599 size_t NumberOfUses() const {
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100600 // TODO: Optimize this method if it is used outside of the HGraphVisualizer.
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100601 size_t result = 0;
602 HUseListNode<HInstruction>* current = uses_;
603 while (current != nullptr) {
604 current = current->GetTail();
605 ++result;
606 }
607 return result;
608 }
609
Roland Levillainccc07a92014-09-16 14:48:16 +0100610 // Does this instruction dominate `other_instruction`? Aborts if
611 // this instruction and `other_instruction` are both phis.
612 bool Dominates(HInstruction* other_instruction) const;
613
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000614 int GetId() const { return id_; }
615 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000616
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100617 int GetSsaIndex() const { return ssa_index_; }
618 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
619 bool HasSsaIndex() const { return ssa_index_ != -1; }
620
621 bool HasEnvironment() const { return environment_ != nullptr; }
622 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100623 void SetEnvironment(HEnvironment* environment) { environment_ = environment; }
624
Nicolas Geoffray39468442014-09-02 15:17:15 +0100625 // Returns the number of entries in the environment. Typically, that is the
626 // number of dex registers in a method. It could be more in case of inlining.
627 size_t EnvironmentSize() const;
628
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000629 LocationSummary* GetLocations() const { return locations_; }
630 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000631
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100632 void ReplaceWith(HInstruction* instruction);
633
Dave Allison20dfc792014-06-16 20:44:29 -0700634 bool HasOnlyOneUse() const {
635 return uses_ != nullptr && uses_->GetTail() == nullptr;
636 }
637
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000638#define INSTRUCTION_TYPE_CHECK(type) \
Roland Levillainccc07a92014-09-16 14:48:16 +0100639 bool Is##type() const { return (As##type() != nullptr); } \
640 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000641 virtual H##type* As##type() { return nullptr; }
642
643 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
644#undef INSTRUCTION_TYPE_CHECK
645
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100646 // Returns whether the instruction can be moved within the graph.
647 virtual bool CanBeMoved() const { return false; }
648
649 // Returns whether the two instructions are of the same kind.
650 virtual bool InstructionTypeEquals(HInstruction* other) const { return false; }
651
652 // Returns whether any data encoded in the two instructions is equal.
653 // This method does not look at the inputs. Both instructions must be
654 // of the same type, otherwise the method has undefined behavior.
655 virtual bool InstructionDataEquals(HInstruction* other) const { return false; }
656
657 // Returns whether two instructions are equal, that is:
658 // 1) They have the same type and contain the same data,
659 // 2) Their inputs are identical.
660 bool Equals(HInstruction* other) const;
661
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100662 size_t GetLifetimePosition() const { return lifetime_position_; }
663 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
664 LiveInterval* GetLiveInterval() const { return live_interval_; }
665 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
666 bool HasLiveInterval() const { return live_interval_ != nullptr; }
667
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000668 private:
669 HInstruction* previous_;
670 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000671 HBasicBlock* block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000672
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000673 // An instruction gets an id when it is added to the graph.
674 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +0100675 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000676 int id_;
677
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100678 // When doing liveness analysis, instructions that have uses get an SSA index.
679 int ssa_index_;
680
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100681 // List of instructions that have this instruction as input.
682 HUseListNode<HInstruction>* uses_;
683
684 // List of environments that contain this instruction.
685 HUseListNode<HEnvironment>* env_uses_;
686
Nicolas Geoffray39468442014-09-02 15:17:15 +0100687 // The environment associated with this instruction. Not null if the instruction
688 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100689 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000690
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000691 // Set by the code generator.
692 LocationSummary* locations_;
693
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100694 // Set by the liveness analysis.
695 LiveInterval* live_interval_;
696
697 // Set by the liveness analysis, this is the position in a linear
698 // order of blocks where this instruction's live interval start.
699 size_t lifetime_position_;
700
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100701 const SideEffects side_effects_;
702
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000703 friend class HBasicBlock;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100704 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000705
706 DISALLOW_COPY_AND_ASSIGN(HInstruction);
707};
708
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100709template<typename T>
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000710class HUseIterator : public ValueObject {
711 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100712 explicit HUseIterator(HUseListNode<T>* uses) : current_(uses) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000713
714 bool Done() const { return current_ == nullptr; }
715
716 void Advance() {
717 DCHECK(!Done());
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000718 current_ = current_->GetTail();
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000719 }
720
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100721 HUseListNode<T>* Current() const {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000722 DCHECK(!Done());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100723 return current_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000724 }
725
726 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100727 HUseListNode<T>* current_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000728
729 friend class HValue;
730};
731
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100732// A HEnvironment object contains the values of virtual registers at a given location.
733class HEnvironment : public ArenaObject {
734 public:
735 HEnvironment(ArenaAllocator* arena, size_t number_of_vregs) : vregs_(arena, number_of_vregs) {
736 vregs_.SetSize(number_of_vregs);
737 for (size_t i = 0; i < number_of_vregs; i++) {
738 vregs_.Put(i, nullptr);
739 }
740 }
741
742 void Populate(const GrowableArray<HInstruction*>& env) {
743 for (size_t i = 0; i < env.Size(); i++) {
744 HInstruction* instruction = env.Get(i);
745 vregs_.Put(i, instruction);
746 if (instruction != nullptr) {
747 instruction->AddEnvUseAt(this, i);
748 }
749 }
750 }
751
752 void SetRawEnvAt(size_t index, HInstruction* instruction) {
753 vregs_.Put(index, instruction);
754 }
755
Nicolas Geoffray39468442014-09-02 15:17:15 +0100756 HInstruction* GetInstructionAt(size_t index) const {
757 return vregs_.Get(index);
758 }
759
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100760 GrowableArray<HInstruction*>* GetVRegs() {
761 return &vregs_;
762 }
763
Nicolas Geoffray39468442014-09-02 15:17:15 +0100764 size_t Size() const { return vregs_.Size(); }
765
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100766 private:
767 GrowableArray<HInstruction*> vregs_;
768
769 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
770};
771
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000772class HInputIterator : public ValueObject {
773 public:
Dave Allison20dfc792014-06-16 20:44:29 -0700774 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000775
776 bool Done() const { return index_ == instruction_->InputCount(); }
777 HInstruction* Current() const { return instruction_->InputAt(index_); }
778 void Advance() { index_++; }
779
780 private:
781 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100782 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000783
784 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
785};
786
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000787class HInstructionIterator : public ValueObject {
788 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100789 explicit HInstructionIterator(const HInstructionList& instructions)
790 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000791 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000792 }
793
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000794 bool Done() const { return instruction_ == nullptr; }
795 HInstruction* Current() const { return instruction_; }
796 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000797 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000798 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000799 }
800
801 private:
802 HInstruction* instruction_;
803 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100804
805 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000806};
807
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100808class HBackwardInstructionIterator : public ValueObject {
809 public:
810 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
811 : instruction_(instructions.last_instruction_) {
812 next_ = Done() ? nullptr : instruction_->GetPrevious();
813 }
814
815 bool Done() const { return instruction_ == nullptr; }
816 HInstruction* Current() const { return instruction_; }
817 void Advance() {
818 instruction_ = next_;
819 next_ = Done() ? nullptr : instruction_->GetPrevious();
820 }
821
822 private:
823 HInstruction* instruction_;
824 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100825
826 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100827};
828
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000829// An embedded container with N elements of type T. Used (with partial
830// specialization for N=0) because embedded arrays cannot have size 0.
831template<typename T, intptr_t N>
832class EmbeddedArray {
833 public:
Dave Allison20dfc792014-06-16 20:44:29 -0700834 EmbeddedArray() : elements_() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000835
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000836 intptr_t GetLength() const { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000837
838 const T& operator[](intptr_t i) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000839 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000840 return elements_[i];
841 }
842
843 T& operator[](intptr_t i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000844 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000845 return elements_[i];
846 }
847
848 const T& At(intptr_t i) const {
849 return (*this)[i];
850 }
851
852 void SetAt(intptr_t i, const T& val) {
853 (*this)[i] = val;
854 }
855
856 private:
857 T elements_[N];
858};
859
860template<typename T>
861class EmbeddedArray<T, 0> {
862 public:
863 intptr_t length() const { return 0; }
864 const T& operator[](intptr_t i) const {
865 LOG(FATAL) << "Unreachable";
866 static T sentinel = 0;
867 return sentinel;
868 }
869 T& operator[](intptr_t i) {
870 LOG(FATAL) << "Unreachable";
871 static T sentinel = 0;
872 return sentinel;
873 }
874};
875
876template<intptr_t N>
877class HTemplateInstruction: public HInstruction {
878 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100879 HTemplateInstruction<N>(SideEffects side_effects)
880 : HInstruction(side_effects), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -0700881 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000882
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100883 virtual size_t InputCount() const { return N; }
884 virtual HInstruction* InputAt(size_t i) const { return inputs_[i]; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000885
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000886 protected:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100887 virtual void SetRawInputAt(size_t i, HInstruction* instruction) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000888 inputs_[i] = instruction;
889 }
890
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000891 private:
892 EmbeddedArray<HInstruction*, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100893
894 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000895};
896
Dave Allison20dfc792014-06-16 20:44:29 -0700897template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100898class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -0700899 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100900 HExpression<N>(Primitive::Type type, SideEffects side_effects)
901 : HTemplateInstruction<N>(side_effects), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -0700902 virtual ~HExpression() {}
903
904 virtual Primitive::Type GetType() const { return type_; }
905
906 private:
907 const Primitive::Type type_;
908};
909
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000910// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
911// instruction that branches to the exit block.
912class HReturnVoid : public HTemplateInstruction<0> {
913 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100914 HReturnVoid() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100915
916 virtual bool IsControlFlow() const { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000917
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100918 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000919
920 private:
921 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
922};
923
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000924// Represents dex's RETURN opcodes. A HReturn is a control flow
925// instruction that branches to the exit block.
926class HReturn : public HTemplateInstruction<1> {
927 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100928 explicit HReturn(HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000929 SetRawInputAt(0, value);
930 }
931
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100932 virtual bool IsControlFlow() const { return true; }
933
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100934 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000935
936 private:
937 DISALLOW_COPY_AND_ASSIGN(HReturn);
938};
939
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000940// The exit instruction is the only instruction of the exit block.
941// Instructions aborting the method (HTrow and HReturn) must branch to the
942// exit block.
943class HExit : public HTemplateInstruction<0> {
944 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100945 HExit() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100946
947 virtual bool IsControlFlow() const { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000948
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100949 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000950
951 private:
952 DISALLOW_COPY_AND_ASSIGN(HExit);
953};
954
955// Jumps from one block to another.
956class HGoto : public HTemplateInstruction<0> {
957 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100958 HGoto() : HTemplateInstruction(SideEffects::None()) {}
959
960 virtual bool IsControlFlow() const { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000961
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000962 HBasicBlock* GetSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100963 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000964 }
965
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100966 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000967
968 private:
969 DISALLOW_COPY_AND_ASSIGN(HGoto);
970};
971
Dave Allison20dfc792014-06-16 20:44:29 -0700972
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000973// Conditional branch. A block ending with an HIf instruction must have
974// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000975class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000976 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100977 explicit HIf(HInstruction* input) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000978 SetRawInputAt(0, input);
979 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000980
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100981 virtual bool IsControlFlow() const { return true; }
982
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000983 HBasicBlock* IfTrueSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100984 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000985 }
986
987 HBasicBlock* IfFalseSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100988 return GetBlock()->GetSuccessors().Get(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000989 }
990
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100991 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000992
Dave Allison20dfc792014-06-16 20:44:29 -0700993 virtual bool IsIfInstruction() const { return true; }
994
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000995 private:
996 DISALLOW_COPY_AND_ASSIGN(HIf);
997};
998
Dave Allison20dfc792014-06-16 20:44:29 -0700999class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001000 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001001 HBinaryOperation(Primitive::Type result_type,
1002 HInstruction* left,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001003 HInstruction* right) : HExpression(result_type, SideEffects::None()) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001004 SetRawInputAt(0, left);
1005 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001006 }
1007
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001008 HInstruction* GetLeft() const { return InputAt(0); }
1009 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07001010 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001011
1012 virtual bool IsCommutative() { return false; }
1013
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001014 virtual bool CanBeMoved() const { return true; }
1015 virtual bool InstructionDataEquals(HInstruction* other) const { return true; }
1016
Roland Levillainccc07a92014-09-16 14:48:16 +01001017 DECLARE_INSTRUCTION(BinaryOperation);
1018
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001019 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001020 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
1021};
1022
Dave Allison20dfc792014-06-16 20:44:29 -07001023class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001024 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001025 HCondition(HInstruction* first, HInstruction* second)
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001026 : HBinaryOperation(Primitive::kPrimBoolean, first, second) {}
1027
1028 virtual bool IsCommutative() { return true; }
Dave Allison20dfc792014-06-16 20:44:29 -07001029 bool NeedsMaterialization() const;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001030
Dave Allison20dfc792014-06-16 20:44:29 -07001031 DECLARE_INSTRUCTION(Condition);
1032
1033 virtual IfCondition GetCondition() const = 0;
1034
1035 private:
1036 DISALLOW_COPY_AND_ASSIGN(HCondition);
1037};
1038
1039// Instruction to check if two inputs are equal to each other.
1040class HEqual : public HCondition {
1041 public:
1042 HEqual(HInstruction* first, HInstruction* second)
1043 : HCondition(first, second) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001044
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001045 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001046
Dave Allison20dfc792014-06-16 20:44:29 -07001047 virtual IfCondition GetCondition() const {
1048 return kCondEQ;
1049 }
1050
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001051 private:
1052 DISALLOW_COPY_AND_ASSIGN(HEqual);
1053};
1054
Dave Allison20dfc792014-06-16 20:44:29 -07001055class HNotEqual : public HCondition {
1056 public:
1057 HNotEqual(HInstruction* first, HInstruction* second)
1058 : HCondition(first, second) {}
1059
1060 DECLARE_INSTRUCTION(NotEqual);
1061
1062 virtual IfCondition GetCondition() const {
1063 return kCondNE;
1064 }
1065
1066 private:
1067 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
1068};
1069
1070class HLessThan : public HCondition {
1071 public:
1072 HLessThan(HInstruction* first, HInstruction* second)
1073 : HCondition(first, second) {}
1074
1075 DECLARE_INSTRUCTION(LessThan);
1076
1077 virtual IfCondition GetCondition() const {
1078 return kCondLT;
1079 }
1080
1081 private:
1082 DISALLOW_COPY_AND_ASSIGN(HLessThan);
1083};
1084
1085class HLessThanOrEqual : public HCondition {
1086 public:
1087 HLessThanOrEqual(HInstruction* first, HInstruction* second)
1088 : HCondition(first, second) {}
1089
1090 DECLARE_INSTRUCTION(LessThanOrEqual);
1091
1092 virtual IfCondition GetCondition() const {
1093 return kCondLE;
1094 }
1095
1096 private:
1097 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
1098};
1099
1100class HGreaterThan : public HCondition {
1101 public:
1102 HGreaterThan(HInstruction* first, HInstruction* second)
1103 : HCondition(first, second) {}
1104
1105 DECLARE_INSTRUCTION(GreaterThan);
1106
1107 virtual IfCondition GetCondition() const {
1108 return kCondGT;
1109 }
1110
1111 private:
1112 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
1113};
1114
1115class HGreaterThanOrEqual : public HCondition {
1116 public:
1117 HGreaterThanOrEqual(HInstruction* first, HInstruction* second)
1118 : HCondition(first, second) {}
1119
1120 DECLARE_INSTRUCTION(GreaterThanOrEqual);
1121
1122 virtual IfCondition GetCondition() const {
1123 return kCondGE;
1124 }
1125
1126 private:
1127 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
1128};
1129
1130
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001131// Instruction to check how two inputs compare to each other.
1132// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
1133class HCompare : public HBinaryOperation {
1134 public:
1135 HCompare(Primitive::Type type, HInstruction* first, HInstruction* second)
1136 : HBinaryOperation(Primitive::kPrimInt, first, second) {
1137 DCHECK_EQ(type, first->GetType());
1138 DCHECK_EQ(type, second->GetType());
1139 }
1140
1141 DECLARE_INSTRUCTION(Compare);
1142
1143 private:
1144 DISALLOW_COPY_AND_ASSIGN(HCompare);
1145};
1146
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001147// A local in the graph. Corresponds to a Dex register.
1148class HLocal : public HTemplateInstruction<0> {
1149 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001150 explicit HLocal(uint16_t reg_number)
1151 : HTemplateInstruction(SideEffects::None()), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001152
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001153 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001154
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001155 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001156
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001157 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001158 // The Dex register number.
1159 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001160
1161 DISALLOW_COPY_AND_ASSIGN(HLocal);
1162};
1163
1164// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07001165class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001166 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001167 explicit HLoadLocal(HLocal* local, Primitive::Type type)
1168 : HExpression(type, SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001169 SetRawInputAt(0, local);
1170 }
1171
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001172 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1173
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001174 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001175
1176 private:
1177 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
1178};
1179
1180// Store a value in a given local. This instruction has two inputs: the value
1181// and the local.
1182class HStoreLocal : public HTemplateInstruction<2> {
1183 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001184 HStoreLocal(HLocal* local, HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001185 SetRawInputAt(0, local);
1186 SetRawInputAt(1, value);
1187 }
1188
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001189 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1190
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001191 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001192
1193 private:
1194 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
1195};
1196
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001197class HConstant : public HExpression<0> {
1198 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001199 explicit HConstant(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
1200
1201 virtual bool CanBeMoved() const { return true; }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001202
1203 DECLARE_INSTRUCTION(Constant);
1204
1205 private:
1206 DISALLOW_COPY_AND_ASSIGN(HConstant);
1207};
1208
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001209// Constants of the type int. Those can be from Dex instructions, or
1210// synthesized (for example with the if-eqz instruction).
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001211class HIntConstant : public HConstant {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001212 public:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001213 explicit HIntConstant(int32_t value) : HConstant(Primitive::kPrimInt), value_(value) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001214
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001215 int32_t GetValue() const { return value_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001216
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001217 virtual bool InstructionDataEquals(HInstruction* other) const {
1218 return other->AsIntConstant()->value_ == value_;
1219 }
1220
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001221 DECLARE_INSTRUCTION(IntConstant);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001222
1223 private:
1224 const int32_t value_;
1225
1226 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
1227};
1228
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001229class HLongConstant : public HConstant {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001230 public:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001231 explicit HLongConstant(int64_t value) : HConstant(Primitive::kPrimLong), value_(value) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001232
1233 int64_t GetValue() const { return value_; }
1234
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001235 virtual bool InstructionDataEquals(HInstruction* other) const {
1236 return other->AsLongConstant()->value_ == value_;
1237 }
1238
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001239 DECLARE_INSTRUCTION(LongConstant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001240
1241 private:
1242 const int64_t value_;
1243
1244 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
1245};
1246
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001247class HInvoke : public HInstruction {
1248 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001249 HInvoke(ArenaAllocator* arena,
1250 uint32_t number_of_arguments,
1251 Primitive::Type return_type,
1252 uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001253 : HInstruction(SideEffects::All()),
1254 inputs_(arena, number_of_arguments),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001255 return_type_(return_type),
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001256 dex_pc_(dex_pc) {
1257 inputs_.SetSize(number_of_arguments);
1258 }
1259
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001260 virtual size_t InputCount() const { return inputs_.Size(); }
1261 virtual HInstruction* InputAt(size_t i) const { return inputs_.Get(i); }
1262
1263 // Runtime needs to walk the stack, so Dex -> Dex calls need to
1264 // know their environment.
1265 virtual bool NeedsEnvironment() const { return true; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001266
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001267 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001268 SetRawInputAt(index, argument);
1269 }
1270
1271 virtual void SetRawInputAt(size_t index, HInstruction* input) {
1272 inputs_.Put(index, input);
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001273 }
1274
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001275 virtual Primitive::Type GetType() const { return return_type_; }
1276
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001277 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001278
1279 protected:
1280 GrowableArray<HInstruction*> inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001281 const Primitive::Type return_type_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001282 const uint32_t dex_pc_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001283
1284 private:
1285 DISALLOW_COPY_AND_ASSIGN(HInvoke);
1286};
1287
1288class HInvokeStatic : public HInvoke {
1289 public:
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001290 HInvokeStatic(ArenaAllocator* arena,
1291 uint32_t number_of_arguments,
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001292 Primitive::Type return_type,
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001293 uint32_t dex_pc,
1294 uint32_t index_in_dex_cache)
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001295 : HInvoke(arena, number_of_arguments, return_type, dex_pc),
1296 index_in_dex_cache_(index_in_dex_cache) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001297
1298 uint32_t GetIndexInDexCache() const { return index_in_dex_cache_; }
1299
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001300 DECLARE_INSTRUCTION(InvokeStatic);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001301
1302 private:
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001303 const uint32_t index_in_dex_cache_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001304
1305 DISALLOW_COPY_AND_ASSIGN(HInvokeStatic);
1306};
1307
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001308class HInvokeVirtual : public HInvoke {
1309 public:
1310 HInvokeVirtual(ArenaAllocator* arena,
1311 uint32_t number_of_arguments,
1312 Primitive::Type return_type,
1313 uint32_t dex_pc,
1314 uint32_t vtable_index)
1315 : HInvoke(arena, number_of_arguments, return_type, dex_pc),
1316 vtable_index_(vtable_index) {}
1317
1318 uint32_t GetVTableIndex() const { return vtable_index_; }
1319
1320 DECLARE_INSTRUCTION(InvokeVirtual);
1321
1322 private:
1323 const uint32_t vtable_index_;
1324
1325 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
1326};
1327
Dave Allison20dfc792014-06-16 20:44:29 -07001328class HNewInstance : public HExpression<0> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001329 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001330 HNewInstance(uint32_t dex_pc, uint16_t type_index)
1331 : HExpression(Primitive::kPrimNot, SideEffects::None()),
1332 dex_pc_(dex_pc),
1333 type_index_(type_index) {}
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001334
1335 uint32_t GetDexPc() const { return dex_pc_; }
1336 uint16_t GetTypeIndex() const { return type_index_; }
1337
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001338 // Calls runtime so needs an environment.
1339 virtual bool NeedsEnvironment() const { return true; }
1340
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001341 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001342
1343 private:
1344 const uint32_t dex_pc_;
1345 const uint16_t type_index_;
1346
1347 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
1348};
1349
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001350class HAdd : public HBinaryOperation {
1351 public:
1352 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1353 : HBinaryOperation(result_type, left, right) {}
1354
1355 virtual bool IsCommutative() { return true; }
1356
1357 DECLARE_INSTRUCTION(Add);
1358
1359 private:
1360 DISALLOW_COPY_AND_ASSIGN(HAdd);
1361};
1362
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001363class HSub : public HBinaryOperation {
1364 public:
1365 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1366 : HBinaryOperation(result_type, left, right) {}
1367
1368 virtual bool IsCommutative() { return false; }
1369
1370 DECLARE_INSTRUCTION(Sub);
1371
1372 private:
1373 DISALLOW_COPY_AND_ASSIGN(HSub);
1374};
1375
1376// The value of a parameter in this method. Its location depends on
1377// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07001378class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001379 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001380 HParameterValue(uint8_t index, Primitive::Type parameter_type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001381 : HExpression(parameter_type, SideEffects::None()), index_(index) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001382
1383 uint8_t GetIndex() const { return index_; }
1384
1385 DECLARE_INSTRUCTION(ParameterValue);
1386
1387 private:
1388 // The index of this parameter in the parameters list. Must be less
1389 // than HGraph::number_of_in_vregs_;
1390 const uint8_t index_;
1391
1392 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
1393};
1394
Dave Allison20dfc792014-06-16 20:44:29 -07001395class HNot : public HExpression<1> {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001396 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001397 explicit HNot(HInstruction* input) : HExpression(Primitive::kPrimBoolean, SideEffects::None()) {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001398 SetRawInputAt(0, input);
1399 }
1400
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001401 virtual bool CanBeMoved() const { return true; }
1402 virtual bool InstructionDataEquals(HInstruction* other) const { return true; }
1403
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001404 DECLARE_INSTRUCTION(Not);
1405
1406 private:
1407 DISALLOW_COPY_AND_ASSIGN(HNot);
1408};
1409
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001410class HPhi : public HInstruction {
1411 public:
1412 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001413 : HInstruction(SideEffects::None()),
1414 inputs_(arena, number_of_inputs),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001415 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001416 type_(type),
1417 is_live_(false) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001418 inputs_.SetSize(number_of_inputs);
1419 }
1420
1421 virtual size_t InputCount() const { return inputs_.Size(); }
1422 virtual HInstruction* InputAt(size_t i) const { return inputs_.Get(i); }
1423
1424 virtual void SetRawInputAt(size_t index, HInstruction* input) {
1425 inputs_.Put(index, input);
1426 }
1427
1428 void AddInput(HInstruction* input);
1429
1430 virtual Primitive::Type GetType() const { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001431 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001432
1433 uint32_t GetRegNumber() const { return reg_number_; }
1434
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001435 void SetDead() { is_live_ = false; }
1436 void SetLive() { is_live_ = true; }
1437 bool IsDead() const { return !is_live_; }
1438 bool IsLive() const { return is_live_; }
1439
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001440 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001441
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001442 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001443 GrowableArray<HInstruction*> inputs_;
1444 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001445 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001446 bool is_live_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001447
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001448 DISALLOW_COPY_AND_ASSIGN(HPhi);
1449};
1450
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001451class HNullCheck : public HExpression<1> {
1452 public:
1453 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001454 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001455 SetRawInputAt(0, value);
1456 }
1457
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001458 virtual bool CanBeMoved() const { return true; }
1459 virtual bool InstructionDataEquals(HInstruction* other) const { return true; }
1460
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001461 virtual bool NeedsEnvironment() const { return true; }
1462
1463 uint32_t GetDexPc() const { return dex_pc_; }
1464
1465 DECLARE_INSTRUCTION(NullCheck);
1466
1467 private:
1468 const uint32_t dex_pc_;
1469
1470 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
1471};
1472
1473class FieldInfo : public ValueObject {
1474 public:
Nicolas Geoffray39468442014-09-02 15:17:15 +01001475 explicit FieldInfo(MemberOffset field_offset, Primitive::Type field_type)
1476 : field_offset_(field_offset), field_type_(field_type) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001477
1478 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001479 Primitive::Type GetFieldType() const { return field_type_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001480
1481 private:
1482 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01001483 const Primitive::Type field_type_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001484};
1485
1486class HInstanceFieldGet : public HExpression<1> {
1487 public:
1488 HInstanceFieldGet(HInstruction* value,
1489 Primitive::Type field_type,
1490 MemberOffset field_offset)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001491 : HExpression(field_type, SideEffects::DependsOnSomething()),
1492 field_info_(field_offset, field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001493 SetRawInputAt(0, value);
1494 }
1495
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001496 virtual bool CanBeMoved() const { return true; }
1497 virtual bool InstructionDataEquals(HInstruction* other) const {
1498 size_t other_offset = other->AsInstanceFieldGet()->GetFieldOffset().SizeValue();
1499 return other_offset == GetFieldOffset().SizeValue();
1500 }
1501
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001502 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001503 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001504
1505 DECLARE_INSTRUCTION(InstanceFieldGet);
1506
1507 private:
1508 const FieldInfo field_info_;
1509
1510 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
1511};
1512
1513class HInstanceFieldSet : public HTemplateInstruction<2> {
1514 public:
1515 HInstanceFieldSet(HInstruction* object,
1516 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01001517 Primitive::Type field_type,
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001518 MemberOffset field_offset)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001519 : HTemplateInstruction(SideEffects::ChangesSomething()),
1520 field_info_(field_offset, field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001521 SetRawInputAt(0, object);
1522 SetRawInputAt(1, value);
1523 }
1524
1525 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001526 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001527
1528 DECLARE_INSTRUCTION(InstanceFieldSet);
1529
1530 private:
1531 const FieldInfo field_info_;
1532
1533 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
1534};
1535
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001536class HArrayGet : public HExpression<2> {
1537 public:
1538 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001539 : HExpression(type, SideEffects::DependsOnSomething()) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001540 SetRawInputAt(0, array);
1541 SetRawInputAt(1, index);
1542 }
1543
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001544 virtual bool CanBeMoved() const { return true; }
1545 virtual bool InstructionDataEquals(HInstruction* other) const { return true; }
1546
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001547 DECLARE_INSTRUCTION(ArrayGet);
1548
1549 private:
1550 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
1551};
1552
1553class HArraySet : public HTemplateInstruction<3> {
1554 public:
1555 HArraySet(HInstruction* array,
1556 HInstruction* index,
1557 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01001558 Primitive::Type component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001559 uint32_t dex_pc)
1560 : HTemplateInstruction(SideEffects::ChangesSomething()),
1561 dex_pc_(dex_pc),
1562 component_type_(component_type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001563 SetRawInputAt(0, array);
1564 SetRawInputAt(1, index);
1565 SetRawInputAt(2, value);
1566 }
1567
1568 virtual bool NeedsEnvironment() const {
1569 // We currently always call a runtime method to catch array store
1570 // exceptions.
1571 return InputAt(2)->GetType() == Primitive::kPrimNot;
1572 }
1573
1574 uint32_t GetDexPc() const { return dex_pc_; }
1575
Nicolas Geoffray39468442014-09-02 15:17:15 +01001576 Primitive::Type GetComponentType() const { return component_type_; }
1577
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001578 DECLARE_INSTRUCTION(ArraySet);
1579
1580 private:
1581 const uint32_t dex_pc_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01001582 const Primitive::Type component_type_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001583
1584 DISALLOW_COPY_AND_ASSIGN(HArraySet);
1585};
1586
1587class HArrayLength : public HExpression<1> {
1588 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001589 explicit HArrayLength(HInstruction* array)
1590 : HExpression(Primitive::kPrimInt, SideEffects::None()) {
1591 // Note that arrays do not change length, so the instruction does not
1592 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001593 SetRawInputAt(0, array);
1594 }
1595
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001596 virtual bool CanBeMoved() const { return true; }
1597 virtual bool InstructionDataEquals(HInstruction* other) const { return true; }
1598
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001599 DECLARE_INSTRUCTION(ArrayLength);
1600
1601 private:
1602 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
1603};
1604
1605class HBoundsCheck : public HExpression<2> {
1606 public:
1607 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001608 : HExpression(index->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001609 DCHECK(index->GetType() == Primitive::kPrimInt);
1610 SetRawInputAt(0, index);
1611 SetRawInputAt(1, length);
1612 }
1613
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001614 virtual bool CanBeMoved() const { return true; }
1615 virtual bool InstructionDataEquals(HInstruction* other) const { return true; }
1616
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001617 virtual bool NeedsEnvironment() const { return true; }
1618
1619 uint32_t GetDexPc() const { return dex_pc_; }
1620
1621 DECLARE_INSTRUCTION(BoundsCheck);
1622
1623 private:
1624 const uint32_t dex_pc_;
1625
1626 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
1627};
1628
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001629/**
1630 * Some DEX instructions are folded into multiple HInstructions that need
1631 * to stay live until the last HInstruction. This class
1632 * is used as a marker for the baseline compiler to ensure its preceding
1633 * HInstruction stays live. `index` is the temporary number that is used
1634 * for knowing the stack offset where to store the instruction.
1635 */
1636class HTemporary : public HTemplateInstruction<0> {
1637 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001638 explicit HTemporary(size_t index) : HTemplateInstruction(SideEffects::None()), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001639
1640 size_t GetIndex() const { return index_; }
1641
1642 DECLARE_INSTRUCTION(Temporary);
1643
1644 private:
1645 const size_t index_;
1646
1647 DISALLOW_COPY_AND_ASSIGN(HTemporary);
1648};
1649
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001650class HSuspendCheck : public HTemplateInstruction<0> {
1651 public:
1652 explicit HSuspendCheck(uint32_t dex_pc)
1653 : HTemplateInstruction(SideEffects::ChangesSomething()), dex_pc_(dex_pc) {}
1654
1655 virtual bool NeedsEnvironment() const {
1656 return true;
1657 }
1658
1659 uint32_t GetDexPc() const { return dex_pc_; }
1660
1661 DECLARE_INSTRUCTION(SuspendCheck);
1662
1663 private:
1664 const uint32_t dex_pc_;
1665
1666 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
1667};
1668
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001669class MoveOperands : public ArenaObject {
1670 public:
1671 MoveOperands(Location source, Location destination)
1672 : source_(source), destination_(destination) {}
1673
1674 Location GetSource() const { return source_; }
1675 Location GetDestination() const { return destination_; }
1676
1677 void SetSource(Location value) { source_ = value; }
1678 void SetDestination(Location value) { destination_ = value; }
1679
1680 // The parallel move resolver marks moves as "in-progress" by clearing the
1681 // destination (but not the source).
1682 Location MarkPending() {
1683 DCHECK(!IsPending());
1684 Location dest = destination_;
1685 destination_ = Location::NoLocation();
1686 return dest;
1687 }
1688
1689 void ClearPending(Location dest) {
1690 DCHECK(IsPending());
1691 destination_ = dest;
1692 }
1693
1694 bool IsPending() const {
1695 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
1696 return destination_.IsInvalid() && !source_.IsInvalid();
1697 }
1698
1699 // True if this blocks a move from the given location.
1700 bool Blocks(Location loc) const {
1701 return !IsEliminated() && source_.Equals(loc);
1702 }
1703
1704 // A move is redundant if it's been eliminated, if its source and
1705 // destination are the same, or if its destination is unneeded.
1706 bool IsRedundant() const {
1707 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
1708 }
1709
1710 // We clear both operands to indicate move that's been eliminated.
1711 void Eliminate() {
1712 source_ = destination_ = Location::NoLocation();
1713 }
1714
1715 bool IsEliminated() const {
1716 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
1717 return source_.IsInvalid();
1718 }
1719
1720 private:
1721 Location source_;
1722 Location destination_;
1723
1724 DISALLOW_COPY_AND_ASSIGN(MoveOperands);
1725};
1726
1727static constexpr size_t kDefaultNumberOfMoves = 4;
1728
1729class HParallelMove : public HTemplateInstruction<0> {
1730 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001731 explicit HParallelMove(ArenaAllocator* arena)
1732 : HTemplateInstruction(SideEffects::None()), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001733
1734 void AddMove(MoveOperands* move) {
1735 moves_.Add(move);
1736 }
1737
1738 MoveOperands* MoveOperandsAt(size_t index) const {
1739 return moves_.Get(index);
1740 }
1741
1742 size_t NumMoves() const { return moves_.Size(); }
1743
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001744 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001745
1746 private:
1747 GrowableArray<MoveOperands*> moves_;
1748
1749 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
1750};
1751
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001752class HGraphVisitor : public ValueObject {
1753 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001754 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
1755 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001756
Dave Allison20dfc792014-06-16 20:44:29 -07001757 virtual void VisitInstruction(HInstruction* instruction) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001758 virtual void VisitBasicBlock(HBasicBlock* block);
1759
1760 void VisitInsertionOrder();
1761
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001762 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001763
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001764 // Visit functions for instruction classes.
1765#define DECLARE_VISIT_INSTRUCTION(name) \
1766 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
1767
1768 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
1769
1770#undef DECLARE_VISIT_INSTRUCTION
1771
1772 private:
1773 HGraph* graph_;
1774
1775 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
1776};
1777
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001778class HInsertionOrderIterator : public ValueObject {
1779 public:
1780 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
1781
1782 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
1783 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
1784 void Advance() { ++index_; }
1785
1786 private:
1787 const HGraph& graph_;
1788 size_t index_;
1789
1790 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
1791};
1792
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001793class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001794 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001795 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001796
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001797 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
1798 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001799 void Advance() { ++index_; }
1800
1801 private:
1802 const HGraph& graph_;
1803 size_t index_;
1804
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001805 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001806};
1807
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001808class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001809 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001810 explicit HPostOrderIterator(const HGraph& graph)
1811 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001812
1813 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001814 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001815 void Advance() { --index_; }
1816
1817 private:
1818 const HGraph& graph_;
1819 size_t index_;
1820
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001821 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001822};
1823
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001824} // namespace art
1825
1826#endif // ART_COMPILER_OPTIMIZING_NODES_H_