blob: ecf8c370f0645a9defc20d6576c653fa55b059bc [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) \
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000527 M(TypeCheck, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +0000528 M(TypeConversion, Instruction) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000529
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100530#define FOR_EACH_INSTRUCTION(M) \
531 FOR_EACH_CONCRETE_INSTRUCTION(M) \
532 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +0100533 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +0100534 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100535 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -0700536
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100537#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000538FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
539#undef FORWARD_DECLARATION
540
Roland Levillainccc07a92014-09-16 14:48:16 +0100541#define DECLARE_INSTRUCTION(type) \
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100542 virtual InstructionKind GetKind() const { return k##type; } \
Roland Levillainccc07a92014-09-16 14:48:16 +0100543 virtual const char* DebugName() const { return #type; } \
544 virtual const H##type* As##type() const OVERRIDE { return this; } \
545 virtual H##type* As##type() OVERRIDE { return this; } \
546 virtual bool InstructionTypeEquals(HInstruction* other) const { \
547 return other->Is##type(); \
548 } \
549 virtual void Accept(HGraphVisitor* visitor)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000550
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100551template <typename T>
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700552class HUseListNode : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000553 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100554 HUseListNode(T* user, size_t index, HUseListNode* tail)
Dave Allison20dfc792014-06-16 20:44:29 -0700555 : user_(user), index_(index), tail_(tail) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000556
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000557 HUseListNode* GetTail() const { return tail_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100558 T* GetUser() const { return user_; }
559 size_t GetIndex() const { return index_; }
560
561 void SetTail(HUseListNode<T>* node) { tail_ = node; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000562
563 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100564 T* const user_;
565 const size_t index_;
566 HUseListNode<T>* tail_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000567
568 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
569};
570
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100571// Represents the side effects an instruction may have.
572class SideEffects : public ValueObject {
573 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100574 SideEffects() : flags_(0) {}
575
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100576 static SideEffects None() {
577 return SideEffects(0);
578 }
579
580 static SideEffects All() {
581 return SideEffects(ChangesSomething().flags_ | DependsOnSomething().flags_);
582 }
583
584 static SideEffects ChangesSomething() {
585 return SideEffects((1 << kFlagChangesCount) - 1);
586 }
587
588 static SideEffects DependsOnSomething() {
589 int count = kFlagDependsOnCount - kFlagChangesCount;
590 return SideEffects(((1 << count) - 1) << kFlagChangesCount);
591 }
592
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100593 SideEffects Union(SideEffects other) const {
594 return SideEffects(flags_ | other.flags_);
595 }
596
Roland Levillain72bceff2014-09-15 18:29:00 +0100597 bool HasSideEffects() const {
598 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
599 return (flags_ & all_bits_set) != 0;
600 }
601
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100602 bool HasAllSideEffects() const {
603 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
604 return all_bits_set == (flags_ & all_bits_set);
605 }
606
607 bool DependsOn(SideEffects other) const {
608 size_t depends_flags = other.ComputeDependsFlags();
609 return (flags_ & depends_flags) != 0;
610 }
611
612 bool HasDependencies() const {
613 int count = kFlagDependsOnCount - kFlagChangesCount;
614 size_t all_bits_set = (1 << count) - 1;
615 return ((flags_ >> kFlagChangesCount) & all_bits_set) != 0;
616 }
617
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100618 private:
619 static constexpr int kFlagChangesSomething = 0;
620 static constexpr int kFlagChangesCount = kFlagChangesSomething + 1;
621
622 static constexpr int kFlagDependsOnSomething = kFlagChangesCount;
623 static constexpr int kFlagDependsOnCount = kFlagDependsOnSomething + 1;
624
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100625 explicit SideEffects(size_t flags) : flags_(flags) {}
626
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100627 size_t ComputeDependsFlags() const {
628 return flags_ << kFlagChangesCount;
629 }
630
631 size_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100632};
633
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700634class HInstruction : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000635 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100636 explicit HInstruction(SideEffects side_effects)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000637 : previous_(nullptr),
638 next_(nullptr),
639 block_(nullptr),
640 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100641 ssa_index_(-1),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000642 uses_(nullptr),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100643 env_uses_(nullptr),
644 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100645 locations_(nullptr),
646 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100647 lifetime_position_(kNoLifetime),
648 side_effects_(side_effects) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000649
Dave Allison20dfc792014-06-16 20:44:29 -0700650 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000651
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100652#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100653 enum InstructionKind {
654 FOR_EACH_INSTRUCTION(DECLARE_KIND)
655 };
656#undef DECLARE_KIND
657
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000658 HInstruction* GetNext() const { return next_; }
659 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000660
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000661 HBasicBlock* GetBlock() const { return block_; }
662 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100663 bool IsInBlock() const { return block_ != nullptr; }
664 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +0100665 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000666
Roland Levillain6b879dd2014-09-22 17:13:44 +0100667 virtual size_t InputCount() const = 0;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100668 virtual HInstruction* InputAt(size_t i) const = 0;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000669
670 virtual void Accept(HGraphVisitor* visitor) = 0;
671 virtual const char* DebugName() const = 0;
672
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100673 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100674 virtual void SetRawInputAt(size_t index, HInstruction* input) = 0;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100675
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100676 virtual bool NeedsEnvironment() const { return false; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100677 virtual bool IsControlFlow() const { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +0100678 virtual bool CanThrow() const { return false; }
Roland Levillain72bceff2014-09-15 18:29:00 +0100679 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100680
681 void AddUseAt(HInstruction* user, size_t index) {
682 uses_ = new (block_->GetGraph()->GetArena()) HUseListNode<HInstruction>(user, index, uses_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000683 }
684
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100685 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +0100686 DCHECK(user != nullptr);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100687 env_uses_ = new (block_->GetGraph()->GetArena()) HUseListNode<HEnvironment>(
688 user, index, env_uses_);
689 }
690
691 void RemoveUser(HInstruction* user, size_t index);
Nicolas Geoffray724c9632014-09-22 12:27:27 +0100692 void RemoveEnvironmentUser(HEnvironment* user, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100693
694 HUseListNode<HInstruction>* GetUses() const { return uses_; }
695 HUseListNode<HEnvironment>* GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000696
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100697 bool HasUses() const { return uses_ != nullptr || env_uses_ != nullptr; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100698 bool HasEnvironmentUses() const { return env_uses_ != nullptr; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000699
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100700 size_t NumberOfUses() const {
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100701 // TODO: Optimize this method if it is used outside of the HGraphVisualizer.
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100702 size_t result = 0;
703 HUseListNode<HInstruction>* current = uses_;
704 while (current != nullptr) {
705 current = current->GetTail();
706 ++result;
707 }
708 return result;
709 }
710
Roland Levillain6c82d402014-10-13 16:10:27 +0100711 // Does this instruction strictly dominate `other_instruction`?
712 // Returns false if this instruction and `other_instruction` are the same.
713 // Aborts if this instruction and `other_instruction` are both phis.
714 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +0100715
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000716 int GetId() const { return id_; }
717 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000718
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100719 int GetSsaIndex() const { return ssa_index_; }
720 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
721 bool HasSsaIndex() const { return ssa_index_ != -1; }
722
723 bool HasEnvironment() const { return environment_ != nullptr; }
724 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100725 void SetEnvironment(HEnvironment* environment) { environment_ = environment; }
726
Nicolas Geoffray39468442014-09-02 15:17:15 +0100727 // Returns the number of entries in the environment. Typically, that is the
728 // number of dex registers in a method. It could be more in case of inlining.
729 size_t EnvironmentSize() const;
730
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000731 LocationSummary* GetLocations() const { return locations_; }
732 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000733
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100734 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100735 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100736
Dave Allison20dfc792014-06-16 20:44:29 -0700737 bool HasOnlyOneUse() const {
738 return uses_ != nullptr && uses_->GetTail() == nullptr;
739 }
740
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100741#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +0100742 bool Is##type() const { return (As##type() != nullptr); } \
743 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000744 virtual H##type* As##type() { return nullptr; }
745
746 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
747#undef INSTRUCTION_TYPE_CHECK
748
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100749 // Returns whether the instruction can be moved within the graph.
750 virtual bool CanBeMoved() const { return false; }
751
752 // Returns whether the two instructions are of the same kind.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700753 virtual bool InstructionTypeEquals(HInstruction* other) const {
754 UNUSED(other);
755 return false;
756 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100757
758 // Returns whether any data encoded in the two instructions is equal.
759 // This method does not look at the inputs. Both instructions must be
760 // of the same type, otherwise the method has undefined behavior.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700761 virtual bool InstructionDataEquals(HInstruction* other) const {
762 UNUSED(other);
763 return false;
764 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100765
766 // Returns whether two instructions are equal, that is:
767 // 1) They have the same type and contain the same data,
768 // 2) Their inputs are identical.
769 bool Equals(HInstruction* other) const;
770
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100771 virtual InstructionKind GetKind() const = 0;
772
773 virtual size_t ComputeHashCode() const {
774 size_t result = GetKind();
775 for (size_t i = 0, e = InputCount(); i < e; ++i) {
776 result = (result * 31) + InputAt(i)->GetId();
777 }
778 return result;
779 }
780
781 SideEffects GetSideEffects() const { return side_effects_; }
782
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100783 size_t GetLifetimePosition() const { return lifetime_position_; }
784 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
785 LiveInterval* GetLiveInterval() const { return live_interval_; }
786 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
787 bool HasLiveInterval() const { return live_interval_ != nullptr; }
788
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000789 private:
790 HInstruction* previous_;
791 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000792 HBasicBlock* block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000793
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000794 // An instruction gets an id when it is added to the graph.
795 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +0100796 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000797 int id_;
798
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100799 // When doing liveness analysis, instructions that have uses get an SSA index.
800 int ssa_index_;
801
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100802 // List of instructions that have this instruction as input.
803 HUseListNode<HInstruction>* uses_;
804
805 // List of environments that contain this instruction.
806 HUseListNode<HEnvironment>* env_uses_;
807
Nicolas Geoffray39468442014-09-02 15:17:15 +0100808 // The environment associated with this instruction. Not null if the instruction
809 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100810 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000811
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000812 // Set by the code generator.
813 LocationSummary* locations_;
814
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100815 // Set by the liveness analysis.
816 LiveInterval* live_interval_;
817
818 // Set by the liveness analysis, this is the position in a linear
819 // order of blocks where this instruction's live interval start.
820 size_t lifetime_position_;
821
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100822 const SideEffects side_effects_;
823
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000824 friend class HBasicBlock;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100825 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000826
827 DISALLOW_COPY_AND_ASSIGN(HInstruction);
828};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700829std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000830
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100831template<typename T>
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000832class HUseIterator : public ValueObject {
833 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100834 explicit HUseIterator(HUseListNode<T>* uses) : current_(uses) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000835
836 bool Done() const { return current_ == nullptr; }
837
838 void Advance() {
839 DCHECK(!Done());
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000840 current_ = current_->GetTail();
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000841 }
842
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100843 HUseListNode<T>* Current() const {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000844 DCHECK(!Done());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100845 return current_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000846 }
847
848 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100849 HUseListNode<T>* current_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000850
851 friend class HValue;
852};
853
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100854// A HEnvironment object contains the values of virtual registers at a given location.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700855class HEnvironment : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100856 public:
857 HEnvironment(ArenaAllocator* arena, size_t number_of_vregs) : vregs_(arena, number_of_vregs) {
858 vregs_.SetSize(number_of_vregs);
859 for (size_t i = 0; i < number_of_vregs; i++) {
860 vregs_.Put(i, nullptr);
861 }
862 }
863
864 void Populate(const GrowableArray<HInstruction*>& env) {
865 for (size_t i = 0; i < env.Size(); i++) {
866 HInstruction* instruction = env.Get(i);
867 vregs_.Put(i, instruction);
868 if (instruction != nullptr) {
869 instruction->AddEnvUseAt(this, i);
870 }
871 }
872 }
873
874 void SetRawEnvAt(size_t index, HInstruction* instruction) {
875 vregs_.Put(index, instruction);
876 }
877
Nicolas Geoffray39468442014-09-02 15:17:15 +0100878 HInstruction* GetInstructionAt(size_t index) const {
879 return vregs_.Get(index);
880 }
881
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100882 GrowableArray<HInstruction*>* GetVRegs() {
883 return &vregs_;
884 }
885
Nicolas Geoffray39468442014-09-02 15:17:15 +0100886 size_t Size() const { return vregs_.Size(); }
887
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100888 private:
889 GrowableArray<HInstruction*> vregs_;
890
891 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
892};
893
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000894class HInputIterator : public ValueObject {
895 public:
Dave Allison20dfc792014-06-16 20:44:29 -0700896 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000897
898 bool Done() const { return index_ == instruction_->InputCount(); }
899 HInstruction* Current() const { return instruction_->InputAt(index_); }
900 void Advance() { index_++; }
901
902 private:
903 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100904 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000905
906 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
907};
908
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000909class HInstructionIterator : public ValueObject {
910 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100911 explicit HInstructionIterator(const HInstructionList& instructions)
912 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000913 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000914 }
915
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000916 bool Done() const { return instruction_ == nullptr; }
917 HInstruction* Current() const { return instruction_; }
918 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000919 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000920 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000921 }
922
923 private:
924 HInstruction* instruction_;
925 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100926
927 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000928};
929
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100930class HBackwardInstructionIterator : public ValueObject {
931 public:
932 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
933 : instruction_(instructions.last_instruction_) {
934 next_ = Done() ? nullptr : instruction_->GetPrevious();
935 }
936
937 bool Done() const { return instruction_ == nullptr; }
938 HInstruction* Current() const { return instruction_; }
939 void Advance() {
940 instruction_ = next_;
941 next_ = Done() ? nullptr : instruction_->GetPrevious();
942 }
943
944 private:
945 HInstruction* instruction_;
946 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100947
948 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100949};
950
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000951// An embedded container with N elements of type T. Used (with partial
952// specialization for N=0) because embedded arrays cannot have size 0.
953template<typename T, intptr_t N>
954class EmbeddedArray {
955 public:
Dave Allison20dfc792014-06-16 20:44:29 -0700956 EmbeddedArray() : elements_() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000957
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000958 intptr_t GetLength() const { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000959
960 const T& operator[](intptr_t i) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000961 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000962 return elements_[i];
963 }
964
965 T& operator[](intptr_t i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000966 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000967 return elements_[i];
968 }
969
970 const T& At(intptr_t i) const {
971 return (*this)[i];
972 }
973
974 void SetAt(intptr_t i, const T& val) {
975 (*this)[i] = val;
976 }
977
978 private:
979 T elements_[N];
980};
981
982template<typename T>
983class EmbeddedArray<T, 0> {
984 public:
985 intptr_t length() const { return 0; }
986 const T& operator[](intptr_t i) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700987 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000988 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700989 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000990 }
991 T& operator[](intptr_t i) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700992 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000993 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700994 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000995 }
996};
997
998template<intptr_t N>
999class HTemplateInstruction: public HInstruction {
1000 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001001 HTemplateInstruction<N>(SideEffects side_effects)
1002 : HInstruction(side_effects), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07001003 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001004
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001005 virtual size_t InputCount() const { return N; }
1006 virtual HInstruction* InputAt(size_t i) const { return inputs_[i]; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001007
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001008 protected:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001009 virtual void SetRawInputAt(size_t i, HInstruction* instruction) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001010 inputs_[i] = instruction;
1011 }
1012
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001013 private:
1014 EmbeddedArray<HInstruction*, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001015
1016 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001017};
1018
Dave Allison20dfc792014-06-16 20:44:29 -07001019template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001020class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07001021 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001022 HExpression<N>(Primitive::Type type, SideEffects side_effects)
1023 : HTemplateInstruction<N>(side_effects), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07001024 virtual ~HExpression() {}
1025
1026 virtual Primitive::Type GetType() const { return type_; }
1027
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001028 protected:
1029 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07001030};
1031
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001032// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
1033// instruction that branches to the exit block.
1034class HReturnVoid : public HTemplateInstruction<0> {
1035 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001036 HReturnVoid() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001037
1038 virtual bool IsControlFlow() const { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001039
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001040 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001041
1042 private:
1043 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
1044};
1045
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001046// Represents dex's RETURN opcodes. A HReturn is a control flow
1047// instruction that branches to the exit block.
1048class HReturn : public HTemplateInstruction<1> {
1049 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001050 explicit HReturn(HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001051 SetRawInputAt(0, value);
1052 }
1053
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001054 virtual bool IsControlFlow() const { return true; }
1055
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001056 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001057
1058 private:
1059 DISALLOW_COPY_AND_ASSIGN(HReturn);
1060};
1061
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001062// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001063// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001064// exit block.
1065class HExit : public HTemplateInstruction<0> {
1066 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001067 HExit() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001068
1069 virtual bool IsControlFlow() const { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001070
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001071 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001072
1073 private:
1074 DISALLOW_COPY_AND_ASSIGN(HExit);
1075};
1076
1077// Jumps from one block to another.
1078class HGoto : public HTemplateInstruction<0> {
1079 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001080 HGoto() : HTemplateInstruction(SideEffects::None()) {}
1081
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001082 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001083
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001084 HBasicBlock* GetSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001085 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001086 }
1087
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001088 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001089
1090 private:
1091 DISALLOW_COPY_AND_ASSIGN(HGoto);
1092};
1093
Dave Allison20dfc792014-06-16 20:44:29 -07001094
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001095// Conditional branch. A block ending with an HIf instruction must have
1096// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001097class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001098 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001099 explicit HIf(HInstruction* input) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001100 SetRawInputAt(0, input);
1101 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001102
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001103 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001104
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001105 HBasicBlock* IfTrueSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001106 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001107 }
1108
1109 HBasicBlock* IfFalseSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001110 return GetBlock()->GetSuccessors().Get(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001111 }
1112
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001113 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001114
Dave Allison20dfc792014-06-16 20:44:29 -07001115 virtual bool IsIfInstruction() const { return true; }
1116
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001117 private:
1118 DISALLOW_COPY_AND_ASSIGN(HIf);
1119};
1120
Roland Levillain88cb1752014-10-20 16:36:47 +01001121class HUnaryOperation : public HExpression<1> {
1122 public:
1123 HUnaryOperation(Primitive::Type result_type, HInstruction* input)
1124 : HExpression(result_type, SideEffects::None()) {
1125 SetRawInputAt(0, input);
1126 }
1127
1128 HInstruction* GetInput() const { return InputAt(0); }
1129 Primitive::Type GetResultType() const { return GetType(); }
1130
1131 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001132 virtual bool InstructionDataEquals(HInstruction* other) const {
1133 UNUSED(other);
1134 return true;
1135 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001136
Roland Levillain9240d6a2014-10-20 16:47:04 +01001137 // Try to statically evaluate `operation` and return a HConstant
1138 // containing the result of this evaluation. If `operation` cannot
1139 // be evaluated as a constant, return nullptr.
1140 HConstant* TryStaticEvaluation() const;
1141
1142 // Apply this operation to `x`.
1143 virtual int32_t Evaluate(int32_t x) const = 0;
1144 virtual int64_t Evaluate(int64_t x) const = 0;
1145
Roland Levillain88cb1752014-10-20 16:36:47 +01001146 DECLARE_INSTRUCTION(UnaryOperation);
1147
1148 private:
1149 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
1150};
1151
Dave Allison20dfc792014-06-16 20:44:29 -07001152class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001153 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001154 HBinaryOperation(Primitive::Type result_type,
1155 HInstruction* left,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001156 HInstruction* right) : HExpression(result_type, SideEffects::None()) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001157 SetRawInputAt(0, left);
1158 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001159 }
1160
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001161 HInstruction* GetLeft() const { return InputAt(0); }
1162 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07001163 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001164
1165 virtual bool IsCommutative() { return false; }
1166
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001167 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001168 virtual bool InstructionDataEquals(HInstruction* other) const {
1169 UNUSED(other);
1170 return true;
1171 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001172
Roland Levillain9240d6a2014-10-20 16:47:04 +01001173 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01001174 // containing the result of this evaluation. If `operation` cannot
1175 // be evaluated as a constant, return nullptr.
Roland Levillain9240d6a2014-10-20 16:47:04 +01001176 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01001177
1178 // Apply this operation to `x` and `y`.
1179 virtual int32_t Evaluate(int32_t x, int32_t y) const = 0;
1180 virtual int64_t Evaluate(int64_t x, int64_t y) const = 0;
1181
Roland Levillainccc07a92014-09-16 14:48:16 +01001182 DECLARE_INSTRUCTION(BinaryOperation);
1183
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001184 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001185 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
1186};
1187
Dave Allison20dfc792014-06-16 20:44:29 -07001188class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001189 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001190 HCondition(HInstruction* first, HInstruction* second)
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001191 : HBinaryOperation(Primitive::kPrimBoolean, first, second),
1192 needs_materialization_(true) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001193
1194 virtual bool IsCommutative() { return true; }
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001195
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001196 bool NeedsMaterialization() const { return needs_materialization_; }
1197 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001198
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001199 // For code generation purposes, returns whether this instruction is just before
1200 // `if_`, and disregard moves in between.
1201 bool IsBeforeWhenDisregardMoves(HIf* if_) const;
1202
Dave Allison20dfc792014-06-16 20:44:29 -07001203 DECLARE_INSTRUCTION(Condition);
1204
1205 virtual IfCondition GetCondition() const = 0;
1206
1207 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001208 // For register allocation purposes, returns whether this instruction needs to be
1209 // materialized (that is, not just be in the processor flags).
1210 bool needs_materialization_;
1211
Dave Allison20dfc792014-06-16 20:44:29 -07001212 DISALLOW_COPY_AND_ASSIGN(HCondition);
1213};
1214
1215// Instruction to check if two inputs are equal to each other.
1216class HEqual : public HCondition {
1217 public:
1218 HEqual(HInstruction* first, HInstruction* second)
1219 : HCondition(first, second) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001220
Roland Levillain93445682014-10-06 19:24:02 +01001221 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1222 return x == y ? 1 : 0;
1223 }
1224 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1225 return x == y ? 1 : 0;
1226 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001227
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001228 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001229
Dave Allison20dfc792014-06-16 20:44:29 -07001230 virtual IfCondition GetCondition() const {
1231 return kCondEQ;
1232 }
1233
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001234 private:
1235 DISALLOW_COPY_AND_ASSIGN(HEqual);
1236};
1237
Dave Allison20dfc792014-06-16 20:44:29 -07001238class HNotEqual : public HCondition {
1239 public:
1240 HNotEqual(HInstruction* first, HInstruction* second)
1241 : HCondition(first, second) {}
1242
Roland Levillain93445682014-10-06 19:24:02 +01001243 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1244 return x != y ? 1 : 0;
1245 }
1246 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1247 return x != y ? 1 : 0;
1248 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001249
Dave Allison20dfc792014-06-16 20:44:29 -07001250 DECLARE_INSTRUCTION(NotEqual);
1251
1252 virtual IfCondition GetCondition() const {
1253 return kCondNE;
1254 }
1255
1256 private:
1257 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
1258};
1259
1260class HLessThan : public HCondition {
1261 public:
1262 HLessThan(HInstruction* first, HInstruction* second)
1263 : HCondition(first, second) {}
1264
Roland Levillain93445682014-10-06 19:24:02 +01001265 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1266 return x < y ? 1 : 0;
1267 }
1268 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1269 return x < y ? 1 : 0;
1270 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001271
Dave Allison20dfc792014-06-16 20:44:29 -07001272 DECLARE_INSTRUCTION(LessThan);
1273
1274 virtual IfCondition GetCondition() const {
1275 return kCondLT;
1276 }
1277
1278 private:
1279 DISALLOW_COPY_AND_ASSIGN(HLessThan);
1280};
1281
1282class HLessThanOrEqual : public HCondition {
1283 public:
1284 HLessThanOrEqual(HInstruction* first, HInstruction* second)
1285 : HCondition(first, second) {}
1286
Roland Levillain93445682014-10-06 19:24:02 +01001287 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1288 return x <= y ? 1 : 0;
1289 }
1290 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1291 return x <= y ? 1 : 0;
1292 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001293
Dave Allison20dfc792014-06-16 20:44:29 -07001294 DECLARE_INSTRUCTION(LessThanOrEqual);
1295
1296 virtual IfCondition GetCondition() const {
1297 return kCondLE;
1298 }
1299
1300 private:
1301 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
1302};
1303
1304class HGreaterThan : public HCondition {
1305 public:
1306 HGreaterThan(HInstruction* first, HInstruction* second)
1307 : HCondition(first, second) {}
1308
Roland Levillain93445682014-10-06 19:24:02 +01001309 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1310 return x > y ? 1 : 0;
1311 }
1312 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1313 return x > y ? 1 : 0;
1314 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001315
Dave Allison20dfc792014-06-16 20:44:29 -07001316 DECLARE_INSTRUCTION(GreaterThan);
1317
1318 virtual IfCondition GetCondition() const {
1319 return kCondGT;
1320 }
1321
1322 private:
1323 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
1324};
1325
1326class HGreaterThanOrEqual : public HCondition {
1327 public:
1328 HGreaterThanOrEqual(HInstruction* first, HInstruction* second)
1329 : HCondition(first, second) {}
1330
Roland Levillain93445682014-10-06 19:24:02 +01001331 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1332 return x >= y ? 1 : 0;
1333 }
1334 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1335 return x >= y ? 1 : 0;
1336 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001337
Dave Allison20dfc792014-06-16 20:44:29 -07001338 DECLARE_INSTRUCTION(GreaterThanOrEqual);
1339
1340 virtual IfCondition GetCondition() const {
1341 return kCondGE;
1342 }
1343
1344 private:
1345 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
1346};
1347
1348
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001349// Instruction to check how two inputs compare to each other.
1350// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
1351class HCompare : public HBinaryOperation {
1352 public:
1353 HCompare(Primitive::Type type, HInstruction* first, HInstruction* second)
1354 : HBinaryOperation(Primitive::kPrimInt, first, second) {
1355 DCHECK_EQ(type, first->GetType());
1356 DCHECK_EQ(type, second->GetType());
1357 }
1358
Roland Levillain93445682014-10-06 19:24:02 +01001359 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001360 return
1361 x == y ? 0 :
1362 x > y ? 1 :
1363 -1;
1364 }
Roland Levillain93445682014-10-06 19:24:02 +01001365 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001366 return
1367 x == y ? 0 :
1368 x > y ? 1 :
1369 -1;
1370 }
1371
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001372 DECLARE_INSTRUCTION(Compare);
1373
1374 private:
1375 DISALLOW_COPY_AND_ASSIGN(HCompare);
1376};
1377
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001378// A local in the graph. Corresponds to a Dex register.
1379class HLocal : public HTemplateInstruction<0> {
1380 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001381 explicit HLocal(uint16_t reg_number)
1382 : HTemplateInstruction(SideEffects::None()), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001383
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001384 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001385
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001386 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001387
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001388 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001389 // The Dex register number.
1390 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001391
1392 DISALLOW_COPY_AND_ASSIGN(HLocal);
1393};
1394
1395// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07001396class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001397 public:
Roland Levillain5799fc02014-09-25 12:15:20 +01001398 HLoadLocal(HLocal* local, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001399 : HExpression(type, SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001400 SetRawInputAt(0, local);
1401 }
1402
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001403 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1404
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001405 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001406
1407 private:
1408 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
1409};
1410
1411// Store a value in a given local. This instruction has two inputs: the value
1412// and the local.
1413class HStoreLocal : public HTemplateInstruction<2> {
1414 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001415 HStoreLocal(HLocal* local, HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001416 SetRawInputAt(0, local);
1417 SetRawInputAt(1, value);
1418 }
1419
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001420 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1421
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001422 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001423
1424 private:
1425 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
1426};
1427
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001428class HConstant : public HExpression<0> {
1429 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001430 explicit HConstant(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
1431
1432 virtual bool CanBeMoved() const { return true; }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001433
1434 DECLARE_INSTRUCTION(Constant);
1435
1436 private:
1437 DISALLOW_COPY_AND_ASSIGN(HConstant);
1438};
1439
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001440class HFloatConstant : public HConstant {
1441 public:
1442 explicit HFloatConstant(float value) : HConstant(Primitive::kPrimFloat), value_(value) {}
1443
1444 float GetValue() const { return value_; }
1445
1446 virtual bool InstructionDataEquals(HInstruction* other) const {
1447 return bit_cast<float, int32_t>(other->AsFloatConstant()->value_) ==
1448 bit_cast<float, int32_t>(value_);
1449 }
1450
1451 virtual size_t ComputeHashCode() const { return static_cast<size_t>(GetValue()); }
1452
1453 DECLARE_INSTRUCTION(FloatConstant);
1454
1455 private:
1456 const float value_;
1457
1458 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
1459};
1460
1461class HDoubleConstant : public HConstant {
1462 public:
1463 explicit HDoubleConstant(double value) : HConstant(Primitive::kPrimDouble), value_(value) {}
1464
1465 double GetValue() const { return value_; }
1466
1467 virtual bool InstructionDataEquals(HInstruction* other) const {
1468 return bit_cast<double, int64_t>(other->AsDoubleConstant()->value_) ==
1469 bit_cast<double, int64_t>(value_);
1470 }
1471
1472 virtual size_t ComputeHashCode() const { return static_cast<size_t>(GetValue()); }
1473
1474 DECLARE_INSTRUCTION(DoubleConstant);
1475
1476 private:
1477 const double value_;
1478
1479 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
1480};
1481
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001482// Constants of the type int. Those can be from Dex instructions, or
1483// synthesized (for example with the if-eqz instruction).
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001484class HIntConstant : public HConstant {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001485 public:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001486 explicit HIntConstant(int32_t value) : HConstant(Primitive::kPrimInt), value_(value) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001487
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001488 int32_t GetValue() const { return value_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001489
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001490 virtual bool InstructionDataEquals(HInstruction* other) const {
1491 return other->AsIntConstant()->value_ == value_;
1492 }
1493
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001494 virtual size_t ComputeHashCode() const { return GetValue(); }
1495
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001496 DECLARE_INSTRUCTION(IntConstant);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001497
1498 private:
1499 const int32_t value_;
1500
1501 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
1502};
1503
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001504class HLongConstant : public HConstant {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001505 public:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001506 explicit HLongConstant(int64_t value) : HConstant(Primitive::kPrimLong), value_(value) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001507
1508 int64_t GetValue() const { return value_; }
1509
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001510 virtual bool InstructionDataEquals(HInstruction* other) const {
1511 return other->AsLongConstant()->value_ == value_;
1512 }
1513
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001514 virtual size_t ComputeHashCode() const { return static_cast<size_t>(GetValue()); }
1515
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001516 DECLARE_INSTRUCTION(LongConstant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001517
1518 private:
1519 const int64_t value_;
1520
1521 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
1522};
1523
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001524class HInvoke : public HInstruction {
1525 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001526 HInvoke(ArenaAllocator* arena,
1527 uint32_t number_of_arguments,
1528 Primitive::Type return_type,
1529 uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001530 : HInstruction(SideEffects::All()),
1531 inputs_(arena, number_of_arguments),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001532 return_type_(return_type),
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001533 dex_pc_(dex_pc) {
1534 inputs_.SetSize(number_of_arguments);
1535 }
1536
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001537 virtual size_t InputCount() const { return inputs_.Size(); }
1538 virtual HInstruction* InputAt(size_t i) const { return inputs_.Get(i); }
1539
1540 // Runtime needs to walk the stack, so Dex -> Dex calls need to
1541 // know their environment.
1542 virtual bool NeedsEnvironment() const { return true; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001543
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001544 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001545 SetRawInputAt(index, argument);
1546 }
1547
1548 virtual void SetRawInputAt(size_t index, HInstruction* input) {
1549 inputs_.Put(index, input);
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001550 }
1551
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001552 virtual Primitive::Type GetType() const { return return_type_; }
1553
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001554 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001555
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001556 DECLARE_INSTRUCTION(Invoke);
1557
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001558 protected:
1559 GrowableArray<HInstruction*> inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001560 const Primitive::Type return_type_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001561 const uint32_t dex_pc_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001562
1563 private:
1564 DISALLOW_COPY_AND_ASSIGN(HInvoke);
1565};
1566
1567class HInvokeStatic : public HInvoke {
1568 public:
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001569 HInvokeStatic(ArenaAllocator* arena,
1570 uint32_t number_of_arguments,
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001571 Primitive::Type return_type,
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001572 uint32_t dex_pc,
1573 uint32_t index_in_dex_cache)
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001574 : HInvoke(arena, number_of_arguments, return_type, dex_pc),
1575 index_in_dex_cache_(index_in_dex_cache) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001576
1577 uint32_t GetIndexInDexCache() const { return index_in_dex_cache_; }
1578
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001579 DECLARE_INSTRUCTION(InvokeStatic);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001580
1581 private:
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001582 const uint32_t index_in_dex_cache_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001583
1584 DISALLOW_COPY_AND_ASSIGN(HInvokeStatic);
1585};
1586
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001587class HInvokeVirtual : public HInvoke {
1588 public:
1589 HInvokeVirtual(ArenaAllocator* arena,
1590 uint32_t number_of_arguments,
1591 Primitive::Type return_type,
1592 uint32_t dex_pc,
1593 uint32_t vtable_index)
1594 : HInvoke(arena, number_of_arguments, return_type, dex_pc),
1595 vtable_index_(vtable_index) {}
1596
1597 uint32_t GetVTableIndex() const { return vtable_index_; }
1598
1599 DECLARE_INSTRUCTION(InvokeVirtual);
1600
1601 private:
1602 const uint32_t vtable_index_;
1603
1604 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
1605};
1606
Dave Allison20dfc792014-06-16 20:44:29 -07001607class HNewInstance : public HExpression<0> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001608 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001609 HNewInstance(uint32_t dex_pc, uint16_t type_index)
1610 : HExpression(Primitive::kPrimNot, SideEffects::None()),
1611 dex_pc_(dex_pc),
1612 type_index_(type_index) {}
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001613
1614 uint32_t GetDexPc() const { return dex_pc_; }
1615 uint16_t GetTypeIndex() const { return type_index_; }
1616
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001617 // Calls runtime so needs an environment.
1618 virtual bool NeedsEnvironment() const { return true; }
1619
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001620 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001621
1622 private:
1623 const uint32_t dex_pc_;
1624 const uint16_t type_index_;
1625
1626 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
1627};
1628
Roland Levillain88cb1752014-10-20 16:36:47 +01001629class HNeg : public HUnaryOperation {
1630 public:
1631 explicit HNeg(Primitive::Type result_type, HInstruction* input)
1632 : HUnaryOperation(result_type, input) {}
1633
Roland Levillain9240d6a2014-10-20 16:47:04 +01001634 virtual int32_t Evaluate(int32_t x) const OVERRIDE { return -x; }
1635 virtual int64_t Evaluate(int64_t x) const OVERRIDE { return -x; }
1636
Roland Levillain88cb1752014-10-20 16:36:47 +01001637 DECLARE_INSTRUCTION(Neg);
1638
1639 private:
1640 DISALLOW_COPY_AND_ASSIGN(HNeg);
1641};
1642
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001643class HNewArray : public HExpression<1> {
1644 public:
1645 HNewArray(HInstruction* length, uint32_t dex_pc, uint16_t type_index)
1646 : HExpression(Primitive::kPrimNot, SideEffects::None()),
1647 dex_pc_(dex_pc),
1648 type_index_(type_index) {
1649 SetRawInputAt(0, length);
1650 }
1651
1652 uint32_t GetDexPc() const { return dex_pc_; }
1653 uint16_t GetTypeIndex() const { return type_index_; }
1654
1655 // Calls runtime so needs an environment.
1656 virtual bool NeedsEnvironment() const { return true; }
1657
1658 DECLARE_INSTRUCTION(NewArray);
1659
1660 private:
1661 const uint32_t dex_pc_;
1662 const uint16_t type_index_;
1663
1664 DISALLOW_COPY_AND_ASSIGN(HNewArray);
1665};
1666
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001667class HAdd : public HBinaryOperation {
1668 public:
1669 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1670 : HBinaryOperation(result_type, left, right) {}
1671
1672 virtual bool IsCommutative() { return true; }
1673
Roland Levillain93445682014-10-06 19:24:02 +01001674 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1675 return x + y;
1676 }
1677 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1678 return x + y;
1679 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001680
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001681 DECLARE_INSTRUCTION(Add);
1682
1683 private:
1684 DISALLOW_COPY_AND_ASSIGN(HAdd);
1685};
1686
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001687class HSub : public HBinaryOperation {
1688 public:
1689 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1690 : HBinaryOperation(result_type, left, right) {}
1691
Roland Levillain93445682014-10-06 19:24:02 +01001692 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1693 return x - y;
1694 }
1695 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1696 return x - y;
1697 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001698
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001699 DECLARE_INSTRUCTION(Sub);
1700
1701 private:
1702 DISALLOW_COPY_AND_ASSIGN(HSub);
1703};
1704
Calin Juravle34bacdf2014-10-07 20:23:36 +01001705class HMul : public HBinaryOperation {
1706 public:
1707 HMul(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1708 : HBinaryOperation(result_type, left, right) {}
1709
1710 virtual bool IsCommutative() { return true; }
1711
1712 virtual int32_t Evaluate(int32_t x, int32_t y) const { return x * y; }
1713 virtual int64_t Evaluate(int64_t x, int64_t y) const { return x * y; }
1714
1715 DECLARE_INSTRUCTION(Mul);
1716
1717 private:
1718 DISALLOW_COPY_AND_ASSIGN(HMul);
1719};
1720
Calin Juravle7c4954d2014-10-28 16:57:40 +00001721class HDiv : public HBinaryOperation {
1722 public:
1723 HDiv(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1724 : HBinaryOperation(result_type, left, right) {}
1725
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00001726 virtual int32_t Evaluate(int32_t x, int32_t y) const {
1727 // Our graph structure ensures we never have 0 for `y` during constant folding.
1728 DCHECK_NE(y, 0);
1729 // Special case -1 to avoid getting a SIGFPE on x86.
1730 return (y == -1) ? -x : x / y;
1731 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00001732 virtual int64_t Evaluate(int64_t x, int64_t y) const { return x / y; }
1733
1734 DECLARE_INSTRUCTION(Div);
1735
1736 private:
1737 DISALLOW_COPY_AND_ASSIGN(HDiv);
1738};
1739
Calin Juravled0d48522014-11-04 16:40:20 +00001740class HDivZeroCheck : public HExpression<1> {
1741 public:
1742 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
1743 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
1744 SetRawInputAt(0, value);
1745 }
1746
1747 bool CanBeMoved() const OVERRIDE { return true; }
1748
1749 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
1750 UNUSED(other);
1751 return true;
1752 }
1753
1754 bool NeedsEnvironment() const OVERRIDE { return true; }
1755 bool CanThrow() const OVERRIDE { return true; }
1756
1757 uint32_t GetDexPc() const { return dex_pc_; }
1758
1759 DECLARE_INSTRUCTION(DivZeroCheck);
1760
1761 private:
1762 const uint32_t dex_pc_;
1763
1764 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
1765};
1766
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001767// The value of a parameter in this method. Its location depends on
1768// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07001769class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001770 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001771 HParameterValue(uint8_t index, Primitive::Type parameter_type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001772 : HExpression(parameter_type, SideEffects::None()), index_(index) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001773
1774 uint8_t GetIndex() const { return index_; }
1775
1776 DECLARE_INSTRUCTION(ParameterValue);
1777
1778 private:
1779 // The index of this parameter in the parameters list. Must be less
1780 // than HGraph::number_of_in_vregs_;
1781 const uint8_t index_;
1782
1783 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
1784};
1785
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001786class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001787 public:
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001788 explicit HNot(Primitive::Type result_type, HInstruction* input)
1789 : HUnaryOperation(result_type, input) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001790
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001791 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001792 virtual bool InstructionDataEquals(HInstruction* other) const {
1793 UNUSED(other);
1794 return true;
1795 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001796
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001797 virtual int32_t Evaluate(int32_t x) const OVERRIDE { return ~x; }
1798 virtual int64_t Evaluate(int64_t x) const OVERRIDE { return ~x; }
1799
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001800 DECLARE_INSTRUCTION(Not);
1801
1802 private:
1803 DISALLOW_COPY_AND_ASSIGN(HNot);
1804};
1805
Roland Levillaindff1f282014-11-05 14:15:05 +00001806class HTypeConversion : public HExpression<1> {
1807 public:
1808 // Instantiate a type conversion of `input` to `result_type`.
1809 HTypeConversion(Primitive::Type result_type, HInstruction* input)
1810 : HExpression(result_type, SideEffects::None()) {
1811 SetRawInputAt(0, input);
1812 DCHECK_NE(input->GetType(), result_type);
1813 }
1814
1815 HInstruction* GetInput() const { return InputAt(0); }
1816 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
1817 Primitive::Type GetResultType() const { return GetType(); }
1818
1819 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00001820 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00001821
1822 DECLARE_INSTRUCTION(TypeConversion);
1823
1824 private:
1825 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
1826};
1827
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001828class HPhi : public HInstruction {
1829 public:
1830 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001831 : HInstruction(SideEffects::None()),
1832 inputs_(arena, number_of_inputs),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001833 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001834 type_(type),
1835 is_live_(false) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001836 inputs_.SetSize(number_of_inputs);
1837 }
1838
1839 virtual size_t InputCount() const { return inputs_.Size(); }
1840 virtual HInstruction* InputAt(size_t i) const { return inputs_.Get(i); }
1841
1842 virtual void SetRawInputAt(size_t index, HInstruction* input) {
1843 inputs_.Put(index, input);
1844 }
1845
1846 void AddInput(HInstruction* input);
1847
1848 virtual Primitive::Type GetType() const { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001849 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001850
1851 uint32_t GetRegNumber() const { return reg_number_; }
1852
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001853 void SetDead() { is_live_ = false; }
1854 void SetLive() { is_live_ = true; }
1855 bool IsDead() const { return !is_live_; }
1856 bool IsLive() const { return is_live_; }
1857
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001858 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001859
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001860 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001861 GrowableArray<HInstruction*> inputs_;
1862 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001863 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001864 bool is_live_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001865
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001866 DISALLOW_COPY_AND_ASSIGN(HPhi);
1867};
1868
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001869class HNullCheck : public HExpression<1> {
1870 public:
1871 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001872 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001873 SetRawInputAt(0, value);
1874 }
1875
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001876 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001877 virtual bool InstructionDataEquals(HInstruction* other) const {
1878 UNUSED(other);
1879 return true;
1880 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001881
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001882 virtual bool NeedsEnvironment() const { return true; }
1883
Roland Levillaine161a2a2014-10-03 12:45:18 +01001884 virtual bool CanThrow() const { return true; }
1885
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001886 uint32_t GetDexPc() const { return dex_pc_; }
1887
1888 DECLARE_INSTRUCTION(NullCheck);
1889
1890 private:
1891 const uint32_t dex_pc_;
1892
1893 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
1894};
1895
1896class FieldInfo : public ValueObject {
1897 public:
Roland Levillain5799fc02014-09-25 12:15:20 +01001898 FieldInfo(MemberOffset field_offset, Primitive::Type field_type)
Nicolas Geoffray39468442014-09-02 15:17:15 +01001899 : field_offset_(field_offset), field_type_(field_type) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001900
1901 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001902 Primitive::Type GetFieldType() const { return field_type_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001903
1904 private:
1905 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01001906 const Primitive::Type field_type_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001907};
1908
1909class HInstanceFieldGet : public HExpression<1> {
1910 public:
1911 HInstanceFieldGet(HInstruction* value,
1912 Primitive::Type field_type,
1913 MemberOffset field_offset)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001914 : HExpression(field_type, SideEffects::DependsOnSomething()),
1915 field_info_(field_offset, field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001916 SetRawInputAt(0, value);
1917 }
1918
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001919 virtual bool CanBeMoved() const { return true; }
1920 virtual bool InstructionDataEquals(HInstruction* other) const {
1921 size_t other_offset = other->AsInstanceFieldGet()->GetFieldOffset().SizeValue();
1922 return other_offset == GetFieldOffset().SizeValue();
1923 }
1924
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001925 virtual size_t ComputeHashCode() const {
1926 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
1927 }
1928
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001929 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001930 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001931
1932 DECLARE_INSTRUCTION(InstanceFieldGet);
1933
1934 private:
1935 const FieldInfo field_info_;
1936
1937 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
1938};
1939
1940class HInstanceFieldSet : public HTemplateInstruction<2> {
1941 public:
1942 HInstanceFieldSet(HInstruction* object,
1943 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01001944 Primitive::Type field_type,
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001945 MemberOffset field_offset)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001946 : HTemplateInstruction(SideEffects::ChangesSomething()),
1947 field_info_(field_offset, field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001948 SetRawInputAt(0, object);
1949 SetRawInputAt(1, value);
1950 }
1951
1952 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001953 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001954
1955 DECLARE_INSTRUCTION(InstanceFieldSet);
1956
1957 private:
1958 const FieldInfo field_info_;
1959
1960 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
1961};
1962
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001963class HArrayGet : public HExpression<2> {
1964 public:
1965 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001966 : HExpression(type, SideEffects::DependsOnSomething()) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001967 SetRawInputAt(0, array);
1968 SetRawInputAt(1, index);
1969 }
1970
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001971 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001972 virtual bool InstructionDataEquals(HInstruction* other) const {
1973 UNUSED(other);
1974 return true;
1975 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001976 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001977
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001978 DECLARE_INSTRUCTION(ArrayGet);
1979
1980 private:
1981 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
1982};
1983
1984class HArraySet : public HTemplateInstruction<3> {
1985 public:
1986 HArraySet(HInstruction* array,
1987 HInstruction* index,
1988 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001989 Primitive::Type expected_component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001990 uint32_t dex_pc)
1991 : HTemplateInstruction(SideEffects::ChangesSomething()),
1992 dex_pc_(dex_pc),
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001993 expected_component_type_(expected_component_type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001994 SetRawInputAt(0, array);
1995 SetRawInputAt(1, index);
1996 SetRawInputAt(2, value);
1997 }
1998
1999 virtual bool NeedsEnvironment() const {
2000 // We currently always call a runtime method to catch array store
2001 // exceptions.
2002 return InputAt(2)->GetType() == Primitive::kPrimNot;
2003 }
2004
2005 uint32_t GetDexPc() const { return dex_pc_; }
2006
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002007 HInstruction* GetValue() const { return InputAt(2); }
2008
2009 Primitive::Type GetComponentType() const {
2010 // The Dex format does not type floating point index operations. Since the
2011 // `expected_component_type_` is set during building and can therefore not
2012 // be correct, we also check what is the value type. If it is a floating
2013 // point type, we must use that type.
2014 Primitive::Type value_type = GetValue()->GetType();
2015 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
2016 ? value_type
2017 : expected_component_type_;
2018 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002019
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002020 DECLARE_INSTRUCTION(ArraySet);
2021
2022 private:
2023 const uint32_t dex_pc_;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002024 const Primitive::Type expected_component_type_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002025
2026 DISALLOW_COPY_AND_ASSIGN(HArraySet);
2027};
2028
2029class HArrayLength : public HExpression<1> {
2030 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002031 explicit HArrayLength(HInstruction* array)
2032 : HExpression(Primitive::kPrimInt, SideEffects::None()) {
2033 // Note that arrays do not change length, so the instruction does not
2034 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002035 SetRawInputAt(0, array);
2036 }
2037
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002038 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002039 virtual bool InstructionDataEquals(HInstruction* other) const {
2040 UNUSED(other);
2041 return true;
2042 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002043
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002044 DECLARE_INSTRUCTION(ArrayLength);
2045
2046 private:
2047 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
2048};
2049
2050class HBoundsCheck : public HExpression<2> {
2051 public:
2052 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002053 : HExpression(index->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002054 DCHECK(index->GetType() == Primitive::kPrimInt);
2055 SetRawInputAt(0, index);
2056 SetRawInputAt(1, length);
2057 }
2058
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002059 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002060 virtual bool InstructionDataEquals(HInstruction* other) const {
2061 UNUSED(other);
2062 return true;
2063 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002064
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002065 virtual bool NeedsEnvironment() const { return true; }
2066
Roland Levillaine161a2a2014-10-03 12:45:18 +01002067 virtual bool CanThrow() const { return true; }
2068
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002069 uint32_t GetDexPc() const { return dex_pc_; }
2070
2071 DECLARE_INSTRUCTION(BoundsCheck);
2072
2073 private:
2074 const uint32_t dex_pc_;
2075
2076 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
2077};
2078
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002079/**
2080 * Some DEX instructions are folded into multiple HInstructions that need
2081 * to stay live until the last HInstruction. This class
2082 * is used as a marker for the baseline compiler to ensure its preceding
2083 * HInstruction stays live. `index` is the temporary number that is used
2084 * for knowing the stack offset where to store the instruction.
2085 */
2086class HTemporary : public HTemplateInstruction<0> {
2087 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002088 explicit HTemporary(size_t index) : HTemplateInstruction(SideEffects::None()), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002089
2090 size_t GetIndex() const { return index_; }
2091
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00002092 Primitive::Type GetType() const OVERRIDE { return GetPrevious()->GetType(); }
2093
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002094 DECLARE_INSTRUCTION(Temporary);
2095
2096 private:
2097 const size_t index_;
2098
2099 DISALLOW_COPY_AND_ASSIGN(HTemporary);
2100};
2101
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002102class HSuspendCheck : public HTemplateInstruction<0> {
2103 public:
2104 explicit HSuspendCheck(uint32_t dex_pc)
Nicolas Geoffray9ebc72c2014-09-25 16:33:42 +01002105 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002106
2107 virtual bool NeedsEnvironment() const {
2108 return true;
2109 }
2110
2111 uint32_t GetDexPc() const { return dex_pc_; }
2112
2113 DECLARE_INSTRUCTION(SuspendCheck);
2114
2115 private:
2116 const uint32_t dex_pc_;
2117
2118 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
2119};
2120
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002121/**
2122 * Instruction to load a Class object.
2123 */
2124class HLoadClass : public HExpression<0> {
2125 public:
2126 HLoadClass(uint16_t type_index,
2127 bool is_referrers_class,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002128 uint32_t dex_pc)
2129 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2130 type_index_(type_index),
2131 is_referrers_class_(is_referrers_class),
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002132 dex_pc_(dex_pc),
2133 generate_clinit_check_(false) {}
2134
2135 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002136
2137 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2138 return other->AsLoadClass()->type_index_ == type_index_;
2139 }
2140
2141 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
2142
2143 uint32_t GetDexPc() const { return dex_pc_; }
2144 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002145 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002146
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002147 bool NeedsEnvironment() const OVERRIDE {
2148 // Will call runtime and load the class if the class is not loaded yet.
2149 // TODO: finer grain decision.
2150 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002151 }
2152
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002153 bool MustGenerateClinitCheck() const {
2154 return generate_clinit_check_;
2155 }
2156
2157 void SetMustGenerateClinitCheck() {
2158 generate_clinit_check_ = true;
2159 }
2160
2161 bool CanCallRuntime() const {
2162 return MustGenerateClinitCheck() || !is_referrers_class_;
2163 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002164
2165 DECLARE_INSTRUCTION(LoadClass);
2166
2167 private:
2168 const uint16_t type_index_;
2169 const bool is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002170 const uint32_t dex_pc_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002171 // Whether this instruction must generate the initialization check.
2172 // Used for code generation.
2173 bool generate_clinit_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002174
2175 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
2176};
2177
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002178class HLoadString : public HExpression<0> {
2179 public:
2180 HLoadString(uint32_t string_index, uint32_t dex_pc)
2181 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2182 string_index_(string_index),
2183 dex_pc_(dex_pc) {}
2184
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002185 bool CanBeMoved() const OVERRIDE { return true; }
2186
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002187 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2188 return other->AsLoadString()->string_index_ == string_index_;
2189 }
2190
2191 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
2192
2193 uint32_t GetDexPc() const { return dex_pc_; }
2194 uint32_t GetStringIndex() const { return string_index_; }
2195
2196 // TODO: Can we deopt or debug when we resolve a string?
2197 bool NeedsEnvironment() const OVERRIDE { return false; }
2198
2199 DECLARE_INSTRUCTION(LoadString);
2200
2201 private:
2202 const uint32_t string_index_;
2203 const uint32_t dex_pc_;
2204
2205 DISALLOW_COPY_AND_ASSIGN(HLoadString);
2206};
2207
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002208// TODO: Pass this check to HInvokeStatic nodes.
2209/**
2210 * Performs an initialization check on its Class object input.
2211 */
2212class HClinitCheck : public HExpression<1> {
2213 public:
2214 explicit HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
2215 : HExpression(Primitive::kPrimNot, SideEffects::All()),
2216 dex_pc_(dex_pc) {
2217 SetRawInputAt(0, constant);
2218 }
2219
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002220 bool CanBeMoved() const OVERRIDE { return true; }
2221 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2222 UNUSED(other);
2223 return true;
2224 }
2225
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002226 bool NeedsEnvironment() const OVERRIDE {
2227 // May call runtime to initialize the class.
2228 return true;
2229 }
2230
2231 uint32_t GetDexPc() const { return dex_pc_; }
2232
2233 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
2234
2235 DECLARE_INSTRUCTION(ClinitCheck);
2236
2237 private:
2238 const uint32_t dex_pc_;
2239
2240 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
2241};
2242
2243class HStaticFieldGet : public HExpression<1> {
2244 public:
2245 HStaticFieldGet(HInstruction* cls,
2246 Primitive::Type field_type,
2247 MemberOffset field_offset)
2248 : HExpression(field_type, SideEffects::DependsOnSomething()),
2249 field_info_(field_offset, field_type) {
2250 SetRawInputAt(0, cls);
2251 }
2252
2253 bool CanBeMoved() const OVERRIDE { return true; }
2254 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2255 size_t other_offset = other->AsStaticFieldGet()->GetFieldOffset().SizeValue();
2256 return other_offset == GetFieldOffset().SizeValue();
2257 }
2258
2259 size_t ComputeHashCode() const OVERRIDE {
2260 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
2261 }
2262
2263 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
2264 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
2265
2266 DECLARE_INSTRUCTION(StaticFieldGet);
2267
2268 private:
2269 const FieldInfo field_info_;
2270
2271 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
2272};
2273
2274class HStaticFieldSet : public HTemplateInstruction<2> {
2275 public:
2276 HStaticFieldSet(HInstruction* cls,
2277 HInstruction* value,
2278 Primitive::Type field_type,
2279 MemberOffset field_offset)
2280 : HTemplateInstruction(SideEffects::ChangesSomething()),
2281 field_info_(field_offset, field_type) {
2282 SetRawInputAt(0, cls);
2283 SetRawInputAt(1, value);
2284 }
2285
2286 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
2287 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
2288
2289 DECLARE_INSTRUCTION(StaticFieldSet);
2290
2291 private:
2292 const FieldInfo field_info_;
2293
2294 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
2295};
2296
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002297// Implement the move-exception DEX instruction.
2298class HLoadException : public HExpression<0> {
2299 public:
2300 HLoadException() : HExpression(Primitive::kPrimNot, SideEffects::None()) {}
2301
2302 DECLARE_INSTRUCTION(LoadException);
2303
2304 private:
2305 DISALLOW_COPY_AND_ASSIGN(HLoadException);
2306};
2307
2308class HThrow : public HTemplateInstruction<1> {
2309 public:
2310 HThrow(HInstruction* exception, uint32_t dex_pc)
2311 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {
2312 SetRawInputAt(0, exception);
2313 }
2314
2315 bool IsControlFlow() const OVERRIDE { return true; }
2316
2317 bool NeedsEnvironment() const OVERRIDE { return true; }
2318
2319 uint32_t GetDexPc() const { return dex_pc_; }
2320
2321 DECLARE_INSTRUCTION(Throw);
2322
2323 private:
2324 uint32_t dex_pc_;
2325
2326 DISALLOW_COPY_AND_ASSIGN(HThrow);
2327};
2328
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002329class HTypeCheck : public HExpression<2> {
2330 public:
2331 explicit HTypeCheck(HInstruction* object,
2332 HLoadClass* constant,
2333 bool class_is_final,
2334 uint32_t dex_pc)
2335 : HExpression(Primitive::kPrimBoolean, SideEffects::None()),
2336 class_is_final_(class_is_final),
2337 dex_pc_(dex_pc) {
2338 SetRawInputAt(0, object);
2339 SetRawInputAt(1, constant);
2340 }
2341
2342 bool CanBeMoved() const OVERRIDE { return true; }
2343
2344 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2345 UNUSED(other);
2346 return true;
2347 }
2348
2349 bool NeedsEnvironment() const OVERRIDE {
2350 // TODO: Can we debug when doing a runtime instanceof check?
2351 return false;
2352 }
2353
2354 uint32_t GetDexPc() const { return dex_pc_; }
2355
2356 bool IsClassFinal() const { return class_is_final_; }
2357
2358 DECLARE_INSTRUCTION(TypeCheck);
2359
2360 private:
2361 const bool class_is_final_;
2362 const uint32_t dex_pc_;
2363
2364 DISALLOW_COPY_AND_ASSIGN(HTypeCheck);
2365};
2366
2367
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002368class MoveOperands : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002369 public:
Nicolas Geoffray740475d2014-09-29 10:33:25 +01002370 MoveOperands(Location source, Location destination, HInstruction* instruction)
2371 : source_(source), destination_(destination), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002372
2373 Location GetSource() const { return source_; }
2374 Location GetDestination() const { return destination_; }
2375
2376 void SetSource(Location value) { source_ = value; }
2377 void SetDestination(Location value) { destination_ = value; }
2378
2379 // The parallel move resolver marks moves as "in-progress" by clearing the
2380 // destination (but not the source).
2381 Location MarkPending() {
2382 DCHECK(!IsPending());
2383 Location dest = destination_;
2384 destination_ = Location::NoLocation();
2385 return dest;
2386 }
2387
2388 void ClearPending(Location dest) {
2389 DCHECK(IsPending());
2390 destination_ = dest;
2391 }
2392
2393 bool IsPending() const {
2394 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
2395 return destination_.IsInvalid() && !source_.IsInvalid();
2396 }
2397
2398 // True if this blocks a move from the given location.
2399 bool Blocks(Location loc) const {
2400 return !IsEliminated() && source_.Equals(loc);
2401 }
2402
2403 // A move is redundant if it's been eliminated, if its source and
2404 // destination are the same, or if its destination is unneeded.
2405 bool IsRedundant() const {
2406 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
2407 }
2408
2409 // We clear both operands to indicate move that's been eliminated.
2410 void Eliminate() {
2411 source_ = destination_ = Location::NoLocation();
2412 }
2413
2414 bool IsEliminated() const {
2415 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
2416 return source_.IsInvalid();
2417 }
2418
Nicolas Geoffray740475d2014-09-29 10:33:25 +01002419 HInstruction* GetInstruction() const { return instruction_; }
2420
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002421 private:
2422 Location source_;
2423 Location destination_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01002424 // The instruction this move is assocatied with. Null when this move is
2425 // for moving an input in the expected locations of user (including a phi user).
2426 // This is only used in debug mode, to ensure we do not connect interval siblings
2427 // in the same parallel move.
2428 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002429
2430 DISALLOW_COPY_AND_ASSIGN(MoveOperands);
2431};
2432
2433static constexpr size_t kDefaultNumberOfMoves = 4;
2434
2435class HParallelMove : public HTemplateInstruction<0> {
2436 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002437 explicit HParallelMove(ArenaAllocator* arena)
2438 : HTemplateInstruction(SideEffects::None()), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002439
2440 void AddMove(MoveOperands* move) {
Nicolas Geoffray740475d2014-09-29 10:33:25 +01002441 if (kIsDebugBuild && move->GetInstruction() != nullptr) {
2442 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
2443 DCHECK_NE(moves_.Get(i)->GetInstruction(), move->GetInstruction())
2444 << "Doing parallel moves for the same instruction.";
2445 }
2446 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002447 moves_.Add(move);
2448 }
2449
2450 MoveOperands* MoveOperandsAt(size_t index) const {
2451 return moves_.Get(index);
2452 }
2453
2454 size_t NumMoves() const { return moves_.Size(); }
2455
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002456 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002457
2458 private:
2459 GrowableArray<MoveOperands*> moves_;
2460
2461 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
2462};
2463
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002464class HGraphVisitor : public ValueObject {
2465 public:
Dave Allison20dfc792014-06-16 20:44:29 -07002466 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
2467 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002468
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002469 virtual void VisitInstruction(HInstruction* instruction) { UNUSED(instruction); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002470 virtual void VisitBasicBlock(HBasicBlock* block);
2471
Roland Levillain633021e2014-10-01 14:12:25 +01002472 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002473 void VisitInsertionOrder();
2474
Roland Levillain633021e2014-10-01 14:12:25 +01002475 // Visit the graph following dominator tree reverse post-order.
2476 void VisitReversePostOrder();
2477
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002478 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002479
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002480 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002481#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002482 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
2483
2484 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
2485
2486#undef DECLARE_VISIT_INSTRUCTION
2487
2488 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07002489 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002490
2491 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
2492};
2493
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002494class HGraphDelegateVisitor : public HGraphVisitor {
2495 public:
2496 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
2497 virtual ~HGraphDelegateVisitor() {}
2498
2499 // Visit functions that delegate to to super class.
2500#define DECLARE_VISIT_INSTRUCTION(name, super) \
2501 virtual void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
2502
2503 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
2504
2505#undef DECLARE_VISIT_INSTRUCTION
2506
2507 private:
2508 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
2509};
2510
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002511class HInsertionOrderIterator : public ValueObject {
2512 public:
2513 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
2514
2515 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
2516 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
2517 void Advance() { ++index_; }
2518
2519 private:
2520 const HGraph& graph_;
2521 size_t index_;
2522
2523 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
2524};
2525
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002526class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002527 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002528 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002529
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002530 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
2531 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002532 void Advance() { ++index_; }
2533
2534 private:
2535 const HGraph& graph_;
2536 size_t index_;
2537
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002538 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002539};
2540
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002541class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002542 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002543 explicit HPostOrderIterator(const HGraph& graph)
2544 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002545
2546 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002547 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002548 void Advance() { --index_; }
2549
2550 private:
2551 const HGraph& graph_;
2552 size_t index_;
2553
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002554 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002555};
2556
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002557} // namespace art
2558
2559#endif // ART_COMPILER_OPTIMIZING_NODES_H_