blob: 476f24e4910195dd338c2fcce70323fb559985bc [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 Geoffray818f2102014-02-18 16:43:35 +000021#include "utils/allocation.h"
Nicolas Geoffray0e336432014-02-26 18:24:38 +000022#include "utils/arena_bit_vector.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000023#include "utils/growable_array.h"
24
25namespace art {
26
27class HBasicBlock;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010028class HEnvironment;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000029class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000030class HIntConstant;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000031class HGraphVisitor;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010032class HPhi;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010033class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000034class LocationSummary;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000035
36static const int kDefaultNumberOfBlocks = 8;
37static const int kDefaultNumberOfSuccessors = 2;
38static const int kDefaultNumberOfPredecessors = 2;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000039static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000040
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010041class HInstructionList {
42 public:
43 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
44
45 void AddInstruction(HInstruction* instruction);
46 void RemoveInstruction(HInstruction* instruction);
47
48 private:
49 HInstruction* first_instruction_;
50 HInstruction* last_instruction_;
51
52 friend class HBasicBlock;
53 friend class HInstructionIterator;
Nicolas Geoffray804d0932014-05-02 08:46:00 +010054 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010055
56 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
57};
58
Nicolas Geoffray818f2102014-02-18 16:43:35 +000059// Control-flow graph of a method. Contains a list of basic blocks.
60class HGraph : public ArenaObject {
61 public:
62 explicit HGraph(ArenaAllocator* arena)
63 : arena_(arena),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000064 blocks_(arena, kDefaultNumberOfBlocks),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +010065 reverse_post_order_(arena, kDefaultNumberOfBlocks),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +010066 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +010067 number_of_vregs_(0),
68 number_of_in_vregs_(0),
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000069 current_instruction_id_(0) { }
Nicolas Geoffray818f2102014-02-18 16:43:35 +000070
Nicolas Geoffray787c3072014-03-17 10:20:19 +000071 ArenaAllocator* GetArena() const { return arena_; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +010072 const GrowableArray<HBasicBlock*>& GetBlocks() const { return blocks_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +000073
Nicolas Geoffray787c3072014-03-17 10:20:19 +000074 HBasicBlock* GetEntryBlock() const { return entry_block_; }
75 HBasicBlock* GetExitBlock() const { return exit_block_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000076
Nicolas Geoffray787c3072014-03-17 10:20:19 +000077 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
78 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000079
Nicolas Geoffray818f2102014-02-18 16:43:35 +000080 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010081
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000082 void BuildDominatorTree();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010083 void TransformToSSA();
84 void SimplifyCFG();
Nicolas Geoffray818f2102014-02-18 16:43:35 +000085
Nicolas Geoffray622d9c32014-05-12 16:11:02 +010086 // Find all natural loops in this graph. Aborts computation and returns false
Nicolas Geoffrayf635e632014-05-14 09:43:38 +010087 // if one loop is not natural, that is the header does not dominate the back
Nicolas Geoffray622d9c32014-05-12 16:11:02 +010088 // edge.
89 bool FindNaturalLoops() const;
90
91 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
92 void SimplifyLoop(HBasicBlock* header);
93
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000094 int GetNextInstructionId() {
95 return current_instruction_id_++;
96 }
97
Nicolas Geoffray4a34a422014-04-03 10:38:37 +010098 uint16_t GetMaximumNumberOfOutVRegs() const {
99 return maximum_number_of_out_vregs_;
100 }
101
102 void UpdateMaximumNumberOfOutVRegs(uint16_t new_value) {
103 maximum_number_of_out_vregs_ = std::max(new_value, maximum_number_of_out_vregs_);
104 }
105
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100106 void SetNumberOfVRegs(uint16_t number_of_vregs) {
107 number_of_vregs_ = number_of_vregs;
108 }
109
110 uint16_t GetNumberOfVRegs() const {
111 return number_of_vregs_;
112 }
113
114 void SetNumberOfInVRegs(uint16_t value) {
115 number_of_in_vregs_ = value;
116 }
117
118 uint16_t GetNumberOfInVRegs() const {
119 return number_of_in_vregs_;
120 }
121
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100122 const GrowableArray<HBasicBlock*>& GetReversePostOrder() const {
123 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100124 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100125
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000126 private:
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000127 HBasicBlock* FindCommonDominator(HBasicBlock* first, HBasicBlock* second) const;
128 void VisitBlockForDominatorTree(HBasicBlock* block,
129 HBasicBlock* predecessor,
130 GrowableArray<size_t>* visits);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100131 void FindBackEdges(ArenaBitVector* visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000132 void VisitBlockForBackEdges(HBasicBlock* block,
133 ArenaBitVector* visited,
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100134 ArenaBitVector* visiting);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000135 void RemoveDeadBlocks(const ArenaBitVector& visited) const;
136
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000137 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000138
139 // List of blocks in insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000140 GrowableArray<HBasicBlock*> blocks_;
141
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100142 // List of blocks to perform a reverse post order tree traversal.
143 GrowableArray<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000144
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000145 HBasicBlock* entry_block_;
146 HBasicBlock* exit_block_;
147
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100148 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100149 uint16_t maximum_number_of_out_vregs_;
150
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100151 // The number of virtual registers in this method. Contains the parameters.
152 uint16_t number_of_vregs_;
153
154 // The number of virtual registers used by parameters of this method.
155 uint16_t number_of_in_vregs_;
156
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000157 // The current id to assign to a newly added instruction. See HInstruction.id_.
158 int current_instruction_id_;
159
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000160 DISALLOW_COPY_AND_ASSIGN(HGraph);
161};
162
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000163class HLoopInformation : public ArenaObject {
164 public:
165 HLoopInformation(HBasicBlock* header, HGraph* graph)
166 : header_(header),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100167 back_edges_(graph->GetArena(), kDefaultNumberOfBackEdges),
168 blocks_(graph->GetArena(), graph->GetBlocks().Size(), false) {}
169
170 HBasicBlock* GetHeader() const {
171 return header_;
172 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000173
174 void AddBackEdge(HBasicBlock* back_edge) {
175 back_edges_.Add(back_edge);
176 }
177
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100178 void RemoveBackEdge(HBasicBlock* back_edge) {
179 back_edges_.Delete(back_edge);
180 }
181
182 bool IsBackEdge(HBasicBlock* block) {
183 for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) {
184 if (back_edges_.Get(i) == block) return true;
185 }
186 return false;
187 }
188
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000189 int NumberOfBackEdges() const {
190 return back_edges_.Size();
191 }
192
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100193 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100194
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100195 const GrowableArray<HBasicBlock*>& GetBackEdges() const {
196 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100197 }
198
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100199 void ClearBackEdges() {
200 back_edges_.Reset();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100201 }
202
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100203 // Find blocks that are part of this loop. Returns whether the loop is a natural loop,
204 // that is the header dominates the back edge.
205 bool Populate();
206
207 // Returns whether this loop information contains `block`.
208 // Note that this loop information *must* be populated before entering this function.
209 bool Contains(const HBasicBlock& block) const;
210
211 // Returns whether this loop information is an inner loop of `other`.
212 // Note that `other` *must* be populated before entering this function.
213 bool IsIn(const HLoopInformation& other) const;
214
215 const ArenaBitVector& GetBlocks() const { return blocks_; }
216
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000217 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100218 // Internal recursive implementation of `Populate`.
219 void PopulateRecursive(HBasicBlock* block);
220
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000221 HBasicBlock* header_;
222 GrowableArray<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100223 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000224
225 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
226};
227
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100228static constexpr size_t kNoLifetime = -1;
229
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000230// A block in a method. Contains the list of instructions represented
231// as a double linked list. Each block knows its predecessors and
232// successors.
233class HBasicBlock : public ArenaObject {
234 public:
235 explicit HBasicBlock(HGraph* graph)
236 : graph_(graph),
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000237 predecessors_(graph->GetArena(), kDefaultNumberOfPredecessors),
238 successors_(graph->GetArena(), kDefaultNumberOfSuccessors),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000239 loop_information_(nullptr),
240 dominator_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100241 block_id_(-1),
242 lifetime_start_(kNoLifetime),
243 lifetime_end_(kNoLifetime) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000244
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100245 const GrowableArray<HBasicBlock*>& GetPredecessors() const {
246 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000247 }
248
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100249 const GrowableArray<HBasicBlock*>& GetSuccessors() const {
250 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000251 }
252
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000253 void AddBackEdge(HBasicBlock* back_edge) {
254 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000255 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000256 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100257 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000258 loop_information_->AddBackEdge(back_edge);
259 }
260
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000261 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000262
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000263 int GetBlockId() const { return block_id_; }
264 void SetBlockId(int id) { block_id_ = id; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000265
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000266 HBasicBlock* GetDominator() const { return dominator_; }
267 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000268
269 int NumberOfBackEdges() const {
270 return loop_information_ == nullptr
271 ? 0
272 : loop_information_->NumberOfBackEdges();
273 }
274
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100275 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
276 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100277 const HInstructionList& GetInstructions() const { return instructions_; }
278 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000279
280 void AddSuccessor(HBasicBlock* block) {
281 successors_.Add(block);
282 block->predecessors_.Add(this);
283 }
284
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100285 void RemovePredecessor(HBasicBlock* block, bool remove_in_successor = true) {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000286 predecessors_.Delete(block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100287 if (remove_in_successor) {
288 block->successors_.Delete(this);
289 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000290 }
291
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100292 void RemoveSuccessor(HBasicBlock* block, bool remove_in_predecessor = true) {
293 successors_.Delete(block);
294 if (remove_in_predecessor) {
295 block->predecessors_.Delete(this);
296 }
297 }
298
299 void ClearAllPredecessors() {
300 predecessors_.Reset();
301 }
302
303 void AddPredecessor(HBasicBlock* block) {
304 predecessors_.Add(block);
305 block->successors_.Add(this);
306 }
307
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100308 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) {
309 for (size_t i = 0, e = predecessors_.Size(); i < e; ++i) {
310 if (predecessors_.Get(i) == predecessor) {
311 return i;
312 }
313 }
314 return -1;
315 }
316
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000317 void AddInstruction(HInstruction* instruction);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100318 void RemoveInstruction(HInstruction* instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100319 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100320 void AddPhi(HPhi* phi);
321 void RemovePhi(HPhi* phi);
322
323 bool IsLoopHeader() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100324 return (loop_information_ != nullptr) && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100325 }
326
327 HLoopInformation* GetLoopInformation() const {
328 return loop_information_;
329 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000330
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100331 // Set the loop_information_ on this block. This method overrides the current
332 // loop_information if it is an outer loop of the passed loop information.
333 void SetInLoop(HLoopInformation* info) {
334 if (IsLoopHeader()) {
335 // Nothing to do. This just means `info` is an outer loop.
336 } else if (loop_information_ == nullptr) {
337 loop_information_ = info;
338 } else if (loop_information_->Contains(*info->GetHeader())) {
339 // Block is currently part of an outer loop. Make it part of this inner loop.
340 // Note that a non loop header having a loop information means this loop information
341 // has already been populated
342 loop_information_ = info;
343 } else {
344 // Block is part of an inner loop. Do not update the loop information.
345 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
346 // at this point, because this method is being called while populating `info`.
347 }
348 }
349
350 // Returns wheter this block dominates the blocked passed as parameter.
351 bool Dominates(HBasicBlock* block) const;
352
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100353 size_t GetLifetimeStart() const { return lifetime_start_; }
354 size_t GetLifetimeEnd() const { return lifetime_end_; }
355
356 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
357 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
358
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000359 private:
360 HGraph* const graph_;
361 GrowableArray<HBasicBlock*> predecessors_;
362 GrowableArray<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100363 HInstructionList instructions_;
364 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000365 HLoopInformation* loop_information_;
366 HBasicBlock* dominator_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000367 int block_id_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100368 size_t lifetime_start_;
369 size_t lifetime_end_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000370
371 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
372};
373
374#define FOR_EACH_INSTRUCTION(M) \
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000375 M(Add) \
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000376 M(Equal) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000377 M(Exit) \
378 M(Goto) \
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000379 M(If) \
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000380 M(IntConstant) \
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000381 M(InvokeStatic) \
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000382 M(LoadLocal) \
383 M(Local) \
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100384 M(LongConstant) \
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +0100385 M(NewInstance) \
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100386 M(Not) \
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100387 M(ParameterValue) \
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100388 M(ParallelMove) \
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100389 M(Phi) \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000390 M(Return) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000391 M(ReturnVoid) \
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000392 M(StoreLocal) \
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100393 M(Sub) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000394
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000395#define FORWARD_DECLARATION(type) class H##type;
396FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
397#undef FORWARD_DECLARATION
398
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000399#define DECLARE_INSTRUCTION(type) \
400 virtual void Accept(HGraphVisitor* visitor); \
401 virtual const char* DebugName() const { return #type; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000402 virtual H##type* As##type() { return this; } \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000403
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100404template <typename T>
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000405class HUseListNode : public ArenaObject {
406 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100407 HUseListNode(T* user, size_t index, HUseListNode* tail)
408 : user_(user), index_(index), tail_(tail) { }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000409
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000410 HUseListNode* GetTail() const { return tail_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100411 T* GetUser() const { return user_; }
412 size_t GetIndex() const { return index_; }
413
414 void SetTail(HUseListNode<T>* node) { tail_ = node; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000415
416 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100417 T* const user_;
418 const size_t index_;
419 HUseListNode<T>* tail_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000420
421 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
422};
423
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000424class HInstruction : public ArenaObject {
425 public:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000426 HInstruction()
427 : previous_(nullptr),
428 next_(nullptr),
429 block_(nullptr),
430 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100431 ssa_index_(-1),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000432 uses_(nullptr),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100433 env_uses_(nullptr),
434 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100435 locations_(nullptr),
436 live_interval_(nullptr),
437 lifetime_position_(kNoLifetime) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000438
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000439 virtual ~HInstruction() { }
440
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000441 HInstruction* GetNext() const { return next_; }
442 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000443
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000444 HBasicBlock* GetBlock() const { return block_; }
445 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000446
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100447 virtual size_t InputCount() const = 0;
448 virtual HInstruction* InputAt(size_t i) const = 0;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000449
450 virtual void Accept(HGraphVisitor* visitor) = 0;
451 virtual const char* DebugName() const = 0;
452
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100453 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100454 virtual void SetRawInputAt(size_t index, HInstruction* input) = 0;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100455
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100456 virtual bool NeedsEnvironment() const { return false; }
457
458 void AddUseAt(HInstruction* user, size_t index) {
459 uses_ = new (block_->GetGraph()->GetArena()) HUseListNode<HInstruction>(user, index, uses_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000460 }
461
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100462 void AddEnvUseAt(HEnvironment* user, size_t index) {
463 env_uses_ = new (block_->GetGraph()->GetArena()) HUseListNode<HEnvironment>(
464 user, index, env_uses_);
465 }
466
467 void RemoveUser(HInstruction* user, size_t index);
468
469 HUseListNode<HInstruction>* GetUses() const { return uses_; }
470 HUseListNode<HEnvironment>* GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000471
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100472 bool HasUses() const { return uses_ != nullptr || env_uses_ != nullptr; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000473
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100474 size_t NumberOfUses() const {
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100475 // TODO: Optimize this method if it is used outside of the HGraphVisualizer.
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100476 size_t result = 0;
477 HUseListNode<HInstruction>* current = uses_;
478 while (current != nullptr) {
479 current = current->GetTail();
480 ++result;
481 }
482 return result;
483 }
484
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000485 int GetId() const { return id_; }
486 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000487
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100488 int GetSsaIndex() const { return ssa_index_; }
489 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
490 bool HasSsaIndex() const { return ssa_index_ != -1; }
491
492 bool HasEnvironment() const { return environment_ != nullptr; }
493 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100494 void SetEnvironment(HEnvironment* environment) { environment_ = environment; }
495
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000496 LocationSummary* GetLocations() const { return locations_; }
497 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000498
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100499 void ReplaceWith(HInstruction* instruction);
500
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000501#define INSTRUCTION_TYPE_CHECK(type) \
502 virtual H##type* As##type() { return nullptr; }
503
504 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
505#undef INSTRUCTION_TYPE_CHECK
506
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100507 size_t GetLifetimePosition() const { return lifetime_position_; }
508 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
509 LiveInterval* GetLiveInterval() const { return live_interval_; }
510 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
511 bool HasLiveInterval() const { return live_interval_ != nullptr; }
512
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000513 private:
514 HInstruction* previous_;
515 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000516 HBasicBlock* block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000517
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000518 // An instruction gets an id when it is added to the graph.
519 // It reflects creation order. A negative id means the instruction
520 // has not beed added to the graph.
521 int id_;
522
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100523 // When doing liveness analysis, instructions that have uses get an SSA index.
524 int ssa_index_;
525
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100526 // List of instructions that have this instruction as input.
527 HUseListNode<HInstruction>* uses_;
528
529 // List of environments that contain this instruction.
530 HUseListNode<HEnvironment>* env_uses_;
531
532 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000533
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000534 // Set by the code generator.
535 LocationSummary* locations_;
536
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100537 // Set by the liveness analysis.
538 LiveInterval* live_interval_;
539
540 // Set by the liveness analysis, this is the position in a linear
541 // order of blocks where this instruction's live interval start.
542 size_t lifetime_position_;
543
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000544 friend class HBasicBlock;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100545 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000546
547 DISALLOW_COPY_AND_ASSIGN(HInstruction);
548};
549
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100550template<typename T>
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000551class HUseIterator : public ValueObject {
552 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100553 explicit HUseIterator(HUseListNode<T>* uses) : current_(uses) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000554
555 bool Done() const { return current_ == nullptr; }
556
557 void Advance() {
558 DCHECK(!Done());
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000559 current_ = current_->GetTail();
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000560 }
561
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100562 HUseListNode<T>* Current() const {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000563 DCHECK(!Done());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100564 return current_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000565 }
566
567 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100568 HUseListNode<T>* current_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000569
570 friend class HValue;
571};
572
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100573// A HEnvironment object contains the values of virtual registers at a given location.
574class HEnvironment : public ArenaObject {
575 public:
576 HEnvironment(ArenaAllocator* arena, size_t number_of_vregs) : vregs_(arena, number_of_vregs) {
577 vregs_.SetSize(number_of_vregs);
578 for (size_t i = 0; i < number_of_vregs; i++) {
579 vregs_.Put(i, nullptr);
580 }
581 }
582
583 void Populate(const GrowableArray<HInstruction*>& env) {
584 for (size_t i = 0; i < env.Size(); i++) {
585 HInstruction* instruction = env.Get(i);
586 vregs_.Put(i, instruction);
587 if (instruction != nullptr) {
588 instruction->AddEnvUseAt(this, i);
589 }
590 }
591 }
592
593 void SetRawEnvAt(size_t index, HInstruction* instruction) {
594 vregs_.Put(index, instruction);
595 }
596
597 GrowableArray<HInstruction*>* GetVRegs() {
598 return &vregs_;
599 }
600
601 private:
602 GrowableArray<HInstruction*> vregs_;
603
604 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
605};
606
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000607class HInputIterator : public ValueObject {
608 public:
609 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) { }
610
611 bool Done() const { return index_ == instruction_->InputCount(); }
612 HInstruction* Current() const { return instruction_->InputAt(index_); }
613 void Advance() { index_++; }
614
615 private:
616 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100617 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000618
619 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
620};
621
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000622class HInstructionIterator : public ValueObject {
623 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100624 explicit HInstructionIterator(const HInstructionList& instructions)
625 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000626 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000627 }
628
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000629 bool Done() const { return instruction_ == nullptr; }
630 HInstruction* Current() const { return instruction_; }
631 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000632 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000633 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000634 }
635
636 private:
637 HInstruction* instruction_;
638 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100639
640 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000641};
642
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100643class HBackwardInstructionIterator : public ValueObject {
644 public:
645 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
646 : instruction_(instructions.last_instruction_) {
647 next_ = Done() ? nullptr : instruction_->GetPrevious();
648 }
649
650 bool Done() const { return instruction_ == nullptr; }
651 HInstruction* Current() const { return instruction_; }
652 void Advance() {
653 instruction_ = next_;
654 next_ = Done() ? nullptr : instruction_->GetPrevious();
655 }
656
657 private:
658 HInstruction* instruction_;
659 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100660
661 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100662};
663
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000664// An embedded container with N elements of type T. Used (with partial
665// specialization for N=0) because embedded arrays cannot have size 0.
666template<typename T, intptr_t N>
667class EmbeddedArray {
668 public:
669 EmbeddedArray() : elements_() { }
670
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000671 intptr_t GetLength() const { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000672
673 const T& operator[](intptr_t i) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000674 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000675 return elements_[i];
676 }
677
678 T& operator[](intptr_t i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000679 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000680 return elements_[i];
681 }
682
683 const T& At(intptr_t i) const {
684 return (*this)[i];
685 }
686
687 void SetAt(intptr_t i, const T& val) {
688 (*this)[i] = val;
689 }
690
691 private:
692 T elements_[N];
693};
694
695template<typename T>
696class EmbeddedArray<T, 0> {
697 public:
698 intptr_t length() const { return 0; }
699 const T& operator[](intptr_t i) const {
700 LOG(FATAL) << "Unreachable";
701 static T sentinel = 0;
702 return sentinel;
703 }
704 T& operator[](intptr_t i) {
705 LOG(FATAL) << "Unreachable";
706 static T sentinel = 0;
707 return sentinel;
708 }
709};
710
711template<intptr_t N>
712class HTemplateInstruction: public HInstruction {
713 public:
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000714 HTemplateInstruction<N>() : inputs_() { }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000715 virtual ~HTemplateInstruction() { }
716
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100717 virtual size_t InputCount() const { return N; }
718 virtual HInstruction* InputAt(size_t i) const { return inputs_[i]; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000719
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000720 protected:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100721 virtual void SetRawInputAt(size_t i, HInstruction* instruction) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000722 inputs_[i] = instruction;
723 }
724
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000725 private:
726 EmbeddedArray<HInstruction*, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100727
728 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000729};
730
731// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
732// instruction that branches to the exit block.
733class HReturnVoid : public HTemplateInstruction<0> {
734 public:
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000735 HReturnVoid() { }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000736
737 DECLARE_INSTRUCTION(ReturnVoid)
738
739 private:
740 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
741};
742
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000743// Represents dex's RETURN opcodes. A HReturn is a control flow
744// instruction that branches to the exit block.
745class HReturn : public HTemplateInstruction<1> {
746 public:
747 explicit HReturn(HInstruction* value) {
748 SetRawInputAt(0, value);
749 }
750
751 DECLARE_INSTRUCTION(Return)
752
753 private:
754 DISALLOW_COPY_AND_ASSIGN(HReturn);
755};
756
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000757// The exit instruction is the only instruction of the exit block.
758// Instructions aborting the method (HTrow and HReturn) must branch to the
759// exit block.
760class HExit : public HTemplateInstruction<0> {
761 public:
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000762 HExit() { }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000763
764 DECLARE_INSTRUCTION(Exit)
765
766 private:
767 DISALLOW_COPY_AND_ASSIGN(HExit);
768};
769
770// Jumps from one block to another.
771class HGoto : public HTemplateInstruction<0> {
772 public:
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000773 HGoto() { }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000774
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000775 HBasicBlock* GetSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100776 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000777 }
778
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000779 DECLARE_INSTRUCTION(Goto)
780
781 private:
782 DISALLOW_COPY_AND_ASSIGN(HGoto);
783};
784
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000785// Conditional branch. A block ending with an HIf instruction must have
786// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000787class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000788 public:
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000789 explicit HIf(HInstruction* input) {
790 SetRawInputAt(0, input);
791 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000792
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000793 HBasicBlock* IfTrueSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100794 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000795 }
796
797 HBasicBlock* IfFalseSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100798 return GetBlock()->GetSuccessors().Get(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000799 }
800
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000801 DECLARE_INSTRUCTION(If)
802
803 private:
804 DISALLOW_COPY_AND_ASSIGN(HIf);
805};
806
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000807class HBinaryOperation : public HTemplateInstruction<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000808 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000809 HBinaryOperation(Primitive::Type result_type,
810 HInstruction* left,
811 HInstruction* right) : result_type_(result_type) {
812 SetRawInputAt(0, left);
813 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000814 }
815
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000816 HInstruction* GetLeft() const { return InputAt(0); }
817 HInstruction* GetRight() const { return InputAt(1); }
818 Primitive::Type GetResultType() const { return result_type_; }
819
820 virtual bool IsCommutative() { return false; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100821 virtual Primitive::Type GetType() const { return GetResultType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000822
823 private:
824 const Primitive::Type result_type_;
825
826 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
827};
828
829
830// Instruction to check if two inputs are equal to each other.
831class HEqual : public HBinaryOperation {
832 public:
833 HEqual(HInstruction* first, HInstruction* second)
834 : HBinaryOperation(Primitive::kPrimBoolean, first, second) {}
835
836 virtual bool IsCommutative() { return true; }
837
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100838 virtual Primitive::Type GetType() const { return Primitive::kPrimBoolean; }
839
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000840 DECLARE_INSTRUCTION(Equal)
841
842 private:
843 DISALLOW_COPY_AND_ASSIGN(HEqual);
844};
845
846// A local in the graph. Corresponds to a Dex register.
847class HLocal : public HTemplateInstruction<0> {
848 public:
849 explicit HLocal(uint16_t reg_number) : reg_number_(reg_number) { }
850
851 DECLARE_INSTRUCTION(Local)
852
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000853 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000854
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000855 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000856 // The Dex register number.
857 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000858
859 DISALLOW_COPY_AND_ASSIGN(HLocal);
860};
861
862// Load a given local. The local is an input of this instruction.
863class HLoadLocal : public HTemplateInstruction<1> {
864 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100865 explicit HLoadLocal(HLocal* local, Primitive::Type type) : type_(type) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000866 SetRawInputAt(0, local);
867 }
868
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100869 virtual Primitive::Type GetType() const { return type_; }
870
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000871 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
872
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000873 DECLARE_INSTRUCTION(LoadLocal)
874
875 private:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100876 const Primitive::Type type_;
877
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000878 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
879};
880
881// Store a value in a given local. This instruction has two inputs: the value
882// and the local.
883class HStoreLocal : public HTemplateInstruction<2> {
884 public:
885 HStoreLocal(HLocal* local, HInstruction* value) {
886 SetRawInputAt(0, local);
887 SetRawInputAt(1, value);
888 }
889
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000890 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
891
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000892 DECLARE_INSTRUCTION(StoreLocal)
893
894 private:
895 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
896};
897
898// Constants of the type int. Those can be from Dex instructions, or
899// synthesized (for example with the if-eqz instruction).
900class HIntConstant : public HTemplateInstruction<0> {
901 public:
902 explicit HIntConstant(int32_t value) : value_(value) { }
903
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000904 int32_t GetValue() const { return value_; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100905 virtual Primitive::Type GetType() const { return Primitive::kPrimInt; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000906
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000907 DECLARE_INSTRUCTION(IntConstant)
908
909 private:
910 const int32_t value_;
911
912 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
913};
914
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100915class HLongConstant : public HTemplateInstruction<0> {
916 public:
917 explicit HLongConstant(int64_t value) : value_(value) { }
918
919 int64_t GetValue() const { return value_; }
920
921 virtual Primitive::Type GetType() const { return Primitive::kPrimLong; }
922
923 DECLARE_INSTRUCTION(LongConstant)
924
925 private:
926 const int64_t value_;
927
928 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
929};
930
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000931class HInvoke : public HInstruction {
932 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100933 HInvoke(ArenaAllocator* arena,
934 uint32_t number_of_arguments,
935 Primitive::Type return_type,
936 uint32_t dex_pc)
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000937 : inputs_(arena, number_of_arguments),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100938 return_type_(return_type),
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000939 dex_pc_(dex_pc) {
940 inputs_.SetSize(number_of_arguments);
941 }
942
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100943 virtual size_t InputCount() const { return inputs_.Size(); }
944 virtual HInstruction* InputAt(size_t i) const { return inputs_.Get(i); }
945
946 // Runtime needs to walk the stack, so Dex -> Dex calls need to
947 // know their environment.
948 virtual bool NeedsEnvironment() const { return true; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000949
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100950 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100951 SetRawInputAt(index, argument);
952 }
953
954 virtual void SetRawInputAt(size_t index, HInstruction* input) {
955 inputs_.Put(index, input);
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100956 }
957
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100958 virtual Primitive::Type GetType() const { return return_type_; }
959
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +0100960 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000961
962 protected:
963 GrowableArray<HInstruction*> inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100964 const Primitive::Type return_type_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +0100965 const uint32_t dex_pc_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000966
967 private:
968 DISALLOW_COPY_AND_ASSIGN(HInvoke);
969};
970
971class HInvokeStatic : public HInvoke {
972 public:
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100973 HInvokeStatic(ArenaAllocator* arena,
974 uint32_t number_of_arguments,
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100975 Primitive::Type return_type,
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +0100976 uint32_t dex_pc,
977 uint32_t index_in_dex_cache)
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100978 : HInvoke(arena, number_of_arguments, return_type, dex_pc),
979 index_in_dex_cache_(index_in_dex_cache) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000980
981 uint32_t GetIndexInDexCache() const { return index_in_dex_cache_; }
982
983 DECLARE_INSTRUCTION(InvokeStatic)
984
985 private:
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100986 const uint32_t index_in_dex_cache_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000987
988 DISALLOW_COPY_AND_ASSIGN(HInvokeStatic);
989};
990
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +0100991class HNewInstance : public HTemplateInstruction<0> {
992 public:
993 HNewInstance(uint32_t dex_pc, uint16_t type_index) : dex_pc_(dex_pc), type_index_(type_index) {}
994
995 uint32_t GetDexPc() const { return dex_pc_; }
996 uint16_t GetTypeIndex() const { return type_index_; }
997
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100998 virtual Primitive::Type GetType() const { return Primitive::kPrimNot; }
999
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001000 // Calls runtime so needs an environment.
1001 virtual bool NeedsEnvironment() const { return true; }
1002
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001003 DECLARE_INSTRUCTION(NewInstance)
1004
1005 private:
1006 const uint32_t dex_pc_;
1007 const uint16_t type_index_;
1008
1009 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
1010};
1011
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001012class HAdd : public HBinaryOperation {
1013 public:
1014 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1015 : HBinaryOperation(result_type, left, right) {}
1016
1017 virtual bool IsCommutative() { return true; }
1018
1019 DECLARE_INSTRUCTION(Add);
1020
1021 private:
1022 DISALLOW_COPY_AND_ASSIGN(HAdd);
1023};
1024
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001025class HSub : public HBinaryOperation {
1026 public:
1027 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1028 : HBinaryOperation(result_type, left, right) {}
1029
1030 virtual bool IsCommutative() { return false; }
1031
1032 DECLARE_INSTRUCTION(Sub);
1033
1034 private:
1035 DISALLOW_COPY_AND_ASSIGN(HSub);
1036};
1037
1038// The value of a parameter in this method. Its location depends on
1039// the calling convention.
1040class HParameterValue : public HTemplateInstruction<0> {
1041 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001042 HParameterValue(uint8_t index, Primitive::Type parameter_type)
1043 : index_(index), parameter_type_(parameter_type) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001044
1045 uint8_t GetIndex() const { return index_; }
1046
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001047 virtual Primitive::Type GetType() const { return parameter_type_; }
1048
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001049 DECLARE_INSTRUCTION(ParameterValue);
1050
1051 private:
1052 // The index of this parameter in the parameters list. Must be less
1053 // than HGraph::number_of_in_vregs_;
1054 const uint8_t index_;
1055
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001056 const Primitive::Type parameter_type_;
1057
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001058 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
1059};
1060
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001061class HNot : public HTemplateInstruction<1> {
1062 public:
1063 explicit HNot(HInstruction* input) {
1064 SetRawInputAt(0, input);
1065 }
1066
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001067 virtual Primitive::Type GetType() const { return Primitive::kPrimBoolean; }
1068
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001069 DECLARE_INSTRUCTION(Not);
1070
1071 private:
1072 DISALLOW_COPY_AND_ASSIGN(HNot);
1073};
1074
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001075class HPhi : public HInstruction {
1076 public:
1077 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
1078 : inputs_(arena, number_of_inputs),
1079 reg_number_(reg_number),
1080 type_(type) {
1081 inputs_.SetSize(number_of_inputs);
1082 }
1083
1084 virtual size_t InputCount() const { return inputs_.Size(); }
1085 virtual HInstruction* InputAt(size_t i) const { return inputs_.Get(i); }
1086
1087 virtual void SetRawInputAt(size_t index, HInstruction* input) {
1088 inputs_.Put(index, input);
1089 }
1090
1091 void AddInput(HInstruction* input);
1092
1093 virtual Primitive::Type GetType() const { return type_; }
1094
1095 uint32_t GetRegNumber() const { return reg_number_; }
1096
1097 DECLARE_INSTRUCTION(Phi)
1098
1099 protected:
1100 GrowableArray<HInstruction*> inputs_;
1101 const uint32_t reg_number_;
1102 const Primitive::Type type_;
1103
1104 private:
1105 DISALLOW_COPY_AND_ASSIGN(HPhi);
1106};
1107
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001108class MoveOperands : public ArenaObject {
1109 public:
1110 MoveOperands(Location source, Location destination)
1111 : source_(source), destination_(destination) {}
1112
1113 Location GetSource() const { return source_; }
1114 Location GetDestination() const { return destination_; }
1115
1116 void SetSource(Location value) { source_ = value; }
1117 void SetDestination(Location value) { destination_ = value; }
1118
1119 // The parallel move resolver marks moves as "in-progress" by clearing the
1120 // destination (but not the source).
1121 Location MarkPending() {
1122 DCHECK(!IsPending());
1123 Location dest = destination_;
1124 destination_ = Location::NoLocation();
1125 return dest;
1126 }
1127
1128 void ClearPending(Location dest) {
1129 DCHECK(IsPending());
1130 destination_ = dest;
1131 }
1132
1133 bool IsPending() const {
1134 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
1135 return destination_.IsInvalid() && !source_.IsInvalid();
1136 }
1137
1138 // True if this blocks a move from the given location.
1139 bool Blocks(Location loc) const {
1140 return !IsEliminated() && source_.Equals(loc);
1141 }
1142
1143 // A move is redundant if it's been eliminated, if its source and
1144 // destination are the same, or if its destination is unneeded.
1145 bool IsRedundant() const {
1146 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
1147 }
1148
1149 // We clear both operands to indicate move that's been eliminated.
1150 void Eliminate() {
1151 source_ = destination_ = Location::NoLocation();
1152 }
1153
1154 bool IsEliminated() const {
1155 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
1156 return source_.IsInvalid();
1157 }
1158
1159 private:
1160 Location source_;
1161 Location destination_;
1162
1163 DISALLOW_COPY_AND_ASSIGN(MoveOperands);
1164};
1165
1166static constexpr size_t kDefaultNumberOfMoves = 4;
1167
1168class HParallelMove : public HTemplateInstruction<0> {
1169 public:
1170 explicit HParallelMove(ArenaAllocator* arena) : moves_(arena, kDefaultNumberOfMoves) {}
1171
1172 void AddMove(MoveOperands* move) {
1173 moves_.Add(move);
1174 }
1175
1176 MoveOperands* MoveOperandsAt(size_t index) const {
1177 return moves_.Get(index);
1178 }
1179
1180 size_t NumMoves() const { return moves_.Size(); }
1181
1182 DECLARE_INSTRUCTION(ParallelMove)
1183
1184 private:
1185 GrowableArray<MoveOperands*> moves_;
1186
1187 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
1188};
1189
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001190class HGraphVisitor : public ValueObject {
1191 public:
1192 explicit HGraphVisitor(HGraph* graph) : graph_(graph) { }
1193 virtual ~HGraphVisitor() { }
1194
1195 virtual void VisitInstruction(HInstruction* instruction) { }
1196 virtual void VisitBasicBlock(HBasicBlock* block);
1197
1198 void VisitInsertionOrder();
1199
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001200 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001201
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001202 // Visit functions for instruction classes.
1203#define DECLARE_VISIT_INSTRUCTION(name) \
1204 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
1205
1206 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
1207
1208#undef DECLARE_VISIT_INSTRUCTION
1209
1210 private:
1211 HGraph* graph_;
1212
1213 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
1214};
1215
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001216class HInsertionOrderIterator : public ValueObject {
1217 public:
1218 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
1219
1220 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
1221 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
1222 void Advance() { ++index_; }
1223
1224 private:
1225 const HGraph& graph_;
1226 size_t index_;
1227
1228 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
1229};
1230
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001231class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001232 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001233 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001234
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001235 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
1236 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001237 void Advance() { ++index_; }
1238
1239 private:
1240 const HGraph& graph_;
1241 size_t index_;
1242
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001243 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001244};
1245
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001246class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001247 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001248 explicit HPostOrderIterator(const HGraph& graph)
1249 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001250
1251 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001252 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001253 void Advance() { --index_; }
1254
1255 private:
1256 const HGraph& graph_;
1257 size_t index_;
1258
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001259 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001260};
1261
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001262} // namespace art
1263
1264#endif // ART_COMPILER_OPTIMIZING_NODES_H_