blob: 10d471c049d4c5c88927b45495bae11f5b6a0bdc [file] [log] [blame]
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ART_COMPILER_OPTIMIZING_NODES_H_
18#define ART_COMPILER_OPTIMIZING_NODES_H_
19
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010020#include "locations.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010021#include "offsets.h"
Ian Rogerse63db272014-07-15 15:36:11 -070022#include "primitive.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000023#include "utils/allocation.h"
Nicolas Geoffray0e336432014-02-26 18:24:38 +000024#include "utils/arena_bit_vector.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000025#include "utils/growable_array.h"
26
27namespace art {
28
29class HBasicBlock;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010030class HEnvironment;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000031class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000032class HIntConstant;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000033class HGraphVisitor;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010034class HPhi;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010035class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000036class LocationSummary;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000037
38static const int kDefaultNumberOfBlocks = 8;
39static const int kDefaultNumberOfSuccessors = 2;
40static const int kDefaultNumberOfPredecessors = 2;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000041static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000042
Dave Allison20dfc792014-06-16 20:44:29 -070043enum IfCondition {
44 kCondEQ,
45 kCondNE,
46 kCondLT,
47 kCondLE,
48 kCondGT,
49 kCondGE,
50};
51
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010052class HInstructionList {
53 public:
54 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
55
56 void AddInstruction(HInstruction* instruction);
57 void RemoveInstruction(HInstruction* instruction);
58
Roland Levillainccc07a92014-09-16 14:48:16 +010059 // Return true if `instruction1` is found before `instruction2` in
60 // this instruction list and false otherwise. Abort if none
61 // of these instructions is found.
62 bool FoundBefore(const HInstruction* instruction1,
63 const HInstruction* instruction2) const;
64
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010065 private:
66 HInstruction* first_instruction_;
67 HInstruction* last_instruction_;
68
69 friend class HBasicBlock;
70 friend class HInstructionIterator;
Nicolas Geoffray804d0932014-05-02 08:46:00 +010071 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010072
73 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
74};
75
Nicolas Geoffray818f2102014-02-18 16:43:35 +000076// Control-flow graph of a method. Contains a list of basic blocks.
77class HGraph : public ArenaObject {
78 public:
79 explicit HGraph(ArenaAllocator* arena)
80 : arena_(arena),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000081 blocks_(arena, kDefaultNumberOfBlocks),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +010082 reverse_post_order_(arena, kDefaultNumberOfBlocks),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +010083 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +010084 number_of_vregs_(0),
85 number_of_in_vregs_(0),
Nicolas Geoffraye5038322014-07-04 09:41:32 +010086 number_of_temporaries_(0),
Dave Allison20dfc792014-06-16 20:44:29 -070087 current_instruction_id_(0) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +000088
Nicolas Geoffray787c3072014-03-17 10:20:19 +000089 ArenaAllocator* GetArena() const { return arena_; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +010090 const GrowableArray<HBasicBlock*>& GetBlocks() const { return blocks_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +000091
Nicolas Geoffray787c3072014-03-17 10:20:19 +000092 HBasicBlock* GetEntryBlock() const { return entry_block_; }
93 HBasicBlock* GetExitBlock() const { return exit_block_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000094
Nicolas Geoffray787c3072014-03-17 10:20:19 +000095 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
96 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000097
Nicolas Geoffray818f2102014-02-18 16:43:35 +000098 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010099
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000100 void BuildDominatorTree();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100101 void TransformToSSA();
102 void SimplifyCFG();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000103
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100104 // Find all natural loops in this graph. Aborts computation and returns false
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100105 // if one loop is not natural, that is the header does not dominate the back
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100106 // edge.
107 bool FindNaturalLoops() const;
108
109 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
110 void SimplifyLoop(HBasicBlock* header);
111
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000112 int GetNextInstructionId() {
113 return current_instruction_id_++;
114 }
115
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100116 uint16_t GetMaximumNumberOfOutVRegs() const {
117 return maximum_number_of_out_vregs_;
118 }
119
120 void UpdateMaximumNumberOfOutVRegs(uint16_t new_value) {
121 maximum_number_of_out_vregs_ = std::max(new_value, maximum_number_of_out_vregs_);
122 }
123
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100124 void UpdateNumberOfTemporaries(size_t count) {
125 number_of_temporaries_ = std::max(count, number_of_temporaries_);
126 }
127
128 size_t GetNumberOfTemporaries() const {
129 return number_of_temporaries_;
130 }
131
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100132 void SetNumberOfVRegs(uint16_t number_of_vregs) {
133 number_of_vregs_ = number_of_vregs;
134 }
135
136 uint16_t GetNumberOfVRegs() const {
137 return number_of_vregs_;
138 }
139
140 void SetNumberOfInVRegs(uint16_t value) {
141 number_of_in_vregs_ = value;
142 }
143
144 uint16_t GetNumberOfInVRegs() const {
145 return number_of_in_vregs_;
146 }
147
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100148 uint16_t GetNumberOfLocalVRegs() const {
149 return number_of_vregs_ - number_of_in_vregs_;
150 }
151
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100152 const GrowableArray<HBasicBlock*>& GetReversePostOrder() const {
153 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100154 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100155
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000156 private:
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000157 HBasicBlock* FindCommonDominator(HBasicBlock* first, HBasicBlock* second) const;
158 void VisitBlockForDominatorTree(HBasicBlock* block,
159 HBasicBlock* predecessor,
160 GrowableArray<size_t>* visits);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100161 void FindBackEdges(ArenaBitVector* visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000162 void VisitBlockForBackEdges(HBasicBlock* block,
163 ArenaBitVector* visited,
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100164 ArenaBitVector* visiting);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000165 void RemoveDeadBlocks(const ArenaBitVector& visited) const;
166
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000167 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000168
169 // List of blocks in insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000170 GrowableArray<HBasicBlock*> blocks_;
171
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100172 // List of blocks to perform a reverse post order tree traversal.
173 GrowableArray<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000174
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000175 HBasicBlock* entry_block_;
176 HBasicBlock* exit_block_;
177
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100178 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100179 uint16_t maximum_number_of_out_vregs_;
180
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100181 // The number of virtual registers in this method. Contains the parameters.
182 uint16_t number_of_vregs_;
183
184 // The number of virtual registers used by parameters of this method.
185 uint16_t number_of_in_vregs_;
186
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100187 // The number of temporaries that will be needed for the baseline compiler.
188 size_t number_of_temporaries_;
189
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000190 // The current id to assign to a newly added instruction. See HInstruction.id_.
191 int current_instruction_id_;
192
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000193 DISALLOW_COPY_AND_ASSIGN(HGraph);
194};
195
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000196class HLoopInformation : public ArenaObject {
197 public:
198 HLoopInformation(HBasicBlock* header, HGraph* graph)
199 : header_(header),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100200 back_edges_(graph->GetArena(), kDefaultNumberOfBackEdges),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100201 // Make bit vector growable, as the number of blocks may change.
202 blocks_(graph->GetArena(), graph->GetBlocks().Size(), true) {}
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100203
204 HBasicBlock* GetHeader() const {
205 return header_;
206 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000207
208 void AddBackEdge(HBasicBlock* back_edge) {
209 back_edges_.Add(back_edge);
210 }
211
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100212 void RemoveBackEdge(HBasicBlock* back_edge) {
213 back_edges_.Delete(back_edge);
214 }
215
216 bool IsBackEdge(HBasicBlock* block) {
217 for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) {
218 if (back_edges_.Get(i) == block) return true;
219 }
220 return false;
221 }
222
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000223 int NumberOfBackEdges() const {
224 return back_edges_.Size();
225 }
226
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100227 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100228
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100229 const GrowableArray<HBasicBlock*>& GetBackEdges() const {
230 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100231 }
232
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100233 void ClearBackEdges() {
234 back_edges_.Reset();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100235 }
236
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100237 // Find blocks that are part of this loop. Returns whether the loop is a natural loop,
238 // that is the header dominates the back edge.
239 bool Populate();
240
241 // Returns whether this loop information contains `block`.
242 // Note that this loop information *must* be populated before entering this function.
243 bool Contains(const HBasicBlock& block) const;
244
245 // Returns whether this loop information is an inner loop of `other`.
246 // Note that `other` *must* be populated before entering this function.
247 bool IsIn(const HLoopInformation& other) const;
248
249 const ArenaBitVector& GetBlocks() const { return blocks_; }
250
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000251 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100252 // Internal recursive implementation of `Populate`.
253 void PopulateRecursive(HBasicBlock* block);
254
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000255 HBasicBlock* header_;
256 GrowableArray<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100257 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000258
259 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
260};
261
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100262static constexpr size_t kNoLifetime = -1;
263
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000264// A block in a method. Contains the list of instructions represented
265// as a double linked list. Each block knows its predecessors and
266// successors.
267class HBasicBlock : public ArenaObject {
268 public:
269 explicit HBasicBlock(HGraph* graph)
270 : graph_(graph),
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000271 predecessors_(graph->GetArena(), kDefaultNumberOfPredecessors),
272 successors_(graph->GetArena(), kDefaultNumberOfSuccessors),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000273 loop_information_(nullptr),
274 dominator_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100275 block_id_(-1),
276 lifetime_start_(kNoLifetime),
277 lifetime_end_(kNoLifetime) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000278
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100279 const GrowableArray<HBasicBlock*>& GetPredecessors() const {
280 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000281 }
282
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100283 const GrowableArray<HBasicBlock*>& GetSuccessors() const {
284 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000285 }
286
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000287 void AddBackEdge(HBasicBlock* back_edge) {
288 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000289 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000290 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100291 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000292 loop_information_->AddBackEdge(back_edge);
293 }
294
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000295 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000296
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000297 int GetBlockId() const { return block_id_; }
298 void SetBlockId(int id) { block_id_ = id; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000299
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000300 HBasicBlock* GetDominator() const { return dominator_; }
301 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000302
303 int NumberOfBackEdges() const {
304 return loop_information_ == nullptr
305 ? 0
306 : loop_information_->NumberOfBackEdges();
307 }
308
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100309 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
310 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100311 const HInstructionList& GetInstructions() const { return instructions_; }
312 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100313 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000314
315 void AddSuccessor(HBasicBlock* block) {
316 successors_.Add(block);
317 block->predecessors_.Add(this);
318 }
319
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100320 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
321 size_t successor_index = GetSuccessorIndexOf(existing);
322 DCHECK_NE(successor_index, static_cast<size_t>(-1));
323 existing->RemovePredecessor(this);
324 new_block->predecessors_.Add(this);
325 successors_.Put(successor_index, new_block);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000326 }
327
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100328 void RemovePredecessor(HBasicBlock* block) {
329 predecessors_.Delete(block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100330 }
331
332 void ClearAllPredecessors() {
333 predecessors_.Reset();
334 }
335
336 void AddPredecessor(HBasicBlock* block) {
337 predecessors_.Add(block);
338 block->successors_.Add(this);
339 }
340
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100341 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) {
342 for (size_t i = 0, e = predecessors_.Size(); i < e; ++i) {
343 if (predecessors_.Get(i) == predecessor) {
344 return i;
345 }
346 }
347 return -1;
348 }
349
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100350 size_t GetSuccessorIndexOf(HBasicBlock* successor) {
351 for (size_t i = 0, e = successors_.Size(); i < e; ++i) {
352 if (successors_.Get(i) == successor) {
353 return i;
354 }
355 }
356 return -1;
357 }
358
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000359 void AddInstruction(HInstruction* instruction);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100360 void RemoveInstruction(HInstruction* instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100361 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +0100362 // Replace instruction `initial` with `replacement` within this block.
363 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
364 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100365 void AddPhi(HPhi* phi);
366 void RemovePhi(HPhi* phi);
367
368 bool IsLoopHeader() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100369 return (loop_information_ != nullptr) && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100370 }
371
372 HLoopInformation* GetLoopInformation() const {
373 return loop_information_;
374 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000375
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100376 // Set the loop_information_ on this block. This method overrides the current
377 // loop_information if it is an outer loop of the passed loop information.
378 void SetInLoop(HLoopInformation* info) {
379 if (IsLoopHeader()) {
380 // Nothing to do. This just means `info` is an outer loop.
381 } else if (loop_information_ == nullptr) {
382 loop_information_ = info;
383 } else if (loop_information_->Contains(*info->GetHeader())) {
384 // Block is currently part of an outer loop. Make it part of this inner loop.
385 // Note that a non loop header having a loop information means this loop information
386 // has already been populated
387 loop_information_ = info;
388 } else {
389 // Block is part of an inner loop. Do not update the loop information.
390 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
391 // at this point, because this method is being called while populating `info`.
392 }
393 }
394
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100395 bool IsInLoop() const { return loop_information_ != nullptr; }
396
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100397 // Returns wheter this block dominates the blocked passed as parameter.
398 bool Dominates(HBasicBlock* block) const;
399
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100400 size_t GetLifetimeStart() const { return lifetime_start_; }
401 size_t GetLifetimeEnd() const { return lifetime_end_; }
402
403 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
404 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
405
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000406 private:
407 HGraph* const graph_;
408 GrowableArray<HBasicBlock*> predecessors_;
409 GrowableArray<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100410 HInstructionList instructions_;
411 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000412 HLoopInformation* loop_information_;
413 HBasicBlock* dominator_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000414 int block_id_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100415 size_t lifetime_start_;
416 size_t lifetime_end_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000417
418 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
419};
420
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100421#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000422 M(Add) \
Dave Allison20dfc792014-06-16 20:44:29 -0700423 M(Condition) \
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000424 M(Equal) \
Dave Allison20dfc792014-06-16 20:44:29 -0700425 M(NotEqual) \
426 M(LessThan) \
427 M(LessThanOrEqual) \
428 M(GreaterThan) \
429 M(GreaterThanOrEqual) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000430 M(Exit) \
431 M(Goto) \
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000432 M(If) \
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000433 M(IntConstant) \
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000434 M(InvokeStatic) \
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100435 M(InvokeVirtual) \
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000436 M(LoadLocal) \
437 M(Local) \
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100438 M(LongConstant) \
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +0100439 M(NewInstance) \
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100440 M(Not) \
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100441 M(ParameterValue) \
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100442 M(ParallelMove) \
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100443 M(Phi) \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000444 M(Return) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000445 M(ReturnVoid) \
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000446 M(StoreLocal) \
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100447 M(Sub) \
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100448 M(Compare) \
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100449 M(InstanceFieldGet) \
450 M(InstanceFieldSet) \
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100451 M(ArrayGet) \
452 M(ArraySet) \
453 M(ArrayLength) \
454 M(BoundsCheck) \
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100455 M(NullCheck) \
456 M(Temporary) \
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000457 M(SuspendCheck) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000458
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100459#define FOR_EACH_INSTRUCTION(M) \
460 FOR_EACH_CONCRETE_INSTRUCTION(M) \
Roland Levillainccc07a92014-09-16 14:48:16 +0100461 M(Constant) \
462 M(BinaryOperation)
Dave Allison20dfc792014-06-16 20:44:29 -0700463
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000464#define FORWARD_DECLARATION(type) class H##type;
465FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
466#undef FORWARD_DECLARATION
467
Roland Levillainccc07a92014-09-16 14:48:16 +0100468#define DECLARE_INSTRUCTION(type) \
469 virtual const char* DebugName() const { return #type; } \
470 virtual const H##type* As##type() const OVERRIDE { return this; } \
471 virtual H##type* As##type() OVERRIDE { return this; } \
472 virtual bool InstructionTypeEquals(HInstruction* other) const { \
473 return other->Is##type(); \
474 } \
475 virtual void Accept(HGraphVisitor* visitor)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000476
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100477template <typename T>
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000478class HUseListNode : public ArenaObject {
479 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100480 HUseListNode(T* user, size_t index, HUseListNode* tail)
Dave Allison20dfc792014-06-16 20:44:29 -0700481 : user_(user), index_(index), tail_(tail) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000482
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000483 HUseListNode* GetTail() const { return tail_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100484 T* GetUser() const { return user_; }
485 size_t GetIndex() const { return index_; }
486
487 void SetTail(HUseListNode<T>* node) { tail_ = node; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000488
489 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100490 T* const user_;
491 const size_t index_;
492 HUseListNode<T>* tail_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000493
494 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
495};
496
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100497// Represents the side effects an instruction may have.
498class SideEffects : public ValueObject {
499 public:
500 static SideEffects None() {
501 return SideEffects(0);
502 }
503
504 static SideEffects All() {
505 return SideEffects(ChangesSomething().flags_ | DependsOnSomething().flags_);
506 }
507
508 static SideEffects ChangesSomething() {
509 return SideEffects((1 << kFlagChangesCount) - 1);
510 }
511
512 static SideEffects DependsOnSomething() {
513 int count = kFlagDependsOnCount - kFlagChangesCount;
514 return SideEffects(((1 << count) - 1) << kFlagChangesCount);
515 }
516
Roland Levillain72bceff2014-09-15 18:29:00 +0100517 bool HasSideEffects() const {
518 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
519 return (flags_ & all_bits_set) != 0;
520 }
521
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100522 private:
523 static constexpr int kFlagChangesSomething = 0;
524 static constexpr int kFlagChangesCount = kFlagChangesSomething + 1;
525
526 static constexpr int kFlagDependsOnSomething = kFlagChangesCount;
527 static constexpr int kFlagDependsOnCount = kFlagDependsOnSomething + 1;
528
529 private:
530 explicit SideEffects(size_t flags) : flags_(flags) {}
531
532 const size_t flags_;
533};
534
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000535class HInstruction : public ArenaObject {
536 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100537 explicit HInstruction(SideEffects side_effects)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000538 : previous_(nullptr),
539 next_(nullptr),
540 block_(nullptr),
541 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100542 ssa_index_(-1),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000543 uses_(nullptr),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100544 env_uses_(nullptr),
545 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100546 locations_(nullptr),
547 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100548 lifetime_position_(kNoLifetime),
549 side_effects_(side_effects) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000550
Dave Allison20dfc792014-06-16 20:44:29 -0700551 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000552
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000553 HInstruction* GetNext() const { return next_; }
554 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000555
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000556 HBasicBlock* GetBlock() const { return block_; }
557 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100558 bool IsInBlock() const { return block_ != nullptr; }
559 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +0100560 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000561
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100562 virtual size_t InputCount() const = 0;
563 virtual HInstruction* InputAt(size_t i) const = 0;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000564
565 virtual void Accept(HGraphVisitor* visitor) = 0;
566 virtual const char* DebugName() const = 0;
567
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100568 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100569 virtual void SetRawInputAt(size_t index, HInstruction* input) = 0;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100570
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100571 virtual bool NeedsEnvironment() const { return false; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100572 virtual bool IsControlFlow() const { return false; }
Roland Levillain72bceff2014-09-15 18:29:00 +0100573 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100574
575 void AddUseAt(HInstruction* user, size_t index) {
576 uses_ = new (block_->GetGraph()->GetArena()) HUseListNode<HInstruction>(user, index, uses_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000577 }
578
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100579 void AddEnvUseAt(HEnvironment* user, size_t index) {
580 env_uses_ = new (block_->GetGraph()->GetArena()) HUseListNode<HEnvironment>(
581 user, index, env_uses_);
582 }
583
584 void RemoveUser(HInstruction* user, size_t index);
585
586 HUseListNode<HInstruction>* GetUses() const { return uses_; }
587 HUseListNode<HEnvironment>* GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000588
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100589 bool HasUses() const { return uses_ != nullptr || env_uses_ != nullptr; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100590 bool HasEnvironmentUses() const { return env_uses_ != nullptr; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000591
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100592 size_t NumberOfUses() const {
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100593 // TODO: Optimize this method if it is used outside of the HGraphVisualizer.
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100594 size_t result = 0;
595 HUseListNode<HInstruction>* current = uses_;
596 while (current != nullptr) {
597 current = current->GetTail();
598 ++result;
599 }
600 return result;
601 }
602
Roland Levillainccc07a92014-09-16 14:48:16 +0100603 // Does this instruction dominate `other_instruction`? Aborts if
604 // this instruction and `other_instruction` are both phis.
605 bool Dominates(HInstruction* other_instruction) const;
606
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000607 int GetId() const { return id_; }
608 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000609
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100610 int GetSsaIndex() const { return ssa_index_; }
611 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
612 bool HasSsaIndex() const { return ssa_index_ != -1; }
613
614 bool HasEnvironment() const { return environment_ != nullptr; }
615 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100616 void SetEnvironment(HEnvironment* environment) { environment_ = environment; }
617
Nicolas Geoffray39468442014-09-02 15:17:15 +0100618 // Returns the number of entries in the environment. Typically, that is the
619 // number of dex registers in a method. It could be more in case of inlining.
620 size_t EnvironmentSize() const;
621
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000622 LocationSummary* GetLocations() const { return locations_; }
623 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000624
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100625 void ReplaceWith(HInstruction* instruction);
626
Dave Allison20dfc792014-06-16 20:44:29 -0700627 bool HasOnlyOneUse() const {
628 return uses_ != nullptr && uses_->GetTail() == nullptr;
629 }
630
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000631#define INSTRUCTION_TYPE_CHECK(type) \
Roland Levillainccc07a92014-09-16 14:48:16 +0100632 bool Is##type() const { return (As##type() != nullptr); } \
633 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000634 virtual H##type* As##type() { return nullptr; }
635
636 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
637#undef INSTRUCTION_TYPE_CHECK
638
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100639 // Returns whether the instruction can be moved within the graph.
640 virtual bool CanBeMoved() const { return false; }
641
642 // Returns whether the two instructions are of the same kind.
643 virtual bool InstructionTypeEquals(HInstruction* other) const { return false; }
644
645 // Returns whether any data encoded in the two instructions is equal.
646 // This method does not look at the inputs. Both instructions must be
647 // of the same type, otherwise the method has undefined behavior.
648 virtual bool InstructionDataEquals(HInstruction* other) const { return false; }
649
650 // Returns whether two instructions are equal, that is:
651 // 1) They have the same type and contain the same data,
652 // 2) Their inputs are identical.
653 bool Equals(HInstruction* other) const;
654
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100655 size_t GetLifetimePosition() const { return lifetime_position_; }
656 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
657 LiveInterval* GetLiveInterval() const { return live_interval_; }
658 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
659 bool HasLiveInterval() const { return live_interval_ != nullptr; }
660
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000661 private:
662 HInstruction* previous_;
663 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000664 HBasicBlock* block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000665
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000666 // An instruction gets an id when it is added to the graph.
667 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +0100668 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000669 int id_;
670
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100671 // When doing liveness analysis, instructions that have uses get an SSA index.
672 int ssa_index_;
673
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100674 // List of instructions that have this instruction as input.
675 HUseListNode<HInstruction>* uses_;
676
677 // List of environments that contain this instruction.
678 HUseListNode<HEnvironment>* env_uses_;
679
Nicolas Geoffray39468442014-09-02 15:17:15 +0100680 // The environment associated with this instruction. Not null if the instruction
681 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100682 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000683
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000684 // Set by the code generator.
685 LocationSummary* locations_;
686
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100687 // Set by the liveness analysis.
688 LiveInterval* live_interval_;
689
690 // Set by the liveness analysis, this is the position in a linear
691 // order of blocks where this instruction's live interval start.
692 size_t lifetime_position_;
693
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100694 const SideEffects side_effects_;
695
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000696 friend class HBasicBlock;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100697 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000698
699 DISALLOW_COPY_AND_ASSIGN(HInstruction);
700};
701
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100702template<typename T>
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000703class HUseIterator : public ValueObject {
704 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100705 explicit HUseIterator(HUseListNode<T>* uses) : current_(uses) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000706
707 bool Done() const { return current_ == nullptr; }
708
709 void Advance() {
710 DCHECK(!Done());
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000711 current_ = current_->GetTail();
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000712 }
713
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100714 HUseListNode<T>* Current() const {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000715 DCHECK(!Done());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100716 return current_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000717 }
718
719 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100720 HUseListNode<T>* current_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000721
722 friend class HValue;
723};
724
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100725// A HEnvironment object contains the values of virtual registers at a given location.
726class HEnvironment : public ArenaObject {
727 public:
728 HEnvironment(ArenaAllocator* arena, size_t number_of_vregs) : vregs_(arena, number_of_vregs) {
729 vregs_.SetSize(number_of_vregs);
730 for (size_t i = 0; i < number_of_vregs; i++) {
731 vregs_.Put(i, nullptr);
732 }
733 }
734
735 void Populate(const GrowableArray<HInstruction*>& env) {
736 for (size_t i = 0; i < env.Size(); i++) {
737 HInstruction* instruction = env.Get(i);
738 vregs_.Put(i, instruction);
739 if (instruction != nullptr) {
740 instruction->AddEnvUseAt(this, i);
741 }
742 }
743 }
744
745 void SetRawEnvAt(size_t index, HInstruction* instruction) {
746 vregs_.Put(index, instruction);
747 }
748
Nicolas Geoffray39468442014-09-02 15:17:15 +0100749 HInstruction* GetInstructionAt(size_t index) const {
750 return vregs_.Get(index);
751 }
752
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100753 GrowableArray<HInstruction*>* GetVRegs() {
754 return &vregs_;
755 }
756
Nicolas Geoffray39468442014-09-02 15:17:15 +0100757 size_t Size() const { return vregs_.Size(); }
758
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100759 private:
760 GrowableArray<HInstruction*> vregs_;
761
762 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
763};
764
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000765class HInputIterator : public ValueObject {
766 public:
Dave Allison20dfc792014-06-16 20:44:29 -0700767 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000768
769 bool Done() const { return index_ == instruction_->InputCount(); }
770 HInstruction* Current() const { return instruction_->InputAt(index_); }
771 void Advance() { index_++; }
772
773 private:
774 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100775 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000776
777 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
778};
779
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000780class HInstructionIterator : public ValueObject {
781 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100782 explicit HInstructionIterator(const HInstructionList& instructions)
783 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000784 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000785 }
786
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000787 bool Done() const { return instruction_ == nullptr; }
788 HInstruction* Current() const { return instruction_; }
789 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000790 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000791 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000792 }
793
794 private:
795 HInstruction* instruction_;
796 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100797
798 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000799};
800
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100801class HBackwardInstructionIterator : public ValueObject {
802 public:
803 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
804 : instruction_(instructions.last_instruction_) {
805 next_ = Done() ? nullptr : instruction_->GetPrevious();
806 }
807
808 bool Done() const { return instruction_ == nullptr; }
809 HInstruction* Current() const { return instruction_; }
810 void Advance() {
811 instruction_ = next_;
812 next_ = Done() ? nullptr : instruction_->GetPrevious();
813 }
814
815 private:
816 HInstruction* instruction_;
817 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100818
819 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100820};
821
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000822// An embedded container with N elements of type T. Used (with partial
823// specialization for N=0) because embedded arrays cannot have size 0.
824template<typename T, intptr_t N>
825class EmbeddedArray {
826 public:
Dave Allison20dfc792014-06-16 20:44:29 -0700827 EmbeddedArray() : elements_() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000828
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000829 intptr_t GetLength() const { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000830
831 const T& operator[](intptr_t i) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000832 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000833 return elements_[i];
834 }
835
836 T& operator[](intptr_t i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000837 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000838 return elements_[i];
839 }
840
841 const T& At(intptr_t i) const {
842 return (*this)[i];
843 }
844
845 void SetAt(intptr_t i, const T& val) {
846 (*this)[i] = val;
847 }
848
849 private:
850 T elements_[N];
851};
852
853template<typename T>
854class EmbeddedArray<T, 0> {
855 public:
856 intptr_t length() const { return 0; }
857 const T& operator[](intptr_t i) const {
858 LOG(FATAL) << "Unreachable";
859 static T sentinel = 0;
860 return sentinel;
861 }
862 T& operator[](intptr_t i) {
863 LOG(FATAL) << "Unreachable";
864 static T sentinel = 0;
865 return sentinel;
866 }
867};
868
869template<intptr_t N>
870class HTemplateInstruction: public HInstruction {
871 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100872 HTemplateInstruction<N>(SideEffects side_effects)
873 : HInstruction(side_effects), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -0700874 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000875
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100876 virtual size_t InputCount() const { return N; }
877 virtual HInstruction* InputAt(size_t i) const { return inputs_[i]; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000878
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000879 protected:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100880 virtual void SetRawInputAt(size_t i, HInstruction* instruction) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000881 inputs_[i] = instruction;
882 }
883
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000884 private:
885 EmbeddedArray<HInstruction*, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100886
887 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000888};
889
Dave Allison20dfc792014-06-16 20:44:29 -0700890template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100891class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -0700892 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100893 HExpression<N>(Primitive::Type type, SideEffects side_effects)
894 : HTemplateInstruction<N>(side_effects), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -0700895 virtual ~HExpression() {}
896
897 virtual Primitive::Type GetType() const { return type_; }
898
899 private:
900 const Primitive::Type type_;
901};
902
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000903// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
904// instruction that branches to the exit block.
905class HReturnVoid : public HTemplateInstruction<0> {
906 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100907 HReturnVoid() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100908
909 virtual bool IsControlFlow() const { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000910
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100911 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000912
913 private:
914 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
915};
916
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000917// Represents dex's RETURN opcodes. A HReturn is a control flow
918// instruction that branches to the exit block.
919class HReturn : public HTemplateInstruction<1> {
920 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100921 explicit HReturn(HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000922 SetRawInputAt(0, value);
923 }
924
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100925 virtual bool IsControlFlow() const { return true; }
926
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100927 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000928
929 private:
930 DISALLOW_COPY_AND_ASSIGN(HReturn);
931};
932
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000933// The exit instruction is the only instruction of the exit block.
934// Instructions aborting the method (HTrow and HReturn) must branch to the
935// exit block.
936class HExit : public HTemplateInstruction<0> {
937 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100938 HExit() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100939
940 virtual bool IsControlFlow() const { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000941
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100942 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000943
944 private:
945 DISALLOW_COPY_AND_ASSIGN(HExit);
946};
947
948// Jumps from one block to another.
949class HGoto : public HTemplateInstruction<0> {
950 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100951 HGoto() : HTemplateInstruction(SideEffects::None()) {}
952
953 virtual bool IsControlFlow() const { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000954
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000955 HBasicBlock* GetSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100956 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000957 }
958
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100959 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000960
961 private:
962 DISALLOW_COPY_AND_ASSIGN(HGoto);
963};
964
Dave Allison20dfc792014-06-16 20:44:29 -0700965
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000966// Conditional branch. A block ending with an HIf instruction must have
967// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000968class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000969 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100970 explicit HIf(HInstruction* input) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000971 SetRawInputAt(0, input);
972 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000973
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100974 virtual bool IsControlFlow() const { return true; }
975
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000976 HBasicBlock* IfTrueSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100977 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000978 }
979
980 HBasicBlock* IfFalseSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100981 return GetBlock()->GetSuccessors().Get(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000982 }
983
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100984 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000985
Dave Allison20dfc792014-06-16 20:44:29 -0700986 virtual bool IsIfInstruction() const { return true; }
987
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000988 private:
989 DISALLOW_COPY_AND_ASSIGN(HIf);
990};
991
Dave Allison20dfc792014-06-16 20:44:29 -0700992class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000993 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000994 HBinaryOperation(Primitive::Type result_type,
995 HInstruction* left,
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100996 HInstruction* right) : HExpression(result_type, SideEffects::None()) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000997 SetRawInputAt(0, left);
998 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000999 }
1000
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001001 HInstruction* GetLeft() const { return InputAt(0); }
1002 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07001003 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001004
1005 virtual bool IsCommutative() { return false; }
1006
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001007 virtual bool CanBeMoved() const { return true; }
1008 virtual bool InstructionDataEquals(HInstruction* other) const { return true; }
1009
Roland Levillain556c3d12014-09-18 15:25:07 +01001010 // Try to statically evaluate `operation` and return an HConstant
1011 // containing the result of this evaluation. If `operation` cannot
1012 // be evaluated as a constant, return nullptr.
1013 HConstant* TryStaticEvaluation(ArenaAllocator* allocator) const;
1014
1015 // Apply this operation to `x` and `y`.
1016 virtual int32_t Evaluate(int32_t x, int32_t y) const = 0;
1017 virtual int64_t Evaluate(int64_t x, int64_t y) const = 0;
1018
Roland Levillainccc07a92014-09-16 14:48:16 +01001019 DECLARE_INSTRUCTION(BinaryOperation);
1020
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001021 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001022 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
1023};
1024
Dave Allison20dfc792014-06-16 20:44:29 -07001025class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001026 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001027 HCondition(HInstruction* first, HInstruction* second)
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001028 : HBinaryOperation(Primitive::kPrimBoolean, first, second) {}
1029
1030 virtual bool IsCommutative() { return true; }
Dave Allison20dfc792014-06-16 20:44:29 -07001031 bool NeedsMaterialization() const;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001032
Dave Allison20dfc792014-06-16 20:44:29 -07001033 DECLARE_INSTRUCTION(Condition);
1034
1035 virtual IfCondition GetCondition() const = 0;
1036
1037 private:
1038 DISALLOW_COPY_AND_ASSIGN(HCondition);
1039};
1040
1041// Instruction to check if two inputs are equal to each other.
1042class HEqual : public HCondition {
1043 public:
1044 HEqual(HInstruction* first, HInstruction* second)
1045 : HCondition(first, second) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001046
Roland Levillain556c3d12014-09-18 15:25:07 +01001047 virtual int32_t Evaluate(int32_t x, int32_t y) const { return x == y; }
1048 virtual int64_t Evaluate(int64_t x, int64_t y) const { return x == y; }
1049
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001050 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001051
Dave Allison20dfc792014-06-16 20:44:29 -07001052 virtual IfCondition GetCondition() const {
1053 return kCondEQ;
1054 }
1055
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001056 private:
1057 DISALLOW_COPY_AND_ASSIGN(HEqual);
1058};
1059
Dave Allison20dfc792014-06-16 20:44:29 -07001060class HNotEqual : public HCondition {
1061 public:
1062 HNotEqual(HInstruction* first, HInstruction* second)
1063 : HCondition(first, second) {}
1064
Roland Levillain556c3d12014-09-18 15:25:07 +01001065 virtual int32_t Evaluate(int32_t x, int32_t y) const { return x != y; }
1066 virtual int64_t Evaluate(int64_t x, int64_t y) const { return x != y; }
1067
Dave Allison20dfc792014-06-16 20:44:29 -07001068 DECLARE_INSTRUCTION(NotEqual);
1069
1070 virtual IfCondition GetCondition() const {
1071 return kCondNE;
1072 }
1073
1074 private:
1075 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
1076};
1077
1078class HLessThan : public HCondition {
1079 public:
1080 HLessThan(HInstruction* first, HInstruction* second)
1081 : HCondition(first, second) {}
1082
Roland Levillain556c3d12014-09-18 15:25:07 +01001083 virtual int32_t Evaluate(int32_t x, int32_t y) const { return x < y; }
1084 virtual int64_t Evaluate(int64_t x, int64_t y) const { return x < y; }
1085
Dave Allison20dfc792014-06-16 20:44:29 -07001086 DECLARE_INSTRUCTION(LessThan);
1087
1088 virtual IfCondition GetCondition() const {
1089 return kCondLT;
1090 }
1091
1092 private:
1093 DISALLOW_COPY_AND_ASSIGN(HLessThan);
1094};
1095
1096class HLessThanOrEqual : public HCondition {
1097 public:
1098 HLessThanOrEqual(HInstruction* first, HInstruction* second)
1099 : HCondition(first, second) {}
1100
Roland Levillain556c3d12014-09-18 15:25:07 +01001101 virtual int32_t Evaluate(int32_t x, int32_t y) const { return x <= y; }
1102 virtual int64_t Evaluate(int64_t x, int64_t y) const { return x <= y; }
1103
Dave Allison20dfc792014-06-16 20:44:29 -07001104 DECLARE_INSTRUCTION(LessThanOrEqual);
1105
1106 virtual IfCondition GetCondition() const {
1107 return kCondLE;
1108 }
1109
1110 private:
1111 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
1112};
1113
1114class HGreaterThan : public HCondition {
1115 public:
1116 HGreaterThan(HInstruction* first, HInstruction* second)
1117 : HCondition(first, second) {}
1118
Roland Levillain556c3d12014-09-18 15:25:07 +01001119 virtual int32_t Evaluate(int32_t x, int32_t y) const { return x > y; }
1120 virtual int64_t Evaluate(int64_t x, int64_t y) const { return x > y; }
1121
Dave Allison20dfc792014-06-16 20:44:29 -07001122 DECLARE_INSTRUCTION(GreaterThan);
1123
1124 virtual IfCondition GetCondition() const {
1125 return kCondGT;
1126 }
1127
1128 private:
1129 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
1130};
1131
1132class HGreaterThanOrEqual : public HCondition {
1133 public:
1134 HGreaterThanOrEqual(HInstruction* first, HInstruction* second)
1135 : HCondition(first, second) {}
1136
Roland Levillain556c3d12014-09-18 15:25:07 +01001137 virtual int32_t Evaluate(int32_t x, int32_t y) const { return x >= y; }
1138 virtual int64_t Evaluate(int64_t x, int64_t y) const { return x >= y; }
1139
Dave Allison20dfc792014-06-16 20:44:29 -07001140 DECLARE_INSTRUCTION(GreaterThanOrEqual);
1141
1142 virtual IfCondition GetCondition() const {
1143 return kCondGE;
1144 }
1145
1146 private:
1147 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
1148};
1149
1150
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001151// Instruction to check how two inputs compare to each other.
1152// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
1153class HCompare : public HBinaryOperation {
1154 public:
1155 HCompare(Primitive::Type type, HInstruction* first, HInstruction* second)
1156 : HBinaryOperation(Primitive::kPrimInt, first, second) {
1157 DCHECK_EQ(type, first->GetType());
1158 DCHECK_EQ(type, second->GetType());
1159 }
1160
Roland Levillain556c3d12014-09-18 15:25:07 +01001161 virtual int32_t Evaluate(int32_t x, int32_t y) const {
1162 return
1163 x == y ? 0 :
1164 x > y ? 1 :
1165 -1;
1166 }
1167 virtual int64_t Evaluate(int64_t x, int64_t y) const {
1168 return
1169 x == y ? 0 :
1170 x > y ? 1 :
1171 -1;
1172 }
1173
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001174 DECLARE_INSTRUCTION(Compare);
1175
1176 private:
1177 DISALLOW_COPY_AND_ASSIGN(HCompare);
1178};
1179
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001180// A local in the graph. Corresponds to a Dex register.
1181class HLocal : public HTemplateInstruction<0> {
1182 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001183 explicit HLocal(uint16_t reg_number)
1184 : HTemplateInstruction(SideEffects::None()), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001185
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001186 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001187
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001188 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001189
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001190 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001191 // The Dex register number.
1192 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001193
1194 DISALLOW_COPY_AND_ASSIGN(HLocal);
1195};
1196
1197// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07001198class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001199 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001200 explicit HLoadLocal(HLocal* local, Primitive::Type type)
1201 : HExpression(type, SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001202 SetRawInputAt(0, local);
1203 }
1204
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001205 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1206
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001207 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001208
1209 private:
1210 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
1211};
1212
1213// Store a value in a given local. This instruction has two inputs: the value
1214// and the local.
1215class HStoreLocal : public HTemplateInstruction<2> {
1216 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001217 HStoreLocal(HLocal* local, HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001218 SetRawInputAt(0, local);
1219 SetRawInputAt(1, value);
1220 }
1221
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001222 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1223
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001224 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001225
1226 private:
1227 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
1228};
1229
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001230class HConstant : public HExpression<0> {
1231 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001232 explicit HConstant(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
1233
1234 virtual bool CanBeMoved() const { return true; }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001235
1236 DECLARE_INSTRUCTION(Constant);
1237
1238 private:
1239 DISALLOW_COPY_AND_ASSIGN(HConstant);
1240};
1241
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001242// Constants of the type int. Those can be from Dex instructions, or
1243// synthesized (for example with the if-eqz instruction).
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001244class HIntConstant : public HConstant {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001245 public:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001246 explicit HIntConstant(int32_t value) : HConstant(Primitive::kPrimInt), value_(value) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001247
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001248 int32_t GetValue() const { return value_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001249
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001250 virtual bool InstructionDataEquals(HInstruction* other) const {
1251 return other->AsIntConstant()->value_ == value_;
1252 }
1253
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001254 DECLARE_INSTRUCTION(IntConstant);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001255
1256 private:
1257 const int32_t value_;
1258
1259 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
1260};
1261
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001262class HLongConstant : public HConstant {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001263 public:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001264 explicit HLongConstant(int64_t value) : HConstant(Primitive::kPrimLong), value_(value) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001265
1266 int64_t GetValue() const { return value_; }
1267
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001268 virtual bool InstructionDataEquals(HInstruction* other) const {
1269 return other->AsLongConstant()->value_ == value_;
1270 }
1271
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001272 DECLARE_INSTRUCTION(LongConstant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001273
1274 private:
1275 const int64_t value_;
1276
1277 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
1278};
1279
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001280class HInvoke : public HInstruction {
1281 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001282 HInvoke(ArenaAllocator* arena,
1283 uint32_t number_of_arguments,
1284 Primitive::Type return_type,
1285 uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001286 : HInstruction(SideEffects::All()),
1287 inputs_(arena, number_of_arguments),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001288 return_type_(return_type),
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001289 dex_pc_(dex_pc) {
1290 inputs_.SetSize(number_of_arguments);
1291 }
1292
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001293 virtual size_t InputCount() const { return inputs_.Size(); }
1294 virtual HInstruction* InputAt(size_t i) const { return inputs_.Get(i); }
1295
1296 // Runtime needs to walk the stack, so Dex -> Dex calls need to
1297 // know their environment.
1298 virtual bool NeedsEnvironment() const { return true; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001299
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001300 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001301 SetRawInputAt(index, argument);
1302 }
1303
1304 virtual void SetRawInputAt(size_t index, HInstruction* input) {
1305 inputs_.Put(index, input);
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001306 }
1307
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001308 virtual Primitive::Type GetType() const { return return_type_; }
1309
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001310 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001311
1312 protected:
1313 GrowableArray<HInstruction*> inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001314 const Primitive::Type return_type_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001315 const uint32_t dex_pc_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001316
1317 private:
1318 DISALLOW_COPY_AND_ASSIGN(HInvoke);
1319};
1320
1321class HInvokeStatic : public HInvoke {
1322 public:
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001323 HInvokeStatic(ArenaAllocator* arena,
1324 uint32_t number_of_arguments,
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001325 Primitive::Type return_type,
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001326 uint32_t dex_pc,
1327 uint32_t index_in_dex_cache)
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001328 : HInvoke(arena, number_of_arguments, return_type, dex_pc),
1329 index_in_dex_cache_(index_in_dex_cache) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001330
1331 uint32_t GetIndexInDexCache() const { return index_in_dex_cache_; }
1332
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001333 DECLARE_INSTRUCTION(InvokeStatic);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001334
1335 private:
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001336 const uint32_t index_in_dex_cache_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001337
1338 DISALLOW_COPY_AND_ASSIGN(HInvokeStatic);
1339};
1340
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001341class HInvokeVirtual : public HInvoke {
1342 public:
1343 HInvokeVirtual(ArenaAllocator* arena,
1344 uint32_t number_of_arguments,
1345 Primitive::Type return_type,
1346 uint32_t dex_pc,
1347 uint32_t vtable_index)
1348 : HInvoke(arena, number_of_arguments, return_type, dex_pc),
1349 vtable_index_(vtable_index) {}
1350
1351 uint32_t GetVTableIndex() const { return vtable_index_; }
1352
1353 DECLARE_INSTRUCTION(InvokeVirtual);
1354
1355 private:
1356 const uint32_t vtable_index_;
1357
1358 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
1359};
1360
Dave Allison20dfc792014-06-16 20:44:29 -07001361class HNewInstance : public HExpression<0> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001362 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001363 HNewInstance(uint32_t dex_pc, uint16_t type_index)
1364 : HExpression(Primitive::kPrimNot, SideEffects::None()),
1365 dex_pc_(dex_pc),
1366 type_index_(type_index) {}
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001367
1368 uint32_t GetDexPc() const { return dex_pc_; }
1369 uint16_t GetTypeIndex() const { return type_index_; }
1370
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001371 // Calls runtime so needs an environment.
1372 virtual bool NeedsEnvironment() const { return true; }
1373
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001374 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001375
1376 private:
1377 const uint32_t dex_pc_;
1378 const uint16_t type_index_;
1379
1380 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
1381};
1382
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001383class HAdd : public HBinaryOperation {
1384 public:
1385 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1386 : HBinaryOperation(result_type, left, right) {}
1387
1388 virtual bool IsCommutative() { return true; }
1389
Roland Levillain556c3d12014-09-18 15:25:07 +01001390 virtual int32_t Evaluate(int32_t x, int32_t y) const { return x + y; }
1391 virtual int64_t Evaluate(int64_t x, int64_t y) const { return x + y; }
1392
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001393 DECLARE_INSTRUCTION(Add);
1394
1395 private:
1396 DISALLOW_COPY_AND_ASSIGN(HAdd);
1397};
1398
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001399class HSub : public HBinaryOperation {
1400 public:
1401 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1402 : HBinaryOperation(result_type, left, right) {}
1403
1404 virtual bool IsCommutative() { return false; }
1405
Roland Levillain556c3d12014-09-18 15:25:07 +01001406 virtual int32_t Evaluate(int32_t x, int32_t y) const { return x + y; }
1407 virtual int64_t Evaluate(int64_t x, int64_t y) const { return x + y; }
1408
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001409 DECLARE_INSTRUCTION(Sub);
1410
1411 private:
1412 DISALLOW_COPY_AND_ASSIGN(HSub);
1413};
1414
1415// The value of a parameter in this method. Its location depends on
1416// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07001417class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001418 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001419 HParameterValue(uint8_t index, Primitive::Type parameter_type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001420 : HExpression(parameter_type, SideEffects::None()), index_(index) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001421
1422 uint8_t GetIndex() const { return index_; }
1423
1424 DECLARE_INSTRUCTION(ParameterValue);
1425
1426 private:
1427 // The index of this parameter in the parameters list. Must be less
1428 // than HGraph::number_of_in_vregs_;
1429 const uint8_t index_;
1430
1431 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
1432};
1433
Dave Allison20dfc792014-06-16 20:44:29 -07001434class HNot : public HExpression<1> {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001435 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001436 explicit HNot(HInstruction* input) : HExpression(Primitive::kPrimBoolean, SideEffects::None()) {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001437 SetRawInputAt(0, input);
1438 }
1439
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001440 virtual bool CanBeMoved() const { return true; }
1441 virtual bool InstructionDataEquals(HInstruction* other) const { return true; }
1442
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001443 DECLARE_INSTRUCTION(Not);
1444
1445 private:
1446 DISALLOW_COPY_AND_ASSIGN(HNot);
1447};
1448
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001449class HPhi : public HInstruction {
1450 public:
1451 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001452 : HInstruction(SideEffects::None()),
1453 inputs_(arena, number_of_inputs),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001454 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001455 type_(type),
1456 is_live_(false) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001457 inputs_.SetSize(number_of_inputs);
1458 }
1459
1460 virtual size_t InputCount() const { return inputs_.Size(); }
1461 virtual HInstruction* InputAt(size_t i) const { return inputs_.Get(i); }
1462
1463 virtual void SetRawInputAt(size_t index, HInstruction* input) {
1464 inputs_.Put(index, input);
1465 }
1466
1467 void AddInput(HInstruction* input);
1468
1469 virtual Primitive::Type GetType() const { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001470 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001471
1472 uint32_t GetRegNumber() const { return reg_number_; }
1473
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001474 void SetDead() { is_live_ = false; }
1475 void SetLive() { is_live_ = true; }
1476 bool IsDead() const { return !is_live_; }
1477 bool IsLive() const { return is_live_; }
1478
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001479 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001480
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001481 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001482 GrowableArray<HInstruction*> inputs_;
1483 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001484 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001485 bool is_live_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001486
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001487 DISALLOW_COPY_AND_ASSIGN(HPhi);
1488};
1489
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001490class HNullCheck : public HExpression<1> {
1491 public:
1492 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001493 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001494 SetRawInputAt(0, value);
1495 }
1496
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001497 virtual bool CanBeMoved() const { return true; }
1498 virtual bool InstructionDataEquals(HInstruction* other) const { return true; }
1499
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001500 virtual bool NeedsEnvironment() const { return true; }
1501
1502 uint32_t GetDexPc() const { return dex_pc_; }
1503
1504 DECLARE_INSTRUCTION(NullCheck);
1505
1506 private:
1507 const uint32_t dex_pc_;
1508
1509 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
1510};
1511
1512class FieldInfo : public ValueObject {
1513 public:
Nicolas Geoffray39468442014-09-02 15:17:15 +01001514 explicit FieldInfo(MemberOffset field_offset, Primitive::Type field_type)
1515 : field_offset_(field_offset), field_type_(field_type) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001516
1517 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001518 Primitive::Type GetFieldType() const { return field_type_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001519
1520 private:
1521 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01001522 const Primitive::Type field_type_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001523};
1524
1525class HInstanceFieldGet : public HExpression<1> {
1526 public:
1527 HInstanceFieldGet(HInstruction* value,
1528 Primitive::Type field_type,
1529 MemberOffset field_offset)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001530 : HExpression(field_type, SideEffects::DependsOnSomething()),
1531 field_info_(field_offset, field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001532 SetRawInputAt(0, value);
1533 }
1534
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001535 virtual bool CanBeMoved() const { return true; }
1536 virtual bool InstructionDataEquals(HInstruction* other) const {
1537 size_t other_offset = other->AsInstanceFieldGet()->GetFieldOffset().SizeValue();
1538 return other_offset == GetFieldOffset().SizeValue();
1539 }
1540
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001541 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001542 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001543
1544 DECLARE_INSTRUCTION(InstanceFieldGet);
1545
1546 private:
1547 const FieldInfo field_info_;
1548
1549 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
1550};
1551
1552class HInstanceFieldSet : public HTemplateInstruction<2> {
1553 public:
1554 HInstanceFieldSet(HInstruction* object,
1555 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01001556 Primitive::Type field_type,
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001557 MemberOffset field_offset)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001558 : HTemplateInstruction(SideEffects::ChangesSomething()),
1559 field_info_(field_offset, field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001560 SetRawInputAt(0, object);
1561 SetRawInputAt(1, value);
1562 }
1563
1564 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001565 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001566
1567 DECLARE_INSTRUCTION(InstanceFieldSet);
1568
1569 private:
1570 const FieldInfo field_info_;
1571
1572 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
1573};
1574
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001575class HArrayGet : public HExpression<2> {
1576 public:
1577 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001578 : HExpression(type, SideEffects::DependsOnSomething()) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001579 SetRawInputAt(0, array);
1580 SetRawInputAt(1, index);
1581 }
1582
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001583 virtual bool CanBeMoved() const { return true; }
1584 virtual bool InstructionDataEquals(HInstruction* other) const { return true; }
1585
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001586 DECLARE_INSTRUCTION(ArrayGet);
1587
1588 private:
1589 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
1590};
1591
1592class HArraySet : public HTemplateInstruction<3> {
1593 public:
1594 HArraySet(HInstruction* array,
1595 HInstruction* index,
1596 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01001597 Primitive::Type component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001598 uint32_t dex_pc)
1599 : HTemplateInstruction(SideEffects::ChangesSomething()),
1600 dex_pc_(dex_pc),
1601 component_type_(component_type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001602 SetRawInputAt(0, array);
1603 SetRawInputAt(1, index);
1604 SetRawInputAt(2, value);
1605 }
1606
1607 virtual bool NeedsEnvironment() const {
1608 // We currently always call a runtime method to catch array store
1609 // exceptions.
1610 return InputAt(2)->GetType() == Primitive::kPrimNot;
1611 }
1612
1613 uint32_t GetDexPc() const { return dex_pc_; }
1614
Nicolas Geoffray39468442014-09-02 15:17:15 +01001615 Primitive::Type GetComponentType() const { return component_type_; }
1616
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001617 DECLARE_INSTRUCTION(ArraySet);
1618
1619 private:
1620 const uint32_t dex_pc_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01001621 const Primitive::Type component_type_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001622
1623 DISALLOW_COPY_AND_ASSIGN(HArraySet);
1624};
1625
1626class HArrayLength : public HExpression<1> {
1627 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001628 explicit HArrayLength(HInstruction* array)
1629 : HExpression(Primitive::kPrimInt, SideEffects::None()) {
1630 // Note that arrays do not change length, so the instruction does not
1631 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001632 SetRawInputAt(0, array);
1633 }
1634
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001635 virtual bool CanBeMoved() const { return true; }
1636 virtual bool InstructionDataEquals(HInstruction* other) const { return true; }
1637
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001638 DECLARE_INSTRUCTION(ArrayLength);
1639
1640 private:
1641 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
1642};
1643
1644class HBoundsCheck : public HExpression<2> {
1645 public:
1646 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001647 : HExpression(index->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001648 DCHECK(index->GetType() == Primitive::kPrimInt);
1649 SetRawInputAt(0, index);
1650 SetRawInputAt(1, length);
1651 }
1652
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001653 virtual bool CanBeMoved() const { return true; }
1654 virtual bool InstructionDataEquals(HInstruction* other) const { return true; }
1655
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001656 virtual bool NeedsEnvironment() const { return true; }
1657
1658 uint32_t GetDexPc() const { return dex_pc_; }
1659
1660 DECLARE_INSTRUCTION(BoundsCheck);
1661
1662 private:
1663 const uint32_t dex_pc_;
1664
1665 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
1666};
1667
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001668/**
1669 * Some DEX instructions are folded into multiple HInstructions that need
1670 * to stay live until the last HInstruction. This class
1671 * is used as a marker for the baseline compiler to ensure its preceding
1672 * HInstruction stays live. `index` is the temporary number that is used
1673 * for knowing the stack offset where to store the instruction.
1674 */
1675class HTemporary : public HTemplateInstruction<0> {
1676 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001677 explicit HTemporary(size_t index) : HTemplateInstruction(SideEffects::None()), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001678
1679 size_t GetIndex() const { return index_; }
1680
1681 DECLARE_INSTRUCTION(Temporary);
1682
1683 private:
1684 const size_t index_;
1685
1686 DISALLOW_COPY_AND_ASSIGN(HTemporary);
1687};
1688
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001689class HSuspendCheck : public HTemplateInstruction<0> {
1690 public:
1691 explicit HSuspendCheck(uint32_t dex_pc)
1692 : HTemplateInstruction(SideEffects::ChangesSomething()), dex_pc_(dex_pc) {}
1693
1694 virtual bool NeedsEnvironment() const {
1695 return true;
1696 }
1697
1698 uint32_t GetDexPc() const { return dex_pc_; }
1699
1700 DECLARE_INSTRUCTION(SuspendCheck);
1701
1702 private:
1703 const uint32_t dex_pc_;
1704
1705 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
1706};
1707
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001708class MoveOperands : public ArenaObject {
1709 public:
1710 MoveOperands(Location source, Location destination)
1711 : source_(source), destination_(destination) {}
1712
1713 Location GetSource() const { return source_; }
1714 Location GetDestination() const { return destination_; }
1715
1716 void SetSource(Location value) { source_ = value; }
1717 void SetDestination(Location value) { destination_ = value; }
1718
1719 // The parallel move resolver marks moves as "in-progress" by clearing the
1720 // destination (but not the source).
1721 Location MarkPending() {
1722 DCHECK(!IsPending());
1723 Location dest = destination_;
1724 destination_ = Location::NoLocation();
1725 return dest;
1726 }
1727
1728 void ClearPending(Location dest) {
1729 DCHECK(IsPending());
1730 destination_ = dest;
1731 }
1732
1733 bool IsPending() const {
1734 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
1735 return destination_.IsInvalid() && !source_.IsInvalid();
1736 }
1737
1738 // True if this blocks a move from the given location.
1739 bool Blocks(Location loc) const {
1740 return !IsEliminated() && source_.Equals(loc);
1741 }
1742
1743 // A move is redundant if it's been eliminated, if its source and
1744 // destination are the same, or if its destination is unneeded.
1745 bool IsRedundant() const {
1746 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
1747 }
1748
1749 // We clear both operands to indicate move that's been eliminated.
1750 void Eliminate() {
1751 source_ = destination_ = Location::NoLocation();
1752 }
1753
1754 bool IsEliminated() const {
1755 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
1756 return source_.IsInvalid();
1757 }
1758
1759 private:
1760 Location source_;
1761 Location destination_;
1762
1763 DISALLOW_COPY_AND_ASSIGN(MoveOperands);
1764};
1765
1766static constexpr size_t kDefaultNumberOfMoves = 4;
1767
1768class HParallelMove : public HTemplateInstruction<0> {
1769 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001770 explicit HParallelMove(ArenaAllocator* arena)
1771 : HTemplateInstruction(SideEffects::None()), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001772
1773 void AddMove(MoveOperands* move) {
1774 moves_.Add(move);
1775 }
1776
1777 MoveOperands* MoveOperandsAt(size_t index) const {
1778 return moves_.Get(index);
1779 }
1780
1781 size_t NumMoves() const { return moves_.Size(); }
1782
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001783 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001784
1785 private:
1786 GrowableArray<MoveOperands*> moves_;
1787
1788 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
1789};
1790
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001791class HGraphVisitor : public ValueObject {
1792 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001793 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
1794 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001795
Dave Allison20dfc792014-06-16 20:44:29 -07001796 virtual void VisitInstruction(HInstruction* instruction) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001797 virtual void VisitBasicBlock(HBasicBlock* block);
1798
1799 void VisitInsertionOrder();
1800
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001801 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001802
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001803 // Visit functions for instruction classes.
1804#define DECLARE_VISIT_INSTRUCTION(name) \
1805 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
1806
1807 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
1808
1809#undef DECLARE_VISIT_INSTRUCTION
1810
1811 private:
1812 HGraph* graph_;
1813
1814 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
1815};
1816
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001817class HInsertionOrderIterator : public ValueObject {
1818 public:
1819 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
1820
1821 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
1822 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
1823 void Advance() { ++index_; }
1824
1825 private:
1826 const HGraph& graph_;
1827 size_t index_;
1828
1829 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
1830};
1831
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001832class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001833 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001834 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001835
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001836 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
1837 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001838 void Advance() { ++index_; }
1839
1840 private:
1841 const HGraph& graph_;
1842 size_t index_;
1843
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001844 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001845};
1846
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001847class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001848 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001849 explicit HPostOrderIterator(const HGraph& graph)
1850 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001851
1852 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001853 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001854 void Advance() { --index_; }
1855
1856 private:
1857 const HGraph& graph_;
1858 size_t index_;
1859
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001860 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001861};
1862
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001863} // namespace art
1864
1865#endif // ART_COMPILER_OPTIMIZING_NODES_H_