blob: 7c933aa4f1bbe2b4d39c43a307a905222df24778 [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.
82class HGraph : public ArenaObject {
83 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),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +010088 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +010089 number_of_vregs_(0),
90 number_of_in_vregs_(0),
Nicolas Geoffraye5038322014-07-04 09:41:32 +010091 number_of_temporaries_(0),
Dave Allison20dfc792014-06-16 20:44:29 -070092 current_instruction_id_(0) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +000093
Nicolas Geoffray787c3072014-03-17 10:20:19 +000094 ArenaAllocator* GetArena() const { return arena_; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +010095 const GrowableArray<HBasicBlock*>& GetBlocks() const { return blocks_; }
Roland Levillain93445682014-10-06 19:24:02 +010096 HBasicBlock* GetBlock(size_t id) const { return blocks_.Get(id); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +000097
Nicolas Geoffray787c3072014-03-17 10:20:19 +000098 HBasicBlock* GetEntryBlock() const { return entry_block_; }
99 HBasicBlock* GetExitBlock() const { return exit_block_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000100
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000101 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
102 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000103
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000104 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100105
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000106 void BuildDominatorTree();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100107 void TransformToSSA();
108 void SimplifyCFG();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000109
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100110 // Find all natural loops in this graph. Aborts computation and returns false
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100111 // if one loop is not natural, that is the header does not dominate the back
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100112 // edge.
113 bool FindNaturalLoops() const;
114
115 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
116 void SimplifyLoop(HBasicBlock* header);
117
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000118 int GetNextInstructionId() {
119 return current_instruction_id_++;
120 }
121
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100122 uint16_t GetMaximumNumberOfOutVRegs() const {
123 return maximum_number_of_out_vregs_;
124 }
125
126 void UpdateMaximumNumberOfOutVRegs(uint16_t new_value) {
127 maximum_number_of_out_vregs_ = std::max(new_value, maximum_number_of_out_vregs_);
128 }
129
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100130 void UpdateNumberOfTemporaries(size_t count) {
131 number_of_temporaries_ = std::max(count, number_of_temporaries_);
132 }
133
134 size_t GetNumberOfTemporaries() const {
135 return number_of_temporaries_;
136 }
137
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100138 void SetNumberOfVRegs(uint16_t number_of_vregs) {
139 number_of_vregs_ = number_of_vregs;
140 }
141
142 uint16_t GetNumberOfVRegs() const {
143 return number_of_vregs_;
144 }
145
146 void SetNumberOfInVRegs(uint16_t value) {
147 number_of_in_vregs_ = value;
148 }
149
150 uint16_t GetNumberOfInVRegs() const {
151 return number_of_in_vregs_;
152 }
153
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100154 uint16_t GetNumberOfLocalVRegs() const {
155 return number_of_vregs_ - number_of_in_vregs_;
156 }
157
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100158 const GrowableArray<HBasicBlock*>& GetReversePostOrder() const {
159 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100160 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100161
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000162 private:
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000163 HBasicBlock* FindCommonDominator(HBasicBlock* first, HBasicBlock* second) const;
164 void VisitBlockForDominatorTree(HBasicBlock* block,
165 HBasicBlock* predecessor,
166 GrowableArray<size_t>* visits);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100167 void FindBackEdges(ArenaBitVector* visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000168 void VisitBlockForBackEdges(HBasicBlock* block,
169 ArenaBitVector* visited,
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100170 ArenaBitVector* visiting);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000171 void RemoveDeadBlocks(const ArenaBitVector& visited) const;
172
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000173 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000174
175 // List of blocks in insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000176 GrowableArray<HBasicBlock*> blocks_;
177
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100178 // List of blocks to perform a reverse post order tree traversal.
179 GrowableArray<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000180
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000181 HBasicBlock* entry_block_;
182 HBasicBlock* exit_block_;
183
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100184 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100185 uint16_t maximum_number_of_out_vregs_;
186
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100187 // The number of virtual registers in this method. Contains the parameters.
188 uint16_t number_of_vregs_;
189
190 // The number of virtual registers used by parameters of this method.
191 uint16_t number_of_in_vregs_;
192
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100193 // The number of temporaries that will be needed for the baseline compiler.
194 size_t number_of_temporaries_;
195
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000196 // The current id to assign to a newly added instruction. See HInstruction.id_.
197 int current_instruction_id_;
198
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000199 DISALLOW_COPY_AND_ASSIGN(HGraph);
200};
201
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000202class HLoopInformation : public ArenaObject {
203 public:
204 HLoopInformation(HBasicBlock* header, HGraph* graph)
205 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100206 suspend_check_(nullptr),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100207 back_edges_(graph->GetArena(), kDefaultNumberOfBackEdges),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100208 // Make bit vector growable, as the number of blocks may change.
209 blocks_(graph->GetArena(), graph->GetBlocks().Size(), true) {}
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100210
211 HBasicBlock* GetHeader() const {
212 return header_;
213 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000214
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100215 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
216 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
217 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
218
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000219 void AddBackEdge(HBasicBlock* back_edge) {
220 back_edges_.Add(back_edge);
221 }
222
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100223 void RemoveBackEdge(HBasicBlock* back_edge) {
224 back_edges_.Delete(back_edge);
225 }
226
227 bool IsBackEdge(HBasicBlock* block) {
228 for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) {
229 if (back_edges_.Get(i) == block) return true;
230 }
231 return false;
232 }
233
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000234 int NumberOfBackEdges() const {
235 return back_edges_.Size();
236 }
237
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100238 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100239
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100240 const GrowableArray<HBasicBlock*>& GetBackEdges() const {
241 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100242 }
243
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100244 void ClearBackEdges() {
245 back_edges_.Reset();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100246 }
247
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100248 // Find blocks that are part of this loop. Returns whether the loop is a natural loop,
249 // that is the header dominates the back edge.
250 bool Populate();
251
252 // Returns whether this loop information contains `block`.
253 // Note that this loop information *must* be populated before entering this function.
254 bool Contains(const HBasicBlock& block) const;
255
256 // Returns whether this loop information is an inner loop of `other`.
257 // Note that `other` *must* be populated before entering this function.
258 bool IsIn(const HLoopInformation& other) const;
259
260 const ArenaBitVector& GetBlocks() const { return blocks_; }
261
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000262 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100263 // Internal recursive implementation of `Populate`.
264 void PopulateRecursive(HBasicBlock* block);
265
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000266 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100267 HSuspendCheck* suspend_check_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000268 GrowableArray<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100269 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000270
271 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
272};
273
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100274static constexpr size_t kNoLifetime = -1;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100275static constexpr uint32_t kNoDexPc = -1;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100276
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000277// A block in a method. Contains the list of instructions represented
278// as a double linked list. Each block knows its predecessors and
279// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100280
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000281class HBasicBlock : public ArenaObject {
282 public:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100283 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000284 : graph_(graph),
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000285 predecessors_(graph->GetArena(), kDefaultNumberOfPredecessors),
286 successors_(graph->GetArena(), kDefaultNumberOfSuccessors),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000287 loop_information_(nullptr),
288 dominator_(nullptr),
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100289 dominated_blocks_(graph->GetArena(), kDefaultNumberOfDominatedBlocks),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100290 block_id_(-1),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100291 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100292 lifetime_start_(kNoLifetime),
293 lifetime_end_(kNoLifetime) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000294
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100295 const GrowableArray<HBasicBlock*>& GetPredecessors() const {
296 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000297 }
298
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100299 const GrowableArray<HBasicBlock*>& GetSuccessors() const {
300 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000301 }
302
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100303 const GrowableArray<HBasicBlock*>& GetDominatedBlocks() const {
304 return dominated_blocks_;
305 }
306
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100307 bool IsEntryBlock() const {
308 return graph_->GetEntryBlock() == this;
309 }
310
311 bool IsExitBlock() const {
312 return graph_->GetExitBlock() == this;
313 }
314
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000315 void AddBackEdge(HBasicBlock* back_edge) {
316 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000317 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000318 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100319 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000320 loop_information_->AddBackEdge(back_edge);
321 }
322
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000323 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000324
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000325 int GetBlockId() const { return block_id_; }
326 void SetBlockId(int id) { block_id_ = id; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000327
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000328 HBasicBlock* GetDominator() const { return dominator_; }
329 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100330 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.Add(block); }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000331
332 int NumberOfBackEdges() const {
333 return loop_information_ == nullptr
334 ? 0
335 : loop_information_->NumberOfBackEdges();
336 }
337
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100338 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
339 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100340 const HInstructionList& GetInstructions() const { return instructions_; }
341 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100342 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000343
344 void AddSuccessor(HBasicBlock* block) {
345 successors_.Add(block);
346 block->predecessors_.Add(this);
347 }
348
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100349 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
350 size_t successor_index = GetSuccessorIndexOf(existing);
351 DCHECK_NE(successor_index, static_cast<size_t>(-1));
352 existing->RemovePredecessor(this);
353 new_block->predecessors_.Add(this);
354 successors_.Put(successor_index, new_block);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000355 }
356
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100357 void RemovePredecessor(HBasicBlock* block) {
358 predecessors_.Delete(block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100359 }
360
361 void ClearAllPredecessors() {
362 predecessors_.Reset();
363 }
364
365 void AddPredecessor(HBasicBlock* block) {
366 predecessors_.Add(block);
367 block->successors_.Add(this);
368 }
369
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100370 void SwapPredecessors() {
Nicolas Geoffrayc83d4412014-09-18 16:46:20 +0100371 DCHECK_EQ(predecessors_.Size(), 2u);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100372 HBasicBlock* temp = predecessors_.Get(0);
373 predecessors_.Put(0, predecessors_.Get(1));
374 predecessors_.Put(1, temp);
375 }
376
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100377 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) {
378 for (size_t i = 0, e = predecessors_.Size(); i < e; ++i) {
379 if (predecessors_.Get(i) == predecessor) {
380 return i;
381 }
382 }
383 return -1;
384 }
385
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100386 size_t GetSuccessorIndexOf(HBasicBlock* successor) {
387 for (size_t i = 0, e = successors_.Size(); i < e; ++i) {
388 if (successors_.Get(i) == successor) {
389 return i;
390 }
391 }
392 return -1;
393 }
394
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000395 void AddInstruction(HInstruction* instruction);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100396 void RemoveInstruction(HInstruction* instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100397 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +0100398 // Replace instruction `initial` with `replacement` within this block.
399 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
400 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100401 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100402 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100403 void RemovePhi(HPhi* phi);
404
405 bool IsLoopHeader() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100406 return (loop_information_ != nullptr) && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100407 }
408
Roland Levillain6b879dd2014-09-22 17:13:44 +0100409 bool IsLoopPreHeaderFirstPredecessor() const {
410 DCHECK(IsLoopHeader());
411 DCHECK(!GetPredecessors().IsEmpty());
412 return GetPredecessors().Get(0) == GetLoopInformation()->GetPreHeader();
413 }
414
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100415 HLoopInformation* GetLoopInformation() const {
416 return loop_information_;
417 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000418
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100419 // Set the loop_information_ on this block. This method overrides the current
420 // loop_information if it is an outer loop of the passed loop information.
421 void SetInLoop(HLoopInformation* info) {
422 if (IsLoopHeader()) {
423 // Nothing to do. This just means `info` is an outer loop.
424 } else if (loop_information_ == nullptr) {
425 loop_information_ = info;
426 } else if (loop_information_->Contains(*info->GetHeader())) {
427 // Block is currently part of an outer loop. Make it part of this inner loop.
428 // Note that a non loop header having a loop information means this loop information
429 // has already been populated
430 loop_information_ = info;
431 } else {
432 // Block is part of an inner loop. Do not update the loop information.
433 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
434 // at this point, because this method is being called while populating `info`.
435 }
436 }
437
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100438 bool IsInLoop() const { return loop_information_ != nullptr; }
439
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100440 // Returns wheter this block dominates the blocked passed as parameter.
441 bool Dominates(HBasicBlock* block) const;
442
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100443 size_t GetLifetimeStart() const { return lifetime_start_; }
444 size_t GetLifetimeEnd() const { return lifetime_end_; }
445
446 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
447 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
448
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100449 uint32_t GetDexPc() const { return dex_pc_; }
450
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000451 private:
452 HGraph* const graph_;
453 GrowableArray<HBasicBlock*> predecessors_;
454 GrowableArray<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100455 HInstructionList instructions_;
456 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000457 HLoopInformation* loop_information_;
458 HBasicBlock* dominator_;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100459 GrowableArray<HBasicBlock*> dominated_blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000460 int block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100461 // The dex program counter of the first instruction of this block.
462 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100463 size_t lifetime_start_;
464 size_t lifetime_end_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000465
466 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
467};
468
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100469#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
470 M(Add, BinaryOperation) \
471 M(Condition, BinaryOperation) \
472 M(Equal, Condition) \
473 M(NotEqual, Condition) \
474 M(LessThan, Condition) \
475 M(LessThanOrEqual, Condition) \
476 M(GreaterThan, Condition) \
477 M(GreaterThanOrEqual, Condition) \
478 M(Exit, Instruction) \
479 M(Goto, Instruction) \
480 M(If, Instruction) \
481 M(IntConstant, Constant) \
482 M(InvokeStatic, Invoke) \
483 M(InvokeVirtual, Invoke) \
484 M(LoadLocal, Instruction) \
485 M(Local, Instruction) \
486 M(LongConstant, Constant) \
487 M(NewInstance, Instruction) \
488 M(Not, Instruction) \
489 M(ParameterValue, Instruction) \
490 M(ParallelMove, Instruction) \
491 M(Phi, Instruction) \
492 M(Return, Instruction) \
493 M(ReturnVoid, Instruction) \
494 M(StoreLocal, Instruction) \
495 M(Sub, BinaryOperation) \
496 M(Compare, BinaryOperation) \
497 M(InstanceFieldGet, Instruction) \
498 M(InstanceFieldSet, Instruction) \
499 M(ArrayGet, Instruction) \
500 M(ArraySet, Instruction) \
501 M(ArrayLength, Instruction) \
502 M(BoundsCheck, Instruction) \
503 M(NullCheck, Instruction) \
504 M(Temporary, Instruction) \
505 M(SuspendCheck, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +0100506 M(Mul, BinaryOperation) \
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100507 M(Neg, UnaryOperation) \
508 M(FloatConstant, Constant) \
509 M(DoubleConstant, Constant) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000510
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100511#define FOR_EACH_INSTRUCTION(M) \
512 FOR_EACH_CONCRETE_INSTRUCTION(M) \
513 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +0100514 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +0100515 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100516 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -0700517
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100518#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000519FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
520#undef FORWARD_DECLARATION
521
Roland Levillainccc07a92014-09-16 14:48:16 +0100522#define DECLARE_INSTRUCTION(type) \
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100523 virtual InstructionKind GetKind() const { return k##type; } \
Roland Levillainccc07a92014-09-16 14:48:16 +0100524 virtual const char* DebugName() const { return #type; } \
525 virtual const H##type* As##type() const OVERRIDE { return this; } \
526 virtual H##type* As##type() OVERRIDE { return this; } \
527 virtual bool InstructionTypeEquals(HInstruction* other) const { \
528 return other->Is##type(); \
529 } \
530 virtual void Accept(HGraphVisitor* visitor)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000531
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100532template <typename T>
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000533class HUseListNode : public ArenaObject {
534 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100535 HUseListNode(T* user, size_t index, HUseListNode* tail)
Dave Allison20dfc792014-06-16 20:44:29 -0700536 : user_(user), index_(index), tail_(tail) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000537
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000538 HUseListNode* GetTail() const { return tail_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100539 T* GetUser() const { return user_; }
540 size_t GetIndex() const { return index_; }
541
542 void SetTail(HUseListNode<T>* node) { tail_ = node; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000543
544 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100545 T* const user_;
546 const size_t index_;
547 HUseListNode<T>* tail_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000548
549 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
550};
551
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100552// Represents the side effects an instruction may have.
553class SideEffects : public ValueObject {
554 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100555 SideEffects() : flags_(0) {}
556
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100557 static SideEffects None() {
558 return SideEffects(0);
559 }
560
561 static SideEffects All() {
562 return SideEffects(ChangesSomething().flags_ | DependsOnSomething().flags_);
563 }
564
565 static SideEffects ChangesSomething() {
566 return SideEffects((1 << kFlagChangesCount) - 1);
567 }
568
569 static SideEffects DependsOnSomething() {
570 int count = kFlagDependsOnCount - kFlagChangesCount;
571 return SideEffects(((1 << count) - 1) << kFlagChangesCount);
572 }
573
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100574 SideEffects Union(SideEffects other) const {
575 return SideEffects(flags_ | other.flags_);
576 }
577
Roland Levillain72bceff2014-09-15 18:29:00 +0100578 bool HasSideEffects() const {
579 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
580 return (flags_ & all_bits_set) != 0;
581 }
582
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100583 bool HasAllSideEffects() const {
584 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
585 return all_bits_set == (flags_ & all_bits_set);
586 }
587
588 bool DependsOn(SideEffects other) const {
589 size_t depends_flags = other.ComputeDependsFlags();
590 return (flags_ & depends_flags) != 0;
591 }
592
593 bool HasDependencies() const {
594 int count = kFlagDependsOnCount - kFlagChangesCount;
595 size_t all_bits_set = (1 << count) - 1;
596 return ((flags_ >> kFlagChangesCount) & all_bits_set) != 0;
597 }
598
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100599 private:
600 static constexpr int kFlagChangesSomething = 0;
601 static constexpr int kFlagChangesCount = kFlagChangesSomething + 1;
602
603 static constexpr int kFlagDependsOnSomething = kFlagChangesCount;
604 static constexpr int kFlagDependsOnCount = kFlagDependsOnSomething + 1;
605
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100606 explicit SideEffects(size_t flags) : flags_(flags) {}
607
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100608 size_t ComputeDependsFlags() const {
609 return flags_ << kFlagChangesCount;
610 }
611
612 size_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100613};
614
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000615class HInstruction : public ArenaObject {
616 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100617 explicit HInstruction(SideEffects side_effects)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000618 : previous_(nullptr),
619 next_(nullptr),
620 block_(nullptr),
621 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100622 ssa_index_(-1),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000623 uses_(nullptr),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100624 env_uses_(nullptr),
625 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100626 locations_(nullptr),
627 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100628 lifetime_position_(kNoLifetime),
629 side_effects_(side_effects) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000630
Dave Allison20dfc792014-06-16 20:44:29 -0700631 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000632
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100633#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100634 enum InstructionKind {
635 FOR_EACH_INSTRUCTION(DECLARE_KIND)
636 };
637#undef DECLARE_KIND
638
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000639 HInstruction* GetNext() const { return next_; }
640 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000641
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000642 HBasicBlock* GetBlock() const { return block_; }
643 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100644 bool IsInBlock() const { return block_ != nullptr; }
645 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +0100646 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000647
Roland Levillain6b879dd2014-09-22 17:13:44 +0100648 virtual size_t InputCount() const = 0;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100649 virtual HInstruction* InputAt(size_t i) const = 0;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000650
651 virtual void Accept(HGraphVisitor* visitor) = 0;
652 virtual const char* DebugName() const = 0;
653
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100654 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100655 virtual void SetRawInputAt(size_t index, HInstruction* input) = 0;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100656
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100657 virtual bool NeedsEnvironment() const { return false; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100658 virtual bool IsControlFlow() const { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +0100659 virtual bool CanThrow() const { return false; }
Roland Levillain72bceff2014-09-15 18:29:00 +0100660 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100661
662 void AddUseAt(HInstruction* user, size_t index) {
663 uses_ = new (block_->GetGraph()->GetArena()) HUseListNode<HInstruction>(user, index, uses_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000664 }
665
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100666 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +0100667 DCHECK(user != nullptr);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100668 env_uses_ = new (block_->GetGraph()->GetArena()) HUseListNode<HEnvironment>(
669 user, index, env_uses_);
670 }
671
672 void RemoveUser(HInstruction* user, size_t index);
Nicolas Geoffray724c9632014-09-22 12:27:27 +0100673 void RemoveEnvironmentUser(HEnvironment* user, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100674
675 HUseListNode<HInstruction>* GetUses() const { return uses_; }
676 HUseListNode<HEnvironment>* GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000677
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100678 bool HasUses() const { return uses_ != nullptr || env_uses_ != nullptr; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100679 bool HasEnvironmentUses() const { return env_uses_ != nullptr; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000680
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100681 size_t NumberOfUses() const {
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100682 // TODO: Optimize this method if it is used outside of the HGraphVisualizer.
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100683 size_t result = 0;
684 HUseListNode<HInstruction>* current = uses_;
685 while (current != nullptr) {
686 current = current->GetTail();
687 ++result;
688 }
689 return result;
690 }
691
Roland Levillain6c82d402014-10-13 16:10:27 +0100692 // Does this instruction strictly dominate `other_instruction`?
693 // Returns false if this instruction and `other_instruction` are the same.
694 // Aborts if this instruction and `other_instruction` are both phis.
695 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +0100696
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000697 int GetId() const { return id_; }
698 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000699
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100700 int GetSsaIndex() const { return ssa_index_; }
701 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
702 bool HasSsaIndex() const { return ssa_index_ != -1; }
703
704 bool HasEnvironment() const { return environment_ != nullptr; }
705 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100706 void SetEnvironment(HEnvironment* environment) { environment_ = environment; }
707
Nicolas Geoffray39468442014-09-02 15:17:15 +0100708 // Returns the number of entries in the environment. Typically, that is the
709 // number of dex registers in a method. It could be more in case of inlining.
710 size_t EnvironmentSize() const;
711
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000712 LocationSummary* GetLocations() const { return locations_; }
713 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000714
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100715 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100716 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100717
Dave Allison20dfc792014-06-16 20:44:29 -0700718 bool HasOnlyOneUse() const {
719 return uses_ != nullptr && uses_->GetTail() == nullptr;
720 }
721
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100722#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +0100723 bool Is##type() const { return (As##type() != nullptr); } \
724 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000725 virtual H##type* As##type() { return nullptr; }
726
727 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
728#undef INSTRUCTION_TYPE_CHECK
729
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100730 // Returns whether the instruction can be moved within the graph.
731 virtual bool CanBeMoved() const { return false; }
732
733 // Returns whether the two instructions are of the same kind.
734 virtual bool InstructionTypeEquals(HInstruction* other) const { return false; }
735
736 // Returns whether any data encoded in the two instructions is equal.
737 // This method does not look at the inputs. Both instructions must be
738 // of the same type, otherwise the method has undefined behavior.
739 virtual bool InstructionDataEquals(HInstruction* other) const { return false; }
740
741 // Returns whether two instructions are equal, that is:
742 // 1) They have the same type and contain the same data,
743 // 2) Their inputs are identical.
744 bool Equals(HInstruction* other) const;
745
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100746 virtual InstructionKind GetKind() const = 0;
747
748 virtual size_t ComputeHashCode() const {
749 size_t result = GetKind();
750 for (size_t i = 0, e = InputCount(); i < e; ++i) {
751 result = (result * 31) + InputAt(i)->GetId();
752 }
753 return result;
754 }
755
756 SideEffects GetSideEffects() const { return side_effects_; }
757
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100758 size_t GetLifetimePosition() const { return lifetime_position_; }
759 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
760 LiveInterval* GetLiveInterval() const { return live_interval_; }
761 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
762 bool HasLiveInterval() const { return live_interval_ != nullptr; }
763
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000764 private:
765 HInstruction* previous_;
766 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000767 HBasicBlock* block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000768
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000769 // An instruction gets an id when it is added to the graph.
770 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +0100771 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000772 int id_;
773
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100774 // When doing liveness analysis, instructions that have uses get an SSA index.
775 int ssa_index_;
776
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100777 // List of instructions that have this instruction as input.
778 HUseListNode<HInstruction>* uses_;
779
780 // List of environments that contain this instruction.
781 HUseListNode<HEnvironment>* env_uses_;
782
Nicolas Geoffray39468442014-09-02 15:17:15 +0100783 // The environment associated with this instruction. Not null if the instruction
784 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100785 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000786
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000787 // Set by the code generator.
788 LocationSummary* locations_;
789
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100790 // Set by the liveness analysis.
791 LiveInterval* live_interval_;
792
793 // Set by the liveness analysis, this is the position in a linear
794 // order of blocks where this instruction's live interval start.
795 size_t lifetime_position_;
796
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100797 const SideEffects side_effects_;
798
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000799 friend class HBasicBlock;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100800 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000801
802 DISALLOW_COPY_AND_ASSIGN(HInstruction);
803};
804
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100805template<typename T>
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000806class HUseIterator : public ValueObject {
807 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100808 explicit HUseIterator(HUseListNode<T>* uses) : current_(uses) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000809
810 bool Done() const { return current_ == nullptr; }
811
812 void Advance() {
813 DCHECK(!Done());
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000814 current_ = current_->GetTail();
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000815 }
816
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100817 HUseListNode<T>* Current() const {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000818 DCHECK(!Done());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100819 return current_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000820 }
821
822 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100823 HUseListNode<T>* current_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000824
825 friend class HValue;
826};
827
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100828// A HEnvironment object contains the values of virtual registers at a given location.
829class HEnvironment : public ArenaObject {
830 public:
831 HEnvironment(ArenaAllocator* arena, size_t number_of_vregs) : vregs_(arena, number_of_vregs) {
832 vregs_.SetSize(number_of_vregs);
833 for (size_t i = 0; i < number_of_vregs; i++) {
834 vregs_.Put(i, nullptr);
835 }
836 }
837
838 void Populate(const GrowableArray<HInstruction*>& env) {
839 for (size_t i = 0; i < env.Size(); i++) {
840 HInstruction* instruction = env.Get(i);
841 vregs_.Put(i, instruction);
842 if (instruction != nullptr) {
843 instruction->AddEnvUseAt(this, i);
844 }
845 }
846 }
847
848 void SetRawEnvAt(size_t index, HInstruction* instruction) {
849 vregs_.Put(index, instruction);
850 }
851
Nicolas Geoffray39468442014-09-02 15:17:15 +0100852 HInstruction* GetInstructionAt(size_t index) const {
853 return vregs_.Get(index);
854 }
855
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100856 GrowableArray<HInstruction*>* GetVRegs() {
857 return &vregs_;
858 }
859
Nicolas Geoffray39468442014-09-02 15:17:15 +0100860 size_t Size() const { return vregs_.Size(); }
861
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100862 private:
863 GrowableArray<HInstruction*> vregs_;
864
865 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
866};
867
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000868class HInputIterator : public ValueObject {
869 public:
Dave Allison20dfc792014-06-16 20:44:29 -0700870 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000871
872 bool Done() const { return index_ == instruction_->InputCount(); }
873 HInstruction* Current() const { return instruction_->InputAt(index_); }
874 void Advance() { index_++; }
875
876 private:
877 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100878 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000879
880 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
881};
882
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000883class HInstructionIterator : public ValueObject {
884 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100885 explicit HInstructionIterator(const HInstructionList& instructions)
886 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000887 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000888 }
889
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000890 bool Done() const { return instruction_ == nullptr; }
891 HInstruction* Current() const { return instruction_; }
892 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000893 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000894 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000895 }
896
897 private:
898 HInstruction* instruction_;
899 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100900
901 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000902};
903
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100904class HBackwardInstructionIterator : public ValueObject {
905 public:
906 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
907 : instruction_(instructions.last_instruction_) {
908 next_ = Done() ? nullptr : instruction_->GetPrevious();
909 }
910
911 bool Done() const { return instruction_ == nullptr; }
912 HInstruction* Current() const { return instruction_; }
913 void Advance() {
914 instruction_ = next_;
915 next_ = Done() ? nullptr : instruction_->GetPrevious();
916 }
917
918 private:
919 HInstruction* instruction_;
920 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100921
922 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100923};
924
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000925// An embedded container with N elements of type T. Used (with partial
926// specialization for N=0) because embedded arrays cannot have size 0.
927template<typename T, intptr_t N>
928class EmbeddedArray {
929 public:
Dave Allison20dfc792014-06-16 20:44:29 -0700930 EmbeddedArray() : elements_() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000931
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000932 intptr_t GetLength() const { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000933
934 const T& operator[](intptr_t i) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000935 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000936 return elements_[i];
937 }
938
939 T& operator[](intptr_t i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000940 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000941 return elements_[i];
942 }
943
944 const T& At(intptr_t i) const {
945 return (*this)[i];
946 }
947
948 void SetAt(intptr_t i, const T& val) {
949 (*this)[i] = val;
950 }
951
952 private:
953 T elements_[N];
954};
955
956template<typename T>
957class EmbeddedArray<T, 0> {
958 public:
959 intptr_t length() const { return 0; }
960 const T& operator[](intptr_t i) const {
961 LOG(FATAL) << "Unreachable";
962 static T sentinel = 0;
963 return sentinel;
964 }
965 T& operator[](intptr_t i) {
966 LOG(FATAL) << "Unreachable";
967 static T sentinel = 0;
968 return sentinel;
969 }
970};
971
972template<intptr_t N>
973class HTemplateInstruction: public HInstruction {
974 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100975 HTemplateInstruction<N>(SideEffects side_effects)
976 : HInstruction(side_effects), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -0700977 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000978
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100979 virtual size_t InputCount() const { return N; }
980 virtual HInstruction* InputAt(size_t i) const { return inputs_[i]; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000981
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000982 protected:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100983 virtual void SetRawInputAt(size_t i, HInstruction* instruction) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000984 inputs_[i] = instruction;
985 }
986
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000987 private:
988 EmbeddedArray<HInstruction*, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100989
990 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000991};
992
Dave Allison20dfc792014-06-16 20:44:29 -0700993template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100994class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -0700995 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100996 HExpression<N>(Primitive::Type type, SideEffects side_effects)
997 : HTemplateInstruction<N>(side_effects), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -0700998 virtual ~HExpression() {}
999
1000 virtual Primitive::Type GetType() const { return type_; }
1001
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001002 protected:
1003 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07001004};
1005
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001006// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
1007// instruction that branches to the exit block.
1008class HReturnVoid : public HTemplateInstruction<0> {
1009 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001010 HReturnVoid() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001011
1012 virtual bool IsControlFlow() const { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001013
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001014 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001015
1016 private:
1017 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
1018};
1019
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001020// Represents dex's RETURN opcodes. A HReturn is a control flow
1021// instruction that branches to the exit block.
1022class HReturn : public HTemplateInstruction<1> {
1023 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001024 explicit HReturn(HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001025 SetRawInputAt(0, value);
1026 }
1027
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001028 virtual bool IsControlFlow() const { return true; }
1029
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001030 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001031
1032 private:
1033 DISALLOW_COPY_AND_ASSIGN(HReturn);
1034};
1035
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001036// The exit instruction is the only instruction of the exit block.
1037// Instructions aborting the method (HTrow and HReturn) must branch to the
1038// exit block.
1039class HExit : public HTemplateInstruction<0> {
1040 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001041 HExit() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001042
1043 virtual bool IsControlFlow() const { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001044
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001045 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001046
1047 private:
1048 DISALLOW_COPY_AND_ASSIGN(HExit);
1049};
1050
1051// Jumps from one block to another.
1052class HGoto : public HTemplateInstruction<0> {
1053 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001054 HGoto() : HTemplateInstruction(SideEffects::None()) {}
1055
1056 virtual bool IsControlFlow() const { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001057
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001058 HBasicBlock* GetSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001059 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001060 }
1061
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001062 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001063
1064 private:
1065 DISALLOW_COPY_AND_ASSIGN(HGoto);
1066};
1067
Dave Allison20dfc792014-06-16 20:44:29 -07001068
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001069// Conditional branch. A block ending with an HIf instruction must have
1070// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001071class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001072 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001073 explicit HIf(HInstruction* input) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001074 SetRawInputAt(0, input);
1075 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001076
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001077 virtual bool IsControlFlow() const { return true; }
1078
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001079 HBasicBlock* IfTrueSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001080 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001081 }
1082
1083 HBasicBlock* IfFalseSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001084 return GetBlock()->GetSuccessors().Get(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001085 }
1086
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001087 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001088
Dave Allison20dfc792014-06-16 20:44:29 -07001089 virtual bool IsIfInstruction() const { return true; }
1090
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001091 private:
1092 DISALLOW_COPY_AND_ASSIGN(HIf);
1093};
1094
Roland Levillain88cb1752014-10-20 16:36:47 +01001095class HUnaryOperation : public HExpression<1> {
1096 public:
1097 HUnaryOperation(Primitive::Type result_type, HInstruction* input)
1098 : HExpression(result_type, SideEffects::None()) {
1099 SetRawInputAt(0, input);
1100 }
1101
1102 HInstruction* GetInput() const { return InputAt(0); }
1103 Primitive::Type GetResultType() const { return GetType(); }
1104
1105 virtual bool CanBeMoved() const { return true; }
1106 virtual bool InstructionDataEquals(HInstruction* other) const { return true; }
1107
Roland Levillain9240d6a2014-10-20 16:47:04 +01001108 // Try to statically evaluate `operation` and return a HConstant
1109 // containing the result of this evaluation. If `operation` cannot
1110 // be evaluated as a constant, return nullptr.
1111 HConstant* TryStaticEvaluation() const;
1112
1113 // Apply this operation to `x`.
1114 virtual int32_t Evaluate(int32_t x) const = 0;
1115 virtual int64_t Evaluate(int64_t x) const = 0;
1116
Roland Levillain88cb1752014-10-20 16:36:47 +01001117 DECLARE_INSTRUCTION(UnaryOperation);
1118
1119 private:
1120 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
1121};
1122
Dave Allison20dfc792014-06-16 20:44:29 -07001123class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001124 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001125 HBinaryOperation(Primitive::Type result_type,
1126 HInstruction* left,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001127 HInstruction* right) : HExpression(result_type, SideEffects::None()) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001128 SetRawInputAt(0, left);
1129 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001130 }
1131
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001132 HInstruction* GetLeft() const { return InputAt(0); }
1133 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07001134 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001135
1136 virtual bool IsCommutative() { return false; }
1137
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001138 virtual bool CanBeMoved() const { return true; }
1139 virtual bool InstructionDataEquals(HInstruction* other) const { return true; }
1140
Roland Levillain9240d6a2014-10-20 16:47:04 +01001141 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01001142 // containing the result of this evaluation. If `operation` cannot
1143 // be evaluated as a constant, return nullptr.
Roland Levillain9240d6a2014-10-20 16:47:04 +01001144 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01001145
1146 // Apply this operation to `x` and `y`.
1147 virtual int32_t Evaluate(int32_t x, int32_t y) const = 0;
1148 virtual int64_t Evaluate(int64_t x, int64_t y) const = 0;
1149
Roland Levillainccc07a92014-09-16 14:48:16 +01001150 DECLARE_INSTRUCTION(BinaryOperation);
1151
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001152 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001153 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
1154};
1155
Dave Allison20dfc792014-06-16 20:44:29 -07001156class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001157 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001158 HCondition(HInstruction* first, HInstruction* second)
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001159 : HBinaryOperation(Primitive::kPrimBoolean, first, second),
1160 needs_materialization_(true) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001161
1162 virtual bool IsCommutative() { return true; }
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001163
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001164 bool NeedsMaterialization() const { return needs_materialization_; }
1165 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001166
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001167 // For code generation purposes, returns whether this instruction is just before
1168 // `if_`, and disregard moves in between.
1169 bool IsBeforeWhenDisregardMoves(HIf* if_) const;
1170
Dave Allison20dfc792014-06-16 20:44:29 -07001171 DECLARE_INSTRUCTION(Condition);
1172
1173 virtual IfCondition GetCondition() const = 0;
1174
1175 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001176 // For register allocation purposes, returns whether this instruction needs to be
1177 // materialized (that is, not just be in the processor flags).
1178 bool needs_materialization_;
1179
Dave Allison20dfc792014-06-16 20:44:29 -07001180 DISALLOW_COPY_AND_ASSIGN(HCondition);
1181};
1182
1183// Instruction to check if two inputs are equal to each other.
1184class HEqual : public HCondition {
1185 public:
1186 HEqual(HInstruction* first, HInstruction* second)
1187 : HCondition(first, second) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001188
Roland Levillain93445682014-10-06 19:24:02 +01001189 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1190 return x == y ? 1 : 0;
1191 }
1192 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1193 return x == y ? 1 : 0;
1194 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001195
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001196 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001197
Dave Allison20dfc792014-06-16 20:44:29 -07001198 virtual IfCondition GetCondition() const {
1199 return kCondEQ;
1200 }
1201
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001202 private:
1203 DISALLOW_COPY_AND_ASSIGN(HEqual);
1204};
1205
Dave Allison20dfc792014-06-16 20:44:29 -07001206class HNotEqual : public HCondition {
1207 public:
1208 HNotEqual(HInstruction* first, HInstruction* second)
1209 : HCondition(first, second) {}
1210
Roland Levillain93445682014-10-06 19:24:02 +01001211 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1212 return x != y ? 1 : 0;
1213 }
1214 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1215 return x != y ? 1 : 0;
1216 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001217
Dave Allison20dfc792014-06-16 20:44:29 -07001218 DECLARE_INSTRUCTION(NotEqual);
1219
1220 virtual IfCondition GetCondition() const {
1221 return kCondNE;
1222 }
1223
1224 private:
1225 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
1226};
1227
1228class HLessThan : public HCondition {
1229 public:
1230 HLessThan(HInstruction* first, HInstruction* second)
1231 : HCondition(first, second) {}
1232
Roland Levillain93445682014-10-06 19:24:02 +01001233 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1234 return x < y ? 1 : 0;
1235 }
1236 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1237 return x < y ? 1 : 0;
1238 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001239
Dave Allison20dfc792014-06-16 20:44:29 -07001240 DECLARE_INSTRUCTION(LessThan);
1241
1242 virtual IfCondition GetCondition() const {
1243 return kCondLT;
1244 }
1245
1246 private:
1247 DISALLOW_COPY_AND_ASSIGN(HLessThan);
1248};
1249
1250class HLessThanOrEqual : public HCondition {
1251 public:
1252 HLessThanOrEqual(HInstruction* first, HInstruction* second)
1253 : HCondition(first, second) {}
1254
Roland Levillain93445682014-10-06 19:24:02 +01001255 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1256 return x <= y ? 1 : 0;
1257 }
1258 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1259 return x <= y ? 1 : 0;
1260 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001261
Dave Allison20dfc792014-06-16 20:44:29 -07001262 DECLARE_INSTRUCTION(LessThanOrEqual);
1263
1264 virtual IfCondition GetCondition() const {
1265 return kCondLE;
1266 }
1267
1268 private:
1269 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
1270};
1271
1272class HGreaterThan : public HCondition {
1273 public:
1274 HGreaterThan(HInstruction* first, HInstruction* second)
1275 : HCondition(first, second) {}
1276
Roland Levillain93445682014-10-06 19:24:02 +01001277 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1278 return x > y ? 1 : 0;
1279 }
1280 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1281 return x > y ? 1 : 0;
1282 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001283
Dave Allison20dfc792014-06-16 20:44:29 -07001284 DECLARE_INSTRUCTION(GreaterThan);
1285
1286 virtual IfCondition GetCondition() const {
1287 return kCondGT;
1288 }
1289
1290 private:
1291 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
1292};
1293
1294class HGreaterThanOrEqual : public HCondition {
1295 public:
1296 HGreaterThanOrEqual(HInstruction* first, HInstruction* second)
1297 : HCondition(first, second) {}
1298
Roland Levillain93445682014-10-06 19:24:02 +01001299 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1300 return x >= y ? 1 : 0;
1301 }
1302 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1303 return x >= y ? 1 : 0;
1304 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001305
Dave Allison20dfc792014-06-16 20:44:29 -07001306 DECLARE_INSTRUCTION(GreaterThanOrEqual);
1307
1308 virtual IfCondition GetCondition() const {
1309 return kCondGE;
1310 }
1311
1312 private:
1313 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
1314};
1315
1316
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001317// Instruction to check how two inputs compare to each other.
1318// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
1319class HCompare : public HBinaryOperation {
1320 public:
1321 HCompare(Primitive::Type type, HInstruction* first, HInstruction* second)
1322 : HBinaryOperation(Primitive::kPrimInt, first, second) {
1323 DCHECK_EQ(type, first->GetType());
1324 DCHECK_EQ(type, second->GetType());
1325 }
1326
Roland Levillain93445682014-10-06 19:24:02 +01001327 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001328 return
1329 x == y ? 0 :
1330 x > y ? 1 :
1331 -1;
1332 }
Roland Levillain93445682014-10-06 19:24:02 +01001333 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001334 return
1335 x == y ? 0 :
1336 x > y ? 1 :
1337 -1;
1338 }
1339
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001340 DECLARE_INSTRUCTION(Compare);
1341
1342 private:
1343 DISALLOW_COPY_AND_ASSIGN(HCompare);
1344};
1345
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001346// A local in the graph. Corresponds to a Dex register.
1347class HLocal : public HTemplateInstruction<0> {
1348 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001349 explicit HLocal(uint16_t reg_number)
1350 : HTemplateInstruction(SideEffects::None()), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001351
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001352 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001353
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001354 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001355
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001356 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001357 // The Dex register number.
1358 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001359
1360 DISALLOW_COPY_AND_ASSIGN(HLocal);
1361};
1362
1363// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07001364class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001365 public:
Roland Levillain5799fc02014-09-25 12:15:20 +01001366 HLoadLocal(HLocal* local, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001367 : HExpression(type, SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001368 SetRawInputAt(0, local);
1369 }
1370
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001371 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1372
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001373 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001374
1375 private:
1376 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
1377};
1378
1379// Store a value in a given local. This instruction has two inputs: the value
1380// and the local.
1381class HStoreLocal : public HTemplateInstruction<2> {
1382 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001383 HStoreLocal(HLocal* local, HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001384 SetRawInputAt(0, local);
1385 SetRawInputAt(1, value);
1386 }
1387
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001388 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1389
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001390 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001391
1392 private:
1393 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
1394};
1395
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001396class HConstant : public HExpression<0> {
1397 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001398 explicit HConstant(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
1399
1400 virtual bool CanBeMoved() const { return true; }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001401
1402 DECLARE_INSTRUCTION(Constant);
1403
1404 private:
1405 DISALLOW_COPY_AND_ASSIGN(HConstant);
1406};
1407
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001408class HFloatConstant : public HConstant {
1409 public:
1410 explicit HFloatConstant(float value) : HConstant(Primitive::kPrimFloat), value_(value) {}
1411
1412 float GetValue() const { return value_; }
1413
1414 virtual bool InstructionDataEquals(HInstruction* other) const {
1415 return bit_cast<float, int32_t>(other->AsFloatConstant()->value_) ==
1416 bit_cast<float, int32_t>(value_);
1417 }
1418
1419 virtual size_t ComputeHashCode() const { return static_cast<size_t>(GetValue()); }
1420
1421 DECLARE_INSTRUCTION(FloatConstant);
1422
1423 private:
1424 const float value_;
1425
1426 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
1427};
1428
1429class HDoubleConstant : public HConstant {
1430 public:
1431 explicit HDoubleConstant(double value) : HConstant(Primitive::kPrimDouble), value_(value) {}
1432
1433 double GetValue() const { return value_; }
1434
1435 virtual bool InstructionDataEquals(HInstruction* other) const {
1436 return bit_cast<double, int64_t>(other->AsDoubleConstant()->value_) ==
1437 bit_cast<double, int64_t>(value_);
1438 }
1439
1440 virtual size_t ComputeHashCode() const { return static_cast<size_t>(GetValue()); }
1441
1442 DECLARE_INSTRUCTION(DoubleConstant);
1443
1444 private:
1445 const double value_;
1446
1447 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
1448};
1449
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001450// Constants of the type int. Those can be from Dex instructions, or
1451// synthesized (for example with the if-eqz instruction).
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001452class HIntConstant : public HConstant {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001453 public:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001454 explicit HIntConstant(int32_t value) : HConstant(Primitive::kPrimInt), value_(value) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001455
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001456 int32_t GetValue() const { return value_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001457
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001458 virtual bool InstructionDataEquals(HInstruction* other) const {
1459 return other->AsIntConstant()->value_ == value_;
1460 }
1461
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001462 virtual size_t ComputeHashCode() const { return GetValue(); }
1463
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001464 DECLARE_INSTRUCTION(IntConstant);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001465
1466 private:
1467 const int32_t value_;
1468
1469 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
1470};
1471
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001472class HLongConstant : public HConstant {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001473 public:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001474 explicit HLongConstant(int64_t value) : HConstant(Primitive::kPrimLong), value_(value) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001475
1476 int64_t GetValue() const { return value_; }
1477
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001478 virtual bool InstructionDataEquals(HInstruction* other) const {
1479 return other->AsLongConstant()->value_ == value_;
1480 }
1481
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001482 virtual size_t ComputeHashCode() const { return static_cast<size_t>(GetValue()); }
1483
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001484 DECLARE_INSTRUCTION(LongConstant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001485
1486 private:
1487 const int64_t value_;
1488
1489 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
1490};
1491
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001492class HInvoke : public HInstruction {
1493 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001494 HInvoke(ArenaAllocator* arena,
1495 uint32_t number_of_arguments,
1496 Primitive::Type return_type,
1497 uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001498 : HInstruction(SideEffects::All()),
1499 inputs_(arena, number_of_arguments),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001500 return_type_(return_type),
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001501 dex_pc_(dex_pc) {
1502 inputs_.SetSize(number_of_arguments);
1503 }
1504
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001505 virtual size_t InputCount() const { return inputs_.Size(); }
1506 virtual HInstruction* InputAt(size_t i) const { return inputs_.Get(i); }
1507
1508 // Runtime needs to walk the stack, so Dex -> Dex calls need to
1509 // know their environment.
1510 virtual bool NeedsEnvironment() const { return true; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001511
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001512 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001513 SetRawInputAt(index, argument);
1514 }
1515
1516 virtual void SetRawInputAt(size_t index, HInstruction* input) {
1517 inputs_.Put(index, input);
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001518 }
1519
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001520 virtual Primitive::Type GetType() const { return return_type_; }
1521
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001522 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001523
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001524 DECLARE_INSTRUCTION(Invoke);
1525
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001526 protected:
1527 GrowableArray<HInstruction*> inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001528 const Primitive::Type return_type_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001529 const uint32_t dex_pc_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001530
1531 private:
1532 DISALLOW_COPY_AND_ASSIGN(HInvoke);
1533};
1534
1535class HInvokeStatic : public HInvoke {
1536 public:
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001537 HInvokeStatic(ArenaAllocator* arena,
1538 uint32_t number_of_arguments,
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001539 Primitive::Type return_type,
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001540 uint32_t dex_pc,
1541 uint32_t index_in_dex_cache)
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001542 : HInvoke(arena, number_of_arguments, return_type, dex_pc),
1543 index_in_dex_cache_(index_in_dex_cache) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001544
1545 uint32_t GetIndexInDexCache() const { return index_in_dex_cache_; }
1546
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001547 DECLARE_INSTRUCTION(InvokeStatic);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001548
1549 private:
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001550 const uint32_t index_in_dex_cache_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001551
1552 DISALLOW_COPY_AND_ASSIGN(HInvokeStatic);
1553};
1554
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001555class HInvokeVirtual : public HInvoke {
1556 public:
1557 HInvokeVirtual(ArenaAllocator* arena,
1558 uint32_t number_of_arguments,
1559 Primitive::Type return_type,
1560 uint32_t dex_pc,
1561 uint32_t vtable_index)
1562 : HInvoke(arena, number_of_arguments, return_type, dex_pc),
1563 vtable_index_(vtable_index) {}
1564
1565 uint32_t GetVTableIndex() const { return vtable_index_; }
1566
1567 DECLARE_INSTRUCTION(InvokeVirtual);
1568
1569 private:
1570 const uint32_t vtable_index_;
1571
1572 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
1573};
1574
Dave Allison20dfc792014-06-16 20:44:29 -07001575class HNewInstance : public HExpression<0> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001576 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001577 HNewInstance(uint32_t dex_pc, uint16_t type_index)
1578 : HExpression(Primitive::kPrimNot, SideEffects::None()),
1579 dex_pc_(dex_pc),
1580 type_index_(type_index) {}
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001581
1582 uint32_t GetDexPc() const { return dex_pc_; }
1583 uint16_t GetTypeIndex() const { return type_index_; }
1584
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001585 // Calls runtime so needs an environment.
1586 virtual bool NeedsEnvironment() const { return true; }
1587
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001588 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001589
1590 private:
1591 const uint32_t dex_pc_;
1592 const uint16_t type_index_;
1593
1594 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
1595};
1596
Roland Levillain88cb1752014-10-20 16:36:47 +01001597class HNeg : public HUnaryOperation {
1598 public:
1599 explicit HNeg(Primitive::Type result_type, HInstruction* input)
1600 : HUnaryOperation(result_type, input) {}
1601
Roland Levillain9240d6a2014-10-20 16:47:04 +01001602 virtual int32_t Evaluate(int32_t x) const OVERRIDE { return -x; }
1603 virtual int64_t Evaluate(int64_t x) const OVERRIDE { return -x; }
1604
Roland Levillain88cb1752014-10-20 16:36:47 +01001605 DECLARE_INSTRUCTION(Neg);
1606
1607 private:
1608 DISALLOW_COPY_AND_ASSIGN(HNeg);
1609};
1610
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001611class HAdd : public HBinaryOperation {
1612 public:
1613 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1614 : HBinaryOperation(result_type, left, right) {}
1615
1616 virtual bool IsCommutative() { return true; }
1617
Roland Levillain93445682014-10-06 19:24:02 +01001618 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1619 return x + y;
1620 }
1621 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1622 return x + y;
1623 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001624
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001625 DECLARE_INSTRUCTION(Add);
1626
1627 private:
1628 DISALLOW_COPY_AND_ASSIGN(HAdd);
1629};
1630
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001631class HSub : public HBinaryOperation {
1632 public:
1633 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1634 : HBinaryOperation(result_type, left, right) {}
1635
1636 virtual bool IsCommutative() { return false; }
1637
Roland Levillain93445682014-10-06 19:24:02 +01001638 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1639 return x - y;
1640 }
1641 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1642 return x - y;
1643 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001644
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001645 DECLARE_INSTRUCTION(Sub);
1646
1647 private:
1648 DISALLOW_COPY_AND_ASSIGN(HSub);
1649};
1650
Calin Juravle34bacdf2014-10-07 20:23:36 +01001651class HMul : public HBinaryOperation {
1652 public:
1653 HMul(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1654 : HBinaryOperation(result_type, left, right) {}
1655
1656 virtual bool IsCommutative() { return true; }
1657
1658 virtual int32_t Evaluate(int32_t x, int32_t y) const { return x * y; }
1659 virtual int64_t Evaluate(int64_t x, int64_t y) const { return x * y; }
1660
1661 DECLARE_INSTRUCTION(Mul);
1662
1663 private:
1664 DISALLOW_COPY_AND_ASSIGN(HMul);
1665};
1666
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001667// The value of a parameter in this method. Its location depends on
1668// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07001669class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001670 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001671 HParameterValue(uint8_t index, Primitive::Type parameter_type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001672 : HExpression(parameter_type, SideEffects::None()), index_(index) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001673
1674 uint8_t GetIndex() const { return index_; }
1675
1676 DECLARE_INSTRUCTION(ParameterValue);
1677
1678 private:
1679 // The index of this parameter in the parameters list. Must be less
1680 // than HGraph::number_of_in_vregs_;
1681 const uint8_t index_;
1682
1683 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
1684};
1685
Dave Allison20dfc792014-06-16 20:44:29 -07001686class HNot : public HExpression<1> {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001687 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001688 explicit HNot(HInstruction* input) : HExpression(Primitive::kPrimBoolean, SideEffects::None()) {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001689 SetRawInputAt(0, input);
1690 }
1691
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001692 virtual bool CanBeMoved() const { return true; }
1693 virtual bool InstructionDataEquals(HInstruction* other) const { return true; }
1694
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001695 DECLARE_INSTRUCTION(Not);
1696
1697 private:
1698 DISALLOW_COPY_AND_ASSIGN(HNot);
1699};
1700
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001701class HPhi : public HInstruction {
1702 public:
1703 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001704 : HInstruction(SideEffects::None()),
1705 inputs_(arena, number_of_inputs),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001706 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001707 type_(type),
1708 is_live_(false) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001709 inputs_.SetSize(number_of_inputs);
1710 }
1711
1712 virtual size_t InputCount() const { return inputs_.Size(); }
1713 virtual HInstruction* InputAt(size_t i) const { return inputs_.Get(i); }
1714
1715 virtual void SetRawInputAt(size_t index, HInstruction* input) {
1716 inputs_.Put(index, input);
1717 }
1718
1719 void AddInput(HInstruction* input);
1720
1721 virtual Primitive::Type GetType() const { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001722 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001723
1724 uint32_t GetRegNumber() const { return reg_number_; }
1725
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001726 void SetDead() { is_live_ = false; }
1727 void SetLive() { is_live_ = true; }
1728 bool IsDead() const { return !is_live_; }
1729 bool IsLive() const { return is_live_; }
1730
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001731 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001732
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001733 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001734 GrowableArray<HInstruction*> inputs_;
1735 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001736 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001737 bool is_live_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001738
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001739 DISALLOW_COPY_AND_ASSIGN(HPhi);
1740};
1741
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001742class HNullCheck : public HExpression<1> {
1743 public:
1744 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001745 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001746 SetRawInputAt(0, value);
1747 }
1748
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001749 virtual bool CanBeMoved() const { return true; }
1750 virtual bool InstructionDataEquals(HInstruction* other) const { return true; }
1751
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001752 virtual bool NeedsEnvironment() const { return true; }
1753
Roland Levillaine161a2a2014-10-03 12:45:18 +01001754 virtual bool CanThrow() const { return true; }
1755
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001756 uint32_t GetDexPc() const { return dex_pc_; }
1757
1758 DECLARE_INSTRUCTION(NullCheck);
1759
1760 private:
1761 const uint32_t dex_pc_;
1762
1763 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
1764};
1765
1766class FieldInfo : public ValueObject {
1767 public:
Roland Levillain5799fc02014-09-25 12:15:20 +01001768 FieldInfo(MemberOffset field_offset, Primitive::Type field_type)
Nicolas Geoffray39468442014-09-02 15:17:15 +01001769 : field_offset_(field_offset), field_type_(field_type) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001770
1771 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001772 Primitive::Type GetFieldType() const { return field_type_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001773
1774 private:
1775 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01001776 const Primitive::Type field_type_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001777};
1778
1779class HInstanceFieldGet : public HExpression<1> {
1780 public:
1781 HInstanceFieldGet(HInstruction* value,
1782 Primitive::Type field_type,
1783 MemberOffset field_offset)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001784 : HExpression(field_type, SideEffects::DependsOnSomething()),
1785 field_info_(field_offset, field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001786 SetRawInputAt(0, value);
1787 }
1788
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001789 virtual bool CanBeMoved() const { return true; }
1790 virtual bool InstructionDataEquals(HInstruction* other) const {
1791 size_t other_offset = other->AsInstanceFieldGet()->GetFieldOffset().SizeValue();
1792 return other_offset == GetFieldOffset().SizeValue();
1793 }
1794
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001795 virtual size_t ComputeHashCode() const {
1796 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
1797 }
1798
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001799 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001800 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001801
1802 DECLARE_INSTRUCTION(InstanceFieldGet);
1803
1804 private:
1805 const FieldInfo field_info_;
1806
1807 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
1808};
1809
1810class HInstanceFieldSet : public HTemplateInstruction<2> {
1811 public:
1812 HInstanceFieldSet(HInstruction* object,
1813 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01001814 Primitive::Type field_type,
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001815 MemberOffset field_offset)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001816 : HTemplateInstruction(SideEffects::ChangesSomething()),
1817 field_info_(field_offset, field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001818 SetRawInputAt(0, object);
1819 SetRawInputAt(1, value);
1820 }
1821
1822 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001823 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001824
1825 DECLARE_INSTRUCTION(InstanceFieldSet);
1826
1827 private:
1828 const FieldInfo field_info_;
1829
1830 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
1831};
1832
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001833class HArrayGet : public HExpression<2> {
1834 public:
1835 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001836 : HExpression(type, SideEffects::DependsOnSomething()) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001837 SetRawInputAt(0, array);
1838 SetRawInputAt(1, index);
1839 }
1840
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001841 virtual bool CanBeMoved() const { return true; }
1842 virtual bool InstructionDataEquals(HInstruction* other) const { return true; }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001843 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001844
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001845 DECLARE_INSTRUCTION(ArrayGet);
1846
1847 private:
1848 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
1849};
1850
1851class HArraySet : public HTemplateInstruction<3> {
1852 public:
1853 HArraySet(HInstruction* array,
1854 HInstruction* index,
1855 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001856 Primitive::Type expected_component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001857 uint32_t dex_pc)
1858 : HTemplateInstruction(SideEffects::ChangesSomething()),
1859 dex_pc_(dex_pc),
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001860 expected_component_type_(expected_component_type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001861 SetRawInputAt(0, array);
1862 SetRawInputAt(1, index);
1863 SetRawInputAt(2, value);
1864 }
1865
1866 virtual bool NeedsEnvironment() const {
1867 // We currently always call a runtime method to catch array store
1868 // exceptions.
1869 return InputAt(2)->GetType() == Primitive::kPrimNot;
1870 }
1871
1872 uint32_t GetDexPc() const { return dex_pc_; }
1873
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001874 HInstruction* GetValue() const { return InputAt(2); }
1875
1876 Primitive::Type GetComponentType() const {
1877 // The Dex format does not type floating point index operations. Since the
1878 // `expected_component_type_` is set during building and can therefore not
1879 // be correct, we also check what is the value type. If it is a floating
1880 // point type, we must use that type.
1881 Primitive::Type value_type = GetValue()->GetType();
1882 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
1883 ? value_type
1884 : expected_component_type_;
1885 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001886
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001887 DECLARE_INSTRUCTION(ArraySet);
1888
1889 private:
1890 const uint32_t dex_pc_;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001891 const Primitive::Type expected_component_type_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001892
1893 DISALLOW_COPY_AND_ASSIGN(HArraySet);
1894};
1895
1896class HArrayLength : public HExpression<1> {
1897 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001898 explicit HArrayLength(HInstruction* array)
1899 : HExpression(Primitive::kPrimInt, SideEffects::None()) {
1900 // Note that arrays do not change length, so the instruction does not
1901 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001902 SetRawInputAt(0, array);
1903 }
1904
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001905 virtual bool CanBeMoved() const { return true; }
1906 virtual bool InstructionDataEquals(HInstruction* other) const { return true; }
1907
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001908 DECLARE_INSTRUCTION(ArrayLength);
1909
1910 private:
1911 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
1912};
1913
1914class HBoundsCheck : public HExpression<2> {
1915 public:
1916 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001917 : HExpression(index->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001918 DCHECK(index->GetType() == Primitive::kPrimInt);
1919 SetRawInputAt(0, index);
1920 SetRawInputAt(1, length);
1921 }
1922
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001923 virtual bool CanBeMoved() const { return true; }
1924 virtual bool InstructionDataEquals(HInstruction* other) const { return true; }
1925
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001926 virtual bool NeedsEnvironment() const { return true; }
1927
Roland Levillaine161a2a2014-10-03 12:45:18 +01001928 virtual bool CanThrow() const { return true; }
1929
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001930 uint32_t GetDexPc() const { return dex_pc_; }
1931
1932 DECLARE_INSTRUCTION(BoundsCheck);
1933
1934 private:
1935 const uint32_t dex_pc_;
1936
1937 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
1938};
1939
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001940/**
1941 * Some DEX instructions are folded into multiple HInstructions that need
1942 * to stay live until the last HInstruction. This class
1943 * is used as a marker for the baseline compiler to ensure its preceding
1944 * HInstruction stays live. `index` is the temporary number that is used
1945 * for knowing the stack offset where to store the instruction.
1946 */
1947class HTemporary : public HTemplateInstruction<0> {
1948 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001949 explicit HTemporary(size_t index) : HTemplateInstruction(SideEffects::None()), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001950
1951 size_t GetIndex() const { return index_; }
1952
1953 DECLARE_INSTRUCTION(Temporary);
1954
1955 private:
1956 const size_t index_;
1957
1958 DISALLOW_COPY_AND_ASSIGN(HTemporary);
1959};
1960
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001961class HSuspendCheck : public HTemplateInstruction<0> {
1962 public:
1963 explicit HSuspendCheck(uint32_t dex_pc)
Nicolas Geoffray9ebc72c2014-09-25 16:33:42 +01001964 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001965
1966 virtual bool NeedsEnvironment() const {
1967 return true;
1968 }
1969
1970 uint32_t GetDexPc() const { return dex_pc_; }
1971
1972 DECLARE_INSTRUCTION(SuspendCheck);
1973
1974 private:
1975 const uint32_t dex_pc_;
1976
1977 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
1978};
1979
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001980class MoveOperands : public ArenaObject {
1981 public:
Nicolas Geoffray740475d2014-09-29 10:33:25 +01001982 MoveOperands(Location source, Location destination, HInstruction* instruction)
1983 : source_(source), destination_(destination), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001984
1985 Location GetSource() const { return source_; }
1986 Location GetDestination() const { return destination_; }
1987
1988 void SetSource(Location value) { source_ = value; }
1989 void SetDestination(Location value) { destination_ = value; }
1990
1991 // The parallel move resolver marks moves as "in-progress" by clearing the
1992 // destination (but not the source).
1993 Location MarkPending() {
1994 DCHECK(!IsPending());
1995 Location dest = destination_;
1996 destination_ = Location::NoLocation();
1997 return dest;
1998 }
1999
2000 void ClearPending(Location dest) {
2001 DCHECK(IsPending());
2002 destination_ = dest;
2003 }
2004
2005 bool IsPending() const {
2006 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
2007 return destination_.IsInvalid() && !source_.IsInvalid();
2008 }
2009
2010 // True if this blocks a move from the given location.
2011 bool Blocks(Location loc) const {
2012 return !IsEliminated() && source_.Equals(loc);
2013 }
2014
2015 // A move is redundant if it's been eliminated, if its source and
2016 // destination are the same, or if its destination is unneeded.
2017 bool IsRedundant() const {
2018 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
2019 }
2020
2021 // We clear both operands to indicate move that's been eliminated.
2022 void Eliminate() {
2023 source_ = destination_ = Location::NoLocation();
2024 }
2025
2026 bool IsEliminated() const {
2027 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
2028 return source_.IsInvalid();
2029 }
2030
Nicolas Geoffray740475d2014-09-29 10:33:25 +01002031 HInstruction* GetInstruction() const { return instruction_; }
2032
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002033 private:
2034 Location source_;
2035 Location destination_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01002036 // The instruction this move is assocatied with. Null when this move is
2037 // for moving an input in the expected locations of user (including a phi user).
2038 // This is only used in debug mode, to ensure we do not connect interval siblings
2039 // in the same parallel move.
2040 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002041
2042 DISALLOW_COPY_AND_ASSIGN(MoveOperands);
2043};
2044
2045static constexpr size_t kDefaultNumberOfMoves = 4;
2046
2047class HParallelMove : public HTemplateInstruction<0> {
2048 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002049 explicit HParallelMove(ArenaAllocator* arena)
2050 : HTemplateInstruction(SideEffects::None()), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002051
2052 void AddMove(MoveOperands* move) {
Nicolas Geoffray740475d2014-09-29 10:33:25 +01002053 if (kIsDebugBuild && move->GetInstruction() != nullptr) {
2054 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
2055 DCHECK_NE(moves_.Get(i)->GetInstruction(), move->GetInstruction())
2056 << "Doing parallel moves for the same instruction.";
2057 }
2058 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002059 moves_.Add(move);
2060 }
2061
2062 MoveOperands* MoveOperandsAt(size_t index) const {
2063 return moves_.Get(index);
2064 }
2065
2066 size_t NumMoves() const { return moves_.Size(); }
2067
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002068 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002069
2070 private:
2071 GrowableArray<MoveOperands*> moves_;
2072
2073 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
2074};
2075
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002076class HGraphVisitor : public ValueObject {
2077 public:
Dave Allison20dfc792014-06-16 20:44:29 -07002078 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
2079 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002080
Dave Allison20dfc792014-06-16 20:44:29 -07002081 virtual void VisitInstruction(HInstruction* instruction) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002082 virtual void VisitBasicBlock(HBasicBlock* block);
2083
Roland Levillain633021e2014-10-01 14:12:25 +01002084 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002085 void VisitInsertionOrder();
2086
Roland Levillain633021e2014-10-01 14:12:25 +01002087 // Visit the graph following dominator tree reverse post-order.
2088 void VisitReversePostOrder();
2089
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002090 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002091
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002092 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002093#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002094 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
2095
2096 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
2097
2098#undef DECLARE_VISIT_INSTRUCTION
2099
2100 private:
2101 HGraph* graph_;
2102
2103 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
2104};
2105
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002106class HGraphDelegateVisitor : public HGraphVisitor {
2107 public:
2108 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
2109 virtual ~HGraphDelegateVisitor() {}
2110
2111 // Visit functions that delegate to to super class.
2112#define DECLARE_VISIT_INSTRUCTION(name, super) \
2113 virtual void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
2114
2115 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
2116
2117#undef DECLARE_VISIT_INSTRUCTION
2118
2119 private:
2120 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
2121};
2122
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002123class HInsertionOrderIterator : public ValueObject {
2124 public:
2125 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
2126
2127 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
2128 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
2129 void Advance() { ++index_; }
2130
2131 private:
2132 const HGraph& graph_;
2133 size_t index_;
2134
2135 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
2136};
2137
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002138class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002139 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002140 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002141
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002142 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
2143 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002144 void Advance() { ++index_; }
2145
2146 private:
2147 const HGraph& graph_;
2148 size_t index_;
2149
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002150 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002151};
2152
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002153class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002154 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002155 explicit HPostOrderIterator(const HGraph& graph)
2156 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002157
2158 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002159 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002160 void Advance() { --index_; }
2161
2162 private:
2163 const HGraph& graph_;
2164 size_t index_;
2165
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002166 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002167};
2168
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002169} // namespace art
2170
2171#endif // ART_COMPILER_OPTIMIZING_NODES_H_