blob: eebd64bbfc85ee10cd294b219554ad29a95e1ea8 [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 Geoffraye5038322014-07-04 09:41:32 +010021#include "offsets.h"
Ian Rogerse63db272014-07-15 15:36:11 -070022#include "primitive.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000023#include "utils/allocation.h"
Nicolas Geoffray0e336432014-02-26 18:24:38 +000024#include "utils/arena_bit_vector.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000025#include "utils/growable_array.h"
26
27namespace art {
28
29class HBasicBlock;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010030class HEnvironment;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000031class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000032class HIntConstant;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000033class HGraphVisitor;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010034class HPhi;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010035class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000036class LocationSummary;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000037
38static const int kDefaultNumberOfBlocks = 8;
39static const int kDefaultNumberOfSuccessors = 2;
40static const int kDefaultNumberOfPredecessors = 2;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000041static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000042
Dave Allison20dfc792014-06-16 20:44:29 -070043enum IfCondition {
44 kCondEQ,
45 kCondNE,
46 kCondLT,
47 kCondLE,
48 kCondGT,
49 kCondGE,
50};
51
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010052class HInstructionList {
53 public:
54 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
55
56 void AddInstruction(HInstruction* instruction);
57 void RemoveInstruction(HInstruction* instruction);
58
Roland Levillainccc07a92014-09-16 14:48:16 +010059 // Return true if `instruction1` is found before `instruction2` in
60 // this instruction list and false otherwise. Abort if none
61 // of these instructions is found.
62 bool FoundBefore(const HInstruction* instruction1,
63 const HInstruction* instruction2) const;
64
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010065 private:
66 HInstruction* first_instruction_;
67 HInstruction* last_instruction_;
68
69 friend class HBasicBlock;
70 friend class HInstructionIterator;
Nicolas Geoffray804d0932014-05-02 08:46:00 +010071 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010072
73 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
74};
75
Nicolas Geoffray818f2102014-02-18 16:43:35 +000076// Control-flow graph of a method. Contains a list of basic blocks.
77class HGraph : public ArenaObject {
78 public:
79 explicit HGraph(ArenaAllocator* arena)
80 : arena_(arena),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000081 blocks_(arena, kDefaultNumberOfBlocks),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +010082 reverse_post_order_(arena, kDefaultNumberOfBlocks),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +010083 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +010084 number_of_vregs_(0),
85 number_of_in_vregs_(0),
Nicolas Geoffraye5038322014-07-04 09:41:32 +010086 number_of_temporaries_(0),
Dave Allison20dfc792014-06-16 20:44:29 -070087 current_instruction_id_(0) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +000088
Nicolas Geoffray787c3072014-03-17 10:20:19 +000089 ArenaAllocator* GetArena() const { return arena_; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +010090 const GrowableArray<HBasicBlock*>& GetBlocks() const { return blocks_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +000091
Nicolas Geoffray787c3072014-03-17 10:20:19 +000092 HBasicBlock* GetEntryBlock() const { return entry_block_; }
93 HBasicBlock* GetExitBlock() const { return exit_block_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000094
Nicolas Geoffray787c3072014-03-17 10:20:19 +000095 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
96 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000097
Nicolas Geoffray818f2102014-02-18 16:43:35 +000098 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010099
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000100 void BuildDominatorTree();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100101 void TransformToSSA();
102 void SimplifyCFG();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000103
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100104 // Find all natural loops in this graph. Aborts computation and returns false
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100105 // if one loop is not natural, that is the header does not dominate the back
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100106 // edge.
107 bool FindNaturalLoops() const;
108
109 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
110 void SimplifyLoop(HBasicBlock* header);
111
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000112 int GetNextInstructionId() {
113 return current_instruction_id_++;
114 }
115
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100116 uint16_t GetMaximumNumberOfOutVRegs() const {
117 return maximum_number_of_out_vregs_;
118 }
119
120 void UpdateMaximumNumberOfOutVRegs(uint16_t new_value) {
121 maximum_number_of_out_vregs_ = std::max(new_value, maximum_number_of_out_vregs_);
122 }
123
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100124 void UpdateNumberOfTemporaries(size_t count) {
125 number_of_temporaries_ = std::max(count, number_of_temporaries_);
126 }
127
128 size_t GetNumberOfTemporaries() const {
129 return number_of_temporaries_;
130 }
131
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100132 void SetNumberOfVRegs(uint16_t number_of_vregs) {
133 number_of_vregs_ = number_of_vregs;
134 }
135
136 uint16_t GetNumberOfVRegs() const {
137 return number_of_vregs_;
138 }
139
140 void SetNumberOfInVRegs(uint16_t value) {
141 number_of_in_vregs_ = value;
142 }
143
144 uint16_t GetNumberOfInVRegs() const {
145 return number_of_in_vregs_;
146 }
147
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100148 uint16_t GetNumberOfLocalVRegs() const {
149 return number_of_vregs_ - number_of_in_vregs_;
150 }
151
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100152 const GrowableArray<HBasicBlock*>& GetReversePostOrder() const {
153 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100154 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100155
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000156 private:
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000157 HBasicBlock* FindCommonDominator(HBasicBlock* first, HBasicBlock* second) const;
158 void VisitBlockForDominatorTree(HBasicBlock* block,
159 HBasicBlock* predecessor,
160 GrowableArray<size_t>* visits);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100161 void FindBackEdges(ArenaBitVector* visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000162 void VisitBlockForBackEdges(HBasicBlock* block,
163 ArenaBitVector* visited,
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100164 ArenaBitVector* visiting);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000165 void RemoveDeadBlocks(const ArenaBitVector& visited) const;
166
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000167 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000168
169 // List of blocks in insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000170 GrowableArray<HBasicBlock*> blocks_;
171
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100172 // List of blocks to perform a reverse post order tree traversal.
173 GrowableArray<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000174
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000175 HBasicBlock* entry_block_;
176 HBasicBlock* exit_block_;
177
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100178 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100179 uint16_t maximum_number_of_out_vregs_;
180
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100181 // The number of virtual registers in this method. Contains the parameters.
182 uint16_t number_of_vregs_;
183
184 // The number of virtual registers used by parameters of this method.
185 uint16_t number_of_in_vregs_;
186
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100187 // The number of temporaries that will be needed for the baseline compiler.
188 size_t number_of_temporaries_;
189
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000190 // The current id to assign to a newly added instruction. See HInstruction.id_.
191 int current_instruction_id_;
192
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000193 DISALLOW_COPY_AND_ASSIGN(HGraph);
194};
195
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000196class HLoopInformation : public ArenaObject {
197 public:
198 HLoopInformation(HBasicBlock* header, HGraph* graph)
199 : header_(header),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100200 back_edges_(graph->GetArena(), kDefaultNumberOfBackEdges),
201 blocks_(graph->GetArena(), graph->GetBlocks().Size(), false) {}
202
203 HBasicBlock* GetHeader() const {
204 return header_;
205 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000206
207 void AddBackEdge(HBasicBlock* back_edge) {
208 back_edges_.Add(back_edge);
209 }
210
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100211 void RemoveBackEdge(HBasicBlock* back_edge) {
212 back_edges_.Delete(back_edge);
213 }
214
215 bool IsBackEdge(HBasicBlock* block) {
216 for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) {
217 if (back_edges_.Get(i) == block) return true;
218 }
219 return false;
220 }
221
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000222 int NumberOfBackEdges() const {
223 return back_edges_.Size();
224 }
225
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100226 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100227
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100228 const GrowableArray<HBasicBlock*>& GetBackEdges() const {
229 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100230 }
231
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100232 void ClearBackEdges() {
233 back_edges_.Reset();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100234 }
235
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100236 // Find blocks that are part of this loop. Returns whether the loop is a natural loop,
237 // that is the header dominates the back edge.
238 bool Populate();
239
240 // Returns whether this loop information contains `block`.
241 // Note that this loop information *must* be populated before entering this function.
242 bool Contains(const HBasicBlock& block) const;
243
244 // Returns whether this loop information is an inner loop of `other`.
245 // Note that `other` *must* be populated before entering this function.
246 bool IsIn(const HLoopInformation& other) const;
247
248 const ArenaBitVector& GetBlocks() const { return blocks_; }
249
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000250 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100251 // Internal recursive implementation of `Populate`.
252 void PopulateRecursive(HBasicBlock* block);
253
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000254 HBasicBlock* header_;
255 GrowableArray<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100256 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000257
258 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
259};
260
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100261static constexpr size_t kNoLifetime = -1;
262
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000263// A block in a method. Contains the list of instructions represented
264// as a double linked list. Each block knows its predecessors and
265// successors.
266class HBasicBlock : public ArenaObject {
267 public:
268 explicit HBasicBlock(HGraph* graph)
269 : graph_(graph),
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000270 predecessors_(graph->GetArena(), kDefaultNumberOfPredecessors),
271 successors_(graph->GetArena(), kDefaultNumberOfSuccessors),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000272 loop_information_(nullptr),
273 dominator_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100274 block_id_(-1),
275 lifetime_start_(kNoLifetime),
276 lifetime_end_(kNoLifetime) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000277
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100278 const GrowableArray<HBasicBlock*>& GetPredecessors() const {
279 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000280 }
281
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100282 const GrowableArray<HBasicBlock*>& GetSuccessors() const {
283 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000284 }
285
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000286 void AddBackEdge(HBasicBlock* back_edge) {
287 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000288 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000289 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100290 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000291 loop_information_->AddBackEdge(back_edge);
292 }
293
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000294 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000295
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000296 int GetBlockId() const { return block_id_; }
297 void SetBlockId(int id) { block_id_ = id; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000298
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000299 HBasicBlock* GetDominator() const { return dominator_; }
300 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000301
302 int NumberOfBackEdges() const {
303 return loop_information_ == nullptr
304 ? 0
305 : loop_information_->NumberOfBackEdges();
306 }
307
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100308 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
309 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100310 const HInstructionList& GetInstructions() const { return instructions_; }
311 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100312 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000313
314 void AddSuccessor(HBasicBlock* block) {
315 successors_.Add(block);
316 block->predecessors_.Add(this);
317 }
318
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100319 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
320 size_t successor_index = GetSuccessorIndexOf(existing);
321 DCHECK_NE(successor_index, static_cast<size_t>(-1));
322 existing->RemovePredecessor(this);
323 new_block->predecessors_.Add(this);
324 successors_.Put(successor_index, new_block);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000325 }
326
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100327 void RemovePredecessor(HBasicBlock* block) {
328 predecessors_.Delete(block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100329 }
330
331 void ClearAllPredecessors() {
332 predecessors_.Reset();
333 }
334
335 void AddPredecessor(HBasicBlock* block) {
336 predecessors_.Add(block);
337 block->successors_.Add(this);
338 }
339
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100340 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) {
341 for (size_t i = 0, e = predecessors_.Size(); i < e; ++i) {
342 if (predecessors_.Get(i) == predecessor) {
343 return i;
344 }
345 }
346 return -1;
347 }
348
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100349 size_t GetSuccessorIndexOf(HBasicBlock* successor) {
350 for (size_t i = 0, e = successors_.Size(); i < e; ++i) {
351 if (successors_.Get(i) == successor) {
352 return i;
353 }
354 }
355 return -1;
356 }
357
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000358 void AddInstruction(HInstruction* instruction);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100359 void RemoveInstruction(HInstruction* instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100360 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +0100361 // Replace instruction `initial` with `replacement` within this block.
362 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
363 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100364 void AddPhi(HPhi* phi);
365 void RemovePhi(HPhi* phi);
366
367 bool IsLoopHeader() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100368 return (loop_information_ != nullptr) && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100369 }
370
371 HLoopInformation* GetLoopInformation() const {
372 return loop_information_;
373 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000374
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100375 // Set the loop_information_ on this block. This method overrides the current
376 // loop_information if it is an outer loop of the passed loop information.
377 void SetInLoop(HLoopInformation* info) {
378 if (IsLoopHeader()) {
379 // Nothing to do. This just means `info` is an outer loop.
380 } else if (loop_information_ == nullptr) {
381 loop_information_ = info;
382 } else if (loop_information_->Contains(*info->GetHeader())) {
383 // Block is currently part of an outer loop. Make it part of this inner loop.
384 // Note that a non loop header having a loop information means this loop information
385 // has already been populated
386 loop_information_ = info;
387 } else {
388 // Block is part of an inner loop. Do not update the loop information.
389 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
390 // at this point, because this method is being called while populating `info`.
391 }
392 }
393
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100394 bool IsInLoop() const { return loop_information_ != nullptr; }
395
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100396 // Returns wheter this block dominates the blocked passed as parameter.
397 bool Dominates(HBasicBlock* block) const;
398
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100399 size_t GetLifetimeStart() const { return lifetime_start_; }
400 size_t GetLifetimeEnd() const { return lifetime_end_; }
401
402 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
403 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
404
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000405 private:
406 HGraph* const graph_;
407 GrowableArray<HBasicBlock*> predecessors_;
408 GrowableArray<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100409 HInstructionList instructions_;
410 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000411 HLoopInformation* loop_information_;
412 HBasicBlock* dominator_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000413 int block_id_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100414 size_t lifetime_start_;
415 size_t lifetime_end_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000416
417 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
418};
419
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100420#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000421 M(Add) \
Dave Allison20dfc792014-06-16 20:44:29 -0700422 M(Condition) \
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000423 M(Equal) \
Dave Allison20dfc792014-06-16 20:44:29 -0700424 M(NotEqual) \
425 M(LessThan) \
426 M(LessThanOrEqual) \
427 M(GreaterThan) \
428 M(GreaterThanOrEqual) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000429 M(Exit) \
430 M(Goto) \
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000431 M(If) \
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000432 M(IntConstant) \
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000433 M(InvokeStatic) \
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100434 M(InvokeVirtual) \
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000435 M(LoadLocal) \
436 M(Local) \
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100437 M(LongConstant) \
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +0100438 M(NewInstance) \
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100439 M(Not) \
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100440 M(ParameterValue) \
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100441 M(ParallelMove) \
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100442 M(Phi) \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000443 M(Return) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000444 M(ReturnVoid) \
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000445 M(StoreLocal) \
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100446 M(Sub) \
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100447 M(Compare) \
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100448 M(InstanceFieldGet) \
449 M(InstanceFieldSet) \
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100450 M(ArrayGet) \
451 M(ArraySet) \
452 M(ArrayLength) \
453 M(BoundsCheck) \
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100454 M(NullCheck) \
455 M(Temporary) \
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000456 M(SuspendCheck) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000457
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100458#define FOR_EACH_INSTRUCTION(M) \
459 FOR_EACH_CONCRETE_INSTRUCTION(M) \
Roland Levillainccc07a92014-09-16 14:48:16 +0100460 M(Constant) \
461 M(BinaryOperation)
Dave Allison20dfc792014-06-16 20:44:29 -0700462
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000463#define FORWARD_DECLARATION(type) class H##type;
464FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
465#undef FORWARD_DECLARATION
466
Roland Levillainccc07a92014-09-16 14:48:16 +0100467#define DECLARE_INSTRUCTION(type) \
468 virtual const char* DebugName() const { return #type; } \
469 virtual const H##type* As##type() const OVERRIDE { return this; } \
470 virtual H##type* As##type() OVERRIDE { return this; } \
471 virtual bool InstructionTypeEquals(HInstruction* other) const { \
472 return other->Is##type(); \
473 } \
474 virtual void Accept(HGraphVisitor* visitor)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000475
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100476template <typename T>
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000477class HUseListNode : public ArenaObject {
478 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100479 HUseListNode(T* user, size_t index, HUseListNode* tail)
Dave Allison20dfc792014-06-16 20:44:29 -0700480 : user_(user), index_(index), tail_(tail) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000481
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000482 HUseListNode* GetTail() const { return tail_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100483 T* GetUser() const { return user_; }
484 size_t GetIndex() const { return index_; }
485
486 void SetTail(HUseListNode<T>* node) { tail_ = node; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000487
488 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100489 T* const user_;
490 const size_t index_;
491 HUseListNode<T>* tail_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000492
493 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
494};
495
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100496// Represents the side effects an instruction may have.
497class SideEffects : public ValueObject {
498 public:
499 static SideEffects None() {
500 return SideEffects(0);
501 }
502
503 static SideEffects All() {
504 return SideEffects(ChangesSomething().flags_ | DependsOnSomething().flags_);
505 }
506
507 static SideEffects ChangesSomething() {
508 return SideEffects((1 << kFlagChangesCount) - 1);
509 }
510
511 static SideEffects DependsOnSomething() {
512 int count = kFlagDependsOnCount - kFlagChangesCount;
513 return SideEffects(((1 << count) - 1) << kFlagChangesCount);
514 }
515
516 private:
517 static constexpr int kFlagChangesSomething = 0;
518 static constexpr int kFlagChangesCount = kFlagChangesSomething + 1;
519
520 static constexpr int kFlagDependsOnSomething = kFlagChangesCount;
521 static constexpr int kFlagDependsOnCount = kFlagDependsOnSomething + 1;
522
523 private:
524 explicit SideEffects(size_t flags) : flags_(flags) {}
525
526 const size_t flags_;
527};
528
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000529class HInstruction : public ArenaObject {
530 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100531 explicit HInstruction(SideEffects side_effects)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000532 : previous_(nullptr),
533 next_(nullptr),
534 block_(nullptr),
535 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100536 ssa_index_(-1),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000537 uses_(nullptr),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100538 env_uses_(nullptr),
539 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100540 locations_(nullptr),
541 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100542 lifetime_position_(kNoLifetime),
543 side_effects_(side_effects) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000544
Dave Allison20dfc792014-06-16 20:44:29 -0700545 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000546
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000547 HInstruction* GetNext() const { return next_; }
548 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000549
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000550 HBasicBlock* GetBlock() const { return block_; }
551 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100552 bool IsInBlock() const { return block_ != nullptr; }
553 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +0100554 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000555
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100556 virtual size_t InputCount() const = 0;
557 virtual HInstruction* InputAt(size_t i) const = 0;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000558
559 virtual void Accept(HGraphVisitor* visitor) = 0;
560 virtual const char* DebugName() const = 0;
561
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100562 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100563 virtual void SetRawInputAt(size_t index, HInstruction* input) = 0;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100564
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100565 virtual bool NeedsEnvironment() const { return false; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100566 virtual bool IsControlFlow() const { return false; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100567
568 void AddUseAt(HInstruction* user, size_t index) {
569 uses_ = new (block_->GetGraph()->GetArena()) HUseListNode<HInstruction>(user, index, uses_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000570 }
571
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100572 void AddEnvUseAt(HEnvironment* user, size_t index) {
573 env_uses_ = new (block_->GetGraph()->GetArena()) HUseListNode<HEnvironment>(
574 user, index, env_uses_);
575 }
576
577 void RemoveUser(HInstruction* user, size_t index);
578
579 HUseListNode<HInstruction>* GetUses() const { return uses_; }
580 HUseListNode<HEnvironment>* GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000581
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100582 bool HasUses() const { return uses_ != nullptr || env_uses_ != nullptr; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100583 bool HasEnvironmentUses() const { return env_uses_ != nullptr; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000584
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100585 size_t NumberOfUses() const {
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100586 // TODO: Optimize this method if it is used outside of the HGraphVisualizer.
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100587 size_t result = 0;
588 HUseListNode<HInstruction>* current = uses_;
589 while (current != nullptr) {
590 current = current->GetTail();
591 ++result;
592 }
593 return result;
594 }
595
Roland Levillainccc07a92014-09-16 14:48:16 +0100596 // Does this instruction dominate `other_instruction`? Aborts if
597 // this instruction and `other_instruction` are both phis.
598 bool Dominates(HInstruction* other_instruction) const;
599
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000600 int GetId() const { return id_; }
601 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000602
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100603 int GetSsaIndex() const { return ssa_index_; }
604 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
605 bool HasSsaIndex() const { return ssa_index_ != -1; }
606
607 bool HasEnvironment() const { return environment_ != nullptr; }
608 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100609 void SetEnvironment(HEnvironment* environment) { environment_ = environment; }
610
Nicolas Geoffray39468442014-09-02 15:17:15 +0100611 // Returns the number of entries in the environment. Typically, that is the
612 // number of dex registers in a method. It could be more in case of inlining.
613 size_t EnvironmentSize() const;
614
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000615 LocationSummary* GetLocations() const { return locations_; }
616 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000617
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100618 void ReplaceWith(HInstruction* instruction);
619
Dave Allison20dfc792014-06-16 20:44:29 -0700620 bool HasOnlyOneUse() const {
621 return uses_ != nullptr && uses_->GetTail() == nullptr;
622 }
623
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000624#define INSTRUCTION_TYPE_CHECK(type) \
Roland Levillainccc07a92014-09-16 14:48:16 +0100625 bool Is##type() const { return (As##type() != nullptr); } \
626 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000627 virtual H##type* As##type() { return nullptr; }
628
629 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
630#undef INSTRUCTION_TYPE_CHECK
631
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100632 // Returns whether the instruction can be moved within the graph.
633 virtual bool CanBeMoved() const { return false; }
634
635 // Returns whether the two instructions are of the same kind.
636 virtual bool InstructionTypeEquals(HInstruction* other) const { return false; }
637
638 // Returns whether any data encoded in the two instructions is equal.
639 // This method does not look at the inputs. Both instructions must be
640 // of the same type, otherwise the method has undefined behavior.
641 virtual bool InstructionDataEquals(HInstruction* other) const { return false; }
642
643 // Returns whether two instructions are equal, that is:
644 // 1) They have the same type and contain the same data,
645 // 2) Their inputs are identical.
646 bool Equals(HInstruction* other) const;
647
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100648 size_t GetLifetimePosition() const { return lifetime_position_; }
649 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
650 LiveInterval* GetLiveInterval() const { return live_interval_; }
651 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
652 bool HasLiveInterval() const { return live_interval_ != nullptr; }
653
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000654 private:
655 HInstruction* previous_;
656 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000657 HBasicBlock* block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000658
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000659 // An instruction gets an id when it is added to the graph.
660 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +0100661 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000662 int id_;
663
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100664 // When doing liveness analysis, instructions that have uses get an SSA index.
665 int ssa_index_;
666
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100667 // List of instructions that have this instruction as input.
668 HUseListNode<HInstruction>* uses_;
669
670 // List of environments that contain this instruction.
671 HUseListNode<HEnvironment>* env_uses_;
672
Nicolas Geoffray39468442014-09-02 15:17:15 +0100673 // The environment associated with this instruction. Not null if the instruction
674 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100675 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000676
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000677 // Set by the code generator.
678 LocationSummary* locations_;
679
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100680 // Set by the liveness analysis.
681 LiveInterval* live_interval_;
682
683 // Set by the liveness analysis, this is the position in a linear
684 // order of blocks where this instruction's live interval start.
685 size_t lifetime_position_;
686
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100687 const SideEffects side_effects_;
688
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000689 friend class HBasicBlock;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100690 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000691
692 DISALLOW_COPY_AND_ASSIGN(HInstruction);
693};
694
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100695template<typename T>
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000696class HUseIterator : public ValueObject {
697 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100698 explicit HUseIterator(HUseListNode<T>* uses) : current_(uses) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000699
700 bool Done() const { return current_ == nullptr; }
701
702 void Advance() {
703 DCHECK(!Done());
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000704 current_ = current_->GetTail();
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000705 }
706
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100707 HUseListNode<T>* Current() const {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000708 DCHECK(!Done());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100709 return current_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000710 }
711
712 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100713 HUseListNode<T>* current_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000714
715 friend class HValue;
716};
717
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100718// A HEnvironment object contains the values of virtual registers at a given location.
719class HEnvironment : public ArenaObject {
720 public:
721 HEnvironment(ArenaAllocator* arena, size_t number_of_vregs) : vregs_(arena, number_of_vregs) {
722 vregs_.SetSize(number_of_vregs);
723 for (size_t i = 0; i < number_of_vregs; i++) {
724 vregs_.Put(i, nullptr);
725 }
726 }
727
728 void Populate(const GrowableArray<HInstruction*>& env) {
729 for (size_t i = 0; i < env.Size(); i++) {
730 HInstruction* instruction = env.Get(i);
731 vregs_.Put(i, instruction);
732 if (instruction != nullptr) {
733 instruction->AddEnvUseAt(this, i);
734 }
735 }
736 }
737
738 void SetRawEnvAt(size_t index, HInstruction* instruction) {
739 vregs_.Put(index, instruction);
740 }
741
Nicolas Geoffray39468442014-09-02 15:17:15 +0100742 HInstruction* GetInstructionAt(size_t index) const {
743 return vregs_.Get(index);
744 }
745
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100746 GrowableArray<HInstruction*>* GetVRegs() {
747 return &vregs_;
748 }
749
Nicolas Geoffray39468442014-09-02 15:17:15 +0100750 size_t Size() const { return vregs_.Size(); }
751
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100752 private:
753 GrowableArray<HInstruction*> vregs_;
754
755 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
756};
757
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000758class HInputIterator : public ValueObject {
759 public:
Dave Allison20dfc792014-06-16 20:44:29 -0700760 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000761
762 bool Done() const { return index_ == instruction_->InputCount(); }
763 HInstruction* Current() const { return instruction_->InputAt(index_); }
764 void Advance() { index_++; }
765
766 private:
767 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100768 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000769
770 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
771};
772
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000773class HInstructionIterator : public ValueObject {
774 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100775 explicit HInstructionIterator(const HInstructionList& instructions)
776 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000777 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000778 }
779
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000780 bool Done() const { return instruction_ == nullptr; }
781 HInstruction* Current() const { return instruction_; }
782 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000783 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000784 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000785 }
786
787 private:
788 HInstruction* instruction_;
789 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100790
791 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000792};
793
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100794class HBackwardInstructionIterator : public ValueObject {
795 public:
796 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
797 : instruction_(instructions.last_instruction_) {
798 next_ = Done() ? nullptr : instruction_->GetPrevious();
799 }
800
801 bool Done() const { return instruction_ == nullptr; }
802 HInstruction* Current() const { return instruction_; }
803 void Advance() {
804 instruction_ = next_;
805 next_ = Done() ? nullptr : instruction_->GetPrevious();
806 }
807
808 private:
809 HInstruction* instruction_;
810 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100811
812 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100813};
814
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000815// An embedded container with N elements of type T. Used (with partial
816// specialization for N=0) because embedded arrays cannot have size 0.
817template<typename T, intptr_t N>
818class EmbeddedArray {
819 public:
Dave Allison20dfc792014-06-16 20:44:29 -0700820 EmbeddedArray() : elements_() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000821
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000822 intptr_t GetLength() const { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000823
824 const T& operator[](intptr_t i) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000825 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000826 return elements_[i];
827 }
828
829 T& operator[](intptr_t i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000830 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000831 return elements_[i];
832 }
833
834 const T& At(intptr_t i) const {
835 return (*this)[i];
836 }
837
838 void SetAt(intptr_t i, const T& val) {
839 (*this)[i] = val;
840 }
841
842 private:
843 T elements_[N];
844};
845
846template<typename T>
847class EmbeddedArray<T, 0> {
848 public:
849 intptr_t length() const { return 0; }
850 const T& operator[](intptr_t i) const {
851 LOG(FATAL) << "Unreachable";
852 static T sentinel = 0;
853 return sentinel;
854 }
855 T& operator[](intptr_t i) {
856 LOG(FATAL) << "Unreachable";
857 static T sentinel = 0;
858 return sentinel;
859 }
860};
861
862template<intptr_t N>
863class HTemplateInstruction: public HInstruction {
864 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100865 HTemplateInstruction<N>(SideEffects side_effects)
866 : HInstruction(side_effects), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -0700867 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000868
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100869 virtual size_t InputCount() const { return N; }
870 virtual HInstruction* InputAt(size_t i) const { return inputs_[i]; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000871
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000872 protected:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100873 virtual void SetRawInputAt(size_t i, HInstruction* instruction) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000874 inputs_[i] = instruction;
875 }
876
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000877 private:
878 EmbeddedArray<HInstruction*, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100879
880 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000881};
882
Dave Allison20dfc792014-06-16 20:44:29 -0700883template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100884class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -0700885 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100886 HExpression<N>(Primitive::Type type, SideEffects side_effects)
887 : HTemplateInstruction<N>(side_effects), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -0700888 virtual ~HExpression() {}
889
890 virtual Primitive::Type GetType() const { return type_; }
891
892 private:
893 const Primitive::Type type_;
894};
895
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000896// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
897// instruction that branches to the exit block.
898class HReturnVoid : public HTemplateInstruction<0> {
899 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100900 HReturnVoid() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100901
902 virtual bool IsControlFlow() const { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000903
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100904 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000905
906 private:
907 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
908};
909
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000910// Represents dex's RETURN opcodes. A HReturn is a control flow
911// instruction that branches to the exit block.
912class HReturn : public HTemplateInstruction<1> {
913 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100914 explicit HReturn(HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000915 SetRawInputAt(0, value);
916 }
917
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100918 virtual bool IsControlFlow() const { return true; }
919
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100920 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000921
922 private:
923 DISALLOW_COPY_AND_ASSIGN(HReturn);
924};
925
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000926// The exit instruction is the only instruction of the exit block.
927// Instructions aborting the method (HTrow and HReturn) must branch to the
928// exit block.
929class HExit : public HTemplateInstruction<0> {
930 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100931 HExit() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100932
933 virtual bool IsControlFlow() const { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000934
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100935 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000936
937 private:
938 DISALLOW_COPY_AND_ASSIGN(HExit);
939};
940
941// Jumps from one block to another.
942class HGoto : public HTemplateInstruction<0> {
943 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100944 HGoto() : HTemplateInstruction(SideEffects::None()) {}
945
946 virtual bool IsControlFlow() const { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000947
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000948 HBasicBlock* GetSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100949 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000950 }
951
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100952 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000953
954 private:
955 DISALLOW_COPY_AND_ASSIGN(HGoto);
956};
957
Dave Allison20dfc792014-06-16 20:44:29 -0700958
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000959// Conditional branch. A block ending with an HIf instruction must have
960// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000961class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000962 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100963 explicit HIf(HInstruction* input) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000964 SetRawInputAt(0, input);
965 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000966
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100967 virtual bool IsControlFlow() const { return true; }
968
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000969 HBasicBlock* IfTrueSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100970 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000971 }
972
973 HBasicBlock* IfFalseSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100974 return GetBlock()->GetSuccessors().Get(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000975 }
976
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100977 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000978
Dave Allison20dfc792014-06-16 20:44:29 -0700979 virtual bool IsIfInstruction() const { return true; }
980
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000981 private:
982 DISALLOW_COPY_AND_ASSIGN(HIf);
983};
984
Dave Allison20dfc792014-06-16 20:44:29 -0700985class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000986 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000987 HBinaryOperation(Primitive::Type result_type,
988 HInstruction* left,
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100989 HInstruction* right) : HExpression(result_type, SideEffects::None()) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000990 SetRawInputAt(0, left);
991 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000992 }
993
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000994 HInstruction* GetLeft() const { return InputAt(0); }
995 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -0700996 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000997
998 virtual bool IsCommutative() { return false; }
999
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001000 virtual bool CanBeMoved() const { return true; }
1001 virtual bool InstructionDataEquals(HInstruction* other) const { return true; }
1002
Roland Levillainccc07a92014-09-16 14:48:16 +01001003 DECLARE_INSTRUCTION(BinaryOperation);
1004
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001005 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001006 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
1007};
1008
Dave Allison20dfc792014-06-16 20:44:29 -07001009class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001010 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001011 HCondition(HInstruction* first, HInstruction* second)
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001012 : HBinaryOperation(Primitive::kPrimBoolean, first, second) {}
1013
1014 virtual bool IsCommutative() { return true; }
Dave Allison20dfc792014-06-16 20:44:29 -07001015 bool NeedsMaterialization() const;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001016
Dave Allison20dfc792014-06-16 20:44:29 -07001017 DECLARE_INSTRUCTION(Condition);
1018
1019 virtual IfCondition GetCondition() const = 0;
1020
1021 private:
1022 DISALLOW_COPY_AND_ASSIGN(HCondition);
1023};
1024
1025// Instruction to check if two inputs are equal to each other.
1026class HEqual : public HCondition {
1027 public:
1028 HEqual(HInstruction* first, HInstruction* second)
1029 : HCondition(first, second) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001030
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001031 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001032
Dave Allison20dfc792014-06-16 20:44:29 -07001033 virtual IfCondition GetCondition() const {
1034 return kCondEQ;
1035 }
1036
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001037 private:
1038 DISALLOW_COPY_AND_ASSIGN(HEqual);
1039};
1040
Dave Allison20dfc792014-06-16 20:44:29 -07001041class HNotEqual : public HCondition {
1042 public:
1043 HNotEqual(HInstruction* first, HInstruction* second)
1044 : HCondition(first, second) {}
1045
1046 DECLARE_INSTRUCTION(NotEqual);
1047
1048 virtual IfCondition GetCondition() const {
1049 return kCondNE;
1050 }
1051
1052 private:
1053 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
1054};
1055
1056class HLessThan : public HCondition {
1057 public:
1058 HLessThan(HInstruction* first, HInstruction* second)
1059 : HCondition(first, second) {}
1060
1061 DECLARE_INSTRUCTION(LessThan);
1062
1063 virtual IfCondition GetCondition() const {
1064 return kCondLT;
1065 }
1066
1067 private:
1068 DISALLOW_COPY_AND_ASSIGN(HLessThan);
1069};
1070
1071class HLessThanOrEqual : public HCondition {
1072 public:
1073 HLessThanOrEqual(HInstruction* first, HInstruction* second)
1074 : HCondition(first, second) {}
1075
1076 DECLARE_INSTRUCTION(LessThanOrEqual);
1077
1078 virtual IfCondition GetCondition() const {
1079 return kCondLE;
1080 }
1081
1082 private:
1083 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
1084};
1085
1086class HGreaterThan : public HCondition {
1087 public:
1088 HGreaterThan(HInstruction* first, HInstruction* second)
1089 : HCondition(first, second) {}
1090
1091 DECLARE_INSTRUCTION(GreaterThan);
1092
1093 virtual IfCondition GetCondition() const {
1094 return kCondGT;
1095 }
1096
1097 private:
1098 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
1099};
1100
1101class HGreaterThanOrEqual : public HCondition {
1102 public:
1103 HGreaterThanOrEqual(HInstruction* first, HInstruction* second)
1104 : HCondition(first, second) {}
1105
1106 DECLARE_INSTRUCTION(GreaterThanOrEqual);
1107
1108 virtual IfCondition GetCondition() const {
1109 return kCondGE;
1110 }
1111
1112 private:
1113 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
1114};
1115
1116
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001117// Instruction to check how two inputs compare to each other.
1118// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
1119class HCompare : public HBinaryOperation {
1120 public:
1121 HCompare(Primitive::Type type, HInstruction* first, HInstruction* second)
1122 : HBinaryOperation(Primitive::kPrimInt, first, second) {
1123 DCHECK_EQ(type, first->GetType());
1124 DCHECK_EQ(type, second->GetType());
1125 }
1126
1127 DECLARE_INSTRUCTION(Compare);
1128
1129 private:
1130 DISALLOW_COPY_AND_ASSIGN(HCompare);
1131};
1132
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001133// A local in the graph. Corresponds to a Dex register.
1134class HLocal : public HTemplateInstruction<0> {
1135 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001136 explicit HLocal(uint16_t reg_number)
1137 : HTemplateInstruction(SideEffects::None()), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001138
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001139 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001140
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001141 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001142
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001143 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001144 // The Dex register number.
1145 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001146
1147 DISALLOW_COPY_AND_ASSIGN(HLocal);
1148};
1149
1150// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07001151class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001152 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001153 explicit HLoadLocal(HLocal* local, Primitive::Type type)
1154 : HExpression(type, SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001155 SetRawInputAt(0, local);
1156 }
1157
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001158 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1159
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001160 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001161
1162 private:
1163 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
1164};
1165
1166// Store a value in a given local. This instruction has two inputs: the value
1167// and the local.
1168class HStoreLocal : public HTemplateInstruction<2> {
1169 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001170 HStoreLocal(HLocal* local, HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001171 SetRawInputAt(0, local);
1172 SetRawInputAt(1, value);
1173 }
1174
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001175 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1176
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001177 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001178
1179 private:
1180 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
1181};
1182
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001183class HConstant : public HExpression<0> {
1184 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001185 explicit HConstant(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
1186
1187 virtual bool CanBeMoved() const { return true; }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001188
1189 DECLARE_INSTRUCTION(Constant);
1190
1191 private:
1192 DISALLOW_COPY_AND_ASSIGN(HConstant);
1193};
1194
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001195// Constants of the type int. Those can be from Dex instructions, or
1196// synthesized (for example with the if-eqz instruction).
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001197class HIntConstant : public HConstant {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001198 public:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001199 explicit HIntConstant(int32_t value) : HConstant(Primitive::kPrimInt), value_(value) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001200
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001201 int32_t GetValue() const { return value_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001202
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001203 virtual bool InstructionDataEquals(HInstruction* other) const {
1204 return other->AsIntConstant()->value_ == value_;
1205 }
1206
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001207 DECLARE_INSTRUCTION(IntConstant);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001208
1209 private:
1210 const int32_t value_;
1211
1212 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
1213};
1214
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001215class HLongConstant : public HConstant {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001216 public:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001217 explicit HLongConstant(int64_t value) : HConstant(Primitive::kPrimLong), value_(value) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001218
1219 int64_t GetValue() const { return value_; }
1220
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001221 virtual bool InstructionDataEquals(HInstruction* other) const {
1222 return other->AsLongConstant()->value_ == value_;
1223 }
1224
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001225 DECLARE_INSTRUCTION(LongConstant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001226
1227 private:
1228 const int64_t value_;
1229
1230 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
1231};
1232
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001233class HInvoke : public HInstruction {
1234 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001235 HInvoke(ArenaAllocator* arena,
1236 uint32_t number_of_arguments,
1237 Primitive::Type return_type,
1238 uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001239 : HInstruction(SideEffects::All()),
1240 inputs_(arena, number_of_arguments),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001241 return_type_(return_type),
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001242 dex_pc_(dex_pc) {
1243 inputs_.SetSize(number_of_arguments);
1244 }
1245
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001246 virtual size_t InputCount() const { return inputs_.Size(); }
1247 virtual HInstruction* InputAt(size_t i) const { return inputs_.Get(i); }
1248
1249 // Runtime needs to walk the stack, so Dex -> Dex calls need to
1250 // know their environment.
1251 virtual bool NeedsEnvironment() const { return true; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001252
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001253 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001254 SetRawInputAt(index, argument);
1255 }
1256
1257 virtual void SetRawInputAt(size_t index, HInstruction* input) {
1258 inputs_.Put(index, input);
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001259 }
1260
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001261 virtual Primitive::Type GetType() const { return return_type_; }
1262
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001263 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001264
1265 protected:
1266 GrowableArray<HInstruction*> inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001267 const Primitive::Type return_type_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001268 const uint32_t dex_pc_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001269
1270 private:
1271 DISALLOW_COPY_AND_ASSIGN(HInvoke);
1272};
1273
1274class HInvokeStatic : public HInvoke {
1275 public:
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001276 HInvokeStatic(ArenaAllocator* arena,
1277 uint32_t number_of_arguments,
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001278 Primitive::Type return_type,
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001279 uint32_t dex_pc,
1280 uint32_t index_in_dex_cache)
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001281 : HInvoke(arena, number_of_arguments, return_type, dex_pc),
1282 index_in_dex_cache_(index_in_dex_cache) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001283
1284 uint32_t GetIndexInDexCache() const { return index_in_dex_cache_; }
1285
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001286 DECLARE_INSTRUCTION(InvokeStatic);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001287
1288 private:
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001289 const uint32_t index_in_dex_cache_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001290
1291 DISALLOW_COPY_AND_ASSIGN(HInvokeStatic);
1292};
1293
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001294class HInvokeVirtual : public HInvoke {
1295 public:
1296 HInvokeVirtual(ArenaAllocator* arena,
1297 uint32_t number_of_arguments,
1298 Primitive::Type return_type,
1299 uint32_t dex_pc,
1300 uint32_t vtable_index)
1301 : HInvoke(arena, number_of_arguments, return_type, dex_pc),
1302 vtable_index_(vtable_index) {}
1303
1304 uint32_t GetVTableIndex() const { return vtable_index_; }
1305
1306 DECLARE_INSTRUCTION(InvokeVirtual);
1307
1308 private:
1309 const uint32_t vtable_index_;
1310
1311 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
1312};
1313
Dave Allison20dfc792014-06-16 20:44:29 -07001314class HNewInstance : public HExpression<0> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001315 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001316 HNewInstance(uint32_t dex_pc, uint16_t type_index)
1317 : HExpression(Primitive::kPrimNot, SideEffects::None()),
1318 dex_pc_(dex_pc),
1319 type_index_(type_index) {}
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001320
1321 uint32_t GetDexPc() const { return dex_pc_; }
1322 uint16_t GetTypeIndex() const { return type_index_; }
1323
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001324 // Calls runtime so needs an environment.
1325 virtual bool NeedsEnvironment() const { return true; }
1326
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001327 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001328
1329 private:
1330 const uint32_t dex_pc_;
1331 const uint16_t type_index_;
1332
1333 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
1334};
1335
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001336class HAdd : public HBinaryOperation {
1337 public:
1338 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1339 : HBinaryOperation(result_type, left, right) {}
1340
1341 virtual bool IsCommutative() { return true; }
1342
1343 DECLARE_INSTRUCTION(Add);
1344
1345 private:
1346 DISALLOW_COPY_AND_ASSIGN(HAdd);
1347};
1348
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001349class HSub : public HBinaryOperation {
1350 public:
1351 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1352 : HBinaryOperation(result_type, left, right) {}
1353
1354 virtual bool IsCommutative() { return false; }
1355
1356 DECLARE_INSTRUCTION(Sub);
1357
1358 private:
1359 DISALLOW_COPY_AND_ASSIGN(HSub);
1360};
1361
1362// The value of a parameter in this method. Its location depends on
1363// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07001364class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001365 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001366 HParameterValue(uint8_t index, Primitive::Type parameter_type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001367 : HExpression(parameter_type, SideEffects::None()), index_(index) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001368
1369 uint8_t GetIndex() const { return index_; }
1370
1371 DECLARE_INSTRUCTION(ParameterValue);
1372
1373 private:
1374 // The index of this parameter in the parameters list. Must be less
1375 // than HGraph::number_of_in_vregs_;
1376 const uint8_t index_;
1377
1378 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
1379};
1380
Dave Allison20dfc792014-06-16 20:44:29 -07001381class HNot : public HExpression<1> {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001382 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001383 explicit HNot(HInstruction* input) : HExpression(Primitive::kPrimBoolean, SideEffects::None()) {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001384 SetRawInputAt(0, input);
1385 }
1386
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001387 virtual bool CanBeMoved() const { return true; }
1388 virtual bool InstructionDataEquals(HInstruction* other) const { return true; }
1389
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001390 DECLARE_INSTRUCTION(Not);
1391
1392 private:
1393 DISALLOW_COPY_AND_ASSIGN(HNot);
1394};
1395
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001396class HPhi : public HInstruction {
1397 public:
1398 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001399 : HInstruction(SideEffects::None()),
1400 inputs_(arena, number_of_inputs),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001401 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001402 type_(type),
1403 is_live_(false) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001404 inputs_.SetSize(number_of_inputs);
1405 }
1406
1407 virtual size_t InputCount() const { return inputs_.Size(); }
1408 virtual HInstruction* InputAt(size_t i) const { return inputs_.Get(i); }
1409
1410 virtual void SetRawInputAt(size_t index, HInstruction* input) {
1411 inputs_.Put(index, input);
1412 }
1413
1414 void AddInput(HInstruction* input);
1415
1416 virtual Primitive::Type GetType() const { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001417 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001418
1419 uint32_t GetRegNumber() const { return reg_number_; }
1420
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001421 void SetDead() { is_live_ = false; }
1422 void SetLive() { is_live_ = true; }
1423 bool IsDead() const { return !is_live_; }
1424 bool IsLive() const { return is_live_; }
1425
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001426 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001427
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001428 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001429 GrowableArray<HInstruction*> inputs_;
1430 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001431 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001432 bool is_live_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001433
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001434 DISALLOW_COPY_AND_ASSIGN(HPhi);
1435};
1436
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001437class HNullCheck : public HExpression<1> {
1438 public:
1439 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001440 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001441 SetRawInputAt(0, value);
1442 }
1443
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001444 virtual bool CanBeMoved() const { return true; }
1445 virtual bool InstructionDataEquals(HInstruction* other) const { return true; }
1446
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001447 virtual bool NeedsEnvironment() const { return true; }
1448
1449 uint32_t GetDexPc() const { return dex_pc_; }
1450
1451 DECLARE_INSTRUCTION(NullCheck);
1452
1453 private:
1454 const uint32_t dex_pc_;
1455
1456 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
1457};
1458
1459class FieldInfo : public ValueObject {
1460 public:
Nicolas Geoffray39468442014-09-02 15:17:15 +01001461 explicit FieldInfo(MemberOffset field_offset, Primitive::Type field_type)
1462 : field_offset_(field_offset), field_type_(field_type) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001463
1464 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001465 Primitive::Type GetFieldType() const { return field_type_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001466
1467 private:
1468 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01001469 const Primitive::Type field_type_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001470};
1471
1472class HInstanceFieldGet : public HExpression<1> {
1473 public:
1474 HInstanceFieldGet(HInstruction* value,
1475 Primitive::Type field_type,
1476 MemberOffset field_offset)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001477 : HExpression(field_type, SideEffects::DependsOnSomething()),
1478 field_info_(field_offset, field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001479 SetRawInputAt(0, value);
1480 }
1481
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001482 virtual bool CanBeMoved() const { return true; }
1483 virtual bool InstructionDataEquals(HInstruction* other) const {
1484 size_t other_offset = other->AsInstanceFieldGet()->GetFieldOffset().SizeValue();
1485 return other_offset == GetFieldOffset().SizeValue();
1486 }
1487
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001488 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001489 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001490
1491 DECLARE_INSTRUCTION(InstanceFieldGet);
1492
1493 private:
1494 const FieldInfo field_info_;
1495
1496 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
1497};
1498
1499class HInstanceFieldSet : public HTemplateInstruction<2> {
1500 public:
1501 HInstanceFieldSet(HInstruction* object,
1502 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01001503 Primitive::Type field_type,
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001504 MemberOffset field_offset)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001505 : HTemplateInstruction(SideEffects::ChangesSomething()),
1506 field_info_(field_offset, field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001507 SetRawInputAt(0, object);
1508 SetRawInputAt(1, value);
1509 }
1510
1511 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001512 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001513
1514 DECLARE_INSTRUCTION(InstanceFieldSet);
1515
1516 private:
1517 const FieldInfo field_info_;
1518
1519 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
1520};
1521
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001522class HArrayGet : public HExpression<2> {
1523 public:
1524 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001525 : HExpression(type, SideEffects::DependsOnSomething()) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001526 SetRawInputAt(0, array);
1527 SetRawInputAt(1, index);
1528 }
1529
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001530 virtual bool CanBeMoved() const { return true; }
1531 virtual bool InstructionDataEquals(HInstruction* other) const { return true; }
1532
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001533 DECLARE_INSTRUCTION(ArrayGet);
1534
1535 private:
1536 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
1537};
1538
1539class HArraySet : public HTemplateInstruction<3> {
1540 public:
1541 HArraySet(HInstruction* array,
1542 HInstruction* index,
1543 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01001544 Primitive::Type component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001545 uint32_t dex_pc)
1546 : HTemplateInstruction(SideEffects::ChangesSomething()),
1547 dex_pc_(dex_pc),
1548 component_type_(component_type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001549 SetRawInputAt(0, array);
1550 SetRawInputAt(1, index);
1551 SetRawInputAt(2, value);
1552 }
1553
1554 virtual bool NeedsEnvironment() const {
1555 // We currently always call a runtime method to catch array store
1556 // exceptions.
1557 return InputAt(2)->GetType() == Primitive::kPrimNot;
1558 }
1559
1560 uint32_t GetDexPc() const { return dex_pc_; }
1561
Nicolas Geoffray39468442014-09-02 15:17:15 +01001562 Primitive::Type GetComponentType() const { return component_type_; }
1563
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001564 DECLARE_INSTRUCTION(ArraySet);
1565
1566 private:
1567 const uint32_t dex_pc_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01001568 const Primitive::Type component_type_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001569
1570 DISALLOW_COPY_AND_ASSIGN(HArraySet);
1571};
1572
1573class HArrayLength : public HExpression<1> {
1574 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001575 explicit HArrayLength(HInstruction* array)
1576 : HExpression(Primitive::kPrimInt, SideEffects::None()) {
1577 // Note that arrays do not change length, so the instruction does not
1578 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001579 SetRawInputAt(0, array);
1580 }
1581
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001582 virtual bool CanBeMoved() const { return true; }
1583 virtual bool InstructionDataEquals(HInstruction* other) const { return true; }
1584
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001585 DECLARE_INSTRUCTION(ArrayLength);
1586
1587 private:
1588 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
1589};
1590
1591class HBoundsCheck : public HExpression<2> {
1592 public:
1593 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001594 : HExpression(index->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001595 DCHECK(index->GetType() == Primitive::kPrimInt);
1596 SetRawInputAt(0, index);
1597 SetRawInputAt(1, length);
1598 }
1599
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001600 virtual bool CanBeMoved() const { return true; }
1601 virtual bool InstructionDataEquals(HInstruction* other) const { return true; }
1602
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001603 virtual bool NeedsEnvironment() const { return true; }
1604
1605 uint32_t GetDexPc() const { return dex_pc_; }
1606
1607 DECLARE_INSTRUCTION(BoundsCheck);
1608
1609 private:
1610 const uint32_t dex_pc_;
1611
1612 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
1613};
1614
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001615/**
1616 * Some DEX instructions are folded into multiple HInstructions that need
1617 * to stay live until the last HInstruction. This class
1618 * is used as a marker for the baseline compiler to ensure its preceding
1619 * HInstruction stays live. `index` is the temporary number that is used
1620 * for knowing the stack offset where to store the instruction.
1621 */
1622class HTemporary : public HTemplateInstruction<0> {
1623 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001624 explicit HTemporary(size_t index) : HTemplateInstruction(SideEffects::None()), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001625
1626 size_t GetIndex() const { return index_; }
1627
1628 DECLARE_INSTRUCTION(Temporary);
1629
1630 private:
1631 const size_t index_;
1632
1633 DISALLOW_COPY_AND_ASSIGN(HTemporary);
1634};
1635
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001636class HSuspendCheck : public HTemplateInstruction<0> {
1637 public:
1638 explicit HSuspendCheck(uint32_t dex_pc)
1639 : HTemplateInstruction(SideEffects::ChangesSomething()), dex_pc_(dex_pc) {}
1640
1641 virtual bool NeedsEnvironment() const {
1642 return true;
1643 }
1644
1645 uint32_t GetDexPc() const { return dex_pc_; }
1646
1647 DECLARE_INSTRUCTION(SuspendCheck);
1648
1649 private:
1650 const uint32_t dex_pc_;
1651
1652 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
1653};
1654
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001655class MoveOperands : public ArenaObject {
1656 public:
1657 MoveOperands(Location source, Location destination)
1658 : source_(source), destination_(destination) {}
1659
1660 Location GetSource() const { return source_; }
1661 Location GetDestination() const { return destination_; }
1662
1663 void SetSource(Location value) { source_ = value; }
1664 void SetDestination(Location value) { destination_ = value; }
1665
1666 // The parallel move resolver marks moves as "in-progress" by clearing the
1667 // destination (but not the source).
1668 Location MarkPending() {
1669 DCHECK(!IsPending());
1670 Location dest = destination_;
1671 destination_ = Location::NoLocation();
1672 return dest;
1673 }
1674
1675 void ClearPending(Location dest) {
1676 DCHECK(IsPending());
1677 destination_ = dest;
1678 }
1679
1680 bool IsPending() const {
1681 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
1682 return destination_.IsInvalid() && !source_.IsInvalid();
1683 }
1684
1685 // True if this blocks a move from the given location.
1686 bool Blocks(Location loc) const {
1687 return !IsEliminated() && source_.Equals(loc);
1688 }
1689
1690 // A move is redundant if it's been eliminated, if its source and
1691 // destination are the same, or if its destination is unneeded.
1692 bool IsRedundant() const {
1693 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
1694 }
1695
1696 // We clear both operands to indicate move that's been eliminated.
1697 void Eliminate() {
1698 source_ = destination_ = Location::NoLocation();
1699 }
1700
1701 bool IsEliminated() const {
1702 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
1703 return source_.IsInvalid();
1704 }
1705
1706 private:
1707 Location source_;
1708 Location destination_;
1709
1710 DISALLOW_COPY_AND_ASSIGN(MoveOperands);
1711};
1712
1713static constexpr size_t kDefaultNumberOfMoves = 4;
1714
1715class HParallelMove : public HTemplateInstruction<0> {
1716 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001717 explicit HParallelMove(ArenaAllocator* arena)
1718 : HTemplateInstruction(SideEffects::None()), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001719
1720 void AddMove(MoveOperands* move) {
1721 moves_.Add(move);
1722 }
1723
1724 MoveOperands* MoveOperandsAt(size_t index) const {
1725 return moves_.Get(index);
1726 }
1727
1728 size_t NumMoves() const { return moves_.Size(); }
1729
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001730 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001731
1732 private:
1733 GrowableArray<MoveOperands*> moves_;
1734
1735 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
1736};
1737
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001738class HGraphVisitor : public ValueObject {
1739 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001740 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
1741 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001742
Dave Allison20dfc792014-06-16 20:44:29 -07001743 virtual void VisitInstruction(HInstruction* instruction) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001744 virtual void VisitBasicBlock(HBasicBlock* block);
1745
1746 void VisitInsertionOrder();
1747
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001748 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001749
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001750 // Visit functions for instruction classes.
1751#define DECLARE_VISIT_INSTRUCTION(name) \
1752 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
1753
1754 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
1755
1756#undef DECLARE_VISIT_INSTRUCTION
1757
1758 private:
1759 HGraph* graph_;
1760
1761 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
1762};
1763
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001764class HInsertionOrderIterator : public ValueObject {
1765 public:
1766 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
1767
1768 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
1769 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
1770 void Advance() { ++index_; }
1771
1772 private:
1773 const HGraph& graph_;
1774 size_t index_;
1775
1776 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
1777};
1778
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001779class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001780 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001781 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001782
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001783 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
1784 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001785 void Advance() { ++index_; }
1786
1787 private:
1788 const HGraph& graph_;
1789 size_t index_;
1790
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001791 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001792};
1793
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001794class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001795 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001796 explicit HPostOrderIterator(const HGraph& graph)
1797 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001798
1799 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001800 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001801 void Advance() { --index_; }
1802
1803 private:
1804 const HGraph& graph_;
1805 size_t index_;
1806
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001807 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001808};
1809
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001810} // namespace art
1811
1812#endif // ART_COMPILER_OPTIMIZING_NODES_H_