blob: 37e5e6b9aa349338c5edbbeec0b57fa8a7be24f1 [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.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070082class HGraph : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +000083 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),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070088 entry_block_(nullptr),
89 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +010090 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +010091 number_of_vregs_(0),
92 number_of_in_vregs_(0),
Nicolas Geoffraye5038322014-07-04 09:41:32 +010093 number_of_temporaries_(0),
Dave Allison20dfc792014-06-16 20:44:29 -070094 current_instruction_id_(0) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +000095
Nicolas Geoffray787c3072014-03-17 10:20:19 +000096 ArenaAllocator* GetArena() const { return arena_; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +010097 const GrowableArray<HBasicBlock*>& GetBlocks() const { return blocks_; }
Roland Levillain93445682014-10-06 19:24:02 +010098 HBasicBlock* GetBlock(size_t id) const { return blocks_.Get(id); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +000099
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000100 HBasicBlock* GetEntryBlock() const { return entry_block_; }
101 HBasicBlock* GetExitBlock() const { return exit_block_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000102
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000103 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
104 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000105
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000106 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100107
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000108 void BuildDominatorTree();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100109 void TransformToSSA();
110 void SimplifyCFG();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000111
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100112 // Find all natural loops in this graph. Aborts computation and returns false
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100113 // if one loop is not natural, that is the header does not dominate the back
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100114 // edge.
115 bool FindNaturalLoops() const;
116
117 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
118 void SimplifyLoop(HBasicBlock* header);
119
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000120 int GetNextInstructionId() {
121 return current_instruction_id_++;
122 }
123
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100124 uint16_t GetMaximumNumberOfOutVRegs() const {
125 return maximum_number_of_out_vregs_;
126 }
127
128 void UpdateMaximumNumberOfOutVRegs(uint16_t new_value) {
129 maximum_number_of_out_vregs_ = std::max(new_value, maximum_number_of_out_vregs_);
130 }
131
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100132 void UpdateNumberOfTemporaries(size_t count) {
133 number_of_temporaries_ = std::max(count, number_of_temporaries_);
134 }
135
136 size_t GetNumberOfTemporaries() const {
137 return number_of_temporaries_;
138 }
139
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100140 void SetNumberOfVRegs(uint16_t number_of_vregs) {
141 number_of_vregs_ = number_of_vregs;
142 }
143
144 uint16_t GetNumberOfVRegs() const {
145 return number_of_vregs_;
146 }
147
148 void SetNumberOfInVRegs(uint16_t value) {
149 number_of_in_vregs_ = value;
150 }
151
152 uint16_t GetNumberOfInVRegs() const {
153 return number_of_in_vregs_;
154 }
155
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100156 uint16_t GetNumberOfLocalVRegs() const {
157 return number_of_vregs_ - number_of_in_vregs_;
158 }
159
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100160 const GrowableArray<HBasicBlock*>& GetReversePostOrder() const {
161 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100162 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100163
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000164 private:
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000165 HBasicBlock* FindCommonDominator(HBasicBlock* first, HBasicBlock* second) const;
166 void VisitBlockForDominatorTree(HBasicBlock* block,
167 HBasicBlock* predecessor,
168 GrowableArray<size_t>* visits);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100169 void FindBackEdges(ArenaBitVector* visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000170 void VisitBlockForBackEdges(HBasicBlock* block,
171 ArenaBitVector* visited,
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100172 ArenaBitVector* visiting);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000173 void RemoveDeadBlocks(const ArenaBitVector& visited) const;
174
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000175 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000176
177 // List of blocks in insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000178 GrowableArray<HBasicBlock*> blocks_;
179
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100180 // List of blocks to perform a reverse post order tree traversal.
181 GrowableArray<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000182
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000183 HBasicBlock* entry_block_;
184 HBasicBlock* exit_block_;
185
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100186 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100187 uint16_t maximum_number_of_out_vregs_;
188
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100189 // The number of virtual registers in this method. Contains the parameters.
190 uint16_t number_of_vregs_;
191
192 // The number of virtual registers used by parameters of this method.
193 uint16_t number_of_in_vregs_;
194
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100195 // The number of temporaries that will be needed for the baseline compiler.
196 size_t number_of_temporaries_;
197
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000198 // The current id to assign to a newly added instruction. See HInstruction.id_.
199 int current_instruction_id_;
200
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000201 DISALLOW_COPY_AND_ASSIGN(HGraph);
202};
203
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700204class HLoopInformation : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000205 public:
206 HLoopInformation(HBasicBlock* header, HGraph* graph)
207 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100208 suspend_check_(nullptr),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100209 back_edges_(graph->GetArena(), kDefaultNumberOfBackEdges),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100210 // Make bit vector growable, as the number of blocks may change.
211 blocks_(graph->GetArena(), graph->GetBlocks().Size(), true) {}
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100212
213 HBasicBlock* GetHeader() const {
214 return header_;
215 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000216
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100217 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
218 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
219 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
220
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000221 void AddBackEdge(HBasicBlock* back_edge) {
222 back_edges_.Add(back_edge);
223 }
224
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100225 void RemoveBackEdge(HBasicBlock* back_edge) {
226 back_edges_.Delete(back_edge);
227 }
228
229 bool IsBackEdge(HBasicBlock* block) {
230 for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) {
231 if (back_edges_.Get(i) == block) return true;
232 }
233 return false;
234 }
235
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000236 int NumberOfBackEdges() const {
237 return back_edges_.Size();
238 }
239
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100240 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100241
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100242 const GrowableArray<HBasicBlock*>& GetBackEdges() const {
243 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100244 }
245
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100246 void ClearBackEdges() {
247 back_edges_.Reset();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100248 }
249
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100250 // Find blocks that are part of this loop. Returns whether the loop is a natural loop,
251 // that is the header dominates the back edge.
252 bool Populate();
253
254 // Returns whether this loop information contains `block`.
255 // Note that this loop information *must* be populated before entering this function.
256 bool Contains(const HBasicBlock& block) const;
257
258 // Returns whether this loop information is an inner loop of `other`.
259 // Note that `other` *must* be populated before entering this function.
260 bool IsIn(const HLoopInformation& other) const;
261
262 const ArenaBitVector& GetBlocks() const { return blocks_; }
263
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000264 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100265 // Internal recursive implementation of `Populate`.
266 void PopulateRecursive(HBasicBlock* block);
267
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000268 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100269 HSuspendCheck* suspend_check_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000270 GrowableArray<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100271 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000272
273 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
274};
275
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100276static constexpr size_t kNoLifetime = -1;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100277static constexpr uint32_t kNoDexPc = -1;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100278
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000279// A block in a method. Contains the list of instructions represented
280// as a double linked list. Each block knows its predecessors and
281// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100282
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700283class HBasicBlock : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000284 public:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100285 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000286 : graph_(graph),
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000287 predecessors_(graph->GetArena(), kDefaultNumberOfPredecessors),
288 successors_(graph->GetArena(), kDefaultNumberOfSuccessors),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000289 loop_information_(nullptr),
290 dominator_(nullptr),
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100291 dominated_blocks_(graph->GetArena(), kDefaultNumberOfDominatedBlocks),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100292 block_id_(-1),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100293 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100294 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000295 lifetime_end_(kNoLifetime),
296 is_catch_block_(false) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000297
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100298 const GrowableArray<HBasicBlock*>& GetPredecessors() const {
299 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000300 }
301
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100302 const GrowableArray<HBasicBlock*>& GetSuccessors() const {
303 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000304 }
305
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100306 const GrowableArray<HBasicBlock*>& GetDominatedBlocks() const {
307 return dominated_blocks_;
308 }
309
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100310 bool IsEntryBlock() const {
311 return graph_->GetEntryBlock() == this;
312 }
313
314 bool IsExitBlock() const {
315 return graph_->GetExitBlock() == this;
316 }
317
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000318 void AddBackEdge(HBasicBlock* back_edge) {
319 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000320 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000321 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100322 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000323 loop_information_->AddBackEdge(back_edge);
324 }
325
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000326 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000327
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000328 int GetBlockId() const { return block_id_; }
329 void SetBlockId(int id) { block_id_ = id; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000330
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000331 HBasicBlock* GetDominator() const { return dominator_; }
332 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100333 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.Add(block); }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000334
335 int NumberOfBackEdges() const {
336 return loop_information_ == nullptr
337 ? 0
338 : loop_information_->NumberOfBackEdges();
339 }
340
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100341 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
342 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100343 const HInstructionList& GetInstructions() const { return instructions_; }
344 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100345 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000346
347 void AddSuccessor(HBasicBlock* block) {
348 successors_.Add(block);
349 block->predecessors_.Add(this);
350 }
351
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100352 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
353 size_t successor_index = GetSuccessorIndexOf(existing);
354 DCHECK_NE(successor_index, static_cast<size_t>(-1));
355 existing->RemovePredecessor(this);
356 new_block->predecessors_.Add(this);
357 successors_.Put(successor_index, new_block);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000358 }
359
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100360 void RemovePredecessor(HBasicBlock* block) {
361 predecessors_.Delete(block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100362 }
363
364 void ClearAllPredecessors() {
365 predecessors_.Reset();
366 }
367
368 void AddPredecessor(HBasicBlock* block) {
369 predecessors_.Add(block);
370 block->successors_.Add(this);
371 }
372
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100373 void SwapPredecessors() {
Nicolas Geoffrayc83d4412014-09-18 16:46:20 +0100374 DCHECK_EQ(predecessors_.Size(), 2u);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100375 HBasicBlock* temp = predecessors_.Get(0);
376 predecessors_.Put(0, predecessors_.Get(1));
377 predecessors_.Put(1, temp);
378 }
379
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100380 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) {
381 for (size_t i = 0, e = predecessors_.Size(); i < e; ++i) {
382 if (predecessors_.Get(i) == predecessor) {
383 return i;
384 }
385 }
386 return -1;
387 }
388
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100389 size_t GetSuccessorIndexOf(HBasicBlock* successor) {
390 for (size_t i = 0, e = successors_.Size(); i < e; ++i) {
391 if (successors_.Get(i) == successor) {
392 return i;
393 }
394 }
395 return -1;
396 }
397
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000398 void AddInstruction(HInstruction* instruction);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100399 void RemoveInstruction(HInstruction* instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100400 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +0100401 // Replace instruction `initial` with `replacement` within this block.
402 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
403 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100404 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100405 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100406 void RemovePhi(HPhi* phi);
407
408 bool IsLoopHeader() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100409 return (loop_information_ != nullptr) && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100410 }
411
Roland Levillain6b879dd2014-09-22 17:13:44 +0100412 bool IsLoopPreHeaderFirstPredecessor() const {
413 DCHECK(IsLoopHeader());
414 DCHECK(!GetPredecessors().IsEmpty());
415 return GetPredecessors().Get(0) == GetLoopInformation()->GetPreHeader();
416 }
417
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100418 HLoopInformation* GetLoopInformation() const {
419 return loop_information_;
420 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000421
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100422 // Set the loop_information_ on this block. This method overrides the current
423 // loop_information if it is an outer loop of the passed loop information.
424 void SetInLoop(HLoopInformation* info) {
425 if (IsLoopHeader()) {
426 // Nothing to do. This just means `info` is an outer loop.
427 } else if (loop_information_ == nullptr) {
428 loop_information_ = info;
429 } else if (loop_information_->Contains(*info->GetHeader())) {
430 // Block is currently part of an outer loop. Make it part of this inner loop.
431 // Note that a non loop header having a loop information means this loop information
432 // has already been populated
433 loop_information_ = info;
434 } else {
435 // Block is part of an inner loop. Do not update the loop information.
436 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
437 // at this point, because this method is being called while populating `info`.
438 }
439 }
440
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100441 bool IsInLoop() const { return loop_information_ != nullptr; }
442
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100443 // Returns wheter this block dominates the blocked passed as parameter.
444 bool Dominates(HBasicBlock* block) const;
445
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100446 size_t GetLifetimeStart() const { return lifetime_start_; }
447 size_t GetLifetimeEnd() const { return lifetime_end_; }
448
449 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
450 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
451
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100452 uint32_t GetDexPc() const { return dex_pc_; }
453
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000454 bool IsCatchBlock() const { return is_catch_block_; }
455 void SetIsCatchBlock() { is_catch_block_ = true; }
456
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000457 private:
458 HGraph* const graph_;
459 GrowableArray<HBasicBlock*> predecessors_;
460 GrowableArray<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100461 HInstructionList instructions_;
462 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000463 HLoopInformation* loop_information_;
464 HBasicBlock* dominator_;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100465 GrowableArray<HBasicBlock*> dominated_blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000466 int block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100467 // The dex program counter of the first instruction of this block.
468 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100469 size_t lifetime_start_;
470 size_t lifetime_end_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000471 bool is_catch_block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000472
473 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
474};
475
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100476#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
477 M(Add, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000478 M(ArrayGet, Instruction) \
479 M(ArrayLength, Instruction) \
480 M(ArraySet, Instruction) \
481 M(BoundsCheck, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100482 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000483 M(Compare, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100484 M(Condition, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000485 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +0000486 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000487 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100488 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000489 M(Exit, Instruction) \
490 M(FloatConstant, Constant) \
491 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100492 M(GreaterThan, Condition) \
493 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100494 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000495 M(InstanceFieldGet, Instruction) \
496 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100497 M(IntConstant, Constant) \
498 M(InvokeStatic, Invoke) \
499 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000500 M(LessThan, Condition) \
501 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000502 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000503 M(LoadException, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100504 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000505 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100506 M(Local, Instruction) \
507 M(LongConstant, Constant) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000508 M(Mul, BinaryOperation) \
509 M(Neg, UnaryOperation) \
510 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100511 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +0100512 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000513 M(NotEqual, Condition) \
514 M(NullCheck, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100515 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000516 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100517 M(Phi, Instruction) \
518 M(Return, Instruction) \
519 M(ReturnVoid, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100520 M(StaticFieldGet, Instruction) \
521 M(StaticFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100522 M(StoreLocal, Instruction) \
523 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100524 M(SuspendCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000525 M(Temporary, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000526 M(Throw, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +0000527 M(TypeConversion, Instruction) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000528
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100529#define FOR_EACH_INSTRUCTION(M) \
530 FOR_EACH_CONCRETE_INSTRUCTION(M) \
531 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +0100532 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +0100533 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100534 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -0700535
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100536#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000537FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
538#undef FORWARD_DECLARATION
539
Roland Levillainccc07a92014-09-16 14:48:16 +0100540#define DECLARE_INSTRUCTION(type) \
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100541 virtual InstructionKind GetKind() const { return k##type; } \
Roland Levillainccc07a92014-09-16 14:48:16 +0100542 virtual const char* DebugName() const { return #type; } \
543 virtual const H##type* As##type() const OVERRIDE { return this; } \
544 virtual H##type* As##type() OVERRIDE { return this; } \
545 virtual bool InstructionTypeEquals(HInstruction* other) const { \
546 return other->Is##type(); \
547 } \
548 virtual void Accept(HGraphVisitor* visitor)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000549
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100550template <typename T>
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700551class HUseListNode : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000552 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100553 HUseListNode(T* user, size_t index, HUseListNode* tail)
Dave Allison20dfc792014-06-16 20:44:29 -0700554 : user_(user), index_(index), tail_(tail) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000555
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000556 HUseListNode* GetTail() const { return tail_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100557 T* GetUser() const { return user_; }
558 size_t GetIndex() const { return index_; }
559
560 void SetTail(HUseListNode<T>* node) { tail_ = node; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000561
562 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100563 T* const user_;
564 const size_t index_;
565 HUseListNode<T>* tail_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000566
567 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
568};
569
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100570// Represents the side effects an instruction may have.
571class SideEffects : public ValueObject {
572 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100573 SideEffects() : flags_(0) {}
574
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100575 static SideEffects None() {
576 return SideEffects(0);
577 }
578
579 static SideEffects All() {
580 return SideEffects(ChangesSomething().flags_ | DependsOnSomething().flags_);
581 }
582
583 static SideEffects ChangesSomething() {
584 return SideEffects((1 << kFlagChangesCount) - 1);
585 }
586
587 static SideEffects DependsOnSomething() {
588 int count = kFlagDependsOnCount - kFlagChangesCount;
589 return SideEffects(((1 << count) - 1) << kFlagChangesCount);
590 }
591
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100592 SideEffects Union(SideEffects other) const {
593 return SideEffects(flags_ | other.flags_);
594 }
595
Roland Levillain72bceff2014-09-15 18:29:00 +0100596 bool HasSideEffects() const {
597 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
598 return (flags_ & all_bits_set) != 0;
599 }
600
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100601 bool HasAllSideEffects() const {
602 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
603 return all_bits_set == (flags_ & all_bits_set);
604 }
605
606 bool DependsOn(SideEffects other) const {
607 size_t depends_flags = other.ComputeDependsFlags();
608 return (flags_ & depends_flags) != 0;
609 }
610
611 bool HasDependencies() const {
612 int count = kFlagDependsOnCount - kFlagChangesCount;
613 size_t all_bits_set = (1 << count) - 1;
614 return ((flags_ >> kFlagChangesCount) & all_bits_set) != 0;
615 }
616
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100617 private:
618 static constexpr int kFlagChangesSomething = 0;
619 static constexpr int kFlagChangesCount = kFlagChangesSomething + 1;
620
621 static constexpr int kFlagDependsOnSomething = kFlagChangesCount;
622 static constexpr int kFlagDependsOnCount = kFlagDependsOnSomething + 1;
623
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100624 explicit SideEffects(size_t flags) : flags_(flags) {}
625
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100626 size_t ComputeDependsFlags() const {
627 return flags_ << kFlagChangesCount;
628 }
629
630 size_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100631};
632
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700633class HInstruction : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000634 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100635 explicit HInstruction(SideEffects side_effects)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000636 : previous_(nullptr),
637 next_(nullptr),
638 block_(nullptr),
639 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100640 ssa_index_(-1),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000641 uses_(nullptr),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100642 env_uses_(nullptr),
643 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100644 locations_(nullptr),
645 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100646 lifetime_position_(kNoLifetime),
647 side_effects_(side_effects) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000648
Dave Allison20dfc792014-06-16 20:44:29 -0700649 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000650
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100651#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100652 enum InstructionKind {
653 FOR_EACH_INSTRUCTION(DECLARE_KIND)
654 };
655#undef DECLARE_KIND
656
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000657 HInstruction* GetNext() const { return next_; }
658 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000659
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000660 HBasicBlock* GetBlock() const { return block_; }
661 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100662 bool IsInBlock() const { return block_ != nullptr; }
663 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +0100664 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000665
Roland Levillain6b879dd2014-09-22 17:13:44 +0100666 virtual size_t InputCount() const = 0;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100667 virtual HInstruction* InputAt(size_t i) const = 0;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000668
669 virtual void Accept(HGraphVisitor* visitor) = 0;
670 virtual const char* DebugName() const = 0;
671
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100672 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100673 virtual void SetRawInputAt(size_t index, HInstruction* input) = 0;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100674
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100675 virtual bool NeedsEnvironment() const { return false; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100676 virtual bool IsControlFlow() const { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +0100677 virtual bool CanThrow() const { return false; }
Roland Levillain72bceff2014-09-15 18:29:00 +0100678 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100679
680 void AddUseAt(HInstruction* user, size_t index) {
681 uses_ = new (block_->GetGraph()->GetArena()) HUseListNode<HInstruction>(user, index, uses_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000682 }
683
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100684 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +0100685 DCHECK(user != nullptr);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100686 env_uses_ = new (block_->GetGraph()->GetArena()) HUseListNode<HEnvironment>(
687 user, index, env_uses_);
688 }
689
690 void RemoveUser(HInstruction* user, size_t index);
Nicolas Geoffray724c9632014-09-22 12:27:27 +0100691 void RemoveEnvironmentUser(HEnvironment* user, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100692
693 HUseListNode<HInstruction>* GetUses() const { return uses_; }
694 HUseListNode<HEnvironment>* GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000695
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100696 bool HasUses() const { return uses_ != nullptr || env_uses_ != nullptr; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100697 bool HasEnvironmentUses() const { return env_uses_ != nullptr; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000698
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100699 size_t NumberOfUses() const {
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100700 // TODO: Optimize this method if it is used outside of the HGraphVisualizer.
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100701 size_t result = 0;
702 HUseListNode<HInstruction>* current = uses_;
703 while (current != nullptr) {
704 current = current->GetTail();
705 ++result;
706 }
707 return result;
708 }
709
Roland Levillain6c82d402014-10-13 16:10:27 +0100710 // Does this instruction strictly dominate `other_instruction`?
711 // Returns false if this instruction and `other_instruction` are the same.
712 // Aborts if this instruction and `other_instruction` are both phis.
713 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +0100714
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000715 int GetId() const { return id_; }
716 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000717
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100718 int GetSsaIndex() const { return ssa_index_; }
719 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
720 bool HasSsaIndex() const { return ssa_index_ != -1; }
721
722 bool HasEnvironment() const { return environment_ != nullptr; }
723 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100724 void SetEnvironment(HEnvironment* environment) { environment_ = environment; }
725
Nicolas Geoffray39468442014-09-02 15:17:15 +0100726 // Returns the number of entries in the environment. Typically, that is the
727 // number of dex registers in a method. It could be more in case of inlining.
728 size_t EnvironmentSize() const;
729
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000730 LocationSummary* GetLocations() const { return locations_; }
731 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000732
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100733 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100734 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100735
Dave Allison20dfc792014-06-16 20:44:29 -0700736 bool HasOnlyOneUse() const {
737 return uses_ != nullptr && uses_->GetTail() == nullptr;
738 }
739
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100740#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +0100741 bool Is##type() const { return (As##type() != nullptr); } \
742 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000743 virtual H##type* As##type() { return nullptr; }
744
745 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
746#undef INSTRUCTION_TYPE_CHECK
747
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100748 // Returns whether the instruction can be moved within the graph.
749 virtual bool CanBeMoved() const { return false; }
750
751 // Returns whether the two instructions are of the same kind.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700752 virtual bool InstructionTypeEquals(HInstruction* other) const {
753 UNUSED(other);
754 return false;
755 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100756
757 // Returns whether any data encoded in the two instructions is equal.
758 // This method does not look at the inputs. Both instructions must be
759 // of the same type, otherwise the method has undefined behavior.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700760 virtual bool InstructionDataEquals(HInstruction* other) const {
761 UNUSED(other);
762 return false;
763 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100764
765 // Returns whether two instructions are equal, that is:
766 // 1) They have the same type and contain the same data,
767 // 2) Their inputs are identical.
768 bool Equals(HInstruction* other) const;
769
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100770 virtual InstructionKind GetKind() const = 0;
771
772 virtual size_t ComputeHashCode() const {
773 size_t result = GetKind();
774 for (size_t i = 0, e = InputCount(); i < e; ++i) {
775 result = (result * 31) + InputAt(i)->GetId();
776 }
777 return result;
778 }
779
780 SideEffects GetSideEffects() const { return side_effects_; }
781
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100782 size_t GetLifetimePosition() const { return lifetime_position_; }
783 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
784 LiveInterval* GetLiveInterval() const { return live_interval_; }
785 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
786 bool HasLiveInterval() const { return live_interval_ != nullptr; }
787
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000788 private:
789 HInstruction* previous_;
790 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000791 HBasicBlock* block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000792
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000793 // An instruction gets an id when it is added to the graph.
794 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +0100795 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000796 int id_;
797
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100798 // When doing liveness analysis, instructions that have uses get an SSA index.
799 int ssa_index_;
800
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100801 // List of instructions that have this instruction as input.
802 HUseListNode<HInstruction>* uses_;
803
804 // List of environments that contain this instruction.
805 HUseListNode<HEnvironment>* env_uses_;
806
Nicolas Geoffray39468442014-09-02 15:17:15 +0100807 // The environment associated with this instruction. Not null if the instruction
808 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100809 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000810
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000811 // Set by the code generator.
812 LocationSummary* locations_;
813
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100814 // Set by the liveness analysis.
815 LiveInterval* live_interval_;
816
817 // Set by the liveness analysis, this is the position in a linear
818 // order of blocks where this instruction's live interval start.
819 size_t lifetime_position_;
820
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100821 const SideEffects side_effects_;
822
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000823 friend class HBasicBlock;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100824 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000825
826 DISALLOW_COPY_AND_ASSIGN(HInstruction);
827};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700828std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000829
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100830template<typename T>
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000831class HUseIterator : public ValueObject {
832 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100833 explicit HUseIterator(HUseListNode<T>* uses) : current_(uses) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000834
835 bool Done() const { return current_ == nullptr; }
836
837 void Advance() {
838 DCHECK(!Done());
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000839 current_ = current_->GetTail();
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000840 }
841
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100842 HUseListNode<T>* Current() const {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000843 DCHECK(!Done());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100844 return current_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000845 }
846
847 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100848 HUseListNode<T>* current_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000849
850 friend class HValue;
851};
852
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100853// A HEnvironment object contains the values of virtual registers at a given location.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700854class HEnvironment : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100855 public:
856 HEnvironment(ArenaAllocator* arena, size_t number_of_vregs) : vregs_(arena, number_of_vregs) {
857 vregs_.SetSize(number_of_vregs);
858 for (size_t i = 0; i < number_of_vregs; i++) {
859 vregs_.Put(i, nullptr);
860 }
861 }
862
863 void Populate(const GrowableArray<HInstruction*>& env) {
864 for (size_t i = 0; i < env.Size(); i++) {
865 HInstruction* instruction = env.Get(i);
866 vregs_.Put(i, instruction);
867 if (instruction != nullptr) {
868 instruction->AddEnvUseAt(this, i);
869 }
870 }
871 }
872
873 void SetRawEnvAt(size_t index, HInstruction* instruction) {
874 vregs_.Put(index, instruction);
875 }
876
Nicolas Geoffray39468442014-09-02 15:17:15 +0100877 HInstruction* GetInstructionAt(size_t index) const {
878 return vregs_.Get(index);
879 }
880
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100881 GrowableArray<HInstruction*>* GetVRegs() {
882 return &vregs_;
883 }
884
Nicolas Geoffray39468442014-09-02 15:17:15 +0100885 size_t Size() const { return vregs_.Size(); }
886
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100887 private:
888 GrowableArray<HInstruction*> vregs_;
889
890 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
891};
892
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000893class HInputIterator : public ValueObject {
894 public:
Dave Allison20dfc792014-06-16 20:44:29 -0700895 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000896
897 bool Done() const { return index_ == instruction_->InputCount(); }
898 HInstruction* Current() const { return instruction_->InputAt(index_); }
899 void Advance() { index_++; }
900
901 private:
902 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100903 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000904
905 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
906};
907
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000908class HInstructionIterator : public ValueObject {
909 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100910 explicit HInstructionIterator(const HInstructionList& instructions)
911 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000912 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000913 }
914
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000915 bool Done() const { return instruction_ == nullptr; }
916 HInstruction* Current() const { return instruction_; }
917 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000918 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000919 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000920 }
921
922 private:
923 HInstruction* instruction_;
924 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100925
926 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000927};
928
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100929class HBackwardInstructionIterator : public ValueObject {
930 public:
931 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
932 : instruction_(instructions.last_instruction_) {
933 next_ = Done() ? nullptr : instruction_->GetPrevious();
934 }
935
936 bool Done() const { return instruction_ == nullptr; }
937 HInstruction* Current() const { return instruction_; }
938 void Advance() {
939 instruction_ = next_;
940 next_ = Done() ? nullptr : instruction_->GetPrevious();
941 }
942
943 private:
944 HInstruction* instruction_;
945 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100946
947 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100948};
949
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000950// An embedded container with N elements of type T. Used (with partial
951// specialization for N=0) because embedded arrays cannot have size 0.
952template<typename T, intptr_t N>
953class EmbeddedArray {
954 public:
Dave Allison20dfc792014-06-16 20:44:29 -0700955 EmbeddedArray() : elements_() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000956
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000957 intptr_t GetLength() const { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000958
959 const T& operator[](intptr_t i) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000960 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000961 return elements_[i];
962 }
963
964 T& operator[](intptr_t i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000965 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000966 return elements_[i];
967 }
968
969 const T& At(intptr_t i) const {
970 return (*this)[i];
971 }
972
973 void SetAt(intptr_t i, const T& val) {
974 (*this)[i] = val;
975 }
976
977 private:
978 T elements_[N];
979};
980
981template<typename T>
982class EmbeddedArray<T, 0> {
983 public:
984 intptr_t length() const { return 0; }
985 const T& operator[](intptr_t i) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700986 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000987 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700988 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000989 }
990 T& operator[](intptr_t i) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700991 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000992 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700993 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000994 }
995};
996
997template<intptr_t N>
998class HTemplateInstruction: public HInstruction {
999 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001000 HTemplateInstruction<N>(SideEffects side_effects)
1001 : HInstruction(side_effects), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07001002 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001003
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001004 virtual size_t InputCount() const { return N; }
1005 virtual HInstruction* InputAt(size_t i) const { return inputs_[i]; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001006
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001007 protected:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001008 virtual void SetRawInputAt(size_t i, HInstruction* instruction) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001009 inputs_[i] = instruction;
1010 }
1011
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001012 private:
1013 EmbeddedArray<HInstruction*, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001014
1015 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001016};
1017
Dave Allison20dfc792014-06-16 20:44:29 -07001018template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001019class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07001020 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001021 HExpression<N>(Primitive::Type type, SideEffects side_effects)
1022 : HTemplateInstruction<N>(side_effects), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07001023 virtual ~HExpression() {}
1024
1025 virtual Primitive::Type GetType() const { return type_; }
1026
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001027 protected:
1028 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07001029};
1030
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001031// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
1032// instruction that branches to the exit block.
1033class HReturnVoid : public HTemplateInstruction<0> {
1034 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001035 HReturnVoid() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001036
1037 virtual bool IsControlFlow() const { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001038
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001039 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001040
1041 private:
1042 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
1043};
1044
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001045// Represents dex's RETURN opcodes. A HReturn is a control flow
1046// instruction that branches to the exit block.
1047class HReturn : public HTemplateInstruction<1> {
1048 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001049 explicit HReturn(HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001050 SetRawInputAt(0, value);
1051 }
1052
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001053 virtual bool IsControlFlow() const { return true; }
1054
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001055 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001056
1057 private:
1058 DISALLOW_COPY_AND_ASSIGN(HReturn);
1059};
1060
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001061// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001062// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001063// exit block.
1064class HExit : public HTemplateInstruction<0> {
1065 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001066 HExit() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001067
1068 virtual bool IsControlFlow() const { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001069
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001070 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001071
1072 private:
1073 DISALLOW_COPY_AND_ASSIGN(HExit);
1074};
1075
1076// Jumps from one block to another.
1077class HGoto : public HTemplateInstruction<0> {
1078 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001079 HGoto() : HTemplateInstruction(SideEffects::None()) {}
1080
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001081 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001082
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001083 HBasicBlock* GetSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001084 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001085 }
1086
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001087 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001088
1089 private:
1090 DISALLOW_COPY_AND_ASSIGN(HGoto);
1091};
1092
Dave Allison20dfc792014-06-16 20:44:29 -07001093
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001094// Conditional branch. A block ending with an HIf instruction must have
1095// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001096class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001097 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001098 explicit HIf(HInstruction* input) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001099 SetRawInputAt(0, input);
1100 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001101
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001102 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001103
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001104 HBasicBlock* IfTrueSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001105 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001106 }
1107
1108 HBasicBlock* IfFalseSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001109 return GetBlock()->GetSuccessors().Get(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001110 }
1111
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001112 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001113
Dave Allison20dfc792014-06-16 20:44:29 -07001114 virtual bool IsIfInstruction() const { return true; }
1115
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001116 private:
1117 DISALLOW_COPY_AND_ASSIGN(HIf);
1118};
1119
Roland Levillain88cb1752014-10-20 16:36:47 +01001120class HUnaryOperation : public HExpression<1> {
1121 public:
1122 HUnaryOperation(Primitive::Type result_type, HInstruction* input)
1123 : HExpression(result_type, SideEffects::None()) {
1124 SetRawInputAt(0, input);
1125 }
1126
1127 HInstruction* GetInput() const { return InputAt(0); }
1128 Primitive::Type GetResultType() const { return GetType(); }
1129
1130 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001131 virtual bool InstructionDataEquals(HInstruction* other) const {
1132 UNUSED(other);
1133 return true;
1134 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001135
Roland Levillain9240d6a2014-10-20 16:47:04 +01001136 // Try to statically evaluate `operation` and return a HConstant
1137 // containing the result of this evaluation. If `operation` cannot
1138 // be evaluated as a constant, return nullptr.
1139 HConstant* TryStaticEvaluation() const;
1140
1141 // Apply this operation to `x`.
1142 virtual int32_t Evaluate(int32_t x) const = 0;
1143 virtual int64_t Evaluate(int64_t x) const = 0;
1144
Roland Levillain88cb1752014-10-20 16:36:47 +01001145 DECLARE_INSTRUCTION(UnaryOperation);
1146
1147 private:
1148 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
1149};
1150
Dave Allison20dfc792014-06-16 20:44:29 -07001151class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001152 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001153 HBinaryOperation(Primitive::Type result_type,
1154 HInstruction* left,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001155 HInstruction* right) : HExpression(result_type, SideEffects::None()) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001156 SetRawInputAt(0, left);
1157 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001158 }
1159
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001160 HInstruction* GetLeft() const { return InputAt(0); }
1161 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07001162 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001163
1164 virtual bool IsCommutative() { return false; }
1165
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001166 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001167 virtual bool InstructionDataEquals(HInstruction* other) const {
1168 UNUSED(other);
1169 return true;
1170 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001171
Roland Levillain9240d6a2014-10-20 16:47:04 +01001172 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01001173 // containing the result of this evaluation. If `operation` cannot
1174 // be evaluated as a constant, return nullptr.
Roland Levillain9240d6a2014-10-20 16:47:04 +01001175 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01001176
1177 // Apply this operation to `x` and `y`.
1178 virtual int32_t Evaluate(int32_t x, int32_t y) const = 0;
1179 virtual int64_t Evaluate(int64_t x, int64_t y) const = 0;
1180
Roland Levillainccc07a92014-09-16 14:48:16 +01001181 DECLARE_INSTRUCTION(BinaryOperation);
1182
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001183 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001184 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
1185};
1186
Dave Allison20dfc792014-06-16 20:44:29 -07001187class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001188 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001189 HCondition(HInstruction* first, HInstruction* second)
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001190 : HBinaryOperation(Primitive::kPrimBoolean, first, second),
1191 needs_materialization_(true) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001192
1193 virtual bool IsCommutative() { return true; }
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001194
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001195 bool NeedsMaterialization() const { return needs_materialization_; }
1196 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001197
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001198 // For code generation purposes, returns whether this instruction is just before
1199 // `if_`, and disregard moves in between.
1200 bool IsBeforeWhenDisregardMoves(HIf* if_) const;
1201
Dave Allison20dfc792014-06-16 20:44:29 -07001202 DECLARE_INSTRUCTION(Condition);
1203
1204 virtual IfCondition GetCondition() const = 0;
1205
1206 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001207 // For register allocation purposes, returns whether this instruction needs to be
1208 // materialized (that is, not just be in the processor flags).
1209 bool needs_materialization_;
1210
Dave Allison20dfc792014-06-16 20:44:29 -07001211 DISALLOW_COPY_AND_ASSIGN(HCondition);
1212};
1213
1214// Instruction to check if two inputs are equal to each other.
1215class HEqual : public HCondition {
1216 public:
1217 HEqual(HInstruction* first, HInstruction* second)
1218 : HCondition(first, second) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001219
Roland Levillain93445682014-10-06 19:24:02 +01001220 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1221 return x == y ? 1 : 0;
1222 }
1223 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1224 return x == y ? 1 : 0;
1225 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001226
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001227 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001228
Dave Allison20dfc792014-06-16 20:44:29 -07001229 virtual IfCondition GetCondition() const {
1230 return kCondEQ;
1231 }
1232
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001233 private:
1234 DISALLOW_COPY_AND_ASSIGN(HEqual);
1235};
1236
Dave Allison20dfc792014-06-16 20:44:29 -07001237class HNotEqual : public HCondition {
1238 public:
1239 HNotEqual(HInstruction* first, HInstruction* second)
1240 : HCondition(first, second) {}
1241
Roland Levillain93445682014-10-06 19:24:02 +01001242 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1243 return x != y ? 1 : 0;
1244 }
1245 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1246 return x != y ? 1 : 0;
1247 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001248
Dave Allison20dfc792014-06-16 20:44:29 -07001249 DECLARE_INSTRUCTION(NotEqual);
1250
1251 virtual IfCondition GetCondition() const {
1252 return kCondNE;
1253 }
1254
1255 private:
1256 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
1257};
1258
1259class HLessThan : public HCondition {
1260 public:
1261 HLessThan(HInstruction* first, HInstruction* second)
1262 : HCondition(first, second) {}
1263
Roland Levillain93445682014-10-06 19:24:02 +01001264 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1265 return x < y ? 1 : 0;
1266 }
1267 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1268 return x < y ? 1 : 0;
1269 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001270
Dave Allison20dfc792014-06-16 20:44:29 -07001271 DECLARE_INSTRUCTION(LessThan);
1272
1273 virtual IfCondition GetCondition() const {
1274 return kCondLT;
1275 }
1276
1277 private:
1278 DISALLOW_COPY_AND_ASSIGN(HLessThan);
1279};
1280
1281class HLessThanOrEqual : public HCondition {
1282 public:
1283 HLessThanOrEqual(HInstruction* first, HInstruction* second)
1284 : HCondition(first, second) {}
1285
Roland Levillain93445682014-10-06 19:24:02 +01001286 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1287 return x <= y ? 1 : 0;
1288 }
1289 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1290 return x <= y ? 1 : 0;
1291 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001292
Dave Allison20dfc792014-06-16 20:44:29 -07001293 DECLARE_INSTRUCTION(LessThanOrEqual);
1294
1295 virtual IfCondition GetCondition() const {
1296 return kCondLE;
1297 }
1298
1299 private:
1300 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
1301};
1302
1303class HGreaterThan : public HCondition {
1304 public:
1305 HGreaterThan(HInstruction* first, HInstruction* second)
1306 : HCondition(first, second) {}
1307
Roland Levillain93445682014-10-06 19:24:02 +01001308 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1309 return x > y ? 1 : 0;
1310 }
1311 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1312 return x > y ? 1 : 0;
1313 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001314
Dave Allison20dfc792014-06-16 20:44:29 -07001315 DECLARE_INSTRUCTION(GreaterThan);
1316
1317 virtual IfCondition GetCondition() const {
1318 return kCondGT;
1319 }
1320
1321 private:
1322 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
1323};
1324
1325class HGreaterThanOrEqual : public HCondition {
1326 public:
1327 HGreaterThanOrEqual(HInstruction* first, HInstruction* second)
1328 : HCondition(first, second) {}
1329
Roland Levillain93445682014-10-06 19:24:02 +01001330 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1331 return x >= y ? 1 : 0;
1332 }
1333 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1334 return x >= y ? 1 : 0;
1335 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001336
Dave Allison20dfc792014-06-16 20:44:29 -07001337 DECLARE_INSTRUCTION(GreaterThanOrEqual);
1338
1339 virtual IfCondition GetCondition() const {
1340 return kCondGE;
1341 }
1342
1343 private:
1344 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
1345};
1346
1347
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001348// Instruction to check how two inputs compare to each other.
1349// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
1350class HCompare : public HBinaryOperation {
1351 public:
1352 HCompare(Primitive::Type type, HInstruction* first, HInstruction* second)
1353 : HBinaryOperation(Primitive::kPrimInt, first, second) {
1354 DCHECK_EQ(type, first->GetType());
1355 DCHECK_EQ(type, second->GetType());
1356 }
1357
Roland Levillain93445682014-10-06 19:24:02 +01001358 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001359 return
1360 x == y ? 0 :
1361 x > y ? 1 :
1362 -1;
1363 }
Roland Levillain93445682014-10-06 19:24:02 +01001364 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001365 return
1366 x == y ? 0 :
1367 x > y ? 1 :
1368 -1;
1369 }
1370
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001371 DECLARE_INSTRUCTION(Compare);
1372
1373 private:
1374 DISALLOW_COPY_AND_ASSIGN(HCompare);
1375};
1376
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001377// A local in the graph. Corresponds to a Dex register.
1378class HLocal : public HTemplateInstruction<0> {
1379 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001380 explicit HLocal(uint16_t reg_number)
1381 : HTemplateInstruction(SideEffects::None()), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001382
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001383 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001384
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001385 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001386
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001387 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001388 // The Dex register number.
1389 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001390
1391 DISALLOW_COPY_AND_ASSIGN(HLocal);
1392};
1393
1394// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07001395class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001396 public:
Roland Levillain5799fc02014-09-25 12:15:20 +01001397 HLoadLocal(HLocal* local, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001398 : HExpression(type, SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001399 SetRawInputAt(0, local);
1400 }
1401
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001402 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1403
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001404 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001405
1406 private:
1407 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
1408};
1409
1410// Store a value in a given local. This instruction has two inputs: the value
1411// and the local.
1412class HStoreLocal : public HTemplateInstruction<2> {
1413 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001414 HStoreLocal(HLocal* local, HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001415 SetRawInputAt(0, local);
1416 SetRawInputAt(1, value);
1417 }
1418
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001419 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1420
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001421 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001422
1423 private:
1424 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
1425};
1426
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001427class HConstant : public HExpression<0> {
1428 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001429 explicit HConstant(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
1430
1431 virtual bool CanBeMoved() const { return true; }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001432
1433 DECLARE_INSTRUCTION(Constant);
1434
1435 private:
1436 DISALLOW_COPY_AND_ASSIGN(HConstant);
1437};
1438
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001439class HFloatConstant : public HConstant {
1440 public:
1441 explicit HFloatConstant(float value) : HConstant(Primitive::kPrimFloat), value_(value) {}
1442
1443 float GetValue() const { return value_; }
1444
1445 virtual bool InstructionDataEquals(HInstruction* other) const {
1446 return bit_cast<float, int32_t>(other->AsFloatConstant()->value_) ==
1447 bit_cast<float, int32_t>(value_);
1448 }
1449
1450 virtual size_t ComputeHashCode() const { return static_cast<size_t>(GetValue()); }
1451
1452 DECLARE_INSTRUCTION(FloatConstant);
1453
1454 private:
1455 const float value_;
1456
1457 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
1458};
1459
1460class HDoubleConstant : public HConstant {
1461 public:
1462 explicit HDoubleConstant(double value) : HConstant(Primitive::kPrimDouble), value_(value) {}
1463
1464 double GetValue() const { return value_; }
1465
1466 virtual bool InstructionDataEquals(HInstruction* other) const {
1467 return bit_cast<double, int64_t>(other->AsDoubleConstant()->value_) ==
1468 bit_cast<double, int64_t>(value_);
1469 }
1470
1471 virtual size_t ComputeHashCode() const { return static_cast<size_t>(GetValue()); }
1472
1473 DECLARE_INSTRUCTION(DoubleConstant);
1474
1475 private:
1476 const double value_;
1477
1478 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
1479};
1480
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001481// Constants of the type int. Those can be from Dex instructions, or
1482// synthesized (for example with the if-eqz instruction).
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001483class HIntConstant : public HConstant {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001484 public:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001485 explicit HIntConstant(int32_t value) : HConstant(Primitive::kPrimInt), value_(value) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001486
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001487 int32_t GetValue() const { return value_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001488
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001489 virtual bool InstructionDataEquals(HInstruction* other) const {
1490 return other->AsIntConstant()->value_ == value_;
1491 }
1492
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001493 virtual size_t ComputeHashCode() const { return GetValue(); }
1494
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001495 DECLARE_INSTRUCTION(IntConstant);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001496
1497 private:
1498 const int32_t value_;
1499
1500 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
1501};
1502
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001503class HLongConstant : public HConstant {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001504 public:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001505 explicit HLongConstant(int64_t value) : HConstant(Primitive::kPrimLong), value_(value) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001506
1507 int64_t GetValue() const { return value_; }
1508
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001509 virtual bool InstructionDataEquals(HInstruction* other) const {
1510 return other->AsLongConstant()->value_ == value_;
1511 }
1512
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001513 virtual size_t ComputeHashCode() const { return static_cast<size_t>(GetValue()); }
1514
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001515 DECLARE_INSTRUCTION(LongConstant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001516
1517 private:
1518 const int64_t value_;
1519
1520 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
1521};
1522
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001523class HInvoke : public HInstruction {
1524 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001525 HInvoke(ArenaAllocator* arena,
1526 uint32_t number_of_arguments,
1527 Primitive::Type return_type,
1528 uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001529 : HInstruction(SideEffects::All()),
1530 inputs_(arena, number_of_arguments),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001531 return_type_(return_type),
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001532 dex_pc_(dex_pc) {
1533 inputs_.SetSize(number_of_arguments);
1534 }
1535
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001536 virtual size_t InputCount() const { return inputs_.Size(); }
1537 virtual HInstruction* InputAt(size_t i) const { return inputs_.Get(i); }
1538
1539 // Runtime needs to walk the stack, so Dex -> Dex calls need to
1540 // know their environment.
1541 virtual bool NeedsEnvironment() const { return true; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001542
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001543 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001544 SetRawInputAt(index, argument);
1545 }
1546
1547 virtual void SetRawInputAt(size_t index, HInstruction* input) {
1548 inputs_.Put(index, input);
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001549 }
1550
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001551 virtual Primitive::Type GetType() const { return return_type_; }
1552
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001553 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001554
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001555 DECLARE_INSTRUCTION(Invoke);
1556
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001557 protected:
1558 GrowableArray<HInstruction*> inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001559 const Primitive::Type return_type_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001560 const uint32_t dex_pc_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001561
1562 private:
1563 DISALLOW_COPY_AND_ASSIGN(HInvoke);
1564};
1565
1566class HInvokeStatic : public HInvoke {
1567 public:
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001568 HInvokeStatic(ArenaAllocator* arena,
1569 uint32_t number_of_arguments,
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001570 Primitive::Type return_type,
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001571 uint32_t dex_pc,
1572 uint32_t index_in_dex_cache)
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001573 : HInvoke(arena, number_of_arguments, return_type, dex_pc),
1574 index_in_dex_cache_(index_in_dex_cache) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001575
1576 uint32_t GetIndexInDexCache() const { return index_in_dex_cache_; }
1577
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001578 DECLARE_INSTRUCTION(InvokeStatic);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001579
1580 private:
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001581 const uint32_t index_in_dex_cache_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001582
1583 DISALLOW_COPY_AND_ASSIGN(HInvokeStatic);
1584};
1585
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001586class HInvokeVirtual : public HInvoke {
1587 public:
1588 HInvokeVirtual(ArenaAllocator* arena,
1589 uint32_t number_of_arguments,
1590 Primitive::Type return_type,
1591 uint32_t dex_pc,
1592 uint32_t vtable_index)
1593 : HInvoke(arena, number_of_arguments, return_type, dex_pc),
1594 vtable_index_(vtable_index) {}
1595
1596 uint32_t GetVTableIndex() const { return vtable_index_; }
1597
1598 DECLARE_INSTRUCTION(InvokeVirtual);
1599
1600 private:
1601 const uint32_t vtable_index_;
1602
1603 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
1604};
1605
Dave Allison20dfc792014-06-16 20:44:29 -07001606class HNewInstance : public HExpression<0> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001607 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001608 HNewInstance(uint32_t dex_pc, uint16_t type_index)
1609 : HExpression(Primitive::kPrimNot, SideEffects::None()),
1610 dex_pc_(dex_pc),
1611 type_index_(type_index) {}
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001612
1613 uint32_t GetDexPc() const { return dex_pc_; }
1614 uint16_t GetTypeIndex() const { return type_index_; }
1615
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001616 // Calls runtime so needs an environment.
1617 virtual bool NeedsEnvironment() const { return true; }
1618
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001619 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001620
1621 private:
1622 const uint32_t dex_pc_;
1623 const uint16_t type_index_;
1624
1625 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
1626};
1627
Roland Levillain88cb1752014-10-20 16:36:47 +01001628class HNeg : public HUnaryOperation {
1629 public:
1630 explicit HNeg(Primitive::Type result_type, HInstruction* input)
1631 : HUnaryOperation(result_type, input) {}
1632
Roland Levillain9240d6a2014-10-20 16:47:04 +01001633 virtual int32_t Evaluate(int32_t x) const OVERRIDE { return -x; }
1634 virtual int64_t Evaluate(int64_t x) const OVERRIDE { return -x; }
1635
Roland Levillain88cb1752014-10-20 16:36:47 +01001636 DECLARE_INSTRUCTION(Neg);
1637
1638 private:
1639 DISALLOW_COPY_AND_ASSIGN(HNeg);
1640};
1641
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001642class HNewArray : public HExpression<1> {
1643 public:
1644 HNewArray(HInstruction* length, uint32_t dex_pc, uint16_t type_index)
1645 : HExpression(Primitive::kPrimNot, SideEffects::None()),
1646 dex_pc_(dex_pc),
1647 type_index_(type_index) {
1648 SetRawInputAt(0, length);
1649 }
1650
1651 uint32_t GetDexPc() const { return dex_pc_; }
1652 uint16_t GetTypeIndex() const { return type_index_; }
1653
1654 // Calls runtime so needs an environment.
1655 virtual bool NeedsEnvironment() const { return true; }
1656
1657 DECLARE_INSTRUCTION(NewArray);
1658
1659 private:
1660 const uint32_t dex_pc_;
1661 const uint16_t type_index_;
1662
1663 DISALLOW_COPY_AND_ASSIGN(HNewArray);
1664};
1665
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001666class HAdd : public HBinaryOperation {
1667 public:
1668 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1669 : HBinaryOperation(result_type, left, right) {}
1670
1671 virtual bool IsCommutative() { return true; }
1672
Roland Levillain93445682014-10-06 19:24:02 +01001673 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1674 return x + y;
1675 }
1676 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1677 return x + y;
1678 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001679
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001680 DECLARE_INSTRUCTION(Add);
1681
1682 private:
1683 DISALLOW_COPY_AND_ASSIGN(HAdd);
1684};
1685
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001686class HSub : public HBinaryOperation {
1687 public:
1688 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1689 : HBinaryOperation(result_type, left, right) {}
1690
Roland Levillain93445682014-10-06 19:24:02 +01001691 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1692 return x - y;
1693 }
1694 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1695 return x - y;
1696 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001697
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001698 DECLARE_INSTRUCTION(Sub);
1699
1700 private:
1701 DISALLOW_COPY_AND_ASSIGN(HSub);
1702};
1703
Calin Juravle34bacdf2014-10-07 20:23:36 +01001704class HMul : public HBinaryOperation {
1705 public:
1706 HMul(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1707 : HBinaryOperation(result_type, left, right) {}
1708
1709 virtual bool IsCommutative() { return true; }
1710
1711 virtual int32_t Evaluate(int32_t x, int32_t y) const { return x * y; }
1712 virtual int64_t Evaluate(int64_t x, int64_t y) const { return x * y; }
1713
1714 DECLARE_INSTRUCTION(Mul);
1715
1716 private:
1717 DISALLOW_COPY_AND_ASSIGN(HMul);
1718};
1719
Calin Juravle7c4954d2014-10-28 16:57:40 +00001720class HDiv : public HBinaryOperation {
1721 public:
1722 HDiv(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1723 : HBinaryOperation(result_type, left, right) {}
1724
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00001725 virtual int32_t Evaluate(int32_t x, int32_t y) const {
1726 // Our graph structure ensures we never have 0 for `y` during constant folding.
1727 DCHECK_NE(y, 0);
1728 // Special case -1 to avoid getting a SIGFPE on x86.
1729 return (y == -1) ? -x : x / y;
1730 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00001731 virtual int64_t Evaluate(int64_t x, int64_t y) const { return x / y; }
1732
1733 DECLARE_INSTRUCTION(Div);
1734
1735 private:
1736 DISALLOW_COPY_AND_ASSIGN(HDiv);
1737};
1738
Calin Juravled0d48522014-11-04 16:40:20 +00001739class HDivZeroCheck : public HExpression<1> {
1740 public:
1741 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
1742 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
1743 SetRawInputAt(0, value);
1744 }
1745
1746 bool CanBeMoved() const OVERRIDE { return true; }
1747
1748 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
1749 UNUSED(other);
1750 return true;
1751 }
1752
1753 bool NeedsEnvironment() const OVERRIDE { return true; }
1754 bool CanThrow() const OVERRIDE { return true; }
1755
1756 uint32_t GetDexPc() const { return dex_pc_; }
1757
1758 DECLARE_INSTRUCTION(DivZeroCheck);
1759
1760 private:
1761 const uint32_t dex_pc_;
1762
1763 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
1764};
1765
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001766// The value of a parameter in this method. Its location depends on
1767// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07001768class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001769 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001770 HParameterValue(uint8_t index, Primitive::Type parameter_type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001771 : HExpression(parameter_type, SideEffects::None()), index_(index) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001772
1773 uint8_t GetIndex() const { return index_; }
1774
1775 DECLARE_INSTRUCTION(ParameterValue);
1776
1777 private:
1778 // The index of this parameter in the parameters list. Must be less
1779 // than HGraph::number_of_in_vregs_;
1780 const uint8_t index_;
1781
1782 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
1783};
1784
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001785class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001786 public:
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001787 explicit HNot(Primitive::Type result_type, HInstruction* input)
1788 : HUnaryOperation(result_type, input) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001789
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001790 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001791 virtual bool InstructionDataEquals(HInstruction* other) const {
1792 UNUSED(other);
1793 return true;
1794 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001795
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001796 virtual int32_t Evaluate(int32_t x) const OVERRIDE { return ~x; }
1797 virtual int64_t Evaluate(int64_t x) const OVERRIDE { return ~x; }
1798
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001799 DECLARE_INSTRUCTION(Not);
1800
1801 private:
1802 DISALLOW_COPY_AND_ASSIGN(HNot);
1803};
1804
Roland Levillaindff1f282014-11-05 14:15:05 +00001805class HTypeConversion : public HExpression<1> {
1806 public:
1807 // Instantiate a type conversion of `input` to `result_type`.
1808 HTypeConversion(Primitive::Type result_type, HInstruction* input)
1809 : HExpression(result_type, SideEffects::None()) {
1810 SetRawInputAt(0, input);
1811 DCHECK_NE(input->GetType(), result_type);
1812 }
1813
1814 HInstruction* GetInput() const { return InputAt(0); }
1815 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
1816 Primitive::Type GetResultType() const { return GetType(); }
1817
1818 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00001819 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00001820
1821 DECLARE_INSTRUCTION(TypeConversion);
1822
1823 private:
1824 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
1825};
1826
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001827class HPhi : public HInstruction {
1828 public:
1829 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001830 : HInstruction(SideEffects::None()),
1831 inputs_(arena, number_of_inputs),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001832 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001833 type_(type),
1834 is_live_(false) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001835 inputs_.SetSize(number_of_inputs);
1836 }
1837
1838 virtual size_t InputCount() const { return inputs_.Size(); }
1839 virtual HInstruction* InputAt(size_t i) const { return inputs_.Get(i); }
1840
1841 virtual void SetRawInputAt(size_t index, HInstruction* input) {
1842 inputs_.Put(index, input);
1843 }
1844
1845 void AddInput(HInstruction* input);
1846
1847 virtual Primitive::Type GetType() const { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001848 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001849
1850 uint32_t GetRegNumber() const { return reg_number_; }
1851
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001852 void SetDead() { is_live_ = false; }
1853 void SetLive() { is_live_ = true; }
1854 bool IsDead() const { return !is_live_; }
1855 bool IsLive() const { return is_live_; }
1856
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001857 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001858
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001859 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001860 GrowableArray<HInstruction*> inputs_;
1861 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001862 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001863 bool is_live_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001864
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001865 DISALLOW_COPY_AND_ASSIGN(HPhi);
1866};
1867
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001868class HNullCheck : public HExpression<1> {
1869 public:
1870 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001871 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001872 SetRawInputAt(0, value);
1873 }
1874
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001875 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001876 virtual bool InstructionDataEquals(HInstruction* other) const {
1877 UNUSED(other);
1878 return true;
1879 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001880
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001881 virtual bool NeedsEnvironment() const { return true; }
1882
Roland Levillaine161a2a2014-10-03 12:45:18 +01001883 virtual bool CanThrow() const { return true; }
1884
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001885 uint32_t GetDexPc() const { return dex_pc_; }
1886
1887 DECLARE_INSTRUCTION(NullCheck);
1888
1889 private:
1890 const uint32_t dex_pc_;
1891
1892 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
1893};
1894
1895class FieldInfo : public ValueObject {
1896 public:
Roland Levillain5799fc02014-09-25 12:15:20 +01001897 FieldInfo(MemberOffset field_offset, Primitive::Type field_type)
Nicolas Geoffray39468442014-09-02 15:17:15 +01001898 : field_offset_(field_offset), field_type_(field_type) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001899
1900 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001901 Primitive::Type GetFieldType() const { return field_type_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001902
1903 private:
1904 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01001905 const Primitive::Type field_type_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001906};
1907
1908class HInstanceFieldGet : public HExpression<1> {
1909 public:
1910 HInstanceFieldGet(HInstruction* value,
1911 Primitive::Type field_type,
1912 MemberOffset field_offset)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001913 : HExpression(field_type, SideEffects::DependsOnSomething()),
1914 field_info_(field_offset, field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001915 SetRawInputAt(0, value);
1916 }
1917
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001918 virtual bool CanBeMoved() const { return true; }
1919 virtual bool InstructionDataEquals(HInstruction* other) const {
1920 size_t other_offset = other->AsInstanceFieldGet()->GetFieldOffset().SizeValue();
1921 return other_offset == GetFieldOffset().SizeValue();
1922 }
1923
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001924 virtual size_t ComputeHashCode() const {
1925 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
1926 }
1927
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001928 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001929 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001930
1931 DECLARE_INSTRUCTION(InstanceFieldGet);
1932
1933 private:
1934 const FieldInfo field_info_;
1935
1936 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
1937};
1938
1939class HInstanceFieldSet : public HTemplateInstruction<2> {
1940 public:
1941 HInstanceFieldSet(HInstruction* object,
1942 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01001943 Primitive::Type field_type,
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001944 MemberOffset field_offset)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001945 : HTemplateInstruction(SideEffects::ChangesSomething()),
1946 field_info_(field_offset, field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001947 SetRawInputAt(0, object);
1948 SetRawInputAt(1, value);
1949 }
1950
1951 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001952 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001953
1954 DECLARE_INSTRUCTION(InstanceFieldSet);
1955
1956 private:
1957 const FieldInfo field_info_;
1958
1959 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
1960};
1961
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001962class HArrayGet : public HExpression<2> {
1963 public:
1964 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001965 : HExpression(type, SideEffects::DependsOnSomething()) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001966 SetRawInputAt(0, array);
1967 SetRawInputAt(1, index);
1968 }
1969
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001970 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001971 virtual bool InstructionDataEquals(HInstruction* other) const {
1972 UNUSED(other);
1973 return true;
1974 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001975 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001976
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001977 DECLARE_INSTRUCTION(ArrayGet);
1978
1979 private:
1980 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
1981};
1982
1983class HArraySet : public HTemplateInstruction<3> {
1984 public:
1985 HArraySet(HInstruction* array,
1986 HInstruction* index,
1987 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001988 Primitive::Type expected_component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001989 uint32_t dex_pc)
1990 : HTemplateInstruction(SideEffects::ChangesSomething()),
1991 dex_pc_(dex_pc),
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001992 expected_component_type_(expected_component_type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001993 SetRawInputAt(0, array);
1994 SetRawInputAt(1, index);
1995 SetRawInputAt(2, value);
1996 }
1997
1998 virtual bool NeedsEnvironment() const {
1999 // We currently always call a runtime method to catch array store
2000 // exceptions.
2001 return InputAt(2)->GetType() == Primitive::kPrimNot;
2002 }
2003
2004 uint32_t GetDexPc() const { return dex_pc_; }
2005
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002006 HInstruction* GetValue() const { return InputAt(2); }
2007
2008 Primitive::Type GetComponentType() const {
2009 // The Dex format does not type floating point index operations. Since the
2010 // `expected_component_type_` is set during building and can therefore not
2011 // be correct, we also check what is the value type. If it is a floating
2012 // point type, we must use that type.
2013 Primitive::Type value_type = GetValue()->GetType();
2014 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
2015 ? value_type
2016 : expected_component_type_;
2017 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002018
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002019 DECLARE_INSTRUCTION(ArraySet);
2020
2021 private:
2022 const uint32_t dex_pc_;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002023 const Primitive::Type expected_component_type_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002024
2025 DISALLOW_COPY_AND_ASSIGN(HArraySet);
2026};
2027
2028class HArrayLength : public HExpression<1> {
2029 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002030 explicit HArrayLength(HInstruction* array)
2031 : HExpression(Primitive::kPrimInt, SideEffects::None()) {
2032 // Note that arrays do not change length, so the instruction does not
2033 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002034 SetRawInputAt(0, array);
2035 }
2036
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002037 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002038 virtual bool InstructionDataEquals(HInstruction* other) const {
2039 UNUSED(other);
2040 return true;
2041 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002042
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002043 DECLARE_INSTRUCTION(ArrayLength);
2044
2045 private:
2046 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
2047};
2048
2049class HBoundsCheck : public HExpression<2> {
2050 public:
2051 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002052 : HExpression(index->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002053 DCHECK(index->GetType() == Primitive::kPrimInt);
2054 SetRawInputAt(0, index);
2055 SetRawInputAt(1, length);
2056 }
2057
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002058 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002059 virtual bool InstructionDataEquals(HInstruction* other) const {
2060 UNUSED(other);
2061 return true;
2062 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002063
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002064 virtual bool NeedsEnvironment() const { return true; }
2065
Roland Levillaine161a2a2014-10-03 12:45:18 +01002066 virtual bool CanThrow() const { return true; }
2067
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002068 uint32_t GetDexPc() const { return dex_pc_; }
2069
2070 DECLARE_INSTRUCTION(BoundsCheck);
2071
2072 private:
2073 const uint32_t dex_pc_;
2074
2075 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
2076};
2077
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002078/**
2079 * Some DEX instructions are folded into multiple HInstructions that need
2080 * to stay live until the last HInstruction. This class
2081 * is used as a marker for the baseline compiler to ensure its preceding
2082 * HInstruction stays live. `index` is the temporary number that is used
2083 * for knowing the stack offset where to store the instruction.
2084 */
2085class HTemporary : public HTemplateInstruction<0> {
2086 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002087 explicit HTemporary(size_t index) : HTemplateInstruction(SideEffects::None()), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002088
2089 size_t GetIndex() const { return index_; }
2090
2091 DECLARE_INSTRUCTION(Temporary);
2092
2093 private:
2094 const size_t index_;
2095
2096 DISALLOW_COPY_AND_ASSIGN(HTemporary);
2097};
2098
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002099class HSuspendCheck : public HTemplateInstruction<0> {
2100 public:
2101 explicit HSuspendCheck(uint32_t dex_pc)
Nicolas Geoffray9ebc72c2014-09-25 16:33:42 +01002102 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002103
2104 virtual bool NeedsEnvironment() const {
2105 return true;
2106 }
2107
2108 uint32_t GetDexPc() const { return dex_pc_; }
2109
2110 DECLARE_INSTRUCTION(SuspendCheck);
2111
2112 private:
2113 const uint32_t dex_pc_;
2114
2115 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
2116};
2117
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002118/**
2119 * Instruction to load a Class object.
2120 */
2121class HLoadClass : public HExpression<0> {
2122 public:
2123 HLoadClass(uint16_t type_index,
2124 bool is_referrers_class,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002125 uint32_t dex_pc)
2126 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2127 type_index_(type_index),
2128 is_referrers_class_(is_referrers_class),
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002129 dex_pc_(dex_pc),
2130 generate_clinit_check_(false) {}
2131
2132 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002133
2134 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2135 return other->AsLoadClass()->type_index_ == type_index_;
2136 }
2137
2138 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
2139
2140 uint32_t GetDexPc() const { return dex_pc_; }
2141 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002142 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002143
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002144 bool NeedsEnvironment() const OVERRIDE {
2145 // Will call runtime and load the class if the class is not loaded yet.
2146 // TODO: finer grain decision.
2147 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002148 }
2149
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002150 bool MustGenerateClinitCheck() const {
2151 return generate_clinit_check_;
2152 }
2153
2154 void SetMustGenerateClinitCheck() {
2155 generate_clinit_check_ = true;
2156 }
2157
2158 bool CanCallRuntime() const {
2159 return MustGenerateClinitCheck() || !is_referrers_class_;
2160 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002161
2162 DECLARE_INSTRUCTION(LoadClass);
2163
2164 private:
2165 const uint16_t type_index_;
2166 const bool is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002167 const uint32_t dex_pc_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002168 // Whether this instruction must generate the initialization check.
2169 // Used for code generation.
2170 bool generate_clinit_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002171
2172 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
2173};
2174
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002175class HLoadString : public HExpression<0> {
2176 public:
2177 HLoadString(uint32_t string_index, uint32_t dex_pc)
2178 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2179 string_index_(string_index),
2180 dex_pc_(dex_pc) {}
2181
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002182 bool CanBeMoved() const OVERRIDE { return true; }
2183
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002184 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2185 return other->AsLoadString()->string_index_ == string_index_;
2186 }
2187
2188 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
2189
2190 uint32_t GetDexPc() const { return dex_pc_; }
2191 uint32_t GetStringIndex() const { return string_index_; }
2192
2193 // TODO: Can we deopt or debug when we resolve a string?
2194 bool NeedsEnvironment() const OVERRIDE { return false; }
2195
2196 DECLARE_INSTRUCTION(LoadString);
2197
2198 private:
2199 const uint32_t string_index_;
2200 const uint32_t dex_pc_;
2201
2202 DISALLOW_COPY_AND_ASSIGN(HLoadString);
2203};
2204
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002205// TODO: Pass this check to HInvokeStatic nodes.
2206/**
2207 * Performs an initialization check on its Class object input.
2208 */
2209class HClinitCheck : public HExpression<1> {
2210 public:
2211 explicit HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
2212 : HExpression(Primitive::kPrimNot, SideEffects::All()),
2213 dex_pc_(dex_pc) {
2214 SetRawInputAt(0, constant);
2215 }
2216
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002217 bool CanBeMoved() const OVERRIDE { return true; }
2218 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2219 UNUSED(other);
2220 return true;
2221 }
2222
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002223 bool NeedsEnvironment() const OVERRIDE {
2224 // May call runtime to initialize the class.
2225 return true;
2226 }
2227
2228 uint32_t GetDexPc() const { return dex_pc_; }
2229
2230 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
2231
2232 DECLARE_INSTRUCTION(ClinitCheck);
2233
2234 private:
2235 const uint32_t dex_pc_;
2236
2237 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
2238};
2239
2240class HStaticFieldGet : public HExpression<1> {
2241 public:
2242 HStaticFieldGet(HInstruction* cls,
2243 Primitive::Type field_type,
2244 MemberOffset field_offset)
2245 : HExpression(field_type, SideEffects::DependsOnSomething()),
2246 field_info_(field_offset, field_type) {
2247 SetRawInputAt(0, cls);
2248 }
2249
2250 bool CanBeMoved() const OVERRIDE { return true; }
2251 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2252 size_t other_offset = other->AsStaticFieldGet()->GetFieldOffset().SizeValue();
2253 return other_offset == GetFieldOffset().SizeValue();
2254 }
2255
2256 size_t ComputeHashCode() const OVERRIDE {
2257 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
2258 }
2259
2260 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
2261 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
2262
2263 DECLARE_INSTRUCTION(StaticFieldGet);
2264
2265 private:
2266 const FieldInfo field_info_;
2267
2268 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
2269};
2270
2271class HStaticFieldSet : public HTemplateInstruction<2> {
2272 public:
2273 HStaticFieldSet(HInstruction* cls,
2274 HInstruction* value,
2275 Primitive::Type field_type,
2276 MemberOffset field_offset)
2277 : HTemplateInstruction(SideEffects::ChangesSomething()),
2278 field_info_(field_offset, field_type) {
2279 SetRawInputAt(0, cls);
2280 SetRawInputAt(1, value);
2281 }
2282
2283 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
2284 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
2285
2286 DECLARE_INSTRUCTION(StaticFieldSet);
2287
2288 private:
2289 const FieldInfo field_info_;
2290
2291 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
2292};
2293
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002294// Implement the move-exception DEX instruction.
2295class HLoadException : public HExpression<0> {
2296 public:
2297 HLoadException() : HExpression(Primitive::kPrimNot, SideEffects::None()) {}
2298
2299 DECLARE_INSTRUCTION(LoadException);
2300
2301 private:
2302 DISALLOW_COPY_AND_ASSIGN(HLoadException);
2303};
2304
2305class HThrow : public HTemplateInstruction<1> {
2306 public:
2307 HThrow(HInstruction* exception, uint32_t dex_pc)
2308 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {
2309 SetRawInputAt(0, exception);
2310 }
2311
2312 bool IsControlFlow() const OVERRIDE { return true; }
2313
2314 bool NeedsEnvironment() const OVERRIDE { return true; }
2315
2316 uint32_t GetDexPc() const { return dex_pc_; }
2317
2318 DECLARE_INSTRUCTION(Throw);
2319
2320 private:
2321 uint32_t dex_pc_;
2322
2323 DISALLOW_COPY_AND_ASSIGN(HThrow);
2324};
2325
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002326class MoveOperands : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002327 public:
Nicolas Geoffray740475d2014-09-29 10:33:25 +01002328 MoveOperands(Location source, Location destination, HInstruction* instruction)
2329 : source_(source), destination_(destination), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002330
2331 Location GetSource() const { return source_; }
2332 Location GetDestination() const { return destination_; }
2333
2334 void SetSource(Location value) { source_ = value; }
2335 void SetDestination(Location value) { destination_ = value; }
2336
2337 // The parallel move resolver marks moves as "in-progress" by clearing the
2338 // destination (but not the source).
2339 Location MarkPending() {
2340 DCHECK(!IsPending());
2341 Location dest = destination_;
2342 destination_ = Location::NoLocation();
2343 return dest;
2344 }
2345
2346 void ClearPending(Location dest) {
2347 DCHECK(IsPending());
2348 destination_ = dest;
2349 }
2350
2351 bool IsPending() const {
2352 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
2353 return destination_.IsInvalid() && !source_.IsInvalid();
2354 }
2355
2356 // True if this blocks a move from the given location.
2357 bool Blocks(Location loc) const {
2358 return !IsEliminated() && source_.Equals(loc);
2359 }
2360
2361 // A move is redundant if it's been eliminated, if its source and
2362 // destination are the same, or if its destination is unneeded.
2363 bool IsRedundant() const {
2364 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
2365 }
2366
2367 // We clear both operands to indicate move that's been eliminated.
2368 void Eliminate() {
2369 source_ = destination_ = Location::NoLocation();
2370 }
2371
2372 bool IsEliminated() const {
2373 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
2374 return source_.IsInvalid();
2375 }
2376
Nicolas Geoffray740475d2014-09-29 10:33:25 +01002377 HInstruction* GetInstruction() const { return instruction_; }
2378
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002379 private:
2380 Location source_;
2381 Location destination_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01002382 // The instruction this move is assocatied with. Null when this move is
2383 // for moving an input in the expected locations of user (including a phi user).
2384 // This is only used in debug mode, to ensure we do not connect interval siblings
2385 // in the same parallel move.
2386 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002387
2388 DISALLOW_COPY_AND_ASSIGN(MoveOperands);
2389};
2390
2391static constexpr size_t kDefaultNumberOfMoves = 4;
2392
2393class HParallelMove : public HTemplateInstruction<0> {
2394 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002395 explicit HParallelMove(ArenaAllocator* arena)
2396 : HTemplateInstruction(SideEffects::None()), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002397
2398 void AddMove(MoveOperands* move) {
Nicolas Geoffray740475d2014-09-29 10:33:25 +01002399 if (kIsDebugBuild && move->GetInstruction() != nullptr) {
2400 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
2401 DCHECK_NE(moves_.Get(i)->GetInstruction(), move->GetInstruction())
2402 << "Doing parallel moves for the same instruction.";
2403 }
2404 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002405 moves_.Add(move);
2406 }
2407
2408 MoveOperands* MoveOperandsAt(size_t index) const {
2409 return moves_.Get(index);
2410 }
2411
2412 size_t NumMoves() const { return moves_.Size(); }
2413
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002414 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002415
2416 private:
2417 GrowableArray<MoveOperands*> moves_;
2418
2419 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
2420};
2421
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002422class HGraphVisitor : public ValueObject {
2423 public:
Dave Allison20dfc792014-06-16 20:44:29 -07002424 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
2425 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002426
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002427 virtual void VisitInstruction(HInstruction* instruction) { UNUSED(instruction); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002428 virtual void VisitBasicBlock(HBasicBlock* block);
2429
Roland Levillain633021e2014-10-01 14:12:25 +01002430 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002431 void VisitInsertionOrder();
2432
Roland Levillain633021e2014-10-01 14:12:25 +01002433 // Visit the graph following dominator tree reverse post-order.
2434 void VisitReversePostOrder();
2435
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002436 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002437
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002438 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002439#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002440 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
2441
2442 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
2443
2444#undef DECLARE_VISIT_INSTRUCTION
2445
2446 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07002447 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002448
2449 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
2450};
2451
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002452class HGraphDelegateVisitor : public HGraphVisitor {
2453 public:
2454 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
2455 virtual ~HGraphDelegateVisitor() {}
2456
2457 // Visit functions that delegate to to super class.
2458#define DECLARE_VISIT_INSTRUCTION(name, super) \
2459 virtual void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
2460
2461 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
2462
2463#undef DECLARE_VISIT_INSTRUCTION
2464
2465 private:
2466 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
2467};
2468
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002469class HInsertionOrderIterator : public ValueObject {
2470 public:
2471 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
2472
2473 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
2474 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
2475 void Advance() { ++index_; }
2476
2477 private:
2478 const HGraph& graph_;
2479 size_t index_;
2480
2481 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
2482};
2483
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002484class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002485 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002486 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002487
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002488 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
2489 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002490 void Advance() { ++index_; }
2491
2492 private:
2493 const HGraph& graph_;
2494 size_t index_;
2495
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002496 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002497};
2498
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002499class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002500 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002501 explicit HPostOrderIterator(const HGraph& graph)
2502 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002503
2504 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002505 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002506 void Advance() { --index_; }
2507
2508 private:
2509 const HGraph& graph_;
2510 size_t index_;
2511
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002512 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002513};
2514
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002515} // namespace art
2516
2517#endif // ART_COMPILER_OPTIMIZING_NODES_H_