blob: 383d8bc09d7bdbfc50c579f1018dd49feeb34363 [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),
Calin Juravlef97f9fb2014-11-11 15:38:19 +000093 temporaries_vreg_slots_(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
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000132 void UpdateTemporariesVRegSlots(size_t slots) {
133 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100134 }
135
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000136 size_t GetTemporariesVRegSlots() const {
137 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100138 }
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
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000195 // Number of vreg size slots that the temporaries use (used in baseline compiler).
196 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100197
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) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000478 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000479 M(ArrayGet, Instruction) \
480 M(ArrayLength, Instruction) \
481 M(ArraySet, Instruction) \
482 M(BoundsCheck, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000483 M(CheckCast, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100484 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000485 M(Compare, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100486 M(Condition, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000487 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +0000488 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000489 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100490 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000491 M(Exit, Instruction) \
492 M(FloatConstant, Constant) \
493 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100494 M(GreaterThan, Condition) \
495 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100496 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000497 M(InstanceFieldGet, Instruction) \
498 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000499 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100500 M(IntConstant, Constant) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +0000501 M(InvokeInterface, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100502 M(InvokeStatic, Invoke) \
503 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000504 M(LessThan, Condition) \
505 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000506 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000507 M(LoadException, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100508 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000509 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100510 M(Local, Instruction) \
511 M(LongConstant, Constant) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +0000512 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000513 M(Mul, BinaryOperation) \
514 M(Neg, UnaryOperation) \
515 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100516 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +0100517 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000518 M(NotEqual, Condition) \
519 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000520 M(Or, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100521 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000522 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100523 M(Phi, Instruction) \
Calin Juravlebacfec32014-11-14 15:54:36 +0000524 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100525 M(Return, Instruction) \
526 M(ReturnVoid, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100527 M(StaticFieldGet, Instruction) \
528 M(StaticFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100529 M(StoreLocal, Instruction) \
530 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100531 M(SuspendCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000532 M(Temporary, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000533 M(Throw, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +0000534 M(TypeConversion, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000535 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000536
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100537#define FOR_EACH_INSTRUCTION(M) \
538 FOR_EACH_CONCRETE_INSTRUCTION(M) \
539 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +0100540 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +0100541 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100542 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -0700543
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100544#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000545FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
546#undef FORWARD_DECLARATION
547
Roland Levillainccc07a92014-09-16 14:48:16 +0100548#define DECLARE_INSTRUCTION(type) \
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100549 virtual InstructionKind GetKind() const { return k##type; } \
Roland Levillainccc07a92014-09-16 14:48:16 +0100550 virtual const char* DebugName() const { return #type; } \
551 virtual const H##type* As##type() const OVERRIDE { return this; } \
552 virtual H##type* As##type() OVERRIDE { return this; } \
553 virtual bool InstructionTypeEquals(HInstruction* other) const { \
554 return other->Is##type(); \
555 } \
556 virtual void Accept(HGraphVisitor* visitor)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000557
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100558template <typename T>
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700559class HUseListNode : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000560 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100561 HUseListNode(T* user, size_t index, HUseListNode* tail)
Dave Allison20dfc792014-06-16 20:44:29 -0700562 : user_(user), index_(index), tail_(tail) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000563
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000564 HUseListNode* GetTail() const { return tail_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100565 T* GetUser() const { return user_; }
566 size_t GetIndex() const { return index_; }
567
568 void SetTail(HUseListNode<T>* node) { tail_ = node; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000569
570 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100571 T* const user_;
572 const size_t index_;
573 HUseListNode<T>* tail_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000574
575 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
576};
577
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100578// Represents the side effects an instruction may have.
579class SideEffects : public ValueObject {
580 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100581 SideEffects() : flags_(0) {}
582
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100583 static SideEffects None() {
584 return SideEffects(0);
585 }
586
587 static SideEffects All() {
588 return SideEffects(ChangesSomething().flags_ | DependsOnSomething().flags_);
589 }
590
591 static SideEffects ChangesSomething() {
592 return SideEffects((1 << kFlagChangesCount) - 1);
593 }
594
595 static SideEffects DependsOnSomething() {
596 int count = kFlagDependsOnCount - kFlagChangesCount;
597 return SideEffects(((1 << count) - 1) << kFlagChangesCount);
598 }
599
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100600 SideEffects Union(SideEffects other) const {
601 return SideEffects(flags_ | other.flags_);
602 }
603
Roland Levillain72bceff2014-09-15 18:29:00 +0100604 bool HasSideEffects() const {
605 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
606 return (flags_ & all_bits_set) != 0;
607 }
608
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100609 bool HasAllSideEffects() const {
610 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
611 return all_bits_set == (flags_ & all_bits_set);
612 }
613
614 bool DependsOn(SideEffects other) const {
615 size_t depends_flags = other.ComputeDependsFlags();
616 return (flags_ & depends_flags) != 0;
617 }
618
619 bool HasDependencies() const {
620 int count = kFlagDependsOnCount - kFlagChangesCount;
621 size_t all_bits_set = (1 << count) - 1;
622 return ((flags_ >> kFlagChangesCount) & all_bits_set) != 0;
623 }
624
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100625 private:
626 static constexpr int kFlagChangesSomething = 0;
627 static constexpr int kFlagChangesCount = kFlagChangesSomething + 1;
628
629 static constexpr int kFlagDependsOnSomething = kFlagChangesCount;
630 static constexpr int kFlagDependsOnCount = kFlagDependsOnSomething + 1;
631
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100632 explicit SideEffects(size_t flags) : flags_(flags) {}
633
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100634 size_t ComputeDependsFlags() const {
635 return flags_ << kFlagChangesCount;
636 }
637
638 size_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100639};
640
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700641class HInstruction : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000642 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100643 explicit HInstruction(SideEffects side_effects)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000644 : previous_(nullptr),
645 next_(nullptr),
646 block_(nullptr),
647 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100648 ssa_index_(-1),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000649 uses_(nullptr),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100650 env_uses_(nullptr),
651 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100652 locations_(nullptr),
653 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100654 lifetime_position_(kNoLifetime),
655 side_effects_(side_effects) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000656
Dave Allison20dfc792014-06-16 20:44:29 -0700657 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000658
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100659#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100660 enum InstructionKind {
661 FOR_EACH_INSTRUCTION(DECLARE_KIND)
662 };
663#undef DECLARE_KIND
664
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000665 HInstruction* GetNext() const { return next_; }
666 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000667
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000668 HBasicBlock* GetBlock() const { return block_; }
669 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100670 bool IsInBlock() const { return block_ != nullptr; }
671 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +0100672 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000673
Roland Levillain6b879dd2014-09-22 17:13:44 +0100674 virtual size_t InputCount() const = 0;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100675 virtual HInstruction* InputAt(size_t i) const = 0;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000676
677 virtual void Accept(HGraphVisitor* visitor) = 0;
678 virtual const char* DebugName() const = 0;
679
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100680 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100681 virtual void SetRawInputAt(size_t index, HInstruction* input) = 0;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100682
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100683 virtual bool NeedsEnvironment() const { return false; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100684 virtual bool IsControlFlow() const { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +0100685 virtual bool CanThrow() const { return false; }
Roland Levillain72bceff2014-09-15 18:29:00 +0100686 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100687
688 void AddUseAt(HInstruction* user, size_t index) {
689 uses_ = new (block_->GetGraph()->GetArena()) HUseListNode<HInstruction>(user, index, uses_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000690 }
691
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100692 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +0100693 DCHECK(user != nullptr);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100694 env_uses_ = new (block_->GetGraph()->GetArena()) HUseListNode<HEnvironment>(
695 user, index, env_uses_);
696 }
697
698 void RemoveUser(HInstruction* user, size_t index);
Nicolas Geoffray724c9632014-09-22 12:27:27 +0100699 void RemoveEnvironmentUser(HEnvironment* user, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100700
701 HUseListNode<HInstruction>* GetUses() const { return uses_; }
702 HUseListNode<HEnvironment>* GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000703
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100704 bool HasUses() const { return uses_ != nullptr || env_uses_ != nullptr; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100705 bool HasEnvironmentUses() const { return env_uses_ != nullptr; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000706
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100707 size_t NumberOfUses() const {
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100708 // TODO: Optimize this method if it is used outside of the HGraphVisualizer.
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100709 size_t result = 0;
710 HUseListNode<HInstruction>* current = uses_;
711 while (current != nullptr) {
712 current = current->GetTail();
713 ++result;
714 }
715 return result;
716 }
717
Roland Levillain6c82d402014-10-13 16:10:27 +0100718 // Does this instruction strictly dominate `other_instruction`?
719 // Returns false if this instruction and `other_instruction` are the same.
720 // Aborts if this instruction and `other_instruction` are both phis.
721 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +0100722
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000723 int GetId() const { return id_; }
724 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000725
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100726 int GetSsaIndex() const { return ssa_index_; }
727 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
728 bool HasSsaIndex() const { return ssa_index_ != -1; }
729
730 bool HasEnvironment() const { return environment_ != nullptr; }
731 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100732 void SetEnvironment(HEnvironment* environment) { environment_ = environment; }
733
Nicolas Geoffray39468442014-09-02 15:17:15 +0100734 // Returns the number of entries in the environment. Typically, that is the
735 // number of dex registers in a method. It could be more in case of inlining.
736 size_t EnvironmentSize() const;
737
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000738 LocationSummary* GetLocations() const { return locations_; }
739 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000740
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100741 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100742 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100743
Dave Allison20dfc792014-06-16 20:44:29 -0700744 bool HasOnlyOneUse() const {
745 return uses_ != nullptr && uses_->GetTail() == nullptr;
746 }
747
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100748#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +0100749 bool Is##type() const { return (As##type() != nullptr); } \
750 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000751 virtual H##type* As##type() { return nullptr; }
752
753 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
754#undef INSTRUCTION_TYPE_CHECK
755
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100756 // Returns whether the instruction can be moved within the graph.
757 virtual bool CanBeMoved() const { return false; }
758
759 // Returns whether the two instructions are of the same kind.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700760 virtual bool InstructionTypeEquals(HInstruction* other) const {
761 UNUSED(other);
762 return false;
763 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100764
765 // Returns whether any data encoded in the two instructions is equal.
766 // This method does not look at the inputs. Both instructions must be
767 // of the same type, otherwise the method has undefined behavior.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700768 virtual bool InstructionDataEquals(HInstruction* other) const {
769 UNUSED(other);
770 return false;
771 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100772
773 // Returns whether two instructions are equal, that is:
774 // 1) They have the same type and contain the same data,
775 // 2) Their inputs are identical.
776 bool Equals(HInstruction* other) const;
777
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100778 virtual InstructionKind GetKind() const = 0;
779
780 virtual size_t ComputeHashCode() const {
781 size_t result = GetKind();
782 for (size_t i = 0, e = InputCount(); i < e; ++i) {
783 result = (result * 31) + InputAt(i)->GetId();
784 }
785 return result;
786 }
787
788 SideEffects GetSideEffects() const { return side_effects_; }
789
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100790 size_t GetLifetimePosition() const { return lifetime_position_; }
791 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
792 LiveInterval* GetLiveInterval() const { return live_interval_; }
793 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
794 bool HasLiveInterval() const { return live_interval_ != nullptr; }
795
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000796 private:
797 HInstruction* previous_;
798 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000799 HBasicBlock* block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000800
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000801 // An instruction gets an id when it is added to the graph.
802 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +0100803 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000804 int id_;
805
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100806 // When doing liveness analysis, instructions that have uses get an SSA index.
807 int ssa_index_;
808
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100809 // List of instructions that have this instruction as input.
810 HUseListNode<HInstruction>* uses_;
811
812 // List of environments that contain this instruction.
813 HUseListNode<HEnvironment>* env_uses_;
814
Nicolas Geoffray39468442014-09-02 15:17:15 +0100815 // The environment associated with this instruction. Not null if the instruction
816 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100817 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000818
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000819 // Set by the code generator.
820 LocationSummary* locations_;
821
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100822 // Set by the liveness analysis.
823 LiveInterval* live_interval_;
824
825 // Set by the liveness analysis, this is the position in a linear
826 // order of blocks where this instruction's live interval start.
827 size_t lifetime_position_;
828
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100829 const SideEffects side_effects_;
830
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000831 friend class HBasicBlock;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100832 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000833
834 DISALLOW_COPY_AND_ASSIGN(HInstruction);
835};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700836std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000837
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100838template<typename T>
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000839class HUseIterator : public ValueObject {
840 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100841 explicit HUseIterator(HUseListNode<T>* uses) : current_(uses) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000842
843 bool Done() const { return current_ == nullptr; }
844
845 void Advance() {
846 DCHECK(!Done());
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000847 current_ = current_->GetTail();
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000848 }
849
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100850 HUseListNode<T>* Current() const {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000851 DCHECK(!Done());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100852 return current_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000853 }
854
855 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100856 HUseListNode<T>* current_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000857
858 friend class HValue;
859};
860
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100861// A HEnvironment object contains the values of virtual registers at a given location.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700862class HEnvironment : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100863 public:
864 HEnvironment(ArenaAllocator* arena, size_t number_of_vregs) : vregs_(arena, number_of_vregs) {
865 vregs_.SetSize(number_of_vregs);
866 for (size_t i = 0; i < number_of_vregs; i++) {
867 vregs_.Put(i, nullptr);
868 }
869 }
870
871 void Populate(const GrowableArray<HInstruction*>& env) {
872 for (size_t i = 0; i < env.Size(); i++) {
873 HInstruction* instruction = env.Get(i);
874 vregs_.Put(i, instruction);
875 if (instruction != nullptr) {
876 instruction->AddEnvUseAt(this, i);
877 }
878 }
879 }
880
881 void SetRawEnvAt(size_t index, HInstruction* instruction) {
882 vregs_.Put(index, instruction);
883 }
884
Nicolas Geoffray39468442014-09-02 15:17:15 +0100885 HInstruction* GetInstructionAt(size_t index) const {
886 return vregs_.Get(index);
887 }
888
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100889 GrowableArray<HInstruction*>* GetVRegs() {
890 return &vregs_;
891 }
892
Nicolas Geoffray39468442014-09-02 15:17:15 +0100893 size_t Size() const { return vregs_.Size(); }
894
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100895 private:
896 GrowableArray<HInstruction*> vregs_;
897
898 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
899};
900
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000901class HInputIterator : public ValueObject {
902 public:
Dave Allison20dfc792014-06-16 20:44:29 -0700903 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000904
905 bool Done() const { return index_ == instruction_->InputCount(); }
906 HInstruction* Current() const { return instruction_->InputAt(index_); }
907 void Advance() { index_++; }
908
909 private:
910 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100911 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000912
913 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
914};
915
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000916class HInstructionIterator : public ValueObject {
917 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100918 explicit HInstructionIterator(const HInstructionList& instructions)
919 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000920 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000921 }
922
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000923 bool Done() const { return instruction_ == nullptr; }
924 HInstruction* Current() const { return instruction_; }
925 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000926 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000927 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000928 }
929
930 private:
931 HInstruction* instruction_;
932 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100933
934 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000935};
936
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100937class HBackwardInstructionIterator : public ValueObject {
938 public:
939 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
940 : instruction_(instructions.last_instruction_) {
941 next_ = Done() ? nullptr : instruction_->GetPrevious();
942 }
943
944 bool Done() const { return instruction_ == nullptr; }
945 HInstruction* Current() const { return instruction_; }
946 void Advance() {
947 instruction_ = next_;
948 next_ = Done() ? nullptr : instruction_->GetPrevious();
949 }
950
951 private:
952 HInstruction* instruction_;
953 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100954
955 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100956};
957
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000958// An embedded container with N elements of type T. Used (with partial
959// specialization for N=0) because embedded arrays cannot have size 0.
960template<typename T, intptr_t N>
961class EmbeddedArray {
962 public:
Dave Allison20dfc792014-06-16 20:44:29 -0700963 EmbeddedArray() : elements_() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000964
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000965 intptr_t GetLength() const { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000966
967 const T& operator[](intptr_t i) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000968 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000969 return elements_[i];
970 }
971
972 T& operator[](intptr_t i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000973 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000974 return elements_[i];
975 }
976
977 const T& At(intptr_t i) const {
978 return (*this)[i];
979 }
980
981 void SetAt(intptr_t i, const T& val) {
982 (*this)[i] = val;
983 }
984
985 private:
986 T elements_[N];
987};
988
989template<typename T>
990class EmbeddedArray<T, 0> {
991 public:
992 intptr_t length() const { return 0; }
993 const T& operator[](intptr_t i) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700994 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000995 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700996 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000997 }
998 T& operator[](intptr_t i) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700999 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001000 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001001 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001002 }
1003};
1004
1005template<intptr_t N>
1006class HTemplateInstruction: public HInstruction {
1007 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001008 HTemplateInstruction<N>(SideEffects side_effects)
1009 : HInstruction(side_effects), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07001010 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001011
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001012 virtual size_t InputCount() const { return N; }
1013 virtual HInstruction* InputAt(size_t i) const { return inputs_[i]; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001014
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001015 protected:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001016 virtual void SetRawInputAt(size_t i, HInstruction* instruction) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001017 inputs_[i] = instruction;
1018 }
1019
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001020 private:
1021 EmbeddedArray<HInstruction*, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001022
1023 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001024};
1025
Dave Allison20dfc792014-06-16 20:44:29 -07001026template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001027class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07001028 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001029 HExpression<N>(Primitive::Type type, SideEffects side_effects)
1030 : HTemplateInstruction<N>(side_effects), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07001031 virtual ~HExpression() {}
1032
1033 virtual Primitive::Type GetType() const { return type_; }
1034
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001035 protected:
1036 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07001037};
1038
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001039// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
1040// instruction that branches to the exit block.
1041class HReturnVoid : public HTemplateInstruction<0> {
1042 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001043 HReturnVoid() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001044
1045 virtual bool IsControlFlow() const { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001046
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001047 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001048
1049 private:
1050 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
1051};
1052
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001053// Represents dex's RETURN opcodes. A HReturn is a control flow
1054// instruction that branches to the exit block.
1055class HReturn : public HTemplateInstruction<1> {
1056 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001057 explicit HReturn(HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001058 SetRawInputAt(0, value);
1059 }
1060
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001061 virtual bool IsControlFlow() const { return true; }
1062
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001063 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001064
1065 private:
1066 DISALLOW_COPY_AND_ASSIGN(HReturn);
1067};
1068
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001069// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001070// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001071// exit block.
1072class HExit : public HTemplateInstruction<0> {
1073 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001074 HExit() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001075
1076 virtual bool IsControlFlow() const { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001077
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001078 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001079
1080 private:
1081 DISALLOW_COPY_AND_ASSIGN(HExit);
1082};
1083
1084// Jumps from one block to another.
1085class HGoto : public HTemplateInstruction<0> {
1086 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001087 HGoto() : HTemplateInstruction(SideEffects::None()) {}
1088
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001089 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001090
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001091 HBasicBlock* GetSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001092 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001093 }
1094
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001095 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001096
1097 private:
1098 DISALLOW_COPY_AND_ASSIGN(HGoto);
1099};
1100
Dave Allison20dfc792014-06-16 20:44:29 -07001101
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001102// Conditional branch. A block ending with an HIf instruction must have
1103// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001104class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001105 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001106 explicit HIf(HInstruction* input) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001107 SetRawInputAt(0, input);
1108 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001109
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001110 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001111
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001112 HBasicBlock* IfTrueSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001113 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001114 }
1115
1116 HBasicBlock* IfFalseSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001117 return GetBlock()->GetSuccessors().Get(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001118 }
1119
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001120 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001121
Dave Allison20dfc792014-06-16 20:44:29 -07001122 virtual bool IsIfInstruction() const { return true; }
1123
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001124 private:
1125 DISALLOW_COPY_AND_ASSIGN(HIf);
1126};
1127
Roland Levillain88cb1752014-10-20 16:36:47 +01001128class HUnaryOperation : public HExpression<1> {
1129 public:
1130 HUnaryOperation(Primitive::Type result_type, HInstruction* input)
1131 : HExpression(result_type, SideEffects::None()) {
1132 SetRawInputAt(0, input);
1133 }
1134
1135 HInstruction* GetInput() const { return InputAt(0); }
1136 Primitive::Type GetResultType() const { return GetType(); }
1137
1138 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001139 virtual bool InstructionDataEquals(HInstruction* other) const {
1140 UNUSED(other);
1141 return true;
1142 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001143
Roland Levillain9240d6a2014-10-20 16:47:04 +01001144 // Try to statically evaluate `operation` and return a HConstant
1145 // containing the result of this evaluation. If `operation` cannot
1146 // be evaluated as a constant, return nullptr.
1147 HConstant* TryStaticEvaluation() const;
1148
1149 // Apply this operation to `x`.
1150 virtual int32_t Evaluate(int32_t x) const = 0;
1151 virtual int64_t Evaluate(int64_t x) const = 0;
1152
Roland Levillain88cb1752014-10-20 16:36:47 +01001153 DECLARE_INSTRUCTION(UnaryOperation);
1154
1155 private:
1156 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
1157};
1158
Dave Allison20dfc792014-06-16 20:44:29 -07001159class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001160 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001161 HBinaryOperation(Primitive::Type result_type,
1162 HInstruction* left,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001163 HInstruction* right) : HExpression(result_type, SideEffects::None()) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001164 SetRawInputAt(0, left);
1165 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001166 }
1167
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001168 HInstruction* GetLeft() const { return InputAt(0); }
1169 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07001170 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001171
1172 virtual bool IsCommutative() { return false; }
1173
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001174 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001175 virtual bool InstructionDataEquals(HInstruction* other) const {
1176 UNUSED(other);
1177 return true;
1178 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001179
Roland Levillain9240d6a2014-10-20 16:47:04 +01001180 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01001181 // containing the result of this evaluation. If `operation` cannot
1182 // be evaluated as a constant, return nullptr.
Roland Levillain9240d6a2014-10-20 16:47:04 +01001183 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01001184
1185 // Apply this operation to `x` and `y`.
1186 virtual int32_t Evaluate(int32_t x, int32_t y) const = 0;
1187 virtual int64_t Evaluate(int64_t x, int64_t y) const = 0;
1188
Roland Levillainccc07a92014-09-16 14:48:16 +01001189 DECLARE_INSTRUCTION(BinaryOperation);
1190
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001191 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001192 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
1193};
1194
Dave Allison20dfc792014-06-16 20:44:29 -07001195class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001196 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001197 HCondition(HInstruction* first, HInstruction* second)
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001198 : HBinaryOperation(Primitive::kPrimBoolean, first, second),
1199 needs_materialization_(true) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001200
1201 virtual bool IsCommutative() { return true; }
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001202
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001203 bool NeedsMaterialization() const { return needs_materialization_; }
1204 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001205
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001206 // For code generation purposes, returns whether this instruction is just before
1207 // `if_`, and disregard moves in between.
1208 bool IsBeforeWhenDisregardMoves(HIf* if_) const;
1209
Dave Allison20dfc792014-06-16 20:44:29 -07001210 DECLARE_INSTRUCTION(Condition);
1211
1212 virtual IfCondition GetCondition() const = 0;
1213
1214 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001215 // For register allocation purposes, returns whether this instruction needs to be
1216 // materialized (that is, not just be in the processor flags).
1217 bool needs_materialization_;
1218
Dave Allison20dfc792014-06-16 20:44:29 -07001219 DISALLOW_COPY_AND_ASSIGN(HCondition);
1220};
1221
1222// Instruction to check if two inputs are equal to each other.
1223class HEqual : public HCondition {
1224 public:
1225 HEqual(HInstruction* first, HInstruction* second)
1226 : HCondition(first, second) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001227
Roland Levillain93445682014-10-06 19:24:02 +01001228 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1229 return x == y ? 1 : 0;
1230 }
1231 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1232 return x == y ? 1 : 0;
1233 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001234
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001235 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001236
Dave Allison20dfc792014-06-16 20:44:29 -07001237 virtual IfCondition GetCondition() const {
1238 return kCondEQ;
1239 }
1240
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001241 private:
1242 DISALLOW_COPY_AND_ASSIGN(HEqual);
1243};
1244
Dave Allison20dfc792014-06-16 20:44:29 -07001245class HNotEqual : public HCondition {
1246 public:
1247 HNotEqual(HInstruction* first, HInstruction* second)
1248 : HCondition(first, second) {}
1249
Roland Levillain93445682014-10-06 19:24:02 +01001250 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1251 return x != y ? 1 : 0;
1252 }
1253 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1254 return x != y ? 1 : 0;
1255 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001256
Dave Allison20dfc792014-06-16 20:44:29 -07001257 DECLARE_INSTRUCTION(NotEqual);
1258
1259 virtual IfCondition GetCondition() const {
1260 return kCondNE;
1261 }
1262
1263 private:
1264 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
1265};
1266
1267class HLessThan : public HCondition {
1268 public:
1269 HLessThan(HInstruction* first, HInstruction* second)
1270 : HCondition(first, second) {}
1271
Roland Levillain93445682014-10-06 19:24:02 +01001272 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1273 return x < y ? 1 : 0;
1274 }
1275 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1276 return x < y ? 1 : 0;
1277 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001278
Dave Allison20dfc792014-06-16 20:44:29 -07001279 DECLARE_INSTRUCTION(LessThan);
1280
1281 virtual IfCondition GetCondition() const {
1282 return kCondLT;
1283 }
1284
1285 private:
1286 DISALLOW_COPY_AND_ASSIGN(HLessThan);
1287};
1288
1289class HLessThanOrEqual : public HCondition {
1290 public:
1291 HLessThanOrEqual(HInstruction* first, HInstruction* second)
1292 : HCondition(first, second) {}
1293
Roland Levillain93445682014-10-06 19:24:02 +01001294 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1295 return x <= y ? 1 : 0;
1296 }
1297 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1298 return x <= y ? 1 : 0;
1299 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001300
Dave Allison20dfc792014-06-16 20:44:29 -07001301 DECLARE_INSTRUCTION(LessThanOrEqual);
1302
1303 virtual IfCondition GetCondition() const {
1304 return kCondLE;
1305 }
1306
1307 private:
1308 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
1309};
1310
1311class HGreaterThan : public HCondition {
1312 public:
1313 HGreaterThan(HInstruction* first, HInstruction* second)
1314 : HCondition(first, second) {}
1315
Roland Levillain93445682014-10-06 19:24:02 +01001316 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1317 return x > y ? 1 : 0;
1318 }
1319 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1320 return x > y ? 1 : 0;
1321 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001322
Dave Allison20dfc792014-06-16 20:44:29 -07001323 DECLARE_INSTRUCTION(GreaterThan);
1324
1325 virtual IfCondition GetCondition() const {
1326 return kCondGT;
1327 }
1328
1329 private:
1330 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
1331};
1332
1333class HGreaterThanOrEqual : public HCondition {
1334 public:
1335 HGreaterThanOrEqual(HInstruction* first, HInstruction* second)
1336 : HCondition(first, second) {}
1337
Roland Levillain93445682014-10-06 19:24:02 +01001338 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1339 return x >= y ? 1 : 0;
1340 }
1341 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1342 return x >= y ? 1 : 0;
1343 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001344
Dave Allison20dfc792014-06-16 20:44:29 -07001345 DECLARE_INSTRUCTION(GreaterThanOrEqual);
1346
1347 virtual IfCondition GetCondition() const {
1348 return kCondGE;
1349 }
1350
1351 private:
1352 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
1353};
1354
1355
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001356// Instruction to check how two inputs compare to each other.
1357// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
1358class HCompare : public HBinaryOperation {
1359 public:
1360 HCompare(Primitive::Type type, HInstruction* first, HInstruction* second)
1361 : HBinaryOperation(Primitive::kPrimInt, first, second) {
1362 DCHECK_EQ(type, first->GetType());
1363 DCHECK_EQ(type, second->GetType());
1364 }
1365
Roland Levillain93445682014-10-06 19:24:02 +01001366 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001367 return
1368 x == y ? 0 :
1369 x > y ? 1 :
1370 -1;
1371 }
Roland Levillain93445682014-10-06 19:24:02 +01001372 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001373 return
1374 x == y ? 0 :
1375 x > y ? 1 :
1376 -1;
1377 }
1378
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001379 DECLARE_INSTRUCTION(Compare);
1380
1381 private:
1382 DISALLOW_COPY_AND_ASSIGN(HCompare);
1383};
1384
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001385// A local in the graph. Corresponds to a Dex register.
1386class HLocal : public HTemplateInstruction<0> {
1387 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001388 explicit HLocal(uint16_t reg_number)
1389 : HTemplateInstruction(SideEffects::None()), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001390
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001391 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001392
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001393 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001394
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001395 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001396 // The Dex register number.
1397 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001398
1399 DISALLOW_COPY_AND_ASSIGN(HLocal);
1400};
1401
1402// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07001403class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001404 public:
Roland Levillain5799fc02014-09-25 12:15:20 +01001405 HLoadLocal(HLocal* local, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001406 : HExpression(type, SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001407 SetRawInputAt(0, local);
1408 }
1409
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001410 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1411
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001412 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001413
1414 private:
1415 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
1416};
1417
1418// Store a value in a given local. This instruction has two inputs: the value
1419// and the local.
1420class HStoreLocal : public HTemplateInstruction<2> {
1421 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001422 HStoreLocal(HLocal* local, HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001423 SetRawInputAt(0, local);
1424 SetRawInputAt(1, value);
1425 }
1426
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001427 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1428
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001429 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001430
1431 private:
1432 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
1433};
1434
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001435class HConstant : public HExpression<0> {
1436 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001437 explicit HConstant(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
1438
1439 virtual bool CanBeMoved() const { return true; }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001440
1441 DECLARE_INSTRUCTION(Constant);
1442
1443 private:
1444 DISALLOW_COPY_AND_ASSIGN(HConstant);
1445};
1446
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001447class HFloatConstant : public HConstant {
1448 public:
1449 explicit HFloatConstant(float value) : HConstant(Primitive::kPrimFloat), value_(value) {}
1450
1451 float GetValue() const { return value_; }
1452
1453 virtual bool InstructionDataEquals(HInstruction* other) const {
1454 return bit_cast<float, int32_t>(other->AsFloatConstant()->value_) ==
1455 bit_cast<float, int32_t>(value_);
1456 }
1457
1458 virtual size_t ComputeHashCode() const { return static_cast<size_t>(GetValue()); }
1459
1460 DECLARE_INSTRUCTION(FloatConstant);
1461
1462 private:
1463 const float value_;
1464
1465 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
1466};
1467
1468class HDoubleConstant : public HConstant {
1469 public:
1470 explicit HDoubleConstant(double value) : HConstant(Primitive::kPrimDouble), value_(value) {}
1471
1472 double GetValue() const { return value_; }
1473
1474 virtual bool InstructionDataEquals(HInstruction* other) const {
1475 return bit_cast<double, int64_t>(other->AsDoubleConstant()->value_) ==
1476 bit_cast<double, int64_t>(value_);
1477 }
1478
1479 virtual size_t ComputeHashCode() const { return static_cast<size_t>(GetValue()); }
1480
1481 DECLARE_INSTRUCTION(DoubleConstant);
1482
1483 private:
1484 const double value_;
1485
1486 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
1487};
1488
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001489// Constants of the type int. Those can be from Dex instructions, or
1490// synthesized (for example with the if-eqz instruction).
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001491class HIntConstant : public HConstant {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001492 public:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001493 explicit HIntConstant(int32_t value) : HConstant(Primitive::kPrimInt), value_(value) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001494
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001495 int32_t GetValue() const { return value_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001496
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001497 virtual bool InstructionDataEquals(HInstruction* other) const {
1498 return other->AsIntConstant()->value_ == value_;
1499 }
1500
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001501 virtual size_t ComputeHashCode() const { return GetValue(); }
1502
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001503 DECLARE_INSTRUCTION(IntConstant);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001504
1505 private:
1506 const int32_t value_;
1507
1508 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
1509};
1510
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001511class HLongConstant : public HConstant {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001512 public:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001513 explicit HLongConstant(int64_t value) : HConstant(Primitive::kPrimLong), value_(value) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001514
1515 int64_t GetValue() const { return value_; }
1516
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001517 virtual bool InstructionDataEquals(HInstruction* other) const {
1518 return other->AsLongConstant()->value_ == value_;
1519 }
1520
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001521 virtual size_t ComputeHashCode() const { return static_cast<size_t>(GetValue()); }
1522
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001523 DECLARE_INSTRUCTION(LongConstant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001524
1525 private:
1526 const int64_t value_;
1527
1528 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
1529};
1530
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001531class HInvoke : public HInstruction {
1532 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001533 HInvoke(ArenaAllocator* arena,
1534 uint32_t number_of_arguments,
1535 Primitive::Type return_type,
1536 uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001537 : HInstruction(SideEffects::All()),
1538 inputs_(arena, number_of_arguments),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001539 return_type_(return_type),
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001540 dex_pc_(dex_pc) {
1541 inputs_.SetSize(number_of_arguments);
1542 }
1543
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001544 virtual size_t InputCount() const { return inputs_.Size(); }
1545 virtual HInstruction* InputAt(size_t i) const { return inputs_.Get(i); }
1546
1547 // Runtime needs to walk the stack, so Dex -> Dex calls need to
1548 // know their environment.
1549 virtual bool NeedsEnvironment() const { return true; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001550
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001551 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001552 SetRawInputAt(index, argument);
1553 }
1554
1555 virtual void SetRawInputAt(size_t index, HInstruction* input) {
1556 inputs_.Put(index, input);
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001557 }
1558
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001559 virtual Primitive::Type GetType() const { return return_type_; }
1560
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001561 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001562
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001563 DECLARE_INSTRUCTION(Invoke);
1564
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001565 protected:
1566 GrowableArray<HInstruction*> inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001567 const Primitive::Type return_type_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001568 const uint32_t dex_pc_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001569
1570 private:
1571 DISALLOW_COPY_AND_ASSIGN(HInvoke);
1572};
1573
1574class HInvokeStatic : public HInvoke {
1575 public:
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001576 HInvokeStatic(ArenaAllocator* arena,
1577 uint32_t number_of_arguments,
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001578 Primitive::Type return_type,
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001579 uint32_t dex_pc,
1580 uint32_t index_in_dex_cache)
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001581 : HInvoke(arena, number_of_arguments, return_type, dex_pc),
1582 index_in_dex_cache_(index_in_dex_cache) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001583
1584 uint32_t GetIndexInDexCache() const { return index_in_dex_cache_; }
1585
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001586 DECLARE_INSTRUCTION(InvokeStatic);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001587
1588 private:
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001589 const uint32_t index_in_dex_cache_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001590
1591 DISALLOW_COPY_AND_ASSIGN(HInvokeStatic);
1592};
1593
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001594class HInvokeVirtual : public HInvoke {
1595 public:
1596 HInvokeVirtual(ArenaAllocator* arena,
1597 uint32_t number_of_arguments,
1598 Primitive::Type return_type,
1599 uint32_t dex_pc,
1600 uint32_t vtable_index)
1601 : HInvoke(arena, number_of_arguments, return_type, dex_pc),
1602 vtable_index_(vtable_index) {}
1603
1604 uint32_t GetVTableIndex() const { return vtable_index_; }
1605
1606 DECLARE_INSTRUCTION(InvokeVirtual);
1607
1608 private:
1609 const uint32_t vtable_index_;
1610
1611 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
1612};
1613
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001614class HInvokeInterface : public HInvoke {
1615 public:
1616 HInvokeInterface(ArenaAllocator* arena,
1617 uint32_t number_of_arguments,
1618 Primitive::Type return_type,
1619 uint32_t dex_pc,
1620 uint32_t dex_method_index,
1621 uint32_t imt_index)
1622 : HInvoke(arena, number_of_arguments, return_type, dex_pc),
1623 dex_method_index_(dex_method_index),
1624 imt_index_(imt_index) {}
1625
1626 uint32_t GetImtIndex() const { return imt_index_; }
1627 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
1628
1629 DECLARE_INSTRUCTION(InvokeInterface);
1630
1631 private:
1632 const uint32_t dex_method_index_;
1633 const uint32_t imt_index_;
1634
1635 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
1636};
1637
Dave Allison20dfc792014-06-16 20:44:29 -07001638class HNewInstance : public HExpression<0> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001639 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001640 HNewInstance(uint32_t dex_pc, uint16_t type_index)
1641 : HExpression(Primitive::kPrimNot, SideEffects::None()),
1642 dex_pc_(dex_pc),
1643 type_index_(type_index) {}
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001644
1645 uint32_t GetDexPc() const { return dex_pc_; }
1646 uint16_t GetTypeIndex() const { return type_index_; }
1647
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001648 // Calls runtime so needs an environment.
1649 virtual bool NeedsEnvironment() const { return true; }
1650
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001651 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001652
1653 private:
1654 const uint32_t dex_pc_;
1655 const uint16_t type_index_;
1656
1657 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
1658};
1659
Roland Levillain88cb1752014-10-20 16:36:47 +01001660class HNeg : public HUnaryOperation {
1661 public:
1662 explicit HNeg(Primitive::Type result_type, HInstruction* input)
1663 : HUnaryOperation(result_type, input) {}
1664
Roland Levillain9240d6a2014-10-20 16:47:04 +01001665 virtual int32_t Evaluate(int32_t x) const OVERRIDE { return -x; }
1666 virtual int64_t Evaluate(int64_t x) const OVERRIDE { return -x; }
1667
Roland Levillain88cb1752014-10-20 16:36:47 +01001668 DECLARE_INSTRUCTION(Neg);
1669
1670 private:
1671 DISALLOW_COPY_AND_ASSIGN(HNeg);
1672};
1673
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001674class HNewArray : public HExpression<1> {
1675 public:
1676 HNewArray(HInstruction* length, uint32_t dex_pc, uint16_t type_index)
1677 : HExpression(Primitive::kPrimNot, SideEffects::None()),
1678 dex_pc_(dex_pc),
1679 type_index_(type_index) {
1680 SetRawInputAt(0, length);
1681 }
1682
1683 uint32_t GetDexPc() const { return dex_pc_; }
1684 uint16_t GetTypeIndex() const { return type_index_; }
1685
1686 // Calls runtime so needs an environment.
1687 virtual bool NeedsEnvironment() const { return true; }
1688
1689 DECLARE_INSTRUCTION(NewArray);
1690
1691 private:
1692 const uint32_t dex_pc_;
1693 const uint16_t type_index_;
1694
1695 DISALLOW_COPY_AND_ASSIGN(HNewArray);
1696};
1697
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001698class HAdd : public HBinaryOperation {
1699 public:
1700 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1701 : HBinaryOperation(result_type, left, right) {}
1702
1703 virtual bool IsCommutative() { return true; }
1704
Roland Levillain93445682014-10-06 19:24:02 +01001705 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1706 return x + y;
1707 }
1708 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1709 return x + y;
1710 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001711
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001712 DECLARE_INSTRUCTION(Add);
1713
1714 private:
1715 DISALLOW_COPY_AND_ASSIGN(HAdd);
1716};
1717
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001718class HSub : public HBinaryOperation {
1719 public:
1720 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1721 : HBinaryOperation(result_type, left, right) {}
1722
Roland Levillain93445682014-10-06 19:24:02 +01001723 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1724 return x - y;
1725 }
1726 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1727 return x - y;
1728 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001729
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001730 DECLARE_INSTRUCTION(Sub);
1731
1732 private:
1733 DISALLOW_COPY_AND_ASSIGN(HSub);
1734};
1735
Calin Juravle34bacdf2014-10-07 20:23:36 +01001736class HMul : public HBinaryOperation {
1737 public:
1738 HMul(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1739 : HBinaryOperation(result_type, left, right) {}
1740
1741 virtual bool IsCommutative() { return true; }
1742
1743 virtual int32_t Evaluate(int32_t x, int32_t y) const { return x * y; }
1744 virtual int64_t Evaluate(int64_t x, int64_t y) const { return x * y; }
1745
1746 DECLARE_INSTRUCTION(Mul);
1747
1748 private:
1749 DISALLOW_COPY_AND_ASSIGN(HMul);
1750};
1751
Calin Juravle7c4954d2014-10-28 16:57:40 +00001752class HDiv : public HBinaryOperation {
1753 public:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001754 HDiv(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
1755 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00001756
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00001757 virtual int32_t Evaluate(int32_t x, int32_t y) const {
1758 // Our graph structure ensures we never have 0 for `y` during constant folding.
1759 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00001760 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00001761 return (y == -1) ? -x : x / y;
1762 }
Calin Juravlebacfec32014-11-14 15:54:36 +00001763
1764 virtual int64_t Evaluate(int64_t x, int64_t y) const {
1765 DCHECK_NE(y, 0);
1766 // Special case -1 to avoid getting a SIGFPE on x86(_64).
1767 return (y == -1) ? -x : x / y;
1768 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00001769
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001770 uint32_t GetDexPc() const { return dex_pc_; }
1771
Calin Juravle7c4954d2014-10-28 16:57:40 +00001772 DECLARE_INSTRUCTION(Div);
1773
1774 private:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001775 const uint32_t dex_pc_;
1776
Calin Juravle7c4954d2014-10-28 16:57:40 +00001777 DISALLOW_COPY_AND_ASSIGN(HDiv);
1778};
1779
Calin Juravlebacfec32014-11-14 15:54:36 +00001780class HRem : public HBinaryOperation {
1781 public:
1782 HRem(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
1783 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
1784
1785 virtual int32_t Evaluate(int32_t x, int32_t y) const {
1786 DCHECK_NE(y, 0);
1787 // Special case -1 to avoid getting a SIGFPE on x86(_64).
1788 return (y == -1) ? 0 : x % y;
1789 }
1790
1791 virtual int64_t Evaluate(int64_t x, int64_t y) const {
1792 DCHECK_NE(y, 0);
1793 // Special case -1 to avoid getting a SIGFPE on x86(_64).
1794 return (y == -1) ? 0 : x % y;
1795 }
1796
1797 uint32_t GetDexPc() const { return dex_pc_; }
1798
1799 DECLARE_INSTRUCTION(Rem);
1800
1801 private:
1802 const uint32_t dex_pc_;
1803
1804 DISALLOW_COPY_AND_ASSIGN(HRem);
1805};
1806
Calin Juravled0d48522014-11-04 16:40:20 +00001807class HDivZeroCheck : public HExpression<1> {
1808 public:
1809 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
1810 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
1811 SetRawInputAt(0, value);
1812 }
1813
1814 bool CanBeMoved() const OVERRIDE { return true; }
1815
1816 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
1817 UNUSED(other);
1818 return true;
1819 }
1820
1821 bool NeedsEnvironment() const OVERRIDE { return true; }
1822 bool CanThrow() const OVERRIDE { return true; }
1823
1824 uint32_t GetDexPc() const { return dex_pc_; }
1825
1826 DECLARE_INSTRUCTION(DivZeroCheck);
1827
1828 private:
1829 const uint32_t dex_pc_;
1830
1831 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
1832};
1833
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001834class HAnd : public HBinaryOperation {
1835 public:
1836 HAnd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1837 : HBinaryOperation(result_type, left, right) {}
1838
1839 bool IsCommutative() OVERRIDE { return true; }
1840
1841 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x & y; }
1842 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x & y; }
1843
1844 DECLARE_INSTRUCTION(And);
1845
1846 private:
1847 DISALLOW_COPY_AND_ASSIGN(HAnd);
1848};
1849
1850class HOr : public HBinaryOperation {
1851 public:
1852 HOr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1853 : HBinaryOperation(result_type, left, right) {}
1854
1855 bool IsCommutative() OVERRIDE { return true; }
1856
1857 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x | y; }
1858 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x | y; }
1859
1860 DECLARE_INSTRUCTION(Or);
1861
1862 private:
1863 DISALLOW_COPY_AND_ASSIGN(HOr);
1864};
1865
1866class HXor : public HBinaryOperation {
1867 public:
1868 HXor(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1869 : HBinaryOperation(result_type, left, right) {}
1870
1871 bool IsCommutative() OVERRIDE { return true; }
1872
1873 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x ^ y; }
1874 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x ^ y; }
1875
1876 DECLARE_INSTRUCTION(Xor);
1877
1878 private:
1879 DISALLOW_COPY_AND_ASSIGN(HXor);
1880};
1881
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001882// The value of a parameter in this method. Its location depends on
1883// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07001884class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001885 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001886 HParameterValue(uint8_t index, Primitive::Type parameter_type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001887 : HExpression(parameter_type, SideEffects::None()), index_(index) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001888
1889 uint8_t GetIndex() const { return index_; }
1890
1891 DECLARE_INSTRUCTION(ParameterValue);
1892
1893 private:
1894 // The index of this parameter in the parameters list. Must be less
1895 // than HGraph::number_of_in_vregs_;
1896 const uint8_t index_;
1897
1898 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
1899};
1900
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001901class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001902 public:
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001903 explicit HNot(Primitive::Type result_type, HInstruction* input)
1904 : HUnaryOperation(result_type, input) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001905
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001906 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001907 virtual bool InstructionDataEquals(HInstruction* other) const {
1908 UNUSED(other);
1909 return true;
1910 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001911
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001912 virtual int32_t Evaluate(int32_t x) const OVERRIDE { return ~x; }
1913 virtual int64_t Evaluate(int64_t x) const OVERRIDE { return ~x; }
1914
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001915 DECLARE_INSTRUCTION(Not);
1916
1917 private:
1918 DISALLOW_COPY_AND_ASSIGN(HNot);
1919};
1920
Roland Levillaindff1f282014-11-05 14:15:05 +00001921class HTypeConversion : public HExpression<1> {
1922 public:
1923 // Instantiate a type conversion of `input` to `result_type`.
1924 HTypeConversion(Primitive::Type result_type, HInstruction* input)
1925 : HExpression(result_type, SideEffects::None()) {
1926 SetRawInputAt(0, input);
1927 DCHECK_NE(input->GetType(), result_type);
1928 }
1929
1930 HInstruction* GetInput() const { return InputAt(0); }
1931 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
1932 Primitive::Type GetResultType() const { return GetType(); }
1933
1934 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00001935 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00001936
1937 DECLARE_INSTRUCTION(TypeConversion);
1938
1939 private:
1940 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
1941};
1942
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001943class HPhi : public HInstruction {
1944 public:
1945 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001946 : HInstruction(SideEffects::None()),
1947 inputs_(arena, number_of_inputs),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001948 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001949 type_(type),
1950 is_live_(false) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001951 inputs_.SetSize(number_of_inputs);
1952 }
1953
1954 virtual size_t InputCount() const { return inputs_.Size(); }
1955 virtual HInstruction* InputAt(size_t i) const { return inputs_.Get(i); }
1956
1957 virtual void SetRawInputAt(size_t index, HInstruction* input) {
1958 inputs_.Put(index, input);
1959 }
1960
1961 void AddInput(HInstruction* input);
1962
1963 virtual Primitive::Type GetType() const { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001964 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001965
1966 uint32_t GetRegNumber() const { return reg_number_; }
1967
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001968 void SetDead() { is_live_ = false; }
1969 void SetLive() { is_live_ = true; }
1970 bool IsDead() const { return !is_live_; }
1971 bool IsLive() const { return is_live_; }
1972
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001973 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001974
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001975 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001976 GrowableArray<HInstruction*> inputs_;
1977 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001978 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001979 bool is_live_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001980
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001981 DISALLOW_COPY_AND_ASSIGN(HPhi);
1982};
1983
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001984class HNullCheck : public HExpression<1> {
1985 public:
1986 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001987 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001988 SetRawInputAt(0, value);
1989 }
1990
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001991 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001992 virtual bool InstructionDataEquals(HInstruction* other) const {
1993 UNUSED(other);
1994 return true;
1995 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001996
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001997 virtual bool NeedsEnvironment() const { return true; }
1998
Roland Levillaine161a2a2014-10-03 12:45:18 +01001999 virtual bool CanThrow() const { return true; }
2000
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002001 uint32_t GetDexPc() const { return dex_pc_; }
2002
2003 DECLARE_INSTRUCTION(NullCheck);
2004
2005 private:
2006 const uint32_t dex_pc_;
2007
2008 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
2009};
2010
2011class FieldInfo : public ValueObject {
2012 public:
Roland Levillain5799fc02014-09-25 12:15:20 +01002013 FieldInfo(MemberOffset field_offset, Primitive::Type field_type)
Nicolas Geoffray39468442014-09-02 15:17:15 +01002014 : field_offset_(field_offset), field_type_(field_type) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002015
2016 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002017 Primitive::Type GetFieldType() const { return field_type_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002018
2019 private:
2020 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01002021 const Primitive::Type field_type_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002022};
2023
2024class HInstanceFieldGet : public HExpression<1> {
2025 public:
2026 HInstanceFieldGet(HInstruction* value,
2027 Primitive::Type field_type,
2028 MemberOffset field_offset)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002029 : HExpression(field_type, SideEffects::DependsOnSomething()),
2030 field_info_(field_offset, field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002031 SetRawInputAt(0, value);
2032 }
2033
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002034 virtual bool CanBeMoved() const { return true; }
2035 virtual bool InstructionDataEquals(HInstruction* other) const {
2036 size_t other_offset = other->AsInstanceFieldGet()->GetFieldOffset().SizeValue();
2037 return other_offset == GetFieldOffset().SizeValue();
2038 }
2039
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002040 virtual size_t ComputeHashCode() const {
2041 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
2042 }
2043
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002044 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002045 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002046
2047 DECLARE_INSTRUCTION(InstanceFieldGet);
2048
2049 private:
2050 const FieldInfo field_info_;
2051
2052 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
2053};
2054
2055class HInstanceFieldSet : public HTemplateInstruction<2> {
2056 public:
2057 HInstanceFieldSet(HInstruction* object,
2058 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01002059 Primitive::Type field_type,
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002060 MemberOffset field_offset)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002061 : HTemplateInstruction(SideEffects::ChangesSomething()),
2062 field_info_(field_offset, field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002063 SetRawInputAt(0, object);
2064 SetRawInputAt(1, value);
2065 }
2066
2067 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002068 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002069
2070 DECLARE_INSTRUCTION(InstanceFieldSet);
2071
2072 private:
2073 const FieldInfo field_info_;
2074
2075 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
2076};
2077
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002078class HArrayGet : public HExpression<2> {
2079 public:
2080 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002081 : HExpression(type, SideEffects::DependsOnSomething()) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002082 SetRawInputAt(0, array);
2083 SetRawInputAt(1, index);
2084 }
2085
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002086 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002087 virtual bool InstructionDataEquals(HInstruction* other) const {
2088 UNUSED(other);
2089 return true;
2090 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002091 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002092
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002093 DECLARE_INSTRUCTION(ArrayGet);
2094
2095 private:
2096 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
2097};
2098
2099class HArraySet : public HTemplateInstruction<3> {
2100 public:
2101 HArraySet(HInstruction* array,
2102 HInstruction* index,
2103 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002104 Primitive::Type expected_component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002105 uint32_t dex_pc)
2106 : HTemplateInstruction(SideEffects::ChangesSomething()),
2107 dex_pc_(dex_pc),
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002108 expected_component_type_(expected_component_type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002109 SetRawInputAt(0, array);
2110 SetRawInputAt(1, index);
2111 SetRawInputAt(2, value);
2112 }
2113
2114 virtual bool NeedsEnvironment() const {
2115 // We currently always call a runtime method to catch array store
2116 // exceptions.
2117 return InputAt(2)->GetType() == Primitive::kPrimNot;
2118 }
2119
2120 uint32_t GetDexPc() const { return dex_pc_; }
2121
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002122 HInstruction* GetValue() const { return InputAt(2); }
2123
2124 Primitive::Type GetComponentType() const {
2125 // The Dex format does not type floating point index operations. Since the
2126 // `expected_component_type_` is set during building and can therefore not
2127 // be correct, we also check what is the value type. If it is a floating
2128 // point type, we must use that type.
2129 Primitive::Type value_type = GetValue()->GetType();
2130 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
2131 ? value_type
2132 : expected_component_type_;
2133 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002134
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002135 DECLARE_INSTRUCTION(ArraySet);
2136
2137 private:
2138 const uint32_t dex_pc_;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002139 const Primitive::Type expected_component_type_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002140
2141 DISALLOW_COPY_AND_ASSIGN(HArraySet);
2142};
2143
2144class HArrayLength : public HExpression<1> {
2145 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002146 explicit HArrayLength(HInstruction* array)
2147 : HExpression(Primitive::kPrimInt, SideEffects::None()) {
2148 // Note that arrays do not change length, so the instruction does not
2149 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002150 SetRawInputAt(0, array);
2151 }
2152
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002153 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002154 virtual bool InstructionDataEquals(HInstruction* other) const {
2155 UNUSED(other);
2156 return true;
2157 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002158
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002159 DECLARE_INSTRUCTION(ArrayLength);
2160
2161 private:
2162 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
2163};
2164
2165class HBoundsCheck : public HExpression<2> {
2166 public:
2167 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002168 : HExpression(index->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002169 DCHECK(index->GetType() == Primitive::kPrimInt);
2170 SetRawInputAt(0, index);
2171 SetRawInputAt(1, length);
2172 }
2173
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002174 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002175 virtual bool InstructionDataEquals(HInstruction* other) const {
2176 UNUSED(other);
2177 return true;
2178 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002179
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002180 virtual bool NeedsEnvironment() const { return true; }
2181
Roland Levillaine161a2a2014-10-03 12:45:18 +01002182 virtual bool CanThrow() const { return true; }
2183
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002184 uint32_t GetDexPc() const { return dex_pc_; }
2185
2186 DECLARE_INSTRUCTION(BoundsCheck);
2187
2188 private:
2189 const uint32_t dex_pc_;
2190
2191 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
2192};
2193
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002194/**
2195 * Some DEX instructions are folded into multiple HInstructions that need
2196 * to stay live until the last HInstruction. This class
2197 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00002198 * HInstruction stays live. `index` represents the stack location index of the
2199 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002200 */
2201class HTemporary : public HTemplateInstruction<0> {
2202 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002203 explicit HTemporary(size_t index) : HTemplateInstruction(SideEffects::None()), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002204
2205 size_t GetIndex() const { return index_; }
2206
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00002207 Primitive::Type GetType() const OVERRIDE {
2208 // The previous instruction is the one that will be stored in the temporary location.
2209 DCHECK(GetPrevious() != nullptr);
2210 return GetPrevious()->GetType();
2211 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00002212
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002213 DECLARE_INSTRUCTION(Temporary);
2214
2215 private:
2216 const size_t index_;
2217
2218 DISALLOW_COPY_AND_ASSIGN(HTemporary);
2219};
2220
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002221class HSuspendCheck : public HTemplateInstruction<0> {
2222 public:
2223 explicit HSuspendCheck(uint32_t dex_pc)
Nicolas Geoffray9ebc72c2014-09-25 16:33:42 +01002224 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002225
2226 virtual bool NeedsEnvironment() const {
2227 return true;
2228 }
2229
2230 uint32_t GetDexPc() const { return dex_pc_; }
2231
2232 DECLARE_INSTRUCTION(SuspendCheck);
2233
2234 private:
2235 const uint32_t dex_pc_;
2236
2237 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
2238};
2239
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002240/**
2241 * Instruction to load a Class object.
2242 */
2243class HLoadClass : public HExpression<0> {
2244 public:
2245 HLoadClass(uint16_t type_index,
2246 bool is_referrers_class,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002247 uint32_t dex_pc)
2248 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2249 type_index_(type_index),
2250 is_referrers_class_(is_referrers_class),
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002251 dex_pc_(dex_pc),
2252 generate_clinit_check_(false) {}
2253
2254 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002255
2256 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2257 return other->AsLoadClass()->type_index_ == type_index_;
2258 }
2259
2260 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
2261
2262 uint32_t GetDexPc() const { return dex_pc_; }
2263 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002264 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002265
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002266 bool NeedsEnvironment() const OVERRIDE {
2267 // Will call runtime and load the class if the class is not loaded yet.
2268 // TODO: finer grain decision.
2269 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002270 }
2271
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002272 bool MustGenerateClinitCheck() const {
2273 return generate_clinit_check_;
2274 }
2275
2276 void SetMustGenerateClinitCheck() {
2277 generate_clinit_check_ = true;
2278 }
2279
2280 bool CanCallRuntime() const {
2281 return MustGenerateClinitCheck() || !is_referrers_class_;
2282 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002283
2284 DECLARE_INSTRUCTION(LoadClass);
2285
2286 private:
2287 const uint16_t type_index_;
2288 const bool is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002289 const uint32_t dex_pc_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002290 // Whether this instruction must generate the initialization check.
2291 // Used for code generation.
2292 bool generate_clinit_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002293
2294 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
2295};
2296
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002297class HLoadString : public HExpression<0> {
2298 public:
2299 HLoadString(uint32_t string_index, uint32_t dex_pc)
2300 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2301 string_index_(string_index),
2302 dex_pc_(dex_pc) {}
2303
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002304 bool CanBeMoved() const OVERRIDE { return true; }
2305
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002306 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2307 return other->AsLoadString()->string_index_ == string_index_;
2308 }
2309
2310 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
2311
2312 uint32_t GetDexPc() const { return dex_pc_; }
2313 uint32_t GetStringIndex() const { return string_index_; }
2314
2315 // TODO: Can we deopt or debug when we resolve a string?
2316 bool NeedsEnvironment() const OVERRIDE { return false; }
2317
2318 DECLARE_INSTRUCTION(LoadString);
2319
2320 private:
2321 const uint32_t string_index_;
2322 const uint32_t dex_pc_;
2323
2324 DISALLOW_COPY_AND_ASSIGN(HLoadString);
2325};
2326
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002327// TODO: Pass this check to HInvokeStatic nodes.
2328/**
2329 * Performs an initialization check on its Class object input.
2330 */
2331class HClinitCheck : public HExpression<1> {
2332 public:
2333 explicit HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
2334 : HExpression(Primitive::kPrimNot, SideEffects::All()),
2335 dex_pc_(dex_pc) {
2336 SetRawInputAt(0, constant);
2337 }
2338
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002339 bool CanBeMoved() const OVERRIDE { return true; }
2340 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2341 UNUSED(other);
2342 return true;
2343 }
2344
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002345 bool NeedsEnvironment() const OVERRIDE {
2346 // May call runtime to initialize the class.
2347 return true;
2348 }
2349
2350 uint32_t GetDexPc() const { return dex_pc_; }
2351
2352 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
2353
2354 DECLARE_INSTRUCTION(ClinitCheck);
2355
2356 private:
2357 const uint32_t dex_pc_;
2358
2359 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
2360};
2361
2362class HStaticFieldGet : public HExpression<1> {
2363 public:
2364 HStaticFieldGet(HInstruction* cls,
2365 Primitive::Type field_type,
2366 MemberOffset field_offset)
2367 : HExpression(field_type, SideEffects::DependsOnSomething()),
2368 field_info_(field_offset, field_type) {
2369 SetRawInputAt(0, cls);
2370 }
2371
2372 bool CanBeMoved() const OVERRIDE { return true; }
2373 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2374 size_t other_offset = other->AsStaticFieldGet()->GetFieldOffset().SizeValue();
2375 return other_offset == GetFieldOffset().SizeValue();
2376 }
2377
2378 size_t ComputeHashCode() const OVERRIDE {
2379 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
2380 }
2381
2382 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
2383 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
2384
2385 DECLARE_INSTRUCTION(StaticFieldGet);
2386
2387 private:
2388 const FieldInfo field_info_;
2389
2390 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
2391};
2392
2393class HStaticFieldSet : public HTemplateInstruction<2> {
2394 public:
2395 HStaticFieldSet(HInstruction* cls,
2396 HInstruction* value,
2397 Primitive::Type field_type,
2398 MemberOffset field_offset)
2399 : HTemplateInstruction(SideEffects::ChangesSomething()),
2400 field_info_(field_offset, field_type) {
2401 SetRawInputAt(0, cls);
2402 SetRawInputAt(1, value);
2403 }
2404
2405 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
2406 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
2407
2408 DECLARE_INSTRUCTION(StaticFieldSet);
2409
2410 private:
2411 const FieldInfo field_info_;
2412
2413 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
2414};
2415
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002416// Implement the move-exception DEX instruction.
2417class HLoadException : public HExpression<0> {
2418 public:
2419 HLoadException() : HExpression(Primitive::kPrimNot, SideEffects::None()) {}
2420
2421 DECLARE_INSTRUCTION(LoadException);
2422
2423 private:
2424 DISALLOW_COPY_AND_ASSIGN(HLoadException);
2425};
2426
2427class HThrow : public HTemplateInstruction<1> {
2428 public:
2429 HThrow(HInstruction* exception, uint32_t dex_pc)
2430 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {
2431 SetRawInputAt(0, exception);
2432 }
2433
2434 bool IsControlFlow() const OVERRIDE { return true; }
2435
2436 bool NeedsEnvironment() const OVERRIDE { return true; }
2437
2438 uint32_t GetDexPc() const { return dex_pc_; }
2439
2440 DECLARE_INSTRUCTION(Throw);
2441
2442 private:
2443 uint32_t dex_pc_;
2444
2445 DISALLOW_COPY_AND_ASSIGN(HThrow);
2446};
2447
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002448class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002449 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002450 HInstanceOf(HInstruction* object,
2451 HLoadClass* constant,
2452 bool class_is_final,
2453 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002454 : HExpression(Primitive::kPrimBoolean, SideEffects::None()),
2455 class_is_final_(class_is_final),
2456 dex_pc_(dex_pc) {
2457 SetRawInputAt(0, object);
2458 SetRawInputAt(1, constant);
2459 }
2460
2461 bool CanBeMoved() const OVERRIDE { return true; }
2462
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002463 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002464 return true;
2465 }
2466
2467 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002468 return false;
2469 }
2470
2471 uint32_t GetDexPc() const { return dex_pc_; }
2472
2473 bool IsClassFinal() const { return class_is_final_; }
2474
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002475 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002476
2477 private:
2478 const bool class_is_final_;
2479 const uint32_t dex_pc_;
2480
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002481 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
2482};
2483
2484class HCheckCast : public HTemplateInstruction<2> {
2485 public:
2486 HCheckCast(HInstruction* object,
2487 HLoadClass* constant,
2488 bool class_is_final,
2489 uint32_t dex_pc)
2490 : HTemplateInstruction(SideEffects::None()),
2491 class_is_final_(class_is_final),
2492 dex_pc_(dex_pc) {
2493 SetRawInputAt(0, object);
2494 SetRawInputAt(1, constant);
2495 }
2496
2497 bool CanBeMoved() const OVERRIDE { return true; }
2498
2499 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2500 return true;
2501 }
2502
2503 bool NeedsEnvironment() const OVERRIDE {
2504 // Instruction may throw a CheckCastError.
2505 return true;
2506 }
2507
2508 bool CanThrow() const OVERRIDE { return true; }
2509
2510 uint32_t GetDexPc() const { return dex_pc_; }
2511
2512 bool IsClassFinal() const { return class_is_final_; }
2513
2514 DECLARE_INSTRUCTION(CheckCast);
2515
2516 private:
2517 const bool class_is_final_;
2518 const uint32_t dex_pc_;
2519
2520 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002521};
2522
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00002523class HMonitorOperation : public HTemplateInstruction<1> {
2524 public:
2525 enum OperationKind {
2526 kEnter,
2527 kExit,
2528 };
2529
2530 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
2531 : HTemplateInstruction(SideEffects::None()), kind_(kind), dex_pc_(dex_pc) {
2532 SetRawInputAt(0, object);
2533 }
2534
2535 // Instruction may throw a Java exception, so we need an environment.
2536 bool NeedsEnvironment() const OVERRIDE { return true; }
2537 bool CanThrow() const OVERRIDE { return true; }
2538
2539 uint32_t GetDexPc() const { return dex_pc_; }
2540
2541 bool IsEnter() const { return kind_ == kEnter; }
2542
2543 DECLARE_INSTRUCTION(MonitorOperation);
2544
2545 protected:
2546 const OperationKind kind_;
2547 const uint32_t dex_pc_;
2548
2549 private:
2550 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
2551};
2552
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002553
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002554class MoveOperands : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002555 public:
Nicolas Geoffray740475d2014-09-29 10:33:25 +01002556 MoveOperands(Location source, Location destination, HInstruction* instruction)
2557 : source_(source), destination_(destination), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002558
2559 Location GetSource() const { return source_; }
2560 Location GetDestination() const { return destination_; }
2561
2562 void SetSource(Location value) { source_ = value; }
2563 void SetDestination(Location value) { destination_ = value; }
2564
2565 // The parallel move resolver marks moves as "in-progress" by clearing the
2566 // destination (but not the source).
2567 Location MarkPending() {
2568 DCHECK(!IsPending());
2569 Location dest = destination_;
2570 destination_ = Location::NoLocation();
2571 return dest;
2572 }
2573
2574 void ClearPending(Location dest) {
2575 DCHECK(IsPending());
2576 destination_ = dest;
2577 }
2578
2579 bool IsPending() const {
2580 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
2581 return destination_.IsInvalid() && !source_.IsInvalid();
2582 }
2583
2584 // True if this blocks a move from the given location.
2585 bool Blocks(Location loc) const {
2586 return !IsEliminated() && source_.Equals(loc);
2587 }
2588
2589 // A move is redundant if it's been eliminated, if its source and
2590 // destination are the same, or if its destination is unneeded.
2591 bool IsRedundant() const {
2592 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
2593 }
2594
2595 // We clear both operands to indicate move that's been eliminated.
2596 void Eliminate() {
2597 source_ = destination_ = Location::NoLocation();
2598 }
2599
2600 bool IsEliminated() const {
2601 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
2602 return source_.IsInvalid();
2603 }
2604
Nicolas Geoffray740475d2014-09-29 10:33:25 +01002605 HInstruction* GetInstruction() const { return instruction_; }
2606
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002607 private:
2608 Location source_;
2609 Location destination_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01002610 // The instruction this move is assocatied with. Null when this move is
2611 // for moving an input in the expected locations of user (including a phi user).
2612 // This is only used in debug mode, to ensure we do not connect interval siblings
2613 // in the same parallel move.
2614 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002615
2616 DISALLOW_COPY_AND_ASSIGN(MoveOperands);
2617};
2618
2619static constexpr size_t kDefaultNumberOfMoves = 4;
2620
2621class HParallelMove : public HTemplateInstruction<0> {
2622 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002623 explicit HParallelMove(ArenaAllocator* arena)
2624 : HTemplateInstruction(SideEffects::None()), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002625
2626 void AddMove(MoveOperands* move) {
Nicolas Geoffray740475d2014-09-29 10:33:25 +01002627 if (kIsDebugBuild && move->GetInstruction() != nullptr) {
2628 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
2629 DCHECK_NE(moves_.Get(i)->GetInstruction(), move->GetInstruction())
2630 << "Doing parallel moves for the same instruction.";
2631 }
2632 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002633 moves_.Add(move);
2634 }
2635
2636 MoveOperands* MoveOperandsAt(size_t index) const {
2637 return moves_.Get(index);
2638 }
2639
2640 size_t NumMoves() const { return moves_.Size(); }
2641
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002642 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002643
2644 private:
2645 GrowableArray<MoveOperands*> moves_;
2646
2647 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
2648};
2649
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002650class HGraphVisitor : public ValueObject {
2651 public:
Dave Allison20dfc792014-06-16 20:44:29 -07002652 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
2653 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002654
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002655 virtual void VisitInstruction(HInstruction* instruction) { UNUSED(instruction); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002656 virtual void VisitBasicBlock(HBasicBlock* block);
2657
Roland Levillain633021e2014-10-01 14:12:25 +01002658 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002659 void VisitInsertionOrder();
2660
Roland Levillain633021e2014-10-01 14:12:25 +01002661 // Visit the graph following dominator tree reverse post-order.
2662 void VisitReversePostOrder();
2663
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002664 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002665
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002666 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002667#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002668 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
2669
2670 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
2671
2672#undef DECLARE_VISIT_INSTRUCTION
2673
2674 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07002675 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002676
2677 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
2678};
2679
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002680class HGraphDelegateVisitor : public HGraphVisitor {
2681 public:
2682 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
2683 virtual ~HGraphDelegateVisitor() {}
2684
2685 // Visit functions that delegate to to super class.
2686#define DECLARE_VISIT_INSTRUCTION(name, super) \
2687 virtual void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
2688
2689 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
2690
2691#undef DECLARE_VISIT_INSTRUCTION
2692
2693 private:
2694 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
2695};
2696
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002697class HInsertionOrderIterator : public ValueObject {
2698 public:
2699 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
2700
2701 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
2702 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
2703 void Advance() { ++index_; }
2704
2705 private:
2706 const HGraph& graph_;
2707 size_t index_;
2708
2709 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
2710};
2711
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002712class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002713 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002714 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002715
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002716 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
2717 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002718 void Advance() { ++index_; }
2719
2720 private:
2721 const HGraph& graph_;
2722 size_t index_;
2723
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002724 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002725};
2726
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002727class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002728 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002729 explicit HPostOrderIterator(const HGraph& graph)
2730 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002731
2732 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002733 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002734 void Advance() { --index_; }
2735
2736 private:
2737 const HGraph& graph_;
2738 size_t index_;
2739
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002740 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002741};
2742
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002743} // namespace art
2744
2745#endif // ART_COMPILER_OPTIMIZING_NODES_H_