blob: c6eb806904c12b5d90c861a4f5d29a575889b25e [file] [log] [blame]
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ART_COMPILER_OPTIMIZING_NODES_H_
18#define ART_COMPILER_OPTIMIZING_NODES_H_
19
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010020#include "locations.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010021#include "offsets.h"
Ian Rogerse63db272014-07-15 15:36:11 -070022#include "primitive.h"
Ian Rogers0279ebb2014-10-08 17:27:48 -070023#include "utils/arena_object.h"
Nicolas Geoffray0e336432014-02-26 18:24:38 +000024#include "utils/arena_bit_vector.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000025#include "utils/growable_array.h"
26
27namespace art {
28
29class HBasicBlock;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010030class HEnvironment;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000031class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000032class HIntConstant;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000033class HGraphVisitor;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010034class HPhi;
Nicolas Geoffray3c049742014-09-24 18:10:46 +010035class HSuspendCheck;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010036class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000037class LocationSummary;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000038
39static const int kDefaultNumberOfBlocks = 8;
40static const int kDefaultNumberOfSuccessors = 2;
41static const int kDefaultNumberOfPredecessors = 2;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010042static const int kDefaultNumberOfDominatedBlocks = 1;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000043static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000044
Dave Allison20dfc792014-06-16 20:44:29 -070045enum IfCondition {
46 kCondEQ,
47 kCondNE,
48 kCondLT,
49 kCondLE,
50 kCondGT,
51 kCondGE,
52};
53
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010054class HInstructionList {
55 public:
56 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
57
58 void AddInstruction(HInstruction* instruction);
59 void RemoveInstruction(HInstruction* instruction);
60
Roland Levillain6b469232014-09-25 10:10:38 +010061 // Return true if this list contains `instruction`.
62 bool Contains(HInstruction* instruction) const;
63
Roland Levillainccc07a92014-09-16 14:48:16 +010064 // Return true if `instruction1` is found before `instruction2` in
65 // this instruction list and false otherwise. Abort if none
66 // of these instructions is found.
67 bool FoundBefore(const HInstruction* instruction1,
68 const HInstruction* instruction2) const;
69
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010070 private:
71 HInstruction* first_instruction_;
72 HInstruction* last_instruction_;
73
74 friend class HBasicBlock;
75 friend class HInstructionIterator;
Nicolas Geoffray804d0932014-05-02 08:46:00 +010076 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010077
78 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
79};
80
Nicolas Geoffray818f2102014-02-18 16:43:35 +000081// Control-flow graph of a method. Contains a list of basic blocks.
82class HGraph : public ArenaObject {
83 public:
84 explicit HGraph(ArenaAllocator* arena)
85 : arena_(arena),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000086 blocks_(arena, kDefaultNumberOfBlocks),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +010087 reverse_post_order_(arena, kDefaultNumberOfBlocks),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +010088 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +010089 number_of_vregs_(0),
90 number_of_in_vregs_(0),
Nicolas Geoffraye5038322014-07-04 09:41:32 +010091 number_of_temporaries_(0),
Dave Allison20dfc792014-06-16 20:44:29 -070092 current_instruction_id_(0) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +000093
Nicolas Geoffray787c3072014-03-17 10:20:19 +000094 ArenaAllocator* GetArena() const { return arena_; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +010095 const GrowableArray<HBasicBlock*>& GetBlocks() const { return blocks_; }
Roland Levillain93445682014-10-06 19:24:02 +010096 HBasicBlock* GetBlock(size_t id) const { return blocks_.Get(id); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +000097
Nicolas Geoffray787c3072014-03-17 10:20:19 +000098 HBasicBlock* GetEntryBlock() const { return entry_block_; }
99 HBasicBlock* GetExitBlock() const { return exit_block_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000100
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000101 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
102 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000103
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000104 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100105
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000106 void BuildDominatorTree();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100107 void TransformToSSA();
108 void SimplifyCFG();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000109
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100110 // Find all natural loops in this graph. Aborts computation and returns false
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100111 // if one loop is not natural, that is the header does not dominate the back
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100112 // edge.
113 bool FindNaturalLoops() const;
114
115 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
116 void SimplifyLoop(HBasicBlock* header);
117
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000118 int GetNextInstructionId() {
119 return current_instruction_id_++;
120 }
121
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100122 uint16_t GetMaximumNumberOfOutVRegs() const {
123 return maximum_number_of_out_vregs_;
124 }
125
126 void UpdateMaximumNumberOfOutVRegs(uint16_t new_value) {
127 maximum_number_of_out_vregs_ = std::max(new_value, maximum_number_of_out_vregs_);
128 }
129
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100130 void UpdateNumberOfTemporaries(size_t count) {
131 number_of_temporaries_ = std::max(count, number_of_temporaries_);
132 }
133
134 size_t GetNumberOfTemporaries() const {
135 return number_of_temporaries_;
136 }
137
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100138 void SetNumberOfVRegs(uint16_t number_of_vregs) {
139 number_of_vregs_ = number_of_vregs;
140 }
141
142 uint16_t GetNumberOfVRegs() const {
143 return number_of_vregs_;
144 }
145
146 void SetNumberOfInVRegs(uint16_t value) {
147 number_of_in_vregs_ = value;
148 }
149
150 uint16_t GetNumberOfInVRegs() const {
151 return number_of_in_vregs_;
152 }
153
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100154 uint16_t GetNumberOfLocalVRegs() const {
155 return number_of_vregs_ - number_of_in_vregs_;
156 }
157
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100158 const GrowableArray<HBasicBlock*>& GetReversePostOrder() const {
159 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100160 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100161
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000162 private:
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000163 HBasicBlock* FindCommonDominator(HBasicBlock* first, HBasicBlock* second) const;
164 void VisitBlockForDominatorTree(HBasicBlock* block,
165 HBasicBlock* predecessor,
166 GrowableArray<size_t>* visits);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100167 void FindBackEdges(ArenaBitVector* visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000168 void VisitBlockForBackEdges(HBasicBlock* block,
169 ArenaBitVector* visited,
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100170 ArenaBitVector* visiting);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000171 void RemoveDeadBlocks(const ArenaBitVector& visited) const;
172
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000173 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000174
175 // List of blocks in insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000176 GrowableArray<HBasicBlock*> blocks_;
177
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100178 // List of blocks to perform a reverse post order tree traversal.
179 GrowableArray<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000180
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000181 HBasicBlock* entry_block_;
182 HBasicBlock* exit_block_;
183
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100184 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100185 uint16_t maximum_number_of_out_vregs_;
186
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100187 // The number of virtual registers in this method. Contains the parameters.
188 uint16_t number_of_vregs_;
189
190 // The number of virtual registers used by parameters of this method.
191 uint16_t number_of_in_vregs_;
192
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100193 // The number of temporaries that will be needed for the baseline compiler.
194 size_t number_of_temporaries_;
195
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000196 // The current id to assign to a newly added instruction. See HInstruction.id_.
197 int current_instruction_id_;
198
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000199 DISALLOW_COPY_AND_ASSIGN(HGraph);
200};
201
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000202class HLoopInformation : public ArenaObject {
203 public:
204 HLoopInformation(HBasicBlock* header, HGraph* graph)
205 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100206 suspend_check_(nullptr),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100207 back_edges_(graph->GetArena(), kDefaultNumberOfBackEdges),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100208 // Make bit vector growable, as the number of blocks may change.
209 blocks_(graph->GetArena(), graph->GetBlocks().Size(), true) {}
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100210
211 HBasicBlock* GetHeader() const {
212 return header_;
213 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000214
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100215 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
216 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
217 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
218
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000219 void AddBackEdge(HBasicBlock* back_edge) {
220 back_edges_.Add(back_edge);
221 }
222
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100223 void RemoveBackEdge(HBasicBlock* back_edge) {
224 back_edges_.Delete(back_edge);
225 }
226
227 bool IsBackEdge(HBasicBlock* block) {
228 for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) {
229 if (back_edges_.Get(i) == block) return true;
230 }
231 return false;
232 }
233
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000234 int NumberOfBackEdges() const {
235 return back_edges_.Size();
236 }
237
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100238 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100239
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100240 const GrowableArray<HBasicBlock*>& GetBackEdges() const {
241 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100242 }
243
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100244 void ClearBackEdges() {
245 back_edges_.Reset();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100246 }
247
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100248 // Find blocks that are part of this loop. Returns whether the loop is a natural loop,
249 // that is the header dominates the back edge.
250 bool Populate();
251
252 // Returns whether this loop information contains `block`.
253 // Note that this loop information *must* be populated before entering this function.
254 bool Contains(const HBasicBlock& block) const;
255
256 // Returns whether this loop information is an inner loop of `other`.
257 // Note that `other` *must* be populated before entering this function.
258 bool IsIn(const HLoopInformation& other) const;
259
260 const ArenaBitVector& GetBlocks() const { return blocks_; }
261
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000262 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100263 // Internal recursive implementation of `Populate`.
264 void PopulateRecursive(HBasicBlock* block);
265
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000266 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100267 HSuspendCheck* suspend_check_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000268 GrowableArray<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100269 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000270
271 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
272};
273
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100274static constexpr size_t kNoLifetime = -1;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100275static constexpr uint32_t kNoDexPc = -1;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100276
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000277// A block in a method. Contains the list of instructions represented
278// as a double linked list. Each block knows its predecessors and
279// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100280
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000281class HBasicBlock : public ArenaObject {
282 public:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100283 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000284 : graph_(graph),
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000285 predecessors_(graph->GetArena(), kDefaultNumberOfPredecessors),
286 successors_(graph->GetArena(), kDefaultNumberOfSuccessors),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000287 loop_information_(nullptr),
288 dominator_(nullptr),
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100289 dominated_blocks_(graph->GetArena(), kDefaultNumberOfDominatedBlocks),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100290 block_id_(-1),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100291 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100292 lifetime_start_(kNoLifetime),
293 lifetime_end_(kNoLifetime) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000294
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100295 const GrowableArray<HBasicBlock*>& GetPredecessors() const {
296 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000297 }
298
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100299 const GrowableArray<HBasicBlock*>& GetSuccessors() const {
300 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000301 }
302
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100303 const GrowableArray<HBasicBlock*>& GetDominatedBlocks() const {
304 return dominated_blocks_;
305 }
306
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100307 bool IsEntryBlock() const {
308 return graph_->GetEntryBlock() == this;
309 }
310
311 bool IsExitBlock() const {
312 return graph_->GetExitBlock() == this;
313 }
314
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000315 void AddBackEdge(HBasicBlock* back_edge) {
316 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000317 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000318 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100319 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000320 loop_information_->AddBackEdge(back_edge);
321 }
322
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000323 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000324
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000325 int GetBlockId() const { return block_id_; }
326 void SetBlockId(int id) { block_id_ = id; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000327
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000328 HBasicBlock* GetDominator() const { return dominator_; }
329 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100330 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.Add(block); }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000331
332 int NumberOfBackEdges() const {
333 return loop_information_ == nullptr
334 ? 0
335 : loop_information_->NumberOfBackEdges();
336 }
337
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100338 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
339 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100340 const HInstructionList& GetInstructions() const { return instructions_; }
341 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100342 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000343
344 void AddSuccessor(HBasicBlock* block) {
345 successors_.Add(block);
346 block->predecessors_.Add(this);
347 }
348
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100349 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
350 size_t successor_index = GetSuccessorIndexOf(existing);
351 DCHECK_NE(successor_index, static_cast<size_t>(-1));
352 existing->RemovePredecessor(this);
353 new_block->predecessors_.Add(this);
354 successors_.Put(successor_index, new_block);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000355 }
356
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100357 void RemovePredecessor(HBasicBlock* block) {
358 predecessors_.Delete(block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100359 }
360
361 void ClearAllPredecessors() {
362 predecessors_.Reset();
363 }
364
365 void AddPredecessor(HBasicBlock* block) {
366 predecessors_.Add(block);
367 block->successors_.Add(this);
368 }
369
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100370 void SwapPredecessors() {
Nicolas Geoffrayc83d4412014-09-18 16:46:20 +0100371 DCHECK_EQ(predecessors_.Size(), 2u);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100372 HBasicBlock* temp = predecessors_.Get(0);
373 predecessors_.Put(0, predecessors_.Get(1));
374 predecessors_.Put(1, temp);
375 }
376
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100377 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) {
378 for (size_t i = 0, e = predecessors_.Size(); i < e; ++i) {
379 if (predecessors_.Get(i) == predecessor) {
380 return i;
381 }
382 }
383 return -1;
384 }
385
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100386 size_t GetSuccessorIndexOf(HBasicBlock* successor) {
387 for (size_t i = 0, e = successors_.Size(); i < e; ++i) {
388 if (successors_.Get(i) == successor) {
389 return i;
390 }
391 }
392 return -1;
393 }
394
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000395 void AddInstruction(HInstruction* instruction);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100396 void RemoveInstruction(HInstruction* instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100397 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +0100398 // Replace instruction `initial` with `replacement` within this block.
399 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
400 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100401 void AddPhi(HPhi* phi);
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 Levillaine161a2a2014-10-03 12:45:18 +0100653 virtual bool CanThrow() const { return false; }
Roland Levillain72bceff2014-09-15 18:29:00 +0100654 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100655
656 void AddUseAt(HInstruction* user, size_t index) {
657 uses_ = new (block_->GetGraph()->GetArena()) HUseListNode<HInstruction>(user, index, uses_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000658 }
659
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100660 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +0100661 DCHECK(user != nullptr);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100662 env_uses_ = new (block_->GetGraph()->GetArena()) HUseListNode<HEnvironment>(
663 user, index, env_uses_);
664 }
665
666 void RemoveUser(HInstruction* user, size_t index);
Nicolas Geoffray724c9632014-09-22 12:27:27 +0100667 void RemoveEnvironmentUser(HEnvironment* user, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100668
669 HUseListNode<HInstruction>* GetUses() const { return uses_; }
670 HUseListNode<HEnvironment>* GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000671
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100672 bool HasUses() const { return uses_ != nullptr || env_uses_ != nullptr; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100673 bool HasEnvironmentUses() const { return env_uses_ != nullptr; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000674
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100675 size_t NumberOfUses() const {
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100676 // TODO: Optimize this method if it is used outside of the HGraphVisualizer.
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100677 size_t result = 0;
678 HUseListNode<HInstruction>* current = uses_;
679 while (current != nullptr) {
680 current = current->GetTail();
681 ++result;
682 }
683 return result;
684 }
685
Roland Levillainccc07a92014-09-16 14:48:16 +0100686 // Does this instruction dominate `other_instruction`? Aborts if
687 // this instruction and `other_instruction` are both phis.
688 bool Dominates(HInstruction* other_instruction) const;
689
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000690 int GetId() const { return id_; }
691 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000692
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100693 int GetSsaIndex() const { return ssa_index_; }
694 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
695 bool HasSsaIndex() const { return ssa_index_ != -1; }
696
697 bool HasEnvironment() const { return environment_ != nullptr; }
698 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100699 void SetEnvironment(HEnvironment* environment) { environment_ = environment; }
700
Nicolas Geoffray39468442014-09-02 15:17:15 +0100701 // Returns the number of entries in the environment. Typically, that is the
702 // number of dex registers in a method. It could be more in case of inlining.
703 size_t EnvironmentSize() const;
704
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000705 LocationSummary* GetLocations() const { return locations_; }
706 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000707
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100708 void ReplaceWith(HInstruction* instruction);
709
Dave Allison20dfc792014-06-16 20:44:29 -0700710 bool HasOnlyOneUse() const {
711 return uses_ != nullptr && uses_->GetTail() == nullptr;
712 }
713
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100714#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +0100715 bool Is##type() const { return (As##type() != nullptr); } \
716 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000717 virtual H##type* As##type() { return nullptr; }
718
719 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
720#undef INSTRUCTION_TYPE_CHECK
721
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100722 // Returns whether the instruction can be moved within the graph.
723 virtual bool CanBeMoved() const { return false; }
724
725 // Returns whether the two instructions are of the same kind.
726 virtual bool InstructionTypeEquals(HInstruction* other) const { return false; }
727
728 // Returns whether any data encoded in the two instructions is equal.
729 // This method does not look at the inputs. Both instructions must be
730 // of the same type, otherwise the method has undefined behavior.
731 virtual bool InstructionDataEquals(HInstruction* other) const { return false; }
732
733 // Returns whether two instructions are equal, that is:
734 // 1) They have the same type and contain the same data,
735 // 2) Their inputs are identical.
736 bool Equals(HInstruction* other) const;
737
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100738 virtual InstructionKind GetKind() const = 0;
739
740 virtual size_t ComputeHashCode() const {
741 size_t result = GetKind();
742 for (size_t i = 0, e = InputCount(); i < e; ++i) {
743 result = (result * 31) + InputAt(i)->GetId();
744 }
745 return result;
746 }
747
748 SideEffects GetSideEffects() const { return side_effects_; }
749
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100750 size_t GetLifetimePosition() const { return lifetime_position_; }
751 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
752 LiveInterval* GetLiveInterval() const { return live_interval_; }
753 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
754 bool HasLiveInterval() const { return live_interval_ != nullptr; }
755
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000756 private:
757 HInstruction* previous_;
758 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000759 HBasicBlock* block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000760
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000761 // An instruction gets an id when it is added to the graph.
762 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +0100763 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000764 int id_;
765
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100766 // When doing liveness analysis, instructions that have uses get an SSA index.
767 int ssa_index_;
768
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100769 // List of instructions that have this instruction as input.
770 HUseListNode<HInstruction>* uses_;
771
772 // List of environments that contain this instruction.
773 HUseListNode<HEnvironment>* env_uses_;
774
Nicolas Geoffray39468442014-09-02 15:17:15 +0100775 // The environment associated with this instruction. Not null if the instruction
776 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100777 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000778
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000779 // Set by the code generator.
780 LocationSummary* locations_;
781
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100782 // Set by the liveness analysis.
783 LiveInterval* live_interval_;
784
785 // Set by the liveness analysis, this is the position in a linear
786 // order of blocks where this instruction's live interval start.
787 size_t lifetime_position_;
788
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100789 const SideEffects side_effects_;
790
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000791 friend class HBasicBlock;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100792 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000793
794 DISALLOW_COPY_AND_ASSIGN(HInstruction);
795};
796
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100797template<typename T>
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000798class HUseIterator : public ValueObject {
799 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100800 explicit HUseIterator(HUseListNode<T>* uses) : current_(uses) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000801
802 bool Done() const { return current_ == nullptr; }
803
804 void Advance() {
805 DCHECK(!Done());
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000806 current_ = current_->GetTail();
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000807 }
808
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100809 HUseListNode<T>* Current() const {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000810 DCHECK(!Done());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100811 return current_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000812 }
813
814 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100815 HUseListNode<T>* current_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000816
817 friend class HValue;
818};
819
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100820// A HEnvironment object contains the values of virtual registers at a given location.
821class HEnvironment : public ArenaObject {
822 public:
823 HEnvironment(ArenaAllocator* arena, size_t number_of_vregs) : vregs_(arena, number_of_vregs) {
824 vregs_.SetSize(number_of_vregs);
825 for (size_t i = 0; i < number_of_vregs; i++) {
826 vregs_.Put(i, nullptr);
827 }
828 }
829
830 void Populate(const GrowableArray<HInstruction*>& env) {
831 for (size_t i = 0; i < env.Size(); i++) {
832 HInstruction* instruction = env.Get(i);
833 vregs_.Put(i, instruction);
834 if (instruction != nullptr) {
835 instruction->AddEnvUseAt(this, i);
836 }
837 }
838 }
839
840 void SetRawEnvAt(size_t index, HInstruction* instruction) {
841 vregs_.Put(index, instruction);
842 }
843
Nicolas Geoffray39468442014-09-02 15:17:15 +0100844 HInstruction* GetInstructionAt(size_t index) const {
845 return vregs_.Get(index);
846 }
847
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100848 GrowableArray<HInstruction*>* GetVRegs() {
849 return &vregs_;
850 }
851
Nicolas Geoffray39468442014-09-02 15:17:15 +0100852 size_t Size() const { return vregs_.Size(); }
853
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100854 private:
855 GrowableArray<HInstruction*> vregs_;
856
857 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
858};
859
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000860class HInputIterator : public ValueObject {
861 public:
Dave Allison20dfc792014-06-16 20:44:29 -0700862 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000863
864 bool Done() const { return index_ == instruction_->InputCount(); }
865 HInstruction* Current() const { return instruction_->InputAt(index_); }
866 void Advance() { index_++; }
867
868 private:
869 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100870 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000871
872 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
873};
874
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000875class HInstructionIterator : public ValueObject {
876 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100877 explicit HInstructionIterator(const HInstructionList& instructions)
878 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000879 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000880 }
881
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000882 bool Done() const { return instruction_ == nullptr; }
883 HInstruction* Current() const { return instruction_; }
884 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000885 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000886 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000887 }
888
889 private:
890 HInstruction* instruction_;
891 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100892
893 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000894};
895
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100896class HBackwardInstructionIterator : public ValueObject {
897 public:
898 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
899 : instruction_(instructions.last_instruction_) {
900 next_ = Done() ? nullptr : instruction_->GetPrevious();
901 }
902
903 bool Done() const { return instruction_ == nullptr; }
904 HInstruction* Current() const { return instruction_; }
905 void Advance() {
906 instruction_ = next_;
907 next_ = Done() ? nullptr : instruction_->GetPrevious();
908 }
909
910 private:
911 HInstruction* instruction_;
912 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100913
914 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100915};
916
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000917// An embedded container with N elements of type T. Used (with partial
918// specialization for N=0) because embedded arrays cannot have size 0.
919template<typename T, intptr_t N>
920class EmbeddedArray {
921 public:
Dave Allison20dfc792014-06-16 20:44:29 -0700922 EmbeddedArray() : elements_() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000923
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000924 intptr_t GetLength() const { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000925
926 const T& operator[](intptr_t i) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000927 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000928 return elements_[i];
929 }
930
931 T& operator[](intptr_t i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000932 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000933 return elements_[i];
934 }
935
936 const T& At(intptr_t i) const {
937 return (*this)[i];
938 }
939
940 void SetAt(intptr_t i, const T& val) {
941 (*this)[i] = val;
942 }
943
944 private:
945 T elements_[N];
946};
947
948template<typename T>
949class EmbeddedArray<T, 0> {
950 public:
951 intptr_t length() const { return 0; }
952 const T& operator[](intptr_t i) const {
953 LOG(FATAL) << "Unreachable";
954 static T sentinel = 0;
955 return sentinel;
956 }
957 T& operator[](intptr_t i) {
958 LOG(FATAL) << "Unreachable";
959 static T sentinel = 0;
960 return sentinel;
961 }
962};
963
964template<intptr_t N>
965class HTemplateInstruction: public HInstruction {
966 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100967 HTemplateInstruction<N>(SideEffects side_effects)
968 : HInstruction(side_effects), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -0700969 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000970
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100971 virtual size_t InputCount() const { return N; }
972 virtual HInstruction* InputAt(size_t i) const { return inputs_[i]; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000973
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000974 protected:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100975 virtual void SetRawInputAt(size_t i, HInstruction* instruction) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000976 inputs_[i] = instruction;
977 }
978
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000979 private:
980 EmbeddedArray<HInstruction*, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100981
982 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000983};
984
Dave Allison20dfc792014-06-16 20:44:29 -0700985template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100986class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -0700987 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100988 HExpression<N>(Primitive::Type type, SideEffects side_effects)
989 : HTemplateInstruction<N>(side_effects), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -0700990 virtual ~HExpression() {}
991
992 virtual Primitive::Type GetType() const { return type_; }
993
994 private:
995 const Primitive::Type type_;
996};
997
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000998// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
999// instruction that branches to the exit block.
1000class HReturnVoid : public HTemplateInstruction<0> {
1001 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001002 HReturnVoid() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001003
1004 virtual bool IsControlFlow() const { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001005
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001006 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001007
1008 private:
1009 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
1010};
1011
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001012// Represents dex's RETURN opcodes. A HReturn is a control flow
1013// instruction that branches to the exit block.
1014class HReturn : public HTemplateInstruction<1> {
1015 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001016 explicit HReturn(HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001017 SetRawInputAt(0, value);
1018 }
1019
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001020 virtual bool IsControlFlow() const { return true; }
1021
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001022 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001023
1024 private:
1025 DISALLOW_COPY_AND_ASSIGN(HReturn);
1026};
1027
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001028// The exit instruction is the only instruction of the exit block.
1029// Instructions aborting the method (HTrow and HReturn) must branch to the
1030// exit block.
1031class HExit : public HTemplateInstruction<0> {
1032 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001033 HExit() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001034
1035 virtual bool IsControlFlow() const { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001036
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001037 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001038
1039 private:
1040 DISALLOW_COPY_AND_ASSIGN(HExit);
1041};
1042
1043// Jumps from one block to another.
1044class HGoto : public HTemplateInstruction<0> {
1045 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001046 HGoto() : HTemplateInstruction(SideEffects::None()) {}
1047
1048 virtual bool IsControlFlow() const { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001049
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001050 HBasicBlock* GetSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001051 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001052 }
1053
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001054 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001055
1056 private:
1057 DISALLOW_COPY_AND_ASSIGN(HGoto);
1058};
1059
Dave Allison20dfc792014-06-16 20:44:29 -07001060
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001061// Conditional branch. A block ending with an HIf instruction must have
1062// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001063class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001064 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001065 explicit HIf(HInstruction* input) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001066 SetRawInputAt(0, input);
1067 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001068
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001069 virtual bool IsControlFlow() const { return true; }
1070
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001071 HBasicBlock* IfTrueSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001072 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001073 }
1074
1075 HBasicBlock* IfFalseSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001076 return GetBlock()->GetSuccessors().Get(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001077 }
1078
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001079 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001080
Dave Allison20dfc792014-06-16 20:44:29 -07001081 virtual bool IsIfInstruction() const { return true; }
1082
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001083 private:
1084 DISALLOW_COPY_AND_ASSIGN(HIf);
1085};
1086
Dave Allison20dfc792014-06-16 20:44:29 -07001087class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001088 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001089 HBinaryOperation(Primitive::Type result_type,
1090 HInstruction* left,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001091 HInstruction* right) : HExpression(result_type, SideEffects::None()) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001092 SetRawInputAt(0, left);
1093 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001094 }
1095
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001096 HInstruction* GetLeft() const { return InputAt(0); }
1097 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07001098 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001099
1100 virtual bool IsCommutative() { return false; }
1101
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001102 virtual bool CanBeMoved() const { return true; }
1103 virtual bool InstructionDataEquals(HInstruction* other) const { return true; }
1104
Roland Levillain556c3d12014-09-18 15:25:07 +01001105 // Try to statically evaluate `operation` and return an HConstant
1106 // containing the result of this evaluation. If `operation` cannot
1107 // be evaluated as a constant, return nullptr.
1108 HConstant* TryStaticEvaluation(ArenaAllocator* allocator) const;
1109
1110 // Apply this operation to `x` and `y`.
1111 virtual int32_t Evaluate(int32_t x, int32_t y) const = 0;
1112 virtual int64_t Evaluate(int64_t x, int64_t y) const = 0;
1113
Roland Levillainccc07a92014-09-16 14:48:16 +01001114 DECLARE_INSTRUCTION(BinaryOperation);
1115
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001116 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001117 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
1118};
1119
Dave Allison20dfc792014-06-16 20:44:29 -07001120class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001121 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001122 HCondition(HInstruction* first, HInstruction* second)
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001123 : HBinaryOperation(Primitive::kPrimBoolean, first, second),
1124 needs_materialization_(true) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001125
1126 virtual bool IsCommutative() { return true; }
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001127
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001128 bool NeedsMaterialization() const { return needs_materialization_; }
1129 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001130
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001131 // For code generation purposes, returns whether this instruction is just before
1132 // `if_`, and disregard moves in between.
1133 bool IsBeforeWhenDisregardMoves(HIf* if_) const;
1134
Dave Allison20dfc792014-06-16 20:44:29 -07001135 DECLARE_INSTRUCTION(Condition);
1136
1137 virtual IfCondition GetCondition() const = 0;
1138
1139 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001140 // For register allocation purposes, returns whether this instruction needs to be
1141 // materialized (that is, not just be in the processor flags).
1142 bool needs_materialization_;
1143
Dave Allison20dfc792014-06-16 20:44:29 -07001144 DISALLOW_COPY_AND_ASSIGN(HCondition);
1145};
1146
1147// Instruction to check if two inputs are equal to each other.
1148class HEqual : public HCondition {
1149 public:
1150 HEqual(HInstruction* first, HInstruction* second)
1151 : HCondition(first, second) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001152
Roland Levillain93445682014-10-06 19:24:02 +01001153 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1154 return x == y ? 1 : 0;
1155 }
1156 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1157 return x == y ? 1 : 0;
1158 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001159
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001160 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001161
Dave Allison20dfc792014-06-16 20:44:29 -07001162 virtual IfCondition GetCondition() const {
1163 return kCondEQ;
1164 }
1165
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001166 private:
1167 DISALLOW_COPY_AND_ASSIGN(HEqual);
1168};
1169
Dave Allison20dfc792014-06-16 20:44:29 -07001170class HNotEqual : public HCondition {
1171 public:
1172 HNotEqual(HInstruction* first, HInstruction* second)
1173 : HCondition(first, second) {}
1174
Roland Levillain93445682014-10-06 19:24:02 +01001175 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1176 return x != y ? 1 : 0;
1177 }
1178 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1179 return x != y ? 1 : 0;
1180 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001181
Dave Allison20dfc792014-06-16 20:44:29 -07001182 DECLARE_INSTRUCTION(NotEqual);
1183
1184 virtual IfCondition GetCondition() const {
1185 return kCondNE;
1186 }
1187
1188 private:
1189 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
1190};
1191
1192class HLessThan : public HCondition {
1193 public:
1194 HLessThan(HInstruction* first, HInstruction* second)
1195 : HCondition(first, second) {}
1196
Roland Levillain93445682014-10-06 19:24:02 +01001197 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1198 return x < y ? 1 : 0;
1199 }
1200 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1201 return x < y ? 1 : 0;
1202 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001203
Dave Allison20dfc792014-06-16 20:44:29 -07001204 DECLARE_INSTRUCTION(LessThan);
1205
1206 virtual IfCondition GetCondition() const {
1207 return kCondLT;
1208 }
1209
1210 private:
1211 DISALLOW_COPY_AND_ASSIGN(HLessThan);
1212};
1213
1214class HLessThanOrEqual : public HCondition {
1215 public:
1216 HLessThanOrEqual(HInstruction* first, HInstruction* second)
1217 : HCondition(first, second) {}
1218
Roland Levillain93445682014-10-06 19:24:02 +01001219 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1220 return x <= y ? 1 : 0;
1221 }
1222 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1223 return x <= y ? 1 : 0;
1224 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001225
Dave Allison20dfc792014-06-16 20:44:29 -07001226 DECLARE_INSTRUCTION(LessThanOrEqual);
1227
1228 virtual IfCondition GetCondition() const {
1229 return kCondLE;
1230 }
1231
1232 private:
1233 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
1234};
1235
1236class HGreaterThan : public HCondition {
1237 public:
1238 HGreaterThan(HInstruction* first, HInstruction* second)
1239 : HCondition(first, second) {}
1240
Roland Levillain93445682014-10-06 19:24:02 +01001241 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1242 return x > y ? 1 : 0;
1243 }
1244 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1245 return x > y ? 1 : 0;
1246 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001247
Dave Allison20dfc792014-06-16 20:44:29 -07001248 DECLARE_INSTRUCTION(GreaterThan);
1249
1250 virtual IfCondition GetCondition() const {
1251 return kCondGT;
1252 }
1253
1254 private:
1255 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
1256};
1257
1258class HGreaterThanOrEqual : public HCondition {
1259 public:
1260 HGreaterThanOrEqual(HInstruction* first, HInstruction* second)
1261 : HCondition(first, second) {}
1262
Roland Levillain93445682014-10-06 19:24:02 +01001263 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1264 return x >= y ? 1 : 0;
1265 }
1266 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1267 return x >= y ? 1 : 0;
1268 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001269
Dave Allison20dfc792014-06-16 20:44:29 -07001270 DECLARE_INSTRUCTION(GreaterThanOrEqual);
1271
1272 virtual IfCondition GetCondition() const {
1273 return kCondGE;
1274 }
1275
1276 private:
1277 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
1278};
1279
1280
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001281// Instruction to check how two inputs compare to each other.
1282// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
1283class HCompare : public HBinaryOperation {
1284 public:
1285 HCompare(Primitive::Type type, HInstruction* first, HInstruction* second)
1286 : HBinaryOperation(Primitive::kPrimInt, first, second) {
1287 DCHECK_EQ(type, first->GetType());
1288 DCHECK_EQ(type, second->GetType());
1289 }
1290
Roland Levillain93445682014-10-06 19:24:02 +01001291 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001292 return
1293 x == y ? 0 :
1294 x > y ? 1 :
1295 -1;
1296 }
Roland Levillain93445682014-10-06 19:24:02 +01001297 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001298 return
1299 x == y ? 0 :
1300 x > y ? 1 :
1301 -1;
1302 }
1303
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001304 DECLARE_INSTRUCTION(Compare);
1305
1306 private:
1307 DISALLOW_COPY_AND_ASSIGN(HCompare);
1308};
1309
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001310// A local in the graph. Corresponds to a Dex register.
1311class HLocal : public HTemplateInstruction<0> {
1312 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001313 explicit HLocal(uint16_t reg_number)
1314 : HTemplateInstruction(SideEffects::None()), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001315
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001316 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001317
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001318 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001319
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001320 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001321 // The Dex register number.
1322 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001323
1324 DISALLOW_COPY_AND_ASSIGN(HLocal);
1325};
1326
1327// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07001328class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001329 public:
Roland Levillain5799fc02014-09-25 12:15:20 +01001330 HLoadLocal(HLocal* local, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001331 : HExpression(type, SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001332 SetRawInputAt(0, local);
1333 }
1334
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001335 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1336
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001337 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001338
1339 private:
1340 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
1341};
1342
1343// Store a value in a given local. This instruction has two inputs: the value
1344// and the local.
1345class HStoreLocal : public HTemplateInstruction<2> {
1346 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001347 HStoreLocal(HLocal* local, HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001348 SetRawInputAt(0, local);
1349 SetRawInputAt(1, value);
1350 }
1351
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001352 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1353
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001354 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001355
1356 private:
1357 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
1358};
1359
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001360class HConstant : public HExpression<0> {
1361 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001362 explicit HConstant(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
1363
1364 virtual bool CanBeMoved() const { return true; }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001365
1366 DECLARE_INSTRUCTION(Constant);
1367
1368 private:
1369 DISALLOW_COPY_AND_ASSIGN(HConstant);
1370};
1371
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001372// Constants of the type int. Those can be from Dex instructions, or
1373// synthesized (for example with the if-eqz instruction).
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001374class HIntConstant : public HConstant {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001375 public:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001376 explicit HIntConstant(int32_t value) : HConstant(Primitive::kPrimInt), value_(value) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001377
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001378 int32_t GetValue() const { return value_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001379
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001380 virtual bool InstructionDataEquals(HInstruction* other) const {
1381 return other->AsIntConstant()->value_ == value_;
1382 }
1383
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001384 virtual size_t ComputeHashCode() const { return GetValue(); }
1385
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001386 DECLARE_INSTRUCTION(IntConstant);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001387
1388 private:
1389 const int32_t value_;
1390
1391 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
1392};
1393
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001394class HLongConstant : public HConstant {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001395 public:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001396 explicit HLongConstant(int64_t value) : HConstant(Primitive::kPrimLong), value_(value) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001397
1398 int64_t GetValue() const { return value_; }
1399
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001400 virtual bool InstructionDataEquals(HInstruction* other) const {
1401 return other->AsLongConstant()->value_ == value_;
1402 }
1403
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001404 virtual size_t ComputeHashCode() const { return static_cast<size_t>(GetValue()); }
1405
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001406 DECLARE_INSTRUCTION(LongConstant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001407
1408 private:
1409 const int64_t value_;
1410
1411 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
1412};
1413
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001414class HInvoke : public HInstruction {
1415 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001416 HInvoke(ArenaAllocator* arena,
1417 uint32_t number_of_arguments,
1418 Primitive::Type return_type,
1419 uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001420 : HInstruction(SideEffects::All()),
1421 inputs_(arena, number_of_arguments),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001422 return_type_(return_type),
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001423 dex_pc_(dex_pc) {
1424 inputs_.SetSize(number_of_arguments);
1425 }
1426
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001427 virtual size_t InputCount() const { return inputs_.Size(); }
1428 virtual HInstruction* InputAt(size_t i) const { return inputs_.Get(i); }
1429
1430 // Runtime needs to walk the stack, so Dex -> Dex calls need to
1431 // know their environment.
1432 virtual bool NeedsEnvironment() const { return true; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001433
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001434 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001435 SetRawInputAt(index, argument);
1436 }
1437
1438 virtual void SetRawInputAt(size_t index, HInstruction* input) {
1439 inputs_.Put(index, input);
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001440 }
1441
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001442 virtual Primitive::Type GetType() const { return return_type_; }
1443
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001444 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001445
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001446 DECLARE_INSTRUCTION(Invoke);
1447
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001448 protected:
1449 GrowableArray<HInstruction*> inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001450 const Primitive::Type return_type_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001451 const uint32_t dex_pc_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001452
1453 private:
1454 DISALLOW_COPY_AND_ASSIGN(HInvoke);
1455};
1456
1457class HInvokeStatic : public HInvoke {
1458 public:
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001459 HInvokeStatic(ArenaAllocator* arena,
1460 uint32_t number_of_arguments,
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001461 Primitive::Type return_type,
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001462 uint32_t dex_pc,
1463 uint32_t index_in_dex_cache)
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001464 : HInvoke(arena, number_of_arguments, return_type, dex_pc),
1465 index_in_dex_cache_(index_in_dex_cache) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001466
1467 uint32_t GetIndexInDexCache() const { return index_in_dex_cache_; }
1468
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001469 DECLARE_INSTRUCTION(InvokeStatic);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001470
1471 private:
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001472 const uint32_t index_in_dex_cache_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001473
1474 DISALLOW_COPY_AND_ASSIGN(HInvokeStatic);
1475};
1476
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001477class HInvokeVirtual : public HInvoke {
1478 public:
1479 HInvokeVirtual(ArenaAllocator* arena,
1480 uint32_t number_of_arguments,
1481 Primitive::Type return_type,
1482 uint32_t dex_pc,
1483 uint32_t vtable_index)
1484 : HInvoke(arena, number_of_arguments, return_type, dex_pc),
1485 vtable_index_(vtable_index) {}
1486
1487 uint32_t GetVTableIndex() const { return vtable_index_; }
1488
1489 DECLARE_INSTRUCTION(InvokeVirtual);
1490
1491 private:
1492 const uint32_t vtable_index_;
1493
1494 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
1495};
1496
Dave Allison20dfc792014-06-16 20:44:29 -07001497class HNewInstance : public HExpression<0> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001498 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001499 HNewInstance(uint32_t dex_pc, uint16_t type_index)
1500 : HExpression(Primitive::kPrimNot, SideEffects::None()),
1501 dex_pc_(dex_pc),
1502 type_index_(type_index) {}
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001503
1504 uint32_t GetDexPc() const { return dex_pc_; }
1505 uint16_t GetTypeIndex() const { return type_index_; }
1506
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001507 // Calls runtime so needs an environment.
1508 virtual bool NeedsEnvironment() const { return true; }
1509
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001510 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001511
1512 private:
1513 const uint32_t dex_pc_;
1514 const uint16_t type_index_;
1515
1516 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
1517};
1518
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001519class HAdd : public HBinaryOperation {
1520 public:
1521 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1522 : HBinaryOperation(result_type, left, right) {}
1523
1524 virtual bool IsCommutative() { return true; }
1525
Roland Levillain93445682014-10-06 19:24:02 +01001526 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1527 return x + y;
1528 }
1529 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1530 return x + y;
1531 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001532
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001533 DECLARE_INSTRUCTION(Add);
1534
1535 private:
1536 DISALLOW_COPY_AND_ASSIGN(HAdd);
1537};
1538
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001539class HSub : public HBinaryOperation {
1540 public:
1541 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1542 : HBinaryOperation(result_type, left, right) {}
1543
1544 virtual bool IsCommutative() { return false; }
1545
Roland Levillain93445682014-10-06 19:24:02 +01001546 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1547 return x - y;
1548 }
1549 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1550 return x - y;
1551 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001552
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001553 DECLARE_INSTRUCTION(Sub);
1554
1555 private:
1556 DISALLOW_COPY_AND_ASSIGN(HSub);
1557};
1558
1559// The value of a parameter in this method. Its location depends on
1560// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07001561class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001562 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001563 HParameterValue(uint8_t index, Primitive::Type parameter_type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001564 : HExpression(parameter_type, SideEffects::None()), index_(index) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001565
1566 uint8_t GetIndex() const { return index_; }
1567
1568 DECLARE_INSTRUCTION(ParameterValue);
1569
1570 private:
1571 // The index of this parameter in the parameters list. Must be less
1572 // than HGraph::number_of_in_vregs_;
1573 const uint8_t index_;
1574
1575 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
1576};
1577
Dave Allison20dfc792014-06-16 20:44:29 -07001578class HNot : public HExpression<1> {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001579 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001580 explicit HNot(HInstruction* input) : HExpression(Primitive::kPrimBoolean, SideEffects::None()) {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001581 SetRawInputAt(0, input);
1582 }
1583
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001584 virtual bool CanBeMoved() const { return true; }
1585 virtual bool InstructionDataEquals(HInstruction* other) const { return true; }
1586
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001587 DECLARE_INSTRUCTION(Not);
1588
1589 private:
1590 DISALLOW_COPY_AND_ASSIGN(HNot);
1591};
1592
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001593class HPhi : public HInstruction {
1594 public:
1595 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001596 : HInstruction(SideEffects::None()),
1597 inputs_(arena, number_of_inputs),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001598 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001599 type_(type),
1600 is_live_(false) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001601 inputs_.SetSize(number_of_inputs);
1602 }
1603
1604 virtual size_t InputCount() const { return inputs_.Size(); }
1605 virtual HInstruction* InputAt(size_t i) const { return inputs_.Get(i); }
1606
1607 virtual void SetRawInputAt(size_t index, HInstruction* input) {
1608 inputs_.Put(index, input);
1609 }
1610
1611 void AddInput(HInstruction* input);
1612
1613 virtual Primitive::Type GetType() const { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001614 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001615
1616 uint32_t GetRegNumber() const { return reg_number_; }
1617
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001618 void SetDead() { is_live_ = false; }
1619 void SetLive() { is_live_ = true; }
1620 bool IsDead() const { return !is_live_; }
1621 bool IsLive() const { return is_live_; }
1622
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001623 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001624
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001625 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001626 GrowableArray<HInstruction*> inputs_;
1627 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001628 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001629 bool is_live_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001630
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001631 DISALLOW_COPY_AND_ASSIGN(HPhi);
1632};
1633
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001634class HNullCheck : public HExpression<1> {
1635 public:
1636 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001637 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001638 SetRawInputAt(0, value);
1639 }
1640
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001641 virtual bool CanBeMoved() const { return true; }
1642 virtual bool InstructionDataEquals(HInstruction* other) const { return true; }
1643
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001644 virtual bool NeedsEnvironment() const { return true; }
1645
Roland Levillaine161a2a2014-10-03 12:45:18 +01001646 virtual bool CanThrow() const { return true; }
1647
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001648 uint32_t GetDexPc() const { return dex_pc_; }
1649
1650 DECLARE_INSTRUCTION(NullCheck);
1651
1652 private:
1653 const uint32_t dex_pc_;
1654
1655 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
1656};
1657
1658class FieldInfo : public ValueObject {
1659 public:
Roland Levillain5799fc02014-09-25 12:15:20 +01001660 FieldInfo(MemberOffset field_offset, Primitive::Type field_type)
Nicolas Geoffray39468442014-09-02 15:17:15 +01001661 : field_offset_(field_offset), field_type_(field_type) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001662
1663 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001664 Primitive::Type GetFieldType() const { return field_type_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001665
1666 private:
1667 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01001668 const Primitive::Type field_type_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001669};
1670
1671class HInstanceFieldGet : public HExpression<1> {
1672 public:
1673 HInstanceFieldGet(HInstruction* value,
1674 Primitive::Type field_type,
1675 MemberOffset field_offset)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001676 : HExpression(field_type, SideEffects::DependsOnSomething()),
1677 field_info_(field_offset, field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001678 SetRawInputAt(0, value);
1679 }
1680
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001681 virtual bool CanBeMoved() const { return true; }
1682 virtual bool InstructionDataEquals(HInstruction* other) const {
1683 size_t other_offset = other->AsInstanceFieldGet()->GetFieldOffset().SizeValue();
1684 return other_offset == GetFieldOffset().SizeValue();
1685 }
1686
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001687 virtual size_t ComputeHashCode() const {
1688 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
1689 }
1690
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001691 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001692 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001693
1694 DECLARE_INSTRUCTION(InstanceFieldGet);
1695
1696 private:
1697 const FieldInfo field_info_;
1698
1699 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
1700};
1701
1702class HInstanceFieldSet : public HTemplateInstruction<2> {
1703 public:
1704 HInstanceFieldSet(HInstruction* object,
1705 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01001706 Primitive::Type field_type,
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001707 MemberOffset field_offset)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001708 : HTemplateInstruction(SideEffects::ChangesSomething()),
1709 field_info_(field_offset, field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001710 SetRawInputAt(0, object);
1711 SetRawInputAt(1, value);
1712 }
1713
1714 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001715 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001716
1717 DECLARE_INSTRUCTION(InstanceFieldSet);
1718
1719 private:
1720 const FieldInfo field_info_;
1721
1722 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
1723};
1724
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001725class HArrayGet : public HExpression<2> {
1726 public:
1727 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001728 : HExpression(type, SideEffects::DependsOnSomething()) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001729 SetRawInputAt(0, array);
1730 SetRawInputAt(1, index);
1731 }
1732
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001733 virtual bool CanBeMoved() const { return true; }
1734 virtual bool InstructionDataEquals(HInstruction* other) const { return true; }
1735
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001736 DECLARE_INSTRUCTION(ArrayGet);
1737
1738 private:
1739 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
1740};
1741
1742class HArraySet : public HTemplateInstruction<3> {
1743 public:
1744 HArraySet(HInstruction* array,
1745 HInstruction* index,
1746 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01001747 Primitive::Type component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001748 uint32_t dex_pc)
1749 : HTemplateInstruction(SideEffects::ChangesSomething()),
1750 dex_pc_(dex_pc),
1751 component_type_(component_type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001752 SetRawInputAt(0, array);
1753 SetRawInputAt(1, index);
1754 SetRawInputAt(2, value);
1755 }
1756
1757 virtual bool NeedsEnvironment() const {
1758 // We currently always call a runtime method to catch array store
1759 // exceptions.
1760 return InputAt(2)->GetType() == Primitive::kPrimNot;
1761 }
1762
1763 uint32_t GetDexPc() const { return dex_pc_; }
1764
Nicolas Geoffray39468442014-09-02 15:17:15 +01001765 Primitive::Type GetComponentType() const { return component_type_; }
1766
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001767 DECLARE_INSTRUCTION(ArraySet);
1768
1769 private:
1770 const uint32_t dex_pc_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01001771 const Primitive::Type component_type_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001772
1773 DISALLOW_COPY_AND_ASSIGN(HArraySet);
1774};
1775
1776class HArrayLength : public HExpression<1> {
1777 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001778 explicit HArrayLength(HInstruction* array)
1779 : HExpression(Primitive::kPrimInt, SideEffects::None()) {
1780 // Note that arrays do not change length, so the instruction does not
1781 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001782 SetRawInputAt(0, array);
1783 }
1784
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001785 virtual bool CanBeMoved() const { return true; }
1786 virtual bool InstructionDataEquals(HInstruction* other) const { return true; }
1787
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001788 DECLARE_INSTRUCTION(ArrayLength);
1789
1790 private:
1791 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
1792};
1793
1794class HBoundsCheck : public HExpression<2> {
1795 public:
1796 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001797 : HExpression(index->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001798 DCHECK(index->GetType() == Primitive::kPrimInt);
1799 SetRawInputAt(0, index);
1800 SetRawInputAt(1, length);
1801 }
1802
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001803 virtual bool CanBeMoved() const { return true; }
1804 virtual bool InstructionDataEquals(HInstruction* other) const { return true; }
1805
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001806 virtual bool NeedsEnvironment() const { return true; }
1807
Roland Levillaine161a2a2014-10-03 12:45:18 +01001808 virtual bool CanThrow() const { return true; }
1809
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001810 uint32_t GetDexPc() const { return dex_pc_; }
1811
1812 DECLARE_INSTRUCTION(BoundsCheck);
1813
1814 private:
1815 const uint32_t dex_pc_;
1816
1817 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
1818};
1819
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001820/**
1821 * Some DEX instructions are folded into multiple HInstructions that need
1822 * to stay live until the last HInstruction. This class
1823 * is used as a marker for the baseline compiler to ensure its preceding
1824 * HInstruction stays live. `index` is the temporary number that is used
1825 * for knowing the stack offset where to store the instruction.
1826 */
1827class HTemporary : public HTemplateInstruction<0> {
1828 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001829 explicit HTemporary(size_t index) : HTemplateInstruction(SideEffects::None()), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001830
1831 size_t GetIndex() const { return index_; }
1832
1833 DECLARE_INSTRUCTION(Temporary);
1834
1835 private:
1836 const size_t index_;
1837
1838 DISALLOW_COPY_AND_ASSIGN(HTemporary);
1839};
1840
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001841class HSuspendCheck : public HTemplateInstruction<0> {
1842 public:
1843 explicit HSuspendCheck(uint32_t dex_pc)
Nicolas Geoffray9ebc72c2014-09-25 16:33:42 +01001844 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001845
1846 virtual bool NeedsEnvironment() const {
1847 return true;
1848 }
1849
1850 uint32_t GetDexPc() const { return dex_pc_; }
1851
1852 DECLARE_INSTRUCTION(SuspendCheck);
1853
1854 private:
1855 const uint32_t dex_pc_;
1856
1857 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
1858};
1859
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001860class MoveOperands : public ArenaObject {
1861 public:
Nicolas Geoffray740475d2014-09-29 10:33:25 +01001862 MoveOperands(Location source, Location destination, HInstruction* instruction)
1863 : source_(source), destination_(destination), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001864
1865 Location GetSource() const { return source_; }
1866 Location GetDestination() const { return destination_; }
1867
1868 void SetSource(Location value) { source_ = value; }
1869 void SetDestination(Location value) { destination_ = value; }
1870
1871 // The parallel move resolver marks moves as "in-progress" by clearing the
1872 // destination (but not the source).
1873 Location MarkPending() {
1874 DCHECK(!IsPending());
1875 Location dest = destination_;
1876 destination_ = Location::NoLocation();
1877 return dest;
1878 }
1879
1880 void ClearPending(Location dest) {
1881 DCHECK(IsPending());
1882 destination_ = dest;
1883 }
1884
1885 bool IsPending() const {
1886 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
1887 return destination_.IsInvalid() && !source_.IsInvalid();
1888 }
1889
1890 // True if this blocks a move from the given location.
1891 bool Blocks(Location loc) const {
1892 return !IsEliminated() && source_.Equals(loc);
1893 }
1894
1895 // A move is redundant if it's been eliminated, if its source and
1896 // destination are the same, or if its destination is unneeded.
1897 bool IsRedundant() const {
1898 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
1899 }
1900
1901 // We clear both operands to indicate move that's been eliminated.
1902 void Eliminate() {
1903 source_ = destination_ = Location::NoLocation();
1904 }
1905
1906 bool IsEliminated() const {
1907 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
1908 return source_.IsInvalid();
1909 }
1910
Nicolas Geoffray740475d2014-09-29 10:33:25 +01001911 HInstruction* GetInstruction() const { return instruction_; }
1912
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001913 private:
1914 Location source_;
1915 Location destination_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01001916 // The instruction this move is assocatied with. Null when this move is
1917 // for moving an input in the expected locations of user (including a phi user).
1918 // This is only used in debug mode, to ensure we do not connect interval siblings
1919 // in the same parallel move.
1920 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001921
1922 DISALLOW_COPY_AND_ASSIGN(MoveOperands);
1923};
1924
1925static constexpr size_t kDefaultNumberOfMoves = 4;
1926
1927class HParallelMove : public HTemplateInstruction<0> {
1928 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001929 explicit HParallelMove(ArenaAllocator* arena)
1930 : HTemplateInstruction(SideEffects::None()), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001931
1932 void AddMove(MoveOperands* move) {
Nicolas Geoffray740475d2014-09-29 10:33:25 +01001933 if (kIsDebugBuild && move->GetInstruction() != nullptr) {
1934 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
1935 DCHECK_NE(moves_.Get(i)->GetInstruction(), move->GetInstruction())
1936 << "Doing parallel moves for the same instruction.";
1937 }
1938 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001939 moves_.Add(move);
1940 }
1941
1942 MoveOperands* MoveOperandsAt(size_t index) const {
1943 return moves_.Get(index);
1944 }
1945
1946 size_t NumMoves() const { return moves_.Size(); }
1947
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001948 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001949
1950 private:
1951 GrowableArray<MoveOperands*> moves_;
1952
1953 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
1954};
1955
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001956class HGraphVisitor : public ValueObject {
1957 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001958 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
1959 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001960
Dave Allison20dfc792014-06-16 20:44:29 -07001961 virtual void VisitInstruction(HInstruction* instruction) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001962 virtual void VisitBasicBlock(HBasicBlock* block);
1963
1964 void VisitInsertionOrder();
1965
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001966 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001967
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001968 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001969#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001970 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
1971
1972 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
1973
1974#undef DECLARE_VISIT_INSTRUCTION
1975
1976 private:
1977 HGraph* graph_;
1978
1979 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
1980};
1981
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001982class HGraphDelegateVisitor : public HGraphVisitor {
1983 public:
1984 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
1985 virtual ~HGraphDelegateVisitor() {}
1986
1987 // Visit functions that delegate to to super class.
1988#define DECLARE_VISIT_INSTRUCTION(name, super) \
1989 virtual void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
1990
1991 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
1992
1993#undef DECLARE_VISIT_INSTRUCTION
1994
1995 private:
1996 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
1997};
1998
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001999class HInsertionOrderIterator : public ValueObject {
2000 public:
2001 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
2002
2003 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
2004 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
2005 void Advance() { ++index_; }
2006
2007 private:
2008 const HGraph& graph_;
2009 size_t index_;
2010
2011 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
2012};
2013
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002014class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002015 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002016 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002017
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002018 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
2019 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002020 void Advance() { ++index_; }
2021
2022 private:
2023 const HGraph& graph_;
2024 size_t index_;
2025
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002026 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002027};
2028
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002029class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002030 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002031 explicit HPostOrderIterator(const HGraph& graph)
2032 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002033
2034 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002035 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002036 void Advance() { --index_; }
2037
2038 private:
2039 const HGraph& graph_;
2040 size_t index_;
2041
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002042 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002043};
2044
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002045} // namespace art
2046
2047#endif // ART_COMPILER_OPTIMIZING_NODES_H_