blob: 2dab605465a8895a84df86ebb55dad4d7ded33bd [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"
Ian Rogers0279ebb2014-10-08 17:27:48 -070023#include "utils/arena_object.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 Geoffray3c049742014-09-24 18:10:46 +010035class HSuspendCheck;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010036class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000037class LocationSummary;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000038
39static const int kDefaultNumberOfBlocks = 8;
40static const int kDefaultNumberOfSuccessors = 2;
41static const int kDefaultNumberOfPredecessors = 2;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010042static const int kDefaultNumberOfDominatedBlocks = 1;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000043static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000044
Dave Allison20dfc792014-06-16 20:44:29 -070045enum IfCondition {
46 kCondEQ,
47 kCondNE,
48 kCondLT,
49 kCondLE,
50 kCondGT,
51 kCondGE,
52};
53
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010054class HInstructionList {
55 public:
56 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
57
58 void AddInstruction(HInstruction* instruction);
59 void RemoveInstruction(HInstruction* instruction);
60
Roland Levillain6b469232014-09-25 10:10:38 +010061 // Return true if this list contains `instruction`.
62 bool Contains(HInstruction* instruction) const;
63
Roland Levillainccc07a92014-09-16 14:48:16 +010064 // Return true if `instruction1` is found before `instruction2` in
65 // this instruction list and false otherwise. Abort if none
66 // of these instructions is found.
67 bool FoundBefore(const HInstruction* instruction1,
68 const HInstruction* instruction2) const;
69
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010070 private:
71 HInstruction* first_instruction_;
72 HInstruction* last_instruction_;
73
74 friend class HBasicBlock;
75 friend class HInstructionIterator;
Nicolas Geoffray804d0932014-05-02 08:46:00 +010076 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010077
78 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
79};
80
Nicolas Geoffray818f2102014-02-18 16:43:35 +000081// Control-flow graph of a method. Contains a list of basic blocks.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070082class HGraph : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +000083 public:
84 explicit HGraph(ArenaAllocator* arena)
85 : arena_(arena),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000086 blocks_(arena, kDefaultNumberOfBlocks),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +010087 reverse_post_order_(arena, kDefaultNumberOfBlocks),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070088 entry_block_(nullptr),
89 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +010090 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +010091 number_of_vregs_(0),
92 number_of_in_vregs_(0),
Nicolas Geoffraye5038322014-07-04 09:41:32 +010093 number_of_temporaries_(0),
Dave Allison20dfc792014-06-16 20:44:29 -070094 current_instruction_id_(0) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +000095
Nicolas Geoffray787c3072014-03-17 10:20:19 +000096 ArenaAllocator* GetArena() const { return arena_; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +010097 const GrowableArray<HBasicBlock*>& GetBlocks() const { return blocks_; }
Roland Levillain93445682014-10-06 19:24:02 +010098 HBasicBlock* GetBlock(size_t id) const { return blocks_.Get(id); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +000099
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000100 HBasicBlock* GetEntryBlock() const { return entry_block_; }
101 HBasicBlock* GetExitBlock() const { return exit_block_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000102
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000103 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
104 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000105
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000106 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100107
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000108 void BuildDominatorTree();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100109 void TransformToSSA();
110 void SimplifyCFG();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000111
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100112 // Find all natural loops in this graph. Aborts computation and returns false
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100113 // if one loop is not natural, that is the header does not dominate the back
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100114 // edge.
115 bool FindNaturalLoops() const;
116
117 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
118 void SimplifyLoop(HBasicBlock* header);
119
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000120 int GetNextInstructionId() {
121 return current_instruction_id_++;
122 }
123
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100124 uint16_t GetMaximumNumberOfOutVRegs() const {
125 return maximum_number_of_out_vregs_;
126 }
127
128 void UpdateMaximumNumberOfOutVRegs(uint16_t new_value) {
129 maximum_number_of_out_vregs_ = std::max(new_value, maximum_number_of_out_vregs_);
130 }
131
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100132 void UpdateNumberOfTemporaries(size_t count) {
133 number_of_temporaries_ = std::max(count, number_of_temporaries_);
134 }
135
136 size_t GetNumberOfTemporaries() const {
137 return number_of_temporaries_;
138 }
139
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100140 void SetNumberOfVRegs(uint16_t number_of_vregs) {
141 number_of_vregs_ = number_of_vregs;
142 }
143
144 uint16_t GetNumberOfVRegs() const {
145 return number_of_vregs_;
146 }
147
148 void SetNumberOfInVRegs(uint16_t value) {
149 number_of_in_vregs_ = value;
150 }
151
152 uint16_t GetNumberOfInVRegs() const {
153 return number_of_in_vregs_;
154 }
155
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100156 uint16_t GetNumberOfLocalVRegs() const {
157 return number_of_vregs_ - number_of_in_vregs_;
158 }
159
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100160 const GrowableArray<HBasicBlock*>& GetReversePostOrder() const {
161 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100162 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100163
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000164 private:
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000165 HBasicBlock* FindCommonDominator(HBasicBlock* first, HBasicBlock* second) const;
166 void VisitBlockForDominatorTree(HBasicBlock* block,
167 HBasicBlock* predecessor,
168 GrowableArray<size_t>* visits);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100169 void FindBackEdges(ArenaBitVector* visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000170 void VisitBlockForBackEdges(HBasicBlock* block,
171 ArenaBitVector* visited,
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100172 ArenaBitVector* visiting);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000173 void RemoveDeadBlocks(const ArenaBitVector& visited) const;
174
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000175 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000176
177 // List of blocks in insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000178 GrowableArray<HBasicBlock*> blocks_;
179
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100180 // List of blocks to perform a reverse post order tree traversal.
181 GrowableArray<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000182
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000183 HBasicBlock* entry_block_;
184 HBasicBlock* exit_block_;
185
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100186 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100187 uint16_t maximum_number_of_out_vregs_;
188
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100189 // The number of virtual registers in this method. Contains the parameters.
190 uint16_t number_of_vregs_;
191
192 // The number of virtual registers used by parameters of this method.
193 uint16_t number_of_in_vregs_;
194
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100195 // The number of temporaries that will be needed for the baseline compiler.
196 size_t number_of_temporaries_;
197
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000198 // The current id to assign to a newly added instruction. See HInstruction.id_.
199 int current_instruction_id_;
200
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000201 DISALLOW_COPY_AND_ASSIGN(HGraph);
202};
203
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700204class HLoopInformation : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000205 public:
206 HLoopInformation(HBasicBlock* header, HGraph* graph)
207 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100208 suspend_check_(nullptr),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100209 back_edges_(graph->GetArena(), kDefaultNumberOfBackEdges),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100210 // Make bit vector growable, as the number of blocks may change.
211 blocks_(graph->GetArena(), graph->GetBlocks().Size(), true) {}
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100212
213 HBasicBlock* GetHeader() const {
214 return header_;
215 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000216
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100217 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
218 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
219 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
220
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000221 void AddBackEdge(HBasicBlock* back_edge) {
222 back_edges_.Add(back_edge);
223 }
224
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100225 void RemoveBackEdge(HBasicBlock* back_edge) {
226 back_edges_.Delete(back_edge);
227 }
228
229 bool IsBackEdge(HBasicBlock* block) {
230 for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) {
231 if (back_edges_.Get(i) == block) return true;
232 }
233 return false;
234 }
235
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000236 int NumberOfBackEdges() const {
237 return back_edges_.Size();
238 }
239
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100240 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100241
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100242 const GrowableArray<HBasicBlock*>& GetBackEdges() const {
243 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100244 }
245
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100246 void ClearBackEdges() {
247 back_edges_.Reset();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100248 }
249
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100250 // Find blocks that are part of this loop. Returns whether the loop is a natural loop,
251 // that is the header dominates the back edge.
252 bool Populate();
253
254 // Returns whether this loop information contains `block`.
255 // Note that this loop information *must* be populated before entering this function.
256 bool Contains(const HBasicBlock& block) const;
257
258 // Returns whether this loop information is an inner loop of `other`.
259 // Note that `other` *must* be populated before entering this function.
260 bool IsIn(const HLoopInformation& other) const;
261
262 const ArenaBitVector& GetBlocks() const { return blocks_; }
263
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000264 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100265 // Internal recursive implementation of `Populate`.
266 void PopulateRecursive(HBasicBlock* block);
267
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000268 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100269 HSuspendCheck* suspend_check_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000270 GrowableArray<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100271 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000272
273 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
274};
275
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100276static constexpr size_t kNoLifetime = -1;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100277static constexpr uint32_t kNoDexPc = -1;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100278
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000279// A block in a method. Contains the list of instructions represented
280// as a double linked list. Each block knows its predecessors and
281// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100282
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700283class HBasicBlock : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000284 public:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100285 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000286 : graph_(graph),
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000287 predecessors_(graph->GetArena(), kDefaultNumberOfPredecessors),
288 successors_(graph->GetArena(), kDefaultNumberOfSuccessors),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000289 loop_information_(nullptr),
290 dominator_(nullptr),
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100291 dominated_blocks_(graph->GetArena(), kDefaultNumberOfDominatedBlocks),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100292 block_id_(-1),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100293 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100294 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000295 lifetime_end_(kNoLifetime),
296 is_catch_block_(false) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000297
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100298 const GrowableArray<HBasicBlock*>& GetPredecessors() const {
299 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000300 }
301
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100302 const GrowableArray<HBasicBlock*>& GetSuccessors() const {
303 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000304 }
305
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100306 const GrowableArray<HBasicBlock*>& GetDominatedBlocks() const {
307 return dominated_blocks_;
308 }
309
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100310 bool IsEntryBlock() const {
311 return graph_->GetEntryBlock() == this;
312 }
313
314 bool IsExitBlock() const {
315 return graph_->GetExitBlock() == this;
316 }
317
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000318 void AddBackEdge(HBasicBlock* back_edge) {
319 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000320 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000321 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100322 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000323 loop_information_->AddBackEdge(back_edge);
324 }
325
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000326 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000327
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000328 int GetBlockId() const { return block_id_; }
329 void SetBlockId(int id) { block_id_ = id; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000330
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000331 HBasicBlock* GetDominator() const { return dominator_; }
332 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100333 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.Add(block); }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000334
335 int NumberOfBackEdges() const {
336 return loop_information_ == nullptr
337 ? 0
338 : loop_information_->NumberOfBackEdges();
339 }
340
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100341 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
342 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100343 const HInstructionList& GetInstructions() const { return instructions_; }
344 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100345 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000346
347 void AddSuccessor(HBasicBlock* block) {
348 successors_.Add(block);
349 block->predecessors_.Add(this);
350 }
351
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100352 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
353 size_t successor_index = GetSuccessorIndexOf(existing);
354 DCHECK_NE(successor_index, static_cast<size_t>(-1));
355 existing->RemovePredecessor(this);
356 new_block->predecessors_.Add(this);
357 successors_.Put(successor_index, new_block);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000358 }
359
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100360 void RemovePredecessor(HBasicBlock* block) {
361 predecessors_.Delete(block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100362 }
363
364 void ClearAllPredecessors() {
365 predecessors_.Reset();
366 }
367
368 void AddPredecessor(HBasicBlock* block) {
369 predecessors_.Add(block);
370 block->successors_.Add(this);
371 }
372
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100373 void SwapPredecessors() {
Nicolas Geoffrayc83d4412014-09-18 16:46:20 +0100374 DCHECK_EQ(predecessors_.Size(), 2u);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100375 HBasicBlock* temp = predecessors_.Get(0);
376 predecessors_.Put(0, predecessors_.Get(1));
377 predecessors_.Put(1, temp);
378 }
379
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100380 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) {
381 for (size_t i = 0, e = predecessors_.Size(); i < e; ++i) {
382 if (predecessors_.Get(i) == predecessor) {
383 return i;
384 }
385 }
386 return -1;
387 }
388
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100389 size_t GetSuccessorIndexOf(HBasicBlock* successor) {
390 for (size_t i = 0, e = successors_.Size(); i < e; ++i) {
391 if (successors_.Get(i) == successor) {
392 return i;
393 }
394 }
395 return -1;
396 }
397
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000398 void AddInstruction(HInstruction* instruction);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100399 void RemoveInstruction(HInstruction* instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100400 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +0100401 // Replace instruction `initial` with `replacement` within this block.
402 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
403 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100404 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100405 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100406 void RemovePhi(HPhi* phi);
407
408 bool IsLoopHeader() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100409 return (loop_information_ != nullptr) && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100410 }
411
Roland Levillain6b879dd2014-09-22 17:13:44 +0100412 bool IsLoopPreHeaderFirstPredecessor() const {
413 DCHECK(IsLoopHeader());
414 DCHECK(!GetPredecessors().IsEmpty());
415 return GetPredecessors().Get(0) == GetLoopInformation()->GetPreHeader();
416 }
417
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100418 HLoopInformation* GetLoopInformation() const {
419 return loop_information_;
420 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000421
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100422 // Set the loop_information_ on this block. This method overrides the current
423 // loop_information if it is an outer loop of the passed loop information.
424 void SetInLoop(HLoopInformation* info) {
425 if (IsLoopHeader()) {
426 // Nothing to do. This just means `info` is an outer loop.
427 } else if (loop_information_ == nullptr) {
428 loop_information_ = info;
429 } else if (loop_information_->Contains(*info->GetHeader())) {
430 // Block is currently part of an outer loop. Make it part of this inner loop.
431 // Note that a non loop header having a loop information means this loop information
432 // has already been populated
433 loop_information_ = info;
434 } else {
435 // Block is part of an inner loop. Do not update the loop information.
436 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
437 // at this point, because this method is being called while populating `info`.
438 }
439 }
440
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100441 bool IsInLoop() const { return loop_information_ != nullptr; }
442
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100443 // Returns wheter this block dominates the blocked passed as parameter.
444 bool Dominates(HBasicBlock* block) const;
445
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100446 size_t GetLifetimeStart() const { return lifetime_start_; }
447 size_t GetLifetimeEnd() const { return lifetime_end_; }
448
449 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
450 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
451
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100452 uint32_t GetDexPc() const { return dex_pc_; }
453
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000454 bool IsCatchBlock() const { return is_catch_block_; }
455 void SetIsCatchBlock() { is_catch_block_ = true; }
456
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000457 private:
458 HGraph* const graph_;
459 GrowableArray<HBasicBlock*> predecessors_;
460 GrowableArray<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100461 HInstructionList instructions_;
462 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000463 HLoopInformation* loop_information_;
464 HBasicBlock* dominator_;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100465 GrowableArray<HBasicBlock*> dominated_blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000466 int block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100467 // The dex program counter of the first instruction of this block.
468 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100469 size_t lifetime_start_;
470 size_t lifetime_end_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000471 bool is_catch_block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000472
473 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
474};
475
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100476#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
477 M(Add, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000478 M(ArrayGet, Instruction) \
479 M(ArrayLength, Instruction) \
480 M(ArraySet, Instruction) \
481 M(BoundsCheck, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100482 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000483 M(Compare, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100484 M(Condition, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000485 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +0000486 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000487 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100488 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000489 M(Exit, Instruction) \
490 M(FloatConstant, Constant) \
491 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100492 M(GreaterThan, Condition) \
493 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100494 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000495 M(InstanceFieldGet, Instruction) \
496 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100497 M(IntConstant, Constant) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +0000498 M(InvokeInterface, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100499 M(InvokeStatic, Invoke) \
500 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000501 M(LessThan, Condition) \
502 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000503 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000504 M(LoadException, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100505 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000506 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100507 M(Local, Instruction) \
508 M(LongConstant, Constant) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000509 M(Mul, BinaryOperation) \
510 M(Neg, UnaryOperation) \
511 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100512 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +0100513 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000514 M(NotEqual, Condition) \
515 M(NullCheck, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100516 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000517 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100518 M(Phi, Instruction) \
519 M(Return, Instruction) \
520 M(ReturnVoid, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100521 M(StaticFieldGet, Instruction) \
522 M(StaticFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100523 M(StoreLocal, Instruction) \
524 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100525 M(SuspendCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000526 M(Temporary, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000527 M(Throw, Instruction) \
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000528 M(TypeCheck, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +0000529 M(TypeConversion, Instruction) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000530
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100531#define FOR_EACH_INSTRUCTION(M) \
532 FOR_EACH_CONCRETE_INSTRUCTION(M) \
533 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +0100534 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +0100535 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100536 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -0700537
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100538#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000539FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
540#undef FORWARD_DECLARATION
541
Roland Levillainccc07a92014-09-16 14:48:16 +0100542#define DECLARE_INSTRUCTION(type) \
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100543 virtual InstructionKind GetKind() const { return k##type; } \
Roland Levillainccc07a92014-09-16 14:48:16 +0100544 virtual const char* DebugName() const { return #type; } \
545 virtual const H##type* As##type() const OVERRIDE { return this; } \
546 virtual H##type* As##type() OVERRIDE { return this; } \
547 virtual bool InstructionTypeEquals(HInstruction* other) const { \
548 return other->Is##type(); \
549 } \
550 virtual void Accept(HGraphVisitor* visitor)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000551
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100552template <typename T>
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700553class HUseListNode : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000554 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100555 HUseListNode(T* user, size_t index, HUseListNode* tail)
Dave Allison20dfc792014-06-16 20:44:29 -0700556 : user_(user), index_(index), tail_(tail) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000557
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000558 HUseListNode* GetTail() const { return tail_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100559 T* GetUser() const { return user_; }
560 size_t GetIndex() const { return index_; }
561
562 void SetTail(HUseListNode<T>* node) { tail_ = node; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000563
564 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100565 T* const user_;
566 const size_t index_;
567 HUseListNode<T>* tail_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000568
569 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
570};
571
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100572// Represents the side effects an instruction may have.
573class SideEffects : public ValueObject {
574 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100575 SideEffects() : flags_(0) {}
576
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100577 static SideEffects None() {
578 return SideEffects(0);
579 }
580
581 static SideEffects All() {
582 return SideEffects(ChangesSomething().flags_ | DependsOnSomething().flags_);
583 }
584
585 static SideEffects ChangesSomething() {
586 return SideEffects((1 << kFlagChangesCount) - 1);
587 }
588
589 static SideEffects DependsOnSomething() {
590 int count = kFlagDependsOnCount - kFlagChangesCount;
591 return SideEffects(((1 << count) - 1) << kFlagChangesCount);
592 }
593
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100594 SideEffects Union(SideEffects other) const {
595 return SideEffects(flags_ | other.flags_);
596 }
597
Roland Levillain72bceff2014-09-15 18:29:00 +0100598 bool HasSideEffects() const {
599 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
600 return (flags_ & all_bits_set) != 0;
601 }
602
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100603 bool HasAllSideEffects() const {
604 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
605 return all_bits_set == (flags_ & all_bits_set);
606 }
607
608 bool DependsOn(SideEffects other) const {
609 size_t depends_flags = other.ComputeDependsFlags();
610 return (flags_ & depends_flags) != 0;
611 }
612
613 bool HasDependencies() const {
614 int count = kFlagDependsOnCount - kFlagChangesCount;
615 size_t all_bits_set = (1 << count) - 1;
616 return ((flags_ >> kFlagChangesCount) & all_bits_set) != 0;
617 }
618
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100619 private:
620 static constexpr int kFlagChangesSomething = 0;
621 static constexpr int kFlagChangesCount = kFlagChangesSomething + 1;
622
623 static constexpr int kFlagDependsOnSomething = kFlagChangesCount;
624 static constexpr int kFlagDependsOnCount = kFlagDependsOnSomething + 1;
625
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100626 explicit SideEffects(size_t flags) : flags_(flags) {}
627
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100628 size_t ComputeDependsFlags() const {
629 return flags_ << kFlagChangesCount;
630 }
631
632 size_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100633};
634
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700635class HInstruction : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000636 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100637 explicit HInstruction(SideEffects side_effects)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000638 : previous_(nullptr),
639 next_(nullptr),
640 block_(nullptr),
641 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100642 ssa_index_(-1),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000643 uses_(nullptr),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100644 env_uses_(nullptr),
645 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100646 locations_(nullptr),
647 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100648 lifetime_position_(kNoLifetime),
649 side_effects_(side_effects) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000650
Dave Allison20dfc792014-06-16 20:44:29 -0700651 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000652
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100653#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100654 enum InstructionKind {
655 FOR_EACH_INSTRUCTION(DECLARE_KIND)
656 };
657#undef DECLARE_KIND
658
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000659 HInstruction* GetNext() const { return next_; }
660 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000661
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000662 HBasicBlock* GetBlock() const { return block_; }
663 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100664 bool IsInBlock() const { return block_ != nullptr; }
665 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +0100666 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000667
Roland Levillain6b879dd2014-09-22 17:13:44 +0100668 virtual size_t InputCount() const = 0;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100669 virtual HInstruction* InputAt(size_t i) const = 0;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000670
671 virtual void Accept(HGraphVisitor* visitor) = 0;
672 virtual const char* DebugName() const = 0;
673
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100674 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100675 virtual void SetRawInputAt(size_t index, HInstruction* input) = 0;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100676
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100677 virtual bool NeedsEnvironment() const { return false; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100678 virtual bool IsControlFlow() const { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +0100679 virtual bool CanThrow() const { return false; }
Roland Levillain72bceff2014-09-15 18:29:00 +0100680 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100681
682 void AddUseAt(HInstruction* user, size_t index) {
683 uses_ = new (block_->GetGraph()->GetArena()) HUseListNode<HInstruction>(user, index, uses_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000684 }
685
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100686 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +0100687 DCHECK(user != nullptr);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100688 env_uses_ = new (block_->GetGraph()->GetArena()) HUseListNode<HEnvironment>(
689 user, index, env_uses_);
690 }
691
692 void RemoveUser(HInstruction* user, size_t index);
Nicolas Geoffray724c9632014-09-22 12:27:27 +0100693 void RemoveEnvironmentUser(HEnvironment* user, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100694
695 HUseListNode<HInstruction>* GetUses() const { return uses_; }
696 HUseListNode<HEnvironment>* GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000697
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100698 bool HasUses() const { return uses_ != nullptr || env_uses_ != nullptr; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100699 bool HasEnvironmentUses() const { return env_uses_ != nullptr; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000700
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100701 size_t NumberOfUses() const {
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100702 // TODO: Optimize this method if it is used outside of the HGraphVisualizer.
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100703 size_t result = 0;
704 HUseListNode<HInstruction>* current = uses_;
705 while (current != nullptr) {
706 current = current->GetTail();
707 ++result;
708 }
709 return result;
710 }
711
Roland Levillain6c82d402014-10-13 16:10:27 +0100712 // Does this instruction strictly dominate `other_instruction`?
713 // Returns false if this instruction and `other_instruction` are the same.
714 // Aborts if this instruction and `other_instruction` are both phis.
715 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +0100716
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000717 int GetId() const { return id_; }
718 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000719
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100720 int GetSsaIndex() const { return ssa_index_; }
721 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
722 bool HasSsaIndex() const { return ssa_index_ != -1; }
723
724 bool HasEnvironment() const { return environment_ != nullptr; }
725 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100726 void SetEnvironment(HEnvironment* environment) { environment_ = environment; }
727
Nicolas Geoffray39468442014-09-02 15:17:15 +0100728 // Returns the number of entries in the environment. Typically, that is the
729 // number of dex registers in a method. It could be more in case of inlining.
730 size_t EnvironmentSize() const;
731
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000732 LocationSummary* GetLocations() const { return locations_; }
733 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000734
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100735 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100736 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100737
Dave Allison20dfc792014-06-16 20:44:29 -0700738 bool HasOnlyOneUse() const {
739 return uses_ != nullptr && uses_->GetTail() == nullptr;
740 }
741
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100742#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +0100743 bool Is##type() const { return (As##type() != nullptr); } \
744 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000745 virtual H##type* As##type() { return nullptr; }
746
747 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
748#undef INSTRUCTION_TYPE_CHECK
749
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100750 // Returns whether the instruction can be moved within the graph.
751 virtual bool CanBeMoved() const { return false; }
752
753 // Returns whether the two instructions are of the same kind.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700754 virtual bool InstructionTypeEquals(HInstruction* other) const {
755 UNUSED(other);
756 return false;
757 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100758
759 // Returns whether any data encoded in the two instructions is equal.
760 // This method does not look at the inputs. Both instructions must be
761 // of the same type, otherwise the method has undefined behavior.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700762 virtual bool InstructionDataEquals(HInstruction* other) const {
763 UNUSED(other);
764 return false;
765 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100766
767 // Returns whether two instructions are equal, that is:
768 // 1) They have the same type and contain the same data,
769 // 2) Their inputs are identical.
770 bool Equals(HInstruction* other) const;
771
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100772 virtual InstructionKind GetKind() const = 0;
773
774 virtual size_t ComputeHashCode() const {
775 size_t result = GetKind();
776 for (size_t i = 0, e = InputCount(); i < e; ++i) {
777 result = (result * 31) + InputAt(i)->GetId();
778 }
779 return result;
780 }
781
782 SideEffects GetSideEffects() const { return side_effects_; }
783
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100784 size_t GetLifetimePosition() const { return lifetime_position_; }
785 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
786 LiveInterval* GetLiveInterval() const { return live_interval_; }
787 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
788 bool HasLiveInterval() const { return live_interval_ != nullptr; }
789
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000790 private:
791 HInstruction* previous_;
792 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000793 HBasicBlock* block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000794
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000795 // An instruction gets an id when it is added to the graph.
796 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +0100797 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000798 int id_;
799
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100800 // When doing liveness analysis, instructions that have uses get an SSA index.
801 int ssa_index_;
802
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100803 // List of instructions that have this instruction as input.
804 HUseListNode<HInstruction>* uses_;
805
806 // List of environments that contain this instruction.
807 HUseListNode<HEnvironment>* env_uses_;
808
Nicolas Geoffray39468442014-09-02 15:17:15 +0100809 // The environment associated with this instruction. Not null if the instruction
810 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100811 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000812
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000813 // Set by the code generator.
814 LocationSummary* locations_;
815
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100816 // Set by the liveness analysis.
817 LiveInterval* live_interval_;
818
819 // Set by the liveness analysis, this is the position in a linear
820 // order of blocks where this instruction's live interval start.
821 size_t lifetime_position_;
822
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100823 const SideEffects side_effects_;
824
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000825 friend class HBasicBlock;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100826 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000827
828 DISALLOW_COPY_AND_ASSIGN(HInstruction);
829};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700830std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000831
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100832template<typename T>
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000833class HUseIterator : public ValueObject {
834 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100835 explicit HUseIterator(HUseListNode<T>* uses) : current_(uses) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000836
837 bool Done() const { return current_ == nullptr; }
838
839 void Advance() {
840 DCHECK(!Done());
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000841 current_ = current_->GetTail();
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000842 }
843
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100844 HUseListNode<T>* Current() const {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000845 DCHECK(!Done());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100846 return current_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000847 }
848
849 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100850 HUseListNode<T>* current_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000851
852 friend class HValue;
853};
854
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100855// A HEnvironment object contains the values of virtual registers at a given location.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700856class HEnvironment : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100857 public:
858 HEnvironment(ArenaAllocator* arena, size_t number_of_vregs) : vregs_(arena, number_of_vregs) {
859 vregs_.SetSize(number_of_vregs);
860 for (size_t i = 0; i < number_of_vregs; i++) {
861 vregs_.Put(i, nullptr);
862 }
863 }
864
865 void Populate(const GrowableArray<HInstruction*>& env) {
866 for (size_t i = 0; i < env.Size(); i++) {
867 HInstruction* instruction = env.Get(i);
868 vregs_.Put(i, instruction);
869 if (instruction != nullptr) {
870 instruction->AddEnvUseAt(this, i);
871 }
872 }
873 }
874
875 void SetRawEnvAt(size_t index, HInstruction* instruction) {
876 vregs_.Put(index, instruction);
877 }
878
Nicolas Geoffray39468442014-09-02 15:17:15 +0100879 HInstruction* GetInstructionAt(size_t index) const {
880 return vregs_.Get(index);
881 }
882
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100883 GrowableArray<HInstruction*>* GetVRegs() {
884 return &vregs_;
885 }
886
Nicolas Geoffray39468442014-09-02 15:17:15 +0100887 size_t Size() const { return vregs_.Size(); }
888
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100889 private:
890 GrowableArray<HInstruction*> vregs_;
891
892 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
893};
894
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000895class HInputIterator : public ValueObject {
896 public:
Dave Allison20dfc792014-06-16 20:44:29 -0700897 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000898
899 bool Done() const { return index_ == instruction_->InputCount(); }
900 HInstruction* Current() const { return instruction_->InputAt(index_); }
901 void Advance() { index_++; }
902
903 private:
904 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100905 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000906
907 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
908};
909
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000910class HInstructionIterator : public ValueObject {
911 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100912 explicit HInstructionIterator(const HInstructionList& instructions)
913 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000914 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000915 }
916
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000917 bool Done() const { return instruction_ == nullptr; }
918 HInstruction* Current() const { return instruction_; }
919 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000920 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000921 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000922 }
923
924 private:
925 HInstruction* instruction_;
926 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100927
928 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000929};
930
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100931class HBackwardInstructionIterator : public ValueObject {
932 public:
933 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
934 : instruction_(instructions.last_instruction_) {
935 next_ = Done() ? nullptr : instruction_->GetPrevious();
936 }
937
938 bool Done() const { return instruction_ == nullptr; }
939 HInstruction* Current() const { return instruction_; }
940 void Advance() {
941 instruction_ = next_;
942 next_ = Done() ? nullptr : instruction_->GetPrevious();
943 }
944
945 private:
946 HInstruction* instruction_;
947 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100948
949 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100950};
951
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000952// An embedded container with N elements of type T. Used (with partial
953// specialization for N=0) because embedded arrays cannot have size 0.
954template<typename T, intptr_t N>
955class EmbeddedArray {
956 public:
Dave Allison20dfc792014-06-16 20:44:29 -0700957 EmbeddedArray() : elements_() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000958
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000959 intptr_t GetLength() const { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000960
961 const T& operator[](intptr_t i) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000962 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000963 return elements_[i];
964 }
965
966 T& operator[](intptr_t i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000967 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000968 return elements_[i];
969 }
970
971 const T& At(intptr_t i) const {
972 return (*this)[i];
973 }
974
975 void SetAt(intptr_t i, const T& val) {
976 (*this)[i] = val;
977 }
978
979 private:
980 T elements_[N];
981};
982
983template<typename T>
984class EmbeddedArray<T, 0> {
985 public:
986 intptr_t length() const { return 0; }
987 const T& operator[](intptr_t i) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700988 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000989 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700990 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000991 }
992 T& operator[](intptr_t i) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700993 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000994 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700995 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000996 }
997};
998
999template<intptr_t N>
1000class HTemplateInstruction: public HInstruction {
1001 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001002 HTemplateInstruction<N>(SideEffects side_effects)
1003 : HInstruction(side_effects), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07001004 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001005
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001006 virtual size_t InputCount() const { return N; }
1007 virtual HInstruction* InputAt(size_t i) const { return inputs_[i]; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001008
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001009 protected:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001010 virtual void SetRawInputAt(size_t i, HInstruction* instruction) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001011 inputs_[i] = instruction;
1012 }
1013
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001014 private:
1015 EmbeddedArray<HInstruction*, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001016
1017 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001018};
1019
Dave Allison20dfc792014-06-16 20:44:29 -07001020template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001021class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07001022 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001023 HExpression<N>(Primitive::Type type, SideEffects side_effects)
1024 : HTemplateInstruction<N>(side_effects), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07001025 virtual ~HExpression() {}
1026
1027 virtual Primitive::Type GetType() const { return type_; }
1028
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001029 protected:
1030 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07001031};
1032
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001033// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
1034// instruction that branches to the exit block.
1035class HReturnVoid : public HTemplateInstruction<0> {
1036 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001037 HReturnVoid() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001038
1039 virtual bool IsControlFlow() const { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001040
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001041 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001042
1043 private:
1044 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
1045};
1046
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001047// Represents dex's RETURN opcodes. A HReturn is a control flow
1048// instruction that branches to the exit block.
1049class HReturn : public HTemplateInstruction<1> {
1050 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001051 explicit HReturn(HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001052 SetRawInputAt(0, value);
1053 }
1054
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001055 virtual bool IsControlFlow() const { return true; }
1056
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001057 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001058
1059 private:
1060 DISALLOW_COPY_AND_ASSIGN(HReturn);
1061};
1062
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001063// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001064// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001065// exit block.
1066class HExit : public HTemplateInstruction<0> {
1067 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001068 HExit() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001069
1070 virtual bool IsControlFlow() const { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001071
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001072 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001073
1074 private:
1075 DISALLOW_COPY_AND_ASSIGN(HExit);
1076};
1077
1078// Jumps from one block to another.
1079class HGoto : public HTemplateInstruction<0> {
1080 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001081 HGoto() : HTemplateInstruction(SideEffects::None()) {}
1082
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001083 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001084
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001085 HBasicBlock* GetSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001086 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001087 }
1088
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001089 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001090
1091 private:
1092 DISALLOW_COPY_AND_ASSIGN(HGoto);
1093};
1094
Dave Allison20dfc792014-06-16 20:44:29 -07001095
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001096// Conditional branch. A block ending with an HIf instruction must have
1097// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001098class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001099 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001100 explicit HIf(HInstruction* input) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001101 SetRawInputAt(0, input);
1102 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001103
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001104 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001105
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001106 HBasicBlock* IfTrueSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001107 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001108 }
1109
1110 HBasicBlock* IfFalseSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001111 return GetBlock()->GetSuccessors().Get(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001112 }
1113
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001114 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001115
Dave Allison20dfc792014-06-16 20:44:29 -07001116 virtual bool IsIfInstruction() const { return true; }
1117
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001118 private:
1119 DISALLOW_COPY_AND_ASSIGN(HIf);
1120};
1121
Roland Levillain88cb1752014-10-20 16:36:47 +01001122class HUnaryOperation : public HExpression<1> {
1123 public:
1124 HUnaryOperation(Primitive::Type result_type, HInstruction* input)
1125 : HExpression(result_type, SideEffects::None()) {
1126 SetRawInputAt(0, input);
1127 }
1128
1129 HInstruction* GetInput() const { return InputAt(0); }
1130 Primitive::Type GetResultType() const { return GetType(); }
1131
1132 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001133 virtual bool InstructionDataEquals(HInstruction* other) const {
1134 UNUSED(other);
1135 return true;
1136 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001137
Roland Levillain9240d6a2014-10-20 16:47:04 +01001138 // Try to statically evaluate `operation` and return a HConstant
1139 // containing the result of this evaluation. If `operation` cannot
1140 // be evaluated as a constant, return nullptr.
1141 HConstant* TryStaticEvaluation() const;
1142
1143 // Apply this operation to `x`.
1144 virtual int32_t Evaluate(int32_t x) const = 0;
1145 virtual int64_t Evaluate(int64_t x) const = 0;
1146
Roland Levillain88cb1752014-10-20 16:36:47 +01001147 DECLARE_INSTRUCTION(UnaryOperation);
1148
1149 private:
1150 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
1151};
1152
Dave Allison20dfc792014-06-16 20:44:29 -07001153class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001154 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001155 HBinaryOperation(Primitive::Type result_type,
1156 HInstruction* left,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001157 HInstruction* right) : HExpression(result_type, SideEffects::None()) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001158 SetRawInputAt(0, left);
1159 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001160 }
1161
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001162 HInstruction* GetLeft() const { return InputAt(0); }
1163 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07001164 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001165
1166 virtual bool IsCommutative() { return false; }
1167
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001168 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001169 virtual bool InstructionDataEquals(HInstruction* other) const {
1170 UNUSED(other);
1171 return true;
1172 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001173
Roland Levillain9240d6a2014-10-20 16:47:04 +01001174 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01001175 // containing the result of this evaluation. If `operation` cannot
1176 // be evaluated as a constant, return nullptr.
Roland Levillain9240d6a2014-10-20 16:47:04 +01001177 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01001178
1179 // Apply this operation to `x` and `y`.
1180 virtual int32_t Evaluate(int32_t x, int32_t y) const = 0;
1181 virtual int64_t Evaluate(int64_t x, int64_t y) const = 0;
1182
Roland Levillainccc07a92014-09-16 14:48:16 +01001183 DECLARE_INSTRUCTION(BinaryOperation);
1184
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001185 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001186 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
1187};
1188
Dave Allison20dfc792014-06-16 20:44:29 -07001189class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001190 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001191 HCondition(HInstruction* first, HInstruction* second)
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001192 : HBinaryOperation(Primitive::kPrimBoolean, first, second),
1193 needs_materialization_(true) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001194
1195 virtual bool IsCommutative() { return true; }
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001196
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001197 bool NeedsMaterialization() const { return needs_materialization_; }
1198 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001199
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001200 // For code generation purposes, returns whether this instruction is just before
1201 // `if_`, and disregard moves in between.
1202 bool IsBeforeWhenDisregardMoves(HIf* if_) const;
1203
Dave Allison20dfc792014-06-16 20:44:29 -07001204 DECLARE_INSTRUCTION(Condition);
1205
1206 virtual IfCondition GetCondition() const = 0;
1207
1208 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001209 // For register allocation purposes, returns whether this instruction needs to be
1210 // materialized (that is, not just be in the processor flags).
1211 bool needs_materialization_;
1212
Dave Allison20dfc792014-06-16 20:44:29 -07001213 DISALLOW_COPY_AND_ASSIGN(HCondition);
1214};
1215
1216// Instruction to check if two inputs are equal to each other.
1217class HEqual : public HCondition {
1218 public:
1219 HEqual(HInstruction* first, HInstruction* second)
1220 : HCondition(first, second) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001221
Roland Levillain93445682014-10-06 19:24:02 +01001222 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1223 return x == y ? 1 : 0;
1224 }
1225 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1226 return x == y ? 1 : 0;
1227 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001228
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001229 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001230
Dave Allison20dfc792014-06-16 20:44:29 -07001231 virtual IfCondition GetCondition() const {
1232 return kCondEQ;
1233 }
1234
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001235 private:
1236 DISALLOW_COPY_AND_ASSIGN(HEqual);
1237};
1238
Dave Allison20dfc792014-06-16 20:44:29 -07001239class HNotEqual : public HCondition {
1240 public:
1241 HNotEqual(HInstruction* first, HInstruction* second)
1242 : HCondition(first, second) {}
1243
Roland Levillain93445682014-10-06 19:24:02 +01001244 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1245 return x != y ? 1 : 0;
1246 }
1247 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1248 return x != y ? 1 : 0;
1249 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001250
Dave Allison20dfc792014-06-16 20:44:29 -07001251 DECLARE_INSTRUCTION(NotEqual);
1252
1253 virtual IfCondition GetCondition() const {
1254 return kCondNE;
1255 }
1256
1257 private:
1258 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
1259};
1260
1261class HLessThan : public HCondition {
1262 public:
1263 HLessThan(HInstruction* first, HInstruction* second)
1264 : HCondition(first, second) {}
1265
Roland Levillain93445682014-10-06 19:24:02 +01001266 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1267 return x < y ? 1 : 0;
1268 }
1269 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1270 return x < y ? 1 : 0;
1271 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001272
Dave Allison20dfc792014-06-16 20:44:29 -07001273 DECLARE_INSTRUCTION(LessThan);
1274
1275 virtual IfCondition GetCondition() const {
1276 return kCondLT;
1277 }
1278
1279 private:
1280 DISALLOW_COPY_AND_ASSIGN(HLessThan);
1281};
1282
1283class HLessThanOrEqual : public HCondition {
1284 public:
1285 HLessThanOrEqual(HInstruction* first, HInstruction* second)
1286 : HCondition(first, second) {}
1287
Roland Levillain93445682014-10-06 19:24:02 +01001288 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1289 return x <= y ? 1 : 0;
1290 }
1291 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1292 return x <= y ? 1 : 0;
1293 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001294
Dave Allison20dfc792014-06-16 20:44:29 -07001295 DECLARE_INSTRUCTION(LessThanOrEqual);
1296
1297 virtual IfCondition GetCondition() const {
1298 return kCondLE;
1299 }
1300
1301 private:
1302 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
1303};
1304
1305class HGreaterThan : public HCondition {
1306 public:
1307 HGreaterThan(HInstruction* first, HInstruction* second)
1308 : HCondition(first, second) {}
1309
Roland Levillain93445682014-10-06 19:24:02 +01001310 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1311 return x > y ? 1 : 0;
1312 }
1313 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1314 return x > y ? 1 : 0;
1315 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001316
Dave Allison20dfc792014-06-16 20:44:29 -07001317 DECLARE_INSTRUCTION(GreaterThan);
1318
1319 virtual IfCondition GetCondition() const {
1320 return kCondGT;
1321 }
1322
1323 private:
1324 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
1325};
1326
1327class HGreaterThanOrEqual : public HCondition {
1328 public:
1329 HGreaterThanOrEqual(HInstruction* first, HInstruction* second)
1330 : HCondition(first, second) {}
1331
Roland Levillain93445682014-10-06 19:24:02 +01001332 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1333 return x >= y ? 1 : 0;
1334 }
1335 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1336 return x >= y ? 1 : 0;
1337 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001338
Dave Allison20dfc792014-06-16 20:44:29 -07001339 DECLARE_INSTRUCTION(GreaterThanOrEqual);
1340
1341 virtual IfCondition GetCondition() const {
1342 return kCondGE;
1343 }
1344
1345 private:
1346 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
1347};
1348
1349
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001350// Instruction to check how two inputs compare to each other.
1351// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
1352class HCompare : public HBinaryOperation {
1353 public:
1354 HCompare(Primitive::Type type, HInstruction* first, HInstruction* second)
1355 : HBinaryOperation(Primitive::kPrimInt, first, second) {
1356 DCHECK_EQ(type, first->GetType());
1357 DCHECK_EQ(type, second->GetType());
1358 }
1359
Roland Levillain93445682014-10-06 19:24:02 +01001360 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001361 return
1362 x == y ? 0 :
1363 x > y ? 1 :
1364 -1;
1365 }
Roland Levillain93445682014-10-06 19:24:02 +01001366 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001367 return
1368 x == y ? 0 :
1369 x > y ? 1 :
1370 -1;
1371 }
1372
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001373 DECLARE_INSTRUCTION(Compare);
1374
1375 private:
1376 DISALLOW_COPY_AND_ASSIGN(HCompare);
1377};
1378
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001379// A local in the graph. Corresponds to a Dex register.
1380class HLocal : public HTemplateInstruction<0> {
1381 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001382 explicit HLocal(uint16_t reg_number)
1383 : HTemplateInstruction(SideEffects::None()), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001384
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001385 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001386
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001387 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001388
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001389 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001390 // The Dex register number.
1391 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001392
1393 DISALLOW_COPY_AND_ASSIGN(HLocal);
1394};
1395
1396// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07001397class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001398 public:
Roland Levillain5799fc02014-09-25 12:15:20 +01001399 HLoadLocal(HLocal* local, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001400 : HExpression(type, SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001401 SetRawInputAt(0, local);
1402 }
1403
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001404 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1405
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001406 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001407
1408 private:
1409 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
1410};
1411
1412// Store a value in a given local. This instruction has two inputs: the value
1413// and the local.
1414class HStoreLocal : public HTemplateInstruction<2> {
1415 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001416 HStoreLocal(HLocal* local, HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001417 SetRawInputAt(0, local);
1418 SetRawInputAt(1, value);
1419 }
1420
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001421 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1422
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001423 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001424
1425 private:
1426 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
1427};
1428
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001429class HConstant : public HExpression<0> {
1430 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001431 explicit HConstant(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
1432
1433 virtual bool CanBeMoved() const { return true; }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001434
1435 DECLARE_INSTRUCTION(Constant);
1436
1437 private:
1438 DISALLOW_COPY_AND_ASSIGN(HConstant);
1439};
1440
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001441class HFloatConstant : public HConstant {
1442 public:
1443 explicit HFloatConstant(float value) : HConstant(Primitive::kPrimFloat), value_(value) {}
1444
1445 float GetValue() const { return value_; }
1446
1447 virtual bool InstructionDataEquals(HInstruction* other) const {
1448 return bit_cast<float, int32_t>(other->AsFloatConstant()->value_) ==
1449 bit_cast<float, int32_t>(value_);
1450 }
1451
1452 virtual size_t ComputeHashCode() const { return static_cast<size_t>(GetValue()); }
1453
1454 DECLARE_INSTRUCTION(FloatConstant);
1455
1456 private:
1457 const float value_;
1458
1459 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
1460};
1461
1462class HDoubleConstant : public HConstant {
1463 public:
1464 explicit HDoubleConstant(double value) : HConstant(Primitive::kPrimDouble), value_(value) {}
1465
1466 double GetValue() const { return value_; }
1467
1468 virtual bool InstructionDataEquals(HInstruction* other) const {
1469 return bit_cast<double, int64_t>(other->AsDoubleConstant()->value_) ==
1470 bit_cast<double, int64_t>(value_);
1471 }
1472
1473 virtual size_t ComputeHashCode() const { return static_cast<size_t>(GetValue()); }
1474
1475 DECLARE_INSTRUCTION(DoubleConstant);
1476
1477 private:
1478 const double value_;
1479
1480 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
1481};
1482
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001483// Constants of the type int. Those can be from Dex instructions, or
1484// synthesized (for example with the if-eqz instruction).
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001485class HIntConstant : public HConstant {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001486 public:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001487 explicit HIntConstant(int32_t value) : HConstant(Primitive::kPrimInt), value_(value) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001488
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001489 int32_t GetValue() const { return value_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001490
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001491 virtual bool InstructionDataEquals(HInstruction* other) const {
1492 return other->AsIntConstant()->value_ == value_;
1493 }
1494
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001495 virtual size_t ComputeHashCode() const { return GetValue(); }
1496
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001497 DECLARE_INSTRUCTION(IntConstant);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001498
1499 private:
1500 const int32_t value_;
1501
1502 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
1503};
1504
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001505class HLongConstant : public HConstant {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001506 public:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001507 explicit HLongConstant(int64_t value) : HConstant(Primitive::kPrimLong), value_(value) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001508
1509 int64_t GetValue() const { return value_; }
1510
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001511 virtual bool InstructionDataEquals(HInstruction* other) const {
1512 return other->AsLongConstant()->value_ == value_;
1513 }
1514
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001515 virtual size_t ComputeHashCode() const { return static_cast<size_t>(GetValue()); }
1516
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001517 DECLARE_INSTRUCTION(LongConstant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001518
1519 private:
1520 const int64_t value_;
1521
1522 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
1523};
1524
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001525class HInvoke : public HInstruction {
1526 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001527 HInvoke(ArenaAllocator* arena,
1528 uint32_t number_of_arguments,
1529 Primitive::Type return_type,
1530 uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001531 : HInstruction(SideEffects::All()),
1532 inputs_(arena, number_of_arguments),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001533 return_type_(return_type),
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001534 dex_pc_(dex_pc) {
1535 inputs_.SetSize(number_of_arguments);
1536 }
1537
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001538 virtual size_t InputCount() const { return inputs_.Size(); }
1539 virtual HInstruction* InputAt(size_t i) const { return inputs_.Get(i); }
1540
1541 // Runtime needs to walk the stack, so Dex -> Dex calls need to
1542 // know their environment.
1543 virtual bool NeedsEnvironment() const { return true; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001544
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001545 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001546 SetRawInputAt(index, argument);
1547 }
1548
1549 virtual void SetRawInputAt(size_t index, HInstruction* input) {
1550 inputs_.Put(index, input);
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001551 }
1552
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001553 virtual Primitive::Type GetType() const { return return_type_; }
1554
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001555 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001556
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001557 DECLARE_INSTRUCTION(Invoke);
1558
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001559 protected:
1560 GrowableArray<HInstruction*> inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001561 const Primitive::Type return_type_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001562 const uint32_t dex_pc_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001563
1564 private:
1565 DISALLOW_COPY_AND_ASSIGN(HInvoke);
1566};
1567
1568class HInvokeStatic : public HInvoke {
1569 public:
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001570 HInvokeStatic(ArenaAllocator* arena,
1571 uint32_t number_of_arguments,
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001572 Primitive::Type return_type,
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001573 uint32_t dex_pc,
1574 uint32_t index_in_dex_cache)
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001575 : HInvoke(arena, number_of_arguments, return_type, dex_pc),
1576 index_in_dex_cache_(index_in_dex_cache) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001577
1578 uint32_t GetIndexInDexCache() const { return index_in_dex_cache_; }
1579
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001580 DECLARE_INSTRUCTION(InvokeStatic);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001581
1582 private:
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001583 const uint32_t index_in_dex_cache_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001584
1585 DISALLOW_COPY_AND_ASSIGN(HInvokeStatic);
1586};
1587
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001588class HInvokeVirtual : public HInvoke {
1589 public:
1590 HInvokeVirtual(ArenaAllocator* arena,
1591 uint32_t number_of_arguments,
1592 Primitive::Type return_type,
1593 uint32_t dex_pc,
1594 uint32_t vtable_index)
1595 : HInvoke(arena, number_of_arguments, return_type, dex_pc),
1596 vtable_index_(vtable_index) {}
1597
1598 uint32_t GetVTableIndex() const { return vtable_index_; }
1599
1600 DECLARE_INSTRUCTION(InvokeVirtual);
1601
1602 private:
1603 const uint32_t vtable_index_;
1604
1605 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
1606};
1607
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001608class HInvokeInterface : public HInvoke {
1609 public:
1610 HInvokeInterface(ArenaAllocator* arena,
1611 uint32_t number_of_arguments,
1612 Primitive::Type return_type,
1613 uint32_t dex_pc,
1614 uint32_t dex_method_index,
1615 uint32_t imt_index)
1616 : HInvoke(arena, number_of_arguments, return_type, dex_pc),
1617 dex_method_index_(dex_method_index),
1618 imt_index_(imt_index) {}
1619
1620 uint32_t GetImtIndex() const { return imt_index_; }
1621 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
1622
1623 DECLARE_INSTRUCTION(InvokeInterface);
1624
1625 private:
1626 const uint32_t dex_method_index_;
1627 const uint32_t imt_index_;
1628
1629 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
1630};
1631
Dave Allison20dfc792014-06-16 20:44:29 -07001632class HNewInstance : public HExpression<0> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001633 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001634 HNewInstance(uint32_t dex_pc, uint16_t type_index)
1635 : HExpression(Primitive::kPrimNot, SideEffects::None()),
1636 dex_pc_(dex_pc),
1637 type_index_(type_index) {}
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001638
1639 uint32_t GetDexPc() const { return dex_pc_; }
1640 uint16_t GetTypeIndex() const { return type_index_; }
1641
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001642 // Calls runtime so needs an environment.
1643 virtual bool NeedsEnvironment() const { return true; }
1644
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001645 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001646
1647 private:
1648 const uint32_t dex_pc_;
1649 const uint16_t type_index_;
1650
1651 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
1652};
1653
Roland Levillain88cb1752014-10-20 16:36:47 +01001654class HNeg : public HUnaryOperation {
1655 public:
1656 explicit HNeg(Primitive::Type result_type, HInstruction* input)
1657 : HUnaryOperation(result_type, input) {}
1658
Roland Levillain9240d6a2014-10-20 16:47:04 +01001659 virtual int32_t Evaluate(int32_t x) const OVERRIDE { return -x; }
1660 virtual int64_t Evaluate(int64_t x) const OVERRIDE { return -x; }
1661
Roland Levillain88cb1752014-10-20 16:36:47 +01001662 DECLARE_INSTRUCTION(Neg);
1663
1664 private:
1665 DISALLOW_COPY_AND_ASSIGN(HNeg);
1666};
1667
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001668class HNewArray : public HExpression<1> {
1669 public:
1670 HNewArray(HInstruction* length, uint32_t dex_pc, uint16_t type_index)
1671 : HExpression(Primitive::kPrimNot, SideEffects::None()),
1672 dex_pc_(dex_pc),
1673 type_index_(type_index) {
1674 SetRawInputAt(0, length);
1675 }
1676
1677 uint32_t GetDexPc() const { return dex_pc_; }
1678 uint16_t GetTypeIndex() const { return type_index_; }
1679
1680 // Calls runtime so needs an environment.
1681 virtual bool NeedsEnvironment() const { return true; }
1682
1683 DECLARE_INSTRUCTION(NewArray);
1684
1685 private:
1686 const uint32_t dex_pc_;
1687 const uint16_t type_index_;
1688
1689 DISALLOW_COPY_AND_ASSIGN(HNewArray);
1690};
1691
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001692class HAdd : public HBinaryOperation {
1693 public:
1694 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1695 : HBinaryOperation(result_type, left, right) {}
1696
1697 virtual bool IsCommutative() { return true; }
1698
Roland Levillain93445682014-10-06 19:24:02 +01001699 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1700 return x + y;
1701 }
1702 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1703 return x + y;
1704 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001705
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001706 DECLARE_INSTRUCTION(Add);
1707
1708 private:
1709 DISALLOW_COPY_AND_ASSIGN(HAdd);
1710};
1711
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001712class HSub : public HBinaryOperation {
1713 public:
1714 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1715 : HBinaryOperation(result_type, left, right) {}
1716
Roland Levillain93445682014-10-06 19:24:02 +01001717 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1718 return x - y;
1719 }
1720 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1721 return x - y;
1722 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001723
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001724 DECLARE_INSTRUCTION(Sub);
1725
1726 private:
1727 DISALLOW_COPY_AND_ASSIGN(HSub);
1728};
1729
Calin Juravle34bacdf2014-10-07 20:23:36 +01001730class HMul : public HBinaryOperation {
1731 public:
1732 HMul(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1733 : HBinaryOperation(result_type, left, right) {}
1734
1735 virtual bool IsCommutative() { return true; }
1736
1737 virtual int32_t Evaluate(int32_t x, int32_t y) const { return x * y; }
1738 virtual int64_t Evaluate(int64_t x, int64_t y) const { return x * y; }
1739
1740 DECLARE_INSTRUCTION(Mul);
1741
1742 private:
1743 DISALLOW_COPY_AND_ASSIGN(HMul);
1744};
1745
Calin Juravle7c4954d2014-10-28 16:57:40 +00001746class HDiv : public HBinaryOperation {
1747 public:
1748 HDiv(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1749 : HBinaryOperation(result_type, left, right) {}
1750
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00001751 virtual int32_t Evaluate(int32_t x, int32_t y) const {
1752 // Our graph structure ensures we never have 0 for `y` during constant folding.
1753 DCHECK_NE(y, 0);
1754 // Special case -1 to avoid getting a SIGFPE on x86.
1755 return (y == -1) ? -x : x / y;
1756 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00001757 virtual int64_t Evaluate(int64_t x, int64_t y) const { return x / y; }
1758
1759 DECLARE_INSTRUCTION(Div);
1760
1761 private:
1762 DISALLOW_COPY_AND_ASSIGN(HDiv);
1763};
1764
Calin Juravled0d48522014-11-04 16:40:20 +00001765class HDivZeroCheck : public HExpression<1> {
1766 public:
1767 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
1768 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
1769 SetRawInputAt(0, value);
1770 }
1771
1772 bool CanBeMoved() const OVERRIDE { return true; }
1773
1774 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
1775 UNUSED(other);
1776 return true;
1777 }
1778
1779 bool NeedsEnvironment() const OVERRIDE { return true; }
1780 bool CanThrow() const OVERRIDE { return true; }
1781
1782 uint32_t GetDexPc() const { return dex_pc_; }
1783
1784 DECLARE_INSTRUCTION(DivZeroCheck);
1785
1786 private:
1787 const uint32_t dex_pc_;
1788
1789 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
1790};
1791
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001792// The value of a parameter in this method. Its location depends on
1793// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07001794class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001795 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001796 HParameterValue(uint8_t index, Primitive::Type parameter_type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001797 : HExpression(parameter_type, SideEffects::None()), index_(index) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001798
1799 uint8_t GetIndex() const { return index_; }
1800
1801 DECLARE_INSTRUCTION(ParameterValue);
1802
1803 private:
1804 // The index of this parameter in the parameters list. Must be less
1805 // than HGraph::number_of_in_vregs_;
1806 const uint8_t index_;
1807
1808 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
1809};
1810
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001811class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001812 public:
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001813 explicit HNot(Primitive::Type result_type, HInstruction* input)
1814 : HUnaryOperation(result_type, input) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001815
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001816 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001817 virtual bool InstructionDataEquals(HInstruction* other) const {
1818 UNUSED(other);
1819 return true;
1820 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001821
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001822 virtual int32_t Evaluate(int32_t x) const OVERRIDE { return ~x; }
1823 virtual int64_t Evaluate(int64_t x) const OVERRIDE { return ~x; }
1824
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001825 DECLARE_INSTRUCTION(Not);
1826
1827 private:
1828 DISALLOW_COPY_AND_ASSIGN(HNot);
1829};
1830
Roland Levillaindff1f282014-11-05 14:15:05 +00001831class HTypeConversion : public HExpression<1> {
1832 public:
1833 // Instantiate a type conversion of `input` to `result_type`.
1834 HTypeConversion(Primitive::Type result_type, HInstruction* input)
1835 : HExpression(result_type, SideEffects::None()) {
1836 SetRawInputAt(0, input);
1837 DCHECK_NE(input->GetType(), result_type);
1838 }
1839
1840 HInstruction* GetInput() const { return InputAt(0); }
1841 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
1842 Primitive::Type GetResultType() const { return GetType(); }
1843
1844 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00001845 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00001846
1847 DECLARE_INSTRUCTION(TypeConversion);
1848
1849 private:
1850 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
1851};
1852
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001853class HPhi : public HInstruction {
1854 public:
1855 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001856 : HInstruction(SideEffects::None()),
1857 inputs_(arena, number_of_inputs),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001858 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001859 type_(type),
1860 is_live_(false) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001861 inputs_.SetSize(number_of_inputs);
1862 }
1863
1864 virtual size_t InputCount() const { return inputs_.Size(); }
1865 virtual HInstruction* InputAt(size_t i) const { return inputs_.Get(i); }
1866
1867 virtual void SetRawInputAt(size_t index, HInstruction* input) {
1868 inputs_.Put(index, input);
1869 }
1870
1871 void AddInput(HInstruction* input);
1872
1873 virtual Primitive::Type GetType() const { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001874 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001875
1876 uint32_t GetRegNumber() const { return reg_number_; }
1877
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001878 void SetDead() { is_live_ = false; }
1879 void SetLive() { is_live_ = true; }
1880 bool IsDead() const { return !is_live_; }
1881 bool IsLive() const { return is_live_; }
1882
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001883 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001884
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001885 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001886 GrowableArray<HInstruction*> inputs_;
1887 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001888 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001889 bool is_live_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001890
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001891 DISALLOW_COPY_AND_ASSIGN(HPhi);
1892};
1893
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001894class HNullCheck : public HExpression<1> {
1895 public:
1896 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001897 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001898 SetRawInputAt(0, value);
1899 }
1900
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001901 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001902 virtual bool InstructionDataEquals(HInstruction* other) const {
1903 UNUSED(other);
1904 return true;
1905 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001906
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001907 virtual bool NeedsEnvironment() const { return true; }
1908
Roland Levillaine161a2a2014-10-03 12:45:18 +01001909 virtual bool CanThrow() const { return true; }
1910
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001911 uint32_t GetDexPc() const { return dex_pc_; }
1912
1913 DECLARE_INSTRUCTION(NullCheck);
1914
1915 private:
1916 const uint32_t dex_pc_;
1917
1918 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
1919};
1920
1921class FieldInfo : public ValueObject {
1922 public:
Roland Levillain5799fc02014-09-25 12:15:20 +01001923 FieldInfo(MemberOffset field_offset, Primitive::Type field_type)
Nicolas Geoffray39468442014-09-02 15:17:15 +01001924 : field_offset_(field_offset), field_type_(field_type) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001925
1926 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001927 Primitive::Type GetFieldType() const { return field_type_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001928
1929 private:
1930 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01001931 const Primitive::Type field_type_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001932};
1933
1934class HInstanceFieldGet : public HExpression<1> {
1935 public:
1936 HInstanceFieldGet(HInstruction* value,
1937 Primitive::Type field_type,
1938 MemberOffset field_offset)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001939 : HExpression(field_type, SideEffects::DependsOnSomething()),
1940 field_info_(field_offset, field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001941 SetRawInputAt(0, value);
1942 }
1943
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001944 virtual bool CanBeMoved() const { return true; }
1945 virtual bool InstructionDataEquals(HInstruction* other) const {
1946 size_t other_offset = other->AsInstanceFieldGet()->GetFieldOffset().SizeValue();
1947 return other_offset == GetFieldOffset().SizeValue();
1948 }
1949
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001950 virtual size_t ComputeHashCode() const {
1951 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
1952 }
1953
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001954 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001955 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001956
1957 DECLARE_INSTRUCTION(InstanceFieldGet);
1958
1959 private:
1960 const FieldInfo field_info_;
1961
1962 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
1963};
1964
1965class HInstanceFieldSet : public HTemplateInstruction<2> {
1966 public:
1967 HInstanceFieldSet(HInstruction* object,
1968 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01001969 Primitive::Type field_type,
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001970 MemberOffset field_offset)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001971 : HTemplateInstruction(SideEffects::ChangesSomething()),
1972 field_info_(field_offset, field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001973 SetRawInputAt(0, object);
1974 SetRawInputAt(1, value);
1975 }
1976
1977 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001978 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001979
1980 DECLARE_INSTRUCTION(InstanceFieldSet);
1981
1982 private:
1983 const FieldInfo field_info_;
1984
1985 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
1986};
1987
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001988class HArrayGet : public HExpression<2> {
1989 public:
1990 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001991 : HExpression(type, SideEffects::DependsOnSomething()) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001992 SetRawInputAt(0, array);
1993 SetRawInputAt(1, index);
1994 }
1995
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001996 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001997 virtual bool InstructionDataEquals(HInstruction* other) const {
1998 UNUSED(other);
1999 return true;
2000 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002001 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002002
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002003 DECLARE_INSTRUCTION(ArrayGet);
2004
2005 private:
2006 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
2007};
2008
2009class HArraySet : public HTemplateInstruction<3> {
2010 public:
2011 HArraySet(HInstruction* array,
2012 HInstruction* index,
2013 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002014 Primitive::Type expected_component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002015 uint32_t dex_pc)
2016 : HTemplateInstruction(SideEffects::ChangesSomething()),
2017 dex_pc_(dex_pc),
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002018 expected_component_type_(expected_component_type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002019 SetRawInputAt(0, array);
2020 SetRawInputAt(1, index);
2021 SetRawInputAt(2, value);
2022 }
2023
2024 virtual bool NeedsEnvironment() const {
2025 // We currently always call a runtime method to catch array store
2026 // exceptions.
2027 return InputAt(2)->GetType() == Primitive::kPrimNot;
2028 }
2029
2030 uint32_t GetDexPc() const { return dex_pc_; }
2031
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002032 HInstruction* GetValue() const { return InputAt(2); }
2033
2034 Primitive::Type GetComponentType() const {
2035 // The Dex format does not type floating point index operations. Since the
2036 // `expected_component_type_` is set during building and can therefore not
2037 // be correct, we also check what is the value type. If it is a floating
2038 // point type, we must use that type.
2039 Primitive::Type value_type = GetValue()->GetType();
2040 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
2041 ? value_type
2042 : expected_component_type_;
2043 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002044
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002045 DECLARE_INSTRUCTION(ArraySet);
2046
2047 private:
2048 const uint32_t dex_pc_;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002049 const Primitive::Type expected_component_type_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002050
2051 DISALLOW_COPY_AND_ASSIGN(HArraySet);
2052};
2053
2054class HArrayLength : public HExpression<1> {
2055 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002056 explicit HArrayLength(HInstruction* array)
2057 : HExpression(Primitive::kPrimInt, SideEffects::None()) {
2058 // Note that arrays do not change length, so the instruction does not
2059 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002060 SetRawInputAt(0, array);
2061 }
2062
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002063 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002064 virtual bool InstructionDataEquals(HInstruction* other) const {
2065 UNUSED(other);
2066 return true;
2067 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002068
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002069 DECLARE_INSTRUCTION(ArrayLength);
2070
2071 private:
2072 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
2073};
2074
2075class HBoundsCheck : public HExpression<2> {
2076 public:
2077 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002078 : HExpression(index->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002079 DCHECK(index->GetType() == Primitive::kPrimInt);
2080 SetRawInputAt(0, index);
2081 SetRawInputAt(1, length);
2082 }
2083
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002084 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002085 virtual bool InstructionDataEquals(HInstruction* other) const {
2086 UNUSED(other);
2087 return true;
2088 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002089
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002090 virtual bool NeedsEnvironment() const { return true; }
2091
Roland Levillaine161a2a2014-10-03 12:45:18 +01002092 virtual bool CanThrow() const { return true; }
2093
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002094 uint32_t GetDexPc() const { return dex_pc_; }
2095
2096 DECLARE_INSTRUCTION(BoundsCheck);
2097
2098 private:
2099 const uint32_t dex_pc_;
2100
2101 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
2102};
2103
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002104/**
2105 * Some DEX instructions are folded into multiple HInstructions that need
2106 * to stay live until the last HInstruction. This class
2107 * is used as a marker for the baseline compiler to ensure its preceding
2108 * HInstruction stays live. `index` is the temporary number that is used
2109 * for knowing the stack offset where to store the instruction.
2110 */
2111class HTemporary : public HTemplateInstruction<0> {
2112 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002113 explicit HTemporary(size_t index) : HTemplateInstruction(SideEffects::None()), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002114
2115 size_t GetIndex() const { return index_; }
2116
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00002117 Primitive::Type GetType() const OVERRIDE { return GetPrevious()->GetType(); }
2118
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002119 DECLARE_INSTRUCTION(Temporary);
2120
2121 private:
2122 const size_t index_;
2123
2124 DISALLOW_COPY_AND_ASSIGN(HTemporary);
2125};
2126
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002127class HSuspendCheck : public HTemplateInstruction<0> {
2128 public:
2129 explicit HSuspendCheck(uint32_t dex_pc)
Nicolas Geoffray9ebc72c2014-09-25 16:33:42 +01002130 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002131
2132 virtual bool NeedsEnvironment() const {
2133 return true;
2134 }
2135
2136 uint32_t GetDexPc() const { return dex_pc_; }
2137
2138 DECLARE_INSTRUCTION(SuspendCheck);
2139
2140 private:
2141 const uint32_t dex_pc_;
2142
2143 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
2144};
2145
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002146/**
2147 * Instruction to load a Class object.
2148 */
2149class HLoadClass : public HExpression<0> {
2150 public:
2151 HLoadClass(uint16_t type_index,
2152 bool is_referrers_class,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002153 uint32_t dex_pc)
2154 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2155 type_index_(type_index),
2156 is_referrers_class_(is_referrers_class),
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002157 dex_pc_(dex_pc),
2158 generate_clinit_check_(false) {}
2159
2160 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002161
2162 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2163 return other->AsLoadClass()->type_index_ == type_index_;
2164 }
2165
2166 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
2167
2168 uint32_t GetDexPc() const { return dex_pc_; }
2169 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002170 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002171
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002172 bool NeedsEnvironment() const OVERRIDE {
2173 // Will call runtime and load the class if the class is not loaded yet.
2174 // TODO: finer grain decision.
2175 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002176 }
2177
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002178 bool MustGenerateClinitCheck() const {
2179 return generate_clinit_check_;
2180 }
2181
2182 void SetMustGenerateClinitCheck() {
2183 generate_clinit_check_ = true;
2184 }
2185
2186 bool CanCallRuntime() const {
2187 return MustGenerateClinitCheck() || !is_referrers_class_;
2188 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002189
2190 DECLARE_INSTRUCTION(LoadClass);
2191
2192 private:
2193 const uint16_t type_index_;
2194 const bool is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002195 const uint32_t dex_pc_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002196 // Whether this instruction must generate the initialization check.
2197 // Used for code generation.
2198 bool generate_clinit_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002199
2200 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
2201};
2202
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002203class HLoadString : public HExpression<0> {
2204 public:
2205 HLoadString(uint32_t string_index, uint32_t dex_pc)
2206 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2207 string_index_(string_index),
2208 dex_pc_(dex_pc) {}
2209
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002210 bool CanBeMoved() const OVERRIDE { return true; }
2211
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002212 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2213 return other->AsLoadString()->string_index_ == string_index_;
2214 }
2215
2216 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
2217
2218 uint32_t GetDexPc() const { return dex_pc_; }
2219 uint32_t GetStringIndex() const { return string_index_; }
2220
2221 // TODO: Can we deopt or debug when we resolve a string?
2222 bool NeedsEnvironment() const OVERRIDE { return false; }
2223
2224 DECLARE_INSTRUCTION(LoadString);
2225
2226 private:
2227 const uint32_t string_index_;
2228 const uint32_t dex_pc_;
2229
2230 DISALLOW_COPY_AND_ASSIGN(HLoadString);
2231};
2232
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002233// TODO: Pass this check to HInvokeStatic nodes.
2234/**
2235 * Performs an initialization check on its Class object input.
2236 */
2237class HClinitCheck : public HExpression<1> {
2238 public:
2239 explicit HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
2240 : HExpression(Primitive::kPrimNot, SideEffects::All()),
2241 dex_pc_(dex_pc) {
2242 SetRawInputAt(0, constant);
2243 }
2244
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002245 bool CanBeMoved() const OVERRIDE { return true; }
2246 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2247 UNUSED(other);
2248 return true;
2249 }
2250
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002251 bool NeedsEnvironment() const OVERRIDE {
2252 // May call runtime to initialize the class.
2253 return true;
2254 }
2255
2256 uint32_t GetDexPc() const { return dex_pc_; }
2257
2258 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
2259
2260 DECLARE_INSTRUCTION(ClinitCheck);
2261
2262 private:
2263 const uint32_t dex_pc_;
2264
2265 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
2266};
2267
2268class HStaticFieldGet : public HExpression<1> {
2269 public:
2270 HStaticFieldGet(HInstruction* cls,
2271 Primitive::Type field_type,
2272 MemberOffset field_offset)
2273 : HExpression(field_type, SideEffects::DependsOnSomething()),
2274 field_info_(field_offset, field_type) {
2275 SetRawInputAt(0, cls);
2276 }
2277
2278 bool CanBeMoved() const OVERRIDE { return true; }
2279 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2280 size_t other_offset = other->AsStaticFieldGet()->GetFieldOffset().SizeValue();
2281 return other_offset == GetFieldOffset().SizeValue();
2282 }
2283
2284 size_t ComputeHashCode() const OVERRIDE {
2285 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
2286 }
2287
2288 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
2289 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
2290
2291 DECLARE_INSTRUCTION(StaticFieldGet);
2292
2293 private:
2294 const FieldInfo field_info_;
2295
2296 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
2297};
2298
2299class HStaticFieldSet : public HTemplateInstruction<2> {
2300 public:
2301 HStaticFieldSet(HInstruction* cls,
2302 HInstruction* value,
2303 Primitive::Type field_type,
2304 MemberOffset field_offset)
2305 : HTemplateInstruction(SideEffects::ChangesSomething()),
2306 field_info_(field_offset, field_type) {
2307 SetRawInputAt(0, cls);
2308 SetRawInputAt(1, value);
2309 }
2310
2311 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
2312 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
2313
2314 DECLARE_INSTRUCTION(StaticFieldSet);
2315
2316 private:
2317 const FieldInfo field_info_;
2318
2319 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
2320};
2321
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002322// Implement the move-exception DEX instruction.
2323class HLoadException : public HExpression<0> {
2324 public:
2325 HLoadException() : HExpression(Primitive::kPrimNot, SideEffects::None()) {}
2326
2327 DECLARE_INSTRUCTION(LoadException);
2328
2329 private:
2330 DISALLOW_COPY_AND_ASSIGN(HLoadException);
2331};
2332
2333class HThrow : public HTemplateInstruction<1> {
2334 public:
2335 HThrow(HInstruction* exception, uint32_t dex_pc)
2336 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {
2337 SetRawInputAt(0, exception);
2338 }
2339
2340 bool IsControlFlow() const OVERRIDE { return true; }
2341
2342 bool NeedsEnvironment() const OVERRIDE { return true; }
2343
2344 uint32_t GetDexPc() const { return dex_pc_; }
2345
2346 DECLARE_INSTRUCTION(Throw);
2347
2348 private:
2349 uint32_t dex_pc_;
2350
2351 DISALLOW_COPY_AND_ASSIGN(HThrow);
2352};
2353
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002354class HTypeCheck : public HExpression<2> {
2355 public:
2356 explicit HTypeCheck(HInstruction* object,
2357 HLoadClass* constant,
2358 bool class_is_final,
2359 uint32_t dex_pc)
2360 : HExpression(Primitive::kPrimBoolean, SideEffects::None()),
2361 class_is_final_(class_is_final),
2362 dex_pc_(dex_pc) {
2363 SetRawInputAt(0, object);
2364 SetRawInputAt(1, constant);
2365 }
2366
2367 bool CanBeMoved() const OVERRIDE { return true; }
2368
2369 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2370 UNUSED(other);
2371 return true;
2372 }
2373
2374 bool NeedsEnvironment() const OVERRIDE {
2375 // TODO: Can we debug when doing a runtime instanceof check?
2376 return false;
2377 }
2378
2379 uint32_t GetDexPc() const { return dex_pc_; }
2380
2381 bool IsClassFinal() const { return class_is_final_; }
2382
2383 DECLARE_INSTRUCTION(TypeCheck);
2384
2385 private:
2386 const bool class_is_final_;
2387 const uint32_t dex_pc_;
2388
2389 DISALLOW_COPY_AND_ASSIGN(HTypeCheck);
2390};
2391
2392
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002393class MoveOperands : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002394 public:
Nicolas Geoffray740475d2014-09-29 10:33:25 +01002395 MoveOperands(Location source, Location destination, HInstruction* instruction)
2396 : source_(source), destination_(destination), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002397
2398 Location GetSource() const { return source_; }
2399 Location GetDestination() const { return destination_; }
2400
2401 void SetSource(Location value) { source_ = value; }
2402 void SetDestination(Location value) { destination_ = value; }
2403
2404 // The parallel move resolver marks moves as "in-progress" by clearing the
2405 // destination (but not the source).
2406 Location MarkPending() {
2407 DCHECK(!IsPending());
2408 Location dest = destination_;
2409 destination_ = Location::NoLocation();
2410 return dest;
2411 }
2412
2413 void ClearPending(Location dest) {
2414 DCHECK(IsPending());
2415 destination_ = dest;
2416 }
2417
2418 bool IsPending() const {
2419 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
2420 return destination_.IsInvalid() && !source_.IsInvalid();
2421 }
2422
2423 // True if this blocks a move from the given location.
2424 bool Blocks(Location loc) const {
2425 return !IsEliminated() && source_.Equals(loc);
2426 }
2427
2428 // A move is redundant if it's been eliminated, if its source and
2429 // destination are the same, or if its destination is unneeded.
2430 bool IsRedundant() const {
2431 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
2432 }
2433
2434 // We clear both operands to indicate move that's been eliminated.
2435 void Eliminate() {
2436 source_ = destination_ = Location::NoLocation();
2437 }
2438
2439 bool IsEliminated() const {
2440 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
2441 return source_.IsInvalid();
2442 }
2443
Nicolas Geoffray740475d2014-09-29 10:33:25 +01002444 HInstruction* GetInstruction() const { return instruction_; }
2445
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002446 private:
2447 Location source_;
2448 Location destination_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01002449 // The instruction this move is assocatied with. Null when this move is
2450 // for moving an input in the expected locations of user (including a phi user).
2451 // This is only used in debug mode, to ensure we do not connect interval siblings
2452 // in the same parallel move.
2453 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002454
2455 DISALLOW_COPY_AND_ASSIGN(MoveOperands);
2456};
2457
2458static constexpr size_t kDefaultNumberOfMoves = 4;
2459
2460class HParallelMove : public HTemplateInstruction<0> {
2461 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002462 explicit HParallelMove(ArenaAllocator* arena)
2463 : HTemplateInstruction(SideEffects::None()), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002464
2465 void AddMove(MoveOperands* move) {
Nicolas Geoffray740475d2014-09-29 10:33:25 +01002466 if (kIsDebugBuild && move->GetInstruction() != nullptr) {
2467 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
2468 DCHECK_NE(moves_.Get(i)->GetInstruction(), move->GetInstruction())
2469 << "Doing parallel moves for the same instruction.";
2470 }
2471 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002472 moves_.Add(move);
2473 }
2474
2475 MoveOperands* MoveOperandsAt(size_t index) const {
2476 return moves_.Get(index);
2477 }
2478
2479 size_t NumMoves() const { return moves_.Size(); }
2480
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002481 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002482
2483 private:
2484 GrowableArray<MoveOperands*> moves_;
2485
2486 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
2487};
2488
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002489class HGraphVisitor : public ValueObject {
2490 public:
Dave Allison20dfc792014-06-16 20:44:29 -07002491 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
2492 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002493
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002494 virtual void VisitInstruction(HInstruction* instruction) { UNUSED(instruction); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002495 virtual void VisitBasicBlock(HBasicBlock* block);
2496
Roland Levillain633021e2014-10-01 14:12:25 +01002497 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002498 void VisitInsertionOrder();
2499
Roland Levillain633021e2014-10-01 14:12:25 +01002500 // Visit the graph following dominator tree reverse post-order.
2501 void VisitReversePostOrder();
2502
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002503 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002504
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002505 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002506#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002507 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
2508
2509 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
2510
2511#undef DECLARE_VISIT_INSTRUCTION
2512
2513 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07002514 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002515
2516 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
2517};
2518
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002519class HGraphDelegateVisitor : public HGraphVisitor {
2520 public:
2521 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
2522 virtual ~HGraphDelegateVisitor() {}
2523
2524 // Visit functions that delegate to to super class.
2525#define DECLARE_VISIT_INSTRUCTION(name, super) \
2526 virtual void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
2527
2528 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
2529
2530#undef DECLARE_VISIT_INSTRUCTION
2531
2532 private:
2533 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
2534};
2535
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002536class HInsertionOrderIterator : public ValueObject {
2537 public:
2538 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
2539
2540 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
2541 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
2542 void Advance() { ++index_; }
2543
2544 private:
2545 const HGraph& graph_;
2546 size_t index_;
2547
2548 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
2549};
2550
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002551class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002552 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002553 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002554
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002555 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
2556 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002557 void Advance() { ++index_; }
2558
2559 private:
2560 const HGraph& graph_;
2561 size_t index_;
2562
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002563 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002564};
2565
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002566class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002567 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002568 explicit HPostOrderIterator(const HGraph& graph)
2569 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002570
2571 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002572 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002573 void Advance() { --index_; }
2574
2575 private:
2576 const HGraph& graph_;
2577 size_t index_;
2578
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002579 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002580};
2581
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002582} // namespace art
2583
2584#endif // ART_COMPILER_OPTIMIZING_NODES_H_