blob: 3f29e53d655f2258001b201c60cc1d94db80234a [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) \
Calin Juravle34bacdf2014-10-07 20:23:36 +0100505 M(Mul, BinaryOperation) \
Roland Levillain88cb1752014-10-20 16:36:47 +0100506 M(Neg, UnaryOperation)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000507
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100508#define FOR_EACH_INSTRUCTION(M) \
509 FOR_EACH_CONCRETE_INSTRUCTION(M) \
510 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +0100511 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +0100512 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100513 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -0700514
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100515#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000516FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
517#undef FORWARD_DECLARATION
518
Roland Levillainccc07a92014-09-16 14:48:16 +0100519#define DECLARE_INSTRUCTION(type) \
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100520 virtual InstructionKind GetKind() const { return k##type; } \
Roland Levillainccc07a92014-09-16 14:48:16 +0100521 virtual const char* DebugName() const { return #type; } \
522 virtual const H##type* As##type() const OVERRIDE { return this; } \
523 virtual H##type* As##type() OVERRIDE { return this; } \
524 virtual bool InstructionTypeEquals(HInstruction* other) const { \
525 return other->Is##type(); \
526 } \
527 virtual void Accept(HGraphVisitor* visitor)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000528
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100529template <typename T>
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000530class HUseListNode : public ArenaObject {
531 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100532 HUseListNode(T* user, size_t index, HUseListNode* tail)
Dave Allison20dfc792014-06-16 20:44:29 -0700533 : user_(user), index_(index), tail_(tail) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000534
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000535 HUseListNode* GetTail() const { return tail_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100536 T* GetUser() const { return user_; }
537 size_t GetIndex() const { return index_; }
538
539 void SetTail(HUseListNode<T>* node) { tail_ = node; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000540
541 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100542 T* const user_;
543 const size_t index_;
544 HUseListNode<T>* tail_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000545
546 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
547};
548
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100549// Represents the side effects an instruction may have.
550class SideEffects : public ValueObject {
551 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100552 SideEffects() : flags_(0) {}
553
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100554 static SideEffects None() {
555 return SideEffects(0);
556 }
557
558 static SideEffects All() {
559 return SideEffects(ChangesSomething().flags_ | DependsOnSomething().flags_);
560 }
561
562 static SideEffects ChangesSomething() {
563 return SideEffects((1 << kFlagChangesCount) - 1);
564 }
565
566 static SideEffects DependsOnSomething() {
567 int count = kFlagDependsOnCount - kFlagChangesCount;
568 return SideEffects(((1 << count) - 1) << kFlagChangesCount);
569 }
570
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100571 SideEffects Union(SideEffects other) const {
572 return SideEffects(flags_ | other.flags_);
573 }
574
Roland Levillain72bceff2014-09-15 18:29:00 +0100575 bool HasSideEffects() const {
576 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
577 return (flags_ & all_bits_set) != 0;
578 }
579
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100580 bool HasAllSideEffects() const {
581 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
582 return all_bits_set == (flags_ & all_bits_set);
583 }
584
585 bool DependsOn(SideEffects other) const {
586 size_t depends_flags = other.ComputeDependsFlags();
587 return (flags_ & depends_flags) != 0;
588 }
589
590 bool HasDependencies() const {
591 int count = kFlagDependsOnCount - kFlagChangesCount;
592 size_t all_bits_set = (1 << count) - 1;
593 return ((flags_ >> kFlagChangesCount) & all_bits_set) != 0;
594 }
595
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100596 private:
597 static constexpr int kFlagChangesSomething = 0;
598 static constexpr int kFlagChangesCount = kFlagChangesSomething + 1;
599
600 static constexpr int kFlagDependsOnSomething = kFlagChangesCount;
601 static constexpr int kFlagDependsOnCount = kFlagDependsOnSomething + 1;
602
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100603 explicit SideEffects(size_t flags) : flags_(flags) {}
604
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100605 size_t ComputeDependsFlags() const {
606 return flags_ << kFlagChangesCount;
607 }
608
609 size_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100610};
611
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000612class HInstruction : public ArenaObject {
613 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100614 explicit HInstruction(SideEffects side_effects)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000615 : previous_(nullptr),
616 next_(nullptr),
617 block_(nullptr),
618 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100619 ssa_index_(-1),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000620 uses_(nullptr),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100621 env_uses_(nullptr),
622 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100623 locations_(nullptr),
624 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100625 lifetime_position_(kNoLifetime),
626 side_effects_(side_effects) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000627
Dave Allison20dfc792014-06-16 20:44:29 -0700628 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000629
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100630#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100631 enum InstructionKind {
632 FOR_EACH_INSTRUCTION(DECLARE_KIND)
633 };
634#undef DECLARE_KIND
635
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000636 HInstruction* GetNext() const { return next_; }
637 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000638
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000639 HBasicBlock* GetBlock() const { return block_; }
640 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100641 bool IsInBlock() const { return block_ != nullptr; }
642 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +0100643 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000644
Roland Levillain6b879dd2014-09-22 17:13:44 +0100645 virtual size_t InputCount() const = 0;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100646 virtual HInstruction* InputAt(size_t i) const = 0;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000647
648 virtual void Accept(HGraphVisitor* visitor) = 0;
649 virtual const char* DebugName() const = 0;
650
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100651 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100652 virtual void SetRawInputAt(size_t index, HInstruction* input) = 0;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100653
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100654 virtual bool NeedsEnvironment() const { return false; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100655 virtual bool IsControlFlow() const { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +0100656 virtual bool CanThrow() const { return false; }
Roland Levillain72bceff2014-09-15 18:29:00 +0100657 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100658
659 void AddUseAt(HInstruction* user, size_t index) {
660 uses_ = new (block_->GetGraph()->GetArena()) HUseListNode<HInstruction>(user, index, uses_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000661 }
662
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100663 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +0100664 DCHECK(user != nullptr);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100665 env_uses_ = new (block_->GetGraph()->GetArena()) HUseListNode<HEnvironment>(
666 user, index, env_uses_);
667 }
668
669 void RemoveUser(HInstruction* user, size_t index);
Nicolas Geoffray724c9632014-09-22 12:27:27 +0100670 void RemoveEnvironmentUser(HEnvironment* user, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100671
672 HUseListNode<HInstruction>* GetUses() const { return uses_; }
673 HUseListNode<HEnvironment>* GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000674
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100675 bool HasUses() const { return uses_ != nullptr || env_uses_ != nullptr; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100676 bool HasEnvironmentUses() const { return env_uses_ != nullptr; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000677
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100678 size_t NumberOfUses() const {
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100679 // TODO: Optimize this method if it is used outside of the HGraphVisualizer.
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100680 size_t result = 0;
681 HUseListNode<HInstruction>* current = uses_;
682 while (current != nullptr) {
683 current = current->GetTail();
684 ++result;
685 }
686 return result;
687 }
688
Roland Levillain6c82d402014-10-13 16:10:27 +0100689 // Does this instruction strictly dominate `other_instruction`?
690 // Returns false if this instruction and `other_instruction` are the same.
691 // Aborts if this instruction and `other_instruction` are both phis.
692 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +0100693
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000694 int GetId() const { return id_; }
695 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000696
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100697 int GetSsaIndex() const { return ssa_index_; }
698 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
699 bool HasSsaIndex() const { return ssa_index_ != -1; }
700
701 bool HasEnvironment() const { return environment_ != nullptr; }
702 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100703 void SetEnvironment(HEnvironment* environment) { environment_ = environment; }
704
Nicolas Geoffray39468442014-09-02 15:17:15 +0100705 // Returns the number of entries in the environment. Typically, that is the
706 // number of dex registers in a method. It could be more in case of inlining.
707 size_t EnvironmentSize() const;
708
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000709 LocationSummary* GetLocations() const { return locations_; }
710 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000711
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100712 void ReplaceWith(HInstruction* instruction);
713
Dave Allison20dfc792014-06-16 20:44:29 -0700714 bool HasOnlyOneUse() const {
715 return uses_ != nullptr && uses_->GetTail() == nullptr;
716 }
717
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100718#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +0100719 bool Is##type() const { return (As##type() != nullptr); } \
720 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000721 virtual H##type* As##type() { return nullptr; }
722
723 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
724#undef INSTRUCTION_TYPE_CHECK
725
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100726 // Returns whether the instruction can be moved within the graph.
727 virtual bool CanBeMoved() const { return false; }
728
729 // Returns whether the two instructions are of the same kind.
730 virtual bool InstructionTypeEquals(HInstruction* other) const { return false; }
731
732 // Returns whether any data encoded in the two instructions is equal.
733 // This method does not look at the inputs. Both instructions must be
734 // of the same type, otherwise the method has undefined behavior.
735 virtual bool InstructionDataEquals(HInstruction* other) const { return false; }
736
737 // Returns whether two instructions are equal, that is:
738 // 1) They have the same type and contain the same data,
739 // 2) Their inputs are identical.
740 bool Equals(HInstruction* other) const;
741
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100742 virtual InstructionKind GetKind() const = 0;
743
744 virtual size_t ComputeHashCode() const {
745 size_t result = GetKind();
746 for (size_t i = 0, e = InputCount(); i < e; ++i) {
747 result = (result * 31) + InputAt(i)->GetId();
748 }
749 return result;
750 }
751
752 SideEffects GetSideEffects() const { return side_effects_; }
753
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100754 size_t GetLifetimePosition() const { return lifetime_position_; }
755 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
756 LiveInterval* GetLiveInterval() const { return live_interval_; }
757 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
758 bool HasLiveInterval() const { return live_interval_ != nullptr; }
759
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000760 private:
761 HInstruction* previous_;
762 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000763 HBasicBlock* block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000764
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000765 // An instruction gets an id when it is added to the graph.
766 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +0100767 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000768 int id_;
769
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100770 // When doing liveness analysis, instructions that have uses get an SSA index.
771 int ssa_index_;
772
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100773 // List of instructions that have this instruction as input.
774 HUseListNode<HInstruction>* uses_;
775
776 // List of environments that contain this instruction.
777 HUseListNode<HEnvironment>* env_uses_;
778
Nicolas Geoffray39468442014-09-02 15:17:15 +0100779 // The environment associated with this instruction. Not null if the instruction
780 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100781 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000782
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000783 // Set by the code generator.
784 LocationSummary* locations_;
785
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100786 // Set by the liveness analysis.
787 LiveInterval* live_interval_;
788
789 // Set by the liveness analysis, this is the position in a linear
790 // order of blocks where this instruction's live interval start.
791 size_t lifetime_position_;
792
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100793 const SideEffects side_effects_;
794
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000795 friend class HBasicBlock;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100796 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000797
798 DISALLOW_COPY_AND_ASSIGN(HInstruction);
799};
800
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100801template<typename T>
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000802class HUseIterator : public ValueObject {
803 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100804 explicit HUseIterator(HUseListNode<T>* uses) : current_(uses) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000805
806 bool Done() const { return current_ == nullptr; }
807
808 void Advance() {
809 DCHECK(!Done());
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000810 current_ = current_->GetTail();
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000811 }
812
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100813 HUseListNode<T>* Current() const {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000814 DCHECK(!Done());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100815 return current_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000816 }
817
818 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100819 HUseListNode<T>* current_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000820
821 friend class HValue;
822};
823
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100824// A HEnvironment object contains the values of virtual registers at a given location.
825class HEnvironment : public ArenaObject {
826 public:
827 HEnvironment(ArenaAllocator* arena, size_t number_of_vregs) : vregs_(arena, number_of_vregs) {
828 vregs_.SetSize(number_of_vregs);
829 for (size_t i = 0; i < number_of_vregs; i++) {
830 vregs_.Put(i, nullptr);
831 }
832 }
833
834 void Populate(const GrowableArray<HInstruction*>& env) {
835 for (size_t i = 0; i < env.Size(); i++) {
836 HInstruction* instruction = env.Get(i);
837 vregs_.Put(i, instruction);
838 if (instruction != nullptr) {
839 instruction->AddEnvUseAt(this, i);
840 }
841 }
842 }
843
844 void SetRawEnvAt(size_t index, HInstruction* instruction) {
845 vregs_.Put(index, instruction);
846 }
847
Nicolas Geoffray39468442014-09-02 15:17:15 +0100848 HInstruction* GetInstructionAt(size_t index) const {
849 return vregs_.Get(index);
850 }
851
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100852 GrowableArray<HInstruction*>* GetVRegs() {
853 return &vregs_;
854 }
855
Nicolas Geoffray39468442014-09-02 15:17:15 +0100856 size_t Size() const { return vregs_.Size(); }
857
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100858 private:
859 GrowableArray<HInstruction*> vregs_;
860
861 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
862};
863
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000864class HInputIterator : public ValueObject {
865 public:
Dave Allison20dfc792014-06-16 20:44:29 -0700866 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000867
868 bool Done() const { return index_ == instruction_->InputCount(); }
869 HInstruction* Current() const { return instruction_->InputAt(index_); }
870 void Advance() { index_++; }
871
872 private:
873 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100874 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000875
876 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
877};
878
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000879class HInstructionIterator : public ValueObject {
880 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100881 explicit HInstructionIterator(const HInstructionList& instructions)
882 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000883 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000884 }
885
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000886 bool Done() const { return instruction_ == nullptr; }
887 HInstruction* Current() const { return instruction_; }
888 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000889 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000890 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000891 }
892
893 private:
894 HInstruction* instruction_;
895 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100896
897 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000898};
899
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100900class HBackwardInstructionIterator : public ValueObject {
901 public:
902 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
903 : instruction_(instructions.last_instruction_) {
904 next_ = Done() ? nullptr : instruction_->GetPrevious();
905 }
906
907 bool Done() const { return instruction_ == nullptr; }
908 HInstruction* Current() const { return instruction_; }
909 void Advance() {
910 instruction_ = next_;
911 next_ = Done() ? nullptr : instruction_->GetPrevious();
912 }
913
914 private:
915 HInstruction* instruction_;
916 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100917
918 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100919};
920
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000921// An embedded container with N elements of type T. Used (with partial
922// specialization for N=0) because embedded arrays cannot have size 0.
923template<typename T, intptr_t N>
924class EmbeddedArray {
925 public:
Dave Allison20dfc792014-06-16 20:44:29 -0700926 EmbeddedArray() : elements_() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000927
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000928 intptr_t GetLength() const { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000929
930 const T& operator[](intptr_t i) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000931 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000932 return elements_[i];
933 }
934
935 T& operator[](intptr_t i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000936 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000937 return elements_[i];
938 }
939
940 const T& At(intptr_t i) const {
941 return (*this)[i];
942 }
943
944 void SetAt(intptr_t i, const T& val) {
945 (*this)[i] = val;
946 }
947
948 private:
949 T elements_[N];
950};
951
952template<typename T>
953class EmbeddedArray<T, 0> {
954 public:
955 intptr_t length() const { return 0; }
956 const T& operator[](intptr_t i) const {
957 LOG(FATAL) << "Unreachable";
958 static T sentinel = 0;
959 return sentinel;
960 }
961 T& operator[](intptr_t i) {
962 LOG(FATAL) << "Unreachable";
963 static T sentinel = 0;
964 return sentinel;
965 }
966};
967
968template<intptr_t N>
969class HTemplateInstruction: public HInstruction {
970 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100971 HTemplateInstruction<N>(SideEffects side_effects)
972 : HInstruction(side_effects), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -0700973 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000974
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100975 virtual size_t InputCount() const { return N; }
976 virtual HInstruction* InputAt(size_t i) const { return inputs_[i]; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000977
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000978 protected:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100979 virtual void SetRawInputAt(size_t i, HInstruction* instruction) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000980 inputs_[i] = instruction;
981 }
982
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000983 private:
984 EmbeddedArray<HInstruction*, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100985
986 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000987};
988
Dave Allison20dfc792014-06-16 20:44:29 -0700989template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100990class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -0700991 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100992 HExpression<N>(Primitive::Type type, SideEffects side_effects)
993 : HTemplateInstruction<N>(side_effects), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -0700994 virtual ~HExpression() {}
995
996 virtual Primitive::Type GetType() const { return type_; }
997
998 private:
999 const Primitive::Type type_;
1000};
1001
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001002// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
1003// instruction that branches to the exit block.
1004class HReturnVoid : public HTemplateInstruction<0> {
1005 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001006 HReturnVoid() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001007
1008 virtual bool IsControlFlow() const { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001009
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001010 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001011
1012 private:
1013 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
1014};
1015
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001016// Represents dex's RETURN opcodes. A HReturn is a control flow
1017// instruction that branches to the exit block.
1018class HReturn : public HTemplateInstruction<1> {
1019 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001020 explicit HReturn(HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001021 SetRawInputAt(0, value);
1022 }
1023
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001024 virtual bool IsControlFlow() const { return true; }
1025
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001026 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001027
1028 private:
1029 DISALLOW_COPY_AND_ASSIGN(HReturn);
1030};
1031
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001032// The exit instruction is the only instruction of the exit block.
1033// Instructions aborting the method (HTrow and HReturn) must branch to the
1034// exit block.
1035class HExit : public HTemplateInstruction<0> {
1036 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001037 HExit() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001038
1039 virtual bool IsControlFlow() const { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001040
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001041 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001042
1043 private:
1044 DISALLOW_COPY_AND_ASSIGN(HExit);
1045};
1046
1047// Jumps from one block to another.
1048class HGoto : public HTemplateInstruction<0> {
1049 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001050 HGoto() : HTemplateInstruction(SideEffects::None()) {}
1051
1052 virtual bool IsControlFlow() const { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001053
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001054 HBasicBlock* GetSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001055 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001056 }
1057
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001058 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001059
1060 private:
1061 DISALLOW_COPY_AND_ASSIGN(HGoto);
1062};
1063
Dave Allison20dfc792014-06-16 20:44:29 -07001064
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001065// Conditional branch. A block ending with an HIf instruction must have
1066// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001067class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001068 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001069 explicit HIf(HInstruction* input) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001070 SetRawInputAt(0, input);
1071 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001072
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001073 virtual bool IsControlFlow() const { return true; }
1074
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001075 HBasicBlock* IfTrueSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001076 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001077 }
1078
1079 HBasicBlock* IfFalseSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001080 return GetBlock()->GetSuccessors().Get(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001081 }
1082
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001083 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001084
Dave Allison20dfc792014-06-16 20:44:29 -07001085 virtual bool IsIfInstruction() const { return true; }
1086
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001087 private:
1088 DISALLOW_COPY_AND_ASSIGN(HIf);
1089};
1090
Roland Levillain88cb1752014-10-20 16:36:47 +01001091class HUnaryOperation : public HExpression<1> {
1092 public:
1093 HUnaryOperation(Primitive::Type result_type, HInstruction* input)
1094 : HExpression(result_type, SideEffects::None()) {
1095 SetRawInputAt(0, input);
1096 }
1097
1098 HInstruction* GetInput() const { return InputAt(0); }
1099 Primitive::Type GetResultType() const { return GetType(); }
1100
1101 virtual bool CanBeMoved() const { return true; }
1102 virtual bool InstructionDataEquals(HInstruction* other) const { return true; }
1103
Roland Levillain9240d6a2014-10-20 16:47:04 +01001104 // Try to statically evaluate `operation` and return a HConstant
1105 // containing the result of this evaluation. If `operation` cannot
1106 // be evaluated as a constant, return nullptr.
1107 HConstant* TryStaticEvaluation() const;
1108
1109 // Apply this operation to `x`.
1110 virtual int32_t Evaluate(int32_t x) const = 0;
1111 virtual int64_t Evaluate(int64_t x) const = 0;
1112
Roland Levillain88cb1752014-10-20 16:36:47 +01001113 DECLARE_INSTRUCTION(UnaryOperation);
1114
1115 private:
1116 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
1117};
1118
Dave Allison20dfc792014-06-16 20:44:29 -07001119class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001120 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001121 HBinaryOperation(Primitive::Type result_type,
1122 HInstruction* left,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001123 HInstruction* right) : HExpression(result_type, SideEffects::None()) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001124 SetRawInputAt(0, left);
1125 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001126 }
1127
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001128 HInstruction* GetLeft() const { return InputAt(0); }
1129 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07001130 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001131
1132 virtual bool IsCommutative() { return false; }
1133
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001134 virtual bool CanBeMoved() const { return true; }
1135 virtual bool InstructionDataEquals(HInstruction* other) const { return true; }
1136
Roland Levillain9240d6a2014-10-20 16:47:04 +01001137 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01001138 // containing the result of this evaluation. If `operation` cannot
1139 // be evaluated as a constant, return nullptr.
Roland Levillain9240d6a2014-10-20 16:47:04 +01001140 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01001141
1142 // Apply this operation to `x` and `y`.
1143 virtual int32_t Evaluate(int32_t x, int32_t y) const = 0;
1144 virtual int64_t Evaluate(int64_t x, int64_t y) const = 0;
1145
Roland Levillainccc07a92014-09-16 14:48:16 +01001146 DECLARE_INSTRUCTION(BinaryOperation);
1147
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001148 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001149 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
1150};
1151
Dave Allison20dfc792014-06-16 20:44:29 -07001152class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001153 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001154 HCondition(HInstruction* first, HInstruction* second)
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001155 : HBinaryOperation(Primitive::kPrimBoolean, first, second),
1156 needs_materialization_(true) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001157
1158 virtual bool IsCommutative() { return true; }
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001159
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001160 bool NeedsMaterialization() const { return needs_materialization_; }
1161 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001162
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001163 // For code generation purposes, returns whether this instruction is just before
1164 // `if_`, and disregard moves in between.
1165 bool IsBeforeWhenDisregardMoves(HIf* if_) const;
1166
Dave Allison20dfc792014-06-16 20:44:29 -07001167 DECLARE_INSTRUCTION(Condition);
1168
1169 virtual IfCondition GetCondition() const = 0;
1170
1171 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001172 // For register allocation purposes, returns whether this instruction needs to be
1173 // materialized (that is, not just be in the processor flags).
1174 bool needs_materialization_;
1175
Dave Allison20dfc792014-06-16 20:44:29 -07001176 DISALLOW_COPY_AND_ASSIGN(HCondition);
1177};
1178
1179// Instruction to check if two inputs are equal to each other.
1180class HEqual : public HCondition {
1181 public:
1182 HEqual(HInstruction* first, HInstruction* second)
1183 : HCondition(first, second) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001184
Roland Levillain93445682014-10-06 19:24:02 +01001185 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1186 return x == y ? 1 : 0;
1187 }
1188 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1189 return x == y ? 1 : 0;
1190 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001191
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001192 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001193
Dave Allison20dfc792014-06-16 20:44:29 -07001194 virtual IfCondition GetCondition() const {
1195 return kCondEQ;
1196 }
1197
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001198 private:
1199 DISALLOW_COPY_AND_ASSIGN(HEqual);
1200};
1201
Dave Allison20dfc792014-06-16 20:44:29 -07001202class HNotEqual : public HCondition {
1203 public:
1204 HNotEqual(HInstruction* first, HInstruction* second)
1205 : HCondition(first, second) {}
1206
Roland Levillain93445682014-10-06 19:24:02 +01001207 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1208 return x != y ? 1 : 0;
1209 }
1210 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1211 return x != y ? 1 : 0;
1212 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001213
Dave Allison20dfc792014-06-16 20:44:29 -07001214 DECLARE_INSTRUCTION(NotEqual);
1215
1216 virtual IfCondition GetCondition() const {
1217 return kCondNE;
1218 }
1219
1220 private:
1221 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
1222};
1223
1224class HLessThan : public HCondition {
1225 public:
1226 HLessThan(HInstruction* first, HInstruction* second)
1227 : HCondition(first, second) {}
1228
Roland Levillain93445682014-10-06 19:24:02 +01001229 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1230 return x < y ? 1 : 0;
1231 }
1232 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1233 return x < y ? 1 : 0;
1234 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001235
Dave Allison20dfc792014-06-16 20:44:29 -07001236 DECLARE_INSTRUCTION(LessThan);
1237
1238 virtual IfCondition GetCondition() const {
1239 return kCondLT;
1240 }
1241
1242 private:
1243 DISALLOW_COPY_AND_ASSIGN(HLessThan);
1244};
1245
1246class HLessThanOrEqual : public HCondition {
1247 public:
1248 HLessThanOrEqual(HInstruction* first, HInstruction* second)
1249 : HCondition(first, second) {}
1250
Roland Levillain93445682014-10-06 19:24:02 +01001251 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1252 return x <= y ? 1 : 0;
1253 }
1254 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1255 return x <= y ? 1 : 0;
1256 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001257
Dave Allison20dfc792014-06-16 20:44:29 -07001258 DECLARE_INSTRUCTION(LessThanOrEqual);
1259
1260 virtual IfCondition GetCondition() const {
1261 return kCondLE;
1262 }
1263
1264 private:
1265 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
1266};
1267
1268class HGreaterThan : public HCondition {
1269 public:
1270 HGreaterThan(HInstruction* first, HInstruction* second)
1271 : HCondition(first, second) {}
1272
Roland Levillain93445682014-10-06 19:24:02 +01001273 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1274 return x > y ? 1 : 0;
1275 }
1276 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1277 return x > y ? 1 : 0;
1278 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001279
Dave Allison20dfc792014-06-16 20:44:29 -07001280 DECLARE_INSTRUCTION(GreaterThan);
1281
1282 virtual IfCondition GetCondition() const {
1283 return kCondGT;
1284 }
1285
1286 private:
1287 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
1288};
1289
1290class HGreaterThanOrEqual : public HCondition {
1291 public:
1292 HGreaterThanOrEqual(HInstruction* first, HInstruction* second)
1293 : HCondition(first, second) {}
1294
Roland Levillain93445682014-10-06 19:24:02 +01001295 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1296 return x >= y ? 1 : 0;
1297 }
1298 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1299 return x >= y ? 1 : 0;
1300 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001301
Dave Allison20dfc792014-06-16 20:44:29 -07001302 DECLARE_INSTRUCTION(GreaterThanOrEqual);
1303
1304 virtual IfCondition GetCondition() const {
1305 return kCondGE;
1306 }
1307
1308 private:
1309 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
1310};
1311
1312
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001313// Instruction to check how two inputs compare to each other.
1314// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
1315class HCompare : public HBinaryOperation {
1316 public:
1317 HCompare(Primitive::Type type, HInstruction* first, HInstruction* second)
1318 : HBinaryOperation(Primitive::kPrimInt, first, second) {
1319 DCHECK_EQ(type, first->GetType());
1320 DCHECK_EQ(type, second->GetType());
1321 }
1322
Roland Levillain93445682014-10-06 19:24:02 +01001323 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001324 return
1325 x == y ? 0 :
1326 x > y ? 1 :
1327 -1;
1328 }
Roland Levillain93445682014-10-06 19:24:02 +01001329 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001330 return
1331 x == y ? 0 :
1332 x > y ? 1 :
1333 -1;
1334 }
1335
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001336 DECLARE_INSTRUCTION(Compare);
1337
1338 private:
1339 DISALLOW_COPY_AND_ASSIGN(HCompare);
1340};
1341
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001342// A local in the graph. Corresponds to a Dex register.
1343class HLocal : public HTemplateInstruction<0> {
1344 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001345 explicit HLocal(uint16_t reg_number)
1346 : HTemplateInstruction(SideEffects::None()), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001347
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001348 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001349
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001350 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001351
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001352 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001353 // The Dex register number.
1354 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001355
1356 DISALLOW_COPY_AND_ASSIGN(HLocal);
1357};
1358
1359// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07001360class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001361 public:
Roland Levillain5799fc02014-09-25 12:15:20 +01001362 HLoadLocal(HLocal* local, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001363 : HExpression(type, SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001364 SetRawInputAt(0, local);
1365 }
1366
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001367 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1368
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001369 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001370
1371 private:
1372 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
1373};
1374
1375// Store a value in a given local. This instruction has two inputs: the value
1376// and the local.
1377class HStoreLocal : public HTemplateInstruction<2> {
1378 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001379 HStoreLocal(HLocal* local, HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001380 SetRawInputAt(0, local);
1381 SetRawInputAt(1, value);
1382 }
1383
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001384 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1385
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001386 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001387
1388 private:
1389 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
1390};
1391
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001392class HConstant : public HExpression<0> {
1393 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001394 explicit HConstant(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
1395
1396 virtual bool CanBeMoved() const { return true; }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001397
1398 DECLARE_INSTRUCTION(Constant);
1399
1400 private:
1401 DISALLOW_COPY_AND_ASSIGN(HConstant);
1402};
1403
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001404// Constants of the type int. Those can be from Dex instructions, or
1405// synthesized (for example with the if-eqz instruction).
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001406class HIntConstant : public HConstant {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001407 public:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001408 explicit HIntConstant(int32_t value) : HConstant(Primitive::kPrimInt), value_(value) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001409
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001410 int32_t GetValue() const { return value_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001411
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001412 virtual bool InstructionDataEquals(HInstruction* other) const {
1413 return other->AsIntConstant()->value_ == value_;
1414 }
1415
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001416 virtual size_t ComputeHashCode() const { return GetValue(); }
1417
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001418 DECLARE_INSTRUCTION(IntConstant);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001419
1420 private:
1421 const int32_t value_;
1422
1423 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
1424};
1425
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001426class HLongConstant : public HConstant {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001427 public:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001428 explicit HLongConstant(int64_t value) : HConstant(Primitive::kPrimLong), value_(value) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001429
1430 int64_t GetValue() const { return value_; }
1431
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001432 virtual bool InstructionDataEquals(HInstruction* other) const {
1433 return other->AsLongConstant()->value_ == value_;
1434 }
1435
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001436 virtual size_t ComputeHashCode() const { return static_cast<size_t>(GetValue()); }
1437
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001438 DECLARE_INSTRUCTION(LongConstant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001439
1440 private:
1441 const int64_t value_;
1442
1443 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
1444};
1445
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001446class HInvoke : public HInstruction {
1447 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001448 HInvoke(ArenaAllocator* arena,
1449 uint32_t number_of_arguments,
1450 Primitive::Type return_type,
1451 uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001452 : HInstruction(SideEffects::All()),
1453 inputs_(arena, number_of_arguments),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001454 return_type_(return_type),
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001455 dex_pc_(dex_pc) {
1456 inputs_.SetSize(number_of_arguments);
1457 }
1458
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001459 virtual size_t InputCount() const { return inputs_.Size(); }
1460 virtual HInstruction* InputAt(size_t i) const { return inputs_.Get(i); }
1461
1462 // Runtime needs to walk the stack, so Dex -> Dex calls need to
1463 // know their environment.
1464 virtual bool NeedsEnvironment() const { return true; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001465
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001466 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001467 SetRawInputAt(index, argument);
1468 }
1469
1470 virtual void SetRawInputAt(size_t index, HInstruction* input) {
1471 inputs_.Put(index, input);
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001472 }
1473
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001474 virtual Primitive::Type GetType() const { return return_type_; }
1475
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001476 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001477
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001478 DECLARE_INSTRUCTION(Invoke);
1479
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001480 protected:
1481 GrowableArray<HInstruction*> inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001482 const Primitive::Type return_type_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001483 const uint32_t dex_pc_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001484
1485 private:
1486 DISALLOW_COPY_AND_ASSIGN(HInvoke);
1487};
1488
1489class HInvokeStatic : public HInvoke {
1490 public:
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001491 HInvokeStatic(ArenaAllocator* arena,
1492 uint32_t number_of_arguments,
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001493 Primitive::Type return_type,
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001494 uint32_t dex_pc,
1495 uint32_t index_in_dex_cache)
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001496 : HInvoke(arena, number_of_arguments, return_type, dex_pc),
1497 index_in_dex_cache_(index_in_dex_cache) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001498
1499 uint32_t GetIndexInDexCache() const { return index_in_dex_cache_; }
1500
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001501 DECLARE_INSTRUCTION(InvokeStatic);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001502
1503 private:
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001504 const uint32_t index_in_dex_cache_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001505
1506 DISALLOW_COPY_AND_ASSIGN(HInvokeStatic);
1507};
1508
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001509class HInvokeVirtual : public HInvoke {
1510 public:
1511 HInvokeVirtual(ArenaAllocator* arena,
1512 uint32_t number_of_arguments,
1513 Primitive::Type return_type,
1514 uint32_t dex_pc,
1515 uint32_t vtable_index)
1516 : HInvoke(arena, number_of_arguments, return_type, dex_pc),
1517 vtable_index_(vtable_index) {}
1518
1519 uint32_t GetVTableIndex() const { return vtable_index_; }
1520
1521 DECLARE_INSTRUCTION(InvokeVirtual);
1522
1523 private:
1524 const uint32_t vtable_index_;
1525
1526 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
1527};
1528
Dave Allison20dfc792014-06-16 20:44:29 -07001529class HNewInstance : public HExpression<0> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001530 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001531 HNewInstance(uint32_t dex_pc, uint16_t type_index)
1532 : HExpression(Primitive::kPrimNot, SideEffects::None()),
1533 dex_pc_(dex_pc),
1534 type_index_(type_index) {}
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001535
1536 uint32_t GetDexPc() const { return dex_pc_; }
1537 uint16_t GetTypeIndex() const { return type_index_; }
1538
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001539 // Calls runtime so needs an environment.
1540 virtual bool NeedsEnvironment() const { return true; }
1541
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001542 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001543
1544 private:
1545 const uint32_t dex_pc_;
1546 const uint16_t type_index_;
1547
1548 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
1549};
1550
Roland Levillain88cb1752014-10-20 16:36:47 +01001551class HNeg : public HUnaryOperation {
1552 public:
1553 explicit HNeg(Primitive::Type result_type, HInstruction* input)
1554 : HUnaryOperation(result_type, input) {}
1555
Roland Levillain9240d6a2014-10-20 16:47:04 +01001556 virtual int32_t Evaluate(int32_t x) const OVERRIDE { return -x; }
1557 virtual int64_t Evaluate(int64_t x) const OVERRIDE { return -x; }
1558
Roland Levillain88cb1752014-10-20 16:36:47 +01001559 DECLARE_INSTRUCTION(Neg);
1560
1561 private:
1562 DISALLOW_COPY_AND_ASSIGN(HNeg);
1563};
1564
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001565class HAdd : public HBinaryOperation {
1566 public:
1567 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1568 : HBinaryOperation(result_type, left, right) {}
1569
1570 virtual bool IsCommutative() { return true; }
1571
Roland Levillain93445682014-10-06 19:24:02 +01001572 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1573 return x + y;
1574 }
1575 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1576 return x + y;
1577 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001578
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001579 DECLARE_INSTRUCTION(Add);
1580
1581 private:
1582 DISALLOW_COPY_AND_ASSIGN(HAdd);
1583};
1584
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001585class HSub : public HBinaryOperation {
1586 public:
1587 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1588 : HBinaryOperation(result_type, left, right) {}
1589
1590 virtual bool IsCommutative() { return false; }
1591
Roland Levillain93445682014-10-06 19:24:02 +01001592 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1593 return x - y;
1594 }
1595 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1596 return x - y;
1597 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001598
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001599 DECLARE_INSTRUCTION(Sub);
1600
1601 private:
1602 DISALLOW_COPY_AND_ASSIGN(HSub);
1603};
1604
Calin Juravle34bacdf2014-10-07 20:23:36 +01001605class HMul : public HBinaryOperation {
1606 public:
1607 HMul(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1608 : HBinaryOperation(result_type, left, right) {}
1609
1610 virtual bool IsCommutative() { return true; }
1611
1612 virtual int32_t Evaluate(int32_t x, int32_t y) const { return x * y; }
1613 virtual int64_t Evaluate(int64_t x, int64_t y) const { return x * y; }
1614
1615 DECLARE_INSTRUCTION(Mul);
1616
1617 private:
1618 DISALLOW_COPY_AND_ASSIGN(HMul);
1619};
1620
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001621// The value of a parameter in this method. Its location depends on
1622// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07001623class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001624 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001625 HParameterValue(uint8_t index, Primitive::Type parameter_type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001626 : HExpression(parameter_type, SideEffects::None()), index_(index) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001627
1628 uint8_t GetIndex() const { return index_; }
1629
1630 DECLARE_INSTRUCTION(ParameterValue);
1631
1632 private:
1633 // The index of this parameter in the parameters list. Must be less
1634 // than HGraph::number_of_in_vregs_;
1635 const uint8_t index_;
1636
1637 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
1638};
1639
Dave Allison20dfc792014-06-16 20:44:29 -07001640class HNot : public HExpression<1> {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001641 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001642 explicit HNot(HInstruction* input) : HExpression(Primitive::kPrimBoolean, SideEffects::None()) {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001643 SetRawInputAt(0, input);
1644 }
1645
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001646 virtual bool CanBeMoved() const { return true; }
1647 virtual bool InstructionDataEquals(HInstruction* other) const { return true; }
1648
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001649 DECLARE_INSTRUCTION(Not);
1650
1651 private:
1652 DISALLOW_COPY_AND_ASSIGN(HNot);
1653};
1654
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001655class HPhi : public HInstruction {
1656 public:
1657 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001658 : HInstruction(SideEffects::None()),
1659 inputs_(arena, number_of_inputs),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001660 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001661 type_(type),
1662 is_live_(false) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001663 inputs_.SetSize(number_of_inputs);
1664 }
1665
1666 virtual size_t InputCount() const { return inputs_.Size(); }
1667 virtual HInstruction* InputAt(size_t i) const { return inputs_.Get(i); }
1668
1669 virtual void SetRawInputAt(size_t index, HInstruction* input) {
1670 inputs_.Put(index, input);
1671 }
1672
1673 void AddInput(HInstruction* input);
1674
1675 virtual Primitive::Type GetType() const { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001676 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001677
1678 uint32_t GetRegNumber() const { return reg_number_; }
1679
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001680 void SetDead() { is_live_ = false; }
1681 void SetLive() { is_live_ = true; }
1682 bool IsDead() const { return !is_live_; }
1683 bool IsLive() const { return is_live_; }
1684
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001685 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001686
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001687 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001688 GrowableArray<HInstruction*> inputs_;
1689 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001690 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001691 bool is_live_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001692
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001693 DISALLOW_COPY_AND_ASSIGN(HPhi);
1694};
1695
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001696class HNullCheck : public HExpression<1> {
1697 public:
1698 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001699 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001700 SetRawInputAt(0, value);
1701 }
1702
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001703 virtual bool CanBeMoved() const { return true; }
1704 virtual bool InstructionDataEquals(HInstruction* other) const { return true; }
1705
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001706 virtual bool NeedsEnvironment() const { return true; }
1707
Roland Levillaine161a2a2014-10-03 12:45:18 +01001708 virtual bool CanThrow() const { return true; }
1709
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001710 uint32_t GetDexPc() const { return dex_pc_; }
1711
1712 DECLARE_INSTRUCTION(NullCheck);
1713
1714 private:
1715 const uint32_t dex_pc_;
1716
1717 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
1718};
1719
1720class FieldInfo : public ValueObject {
1721 public:
Roland Levillain5799fc02014-09-25 12:15:20 +01001722 FieldInfo(MemberOffset field_offset, Primitive::Type field_type)
Nicolas Geoffray39468442014-09-02 15:17:15 +01001723 : field_offset_(field_offset), field_type_(field_type) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001724
1725 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001726 Primitive::Type GetFieldType() const { return field_type_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001727
1728 private:
1729 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01001730 const Primitive::Type field_type_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001731};
1732
1733class HInstanceFieldGet : public HExpression<1> {
1734 public:
1735 HInstanceFieldGet(HInstruction* value,
1736 Primitive::Type field_type,
1737 MemberOffset field_offset)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001738 : HExpression(field_type, SideEffects::DependsOnSomething()),
1739 field_info_(field_offset, field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001740 SetRawInputAt(0, value);
1741 }
1742
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001743 virtual bool CanBeMoved() const { return true; }
1744 virtual bool InstructionDataEquals(HInstruction* other) const {
1745 size_t other_offset = other->AsInstanceFieldGet()->GetFieldOffset().SizeValue();
1746 return other_offset == GetFieldOffset().SizeValue();
1747 }
1748
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001749 virtual size_t ComputeHashCode() const {
1750 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
1751 }
1752
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001753 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001754 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001755
1756 DECLARE_INSTRUCTION(InstanceFieldGet);
1757
1758 private:
1759 const FieldInfo field_info_;
1760
1761 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
1762};
1763
1764class HInstanceFieldSet : public HTemplateInstruction<2> {
1765 public:
1766 HInstanceFieldSet(HInstruction* object,
1767 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01001768 Primitive::Type field_type,
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001769 MemberOffset field_offset)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001770 : HTemplateInstruction(SideEffects::ChangesSomething()),
1771 field_info_(field_offset, field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001772 SetRawInputAt(0, object);
1773 SetRawInputAt(1, value);
1774 }
1775
1776 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001777 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001778
1779 DECLARE_INSTRUCTION(InstanceFieldSet);
1780
1781 private:
1782 const FieldInfo field_info_;
1783
1784 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
1785};
1786
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001787class HArrayGet : public HExpression<2> {
1788 public:
1789 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001790 : HExpression(type, SideEffects::DependsOnSomething()) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001791 SetRawInputAt(0, array);
1792 SetRawInputAt(1, index);
1793 }
1794
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001795 virtual bool CanBeMoved() const { return true; }
1796 virtual bool InstructionDataEquals(HInstruction* other) const { return true; }
1797
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001798 DECLARE_INSTRUCTION(ArrayGet);
1799
1800 private:
1801 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
1802};
1803
1804class HArraySet : public HTemplateInstruction<3> {
1805 public:
1806 HArraySet(HInstruction* array,
1807 HInstruction* index,
1808 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01001809 Primitive::Type component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001810 uint32_t dex_pc)
1811 : HTemplateInstruction(SideEffects::ChangesSomething()),
1812 dex_pc_(dex_pc),
1813 component_type_(component_type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001814 SetRawInputAt(0, array);
1815 SetRawInputAt(1, index);
1816 SetRawInputAt(2, value);
1817 }
1818
1819 virtual bool NeedsEnvironment() const {
1820 // We currently always call a runtime method to catch array store
1821 // exceptions.
1822 return InputAt(2)->GetType() == Primitive::kPrimNot;
1823 }
1824
1825 uint32_t GetDexPc() const { return dex_pc_; }
1826
Nicolas Geoffray39468442014-09-02 15:17:15 +01001827 Primitive::Type GetComponentType() const { return component_type_; }
1828
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001829 DECLARE_INSTRUCTION(ArraySet);
1830
1831 private:
1832 const uint32_t dex_pc_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01001833 const Primitive::Type component_type_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001834
1835 DISALLOW_COPY_AND_ASSIGN(HArraySet);
1836};
1837
1838class HArrayLength : public HExpression<1> {
1839 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001840 explicit HArrayLength(HInstruction* array)
1841 : HExpression(Primitive::kPrimInt, SideEffects::None()) {
1842 // Note that arrays do not change length, so the instruction does not
1843 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001844 SetRawInputAt(0, array);
1845 }
1846
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001847 virtual bool CanBeMoved() const { return true; }
1848 virtual bool InstructionDataEquals(HInstruction* other) const { return true; }
1849
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001850 DECLARE_INSTRUCTION(ArrayLength);
1851
1852 private:
1853 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
1854};
1855
1856class HBoundsCheck : public HExpression<2> {
1857 public:
1858 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001859 : HExpression(index->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001860 DCHECK(index->GetType() == Primitive::kPrimInt);
1861 SetRawInputAt(0, index);
1862 SetRawInputAt(1, length);
1863 }
1864
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001865 virtual bool CanBeMoved() const { return true; }
1866 virtual bool InstructionDataEquals(HInstruction* other) const { return true; }
1867
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001868 virtual bool NeedsEnvironment() const { return true; }
1869
Roland Levillaine161a2a2014-10-03 12:45:18 +01001870 virtual bool CanThrow() const { return true; }
1871
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001872 uint32_t GetDexPc() const { return dex_pc_; }
1873
1874 DECLARE_INSTRUCTION(BoundsCheck);
1875
1876 private:
1877 const uint32_t dex_pc_;
1878
1879 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
1880};
1881
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001882/**
1883 * Some DEX instructions are folded into multiple HInstructions that need
1884 * to stay live until the last HInstruction. This class
1885 * is used as a marker for the baseline compiler to ensure its preceding
1886 * HInstruction stays live. `index` is the temporary number that is used
1887 * for knowing the stack offset where to store the instruction.
1888 */
1889class HTemporary : public HTemplateInstruction<0> {
1890 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001891 explicit HTemporary(size_t index) : HTemplateInstruction(SideEffects::None()), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001892
1893 size_t GetIndex() const { return index_; }
1894
1895 DECLARE_INSTRUCTION(Temporary);
1896
1897 private:
1898 const size_t index_;
1899
1900 DISALLOW_COPY_AND_ASSIGN(HTemporary);
1901};
1902
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001903class HSuspendCheck : public HTemplateInstruction<0> {
1904 public:
1905 explicit HSuspendCheck(uint32_t dex_pc)
Nicolas Geoffray9ebc72c2014-09-25 16:33:42 +01001906 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001907
1908 virtual bool NeedsEnvironment() const {
1909 return true;
1910 }
1911
1912 uint32_t GetDexPc() const { return dex_pc_; }
1913
1914 DECLARE_INSTRUCTION(SuspendCheck);
1915
1916 private:
1917 const uint32_t dex_pc_;
1918
1919 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
1920};
1921
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001922class MoveOperands : public ArenaObject {
1923 public:
Nicolas Geoffray740475d2014-09-29 10:33:25 +01001924 MoveOperands(Location source, Location destination, HInstruction* instruction)
1925 : source_(source), destination_(destination), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001926
1927 Location GetSource() const { return source_; }
1928 Location GetDestination() const { return destination_; }
1929
1930 void SetSource(Location value) { source_ = value; }
1931 void SetDestination(Location value) { destination_ = value; }
1932
1933 // The parallel move resolver marks moves as "in-progress" by clearing the
1934 // destination (but not the source).
1935 Location MarkPending() {
1936 DCHECK(!IsPending());
1937 Location dest = destination_;
1938 destination_ = Location::NoLocation();
1939 return dest;
1940 }
1941
1942 void ClearPending(Location dest) {
1943 DCHECK(IsPending());
1944 destination_ = dest;
1945 }
1946
1947 bool IsPending() const {
1948 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
1949 return destination_.IsInvalid() && !source_.IsInvalid();
1950 }
1951
1952 // True if this blocks a move from the given location.
1953 bool Blocks(Location loc) const {
1954 return !IsEliminated() && source_.Equals(loc);
1955 }
1956
1957 // A move is redundant if it's been eliminated, if its source and
1958 // destination are the same, or if its destination is unneeded.
1959 bool IsRedundant() const {
1960 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
1961 }
1962
1963 // We clear both operands to indicate move that's been eliminated.
1964 void Eliminate() {
1965 source_ = destination_ = Location::NoLocation();
1966 }
1967
1968 bool IsEliminated() const {
1969 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
1970 return source_.IsInvalid();
1971 }
1972
Nicolas Geoffray740475d2014-09-29 10:33:25 +01001973 HInstruction* GetInstruction() const { return instruction_; }
1974
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001975 private:
1976 Location source_;
1977 Location destination_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01001978 // The instruction this move is assocatied with. Null when this move is
1979 // for moving an input in the expected locations of user (including a phi user).
1980 // This is only used in debug mode, to ensure we do not connect interval siblings
1981 // in the same parallel move.
1982 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001983
1984 DISALLOW_COPY_AND_ASSIGN(MoveOperands);
1985};
1986
1987static constexpr size_t kDefaultNumberOfMoves = 4;
1988
1989class HParallelMove : public HTemplateInstruction<0> {
1990 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001991 explicit HParallelMove(ArenaAllocator* arena)
1992 : HTemplateInstruction(SideEffects::None()), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001993
1994 void AddMove(MoveOperands* move) {
Nicolas Geoffray740475d2014-09-29 10:33:25 +01001995 if (kIsDebugBuild && move->GetInstruction() != nullptr) {
1996 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
1997 DCHECK_NE(moves_.Get(i)->GetInstruction(), move->GetInstruction())
1998 << "Doing parallel moves for the same instruction.";
1999 }
2000 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002001 moves_.Add(move);
2002 }
2003
2004 MoveOperands* MoveOperandsAt(size_t index) const {
2005 return moves_.Get(index);
2006 }
2007
2008 size_t NumMoves() const { return moves_.Size(); }
2009
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002010 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002011
2012 private:
2013 GrowableArray<MoveOperands*> moves_;
2014
2015 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
2016};
2017
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002018class HGraphVisitor : public ValueObject {
2019 public:
Dave Allison20dfc792014-06-16 20:44:29 -07002020 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
2021 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002022
Dave Allison20dfc792014-06-16 20:44:29 -07002023 virtual void VisitInstruction(HInstruction* instruction) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002024 virtual void VisitBasicBlock(HBasicBlock* block);
2025
Roland Levillain633021e2014-10-01 14:12:25 +01002026 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002027 void VisitInsertionOrder();
2028
Roland Levillain633021e2014-10-01 14:12:25 +01002029 // Visit the graph following dominator tree reverse post-order.
2030 void VisitReversePostOrder();
2031
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002032 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002033
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002034 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002035#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002036 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
2037
2038 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
2039
2040#undef DECLARE_VISIT_INSTRUCTION
2041
2042 private:
2043 HGraph* graph_;
2044
2045 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
2046};
2047
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002048class HGraphDelegateVisitor : public HGraphVisitor {
2049 public:
2050 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
2051 virtual ~HGraphDelegateVisitor() {}
2052
2053 // Visit functions that delegate to to super class.
2054#define DECLARE_VISIT_INSTRUCTION(name, super) \
2055 virtual void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
2056
2057 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
2058
2059#undef DECLARE_VISIT_INSTRUCTION
2060
2061 private:
2062 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
2063};
2064
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002065class HInsertionOrderIterator : public ValueObject {
2066 public:
2067 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
2068
2069 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
2070 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
2071 void Advance() { ++index_; }
2072
2073 private:
2074 const HGraph& graph_;
2075 size_t index_;
2076
2077 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
2078};
2079
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002080class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002081 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002082 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002083
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002084 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
2085 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002086 void Advance() { ++index_; }
2087
2088 private:
2089 const HGraph& graph_;
2090 size_t index_;
2091
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002092 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002093};
2094
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002095class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002096 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002097 explicit HPostOrderIterator(const HGraph& graph)
2098 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002099
2100 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002101 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002102 void Advance() { --index_; }
2103
2104 private:
2105 const HGraph& graph_;
2106 size_t index_;
2107
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002108 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002109};
2110
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002111} // namespace art
2112
2113#endif // ART_COMPILER_OPTIMIZING_NODES_H_