blob: e74ed827ec39f83fadb5d1d9d98bc244eb00f71b [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
20#include "utils/allocation.h"
Nicolas Geoffray0e336432014-02-26 18:24:38 +000021#include "utils/arena_bit_vector.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000022#include "utils/growable_array.h"
23
24namespace art {
25
26class HBasicBlock;
27class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000028class HIntConstant;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000029class HGraphVisitor;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000030class LocationSummary;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000031
32static const int kDefaultNumberOfBlocks = 8;
33static const int kDefaultNumberOfSuccessors = 2;
34static const int kDefaultNumberOfPredecessors = 2;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000035static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000036
37// Control-flow graph of a method. Contains a list of basic blocks.
38class HGraph : public ArenaObject {
39 public:
40 explicit HGraph(ArenaAllocator* arena)
41 : arena_(arena),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000042 blocks_(arena, kDefaultNumberOfBlocks),
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000043 dominator_order_(arena, kDefaultNumberOfBlocks),
44 current_instruction_id_(0) { }
Nicolas Geoffray818f2102014-02-18 16:43:35 +000045
Nicolas Geoffray787c3072014-03-17 10:20:19 +000046 ArenaAllocator* GetArena() const { return arena_; }
47 const GrowableArray<HBasicBlock*>* GetBlocks() const { return &blocks_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +000048
Nicolas Geoffray787c3072014-03-17 10:20:19 +000049 HBasicBlock* GetEntryBlock() const { return entry_block_; }
50 HBasicBlock* GetExitBlock() const { return exit_block_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000051
Nicolas Geoffray787c3072014-03-17 10:20:19 +000052 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
53 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000054
Nicolas Geoffray818f2102014-02-18 16:43:35 +000055 void AddBlock(HBasicBlock* block);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000056 void BuildDominatorTree();
Nicolas Geoffray818f2102014-02-18 16:43:35 +000057
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000058 int GetNextInstructionId() {
59 return current_instruction_id_++;
60 }
61
Nicolas Geoffray818f2102014-02-18 16:43:35 +000062 private:
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000063 HBasicBlock* FindCommonDominator(HBasicBlock* first, HBasicBlock* second) const;
64 void VisitBlockForDominatorTree(HBasicBlock* block,
65 HBasicBlock* predecessor,
66 GrowableArray<size_t>* visits);
67 void FindBackEdges(ArenaBitVector* visited) const;
68 void VisitBlockForBackEdges(HBasicBlock* block,
69 ArenaBitVector* visited,
70 ArenaBitVector* visiting) const;
71 void RemoveDeadBlocks(const ArenaBitVector& visited) const;
72
Nicolas Geoffray818f2102014-02-18 16:43:35 +000073 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000074
75 // List of blocks in insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +000076 GrowableArray<HBasicBlock*> blocks_;
77
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000078 // List of blocks to perform a pre-order dominator tree traversal.
79 GrowableArray<HBasicBlock*> dominator_order_;
80
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000081 HBasicBlock* entry_block_;
82 HBasicBlock* exit_block_;
83
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000084 // The current id to assign to a newly added instruction. See HInstruction.id_.
85 int current_instruction_id_;
86
Nicolas Geoffray818f2102014-02-18 16:43:35 +000087 DISALLOW_COPY_AND_ASSIGN(HGraph);
88};
89
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000090class HLoopInformation : public ArenaObject {
91 public:
92 HLoopInformation(HBasicBlock* header, HGraph* graph)
93 : header_(header),
Nicolas Geoffray787c3072014-03-17 10:20:19 +000094 back_edges_(graph->GetArena(), kDefaultNumberOfBackEdges) { }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000095
96 void AddBackEdge(HBasicBlock* back_edge) {
97 back_edges_.Add(back_edge);
98 }
99
100 int NumberOfBackEdges() const {
101 return back_edges_.Size();
102 }
103
104 private:
105 HBasicBlock* header_;
106 GrowableArray<HBasicBlock*> back_edges_;
107
108 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
109};
110
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000111// A block in a method. Contains the list of instructions represented
112// as a double linked list. Each block knows its predecessors and
113// successors.
114class HBasicBlock : public ArenaObject {
115 public:
116 explicit HBasicBlock(HGraph* graph)
117 : graph_(graph),
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000118 predecessors_(graph->GetArena(), kDefaultNumberOfPredecessors),
119 successors_(graph->GetArena(), kDefaultNumberOfSuccessors),
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000120 first_instruction_(nullptr),
121 last_instruction_(nullptr),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000122 loop_information_(nullptr),
123 dominator_(nullptr),
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000124 block_id_(-1) { }
125
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000126 const GrowableArray<HBasicBlock*>* GetPredecessors() const {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000127 return &predecessors_;
128 }
129
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000130 const GrowableArray<HBasicBlock*>* GetSuccessors() const {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000131 return &successors_;
132 }
133
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000134 void AddBackEdge(HBasicBlock* back_edge) {
135 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000136 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000137 }
138 loop_information_->AddBackEdge(back_edge);
139 }
140
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000141 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000142
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000143 int GetBlockId() const { return block_id_; }
144 void SetBlockId(int id) { block_id_ = id; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000145
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000146 HBasicBlock* GetDominator() const { return dominator_; }
147 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000148
149 int NumberOfBackEdges() const {
150 return loop_information_ == nullptr
151 ? 0
152 : loop_information_->NumberOfBackEdges();
153 }
154
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000155 HInstruction* GetFirstInstruction() const { return first_instruction_; }
156 HInstruction* GetLastInstruction() const { return last_instruction_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000157
158 void AddSuccessor(HBasicBlock* block) {
159 successors_.Add(block);
160 block->predecessors_.Add(this);
161 }
162
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000163 void RemovePredecessor(HBasicBlock* block) {
164 predecessors_.Delete(block);
165 }
166
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000167 void AddInstruction(HInstruction* instruction);
168
169 private:
170 HGraph* const graph_;
171 GrowableArray<HBasicBlock*> predecessors_;
172 GrowableArray<HBasicBlock*> successors_;
173 HInstruction* first_instruction_;
174 HInstruction* last_instruction_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000175 HLoopInformation* loop_information_;
176 HBasicBlock* dominator_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000177 int block_id_;
178
179 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
180};
181
182#define FOR_EACH_INSTRUCTION(M) \
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000183 M(Equal) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000184 M(Exit) \
185 M(Goto) \
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000186 M(If) \
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000187 M(IntConstant) \
188 M(LoadLocal) \
189 M(Local) \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000190 M(Return) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000191 M(ReturnVoid) \
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000192 M(StoreLocal) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000193
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000194#define FORWARD_DECLARATION(type) class H##type;
195FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
196#undef FORWARD_DECLARATION
197
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000198#define DECLARE_INSTRUCTION(type) \
199 virtual void Accept(HGraphVisitor* visitor); \
200 virtual const char* DebugName() const { return #type; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000201 virtual H##type* As##type() { return this; } \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000202
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000203class HUseListNode : public ArenaObject {
204 public:
205 HUseListNode(HInstruction* instruction, HUseListNode* tail)
206 : instruction_(instruction), tail_(tail) { }
207
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000208 HUseListNode* GetTail() const { return tail_; }
209 HInstruction* GetInstruction() const { return instruction_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000210
211 private:
212 HInstruction* const instruction_;
213 HUseListNode* const tail_;
214
215 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
216};
217
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000218class HInstruction : public ArenaObject {
219 public:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000220 HInstruction()
221 : previous_(nullptr),
222 next_(nullptr),
223 block_(nullptr),
224 id_(-1),
225 uses_(nullptr),
226 locations_(nullptr) { }
227
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000228 virtual ~HInstruction() { }
229
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000230 HInstruction* GetNext() const { return next_; }
231 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000232
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000233 HBasicBlock* GetBlock() const { return block_; }
234 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000235
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000236 virtual intptr_t InputCount() const = 0;
237 virtual HInstruction* InputAt(intptr_t i) const = 0;
238
239 virtual void Accept(HGraphVisitor* visitor) = 0;
240 virtual const char* DebugName() const = 0;
241
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000242 void AddUse(HInstruction* user) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000243 uses_ = new (block_->GetGraph()->GetArena()) HUseListNode(user, uses_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000244 }
245
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000246 HUseListNode* GetUses() const { return uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000247
248 bool HasUses() const { return uses_ != nullptr; }
249
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000250 int GetId() const { return id_; }
251 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000252
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000253 LocationSummary* GetLocations() const { return locations_; }
254 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000255
256#define INSTRUCTION_TYPE_CHECK(type) \
257 virtual H##type* As##type() { return nullptr; }
258
259 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
260#undef INSTRUCTION_TYPE_CHECK
261
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000262 private:
263 HInstruction* previous_;
264 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000265 HBasicBlock* block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000266
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000267 // An instruction gets an id when it is added to the graph.
268 // It reflects creation order. A negative id means the instruction
269 // has not beed added to the graph.
270 int id_;
271
272 HUseListNode* uses_;
273
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000274 // Set by the code generator.
275 LocationSummary* locations_;
276
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000277 friend class HBasicBlock;
278
279 DISALLOW_COPY_AND_ASSIGN(HInstruction);
280};
281
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000282class HUseIterator : public ValueObject {
283 public:
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000284 explicit HUseIterator(HInstruction* instruction) : current_(instruction->GetUses()) { }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000285
286 bool Done() const { return current_ == nullptr; }
287
288 void Advance() {
289 DCHECK(!Done());
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000290 current_ = current_->GetTail();
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000291 }
292
293 HInstruction* Current() const {
294 DCHECK(!Done());
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000295 return current_->GetInstruction();
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000296 }
297
298 private:
299 HUseListNode* current_;
300
301 friend class HValue;
302};
303
304class HInputIterator : public ValueObject {
305 public:
306 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) { }
307
308 bool Done() const { return index_ == instruction_->InputCount(); }
309 HInstruction* Current() const { return instruction_->InputAt(index_); }
310 void Advance() { index_++; }
311
312 private:
313 HInstruction* instruction_;
314 int index_;
315
316 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
317};
318
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000319class HInstructionIterator : public ValueObject {
320 public:
321 explicit HInstructionIterator(HBasicBlock* block)
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000322 : instruction_(block->GetFirstInstruction()) {
323 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000324 }
325
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000326 bool Done() const { return instruction_ == nullptr; }
327 HInstruction* Current() const { return instruction_; }
328 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000329 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000330 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000331 }
332
333 private:
334 HInstruction* instruction_;
335 HInstruction* next_;
336};
337
338// An embedded container with N elements of type T. Used (with partial
339// specialization for N=0) because embedded arrays cannot have size 0.
340template<typename T, intptr_t N>
341class EmbeddedArray {
342 public:
343 EmbeddedArray() : elements_() { }
344
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000345 intptr_t GetLength() const { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000346
347 const T& operator[](intptr_t i) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000348 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000349 return elements_[i];
350 }
351
352 T& operator[](intptr_t i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000353 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000354 return elements_[i];
355 }
356
357 const T& At(intptr_t i) const {
358 return (*this)[i];
359 }
360
361 void SetAt(intptr_t i, const T& val) {
362 (*this)[i] = val;
363 }
364
365 private:
366 T elements_[N];
367};
368
369template<typename T>
370class EmbeddedArray<T, 0> {
371 public:
372 intptr_t length() const { return 0; }
373 const T& operator[](intptr_t i) const {
374 LOG(FATAL) << "Unreachable";
375 static T sentinel = 0;
376 return sentinel;
377 }
378 T& operator[](intptr_t i) {
379 LOG(FATAL) << "Unreachable";
380 static T sentinel = 0;
381 return sentinel;
382 }
383};
384
385template<intptr_t N>
386class HTemplateInstruction: public HInstruction {
387 public:
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000388 HTemplateInstruction<N>() : inputs_() { }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000389 virtual ~HTemplateInstruction() { }
390
391 virtual intptr_t InputCount() const { return N; }
392 virtual HInstruction* InputAt(intptr_t i) const { return inputs_[i]; }
393
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000394 protected:
395 void SetRawInputAt(intptr_t i, HInstruction* instruction) {
396 inputs_[i] = instruction;
397 }
398
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000399 private:
400 EmbeddedArray<HInstruction*, N> inputs_;
401};
402
403// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
404// instruction that branches to the exit block.
405class HReturnVoid : public HTemplateInstruction<0> {
406 public:
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000407 HReturnVoid() { }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000408
409 DECLARE_INSTRUCTION(ReturnVoid)
410
411 private:
412 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
413};
414
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000415// Represents dex's RETURN opcodes. A HReturn is a control flow
416// instruction that branches to the exit block.
417class HReturn : public HTemplateInstruction<1> {
418 public:
419 explicit HReturn(HInstruction* value) {
420 SetRawInputAt(0, value);
421 }
422
423 DECLARE_INSTRUCTION(Return)
424
425 private:
426 DISALLOW_COPY_AND_ASSIGN(HReturn);
427};
428
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000429// The exit instruction is the only instruction of the exit block.
430// Instructions aborting the method (HTrow and HReturn) must branch to the
431// exit block.
432class HExit : public HTemplateInstruction<0> {
433 public:
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000434 HExit() { }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000435
436 DECLARE_INSTRUCTION(Exit)
437
438 private:
439 DISALLOW_COPY_AND_ASSIGN(HExit);
440};
441
442// Jumps from one block to another.
443class HGoto : public HTemplateInstruction<0> {
444 public:
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000445 HGoto() { }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000446
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000447 HBasicBlock* GetSuccessor() const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000448 return GetBlock()->GetSuccessors()->Get(0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000449 }
450
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000451 DECLARE_INSTRUCTION(Goto)
452
453 private:
454 DISALLOW_COPY_AND_ASSIGN(HGoto);
455};
456
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000457// Conditional branch. A block ending with an HIf instruction must have
458// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000459class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000460 public:
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000461 explicit HIf(HInstruction* input) {
462 SetRawInputAt(0, input);
463 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000464
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000465 HBasicBlock* IfTrueSuccessor() const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000466 return GetBlock()->GetSuccessors()->Get(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000467 }
468
469 HBasicBlock* IfFalseSuccessor() const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000470 return GetBlock()->GetSuccessors()->Get(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000471 }
472
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000473 DECLARE_INSTRUCTION(If)
474
475 private:
476 DISALLOW_COPY_AND_ASSIGN(HIf);
477};
478
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000479// Instruction to check if two inputs are equal to each other.
480class HEqual : public HTemplateInstruction<2> {
481 public:
482 HEqual(HInstruction* first, HInstruction* second) {
483 SetRawInputAt(0, first);
484 SetRawInputAt(1, second);
485 }
486
487 DECLARE_INSTRUCTION(Equal)
488
489 private:
490 DISALLOW_COPY_AND_ASSIGN(HEqual);
491};
492
493// A local in the graph. Corresponds to a Dex register.
494class HLocal : public HTemplateInstruction<0> {
495 public:
496 explicit HLocal(uint16_t reg_number) : reg_number_(reg_number) { }
497
498 DECLARE_INSTRUCTION(Local)
499
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000500 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000501
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000502 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000503 // The Dex register number.
504 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000505
506 DISALLOW_COPY_AND_ASSIGN(HLocal);
507};
508
509// Load a given local. The local is an input of this instruction.
510class HLoadLocal : public HTemplateInstruction<1> {
511 public:
512 explicit HLoadLocal(HLocal* local) {
513 SetRawInputAt(0, local);
514 }
515
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000516 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
517
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000518 DECLARE_INSTRUCTION(LoadLocal)
519
520 private:
521 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
522};
523
524// Store a value in a given local. This instruction has two inputs: the value
525// and the local.
526class HStoreLocal : public HTemplateInstruction<2> {
527 public:
528 HStoreLocal(HLocal* local, HInstruction* value) {
529 SetRawInputAt(0, local);
530 SetRawInputAt(1, value);
531 }
532
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000533 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
534
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000535 DECLARE_INSTRUCTION(StoreLocal)
536
537 private:
538 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
539};
540
541// Constants of the type int. Those can be from Dex instructions, or
542// synthesized (for example with the if-eqz instruction).
543class HIntConstant : public HTemplateInstruction<0> {
544 public:
545 explicit HIntConstant(int32_t value) : value_(value) { }
546
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000547 int32_t GetValue() const { return value_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000548
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000549 DECLARE_INSTRUCTION(IntConstant)
550
551 private:
552 const int32_t value_;
553
554 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
555};
556
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000557class HGraphVisitor : public ValueObject {
558 public:
559 explicit HGraphVisitor(HGraph* graph) : graph_(graph) { }
560 virtual ~HGraphVisitor() { }
561
562 virtual void VisitInstruction(HInstruction* instruction) { }
563 virtual void VisitBasicBlock(HBasicBlock* block);
564
565 void VisitInsertionOrder();
566
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000567 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000568
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000569 // Visit functions for instruction classes.
570#define DECLARE_VISIT_INSTRUCTION(name) \
571 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
572
573 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
574
575#undef DECLARE_VISIT_INSTRUCTION
576
577 private:
578 HGraph* graph_;
579
580 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
581};
582
583} // namespace art
584
585#endif // ART_COMPILER_OPTIMIZING_NODES_H_