blob: b1c801611215ef3ace31fb93eb970f97af7ef4ac [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 Geoffraya7062e02014-05-22 12:50:17 +0100279 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000280
281 void AddSuccessor(HBasicBlock* block) {
282 successors_.Add(block);
283 block->predecessors_.Add(this);
284 }
285
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100286 void RemovePredecessor(HBasicBlock* block, bool remove_in_successor = true) {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000287 predecessors_.Delete(block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100288 if (remove_in_successor) {
289 block->successors_.Delete(this);
290 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000291 }
292
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100293 void RemoveSuccessor(HBasicBlock* block, bool remove_in_predecessor = true) {
294 successors_.Delete(block);
295 if (remove_in_predecessor) {
296 block->predecessors_.Delete(this);
297 }
298 }
299
300 void ClearAllPredecessors() {
301 predecessors_.Reset();
302 }
303
304 void AddPredecessor(HBasicBlock* block) {
305 predecessors_.Add(block);
306 block->successors_.Add(this);
307 }
308
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100309 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) {
310 for (size_t i = 0, e = predecessors_.Size(); i < e; ++i) {
311 if (predecessors_.Get(i) == predecessor) {
312 return i;
313 }
314 }
315 return -1;
316 }
317
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000318 void AddInstruction(HInstruction* instruction);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100319 void RemoveInstruction(HInstruction* instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100320 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100321 void AddPhi(HPhi* phi);
322 void RemovePhi(HPhi* phi);
323
324 bool IsLoopHeader() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100325 return (loop_information_ != nullptr) && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100326 }
327
328 HLoopInformation* GetLoopInformation() const {
329 return loop_information_;
330 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000331
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100332 // Set the loop_information_ on this block. This method overrides the current
333 // loop_information if it is an outer loop of the passed loop information.
334 void SetInLoop(HLoopInformation* info) {
335 if (IsLoopHeader()) {
336 // Nothing to do. This just means `info` is an outer loop.
337 } else if (loop_information_ == nullptr) {
338 loop_information_ = info;
339 } else if (loop_information_->Contains(*info->GetHeader())) {
340 // Block is currently part of an outer loop. Make it part of this inner loop.
341 // Note that a non loop header having a loop information means this loop information
342 // has already been populated
343 loop_information_ = info;
344 } else {
345 // Block is part of an inner loop. Do not update the loop information.
346 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
347 // at this point, because this method is being called while populating `info`.
348 }
349 }
350
351 // Returns wheter this block dominates the blocked passed as parameter.
352 bool Dominates(HBasicBlock* block) const;
353
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100354 size_t GetLifetimeStart() const { return lifetime_start_; }
355 size_t GetLifetimeEnd() const { return lifetime_end_; }
356
357 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
358 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
359
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000360 private:
361 HGraph* const graph_;
362 GrowableArray<HBasicBlock*> predecessors_;
363 GrowableArray<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100364 HInstructionList instructions_;
365 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000366 HLoopInformation* loop_information_;
367 HBasicBlock* dominator_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000368 int block_id_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100369 size_t lifetime_start_;
370 size_t lifetime_end_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000371
372 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
373};
374
375#define FOR_EACH_INSTRUCTION(M) \
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000376 M(Add) \
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000377 M(Equal) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000378 M(Exit) \
379 M(Goto) \
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000380 M(If) \
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000381 M(IntConstant) \
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000382 M(InvokeStatic) \
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000383 M(LoadLocal) \
384 M(Local) \
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100385 M(LongConstant) \
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +0100386 M(NewInstance) \
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100387 M(Not) \
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100388 M(ParameterValue) \
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100389 M(ParallelMove) \
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100390 M(Phi) \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000391 M(Return) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000392 M(ReturnVoid) \
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000393 M(StoreLocal) \
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100394 M(Sub) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000395
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000396#define FORWARD_DECLARATION(type) class H##type;
397FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
398#undef FORWARD_DECLARATION
399
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000400#define DECLARE_INSTRUCTION(type) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000401 virtual const char* DebugName() const { return #type; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000402 virtual H##type* As##type() { return this; } \
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100403 virtual void Accept(HGraphVisitor* visitor) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000404
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100405template <typename T>
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000406class HUseListNode : public ArenaObject {
407 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100408 HUseListNode(T* user, size_t index, HUseListNode* tail)
409 : user_(user), index_(index), tail_(tail) { }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000410
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000411 HUseListNode* GetTail() const { return tail_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100412 T* GetUser() const { return user_; }
413 size_t GetIndex() const { return index_; }
414
415 void SetTail(HUseListNode<T>* node) { tail_ = node; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000416
417 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100418 T* const user_;
419 const size_t index_;
420 HUseListNode<T>* tail_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000421
422 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
423};
424
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000425class HInstruction : public ArenaObject {
426 public:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000427 HInstruction()
428 : previous_(nullptr),
429 next_(nullptr),
430 block_(nullptr),
431 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100432 ssa_index_(-1),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000433 uses_(nullptr),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100434 env_uses_(nullptr),
435 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100436 locations_(nullptr),
437 live_interval_(nullptr),
438 lifetime_position_(kNoLifetime) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000439
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000440 virtual ~HInstruction() { }
441
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000442 HInstruction* GetNext() const { return next_; }
443 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000444
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000445 HBasicBlock* GetBlock() const { return block_; }
446 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000447
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100448 virtual size_t InputCount() const = 0;
449 virtual HInstruction* InputAt(size_t i) const = 0;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000450
451 virtual void Accept(HGraphVisitor* visitor) = 0;
452 virtual const char* DebugName() const = 0;
453
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100454 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100455 virtual void SetRawInputAt(size_t index, HInstruction* input) = 0;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100456
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100457 virtual bool NeedsEnvironment() const { return false; }
458
459 void AddUseAt(HInstruction* user, size_t index) {
460 uses_ = new (block_->GetGraph()->GetArena()) HUseListNode<HInstruction>(user, index, uses_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000461 }
462
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100463 void AddEnvUseAt(HEnvironment* user, size_t index) {
464 env_uses_ = new (block_->GetGraph()->GetArena()) HUseListNode<HEnvironment>(
465 user, index, env_uses_);
466 }
467
468 void RemoveUser(HInstruction* user, size_t index);
469
470 HUseListNode<HInstruction>* GetUses() const { return uses_; }
471 HUseListNode<HEnvironment>* GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000472
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100473 bool HasUses() const { return uses_ != nullptr || env_uses_ != nullptr; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000474
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100475 size_t NumberOfUses() const {
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100476 // TODO: Optimize this method if it is used outside of the HGraphVisualizer.
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100477 size_t result = 0;
478 HUseListNode<HInstruction>* current = uses_;
479 while (current != nullptr) {
480 current = current->GetTail();
481 ++result;
482 }
483 return result;
484 }
485
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000486 int GetId() const { return id_; }
487 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000488
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100489 int GetSsaIndex() const { return ssa_index_; }
490 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
491 bool HasSsaIndex() const { return ssa_index_ != -1; }
492
493 bool HasEnvironment() const { return environment_ != nullptr; }
494 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100495 void SetEnvironment(HEnvironment* environment) { environment_ = environment; }
496
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000497 LocationSummary* GetLocations() const { return locations_; }
498 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000499
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100500 void ReplaceWith(HInstruction* instruction);
501
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000502#define INSTRUCTION_TYPE_CHECK(type) \
503 virtual H##type* As##type() { return nullptr; }
504
505 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
506#undef INSTRUCTION_TYPE_CHECK
507
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100508 size_t GetLifetimePosition() const { return lifetime_position_; }
509 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
510 LiveInterval* GetLiveInterval() const { return live_interval_; }
511 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
512 bool HasLiveInterval() const { return live_interval_ != nullptr; }
513
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000514 private:
515 HInstruction* previous_;
516 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000517 HBasicBlock* block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000518
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000519 // An instruction gets an id when it is added to the graph.
520 // It reflects creation order. A negative id means the instruction
521 // has not beed added to the graph.
522 int id_;
523
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100524 // When doing liveness analysis, instructions that have uses get an SSA index.
525 int ssa_index_;
526
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100527 // List of instructions that have this instruction as input.
528 HUseListNode<HInstruction>* uses_;
529
530 // List of environments that contain this instruction.
531 HUseListNode<HEnvironment>* env_uses_;
532
533 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000534
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000535 // Set by the code generator.
536 LocationSummary* locations_;
537
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100538 // Set by the liveness analysis.
539 LiveInterval* live_interval_;
540
541 // Set by the liveness analysis, this is the position in a linear
542 // order of blocks where this instruction's live interval start.
543 size_t lifetime_position_;
544
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000545 friend class HBasicBlock;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100546 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000547
548 DISALLOW_COPY_AND_ASSIGN(HInstruction);
549};
550
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100551template<typename T>
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000552class HUseIterator : public ValueObject {
553 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100554 explicit HUseIterator(HUseListNode<T>* uses) : current_(uses) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000555
556 bool Done() const { return current_ == nullptr; }
557
558 void Advance() {
559 DCHECK(!Done());
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000560 current_ = current_->GetTail();
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000561 }
562
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100563 HUseListNode<T>* Current() const {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000564 DCHECK(!Done());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100565 return current_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000566 }
567
568 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100569 HUseListNode<T>* current_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000570
571 friend class HValue;
572};
573
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100574// A HEnvironment object contains the values of virtual registers at a given location.
575class HEnvironment : public ArenaObject {
576 public:
577 HEnvironment(ArenaAllocator* arena, size_t number_of_vregs) : vregs_(arena, number_of_vregs) {
578 vregs_.SetSize(number_of_vregs);
579 for (size_t i = 0; i < number_of_vregs; i++) {
580 vregs_.Put(i, nullptr);
581 }
582 }
583
584 void Populate(const GrowableArray<HInstruction*>& env) {
585 for (size_t i = 0; i < env.Size(); i++) {
586 HInstruction* instruction = env.Get(i);
587 vregs_.Put(i, instruction);
588 if (instruction != nullptr) {
589 instruction->AddEnvUseAt(this, i);
590 }
591 }
592 }
593
594 void SetRawEnvAt(size_t index, HInstruction* instruction) {
595 vregs_.Put(index, instruction);
596 }
597
598 GrowableArray<HInstruction*>* GetVRegs() {
599 return &vregs_;
600 }
601
602 private:
603 GrowableArray<HInstruction*> vregs_;
604
605 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
606};
607
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000608class HInputIterator : public ValueObject {
609 public:
610 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) { }
611
612 bool Done() const { return index_ == instruction_->InputCount(); }
613 HInstruction* Current() const { return instruction_->InputAt(index_); }
614 void Advance() { index_++; }
615
616 private:
617 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100618 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000619
620 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
621};
622
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000623class HInstructionIterator : public ValueObject {
624 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100625 explicit HInstructionIterator(const HInstructionList& instructions)
626 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000627 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000628 }
629
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000630 bool Done() const { return instruction_ == nullptr; }
631 HInstruction* Current() const { return instruction_; }
632 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000633 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000634 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000635 }
636
637 private:
638 HInstruction* instruction_;
639 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100640
641 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000642};
643
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100644class HBackwardInstructionIterator : public ValueObject {
645 public:
646 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
647 : instruction_(instructions.last_instruction_) {
648 next_ = Done() ? nullptr : instruction_->GetPrevious();
649 }
650
651 bool Done() const { return instruction_ == nullptr; }
652 HInstruction* Current() const { return instruction_; }
653 void Advance() {
654 instruction_ = next_;
655 next_ = Done() ? nullptr : instruction_->GetPrevious();
656 }
657
658 private:
659 HInstruction* instruction_;
660 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100661
662 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100663};
664
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000665// An embedded container with N elements of type T. Used (with partial
666// specialization for N=0) because embedded arrays cannot have size 0.
667template<typename T, intptr_t N>
668class EmbeddedArray {
669 public:
670 EmbeddedArray() : elements_() { }
671
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000672 intptr_t GetLength() const { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000673
674 const T& operator[](intptr_t i) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000675 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000676 return elements_[i];
677 }
678
679 T& operator[](intptr_t i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000680 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000681 return elements_[i];
682 }
683
684 const T& At(intptr_t i) const {
685 return (*this)[i];
686 }
687
688 void SetAt(intptr_t i, const T& val) {
689 (*this)[i] = val;
690 }
691
692 private:
693 T elements_[N];
694};
695
696template<typename T>
697class EmbeddedArray<T, 0> {
698 public:
699 intptr_t length() const { return 0; }
700 const T& operator[](intptr_t i) const {
701 LOG(FATAL) << "Unreachable";
702 static T sentinel = 0;
703 return sentinel;
704 }
705 T& operator[](intptr_t i) {
706 LOG(FATAL) << "Unreachable";
707 static T sentinel = 0;
708 return sentinel;
709 }
710};
711
712template<intptr_t N>
713class HTemplateInstruction: public HInstruction {
714 public:
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000715 HTemplateInstruction<N>() : inputs_() { }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000716 virtual ~HTemplateInstruction() { }
717
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100718 virtual size_t InputCount() const { return N; }
719 virtual HInstruction* InputAt(size_t i) const { return inputs_[i]; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000720
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000721 protected:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100722 virtual void SetRawInputAt(size_t i, HInstruction* instruction) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000723 inputs_[i] = instruction;
724 }
725
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000726 private:
727 EmbeddedArray<HInstruction*, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100728
729 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000730};
731
732// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
733// instruction that branches to the exit block.
734class HReturnVoid : public HTemplateInstruction<0> {
735 public:
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000736 HReturnVoid() { }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000737
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100738 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000739
740 private:
741 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
742};
743
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000744// Represents dex's RETURN opcodes. A HReturn is a control flow
745// instruction that branches to the exit block.
746class HReturn : public HTemplateInstruction<1> {
747 public:
748 explicit HReturn(HInstruction* value) {
749 SetRawInputAt(0, value);
750 }
751
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100752 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000753
754 private:
755 DISALLOW_COPY_AND_ASSIGN(HReturn);
756};
757
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000758// The exit instruction is the only instruction of the exit block.
759// Instructions aborting the method (HTrow and HReturn) must branch to the
760// exit block.
761class HExit : public HTemplateInstruction<0> {
762 public:
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000763 HExit() { }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000764
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100765 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000766
767 private:
768 DISALLOW_COPY_AND_ASSIGN(HExit);
769};
770
771// Jumps from one block to another.
772class HGoto : public HTemplateInstruction<0> {
773 public:
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000774 HGoto() { }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000775
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000776 HBasicBlock* GetSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100777 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000778 }
779
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100780 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000781
782 private:
783 DISALLOW_COPY_AND_ASSIGN(HGoto);
784};
785
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000786// Conditional branch. A block ending with an HIf instruction must have
787// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000788class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000789 public:
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000790 explicit HIf(HInstruction* input) {
791 SetRawInputAt(0, input);
792 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000793
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000794 HBasicBlock* IfTrueSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100795 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000796 }
797
798 HBasicBlock* IfFalseSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100799 return GetBlock()->GetSuccessors().Get(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000800 }
801
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100802 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000803
804 private:
805 DISALLOW_COPY_AND_ASSIGN(HIf);
806};
807
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000808class HBinaryOperation : public HTemplateInstruction<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000809 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000810 HBinaryOperation(Primitive::Type result_type,
811 HInstruction* left,
812 HInstruction* right) : result_type_(result_type) {
813 SetRawInputAt(0, left);
814 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000815 }
816
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000817 HInstruction* GetLeft() const { return InputAt(0); }
818 HInstruction* GetRight() const { return InputAt(1); }
819 Primitive::Type GetResultType() const { return result_type_; }
820
821 virtual bool IsCommutative() { return false; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100822 virtual Primitive::Type GetType() const { return GetResultType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000823
824 private:
825 const Primitive::Type result_type_;
826
827 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
828};
829
830
831// Instruction to check if two inputs are equal to each other.
832class HEqual : public HBinaryOperation {
833 public:
834 HEqual(HInstruction* first, HInstruction* second)
835 : HBinaryOperation(Primitive::kPrimBoolean, first, second) {}
836
837 virtual bool IsCommutative() { return true; }
838
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100839 virtual Primitive::Type GetType() const { return Primitive::kPrimBoolean; }
840
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100841 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000842
843 private:
844 DISALLOW_COPY_AND_ASSIGN(HEqual);
845};
846
847// A local in the graph. Corresponds to a Dex register.
848class HLocal : public HTemplateInstruction<0> {
849 public:
850 explicit HLocal(uint16_t reg_number) : reg_number_(reg_number) { }
851
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100852 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000853
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000854 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000855
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000856 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000857 // The Dex register number.
858 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000859
860 DISALLOW_COPY_AND_ASSIGN(HLocal);
861};
862
863// Load a given local. The local is an input of this instruction.
864class HLoadLocal : public HTemplateInstruction<1> {
865 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100866 explicit HLoadLocal(HLocal* local, Primitive::Type type) : type_(type) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000867 SetRawInputAt(0, local);
868 }
869
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100870 virtual Primitive::Type GetType() const { return type_; }
871
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000872 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
873
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100874 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000875
876 private:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100877 const Primitive::Type type_;
878
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000879 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
880};
881
882// Store a value in a given local. This instruction has two inputs: the value
883// and the local.
884class HStoreLocal : public HTemplateInstruction<2> {
885 public:
886 HStoreLocal(HLocal* local, HInstruction* value) {
887 SetRawInputAt(0, local);
888 SetRawInputAt(1, value);
889 }
890
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000891 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
892
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100893 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000894
895 private:
896 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
897};
898
899// Constants of the type int. Those can be from Dex instructions, or
900// synthesized (for example with the if-eqz instruction).
901class HIntConstant : public HTemplateInstruction<0> {
902 public:
903 explicit HIntConstant(int32_t value) : value_(value) { }
904
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000905 int32_t GetValue() const { return value_; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100906 virtual Primitive::Type GetType() const { return Primitive::kPrimInt; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000907
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100908 DECLARE_INSTRUCTION(IntConstant);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000909
910 private:
911 const int32_t value_;
912
913 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
914};
915
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100916class HLongConstant : public HTemplateInstruction<0> {
917 public:
918 explicit HLongConstant(int64_t value) : value_(value) { }
919
920 int64_t GetValue() const { return value_; }
921
922 virtual Primitive::Type GetType() const { return Primitive::kPrimLong; }
923
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100924 DECLARE_INSTRUCTION(LongConstant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100925
926 private:
927 const int64_t value_;
928
929 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
930};
931
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000932class HInvoke : public HInstruction {
933 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100934 HInvoke(ArenaAllocator* arena,
935 uint32_t number_of_arguments,
936 Primitive::Type return_type,
937 uint32_t dex_pc)
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000938 : inputs_(arena, number_of_arguments),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100939 return_type_(return_type),
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000940 dex_pc_(dex_pc) {
941 inputs_.SetSize(number_of_arguments);
942 }
943
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100944 virtual size_t InputCount() const { return inputs_.Size(); }
945 virtual HInstruction* InputAt(size_t i) const { return inputs_.Get(i); }
946
947 // Runtime needs to walk the stack, so Dex -> Dex calls need to
948 // know their environment.
949 virtual bool NeedsEnvironment() const { return true; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000950
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100951 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100952 SetRawInputAt(index, argument);
953 }
954
955 virtual void SetRawInputAt(size_t index, HInstruction* input) {
956 inputs_.Put(index, input);
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100957 }
958
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100959 virtual Primitive::Type GetType() const { return return_type_; }
960
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +0100961 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000962
963 protected:
964 GrowableArray<HInstruction*> inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100965 const Primitive::Type return_type_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +0100966 const uint32_t dex_pc_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000967
968 private:
969 DISALLOW_COPY_AND_ASSIGN(HInvoke);
970};
971
972class HInvokeStatic : public HInvoke {
973 public:
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100974 HInvokeStatic(ArenaAllocator* arena,
975 uint32_t number_of_arguments,
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100976 Primitive::Type return_type,
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +0100977 uint32_t dex_pc,
978 uint32_t index_in_dex_cache)
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100979 : HInvoke(arena, number_of_arguments, return_type, dex_pc),
980 index_in_dex_cache_(index_in_dex_cache) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000981
982 uint32_t GetIndexInDexCache() const { return index_in_dex_cache_; }
983
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100984 DECLARE_INSTRUCTION(InvokeStatic);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000985
986 private:
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100987 const uint32_t index_in_dex_cache_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000988
989 DISALLOW_COPY_AND_ASSIGN(HInvokeStatic);
990};
991
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +0100992class HNewInstance : public HTemplateInstruction<0> {
993 public:
994 HNewInstance(uint32_t dex_pc, uint16_t type_index) : dex_pc_(dex_pc), type_index_(type_index) {}
995
996 uint32_t GetDexPc() const { return dex_pc_; }
997 uint16_t GetTypeIndex() const { return type_index_; }
998
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100999 virtual Primitive::Type GetType() const { return Primitive::kPrimNot; }
1000
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001001 // Calls runtime so needs an environment.
1002 virtual bool NeedsEnvironment() const { return true; }
1003
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001004 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001005
1006 private:
1007 const uint32_t dex_pc_;
1008 const uint16_t type_index_;
1009
1010 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
1011};
1012
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001013class HAdd : public HBinaryOperation {
1014 public:
1015 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1016 : HBinaryOperation(result_type, left, right) {}
1017
1018 virtual bool IsCommutative() { return true; }
1019
1020 DECLARE_INSTRUCTION(Add);
1021
1022 private:
1023 DISALLOW_COPY_AND_ASSIGN(HAdd);
1024};
1025
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001026class HSub : public HBinaryOperation {
1027 public:
1028 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1029 : HBinaryOperation(result_type, left, right) {}
1030
1031 virtual bool IsCommutative() { return false; }
1032
1033 DECLARE_INSTRUCTION(Sub);
1034
1035 private:
1036 DISALLOW_COPY_AND_ASSIGN(HSub);
1037};
1038
1039// The value of a parameter in this method. Its location depends on
1040// the calling convention.
1041class HParameterValue : public HTemplateInstruction<0> {
1042 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001043 HParameterValue(uint8_t index, Primitive::Type parameter_type)
1044 : index_(index), parameter_type_(parameter_type) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001045
1046 uint8_t GetIndex() const { return index_; }
1047
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001048 virtual Primitive::Type GetType() const { return parameter_type_; }
1049
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001050 DECLARE_INSTRUCTION(ParameterValue);
1051
1052 private:
1053 // The index of this parameter in the parameters list. Must be less
1054 // than HGraph::number_of_in_vregs_;
1055 const uint8_t index_;
1056
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001057 const Primitive::Type parameter_type_;
1058
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001059 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
1060};
1061
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001062class HNot : public HTemplateInstruction<1> {
1063 public:
1064 explicit HNot(HInstruction* input) {
1065 SetRawInputAt(0, input);
1066 }
1067
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001068 virtual Primitive::Type GetType() const { return Primitive::kPrimBoolean; }
1069
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001070 DECLARE_INSTRUCTION(Not);
1071
1072 private:
1073 DISALLOW_COPY_AND_ASSIGN(HNot);
1074};
1075
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001076class HPhi : public HInstruction {
1077 public:
1078 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
1079 : inputs_(arena, number_of_inputs),
1080 reg_number_(reg_number),
1081 type_(type) {
1082 inputs_.SetSize(number_of_inputs);
1083 }
1084
1085 virtual size_t InputCount() const { return inputs_.Size(); }
1086 virtual HInstruction* InputAt(size_t i) const { return inputs_.Get(i); }
1087
1088 virtual void SetRawInputAt(size_t index, HInstruction* input) {
1089 inputs_.Put(index, input);
1090 }
1091
1092 void AddInput(HInstruction* input);
1093
1094 virtual Primitive::Type GetType() const { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001095 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001096
1097 uint32_t GetRegNumber() const { return reg_number_; }
1098
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001099 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001100
1101 protected:
1102 GrowableArray<HInstruction*> inputs_;
1103 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001104 Primitive::Type type_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001105
1106 private:
1107 DISALLOW_COPY_AND_ASSIGN(HPhi);
1108};
1109
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001110class MoveOperands : public ArenaObject {
1111 public:
1112 MoveOperands(Location source, Location destination)
1113 : source_(source), destination_(destination) {}
1114
1115 Location GetSource() const { return source_; }
1116 Location GetDestination() const { return destination_; }
1117
1118 void SetSource(Location value) { source_ = value; }
1119 void SetDestination(Location value) { destination_ = value; }
1120
1121 // The parallel move resolver marks moves as "in-progress" by clearing the
1122 // destination (but not the source).
1123 Location MarkPending() {
1124 DCHECK(!IsPending());
1125 Location dest = destination_;
1126 destination_ = Location::NoLocation();
1127 return dest;
1128 }
1129
1130 void ClearPending(Location dest) {
1131 DCHECK(IsPending());
1132 destination_ = dest;
1133 }
1134
1135 bool IsPending() const {
1136 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
1137 return destination_.IsInvalid() && !source_.IsInvalid();
1138 }
1139
1140 // True if this blocks a move from the given location.
1141 bool Blocks(Location loc) const {
1142 return !IsEliminated() && source_.Equals(loc);
1143 }
1144
1145 // A move is redundant if it's been eliminated, if its source and
1146 // destination are the same, or if its destination is unneeded.
1147 bool IsRedundant() const {
1148 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
1149 }
1150
1151 // We clear both operands to indicate move that's been eliminated.
1152 void Eliminate() {
1153 source_ = destination_ = Location::NoLocation();
1154 }
1155
1156 bool IsEliminated() const {
1157 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
1158 return source_.IsInvalid();
1159 }
1160
1161 private:
1162 Location source_;
1163 Location destination_;
1164
1165 DISALLOW_COPY_AND_ASSIGN(MoveOperands);
1166};
1167
1168static constexpr size_t kDefaultNumberOfMoves = 4;
1169
1170class HParallelMove : public HTemplateInstruction<0> {
1171 public:
1172 explicit HParallelMove(ArenaAllocator* arena) : moves_(arena, kDefaultNumberOfMoves) {}
1173
1174 void AddMove(MoveOperands* move) {
1175 moves_.Add(move);
1176 }
1177
1178 MoveOperands* MoveOperandsAt(size_t index) const {
1179 return moves_.Get(index);
1180 }
1181
1182 size_t NumMoves() const { return moves_.Size(); }
1183
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001184 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001185
1186 private:
1187 GrowableArray<MoveOperands*> moves_;
1188
1189 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
1190};
1191
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001192class HGraphVisitor : public ValueObject {
1193 public:
1194 explicit HGraphVisitor(HGraph* graph) : graph_(graph) { }
1195 virtual ~HGraphVisitor() { }
1196
1197 virtual void VisitInstruction(HInstruction* instruction) { }
1198 virtual void VisitBasicBlock(HBasicBlock* block);
1199
1200 void VisitInsertionOrder();
1201
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001202 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001203
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001204 // Visit functions for instruction classes.
1205#define DECLARE_VISIT_INSTRUCTION(name) \
1206 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
1207
1208 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
1209
1210#undef DECLARE_VISIT_INSTRUCTION
1211
1212 private:
1213 HGraph* graph_;
1214
1215 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
1216};
1217
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001218class HInsertionOrderIterator : public ValueObject {
1219 public:
1220 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
1221
1222 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
1223 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
1224 void Advance() { ++index_; }
1225
1226 private:
1227 const HGraph& graph_;
1228 size_t index_;
1229
1230 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
1231};
1232
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001233class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001234 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001235 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001236
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001237 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
1238 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001239 void Advance() { ++index_; }
1240
1241 private:
1242 const HGraph& graph_;
1243 size_t index_;
1244
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001245 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001246};
1247
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001248class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001249 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001250 explicit HPostOrderIterator(const HGraph& graph)
1251 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001252
1253 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001254 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001255 void Advance() { --index_; }
1256
1257 private:
1258 const HGraph& graph_;
1259 size_t index_;
1260
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001261 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001262};
1263
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001264} // namespace art
1265
1266#endif // ART_COMPILER_OPTIMIZING_NODES_H_