blob: af173c80872f7b8267bb163e7c5459ca0d1de830 [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) {
627 env_uses_ = new (block_->GetGraph()->GetArena()) HUseListNode<HEnvironment>(
628 user, index, env_uses_);
629 }
630
631 void RemoveUser(HInstruction* user, size_t index);
632
633 HUseListNode<HInstruction>* GetUses() const { return uses_; }
634 HUseListNode<HEnvironment>* GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000635
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100636 bool HasUses() const { return uses_ != nullptr || env_uses_ != nullptr; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100637 bool HasEnvironmentUses() const { return env_uses_ != nullptr; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000638
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100639 size_t NumberOfUses() const {
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100640 // TODO: Optimize this method if it is used outside of the HGraphVisualizer.
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100641 size_t result = 0;
642 HUseListNode<HInstruction>* current = uses_;
643 while (current != nullptr) {
644 current = current->GetTail();
645 ++result;
646 }
647 return result;
648 }
649
Roland Levillainccc07a92014-09-16 14:48:16 +0100650 // Does this instruction dominate `other_instruction`? Aborts if
651 // this instruction and `other_instruction` are both phis.
652 bool Dominates(HInstruction* other_instruction) const;
653
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000654 int GetId() const { return id_; }
655 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000656
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100657 int GetSsaIndex() const { return ssa_index_; }
658 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
659 bool HasSsaIndex() const { return ssa_index_ != -1; }
660
661 bool HasEnvironment() const { return environment_ != nullptr; }
662 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100663 void SetEnvironment(HEnvironment* environment) { environment_ = environment; }
664
Nicolas Geoffray39468442014-09-02 15:17:15 +0100665 // Returns the number of entries in the environment. Typically, that is the
666 // number of dex registers in a method. It could be more in case of inlining.
667 size_t EnvironmentSize() const;
668
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000669 LocationSummary* GetLocations() const { return locations_; }
670 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000671
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100672 void ReplaceWith(HInstruction* instruction);
673
Dave Allison20dfc792014-06-16 20:44:29 -0700674 bool HasOnlyOneUse() const {
675 return uses_ != nullptr && uses_->GetTail() == nullptr;
676 }
677
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000678#define INSTRUCTION_TYPE_CHECK(type) \
Roland Levillainccc07a92014-09-16 14:48:16 +0100679 bool Is##type() const { return (As##type() != nullptr); } \
680 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000681 virtual H##type* As##type() { return nullptr; }
682
683 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
684#undef INSTRUCTION_TYPE_CHECK
685
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100686 // Returns whether the instruction can be moved within the graph.
687 virtual bool CanBeMoved() const { return false; }
688
689 // Returns whether the two instructions are of the same kind.
690 virtual bool InstructionTypeEquals(HInstruction* other) const { return false; }
691
692 // Returns whether any data encoded in the two instructions is equal.
693 // This method does not look at the inputs. Both instructions must be
694 // of the same type, otherwise the method has undefined behavior.
695 virtual bool InstructionDataEquals(HInstruction* other) const { return false; }
696
697 // Returns whether two instructions are equal, that is:
698 // 1) They have the same type and contain the same data,
699 // 2) Their inputs are identical.
700 bool Equals(HInstruction* other) const;
701
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100702 virtual InstructionKind GetKind() const = 0;
703
704 virtual size_t ComputeHashCode() const {
705 size_t result = GetKind();
706 for (size_t i = 0, e = InputCount(); i < e; ++i) {
707 result = (result * 31) + InputAt(i)->GetId();
708 }
709 return result;
710 }
711
712 SideEffects GetSideEffects() const { return side_effects_; }
713
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100714 size_t GetLifetimePosition() const { return lifetime_position_; }
715 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
716 LiveInterval* GetLiveInterval() const { return live_interval_; }
717 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
718 bool HasLiveInterval() const { return live_interval_ != nullptr; }
719
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000720 private:
721 HInstruction* previous_;
722 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000723 HBasicBlock* block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000724
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000725 // An instruction gets an id when it is added to the graph.
726 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +0100727 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000728 int id_;
729
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100730 // When doing liveness analysis, instructions that have uses get an SSA index.
731 int ssa_index_;
732
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100733 // List of instructions that have this instruction as input.
734 HUseListNode<HInstruction>* uses_;
735
736 // List of environments that contain this instruction.
737 HUseListNode<HEnvironment>* env_uses_;
738
Nicolas Geoffray39468442014-09-02 15:17:15 +0100739 // The environment associated with this instruction. Not null if the instruction
740 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100741 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000742
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000743 // Set by the code generator.
744 LocationSummary* locations_;
745
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100746 // Set by the liveness analysis.
747 LiveInterval* live_interval_;
748
749 // Set by the liveness analysis, this is the position in a linear
750 // order of blocks where this instruction's live interval start.
751 size_t lifetime_position_;
752
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100753 const SideEffects side_effects_;
754
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000755 friend class HBasicBlock;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100756 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000757
758 DISALLOW_COPY_AND_ASSIGN(HInstruction);
759};
760
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100761template<typename T>
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000762class HUseIterator : public ValueObject {
763 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100764 explicit HUseIterator(HUseListNode<T>* uses) : current_(uses) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000765
766 bool Done() const { return current_ == nullptr; }
767
768 void Advance() {
769 DCHECK(!Done());
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000770 current_ = current_->GetTail();
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000771 }
772
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100773 HUseListNode<T>* Current() const {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000774 DCHECK(!Done());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100775 return current_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000776 }
777
778 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100779 HUseListNode<T>* current_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000780
781 friend class HValue;
782};
783
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100784// A HEnvironment object contains the values of virtual registers at a given location.
785class HEnvironment : public ArenaObject {
786 public:
787 HEnvironment(ArenaAllocator* arena, size_t number_of_vregs) : vregs_(arena, number_of_vregs) {
788 vregs_.SetSize(number_of_vregs);
789 for (size_t i = 0; i < number_of_vregs; i++) {
790 vregs_.Put(i, nullptr);
791 }
792 }
793
794 void Populate(const GrowableArray<HInstruction*>& env) {
795 for (size_t i = 0; i < env.Size(); i++) {
796 HInstruction* instruction = env.Get(i);
797 vregs_.Put(i, instruction);
798 if (instruction != nullptr) {
799 instruction->AddEnvUseAt(this, i);
800 }
801 }
802 }
803
804 void SetRawEnvAt(size_t index, HInstruction* instruction) {
805 vregs_.Put(index, instruction);
806 }
807
Nicolas Geoffray39468442014-09-02 15:17:15 +0100808 HInstruction* GetInstructionAt(size_t index) const {
809 return vregs_.Get(index);
810 }
811
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100812 GrowableArray<HInstruction*>* GetVRegs() {
813 return &vregs_;
814 }
815
Nicolas Geoffray39468442014-09-02 15:17:15 +0100816 size_t Size() const { return vregs_.Size(); }
817
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100818 private:
819 GrowableArray<HInstruction*> vregs_;
820
821 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
822};
823
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000824class HInputIterator : public ValueObject {
825 public:
Dave Allison20dfc792014-06-16 20:44:29 -0700826 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000827
828 bool Done() const { return index_ == instruction_->InputCount(); }
829 HInstruction* Current() const { return instruction_->InputAt(index_); }
830 void Advance() { index_++; }
831
832 private:
833 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100834 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000835
836 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
837};
838
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000839class HInstructionIterator : public ValueObject {
840 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100841 explicit HInstructionIterator(const HInstructionList& instructions)
842 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000843 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000844 }
845
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000846 bool Done() const { return instruction_ == nullptr; }
847 HInstruction* Current() const { return instruction_; }
848 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000849 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000850 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000851 }
852
853 private:
854 HInstruction* instruction_;
855 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100856
857 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000858};
859
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100860class HBackwardInstructionIterator : public ValueObject {
861 public:
862 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
863 : instruction_(instructions.last_instruction_) {
864 next_ = Done() ? nullptr : instruction_->GetPrevious();
865 }
866
867 bool Done() const { return instruction_ == nullptr; }
868 HInstruction* Current() const { return instruction_; }
869 void Advance() {
870 instruction_ = next_;
871 next_ = Done() ? nullptr : instruction_->GetPrevious();
872 }
873
874 private:
875 HInstruction* instruction_;
876 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100877
878 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100879};
880
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000881// An embedded container with N elements of type T. Used (with partial
882// specialization for N=0) because embedded arrays cannot have size 0.
883template<typename T, intptr_t N>
884class EmbeddedArray {
885 public:
Dave Allison20dfc792014-06-16 20:44:29 -0700886 EmbeddedArray() : elements_() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000887
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000888 intptr_t GetLength() const { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000889
890 const T& operator[](intptr_t i) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000891 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000892 return elements_[i];
893 }
894
895 T& operator[](intptr_t i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000896 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000897 return elements_[i];
898 }
899
900 const T& At(intptr_t i) const {
901 return (*this)[i];
902 }
903
904 void SetAt(intptr_t i, const T& val) {
905 (*this)[i] = val;
906 }
907
908 private:
909 T elements_[N];
910};
911
912template<typename T>
913class EmbeddedArray<T, 0> {
914 public:
915 intptr_t length() const { return 0; }
916 const T& operator[](intptr_t i) const {
917 LOG(FATAL) << "Unreachable";
918 static T sentinel = 0;
919 return sentinel;
920 }
921 T& operator[](intptr_t i) {
922 LOG(FATAL) << "Unreachable";
923 static T sentinel = 0;
924 return sentinel;
925 }
926};
927
928template<intptr_t N>
929class HTemplateInstruction: public HInstruction {
930 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100931 HTemplateInstruction<N>(SideEffects side_effects)
932 : HInstruction(side_effects), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -0700933 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000934
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100935 virtual size_t InputCount() const { return N; }
936 virtual HInstruction* InputAt(size_t i) const { return inputs_[i]; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000937
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000938 protected:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100939 virtual void SetRawInputAt(size_t i, HInstruction* instruction) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000940 inputs_[i] = instruction;
941 }
942
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000943 private:
944 EmbeddedArray<HInstruction*, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100945
946 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000947};
948
Dave Allison20dfc792014-06-16 20:44:29 -0700949template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100950class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -0700951 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100952 HExpression<N>(Primitive::Type type, SideEffects side_effects)
953 : HTemplateInstruction<N>(side_effects), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -0700954 virtual ~HExpression() {}
955
956 virtual Primitive::Type GetType() const { return type_; }
957
958 private:
959 const Primitive::Type type_;
960};
961
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000962// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
963// instruction that branches to the exit block.
964class HReturnVoid : public HTemplateInstruction<0> {
965 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100966 HReturnVoid() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100967
968 virtual bool IsControlFlow() const { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000969
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100970 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000971
972 private:
973 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
974};
975
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000976// Represents dex's RETURN opcodes. A HReturn is a control flow
977// instruction that branches to the exit block.
978class HReturn : public HTemplateInstruction<1> {
979 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100980 explicit HReturn(HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000981 SetRawInputAt(0, value);
982 }
983
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100984 virtual bool IsControlFlow() const { return true; }
985
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100986 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000987
988 private:
989 DISALLOW_COPY_AND_ASSIGN(HReturn);
990};
991
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000992// The exit instruction is the only instruction of the exit block.
993// Instructions aborting the method (HTrow and HReturn) must branch to the
994// exit block.
995class HExit : public HTemplateInstruction<0> {
996 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100997 HExit() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100998
999 virtual bool IsControlFlow() const { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001000
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001001 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001002
1003 private:
1004 DISALLOW_COPY_AND_ASSIGN(HExit);
1005};
1006
1007// Jumps from one block to another.
1008class HGoto : public HTemplateInstruction<0> {
1009 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001010 HGoto() : HTemplateInstruction(SideEffects::None()) {}
1011
1012 virtual bool IsControlFlow() const { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001013
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001014 HBasicBlock* GetSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001015 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001016 }
1017
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001018 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001019
1020 private:
1021 DISALLOW_COPY_AND_ASSIGN(HGoto);
1022};
1023
Dave Allison20dfc792014-06-16 20:44:29 -07001024
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001025// Conditional branch. A block ending with an HIf instruction must have
1026// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001027class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001028 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001029 explicit HIf(HInstruction* input) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001030 SetRawInputAt(0, input);
1031 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001032
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001033 virtual bool IsControlFlow() const { return true; }
1034
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001035 HBasicBlock* IfTrueSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001036 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001037 }
1038
1039 HBasicBlock* IfFalseSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001040 return GetBlock()->GetSuccessors().Get(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001041 }
1042
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001043 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001044
Dave Allison20dfc792014-06-16 20:44:29 -07001045 virtual bool IsIfInstruction() const { return true; }
1046
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001047 private:
1048 DISALLOW_COPY_AND_ASSIGN(HIf);
1049};
1050
Dave Allison20dfc792014-06-16 20:44:29 -07001051class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001052 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001053 HBinaryOperation(Primitive::Type result_type,
1054 HInstruction* left,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001055 HInstruction* right) : HExpression(result_type, SideEffects::None()) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001056 SetRawInputAt(0, left);
1057 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001058 }
1059
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001060 HInstruction* GetLeft() const { return InputAt(0); }
1061 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07001062 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001063
1064 virtual bool IsCommutative() { return false; }
1065
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001066 virtual bool CanBeMoved() const { return true; }
1067 virtual bool InstructionDataEquals(HInstruction* other) const { return true; }
1068
Roland Levillain556c3d12014-09-18 15:25:07 +01001069 // Try to statically evaluate `operation` and return an HConstant
1070 // containing the result of this evaluation. If `operation` cannot
1071 // be evaluated as a constant, return nullptr.
1072 HConstant* TryStaticEvaluation(ArenaAllocator* allocator) const;
1073
1074 // Apply this operation to `x` and `y`.
1075 virtual int32_t Evaluate(int32_t x, int32_t y) const = 0;
1076 virtual int64_t Evaluate(int64_t x, int64_t y) const = 0;
1077
Roland Levillainccc07a92014-09-16 14:48:16 +01001078 DECLARE_INSTRUCTION(BinaryOperation);
1079
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001080 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001081 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
1082};
1083
Dave Allison20dfc792014-06-16 20:44:29 -07001084class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001085 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001086 HCondition(HInstruction* first, HInstruction* second)
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001087 : HBinaryOperation(Primitive::kPrimBoolean, first, second) {}
1088
1089 virtual bool IsCommutative() { return true; }
Dave Allison20dfc792014-06-16 20:44:29 -07001090 bool NeedsMaterialization() const;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001091
Dave Allison20dfc792014-06-16 20:44:29 -07001092 DECLARE_INSTRUCTION(Condition);
1093
1094 virtual IfCondition GetCondition() const = 0;
1095
1096 private:
1097 DISALLOW_COPY_AND_ASSIGN(HCondition);
1098};
1099
1100// Instruction to check if two inputs are equal to each other.
1101class HEqual : public HCondition {
1102 public:
1103 HEqual(HInstruction* first, HInstruction* second)
1104 : HCondition(first, second) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001105
Roland Levillain556c3d12014-09-18 15:25:07 +01001106 virtual int32_t Evaluate(int32_t x, int32_t y) const { return x == y; }
1107 virtual int64_t Evaluate(int64_t x, int64_t y) const { return x == y; }
1108
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001109 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001110
Dave Allison20dfc792014-06-16 20:44:29 -07001111 virtual IfCondition GetCondition() const {
1112 return kCondEQ;
1113 }
1114
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001115 private:
1116 DISALLOW_COPY_AND_ASSIGN(HEqual);
1117};
1118
Dave Allison20dfc792014-06-16 20:44:29 -07001119class HNotEqual : public HCondition {
1120 public:
1121 HNotEqual(HInstruction* first, HInstruction* second)
1122 : HCondition(first, second) {}
1123
Roland Levillain556c3d12014-09-18 15:25:07 +01001124 virtual int32_t Evaluate(int32_t x, int32_t y) const { return x != y; }
1125 virtual int64_t Evaluate(int64_t x, int64_t y) const { return x != y; }
1126
Dave Allison20dfc792014-06-16 20:44:29 -07001127 DECLARE_INSTRUCTION(NotEqual);
1128
1129 virtual IfCondition GetCondition() const {
1130 return kCondNE;
1131 }
1132
1133 private:
1134 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
1135};
1136
1137class HLessThan : public HCondition {
1138 public:
1139 HLessThan(HInstruction* first, HInstruction* second)
1140 : HCondition(first, second) {}
1141
Roland Levillain556c3d12014-09-18 15:25:07 +01001142 virtual int32_t Evaluate(int32_t x, int32_t y) const { return x < y; }
1143 virtual int64_t Evaluate(int64_t x, int64_t y) const { return x < y; }
1144
Dave Allison20dfc792014-06-16 20:44:29 -07001145 DECLARE_INSTRUCTION(LessThan);
1146
1147 virtual IfCondition GetCondition() const {
1148 return kCondLT;
1149 }
1150
1151 private:
1152 DISALLOW_COPY_AND_ASSIGN(HLessThan);
1153};
1154
1155class HLessThanOrEqual : public HCondition {
1156 public:
1157 HLessThanOrEqual(HInstruction* first, HInstruction* second)
1158 : HCondition(first, second) {}
1159
Roland Levillain556c3d12014-09-18 15:25:07 +01001160 virtual int32_t Evaluate(int32_t x, int32_t y) const { return x <= y; }
1161 virtual int64_t Evaluate(int64_t x, int64_t y) const { return x <= y; }
1162
Dave Allison20dfc792014-06-16 20:44:29 -07001163 DECLARE_INSTRUCTION(LessThanOrEqual);
1164
1165 virtual IfCondition GetCondition() const {
1166 return kCondLE;
1167 }
1168
1169 private:
1170 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
1171};
1172
1173class HGreaterThan : public HCondition {
1174 public:
1175 HGreaterThan(HInstruction* first, HInstruction* second)
1176 : HCondition(first, second) {}
1177
Roland Levillain556c3d12014-09-18 15:25:07 +01001178 virtual int32_t Evaluate(int32_t x, int32_t y) const { return x > y; }
1179 virtual int64_t Evaluate(int64_t x, int64_t y) const { return x > y; }
1180
Dave Allison20dfc792014-06-16 20:44:29 -07001181 DECLARE_INSTRUCTION(GreaterThan);
1182
1183 virtual IfCondition GetCondition() const {
1184 return kCondGT;
1185 }
1186
1187 private:
1188 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
1189};
1190
1191class HGreaterThanOrEqual : public HCondition {
1192 public:
1193 HGreaterThanOrEqual(HInstruction* first, HInstruction* second)
1194 : HCondition(first, second) {}
1195
Roland Levillain556c3d12014-09-18 15:25:07 +01001196 virtual int32_t Evaluate(int32_t x, int32_t y) const { return x >= y; }
1197 virtual int64_t Evaluate(int64_t x, int64_t y) const { return x >= y; }
1198
Dave Allison20dfc792014-06-16 20:44:29 -07001199 DECLARE_INSTRUCTION(GreaterThanOrEqual);
1200
1201 virtual IfCondition GetCondition() const {
1202 return kCondGE;
1203 }
1204
1205 private:
1206 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
1207};
1208
1209
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001210// Instruction to check how two inputs compare to each other.
1211// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
1212class HCompare : public HBinaryOperation {
1213 public:
1214 HCompare(Primitive::Type type, HInstruction* first, HInstruction* second)
1215 : HBinaryOperation(Primitive::kPrimInt, first, second) {
1216 DCHECK_EQ(type, first->GetType());
1217 DCHECK_EQ(type, second->GetType());
1218 }
1219
Roland Levillain556c3d12014-09-18 15:25:07 +01001220 virtual int32_t Evaluate(int32_t x, int32_t y) const {
1221 return
1222 x == y ? 0 :
1223 x > y ? 1 :
1224 -1;
1225 }
1226 virtual int64_t Evaluate(int64_t x, int64_t y) const {
1227 return
1228 x == y ? 0 :
1229 x > y ? 1 :
1230 -1;
1231 }
1232
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001233 DECLARE_INSTRUCTION(Compare);
1234
1235 private:
1236 DISALLOW_COPY_AND_ASSIGN(HCompare);
1237};
1238
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001239// A local in the graph. Corresponds to a Dex register.
1240class HLocal : public HTemplateInstruction<0> {
1241 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001242 explicit HLocal(uint16_t reg_number)
1243 : HTemplateInstruction(SideEffects::None()), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001244
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001245 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001246
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001247 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001248
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001249 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001250 // The Dex register number.
1251 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001252
1253 DISALLOW_COPY_AND_ASSIGN(HLocal);
1254};
1255
1256// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07001257class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001258 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001259 explicit HLoadLocal(HLocal* local, Primitive::Type type)
1260 : HExpression(type, SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001261 SetRawInputAt(0, local);
1262 }
1263
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001264 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1265
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001266 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001267
1268 private:
1269 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
1270};
1271
1272// Store a value in a given local. This instruction has two inputs: the value
1273// and the local.
1274class HStoreLocal : public HTemplateInstruction<2> {
1275 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001276 HStoreLocal(HLocal* local, HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001277 SetRawInputAt(0, local);
1278 SetRawInputAt(1, value);
1279 }
1280
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001281 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1282
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001283 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001284
1285 private:
1286 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
1287};
1288
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001289class HConstant : public HExpression<0> {
1290 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001291 explicit HConstant(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
1292
1293 virtual bool CanBeMoved() const { return true; }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001294
1295 DECLARE_INSTRUCTION(Constant);
1296
1297 private:
1298 DISALLOW_COPY_AND_ASSIGN(HConstant);
1299};
1300
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001301// Constants of the type int. Those can be from Dex instructions, or
1302// synthesized (for example with the if-eqz instruction).
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001303class HIntConstant : public HConstant {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001304 public:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001305 explicit HIntConstant(int32_t value) : HConstant(Primitive::kPrimInt), value_(value) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001306
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001307 int32_t GetValue() const { return value_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001308
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001309 virtual bool InstructionDataEquals(HInstruction* other) const {
1310 return other->AsIntConstant()->value_ == value_;
1311 }
1312
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001313 virtual size_t ComputeHashCode() const { return GetValue(); }
1314
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001315 DECLARE_INSTRUCTION(IntConstant);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001316
1317 private:
1318 const int32_t value_;
1319
1320 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
1321};
1322
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001323class HLongConstant : public HConstant {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001324 public:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001325 explicit HLongConstant(int64_t value) : HConstant(Primitive::kPrimLong), value_(value) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001326
1327 int64_t GetValue() const { return value_; }
1328
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001329 virtual bool InstructionDataEquals(HInstruction* other) const {
1330 return other->AsLongConstant()->value_ == value_;
1331 }
1332
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001333 virtual size_t ComputeHashCode() const { return static_cast<size_t>(GetValue()); }
1334
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001335 DECLARE_INSTRUCTION(LongConstant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001336
1337 private:
1338 const int64_t value_;
1339
1340 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
1341};
1342
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001343class HInvoke : public HInstruction {
1344 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001345 HInvoke(ArenaAllocator* arena,
1346 uint32_t number_of_arguments,
1347 Primitive::Type return_type,
1348 uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001349 : HInstruction(SideEffects::All()),
1350 inputs_(arena, number_of_arguments),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001351 return_type_(return_type),
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001352 dex_pc_(dex_pc) {
1353 inputs_.SetSize(number_of_arguments);
1354 }
1355
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001356 virtual size_t InputCount() const { return inputs_.Size(); }
1357 virtual HInstruction* InputAt(size_t i) const { return inputs_.Get(i); }
1358
1359 // Runtime needs to walk the stack, so Dex -> Dex calls need to
1360 // know their environment.
1361 virtual bool NeedsEnvironment() const { return true; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001362
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001363 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001364 SetRawInputAt(index, argument);
1365 }
1366
1367 virtual void SetRawInputAt(size_t index, HInstruction* input) {
1368 inputs_.Put(index, input);
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001369 }
1370
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001371 virtual Primitive::Type GetType() const { return return_type_; }
1372
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001373 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001374
1375 protected:
1376 GrowableArray<HInstruction*> inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001377 const Primitive::Type return_type_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001378 const uint32_t dex_pc_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001379
1380 private:
1381 DISALLOW_COPY_AND_ASSIGN(HInvoke);
1382};
1383
1384class HInvokeStatic : public HInvoke {
1385 public:
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001386 HInvokeStatic(ArenaAllocator* arena,
1387 uint32_t number_of_arguments,
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001388 Primitive::Type return_type,
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001389 uint32_t dex_pc,
1390 uint32_t index_in_dex_cache)
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001391 : HInvoke(arena, number_of_arguments, return_type, dex_pc),
1392 index_in_dex_cache_(index_in_dex_cache) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001393
1394 uint32_t GetIndexInDexCache() const { return index_in_dex_cache_; }
1395
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001396 DECLARE_INSTRUCTION(InvokeStatic);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001397
1398 private:
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001399 const uint32_t index_in_dex_cache_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001400
1401 DISALLOW_COPY_AND_ASSIGN(HInvokeStatic);
1402};
1403
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001404class HInvokeVirtual : public HInvoke {
1405 public:
1406 HInvokeVirtual(ArenaAllocator* arena,
1407 uint32_t number_of_arguments,
1408 Primitive::Type return_type,
1409 uint32_t dex_pc,
1410 uint32_t vtable_index)
1411 : HInvoke(arena, number_of_arguments, return_type, dex_pc),
1412 vtable_index_(vtable_index) {}
1413
1414 uint32_t GetVTableIndex() const { return vtable_index_; }
1415
1416 DECLARE_INSTRUCTION(InvokeVirtual);
1417
1418 private:
1419 const uint32_t vtable_index_;
1420
1421 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
1422};
1423
Dave Allison20dfc792014-06-16 20:44:29 -07001424class HNewInstance : public HExpression<0> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001425 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001426 HNewInstance(uint32_t dex_pc, uint16_t type_index)
1427 : HExpression(Primitive::kPrimNot, SideEffects::None()),
1428 dex_pc_(dex_pc),
1429 type_index_(type_index) {}
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001430
1431 uint32_t GetDexPc() const { return dex_pc_; }
1432 uint16_t GetTypeIndex() const { return type_index_; }
1433
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001434 // Calls runtime so needs an environment.
1435 virtual bool NeedsEnvironment() const { return true; }
1436
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001437 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001438
1439 private:
1440 const uint32_t dex_pc_;
1441 const uint16_t type_index_;
1442
1443 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
1444};
1445
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001446class HAdd : public HBinaryOperation {
1447 public:
1448 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1449 : HBinaryOperation(result_type, left, right) {}
1450
1451 virtual bool IsCommutative() { return true; }
1452
Roland Levillain556c3d12014-09-18 15:25:07 +01001453 virtual int32_t Evaluate(int32_t x, int32_t y) const { return x + y; }
1454 virtual int64_t Evaluate(int64_t x, int64_t y) const { return x + y; }
1455
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001456 DECLARE_INSTRUCTION(Add);
1457
1458 private:
1459 DISALLOW_COPY_AND_ASSIGN(HAdd);
1460};
1461
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001462class HSub : public HBinaryOperation {
1463 public:
1464 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1465 : HBinaryOperation(result_type, left, right) {}
1466
1467 virtual bool IsCommutative() { return false; }
1468
Roland Levillain556c3d12014-09-18 15:25:07 +01001469 virtual int32_t Evaluate(int32_t x, int32_t y) const { return x + y; }
1470 virtual int64_t Evaluate(int64_t x, int64_t y) const { return x + y; }
1471
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001472 DECLARE_INSTRUCTION(Sub);
1473
1474 private:
1475 DISALLOW_COPY_AND_ASSIGN(HSub);
1476};
1477
1478// The value of a parameter in this method. Its location depends on
1479// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07001480class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001481 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001482 HParameterValue(uint8_t index, Primitive::Type parameter_type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001483 : HExpression(parameter_type, SideEffects::None()), index_(index) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001484
1485 uint8_t GetIndex() const { return index_; }
1486
1487 DECLARE_INSTRUCTION(ParameterValue);
1488
1489 private:
1490 // The index of this parameter in the parameters list. Must be less
1491 // than HGraph::number_of_in_vregs_;
1492 const uint8_t index_;
1493
1494 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
1495};
1496
Dave Allison20dfc792014-06-16 20:44:29 -07001497class HNot : public HExpression<1> {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001498 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001499 explicit HNot(HInstruction* input) : HExpression(Primitive::kPrimBoolean, SideEffects::None()) {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001500 SetRawInputAt(0, input);
1501 }
1502
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001503 virtual bool CanBeMoved() const { return true; }
1504 virtual bool InstructionDataEquals(HInstruction* other) const { return true; }
1505
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001506 DECLARE_INSTRUCTION(Not);
1507
1508 private:
1509 DISALLOW_COPY_AND_ASSIGN(HNot);
1510};
1511
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001512class HPhi : public HInstruction {
1513 public:
1514 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001515 : HInstruction(SideEffects::None()),
1516 inputs_(arena, number_of_inputs),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001517 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001518 type_(type),
1519 is_live_(false) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001520 inputs_.SetSize(number_of_inputs);
1521 }
1522
1523 virtual size_t InputCount() const { return inputs_.Size(); }
1524 virtual HInstruction* InputAt(size_t i) const { return inputs_.Get(i); }
1525
1526 virtual void SetRawInputAt(size_t index, HInstruction* input) {
1527 inputs_.Put(index, input);
1528 }
1529
1530 void AddInput(HInstruction* input);
1531
1532 virtual Primitive::Type GetType() const { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001533 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001534
1535 uint32_t GetRegNumber() const { return reg_number_; }
1536
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001537 void SetDead() { is_live_ = false; }
1538 void SetLive() { is_live_ = true; }
1539 bool IsDead() const { return !is_live_; }
1540 bool IsLive() const { return is_live_; }
1541
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001542 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001543
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001544 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001545 GrowableArray<HInstruction*> inputs_;
1546 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001547 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001548 bool is_live_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001549
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001550 DISALLOW_COPY_AND_ASSIGN(HPhi);
1551};
1552
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001553class HNullCheck : public HExpression<1> {
1554 public:
1555 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001556 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001557 SetRawInputAt(0, value);
1558 }
1559
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001560 virtual bool CanBeMoved() const { return true; }
1561 virtual bool InstructionDataEquals(HInstruction* other) const { return true; }
1562
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001563 virtual bool NeedsEnvironment() const { return true; }
1564
1565 uint32_t GetDexPc() const { return dex_pc_; }
1566
1567 DECLARE_INSTRUCTION(NullCheck);
1568
1569 private:
1570 const uint32_t dex_pc_;
1571
1572 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
1573};
1574
1575class FieldInfo : public ValueObject {
1576 public:
Nicolas Geoffray39468442014-09-02 15:17:15 +01001577 explicit FieldInfo(MemberOffset field_offset, Primitive::Type field_type)
1578 : field_offset_(field_offset), field_type_(field_type) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001579
1580 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001581 Primitive::Type GetFieldType() const { return field_type_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001582
1583 private:
1584 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01001585 const Primitive::Type field_type_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001586};
1587
1588class HInstanceFieldGet : public HExpression<1> {
1589 public:
1590 HInstanceFieldGet(HInstruction* value,
1591 Primitive::Type field_type,
1592 MemberOffset field_offset)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001593 : HExpression(field_type, SideEffects::DependsOnSomething()),
1594 field_info_(field_offset, field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001595 SetRawInputAt(0, value);
1596 }
1597
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001598 virtual bool CanBeMoved() const { return true; }
1599 virtual bool InstructionDataEquals(HInstruction* other) const {
1600 size_t other_offset = other->AsInstanceFieldGet()->GetFieldOffset().SizeValue();
1601 return other_offset == GetFieldOffset().SizeValue();
1602 }
1603
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001604 virtual size_t ComputeHashCode() const {
1605 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
1606 }
1607
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001608 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001609 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001610
1611 DECLARE_INSTRUCTION(InstanceFieldGet);
1612
1613 private:
1614 const FieldInfo field_info_;
1615
1616 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
1617};
1618
1619class HInstanceFieldSet : public HTemplateInstruction<2> {
1620 public:
1621 HInstanceFieldSet(HInstruction* object,
1622 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01001623 Primitive::Type field_type,
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001624 MemberOffset field_offset)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001625 : HTemplateInstruction(SideEffects::ChangesSomething()),
1626 field_info_(field_offset, field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001627 SetRawInputAt(0, object);
1628 SetRawInputAt(1, value);
1629 }
1630
1631 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001632 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001633
1634 DECLARE_INSTRUCTION(InstanceFieldSet);
1635
1636 private:
1637 const FieldInfo field_info_;
1638
1639 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
1640};
1641
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001642class HArrayGet : public HExpression<2> {
1643 public:
1644 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001645 : HExpression(type, SideEffects::DependsOnSomething()) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001646 SetRawInputAt(0, array);
1647 SetRawInputAt(1, index);
1648 }
1649
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001650 virtual bool CanBeMoved() const { return true; }
1651 virtual bool InstructionDataEquals(HInstruction* other) const { return true; }
1652
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001653 DECLARE_INSTRUCTION(ArrayGet);
1654
1655 private:
1656 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
1657};
1658
1659class HArraySet : public HTemplateInstruction<3> {
1660 public:
1661 HArraySet(HInstruction* array,
1662 HInstruction* index,
1663 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01001664 Primitive::Type component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001665 uint32_t dex_pc)
1666 : HTemplateInstruction(SideEffects::ChangesSomething()),
1667 dex_pc_(dex_pc),
1668 component_type_(component_type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001669 SetRawInputAt(0, array);
1670 SetRawInputAt(1, index);
1671 SetRawInputAt(2, value);
1672 }
1673
1674 virtual bool NeedsEnvironment() const {
1675 // We currently always call a runtime method to catch array store
1676 // exceptions.
1677 return InputAt(2)->GetType() == Primitive::kPrimNot;
1678 }
1679
1680 uint32_t GetDexPc() const { return dex_pc_; }
1681
Nicolas Geoffray39468442014-09-02 15:17:15 +01001682 Primitive::Type GetComponentType() const { return component_type_; }
1683
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001684 DECLARE_INSTRUCTION(ArraySet);
1685
1686 private:
1687 const uint32_t dex_pc_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01001688 const Primitive::Type component_type_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001689
1690 DISALLOW_COPY_AND_ASSIGN(HArraySet);
1691};
1692
1693class HArrayLength : public HExpression<1> {
1694 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001695 explicit HArrayLength(HInstruction* array)
1696 : HExpression(Primitive::kPrimInt, SideEffects::None()) {
1697 // Note that arrays do not change length, so the instruction does not
1698 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001699 SetRawInputAt(0, array);
1700 }
1701
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001702 virtual bool CanBeMoved() const { return true; }
1703 virtual bool InstructionDataEquals(HInstruction* other) const { return true; }
1704
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001705 DECLARE_INSTRUCTION(ArrayLength);
1706
1707 private:
1708 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
1709};
1710
1711class HBoundsCheck : public HExpression<2> {
1712 public:
1713 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001714 : HExpression(index->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001715 DCHECK(index->GetType() == Primitive::kPrimInt);
1716 SetRawInputAt(0, index);
1717 SetRawInputAt(1, length);
1718 }
1719
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001720 virtual bool CanBeMoved() const { return true; }
1721 virtual bool InstructionDataEquals(HInstruction* other) const { return true; }
1722
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001723 virtual bool NeedsEnvironment() const { return true; }
1724
1725 uint32_t GetDexPc() const { return dex_pc_; }
1726
1727 DECLARE_INSTRUCTION(BoundsCheck);
1728
1729 private:
1730 const uint32_t dex_pc_;
1731
1732 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
1733};
1734
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001735/**
1736 * Some DEX instructions are folded into multiple HInstructions that need
1737 * to stay live until the last HInstruction. This class
1738 * is used as a marker for the baseline compiler to ensure its preceding
1739 * HInstruction stays live. `index` is the temporary number that is used
1740 * for knowing the stack offset where to store the instruction.
1741 */
1742class HTemporary : public HTemplateInstruction<0> {
1743 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001744 explicit HTemporary(size_t index) : HTemplateInstruction(SideEffects::None()), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001745
1746 size_t GetIndex() const { return index_; }
1747
1748 DECLARE_INSTRUCTION(Temporary);
1749
1750 private:
1751 const size_t index_;
1752
1753 DISALLOW_COPY_AND_ASSIGN(HTemporary);
1754};
1755
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001756class HSuspendCheck : public HTemplateInstruction<0> {
1757 public:
1758 explicit HSuspendCheck(uint32_t dex_pc)
1759 : HTemplateInstruction(SideEffects::ChangesSomething()), dex_pc_(dex_pc) {}
1760
1761 virtual bool NeedsEnvironment() const {
1762 return true;
1763 }
1764
1765 uint32_t GetDexPc() const { return dex_pc_; }
1766
1767 DECLARE_INSTRUCTION(SuspendCheck);
1768
1769 private:
1770 const uint32_t dex_pc_;
1771
1772 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
1773};
1774
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001775class MoveOperands : public ArenaObject {
1776 public:
1777 MoveOperands(Location source, Location destination)
1778 : source_(source), destination_(destination) {}
1779
1780 Location GetSource() const { return source_; }
1781 Location GetDestination() const { return destination_; }
1782
1783 void SetSource(Location value) { source_ = value; }
1784 void SetDestination(Location value) { destination_ = value; }
1785
1786 // The parallel move resolver marks moves as "in-progress" by clearing the
1787 // destination (but not the source).
1788 Location MarkPending() {
1789 DCHECK(!IsPending());
1790 Location dest = destination_;
1791 destination_ = Location::NoLocation();
1792 return dest;
1793 }
1794
1795 void ClearPending(Location dest) {
1796 DCHECK(IsPending());
1797 destination_ = dest;
1798 }
1799
1800 bool IsPending() const {
1801 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
1802 return destination_.IsInvalid() && !source_.IsInvalid();
1803 }
1804
1805 // True if this blocks a move from the given location.
1806 bool Blocks(Location loc) const {
1807 return !IsEliminated() && source_.Equals(loc);
1808 }
1809
1810 // A move is redundant if it's been eliminated, if its source and
1811 // destination are the same, or if its destination is unneeded.
1812 bool IsRedundant() const {
1813 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
1814 }
1815
1816 // We clear both operands to indicate move that's been eliminated.
1817 void Eliminate() {
1818 source_ = destination_ = Location::NoLocation();
1819 }
1820
1821 bool IsEliminated() const {
1822 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
1823 return source_.IsInvalid();
1824 }
1825
1826 private:
1827 Location source_;
1828 Location destination_;
1829
1830 DISALLOW_COPY_AND_ASSIGN(MoveOperands);
1831};
1832
1833static constexpr size_t kDefaultNumberOfMoves = 4;
1834
1835class HParallelMove : public HTemplateInstruction<0> {
1836 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001837 explicit HParallelMove(ArenaAllocator* arena)
1838 : HTemplateInstruction(SideEffects::None()), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001839
1840 void AddMove(MoveOperands* move) {
1841 moves_.Add(move);
1842 }
1843
1844 MoveOperands* MoveOperandsAt(size_t index) const {
1845 return moves_.Get(index);
1846 }
1847
1848 size_t NumMoves() const { return moves_.Size(); }
1849
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001850 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001851
1852 private:
1853 GrowableArray<MoveOperands*> moves_;
1854
1855 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
1856};
1857
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001858class HGraphVisitor : public ValueObject {
1859 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001860 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
1861 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001862
Dave Allison20dfc792014-06-16 20:44:29 -07001863 virtual void VisitInstruction(HInstruction* instruction) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001864 virtual void VisitBasicBlock(HBasicBlock* block);
1865
1866 void VisitInsertionOrder();
1867
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001868 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001869
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001870 // Visit functions for instruction classes.
1871#define DECLARE_VISIT_INSTRUCTION(name) \
1872 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
1873
1874 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
1875
1876#undef DECLARE_VISIT_INSTRUCTION
1877
1878 private:
1879 HGraph* graph_;
1880
1881 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
1882};
1883
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001884class HInsertionOrderIterator : public ValueObject {
1885 public:
1886 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
1887
1888 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
1889 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
1890 void Advance() { ++index_; }
1891
1892 private:
1893 const HGraph& graph_;
1894 size_t index_;
1895
1896 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
1897};
1898
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001899class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001900 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001901 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001902
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001903 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
1904 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001905 void Advance() { ++index_; }
1906
1907 private:
1908 const HGraph& graph_;
1909 size_t index_;
1910
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001911 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001912};
1913
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001914class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001915 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001916 explicit HPostOrderIterator(const HGraph& graph)
1917 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001918
1919 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001920 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001921 void Advance() { --index_; }
1922
1923 private:
1924 const HGraph& graph_;
1925 size_t index_;
1926
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001927 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001928};
1929
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001930} // namespace art
1931
1932#endif // ART_COMPILER_OPTIMIZING_NODES_H_