blob: d98d2ad75f05f9a924036daa9cdc1c0dcb0a95fc [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 Levillain556c3d12014-09-18 15:25:07 +01001017 // Try to statically evaluate `operation` and return an HConstant
1018 // containing the result of this evaluation. If `operation` cannot
1019 // be evaluated as a constant, return nullptr.
1020 HConstant* TryStaticEvaluation(ArenaAllocator* allocator) const;
1021
1022 // Apply this operation to `x` and `y`.
1023 virtual int32_t Evaluate(int32_t x, int32_t y) const = 0;
1024 virtual int64_t Evaluate(int64_t x, int64_t y) const = 0;
1025
Roland Levillainccc07a92014-09-16 14:48:16 +01001026 DECLARE_INSTRUCTION(BinaryOperation);
1027
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001028 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001029 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
1030};
1031
Dave Allison20dfc792014-06-16 20:44:29 -07001032class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001033 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001034 HCondition(HInstruction* first, HInstruction* second)
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001035 : HBinaryOperation(Primitive::kPrimBoolean, first, second) {}
1036
1037 virtual bool IsCommutative() { return true; }
Dave Allison20dfc792014-06-16 20:44:29 -07001038 bool NeedsMaterialization() const;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001039
Dave Allison20dfc792014-06-16 20:44:29 -07001040 DECLARE_INSTRUCTION(Condition);
1041
1042 virtual IfCondition GetCondition() const = 0;
1043
1044 private:
1045 DISALLOW_COPY_AND_ASSIGN(HCondition);
1046};
1047
1048// Instruction to check if two inputs are equal to each other.
1049class HEqual : public HCondition {
1050 public:
1051 HEqual(HInstruction* first, HInstruction* second)
1052 : HCondition(first, second) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001053
Roland Levillain556c3d12014-09-18 15:25:07 +01001054 virtual int32_t Evaluate(int32_t x, int32_t y) const { return x == y; }
1055 virtual int64_t Evaluate(int64_t x, int64_t y) const { return x == y; }
1056
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001057 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001058
Dave Allison20dfc792014-06-16 20:44:29 -07001059 virtual IfCondition GetCondition() const {
1060 return kCondEQ;
1061 }
1062
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001063 private:
1064 DISALLOW_COPY_AND_ASSIGN(HEqual);
1065};
1066
Dave Allison20dfc792014-06-16 20:44:29 -07001067class HNotEqual : public HCondition {
1068 public:
1069 HNotEqual(HInstruction* first, HInstruction* second)
1070 : HCondition(first, second) {}
1071
Roland Levillain556c3d12014-09-18 15:25:07 +01001072 virtual int32_t Evaluate(int32_t x, int32_t y) const { return x != y; }
1073 virtual int64_t Evaluate(int64_t x, int64_t y) const { return x != y; }
1074
Dave Allison20dfc792014-06-16 20:44:29 -07001075 DECLARE_INSTRUCTION(NotEqual);
1076
1077 virtual IfCondition GetCondition() const {
1078 return kCondNE;
1079 }
1080
1081 private:
1082 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
1083};
1084
1085class HLessThan : public HCondition {
1086 public:
1087 HLessThan(HInstruction* first, HInstruction* second)
1088 : HCondition(first, second) {}
1089
Roland Levillain556c3d12014-09-18 15:25:07 +01001090 virtual int32_t Evaluate(int32_t x, int32_t y) const { return x < y; }
1091 virtual int64_t Evaluate(int64_t x, int64_t y) const { return x < y; }
1092
Dave Allison20dfc792014-06-16 20:44:29 -07001093 DECLARE_INSTRUCTION(LessThan);
1094
1095 virtual IfCondition GetCondition() const {
1096 return kCondLT;
1097 }
1098
1099 private:
1100 DISALLOW_COPY_AND_ASSIGN(HLessThan);
1101};
1102
1103class HLessThanOrEqual : public HCondition {
1104 public:
1105 HLessThanOrEqual(HInstruction* first, HInstruction* second)
1106 : HCondition(first, second) {}
1107
Roland Levillain556c3d12014-09-18 15:25:07 +01001108 virtual int32_t Evaluate(int32_t x, int32_t y) const { return x <= y; }
1109 virtual int64_t Evaluate(int64_t x, int64_t y) const { return x <= y; }
1110
Dave Allison20dfc792014-06-16 20:44:29 -07001111 DECLARE_INSTRUCTION(LessThanOrEqual);
1112
1113 virtual IfCondition GetCondition() const {
1114 return kCondLE;
1115 }
1116
1117 private:
1118 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
1119};
1120
1121class HGreaterThan : public HCondition {
1122 public:
1123 HGreaterThan(HInstruction* first, HInstruction* second)
1124 : HCondition(first, second) {}
1125
Roland Levillain556c3d12014-09-18 15:25:07 +01001126 virtual int32_t Evaluate(int32_t x, int32_t y) const { return x > y; }
1127 virtual int64_t Evaluate(int64_t x, int64_t y) const { return x > y; }
1128
Dave Allison20dfc792014-06-16 20:44:29 -07001129 DECLARE_INSTRUCTION(GreaterThan);
1130
1131 virtual IfCondition GetCondition() const {
1132 return kCondGT;
1133 }
1134
1135 private:
1136 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
1137};
1138
1139class HGreaterThanOrEqual : public HCondition {
1140 public:
1141 HGreaterThanOrEqual(HInstruction* first, HInstruction* second)
1142 : HCondition(first, second) {}
1143
Roland Levillain556c3d12014-09-18 15:25:07 +01001144 virtual int32_t Evaluate(int32_t x, int32_t y) const { return x >= y; }
1145 virtual int64_t Evaluate(int64_t x, int64_t y) const { return x >= y; }
1146
Dave Allison20dfc792014-06-16 20:44:29 -07001147 DECLARE_INSTRUCTION(GreaterThanOrEqual);
1148
1149 virtual IfCondition GetCondition() const {
1150 return kCondGE;
1151 }
1152
1153 private:
1154 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
1155};
1156
1157
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001158// Instruction to check how two inputs compare to each other.
1159// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
1160class HCompare : public HBinaryOperation {
1161 public:
1162 HCompare(Primitive::Type type, HInstruction* first, HInstruction* second)
1163 : HBinaryOperation(Primitive::kPrimInt, first, second) {
1164 DCHECK_EQ(type, first->GetType());
1165 DCHECK_EQ(type, second->GetType());
1166 }
1167
Roland Levillain556c3d12014-09-18 15:25:07 +01001168 virtual int32_t Evaluate(int32_t x, int32_t y) const {
1169 return
1170 x == y ? 0 :
1171 x > y ? 1 :
1172 -1;
1173 }
1174 virtual int64_t Evaluate(int64_t x, int64_t y) const {
1175 return
1176 x == y ? 0 :
1177 x > y ? 1 :
1178 -1;
1179 }
1180
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001181 DECLARE_INSTRUCTION(Compare);
1182
1183 private:
1184 DISALLOW_COPY_AND_ASSIGN(HCompare);
1185};
1186
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001187// A local in the graph. Corresponds to a Dex register.
1188class HLocal : public HTemplateInstruction<0> {
1189 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001190 explicit HLocal(uint16_t reg_number)
1191 : HTemplateInstruction(SideEffects::None()), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001192
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001193 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001194
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001195 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001196
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001197 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001198 // The Dex register number.
1199 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001200
1201 DISALLOW_COPY_AND_ASSIGN(HLocal);
1202};
1203
1204// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07001205class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001206 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001207 explicit HLoadLocal(HLocal* local, Primitive::Type type)
1208 : HExpression(type, SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001209 SetRawInputAt(0, local);
1210 }
1211
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001212 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1213
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001214 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001215
1216 private:
1217 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
1218};
1219
1220// Store a value in a given local. This instruction has two inputs: the value
1221// and the local.
1222class HStoreLocal : public HTemplateInstruction<2> {
1223 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001224 HStoreLocal(HLocal* local, HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001225 SetRawInputAt(0, local);
1226 SetRawInputAt(1, value);
1227 }
1228
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001229 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1230
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001231 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001232
1233 private:
1234 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
1235};
1236
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001237class HConstant : public HExpression<0> {
1238 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001239 explicit HConstant(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
1240
1241 virtual bool CanBeMoved() const { return true; }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001242
1243 DECLARE_INSTRUCTION(Constant);
1244
1245 private:
1246 DISALLOW_COPY_AND_ASSIGN(HConstant);
1247};
1248
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001249// Constants of the type int. Those can be from Dex instructions, or
1250// synthesized (for example with the if-eqz instruction).
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001251class HIntConstant : public HConstant {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001252 public:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001253 explicit HIntConstant(int32_t value) : HConstant(Primitive::kPrimInt), value_(value) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001254
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001255 int32_t GetValue() const { return value_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001256
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001257 virtual bool InstructionDataEquals(HInstruction* other) const {
1258 return other->AsIntConstant()->value_ == value_;
1259 }
1260
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001261 DECLARE_INSTRUCTION(IntConstant);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001262
1263 private:
1264 const int32_t value_;
1265
1266 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
1267};
1268
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001269class HLongConstant : public HConstant {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001270 public:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001271 explicit HLongConstant(int64_t value) : HConstant(Primitive::kPrimLong), value_(value) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001272
1273 int64_t GetValue() const { return value_; }
1274
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001275 virtual bool InstructionDataEquals(HInstruction* other) const {
1276 return other->AsLongConstant()->value_ == value_;
1277 }
1278
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001279 DECLARE_INSTRUCTION(LongConstant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001280
1281 private:
1282 const int64_t value_;
1283
1284 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
1285};
1286
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001287class HInvoke : public HInstruction {
1288 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001289 HInvoke(ArenaAllocator* arena,
1290 uint32_t number_of_arguments,
1291 Primitive::Type return_type,
1292 uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001293 : HInstruction(SideEffects::All()),
1294 inputs_(arena, number_of_arguments),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001295 return_type_(return_type),
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001296 dex_pc_(dex_pc) {
1297 inputs_.SetSize(number_of_arguments);
1298 }
1299
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001300 virtual size_t InputCount() const { return inputs_.Size(); }
1301 virtual HInstruction* InputAt(size_t i) const { return inputs_.Get(i); }
1302
1303 // Runtime needs to walk the stack, so Dex -> Dex calls need to
1304 // know their environment.
1305 virtual bool NeedsEnvironment() const { return true; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001306
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001307 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001308 SetRawInputAt(index, argument);
1309 }
1310
1311 virtual void SetRawInputAt(size_t index, HInstruction* input) {
1312 inputs_.Put(index, input);
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001313 }
1314
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001315 virtual Primitive::Type GetType() const { return return_type_; }
1316
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001317 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001318
1319 protected:
1320 GrowableArray<HInstruction*> inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001321 const Primitive::Type return_type_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001322 const uint32_t dex_pc_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001323
1324 private:
1325 DISALLOW_COPY_AND_ASSIGN(HInvoke);
1326};
1327
1328class HInvokeStatic : public HInvoke {
1329 public:
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001330 HInvokeStatic(ArenaAllocator* arena,
1331 uint32_t number_of_arguments,
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001332 Primitive::Type return_type,
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001333 uint32_t dex_pc,
1334 uint32_t index_in_dex_cache)
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001335 : HInvoke(arena, number_of_arguments, return_type, dex_pc),
1336 index_in_dex_cache_(index_in_dex_cache) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001337
1338 uint32_t GetIndexInDexCache() const { return index_in_dex_cache_; }
1339
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001340 DECLARE_INSTRUCTION(InvokeStatic);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001341
1342 private:
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001343 const uint32_t index_in_dex_cache_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001344
1345 DISALLOW_COPY_AND_ASSIGN(HInvokeStatic);
1346};
1347
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001348class HInvokeVirtual : public HInvoke {
1349 public:
1350 HInvokeVirtual(ArenaAllocator* arena,
1351 uint32_t number_of_arguments,
1352 Primitive::Type return_type,
1353 uint32_t dex_pc,
1354 uint32_t vtable_index)
1355 : HInvoke(arena, number_of_arguments, return_type, dex_pc),
1356 vtable_index_(vtable_index) {}
1357
1358 uint32_t GetVTableIndex() const { return vtable_index_; }
1359
1360 DECLARE_INSTRUCTION(InvokeVirtual);
1361
1362 private:
1363 const uint32_t vtable_index_;
1364
1365 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
1366};
1367
Dave Allison20dfc792014-06-16 20:44:29 -07001368class HNewInstance : public HExpression<0> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001369 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001370 HNewInstance(uint32_t dex_pc, uint16_t type_index)
1371 : HExpression(Primitive::kPrimNot, SideEffects::None()),
1372 dex_pc_(dex_pc),
1373 type_index_(type_index) {}
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001374
1375 uint32_t GetDexPc() const { return dex_pc_; }
1376 uint16_t GetTypeIndex() const { return type_index_; }
1377
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001378 // Calls runtime so needs an environment.
1379 virtual bool NeedsEnvironment() const { return true; }
1380
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001381 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001382
1383 private:
1384 const uint32_t dex_pc_;
1385 const uint16_t type_index_;
1386
1387 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
1388};
1389
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001390class HAdd : public HBinaryOperation {
1391 public:
1392 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1393 : HBinaryOperation(result_type, left, right) {}
1394
1395 virtual bool IsCommutative() { return true; }
1396
Roland Levillain556c3d12014-09-18 15:25:07 +01001397 virtual int32_t Evaluate(int32_t x, int32_t y) const { return x + y; }
1398 virtual int64_t Evaluate(int64_t x, int64_t y) const { return x + y; }
1399
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001400 DECLARE_INSTRUCTION(Add);
1401
1402 private:
1403 DISALLOW_COPY_AND_ASSIGN(HAdd);
1404};
1405
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001406class HSub : public HBinaryOperation {
1407 public:
1408 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1409 : HBinaryOperation(result_type, left, right) {}
1410
1411 virtual bool IsCommutative() { return false; }
1412
Roland Levillain556c3d12014-09-18 15:25:07 +01001413 virtual int32_t Evaluate(int32_t x, int32_t y) const { return x + y; }
1414 virtual int64_t Evaluate(int64_t x, int64_t y) const { return x + y; }
1415
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001416 DECLARE_INSTRUCTION(Sub);
1417
1418 private:
1419 DISALLOW_COPY_AND_ASSIGN(HSub);
1420};
1421
1422// The value of a parameter in this method. Its location depends on
1423// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07001424class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001425 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001426 HParameterValue(uint8_t index, Primitive::Type parameter_type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001427 : HExpression(parameter_type, SideEffects::None()), index_(index) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001428
1429 uint8_t GetIndex() const { return index_; }
1430
1431 DECLARE_INSTRUCTION(ParameterValue);
1432
1433 private:
1434 // The index of this parameter in the parameters list. Must be less
1435 // than HGraph::number_of_in_vregs_;
1436 const uint8_t index_;
1437
1438 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
1439};
1440
Dave Allison20dfc792014-06-16 20:44:29 -07001441class HNot : public HExpression<1> {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001442 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001443 explicit HNot(HInstruction* input) : HExpression(Primitive::kPrimBoolean, SideEffects::None()) {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001444 SetRawInputAt(0, input);
1445 }
1446
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001447 virtual bool CanBeMoved() const { return true; }
1448 virtual bool InstructionDataEquals(HInstruction* other) const { return true; }
1449
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001450 DECLARE_INSTRUCTION(Not);
1451
1452 private:
1453 DISALLOW_COPY_AND_ASSIGN(HNot);
1454};
1455
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001456class HPhi : public HInstruction {
1457 public:
1458 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001459 : HInstruction(SideEffects::None()),
1460 inputs_(arena, number_of_inputs),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001461 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001462 type_(type),
1463 is_live_(false) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001464 inputs_.SetSize(number_of_inputs);
1465 }
1466
1467 virtual size_t InputCount() const { return inputs_.Size(); }
1468 virtual HInstruction* InputAt(size_t i) const { return inputs_.Get(i); }
1469
1470 virtual void SetRawInputAt(size_t index, HInstruction* input) {
1471 inputs_.Put(index, input);
1472 }
1473
1474 void AddInput(HInstruction* input);
1475
1476 virtual Primitive::Type GetType() const { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001477 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001478
1479 uint32_t GetRegNumber() const { return reg_number_; }
1480
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001481 void SetDead() { is_live_ = false; }
1482 void SetLive() { is_live_ = true; }
1483 bool IsDead() const { return !is_live_; }
1484 bool IsLive() const { return is_live_; }
1485
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001486 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001487
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001488 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001489 GrowableArray<HInstruction*> inputs_;
1490 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001491 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001492 bool is_live_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001493
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001494 DISALLOW_COPY_AND_ASSIGN(HPhi);
1495};
1496
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001497class HNullCheck : public HExpression<1> {
1498 public:
1499 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001500 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001501 SetRawInputAt(0, value);
1502 }
1503
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001504 virtual bool CanBeMoved() const { return true; }
1505 virtual bool InstructionDataEquals(HInstruction* other) const { return true; }
1506
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001507 virtual bool NeedsEnvironment() const { return true; }
1508
1509 uint32_t GetDexPc() const { return dex_pc_; }
1510
1511 DECLARE_INSTRUCTION(NullCheck);
1512
1513 private:
1514 const uint32_t dex_pc_;
1515
1516 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
1517};
1518
1519class FieldInfo : public ValueObject {
1520 public:
Nicolas Geoffray39468442014-09-02 15:17:15 +01001521 explicit FieldInfo(MemberOffset field_offset, Primitive::Type field_type)
1522 : field_offset_(field_offset), field_type_(field_type) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001523
1524 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001525 Primitive::Type GetFieldType() const { return field_type_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001526
1527 private:
1528 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01001529 const Primitive::Type field_type_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001530};
1531
1532class HInstanceFieldGet : public HExpression<1> {
1533 public:
1534 HInstanceFieldGet(HInstruction* value,
1535 Primitive::Type field_type,
1536 MemberOffset field_offset)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001537 : HExpression(field_type, SideEffects::DependsOnSomething()),
1538 field_info_(field_offset, field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001539 SetRawInputAt(0, value);
1540 }
1541
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001542 virtual bool CanBeMoved() const { return true; }
1543 virtual bool InstructionDataEquals(HInstruction* other) const {
1544 size_t other_offset = other->AsInstanceFieldGet()->GetFieldOffset().SizeValue();
1545 return other_offset == GetFieldOffset().SizeValue();
1546 }
1547
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001548 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001549 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001550
1551 DECLARE_INSTRUCTION(InstanceFieldGet);
1552
1553 private:
1554 const FieldInfo field_info_;
1555
1556 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
1557};
1558
1559class HInstanceFieldSet : public HTemplateInstruction<2> {
1560 public:
1561 HInstanceFieldSet(HInstruction* object,
1562 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01001563 Primitive::Type field_type,
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001564 MemberOffset field_offset)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001565 : HTemplateInstruction(SideEffects::ChangesSomething()),
1566 field_info_(field_offset, field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001567 SetRawInputAt(0, object);
1568 SetRawInputAt(1, value);
1569 }
1570
1571 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001572 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001573
1574 DECLARE_INSTRUCTION(InstanceFieldSet);
1575
1576 private:
1577 const FieldInfo field_info_;
1578
1579 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
1580};
1581
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001582class HArrayGet : public HExpression<2> {
1583 public:
1584 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001585 : HExpression(type, SideEffects::DependsOnSomething()) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001586 SetRawInputAt(0, array);
1587 SetRawInputAt(1, index);
1588 }
1589
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001590 virtual bool CanBeMoved() const { return true; }
1591 virtual bool InstructionDataEquals(HInstruction* other) const { return true; }
1592
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001593 DECLARE_INSTRUCTION(ArrayGet);
1594
1595 private:
1596 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
1597};
1598
1599class HArraySet : public HTemplateInstruction<3> {
1600 public:
1601 HArraySet(HInstruction* array,
1602 HInstruction* index,
1603 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01001604 Primitive::Type component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001605 uint32_t dex_pc)
1606 : HTemplateInstruction(SideEffects::ChangesSomething()),
1607 dex_pc_(dex_pc),
1608 component_type_(component_type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001609 SetRawInputAt(0, array);
1610 SetRawInputAt(1, index);
1611 SetRawInputAt(2, value);
1612 }
1613
1614 virtual bool NeedsEnvironment() const {
1615 // We currently always call a runtime method to catch array store
1616 // exceptions.
1617 return InputAt(2)->GetType() == Primitive::kPrimNot;
1618 }
1619
1620 uint32_t GetDexPc() const { return dex_pc_; }
1621
Nicolas Geoffray39468442014-09-02 15:17:15 +01001622 Primitive::Type GetComponentType() const { return component_type_; }
1623
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001624 DECLARE_INSTRUCTION(ArraySet);
1625
1626 private:
1627 const uint32_t dex_pc_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01001628 const Primitive::Type component_type_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001629
1630 DISALLOW_COPY_AND_ASSIGN(HArraySet);
1631};
1632
1633class HArrayLength : public HExpression<1> {
1634 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001635 explicit HArrayLength(HInstruction* array)
1636 : HExpression(Primitive::kPrimInt, SideEffects::None()) {
1637 // Note that arrays do not change length, so the instruction does not
1638 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001639 SetRawInputAt(0, array);
1640 }
1641
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001642 virtual bool CanBeMoved() const { return true; }
1643 virtual bool InstructionDataEquals(HInstruction* other) const { return true; }
1644
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001645 DECLARE_INSTRUCTION(ArrayLength);
1646
1647 private:
1648 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
1649};
1650
1651class HBoundsCheck : public HExpression<2> {
1652 public:
1653 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001654 : HExpression(index->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001655 DCHECK(index->GetType() == Primitive::kPrimInt);
1656 SetRawInputAt(0, index);
1657 SetRawInputAt(1, length);
1658 }
1659
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001660 virtual bool CanBeMoved() const { return true; }
1661 virtual bool InstructionDataEquals(HInstruction* other) const { return true; }
1662
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001663 virtual bool NeedsEnvironment() const { return true; }
1664
1665 uint32_t GetDexPc() const { return dex_pc_; }
1666
1667 DECLARE_INSTRUCTION(BoundsCheck);
1668
1669 private:
1670 const uint32_t dex_pc_;
1671
1672 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
1673};
1674
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001675/**
1676 * Some DEX instructions are folded into multiple HInstructions that need
1677 * to stay live until the last HInstruction. This class
1678 * is used as a marker for the baseline compiler to ensure its preceding
1679 * HInstruction stays live. `index` is the temporary number that is used
1680 * for knowing the stack offset where to store the instruction.
1681 */
1682class HTemporary : public HTemplateInstruction<0> {
1683 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001684 explicit HTemporary(size_t index) : HTemplateInstruction(SideEffects::None()), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001685
1686 size_t GetIndex() const { return index_; }
1687
1688 DECLARE_INSTRUCTION(Temporary);
1689
1690 private:
1691 const size_t index_;
1692
1693 DISALLOW_COPY_AND_ASSIGN(HTemporary);
1694};
1695
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001696class HSuspendCheck : public HTemplateInstruction<0> {
1697 public:
1698 explicit HSuspendCheck(uint32_t dex_pc)
1699 : HTemplateInstruction(SideEffects::ChangesSomething()), dex_pc_(dex_pc) {}
1700
1701 virtual bool NeedsEnvironment() const {
1702 return true;
1703 }
1704
1705 uint32_t GetDexPc() const { return dex_pc_; }
1706
1707 DECLARE_INSTRUCTION(SuspendCheck);
1708
1709 private:
1710 const uint32_t dex_pc_;
1711
1712 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
1713};
1714
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001715class MoveOperands : public ArenaObject {
1716 public:
1717 MoveOperands(Location source, Location destination)
1718 : source_(source), destination_(destination) {}
1719
1720 Location GetSource() const { return source_; }
1721 Location GetDestination() const { return destination_; }
1722
1723 void SetSource(Location value) { source_ = value; }
1724 void SetDestination(Location value) { destination_ = value; }
1725
1726 // The parallel move resolver marks moves as "in-progress" by clearing the
1727 // destination (but not the source).
1728 Location MarkPending() {
1729 DCHECK(!IsPending());
1730 Location dest = destination_;
1731 destination_ = Location::NoLocation();
1732 return dest;
1733 }
1734
1735 void ClearPending(Location dest) {
1736 DCHECK(IsPending());
1737 destination_ = dest;
1738 }
1739
1740 bool IsPending() const {
1741 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
1742 return destination_.IsInvalid() && !source_.IsInvalid();
1743 }
1744
1745 // True if this blocks a move from the given location.
1746 bool Blocks(Location loc) const {
1747 return !IsEliminated() && source_.Equals(loc);
1748 }
1749
1750 // A move is redundant if it's been eliminated, if its source and
1751 // destination are the same, or if its destination is unneeded.
1752 bool IsRedundant() const {
1753 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
1754 }
1755
1756 // We clear both operands to indicate move that's been eliminated.
1757 void Eliminate() {
1758 source_ = destination_ = Location::NoLocation();
1759 }
1760
1761 bool IsEliminated() const {
1762 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
1763 return source_.IsInvalid();
1764 }
1765
1766 private:
1767 Location source_;
1768 Location destination_;
1769
1770 DISALLOW_COPY_AND_ASSIGN(MoveOperands);
1771};
1772
1773static constexpr size_t kDefaultNumberOfMoves = 4;
1774
1775class HParallelMove : public HTemplateInstruction<0> {
1776 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001777 explicit HParallelMove(ArenaAllocator* arena)
1778 : HTemplateInstruction(SideEffects::None()), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001779
1780 void AddMove(MoveOperands* move) {
1781 moves_.Add(move);
1782 }
1783
1784 MoveOperands* MoveOperandsAt(size_t index) const {
1785 return moves_.Get(index);
1786 }
1787
1788 size_t NumMoves() const { return moves_.Size(); }
1789
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001790 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001791
1792 private:
1793 GrowableArray<MoveOperands*> moves_;
1794
1795 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
1796};
1797
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001798class HGraphVisitor : public ValueObject {
1799 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001800 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
1801 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001802
Dave Allison20dfc792014-06-16 20:44:29 -07001803 virtual void VisitInstruction(HInstruction* instruction) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001804 virtual void VisitBasicBlock(HBasicBlock* block);
1805
1806 void VisitInsertionOrder();
1807
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001808 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001809
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001810 // Visit functions for instruction classes.
1811#define DECLARE_VISIT_INSTRUCTION(name) \
1812 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
1813
1814 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
1815
1816#undef DECLARE_VISIT_INSTRUCTION
1817
1818 private:
1819 HGraph* graph_;
1820
1821 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
1822};
1823
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001824class HInsertionOrderIterator : public ValueObject {
1825 public:
1826 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
1827
1828 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
1829 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
1830 void Advance() { ++index_; }
1831
1832 private:
1833 const HGraph& graph_;
1834 size_t index_;
1835
1836 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
1837};
1838
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001839class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001840 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001841 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001842
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001843 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
1844 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001845 void Advance() { ++index_; }
1846
1847 private:
1848 const HGraph& graph_;
1849 size_t index_;
1850
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001851 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001852};
1853
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001854class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001855 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001856 explicit HPostOrderIterator(const HGraph& graph)
1857 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001858
1859 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001860 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001861 void Advance() { --index_; }
1862
1863 private:
1864 const HGraph& graph_;
1865 size_t index_;
1866
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001867 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001868};
1869
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001870} // namespace art
1871
1872#endif // ART_COMPILER_OPTIMIZING_NODES_H_