blob: 68848de63620a086e7b4789532f05ff83929ace6 [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 Geoffrayec7e4722014-06-06 11:24:33 +0100286 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
287 size_t successor_index = GetSuccessorIndexOf(existing);
288 DCHECK_NE(successor_index, static_cast<size_t>(-1));
289 existing->RemovePredecessor(this);
290 new_block->predecessors_.Add(this);
291 successors_.Put(successor_index, new_block);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000292 }
293
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100294 void RemovePredecessor(HBasicBlock* block) {
295 predecessors_.Delete(block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100296 }
297
298 void ClearAllPredecessors() {
299 predecessors_.Reset();
300 }
301
302 void AddPredecessor(HBasicBlock* block) {
303 predecessors_.Add(block);
304 block->successors_.Add(this);
305 }
306
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100307 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) {
308 for (size_t i = 0, e = predecessors_.Size(); i < e; ++i) {
309 if (predecessors_.Get(i) == predecessor) {
310 return i;
311 }
312 }
313 return -1;
314 }
315
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100316 size_t GetSuccessorIndexOf(HBasicBlock* successor) {
317 for (size_t i = 0, e = successors_.Size(); i < e; ++i) {
318 if (successors_.Get(i) == successor) {
319 return i;
320 }
321 }
322 return -1;
323 }
324
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000325 void AddInstruction(HInstruction* instruction);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100326 void RemoveInstruction(HInstruction* instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100327 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100328 void AddPhi(HPhi* phi);
329 void RemovePhi(HPhi* phi);
330
331 bool IsLoopHeader() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100332 return (loop_information_ != nullptr) && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100333 }
334
335 HLoopInformation* GetLoopInformation() const {
336 return loop_information_;
337 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000338
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100339 // Set the loop_information_ on this block. This method overrides the current
340 // loop_information if it is an outer loop of the passed loop information.
341 void SetInLoop(HLoopInformation* info) {
342 if (IsLoopHeader()) {
343 // Nothing to do. This just means `info` is an outer loop.
344 } else if (loop_information_ == nullptr) {
345 loop_information_ = info;
346 } else if (loop_information_->Contains(*info->GetHeader())) {
347 // Block is currently part of an outer loop. Make it part of this inner loop.
348 // Note that a non loop header having a loop information means this loop information
349 // has already been populated
350 loop_information_ = info;
351 } else {
352 // Block is part of an inner loop. Do not update the loop information.
353 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
354 // at this point, because this method is being called while populating `info`.
355 }
356 }
357
358 // Returns wheter this block dominates the blocked passed as parameter.
359 bool Dominates(HBasicBlock* block) const;
360
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100361 size_t GetLifetimeStart() const { return lifetime_start_; }
362 size_t GetLifetimeEnd() const { return lifetime_end_; }
363
364 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
365 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
366
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000367 private:
368 HGraph* const graph_;
369 GrowableArray<HBasicBlock*> predecessors_;
370 GrowableArray<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100371 HInstructionList instructions_;
372 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000373 HLoopInformation* loop_information_;
374 HBasicBlock* dominator_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000375 int block_id_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100376 size_t lifetime_start_;
377 size_t lifetime_end_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000378
379 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
380};
381
382#define FOR_EACH_INSTRUCTION(M) \
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000383 M(Add) \
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000384 M(Equal) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000385 M(Exit) \
386 M(Goto) \
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000387 M(If) \
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000388 M(IntConstant) \
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000389 M(InvokeStatic) \
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000390 M(LoadLocal) \
391 M(Local) \
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100392 M(LongConstant) \
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +0100393 M(NewInstance) \
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100394 M(Not) \
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100395 M(ParameterValue) \
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100396 M(ParallelMove) \
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100397 M(Phi) \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000398 M(Return) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000399 M(ReturnVoid) \
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000400 M(StoreLocal) \
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100401 M(Sub) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000402
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000403#define FORWARD_DECLARATION(type) class H##type;
404FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
405#undef FORWARD_DECLARATION
406
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000407#define DECLARE_INSTRUCTION(type) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000408 virtual const char* DebugName() const { return #type; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000409 virtual H##type* As##type() { return this; } \
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100410 virtual void Accept(HGraphVisitor* visitor) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000411
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100412template <typename T>
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000413class HUseListNode : public ArenaObject {
414 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100415 HUseListNode(T* user, size_t index, HUseListNode* tail)
416 : user_(user), index_(index), tail_(tail) { }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000417
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000418 HUseListNode* GetTail() const { return tail_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100419 T* GetUser() const { return user_; }
420 size_t GetIndex() const { return index_; }
421
422 void SetTail(HUseListNode<T>* node) { tail_ = node; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000423
424 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100425 T* const user_;
426 const size_t index_;
427 HUseListNode<T>* tail_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000428
429 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
430};
431
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000432class HInstruction : public ArenaObject {
433 public:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000434 HInstruction()
435 : previous_(nullptr),
436 next_(nullptr),
437 block_(nullptr),
438 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100439 ssa_index_(-1),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000440 uses_(nullptr),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100441 env_uses_(nullptr),
442 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100443 locations_(nullptr),
444 live_interval_(nullptr),
445 lifetime_position_(kNoLifetime) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000446
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000447 virtual ~HInstruction() { }
448
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000449 HInstruction* GetNext() const { return next_; }
450 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000451
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000452 HBasicBlock* GetBlock() const { return block_; }
453 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000454
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100455 virtual size_t InputCount() const = 0;
456 virtual HInstruction* InputAt(size_t i) const = 0;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000457
458 virtual void Accept(HGraphVisitor* visitor) = 0;
459 virtual const char* DebugName() const = 0;
460
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100461 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100462 virtual void SetRawInputAt(size_t index, HInstruction* input) = 0;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100463
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100464 virtual bool NeedsEnvironment() const { return false; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100465 virtual bool IsControlFlow() const { return false; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100466
467 void AddUseAt(HInstruction* user, size_t index) {
468 uses_ = new (block_->GetGraph()->GetArena()) HUseListNode<HInstruction>(user, index, uses_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000469 }
470
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100471 void AddEnvUseAt(HEnvironment* user, size_t index) {
472 env_uses_ = new (block_->GetGraph()->GetArena()) HUseListNode<HEnvironment>(
473 user, index, env_uses_);
474 }
475
476 void RemoveUser(HInstruction* user, size_t index);
477
478 HUseListNode<HInstruction>* GetUses() const { return uses_; }
479 HUseListNode<HEnvironment>* GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000480
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100481 bool HasUses() const { return uses_ != nullptr || env_uses_ != nullptr; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000482
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100483 size_t NumberOfUses() const {
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100484 // TODO: Optimize this method if it is used outside of the HGraphVisualizer.
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100485 size_t result = 0;
486 HUseListNode<HInstruction>* current = uses_;
487 while (current != nullptr) {
488 current = current->GetTail();
489 ++result;
490 }
491 return result;
492 }
493
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000494 int GetId() const { return id_; }
495 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000496
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100497 int GetSsaIndex() const { return ssa_index_; }
498 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
499 bool HasSsaIndex() const { return ssa_index_ != -1; }
500
501 bool HasEnvironment() const { return environment_ != nullptr; }
502 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100503 void SetEnvironment(HEnvironment* environment) { environment_ = environment; }
504
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000505 LocationSummary* GetLocations() const { return locations_; }
506 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000507
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100508 void ReplaceWith(HInstruction* instruction);
509
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000510#define INSTRUCTION_TYPE_CHECK(type) \
511 virtual H##type* As##type() { return nullptr; }
512
513 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
514#undef INSTRUCTION_TYPE_CHECK
515
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100516 size_t GetLifetimePosition() const { return lifetime_position_; }
517 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
518 LiveInterval* GetLiveInterval() const { return live_interval_; }
519 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
520 bool HasLiveInterval() const { return live_interval_ != nullptr; }
521
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000522 private:
523 HInstruction* previous_;
524 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000525 HBasicBlock* block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000526
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000527 // An instruction gets an id when it is added to the graph.
528 // It reflects creation order. A negative id means the instruction
529 // has not beed added to the graph.
530 int id_;
531
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100532 // When doing liveness analysis, instructions that have uses get an SSA index.
533 int ssa_index_;
534
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100535 // List of instructions that have this instruction as input.
536 HUseListNode<HInstruction>* uses_;
537
538 // List of environments that contain this instruction.
539 HUseListNode<HEnvironment>* env_uses_;
540
541 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000542
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000543 // Set by the code generator.
544 LocationSummary* locations_;
545
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100546 // Set by the liveness analysis.
547 LiveInterval* live_interval_;
548
549 // Set by the liveness analysis, this is the position in a linear
550 // order of blocks where this instruction's live interval start.
551 size_t lifetime_position_;
552
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000553 friend class HBasicBlock;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100554 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000555
556 DISALLOW_COPY_AND_ASSIGN(HInstruction);
557};
558
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100559template<typename T>
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000560class HUseIterator : public ValueObject {
561 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100562 explicit HUseIterator(HUseListNode<T>* uses) : current_(uses) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000563
564 bool Done() const { return current_ == nullptr; }
565
566 void Advance() {
567 DCHECK(!Done());
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000568 current_ = current_->GetTail();
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000569 }
570
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100571 HUseListNode<T>* Current() const {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000572 DCHECK(!Done());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100573 return current_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000574 }
575
576 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100577 HUseListNode<T>* current_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000578
579 friend class HValue;
580};
581
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100582// A HEnvironment object contains the values of virtual registers at a given location.
583class HEnvironment : public ArenaObject {
584 public:
585 HEnvironment(ArenaAllocator* arena, size_t number_of_vregs) : vregs_(arena, number_of_vregs) {
586 vregs_.SetSize(number_of_vregs);
587 for (size_t i = 0; i < number_of_vregs; i++) {
588 vregs_.Put(i, nullptr);
589 }
590 }
591
592 void Populate(const GrowableArray<HInstruction*>& env) {
593 for (size_t i = 0; i < env.Size(); i++) {
594 HInstruction* instruction = env.Get(i);
595 vregs_.Put(i, instruction);
596 if (instruction != nullptr) {
597 instruction->AddEnvUseAt(this, i);
598 }
599 }
600 }
601
602 void SetRawEnvAt(size_t index, HInstruction* instruction) {
603 vregs_.Put(index, instruction);
604 }
605
606 GrowableArray<HInstruction*>* GetVRegs() {
607 return &vregs_;
608 }
609
610 private:
611 GrowableArray<HInstruction*> vregs_;
612
613 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
614};
615
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000616class HInputIterator : public ValueObject {
617 public:
618 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) { }
619
620 bool Done() const { return index_ == instruction_->InputCount(); }
621 HInstruction* Current() const { return instruction_->InputAt(index_); }
622 void Advance() { index_++; }
623
624 private:
625 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100626 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000627
628 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
629};
630
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000631class HInstructionIterator : public ValueObject {
632 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100633 explicit HInstructionIterator(const HInstructionList& instructions)
634 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000635 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000636 }
637
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000638 bool Done() const { return instruction_ == nullptr; }
639 HInstruction* Current() const { return instruction_; }
640 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000641 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000642 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000643 }
644
645 private:
646 HInstruction* instruction_;
647 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100648
649 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000650};
651
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100652class HBackwardInstructionIterator : public ValueObject {
653 public:
654 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
655 : instruction_(instructions.last_instruction_) {
656 next_ = Done() ? nullptr : instruction_->GetPrevious();
657 }
658
659 bool Done() const { return instruction_ == nullptr; }
660 HInstruction* Current() const { return instruction_; }
661 void Advance() {
662 instruction_ = next_;
663 next_ = Done() ? nullptr : instruction_->GetPrevious();
664 }
665
666 private:
667 HInstruction* instruction_;
668 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100669
670 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100671};
672
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000673// An embedded container with N elements of type T. Used (with partial
674// specialization for N=0) because embedded arrays cannot have size 0.
675template<typename T, intptr_t N>
676class EmbeddedArray {
677 public:
678 EmbeddedArray() : elements_() { }
679
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000680 intptr_t GetLength() const { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000681
682 const T& operator[](intptr_t i) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000683 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000684 return elements_[i];
685 }
686
687 T& operator[](intptr_t i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000688 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000689 return elements_[i];
690 }
691
692 const T& At(intptr_t i) const {
693 return (*this)[i];
694 }
695
696 void SetAt(intptr_t i, const T& val) {
697 (*this)[i] = val;
698 }
699
700 private:
701 T elements_[N];
702};
703
704template<typename T>
705class EmbeddedArray<T, 0> {
706 public:
707 intptr_t length() const { return 0; }
708 const T& operator[](intptr_t i) const {
709 LOG(FATAL) << "Unreachable";
710 static T sentinel = 0;
711 return sentinel;
712 }
713 T& operator[](intptr_t i) {
714 LOG(FATAL) << "Unreachable";
715 static T sentinel = 0;
716 return sentinel;
717 }
718};
719
720template<intptr_t N>
721class HTemplateInstruction: public HInstruction {
722 public:
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000723 HTemplateInstruction<N>() : inputs_() { }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000724 virtual ~HTemplateInstruction() { }
725
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100726 virtual size_t InputCount() const { return N; }
727 virtual HInstruction* InputAt(size_t i) const { return inputs_[i]; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000728
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000729 protected:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100730 virtual void SetRawInputAt(size_t i, HInstruction* instruction) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000731 inputs_[i] = instruction;
732 }
733
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000734 private:
735 EmbeddedArray<HInstruction*, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100736
737 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000738};
739
740// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
741// instruction that branches to the exit block.
742class HReturnVoid : public HTemplateInstruction<0> {
743 public:
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100744 HReturnVoid() {}
745
746 virtual bool IsControlFlow() const { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000747
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100748 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000749
750 private:
751 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
752};
753
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000754// Represents dex's RETURN opcodes. A HReturn is a control flow
755// instruction that branches to the exit block.
756class HReturn : public HTemplateInstruction<1> {
757 public:
758 explicit HReturn(HInstruction* value) {
759 SetRawInputAt(0, value);
760 }
761
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100762 virtual bool IsControlFlow() const { return true; }
763
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100764 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000765
766 private:
767 DISALLOW_COPY_AND_ASSIGN(HReturn);
768};
769
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000770// The exit instruction is the only instruction of the exit block.
771// Instructions aborting the method (HTrow and HReturn) must branch to the
772// exit block.
773class HExit : public HTemplateInstruction<0> {
774 public:
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100775 HExit() {}
776
777 virtual bool IsControlFlow() const { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000778
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100779 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000780
781 private:
782 DISALLOW_COPY_AND_ASSIGN(HExit);
783};
784
785// Jumps from one block to another.
786class HGoto : public HTemplateInstruction<0> {
787 public:
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100788 HGoto() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000789
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000790 HBasicBlock* GetSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100791 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000792 }
793
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100794 virtual bool IsControlFlow() const { return true; }
795
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100796 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000797
798 private:
799 DISALLOW_COPY_AND_ASSIGN(HGoto);
800};
801
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000802// Conditional branch. A block ending with an HIf instruction must have
803// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000804class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000805 public:
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000806 explicit HIf(HInstruction* input) {
807 SetRawInputAt(0, input);
808 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000809
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000810 HBasicBlock* IfTrueSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100811 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000812 }
813
814 HBasicBlock* IfFalseSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100815 return GetBlock()->GetSuccessors().Get(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000816 }
817
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100818 virtual bool IsControlFlow() const { return true; }
819
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100820 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000821
822 private:
823 DISALLOW_COPY_AND_ASSIGN(HIf);
824};
825
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000826class HBinaryOperation : public HTemplateInstruction<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000827 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000828 HBinaryOperation(Primitive::Type result_type,
829 HInstruction* left,
830 HInstruction* right) : result_type_(result_type) {
831 SetRawInputAt(0, left);
832 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000833 }
834
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000835 HInstruction* GetLeft() const { return InputAt(0); }
836 HInstruction* GetRight() const { return InputAt(1); }
837 Primitive::Type GetResultType() const { return result_type_; }
838
839 virtual bool IsCommutative() { return false; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100840 virtual Primitive::Type GetType() const { return GetResultType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000841
842 private:
843 const Primitive::Type result_type_;
844
845 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
846};
847
848
849// Instruction to check if two inputs are equal to each other.
850class HEqual : public HBinaryOperation {
851 public:
852 HEqual(HInstruction* first, HInstruction* second)
853 : HBinaryOperation(Primitive::kPrimBoolean, first, second) {}
854
855 virtual bool IsCommutative() { return true; }
856
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100857 virtual Primitive::Type GetType() const { return Primitive::kPrimBoolean; }
858
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100859 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000860
861 private:
862 DISALLOW_COPY_AND_ASSIGN(HEqual);
863};
864
865// A local in the graph. Corresponds to a Dex register.
866class HLocal : public HTemplateInstruction<0> {
867 public:
868 explicit HLocal(uint16_t reg_number) : reg_number_(reg_number) { }
869
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100870 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000871
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000872 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000873
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000874 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000875 // The Dex register number.
876 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000877
878 DISALLOW_COPY_AND_ASSIGN(HLocal);
879};
880
881// Load a given local. The local is an input of this instruction.
882class HLoadLocal : public HTemplateInstruction<1> {
883 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100884 explicit HLoadLocal(HLocal* local, Primitive::Type type) : type_(type) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000885 SetRawInputAt(0, local);
886 }
887
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100888 virtual Primitive::Type GetType() const { return type_; }
889
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000890 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
891
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100892 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000893
894 private:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100895 const Primitive::Type type_;
896
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000897 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
898};
899
900// Store a value in a given local. This instruction has two inputs: the value
901// and the local.
902class HStoreLocal : public HTemplateInstruction<2> {
903 public:
904 HStoreLocal(HLocal* local, HInstruction* value) {
905 SetRawInputAt(0, local);
906 SetRawInputAt(1, value);
907 }
908
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000909 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
910
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100911 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000912
913 private:
914 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
915};
916
917// Constants of the type int. Those can be from Dex instructions, or
918// synthesized (for example with the if-eqz instruction).
919class HIntConstant : public HTemplateInstruction<0> {
920 public:
921 explicit HIntConstant(int32_t value) : value_(value) { }
922
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000923 int32_t GetValue() const { return value_; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100924 virtual Primitive::Type GetType() const { return Primitive::kPrimInt; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000925
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100926 DECLARE_INSTRUCTION(IntConstant);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000927
928 private:
929 const int32_t value_;
930
931 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
932};
933
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100934class HLongConstant : public HTemplateInstruction<0> {
935 public:
936 explicit HLongConstant(int64_t value) : value_(value) { }
937
938 int64_t GetValue() const { return value_; }
939
940 virtual Primitive::Type GetType() const { return Primitive::kPrimLong; }
941
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100942 DECLARE_INSTRUCTION(LongConstant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100943
944 private:
945 const int64_t value_;
946
947 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
948};
949
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000950class HInvoke : public HInstruction {
951 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100952 HInvoke(ArenaAllocator* arena,
953 uint32_t number_of_arguments,
954 Primitive::Type return_type,
955 uint32_t dex_pc)
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000956 : inputs_(arena, number_of_arguments),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100957 return_type_(return_type),
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000958 dex_pc_(dex_pc) {
959 inputs_.SetSize(number_of_arguments);
960 }
961
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100962 virtual size_t InputCount() const { return inputs_.Size(); }
963 virtual HInstruction* InputAt(size_t i) const { return inputs_.Get(i); }
964
965 // Runtime needs to walk the stack, so Dex -> Dex calls need to
966 // know their environment.
967 virtual bool NeedsEnvironment() const { return true; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000968
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100969 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100970 SetRawInputAt(index, argument);
971 }
972
973 virtual void SetRawInputAt(size_t index, HInstruction* input) {
974 inputs_.Put(index, input);
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100975 }
976
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100977 virtual Primitive::Type GetType() const { return return_type_; }
978
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +0100979 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000980
981 protected:
982 GrowableArray<HInstruction*> inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100983 const Primitive::Type return_type_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +0100984 const uint32_t dex_pc_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000985
986 private:
987 DISALLOW_COPY_AND_ASSIGN(HInvoke);
988};
989
990class HInvokeStatic : public HInvoke {
991 public:
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100992 HInvokeStatic(ArenaAllocator* arena,
993 uint32_t number_of_arguments,
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100994 Primitive::Type return_type,
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +0100995 uint32_t dex_pc,
996 uint32_t index_in_dex_cache)
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100997 : HInvoke(arena, number_of_arguments, return_type, dex_pc),
998 index_in_dex_cache_(index_in_dex_cache) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000999
1000 uint32_t GetIndexInDexCache() const { return index_in_dex_cache_; }
1001
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001002 DECLARE_INSTRUCTION(InvokeStatic);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001003
1004 private:
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001005 const uint32_t index_in_dex_cache_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001006
1007 DISALLOW_COPY_AND_ASSIGN(HInvokeStatic);
1008};
1009
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001010class HNewInstance : public HTemplateInstruction<0> {
1011 public:
1012 HNewInstance(uint32_t dex_pc, uint16_t type_index) : dex_pc_(dex_pc), type_index_(type_index) {}
1013
1014 uint32_t GetDexPc() const { return dex_pc_; }
1015 uint16_t GetTypeIndex() const { return type_index_; }
1016
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001017 virtual Primitive::Type GetType() const { return Primitive::kPrimNot; }
1018
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001019 // Calls runtime so needs an environment.
1020 virtual bool NeedsEnvironment() const { return true; }
1021
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001022 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001023
1024 private:
1025 const uint32_t dex_pc_;
1026 const uint16_t type_index_;
1027
1028 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
1029};
1030
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001031class HAdd : public HBinaryOperation {
1032 public:
1033 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1034 : HBinaryOperation(result_type, left, right) {}
1035
1036 virtual bool IsCommutative() { return true; }
1037
1038 DECLARE_INSTRUCTION(Add);
1039
1040 private:
1041 DISALLOW_COPY_AND_ASSIGN(HAdd);
1042};
1043
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001044class HSub : public HBinaryOperation {
1045 public:
1046 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1047 : HBinaryOperation(result_type, left, right) {}
1048
1049 virtual bool IsCommutative() { return false; }
1050
1051 DECLARE_INSTRUCTION(Sub);
1052
1053 private:
1054 DISALLOW_COPY_AND_ASSIGN(HSub);
1055};
1056
1057// The value of a parameter in this method. Its location depends on
1058// the calling convention.
1059class HParameterValue : public HTemplateInstruction<0> {
1060 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001061 HParameterValue(uint8_t index, Primitive::Type parameter_type)
1062 : index_(index), parameter_type_(parameter_type) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001063
1064 uint8_t GetIndex() const { return index_; }
1065
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001066 virtual Primitive::Type GetType() const { return parameter_type_; }
1067
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001068 DECLARE_INSTRUCTION(ParameterValue);
1069
1070 private:
1071 // The index of this parameter in the parameters list. Must be less
1072 // than HGraph::number_of_in_vregs_;
1073 const uint8_t index_;
1074
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001075 const Primitive::Type parameter_type_;
1076
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001077 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
1078};
1079
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001080class HNot : public HTemplateInstruction<1> {
1081 public:
1082 explicit HNot(HInstruction* input) {
1083 SetRawInputAt(0, input);
1084 }
1085
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001086 virtual Primitive::Type GetType() const { return Primitive::kPrimBoolean; }
1087
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001088 DECLARE_INSTRUCTION(Not);
1089
1090 private:
1091 DISALLOW_COPY_AND_ASSIGN(HNot);
1092};
1093
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001094class HPhi : public HInstruction {
1095 public:
1096 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
1097 : inputs_(arena, number_of_inputs),
1098 reg_number_(reg_number),
1099 type_(type) {
1100 inputs_.SetSize(number_of_inputs);
1101 }
1102
1103 virtual size_t InputCount() const { return inputs_.Size(); }
1104 virtual HInstruction* InputAt(size_t i) const { return inputs_.Get(i); }
1105
1106 virtual void SetRawInputAt(size_t index, HInstruction* input) {
1107 inputs_.Put(index, input);
1108 }
1109
1110 void AddInput(HInstruction* input);
1111
1112 virtual Primitive::Type GetType() const { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001113 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001114
1115 uint32_t GetRegNumber() const { return reg_number_; }
1116
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001117 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001118
1119 protected:
1120 GrowableArray<HInstruction*> inputs_;
1121 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001122 Primitive::Type type_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001123
1124 private:
1125 DISALLOW_COPY_AND_ASSIGN(HPhi);
1126};
1127
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001128class MoveOperands : public ArenaObject {
1129 public:
1130 MoveOperands(Location source, Location destination)
1131 : source_(source), destination_(destination) {}
1132
1133 Location GetSource() const { return source_; }
1134 Location GetDestination() const { return destination_; }
1135
1136 void SetSource(Location value) { source_ = value; }
1137 void SetDestination(Location value) { destination_ = value; }
1138
1139 // The parallel move resolver marks moves as "in-progress" by clearing the
1140 // destination (but not the source).
1141 Location MarkPending() {
1142 DCHECK(!IsPending());
1143 Location dest = destination_;
1144 destination_ = Location::NoLocation();
1145 return dest;
1146 }
1147
1148 void ClearPending(Location dest) {
1149 DCHECK(IsPending());
1150 destination_ = dest;
1151 }
1152
1153 bool IsPending() const {
1154 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
1155 return destination_.IsInvalid() && !source_.IsInvalid();
1156 }
1157
1158 // True if this blocks a move from the given location.
1159 bool Blocks(Location loc) const {
1160 return !IsEliminated() && source_.Equals(loc);
1161 }
1162
1163 // A move is redundant if it's been eliminated, if its source and
1164 // destination are the same, or if its destination is unneeded.
1165 bool IsRedundant() const {
1166 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
1167 }
1168
1169 // We clear both operands to indicate move that's been eliminated.
1170 void Eliminate() {
1171 source_ = destination_ = Location::NoLocation();
1172 }
1173
1174 bool IsEliminated() const {
1175 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
1176 return source_.IsInvalid();
1177 }
1178
1179 private:
1180 Location source_;
1181 Location destination_;
1182
1183 DISALLOW_COPY_AND_ASSIGN(MoveOperands);
1184};
1185
1186static constexpr size_t kDefaultNumberOfMoves = 4;
1187
1188class HParallelMove : public HTemplateInstruction<0> {
1189 public:
1190 explicit HParallelMove(ArenaAllocator* arena) : moves_(arena, kDefaultNumberOfMoves) {}
1191
1192 void AddMove(MoveOperands* move) {
1193 moves_.Add(move);
1194 }
1195
1196 MoveOperands* MoveOperandsAt(size_t index) const {
1197 return moves_.Get(index);
1198 }
1199
1200 size_t NumMoves() const { return moves_.Size(); }
1201
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001202 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001203
1204 private:
1205 GrowableArray<MoveOperands*> moves_;
1206
1207 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
1208};
1209
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001210class HGraphVisitor : public ValueObject {
1211 public:
1212 explicit HGraphVisitor(HGraph* graph) : graph_(graph) { }
1213 virtual ~HGraphVisitor() { }
1214
1215 virtual void VisitInstruction(HInstruction* instruction) { }
1216 virtual void VisitBasicBlock(HBasicBlock* block);
1217
1218 void VisitInsertionOrder();
1219
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001220 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001221
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001222 // Visit functions for instruction classes.
1223#define DECLARE_VISIT_INSTRUCTION(name) \
1224 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
1225
1226 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
1227
1228#undef DECLARE_VISIT_INSTRUCTION
1229
1230 private:
1231 HGraph* graph_;
1232
1233 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
1234};
1235
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001236class HInsertionOrderIterator : public ValueObject {
1237 public:
1238 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
1239
1240 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
1241 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
1242 void Advance() { ++index_; }
1243
1244 private:
1245 const HGraph& graph_;
1246 size_t index_;
1247
1248 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
1249};
1250
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001251class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001252 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001253 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001254
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001255 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
1256 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001257 void Advance() { ++index_; }
1258
1259 private:
1260 const HGraph& graph_;
1261 size_t index_;
1262
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001263 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001264};
1265
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001266class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001267 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001268 explicit HPostOrderIterator(const HGraph& graph)
1269 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001270
1271 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001272 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001273 void Advance() { --index_; }
1274
1275 private:
1276 const HGraph& graph_;
1277 size_t index_;
1278
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001279 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001280};
1281
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001282} // namespace art
1283
1284#endif // ART_COMPILER_OPTIMIZING_NODES_H_