blob: 2b219052249f642ad3e4822610370d2b915ba7d6 [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),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +010044 maximum_number_of_out_vregs_(0),
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000045 current_instruction_id_(0) { }
Nicolas Geoffray818f2102014-02-18 16:43:35 +000046
Nicolas Geoffray787c3072014-03-17 10:20:19 +000047 ArenaAllocator* GetArena() const { return arena_; }
48 const GrowableArray<HBasicBlock*>* GetBlocks() const { return &blocks_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +000049
Nicolas Geoffray787c3072014-03-17 10:20:19 +000050 HBasicBlock* GetEntryBlock() const { return entry_block_; }
51 HBasicBlock* GetExitBlock() const { return exit_block_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000052
Nicolas Geoffray787c3072014-03-17 10:20:19 +000053 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
54 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000055
Nicolas Geoffray818f2102014-02-18 16:43:35 +000056 void AddBlock(HBasicBlock* block);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000057 void BuildDominatorTree();
Nicolas Geoffray818f2102014-02-18 16:43:35 +000058
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000059 int GetNextInstructionId() {
60 return current_instruction_id_++;
61 }
62
Nicolas Geoffray4a34a422014-04-03 10:38:37 +010063 uint16_t GetMaximumNumberOfOutVRegs() const {
64 return maximum_number_of_out_vregs_;
65 }
66
67 void UpdateMaximumNumberOfOutVRegs(uint16_t new_value) {
68 maximum_number_of_out_vregs_ = std::max(new_value, maximum_number_of_out_vregs_);
69 }
70
Nicolas Geoffray818f2102014-02-18 16:43:35 +000071 private:
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000072 HBasicBlock* FindCommonDominator(HBasicBlock* first, HBasicBlock* second) const;
73 void VisitBlockForDominatorTree(HBasicBlock* block,
74 HBasicBlock* predecessor,
75 GrowableArray<size_t>* visits);
76 void FindBackEdges(ArenaBitVector* visited) const;
77 void VisitBlockForBackEdges(HBasicBlock* block,
78 ArenaBitVector* visited,
79 ArenaBitVector* visiting) const;
80 void RemoveDeadBlocks(const ArenaBitVector& visited) const;
81
Nicolas Geoffray818f2102014-02-18 16:43:35 +000082 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000083
84 // List of blocks in insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +000085 GrowableArray<HBasicBlock*> blocks_;
86
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000087 // List of blocks to perform a pre-order dominator tree traversal.
88 GrowableArray<HBasicBlock*> dominator_order_;
89
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000090 HBasicBlock* entry_block_;
91 HBasicBlock* exit_block_;
92
Nicolas Geoffray4a34a422014-04-03 10:38:37 +010093 // The maximum number of arguments passed to a HInvoke in this graph.
94 uint16_t maximum_number_of_out_vregs_;
95
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000096 // The current id to assign to a newly added instruction. See HInstruction.id_.
97 int current_instruction_id_;
98
Nicolas Geoffray818f2102014-02-18 16:43:35 +000099 DISALLOW_COPY_AND_ASSIGN(HGraph);
100};
101
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000102class HLoopInformation : public ArenaObject {
103 public:
104 HLoopInformation(HBasicBlock* header, HGraph* graph)
105 : header_(header),
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000106 back_edges_(graph->GetArena(), kDefaultNumberOfBackEdges) { }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000107
108 void AddBackEdge(HBasicBlock* back_edge) {
109 back_edges_.Add(back_edge);
110 }
111
112 int NumberOfBackEdges() const {
113 return back_edges_.Size();
114 }
115
116 private:
117 HBasicBlock* header_;
118 GrowableArray<HBasicBlock*> back_edges_;
119
120 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
121};
122
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000123// A block in a method. Contains the list of instructions represented
124// as a double linked list. Each block knows its predecessors and
125// successors.
126class HBasicBlock : public ArenaObject {
127 public:
128 explicit HBasicBlock(HGraph* graph)
129 : graph_(graph),
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000130 predecessors_(graph->GetArena(), kDefaultNumberOfPredecessors),
131 successors_(graph->GetArena(), kDefaultNumberOfSuccessors),
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000132 first_instruction_(nullptr),
133 last_instruction_(nullptr),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000134 loop_information_(nullptr),
135 dominator_(nullptr),
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000136 block_id_(-1) { }
137
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000138 const GrowableArray<HBasicBlock*>* GetPredecessors() const {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000139 return &predecessors_;
140 }
141
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000142 const GrowableArray<HBasicBlock*>* GetSuccessors() const {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000143 return &successors_;
144 }
145
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000146 void AddBackEdge(HBasicBlock* back_edge) {
147 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000148 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000149 }
150 loop_information_->AddBackEdge(back_edge);
151 }
152
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000153 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000154
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000155 int GetBlockId() const { return block_id_; }
156 void SetBlockId(int id) { block_id_ = id; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000157
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000158 HBasicBlock* GetDominator() const { return dominator_; }
159 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000160
161 int NumberOfBackEdges() const {
162 return loop_information_ == nullptr
163 ? 0
164 : loop_information_->NumberOfBackEdges();
165 }
166
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000167 HInstruction* GetFirstInstruction() const { return first_instruction_; }
168 HInstruction* GetLastInstruction() const { return last_instruction_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000169
170 void AddSuccessor(HBasicBlock* block) {
171 successors_.Add(block);
172 block->predecessors_.Add(this);
173 }
174
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000175 void RemovePredecessor(HBasicBlock* block) {
176 predecessors_.Delete(block);
177 }
178
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000179 void AddInstruction(HInstruction* instruction);
180
181 private:
182 HGraph* const graph_;
183 GrowableArray<HBasicBlock*> predecessors_;
184 GrowableArray<HBasicBlock*> successors_;
185 HInstruction* first_instruction_;
186 HInstruction* last_instruction_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000187 HLoopInformation* loop_information_;
188 HBasicBlock* dominator_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000189 int block_id_;
190
191 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
192};
193
194#define FOR_EACH_INSTRUCTION(M) \
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000195 M(Add) \
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000196 M(Equal) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000197 M(Exit) \
198 M(Goto) \
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000199 M(If) \
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000200 M(IntConstant) \
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000201 M(InvokeStatic) \
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000202 M(LoadLocal) \
203 M(Local) \
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100204 M(PushArgument) \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000205 M(Return) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000206 M(ReturnVoid) \
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000207 M(StoreLocal) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000208
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000209#define FORWARD_DECLARATION(type) class H##type;
210FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
211#undef FORWARD_DECLARATION
212
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000213#define DECLARE_INSTRUCTION(type) \
214 virtual void Accept(HGraphVisitor* visitor); \
215 virtual const char* DebugName() const { return #type; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000216 virtual H##type* As##type() { return this; } \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000217
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000218class HUseListNode : public ArenaObject {
219 public:
220 HUseListNode(HInstruction* instruction, HUseListNode* tail)
221 : instruction_(instruction), tail_(tail) { }
222
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000223 HUseListNode* GetTail() const { return tail_; }
224 HInstruction* GetInstruction() const { return instruction_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000225
226 private:
227 HInstruction* const instruction_;
228 HUseListNode* const tail_;
229
230 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
231};
232
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000233class HInstruction : public ArenaObject {
234 public:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000235 HInstruction()
236 : previous_(nullptr),
237 next_(nullptr),
238 block_(nullptr),
239 id_(-1),
240 uses_(nullptr),
241 locations_(nullptr) { }
242
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000243 virtual ~HInstruction() { }
244
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000245 HInstruction* GetNext() const { return next_; }
246 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000247
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000248 HBasicBlock* GetBlock() const { return block_; }
249 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000250
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000251 virtual intptr_t InputCount() const = 0;
252 virtual HInstruction* InputAt(intptr_t i) const = 0;
253
254 virtual void Accept(HGraphVisitor* visitor) = 0;
255 virtual const char* DebugName() const = 0;
256
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000257 void AddUse(HInstruction* user) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000258 uses_ = new (block_->GetGraph()->GetArena()) HUseListNode(user, uses_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000259 }
260
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000261 HUseListNode* GetUses() const { return uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000262
263 bool HasUses() const { return uses_ != nullptr; }
264
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000265 int GetId() const { return id_; }
266 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000267
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000268 LocationSummary* GetLocations() const { return locations_; }
269 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000270
271#define INSTRUCTION_TYPE_CHECK(type) \
272 virtual H##type* As##type() { return nullptr; }
273
274 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
275#undef INSTRUCTION_TYPE_CHECK
276
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000277 private:
278 HInstruction* previous_;
279 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000280 HBasicBlock* block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000281
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000282 // An instruction gets an id when it is added to the graph.
283 // It reflects creation order. A negative id means the instruction
284 // has not beed added to the graph.
285 int id_;
286
287 HUseListNode* uses_;
288
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000289 // Set by the code generator.
290 LocationSummary* locations_;
291
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000292 friend class HBasicBlock;
293
294 DISALLOW_COPY_AND_ASSIGN(HInstruction);
295};
296
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000297class HUseIterator : public ValueObject {
298 public:
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000299 explicit HUseIterator(HInstruction* instruction) : current_(instruction->GetUses()) { }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000300
301 bool Done() const { return current_ == nullptr; }
302
303 void Advance() {
304 DCHECK(!Done());
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000305 current_ = current_->GetTail();
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000306 }
307
308 HInstruction* Current() const {
309 DCHECK(!Done());
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000310 return current_->GetInstruction();
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000311 }
312
313 private:
314 HUseListNode* current_;
315
316 friend class HValue;
317};
318
319class HInputIterator : public ValueObject {
320 public:
321 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) { }
322
323 bool Done() const { return index_ == instruction_->InputCount(); }
324 HInstruction* Current() const { return instruction_->InputAt(index_); }
325 void Advance() { index_++; }
326
327 private:
328 HInstruction* instruction_;
329 int index_;
330
331 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
332};
333
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000334class HInstructionIterator : public ValueObject {
335 public:
336 explicit HInstructionIterator(HBasicBlock* block)
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000337 : instruction_(block->GetFirstInstruction()) {
338 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000339 }
340
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000341 bool Done() const { return instruction_ == nullptr; }
342 HInstruction* Current() const { return instruction_; }
343 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000344 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000345 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000346 }
347
348 private:
349 HInstruction* instruction_;
350 HInstruction* next_;
351};
352
353// An embedded container with N elements of type T. Used (with partial
354// specialization for N=0) because embedded arrays cannot have size 0.
355template<typename T, intptr_t N>
356class EmbeddedArray {
357 public:
358 EmbeddedArray() : elements_() { }
359
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000360 intptr_t GetLength() const { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000361
362 const T& operator[](intptr_t i) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000363 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000364 return elements_[i];
365 }
366
367 T& operator[](intptr_t i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000368 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000369 return elements_[i];
370 }
371
372 const T& At(intptr_t i) const {
373 return (*this)[i];
374 }
375
376 void SetAt(intptr_t i, const T& val) {
377 (*this)[i] = val;
378 }
379
380 private:
381 T elements_[N];
382};
383
384template<typename T>
385class EmbeddedArray<T, 0> {
386 public:
387 intptr_t length() const { return 0; }
388 const T& operator[](intptr_t i) const {
389 LOG(FATAL) << "Unreachable";
390 static T sentinel = 0;
391 return sentinel;
392 }
393 T& operator[](intptr_t i) {
394 LOG(FATAL) << "Unreachable";
395 static T sentinel = 0;
396 return sentinel;
397 }
398};
399
400template<intptr_t N>
401class HTemplateInstruction: public HInstruction {
402 public:
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000403 HTemplateInstruction<N>() : inputs_() { }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000404 virtual ~HTemplateInstruction() { }
405
406 virtual intptr_t InputCount() const { return N; }
407 virtual HInstruction* InputAt(intptr_t i) const { return inputs_[i]; }
408
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000409 protected:
410 void SetRawInputAt(intptr_t i, HInstruction* instruction) {
411 inputs_[i] = instruction;
412 }
413
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000414 private:
415 EmbeddedArray<HInstruction*, N> inputs_;
416};
417
418// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
419// instruction that branches to the exit block.
420class HReturnVoid : public HTemplateInstruction<0> {
421 public:
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000422 HReturnVoid() { }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000423
424 DECLARE_INSTRUCTION(ReturnVoid)
425
426 private:
427 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
428};
429
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000430// Represents dex's RETURN opcodes. A HReturn is a control flow
431// instruction that branches to the exit block.
432class HReturn : public HTemplateInstruction<1> {
433 public:
434 explicit HReturn(HInstruction* value) {
435 SetRawInputAt(0, value);
436 }
437
438 DECLARE_INSTRUCTION(Return)
439
440 private:
441 DISALLOW_COPY_AND_ASSIGN(HReturn);
442};
443
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000444// The exit instruction is the only instruction of the exit block.
445// Instructions aborting the method (HTrow and HReturn) must branch to the
446// exit block.
447class HExit : public HTemplateInstruction<0> {
448 public:
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000449 HExit() { }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000450
451 DECLARE_INSTRUCTION(Exit)
452
453 private:
454 DISALLOW_COPY_AND_ASSIGN(HExit);
455};
456
457// Jumps from one block to another.
458class HGoto : public HTemplateInstruction<0> {
459 public:
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000460 HGoto() { }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000461
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000462 HBasicBlock* GetSuccessor() const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000463 return GetBlock()->GetSuccessors()->Get(0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000464 }
465
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000466 DECLARE_INSTRUCTION(Goto)
467
468 private:
469 DISALLOW_COPY_AND_ASSIGN(HGoto);
470};
471
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000472// Conditional branch. A block ending with an HIf instruction must have
473// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000474class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000475 public:
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000476 explicit HIf(HInstruction* input) {
477 SetRawInputAt(0, input);
478 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000479
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000480 HBasicBlock* IfTrueSuccessor() const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000481 return GetBlock()->GetSuccessors()->Get(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000482 }
483
484 HBasicBlock* IfFalseSuccessor() const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000485 return GetBlock()->GetSuccessors()->Get(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000486 }
487
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000488 DECLARE_INSTRUCTION(If)
489
490 private:
491 DISALLOW_COPY_AND_ASSIGN(HIf);
492};
493
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000494class HBinaryOperation : public HTemplateInstruction<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000495 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000496 HBinaryOperation(Primitive::Type result_type,
497 HInstruction* left,
498 HInstruction* right) : result_type_(result_type) {
499 SetRawInputAt(0, left);
500 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000501 }
502
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000503 HInstruction* GetLeft() const { return InputAt(0); }
504 HInstruction* GetRight() const { return InputAt(1); }
505 Primitive::Type GetResultType() const { return result_type_; }
506
507 virtual bool IsCommutative() { return false; }
508
509 private:
510 const Primitive::Type result_type_;
511
512 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
513};
514
515
516// Instruction to check if two inputs are equal to each other.
517class HEqual : public HBinaryOperation {
518 public:
519 HEqual(HInstruction* first, HInstruction* second)
520 : HBinaryOperation(Primitive::kPrimBoolean, first, second) {}
521
522 virtual bool IsCommutative() { return true; }
523
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000524 DECLARE_INSTRUCTION(Equal)
525
526 private:
527 DISALLOW_COPY_AND_ASSIGN(HEqual);
528};
529
530// A local in the graph. Corresponds to a Dex register.
531class HLocal : public HTemplateInstruction<0> {
532 public:
533 explicit HLocal(uint16_t reg_number) : reg_number_(reg_number) { }
534
535 DECLARE_INSTRUCTION(Local)
536
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000537 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000538
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000539 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000540 // The Dex register number.
541 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000542
543 DISALLOW_COPY_AND_ASSIGN(HLocal);
544};
545
546// Load a given local. The local is an input of this instruction.
547class HLoadLocal : public HTemplateInstruction<1> {
548 public:
549 explicit HLoadLocal(HLocal* local) {
550 SetRawInputAt(0, local);
551 }
552
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000553 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
554
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000555 DECLARE_INSTRUCTION(LoadLocal)
556
557 private:
558 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
559};
560
561// Store a value in a given local. This instruction has two inputs: the value
562// and the local.
563class HStoreLocal : public HTemplateInstruction<2> {
564 public:
565 HStoreLocal(HLocal* local, HInstruction* value) {
566 SetRawInputAt(0, local);
567 SetRawInputAt(1, value);
568 }
569
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000570 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
571
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000572 DECLARE_INSTRUCTION(StoreLocal)
573
574 private:
575 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
576};
577
578// Constants of the type int. Those can be from Dex instructions, or
579// synthesized (for example with the if-eqz instruction).
580class HIntConstant : public HTemplateInstruction<0> {
581 public:
582 explicit HIntConstant(int32_t value) : value_(value) { }
583
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000584 int32_t GetValue() const { return value_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000585
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000586 DECLARE_INSTRUCTION(IntConstant)
587
588 private:
589 const int32_t value_;
590
591 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
592};
593
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000594class HInvoke : public HInstruction {
595 public:
596 HInvoke(ArenaAllocator* arena, uint32_t number_of_arguments, int32_t dex_pc)
597 : inputs_(arena, number_of_arguments),
598 dex_pc_(dex_pc) {
599 inputs_.SetSize(number_of_arguments);
600 }
601
602 virtual intptr_t InputCount() const { return inputs_.Size(); }
603 virtual HInstruction* InputAt(intptr_t i) const { return inputs_.Get(i); }
604
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100605 void SetArgumentAt(size_t index, HInstruction* argument) {
606 inputs_.Put(index, argument);
607 }
608
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000609 int32_t GetDexPc() const { return dex_pc_; }
610
611 protected:
612 GrowableArray<HInstruction*> inputs_;
613 const int32_t dex_pc_;
614
615 private:
616 DISALLOW_COPY_AND_ASSIGN(HInvoke);
617};
618
619class HInvokeStatic : public HInvoke {
620 public:
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100621 HInvokeStatic(ArenaAllocator* arena,
622 uint32_t number_of_arguments,
623 int32_t dex_pc,
624 int32_t index_in_dex_cache)
625 : HInvoke(arena, number_of_arguments, dex_pc), index_in_dex_cache_(index_in_dex_cache) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000626
627 uint32_t GetIndexInDexCache() const { return index_in_dex_cache_; }
628
629 DECLARE_INSTRUCTION(InvokeStatic)
630
631 private:
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100632 const uint32_t index_in_dex_cache_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000633
634 DISALLOW_COPY_AND_ASSIGN(HInvokeStatic);
635};
636
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100637// HPushArgument nodes are inserted after the evaluation of an argument
638// of a call. Their mere purpose is to ease the code generator's work.
639class HPushArgument : public HTemplateInstruction<1> {
640 public:
641 HPushArgument(HInstruction* argument, uint8_t argument_index) : argument_index_(argument_index) {
642 SetRawInputAt(0, argument);
643 }
644
645 uint8_t GetArgumentIndex() const { return argument_index_; }
646
647 DECLARE_INSTRUCTION(PushArgument)
648
649 private:
650 const uint8_t argument_index_;
651
652 DISALLOW_COPY_AND_ASSIGN(HPushArgument);
653};
654
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000655class HAdd : public HBinaryOperation {
656 public:
657 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
658 : HBinaryOperation(result_type, left, right) {}
659
660 virtual bool IsCommutative() { return true; }
661
662 DECLARE_INSTRUCTION(Add);
663
664 private:
665 DISALLOW_COPY_AND_ASSIGN(HAdd);
666};
667
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000668class HGraphVisitor : public ValueObject {
669 public:
670 explicit HGraphVisitor(HGraph* graph) : graph_(graph) { }
671 virtual ~HGraphVisitor() { }
672
673 virtual void VisitInstruction(HInstruction* instruction) { }
674 virtual void VisitBasicBlock(HBasicBlock* block);
675
676 void VisitInsertionOrder();
677
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000678 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000679
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000680 // Visit functions for instruction classes.
681#define DECLARE_VISIT_INSTRUCTION(name) \
682 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
683
684 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
685
686#undef DECLARE_VISIT_INSTRUCTION
687
688 private:
689 HGraph* graph_;
690
691 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
692};
693
694} // namespace art
695
696#endif // ART_COMPILER_OPTIMIZING_NODES_H_