blob: 47c8eda8d49a0e68b4754b7ae11a3f35225f92b7 [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 Geoffrayd31cf3d2014-09-08 17:30:24 +010041static const int kDefaultNumberOfDominatedBlocks = 1;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000042static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000043
Dave Allison20dfc792014-06-16 20:44:29 -070044enum IfCondition {
45 kCondEQ,
46 kCondNE,
47 kCondLT,
48 kCondLE,
49 kCondGT,
50 kCondGE,
51};
52
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010053class HInstructionList {
54 public:
55 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
56
57 void AddInstruction(HInstruction* instruction);
58 void RemoveInstruction(HInstruction* instruction);
59
Roland Levillainccc07a92014-09-16 14:48:16 +010060 // Return true if `instruction1` is found before `instruction2` in
61 // this instruction list and false otherwise. Abort if none
62 // of these instructions is found.
63 bool FoundBefore(const HInstruction* instruction1,
64 const HInstruction* instruction2) const;
65
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010066 private:
67 HInstruction* first_instruction_;
68 HInstruction* last_instruction_;
69
70 friend class HBasicBlock;
71 friend class HInstructionIterator;
Nicolas Geoffray804d0932014-05-02 08:46:00 +010072 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010073
74 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
75};
76
Nicolas Geoffray818f2102014-02-18 16:43:35 +000077// Control-flow graph of a method. Contains a list of basic blocks.
78class HGraph : public ArenaObject {
79 public:
80 explicit HGraph(ArenaAllocator* arena)
81 : arena_(arena),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000082 blocks_(arena, kDefaultNumberOfBlocks),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +010083 reverse_post_order_(arena, kDefaultNumberOfBlocks),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +010084 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +010085 number_of_vregs_(0),
86 number_of_in_vregs_(0),
Nicolas Geoffraye5038322014-07-04 09:41:32 +010087 number_of_temporaries_(0),
Dave Allison20dfc792014-06-16 20:44:29 -070088 current_instruction_id_(0) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +000089
Nicolas Geoffray787c3072014-03-17 10:20:19 +000090 ArenaAllocator* GetArena() const { return arena_; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +010091 const GrowableArray<HBasicBlock*>& GetBlocks() const { return blocks_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +000092
Nicolas Geoffray787c3072014-03-17 10:20:19 +000093 HBasicBlock* GetEntryBlock() const { return entry_block_; }
94 HBasicBlock* GetExitBlock() const { return exit_block_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000095
Nicolas Geoffray787c3072014-03-17 10:20:19 +000096 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
97 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000098
Nicolas Geoffray818f2102014-02-18 16:43:35 +000099 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100100
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000101 void BuildDominatorTree();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100102 void TransformToSSA();
103 void SimplifyCFG();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000104
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100105 // Find all natural loops in this graph. Aborts computation and returns false
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100106 // if one loop is not natural, that is the header does not dominate the back
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100107 // edge.
108 bool FindNaturalLoops() const;
109
110 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
111 void SimplifyLoop(HBasicBlock* header);
112
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000113 int GetNextInstructionId() {
114 return current_instruction_id_++;
115 }
116
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100117 uint16_t GetMaximumNumberOfOutVRegs() const {
118 return maximum_number_of_out_vregs_;
119 }
120
121 void UpdateMaximumNumberOfOutVRegs(uint16_t new_value) {
122 maximum_number_of_out_vregs_ = std::max(new_value, maximum_number_of_out_vregs_);
123 }
124
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100125 void UpdateNumberOfTemporaries(size_t count) {
126 number_of_temporaries_ = std::max(count, number_of_temporaries_);
127 }
128
129 size_t GetNumberOfTemporaries() const {
130 return number_of_temporaries_;
131 }
132
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100133 void SetNumberOfVRegs(uint16_t number_of_vregs) {
134 number_of_vregs_ = number_of_vregs;
135 }
136
137 uint16_t GetNumberOfVRegs() const {
138 return number_of_vregs_;
139 }
140
141 void SetNumberOfInVRegs(uint16_t value) {
142 number_of_in_vregs_ = value;
143 }
144
145 uint16_t GetNumberOfInVRegs() const {
146 return number_of_in_vregs_;
147 }
148
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100149 uint16_t GetNumberOfLocalVRegs() const {
150 return number_of_vregs_ - number_of_in_vregs_;
151 }
152
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100153 const GrowableArray<HBasicBlock*>& GetReversePostOrder() const {
154 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100155 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100156
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000157 private:
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000158 HBasicBlock* FindCommonDominator(HBasicBlock* first, HBasicBlock* second) const;
159 void VisitBlockForDominatorTree(HBasicBlock* block,
160 HBasicBlock* predecessor,
161 GrowableArray<size_t>* visits);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100162 void FindBackEdges(ArenaBitVector* visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000163 void VisitBlockForBackEdges(HBasicBlock* block,
164 ArenaBitVector* visited,
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100165 ArenaBitVector* visiting);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000166 void RemoveDeadBlocks(const ArenaBitVector& visited) const;
167
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000168 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000169
170 // List of blocks in insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000171 GrowableArray<HBasicBlock*> blocks_;
172
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100173 // List of blocks to perform a reverse post order tree traversal.
174 GrowableArray<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000175
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000176 HBasicBlock* entry_block_;
177 HBasicBlock* exit_block_;
178
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100179 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100180 uint16_t maximum_number_of_out_vregs_;
181
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100182 // The number of virtual registers in this method. Contains the parameters.
183 uint16_t number_of_vregs_;
184
185 // The number of virtual registers used by parameters of this method.
186 uint16_t number_of_in_vregs_;
187
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100188 // The number of temporaries that will be needed for the baseline compiler.
189 size_t number_of_temporaries_;
190
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000191 // The current id to assign to a newly added instruction. See HInstruction.id_.
192 int current_instruction_id_;
193
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000194 DISALLOW_COPY_AND_ASSIGN(HGraph);
195};
196
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000197class HLoopInformation : public ArenaObject {
198 public:
199 HLoopInformation(HBasicBlock* header, HGraph* graph)
200 : header_(header),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100201 back_edges_(graph->GetArena(), kDefaultNumberOfBackEdges),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100202 // Make bit vector growable, as the number of blocks may change.
203 blocks_(graph->GetArena(), graph->GetBlocks().Size(), true) {}
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100204
205 HBasicBlock* GetHeader() const {
206 return header_;
207 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000208
209 void AddBackEdge(HBasicBlock* back_edge) {
210 back_edges_.Add(back_edge);
211 }
212
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100213 void RemoveBackEdge(HBasicBlock* back_edge) {
214 back_edges_.Delete(back_edge);
215 }
216
217 bool IsBackEdge(HBasicBlock* block) {
218 for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) {
219 if (back_edges_.Get(i) == block) return true;
220 }
221 return false;
222 }
223
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000224 int NumberOfBackEdges() const {
225 return back_edges_.Size();
226 }
227
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100228 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100229
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100230 const GrowableArray<HBasicBlock*>& GetBackEdges() const {
231 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100232 }
233
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100234 void ClearBackEdges() {
235 back_edges_.Reset();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100236 }
237
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100238 // Find blocks that are part of this loop. Returns whether the loop is a natural loop,
239 // that is the header dominates the back edge.
240 bool Populate();
241
242 // Returns whether this loop information contains `block`.
243 // Note that this loop information *must* be populated before entering this function.
244 bool Contains(const HBasicBlock& block) const;
245
246 // Returns whether this loop information is an inner loop of `other`.
247 // Note that `other` *must* be populated before entering this function.
248 bool IsIn(const HLoopInformation& other) const;
249
250 const ArenaBitVector& GetBlocks() const { return blocks_; }
251
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000252 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100253 // Internal recursive implementation of `Populate`.
254 void PopulateRecursive(HBasicBlock* block);
255
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000256 HBasicBlock* header_;
257 GrowableArray<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100258 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000259
260 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
261};
262
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100263static constexpr size_t kNoLifetime = -1;
264
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000265// A block in a method. Contains the list of instructions represented
266// as a double linked list. Each block knows its predecessors and
267// successors.
268class HBasicBlock : public ArenaObject {
269 public:
270 explicit HBasicBlock(HGraph* graph)
271 : graph_(graph),
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000272 predecessors_(graph->GetArena(), kDefaultNumberOfPredecessors),
273 successors_(graph->GetArena(), kDefaultNumberOfSuccessors),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000274 loop_information_(nullptr),
275 dominator_(nullptr),
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100276 dominated_blocks_(graph->GetArena(), kDefaultNumberOfDominatedBlocks),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100277 block_id_(-1),
278 lifetime_start_(kNoLifetime),
279 lifetime_end_(kNoLifetime) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000280
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100281 const GrowableArray<HBasicBlock*>& GetPredecessors() const {
282 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000283 }
284
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100285 const GrowableArray<HBasicBlock*>& GetSuccessors() const {
286 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000287 }
288
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100289 const GrowableArray<HBasicBlock*>& GetDominatedBlocks() const {
290 return dominated_blocks_;
291 }
292
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000293 void AddBackEdge(HBasicBlock* back_edge) {
294 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000295 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000296 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100297 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000298 loop_information_->AddBackEdge(back_edge);
299 }
300
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000301 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000302
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000303 int GetBlockId() const { return block_id_; }
304 void SetBlockId(int id) { block_id_ = id; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000305
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000306 HBasicBlock* GetDominator() const { return dominator_; }
307 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100308 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.Add(block); }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000309
310 int NumberOfBackEdges() const {
311 return loop_information_ == nullptr
312 ? 0
313 : loop_information_->NumberOfBackEdges();
314 }
315
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100316 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
317 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100318 const HInstructionList& GetInstructions() const { return instructions_; }
319 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100320 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000321
322 void AddSuccessor(HBasicBlock* block) {
323 successors_.Add(block);
324 block->predecessors_.Add(this);
325 }
326
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100327 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
328 size_t successor_index = GetSuccessorIndexOf(existing);
329 DCHECK_NE(successor_index, static_cast<size_t>(-1));
330 existing->RemovePredecessor(this);
331 new_block->predecessors_.Add(this);
332 successors_.Put(successor_index, new_block);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000333 }
334
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100335 void RemovePredecessor(HBasicBlock* block) {
336 predecessors_.Delete(block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100337 }
338
339 void ClearAllPredecessors() {
340 predecessors_.Reset();
341 }
342
343 void AddPredecessor(HBasicBlock* block) {
344 predecessors_.Add(block);
345 block->successors_.Add(this);
346 }
347
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100348 void SwapPredecessors() {
Nicolas Geoffrayc83d4412014-09-18 16:46:20 +0100349 DCHECK_EQ(predecessors_.Size(), 2u);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100350 HBasicBlock* temp = predecessors_.Get(0);
351 predecessors_.Put(0, predecessors_.Get(1));
352 predecessors_.Put(1, temp);
353 }
354
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100355 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) {
356 for (size_t i = 0, e = predecessors_.Size(); i < e; ++i) {
357 if (predecessors_.Get(i) == predecessor) {
358 return i;
359 }
360 }
361 return -1;
362 }
363
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100364 size_t GetSuccessorIndexOf(HBasicBlock* successor) {
365 for (size_t i = 0, e = successors_.Size(); i < e; ++i) {
366 if (successors_.Get(i) == successor) {
367 return i;
368 }
369 }
370 return -1;
371 }
372
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000373 void AddInstruction(HInstruction* instruction);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100374 void RemoveInstruction(HInstruction* instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100375 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +0100376 // Replace instruction `initial` with `replacement` within this block.
377 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
378 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100379 void AddPhi(HPhi* phi);
380 void RemovePhi(HPhi* phi);
381
382 bool IsLoopHeader() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100383 return (loop_information_ != nullptr) && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100384 }
385
386 HLoopInformation* GetLoopInformation() const {
387 return loop_information_;
388 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000389
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100390 // Set the loop_information_ on this block. This method overrides the current
391 // loop_information if it is an outer loop of the passed loop information.
392 void SetInLoop(HLoopInformation* info) {
393 if (IsLoopHeader()) {
394 // Nothing to do. This just means `info` is an outer loop.
395 } else if (loop_information_ == nullptr) {
396 loop_information_ = info;
397 } else if (loop_information_->Contains(*info->GetHeader())) {
398 // Block is currently part of an outer loop. Make it part of this inner loop.
399 // Note that a non loop header having a loop information means this loop information
400 // has already been populated
401 loop_information_ = info;
402 } else {
403 // Block is part of an inner loop. Do not update the loop information.
404 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
405 // at this point, because this method is being called while populating `info`.
406 }
407 }
408
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100409 bool IsInLoop() const { return loop_information_ != nullptr; }
410
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100411 // Returns wheter this block dominates the blocked passed as parameter.
412 bool Dominates(HBasicBlock* block) const;
413
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100414 size_t GetLifetimeStart() const { return lifetime_start_; }
415 size_t GetLifetimeEnd() const { return lifetime_end_; }
416
417 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
418 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
419
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000420 private:
421 HGraph* const graph_;
422 GrowableArray<HBasicBlock*> predecessors_;
423 GrowableArray<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100424 HInstructionList instructions_;
425 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000426 HLoopInformation* loop_information_;
427 HBasicBlock* dominator_;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100428 GrowableArray<HBasicBlock*> dominated_blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000429 int block_id_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100430 size_t lifetime_start_;
431 size_t lifetime_end_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000432
433 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
434};
435
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100436#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000437 M(Add) \
Dave Allison20dfc792014-06-16 20:44:29 -0700438 M(Condition) \
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000439 M(Equal) \
Dave Allison20dfc792014-06-16 20:44:29 -0700440 M(NotEqual) \
441 M(LessThan) \
442 M(LessThanOrEqual) \
443 M(GreaterThan) \
444 M(GreaterThanOrEqual) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000445 M(Exit) \
446 M(Goto) \
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000447 M(If) \
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000448 M(IntConstant) \
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000449 M(InvokeStatic) \
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100450 M(InvokeVirtual) \
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000451 M(LoadLocal) \
452 M(Local) \
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100453 M(LongConstant) \
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +0100454 M(NewInstance) \
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100455 M(Not) \
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100456 M(ParameterValue) \
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100457 M(ParallelMove) \
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100458 M(Phi) \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000459 M(Return) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000460 M(ReturnVoid) \
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000461 M(StoreLocal) \
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100462 M(Sub) \
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100463 M(Compare) \
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100464 M(InstanceFieldGet) \
465 M(InstanceFieldSet) \
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100466 M(ArrayGet) \
467 M(ArraySet) \
468 M(ArrayLength) \
469 M(BoundsCheck) \
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100470 M(NullCheck) \
471 M(Temporary) \
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000472 M(SuspendCheck) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000473
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100474#define FOR_EACH_INSTRUCTION(M) \
475 FOR_EACH_CONCRETE_INSTRUCTION(M) \
Roland Levillainccc07a92014-09-16 14:48:16 +0100476 M(Constant) \
477 M(BinaryOperation)
Dave Allison20dfc792014-06-16 20:44:29 -0700478
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000479#define FORWARD_DECLARATION(type) class H##type;
480FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
481#undef FORWARD_DECLARATION
482
Roland Levillainccc07a92014-09-16 14:48:16 +0100483#define DECLARE_INSTRUCTION(type) \
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100484 virtual InstructionKind GetKind() const { return k##type; } \
Roland Levillainccc07a92014-09-16 14:48:16 +0100485 virtual const char* DebugName() const { return #type; } \
486 virtual const H##type* As##type() const OVERRIDE { return this; } \
487 virtual H##type* As##type() OVERRIDE { return this; } \
488 virtual bool InstructionTypeEquals(HInstruction* other) const { \
489 return other->Is##type(); \
490 } \
491 virtual void Accept(HGraphVisitor* visitor)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000492
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100493template <typename T>
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000494class HUseListNode : public ArenaObject {
495 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100496 HUseListNode(T* user, size_t index, HUseListNode* tail)
Dave Allison20dfc792014-06-16 20:44:29 -0700497 : user_(user), index_(index), tail_(tail) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000498
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000499 HUseListNode* GetTail() const { return tail_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100500 T* GetUser() const { return user_; }
501 size_t GetIndex() const { return index_; }
502
503 void SetTail(HUseListNode<T>* node) { tail_ = node; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000504
505 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100506 T* const user_;
507 const size_t index_;
508 HUseListNode<T>* tail_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000509
510 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
511};
512
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100513// Represents the side effects an instruction may have.
514class SideEffects : public ValueObject {
515 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100516 SideEffects() : flags_(0) {}
517
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100518 static SideEffects None() {
519 return SideEffects(0);
520 }
521
522 static SideEffects All() {
523 return SideEffects(ChangesSomething().flags_ | DependsOnSomething().flags_);
524 }
525
526 static SideEffects ChangesSomething() {
527 return SideEffects((1 << kFlagChangesCount) - 1);
528 }
529
530 static SideEffects DependsOnSomething() {
531 int count = kFlagDependsOnCount - kFlagChangesCount;
532 return SideEffects(((1 << count) - 1) << kFlagChangesCount);
533 }
534
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100535 SideEffects Union(SideEffects other) const {
536 return SideEffects(flags_ | other.flags_);
537 }
538
Roland Levillain72bceff2014-09-15 18:29:00 +0100539 bool HasSideEffects() const {
540 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
541 return (flags_ & all_bits_set) != 0;
542 }
543
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100544 bool HasAllSideEffects() const {
545 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
546 return all_bits_set == (flags_ & all_bits_set);
547 }
548
549 bool DependsOn(SideEffects other) const {
550 size_t depends_flags = other.ComputeDependsFlags();
551 return (flags_ & depends_flags) != 0;
552 }
553
554 bool HasDependencies() const {
555 int count = kFlagDependsOnCount - kFlagChangesCount;
556 size_t all_bits_set = (1 << count) - 1;
557 return ((flags_ >> kFlagChangesCount) & all_bits_set) != 0;
558 }
559
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100560 private:
561 static constexpr int kFlagChangesSomething = 0;
562 static constexpr int kFlagChangesCount = kFlagChangesSomething + 1;
563
564 static constexpr int kFlagDependsOnSomething = kFlagChangesCount;
565 static constexpr int kFlagDependsOnCount = kFlagDependsOnSomething + 1;
566
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100567 explicit SideEffects(size_t flags) : flags_(flags) {}
568
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100569 size_t ComputeDependsFlags() const {
570 return flags_ << kFlagChangesCount;
571 }
572
573 size_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100574};
575
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000576class HInstruction : public ArenaObject {
577 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100578 explicit HInstruction(SideEffects side_effects)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000579 : previous_(nullptr),
580 next_(nullptr),
581 block_(nullptr),
582 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100583 ssa_index_(-1),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000584 uses_(nullptr),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100585 env_uses_(nullptr),
586 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100587 locations_(nullptr),
588 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100589 lifetime_position_(kNoLifetime),
590 side_effects_(side_effects) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000591
Dave Allison20dfc792014-06-16 20:44:29 -0700592 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000593
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100594#define DECLARE_KIND(type) k##type,
595 enum InstructionKind {
596 FOR_EACH_INSTRUCTION(DECLARE_KIND)
597 };
598#undef DECLARE_KIND
599
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000600 HInstruction* GetNext() const { return next_; }
601 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000602
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000603 HBasicBlock* GetBlock() const { return block_; }
604 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100605 bool IsInBlock() const { return block_ != nullptr; }
606 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +0100607 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000608
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100609 virtual size_t InputCount() const = 0;
610 virtual HInstruction* InputAt(size_t i) const = 0;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000611
612 virtual void Accept(HGraphVisitor* visitor) = 0;
613 virtual const char* DebugName() const = 0;
614
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100615 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100616 virtual void SetRawInputAt(size_t index, HInstruction* input) = 0;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100617
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100618 virtual bool NeedsEnvironment() const { return false; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100619 virtual bool IsControlFlow() const { return false; }
Roland Levillain72bceff2014-09-15 18:29:00 +0100620 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100621
622 void AddUseAt(HInstruction* user, size_t index) {
623 uses_ = new (block_->GetGraph()->GetArena()) HUseListNode<HInstruction>(user, index, uses_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000624 }
625
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100626 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +0100627 DCHECK(user != nullptr);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100628 env_uses_ = new (block_->GetGraph()->GetArena()) HUseListNode<HEnvironment>(
629 user, index, env_uses_);
630 }
631
632 void RemoveUser(HInstruction* user, size_t index);
Nicolas Geoffray724c9632014-09-22 12:27:27 +0100633 void RemoveEnvironmentUser(HEnvironment* user, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100634
635 HUseListNode<HInstruction>* GetUses() const { return uses_; }
636 HUseListNode<HEnvironment>* GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000637
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100638 bool HasUses() const { return uses_ != nullptr || env_uses_ != nullptr; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100639 bool HasEnvironmentUses() const { return env_uses_ != nullptr; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000640
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100641 size_t NumberOfUses() const {
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100642 // TODO: Optimize this method if it is used outside of the HGraphVisualizer.
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100643 size_t result = 0;
644 HUseListNode<HInstruction>* current = uses_;
645 while (current != nullptr) {
646 current = current->GetTail();
647 ++result;
648 }
649 return result;
650 }
651
Roland Levillainccc07a92014-09-16 14:48:16 +0100652 // Does this instruction dominate `other_instruction`? Aborts if
653 // this instruction and `other_instruction` are both phis.
654 bool Dominates(HInstruction* other_instruction) const;
655
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000656 int GetId() const { return id_; }
657 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000658
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100659 int GetSsaIndex() const { return ssa_index_; }
660 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
661 bool HasSsaIndex() const { return ssa_index_ != -1; }
662
663 bool HasEnvironment() const { return environment_ != nullptr; }
664 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100665 void SetEnvironment(HEnvironment* environment) { environment_ = environment; }
666
Nicolas Geoffray39468442014-09-02 15:17:15 +0100667 // Returns the number of entries in the environment. Typically, that is the
668 // number of dex registers in a method. It could be more in case of inlining.
669 size_t EnvironmentSize() const;
670
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000671 LocationSummary* GetLocations() const { return locations_; }
672 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000673
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100674 void ReplaceWith(HInstruction* instruction);
675
Dave Allison20dfc792014-06-16 20:44:29 -0700676 bool HasOnlyOneUse() const {
677 return uses_ != nullptr && uses_->GetTail() == nullptr;
678 }
679
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000680#define INSTRUCTION_TYPE_CHECK(type) \
Roland Levillainccc07a92014-09-16 14:48:16 +0100681 bool Is##type() const { return (As##type() != nullptr); } \
682 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000683 virtual H##type* As##type() { return nullptr; }
684
685 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
686#undef INSTRUCTION_TYPE_CHECK
687
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100688 // Returns whether the instruction can be moved within the graph.
689 virtual bool CanBeMoved() const { return false; }
690
691 // Returns whether the two instructions are of the same kind.
692 virtual bool InstructionTypeEquals(HInstruction* other) const { return false; }
693
694 // Returns whether any data encoded in the two instructions is equal.
695 // This method does not look at the inputs. Both instructions must be
696 // of the same type, otherwise the method has undefined behavior.
697 virtual bool InstructionDataEquals(HInstruction* other) const { return false; }
698
699 // Returns whether two instructions are equal, that is:
700 // 1) They have the same type and contain the same data,
701 // 2) Their inputs are identical.
702 bool Equals(HInstruction* other) const;
703
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100704 virtual InstructionKind GetKind() const = 0;
705
706 virtual size_t ComputeHashCode() const {
707 size_t result = GetKind();
708 for (size_t i = 0, e = InputCount(); i < e; ++i) {
709 result = (result * 31) + InputAt(i)->GetId();
710 }
711 return result;
712 }
713
714 SideEffects GetSideEffects() const { return side_effects_; }
715
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100716 size_t GetLifetimePosition() const { return lifetime_position_; }
717 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
718 LiveInterval* GetLiveInterval() const { return live_interval_; }
719 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
720 bool HasLiveInterval() const { return live_interval_ != nullptr; }
721
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000722 private:
723 HInstruction* previous_;
724 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000725 HBasicBlock* block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000726
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000727 // An instruction gets an id when it is added to the graph.
728 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +0100729 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000730 int id_;
731
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100732 // When doing liveness analysis, instructions that have uses get an SSA index.
733 int ssa_index_;
734
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100735 // List of instructions that have this instruction as input.
736 HUseListNode<HInstruction>* uses_;
737
738 // List of environments that contain this instruction.
739 HUseListNode<HEnvironment>* env_uses_;
740
Nicolas Geoffray39468442014-09-02 15:17:15 +0100741 // The environment associated with this instruction. Not null if the instruction
742 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100743 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000744
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000745 // Set by the code generator.
746 LocationSummary* locations_;
747
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100748 // Set by the liveness analysis.
749 LiveInterval* live_interval_;
750
751 // Set by the liveness analysis, this is the position in a linear
752 // order of blocks where this instruction's live interval start.
753 size_t lifetime_position_;
754
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100755 const SideEffects side_effects_;
756
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000757 friend class HBasicBlock;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100758 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000759
760 DISALLOW_COPY_AND_ASSIGN(HInstruction);
761};
762
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100763template<typename T>
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000764class HUseIterator : public ValueObject {
765 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100766 explicit HUseIterator(HUseListNode<T>* uses) : current_(uses) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000767
768 bool Done() const { return current_ == nullptr; }
769
770 void Advance() {
771 DCHECK(!Done());
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000772 current_ = current_->GetTail();
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000773 }
774
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100775 HUseListNode<T>* Current() const {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000776 DCHECK(!Done());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100777 return current_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000778 }
779
780 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100781 HUseListNode<T>* current_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000782
783 friend class HValue;
784};
785
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100786// A HEnvironment object contains the values of virtual registers at a given location.
787class HEnvironment : public ArenaObject {
788 public:
789 HEnvironment(ArenaAllocator* arena, size_t number_of_vregs) : vregs_(arena, number_of_vregs) {
790 vregs_.SetSize(number_of_vregs);
791 for (size_t i = 0; i < number_of_vregs; i++) {
792 vregs_.Put(i, nullptr);
793 }
794 }
795
796 void Populate(const GrowableArray<HInstruction*>& env) {
797 for (size_t i = 0; i < env.Size(); i++) {
798 HInstruction* instruction = env.Get(i);
799 vregs_.Put(i, instruction);
800 if (instruction != nullptr) {
801 instruction->AddEnvUseAt(this, i);
802 }
803 }
804 }
805
806 void SetRawEnvAt(size_t index, HInstruction* instruction) {
807 vregs_.Put(index, instruction);
808 }
809
Nicolas Geoffray39468442014-09-02 15:17:15 +0100810 HInstruction* GetInstructionAt(size_t index) const {
811 return vregs_.Get(index);
812 }
813
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100814 GrowableArray<HInstruction*>* GetVRegs() {
815 return &vregs_;
816 }
817
Nicolas Geoffray39468442014-09-02 15:17:15 +0100818 size_t Size() const { return vregs_.Size(); }
819
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100820 private:
821 GrowableArray<HInstruction*> vregs_;
822
823 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
824};
825
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000826class HInputIterator : public ValueObject {
827 public:
Dave Allison20dfc792014-06-16 20:44:29 -0700828 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000829
830 bool Done() const { return index_ == instruction_->InputCount(); }
831 HInstruction* Current() const { return instruction_->InputAt(index_); }
832 void Advance() { index_++; }
833
834 private:
835 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100836 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000837
838 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
839};
840
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000841class HInstructionIterator : public ValueObject {
842 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100843 explicit HInstructionIterator(const HInstructionList& instructions)
844 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000845 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000846 }
847
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000848 bool Done() const { return instruction_ == nullptr; }
849 HInstruction* Current() const { return instruction_; }
850 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000851 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000852 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000853 }
854
855 private:
856 HInstruction* instruction_;
857 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100858
859 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000860};
861
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100862class HBackwardInstructionIterator : public ValueObject {
863 public:
864 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
865 : instruction_(instructions.last_instruction_) {
866 next_ = Done() ? nullptr : instruction_->GetPrevious();
867 }
868
869 bool Done() const { return instruction_ == nullptr; }
870 HInstruction* Current() const { return instruction_; }
871 void Advance() {
872 instruction_ = next_;
873 next_ = Done() ? nullptr : instruction_->GetPrevious();
874 }
875
876 private:
877 HInstruction* instruction_;
878 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100879
880 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100881};
882
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000883// An embedded container with N elements of type T. Used (with partial
884// specialization for N=0) because embedded arrays cannot have size 0.
885template<typename T, intptr_t N>
886class EmbeddedArray {
887 public:
Dave Allison20dfc792014-06-16 20:44:29 -0700888 EmbeddedArray() : elements_() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000889
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000890 intptr_t GetLength() const { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000891
892 const T& operator[](intptr_t i) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000893 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000894 return elements_[i];
895 }
896
897 T& operator[](intptr_t i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000898 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000899 return elements_[i];
900 }
901
902 const T& At(intptr_t i) const {
903 return (*this)[i];
904 }
905
906 void SetAt(intptr_t i, const T& val) {
907 (*this)[i] = val;
908 }
909
910 private:
911 T elements_[N];
912};
913
914template<typename T>
915class EmbeddedArray<T, 0> {
916 public:
917 intptr_t length() const { return 0; }
918 const T& operator[](intptr_t i) const {
919 LOG(FATAL) << "Unreachable";
920 static T sentinel = 0;
921 return sentinel;
922 }
923 T& operator[](intptr_t i) {
924 LOG(FATAL) << "Unreachable";
925 static T sentinel = 0;
926 return sentinel;
927 }
928};
929
930template<intptr_t N>
931class HTemplateInstruction: public HInstruction {
932 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100933 HTemplateInstruction<N>(SideEffects side_effects)
934 : HInstruction(side_effects), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -0700935 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000936
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100937 virtual size_t InputCount() const { return N; }
938 virtual HInstruction* InputAt(size_t i) const { return inputs_[i]; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000939
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000940 protected:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100941 virtual void SetRawInputAt(size_t i, HInstruction* instruction) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000942 inputs_[i] = instruction;
943 }
944
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000945 private:
946 EmbeddedArray<HInstruction*, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100947
948 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000949};
950
Dave Allison20dfc792014-06-16 20:44:29 -0700951template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100952class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -0700953 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100954 HExpression<N>(Primitive::Type type, SideEffects side_effects)
955 : HTemplateInstruction<N>(side_effects), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -0700956 virtual ~HExpression() {}
957
958 virtual Primitive::Type GetType() const { return type_; }
959
960 private:
961 const Primitive::Type type_;
962};
963
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000964// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
965// instruction that branches to the exit block.
966class HReturnVoid : public HTemplateInstruction<0> {
967 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100968 HReturnVoid() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100969
970 virtual bool IsControlFlow() const { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000971
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100972 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000973
974 private:
975 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
976};
977
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000978// Represents dex's RETURN opcodes. A HReturn is a control flow
979// instruction that branches to the exit block.
980class HReturn : public HTemplateInstruction<1> {
981 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100982 explicit HReturn(HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000983 SetRawInputAt(0, value);
984 }
985
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100986 virtual bool IsControlFlow() const { return true; }
987
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100988 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000989
990 private:
991 DISALLOW_COPY_AND_ASSIGN(HReturn);
992};
993
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000994// The exit instruction is the only instruction of the exit block.
995// Instructions aborting the method (HTrow and HReturn) must branch to the
996// exit block.
997class HExit : public HTemplateInstruction<0> {
998 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100999 HExit() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001000
1001 virtual bool IsControlFlow() const { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001002
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001003 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001004
1005 private:
1006 DISALLOW_COPY_AND_ASSIGN(HExit);
1007};
1008
1009// Jumps from one block to another.
1010class HGoto : public HTemplateInstruction<0> {
1011 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001012 HGoto() : HTemplateInstruction(SideEffects::None()) {}
1013
1014 virtual bool IsControlFlow() const { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001015
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001016 HBasicBlock* GetSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001017 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001018 }
1019
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001020 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001021
1022 private:
1023 DISALLOW_COPY_AND_ASSIGN(HGoto);
1024};
1025
Dave Allison20dfc792014-06-16 20:44:29 -07001026
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001027// Conditional branch. A block ending with an HIf instruction must have
1028// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001029class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001030 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001031 explicit HIf(HInstruction* input) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001032 SetRawInputAt(0, input);
1033 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001034
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001035 virtual bool IsControlFlow() const { return true; }
1036
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001037 HBasicBlock* IfTrueSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001038 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001039 }
1040
1041 HBasicBlock* IfFalseSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001042 return GetBlock()->GetSuccessors().Get(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001043 }
1044
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001045 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001046
Dave Allison20dfc792014-06-16 20:44:29 -07001047 virtual bool IsIfInstruction() const { return true; }
1048
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001049 private:
1050 DISALLOW_COPY_AND_ASSIGN(HIf);
1051};
1052
Dave Allison20dfc792014-06-16 20:44:29 -07001053class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001054 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001055 HBinaryOperation(Primitive::Type result_type,
1056 HInstruction* left,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001057 HInstruction* right) : HExpression(result_type, SideEffects::None()) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001058 SetRawInputAt(0, left);
1059 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001060 }
1061
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001062 HInstruction* GetLeft() const { return InputAt(0); }
1063 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07001064 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001065
1066 virtual bool IsCommutative() { return false; }
1067
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001068 virtual bool CanBeMoved() const { return true; }
1069 virtual bool InstructionDataEquals(HInstruction* other) const { return true; }
1070
Roland Levillain556c3d12014-09-18 15:25:07 +01001071 // Try to statically evaluate `operation` and return an HConstant
1072 // containing the result of this evaluation. If `operation` cannot
1073 // be evaluated as a constant, return nullptr.
1074 HConstant* TryStaticEvaluation(ArenaAllocator* allocator) const;
1075
1076 // Apply this operation to `x` and `y`.
1077 virtual int32_t Evaluate(int32_t x, int32_t y) const = 0;
1078 virtual int64_t Evaluate(int64_t x, int64_t y) const = 0;
1079
Roland Levillainccc07a92014-09-16 14:48:16 +01001080 DECLARE_INSTRUCTION(BinaryOperation);
1081
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001082 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001083 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
1084};
1085
Dave Allison20dfc792014-06-16 20:44:29 -07001086class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001087 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001088 HCondition(HInstruction* first, HInstruction* second)
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001089 : HBinaryOperation(Primitive::kPrimBoolean, first, second) {}
1090
1091 virtual bool IsCommutative() { return true; }
Dave Allison20dfc792014-06-16 20:44:29 -07001092 bool NeedsMaterialization() const;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001093
Dave Allison20dfc792014-06-16 20:44:29 -07001094 DECLARE_INSTRUCTION(Condition);
1095
1096 virtual IfCondition GetCondition() const = 0;
1097
1098 private:
1099 DISALLOW_COPY_AND_ASSIGN(HCondition);
1100};
1101
1102// Instruction to check if two inputs are equal to each other.
1103class HEqual : public HCondition {
1104 public:
1105 HEqual(HInstruction* first, HInstruction* second)
1106 : HCondition(first, second) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001107
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
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001111 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001112
Dave Allison20dfc792014-06-16 20:44:29 -07001113 virtual IfCondition GetCondition() const {
1114 return kCondEQ;
1115 }
1116
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001117 private:
1118 DISALLOW_COPY_AND_ASSIGN(HEqual);
1119};
1120
Dave Allison20dfc792014-06-16 20:44:29 -07001121class HNotEqual : public HCondition {
1122 public:
1123 HNotEqual(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(NotEqual);
1130
1131 virtual IfCondition GetCondition() const {
1132 return kCondNE;
1133 }
1134
1135 private:
1136 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
1137};
1138
1139class HLessThan : public HCondition {
1140 public:
1141 HLessThan(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(LessThan);
1148
1149 virtual IfCondition GetCondition() const {
1150 return kCondLT;
1151 }
1152
1153 private:
1154 DISALLOW_COPY_AND_ASSIGN(HLessThan);
1155};
1156
1157class HLessThanOrEqual : public HCondition {
1158 public:
1159 HLessThanOrEqual(HInstruction* first, HInstruction* second)
1160 : HCondition(first, second) {}
1161
Roland Levillain556c3d12014-09-18 15:25:07 +01001162 virtual int32_t Evaluate(int32_t x, int32_t y) const { return x <= y; }
1163 virtual int64_t Evaluate(int64_t x, int64_t y) const { return x <= y; }
1164
Dave Allison20dfc792014-06-16 20:44:29 -07001165 DECLARE_INSTRUCTION(LessThanOrEqual);
1166
1167 virtual IfCondition GetCondition() const {
1168 return kCondLE;
1169 }
1170
1171 private:
1172 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
1173};
1174
1175class HGreaterThan : public HCondition {
1176 public:
1177 HGreaterThan(HInstruction* first, HInstruction* second)
1178 : HCondition(first, second) {}
1179
Roland Levillain556c3d12014-09-18 15:25:07 +01001180 virtual int32_t Evaluate(int32_t x, int32_t y) const { return x > y; }
1181 virtual int64_t Evaluate(int64_t x, int64_t y) const { return x > y; }
1182
Dave Allison20dfc792014-06-16 20:44:29 -07001183 DECLARE_INSTRUCTION(GreaterThan);
1184
1185 virtual IfCondition GetCondition() const {
1186 return kCondGT;
1187 }
1188
1189 private:
1190 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
1191};
1192
1193class HGreaterThanOrEqual : public HCondition {
1194 public:
1195 HGreaterThanOrEqual(HInstruction* first, HInstruction* second)
1196 : HCondition(first, second) {}
1197
Roland Levillain556c3d12014-09-18 15:25:07 +01001198 virtual int32_t Evaluate(int32_t x, int32_t y) const { return x >= y; }
1199 virtual int64_t Evaluate(int64_t x, int64_t y) const { return x >= y; }
1200
Dave Allison20dfc792014-06-16 20:44:29 -07001201 DECLARE_INSTRUCTION(GreaterThanOrEqual);
1202
1203 virtual IfCondition GetCondition() const {
1204 return kCondGE;
1205 }
1206
1207 private:
1208 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
1209};
1210
1211
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001212// Instruction to check how two inputs compare to each other.
1213// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
1214class HCompare : public HBinaryOperation {
1215 public:
1216 HCompare(Primitive::Type type, HInstruction* first, HInstruction* second)
1217 : HBinaryOperation(Primitive::kPrimInt, first, second) {
1218 DCHECK_EQ(type, first->GetType());
1219 DCHECK_EQ(type, second->GetType());
1220 }
1221
Roland Levillain556c3d12014-09-18 15:25:07 +01001222 virtual int32_t Evaluate(int32_t x, int32_t y) const {
1223 return
1224 x == y ? 0 :
1225 x > y ? 1 :
1226 -1;
1227 }
1228 virtual int64_t Evaluate(int64_t x, int64_t y) const {
1229 return
1230 x == y ? 0 :
1231 x > y ? 1 :
1232 -1;
1233 }
1234
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001235 DECLARE_INSTRUCTION(Compare);
1236
1237 private:
1238 DISALLOW_COPY_AND_ASSIGN(HCompare);
1239};
1240
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001241// A local in the graph. Corresponds to a Dex register.
1242class HLocal : public HTemplateInstruction<0> {
1243 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001244 explicit HLocal(uint16_t reg_number)
1245 : HTemplateInstruction(SideEffects::None()), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001246
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001247 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001248
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001249 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001250
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001251 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001252 // The Dex register number.
1253 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001254
1255 DISALLOW_COPY_AND_ASSIGN(HLocal);
1256};
1257
1258// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07001259class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001260 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001261 explicit HLoadLocal(HLocal* local, Primitive::Type type)
1262 : HExpression(type, SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001263 SetRawInputAt(0, local);
1264 }
1265
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001266 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1267
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001268 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001269
1270 private:
1271 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
1272};
1273
1274// Store a value in a given local. This instruction has two inputs: the value
1275// and the local.
1276class HStoreLocal : public HTemplateInstruction<2> {
1277 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001278 HStoreLocal(HLocal* local, HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001279 SetRawInputAt(0, local);
1280 SetRawInputAt(1, value);
1281 }
1282
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001283 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1284
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001285 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001286
1287 private:
1288 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
1289};
1290
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001291class HConstant : public HExpression<0> {
1292 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001293 explicit HConstant(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
1294
1295 virtual bool CanBeMoved() const { return true; }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001296
1297 DECLARE_INSTRUCTION(Constant);
1298
1299 private:
1300 DISALLOW_COPY_AND_ASSIGN(HConstant);
1301};
1302
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001303// Constants of the type int. Those can be from Dex instructions, or
1304// synthesized (for example with the if-eqz instruction).
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001305class HIntConstant : public HConstant {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001306 public:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001307 explicit HIntConstant(int32_t value) : HConstant(Primitive::kPrimInt), value_(value) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001308
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001309 int32_t GetValue() const { return value_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001310
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001311 virtual bool InstructionDataEquals(HInstruction* other) const {
1312 return other->AsIntConstant()->value_ == value_;
1313 }
1314
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001315 virtual size_t ComputeHashCode() const { return GetValue(); }
1316
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001317 DECLARE_INSTRUCTION(IntConstant);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001318
1319 private:
1320 const int32_t value_;
1321
1322 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
1323};
1324
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001325class HLongConstant : public HConstant {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001326 public:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001327 explicit HLongConstant(int64_t value) : HConstant(Primitive::kPrimLong), value_(value) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001328
1329 int64_t GetValue() const { return value_; }
1330
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001331 virtual bool InstructionDataEquals(HInstruction* other) const {
1332 return other->AsLongConstant()->value_ == value_;
1333 }
1334
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001335 virtual size_t ComputeHashCode() const { return static_cast<size_t>(GetValue()); }
1336
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001337 DECLARE_INSTRUCTION(LongConstant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001338
1339 private:
1340 const int64_t value_;
1341
1342 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
1343};
1344
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001345class HInvoke : public HInstruction {
1346 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001347 HInvoke(ArenaAllocator* arena,
1348 uint32_t number_of_arguments,
1349 Primitive::Type return_type,
1350 uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001351 : HInstruction(SideEffects::All()),
1352 inputs_(arena, number_of_arguments),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001353 return_type_(return_type),
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001354 dex_pc_(dex_pc) {
1355 inputs_.SetSize(number_of_arguments);
1356 }
1357
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001358 virtual size_t InputCount() const { return inputs_.Size(); }
1359 virtual HInstruction* InputAt(size_t i) const { return inputs_.Get(i); }
1360
1361 // Runtime needs to walk the stack, so Dex -> Dex calls need to
1362 // know their environment.
1363 virtual bool NeedsEnvironment() const { return true; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001364
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001365 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001366 SetRawInputAt(index, argument);
1367 }
1368
1369 virtual void SetRawInputAt(size_t index, HInstruction* input) {
1370 inputs_.Put(index, input);
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001371 }
1372
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001373 virtual Primitive::Type GetType() const { return return_type_; }
1374
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001375 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001376
1377 protected:
1378 GrowableArray<HInstruction*> inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001379 const Primitive::Type return_type_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001380 const uint32_t dex_pc_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001381
1382 private:
1383 DISALLOW_COPY_AND_ASSIGN(HInvoke);
1384};
1385
1386class HInvokeStatic : public HInvoke {
1387 public:
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001388 HInvokeStatic(ArenaAllocator* arena,
1389 uint32_t number_of_arguments,
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001390 Primitive::Type return_type,
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001391 uint32_t dex_pc,
1392 uint32_t index_in_dex_cache)
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001393 : HInvoke(arena, number_of_arguments, return_type, dex_pc),
1394 index_in_dex_cache_(index_in_dex_cache) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001395
1396 uint32_t GetIndexInDexCache() const { return index_in_dex_cache_; }
1397
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001398 DECLARE_INSTRUCTION(InvokeStatic);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001399
1400 private:
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001401 const uint32_t index_in_dex_cache_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001402
1403 DISALLOW_COPY_AND_ASSIGN(HInvokeStatic);
1404};
1405
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001406class HInvokeVirtual : public HInvoke {
1407 public:
1408 HInvokeVirtual(ArenaAllocator* arena,
1409 uint32_t number_of_arguments,
1410 Primitive::Type return_type,
1411 uint32_t dex_pc,
1412 uint32_t vtable_index)
1413 : HInvoke(arena, number_of_arguments, return_type, dex_pc),
1414 vtable_index_(vtable_index) {}
1415
1416 uint32_t GetVTableIndex() const { return vtable_index_; }
1417
1418 DECLARE_INSTRUCTION(InvokeVirtual);
1419
1420 private:
1421 const uint32_t vtable_index_;
1422
1423 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
1424};
1425
Dave Allison20dfc792014-06-16 20:44:29 -07001426class HNewInstance : public HExpression<0> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001427 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001428 HNewInstance(uint32_t dex_pc, uint16_t type_index)
1429 : HExpression(Primitive::kPrimNot, SideEffects::None()),
1430 dex_pc_(dex_pc),
1431 type_index_(type_index) {}
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001432
1433 uint32_t GetDexPc() const { return dex_pc_; }
1434 uint16_t GetTypeIndex() const { return type_index_; }
1435
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001436 // Calls runtime so needs an environment.
1437 virtual bool NeedsEnvironment() const { return true; }
1438
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001439 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001440
1441 private:
1442 const uint32_t dex_pc_;
1443 const uint16_t type_index_;
1444
1445 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
1446};
1447
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001448class HAdd : public HBinaryOperation {
1449 public:
1450 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1451 : HBinaryOperation(result_type, left, right) {}
1452
1453 virtual bool IsCommutative() { return true; }
1454
Roland Levillain556c3d12014-09-18 15:25:07 +01001455 virtual int32_t Evaluate(int32_t x, int32_t y) const { return x + y; }
1456 virtual int64_t Evaluate(int64_t x, int64_t y) const { return x + y; }
1457
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001458 DECLARE_INSTRUCTION(Add);
1459
1460 private:
1461 DISALLOW_COPY_AND_ASSIGN(HAdd);
1462};
1463
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001464class HSub : public HBinaryOperation {
1465 public:
1466 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1467 : HBinaryOperation(result_type, left, right) {}
1468
1469 virtual bool IsCommutative() { return false; }
1470
Roland Levillain556c3d12014-09-18 15:25:07 +01001471 virtual int32_t Evaluate(int32_t x, int32_t y) const { return x + y; }
1472 virtual int64_t Evaluate(int64_t x, int64_t y) const { return x + y; }
1473
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001474 DECLARE_INSTRUCTION(Sub);
1475
1476 private:
1477 DISALLOW_COPY_AND_ASSIGN(HSub);
1478};
1479
1480// The value of a parameter in this method. Its location depends on
1481// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07001482class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001483 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001484 HParameterValue(uint8_t index, Primitive::Type parameter_type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001485 : HExpression(parameter_type, SideEffects::None()), index_(index) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001486
1487 uint8_t GetIndex() const { return index_; }
1488
1489 DECLARE_INSTRUCTION(ParameterValue);
1490
1491 private:
1492 // The index of this parameter in the parameters list. Must be less
1493 // than HGraph::number_of_in_vregs_;
1494 const uint8_t index_;
1495
1496 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
1497};
1498
Dave Allison20dfc792014-06-16 20:44:29 -07001499class HNot : public HExpression<1> {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001500 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001501 explicit HNot(HInstruction* input) : HExpression(Primitive::kPrimBoolean, SideEffects::None()) {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001502 SetRawInputAt(0, input);
1503 }
1504
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001505 virtual bool CanBeMoved() const { return true; }
1506 virtual bool InstructionDataEquals(HInstruction* other) const { return true; }
1507
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001508 DECLARE_INSTRUCTION(Not);
1509
1510 private:
1511 DISALLOW_COPY_AND_ASSIGN(HNot);
1512};
1513
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001514class HPhi : public HInstruction {
1515 public:
1516 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001517 : HInstruction(SideEffects::None()),
1518 inputs_(arena, number_of_inputs),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001519 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001520 type_(type),
1521 is_live_(false) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001522 inputs_.SetSize(number_of_inputs);
1523 }
1524
1525 virtual size_t InputCount() const { return inputs_.Size(); }
1526 virtual HInstruction* InputAt(size_t i) const { return inputs_.Get(i); }
1527
1528 virtual void SetRawInputAt(size_t index, HInstruction* input) {
1529 inputs_.Put(index, input);
1530 }
1531
1532 void AddInput(HInstruction* input);
1533
1534 virtual Primitive::Type GetType() const { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001535 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001536
1537 uint32_t GetRegNumber() const { return reg_number_; }
1538
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001539 void SetDead() { is_live_ = false; }
1540 void SetLive() { is_live_ = true; }
1541 bool IsDead() const { return !is_live_; }
1542 bool IsLive() const { return is_live_; }
1543
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001544 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001545
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001546 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001547 GrowableArray<HInstruction*> inputs_;
1548 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001549 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001550 bool is_live_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001551
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001552 DISALLOW_COPY_AND_ASSIGN(HPhi);
1553};
1554
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001555class HNullCheck : public HExpression<1> {
1556 public:
1557 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001558 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001559 SetRawInputAt(0, value);
1560 }
1561
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001562 virtual bool CanBeMoved() const { return true; }
1563 virtual bool InstructionDataEquals(HInstruction* other) const { return true; }
1564
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001565 virtual bool NeedsEnvironment() const { return true; }
1566
1567 uint32_t GetDexPc() const { return dex_pc_; }
1568
1569 DECLARE_INSTRUCTION(NullCheck);
1570
1571 private:
1572 const uint32_t dex_pc_;
1573
1574 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
1575};
1576
1577class FieldInfo : public ValueObject {
1578 public:
Nicolas Geoffray39468442014-09-02 15:17:15 +01001579 explicit FieldInfo(MemberOffset field_offset, Primitive::Type field_type)
1580 : field_offset_(field_offset), field_type_(field_type) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001581
1582 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001583 Primitive::Type GetFieldType() const { return field_type_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001584
1585 private:
1586 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01001587 const Primitive::Type field_type_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001588};
1589
1590class HInstanceFieldGet : public HExpression<1> {
1591 public:
1592 HInstanceFieldGet(HInstruction* value,
1593 Primitive::Type field_type,
1594 MemberOffset field_offset)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001595 : HExpression(field_type, SideEffects::DependsOnSomething()),
1596 field_info_(field_offset, field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001597 SetRawInputAt(0, value);
1598 }
1599
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001600 virtual bool CanBeMoved() const { return true; }
1601 virtual bool InstructionDataEquals(HInstruction* other) const {
1602 size_t other_offset = other->AsInstanceFieldGet()->GetFieldOffset().SizeValue();
1603 return other_offset == GetFieldOffset().SizeValue();
1604 }
1605
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001606 virtual size_t ComputeHashCode() const {
1607 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
1608 }
1609
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001610 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001611 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001612
1613 DECLARE_INSTRUCTION(InstanceFieldGet);
1614
1615 private:
1616 const FieldInfo field_info_;
1617
1618 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
1619};
1620
1621class HInstanceFieldSet : public HTemplateInstruction<2> {
1622 public:
1623 HInstanceFieldSet(HInstruction* object,
1624 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01001625 Primitive::Type field_type,
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001626 MemberOffset field_offset)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001627 : HTemplateInstruction(SideEffects::ChangesSomething()),
1628 field_info_(field_offset, field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001629 SetRawInputAt(0, object);
1630 SetRawInputAt(1, value);
1631 }
1632
1633 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001634 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001635
1636 DECLARE_INSTRUCTION(InstanceFieldSet);
1637
1638 private:
1639 const FieldInfo field_info_;
1640
1641 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
1642};
1643
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001644class HArrayGet : public HExpression<2> {
1645 public:
1646 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001647 : HExpression(type, SideEffects::DependsOnSomething()) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001648 SetRawInputAt(0, array);
1649 SetRawInputAt(1, index);
1650 }
1651
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001652 virtual bool CanBeMoved() const { return true; }
1653 virtual bool InstructionDataEquals(HInstruction* other) const { return true; }
1654
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001655 DECLARE_INSTRUCTION(ArrayGet);
1656
1657 private:
1658 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
1659};
1660
1661class HArraySet : public HTemplateInstruction<3> {
1662 public:
1663 HArraySet(HInstruction* array,
1664 HInstruction* index,
1665 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01001666 Primitive::Type component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001667 uint32_t dex_pc)
1668 : HTemplateInstruction(SideEffects::ChangesSomething()),
1669 dex_pc_(dex_pc),
1670 component_type_(component_type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001671 SetRawInputAt(0, array);
1672 SetRawInputAt(1, index);
1673 SetRawInputAt(2, value);
1674 }
1675
1676 virtual bool NeedsEnvironment() const {
1677 // We currently always call a runtime method to catch array store
1678 // exceptions.
1679 return InputAt(2)->GetType() == Primitive::kPrimNot;
1680 }
1681
1682 uint32_t GetDexPc() const { return dex_pc_; }
1683
Nicolas Geoffray39468442014-09-02 15:17:15 +01001684 Primitive::Type GetComponentType() const { return component_type_; }
1685
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001686 DECLARE_INSTRUCTION(ArraySet);
1687
1688 private:
1689 const uint32_t dex_pc_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01001690 const Primitive::Type component_type_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001691
1692 DISALLOW_COPY_AND_ASSIGN(HArraySet);
1693};
1694
1695class HArrayLength : public HExpression<1> {
1696 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001697 explicit HArrayLength(HInstruction* array)
1698 : HExpression(Primitive::kPrimInt, SideEffects::None()) {
1699 // Note that arrays do not change length, so the instruction does not
1700 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001701 SetRawInputAt(0, array);
1702 }
1703
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001704 virtual bool CanBeMoved() const { return true; }
1705 virtual bool InstructionDataEquals(HInstruction* other) const { return true; }
1706
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001707 DECLARE_INSTRUCTION(ArrayLength);
1708
1709 private:
1710 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
1711};
1712
1713class HBoundsCheck : public HExpression<2> {
1714 public:
1715 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001716 : HExpression(index->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001717 DCHECK(index->GetType() == Primitive::kPrimInt);
1718 SetRawInputAt(0, index);
1719 SetRawInputAt(1, length);
1720 }
1721
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001722 virtual bool CanBeMoved() const { return true; }
1723 virtual bool InstructionDataEquals(HInstruction* other) const { return true; }
1724
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001725 virtual bool NeedsEnvironment() const { return true; }
1726
1727 uint32_t GetDexPc() const { return dex_pc_; }
1728
1729 DECLARE_INSTRUCTION(BoundsCheck);
1730
1731 private:
1732 const uint32_t dex_pc_;
1733
1734 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
1735};
1736
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001737/**
1738 * Some DEX instructions are folded into multiple HInstructions that need
1739 * to stay live until the last HInstruction. This class
1740 * is used as a marker for the baseline compiler to ensure its preceding
1741 * HInstruction stays live. `index` is the temporary number that is used
1742 * for knowing the stack offset where to store the instruction.
1743 */
1744class HTemporary : public HTemplateInstruction<0> {
1745 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001746 explicit HTemporary(size_t index) : HTemplateInstruction(SideEffects::None()), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001747
1748 size_t GetIndex() const { return index_; }
1749
1750 DECLARE_INSTRUCTION(Temporary);
1751
1752 private:
1753 const size_t index_;
1754
1755 DISALLOW_COPY_AND_ASSIGN(HTemporary);
1756};
1757
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001758class HSuspendCheck : public HTemplateInstruction<0> {
1759 public:
1760 explicit HSuspendCheck(uint32_t dex_pc)
1761 : HTemplateInstruction(SideEffects::ChangesSomething()), dex_pc_(dex_pc) {}
1762
1763 virtual bool NeedsEnvironment() const {
1764 return true;
1765 }
1766
1767 uint32_t GetDexPc() const { return dex_pc_; }
1768
1769 DECLARE_INSTRUCTION(SuspendCheck);
1770
1771 private:
1772 const uint32_t dex_pc_;
1773
1774 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
1775};
1776
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001777class MoveOperands : public ArenaObject {
1778 public:
1779 MoveOperands(Location source, Location destination)
1780 : source_(source), destination_(destination) {}
1781
1782 Location GetSource() const { return source_; }
1783 Location GetDestination() const { return destination_; }
1784
1785 void SetSource(Location value) { source_ = value; }
1786 void SetDestination(Location value) { destination_ = value; }
1787
1788 // The parallel move resolver marks moves as "in-progress" by clearing the
1789 // destination (but not the source).
1790 Location MarkPending() {
1791 DCHECK(!IsPending());
1792 Location dest = destination_;
1793 destination_ = Location::NoLocation();
1794 return dest;
1795 }
1796
1797 void ClearPending(Location dest) {
1798 DCHECK(IsPending());
1799 destination_ = dest;
1800 }
1801
1802 bool IsPending() const {
1803 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
1804 return destination_.IsInvalid() && !source_.IsInvalid();
1805 }
1806
1807 // True if this blocks a move from the given location.
1808 bool Blocks(Location loc) const {
1809 return !IsEliminated() && source_.Equals(loc);
1810 }
1811
1812 // A move is redundant if it's been eliminated, if its source and
1813 // destination are the same, or if its destination is unneeded.
1814 bool IsRedundant() const {
1815 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
1816 }
1817
1818 // We clear both operands to indicate move that's been eliminated.
1819 void Eliminate() {
1820 source_ = destination_ = Location::NoLocation();
1821 }
1822
1823 bool IsEliminated() const {
1824 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
1825 return source_.IsInvalid();
1826 }
1827
1828 private:
1829 Location source_;
1830 Location destination_;
1831
1832 DISALLOW_COPY_AND_ASSIGN(MoveOperands);
1833};
1834
1835static constexpr size_t kDefaultNumberOfMoves = 4;
1836
1837class HParallelMove : public HTemplateInstruction<0> {
1838 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001839 explicit HParallelMove(ArenaAllocator* arena)
1840 : HTemplateInstruction(SideEffects::None()), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001841
1842 void AddMove(MoveOperands* move) {
1843 moves_.Add(move);
1844 }
1845
1846 MoveOperands* MoveOperandsAt(size_t index) const {
1847 return moves_.Get(index);
1848 }
1849
1850 size_t NumMoves() const { return moves_.Size(); }
1851
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001852 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001853
1854 private:
1855 GrowableArray<MoveOperands*> moves_;
1856
1857 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
1858};
1859
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001860class HGraphVisitor : public ValueObject {
1861 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001862 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
1863 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001864
Dave Allison20dfc792014-06-16 20:44:29 -07001865 virtual void VisitInstruction(HInstruction* instruction) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001866 virtual void VisitBasicBlock(HBasicBlock* block);
1867
1868 void VisitInsertionOrder();
1869
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001870 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001871
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001872 // Visit functions for instruction classes.
1873#define DECLARE_VISIT_INSTRUCTION(name) \
1874 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
1875
1876 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
1877
1878#undef DECLARE_VISIT_INSTRUCTION
1879
1880 private:
1881 HGraph* graph_;
1882
1883 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
1884};
1885
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001886class HInsertionOrderIterator : public ValueObject {
1887 public:
1888 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
1889
1890 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
1891 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
1892 void Advance() { ++index_; }
1893
1894 private:
1895 const HGraph& graph_;
1896 size_t index_;
1897
1898 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
1899};
1900
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001901class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001902 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001903 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001904
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001905 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
1906 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001907 void Advance() { ++index_; }
1908
1909 private:
1910 const HGraph& graph_;
1911 size_t index_;
1912
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001913 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001914};
1915
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001916class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001917 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001918 explicit HPostOrderIterator(const HGraph& graph)
1919 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001920
1921 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001922 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001923 void Advance() { --index_; }
1924
1925 private:
1926 const HGraph& graph_;
1927 size_t index_;
1928
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001929 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001930};
1931
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001932} // namespace art
1933
1934#endif // ART_COMPILER_OPTIMIZING_NODES_H_