blob: f530708aeb036318f5b47128932b20d008caace4 [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"
Ian Rogers0279ebb2014-10-08 17:27:48 -070023#include "utils/arena_object.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 Geoffray3c049742014-09-24 18:10:46 +010035class HSuspendCheck;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010036class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000037class LocationSummary;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000038
39static const int kDefaultNumberOfBlocks = 8;
40static const int kDefaultNumberOfSuccessors = 2;
41static const int kDefaultNumberOfPredecessors = 2;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010042static const int kDefaultNumberOfDominatedBlocks = 1;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000043static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000044
Dave Allison20dfc792014-06-16 20:44:29 -070045enum IfCondition {
46 kCondEQ,
47 kCondNE,
48 kCondLT,
49 kCondLE,
50 kCondGT,
51 kCondGE,
52};
53
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010054class HInstructionList {
55 public:
56 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
57
58 void AddInstruction(HInstruction* instruction);
59 void RemoveInstruction(HInstruction* instruction);
60
Roland Levillain6b469232014-09-25 10:10:38 +010061 // Return true if this list contains `instruction`.
62 bool Contains(HInstruction* instruction) const;
63
Roland Levillainccc07a92014-09-16 14:48:16 +010064 // Return true if `instruction1` is found before `instruction2` in
65 // this instruction list and false otherwise. Abort if none
66 // of these instructions is found.
67 bool FoundBefore(const HInstruction* instruction1,
68 const HInstruction* instruction2) const;
69
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010070 private:
71 HInstruction* first_instruction_;
72 HInstruction* last_instruction_;
73
74 friend class HBasicBlock;
75 friend class HInstructionIterator;
Nicolas Geoffray804d0932014-05-02 08:46:00 +010076 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010077
78 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
79};
80
Nicolas Geoffray818f2102014-02-18 16:43:35 +000081// Control-flow graph of a method. Contains a list of basic blocks.
82class HGraph : public ArenaObject {
83 public:
84 explicit HGraph(ArenaAllocator* arena)
85 : arena_(arena),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000086 blocks_(arena, kDefaultNumberOfBlocks),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +010087 reverse_post_order_(arena, kDefaultNumberOfBlocks),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +010088 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +010089 number_of_vregs_(0),
90 number_of_in_vregs_(0),
Nicolas Geoffraye5038322014-07-04 09:41:32 +010091 number_of_temporaries_(0),
Dave Allison20dfc792014-06-16 20:44:29 -070092 current_instruction_id_(0) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +000093
Nicolas Geoffray787c3072014-03-17 10:20:19 +000094 ArenaAllocator* GetArena() const { return arena_; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +010095 const GrowableArray<HBasicBlock*>& GetBlocks() const { return blocks_; }
Roland Levillain93445682014-10-06 19:24:02 +010096 HBasicBlock* GetBlock(size_t id) const { return blocks_.Get(id); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +000097
Nicolas Geoffray787c3072014-03-17 10:20:19 +000098 HBasicBlock* GetEntryBlock() const { return entry_block_; }
99 HBasicBlock* GetExitBlock() const { return exit_block_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000100
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000101 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
102 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000103
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000104 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100105
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000106 void BuildDominatorTree();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100107 void TransformToSSA();
108 void SimplifyCFG();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000109
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100110 // Find all natural loops in this graph. Aborts computation and returns false
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100111 // if one loop is not natural, that is the header does not dominate the back
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100112 // edge.
113 bool FindNaturalLoops() const;
114
115 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
116 void SimplifyLoop(HBasicBlock* header);
117
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000118 int GetNextInstructionId() {
119 return current_instruction_id_++;
120 }
121
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100122 uint16_t GetMaximumNumberOfOutVRegs() const {
123 return maximum_number_of_out_vregs_;
124 }
125
126 void UpdateMaximumNumberOfOutVRegs(uint16_t new_value) {
127 maximum_number_of_out_vregs_ = std::max(new_value, maximum_number_of_out_vregs_);
128 }
129
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100130 void UpdateNumberOfTemporaries(size_t count) {
131 number_of_temporaries_ = std::max(count, number_of_temporaries_);
132 }
133
134 size_t GetNumberOfTemporaries() const {
135 return number_of_temporaries_;
136 }
137
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100138 void SetNumberOfVRegs(uint16_t number_of_vregs) {
139 number_of_vregs_ = number_of_vregs;
140 }
141
142 uint16_t GetNumberOfVRegs() const {
143 return number_of_vregs_;
144 }
145
146 void SetNumberOfInVRegs(uint16_t value) {
147 number_of_in_vregs_ = value;
148 }
149
150 uint16_t GetNumberOfInVRegs() const {
151 return number_of_in_vregs_;
152 }
153
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100154 uint16_t GetNumberOfLocalVRegs() const {
155 return number_of_vregs_ - number_of_in_vregs_;
156 }
157
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100158 const GrowableArray<HBasicBlock*>& GetReversePostOrder() const {
159 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100160 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100161
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000162 private:
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000163 HBasicBlock* FindCommonDominator(HBasicBlock* first, HBasicBlock* second) const;
164 void VisitBlockForDominatorTree(HBasicBlock* block,
165 HBasicBlock* predecessor,
166 GrowableArray<size_t>* visits);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100167 void FindBackEdges(ArenaBitVector* visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000168 void VisitBlockForBackEdges(HBasicBlock* block,
169 ArenaBitVector* visited,
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100170 ArenaBitVector* visiting);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000171 void RemoveDeadBlocks(const ArenaBitVector& visited) const;
172
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000173 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000174
175 // List of blocks in insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000176 GrowableArray<HBasicBlock*> blocks_;
177
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100178 // List of blocks to perform a reverse post order tree traversal.
179 GrowableArray<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000180
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000181 HBasicBlock* entry_block_;
182 HBasicBlock* exit_block_;
183
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100184 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100185 uint16_t maximum_number_of_out_vregs_;
186
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100187 // The number of virtual registers in this method. Contains the parameters.
188 uint16_t number_of_vregs_;
189
190 // The number of virtual registers used by parameters of this method.
191 uint16_t number_of_in_vregs_;
192
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100193 // The number of temporaries that will be needed for the baseline compiler.
194 size_t number_of_temporaries_;
195
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000196 // The current id to assign to a newly added instruction. See HInstruction.id_.
197 int current_instruction_id_;
198
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000199 DISALLOW_COPY_AND_ASSIGN(HGraph);
200};
201
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000202class HLoopInformation : public ArenaObject {
203 public:
204 HLoopInformation(HBasicBlock* header, HGraph* graph)
205 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100206 suspend_check_(nullptr),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100207 back_edges_(graph->GetArena(), kDefaultNumberOfBackEdges),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100208 // Make bit vector growable, as the number of blocks may change.
209 blocks_(graph->GetArena(), graph->GetBlocks().Size(), true) {}
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100210
211 HBasicBlock* GetHeader() const {
212 return header_;
213 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000214
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100215 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
216 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
217 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
218
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000219 void AddBackEdge(HBasicBlock* back_edge) {
220 back_edges_.Add(back_edge);
221 }
222
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100223 void RemoveBackEdge(HBasicBlock* back_edge) {
224 back_edges_.Delete(back_edge);
225 }
226
227 bool IsBackEdge(HBasicBlock* block) {
228 for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) {
229 if (back_edges_.Get(i) == block) return true;
230 }
231 return false;
232 }
233
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000234 int NumberOfBackEdges() const {
235 return back_edges_.Size();
236 }
237
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100238 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100239
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100240 const GrowableArray<HBasicBlock*>& GetBackEdges() const {
241 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100242 }
243
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100244 void ClearBackEdges() {
245 back_edges_.Reset();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100246 }
247
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100248 // Find blocks that are part of this loop. Returns whether the loop is a natural loop,
249 // that is the header dominates the back edge.
250 bool Populate();
251
252 // Returns whether this loop information contains `block`.
253 // Note that this loop information *must* be populated before entering this function.
254 bool Contains(const HBasicBlock& block) const;
255
256 // Returns whether this loop information is an inner loop of `other`.
257 // Note that `other` *must* be populated before entering this function.
258 bool IsIn(const HLoopInformation& other) const;
259
260 const ArenaBitVector& GetBlocks() const { return blocks_; }
261
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000262 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100263 // Internal recursive implementation of `Populate`.
264 void PopulateRecursive(HBasicBlock* block);
265
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000266 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100267 HSuspendCheck* suspend_check_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000268 GrowableArray<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100269 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000270
271 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
272};
273
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100274static constexpr size_t kNoLifetime = -1;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100275static constexpr uint32_t kNoDexPc = -1;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100276
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000277// A block in a method. Contains the list of instructions represented
278// as a double linked list. Each block knows its predecessors and
279// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100280
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000281class HBasicBlock : public ArenaObject {
282 public:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100283 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000284 : graph_(graph),
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000285 predecessors_(graph->GetArena(), kDefaultNumberOfPredecessors),
286 successors_(graph->GetArena(), kDefaultNumberOfSuccessors),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000287 loop_information_(nullptr),
288 dominator_(nullptr),
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100289 dominated_blocks_(graph->GetArena(), kDefaultNumberOfDominatedBlocks),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100290 block_id_(-1),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100291 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100292 lifetime_start_(kNoLifetime),
293 lifetime_end_(kNoLifetime) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000294
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100295 const GrowableArray<HBasicBlock*>& GetPredecessors() const {
296 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000297 }
298
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100299 const GrowableArray<HBasicBlock*>& GetSuccessors() const {
300 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000301 }
302
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100303 const GrowableArray<HBasicBlock*>& GetDominatedBlocks() const {
304 return dominated_blocks_;
305 }
306
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100307 bool IsEntryBlock() const {
308 return graph_->GetEntryBlock() == this;
309 }
310
311 bool IsExitBlock() const {
312 return graph_->GetExitBlock() == this;
313 }
314
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000315 void AddBackEdge(HBasicBlock* back_edge) {
316 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000317 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000318 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100319 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000320 loop_information_->AddBackEdge(back_edge);
321 }
322
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000323 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000324
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000325 int GetBlockId() const { return block_id_; }
326 void SetBlockId(int id) { block_id_ = id; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000327
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000328 HBasicBlock* GetDominator() const { return dominator_; }
329 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100330 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.Add(block); }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000331
332 int NumberOfBackEdges() const {
333 return loop_information_ == nullptr
334 ? 0
335 : loop_information_->NumberOfBackEdges();
336 }
337
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100338 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
339 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100340 const HInstructionList& GetInstructions() const { return instructions_; }
341 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100342 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000343
344 void AddSuccessor(HBasicBlock* block) {
345 successors_.Add(block);
346 block->predecessors_.Add(this);
347 }
348
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100349 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
350 size_t successor_index = GetSuccessorIndexOf(existing);
351 DCHECK_NE(successor_index, static_cast<size_t>(-1));
352 existing->RemovePredecessor(this);
353 new_block->predecessors_.Add(this);
354 successors_.Put(successor_index, new_block);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000355 }
356
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100357 void RemovePredecessor(HBasicBlock* block) {
358 predecessors_.Delete(block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100359 }
360
361 void ClearAllPredecessors() {
362 predecessors_.Reset();
363 }
364
365 void AddPredecessor(HBasicBlock* block) {
366 predecessors_.Add(block);
367 block->successors_.Add(this);
368 }
369
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100370 void SwapPredecessors() {
Nicolas Geoffrayc83d4412014-09-18 16:46:20 +0100371 DCHECK_EQ(predecessors_.Size(), 2u);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100372 HBasicBlock* temp = predecessors_.Get(0);
373 predecessors_.Put(0, predecessors_.Get(1));
374 predecessors_.Put(1, temp);
375 }
376
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100377 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) {
378 for (size_t i = 0, e = predecessors_.Size(); i < e; ++i) {
379 if (predecessors_.Get(i) == predecessor) {
380 return i;
381 }
382 }
383 return -1;
384 }
385
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100386 size_t GetSuccessorIndexOf(HBasicBlock* successor) {
387 for (size_t i = 0, e = successors_.Size(); i < e; ++i) {
388 if (successors_.Get(i) == successor) {
389 return i;
390 }
391 }
392 return -1;
393 }
394
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000395 void AddInstruction(HInstruction* instruction);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100396 void RemoveInstruction(HInstruction* instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100397 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +0100398 // Replace instruction `initial` with `replacement` within this block.
399 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
400 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100401 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100402 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100403 void RemovePhi(HPhi* phi);
404
405 bool IsLoopHeader() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100406 return (loop_information_ != nullptr) && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100407 }
408
Roland Levillain6b879dd2014-09-22 17:13:44 +0100409 bool IsLoopPreHeaderFirstPredecessor() const {
410 DCHECK(IsLoopHeader());
411 DCHECK(!GetPredecessors().IsEmpty());
412 return GetPredecessors().Get(0) == GetLoopInformation()->GetPreHeader();
413 }
414
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100415 HLoopInformation* GetLoopInformation() const {
416 return loop_information_;
417 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000418
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100419 // Set the loop_information_ on this block. This method overrides the current
420 // loop_information if it is an outer loop of the passed loop information.
421 void SetInLoop(HLoopInformation* info) {
422 if (IsLoopHeader()) {
423 // Nothing to do. This just means `info` is an outer loop.
424 } else if (loop_information_ == nullptr) {
425 loop_information_ = info;
426 } else if (loop_information_->Contains(*info->GetHeader())) {
427 // Block is currently part of an outer loop. Make it part of this inner loop.
428 // Note that a non loop header having a loop information means this loop information
429 // has already been populated
430 loop_information_ = info;
431 } else {
432 // Block is part of an inner loop. Do not update the loop information.
433 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
434 // at this point, because this method is being called while populating `info`.
435 }
436 }
437
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100438 bool IsInLoop() const { return loop_information_ != nullptr; }
439
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100440 // Returns wheter this block dominates the blocked passed as parameter.
441 bool Dominates(HBasicBlock* block) const;
442
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100443 size_t GetLifetimeStart() const { return lifetime_start_; }
444 size_t GetLifetimeEnd() const { return lifetime_end_; }
445
446 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
447 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
448
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100449 uint32_t GetDexPc() const { return dex_pc_; }
450
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000451 private:
452 HGraph* const graph_;
453 GrowableArray<HBasicBlock*> predecessors_;
454 GrowableArray<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100455 HInstructionList instructions_;
456 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000457 HLoopInformation* loop_information_;
458 HBasicBlock* dominator_;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100459 GrowableArray<HBasicBlock*> dominated_blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000460 int block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100461 // The dex program counter of the first instruction of this block.
462 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100463 size_t lifetime_start_;
464 size_t lifetime_end_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000465
466 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
467};
468
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100469#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
470 M(Add, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000471 M(ArrayGet, Instruction) \
472 M(ArrayLength, Instruction) \
473 M(ArraySet, Instruction) \
474 M(BoundsCheck, Instruction) \
475 M(Compare, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100476 M(Condition, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000477 M(Div, BinaryOperation) \
478 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100479 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000480 M(Exit, Instruction) \
481 M(FloatConstant, Constant) \
482 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100483 M(GreaterThan, Condition) \
484 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100485 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000486 M(InstanceFieldGet, Instruction) \
487 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100488 M(IntConstant, Constant) \
489 M(InvokeStatic, Invoke) \
490 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000491 M(LessThan, Condition) \
492 M(LessThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100493 M(LoadLocal, Instruction) \
494 M(Local, Instruction) \
495 M(LongConstant, Constant) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000496 M(Mul, BinaryOperation) \
497 M(Neg, UnaryOperation) \
498 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100499 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +0100500 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000501 M(NotEqual, Condition) \
502 M(NullCheck, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100503 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000504 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100505 M(Phi, Instruction) \
506 M(Return, Instruction) \
507 M(ReturnVoid, Instruction) \
508 M(StoreLocal, Instruction) \
509 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100510 M(SuspendCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000511 M(Temporary, Instruction) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000512
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100513#define FOR_EACH_INSTRUCTION(M) \
514 FOR_EACH_CONCRETE_INSTRUCTION(M) \
515 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +0100516 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +0100517 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100518 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -0700519
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100520#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000521FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
522#undef FORWARD_DECLARATION
523
Roland Levillainccc07a92014-09-16 14:48:16 +0100524#define DECLARE_INSTRUCTION(type) \
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100525 virtual InstructionKind GetKind() const { return k##type; } \
Roland Levillainccc07a92014-09-16 14:48:16 +0100526 virtual const char* DebugName() const { return #type; } \
527 virtual const H##type* As##type() const OVERRIDE { return this; } \
528 virtual H##type* As##type() OVERRIDE { return this; } \
529 virtual bool InstructionTypeEquals(HInstruction* other) const { \
530 return other->Is##type(); \
531 } \
532 virtual void Accept(HGraphVisitor* visitor)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000533
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100534template <typename T>
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000535class HUseListNode : public ArenaObject {
536 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100537 HUseListNode(T* user, size_t index, HUseListNode* tail)
Dave Allison20dfc792014-06-16 20:44:29 -0700538 : user_(user), index_(index), tail_(tail) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000539
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000540 HUseListNode* GetTail() const { return tail_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100541 T* GetUser() const { return user_; }
542 size_t GetIndex() const { return index_; }
543
544 void SetTail(HUseListNode<T>* node) { tail_ = node; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000545
546 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100547 T* const user_;
548 const size_t index_;
549 HUseListNode<T>* tail_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000550
551 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
552};
553
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100554// Represents the side effects an instruction may have.
555class SideEffects : public ValueObject {
556 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100557 SideEffects() : flags_(0) {}
558
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100559 static SideEffects None() {
560 return SideEffects(0);
561 }
562
563 static SideEffects All() {
564 return SideEffects(ChangesSomething().flags_ | DependsOnSomething().flags_);
565 }
566
567 static SideEffects ChangesSomething() {
568 return SideEffects((1 << kFlagChangesCount) - 1);
569 }
570
571 static SideEffects DependsOnSomething() {
572 int count = kFlagDependsOnCount - kFlagChangesCount;
573 return SideEffects(((1 << count) - 1) << kFlagChangesCount);
574 }
575
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100576 SideEffects Union(SideEffects other) const {
577 return SideEffects(flags_ | other.flags_);
578 }
579
Roland Levillain72bceff2014-09-15 18:29:00 +0100580 bool HasSideEffects() const {
581 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
582 return (flags_ & all_bits_set) != 0;
583 }
584
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100585 bool HasAllSideEffects() const {
586 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
587 return all_bits_set == (flags_ & all_bits_set);
588 }
589
590 bool DependsOn(SideEffects other) const {
591 size_t depends_flags = other.ComputeDependsFlags();
592 return (flags_ & depends_flags) != 0;
593 }
594
595 bool HasDependencies() const {
596 int count = kFlagDependsOnCount - kFlagChangesCount;
597 size_t all_bits_set = (1 << count) - 1;
598 return ((flags_ >> kFlagChangesCount) & all_bits_set) != 0;
599 }
600
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100601 private:
602 static constexpr int kFlagChangesSomething = 0;
603 static constexpr int kFlagChangesCount = kFlagChangesSomething + 1;
604
605 static constexpr int kFlagDependsOnSomething = kFlagChangesCount;
606 static constexpr int kFlagDependsOnCount = kFlagDependsOnSomething + 1;
607
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100608 explicit SideEffects(size_t flags) : flags_(flags) {}
609
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100610 size_t ComputeDependsFlags() const {
611 return flags_ << kFlagChangesCount;
612 }
613
614 size_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100615};
616
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000617class HInstruction : public ArenaObject {
618 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100619 explicit HInstruction(SideEffects side_effects)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000620 : previous_(nullptr),
621 next_(nullptr),
622 block_(nullptr),
623 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100624 ssa_index_(-1),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000625 uses_(nullptr),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100626 env_uses_(nullptr),
627 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100628 locations_(nullptr),
629 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100630 lifetime_position_(kNoLifetime),
631 side_effects_(side_effects) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000632
Dave Allison20dfc792014-06-16 20:44:29 -0700633 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000634
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100635#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100636 enum InstructionKind {
637 FOR_EACH_INSTRUCTION(DECLARE_KIND)
638 };
639#undef DECLARE_KIND
640
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000641 HInstruction* GetNext() const { return next_; }
642 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000643
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000644 HBasicBlock* GetBlock() const { return block_; }
645 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100646 bool IsInBlock() const { return block_ != nullptr; }
647 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +0100648 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000649
Roland Levillain6b879dd2014-09-22 17:13:44 +0100650 virtual size_t InputCount() const = 0;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100651 virtual HInstruction* InputAt(size_t i) const = 0;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000652
653 virtual void Accept(HGraphVisitor* visitor) = 0;
654 virtual const char* DebugName() const = 0;
655
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100656 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100657 virtual void SetRawInputAt(size_t index, HInstruction* input) = 0;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100658
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100659 virtual bool NeedsEnvironment() const { return false; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100660 virtual bool IsControlFlow() const { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +0100661 virtual bool CanThrow() const { return false; }
Roland Levillain72bceff2014-09-15 18:29:00 +0100662 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100663
664 void AddUseAt(HInstruction* user, size_t index) {
665 uses_ = new (block_->GetGraph()->GetArena()) HUseListNode<HInstruction>(user, index, uses_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000666 }
667
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100668 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +0100669 DCHECK(user != nullptr);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100670 env_uses_ = new (block_->GetGraph()->GetArena()) HUseListNode<HEnvironment>(
671 user, index, env_uses_);
672 }
673
674 void RemoveUser(HInstruction* user, size_t index);
Nicolas Geoffray724c9632014-09-22 12:27:27 +0100675 void RemoveEnvironmentUser(HEnvironment* user, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100676
677 HUseListNode<HInstruction>* GetUses() const { return uses_; }
678 HUseListNode<HEnvironment>* GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000679
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100680 bool HasUses() const { return uses_ != nullptr || env_uses_ != nullptr; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100681 bool HasEnvironmentUses() const { return env_uses_ != nullptr; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000682
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100683 size_t NumberOfUses() const {
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100684 // TODO: Optimize this method if it is used outside of the HGraphVisualizer.
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100685 size_t result = 0;
686 HUseListNode<HInstruction>* current = uses_;
687 while (current != nullptr) {
688 current = current->GetTail();
689 ++result;
690 }
691 return result;
692 }
693
Roland Levillain6c82d402014-10-13 16:10:27 +0100694 // Does this instruction strictly dominate `other_instruction`?
695 // Returns false if this instruction and `other_instruction` are the same.
696 // Aborts if this instruction and `other_instruction` are both phis.
697 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +0100698
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000699 int GetId() const { return id_; }
700 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000701
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100702 int GetSsaIndex() const { return ssa_index_; }
703 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
704 bool HasSsaIndex() const { return ssa_index_ != -1; }
705
706 bool HasEnvironment() const { return environment_ != nullptr; }
707 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100708 void SetEnvironment(HEnvironment* environment) { environment_ = environment; }
709
Nicolas Geoffray39468442014-09-02 15:17:15 +0100710 // Returns the number of entries in the environment. Typically, that is the
711 // number of dex registers in a method. It could be more in case of inlining.
712 size_t EnvironmentSize() const;
713
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000714 LocationSummary* GetLocations() const { return locations_; }
715 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000716
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100717 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100718 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100719
Dave Allison20dfc792014-06-16 20:44:29 -0700720 bool HasOnlyOneUse() const {
721 return uses_ != nullptr && uses_->GetTail() == nullptr;
722 }
723
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100724#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +0100725 bool Is##type() const { return (As##type() != nullptr); } \
726 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000727 virtual H##type* As##type() { return nullptr; }
728
729 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
730#undef INSTRUCTION_TYPE_CHECK
731
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100732 // Returns whether the instruction can be moved within the graph.
733 virtual bool CanBeMoved() const { return false; }
734
735 // Returns whether the two instructions are of the same kind.
736 virtual bool InstructionTypeEquals(HInstruction* other) const { return false; }
737
738 // Returns whether any data encoded in the two instructions is equal.
739 // This method does not look at the inputs. Both instructions must be
740 // of the same type, otherwise the method has undefined behavior.
741 virtual bool InstructionDataEquals(HInstruction* other) const { return false; }
742
743 // Returns whether two instructions are equal, that is:
744 // 1) They have the same type and contain the same data,
745 // 2) Their inputs are identical.
746 bool Equals(HInstruction* other) const;
747
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100748 virtual InstructionKind GetKind() const = 0;
749
750 virtual size_t ComputeHashCode() const {
751 size_t result = GetKind();
752 for (size_t i = 0, e = InputCount(); i < e; ++i) {
753 result = (result * 31) + InputAt(i)->GetId();
754 }
755 return result;
756 }
757
758 SideEffects GetSideEffects() const { return side_effects_; }
759
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100760 size_t GetLifetimePosition() const { return lifetime_position_; }
761 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
762 LiveInterval* GetLiveInterval() const { return live_interval_; }
763 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
764 bool HasLiveInterval() const { return live_interval_ != nullptr; }
765
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000766 private:
767 HInstruction* previous_;
768 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000769 HBasicBlock* block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000770
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000771 // An instruction gets an id when it is added to the graph.
772 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +0100773 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000774 int id_;
775
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100776 // When doing liveness analysis, instructions that have uses get an SSA index.
777 int ssa_index_;
778
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100779 // List of instructions that have this instruction as input.
780 HUseListNode<HInstruction>* uses_;
781
782 // List of environments that contain this instruction.
783 HUseListNode<HEnvironment>* env_uses_;
784
Nicolas Geoffray39468442014-09-02 15:17:15 +0100785 // The environment associated with this instruction. Not null if the instruction
786 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100787 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000788
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000789 // Set by the code generator.
790 LocationSummary* locations_;
791
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100792 // Set by the liveness analysis.
793 LiveInterval* live_interval_;
794
795 // Set by the liveness analysis, this is the position in a linear
796 // order of blocks where this instruction's live interval start.
797 size_t lifetime_position_;
798
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100799 const SideEffects side_effects_;
800
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000801 friend class HBasicBlock;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100802 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000803
804 DISALLOW_COPY_AND_ASSIGN(HInstruction);
805};
806
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100807template<typename T>
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000808class HUseIterator : public ValueObject {
809 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100810 explicit HUseIterator(HUseListNode<T>* uses) : current_(uses) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000811
812 bool Done() const { return current_ == nullptr; }
813
814 void Advance() {
815 DCHECK(!Done());
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000816 current_ = current_->GetTail();
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000817 }
818
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100819 HUseListNode<T>* Current() const {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000820 DCHECK(!Done());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100821 return current_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000822 }
823
824 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100825 HUseListNode<T>* current_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000826
827 friend class HValue;
828};
829
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100830// A HEnvironment object contains the values of virtual registers at a given location.
831class HEnvironment : public ArenaObject {
832 public:
833 HEnvironment(ArenaAllocator* arena, size_t number_of_vregs) : vregs_(arena, number_of_vregs) {
834 vregs_.SetSize(number_of_vregs);
835 for (size_t i = 0; i < number_of_vregs; i++) {
836 vregs_.Put(i, nullptr);
837 }
838 }
839
840 void Populate(const GrowableArray<HInstruction*>& env) {
841 for (size_t i = 0; i < env.Size(); i++) {
842 HInstruction* instruction = env.Get(i);
843 vregs_.Put(i, instruction);
844 if (instruction != nullptr) {
845 instruction->AddEnvUseAt(this, i);
846 }
847 }
848 }
849
850 void SetRawEnvAt(size_t index, HInstruction* instruction) {
851 vregs_.Put(index, instruction);
852 }
853
Nicolas Geoffray39468442014-09-02 15:17:15 +0100854 HInstruction* GetInstructionAt(size_t index) const {
855 return vregs_.Get(index);
856 }
857
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100858 GrowableArray<HInstruction*>* GetVRegs() {
859 return &vregs_;
860 }
861
Nicolas Geoffray39468442014-09-02 15:17:15 +0100862 size_t Size() const { return vregs_.Size(); }
863
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100864 private:
865 GrowableArray<HInstruction*> vregs_;
866
867 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
868};
869
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000870class HInputIterator : public ValueObject {
871 public:
Dave Allison20dfc792014-06-16 20:44:29 -0700872 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000873
874 bool Done() const { return index_ == instruction_->InputCount(); }
875 HInstruction* Current() const { return instruction_->InputAt(index_); }
876 void Advance() { index_++; }
877
878 private:
879 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100880 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000881
882 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
883};
884
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000885class HInstructionIterator : public ValueObject {
886 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100887 explicit HInstructionIterator(const HInstructionList& instructions)
888 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000889 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000890 }
891
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000892 bool Done() const { return instruction_ == nullptr; }
893 HInstruction* Current() const { return instruction_; }
894 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000895 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000896 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000897 }
898
899 private:
900 HInstruction* instruction_;
901 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100902
903 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000904};
905
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100906class HBackwardInstructionIterator : public ValueObject {
907 public:
908 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
909 : instruction_(instructions.last_instruction_) {
910 next_ = Done() ? nullptr : instruction_->GetPrevious();
911 }
912
913 bool Done() const { return instruction_ == nullptr; }
914 HInstruction* Current() const { return instruction_; }
915 void Advance() {
916 instruction_ = next_;
917 next_ = Done() ? nullptr : instruction_->GetPrevious();
918 }
919
920 private:
921 HInstruction* instruction_;
922 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100923
924 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100925};
926
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000927// An embedded container with N elements of type T. Used (with partial
928// specialization for N=0) because embedded arrays cannot have size 0.
929template<typename T, intptr_t N>
930class EmbeddedArray {
931 public:
Dave Allison20dfc792014-06-16 20:44:29 -0700932 EmbeddedArray() : elements_() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000933
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000934 intptr_t GetLength() const { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000935
936 const T& operator[](intptr_t i) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000937 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000938 return elements_[i];
939 }
940
941 T& operator[](intptr_t i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000942 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000943 return elements_[i];
944 }
945
946 const T& At(intptr_t i) const {
947 return (*this)[i];
948 }
949
950 void SetAt(intptr_t i, const T& val) {
951 (*this)[i] = val;
952 }
953
954 private:
955 T elements_[N];
956};
957
958template<typename T>
959class EmbeddedArray<T, 0> {
960 public:
961 intptr_t length() const { return 0; }
962 const T& operator[](intptr_t i) const {
963 LOG(FATAL) << "Unreachable";
964 static T sentinel = 0;
965 return sentinel;
966 }
967 T& operator[](intptr_t i) {
968 LOG(FATAL) << "Unreachable";
969 static T sentinel = 0;
970 return sentinel;
971 }
972};
973
974template<intptr_t N>
975class HTemplateInstruction: public HInstruction {
976 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100977 HTemplateInstruction<N>(SideEffects side_effects)
978 : HInstruction(side_effects), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -0700979 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000980
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100981 virtual size_t InputCount() const { return N; }
982 virtual HInstruction* InputAt(size_t i) const { return inputs_[i]; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000983
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000984 protected:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100985 virtual void SetRawInputAt(size_t i, HInstruction* instruction) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000986 inputs_[i] = instruction;
987 }
988
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000989 private:
990 EmbeddedArray<HInstruction*, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100991
992 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000993};
994
Dave Allison20dfc792014-06-16 20:44:29 -0700995template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100996class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -0700997 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100998 HExpression<N>(Primitive::Type type, SideEffects side_effects)
999 : HTemplateInstruction<N>(side_effects), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07001000 virtual ~HExpression() {}
1001
1002 virtual Primitive::Type GetType() const { return type_; }
1003
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001004 protected:
1005 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07001006};
1007
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001008// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
1009// instruction that branches to the exit block.
1010class HReturnVoid : public HTemplateInstruction<0> {
1011 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001012 HReturnVoid() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001013
1014 virtual bool IsControlFlow() const { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001015
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001016 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001017
1018 private:
1019 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
1020};
1021
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001022// Represents dex's RETURN opcodes. A HReturn is a control flow
1023// instruction that branches to the exit block.
1024class HReturn : public HTemplateInstruction<1> {
1025 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001026 explicit HReturn(HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001027 SetRawInputAt(0, value);
1028 }
1029
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001030 virtual bool IsControlFlow() const { return true; }
1031
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001032 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001033
1034 private:
1035 DISALLOW_COPY_AND_ASSIGN(HReturn);
1036};
1037
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001038// The exit instruction is the only instruction of the exit block.
1039// Instructions aborting the method (HTrow and HReturn) must branch to the
1040// exit block.
1041class HExit : public HTemplateInstruction<0> {
1042 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001043 HExit() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001044
1045 virtual bool IsControlFlow() const { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001046
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001047 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001048
1049 private:
1050 DISALLOW_COPY_AND_ASSIGN(HExit);
1051};
1052
1053// Jumps from one block to another.
1054class HGoto : public HTemplateInstruction<0> {
1055 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001056 HGoto() : HTemplateInstruction(SideEffects::None()) {}
1057
1058 virtual bool IsControlFlow() const { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001059
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001060 HBasicBlock* GetSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001061 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001062 }
1063
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001064 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001065
1066 private:
1067 DISALLOW_COPY_AND_ASSIGN(HGoto);
1068};
1069
Dave Allison20dfc792014-06-16 20:44:29 -07001070
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001071// Conditional branch. A block ending with an HIf instruction must have
1072// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001073class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001074 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001075 explicit HIf(HInstruction* input) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001076 SetRawInputAt(0, input);
1077 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001078
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001079 virtual bool IsControlFlow() const { return true; }
1080
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001081 HBasicBlock* IfTrueSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001082 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001083 }
1084
1085 HBasicBlock* IfFalseSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001086 return GetBlock()->GetSuccessors().Get(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001087 }
1088
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001089 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001090
Dave Allison20dfc792014-06-16 20:44:29 -07001091 virtual bool IsIfInstruction() const { return true; }
1092
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001093 private:
1094 DISALLOW_COPY_AND_ASSIGN(HIf);
1095};
1096
Roland Levillain88cb1752014-10-20 16:36:47 +01001097class HUnaryOperation : public HExpression<1> {
1098 public:
1099 HUnaryOperation(Primitive::Type result_type, HInstruction* input)
1100 : HExpression(result_type, SideEffects::None()) {
1101 SetRawInputAt(0, input);
1102 }
1103
1104 HInstruction* GetInput() const { return InputAt(0); }
1105 Primitive::Type GetResultType() const { return GetType(); }
1106
1107 virtual bool CanBeMoved() const { return true; }
1108 virtual bool InstructionDataEquals(HInstruction* other) const { return true; }
1109
Roland Levillain9240d6a2014-10-20 16:47:04 +01001110 // Try to statically evaluate `operation` and return a HConstant
1111 // containing the result of this evaluation. If `operation` cannot
1112 // be evaluated as a constant, return nullptr.
1113 HConstant* TryStaticEvaluation() const;
1114
1115 // Apply this operation to `x`.
1116 virtual int32_t Evaluate(int32_t x) const = 0;
1117 virtual int64_t Evaluate(int64_t x) const = 0;
1118
Roland Levillain88cb1752014-10-20 16:36:47 +01001119 DECLARE_INSTRUCTION(UnaryOperation);
1120
1121 private:
1122 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
1123};
1124
Dave Allison20dfc792014-06-16 20:44:29 -07001125class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001126 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001127 HBinaryOperation(Primitive::Type result_type,
1128 HInstruction* left,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001129 HInstruction* right) : HExpression(result_type, SideEffects::None()) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001130 SetRawInputAt(0, left);
1131 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001132 }
1133
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001134 HInstruction* GetLeft() const { return InputAt(0); }
1135 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07001136 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001137
1138 virtual bool IsCommutative() { return false; }
1139
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001140 virtual bool CanBeMoved() const { return true; }
1141 virtual bool InstructionDataEquals(HInstruction* other) const { return true; }
1142
Roland Levillain9240d6a2014-10-20 16:47:04 +01001143 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01001144 // containing the result of this evaluation. If `operation` cannot
1145 // be evaluated as a constant, return nullptr.
Roland Levillain9240d6a2014-10-20 16:47:04 +01001146 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01001147
1148 // Apply this operation to `x` and `y`.
1149 virtual int32_t Evaluate(int32_t x, int32_t y) const = 0;
1150 virtual int64_t Evaluate(int64_t x, int64_t y) const = 0;
1151
Roland Levillainccc07a92014-09-16 14:48:16 +01001152 DECLARE_INSTRUCTION(BinaryOperation);
1153
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001154 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001155 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
1156};
1157
Dave Allison20dfc792014-06-16 20:44:29 -07001158class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001159 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001160 HCondition(HInstruction* first, HInstruction* second)
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001161 : HBinaryOperation(Primitive::kPrimBoolean, first, second),
1162 needs_materialization_(true) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001163
1164 virtual bool IsCommutative() { return true; }
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001165
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001166 bool NeedsMaterialization() const { return needs_materialization_; }
1167 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001168
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001169 // For code generation purposes, returns whether this instruction is just before
1170 // `if_`, and disregard moves in between.
1171 bool IsBeforeWhenDisregardMoves(HIf* if_) const;
1172
Dave Allison20dfc792014-06-16 20:44:29 -07001173 DECLARE_INSTRUCTION(Condition);
1174
1175 virtual IfCondition GetCondition() const = 0;
1176
1177 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001178 // For register allocation purposes, returns whether this instruction needs to be
1179 // materialized (that is, not just be in the processor flags).
1180 bool needs_materialization_;
1181
Dave Allison20dfc792014-06-16 20:44:29 -07001182 DISALLOW_COPY_AND_ASSIGN(HCondition);
1183};
1184
1185// Instruction to check if two inputs are equal to each other.
1186class HEqual : public HCondition {
1187 public:
1188 HEqual(HInstruction* first, HInstruction* second)
1189 : HCondition(first, second) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001190
Roland Levillain93445682014-10-06 19:24:02 +01001191 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1192 return x == y ? 1 : 0;
1193 }
1194 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1195 return x == y ? 1 : 0;
1196 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001197
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001198 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001199
Dave Allison20dfc792014-06-16 20:44:29 -07001200 virtual IfCondition GetCondition() const {
1201 return kCondEQ;
1202 }
1203
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001204 private:
1205 DISALLOW_COPY_AND_ASSIGN(HEqual);
1206};
1207
Dave Allison20dfc792014-06-16 20:44:29 -07001208class HNotEqual : public HCondition {
1209 public:
1210 HNotEqual(HInstruction* first, HInstruction* second)
1211 : HCondition(first, second) {}
1212
Roland Levillain93445682014-10-06 19:24:02 +01001213 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1214 return x != y ? 1 : 0;
1215 }
1216 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1217 return x != y ? 1 : 0;
1218 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001219
Dave Allison20dfc792014-06-16 20:44:29 -07001220 DECLARE_INSTRUCTION(NotEqual);
1221
1222 virtual IfCondition GetCondition() const {
1223 return kCondNE;
1224 }
1225
1226 private:
1227 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
1228};
1229
1230class HLessThan : public HCondition {
1231 public:
1232 HLessThan(HInstruction* first, HInstruction* second)
1233 : HCondition(first, second) {}
1234
Roland Levillain93445682014-10-06 19:24:02 +01001235 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1236 return x < y ? 1 : 0;
1237 }
1238 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1239 return x < y ? 1 : 0;
1240 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001241
Dave Allison20dfc792014-06-16 20:44:29 -07001242 DECLARE_INSTRUCTION(LessThan);
1243
1244 virtual IfCondition GetCondition() const {
1245 return kCondLT;
1246 }
1247
1248 private:
1249 DISALLOW_COPY_AND_ASSIGN(HLessThan);
1250};
1251
1252class HLessThanOrEqual : public HCondition {
1253 public:
1254 HLessThanOrEqual(HInstruction* first, HInstruction* second)
1255 : HCondition(first, second) {}
1256
Roland Levillain93445682014-10-06 19:24:02 +01001257 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1258 return x <= y ? 1 : 0;
1259 }
1260 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1261 return x <= y ? 1 : 0;
1262 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001263
Dave Allison20dfc792014-06-16 20:44:29 -07001264 DECLARE_INSTRUCTION(LessThanOrEqual);
1265
1266 virtual IfCondition GetCondition() const {
1267 return kCondLE;
1268 }
1269
1270 private:
1271 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
1272};
1273
1274class HGreaterThan : public HCondition {
1275 public:
1276 HGreaterThan(HInstruction* first, HInstruction* second)
1277 : HCondition(first, second) {}
1278
Roland Levillain93445682014-10-06 19:24:02 +01001279 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1280 return x > y ? 1 : 0;
1281 }
1282 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1283 return x > y ? 1 : 0;
1284 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001285
Dave Allison20dfc792014-06-16 20:44:29 -07001286 DECLARE_INSTRUCTION(GreaterThan);
1287
1288 virtual IfCondition GetCondition() const {
1289 return kCondGT;
1290 }
1291
1292 private:
1293 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
1294};
1295
1296class HGreaterThanOrEqual : public HCondition {
1297 public:
1298 HGreaterThanOrEqual(HInstruction* first, HInstruction* second)
1299 : HCondition(first, second) {}
1300
Roland Levillain93445682014-10-06 19:24:02 +01001301 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1302 return x >= y ? 1 : 0;
1303 }
1304 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1305 return x >= y ? 1 : 0;
1306 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001307
Dave Allison20dfc792014-06-16 20:44:29 -07001308 DECLARE_INSTRUCTION(GreaterThanOrEqual);
1309
1310 virtual IfCondition GetCondition() const {
1311 return kCondGE;
1312 }
1313
1314 private:
1315 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
1316};
1317
1318
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001319// Instruction to check how two inputs compare to each other.
1320// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
1321class HCompare : public HBinaryOperation {
1322 public:
1323 HCompare(Primitive::Type type, HInstruction* first, HInstruction* second)
1324 : HBinaryOperation(Primitive::kPrimInt, first, second) {
1325 DCHECK_EQ(type, first->GetType());
1326 DCHECK_EQ(type, second->GetType());
1327 }
1328
Roland Levillain93445682014-10-06 19:24:02 +01001329 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001330 return
1331 x == y ? 0 :
1332 x > y ? 1 :
1333 -1;
1334 }
Roland Levillain93445682014-10-06 19:24:02 +01001335 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001336 return
1337 x == y ? 0 :
1338 x > y ? 1 :
1339 -1;
1340 }
1341
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001342 DECLARE_INSTRUCTION(Compare);
1343
1344 private:
1345 DISALLOW_COPY_AND_ASSIGN(HCompare);
1346};
1347
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001348// A local in the graph. Corresponds to a Dex register.
1349class HLocal : public HTemplateInstruction<0> {
1350 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001351 explicit HLocal(uint16_t reg_number)
1352 : HTemplateInstruction(SideEffects::None()), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001353
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001354 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001355
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001356 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001357
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001358 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001359 // The Dex register number.
1360 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001361
1362 DISALLOW_COPY_AND_ASSIGN(HLocal);
1363};
1364
1365// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07001366class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001367 public:
Roland Levillain5799fc02014-09-25 12:15:20 +01001368 HLoadLocal(HLocal* local, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001369 : HExpression(type, SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001370 SetRawInputAt(0, local);
1371 }
1372
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001373 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1374
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001375 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001376
1377 private:
1378 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
1379};
1380
1381// Store a value in a given local. This instruction has two inputs: the value
1382// and the local.
1383class HStoreLocal : public HTemplateInstruction<2> {
1384 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001385 HStoreLocal(HLocal* local, HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001386 SetRawInputAt(0, local);
1387 SetRawInputAt(1, value);
1388 }
1389
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001390 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1391
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001392 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001393
1394 private:
1395 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
1396};
1397
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001398class HConstant : public HExpression<0> {
1399 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001400 explicit HConstant(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
1401
1402 virtual bool CanBeMoved() const { return true; }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001403
1404 DECLARE_INSTRUCTION(Constant);
1405
1406 private:
1407 DISALLOW_COPY_AND_ASSIGN(HConstant);
1408};
1409
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001410class HFloatConstant : public HConstant {
1411 public:
1412 explicit HFloatConstant(float value) : HConstant(Primitive::kPrimFloat), value_(value) {}
1413
1414 float GetValue() const { return value_; }
1415
1416 virtual bool InstructionDataEquals(HInstruction* other) const {
1417 return bit_cast<float, int32_t>(other->AsFloatConstant()->value_) ==
1418 bit_cast<float, int32_t>(value_);
1419 }
1420
1421 virtual size_t ComputeHashCode() const { return static_cast<size_t>(GetValue()); }
1422
1423 DECLARE_INSTRUCTION(FloatConstant);
1424
1425 private:
1426 const float value_;
1427
1428 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
1429};
1430
1431class HDoubleConstant : public HConstant {
1432 public:
1433 explicit HDoubleConstant(double value) : HConstant(Primitive::kPrimDouble), value_(value) {}
1434
1435 double GetValue() const { return value_; }
1436
1437 virtual bool InstructionDataEquals(HInstruction* other) const {
1438 return bit_cast<double, int64_t>(other->AsDoubleConstant()->value_) ==
1439 bit_cast<double, int64_t>(value_);
1440 }
1441
1442 virtual size_t ComputeHashCode() const { return static_cast<size_t>(GetValue()); }
1443
1444 DECLARE_INSTRUCTION(DoubleConstant);
1445
1446 private:
1447 const double value_;
1448
1449 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
1450};
1451
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001452// Constants of the type int. Those can be from Dex instructions, or
1453// synthesized (for example with the if-eqz instruction).
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001454class HIntConstant : public HConstant {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001455 public:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001456 explicit HIntConstant(int32_t value) : HConstant(Primitive::kPrimInt), value_(value) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001457
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001458 int32_t GetValue() const { return value_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001459
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001460 virtual bool InstructionDataEquals(HInstruction* other) const {
1461 return other->AsIntConstant()->value_ == value_;
1462 }
1463
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001464 virtual size_t ComputeHashCode() const { return GetValue(); }
1465
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001466 DECLARE_INSTRUCTION(IntConstant);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001467
1468 private:
1469 const int32_t value_;
1470
1471 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
1472};
1473
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001474class HLongConstant : public HConstant {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001475 public:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001476 explicit HLongConstant(int64_t value) : HConstant(Primitive::kPrimLong), value_(value) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001477
1478 int64_t GetValue() const { return value_; }
1479
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001480 virtual bool InstructionDataEquals(HInstruction* other) const {
1481 return other->AsLongConstant()->value_ == value_;
1482 }
1483
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001484 virtual size_t ComputeHashCode() const { return static_cast<size_t>(GetValue()); }
1485
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001486 DECLARE_INSTRUCTION(LongConstant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001487
1488 private:
1489 const int64_t value_;
1490
1491 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
1492};
1493
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001494class HInvoke : public HInstruction {
1495 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001496 HInvoke(ArenaAllocator* arena,
1497 uint32_t number_of_arguments,
1498 Primitive::Type return_type,
1499 uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001500 : HInstruction(SideEffects::All()),
1501 inputs_(arena, number_of_arguments),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001502 return_type_(return_type),
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001503 dex_pc_(dex_pc) {
1504 inputs_.SetSize(number_of_arguments);
1505 }
1506
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001507 virtual size_t InputCount() const { return inputs_.Size(); }
1508 virtual HInstruction* InputAt(size_t i) const { return inputs_.Get(i); }
1509
1510 // Runtime needs to walk the stack, so Dex -> Dex calls need to
1511 // know their environment.
1512 virtual bool NeedsEnvironment() const { return true; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001513
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001514 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001515 SetRawInputAt(index, argument);
1516 }
1517
1518 virtual void SetRawInputAt(size_t index, HInstruction* input) {
1519 inputs_.Put(index, input);
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001520 }
1521
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001522 virtual Primitive::Type GetType() const { return return_type_; }
1523
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001524 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001525
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001526 DECLARE_INSTRUCTION(Invoke);
1527
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001528 protected:
1529 GrowableArray<HInstruction*> inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001530 const Primitive::Type return_type_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001531 const uint32_t dex_pc_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001532
1533 private:
1534 DISALLOW_COPY_AND_ASSIGN(HInvoke);
1535};
1536
1537class HInvokeStatic : public HInvoke {
1538 public:
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001539 HInvokeStatic(ArenaAllocator* arena,
1540 uint32_t number_of_arguments,
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001541 Primitive::Type return_type,
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001542 uint32_t dex_pc,
1543 uint32_t index_in_dex_cache)
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001544 : HInvoke(arena, number_of_arguments, return_type, dex_pc),
1545 index_in_dex_cache_(index_in_dex_cache) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001546
1547 uint32_t GetIndexInDexCache() const { return index_in_dex_cache_; }
1548
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001549 DECLARE_INSTRUCTION(InvokeStatic);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001550
1551 private:
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001552 const uint32_t index_in_dex_cache_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001553
1554 DISALLOW_COPY_AND_ASSIGN(HInvokeStatic);
1555};
1556
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001557class HInvokeVirtual : public HInvoke {
1558 public:
1559 HInvokeVirtual(ArenaAllocator* arena,
1560 uint32_t number_of_arguments,
1561 Primitive::Type return_type,
1562 uint32_t dex_pc,
1563 uint32_t vtable_index)
1564 : HInvoke(arena, number_of_arguments, return_type, dex_pc),
1565 vtable_index_(vtable_index) {}
1566
1567 uint32_t GetVTableIndex() const { return vtable_index_; }
1568
1569 DECLARE_INSTRUCTION(InvokeVirtual);
1570
1571 private:
1572 const uint32_t vtable_index_;
1573
1574 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
1575};
1576
Dave Allison20dfc792014-06-16 20:44:29 -07001577class HNewInstance : public HExpression<0> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001578 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001579 HNewInstance(uint32_t dex_pc, uint16_t type_index)
1580 : HExpression(Primitive::kPrimNot, SideEffects::None()),
1581 dex_pc_(dex_pc),
1582 type_index_(type_index) {}
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001583
1584 uint32_t GetDexPc() const { return dex_pc_; }
1585 uint16_t GetTypeIndex() const { return type_index_; }
1586
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001587 // Calls runtime so needs an environment.
1588 virtual bool NeedsEnvironment() const { return true; }
1589
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001590 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001591
1592 private:
1593 const uint32_t dex_pc_;
1594 const uint16_t type_index_;
1595
1596 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
1597};
1598
Roland Levillain88cb1752014-10-20 16:36:47 +01001599class HNeg : public HUnaryOperation {
1600 public:
1601 explicit HNeg(Primitive::Type result_type, HInstruction* input)
1602 : HUnaryOperation(result_type, input) {}
1603
Roland Levillain9240d6a2014-10-20 16:47:04 +01001604 virtual int32_t Evaluate(int32_t x) const OVERRIDE { return -x; }
1605 virtual int64_t Evaluate(int64_t x) const OVERRIDE { return -x; }
1606
Roland Levillain88cb1752014-10-20 16:36:47 +01001607 DECLARE_INSTRUCTION(Neg);
1608
1609 private:
1610 DISALLOW_COPY_AND_ASSIGN(HNeg);
1611};
1612
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001613class HNewArray : public HExpression<1> {
1614 public:
1615 HNewArray(HInstruction* length, uint32_t dex_pc, uint16_t type_index)
1616 : HExpression(Primitive::kPrimNot, SideEffects::None()),
1617 dex_pc_(dex_pc),
1618 type_index_(type_index) {
1619 SetRawInputAt(0, length);
1620 }
1621
1622 uint32_t GetDexPc() const { return dex_pc_; }
1623 uint16_t GetTypeIndex() const { return type_index_; }
1624
1625 // Calls runtime so needs an environment.
1626 virtual bool NeedsEnvironment() const { return true; }
1627
1628 DECLARE_INSTRUCTION(NewArray);
1629
1630 private:
1631 const uint32_t dex_pc_;
1632 const uint16_t type_index_;
1633
1634 DISALLOW_COPY_AND_ASSIGN(HNewArray);
1635};
1636
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001637class HAdd : public HBinaryOperation {
1638 public:
1639 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1640 : HBinaryOperation(result_type, left, right) {}
1641
1642 virtual bool IsCommutative() { return true; }
1643
Roland Levillain93445682014-10-06 19:24:02 +01001644 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1645 return x + y;
1646 }
1647 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1648 return x + y;
1649 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001650
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001651 DECLARE_INSTRUCTION(Add);
1652
1653 private:
1654 DISALLOW_COPY_AND_ASSIGN(HAdd);
1655};
1656
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001657class HSub : public HBinaryOperation {
1658 public:
1659 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1660 : HBinaryOperation(result_type, left, right) {}
1661
Roland Levillain93445682014-10-06 19:24:02 +01001662 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1663 return x - y;
1664 }
1665 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1666 return x - y;
1667 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001668
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001669 DECLARE_INSTRUCTION(Sub);
1670
1671 private:
1672 DISALLOW_COPY_AND_ASSIGN(HSub);
1673};
1674
Calin Juravle34bacdf2014-10-07 20:23:36 +01001675class HMul : public HBinaryOperation {
1676 public:
1677 HMul(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1678 : HBinaryOperation(result_type, left, right) {}
1679
1680 virtual bool IsCommutative() { return true; }
1681
1682 virtual int32_t Evaluate(int32_t x, int32_t y) const { return x * y; }
1683 virtual int64_t Evaluate(int64_t x, int64_t y) const { return x * y; }
1684
1685 DECLARE_INSTRUCTION(Mul);
1686
1687 private:
1688 DISALLOW_COPY_AND_ASSIGN(HMul);
1689};
1690
Calin Juravle7c4954d2014-10-28 16:57:40 +00001691class HDiv : public HBinaryOperation {
1692 public:
1693 HDiv(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1694 : HBinaryOperation(result_type, left, right) {}
1695
1696 virtual int32_t Evaluate(int32_t x, int32_t y) const { return x / y; }
1697 virtual int64_t Evaluate(int64_t x, int64_t y) const { return x / y; }
1698
1699 DECLARE_INSTRUCTION(Div);
1700
1701 private:
1702 DISALLOW_COPY_AND_ASSIGN(HDiv);
1703};
1704
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001705// The value of a parameter in this method. Its location depends on
1706// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07001707class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001708 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001709 HParameterValue(uint8_t index, Primitive::Type parameter_type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001710 : HExpression(parameter_type, SideEffects::None()), index_(index) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001711
1712 uint8_t GetIndex() const { return index_; }
1713
1714 DECLARE_INSTRUCTION(ParameterValue);
1715
1716 private:
1717 // The index of this parameter in the parameters list. Must be less
1718 // than HGraph::number_of_in_vregs_;
1719 const uint8_t index_;
1720
1721 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
1722};
1723
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001724class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001725 public:
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001726 explicit HNot(Primitive::Type result_type, HInstruction* input)
1727 : HUnaryOperation(result_type, input) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001728
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001729 virtual bool CanBeMoved() const { return true; }
1730 virtual bool InstructionDataEquals(HInstruction* other) const { return true; }
1731
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001732 virtual int32_t Evaluate(int32_t x) const OVERRIDE { return ~x; }
1733 virtual int64_t Evaluate(int64_t x) const OVERRIDE { return ~x; }
1734
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001735 DECLARE_INSTRUCTION(Not);
1736
1737 private:
1738 DISALLOW_COPY_AND_ASSIGN(HNot);
1739};
1740
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001741class HPhi : public HInstruction {
1742 public:
1743 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001744 : HInstruction(SideEffects::None()),
1745 inputs_(arena, number_of_inputs),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001746 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001747 type_(type),
1748 is_live_(false) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001749 inputs_.SetSize(number_of_inputs);
1750 }
1751
1752 virtual size_t InputCount() const { return inputs_.Size(); }
1753 virtual HInstruction* InputAt(size_t i) const { return inputs_.Get(i); }
1754
1755 virtual void SetRawInputAt(size_t index, HInstruction* input) {
1756 inputs_.Put(index, input);
1757 }
1758
1759 void AddInput(HInstruction* input);
1760
1761 virtual Primitive::Type GetType() const { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001762 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001763
1764 uint32_t GetRegNumber() const { return reg_number_; }
1765
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001766 void SetDead() { is_live_ = false; }
1767 void SetLive() { is_live_ = true; }
1768 bool IsDead() const { return !is_live_; }
1769 bool IsLive() const { return is_live_; }
1770
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001771 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001772
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001773 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001774 GrowableArray<HInstruction*> inputs_;
1775 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001776 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001777 bool is_live_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001778
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001779 DISALLOW_COPY_AND_ASSIGN(HPhi);
1780};
1781
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001782class HNullCheck : public HExpression<1> {
1783 public:
1784 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001785 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001786 SetRawInputAt(0, value);
1787 }
1788
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001789 virtual bool CanBeMoved() const { return true; }
1790 virtual bool InstructionDataEquals(HInstruction* other) const { return true; }
1791
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001792 virtual bool NeedsEnvironment() const { return true; }
1793
Roland Levillaine161a2a2014-10-03 12:45:18 +01001794 virtual bool CanThrow() const { return true; }
1795
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001796 uint32_t GetDexPc() const { return dex_pc_; }
1797
1798 DECLARE_INSTRUCTION(NullCheck);
1799
1800 private:
1801 const uint32_t dex_pc_;
1802
1803 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
1804};
1805
1806class FieldInfo : public ValueObject {
1807 public:
Roland Levillain5799fc02014-09-25 12:15:20 +01001808 FieldInfo(MemberOffset field_offset, Primitive::Type field_type)
Nicolas Geoffray39468442014-09-02 15:17:15 +01001809 : field_offset_(field_offset), field_type_(field_type) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001810
1811 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001812 Primitive::Type GetFieldType() const { return field_type_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001813
1814 private:
1815 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01001816 const Primitive::Type field_type_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001817};
1818
1819class HInstanceFieldGet : public HExpression<1> {
1820 public:
1821 HInstanceFieldGet(HInstruction* value,
1822 Primitive::Type field_type,
1823 MemberOffset field_offset)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001824 : HExpression(field_type, SideEffects::DependsOnSomething()),
1825 field_info_(field_offset, field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001826 SetRawInputAt(0, value);
1827 }
1828
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001829 virtual bool CanBeMoved() const { return true; }
1830 virtual bool InstructionDataEquals(HInstruction* other) const {
1831 size_t other_offset = other->AsInstanceFieldGet()->GetFieldOffset().SizeValue();
1832 return other_offset == GetFieldOffset().SizeValue();
1833 }
1834
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001835 virtual size_t ComputeHashCode() const {
1836 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
1837 }
1838
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001839 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001840 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001841
1842 DECLARE_INSTRUCTION(InstanceFieldGet);
1843
1844 private:
1845 const FieldInfo field_info_;
1846
1847 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
1848};
1849
1850class HInstanceFieldSet : public HTemplateInstruction<2> {
1851 public:
1852 HInstanceFieldSet(HInstruction* object,
1853 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01001854 Primitive::Type field_type,
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001855 MemberOffset field_offset)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001856 : HTemplateInstruction(SideEffects::ChangesSomething()),
1857 field_info_(field_offset, field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001858 SetRawInputAt(0, object);
1859 SetRawInputAt(1, value);
1860 }
1861
1862 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001863 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001864
1865 DECLARE_INSTRUCTION(InstanceFieldSet);
1866
1867 private:
1868 const FieldInfo field_info_;
1869
1870 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
1871};
1872
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001873class HArrayGet : public HExpression<2> {
1874 public:
1875 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001876 : HExpression(type, SideEffects::DependsOnSomething()) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001877 SetRawInputAt(0, array);
1878 SetRawInputAt(1, index);
1879 }
1880
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001881 virtual bool CanBeMoved() const { return true; }
1882 virtual bool InstructionDataEquals(HInstruction* other) const { return true; }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001883 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001884
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001885 DECLARE_INSTRUCTION(ArrayGet);
1886
1887 private:
1888 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
1889};
1890
1891class HArraySet : public HTemplateInstruction<3> {
1892 public:
1893 HArraySet(HInstruction* array,
1894 HInstruction* index,
1895 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001896 Primitive::Type expected_component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001897 uint32_t dex_pc)
1898 : HTemplateInstruction(SideEffects::ChangesSomething()),
1899 dex_pc_(dex_pc),
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001900 expected_component_type_(expected_component_type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001901 SetRawInputAt(0, array);
1902 SetRawInputAt(1, index);
1903 SetRawInputAt(2, value);
1904 }
1905
1906 virtual bool NeedsEnvironment() const {
1907 // We currently always call a runtime method to catch array store
1908 // exceptions.
1909 return InputAt(2)->GetType() == Primitive::kPrimNot;
1910 }
1911
1912 uint32_t GetDexPc() const { return dex_pc_; }
1913
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001914 HInstruction* GetValue() const { return InputAt(2); }
1915
1916 Primitive::Type GetComponentType() const {
1917 // The Dex format does not type floating point index operations. Since the
1918 // `expected_component_type_` is set during building and can therefore not
1919 // be correct, we also check what is the value type. If it is a floating
1920 // point type, we must use that type.
1921 Primitive::Type value_type = GetValue()->GetType();
1922 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
1923 ? value_type
1924 : expected_component_type_;
1925 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001926
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001927 DECLARE_INSTRUCTION(ArraySet);
1928
1929 private:
1930 const uint32_t dex_pc_;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001931 const Primitive::Type expected_component_type_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001932
1933 DISALLOW_COPY_AND_ASSIGN(HArraySet);
1934};
1935
1936class HArrayLength : public HExpression<1> {
1937 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001938 explicit HArrayLength(HInstruction* array)
1939 : HExpression(Primitive::kPrimInt, SideEffects::None()) {
1940 // Note that arrays do not change length, so the instruction does not
1941 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001942 SetRawInputAt(0, array);
1943 }
1944
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001945 virtual bool CanBeMoved() const { return true; }
1946 virtual bool InstructionDataEquals(HInstruction* other) const { return true; }
1947
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001948 DECLARE_INSTRUCTION(ArrayLength);
1949
1950 private:
1951 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
1952};
1953
1954class HBoundsCheck : public HExpression<2> {
1955 public:
1956 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001957 : HExpression(index->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001958 DCHECK(index->GetType() == Primitive::kPrimInt);
1959 SetRawInputAt(0, index);
1960 SetRawInputAt(1, length);
1961 }
1962
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001963 virtual bool CanBeMoved() const { return true; }
1964 virtual bool InstructionDataEquals(HInstruction* other) const { return true; }
1965
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001966 virtual bool NeedsEnvironment() const { return true; }
1967
Roland Levillaine161a2a2014-10-03 12:45:18 +01001968 virtual bool CanThrow() const { return true; }
1969
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001970 uint32_t GetDexPc() const { return dex_pc_; }
1971
1972 DECLARE_INSTRUCTION(BoundsCheck);
1973
1974 private:
1975 const uint32_t dex_pc_;
1976
1977 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
1978};
1979
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001980/**
1981 * Some DEX instructions are folded into multiple HInstructions that need
1982 * to stay live until the last HInstruction. This class
1983 * is used as a marker for the baseline compiler to ensure its preceding
1984 * HInstruction stays live. `index` is the temporary number that is used
1985 * for knowing the stack offset where to store the instruction.
1986 */
1987class HTemporary : public HTemplateInstruction<0> {
1988 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001989 explicit HTemporary(size_t index) : HTemplateInstruction(SideEffects::None()), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001990
1991 size_t GetIndex() const { return index_; }
1992
1993 DECLARE_INSTRUCTION(Temporary);
1994
1995 private:
1996 const size_t index_;
1997
1998 DISALLOW_COPY_AND_ASSIGN(HTemporary);
1999};
2000
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002001class HSuspendCheck : public HTemplateInstruction<0> {
2002 public:
2003 explicit HSuspendCheck(uint32_t dex_pc)
Nicolas Geoffray9ebc72c2014-09-25 16:33:42 +01002004 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002005
2006 virtual bool NeedsEnvironment() const {
2007 return true;
2008 }
2009
2010 uint32_t GetDexPc() const { return dex_pc_; }
2011
2012 DECLARE_INSTRUCTION(SuspendCheck);
2013
2014 private:
2015 const uint32_t dex_pc_;
2016
2017 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
2018};
2019
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002020class MoveOperands : public ArenaObject {
2021 public:
Nicolas Geoffray740475d2014-09-29 10:33:25 +01002022 MoveOperands(Location source, Location destination, HInstruction* instruction)
2023 : source_(source), destination_(destination), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002024
2025 Location GetSource() const { return source_; }
2026 Location GetDestination() const { return destination_; }
2027
2028 void SetSource(Location value) { source_ = value; }
2029 void SetDestination(Location value) { destination_ = value; }
2030
2031 // The parallel move resolver marks moves as "in-progress" by clearing the
2032 // destination (but not the source).
2033 Location MarkPending() {
2034 DCHECK(!IsPending());
2035 Location dest = destination_;
2036 destination_ = Location::NoLocation();
2037 return dest;
2038 }
2039
2040 void ClearPending(Location dest) {
2041 DCHECK(IsPending());
2042 destination_ = dest;
2043 }
2044
2045 bool IsPending() const {
2046 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
2047 return destination_.IsInvalid() && !source_.IsInvalid();
2048 }
2049
2050 // True if this blocks a move from the given location.
2051 bool Blocks(Location loc) const {
2052 return !IsEliminated() && source_.Equals(loc);
2053 }
2054
2055 // A move is redundant if it's been eliminated, if its source and
2056 // destination are the same, or if its destination is unneeded.
2057 bool IsRedundant() const {
2058 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
2059 }
2060
2061 // We clear both operands to indicate move that's been eliminated.
2062 void Eliminate() {
2063 source_ = destination_ = Location::NoLocation();
2064 }
2065
2066 bool IsEliminated() const {
2067 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
2068 return source_.IsInvalid();
2069 }
2070
Nicolas Geoffray740475d2014-09-29 10:33:25 +01002071 HInstruction* GetInstruction() const { return instruction_; }
2072
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002073 private:
2074 Location source_;
2075 Location destination_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01002076 // The instruction this move is assocatied with. Null when this move is
2077 // for moving an input in the expected locations of user (including a phi user).
2078 // This is only used in debug mode, to ensure we do not connect interval siblings
2079 // in the same parallel move.
2080 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002081
2082 DISALLOW_COPY_AND_ASSIGN(MoveOperands);
2083};
2084
2085static constexpr size_t kDefaultNumberOfMoves = 4;
2086
2087class HParallelMove : public HTemplateInstruction<0> {
2088 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002089 explicit HParallelMove(ArenaAllocator* arena)
2090 : HTemplateInstruction(SideEffects::None()), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002091
2092 void AddMove(MoveOperands* move) {
Nicolas Geoffray740475d2014-09-29 10:33:25 +01002093 if (kIsDebugBuild && move->GetInstruction() != nullptr) {
2094 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
2095 DCHECK_NE(moves_.Get(i)->GetInstruction(), move->GetInstruction())
2096 << "Doing parallel moves for the same instruction.";
2097 }
2098 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002099 moves_.Add(move);
2100 }
2101
2102 MoveOperands* MoveOperandsAt(size_t index) const {
2103 return moves_.Get(index);
2104 }
2105
2106 size_t NumMoves() const { return moves_.Size(); }
2107
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002108 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002109
2110 private:
2111 GrowableArray<MoveOperands*> moves_;
2112
2113 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
2114};
2115
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002116class HGraphVisitor : public ValueObject {
2117 public:
Dave Allison20dfc792014-06-16 20:44:29 -07002118 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
2119 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002120
Dave Allison20dfc792014-06-16 20:44:29 -07002121 virtual void VisitInstruction(HInstruction* instruction) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002122 virtual void VisitBasicBlock(HBasicBlock* block);
2123
Roland Levillain633021e2014-10-01 14:12:25 +01002124 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002125 void VisitInsertionOrder();
2126
Roland Levillain633021e2014-10-01 14:12:25 +01002127 // Visit the graph following dominator tree reverse post-order.
2128 void VisitReversePostOrder();
2129
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002130 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002131
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002132 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002133#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002134 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
2135
2136 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
2137
2138#undef DECLARE_VISIT_INSTRUCTION
2139
2140 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07002141 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002142
2143 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
2144};
2145
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002146class HGraphDelegateVisitor : public HGraphVisitor {
2147 public:
2148 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
2149 virtual ~HGraphDelegateVisitor() {}
2150
2151 // Visit functions that delegate to to super class.
2152#define DECLARE_VISIT_INSTRUCTION(name, super) \
2153 virtual void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
2154
2155 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
2156
2157#undef DECLARE_VISIT_INSTRUCTION
2158
2159 private:
2160 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
2161};
2162
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002163class HInsertionOrderIterator : public ValueObject {
2164 public:
2165 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
2166
2167 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
2168 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
2169 void Advance() { ++index_; }
2170
2171 private:
2172 const HGraph& graph_;
2173 size_t index_;
2174
2175 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
2176};
2177
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002178class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002179 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002180 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002181
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002182 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
2183 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002184 void Advance() { ++index_; }
2185
2186 private:
2187 const HGraph& graph_;
2188 size_t index_;
2189
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002190 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002191};
2192
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002193class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002194 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002195 explicit HPostOrderIterator(const HGraph& graph)
2196 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002197
2198 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002199 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002200 void Advance() { --index_; }
2201
2202 private:
2203 const HGraph& graph_;
2204 size_t index_;
2205
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002206 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002207};
2208
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002209} // namespace art
2210
2211#endif // ART_COMPILER_OPTIMIZING_NODES_H_