blob: e9b6b4177ada1d9156435b903b17c864b1fe56fe [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 Geoffraya3d05a42014-10-20 17:41:32 +0100510 M(NewArray, Instruction) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000511
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100512#define FOR_EACH_INSTRUCTION(M) \
513 FOR_EACH_CONCRETE_INSTRUCTION(M) \
514 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +0100515 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +0100516 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100517 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -0700518
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100519#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000520FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
521#undef FORWARD_DECLARATION
522
Roland Levillainccc07a92014-09-16 14:48:16 +0100523#define DECLARE_INSTRUCTION(type) \
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100524 virtual InstructionKind GetKind() const { return k##type; } \
Roland Levillainccc07a92014-09-16 14:48:16 +0100525 virtual const char* DebugName() const { return #type; } \
526 virtual const H##type* As##type() const OVERRIDE { return this; } \
527 virtual H##type* As##type() OVERRIDE { return this; } \
528 virtual bool InstructionTypeEquals(HInstruction* other) const { \
529 return other->Is##type(); \
530 } \
531 virtual void Accept(HGraphVisitor* visitor)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000532
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100533template <typename T>
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000534class HUseListNode : public ArenaObject {
535 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100536 HUseListNode(T* user, size_t index, HUseListNode* tail)
Dave Allison20dfc792014-06-16 20:44:29 -0700537 : user_(user), index_(index), tail_(tail) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000538
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000539 HUseListNode* GetTail() const { return tail_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100540 T* GetUser() const { return user_; }
541 size_t GetIndex() const { return index_; }
542
543 void SetTail(HUseListNode<T>* node) { tail_ = node; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000544
545 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100546 T* const user_;
547 const size_t index_;
548 HUseListNode<T>* tail_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000549
550 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
551};
552
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100553// Represents the side effects an instruction may have.
554class SideEffects : public ValueObject {
555 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100556 SideEffects() : flags_(0) {}
557
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100558 static SideEffects None() {
559 return SideEffects(0);
560 }
561
562 static SideEffects All() {
563 return SideEffects(ChangesSomething().flags_ | DependsOnSomething().flags_);
564 }
565
566 static SideEffects ChangesSomething() {
567 return SideEffects((1 << kFlagChangesCount) - 1);
568 }
569
570 static SideEffects DependsOnSomething() {
571 int count = kFlagDependsOnCount - kFlagChangesCount;
572 return SideEffects(((1 << count) - 1) << kFlagChangesCount);
573 }
574
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100575 SideEffects Union(SideEffects other) const {
576 return SideEffects(flags_ | other.flags_);
577 }
578
Roland Levillain72bceff2014-09-15 18:29:00 +0100579 bool HasSideEffects() const {
580 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
581 return (flags_ & all_bits_set) != 0;
582 }
583
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100584 bool HasAllSideEffects() const {
585 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
586 return all_bits_set == (flags_ & all_bits_set);
587 }
588
589 bool DependsOn(SideEffects other) const {
590 size_t depends_flags = other.ComputeDependsFlags();
591 return (flags_ & depends_flags) != 0;
592 }
593
594 bool HasDependencies() const {
595 int count = kFlagDependsOnCount - kFlagChangesCount;
596 size_t all_bits_set = (1 << count) - 1;
597 return ((flags_ >> kFlagChangesCount) & all_bits_set) != 0;
598 }
599
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100600 private:
601 static constexpr int kFlagChangesSomething = 0;
602 static constexpr int kFlagChangesCount = kFlagChangesSomething + 1;
603
604 static constexpr int kFlagDependsOnSomething = kFlagChangesCount;
605 static constexpr int kFlagDependsOnCount = kFlagDependsOnSomething + 1;
606
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100607 explicit SideEffects(size_t flags) : flags_(flags) {}
608
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100609 size_t ComputeDependsFlags() const {
610 return flags_ << kFlagChangesCount;
611 }
612
613 size_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100614};
615
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000616class HInstruction : public ArenaObject {
617 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100618 explicit HInstruction(SideEffects side_effects)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000619 : previous_(nullptr),
620 next_(nullptr),
621 block_(nullptr),
622 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100623 ssa_index_(-1),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000624 uses_(nullptr),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100625 env_uses_(nullptr),
626 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100627 locations_(nullptr),
628 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100629 lifetime_position_(kNoLifetime),
630 side_effects_(side_effects) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000631
Dave Allison20dfc792014-06-16 20:44:29 -0700632 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000633
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100634#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100635 enum InstructionKind {
636 FOR_EACH_INSTRUCTION(DECLARE_KIND)
637 };
638#undef DECLARE_KIND
639
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000640 HInstruction* GetNext() const { return next_; }
641 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000642
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000643 HBasicBlock* GetBlock() const { return block_; }
644 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100645 bool IsInBlock() const { return block_ != nullptr; }
646 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +0100647 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000648
Roland Levillain6b879dd2014-09-22 17:13:44 +0100649 virtual size_t InputCount() const = 0;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100650 virtual HInstruction* InputAt(size_t i) const = 0;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000651
652 virtual void Accept(HGraphVisitor* visitor) = 0;
653 virtual const char* DebugName() const = 0;
654
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100655 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100656 virtual void SetRawInputAt(size_t index, HInstruction* input) = 0;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100657
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100658 virtual bool NeedsEnvironment() const { return false; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100659 virtual bool IsControlFlow() const { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +0100660 virtual bool CanThrow() const { return false; }
Roland Levillain72bceff2014-09-15 18:29:00 +0100661 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100662
663 void AddUseAt(HInstruction* user, size_t index) {
664 uses_ = new (block_->GetGraph()->GetArena()) HUseListNode<HInstruction>(user, index, uses_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000665 }
666
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100667 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +0100668 DCHECK(user != nullptr);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100669 env_uses_ = new (block_->GetGraph()->GetArena()) HUseListNode<HEnvironment>(
670 user, index, env_uses_);
671 }
672
673 void RemoveUser(HInstruction* user, size_t index);
Nicolas Geoffray724c9632014-09-22 12:27:27 +0100674 void RemoveEnvironmentUser(HEnvironment* user, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100675
676 HUseListNode<HInstruction>* GetUses() const { return uses_; }
677 HUseListNode<HEnvironment>* GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000678
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100679 bool HasUses() const { return uses_ != nullptr || env_uses_ != nullptr; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100680 bool HasEnvironmentUses() const { return env_uses_ != nullptr; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000681
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100682 size_t NumberOfUses() const {
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100683 // TODO: Optimize this method if it is used outside of the HGraphVisualizer.
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100684 size_t result = 0;
685 HUseListNode<HInstruction>* current = uses_;
686 while (current != nullptr) {
687 current = current->GetTail();
688 ++result;
689 }
690 return result;
691 }
692
Roland Levillain6c82d402014-10-13 16:10:27 +0100693 // Does this instruction strictly dominate `other_instruction`?
694 // Returns false if this instruction and `other_instruction` are the same.
695 // Aborts if this instruction and `other_instruction` are both phis.
696 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +0100697
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000698 int GetId() const { return id_; }
699 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000700
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100701 int GetSsaIndex() const { return ssa_index_; }
702 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
703 bool HasSsaIndex() const { return ssa_index_ != -1; }
704
705 bool HasEnvironment() const { return environment_ != nullptr; }
706 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100707 void SetEnvironment(HEnvironment* environment) { environment_ = environment; }
708
Nicolas Geoffray39468442014-09-02 15:17:15 +0100709 // Returns the number of entries in the environment. Typically, that is the
710 // number of dex registers in a method. It could be more in case of inlining.
711 size_t EnvironmentSize() const;
712
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000713 LocationSummary* GetLocations() const { return locations_; }
714 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000715
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100716 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100717 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100718
Dave Allison20dfc792014-06-16 20:44:29 -0700719 bool HasOnlyOneUse() const {
720 return uses_ != nullptr && uses_->GetTail() == nullptr;
721 }
722
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100723#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +0100724 bool Is##type() const { return (As##type() != nullptr); } \
725 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000726 virtual H##type* As##type() { return nullptr; }
727
728 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
729#undef INSTRUCTION_TYPE_CHECK
730
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100731 // Returns whether the instruction can be moved within the graph.
732 virtual bool CanBeMoved() const { return false; }
733
734 // Returns whether the two instructions are of the same kind.
735 virtual bool InstructionTypeEquals(HInstruction* other) const { return false; }
736
737 // Returns whether any data encoded in the two instructions is equal.
738 // This method does not look at the inputs. Both instructions must be
739 // of the same type, otherwise the method has undefined behavior.
740 virtual bool InstructionDataEquals(HInstruction* other) const { return false; }
741
742 // Returns whether two instructions are equal, that is:
743 // 1) They have the same type and contain the same data,
744 // 2) Their inputs are identical.
745 bool Equals(HInstruction* other) const;
746
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100747 virtual InstructionKind GetKind() const = 0;
748
749 virtual size_t ComputeHashCode() const {
750 size_t result = GetKind();
751 for (size_t i = 0, e = InputCount(); i < e; ++i) {
752 result = (result * 31) + InputAt(i)->GetId();
753 }
754 return result;
755 }
756
757 SideEffects GetSideEffects() const { return side_effects_; }
758
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100759 size_t GetLifetimePosition() const { return lifetime_position_; }
760 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
761 LiveInterval* GetLiveInterval() const { return live_interval_; }
762 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
763 bool HasLiveInterval() const { return live_interval_ != nullptr; }
764
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000765 private:
766 HInstruction* previous_;
767 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000768 HBasicBlock* block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000769
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000770 // An instruction gets an id when it is added to the graph.
771 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +0100772 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000773 int id_;
774
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100775 // When doing liveness analysis, instructions that have uses get an SSA index.
776 int ssa_index_;
777
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100778 // List of instructions that have this instruction as input.
779 HUseListNode<HInstruction>* uses_;
780
781 // List of environments that contain this instruction.
782 HUseListNode<HEnvironment>* env_uses_;
783
Nicolas Geoffray39468442014-09-02 15:17:15 +0100784 // The environment associated with this instruction. Not null if the instruction
785 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100786 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000787
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000788 // Set by the code generator.
789 LocationSummary* locations_;
790
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100791 // Set by the liveness analysis.
792 LiveInterval* live_interval_;
793
794 // Set by the liveness analysis, this is the position in a linear
795 // order of blocks where this instruction's live interval start.
796 size_t lifetime_position_;
797
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100798 const SideEffects side_effects_;
799
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000800 friend class HBasicBlock;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100801 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000802
803 DISALLOW_COPY_AND_ASSIGN(HInstruction);
804};
805
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100806template<typename T>
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000807class HUseIterator : public ValueObject {
808 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100809 explicit HUseIterator(HUseListNode<T>* uses) : current_(uses) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000810
811 bool Done() const { return current_ == nullptr; }
812
813 void Advance() {
814 DCHECK(!Done());
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000815 current_ = current_->GetTail();
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000816 }
817
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100818 HUseListNode<T>* Current() const {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000819 DCHECK(!Done());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100820 return current_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000821 }
822
823 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100824 HUseListNode<T>* current_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000825
826 friend class HValue;
827};
828
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100829// A HEnvironment object contains the values of virtual registers at a given location.
830class HEnvironment : public ArenaObject {
831 public:
832 HEnvironment(ArenaAllocator* arena, size_t number_of_vregs) : vregs_(arena, number_of_vregs) {
833 vregs_.SetSize(number_of_vregs);
834 for (size_t i = 0; i < number_of_vregs; i++) {
835 vregs_.Put(i, nullptr);
836 }
837 }
838
839 void Populate(const GrowableArray<HInstruction*>& env) {
840 for (size_t i = 0; i < env.Size(); i++) {
841 HInstruction* instruction = env.Get(i);
842 vregs_.Put(i, instruction);
843 if (instruction != nullptr) {
844 instruction->AddEnvUseAt(this, i);
845 }
846 }
847 }
848
849 void SetRawEnvAt(size_t index, HInstruction* instruction) {
850 vregs_.Put(index, instruction);
851 }
852
Nicolas Geoffray39468442014-09-02 15:17:15 +0100853 HInstruction* GetInstructionAt(size_t index) const {
854 return vregs_.Get(index);
855 }
856
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100857 GrowableArray<HInstruction*>* GetVRegs() {
858 return &vregs_;
859 }
860
Nicolas Geoffray39468442014-09-02 15:17:15 +0100861 size_t Size() const { return vregs_.Size(); }
862
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100863 private:
864 GrowableArray<HInstruction*> vregs_;
865
866 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
867};
868
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000869class HInputIterator : public ValueObject {
870 public:
Dave Allison20dfc792014-06-16 20:44:29 -0700871 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000872
873 bool Done() const { return index_ == instruction_->InputCount(); }
874 HInstruction* Current() const { return instruction_->InputAt(index_); }
875 void Advance() { index_++; }
876
877 private:
878 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100879 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000880
881 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
882};
883
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000884class HInstructionIterator : public ValueObject {
885 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100886 explicit HInstructionIterator(const HInstructionList& instructions)
887 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000888 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000889 }
890
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000891 bool Done() const { return instruction_ == nullptr; }
892 HInstruction* Current() const { return instruction_; }
893 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000894 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000895 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000896 }
897
898 private:
899 HInstruction* instruction_;
900 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100901
902 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000903};
904
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100905class HBackwardInstructionIterator : public ValueObject {
906 public:
907 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
908 : instruction_(instructions.last_instruction_) {
909 next_ = Done() ? nullptr : instruction_->GetPrevious();
910 }
911
912 bool Done() const { return instruction_ == nullptr; }
913 HInstruction* Current() const { return instruction_; }
914 void Advance() {
915 instruction_ = next_;
916 next_ = Done() ? nullptr : instruction_->GetPrevious();
917 }
918
919 private:
920 HInstruction* instruction_;
921 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100922
923 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100924};
925
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000926// An embedded container with N elements of type T. Used (with partial
927// specialization for N=0) because embedded arrays cannot have size 0.
928template<typename T, intptr_t N>
929class EmbeddedArray {
930 public:
Dave Allison20dfc792014-06-16 20:44:29 -0700931 EmbeddedArray() : elements_() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000932
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000933 intptr_t GetLength() const { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000934
935 const T& operator[](intptr_t i) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000936 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000937 return elements_[i];
938 }
939
940 T& operator[](intptr_t i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000941 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000942 return elements_[i];
943 }
944
945 const T& At(intptr_t i) const {
946 return (*this)[i];
947 }
948
949 void SetAt(intptr_t i, const T& val) {
950 (*this)[i] = val;
951 }
952
953 private:
954 T elements_[N];
955};
956
957template<typename T>
958class EmbeddedArray<T, 0> {
959 public:
960 intptr_t length() const { return 0; }
961 const T& operator[](intptr_t i) const {
962 LOG(FATAL) << "Unreachable";
963 static T sentinel = 0;
964 return sentinel;
965 }
966 T& operator[](intptr_t i) {
967 LOG(FATAL) << "Unreachable";
968 static T sentinel = 0;
969 return sentinel;
970 }
971};
972
973template<intptr_t N>
974class HTemplateInstruction: public HInstruction {
975 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100976 HTemplateInstruction<N>(SideEffects side_effects)
977 : HInstruction(side_effects), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -0700978 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000979
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100980 virtual size_t InputCount() const { return N; }
981 virtual HInstruction* InputAt(size_t i) const { return inputs_[i]; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000982
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000983 protected:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100984 virtual void SetRawInputAt(size_t i, HInstruction* instruction) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000985 inputs_[i] = instruction;
986 }
987
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000988 private:
989 EmbeddedArray<HInstruction*, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100990
991 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000992};
993
Dave Allison20dfc792014-06-16 20:44:29 -0700994template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100995class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -0700996 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100997 HExpression<N>(Primitive::Type type, SideEffects side_effects)
998 : HTemplateInstruction<N>(side_effects), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -0700999 virtual ~HExpression() {}
1000
1001 virtual Primitive::Type GetType() const { return type_; }
1002
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001003 protected:
1004 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07001005};
1006
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001007// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
1008// instruction that branches to the exit block.
1009class HReturnVoid : public HTemplateInstruction<0> {
1010 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001011 HReturnVoid() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001012
1013 virtual bool IsControlFlow() const { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001014
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001015 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001016
1017 private:
1018 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
1019};
1020
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001021// Represents dex's RETURN opcodes. A HReturn is a control flow
1022// instruction that branches to the exit block.
1023class HReturn : public HTemplateInstruction<1> {
1024 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001025 explicit HReturn(HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001026 SetRawInputAt(0, value);
1027 }
1028
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001029 virtual bool IsControlFlow() const { return true; }
1030
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001031 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001032
1033 private:
1034 DISALLOW_COPY_AND_ASSIGN(HReturn);
1035};
1036
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001037// The exit instruction is the only instruction of the exit block.
1038// Instructions aborting the method (HTrow and HReturn) must branch to the
1039// exit block.
1040class HExit : public HTemplateInstruction<0> {
1041 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001042 HExit() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001043
1044 virtual bool IsControlFlow() const { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001045
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001046 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001047
1048 private:
1049 DISALLOW_COPY_AND_ASSIGN(HExit);
1050};
1051
1052// Jumps from one block to another.
1053class HGoto : public HTemplateInstruction<0> {
1054 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001055 HGoto() : HTemplateInstruction(SideEffects::None()) {}
1056
1057 virtual bool IsControlFlow() const { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001058
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001059 HBasicBlock* GetSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001060 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001061 }
1062
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001063 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001064
1065 private:
1066 DISALLOW_COPY_AND_ASSIGN(HGoto);
1067};
1068
Dave Allison20dfc792014-06-16 20:44:29 -07001069
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001070// Conditional branch. A block ending with an HIf instruction must have
1071// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001072class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001073 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001074 explicit HIf(HInstruction* input) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001075 SetRawInputAt(0, input);
1076 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001077
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001078 virtual bool IsControlFlow() const { return true; }
1079
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001080 HBasicBlock* IfTrueSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001081 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001082 }
1083
1084 HBasicBlock* IfFalseSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001085 return GetBlock()->GetSuccessors().Get(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001086 }
1087
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001088 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001089
Dave Allison20dfc792014-06-16 20:44:29 -07001090 virtual bool IsIfInstruction() const { return true; }
1091
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001092 private:
1093 DISALLOW_COPY_AND_ASSIGN(HIf);
1094};
1095
Roland Levillain88cb1752014-10-20 16:36:47 +01001096class HUnaryOperation : public HExpression<1> {
1097 public:
1098 HUnaryOperation(Primitive::Type result_type, HInstruction* input)
1099 : HExpression(result_type, SideEffects::None()) {
1100 SetRawInputAt(0, input);
1101 }
1102
1103 HInstruction* GetInput() const { return InputAt(0); }
1104 Primitive::Type GetResultType() const { return GetType(); }
1105
1106 virtual bool CanBeMoved() const { return true; }
1107 virtual bool InstructionDataEquals(HInstruction* other) const { return true; }
1108
Roland Levillain9240d6a2014-10-20 16:47:04 +01001109 // Try to statically evaluate `operation` and return a HConstant
1110 // containing the result of this evaluation. If `operation` cannot
1111 // be evaluated as a constant, return nullptr.
1112 HConstant* TryStaticEvaluation() const;
1113
1114 // Apply this operation to `x`.
1115 virtual int32_t Evaluate(int32_t x) const = 0;
1116 virtual int64_t Evaluate(int64_t x) const = 0;
1117
Roland Levillain88cb1752014-10-20 16:36:47 +01001118 DECLARE_INSTRUCTION(UnaryOperation);
1119
1120 private:
1121 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
1122};
1123
Dave Allison20dfc792014-06-16 20:44:29 -07001124class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001125 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001126 HBinaryOperation(Primitive::Type result_type,
1127 HInstruction* left,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001128 HInstruction* right) : HExpression(result_type, SideEffects::None()) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001129 SetRawInputAt(0, left);
1130 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001131 }
1132
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001133 HInstruction* GetLeft() const { return InputAt(0); }
1134 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07001135 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001136
1137 virtual bool IsCommutative() { return false; }
1138
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001139 virtual bool CanBeMoved() const { return true; }
1140 virtual bool InstructionDataEquals(HInstruction* other) const { return true; }
1141
Roland Levillain9240d6a2014-10-20 16:47:04 +01001142 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01001143 // containing the result of this evaluation. If `operation` cannot
1144 // be evaluated as a constant, return nullptr.
Roland Levillain9240d6a2014-10-20 16:47:04 +01001145 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01001146
1147 // Apply this operation to `x` and `y`.
1148 virtual int32_t Evaluate(int32_t x, int32_t y) const = 0;
1149 virtual int64_t Evaluate(int64_t x, int64_t y) const = 0;
1150
Roland Levillainccc07a92014-09-16 14:48:16 +01001151 DECLARE_INSTRUCTION(BinaryOperation);
1152
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001153 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001154 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
1155};
1156
Dave Allison20dfc792014-06-16 20:44:29 -07001157class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001158 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001159 HCondition(HInstruction* first, HInstruction* second)
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001160 : HBinaryOperation(Primitive::kPrimBoolean, first, second),
1161 needs_materialization_(true) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001162
1163 virtual bool IsCommutative() { return true; }
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001164
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001165 bool NeedsMaterialization() const { return needs_materialization_; }
1166 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001167
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001168 // For code generation purposes, returns whether this instruction is just before
1169 // `if_`, and disregard moves in between.
1170 bool IsBeforeWhenDisregardMoves(HIf* if_) const;
1171
Dave Allison20dfc792014-06-16 20:44:29 -07001172 DECLARE_INSTRUCTION(Condition);
1173
1174 virtual IfCondition GetCondition() const = 0;
1175
1176 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001177 // For register allocation purposes, returns whether this instruction needs to be
1178 // materialized (that is, not just be in the processor flags).
1179 bool needs_materialization_;
1180
Dave Allison20dfc792014-06-16 20:44:29 -07001181 DISALLOW_COPY_AND_ASSIGN(HCondition);
1182};
1183
1184// Instruction to check if two inputs are equal to each other.
1185class HEqual : public HCondition {
1186 public:
1187 HEqual(HInstruction* first, HInstruction* second)
1188 : HCondition(first, second) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001189
Roland Levillain93445682014-10-06 19:24:02 +01001190 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1191 return x == y ? 1 : 0;
1192 }
1193 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1194 return x == y ? 1 : 0;
1195 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001196
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001197 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001198
Dave Allison20dfc792014-06-16 20:44:29 -07001199 virtual IfCondition GetCondition() const {
1200 return kCondEQ;
1201 }
1202
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001203 private:
1204 DISALLOW_COPY_AND_ASSIGN(HEqual);
1205};
1206
Dave Allison20dfc792014-06-16 20:44:29 -07001207class HNotEqual : public HCondition {
1208 public:
1209 HNotEqual(HInstruction* first, HInstruction* second)
1210 : HCondition(first, second) {}
1211
Roland Levillain93445682014-10-06 19:24:02 +01001212 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1213 return x != y ? 1 : 0;
1214 }
1215 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1216 return x != y ? 1 : 0;
1217 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001218
Dave Allison20dfc792014-06-16 20:44:29 -07001219 DECLARE_INSTRUCTION(NotEqual);
1220
1221 virtual IfCondition GetCondition() const {
1222 return kCondNE;
1223 }
1224
1225 private:
1226 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
1227};
1228
1229class HLessThan : public HCondition {
1230 public:
1231 HLessThan(HInstruction* first, HInstruction* second)
1232 : HCondition(first, second) {}
1233
Roland Levillain93445682014-10-06 19:24:02 +01001234 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1235 return x < y ? 1 : 0;
1236 }
1237 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1238 return x < y ? 1 : 0;
1239 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001240
Dave Allison20dfc792014-06-16 20:44:29 -07001241 DECLARE_INSTRUCTION(LessThan);
1242
1243 virtual IfCondition GetCondition() const {
1244 return kCondLT;
1245 }
1246
1247 private:
1248 DISALLOW_COPY_AND_ASSIGN(HLessThan);
1249};
1250
1251class HLessThanOrEqual : public HCondition {
1252 public:
1253 HLessThanOrEqual(HInstruction* first, HInstruction* second)
1254 : HCondition(first, second) {}
1255
Roland Levillain93445682014-10-06 19:24:02 +01001256 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1257 return x <= y ? 1 : 0;
1258 }
1259 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1260 return x <= y ? 1 : 0;
1261 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001262
Dave Allison20dfc792014-06-16 20:44:29 -07001263 DECLARE_INSTRUCTION(LessThanOrEqual);
1264
1265 virtual IfCondition GetCondition() const {
1266 return kCondLE;
1267 }
1268
1269 private:
1270 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
1271};
1272
1273class HGreaterThan : public HCondition {
1274 public:
1275 HGreaterThan(HInstruction* first, HInstruction* second)
1276 : HCondition(first, second) {}
1277
Roland Levillain93445682014-10-06 19:24:02 +01001278 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1279 return x > y ? 1 : 0;
1280 }
1281 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1282 return x > y ? 1 : 0;
1283 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001284
Dave Allison20dfc792014-06-16 20:44:29 -07001285 DECLARE_INSTRUCTION(GreaterThan);
1286
1287 virtual IfCondition GetCondition() const {
1288 return kCondGT;
1289 }
1290
1291 private:
1292 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
1293};
1294
1295class HGreaterThanOrEqual : public HCondition {
1296 public:
1297 HGreaterThanOrEqual(HInstruction* first, HInstruction* second)
1298 : HCondition(first, second) {}
1299
Roland Levillain93445682014-10-06 19:24:02 +01001300 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1301 return x >= y ? 1 : 0;
1302 }
1303 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1304 return x >= y ? 1 : 0;
1305 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001306
Dave Allison20dfc792014-06-16 20:44:29 -07001307 DECLARE_INSTRUCTION(GreaterThanOrEqual);
1308
1309 virtual IfCondition GetCondition() const {
1310 return kCondGE;
1311 }
1312
1313 private:
1314 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
1315};
1316
1317
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001318// Instruction to check how two inputs compare to each other.
1319// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
1320class HCompare : public HBinaryOperation {
1321 public:
1322 HCompare(Primitive::Type type, HInstruction* first, HInstruction* second)
1323 : HBinaryOperation(Primitive::kPrimInt, first, second) {
1324 DCHECK_EQ(type, first->GetType());
1325 DCHECK_EQ(type, second->GetType());
1326 }
1327
Roland Levillain93445682014-10-06 19:24:02 +01001328 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001329 return
1330 x == y ? 0 :
1331 x > y ? 1 :
1332 -1;
1333 }
Roland Levillain93445682014-10-06 19:24:02 +01001334 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001335 return
1336 x == y ? 0 :
1337 x > y ? 1 :
1338 -1;
1339 }
1340
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001341 DECLARE_INSTRUCTION(Compare);
1342
1343 private:
1344 DISALLOW_COPY_AND_ASSIGN(HCompare);
1345};
1346
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001347// A local in the graph. Corresponds to a Dex register.
1348class HLocal : public HTemplateInstruction<0> {
1349 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001350 explicit HLocal(uint16_t reg_number)
1351 : HTemplateInstruction(SideEffects::None()), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001352
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001353 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001354
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001355 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001356
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001357 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001358 // The Dex register number.
1359 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001360
1361 DISALLOW_COPY_AND_ASSIGN(HLocal);
1362};
1363
1364// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07001365class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001366 public:
Roland Levillain5799fc02014-09-25 12:15:20 +01001367 HLoadLocal(HLocal* local, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001368 : HExpression(type, SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001369 SetRawInputAt(0, local);
1370 }
1371
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001372 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1373
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001374 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001375
1376 private:
1377 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
1378};
1379
1380// Store a value in a given local. This instruction has two inputs: the value
1381// and the local.
1382class HStoreLocal : public HTemplateInstruction<2> {
1383 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001384 HStoreLocal(HLocal* local, HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001385 SetRawInputAt(0, local);
1386 SetRawInputAt(1, value);
1387 }
1388
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001389 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1390
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001391 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001392
1393 private:
1394 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
1395};
1396
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001397class HConstant : public HExpression<0> {
1398 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001399 explicit HConstant(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
1400
1401 virtual bool CanBeMoved() const { return true; }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001402
1403 DECLARE_INSTRUCTION(Constant);
1404
1405 private:
1406 DISALLOW_COPY_AND_ASSIGN(HConstant);
1407};
1408
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001409class HFloatConstant : public HConstant {
1410 public:
1411 explicit HFloatConstant(float value) : HConstant(Primitive::kPrimFloat), value_(value) {}
1412
1413 float GetValue() const { return value_; }
1414
1415 virtual bool InstructionDataEquals(HInstruction* other) const {
1416 return bit_cast<float, int32_t>(other->AsFloatConstant()->value_) ==
1417 bit_cast<float, int32_t>(value_);
1418 }
1419
1420 virtual size_t ComputeHashCode() const { return static_cast<size_t>(GetValue()); }
1421
1422 DECLARE_INSTRUCTION(FloatConstant);
1423
1424 private:
1425 const float value_;
1426
1427 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
1428};
1429
1430class HDoubleConstant : public HConstant {
1431 public:
1432 explicit HDoubleConstant(double value) : HConstant(Primitive::kPrimDouble), value_(value) {}
1433
1434 double GetValue() const { return value_; }
1435
1436 virtual bool InstructionDataEquals(HInstruction* other) const {
1437 return bit_cast<double, int64_t>(other->AsDoubleConstant()->value_) ==
1438 bit_cast<double, int64_t>(value_);
1439 }
1440
1441 virtual size_t ComputeHashCode() const { return static_cast<size_t>(GetValue()); }
1442
1443 DECLARE_INSTRUCTION(DoubleConstant);
1444
1445 private:
1446 const double value_;
1447
1448 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
1449};
1450
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001451// Constants of the type int. Those can be from Dex instructions, or
1452// synthesized (for example with the if-eqz instruction).
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001453class HIntConstant : public HConstant {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001454 public:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001455 explicit HIntConstant(int32_t value) : HConstant(Primitive::kPrimInt), value_(value) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001456
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001457 int32_t GetValue() const { return value_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001458
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001459 virtual bool InstructionDataEquals(HInstruction* other) const {
1460 return other->AsIntConstant()->value_ == value_;
1461 }
1462
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001463 virtual size_t ComputeHashCode() const { return GetValue(); }
1464
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001465 DECLARE_INSTRUCTION(IntConstant);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001466
1467 private:
1468 const int32_t value_;
1469
1470 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
1471};
1472
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001473class HLongConstant : public HConstant {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001474 public:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001475 explicit HLongConstant(int64_t value) : HConstant(Primitive::kPrimLong), value_(value) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001476
1477 int64_t GetValue() const { return value_; }
1478
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001479 virtual bool InstructionDataEquals(HInstruction* other) const {
1480 return other->AsLongConstant()->value_ == value_;
1481 }
1482
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001483 virtual size_t ComputeHashCode() const { return static_cast<size_t>(GetValue()); }
1484
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001485 DECLARE_INSTRUCTION(LongConstant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001486
1487 private:
1488 const int64_t value_;
1489
1490 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
1491};
1492
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001493class HInvoke : public HInstruction {
1494 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001495 HInvoke(ArenaAllocator* arena,
1496 uint32_t number_of_arguments,
1497 Primitive::Type return_type,
1498 uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001499 : HInstruction(SideEffects::All()),
1500 inputs_(arena, number_of_arguments),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001501 return_type_(return_type),
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001502 dex_pc_(dex_pc) {
1503 inputs_.SetSize(number_of_arguments);
1504 }
1505
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001506 virtual size_t InputCount() const { return inputs_.Size(); }
1507 virtual HInstruction* InputAt(size_t i) const { return inputs_.Get(i); }
1508
1509 // Runtime needs to walk the stack, so Dex -> Dex calls need to
1510 // know their environment.
1511 virtual bool NeedsEnvironment() const { return true; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001512
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001513 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001514 SetRawInputAt(index, argument);
1515 }
1516
1517 virtual void SetRawInputAt(size_t index, HInstruction* input) {
1518 inputs_.Put(index, input);
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001519 }
1520
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001521 virtual Primitive::Type GetType() const { return return_type_; }
1522
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001523 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001524
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001525 DECLARE_INSTRUCTION(Invoke);
1526
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001527 protected:
1528 GrowableArray<HInstruction*> inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001529 const Primitive::Type return_type_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001530 const uint32_t dex_pc_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001531
1532 private:
1533 DISALLOW_COPY_AND_ASSIGN(HInvoke);
1534};
1535
1536class HInvokeStatic : public HInvoke {
1537 public:
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001538 HInvokeStatic(ArenaAllocator* arena,
1539 uint32_t number_of_arguments,
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001540 Primitive::Type return_type,
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001541 uint32_t dex_pc,
1542 uint32_t index_in_dex_cache)
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001543 : HInvoke(arena, number_of_arguments, return_type, dex_pc),
1544 index_in_dex_cache_(index_in_dex_cache) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001545
1546 uint32_t GetIndexInDexCache() const { return index_in_dex_cache_; }
1547
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001548 DECLARE_INSTRUCTION(InvokeStatic);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001549
1550 private:
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001551 const uint32_t index_in_dex_cache_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001552
1553 DISALLOW_COPY_AND_ASSIGN(HInvokeStatic);
1554};
1555
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001556class HInvokeVirtual : public HInvoke {
1557 public:
1558 HInvokeVirtual(ArenaAllocator* arena,
1559 uint32_t number_of_arguments,
1560 Primitive::Type return_type,
1561 uint32_t dex_pc,
1562 uint32_t vtable_index)
1563 : HInvoke(arena, number_of_arguments, return_type, dex_pc),
1564 vtable_index_(vtable_index) {}
1565
1566 uint32_t GetVTableIndex() const { return vtable_index_; }
1567
1568 DECLARE_INSTRUCTION(InvokeVirtual);
1569
1570 private:
1571 const uint32_t vtable_index_;
1572
1573 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
1574};
1575
Dave Allison20dfc792014-06-16 20:44:29 -07001576class HNewInstance : public HExpression<0> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001577 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001578 HNewInstance(uint32_t dex_pc, uint16_t type_index)
1579 : HExpression(Primitive::kPrimNot, SideEffects::None()),
1580 dex_pc_(dex_pc),
1581 type_index_(type_index) {}
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001582
1583 uint32_t GetDexPc() const { return dex_pc_; }
1584 uint16_t GetTypeIndex() const { return type_index_; }
1585
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001586 // Calls runtime so needs an environment.
1587 virtual bool NeedsEnvironment() const { return true; }
1588
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001589 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001590
1591 private:
1592 const uint32_t dex_pc_;
1593 const uint16_t type_index_;
1594
1595 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
1596};
1597
Roland Levillain88cb1752014-10-20 16:36:47 +01001598class HNeg : public HUnaryOperation {
1599 public:
1600 explicit HNeg(Primitive::Type result_type, HInstruction* input)
1601 : HUnaryOperation(result_type, input) {}
1602
Roland Levillain9240d6a2014-10-20 16:47:04 +01001603 virtual int32_t Evaluate(int32_t x) const OVERRIDE { return -x; }
1604 virtual int64_t Evaluate(int64_t x) const OVERRIDE { return -x; }
1605
Roland Levillain88cb1752014-10-20 16:36:47 +01001606 DECLARE_INSTRUCTION(Neg);
1607
1608 private:
1609 DISALLOW_COPY_AND_ASSIGN(HNeg);
1610};
1611
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001612class HNewArray : public HExpression<1> {
1613 public:
1614 HNewArray(HInstruction* length, uint32_t dex_pc, uint16_t type_index)
1615 : HExpression(Primitive::kPrimNot, SideEffects::None()),
1616 dex_pc_(dex_pc),
1617 type_index_(type_index) {
1618 SetRawInputAt(0, length);
1619 }
1620
1621 uint32_t GetDexPc() const { return dex_pc_; }
1622 uint16_t GetTypeIndex() const { return type_index_; }
1623
1624 // Calls runtime so needs an environment.
1625 virtual bool NeedsEnvironment() const { return true; }
1626
1627 DECLARE_INSTRUCTION(NewArray);
1628
1629 private:
1630 const uint32_t dex_pc_;
1631 const uint16_t type_index_;
1632
1633 DISALLOW_COPY_AND_ASSIGN(HNewArray);
1634};
1635
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001636class HAdd : public HBinaryOperation {
1637 public:
1638 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1639 : HBinaryOperation(result_type, left, right) {}
1640
1641 virtual bool IsCommutative() { return true; }
1642
Roland Levillain93445682014-10-06 19:24:02 +01001643 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1644 return x + y;
1645 }
1646 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1647 return x + y;
1648 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001649
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001650 DECLARE_INSTRUCTION(Add);
1651
1652 private:
1653 DISALLOW_COPY_AND_ASSIGN(HAdd);
1654};
1655
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001656class HSub : public HBinaryOperation {
1657 public:
1658 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1659 : HBinaryOperation(result_type, left, right) {}
1660
1661 virtual bool IsCommutative() { return false; }
1662
Roland Levillain93445682014-10-06 19:24:02 +01001663 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1664 return x - y;
1665 }
1666 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1667 return x - y;
1668 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001669
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001670 DECLARE_INSTRUCTION(Sub);
1671
1672 private:
1673 DISALLOW_COPY_AND_ASSIGN(HSub);
1674};
1675
Calin Juravle34bacdf2014-10-07 20:23:36 +01001676class HMul : public HBinaryOperation {
1677 public:
1678 HMul(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1679 : HBinaryOperation(result_type, left, right) {}
1680
1681 virtual bool IsCommutative() { return true; }
1682
1683 virtual int32_t Evaluate(int32_t x, int32_t y) const { return x * y; }
1684 virtual int64_t Evaluate(int64_t x, int64_t y) const { return x * y; }
1685
1686 DECLARE_INSTRUCTION(Mul);
1687
1688 private:
1689 DISALLOW_COPY_AND_ASSIGN(HMul);
1690};
1691
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001692// The value of a parameter in this method. Its location depends on
1693// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07001694class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001695 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001696 HParameterValue(uint8_t index, Primitive::Type parameter_type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001697 : HExpression(parameter_type, SideEffects::None()), index_(index) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001698
1699 uint8_t GetIndex() const { return index_; }
1700
1701 DECLARE_INSTRUCTION(ParameterValue);
1702
1703 private:
1704 // The index of this parameter in the parameters list. Must be less
1705 // than HGraph::number_of_in_vregs_;
1706 const uint8_t index_;
1707
1708 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
1709};
1710
Dave Allison20dfc792014-06-16 20:44:29 -07001711class HNot : public HExpression<1> {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001712 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001713 explicit HNot(HInstruction* input) : HExpression(Primitive::kPrimBoolean, SideEffects::None()) {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001714 SetRawInputAt(0, input);
1715 }
1716
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001717 virtual bool CanBeMoved() const { return true; }
1718 virtual bool InstructionDataEquals(HInstruction* other) const { return true; }
1719
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001720 DECLARE_INSTRUCTION(Not);
1721
1722 private:
1723 DISALLOW_COPY_AND_ASSIGN(HNot);
1724};
1725
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001726class HPhi : public HInstruction {
1727 public:
1728 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001729 : HInstruction(SideEffects::None()),
1730 inputs_(arena, number_of_inputs),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001731 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001732 type_(type),
1733 is_live_(false) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001734 inputs_.SetSize(number_of_inputs);
1735 }
1736
1737 virtual size_t InputCount() const { return inputs_.Size(); }
1738 virtual HInstruction* InputAt(size_t i) const { return inputs_.Get(i); }
1739
1740 virtual void SetRawInputAt(size_t index, HInstruction* input) {
1741 inputs_.Put(index, input);
1742 }
1743
1744 void AddInput(HInstruction* input);
1745
1746 virtual Primitive::Type GetType() const { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001747 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001748
1749 uint32_t GetRegNumber() const { return reg_number_; }
1750
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001751 void SetDead() { is_live_ = false; }
1752 void SetLive() { is_live_ = true; }
1753 bool IsDead() const { return !is_live_; }
1754 bool IsLive() const { return is_live_; }
1755
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001756 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001757
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001758 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001759 GrowableArray<HInstruction*> inputs_;
1760 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001761 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001762 bool is_live_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001763
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001764 DISALLOW_COPY_AND_ASSIGN(HPhi);
1765};
1766
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001767class HNullCheck : public HExpression<1> {
1768 public:
1769 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001770 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001771 SetRawInputAt(0, value);
1772 }
1773
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001774 virtual bool CanBeMoved() const { return true; }
1775 virtual bool InstructionDataEquals(HInstruction* other) const { return true; }
1776
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001777 virtual bool NeedsEnvironment() const { return true; }
1778
Roland Levillaine161a2a2014-10-03 12:45:18 +01001779 virtual bool CanThrow() const { return true; }
1780
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001781 uint32_t GetDexPc() const { return dex_pc_; }
1782
1783 DECLARE_INSTRUCTION(NullCheck);
1784
1785 private:
1786 const uint32_t dex_pc_;
1787
1788 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
1789};
1790
1791class FieldInfo : public ValueObject {
1792 public:
Roland Levillain5799fc02014-09-25 12:15:20 +01001793 FieldInfo(MemberOffset field_offset, Primitive::Type field_type)
Nicolas Geoffray39468442014-09-02 15:17:15 +01001794 : field_offset_(field_offset), field_type_(field_type) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001795
1796 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001797 Primitive::Type GetFieldType() const { return field_type_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001798
1799 private:
1800 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01001801 const Primitive::Type field_type_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001802};
1803
1804class HInstanceFieldGet : public HExpression<1> {
1805 public:
1806 HInstanceFieldGet(HInstruction* value,
1807 Primitive::Type field_type,
1808 MemberOffset field_offset)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001809 : HExpression(field_type, SideEffects::DependsOnSomething()),
1810 field_info_(field_offset, field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001811 SetRawInputAt(0, value);
1812 }
1813
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001814 virtual bool CanBeMoved() const { return true; }
1815 virtual bool InstructionDataEquals(HInstruction* other) const {
1816 size_t other_offset = other->AsInstanceFieldGet()->GetFieldOffset().SizeValue();
1817 return other_offset == GetFieldOffset().SizeValue();
1818 }
1819
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001820 virtual size_t ComputeHashCode() const {
1821 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
1822 }
1823
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001824 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001825 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001826
1827 DECLARE_INSTRUCTION(InstanceFieldGet);
1828
1829 private:
1830 const FieldInfo field_info_;
1831
1832 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
1833};
1834
1835class HInstanceFieldSet : public HTemplateInstruction<2> {
1836 public:
1837 HInstanceFieldSet(HInstruction* object,
1838 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01001839 Primitive::Type field_type,
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001840 MemberOffset field_offset)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001841 : HTemplateInstruction(SideEffects::ChangesSomething()),
1842 field_info_(field_offset, field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001843 SetRawInputAt(0, object);
1844 SetRawInputAt(1, value);
1845 }
1846
1847 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001848 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001849
1850 DECLARE_INSTRUCTION(InstanceFieldSet);
1851
1852 private:
1853 const FieldInfo field_info_;
1854
1855 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
1856};
1857
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001858class HArrayGet : public HExpression<2> {
1859 public:
1860 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001861 : HExpression(type, SideEffects::DependsOnSomething()) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001862 SetRawInputAt(0, array);
1863 SetRawInputAt(1, index);
1864 }
1865
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001866 virtual bool CanBeMoved() const { return true; }
1867 virtual bool InstructionDataEquals(HInstruction* other) const { return true; }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001868 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001869
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001870 DECLARE_INSTRUCTION(ArrayGet);
1871
1872 private:
1873 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
1874};
1875
1876class HArraySet : public HTemplateInstruction<3> {
1877 public:
1878 HArraySet(HInstruction* array,
1879 HInstruction* index,
1880 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001881 Primitive::Type expected_component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001882 uint32_t dex_pc)
1883 : HTemplateInstruction(SideEffects::ChangesSomething()),
1884 dex_pc_(dex_pc),
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001885 expected_component_type_(expected_component_type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001886 SetRawInputAt(0, array);
1887 SetRawInputAt(1, index);
1888 SetRawInputAt(2, value);
1889 }
1890
1891 virtual bool NeedsEnvironment() const {
1892 // We currently always call a runtime method to catch array store
1893 // exceptions.
1894 return InputAt(2)->GetType() == Primitive::kPrimNot;
1895 }
1896
1897 uint32_t GetDexPc() const { return dex_pc_; }
1898
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001899 HInstruction* GetValue() const { return InputAt(2); }
1900
1901 Primitive::Type GetComponentType() const {
1902 // The Dex format does not type floating point index operations. Since the
1903 // `expected_component_type_` is set during building and can therefore not
1904 // be correct, we also check what is the value type. If it is a floating
1905 // point type, we must use that type.
1906 Primitive::Type value_type = GetValue()->GetType();
1907 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
1908 ? value_type
1909 : expected_component_type_;
1910 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001911
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001912 DECLARE_INSTRUCTION(ArraySet);
1913
1914 private:
1915 const uint32_t dex_pc_;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001916 const Primitive::Type expected_component_type_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001917
1918 DISALLOW_COPY_AND_ASSIGN(HArraySet);
1919};
1920
1921class HArrayLength : public HExpression<1> {
1922 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001923 explicit HArrayLength(HInstruction* array)
1924 : HExpression(Primitive::kPrimInt, SideEffects::None()) {
1925 // Note that arrays do not change length, so the instruction does not
1926 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001927 SetRawInputAt(0, array);
1928 }
1929
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001930 virtual bool CanBeMoved() const { return true; }
1931 virtual bool InstructionDataEquals(HInstruction* other) const { return true; }
1932
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001933 DECLARE_INSTRUCTION(ArrayLength);
1934
1935 private:
1936 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
1937};
1938
1939class HBoundsCheck : public HExpression<2> {
1940 public:
1941 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001942 : HExpression(index->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001943 DCHECK(index->GetType() == Primitive::kPrimInt);
1944 SetRawInputAt(0, index);
1945 SetRawInputAt(1, length);
1946 }
1947
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001948 virtual bool CanBeMoved() const { return true; }
1949 virtual bool InstructionDataEquals(HInstruction* other) const { return true; }
1950
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001951 virtual bool NeedsEnvironment() const { return true; }
1952
Roland Levillaine161a2a2014-10-03 12:45:18 +01001953 virtual bool CanThrow() const { return true; }
1954
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001955 uint32_t GetDexPc() const { return dex_pc_; }
1956
1957 DECLARE_INSTRUCTION(BoundsCheck);
1958
1959 private:
1960 const uint32_t dex_pc_;
1961
1962 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
1963};
1964
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001965/**
1966 * Some DEX instructions are folded into multiple HInstructions that need
1967 * to stay live until the last HInstruction. This class
1968 * is used as a marker for the baseline compiler to ensure its preceding
1969 * HInstruction stays live. `index` is the temporary number that is used
1970 * for knowing the stack offset where to store the instruction.
1971 */
1972class HTemporary : public HTemplateInstruction<0> {
1973 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001974 explicit HTemporary(size_t index) : HTemplateInstruction(SideEffects::None()), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001975
1976 size_t GetIndex() const { return index_; }
1977
1978 DECLARE_INSTRUCTION(Temporary);
1979
1980 private:
1981 const size_t index_;
1982
1983 DISALLOW_COPY_AND_ASSIGN(HTemporary);
1984};
1985
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001986class HSuspendCheck : public HTemplateInstruction<0> {
1987 public:
1988 explicit HSuspendCheck(uint32_t dex_pc)
Nicolas Geoffray9ebc72c2014-09-25 16:33:42 +01001989 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001990
1991 virtual bool NeedsEnvironment() const {
1992 return true;
1993 }
1994
1995 uint32_t GetDexPc() const { return dex_pc_; }
1996
1997 DECLARE_INSTRUCTION(SuspendCheck);
1998
1999 private:
2000 const uint32_t dex_pc_;
2001
2002 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
2003};
2004
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002005class MoveOperands : public ArenaObject {
2006 public:
Nicolas Geoffray740475d2014-09-29 10:33:25 +01002007 MoveOperands(Location source, Location destination, HInstruction* instruction)
2008 : source_(source), destination_(destination), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002009
2010 Location GetSource() const { return source_; }
2011 Location GetDestination() const { return destination_; }
2012
2013 void SetSource(Location value) { source_ = value; }
2014 void SetDestination(Location value) { destination_ = value; }
2015
2016 // The parallel move resolver marks moves as "in-progress" by clearing the
2017 // destination (but not the source).
2018 Location MarkPending() {
2019 DCHECK(!IsPending());
2020 Location dest = destination_;
2021 destination_ = Location::NoLocation();
2022 return dest;
2023 }
2024
2025 void ClearPending(Location dest) {
2026 DCHECK(IsPending());
2027 destination_ = dest;
2028 }
2029
2030 bool IsPending() const {
2031 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
2032 return destination_.IsInvalid() && !source_.IsInvalid();
2033 }
2034
2035 // True if this blocks a move from the given location.
2036 bool Blocks(Location loc) const {
2037 return !IsEliminated() && source_.Equals(loc);
2038 }
2039
2040 // A move is redundant if it's been eliminated, if its source and
2041 // destination are the same, or if its destination is unneeded.
2042 bool IsRedundant() const {
2043 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
2044 }
2045
2046 // We clear both operands to indicate move that's been eliminated.
2047 void Eliminate() {
2048 source_ = destination_ = Location::NoLocation();
2049 }
2050
2051 bool IsEliminated() const {
2052 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
2053 return source_.IsInvalid();
2054 }
2055
Nicolas Geoffray740475d2014-09-29 10:33:25 +01002056 HInstruction* GetInstruction() const { return instruction_; }
2057
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002058 private:
2059 Location source_;
2060 Location destination_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01002061 // The instruction this move is assocatied with. Null when this move is
2062 // for moving an input in the expected locations of user (including a phi user).
2063 // This is only used in debug mode, to ensure we do not connect interval siblings
2064 // in the same parallel move.
2065 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002066
2067 DISALLOW_COPY_AND_ASSIGN(MoveOperands);
2068};
2069
2070static constexpr size_t kDefaultNumberOfMoves = 4;
2071
2072class HParallelMove : public HTemplateInstruction<0> {
2073 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002074 explicit HParallelMove(ArenaAllocator* arena)
2075 : HTemplateInstruction(SideEffects::None()), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002076
2077 void AddMove(MoveOperands* move) {
Nicolas Geoffray740475d2014-09-29 10:33:25 +01002078 if (kIsDebugBuild && move->GetInstruction() != nullptr) {
2079 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
2080 DCHECK_NE(moves_.Get(i)->GetInstruction(), move->GetInstruction())
2081 << "Doing parallel moves for the same instruction.";
2082 }
2083 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002084 moves_.Add(move);
2085 }
2086
2087 MoveOperands* MoveOperandsAt(size_t index) const {
2088 return moves_.Get(index);
2089 }
2090
2091 size_t NumMoves() const { return moves_.Size(); }
2092
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002093 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002094
2095 private:
2096 GrowableArray<MoveOperands*> moves_;
2097
2098 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
2099};
2100
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002101class HGraphVisitor : public ValueObject {
2102 public:
Dave Allison20dfc792014-06-16 20:44:29 -07002103 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
2104 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002105
Dave Allison20dfc792014-06-16 20:44:29 -07002106 virtual void VisitInstruction(HInstruction* instruction) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002107 virtual void VisitBasicBlock(HBasicBlock* block);
2108
Roland Levillain633021e2014-10-01 14:12:25 +01002109 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002110 void VisitInsertionOrder();
2111
Roland Levillain633021e2014-10-01 14:12:25 +01002112 // Visit the graph following dominator tree reverse post-order.
2113 void VisitReversePostOrder();
2114
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002115 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002116
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002117 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002118#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002119 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
2120
2121 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
2122
2123#undef DECLARE_VISIT_INSTRUCTION
2124
2125 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07002126 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002127
2128 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
2129};
2130
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002131class HGraphDelegateVisitor : public HGraphVisitor {
2132 public:
2133 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
2134 virtual ~HGraphDelegateVisitor() {}
2135
2136 // Visit functions that delegate to to super class.
2137#define DECLARE_VISIT_INSTRUCTION(name, super) \
2138 virtual void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
2139
2140 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
2141
2142#undef DECLARE_VISIT_INSTRUCTION
2143
2144 private:
2145 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
2146};
2147
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002148class HInsertionOrderIterator : public ValueObject {
2149 public:
2150 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
2151
2152 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
2153 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
2154 void Advance() { ++index_; }
2155
2156 private:
2157 const HGraph& graph_;
2158 size_t index_;
2159
2160 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
2161};
2162
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002163class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002164 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002165 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002166
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002167 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
2168 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002169 void Advance() { ++index_; }
2170
2171 private:
2172 const HGraph& graph_;
2173 size_t index_;
2174
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002175 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002176};
2177
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002178class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002179 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002180 explicit HPostOrderIterator(const HGraph& graph)
2181 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002182
2183 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002184 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002185 void Advance() { --index_; }
2186
2187 private:
2188 const HGraph& graph_;
2189 size_t index_;
2190
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002191 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002192};
2193
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002194} // namespace art
2195
2196#endif // ART_COMPILER_OPTIMIZING_NODES_H_