blob: 0c75e41fc49ed19a8ed0c67a12b29eb82d7a0446 [file] [log] [blame]
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ART_COMPILER_OPTIMIZING_NODES_H_
18#define ART_COMPILER_OPTIMIZING_NODES_H_
19
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010020#include "locations.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010021#include "offsets.h"
Ian Rogerse63db272014-07-15 15:36:11 -070022#include "primitive.h"
Ian Rogers0279ebb2014-10-08 17:27:48 -070023#include "utils/arena_object.h"
Nicolas Geoffray0e336432014-02-26 18:24:38 +000024#include "utils/arena_bit_vector.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000025#include "utils/growable_array.h"
26
27namespace art {
28
29class HBasicBlock;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010030class HEnvironment;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000031class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000032class HIntConstant;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000033class HGraphVisitor;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010034class HPhi;
Nicolas Geoffray3c049742014-09-24 18:10:46 +010035class HSuspendCheck;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010036class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000037class LocationSummary;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000038
39static const int kDefaultNumberOfBlocks = 8;
40static const int kDefaultNumberOfSuccessors = 2;
41static const int kDefaultNumberOfPredecessors = 2;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010042static const int kDefaultNumberOfDominatedBlocks = 1;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000043static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000044
Dave Allison20dfc792014-06-16 20:44:29 -070045enum IfCondition {
46 kCondEQ,
47 kCondNE,
48 kCondLT,
49 kCondLE,
50 kCondGT,
51 kCondGE,
52};
53
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010054class HInstructionList {
55 public:
56 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
57
58 void AddInstruction(HInstruction* instruction);
59 void RemoveInstruction(HInstruction* instruction);
60
Roland Levillain6b469232014-09-25 10:10:38 +010061 // Return true if this list contains `instruction`.
62 bool Contains(HInstruction* instruction) const;
63
Roland Levillainccc07a92014-09-16 14:48:16 +010064 // Return true if `instruction1` is found before `instruction2` in
65 // this instruction list and false otherwise. Abort if none
66 // of these instructions is found.
67 bool FoundBefore(const HInstruction* instruction1,
68 const HInstruction* instruction2) const;
69
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010070 private:
71 HInstruction* first_instruction_;
72 HInstruction* last_instruction_;
73
74 friend class HBasicBlock;
75 friend class HInstructionIterator;
Nicolas Geoffray804d0932014-05-02 08:46:00 +010076 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010077
78 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
79};
80
Nicolas Geoffray818f2102014-02-18 16:43:35 +000081// Control-flow graph of a method. Contains a list of basic blocks.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070082class HGraph : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +000083 public:
84 explicit HGraph(ArenaAllocator* arena)
85 : arena_(arena),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000086 blocks_(arena, kDefaultNumberOfBlocks),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +010087 reverse_post_order_(arena, kDefaultNumberOfBlocks),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070088 entry_block_(nullptr),
89 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +010090 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +010091 number_of_vregs_(0),
92 number_of_in_vregs_(0),
Nicolas Geoffraye5038322014-07-04 09:41:32 +010093 number_of_temporaries_(0),
Dave Allison20dfc792014-06-16 20:44:29 -070094 current_instruction_id_(0) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +000095
Nicolas Geoffray787c3072014-03-17 10:20:19 +000096 ArenaAllocator* GetArena() const { return arena_; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +010097 const GrowableArray<HBasicBlock*>& GetBlocks() const { return blocks_; }
Roland Levillain93445682014-10-06 19:24:02 +010098 HBasicBlock* GetBlock(size_t id) const { return blocks_.Get(id); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +000099
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000100 HBasicBlock* GetEntryBlock() const { return entry_block_; }
101 HBasicBlock* GetExitBlock() const { return exit_block_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000102
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000103 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
104 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000105
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000106 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100107
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000108 void BuildDominatorTree();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100109 void TransformToSSA();
110 void SimplifyCFG();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000111
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100112 // Find all natural loops in this graph. Aborts computation and returns false
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100113 // if one loop is not natural, that is the header does not dominate the back
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100114 // edge.
115 bool FindNaturalLoops() const;
116
117 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
118 void SimplifyLoop(HBasicBlock* header);
119
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000120 int GetNextInstructionId() {
121 return current_instruction_id_++;
122 }
123
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100124 uint16_t GetMaximumNumberOfOutVRegs() const {
125 return maximum_number_of_out_vregs_;
126 }
127
128 void UpdateMaximumNumberOfOutVRegs(uint16_t new_value) {
129 maximum_number_of_out_vregs_ = std::max(new_value, maximum_number_of_out_vregs_);
130 }
131
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100132 void UpdateNumberOfTemporaries(size_t count) {
133 number_of_temporaries_ = std::max(count, number_of_temporaries_);
134 }
135
136 size_t GetNumberOfTemporaries() const {
137 return number_of_temporaries_;
138 }
139
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100140 void SetNumberOfVRegs(uint16_t number_of_vregs) {
141 number_of_vregs_ = number_of_vregs;
142 }
143
144 uint16_t GetNumberOfVRegs() const {
145 return number_of_vregs_;
146 }
147
148 void SetNumberOfInVRegs(uint16_t value) {
149 number_of_in_vregs_ = value;
150 }
151
152 uint16_t GetNumberOfInVRegs() const {
153 return number_of_in_vregs_;
154 }
155
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100156 uint16_t GetNumberOfLocalVRegs() const {
157 return number_of_vregs_ - number_of_in_vregs_;
158 }
159
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100160 const GrowableArray<HBasicBlock*>& GetReversePostOrder() const {
161 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100162 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100163
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000164 private:
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000165 HBasicBlock* FindCommonDominator(HBasicBlock* first, HBasicBlock* second) const;
166 void VisitBlockForDominatorTree(HBasicBlock* block,
167 HBasicBlock* predecessor,
168 GrowableArray<size_t>* visits);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100169 void FindBackEdges(ArenaBitVector* visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000170 void VisitBlockForBackEdges(HBasicBlock* block,
171 ArenaBitVector* visited,
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100172 ArenaBitVector* visiting);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000173 void RemoveDeadBlocks(const ArenaBitVector& visited) const;
174
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000175 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000176
177 // List of blocks in insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000178 GrowableArray<HBasicBlock*> blocks_;
179
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100180 // List of blocks to perform a reverse post order tree traversal.
181 GrowableArray<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000182
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000183 HBasicBlock* entry_block_;
184 HBasicBlock* exit_block_;
185
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100186 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100187 uint16_t maximum_number_of_out_vregs_;
188
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100189 // The number of virtual registers in this method. Contains the parameters.
190 uint16_t number_of_vregs_;
191
192 // The number of virtual registers used by parameters of this method.
193 uint16_t number_of_in_vregs_;
194
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100195 // The number of temporaries that will be needed for the baseline compiler.
196 size_t number_of_temporaries_;
197
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000198 // The current id to assign to a newly added instruction. See HInstruction.id_.
199 int current_instruction_id_;
200
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000201 DISALLOW_COPY_AND_ASSIGN(HGraph);
202};
203
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700204class HLoopInformation : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000205 public:
206 HLoopInformation(HBasicBlock* header, HGraph* graph)
207 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100208 suspend_check_(nullptr),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100209 back_edges_(graph->GetArena(), kDefaultNumberOfBackEdges),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100210 // Make bit vector growable, as the number of blocks may change.
211 blocks_(graph->GetArena(), graph->GetBlocks().Size(), true) {}
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100212
213 HBasicBlock* GetHeader() const {
214 return header_;
215 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000216
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100217 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
218 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
219 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
220
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000221 void AddBackEdge(HBasicBlock* back_edge) {
222 back_edges_.Add(back_edge);
223 }
224
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100225 void RemoveBackEdge(HBasicBlock* back_edge) {
226 back_edges_.Delete(back_edge);
227 }
228
229 bool IsBackEdge(HBasicBlock* block) {
230 for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) {
231 if (back_edges_.Get(i) == block) return true;
232 }
233 return false;
234 }
235
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000236 int NumberOfBackEdges() const {
237 return back_edges_.Size();
238 }
239
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100240 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100241
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100242 const GrowableArray<HBasicBlock*>& GetBackEdges() const {
243 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100244 }
245
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100246 void ClearBackEdges() {
247 back_edges_.Reset();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100248 }
249
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100250 // Find blocks that are part of this loop. Returns whether the loop is a natural loop,
251 // that is the header dominates the back edge.
252 bool Populate();
253
254 // Returns whether this loop information contains `block`.
255 // Note that this loop information *must* be populated before entering this function.
256 bool Contains(const HBasicBlock& block) const;
257
258 // Returns whether this loop information is an inner loop of `other`.
259 // Note that `other` *must* be populated before entering this function.
260 bool IsIn(const HLoopInformation& other) const;
261
262 const ArenaBitVector& GetBlocks() const { return blocks_; }
263
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000264 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100265 // Internal recursive implementation of `Populate`.
266 void PopulateRecursive(HBasicBlock* block);
267
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000268 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100269 HSuspendCheck* suspend_check_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000270 GrowableArray<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100271 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000272
273 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
274};
275
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100276static constexpr size_t kNoLifetime = -1;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100277static constexpr uint32_t kNoDexPc = -1;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100278
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000279// A block in a method. Contains the list of instructions represented
280// as a double linked list. Each block knows its predecessors and
281// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100282
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700283class HBasicBlock : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000284 public:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100285 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000286 : graph_(graph),
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000287 predecessors_(graph->GetArena(), kDefaultNumberOfPredecessors),
288 successors_(graph->GetArena(), kDefaultNumberOfSuccessors),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000289 loop_information_(nullptr),
290 dominator_(nullptr),
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100291 dominated_blocks_(graph->GetArena(), kDefaultNumberOfDominatedBlocks),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100292 block_id_(-1),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100293 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100294 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000295 lifetime_end_(kNoLifetime),
296 is_catch_block_(false) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000297
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100298 const GrowableArray<HBasicBlock*>& GetPredecessors() const {
299 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000300 }
301
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100302 const GrowableArray<HBasicBlock*>& GetSuccessors() const {
303 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000304 }
305
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100306 const GrowableArray<HBasicBlock*>& GetDominatedBlocks() const {
307 return dominated_blocks_;
308 }
309
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100310 bool IsEntryBlock() const {
311 return graph_->GetEntryBlock() == this;
312 }
313
314 bool IsExitBlock() const {
315 return graph_->GetExitBlock() == this;
316 }
317
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000318 void AddBackEdge(HBasicBlock* back_edge) {
319 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000320 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000321 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100322 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000323 loop_information_->AddBackEdge(back_edge);
324 }
325
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000326 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000327
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000328 int GetBlockId() const { return block_id_; }
329 void SetBlockId(int id) { block_id_ = id; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000330
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000331 HBasicBlock* GetDominator() const { return dominator_; }
332 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100333 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.Add(block); }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000334
335 int NumberOfBackEdges() const {
336 return loop_information_ == nullptr
337 ? 0
338 : loop_information_->NumberOfBackEdges();
339 }
340
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100341 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
342 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100343 const HInstructionList& GetInstructions() const { return instructions_; }
344 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100345 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000346
347 void AddSuccessor(HBasicBlock* block) {
348 successors_.Add(block);
349 block->predecessors_.Add(this);
350 }
351
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100352 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
353 size_t successor_index = GetSuccessorIndexOf(existing);
354 DCHECK_NE(successor_index, static_cast<size_t>(-1));
355 existing->RemovePredecessor(this);
356 new_block->predecessors_.Add(this);
357 successors_.Put(successor_index, new_block);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000358 }
359
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100360 void RemovePredecessor(HBasicBlock* block) {
361 predecessors_.Delete(block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100362 }
363
364 void ClearAllPredecessors() {
365 predecessors_.Reset();
366 }
367
368 void AddPredecessor(HBasicBlock* block) {
369 predecessors_.Add(block);
370 block->successors_.Add(this);
371 }
372
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100373 void SwapPredecessors() {
Nicolas Geoffrayc83d4412014-09-18 16:46:20 +0100374 DCHECK_EQ(predecessors_.Size(), 2u);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100375 HBasicBlock* temp = predecessors_.Get(0);
376 predecessors_.Put(0, predecessors_.Get(1));
377 predecessors_.Put(1, temp);
378 }
379
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100380 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) {
381 for (size_t i = 0, e = predecessors_.Size(); i < e; ++i) {
382 if (predecessors_.Get(i) == predecessor) {
383 return i;
384 }
385 }
386 return -1;
387 }
388
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100389 size_t GetSuccessorIndexOf(HBasicBlock* successor) {
390 for (size_t i = 0, e = successors_.Size(); i < e; ++i) {
391 if (successors_.Get(i) == successor) {
392 return i;
393 }
394 }
395 return -1;
396 }
397
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000398 void AddInstruction(HInstruction* instruction);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100399 void RemoveInstruction(HInstruction* instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100400 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +0100401 // Replace instruction `initial` with `replacement` within this block.
402 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
403 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100404 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100405 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100406 void RemovePhi(HPhi* phi);
407
408 bool IsLoopHeader() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100409 return (loop_information_ != nullptr) && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100410 }
411
Roland Levillain6b879dd2014-09-22 17:13:44 +0100412 bool IsLoopPreHeaderFirstPredecessor() const {
413 DCHECK(IsLoopHeader());
414 DCHECK(!GetPredecessors().IsEmpty());
415 return GetPredecessors().Get(0) == GetLoopInformation()->GetPreHeader();
416 }
417
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100418 HLoopInformation* GetLoopInformation() const {
419 return loop_information_;
420 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000421
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100422 // Set the loop_information_ on this block. This method overrides the current
423 // loop_information if it is an outer loop of the passed loop information.
424 void SetInLoop(HLoopInformation* info) {
425 if (IsLoopHeader()) {
426 // Nothing to do. This just means `info` is an outer loop.
427 } else if (loop_information_ == nullptr) {
428 loop_information_ = info;
429 } else if (loop_information_->Contains(*info->GetHeader())) {
430 // Block is currently part of an outer loop. Make it part of this inner loop.
431 // Note that a non loop header having a loop information means this loop information
432 // has already been populated
433 loop_information_ = info;
434 } else {
435 // Block is part of an inner loop. Do not update the loop information.
436 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
437 // at this point, because this method is being called while populating `info`.
438 }
439 }
440
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100441 bool IsInLoop() const { return loop_information_ != nullptr; }
442
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100443 // Returns wheter this block dominates the blocked passed as parameter.
444 bool Dominates(HBasicBlock* block) const;
445
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100446 size_t GetLifetimeStart() const { return lifetime_start_; }
447 size_t GetLifetimeEnd() const { return lifetime_end_; }
448
449 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
450 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
451
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100452 uint32_t GetDexPc() const { return dex_pc_; }
453
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000454 bool IsCatchBlock() const { return is_catch_block_; }
455 void SetIsCatchBlock() { is_catch_block_ = true; }
456
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000457 private:
458 HGraph* const graph_;
459 GrowableArray<HBasicBlock*> predecessors_;
460 GrowableArray<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100461 HInstructionList instructions_;
462 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000463 HLoopInformation* loop_information_;
464 HBasicBlock* dominator_;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100465 GrowableArray<HBasicBlock*> dominated_blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000466 int block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100467 // The dex program counter of the first instruction of this block.
468 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100469 size_t lifetime_start_;
470 size_t lifetime_end_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000471 bool is_catch_block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000472
473 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
474};
475
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100476#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
477 M(Add, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000478 M(ArrayGet, Instruction) \
479 M(ArrayLength, Instruction) \
480 M(ArraySet, Instruction) \
481 M(BoundsCheck, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000482 M(CheckCast, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100483 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000484 M(Compare, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100485 M(Condition, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000486 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +0000487 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000488 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100489 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000490 M(Exit, Instruction) \
491 M(FloatConstant, Constant) \
492 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100493 M(GreaterThan, Condition) \
494 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100495 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000496 M(InstanceFieldGet, Instruction) \
497 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000498 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100499 M(IntConstant, Constant) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +0000500 M(InvokeInterface, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100501 M(InvokeStatic, Invoke) \
502 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000503 M(LessThan, Condition) \
504 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000505 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000506 M(LoadException, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100507 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000508 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100509 M(Local, Instruction) \
510 M(LongConstant, Constant) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000511 M(Mul, BinaryOperation) \
512 M(Neg, UnaryOperation) \
513 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100514 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +0100515 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000516 M(NotEqual, Condition) \
517 M(NullCheck, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100518 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000519 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100520 M(Phi, Instruction) \
521 M(Return, Instruction) \
522 M(ReturnVoid, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100523 M(StaticFieldGet, Instruction) \
524 M(StaticFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100525 M(StoreLocal, Instruction) \
526 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100527 M(SuspendCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000528 M(Temporary, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000529 M(Throw, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +0000530 M(TypeConversion, Instruction) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000531
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100532#define FOR_EACH_INSTRUCTION(M) \
533 FOR_EACH_CONCRETE_INSTRUCTION(M) \
534 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +0100535 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +0100536 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100537 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -0700538
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100539#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000540FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
541#undef FORWARD_DECLARATION
542
Roland Levillainccc07a92014-09-16 14:48:16 +0100543#define DECLARE_INSTRUCTION(type) \
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100544 virtual InstructionKind GetKind() const { return k##type; } \
Roland Levillainccc07a92014-09-16 14:48:16 +0100545 virtual const char* DebugName() const { return #type; } \
546 virtual const H##type* As##type() const OVERRIDE { return this; } \
547 virtual H##type* As##type() OVERRIDE { return this; } \
548 virtual bool InstructionTypeEquals(HInstruction* other) const { \
549 return other->Is##type(); \
550 } \
551 virtual void Accept(HGraphVisitor* visitor)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000552
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100553template <typename T>
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700554class HUseListNode : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000555 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100556 HUseListNode(T* user, size_t index, HUseListNode* tail)
Dave Allison20dfc792014-06-16 20:44:29 -0700557 : user_(user), index_(index), tail_(tail) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000558
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000559 HUseListNode* GetTail() const { return tail_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100560 T* GetUser() const { return user_; }
561 size_t GetIndex() const { return index_; }
562
563 void SetTail(HUseListNode<T>* node) { tail_ = node; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000564
565 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100566 T* const user_;
567 const size_t index_;
568 HUseListNode<T>* tail_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000569
570 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
571};
572
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100573// Represents the side effects an instruction may have.
574class SideEffects : public ValueObject {
575 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100576 SideEffects() : flags_(0) {}
577
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100578 static SideEffects None() {
579 return SideEffects(0);
580 }
581
582 static SideEffects All() {
583 return SideEffects(ChangesSomething().flags_ | DependsOnSomething().flags_);
584 }
585
586 static SideEffects ChangesSomething() {
587 return SideEffects((1 << kFlagChangesCount) - 1);
588 }
589
590 static SideEffects DependsOnSomething() {
591 int count = kFlagDependsOnCount - kFlagChangesCount;
592 return SideEffects(((1 << count) - 1) << kFlagChangesCount);
593 }
594
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100595 SideEffects Union(SideEffects other) const {
596 return SideEffects(flags_ | other.flags_);
597 }
598
Roland Levillain72bceff2014-09-15 18:29:00 +0100599 bool HasSideEffects() const {
600 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
601 return (flags_ & all_bits_set) != 0;
602 }
603
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100604 bool HasAllSideEffects() const {
605 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
606 return all_bits_set == (flags_ & all_bits_set);
607 }
608
609 bool DependsOn(SideEffects other) const {
610 size_t depends_flags = other.ComputeDependsFlags();
611 return (flags_ & depends_flags) != 0;
612 }
613
614 bool HasDependencies() const {
615 int count = kFlagDependsOnCount - kFlagChangesCount;
616 size_t all_bits_set = (1 << count) - 1;
617 return ((flags_ >> kFlagChangesCount) & all_bits_set) != 0;
618 }
619
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100620 private:
621 static constexpr int kFlagChangesSomething = 0;
622 static constexpr int kFlagChangesCount = kFlagChangesSomething + 1;
623
624 static constexpr int kFlagDependsOnSomething = kFlagChangesCount;
625 static constexpr int kFlagDependsOnCount = kFlagDependsOnSomething + 1;
626
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100627 explicit SideEffects(size_t flags) : flags_(flags) {}
628
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100629 size_t ComputeDependsFlags() const {
630 return flags_ << kFlagChangesCount;
631 }
632
633 size_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100634};
635
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700636class HInstruction : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000637 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100638 explicit HInstruction(SideEffects side_effects)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000639 : previous_(nullptr),
640 next_(nullptr),
641 block_(nullptr),
642 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100643 ssa_index_(-1),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000644 uses_(nullptr),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100645 env_uses_(nullptr),
646 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100647 locations_(nullptr),
648 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100649 lifetime_position_(kNoLifetime),
650 side_effects_(side_effects) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000651
Dave Allison20dfc792014-06-16 20:44:29 -0700652 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000653
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100654#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100655 enum InstructionKind {
656 FOR_EACH_INSTRUCTION(DECLARE_KIND)
657 };
658#undef DECLARE_KIND
659
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000660 HInstruction* GetNext() const { return next_; }
661 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000662
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000663 HBasicBlock* GetBlock() const { return block_; }
664 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100665 bool IsInBlock() const { return block_ != nullptr; }
666 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +0100667 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000668
Roland Levillain6b879dd2014-09-22 17:13:44 +0100669 virtual size_t InputCount() const = 0;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100670 virtual HInstruction* InputAt(size_t i) const = 0;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000671
672 virtual void Accept(HGraphVisitor* visitor) = 0;
673 virtual const char* DebugName() const = 0;
674
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100675 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100676 virtual void SetRawInputAt(size_t index, HInstruction* input) = 0;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100677
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100678 virtual bool NeedsEnvironment() const { return false; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100679 virtual bool IsControlFlow() const { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +0100680 virtual bool CanThrow() const { return false; }
Roland Levillain72bceff2014-09-15 18:29:00 +0100681 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100682
683 void AddUseAt(HInstruction* user, size_t index) {
684 uses_ = new (block_->GetGraph()->GetArena()) HUseListNode<HInstruction>(user, index, uses_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000685 }
686
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100687 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +0100688 DCHECK(user != nullptr);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100689 env_uses_ = new (block_->GetGraph()->GetArena()) HUseListNode<HEnvironment>(
690 user, index, env_uses_);
691 }
692
693 void RemoveUser(HInstruction* user, size_t index);
Nicolas Geoffray724c9632014-09-22 12:27:27 +0100694 void RemoveEnvironmentUser(HEnvironment* user, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100695
696 HUseListNode<HInstruction>* GetUses() const { return uses_; }
697 HUseListNode<HEnvironment>* GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000698
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100699 bool HasUses() const { return uses_ != nullptr || env_uses_ != nullptr; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100700 bool HasEnvironmentUses() const { return env_uses_ != nullptr; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000701
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100702 size_t NumberOfUses() const {
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100703 // TODO: Optimize this method if it is used outside of the HGraphVisualizer.
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100704 size_t result = 0;
705 HUseListNode<HInstruction>* current = uses_;
706 while (current != nullptr) {
707 current = current->GetTail();
708 ++result;
709 }
710 return result;
711 }
712
Roland Levillain6c82d402014-10-13 16:10:27 +0100713 // Does this instruction strictly dominate `other_instruction`?
714 // Returns false if this instruction and `other_instruction` are the same.
715 // Aborts if this instruction and `other_instruction` are both phis.
716 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +0100717
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000718 int GetId() const { return id_; }
719 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000720
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100721 int GetSsaIndex() const { return ssa_index_; }
722 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
723 bool HasSsaIndex() const { return ssa_index_ != -1; }
724
725 bool HasEnvironment() const { return environment_ != nullptr; }
726 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100727 void SetEnvironment(HEnvironment* environment) { environment_ = environment; }
728
Nicolas Geoffray39468442014-09-02 15:17:15 +0100729 // Returns the number of entries in the environment. Typically, that is the
730 // number of dex registers in a method. It could be more in case of inlining.
731 size_t EnvironmentSize() const;
732
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000733 LocationSummary* GetLocations() const { return locations_; }
734 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000735
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100736 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100737 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100738
Dave Allison20dfc792014-06-16 20:44:29 -0700739 bool HasOnlyOneUse() const {
740 return uses_ != nullptr && uses_->GetTail() == nullptr;
741 }
742
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100743#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +0100744 bool Is##type() const { return (As##type() != nullptr); } \
745 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000746 virtual H##type* As##type() { return nullptr; }
747
748 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
749#undef INSTRUCTION_TYPE_CHECK
750
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100751 // Returns whether the instruction can be moved within the graph.
752 virtual bool CanBeMoved() const { return false; }
753
754 // Returns whether the two instructions are of the same kind.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700755 virtual bool InstructionTypeEquals(HInstruction* other) const {
756 UNUSED(other);
757 return false;
758 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100759
760 // Returns whether any data encoded in the two instructions is equal.
761 // This method does not look at the inputs. Both instructions must be
762 // of the same type, otherwise the method has undefined behavior.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700763 virtual bool InstructionDataEquals(HInstruction* other) const {
764 UNUSED(other);
765 return false;
766 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100767
768 // Returns whether two instructions are equal, that is:
769 // 1) They have the same type and contain the same data,
770 // 2) Their inputs are identical.
771 bool Equals(HInstruction* other) const;
772
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100773 virtual InstructionKind GetKind() const = 0;
774
775 virtual size_t ComputeHashCode() const {
776 size_t result = GetKind();
777 for (size_t i = 0, e = InputCount(); i < e; ++i) {
778 result = (result * 31) + InputAt(i)->GetId();
779 }
780 return result;
781 }
782
783 SideEffects GetSideEffects() const { return side_effects_; }
784
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100785 size_t GetLifetimePosition() const { return lifetime_position_; }
786 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
787 LiveInterval* GetLiveInterval() const { return live_interval_; }
788 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
789 bool HasLiveInterval() const { return live_interval_ != nullptr; }
790
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000791 private:
792 HInstruction* previous_;
793 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000794 HBasicBlock* block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000795
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000796 // An instruction gets an id when it is added to the graph.
797 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +0100798 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000799 int id_;
800
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100801 // When doing liveness analysis, instructions that have uses get an SSA index.
802 int ssa_index_;
803
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100804 // List of instructions that have this instruction as input.
805 HUseListNode<HInstruction>* uses_;
806
807 // List of environments that contain this instruction.
808 HUseListNode<HEnvironment>* env_uses_;
809
Nicolas Geoffray39468442014-09-02 15:17:15 +0100810 // The environment associated with this instruction. Not null if the instruction
811 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100812 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000813
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000814 // Set by the code generator.
815 LocationSummary* locations_;
816
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100817 // Set by the liveness analysis.
818 LiveInterval* live_interval_;
819
820 // Set by the liveness analysis, this is the position in a linear
821 // order of blocks where this instruction's live interval start.
822 size_t lifetime_position_;
823
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100824 const SideEffects side_effects_;
825
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000826 friend class HBasicBlock;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100827 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000828
829 DISALLOW_COPY_AND_ASSIGN(HInstruction);
830};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700831std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000832
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100833template<typename T>
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000834class HUseIterator : public ValueObject {
835 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100836 explicit HUseIterator(HUseListNode<T>* uses) : current_(uses) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000837
838 bool Done() const { return current_ == nullptr; }
839
840 void Advance() {
841 DCHECK(!Done());
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000842 current_ = current_->GetTail();
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000843 }
844
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100845 HUseListNode<T>* Current() const {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000846 DCHECK(!Done());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100847 return current_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000848 }
849
850 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100851 HUseListNode<T>* current_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000852
853 friend class HValue;
854};
855
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100856// A HEnvironment object contains the values of virtual registers at a given location.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700857class HEnvironment : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100858 public:
859 HEnvironment(ArenaAllocator* arena, size_t number_of_vregs) : vregs_(arena, number_of_vregs) {
860 vregs_.SetSize(number_of_vregs);
861 for (size_t i = 0; i < number_of_vregs; i++) {
862 vregs_.Put(i, nullptr);
863 }
864 }
865
866 void Populate(const GrowableArray<HInstruction*>& env) {
867 for (size_t i = 0; i < env.Size(); i++) {
868 HInstruction* instruction = env.Get(i);
869 vregs_.Put(i, instruction);
870 if (instruction != nullptr) {
871 instruction->AddEnvUseAt(this, i);
872 }
873 }
874 }
875
876 void SetRawEnvAt(size_t index, HInstruction* instruction) {
877 vregs_.Put(index, instruction);
878 }
879
Nicolas Geoffray39468442014-09-02 15:17:15 +0100880 HInstruction* GetInstructionAt(size_t index) const {
881 return vregs_.Get(index);
882 }
883
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100884 GrowableArray<HInstruction*>* GetVRegs() {
885 return &vregs_;
886 }
887
Nicolas Geoffray39468442014-09-02 15:17:15 +0100888 size_t Size() const { return vregs_.Size(); }
889
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100890 private:
891 GrowableArray<HInstruction*> vregs_;
892
893 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
894};
895
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000896class HInputIterator : public ValueObject {
897 public:
Dave Allison20dfc792014-06-16 20:44:29 -0700898 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000899
900 bool Done() const { return index_ == instruction_->InputCount(); }
901 HInstruction* Current() const { return instruction_->InputAt(index_); }
902 void Advance() { index_++; }
903
904 private:
905 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100906 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000907
908 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
909};
910
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000911class HInstructionIterator : public ValueObject {
912 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100913 explicit HInstructionIterator(const HInstructionList& instructions)
914 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000915 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000916 }
917
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000918 bool Done() const { return instruction_ == nullptr; }
919 HInstruction* Current() const { return instruction_; }
920 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000921 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000922 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000923 }
924
925 private:
926 HInstruction* instruction_;
927 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100928
929 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000930};
931
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100932class HBackwardInstructionIterator : public ValueObject {
933 public:
934 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
935 : instruction_(instructions.last_instruction_) {
936 next_ = Done() ? nullptr : instruction_->GetPrevious();
937 }
938
939 bool Done() const { return instruction_ == nullptr; }
940 HInstruction* Current() const { return instruction_; }
941 void Advance() {
942 instruction_ = next_;
943 next_ = Done() ? nullptr : instruction_->GetPrevious();
944 }
945
946 private:
947 HInstruction* instruction_;
948 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100949
950 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100951};
952
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000953// An embedded container with N elements of type T. Used (with partial
954// specialization for N=0) because embedded arrays cannot have size 0.
955template<typename T, intptr_t N>
956class EmbeddedArray {
957 public:
Dave Allison20dfc792014-06-16 20:44:29 -0700958 EmbeddedArray() : elements_() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000959
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000960 intptr_t GetLength() const { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000961
962 const T& operator[](intptr_t i) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000963 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000964 return elements_[i];
965 }
966
967 T& operator[](intptr_t i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000968 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000969 return elements_[i];
970 }
971
972 const T& At(intptr_t i) const {
973 return (*this)[i];
974 }
975
976 void SetAt(intptr_t i, const T& val) {
977 (*this)[i] = val;
978 }
979
980 private:
981 T elements_[N];
982};
983
984template<typename T>
985class EmbeddedArray<T, 0> {
986 public:
987 intptr_t length() const { return 0; }
988 const T& operator[](intptr_t i) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700989 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000990 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700991 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000992 }
993 T& operator[](intptr_t i) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700994 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000995 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700996 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000997 }
998};
999
1000template<intptr_t N>
1001class HTemplateInstruction: public HInstruction {
1002 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001003 HTemplateInstruction<N>(SideEffects side_effects)
1004 : HInstruction(side_effects), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07001005 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001006
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001007 virtual size_t InputCount() const { return N; }
1008 virtual HInstruction* InputAt(size_t i) const { return inputs_[i]; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001009
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001010 protected:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001011 virtual void SetRawInputAt(size_t i, HInstruction* instruction) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001012 inputs_[i] = instruction;
1013 }
1014
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001015 private:
1016 EmbeddedArray<HInstruction*, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001017
1018 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001019};
1020
Dave Allison20dfc792014-06-16 20:44:29 -07001021template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001022class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07001023 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001024 HExpression<N>(Primitive::Type type, SideEffects side_effects)
1025 : HTemplateInstruction<N>(side_effects), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07001026 virtual ~HExpression() {}
1027
1028 virtual Primitive::Type GetType() const { return type_; }
1029
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001030 protected:
1031 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07001032};
1033
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001034// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
1035// instruction that branches to the exit block.
1036class HReturnVoid : public HTemplateInstruction<0> {
1037 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001038 HReturnVoid() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001039
1040 virtual bool IsControlFlow() const { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001041
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001042 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001043
1044 private:
1045 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
1046};
1047
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001048// Represents dex's RETURN opcodes. A HReturn is a control flow
1049// instruction that branches to the exit block.
1050class HReturn : public HTemplateInstruction<1> {
1051 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001052 explicit HReturn(HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001053 SetRawInputAt(0, value);
1054 }
1055
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001056 virtual bool IsControlFlow() const { return true; }
1057
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001058 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001059
1060 private:
1061 DISALLOW_COPY_AND_ASSIGN(HReturn);
1062};
1063
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001064// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001065// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001066// exit block.
1067class HExit : public HTemplateInstruction<0> {
1068 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001069 HExit() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001070
1071 virtual bool IsControlFlow() const { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001072
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001073 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001074
1075 private:
1076 DISALLOW_COPY_AND_ASSIGN(HExit);
1077};
1078
1079// Jumps from one block to another.
1080class HGoto : public HTemplateInstruction<0> {
1081 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001082 HGoto() : HTemplateInstruction(SideEffects::None()) {}
1083
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001084 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001085
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001086 HBasicBlock* GetSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001087 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001088 }
1089
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001090 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001091
1092 private:
1093 DISALLOW_COPY_AND_ASSIGN(HGoto);
1094};
1095
Dave Allison20dfc792014-06-16 20:44:29 -07001096
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001097// Conditional branch. A block ending with an HIf instruction must have
1098// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001099class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001100 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001101 explicit HIf(HInstruction* input) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001102 SetRawInputAt(0, input);
1103 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001104
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001105 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001106
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001107 HBasicBlock* IfTrueSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001108 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001109 }
1110
1111 HBasicBlock* IfFalseSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001112 return GetBlock()->GetSuccessors().Get(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001113 }
1114
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001115 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001116
Dave Allison20dfc792014-06-16 20:44:29 -07001117 virtual bool IsIfInstruction() const { return true; }
1118
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001119 private:
1120 DISALLOW_COPY_AND_ASSIGN(HIf);
1121};
1122
Roland Levillain88cb1752014-10-20 16:36:47 +01001123class HUnaryOperation : public HExpression<1> {
1124 public:
1125 HUnaryOperation(Primitive::Type result_type, HInstruction* input)
1126 : HExpression(result_type, SideEffects::None()) {
1127 SetRawInputAt(0, input);
1128 }
1129
1130 HInstruction* GetInput() const { return InputAt(0); }
1131 Primitive::Type GetResultType() const { return GetType(); }
1132
1133 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001134 virtual bool InstructionDataEquals(HInstruction* other) const {
1135 UNUSED(other);
1136 return true;
1137 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001138
Roland Levillain9240d6a2014-10-20 16:47:04 +01001139 // Try to statically evaluate `operation` and return a HConstant
1140 // containing the result of this evaluation. If `operation` cannot
1141 // be evaluated as a constant, return nullptr.
1142 HConstant* TryStaticEvaluation() const;
1143
1144 // Apply this operation to `x`.
1145 virtual int32_t Evaluate(int32_t x) const = 0;
1146 virtual int64_t Evaluate(int64_t x) const = 0;
1147
Roland Levillain88cb1752014-10-20 16:36:47 +01001148 DECLARE_INSTRUCTION(UnaryOperation);
1149
1150 private:
1151 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
1152};
1153
Dave Allison20dfc792014-06-16 20:44:29 -07001154class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001155 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001156 HBinaryOperation(Primitive::Type result_type,
1157 HInstruction* left,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001158 HInstruction* right) : HExpression(result_type, SideEffects::None()) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001159 SetRawInputAt(0, left);
1160 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001161 }
1162
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001163 HInstruction* GetLeft() const { return InputAt(0); }
1164 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07001165 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001166
1167 virtual bool IsCommutative() { return false; }
1168
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001169 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001170 virtual bool InstructionDataEquals(HInstruction* other) const {
1171 UNUSED(other);
1172 return true;
1173 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001174
Roland Levillain9240d6a2014-10-20 16:47:04 +01001175 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01001176 // containing the result of this evaluation. If `operation` cannot
1177 // be evaluated as a constant, return nullptr.
Roland Levillain9240d6a2014-10-20 16:47:04 +01001178 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01001179
1180 // Apply this operation to `x` and `y`.
1181 virtual int32_t Evaluate(int32_t x, int32_t y) const = 0;
1182 virtual int64_t Evaluate(int64_t x, int64_t y) const = 0;
1183
Roland Levillainccc07a92014-09-16 14:48:16 +01001184 DECLARE_INSTRUCTION(BinaryOperation);
1185
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001186 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001187 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
1188};
1189
Dave Allison20dfc792014-06-16 20:44:29 -07001190class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001191 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001192 HCondition(HInstruction* first, HInstruction* second)
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001193 : HBinaryOperation(Primitive::kPrimBoolean, first, second),
1194 needs_materialization_(true) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001195
1196 virtual bool IsCommutative() { return true; }
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001197
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001198 bool NeedsMaterialization() const { return needs_materialization_; }
1199 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001200
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001201 // For code generation purposes, returns whether this instruction is just before
1202 // `if_`, and disregard moves in between.
1203 bool IsBeforeWhenDisregardMoves(HIf* if_) const;
1204
Dave Allison20dfc792014-06-16 20:44:29 -07001205 DECLARE_INSTRUCTION(Condition);
1206
1207 virtual IfCondition GetCondition() const = 0;
1208
1209 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001210 // For register allocation purposes, returns whether this instruction needs to be
1211 // materialized (that is, not just be in the processor flags).
1212 bool needs_materialization_;
1213
Dave Allison20dfc792014-06-16 20:44:29 -07001214 DISALLOW_COPY_AND_ASSIGN(HCondition);
1215};
1216
1217// Instruction to check if two inputs are equal to each other.
1218class HEqual : public HCondition {
1219 public:
1220 HEqual(HInstruction* first, HInstruction* second)
1221 : HCondition(first, second) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001222
Roland Levillain93445682014-10-06 19:24:02 +01001223 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1224 return x == y ? 1 : 0;
1225 }
1226 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1227 return x == y ? 1 : 0;
1228 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001229
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001230 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001231
Dave Allison20dfc792014-06-16 20:44:29 -07001232 virtual IfCondition GetCondition() const {
1233 return kCondEQ;
1234 }
1235
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001236 private:
1237 DISALLOW_COPY_AND_ASSIGN(HEqual);
1238};
1239
Dave Allison20dfc792014-06-16 20:44:29 -07001240class HNotEqual : public HCondition {
1241 public:
1242 HNotEqual(HInstruction* first, HInstruction* second)
1243 : HCondition(first, second) {}
1244
Roland Levillain93445682014-10-06 19:24:02 +01001245 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1246 return x != y ? 1 : 0;
1247 }
1248 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1249 return x != y ? 1 : 0;
1250 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001251
Dave Allison20dfc792014-06-16 20:44:29 -07001252 DECLARE_INSTRUCTION(NotEqual);
1253
1254 virtual IfCondition GetCondition() const {
1255 return kCondNE;
1256 }
1257
1258 private:
1259 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
1260};
1261
1262class HLessThan : public HCondition {
1263 public:
1264 HLessThan(HInstruction* first, HInstruction* second)
1265 : HCondition(first, second) {}
1266
Roland Levillain93445682014-10-06 19:24:02 +01001267 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1268 return x < y ? 1 : 0;
1269 }
1270 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1271 return x < y ? 1 : 0;
1272 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001273
Dave Allison20dfc792014-06-16 20:44:29 -07001274 DECLARE_INSTRUCTION(LessThan);
1275
1276 virtual IfCondition GetCondition() const {
1277 return kCondLT;
1278 }
1279
1280 private:
1281 DISALLOW_COPY_AND_ASSIGN(HLessThan);
1282};
1283
1284class HLessThanOrEqual : public HCondition {
1285 public:
1286 HLessThanOrEqual(HInstruction* first, HInstruction* second)
1287 : HCondition(first, second) {}
1288
Roland Levillain93445682014-10-06 19:24:02 +01001289 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1290 return x <= y ? 1 : 0;
1291 }
1292 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1293 return x <= y ? 1 : 0;
1294 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001295
Dave Allison20dfc792014-06-16 20:44:29 -07001296 DECLARE_INSTRUCTION(LessThanOrEqual);
1297
1298 virtual IfCondition GetCondition() const {
1299 return kCondLE;
1300 }
1301
1302 private:
1303 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
1304};
1305
1306class HGreaterThan : public HCondition {
1307 public:
1308 HGreaterThan(HInstruction* first, HInstruction* second)
1309 : HCondition(first, second) {}
1310
Roland Levillain93445682014-10-06 19:24:02 +01001311 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1312 return x > y ? 1 : 0;
1313 }
1314 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1315 return x > y ? 1 : 0;
1316 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001317
Dave Allison20dfc792014-06-16 20:44:29 -07001318 DECLARE_INSTRUCTION(GreaterThan);
1319
1320 virtual IfCondition GetCondition() const {
1321 return kCondGT;
1322 }
1323
1324 private:
1325 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
1326};
1327
1328class HGreaterThanOrEqual : public HCondition {
1329 public:
1330 HGreaterThanOrEqual(HInstruction* first, HInstruction* second)
1331 : HCondition(first, second) {}
1332
Roland Levillain93445682014-10-06 19:24:02 +01001333 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1334 return x >= y ? 1 : 0;
1335 }
1336 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1337 return x >= y ? 1 : 0;
1338 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001339
Dave Allison20dfc792014-06-16 20:44:29 -07001340 DECLARE_INSTRUCTION(GreaterThanOrEqual);
1341
1342 virtual IfCondition GetCondition() const {
1343 return kCondGE;
1344 }
1345
1346 private:
1347 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
1348};
1349
1350
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001351// Instruction to check how two inputs compare to each other.
1352// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
1353class HCompare : public HBinaryOperation {
1354 public:
1355 HCompare(Primitive::Type type, HInstruction* first, HInstruction* second)
1356 : HBinaryOperation(Primitive::kPrimInt, first, second) {
1357 DCHECK_EQ(type, first->GetType());
1358 DCHECK_EQ(type, second->GetType());
1359 }
1360
Roland Levillain93445682014-10-06 19:24:02 +01001361 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001362 return
1363 x == y ? 0 :
1364 x > y ? 1 :
1365 -1;
1366 }
Roland Levillain93445682014-10-06 19:24:02 +01001367 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001368 return
1369 x == y ? 0 :
1370 x > y ? 1 :
1371 -1;
1372 }
1373
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001374 DECLARE_INSTRUCTION(Compare);
1375
1376 private:
1377 DISALLOW_COPY_AND_ASSIGN(HCompare);
1378};
1379
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001380// A local in the graph. Corresponds to a Dex register.
1381class HLocal : public HTemplateInstruction<0> {
1382 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001383 explicit HLocal(uint16_t reg_number)
1384 : HTemplateInstruction(SideEffects::None()), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001385
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001386 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001387
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001388 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001389
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001390 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001391 // The Dex register number.
1392 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001393
1394 DISALLOW_COPY_AND_ASSIGN(HLocal);
1395};
1396
1397// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07001398class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001399 public:
Roland Levillain5799fc02014-09-25 12:15:20 +01001400 HLoadLocal(HLocal* local, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001401 : HExpression(type, SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001402 SetRawInputAt(0, local);
1403 }
1404
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001405 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1406
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001407 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001408
1409 private:
1410 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
1411};
1412
1413// Store a value in a given local. This instruction has two inputs: the value
1414// and the local.
1415class HStoreLocal : public HTemplateInstruction<2> {
1416 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001417 HStoreLocal(HLocal* local, HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001418 SetRawInputAt(0, local);
1419 SetRawInputAt(1, value);
1420 }
1421
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001422 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1423
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001424 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001425
1426 private:
1427 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
1428};
1429
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001430class HConstant : public HExpression<0> {
1431 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001432 explicit HConstant(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
1433
1434 virtual bool CanBeMoved() const { return true; }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001435
1436 DECLARE_INSTRUCTION(Constant);
1437
1438 private:
1439 DISALLOW_COPY_AND_ASSIGN(HConstant);
1440};
1441
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001442class HFloatConstant : public HConstant {
1443 public:
1444 explicit HFloatConstant(float value) : HConstant(Primitive::kPrimFloat), value_(value) {}
1445
1446 float GetValue() const { return value_; }
1447
1448 virtual bool InstructionDataEquals(HInstruction* other) const {
1449 return bit_cast<float, int32_t>(other->AsFloatConstant()->value_) ==
1450 bit_cast<float, int32_t>(value_);
1451 }
1452
1453 virtual size_t ComputeHashCode() const { return static_cast<size_t>(GetValue()); }
1454
1455 DECLARE_INSTRUCTION(FloatConstant);
1456
1457 private:
1458 const float value_;
1459
1460 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
1461};
1462
1463class HDoubleConstant : public HConstant {
1464 public:
1465 explicit HDoubleConstant(double value) : HConstant(Primitive::kPrimDouble), value_(value) {}
1466
1467 double GetValue() const { return value_; }
1468
1469 virtual bool InstructionDataEquals(HInstruction* other) const {
1470 return bit_cast<double, int64_t>(other->AsDoubleConstant()->value_) ==
1471 bit_cast<double, int64_t>(value_);
1472 }
1473
1474 virtual size_t ComputeHashCode() const { return static_cast<size_t>(GetValue()); }
1475
1476 DECLARE_INSTRUCTION(DoubleConstant);
1477
1478 private:
1479 const double value_;
1480
1481 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
1482};
1483
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001484// Constants of the type int. Those can be from Dex instructions, or
1485// synthesized (for example with the if-eqz instruction).
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001486class HIntConstant : public HConstant {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001487 public:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001488 explicit HIntConstant(int32_t value) : HConstant(Primitive::kPrimInt), value_(value) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001489
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001490 int32_t GetValue() const { return value_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001491
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001492 virtual bool InstructionDataEquals(HInstruction* other) const {
1493 return other->AsIntConstant()->value_ == value_;
1494 }
1495
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001496 virtual size_t ComputeHashCode() const { return GetValue(); }
1497
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001498 DECLARE_INSTRUCTION(IntConstant);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001499
1500 private:
1501 const int32_t value_;
1502
1503 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
1504};
1505
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001506class HLongConstant : public HConstant {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001507 public:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001508 explicit HLongConstant(int64_t value) : HConstant(Primitive::kPrimLong), value_(value) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001509
1510 int64_t GetValue() const { return value_; }
1511
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001512 virtual bool InstructionDataEquals(HInstruction* other) const {
1513 return other->AsLongConstant()->value_ == value_;
1514 }
1515
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001516 virtual size_t ComputeHashCode() const { return static_cast<size_t>(GetValue()); }
1517
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001518 DECLARE_INSTRUCTION(LongConstant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001519
1520 private:
1521 const int64_t value_;
1522
1523 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
1524};
1525
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001526class HInvoke : public HInstruction {
1527 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001528 HInvoke(ArenaAllocator* arena,
1529 uint32_t number_of_arguments,
1530 Primitive::Type return_type,
1531 uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001532 : HInstruction(SideEffects::All()),
1533 inputs_(arena, number_of_arguments),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001534 return_type_(return_type),
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001535 dex_pc_(dex_pc) {
1536 inputs_.SetSize(number_of_arguments);
1537 }
1538
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001539 virtual size_t InputCount() const { return inputs_.Size(); }
1540 virtual HInstruction* InputAt(size_t i) const { return inputs_.Get(i); }
1541
1542 // Runtime needs to walk the stack, so Dex -> Dex calls need to
1543 // know their environment.
1544 virtual bool NeedsEnvironment() const { return true; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001545
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001546 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001547 SetRawInputAt(index, argument);
1548 }
1549
1550 virtual void SetRawInputAt(size_t index, HInstruction* input) {
1551 inputs_.Put(index, input);
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001552 }
1553
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001554 virtual Primitive::Type GetType() const { return return_type_; }
1555
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001556 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001557
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001558 DECLARE_INSTRUCTION(Invoke);
1559
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001560 protected:
1561 GrowableArray<HInstruction*> inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001562 const Primitive::Type return_type_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001563 const uint32_t dex_pc_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001564
1565 private:
1566 DISALLOW_COPY_AND_ASSIGN(HInvoke);
1567};
1568
1569class HInvokeStatic : public HInvoke {
1570 public:
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001571 HInvokeStatic(ArenaAllocator* arena,
1572 uint32_t number_of_arguments,
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001573 Primitive::Type return_type,
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001574 uint32_t dex_pc,
1575 uint32_t index_in_dex_cache)
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001576 : HInvoke(arena, number_of_arguments, return_type, dex_pc),
1577 index_in_dex_cache_(index_in_dex_cache) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001578
1579 uint32_t GetIndexInDexCache() const { return index_in_dex_cache_; }
1580
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001581 DECLARE_INSTRUCTION(InvokeStatic);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001582
1583 private:
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001584 const uint32_t index_in_dex_cache_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001585
1586 DISALLOW_COPY_AND_ASSIGN(HInvokeStatic);
1587};
1588
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001589class HInvokeVirtual : public HInvoke {
1590 public:
1591 HInvokeVirtual(ArenaAllocator* arena,
1592 uint32_t number_of_arguments,
1593 Primitive::Type return_type,
1594 uint32_t dex_pc,
1595 uint32_t vtable_index)
1596 : HInvoke(arena, number_of_arguments, return_type, dex_pc),
1597 vtable_index_(vtable_index) {}
1598
1599 uint32_t GetVTableIndex() const { return vtable_index_; }
1600
1601 DECLARE_INSTRUCTION(InvokeVirtual);
1602
1603 private:
1604 const uint32_t vtable_index_;
1605
1606 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
1607};
1608
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001609class HInvokeInterface : public HInvoke {
1610 public:
1611 HInvokeInterface(ArenaAllocator* arena,
1612 uint32_t number_of_arguments,
1613 Primitive::Type return_type,
1614 uint32_t dex_pc,
1615 uint32_t dex_method_index,
1616 uint32_t imt_index)
1617 : HInvoke(arena, number_of_arguments, return_type, dex_pc),
1618 dex_method_index_(dex_method_index),
1619 imt_index_(imt_index) {}
1620
1621 uint32_t GetImtIndex() const { return imt_index_; }
1622 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
1623
1624 DECLARE_INSTRUCTION(InvokeInterface);
1625
1626 private:
1627 const uint32_t dex_method_index_;
1628 const uint32_t imt_index_;
1629
1630 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
1631};
1632
Dave Allison20dfc792014-06-16 20:44:29 -07001633class HNewInstance : public HExpression<0> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001634 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001635 HNewInstance(uint32_t dex_pc, uint16_t type_index)
1636 : HExpression(Primitive::kPrimNot, SideEffects::None()),
1637 dex_pc_(dex_pc),
1638 type_index_(type_index) {}
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001639
1640 uint32_t GetDexPc() const { return dex_pc_; }
1641 uint16_t GetTypeIndex() const { return type_index_; }
1642
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001643 // Calls runtime so needs an environment.
1644 virtual bool NeedsEnvironment() const { return true; }
1645
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001646 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001647
1648 private:
1649 const uint32_t dex_pc_;
1650 const uint16_t type_index_;
1651
1652 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
1653};
1654
Roland Levillain88cb1752014-10-20 16:36:47 +01001655class HNeg : public HUnaryOperation {
1656 public:
1657 explicit HNeg(Primitive::Type result_type, HInstruction* input)
1658 : HUnaryOperation(result_type, input) {}
1659
Roland Levillain9240d6a2014-10-20 16:47:04 +01001660 virtual int32_t Evaluate(int32_t x) const OVERRIDE { return -x; }
1661 virtual int64_t Evaluate(int64_t x) const OVERRIDE { return -x; }
1662
Roland Levillain88cb1752014-10-20 16:36:47 +01001663 DECLARE_INSTRUCTION(Neg);
1664
1665 private:
1666 DISALLOW_COPY_AND_ASSIGN(HNeg);
1667};
1668
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001669class HNewArray : public HExpression<1> {
1670 public:
1671 HNewArray(HInstruction* length, uint32_t dex_pc, uint16_t type_index)
1672 : HExpression(Primitive::kPrimNot, SideEffects::None()),
1673 dex_pc_(dex_pc),
1674 type_index_(type_index) {
1675 SetRawInputAt(0, length);
1676 }
1677
1678 uint32_t GetDexPc() const { return dex_pc_; }
1679 uint16_t GetTypeIndex() const { return type_index_; }
1680
1681 // Calls runtime so needs an environment.
1682 virtual bool NeedsEnvironment() const { return true; }
1683
1684 DECLARE_INSTRUCTION(NewArray);
1685
1686 private:
1687 const uint32_t dex_pc_;
1688 const uint16_t type_index_;
1689
1690 DISALLOW_COPY_AND_ASSIGN(HNewArray);
1691};
1692
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001693class HAdd : public HBinaryOperation {
1694 public:
1695 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1696 : HBinaryOperation(result_type, left, right) {}
1697
1698 virtual bool IsCommutative() { return true; }
1699
Roland Levillain93445682014-10-06 19:24:02 +01001700 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1701 return x + y;
1702 }
1703 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1704 return x + y;
1705 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001706
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001707 DECLARE_INSTRUCTION(Add);
1708
1709 private:
1710 DISALLOW_COPY_AND_ASSIGN(HAdd);
1711};
1712
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001713class HSub : public HBinaryOperation {
1714 public:
1715 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1716 : HBinaryOperation(result_type, left, right) {}
1717
Roland Levillain93445682014-10-06 19:24:02 +01001718 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1719 return x - y;
1720 }
1721 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1722 return x - y;
1723 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001724
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001725 DECLARE_INSTRUCTION(Sub);
1726
1727 private:
1728 DISALLOW_COPY_AND_ASSIGN(HSub);
1729};
1730
Calin Juravle34bacdf2014-10-07 20:23:36 +01001731class HMul : public HBinaryOperation {
1732 public:
1733 HMul(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1734 : HBinaryOperation(result_type, left, right) {}
1735
1736 virtual bool IsCommutative() { return true; }
1737
1738 virtual int32_t Evaluate(int32_t x, int32_t y) const { return x * y; }
1739 virtual int64_t Evaluate(int64_t x, int64_t y) const { return x * y; }
1740
1741 DECLARE_INSTRUCTION(Mul);
1742
1743 private:
1744 DISALLOW_COPY_AND_ASSIGN(HMul);
1745};
1746
Calin Juravle7c4954d2014-10-28 16:57:40 +00001747class HDiv : public HBinaryOperation {
1748 public:
1749 HDiv(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1750 : HBinaryOperation(result_type, left, right) {}
1751
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00001752 virtual int32_t Evaluate(int32_t x, int32_t y) const {
1753 // Our graph structure ensures we never have 0 for `y` during constant folding.
1754 DCHECK_NE(y, 0);
1755 // Special case -1 to avoid getting a SIGFPE on x86.
1756 return (y == -1) ? -x : x / y;
1757 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00001758 virtual int64_t Evaluate(int64_t x, int64_t y) const { return x / y; }
1759
1760 DECLARE_INSTRUCTION(Div);
1761
1762 private:
1763 DISALLOW_COPY_AND_ASSIGN(HDiv);
1764};
1765
Calin Juravled0d48522014-11-04 16:40:20 +00001766class HDivZeroCheck : public HExpression<1> {
1767 public:
1768 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
1769 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
1770 SetRawInputAt(0, value);
1771 }
1772
1773 bool CanBeMoved() const OVERRIDE { return true; }
1774
1775 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
1776 UNUSED(other);
1777 return true;
1778 }
1779
1780 bool NeedsEnvironment() const OVERRIDE { return true; }
1781 bool CanThrow() const OVERRIDE { return true; }
1782
1783 uint32_t GetDexPc() const { return dex_pc_; }
1784
1785 DECLARE_INSTRUCTION(DivZeroCheck);
1786
1787 private:
1788 const uint32_t dex_pc_;
1789
1790 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
1791};
1792
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001793// The value of a parameter in this method. Its location depends on
1794// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07001795class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001796 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001797 HParameterValue(uint8_t index, Primitive::Type parameter_type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001798 : HExpression(parameter_type, SideEffects::None()), index_(index) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001799
1800 uint8_t GetIndex() const { return index_; }
1801
1802 DECLARE_INSTRUCTION(ParameterValue);
1803
1804 private:
1805 // The index of this parameter in the parameters list. Must be less
1806 // than HGraph::number_of_in_vregs_;
1807 const uint8_t index_;
1808
1809 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
1810};
1811
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001812class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001813 public:
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001814 explicit HNot(Primitive::Type result_type, HInstruction* input)
1815 : HUnaryOperation(result_type, input) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001816
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001817 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001818 virtual bool InstructionDataEquals(HInstruction* other) const {
1819 UNUSED(other);
1820 return true;
1821 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001822
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001823 virtual int32_t Evaluate(int32_t x) const OVERRIDE { return ~x; }
1824 virtual int64_t Evaluate(int64_t x) const OVERRIDE { return ~x; }
1825
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001826 DECLARE_INSTRUCTION(Not);
1827
1828 private:
1829 DISALLOW_COPY_AND_ASSIGN(HNot);
1830};
1831
Roland Levillaindff1f282014-11-05 14:15:05 +00001832class HTypeConversion : public HExpression<1> {
1833 public:
1834 // Instantiate a type conversion of `input` to `result_type`.
1835 HTypeConversion(Primitive::Type result_type, HInstruction* input)
1836 : HExpression(result_type, SideEffects::None()) {
1837 SetRawInputAt(0, input);
1838 DCHECK_NE(input->GetType(), result_type);
1839 }
1840
1841 HInstruction* GetInput() const { return InputAt(0); }
1842 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
1843 Primitive::Type GetResultType() const { return GetType(); }
1844
1845 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00001846 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00001847
1848 DECLARE_INSTRUCTION(TypeConversion);
1849
1850 private:
1851 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
1852};
1853
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001854class HPhi : public HInstruction {
1855 public:
1856 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001857 : HInstruction(SideEffects::None()),
1858 inputs_(arena, number_of_inputs),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001859 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001860 type_(type),
1861 is_live_(false) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001862 inputs_.SetSize(number_of_inputs);
1863 }
1864
1865 virtual size_t InputCount() const { return inputs_.Size(); }
1866 virtual HInstruction* InputAt(size_t i) const { return inputs_.Get(i); }
1867
1868 virtual void SetRawInputAt(size_t index, HInstruction* input) {
1869 inputs_.Put(index, input);
1870 }
1871
1872 void AddInput(HInstruction* input);
1873
1874 virtual Primitive::Type GetType() const { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001875 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001876
1877 uint32_t GetRegNumber() const { return reg_number_; }
1878
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001879 void SetDead() { is_live_ = false; }
1880 void SetLive() { is_live_ = true; }
1881 bool IsDead() const { return !is_live_; }
1882 bool IsLive() const { return is_live_; }
1883
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001884 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001885
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001886 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001887 GrowableArray<HInstruction*> inputs_;
1888 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001889 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001890 bool is_live_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001891
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001892 DISALLOW_COPY_AND_ASSIGN(HPhi);
1893};
1894
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001895class HNullCheck : public HExpression<1> {
1896 public:
1897 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001898 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001899 SetRawInputAt(0, value);
1900 }
1901
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001902 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001903 virtual bool InstructionDataEquals(HInstruction* other) const {
1904 UNUSED(other);
1905 return true;
1906 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001907
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001908 virtual bool NeedsEnvironment() const { return true; }
1909
Roland Levillaine161a2a2014-10-03 12:45:18 +01001910 virtual bool CanThrow() const { return true; }
1911
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001912 uint32_t GetDexPc() const { return dex_pc_; }
1913
1914 DECLARE_INSTRUCTION(NullCheck);
1915
1916 private:
1917 const uint32_t dex_pc_;
1918
1919 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
1920};
1921
1922class FieldInfo : public ValueObject {
1923 public:
Roland Levillain5799fc02014-09-25 12:15:20 +01001924 FieldInfo(MemberOffset field_offset, Primitive::Type field_type)
Nicolas Geoffray39468442014-09-02 15:17:15 +01001925 : field_offset_(field_offset), field_type_(field_type) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001926
1927 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001928 Primitive::Type GetFieldType() const { return field_type_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001929
1930 private:
1931 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01001932 const Primitive::Type field_type_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001933};
1934
1935class HInstanceFieldGet : public HExpression<1> {
1936 public:
1937 HInstanceFieldGet(HInstruction* value,
1938 Primitive::Type field_type,
1939 MemberOffset field_offset)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001940 : HExpression(field_type, SideEffects::DependsOnSomething()),
1941 field_info_(field_offset, field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001942 SetRawInputAt(0, value);
1943 }
1944
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001945 virtual bool CanBeMoved() const { return true; }
1946 virtual bool InstructionDataEquals(HInstruction* other) const {
1947 size_t other_offset = other->AsInstanceFieldGet()->GetFieldOffset().SizeValue();
1948 return other_offset == GetFieldOffset().SizeValue();
1949 }
1950
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001951 virtual size_t ComputeHashCode() const {
1952 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
1953 }
1954
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001955 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001956 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001957
1958 DECLARE_INSTRUCTION(InstanceFieldGet);
1959
1960 private:
1961 const FieldInfo field_info_;
1962
1963 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
1964};
1965
1966class HInstanceFieldSet : public HTemplateInstruction<2> {
1967 public:
1968 HInstanceFieldSet(HInstruction* object,
1969 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01001970 Primitive::Type field_type,
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001971 MemberOffset field_offset)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001972 : HTemplateInstruction(SideEffects::ChangesSomething()),
1973 field_info_(field_offset, field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001974 SetRawInputAt(0, object);
1975 SetRawInputAt(1, value);
1976 }
1977
1978 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001979 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001980
1981 DECLARE_INSTRUCTION(InstanceFieldSet);
1982
1983 private:
1984 const FieldInfo field_info_;
1985
1986 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
1987};
1988
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001989class HArrayGet : public HExpression<2> {
1990 public:
1991 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001992 : HExpression(type, SideEffects::DependsOnSomething()) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001993 SetRawInputAt(0, array);
1994 SetRawInputAt(1, index);
1995 }
1996
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001997 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001998 virtual bool InstructionDataEquals(HInstruction* other) const {
1999 UNUSED(other);
2000 return true;
2001 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002002 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002003
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002004 DECLARE_INSTRUCTION(ArrayGet);
2005
2006 private:
2007 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
2008};
2009
2010class HArraySet : public HTemplateInstruction<3> {
2011 public:
2012 HArraySet(HInstruction* array,
2013 HInstruction* index,
2014 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002015 Primitive::Type expected_component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002016 uint32_t dex_pc)
2017 : HTemplateInstruction(SideEffects::ChangesSomething()),
2018 dex_pc_(dex_pc),
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002019 expected_component_type_(expected_component_type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002020 SetRawInputAt(0, array);
2021 SetRawInputAt(1, index);
2022 SetRawInputAt(2, value);
2023 }
2024
2025 virtual bool NeedsEnvironment() const {
2026 // We currently always call a runtime method to catch array store
2027 // exceptions.
2028 return InputAt(2)->GetType() == Primitive::kPrimNot;
2029 }
2030
2031 uint32_t GetDexPc() const { return dex_pc_; }
2032
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002033 HInstruction* GetValue() const { return InputAt(2); }
2034
2035 Primitive::Type GetComponentType() const {
2036 // The Dex format does not type floating point index operations. Since the
2037 // `expected_component_type_` is set during building and can therefore not
2038 // be correct, we also check what is the value type. If it is a floating
2039 // point type, we must use that type.
2040 Primitive::Type value_type = GetValue()->GetType();
2041 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
2042 ? value_type
2043 : expected_component_type_;
2044 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002045
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002046 DECLARE_INSTRUCTION(ArraySet);
2047
2048 private:
2049 const uint32_t dex_pc_;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002050 const Primitive::Type expected_component_type_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002051
2052 DISALLOW_COPY_AND_ASSIGN(HArraySet);
2053};
2054
2055class HArrayLength : public HExpression<1> {
2056 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002057 explicit HArrayLength(HInstruction* array)
2058 : HExpression(Primitive::kPrimInt, SideEffects::None()) {
2059 // Note that arrays do not change length, so the instruction does not
2060 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002061 SetRawInputAt(0, array);
2062 }
2063
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002064 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002065 virtual bool InstructionDataEquals(HInstruction* other) const {
2066 UNUSED(other);
2067 return true;
2068 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002069
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002070 DECLARE_INSTRUCTION(ArrayLength);
2071
2072 private:
2073 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
2074};
2075
2076class HBoundsCheck : public HExpression<2> {
2077 public:
2078 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002079 : HExpression(index->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002080 DCHECK(index->GetType() == Primitive::kPrimInt);
2081 SetRawInputAt(0, index);
2082 SetRawInputAt(1, length);
2083 }
2084
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002085 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002086 virtual bool InstructionDataEquals(HInstruction* other) const {
2087 UNUSED(other);
2088 return true;
2089 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002090
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002091 virtual bool NeedsEnvironment() const { return true; }
2092
Roland Levillaine161a2a2014-10-03 12:45:18 +01002093 virtual bool CanThrow() const { return true; }
2094
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002095 uint32_t GetDexPc() const { return dex_pc_; }
2096
2097 DECLARE_INSTRUCTION(BoundsCheck);
2098
2099 private:
2100 const uint32_t dex_pc_;
2101
2102 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
2103};
2104
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002105/**
2106 * Some DEX instructions are folded into multiple HInstructions that need
2107 * to stay live until the last HInstruction. This class
2108 * is used as a marker for the baseline compiler to ensure its preceding
2109 * HInstruction stays live. `index` is the temporary number that is used
2110 * for knowing the stack offset where to store the instruction.
2111 */
2112class HTemporary : public HTemplateInstruction<0> {
2113 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002114 explicit HTemporary(size_t index) : HTemplateInstruction(SideEffects::None()), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002115
2116 size_t GetIndex() const { return index_; }
2117
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00002118 Primitive::Type GetType() const OVERRIDE {
2119 // The previous instruction is the one that will be stored in the temporary location.
2120 DCHECK(GetPrevious() != nullptr);
2121 return GetPrevious()->GetType();
2122 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00002123
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002124 DECLARE_INSTRUCTION(Temporary);
2125
2126 private:
2127 const size_t index_;
2128
2129 DISALLOW_COPY_AND_ASSIGN(HTemporary);
2130};
2131
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002132class HSuspendCheck : public HTemplateInstruction<0> {
2133 public:
2134 explicit HSuspendCheck(uint32_t dex_pc)
Nicolas Geoffray9ebc72c2014-09-25 16:33:42 +01002135 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002136
2137 virtual bool NeedsEnvironment() const {
2138 return true;
2139 }
2140
2141 uint32_t GetDexPc() const { return dex_pc_; }
2142
2143 DECLARE_INSTRUCTION(SuspendCheck);
2144
2145 private:
2146 const uint32_t dex_pc_;
2147
2148 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
2149};
2150
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002151/**
2152 * Instruction to load a Class object.
2153 */
2154class HLoadClass : public HExpression<0> {
2155 public:
2156 HLoadClass(uint16_t type_index,
2157 bool is_referrers_class,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002158 uint32_t dex_pc)
2159 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2160 type_index_(type_index),
2161 is_referrers_class_(is_referrers_class),
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002162 dex_pc_(dex_pc),
2163 generate_clinit_check_(false) {}
2164
2165 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002166
2167 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2168 return other->AsLoadClass()->type_index_ == type_index_;
2169 }
2170
2171 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
2172
2173 uint32_t GetDexPc() const { return dex_pc_; }
2174 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002175 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002176
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002177 bool NeedsEnvironment() const OVERRIDE {
2178 // Will call runtime and load the class if the class is not loaded yet.
2179 // TODO: finer grain decision.
2180 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002181 }
2182
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002183 bool MustGenerateClinitCheck() const {
2184 return generate_clinit_check_;
2185 }
2186
2187 void SetMustGenerateClinitCheck() {
2188 generate_clinit_check_ = true;
2189 }
2190
2191 bool CanCallRuntime() const {
2192 return MustGenerateClinitCheck() || !is_referrers_class_;
2193 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002194
2195 DECLARE_INSTRUCTION(LoadClass);
2196
2197 private:
2198 const uint16_t type_index_;
2199 const bool is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002200 const uint32_t dex_pc_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002201 // Whether this instruction must generate the initialization check.
2202 // Used for code generation.
2203 bool generate_clinit_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002204
2205 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
2206};
2207
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002208class HLoadString : public HExpression<0> {
2209 public:
2210 HLoadString(uint32_t string_index, uint32_t dex_pc)
2211 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2212 string_index_(string_index),
2213 dex_pc_(dex_pc) {}
2214
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002215 bool CanBeMoved() const OVERRIDE { return true; }
2216
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002217 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2218 return other->AsLoadString()->string_index_ == string_index_;
2219 }
2220
2221 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
2222
2223 uint32_t GetDexPc() const { return dex_pc_; }
2224 uint32_t GetStringIndex() const { return string_index_; }
2225
2226 // TODO: Can we deopt or debug when we resolve a string?
2227 bool NeedsEnvironment() const OVERRIDE { return false; }
2228
2229 DECLARE_INSTRUCTION(LoadString);
2230
2231 private:
2232 const uint32_t string_index_;
2233 const uint32_t dex_pc_;
2234
2235 DISALLOW_COPY_AND_ASSIGN(HLoadString);
2236};
2237
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002238// TODO: Pass this check to HInvokeStatic nodes.
2239/**
2240 * Performs an initialization check on its Class object input.
2241 */
2242class HClinitCheck : public HExpression<1> {
2243 public:
2244 explicit HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
2245 : HExpression(Primitive::kPrimNot, SideEffects::All()),
2246 dex_pc_(dex_pc) {
2247 SetRawInputAt(0, constant);
2248 }
2249
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002250 bool CanBeMoved() const OVERRIDE { return true; }
2251 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2252 UNUSED(other);
2253 return true;
2254 }
2255
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002256 bool NeedsEnvironment() const OVERRIDE {
2257 // May call runtime to initialize the class.
2258 return true;
2259 }
2260
2261 uint32_t GetDexPc() const { return dex_pc_; }
2262
2263 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
2264
2265 DECLARE_INSTRUCTION(ClinitCheck);
2266
2267 private:
2268 const uint32_t dex_pc_;
2269
2270 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
2271};
2272
2273class HStaticFieldGet : public HExpression<1> {
2274 public:
2275 HStaticFieldGet(HInstruction* cls,
2276 Primitive::Type field_type,
2277 MemberOffset field_offset)
2278 : HExpression(field_type, SideEffects::DependsOnSomething()),
2279 field_info_(field_offset, field_type) {
2280 SetRawInputAt(0, cls);
2281 }
2282
2283 bool CanBeMoved() const OVERRIDE { return true; }
2284 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2285 size_t other_offset = other->AsStaticFieldGet()->GetFieldOffset().SizeValue();
2286 return other_offset == GetFieldOffset().SizeValue();
2287 }
2288
2289 size_t ComputeHashCode() const OVERRIDE {
2290 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
2291 }
2292
2293 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
2294 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
2295
2296 DECLARE_INSTRUCTION(StaticFieldGet);
2297
2298 private:
2299 const FieldInfo field_info_;
2300
2301 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
2302};
2303
2304class HStaticFieldSet : public HTemplateInstruction<2> {
2305 public:
2306 HStaticFieldSet(HInstruction* cls,
2307 HInstruction* value,
2308 Primitive::Type field_type,
2309 MemberOffset field_offset)
2310 : HTemplateInstruction(SideEffects::ChangesSomething()),
2311 field_info_(field_offset, field_type) {
2312 SetRawInputAt(0, cls);
2313 SetRawInputAt(1, value);
2314 }
2315
2316 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
2317 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
2318
2319 DECLARE_INSTRUCTION(StaticFieldSet);
2320
2321 private:
2322 const FieldInfo field_info_;
2323
2324 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
2325};
2326
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002327// Implement the move-exception DEX instruction.
2328class HLoadException : public HExpression<0> {
2329 public:
2330 HLoadException() : HExpression(Primitive::kPrimNot, SideEffects::None()) {}
2331
2332 DECLARE_INSTRUCTION(LoadException);
2333
2334 private:
2335 DISALLOW_COPY_AND_ASSIGN(HLoadException);
2336};
2337
2338class HThrow : public HTemplateInstruction<1> {
2339 public:
2340 HThrow(HInstruction* exception, uint32_t dex_pc)
2341 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {
2342 SetRawInputAt(0, exception);
2343 }
2344
2345 bool IsControlFlow() const OVERRIDE { return true; }
2346
2347 bool NeedsEnvironment() const OVERRIDE { return true; }
2348
2349 uint32_t GetDexPc() const { return dex_pc_; }
2350
2351 DECLARE_INSTRUCTION(Throw);
2352
2353 private:
2354 uint32_t dex_pc_;
2355
2356 DISALLOW_COPY_AND_ASSIGN(HThrow);
2357};
2358
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002359class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002360 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002361 HInstanceOf(HInstruction* object,
2362 HLoadClass* constant,
2363 bool class_is_final,
2364 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002365 : HExpression(Primitive::kPrimBoolean, SideEffects::None()),
2366 class_is_final_(class_is_final),
2367 dex_pc_(dex_pc) {
2368 SetRawInputAt(0, object);
2369 SetRawInputAt(1, constant);
2370 }
2371
2372 bool CanBeMoved() const OVERRIDE { return true; }
2373
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002374 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002375 return true;
2376 }
2377
2378 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002379 return false;
2380 }
2381
2382 uint32_t GetDexPc() const { return dex_pc_; }
2383
2384 bool IsClassFinal() const { return class_is_final_; }
2385
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002386 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002387
2388 private:
2389 const bool class_is_final_;
2390 const uint32_t dex_pc_;
2391
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002392 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
2393};
2394
2395class HCheckCast : public HTemplateInstruction<2> {
2396 public:
2397 HCheckCast(HInstruction* object,
2398 HLoadClass* constant,
2399 bool class_is_final,
2400 uint32_t dex_pc)
2401 : HTemplateInstruction(SideEffects::None()),
2402 class_is_final_(class_is_final),
2403 dex_pc_(dex_pc) {
2404 SetRawInputAt(0, object);
2405 SetRawInputAt(1, constant);
2406 }
2407
2408 bool CanBeMoved() const OVERRIDE { return true; }
2409
2410 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2411 return true;
2412 }
2413
2414 bool NeedsEnvironment() const OVERRIDE {
2415 // Instruction may throw a CheckCastError.
2416 return true;
2417 }
2418
2419 bool CanThrow() const OVERRIDE { return true; }
2420
2421 uint32_t GetDexPc() const { return dex_pc_; }
2422
2423 bool IsClassFinal() const { return class_is_final_; }
2424
2425 DECLARE_INSTRUCTION(CheckCast);
2426
2427 private:
2428 const bool class_is_final_;
2429 const uint32_t dex_pc_;
2430
2431 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002432};
2433
2434
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002435class MoveOperands : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002436 public:
Nicolas Geoffray740475d2014-09-29 10:33:25 +01002437 MoveOperands(Location source, Location destination, HInstruction* instruction)
2438 : source_(source), destination_(destination), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002439
2440 Location GetSource() const { return source_; }
2441 Location GetDestination() const { return destination_; }
2442
2443 void SetSource(Location value) { source_ = value; }
2444 void SetDestination(Location value) { destination_ = value; }
2445
2446 // The parallel move resolver marks moves as "in-progress" by clearing the
2447 // destination (but not the source).
2448 Location MarkPending() {
2449 DCHECK(!IsPending());
2450 Location dest = destination_;
2451 destination_ = Location::NoLocation();
2452 return dest;
2453 }
2454
2455 void ClearPending(Location dest) {
2456 DCHECK(IsPending());
2457 destination_ = dest;
2458 }
2459
2460 bool IsPending() const {
2461 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
2462 return destination_.IsInvalid() && !source_.IsInvalid();
2463 }
2464
2465 // True if this blocks a move from the given location.
2466 bool Blocks(Location loc) const {
2467 return !IsEliminated() && source_.Equals(loc);
2468 }
2469
2470 // A move is redundant if it's been eliminated, if its source and
2471 // destination are the same, or if its destination is unneeded.
2472 bool IsRedundant() const {
2473 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
2474 }
2475
2476 // We clear both operands to indicate move that's been eliminated.
2477 void Eliminate() {
2478 source_ = destination_ = Location::NoLocation();
2479 }
2480
2481 bool IsEliminated() const {
2482 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
2483 return source_.IsInvalid();
2484 }
2485
Nicolas Geoffray740475d2014-09-29 10:33:25 +01002486 HInstruction* GetInstruction() const { return instruction_; }
2487
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002488 private:
2489 Location source_;
2490 Location destination_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01002491 // The instruction this move is assocatied with. Null when this move is
2492 // for moving an input in the expected locations of user (including a phi user).
2493 // This is only used in debug mode, to ensure we do not connect interval siblings
2494 // in the same parallel move.
2495 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002496
2497 DISALLOW_COPY_AND_ASSIGN(MoveOperands);
2498};
2499
2500static constexpr size_t kDefaultNumberOfMoves = 4;
2501
2502class HParallelMove : public HTemplateInstruction<0> {
2503 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002504 explicit HParallelMove(ArenaAllocator* arena)
2505 : HTemplateInstruction(SideEffects::None()), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002506
2507 void AddMove(MoveOperands* move) {
Nicolas Geoffray740475d2014-09-29 10:33:25 +01002508 if (kIsDebugBuild && move->GetInstruction() != nullptr) {
2509 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
2510 DCHECK_NE(moves_.Get(i)->GetInstruction(), move->GetInstruction())
2511 << "Doing parallel moves for the same instruction.";
2512 }
2513 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002514 moves_.Add(move);
2515 }
2516
2517 MoveOperands* MoveOperandsAt(size_t index) const {
2518 return moves_.Get(index);
2519 }
2520
2521 size_t NumMoves() const { return moves_.Size(); }
2522
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002523 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002524
2525 private:
2526 GrowableArray<MoveOperands*> moves_;
2527
2528 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
2529};
2530
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002531class HGraphVisitor : public ValueObject {
2532 public:
Dave Allison20dfc792014-06-16 20:44:29 -07002533 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
2534 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002535
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002536 virtual void VisitInstruction(HInstruction* instruction) { UNUSED(instruction); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002537 virtual void VisitBasicBlock(HBasicBlock* block);
2538
Roland Levillain633021e2014-10-01 14:12:25 +01002539 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002540 void VisitInsertionOrder();
2541
Roland Levillain633021e2014-10-01 14:12:25 +01002542 // Visit the graph following dominator tree reverse post-order.
2543 void VisitReversePostOrder();
2544
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002545 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002546
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002547 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002548#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002549 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
2550
2551 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
2552
2553#undef DECLARE_VISIT_INSTRUCTION
2554
2555 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07002556 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002557
2558 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
2559};
2560
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002561class HGraphDelegateVisitor : public HGraphVisitor {
2562 public:
2563 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
2564 virtual ~HGraphDelegateVisitor() {}
2565
2566 // Visit functions that delegate to to super class.
2567#define DECLARE_VISIT_INSTRUCTION(name, super) \
2568 virtual void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
2569
2570 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
2571
2572#undef DECLARE_VISIT_INSTRUCTION
2573
2574 private:
2575 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
2576};
2577
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002578class HInsertionOrderIterator : public ValueObject {
2579 public:
2580 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
2581
2582 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
2583 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
2584 void Advance() { ++index_; }
2585
2586 private:
2587 const HGraph& graph_;
2588 size_t index_;
2589
2590 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
2591};
2592
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002593class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002594 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002595 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002596
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002597 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
2598 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002599 void Advance() { ++index_; }
2600
2601 private:
2602 const HGraph& graph_;
2603 size_t index_;
2604
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002605 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002606};
2607
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002608class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002609 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002610 explicit HPostOrderIterator(const HGraph& graph)
2611 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002612
2613 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002614 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002615 void Advance() { --index_; }
2616
2617 private:
2618 const HGraph& graph_;
2619 size_t index_;
2620
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002621 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002622};
2623
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002624} // namespace art
2625
2626#endif // ART_COMPILER_OPTIMIZING_NODES_H_