blob: 41713a401b09333efe68ed2744bb79338788da79 [file] [log] [blame]
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ART_COMPILER_OPTIMIZING_NODES_H_
18#define ART_COMPILER_OPTIMIZING_NODES_H_
19
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010020#include "locations.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010021#include "offsets.h"
Ian Rogerse63db272014-07-15 15:36:11 -070022#include "primitive.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000023#include "utils/allocation.h"
Nicolas Geoffray0e336432014-02-26 18:24:38 +000024#include "utils/arena_bit_vector.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000025#include "utils/growable_array.h"
26
27namespace art {
28
29class HBasicBlock;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010030class HEnvironment;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000031class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000032class HIntConstant;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000033class HGraphVisitor;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010034class HPhi;
Nicolas 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);
402 void RemovePhi(HPhi* phi);
403
404 bool IsLoopHeader() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100405 return (loop_information_ != nullptr) && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100406 }
407
Roland Levillain6b879dd2014-09-22 17:13:44 +0100408 bool IsLoopPreHeaderFirstPredecessor() const {
409 DCHECK(IsLoopHeader());
410 DCHECK(!GetPredecessors().IsEmpty());
411 return GetPredecessors().Get(0) == GetLoopInformation()->GetPreHeader();
412 }
413
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100414 HLoopInformation* GetLoopInformation() const {
415 return loop_information_;
416 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000417
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100418 // Set the loop_information_ on this block. This method overrides the current
419 // loop_information if it is an outer loop of the passed loop information.
420 void SetInLoop(HLoopInformation* info) {
421 if (IsLoopHeader()) {
422 // Nothing to do. This just means `info` is an outer loop.
423 } else if (loop_information_ == nullptr) {
424 loop_information_ = info;
425 } else if (loop_information_->Contains(*info->GetHeader())) {
426 // Block is currently part of an outer loop. Make it part of this inner loop.
427 // Note that a non loop header having a loop information means this loop information
428 // has already been populated
429 loop_information_ = info;
430 } else {
431 // Block is part of an inner loop. Do not update the loop information.
432 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
433 // at this point, because this method is being called while populating `info`.
434 }
435 }
436
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100437 bool IsInLoop() const { return loop_information_ != nullptr; }
438
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100439 // Returns wheter this block dominates the blocked passed as parameter.
440 bool Dominates(HBasicBlock* block) const;
441
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100442 size_t GetLifetimeStart() const { return lifetime_start_; }
443 size_t GetLifetimeEnd() const { return lifetime_end_; }
444
445 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
446 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
447
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100448 uint32_t GetDexPc() const { return dex_pc_; }
449
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000450 private:
451 HGraph* const graph_;
452 GrowableArray<HBasicBlock*> predecessors_;
453 GrowableArray<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100454 HInstructionList instructions_;
455 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000456 HLoopInformation* loop_information_;
457 HBasicBlock* dominator_;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100458 GrowableArray<HBasicBlock*> dominated_blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000459 int block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100460 // The dex program counter of the first instruction of this block.
461 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100462 size_t lifetime_start_;
463 size_t lifetime_end_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000464
465 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
466};
467
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100468#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
469 M(Add, BinaryOperation) \
470 M(Condition, BinaryOperation) \
471 M(Equal, Condition) \
472 M(NotEqual, Condition) \
473 M(LessThan, Condition) \
474 M(LessThanOrEqual, Condition) \
475 M(GreaterThan, Condition) \
476 M(GreaterThanOrEqual, Condition) \
477 M(Exit, Instruction) \
478 M(Goto, Instruction) \
479 M(If, Instruction) \
480 M(IntConstant, Constant) \
481 M(InvokeStatic, Invoke) \
482 M(InvokeVirtual, Invoke) \
483 M(LoadLocal, Instruction) \
484 M(Local, Instruction) \
485 M(LongConstant, Constant) \
486 M(NewInstance, Instruction) \
487 M(Not, Instruction) \
488 M(ParameterValue, Instruction) \
489 M(ParallelMove, Instruction) \
490 M(Phi, Instruction) \
491 M(Return, Instruction) \
492 M(ReturnVoid, Instruction) \
493 M(StoreLocal, Instruction) \
494 M(Sub, BinaryOperation) \
495 M(Compare, BinaryOperation) \
496 M(InstanceFieldGet, Instruction) \
497 M(InstanceFieldSet, Instruction) \
498 M(ArrayGet, Instruction) \
499 M(ArraySet, Instruction) \
500 M(ArrayLength, Instruction) \
501 M(BoundsCheck, Instruction) \
502 M(NullCheck, Instruction) \
503 M(Temporary, Instruction) \
504 M(SuspendCheck, Instruction) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000505
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100506#define FOR_EACH_INSTRUCTION(M) \
507 FOR_EACH_CONCRETE_INSTRUCTION(M) \
508 M(Constant, Instruction) \
509 M(BinaryOperation, Instruction) \
510 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -0700511
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100512#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000513FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
514#undef FORWARD_DECLARATION
515
Roland Levillainccc07a92014-09-16 14:48:16 +0100516#define DECLARE_INSTRUCTION(type) \
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100517 virtual InstructionKind GetKind() const { return k##type; } \
Roland Levillainccc07a92014-09-16 14:48:16 +0100518 virtual const char* DebugName() const { return #type; } \
519 virtual const H##type* As##type() const OVERRIDE { return this; } \
520 virtual H##type* As##type() OVERRIDE { return this; } \
521 virtual bool InstructionTypeEquals(HInstruction* other) const { \
522 return other->Is##type(); \
523 } \
524 virtual void Accept(HGraphVisitor* visitor)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000525
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100526template <typename T>
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000527class HUseListNode : public ArenaObject {
528 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100529 HUseListNode(T* user, size_t index, HUseListNode* tail)
Dave Allison20dfc792014-06-16 20:44:29 -0700530 : user_(user), index_(index), tail_(tail) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000531
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000532 HUseListNode* GetTail() const { return tail_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100533 T* GetUser() const { return user_; }
534 size_t GetIndex() const { return index_; }
535
536 void SetTail(HUseListNode<T>* node) { tail_ = node; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000537
538 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100539 T* const user_;
540 const size_t index_;
541 HUseListNode<T>* tail_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000542
543 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
544};
545
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100546// Represents the side effects an instruction may have.
547class SideEffects : public ValueObject {
548 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100549 SideEffects() : flags_(0) {}
550
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100551 static SideEffects None() {
552 return SideEffects(0);
553 }
554
555 static SideEffects All() {
556 return SideEffects(ChangesSomething().flags_ | DependsOnSomething().flags_);
557 }
558
559 static SideEffects ChangesSomething() {
560 return SideEffects((1 << kFlagChangesCount) - 1);
561 }
562
563 static SideEffects DependsOnSomething() {
564 int count = kFlagDependsOnCount - kFlagChangesCount;
565 return SideEffects(((1 << count) - 1) << kFlagChangesCount);
566 }
567
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100568 SideEffects Union(SideEffects other) const {
569 return SideEffects(flags_ | other.flags_);
570 }
571
Roland Levillain72bceff2014-09-15 18:29:00 +0100572 bool HasSideEffects() const {
573 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
574 return (flags_ & all_bits_set) != 0;
575 }
576
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100577 bool HasAllSideEffects() const {
578 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
579 return all_bits_set == (flags_ & all_bits_set);
580 }
581
582 bool DependsOn(SideEffects other) const {
583 size_t depends_flags = other.ComputeDependsFlags();
584 return (flags_ & depends_flags) != 0;
585 }
586
587 bool HasDependencies() const {
588 int count = kFlagDependsOnCount - kFlagChangesCount;
589 size_t all_bits_set = (1 << count) - 1;
590 return ((flags_ >> kFlagChangesCount) & all_bits_set) != 0;
591 }
592
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100593 private:
594 static constexpr int kFlagChangesSomething = 0;
595 static constexpr int kFlagChangesCount = kFlagChangesSomething + 1;
596
597 static constexpr int kFlagDependsOnSomething = kFlagChangesCount;
598 static constexpr int kFlagDependsOnCount = kFlagDependsOnSomething + 1;
599
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100600 explicit SideEffects(size_t flags) : flags_(flags) {}
601
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100602 size_t ComputeDependsFlags() const {
603 return flags_ << kFlagChangesCount;
604 }
605
606 size_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100607};
608
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000609class HInstruction : public ArenaObject {
610 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100611 explicit HInstruction(SideEffects side_effects)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000612 : previous_(nullptr),
613 next_(nullptr),
614 block_(nullptr),
615 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100616 ssa_index_(-1),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000617 uses_(nullptr),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100618 env_uses_(nullptr),
619 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100620 locations_(nullptr),
621 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100622 lifetime_position_(kNoLifetime),
623 side_effects_(side_effects) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000624
Dave Allison20dfc792014-06-16 20:44:29 -0700625 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000626
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100627#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100628 enum InstructionKind {
629 FOR_EACH_INSTRUCTION(DECLARE_KIND)
630 };
631#undef DECLARE_KIND
632
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000633 HInstruction* GetNext() const { return next_; }
634 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000635
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000636 HBasicBlock* GetBlock() const { return block_; }
637 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100638 bool IsInBlock() const { return block_ != nullptr; }
639 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +0100640 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000641
Roland Levillain6b879dd2014-09-22 17:13:44 +0100642 virtual size_t InputCount() const = 0;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100643 virtual HInstruction* InputAt(size_t i) const = 0;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000644
645 virtual void Accept(HGraphVisitor* visitor) = 0;
646 virtual const char* DebugName() const = 0;
647
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100648 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100649 virtual void SetRawInputAt(size_t index, HInstruction* input) = 0;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100650
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100651 virtual bool NeedsEnvironment() const { return false; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100652 virtual bool IsControlFlow() const { return false; }
Roland Levillain72bceff2014-09-15 18:29:00 +0100653 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100654
655 void AddUseAt(HInstruction* user, size_t index) {
656 uses_ = new (block_->GetGraph()->GetArena()) HUseListNode<HInstruction>(user, index, uses_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000657 }
658
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100659 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +0100660 DCHECK(user != nullptr);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100661 env_uses_ = new (block_->GetGraph()->GetArena()) HUseListNode<HEnvironment>(
662 user, index, env_uses_);
663 }
664
665 void RemoveUser(HInstruction* user, size_t index);
Nicolas Geoffray724c9632014-09-22 12:27:27 +0100666 void RemoveEnvironmentUser(HEnvironment* user, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100667
668 HUseListNode<HInstruction>* GetUses() const { return uses_; }
669 HUseListNode<HEnvironment>* GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000670
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100671 bool HasUses() const { return uses_ != nullptr || env_uses_ != nullptr; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100672 bool HasEnvironmentUses() const { return env_uses_ != nullptr; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000673
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100674 size_t NumberOfUses() const {
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100675 // TODO: Optimize this method if it is used outside of the HGraphVisualizer.
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100676 size_t result = 0;
677 HUseListNode<HInstruction>* current = uses_;
678 while (current != nullptr) {
679 current = current->GetTail();
680 ++result;
681 }
682 return result;
683 }
684
Roland Levillainccc07a92014-09-16 14:48:16 +0100685 // Does this instruction dominate `other_instruction`? Aborts if
686 // this instruction and `other_instruction` are both phis.
687 bool Dominates(HInstruction* other_instruction) const;
688
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000689 int GetId() const { return id_; }
690 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000691
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100692 int GetSsaIndex() const { return ssa_index_; }
693 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
694 bool HasSsaIndex() const { return ssa_index_ != -1; }
695
696 bool HasEnvironment() const { return environment_ != nullptr; }
697 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100698 void SetEnvironment(HEnvironment* environment) { environment_ = environment; }
699
Nicolas Geoffray39468442014-09-02 15:17:15 +0100700 // Returns the number of entries in the environment. Typically, that is the
701 // number of dex registers in a method. It could be more in case of inlining.
702 size_t EnvironmentSize() const;
703
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000704 LocationSummary* GetLocations() const { return locations_; }
705 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000706
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100707 void ReplaceWith(HInstruction* instruction);
708
Dave Allison20dfc792014-06-16 20:44:29 -0700709 bool HasOnlyOneUse() const {
710 return uses_ != nullptr && uses_->GetTail() == nullptr;
711 }
712
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100713#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +0100714 bool Is##type() const { return (As##type() != nullptr); } \
715 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000716 virtual H##type* As##type() { return nullptr; }
717
718 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
719#undef INSTRUCTION_TYPE_CHECK
720
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100721 // Returns whether the instruction can be moved within the graph.
722 virtual bool CanBeMoved() const { return false; }
723
724 // Returns whether the two instructions are of the same kind.
725 virtual bool InstructionTypeEquals(HInstruction* other) const { return false; }
726
727 // Returns whether any data encoded in the two instructions is equal.
728 // This method does not look at the inputs. Both instructions must be
729 // of the same type, otherwise the method has undefined behavior.
730 virtual bool InstructionDataEquals(HInstruction* other) const { return false; }
731
732 // Returns whether two instructions are equal, that is:
733 // 1) They have the same type and contain the same data,
734 // 2) Their inputs are identical.
735 bool Equals(HInstruction* other) const;
736
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100737 virtual InstructionKind GetKind() const = 0;
738
739 virtual size_t ComputeHashCode() const {
740 size_t result = GetKind();
741 for (size_t i = 0, e = InputCount(); i < e; ++i) {
742 result = (result * 31) + InputAt(i)->GetId();
743 }
744 return result;
745 }
746
747 SideEffects GetSideEffects() const { return side_effects_; }
748
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100749 size_t GetLifetimePosition() const { return lifetime_position_; }
750 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
751 LiveInterval* GetLiveInterval() const { return live_interval_; }
752 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
753 bool HasLiveInterval() const { return live_interval_ != nullptr; }
754
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000755 private:
756 HInstruction* previous_;
757 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000758 HBasicBlock* block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000759
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000760 // An instruction gets an id when it is added to the graph.
761 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +0100762 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000763 int id_;
764
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100765 // When doing liveness analysis, instructions that have uses get an SSA index.
766 int ssa_index_;
767
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100768 // List of instructions that have this instruction as input.
769 HUseListNode<HInstruction>* uses_;
770
771 // List of environments that contain this instruction.
772 HUseListNode<HEnvironment>* env_uses_;
773
Nicolas Geoffray39468442014-09-02 15:17:15 +0100774 // The environment associated with this instruction. Not null if the instruction
775 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100776 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000777
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000778 // Set by the code generator.
779 LocationSummary* locations_;
780
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100781 // Set by the liveness analysis.
782 LiveInterval* live_interval_;
783
784 // Set by the liveness analysis, this is the position in a linear
785 // order of blocks where this instruction's live interval start.
786 size_t lifetime_position_;
787
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100788 const SideEffects side_effects_;
789
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000790 friend class HBasicBlock;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100791 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000792
793 DISALLOW_COPY_AND_ASSIGN(HInstruction);
794};
795
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100796template<typename T>
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000797class HUseIterator : public ValueObject {
798 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100799 explicit HUseIterator(HUseListNode<T>* uses) : current_(uses) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000800
801 bool Done() const { return current_ == nullptr; }
802
803 void Advance() {
804 DCHECK(!Done());
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000805 current_ = current_->GetTail();
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000806 }
807
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100808 HUseListNode<T>* Current() const {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000809 DCHECK(!Done());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100810 return current_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000811 }
812
813 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100814 HUseListNode<T>* current_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000815
816 friend class HValue;
817};
818
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100819// A HEnvironment object contains the values of virtual registers at a given location.
820class HEnvironment : public ArenaObject {
821 public:
822 HEnvironment(ArenaAllocator* arena, size_t number_of_vregs) : vregs_(arena, number_of_vregs) {
823 vregs_.SetSize(number_of_vregs);
824 for (size_t i = 0; i < number_of_vregs; i++) {
825 vregs_.Put(i, nullptr);
826 }
827 }
828
829 void Populate(const GrowableArray<HInstruction*>& env) {
830 for (size_t i = 0; i < env.Size(); i++) {
831 HInstruction* instruction = env.Get(i);
832 vregs_.Put(i, instruction);
833 if (instruction != nullptr) {
834 instruction->AddEnvUseAt(this, i);
835 }
836 }
837 }
838
839 void SetRawEnvAt(size_t index, HInstruction* instruction) {
840 vregs_.Put(index, instruction);
841 }
842
Nicolas Geoffray39468442014-09-02 15:17:15 +0100843 HInstruction* GetInstructionAt(size_t index) const {
844 return vregs_.Get(index);
845 }
846
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100847 GrowableArray<HInstruction*>* GetVRegs() {
848 return &vregs_;
849 }
850
Nicolas Geoffray39468442014-09-02 15:17:15 +0100851 size_t Size() const { return vregs_.Size(); }
852
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100853 private:
854 GrowableArray<HInstruction*> vregs_;
855
856 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
857};
858
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000859class HInputIterator : public ValueObject {
860 public:
Dave Allison20dfc792014-06-16 20:44:29 -0700861 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000862
863 bool Done() const { return index_ == instruction_->InputCount(); }
864 HInstruction* Current() const { return instruction_->InputAt(index_); }
865 void Advance() { index_++; }
866
867 private:
868 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100869 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000870
871 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
872};
873
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000874class HInstructionIterator : public ValueObject {
875 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100876 explicit HInstructionIterator(const HInstructionList& instructions)
877 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000878 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000879 }
880
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000881 bool Done() const { return instruction_ == nullptr; }
882 HInstruction* Current() const { return instruction_; }
883 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000884 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000885 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000886 }
887
888 private:
889 HInstruction* instruction_;
890 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100891
892 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000893};
894
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100895class HBackwardInstructionIterator : public ValueObject {
896 public:
897 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
898 : instruction_(instructions.last_instruction_) {
899 next_ = Done() ? nullptr : instruction_->GetPrevious();
900 }
901
902 bool Done() const { return instruction_ == nullptr; }
903 HInstruction* Current() const { return instruction_; }
904 void Advance() {
905 instruction_ = next_;
906 next_ = Done() ? nullptr : instruction_->GetPrevious();
907 }
908
909 private:
910 HInstruction* instruction_;
911 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100912
913 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100914};
915
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000916// An embedded container with N elements of type T. Used (with partial
917// specialization for N=0) because embedded arrays cannot have size 0.
918template<typename T, intptr_t N>
919class EmbeddedArray {
920 public:
Dave Allison20dfc792014-06-16 20:44:29 -0700921 EmbeddedArray() : elements_() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000922
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000923 intptr_t GetLength() const { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000924
925 const T& operator[](intptr_t i) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000926 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000927 return elements_[i];
928 }
929
930 T& operator[](intptr_t i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000931 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000932 return elements_[i];
933 }
934
935 const T& At(intptr_t i) const {
936 return (*this)[i];
937 }
938
939 void SetAt(intptr_t i, const T& val) {
940 (*this)[i] = val;
941 }
942
943 private:
944 T elements_[N];
945};
946
947template<typename T>
948class EmbeddedArray<T, 0> {
949 public:
950 intptr_t length() const { return 0; }
951 const T& operator[](intptr_t i) const {
952 LOG(FATAL) << "Unreachable";
953 static T sentinel = 0;
954 return sentinel;
955 }
956 T& operator[](intptr_t i) {
957 LOG(FATAL) << "Unreachable";
958 static T sentinel = 0;
959 return sentinel;
960 }
961};
962
963template<intptr_t N>
964class HTemplateInstruction: public HInstruction {
965 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100966 HTemplateInstruction<N>(SideEffects side_effects)
967 : HInstruction(side_effects), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -0700968 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000969
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100970 virtual size_t InputCount() const { return N; }
971 virtual HInstruction* InputAt(size_t i) const { return inputs_[i]; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000972
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000973 protected:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100974 virtual void SetRawInputAt(size_t i, HInstruction* instruction) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000975 inputs_[i] = instruction;
976 }
977
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000978 private:
979 EmbeddedArray<HInstruction*, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100980
981 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000982};
983
Dave Allison20dfc792014-06-16 20:44:29 -0700984template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100985class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -0700986 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100987 HExpression<N>(Primitive::Type type, SideEffects side_effects)
988 : HTemplateInstruction<N>(side_effects), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -0700989 virtual ~HExpression() {}
990
991 virtual Primitive::Type GetType() const { return type_; }
992
993 private:
994 const Primitive::Type type_;
995};
996
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000997// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
998// instruction that branches to the exit block.
999class HReturnVoid : public HTemplateInstruction<0> {
1000 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001001 HReturnVoid() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001002
1003 virtual bool IsControlFlow() const { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001004
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001005 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001006
1007 private:
1008 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
1009};
1010
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001011// Represents dex's RETURN opcodes. A HReturn is a control flow
1012// instruction that branches to the exit block.
1013class HReturn : public HTemplateInstruction<1> {
1014 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001015 explicit HReturn(HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001016 SetRawInputAt(0, value);
1017 }
1018
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001019 virtual bool IsControlFlow() const { return true; }
1020
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001021 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001022
1023 private:
1024 DISALLOW_COPY_AND_ASSIGN(HReturn);
1025};
1026
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001027// The exit instruction is the only instruction of the exit block.
1028// Instructions aborting the method (HTrow and HReturn) must branch to the
1029// exit block.
1030class HExit : public HTemplateInstruction<0> {
1031 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001032 HExit() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001033
1034 virtual bool IsControlFlow() const { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001035
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001036 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001037
1038 private:
1039 DISALLOW_COPY_AND_ASSIGN(HExit);
1040};
1041
1042// Jumps from one block to another.
1043class HGoto : public HTemplateInstruction<0> {
1044 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001045 HGoto() : HTemplateInstruction(SideEffects::None()) {}
1046
1047 virtual bool IsControlFlow() const { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001048
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001049 HBasicBlock* GetSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001050 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001051 }
1052
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001053 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001054
1055 private:
1056 DISALLOW_COPY_AND_ASSIGN(HGoto);
1057};
1058
Dave Allison20dfc792014-06-16 20:44:29 -07001059
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001060// Conditional branch. A block ending with an HIf instruction must have
1061// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001062class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001063 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001064 explicit HIf(HInstruction* input) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001065 SetRawInputAt(0, input);
1066 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001067
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001068 virtual bool IsControlFlow() const { return true; }
1069
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001070 HBasicBlock* IfTrueSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001071 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001072 }
1073
1074 HBasicBlock* IfFalseSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001075 return GetBlock()->GetSuccessors().Get(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001076 }
1077
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001078 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001079
Dave Allison20dfc792014-06-16 20:44:29 -07001080 virtual bool IsIfInstruction() const { return true; }
1081
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001082 private:
1083 DISALLOW_COPY_AND_ASSIGN(HIf);
1084};
1085
Dave Allison20dfc792014-06-16 20:44:29 -07001086class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001087 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001088 HBinaryOperation(Primitive::Type result_type,
1089 HInstruction* left,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001090 HInstruction* right) : HExpression(result_type, SideEffects::None()) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001091 SetRawInputAt(0, left);
1092 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001093 }
1094
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001095 HInstruction* GetLeft() const { return InputAt(0); }
1096 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07001097 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001098
1099 virtual bool IsCommutative() { return false; }
1100
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001101 virtual bool CanBeMoved() const { return true; }
1102 virtual bool InstructionDataEquals(HInstruction* other) const { return true; }
1103
Roland Levillain556c3d12014-09-18 15:25:07 +01001104 // Try to statically evaluate `operation` and return an HConstant
1105 // containing the result of this evaluation. If `operation` cannot
1106 // be evaluated as a constant, return nullptr.
1107 HConstant* TryStaticEvaluation(ArenaAllocator* allocator) const;
1108
1109 // Apply this operation to `x` and `y`.
1110 virtual int32_t Evaluate(int32_t x, int32_t y) const = 0;
1111 virtual int64_t Evaluate(int64_t x, int64_t y) const = 0;
1112
Roland Levillainccc07a92014-09-16 14:48:16 +01001113 DECLARE_INSTRUCTION(BinaryOperation);
1114
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001115 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001116 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
1117};
1118
Dave Allison20dfc792014-06-16 20:44:29 -07001119class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001120 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001121 HCondition(HInstruction* first, HInstruction* second)
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001122 : HBinaryOperation(Primitive::kPrimBoolean, first, second),
1123 needs_materialization_(true) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001124
1125 virtual bool IsCommutative() { return true; }
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001126
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001127 bool NeedsMaterialization() const { return needs_materialization_; }
1128 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001129
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001130 // For code generation purposes, returns whether this instruction is just before
1131 // `if_`, and disregard moves in between.
1132 bool IsBeforeWhenDisregardMoves(HIf* if_) const;
1133
Dave Allison20dfc792014-06-16 20:44:29 -07001134 DECLARE_INSTRUCTION(Condition);
1135
1136 virtual IfCondition GetCondition() const = 0;
1137
1138 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001139 // For register allocation purposes, returns whether this instruction needs to be
1140 // materialized (that is, not just be in the processor flags).
1141 bool needs_materialization_;
1142
Dave Allison20dfc792014-06-16 20:44:29 -07001143 DISALLOW_COPY_AND_ASSIGN(HCondition);
1144};
1145
1146// Instruction to check if two inputs are equal to each other.
1147class HEqual : public HCondition {
1148 public:
1149 HEqual(HInstruction* first, HInstruction* second)
1150 : HCondition(first, second) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001151
Roland Levillain93445682014-10-06 19:24:02 +01001152 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1153 return x == y ? 1 : 0;
1154 }
1155 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1156 return x == y ? 1 : 0;
1157 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001158
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001159 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001160
Dave Allison20dfc792014-06-16 20:44:29 -07001161 virtual IfCondition GetCondition() const {
1162 return kCondEQ;
1163 }
1164
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001165 private:
1166 DISALLOW_COPY_AND_ASSIGN(HEqual);
1167};
1168
Dave Allison20dfc792014-06-16 20:44:29 -07001169class HNotEqual : public HCondition {
1170 public:
1171 HNotEqual(HInstruction* first, HInstruction* second)
1172 : HCondition(first, second) {}
1173
Roland Levillain93445682014-10-06 19:24:02 +01001174 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1175 return x != y ? 1 : 0;
1176 }
1177 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1178 return x != y ? 1 : 0;
1179 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001180
Dave Allison20dfc792014-06-16 20:44:29 -07001181 DECLARE_INSTRUCTION(NotEqual);
1182
1183 virtual IfCondition GetCondition() const {
1184 return kCondNE;
1185 }
1186
1187 private:
1188 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
1189};
1190
1191class HLessThan : public HCondition {
1192 public:
1193 HLessThan(HInstruction* first, HInstruction* second)
1194 : HCondition(first, second) {}
1195
Roland Levillain93445682014-10-06 19:24:02 +01001196 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1197 return x < y ? 1 : 0;
1198 }
1199 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1200 return x < y ? 1 : 0;
1201 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001202
Dave Allison20dfc792014-06-16 20:44:29 -07001203 DECLARE_INSTRUCTION(LessThan);
1204
1205 virtual IfCondition GetCondition() const {
1206 return kCondLT;
1207 }
1208
1209 private:
1210 DISALLOW_COPY_AND_ASSIGN(HLessThan);
1211};
1212
1213class HLessThanOrEqual : public HCondition {
1214 public:
1215 HLessThanOrEqual(HInstruction* first, HInstruction* second)
1216 : HCondition(first, second) {}
1217
Roland Levillain93445682014-10-06 19:24:02 +01001218 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1219 return x <= y ? 1 : 0;
1220 }
1221 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1222 return x <= y ? 1 : 0;
1223 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001224
Dave Allison20dfc792014-06-16 20:44:29 -07001225 DECLARE_INSTRUCTION(LessThanOrEqual);
1226
1227 virtual IfCondition GetCondition() const {
1228 return kCondLE;
1229 }
1230
1231 private:
1232 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
1233};
1234
1235class HGreaterThan : public HCondition {
1236 public:
1237 HGreaterThan(HInstruction* first, HInstruction* second)
1238 : HCondition(first, second) {}
1239
Roland Levillain93445682014-10-06 19:24:02 +01001240 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1241 return x > y ? 1 : 0;
1242 }
1243 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1244 return x > y ? 1 : 0;
1245 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001246
Dave Allison20dfc792014-06-16 20:44:29 -07001247 DECLARE_INSTRUCTION(GreaterThan);
1248
1249 virtual IfCondition GetCondition() const {
1250 return kCondGT;
1251 }
1252
1253 private:
1254 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
1255};
1256
1257class HGreaterThanOrEqual : public HCondition {
1258 public:
1259 HGreaterThanOrEqual(HInstruction* first, HInstruction* second)
1260 : HCondition(first, second) {}
1261
Roland Levillain93445682014-10-06 19:24:02 +01001262 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1263 return x >= y ? 1 : 0;
1264 }
1265 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1266 return x >= y ? 1 : 0;
1267 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001268
Dave Allison20dfc792014-06-16 20:44:29 -07001269 DECLARE_INSTRUCTION(GreaterThanOrEqual);
1270
1271 virtual IfCondition GetCondition() const {
1272 return kCondGE;
1273 }
1274
1275 private:
1276 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
1277};
1278
1279
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001280// Instruction to check how two inputs compare to each other.
1281// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
1282class HCompare : public HBinaryOperation {
1283 public:
1284 HCompare(Primitive::Type type, HInstruction* first, HInstruction* second)
1285 : HBinaryOperation(Primitive::kPrimInt, first, second) {
1286 DCHECK_EQ(type, first->GetType());
1287 DCHECK_EQ(type, second->GetType());
1288 }
1289
Roland Levillain93445682014-10-06 19:24:02 +01001290 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001291 return
1292 x == y ? 0 :
1293 x > y ? 1 :
1294 -1;
1295 }
Roland Levillain93445682014-10-06 19:24:02 +01001296 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001297 return
1298 x == y ? 0 :
1299 x > y ? 1 :
1300 -1;
1301 }
1302
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001303 DECLARE_INSTRUCTION(Compare);
1304
1305 private:
1306 DISALLOW_COPY_AND_ASSIGN(HCompare);
1307};
1308
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001309// A local in the graph. Corresponds to a Dex register.
1310class HLocal : public HTemplateInstruction<0> {
1311 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001312 explicit HLocal(uint16_t reg_number)
1313 : HTemplateInstruction(SideEffects::None()), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001314
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001315 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001316
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001317 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001318
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001319 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001320 // The Dex register number.
1321 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001322
1323 DISALLOW_COPY_AND_ASSIGN(HLocal);
1324};
1325
1326// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07001327class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001328 public:
Roland Levillain5799fc02014-09-25 12:15:20 +01001329 HLoadLocal(HLocal* local, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001330 : HExpression(type, SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001331 SetRawInputAt(0, local);
1332 }
1333
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001334 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1335
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001336 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001337
1338 private:
1339 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
1340};
1341
1342// Store a value in a given local. This instruction has two inputs: the value
1343// and the local.
1344class HStoreLocal : public HTemplateInstruction<2> {
1345 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001346 HStoreLocal(HLocal* local, HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001347 SetRawInputAt(0, local);
1348 SetRawInputAt(1, value);
1349 }
1350
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001351 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1352
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001353 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001354
1355 private:
1356 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
1357};
1358
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001359class HConstant : public HExpression<0> {
1360 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001361 explicit HConstant(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
1362
1363 virtual bool CanBeMoved() const { return true; }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001364
1365 DECLARE_INSTRUCTION(Constant);
1366
1367 private:
1368 DISALLOW_COPY_AND_ASSIGN(HConstant);
1369};
1370
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001371// Constants of the type int. Those can be from Dex instructions, or
1372// synthesized (for example with the if-eqz instruction).
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001373class HIntConstant : public HConstant {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001374 public:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001375 explicit HIntConstant(int32_t value) : HConstant(Primitive::kPrimInt), value_(value) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001376
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001377 int32_t GetValue() const { return value_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001378
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001379 virtual bool InstructionDataEquals(HInstruction* other) const {
1380 return other->AsIntConstant()->value_ == value_;
1381 }
1382
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001383 virtual size_t ComputeHashCode() const { return GetValue(); }
1384
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001385 DECLARE_INSTRUCTION(IntConstant);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001386
1387 private:
1388 const int32_t value_;
1389
1390 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
1391};
1392
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001393class HLongConstant : public HConstant {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001394 public:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001395 explicit HLongConstant(int64_t value) : HConstant(Primitive::kPrimLong), value_(value) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001396
1397 int64_t GetValue() const { return value_; }
1398
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001399 virtual bool InstructionDataEquals(HInstruction* other) const {
1400 return other->AsLongConstant()->value_ == value_;
1401 }
1402
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001403 virtual size_t ComputeHashCode() const { return static_cast<size_t>(GetValue()); }
1404
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001405 DECLARE_INSTRUCTION(LongConstant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001406
1407 private:
1408 const int64_t value_;
1409
1410 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
1411};
1412
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001413class HInvoke : public HInstruction {
1414 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001415 HInvoke(ArenaAllocator* arena,
1416 uint32_t number_of_arguments,
1417 Primitive::Type return_type,
1418 uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001419 : HInstruction(SideEffects::All()),
1420 inputs_(arena, number_of_arguments),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001421 return_type_(return_type),
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001422 dex_pc_(dex_pc) {
1423 inputs_.SetSize(number_of_arguments);
1424 }
1425
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001426 virtual size_t InputCount() const { return inputs_.Size(); }
1427 virtual HInstruction* InputAt(size_t i) const { return inputs_.Get(i); }
1428
1429 // Runtime needs to walk the stack, so Dex -> Dex calls need to
1430 // know their environment.
1431 virtual bool NeedsEnvironment() const { return true; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001432
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001433 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001434 SetRawInputAt(index, argument);
1435 }
1436
1437 virtual void SetRawInputAt(size_t index, HInstruction* input) {
1438 inputs_.Put(index, input);
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001439 }
1440
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001441 virtual Primitive::Type GetType() const { return return_type_; }
1442
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001443 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001444
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001445 DECLARE_INSTRUCTION(Invoke);
1446
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001447 protected:
1448 GrowableArray<HInstruction*> inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001449 const Primitive::Type return_type_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001450 const uint32_t dex_pc_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001451
1452 private:
1453 DISALLOW_COPY_AND_ASSIGN(HInvoke);
1454};
1455
1456class HInvokeStatic : public HInvoke {
1457 public:
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001458 HInvokeStatic(ArenaAllocator* arena,
1459 uint32_t number_of_arguments,
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001460 Primitive::Type return_type,
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001461 uint32_t dex_pc,
1462 uint32_t index_in_dex_cache)
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001463 : HInvoke(arena, number_of_arguments, return_type, dex_pc),
1464 index_in_dex_cache_(index_in_dex_cache) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001465
1466 uint32_t GetIndexInDexCache() const { return index_in_dex_cache_; }
1467
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001468 DECLARE_INSTRUCTION(InvokeStatic);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001469
1470 private:
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001471 const uint32_t index_in_dex_cache_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001472
1473 DISALLOW_COPY_AND_ASSIGN(HInvokeStatic);
1474};
1475
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001476class HInvokeVirtual : public HInvoke {
1477 public:
1478 HInvokeVirtual(ArenaAllocator* arena,
1479 uint32_t number_of_arguments,
1480 Primitive::Type return_type,
1481 uint32_t dex_pc,
1482 uint32_t vtable_index)
1483 : HInvoke(arena, number_of_arguments, return_type, dex_pc),
1484 vtable_index_(vtable_index) {}
1485
1486 uint32_t GetVTableIndex() const { return vtable_index_; }
1487
1488 DECLARE_INSTRUCTION(InvokeVirtual);
1489
1490 private:
1491 const uint32_t vtable_index_;
1492
1493 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
1494};
1495
Dave Allison20dfc792014-06-16 20:44:29 -07001496class HNewInstance : public HExpression<0> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001497 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001498 HNewInstance(uint32_t dex_pc, uint16_t type_index)
1499 : HExpression(Primitive::kPrimNot, SideEffects::None()),
1500 dex_pc_(dex_pc),
1501 type_index_(type_index) {}
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001502
1503 uint32_t GetDexPc() const { return dex_pc_; }
1504 uint16_t GetTypeIndex() const { return type_index_; }
1505
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001506 // Calls runtime so needs an environment.
1507 virtual bool NeedsEnvironment() const { return true; }
1508
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001509 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001510
1511 private:
1512 const uint32_t dex_pc_;
1513 const uint16_t type_index_;
1514
1515 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
1516};
1517
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001518class HAdd : public HBinaryOperation {
1519 public:
1520 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1521 : HBinaryOperation(result_type, left, right) {}
1522
1523 virtual bool IsCommutative() { return true; }
1524
Roland Levillain93445682014-10-06 19:24:02 +01001525 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1526 return x + y;
1527 }
1528 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1529 return x + y;
1530 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001531
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001532 DECLARE_INSTRUCTION(Add);
1533
1534 private:
1535 DISALLOW_COPY_AND_ASSIGN(HAdd);
1536};
1537
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001538class HSub : public HBinaryOperation {
1539 public:
1540 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1541 : HBinaryOperation(result_type, left, right) {}
1542
1543 virtual bool IsCommutative() { return false; }
1544
Roland Levillain93445682014-10-06 19:24:02 +01001545 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1546 return x - y;
1547 }
1548 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1549 return x - y;
1550 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001551
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001552 DECLARE_INSTRUCTION(Sub);
1553
1554 private:
1555 DISALLOW_COPY_AND_ASSIGN(HSub);
1556};
1557
1558// The value of a parameter in this method. Its location depends on
1559// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07001560class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001561 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001562 HParameterValue(uint8_t index, Primitive::Type parameter_type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001563 : HExpression(parameter_type, SideEffects::None()), index_(index) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001564
1565 uint8_t GetIndex() const { return index_; }
1566
1567 DECLARE_INSTRUCTION(ParameterValue);
1568
1569 private:
1570 // The index of this parameter in the parameters list. Must be less
1571 // than HGraph::number_of_in_vregs_;
1572 const uint8_t index_;
1573
1574 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
1575};
1576
Dave Allison20dfc792014-06-16 20:44:29 -07001577class HNot : public HExpression<1> {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001578 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001579 explicit HNot(HInstruction* input) : HExpression(Primitive::kPrimBoolean, SideEffects::None()) {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001580 SetRawInputAt(0, input);
1581 }
1582
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001583 virtual bool CanBeMoved() const { return true; }
1584 virtual bool InstructionDataEquals(HInstruction* other) const { return true; }
1585
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001586 DECLARE_INSTRUCTION(Not);
1587
1588 private:
1589 DISALLOW_COPY_AND_ASSIGN(HNot);
1590};
1591
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001592class HPhi : public HInstruction {
1593 public:
1594 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001595 : HInstruction(SideEffects::None()),
1596 inputs_(arena, number_of_inputs),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001597 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001598 type_(type),
1599 is_live_(false) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001600 inputs_.SetSize(number_of_inputs);
1601 }
1602
1603 virtual size_t InputCount() const { return inputs_.Size(); }
1604 virtual HInstruction* InputAt(size_t i) const { return inputs_.Get(i); }
1605
1606 virtual void SetRawInputAt(size_t index, HInstruction* input) {
1607 inputs_.Put(index, input);
1608 }
1609
1610 void AddInput(HInstruction* input);
1611
1612 virtual Primitive::Type GetType() const { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001613 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001614
1615 uint32_t GetRegNumber() const { return reg_number_; }
1616
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001617 void SetDead() { is_live_ = false; }
1618 void SetLive() { is_live_ = true; }
1619 bool IsDead() const { return !is_live_; }
1620 bool IsLive() const { return is_live_; }
1621
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001622 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001623
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001624 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001625 GrowableArray<HInstruction*> inputs_;
1626 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001627 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001628 bool is_live_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001629
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001630 DISALLOW_COPY_AND_ASSIGN(HPhi);
1631};
1632
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001633class HNullCheck : public HExpression<1> {
1634 public:
1635 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001636 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001637 SetRawInputAt(0, value);
1638 }
1639
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001640 virtual bool CanBeMoved() const { return true; }
1641 virtual bool InstructionDataEquals(HInstruction* other) const { return true; }
1642
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001643 virtual bool NeedsEnvironment() const { return true; }
1644
1645 uint32_t GetDexPc() const { return dex_pc_; }
1646
1647 DECLARE_INSTRUCTION(NullCheck);
1648
1649 private:
1650 const uint32_t dex_pc_;
1651
1652 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
1653};
1654
1655class FieldInfo : public ValueObject {
1656 public:
Roland Levillain5799fc02014-09-25 12:15:20 +01001657 FieldInfo(MemberOffset field_offset, Primitive::Type field_type)
Nicolas Geoffray39468442014-09-02 15:17:15 +01001658 : field_offset_(field_offset), field_type_(field_type) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001659
1660 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001661 Primitive::Type GetFieldType() const { return field_type_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001662
1663 private:
1664 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01001665 const Primitive::Type field_type_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001666};
1667
1668class HInstanceFieldGet : public HExpression<1> {
1669 public:
1670 HInstanceFieldGet(HInstruction* value,
1671 Primitive::Type field_type,
1672 MemberOffset field_offset)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001673 : HExpression(field_type, SideEffects::DependsOnSomething()),
1674 field_info_(field_offset, field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001675 SetRawInputAt(0, value);
1676 }
1677
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001678 virtual bool CanBeMoved() const { return true; }
1679 virtual bool InstructionDataEquals(HInstruction* other) const {
1680 size_t other_offset = other->AsInstanceFieldGet()->GetFieldOffset().SizeValue();
1681 return other_offset == GetFieldOffset().SizeValue();
1682 }
1683
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001684 virtual size_t ComputeHashCode() const {
1685 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
1686 }
1687
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001688 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001689 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001690
1691 DECLARE_INSTRUCTION(InstanceFieldGet);
1692
1693 private:
1694 const FieldInfo field_info_;
1695
1696 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
1697};
1698
1699class HInstanceFieldSet : public HTemplateInstruction<2> {
1700 public:
1701 HInstanceFieldSet(HInstruction* object,
1702 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01001703 Primitive::Type field_type,
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001704 MemberOffset field_offset)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001705 : HTemplateInstruction(SideEffects::ChangesSomething()),
1706 field_info_(field_offset, field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001707 SetRawInputAt(0, object);
1708 SetRawInputAt(1, value);
1709 }
1710
1711 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001712 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001713
1714 DECLARE_INSTRUCTION(InstanceFieldSet);
1715
1716 private:
1717 const FieldInfo field_info_;
1718
1719 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
1720};
1721
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001722class HArrayGet : public HExpression<2> {
1723 public:
1724 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001725 : HExpression(type, SideEffects::DependsOnSomething()) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001726 SetRawInputAt(0, array);
1727 SetRawInputAt(1, index);
1728 }
1729
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001730 virtual bool CanBeMoved() const { return true; }
1731 virtual bool InstructionDataEquals(HInstruction* other) const { return true; }
1732
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001733 DECLARE_INSTRUCTION(ArrayGet);
1734
1735 private:
1736 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
1737};
1738
1739class HArraySet : public HTemplateInstruction<3> {
1740 public:
1741 HArraySet(HInstruction* array,
1742 HInstruction* index,
1743 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01001744 Primitive::Type component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001745 uint32_t dex_pc)
1746 : HTemplateInstruction(SideEffects::ChangesSomething()),
1747 dex_pc_(dex_pc),
1748 component_type_(component_type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001749 SetRawInputAt(0, array);
1750 SetRawInputAt(1, index);
1751 SetRawInputAt(2, value);
1752 }
1753
1754 virtual bool NeedsEnvironment() const {
1755 // We currently always call a runtime method to catch array store
1756 // exceptions.
1757 return InputAt(2)->GetType() == Primitive::kPrimNot;
1758 }
1759
1760 uint32_t GetDexPc() const { return dex_pc_; }
1761
Nicolas Geoffray39468442014-09-02 15:17:15 +01001762 Primitive::Type GetComponentType() const { return component_type_; }
1763
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001764 DECLARE_INSTRUCTION(ArraySet);
1765
1766 private:
1767 const uint32_t dex_pc_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01001768 const Primitive::Type component_type_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001769
1770 DISALLOW_COPY_AND_ASSIGN(HArraySet);
1771};
1772
1773class HArrayLength : public HExpression<1> {
1774 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001775 explicit HArrayLength(HInstruction* array)
1776 : HExpression(Primitive::kPrimInt, SideEffects::None()) {
1777 // Note that arrays do not change length, so the instruction does not
1778 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001779 SetRawInputAt(0, array);
1780 }
1781
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001782 virtual bool CanBeMoved() const { return true; }
1783 virtual bool InstructionDataEquals(HInstruction* other) const { return true; }
1784
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001785 DECLARE_INSTRUCTION(ArrayLength);
1786
1787 private:
1788 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
1789};
1790
1791class HBoundsCheck : public HExpression<2> {
1792 public:
1793 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001794 : HExpression(index->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001795 DCHECK(index->GetType() == Primitive::kPrimInt);
1796 SetRawInputAt(0, index);
1797 SetRawInputAt(1, length);
1798 }
1799
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001800 virtual bool CanBeMoved() const { return true; }
1801 virtual bool InstructionDataEquals(HInstruction* other) const { return true; }
1802
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001803 virtual bool NeedsEnvironment() const { return true; }
1804
1805 uint32_t GetDexPc() const { return dex_pc_; }
1806
1807 DECLARE_INSTRUCTION(BoundsCheck);
1808
1809 private:
1810 const uint32_t dex_pc_;
1811
1812 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
1813};
1814
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001815/**
1816 * Some DEX instructions are folded into multiple HInstructions that need
1817 * to stay live until the last HInstruction. This class
1818 * is used as a marker for the baseline compiler to ensure its preceding
1819 * HInstruction stays live. `index` is the temporary number that is used
1820 * for knowing the stack offset where to store the instruction.
1821 */
1822class HTemporary : public HTemplateInstruction<0> {
1823 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001824 explicit HTemporary(size_t index) : HTemplateInstruction(SideEffects::None()), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001825
1826 size_t GetIndex() const { return index_; }
1827
1828 DECLARE_INSTRUCTION(Temporary);
1829
1830 private:
1831 const size_t index_;
1832
1833 DISALLOW_COPY_AND_ASSIGN(HTemporary);
1834};
1835
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001836class HSuspendCheck : public HTemplateInstruction<0> {
1837 public:
1838 explicit HSuspendCheck(uint32_t dex_pc)
Nicolas Geoffray9ebc72c2014-09-25 16:33:42 +01001839 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001840
1841 virtual bool NeedsEnvironment() const {
1842 return true;
1843 }
1844
1845 uint32_t GetDexPc() const { return dex_pc_; }
1846
1847 DECLARE_INSTRUCTION(SuspendCheck);
1848
1849 private:
1850 const uint32_t dex_pc_;
1851
1852 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
1853};
1854
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001855class MoveOperands : public ArenaObject {
1856 public:
Nicolas Geoffray740475d2014-09-29 10:33:25 +01001857 MoveOperands(Location source, Location destination, HInstruction* instruction)
1858 : source_(source), destination_(destination), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001859
1860 Location GetSource() const { return source_; }
1861 Location GetDestination() const { return destination_; }
1862
1863 void SetSource(Location value) { source_ = value; }
1864 void SetDestination(Location value) { destination_ = value; }
1865
1866 // The parallel move resolver marks moves as "in-progress" by clearing the
1867 // destination (but not the source).
1868 Location MarkPending() {
1869 DCHECK(!IsPending());
1870 Location dest = destination_;
1871 destination_ = Location::NoLocation();
1872 return dest;
1873 }
1874
1875 void ClearPending(Location dest) {
1876 DCHECK(IsPending());
1877 destination_ = dest;
1878 }
1879
1880 bool IsPending() const {
1881 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
1882 return destination_.IsInvalid() && !source_.IsInvalid();
1883 }
1884
1885 // True if this blocks a move from the given location.
1886 bool Blocks(Location loc) const {
1887 return !IsEliminated() && source_.Equals(loc);
1888 }
1889
1890 // A move is redundant if it's been eliminated, if its source and
1891 // destination are the same, or if its destination is unneeded.
1892 bool IsRedundant() const {
1893 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
1894 }
1895
1896 // We clear both operands to indicate move that's been eliminated.
1897 void Eliminate() {
1898 source_ = destination_ = Location::NoLocation();
1899 }
1900
1901 bool IsEliminated() const {
1902 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
1903 return source_.IsInvalid();
1904 }
1905
Nicolas Geoffray740475d2014-09-29 10:33:25 +01001906 HInstruction* GetInstruction() const { return instruction_; }
1907
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001908 private:
1909 Location source_;
1910 Location destination_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01001911 // The instruction this move is assocatied with. Null when this move is
1912 // for moving an input in the expected locations of user (including a phi user).
1913 // This is only used in debug mode, to ensure we do not connect interval siblings
1914 // in the same parallel move.
1915 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001916
1917 DISALLOW_COPY_AND_ASSIGN(MoveOperands);
1918};
1919
1920static constexpr size_t kDefaultNumberOfMoves = 4;
1921
1922class HParallelMove : public HTemplateInstruction<0> {
1923 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001924 explicit HParallelMove(ArenaAllocator* arena)
1925 : HTemplateInstruction(SideEffects::None()), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001926
1927 void AddMove(MoveOperands* move) {
Nicolas Geoffray740475d2014-09-29 10:33:25 +01001928 if (kIsDebugBuild && move->GetInstruction() != nullptr) {
1929 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
1930 DCHECK_NE(moves_.Get(i)->GetInstruction(), move->GetInstruction())
1931 << "Doing parallel moves for the same instruction.";
1932 }
1933 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001934 moves_.Add(move);
1935 }
1936
1937 MoveOperands* MoveOperandsAt(size_t index) const {
1938 return moves_.Get(index);
1939 }
1940
1941 size_t NumMoves() const { return moves_.Size(); }
1942
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001943 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001944
1945 private:
1946 GrowableArray<MoveOperands*> moves_;
1947
1948 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
1949};
1950
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001951class HGraphVisitor : public ValueObject {
1952 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001953 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
1954 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001955
Dave Allison20dfc792014-06-16 20:44:29 -07001956 virtual void VisitInstruction(HInstruction* instruction) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001957 virtual void VisitBasicBlock(HBasicBlock* block);
1958
1959 void VisitInsertionOrder();
1960
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001961 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001962
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001963 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001964#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001965 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
1966
1967 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
1968
1969#undef DECLARE_VISIT_INSTRUCTION
1970
1971 private:
1972 HGraph* graph_;
1973
1974 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
1975};
1976
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001977class HGraphDelegateVisitor : public HGraphVisitor {
1978 public:
1979 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
1980 virtual ~HGraphDelegateVisitor() {}
1981
1982 // Visit functions that delegate to to super class.
1983#define DECLARE_VISIT_INSTRUCTION(name, super) \
1984 virtual void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
1985
1986 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
1987
1988#undef DECLARE_VISIT_INSTRUCTION
1989
1990 private:
1991 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
1992};
1993
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001994class HInsertionOrderIterator : public ValueObject {
1995 public:
1996 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
1997
1998 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
1999 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
2000 void Advance() { ++index_; }
2001
2002 private:
2003 const HGraph& graph_;
2004 size_t index_;
2005
2006 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
2007};
2008
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002009class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002010 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002011 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002012
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002013 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
2014 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002015 void Advance() { ++index_; }
2016
2017 private:
2018 const HGraph& graph_;
2019 size_t index_;
2020
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002021 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002022};
2023
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002024class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002025 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002026 explicit HPostOrderIterator(const HGraph& graph)
2027 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002028
2029 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002030 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002031 void Advance() { --index_; }
2032
2033 private:
2034 const HGraph& graph_;
2035 size_t index_;
2036
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002037 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002038};
2039
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002040} // namespace art
2041
2042#endif // ART_COMPILER_OPTIMIZING_NODES_H_