blob: 50d5c596724bad1bb9df24dab0d71936cad34b6b [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) \
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000188 M(InvokeStatic) \
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000189 M(LoadLocal) \
190 M(Local) \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000191 M(Return) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000192 M(ReturnVoid) \
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000193 M(StoreLocal) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000194
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000195#define FORWARD_DECLARATION(type) class H##type;
196FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
197#undef FORWARD_DECLARATION
198
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000199#define DECLARE_INSTRUCTION(type) \
200 virtual void Accept(HGraphVisitor* visitor); \
201 virtual const char* DebugName() const { return #type; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000202 virtual H##type* As##type() { return this; } \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000203
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000204class HUseListNode : public ArenaObject {
205 public:
206 HUseListNode(HInstruction* instruction, HUseListNode* tail)
207 : instruction_(instruction), tail_(tail) { }
208
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000209 HUseListNode* GetTail() const { return tail_; }
210 HInstruction* GetInstruction() const { return instruction_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000211
212 private:
213 HInstruction* const instruction_;
214 HUseListNode* const tail_;
215
216 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
217};
218
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000219class HInstruction : public ArenaObject {
220 public:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000221 HInstruction()
222 : previous_(nullptr),
223 next_(nullptr),
224 block_(nullptr),
225 id_(-1),
226 uses_(nullptr),
227 locations_(nullptr) { }
228
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000229 virtual ~HInstruction() { }
230
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000231 HInstruction* GetNext() const { return next_; }
232 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000233
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000234 HBasicBlock* GetBlock() const { return block_; }
235 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000236
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000237 virtual intptr_t InputCount() const = 0;
238 virtual HInstruction* InputAt(intptr_t i) const = 0;
239
240 virtual void Accept(HGraphVisitor* visitor) = 0;
241 virtual const char* DebugName() const = 0;
242
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000243 void AddUse(HInstruction* user) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000244 uses_ = new (block_->GetGraph()->GetArena()) HUseListNode(user, uses_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000245 }
246
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000247 HUseListNode* GetUses() const { return uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000248
249 bool HasUses() const { return uses_ != nullptr; }
250
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000251 int GetId() const { return id_; }
252 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000253
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000254 LocationSummary* GetLocations() const { return locations_; }
255 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000256
257#define INSTRUCTION_TYPE_CHECK(type) \
258 virtual H##type* As##type() { return nullptr; }
259
260 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
261#undef INSTRUCTION_TYPE_CHECK
262
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000263 private:
264 HInstruction* previous_;
265 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000266 HBasicBlock* block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000267
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000268 // An instruction gets an id when it is added to the graph.
269 // It reflects creation order. A negative id means the instruction
270 // has not beed added to the graph.
271 int id_;
272
273 HUseListNode* uses_;
274
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000275 // Set by the code generator.
276 LocationSummary* locations_;
277
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000278 friend class HBasicBlock;
279
280 DISALLOW_COPY_AND_ASSIGN(HInstruction);
281};
282
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000283class HUseIterator : public ValueObject {
284 public:
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000285 explicit HUseIterator(HInstruction* instruction) : current_(instruction->GetUses()) { }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000286
287 bool Done() const { return current_ == nullptr; }
288
289 void Advance() {
290 DCHECK(!Done());
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000291 current_ = current_->GetTail();
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000292 }
293
294 HInstruction* Current() const {
295 DCHECK(!Done());
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000296 return current_->GetInstruction();
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000297 }
298
299 private:
300 HUseListNode* current_;
301
302 friend class HValue;
303};
304
305class HInputIterator : public ValueObject {
306 public:
307 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) { }
308
309 bool Done() const { return index_ == instruction_->InputCount(); }
310 HInstruction* Current() const { return instruction_->InputAt(index_); }
311 void Advance() { index_++; }
312
313 private:
314 HInstruction* instruction_;
315 int index_;
316
317 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
318};
319
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000320class HInstructionIterator : public ValueObject {
321 public:
322 explicit HInstructionIterator(HBasicBlock* block)
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000323 : instruction_(block->GetFirstInstruction()) {
324 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000325 }
326
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000327 bool Done() const { return instruction_ == nullptr; }
328 HInstruction* Current() const { return instruction_; }
329 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000330 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000331 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000332 }
333
334 private:
335 HInstruction* instruction_;
336 HInstruction* next_;
337};
338
339// An embedded container with N elements of type T. Used (with partial
340// specialization for N=0) because embedded arrays cannot have size 0.
341template<typename T, intptr_t N>
342class EmbeddedArray {
343 public:
344 EmbeddedArray() : elements_() { }
345
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000346 intptr_t GetLength() const { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000347
348 const T& operator[](intptr_t i) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000349 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000350 return elements_[i];
351 }
352
353 T& operator[](intptr_t i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000354 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000355 return elements_[i];
356 }
357
358 const T& At(intptr_t i) const {
359 return (*this)[i];
360 }
361
362 void SetAt(intptr_t i, const T& val) {
363 (*this)[i] = val;
364 }
365
366 private:
367 T elements_[N];
368};
369
370template<typename T>
371class EmbeddedArray<T, 0> {
372 public:
373 intptr_t length() const { return 0; }
374 const T& operator[](intptr_t i) const {
375 LOG(FATAL) << "Unreachable";
376 static T sentinel = 0;
377 return sentinel;
378 }
379 T& operator[](intptr_t i) {
380 LOG(FATAL) << "Unreachable";
381 static T sentinel = 0;
382 return sentinel;
383 }
384};
385
386template<intptr_t N>
387class HTemplateInstruction: public HInstruction {
388 public:
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000389 HTemplateInstruction<N>() : inputs_() { }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000390 virtual ~HTemplateInstruction() { }
391
392 virtual intptr_t InputCount() const { return N; }
393 virtual HInstruction* InputAt(intptr_t i) const { return inputs_[i]; }
394
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000395 protected:
396 void SetRawInputAt(intptr_t i, HInstruction* instruction) {
397 inputs_[i] = instruction;
398 }
399
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000400 private:
401 EmbeddedArray<HInstruction*, N> inputs_;
402};
403
404// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
405// instruction that branches to the exit block.
406class HReturnVoid : public HTemplateInstruction<0> {
407 public:
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000408 HReturnVoid() { }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000409
410 DECLARE_INSTRUCTION(ReturnVoid)
411
412 private:
413 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
414};
415
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000416// Represents dex's RETURN opcodes. A HReturn is a control flow
417// instruction that branches to the exit block.
418class HReturn : public HTemplateInstruction<1> {
419 public:
420 explicit HReturn(HInstruction* value) {
421 SetRawInputAt(0, value);
422 }
423
424 DECLARE_INSTRUCTION(Return)
425
426 private:
427 DISALLOW_COPY_AND_ASSIGN(HReturn);
428};
429
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000430// The exit instruction is the only instruction of the exit block.
431// Instructions aborting the method (HTrow and HReturn) must branch to the
432// exit block.
433class HExit : public HTemplateInstruction<0> {
434 public:
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000435 HExit() { }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000436
437 DECLARE_INSTRUCTION(Exit)
438
439 private:
440 DISALLOW_COPY_AND_ASSIGN(HExit);
441};
442
443// Jumps from one block to another.
444class HGoto : public HTemplateInstruction<0> {
445 public:
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000446 HGoto() { }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000447
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000448 HBasicBlock* GetSuccessor() const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000449 return GetBlock()->GetSuccessors()->Get(0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000450 }
451
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000452 DECLARE_INSTRUCTION(Goto)
453
454 private:
455 DISALLOW_COPY_AND_ASSIGN(HGoto);
456};
457
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000458// Conditional branch. A block ending with an HIf instruction must have
459// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000460class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000461 public:
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000462 explicit HIf(HInstruction* input) {
463 SetRawInputAt(0, input);
464 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000465
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000466 HBasicBlock* IfTrueSuccessor() const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000467 return GetBlock()->GetSuccessors()->Get(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000468 }
469
470 HBasicBlock* IfFalseSuccessor() const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000471 return GetBlock()->GetSuccessors()->Get(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000472 }
473
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000474 DECLARE_INSTRUCTION(If)
475
476 private:
477 DISALLOW_COPY_AND_ASSIGN(HIf);
478};
479
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000480// Instruction to check if two inputs are equal to each other.
481class HEqual : public HTemplateInstruction<2> {
482 public:
483 HEqual(HInstruction* first, HInstruction* second) {
484 SetRawInputAt(0, first);
485 SetRawInputAt(1, second);
486 }
487
488 DECLARE_INSTRUCTION(Equal)
489
490 private:
491 DISALLOW_COPY_AND_ASSIGN(HEqual);
492};
493
494// A local in the graph. Corresponds to a Dex register.
495class HLocal : public HTemplateInstruction<0> {
496 public:
497 explicit HLocal(uint16_t reg_number) : reg_number_(reg_number) { }
498
499 DECLARE_INSTRUCTION(Local)
500
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000501 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000502
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000503 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000504 // The Dex register number.
505 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000506
507 DISALLOW_COPY_AND_ASSIGN(HLocal);
508};
509
510// Load a given local. The local is an input of this instruction.
511class HLoadLocal : public HTemplateInstruction<1> {
512 public:
513 explicit HLoadLocal(HLocal* local) {
514 SetRawInputAt(0, local);
515 }
516
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000517 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
518
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000519 DECLARE_INSTRUCTION(LoadLocal)
520
521 private:
522 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
523};
524
525// Store a value in a given local. This instruction has two inputs: the value
526// and the local.
527class HStoreLocal : public HTemplateInstruction<2> {
528 public:
529 HStoreLocal(HLocal* local, HInstruction* value) {
530 SetRawInputAt(0, local);
531 SetRawInputAt(1, value);
532 }
533
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000534 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
535
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000536 DECLARE_INSTRUCTION(StoreLocal)
537
538 private:
539 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
540};
541
542// Constants of the type int. Those can be from Dex instructions, or
543// synthesized (for example with the if-eqz instruction).
544class HIntConstant : public HTemplateInstruction<0> {
545 public:
546 explicit HIntConstant(int32_t value) : value_(value) { }
547
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000548 int32_t GetValue() const { return value_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000549
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000550 DECLARE_INSTRUCTION(IntConstant)
551
552 private:
553 const int32_t value_;
554
555 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
556};
557
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000558class HInvoke : public HInstruction {
559 public:
560 HInvoke(ArenaAllocator* arena, uint32_t number_of_arguments, int32_t dex_pc)
561 : inputs_(arena, number_of_arguments),
562 dex_pc_(dex_pc) {
563 inputs_.SetSize(number_of_arguments);
564 }
565
566 virtual intptr_t InputCount() const { return inputs_.Size(); }
567 virtual HInstruction* InputAt(intptr_t i) const { return inputs_.Get(i); }
568
569 int32_t GetDexPc() const { return dex_pc_; }
570
571 protected:
572 GrowableArray<HInstruction*> inputs_;
573 const int32_t dex_pc_;
574
575 private:
576 DISALLOW_COPY_AND_ASSIGN(HInvoke);
577};
578
579class HInvokeStatic : public HInvoke {
580 public:
581 HInvokeStatic(ArenaAllocator* arena, uint32_t number_of_arguments, int32_t dex_pc, int32_t index_in_dex_cache)
582 : HInvoke(arena, number_of_arguments, dex_pc), index_in_dex_cache_(index_in_dex_cache) { }
583
584 uint32_t GetIndexInDexCache() const { return index_in_dex_cache_; }
585
586 DECLARE_INSTRUCTION(InvokeStatic)
587
588 private:
589 uint32_t index_in_dex_cache_;
590
591 DISALLOW_COPY_AND_ASSIGN(HInvokeStatic);
592};
593
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000594class HGraphVisitor : public ValueObject {
595 public:
596 explicit HGraphVisitor(HGraph* graph) : graph_(graph) { }
597 virtual ~HGraphVisitor() { }
598
599 virtual void VisitInstruction(HInstruction* instruction) { }
600 virtual void VisitBasicBlock(HBasicBlock* block);
601
602 void VisitInsertionOrder();
603
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000604 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000605
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000606 // Visit functions for instruction classes.
607#define DECLARE_VISIT_INSTRUCTION(name) \
608 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
609
610 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
611
612#undef DECLARE_VISIT_INSTRUCTION
613
614 private:
615 HGraph* graph_;
616
617 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
618};
619
620} // namespace art
621
622#endif // ART_COMPILER_OPTIMIZING_NODES_H_