blob: 5af3cdd2d6b82c70f051d82a23d2a4c9b8b49bd6 [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) \
524 M(Return, Instruction) \
525 M(ReturnVoid, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100526 M(StaticFieldGet, Instruction) \
527 M(StaticFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100528 M(StoreLocal, Instruction) \
529 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100530 M(SuspendCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000531 M(Temporary, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000532 M(Throw, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +0000533 M(TypeConversion, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000534 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000535
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100536#define FOR_EACH_INSTRUCTION(M) \
537 FOR_EACH_CONCRETE_INSTRUCTION(M) \
538 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +0100539 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +0100540 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100541 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -0700542
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100543#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000544FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
545#undef FORWARD_DECLARATION
546
Roland Levillainccc07a92014-09-16 14:48:16 +0100547#define DECLARE_INSTRUCTION(type) \
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100548 virtual InstructionKind GetKind() const { return k##type; } \
Roland Levillainccc07a92014-09-16 14:48:16 +0100549 virtual const char* DebugName() const { return #type; } \
550 virtual const H##type* As##type() const OVERRIDE { return this; } \
551 virtual H##type* As##type() OVERRIDE { return this; } \
552 virtual bool InstructionTypeEquals(HInstruction* other) const { \
553 return other->Is##type(); \
554 } \
555 virtual void Accept(HGraphVisitor* visitor)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000556
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100557template <typename T>
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700558class HUseListNode : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000559 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100560 HUseListNode(T* user, size_t index, HUseListNode* tail)
Dave Allison20dfc792014-06-16 20:44:29 -0700561 : user_(user), index_(index), tail_(tail) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000562
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000563 HUseListNode* GetTail() const { return tail_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100564 T* GetUser() const { return user_; }
565 size_t GetIndex() const { return index_; }
566
567 void SetTail(HUseListNode<T>* node) { tail_ = node; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000568
569 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100570 T* const user_;
571 const size_t index_;
572 HUseListNode<T>* tail_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000573
574 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
575};
576
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100577// Represents the side effects an instruction may have.
578class SideEffects : public ValueObject {
579 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100580 SideEffects() : flags_(0) {}
581
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100582 static SideEffects None() {
583 return SideEffects(0);
584 }
585
586 static SideEffects All() {
587 return SideEffects(ChangesSomething().flags_ | DependsOnSomething().flags_);
588 }
589
590 static SideEffects ChangesSomething() {
591 return SideEffects((1 << kFlagChangesCount) - 1);
592 }
593
594 static SideEffects DependsOnSomething() {
595 int count = kFlagDependsOnCount - kFlagChangesCount;
596 return SideEffects(((1 << count) - 1) << kFlagChangesCount);
597 }
598
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100599 SideEffects Union(SideEffects other) const {
600 return SideEffects(flags_ | other.flags_);
601 }
602
Roland Levillain72bceff2014-09-15 18:29:00 +0100603 bool HasSideEffects() const {
604 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
605 return (flags_ & all_bits_set) != 0;
606 }
607
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100608 bool HasAllSideEffects() const {
609 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
610 return all_bits_set == (flags_ & all_bits_set);
611 }
612
613 bool DependsOn(SideEffects other) const {
614 size_t depends_flags = other.ComputeDependsFlags();
615 return (flags_ & depends_flags) != 0;
616 }
617
618 bool HasDependencies() const {
619 int count = kFlagDependsOnCount - kFlagChangesCount;
620 size_t all_bits_set = (1 << count) - 1;
621 return ((flags_ >> kFlagChangesCount) & all_bits_set) != 0;
622 }
623
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100624 private:
625 static constexpr int kFlagChangesSomething = 0;
626 static constexpr int kFlagChangesCount = kFlagChangesSomething + 1;
627
628 static constexpr int kFlagDependsOnSomething = kFlagChangesCount;
629 static constexpr int kFlagDependsOnCount = kFlagDependsOnSomething + 1;
630
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100631 explicit SideEffects(size_t flags) : flags_(flags) {}
632
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100633 size_t ComputeDependsFlags() const {
634 return flags_ << kFlagChangesCount;
635 }
636
637 size_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100638};
639
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700640class HInstruction : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000641 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100642 explicit HInstruction(SideEffects side_effects)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000643 : previous_(nullptr),
644 next_(nullptr),
645 block_(nullptr),
646 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100647 ssa_index_(-1),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000648 uses_(nullptr),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100649 env_uses_(nullptr),
650 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100651 locations_(nullptr),
652 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100653 lifetime_position_(kNoLifetime),
654 side_effects_(side_effects) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000655
Dave Allison20dfc792014-06-16 20:44:29 -0700656 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000657
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100658#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100659 enum InstructionKind {
660 FOR_EACH_INSTRUCTION(DECLARE_KIND)
661 };
662#undef DECLARE_KIND
663
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000664 HInstruction* GetNext() const { return next_; }
665 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000666
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000667 HBasicBlock* GetBlock() const { return block_; }
668 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100669 bool IsInBlock() const { return block_ != nullptr; }
670 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +0100671 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000672
Roland Levillain6b879dd2014-09-22 17:13:44 +0100673 virtual size_t InputCount() const = 0;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100674 virtual HInstruction* InputAt(size_t i) const = 0;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000675
676 virtual void Accept(HGraphVisitor* visitor) = 0;
677 virtual const char* DebugName() const = 0;
678
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100679 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100680 virtual void SetRawInputAt(size_t index, HInstruction* input) = 0;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100681
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100682 virtual bool NeedsEnvironment() const { return false; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100683 virtual bool IsControlFlow() const { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +0100684 virtual bool CanThrow() const { return false; }
Roland Levillain72bceff2014-09-15 18:29:00 +0100685 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100686
687 void AddUseAt(HInstruction* user, size_t index) {
688 uses_ = new (block_->GetGraph()->GetArena()) HUseListNode<HInstruction>(user, index, uses_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000689 }
690
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100691 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +0100692 DCHECK(user != nullptr);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100693 env_uses_ = new (block_->GetGraph()->GetArena()) HUseListNode<HEnvironment>(
694 user, index, env_uses_);
695 }
696
697 void RemoveUser(HInstruction* user, size_t index);
Nicolas Geoffray724c9632014-09-22 12:27:27 +0100698 void RemoveEnvironmentUser(HEnvironment* user, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100699
700 HUseListNode<HInstruction>* GetUses() const { return uses_; }
701 HUseListNode<HEnvironment>* GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000702
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100703 bool HasUses() const { return uses_ != nullptr || env_uses_ != nullptr; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100704 bool HasEnvironmentUses() const { return env_uses_ != nullptr; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000705
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100706 size_t NumberOfUses() const {
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100707 // TODO: Optimize this method if it is used outside of the HGraphVisualizer.
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100708 size_t result = 0;
709 HUseListNode<HInstruction>* current = uses_;
710 while (current != nullptr) {
711 current = current->GetTail();
712 ++result;
713 }
714 return result;
715 }
716
Roland Levillain6c82d402014-10-13 16:10:27 +0100717 // Does this instruction strictly dominate `other_instruction`?
718 // Returns false if this instruction and `other_instruction` are the same.
719 // Aborts if this instruction and `other_instruction` are both phis.
720 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +0100721
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000722 int GetId() const { return id_; }
723 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000724
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100725 int GetSsaIndex() const { return ssa_index_; }
726 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
727 bool HasSsaIndex() const { return ssa_index_ != -1; }
728
729 bool HasEnvironment() const { return environment_ != nullptr; }
730 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100731 void SetEnvironment(HEnvironment* environment) { environment_ = environment; }
732
Nicolas Geoffray39468442014-09-02 15:17:15 +0100733 // Returns the number of entries in the environment. Typically, that is the
734 // number of dex registers in a method. It could be more in case of inlining.
735 size_t EnvironmentSize() const;
736
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000737 LocationSummary* GetLocations() const { return locations_; }
738 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000739
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100740 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100741 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100742
Dave Allison20dfc792014-06-16 20:44:29 -0700743 bool HasOnlyOneUse() const {
744 return uses_ != nullptr && uses_->GetTail() == nullptr;
745 }
746
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100747#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +0100748 bool Is##type() const { return (As##type() != nullptr); } \
749 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000750 virtual H##type* As##type() { return nullptr; }
751
752 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
753#undef INSTRUCTION_TYPE_CHECK
754
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100755 // Returns whether the instruction can be moved within the graph.
756 virtual bool CanBeMoved() const { return false; }
757
758 // Returns whether the two instructions are of the same kind.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700759 virtual bool InstructionTypeEquals(HInstruction* other) const {
760 UNUSED(other);
761 return false;
762 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100763
764 // Returns whether any data encoded in the two instructions is equal.
765 // This method does not look at the inputs. Both instructions must be
766 // of the same type, otherwise the method has undefined behavior.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700767 virtual bool InstructionDataEquals(HInstruction* other) const {
768 UNUSED(other);
769 return false;
770 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100771
772 // Returns whether two instructions are equal, that is:
773 // 1) They have the same type and contain the same data,
774 // 2) Their inputs are identical.
775 bool Equals(HInstruction* other) const;
776
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100777 virtual InstructionKind GetKind() const = 0;
778
779 virtual size_t ComputeHashCode() const {
780 size_t result = GetKind();
781 for (size_t i = 0, e = InputCount(); i < e; ++i) {
782 result = (result * 31) + InputAt(i)->GetId();
783 }
784 return result;
785 }
786
787 SideEffects GetSideEffects() const { return side_effects_; }
788
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100789 size_t GetLifetimePosition() const { return lifetime_position_; }
790 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
791 LiveInterval* GetLiveInterval() const { return live_interval_; }
792 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
793 bool HasLiveInterval() const { return live_interval_ != nullptr; }
794
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000795 private:
796 HInstruction* previous_;
797 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000798 HBasicBlock* block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000799
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000800 // An instruction gets an id when it is added to the graph.
801 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +0100802 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000803 int id_;
804
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100805 // When doing liveness analysis, instructions that have uses get an SSA index.
806 int ssa_index_;
807
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100808 // List of instructions that have this instruction as input.
809 HUseListNode<HInstruction>* uses_;
810
811 // List of environments that contain this instruction.
812 HUseListNode<HEnvironment>* env_uses_;
813
Nicolas Geoffray39468442014-09-02 15:17:15 +0100814 // The environment associated with this instruction. Not null if the instruction
815 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100816 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000817
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000818 // Set by the code generator.
819 LocationSummary* locations_;
820
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100821 // Set by the liveness analysis.
822 LiveInterval* live_interval_;
823
824 // Set by the liveness analysis, this is the position in a linear
825 // order of blocks where this instruction's live interval start.
826 size_t lifetime_position_;
827
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100828 const SideEffects side_effects_;
829
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000830 friend class HBasicBlock;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100831 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000832
833 DISALLOW_COPY_AND_ASSIGN(HInstruction);
834};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700835std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000836
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100837template<typename T>
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000838class HUseIterator : public ValueObject {
839 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100840 explicit HUseIterator(HUseListNode<T>* uses) : current_(uses) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000841
842 bool Done() const { return current_ == nullptr; }
843
844 void Advance() {
845 DCHECK(!Done());
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000846 current_ = current_->GetTail();
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000847 }
848
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100849 HUseListNode<T>* Current() const {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000850 DCHECK(!Done());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100851 return current_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000852 }
853
854 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100855 HUseListNode<T>* current_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000856
857 friend class HValue;
858};
859
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100860// A HEnvironment object contains the values of virtual registers at a given location.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700861class HEnvironment : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100862 public:
863 HEnvironment(ArenaAllocator* arena, size_t number_of_vregs) : vregs_(arena, number_of_vregs) {
864 vregs_.SetSize(number_of_vregs);
865 for (size_t i = 0; i < number_of_vregs; i++) {
866 vregs_.Put(i, nullptr);
867 }
868 }
869
870 void Populate(const GrowableArray<HInstruction*>& env) {
871 for (size_t i = 0; i < env.Size(); i++) {
872 HInstruction* instruction = env.Get(i);
873 vregs_.Put(i, instruction);
874 if (instruction != nullptr) {
875 instruction->AddEnvUseAt(this, i);
876 }
877 }
878 }
879
880 void SetRawEnvAt(size_t index, HInstruction* instruction) {
881 vregs_.Put(index, instruction);
882 }
883
Nicolas Geoffray39468442014-09-02 15:17:15 +0100884 HInstruction* GetInstructionAt(size_t index) const {
885 return vregs_.Get(index);
886 }
887
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100888 GrowableArray<HInstruction*>* GetVRegs() {
889 return &vregs_;
890 }
891
Nicolas Geoffray39468442014-09-02 15:17:15 +0100892 size_t Size() const { return vregs_.Size(); }
893
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100894 private:
895 GrowableArray<HInstruction*> vregs_;
896
897 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
898};
899
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000900class HInputIterator : public ValueObject {
901 public:
Dave Allison20dfc792014-06-16 20:44:29 -0700902 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000903
904 bool Done() const { return index_ == instruction_->InputCount(); }
905 HInstruction* Current() const { return instruction_->InputAt(index_); }
906 void Advance() { index_++; }
907
908 private:
909 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100910 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000911
912 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
913};
914
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000915class HInstructionIterator : public ValueObject {
916 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100917 explicit HInstructionIterator(const HInstructionList& instructions)
918 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000919 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000920 }
921
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000922 bool Done() const { return instruction_ == nullptr; }
923 HInstruction* Current() const { return instruction_; }
924 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000925 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000926 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000927 }
928
929 private:
930 HInstruction* instruction_;
931 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100932
933 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000934};
935
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100936class HBackwardInstructionIterator : public ValueObject {
937 public:
938 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
939 : instruction_(instructions.last_instruction_) {
940 next_ = Done() ? nullptr : instruction_->GetPrevious();
941 }
942
943 bool Done() const { return instruction_ == nullptr; }
944 HInstruction* Current() const { return instruction_; }
945 void Advance() {
946 instruction_ = next_;
947 next_ = Done() ? nullptr : instruction_->GetPrevious();
948 }
949
950 private:
951 HInstruction* instruction_;
952 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100953
954 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100955};
956
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000957// An embedded container with N elements of type T. Used (with partial
958// specialization for N=0) because embedded arrays cannot have size 0.
959template<typename T, intptr_t N>
960class EmbeddedArray {
961 public:
Dave Allison20dfc792014-06-16 20:44:29 -0700962 EmbeddedArray() : elements_() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000963
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000964 intptr_t GetLength() const { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000965
966 const T& operator[](intptr_t i) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000967 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000968 return elements_[i];
969 }
970
971 T& operator[](intptr_t i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000972 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000973 return elements_[i];
974 }
975
976 const T& At(intptr_t i) const {
977 return (*this)[i];
978 }
979
980 void SetAt(intptr_t i, const T& val) {
981 (*this)[i] = val;
982 }
983
984 private:
985 T elements_[N];
986};
987
988template<typename T>
989class EmbeddedArray<T, 0> {
990 public:
991 intptr_t length() const { return 0; }
992 const T& operator[](intptr_t i) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700993 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000994 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700995 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000996 }
997 T& operator[](intptr_t i) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700998 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000999 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001000 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001001 }
1002};
1003
1004template<intptr_t N>
1005class HTemplateInstruction: public HInstruction {
1006 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001007 HTemplateInstruction<N>(SideEffects side_effects)
1008 : HInstruction(side_effects), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07001009 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001010
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001011 virtual size_t InputCount() const { return N; }
1012 virtual HInstruction* InputAt(size_t i) const { return inputs_[i]; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001013
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001014 protected:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001015 virtual void SetRawInputAt(size_t i, HInstruction* instruction) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001016 inputs_[i] = instruction;
1017 }
1018
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001019 private:
1020 EmbeddedArray<HInstruction*, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001021
1022 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001023};
1024
Dave Allison20dfc792014-06-16 20:44:29 -07001025template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001026class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07001027 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001028 HExpression<N>(Primitive::Type type, SideEffects side_effects)
1029 : HTemplateInstruction<N>(side_effects), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07001030 virtual ~HExpression() {}
1031
1032 virtual Primitive::Type GetType() const { return type_; }
1033
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001034 protected:
1035 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07001036};
1037
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001038// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
1039// instruction that branches to the exit block.
1040class HReturnVoid : public HTemplateInstruction<0> {
1041 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001042 HReturnVoid() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001043
1044 virtual bool IsControlFlow() const { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001045
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001046 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001047
1048 private:
1049 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
1050};
1051
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001052// Represents dex's RETURN opcodes. A HReturn is a control flow
1053// instruction that branches to the exit block.
1054class HReturn : public HTemplateInstruction<1> {
1055 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001056 explicit HReturn(HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001057 SetRawInputAt(0, value);
1058 }
1059
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001060 virtual bool IsControlFlow() const { return true; }
1061
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001062 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001063
1064 private:
1065 DISALLOW_COPY_AND_ASSIGN(HReturn);
1066};
1067
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001068// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001069// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001070// exit block.
1071class HExit : public HTemplateInstruction<0> {
1072 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001073 HExit() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001074
1075 virtual bool IsControlFlow() const { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001076
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001077 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001078
1079 private:
1080 DISALLOW_COPY_AND_ASSIGN(HExit);
1081};
1082
1083// Jumps from one block to another.
1084class HGoto : public HTemplateInstruction<0> {
1085 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001086 HGoto() : HTemplateInstruction(SideEffects::None()) {}
1087
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001088 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001089
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001090 HBasicBlock* GetSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001091 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001092 }
1093
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001094 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001095
1096 private:
1097 DISALLOW_COPY_AND_ASSIGN(HGoto);
1098};
1099
Dave Allison20dfc792014-06-16 20:44:29 -07001100
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001101// Conditional branch. A block ending with an HIf instruction must have
1102// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001103class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001104 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001105 explicit HIf(HInstruction* input) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001106 SetRawInputAt(0, input);
1107 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001108
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001109 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001110
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001111 HBasicBlock* IfTrueSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001112 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001113 }
1114
1115 HBasicBlock* IfFalseSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001116 return GetBlock()->GetSuccessors().Get(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001117 }
1118
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001119 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001120
Dave Allison20dfc792014-06-16 20:44:29 -07001121 virtual bool IsIfInstruction() const { return true; }
1122
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001123 private:
1124 DISALLOW_COPY_AND_ASSIGN(HIf);
1125};
1126
Roland Levillain88cb1752014-10-20 16:36:47 +01001127class HUnaryOperation : public HExpression<1> {
1128 public:
1129 HUnaryOperation(Primitive::Type result_type, HInstruction* input)
1130 : HExpression(result_type, SideEffects::None()) {
1131 SetRawInputAt(0, input);
1132 }
1133
1134 HInstruction* GetInput() const { return InputAt(0); }
1135 Primitive::Type GetResultType() const { return GetType(); }
1136
1137 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001138 virtual bool InstructionDataEquals(HInstruction* other) const {
1139 UNUSED(other);
1140 return true;
1141 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001142
Roland Levillain9240d6a2014-10-20 16:47:04 +01001143 // Try to statically evaluate `operation` and return a HConstant
1144 // containing the result of this evaluation. If `operation` cannot
1145 // be evaluated as a constant, return nullptr.
1146 HConstant* TryStaticEvaluation() const;
1147
1148 // Apply this operation to `x`.
1149 virtual int32_t Evaluate(int32_t x) const = 0;
1150 virtual int64_t Evaluate(int64_t x) const = 0;
1151
Roland Levillain88cb1752014-10-20 16:36:47 +01001152 DECLARE_INSTRUCTION(UnaryOperation);
1153
1154 private:
1155 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
1156};
1157
Dave Allison20dfc792014-06-16 20:44:29 -07001158class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001159 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001160 HBinaryOperation(Primitive::Type result_type,
1161 HInstruction* left,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001162 HInstruction* right) : HExpression(result_type, SideEffects::None()) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001163 SetRawInputAt(0, left);
1164 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001165 }
1166
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001167 HInstruction* GetLeft() const { return InputAt(0); }
1168 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07001169 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001170
1171 virtual bool IsCommutative() { return false; }
1172
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001173 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001174 virtual bool InstructionDataEquals(HInstruction* other) const {
1175 UNUSED(other);
1176 return true;
1177 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001178
Roland Levillain9240d6a2014-10-20 16:47:04 +01001179 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01001180 // containing the result of this evaluation. If `operation` cannot
1181 // be evaluated as a constant, return nullptr.
Roland Levillain9240d6a2014-10-20 16:47:04 +01001182 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01001183
1184 // Apply this operation to `x` and `y`.
1185 virtual int32_t Evaluate(int32_t x, int32_t y) const = 0;
1186 virtual int64_t Evaluate(int64_t x, int64_t y) const = 0;
1187
Roland Levillainccc07a92014-09-16 14:48:16 +01001188 DECLARE_INSTRUCTION(BinaryOperation);
1189
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001190 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001191 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
1192};
1193
Dave Allison20dfc792014-06-16 20:44:29 -07001194class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001195 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001196 HCondition(HInstruction* first, HInstruction* second)
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001197 : HBinaryOperation(Primitive::kPrimBoolean, first, second),
1198 needs_materialization_(true) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001199
1200 virtual bool IsCommutative() { return true; }
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001201
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001202 bool NeedsMaterialization() const { return needs_materialization_; }
1203 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001204
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001205 // For code generation purposes, returns whether this instruction is just before
1206 // `if_`, and disregard moves in between.
1207 bool IsBeforeWhenDisregardMoves(HIf* if_) const;
1208
Dave Allison20dfc792014-06-16 20:44:29 -07001209 DECLARE_INSTRUCTION(Condition);
1210
1211 virtual IfCondition GetCondition() const = 0;
1212
1213 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001214 // For register allocation purposes, returns whether this instruction needs to be
1215 // materialized (that is, not just be in the processor flags).
1216 bool needs_materialization_;
1217
Dave Allison20dfc792014-06-16 20:44:29 -07001218 DISALLOW_COPY_AND_ASSIGN(HCondition);
1219};
1220
1221// Instruction to check if two inputs are equal to each other.
1222class HEqual : public HCondition {
1223 public:
1224 HEqual(HInstruction* first, HInstruction* second)
1225 : HCondition(first, second) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001226
Roland Levillain93445682014-10-06 19:24:02 +01001227 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1228 return x == y ? 1 : 0;
1229 }
1230 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1231 return x == y ? 1 : 0;
1232 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001233
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001234 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001235
Dave Allison20dfc792014-06-16 20:44:29 -07001236 virtual IfCondition GetCondition() const {
1237 return kCondEQ;
1238 }
1239
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001240 private:
1241 DISALLOW_COPY_AND_ASSIGN(HEqual);
1242};
1243
Dave Allison20dfc792014-06-16 20:44:29 -07001244class HNotEqual : public HCondition {
1245 public:
1246 HNotEqual(HInstruction* first, HInstruction* second)
1247 : HCondition(first, second) {}
1248
Roland Levillain93445682014-10-06 19:24:02 +01001249 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1250 return x != y ? 1 : 0;
1251 }
1252 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1253 return x != y ? 1 : 0;
1254 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001255
Dave Allison20dfc792014-06-16 20:44:29 -07001256 DECLARE_INSTRUCTION(NotEqual);
1257
1258 virtual IfCondition GetCondition() const {
1259 return kCondNE;
1260 }
1261
1262 private:
1263 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
1264};
1265
1266class HLessThan : public HCondition {
1267 public:
1268 HLessThan(HInstruction* first, HInstruction* second)
1269 : HCondition(first, second) {}
1270
Roland Levillain93445682014-10-06 19:24:02 +01001271 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1272 return x < y ? 1 : 0;
1273 }
1274 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1275 return x < y ? 1 : 0;
1276 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001277
Dave Allison20dfc792014-06-16 20:44:29 -07001278 DECLARE_INSTRUCTION(LessThan);
1279
1280 virtual IfCondition GetCondition() const {
1281 return kCondLT;
1282 }
1283
1284 private:
1285 DISALLOW_COPY_AND_ASSIGN(HLessThan);
1286};
1287
1288class HLessThanOrEqual : public HCondition {
1289 public:
1290 HLessThanOrEqual(HInstruction* first, HInstruction* second)
1291 : HCondition(first, second) {}
1292
Roland Levillain93445682014-10-06 19:24:02 +01001293 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1294 return x <= y ? 1 : 0;
1295 }
1296 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1297 return x <= y ? 1 : 0;
1298 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001299
Dave Allison20dfc792014-06-16 20:44:29 -07001300 DECLARE_INSTRUCTION(LessThanOrEqual);
1301
1302 virtual IfCondition GetCondition() const {
1303 return kCondLE;
1304 }
1305
1306 private:
1307 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
1308};
1309
1310class HGreaterThan : public HCondition {
1311 public:
1312 HGreaterThan(HInstruction* first, HInstruction* second)
1313 : HCondition(first, second) {}
1314
Roland Levillain93445682014-10-06 19:24:02 +01001315 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1316 return x > y ? 1 : 0;
1317 }
1318 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1319 return x > y ? 1 : 0;
1320 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001321
Dave Allison20dfc792014-06-16 20:44:29 -07001322 DECLARE_INSTRUCTION(GreaterThan);
1323
1324 virtual IfCondition GetCondition() const {
1325 return kCondGT;
1326 }
1327
1328 private:
1329 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
1330};
1331
1332class HGreaterThanOrEqual : public HCondition {
1333 public:
1334 HGreaterThanOrEqual(HInstruction* first, HInstruction* second)
1335 : HCondition(first, second) {}
1336
Roland Levillain93445682014-10-06 19:24:02 +01001337 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1338 return x >= y ? 1 : 0;
1339 }
1340 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1341 return x >= y ? 1 : 0;
1342 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001343
Dave Allison20dfc792014-06-16 20:44:29 -07001344 DECLARE_INSTRUCTION(GreaterThanOrEqual);
1345
1346 virtual IfCondition GetCondition() const {
1347 return kCondGE;
1348 }
1349
1350 private:
1351 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
1352};
1353
1354
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001355// Instruction to check how two inputs compare to each other.
1356// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
1357class HCompare : public HBinaryOperation {
1358 public:
1359 HCompare(Primitive::Type type, HInstruction* first, HInstruction* second)
1360 : HBinaryOperation(Primitive::kPrimInt, first, second) {
1361 DCHECK_EQ(type, first->GetType());
1362 DCHECK_EQ(type, second->GetType());
1363 }
1364
Roland Levillain93445682014-10-06 19:24:02 +01001365 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001366 return
1367 x == y ? 0 :
1368 x > y ? 1 :
1369 -1;
1370 }
Roland Levillain93445682014-10-06 19:24:02 +01001371 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001372 return
1373 x == y ? 0 :
1374 x > y ? 1 :
1375 -1;
1376 }
1377
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001378 DECLARE_INSTRUCTION(Compare);
1379
1380 private:
1381 DISALLOW_COPY_AND_ASSIGN(HCompare);
1382};
1383
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001384// A local in the graph. Corresponds to a Dex register.
1385class HLocal : public HTemplateInstruction<0> {
1386 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001387 explicit HLocal(uint16_t reg_number)
1388 : HTemplateInstruction(SideEffects::None()), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001389
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001390 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001391
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001392 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001393
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001394 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001395 // The Dex register number.
1396 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001397
1398 DISALLOW_COPY_AND_ASSIGN(HLocal);
1399};
1400
1401// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07001402class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001403 public:
Roland Levillain5799fc02014-09-25 12:15:20 +01001404 HLoadLocal(HLocal* local, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001405 : HExpression(type, SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001406 SetRawInputAt(0, local);
1407 }
1408
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001409 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1410
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001411 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001412
1413 private:
1414 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
1415};
1416
1417// Store a value in a given local. This instruction has two inputs: the value
1418// and the local.
1419class HStoreLocal : public HTemplateInstruction<2> {
1420 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001421 HStoreLocal(HLocal* local, HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001422 SetRawInputAt(0, local);
1423 SetRawInputAt(1, value);
1424 }
1425
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001426 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1427
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001428 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001429
1430 private:
1431 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
1432};
1433
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001434class HConstant : public HExpression<0> {
1435 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001436 explicit HConstant(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
1437
1438 virtual bool CanBeMoved() const { return true; }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001439
1440 DECLARE_INSTRUCTION(Constant);
1441
1442 private:
1443 DISALLOW_COPY_AND_ASSIGN(HConstant);
1444};
1445
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001446class HFloatConstant : public HConstant {
1447 public:
1448 explicit HFloatConstant(float value) : HConstant(Primitive::kPrimFloat), value_(value) {}
1449
1450 float GetValue() const { return value_; }
1451
1452 virtual bool InstructionDataEquals(HInstruction* other) const {
1453 return bit_cast<float, int32_t>(other->AsFloatConstant()->value_) ==
1454 bit_cast<float, int32_t>(value_);
1455 }
1456
1457 virtual size_t ComputeHashCode() const { return static_cast<size_t>(GetValue()); }
1458
1459 DECLARE_INSTRUCTION(FloatConstant);
1460
1461 private:
1462 const float value_;
1463
1464 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
1465};
1466
1467class HDoubleConstant : public HConstant {
1468 public:
1469 explicit HDoubleConstant(double value) : HConstant(Primitive::kPrimDouble), value_(value) {}
1470
1471 double GetValue() const { return value_; }
1472
1473 virtual bool InstructionDataEquals(HInstruction* other) const {
1474 return bit_cast<double, int64_t>(other->AsDoubleConstant()->value_) ==
1475 bit_cast<double, int64_t>(value_);
1476 }
1477
1478 virtual size_t ComputeHashCode() const { return static_cast<size_t>(GetValue()); }
1479
1480 DECLARE_INSTRUCTION(DoubleConstant);
1481
1482 private:
1483 const double value_;
1484
1485 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
1486};
1487
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001488// Constants of the type int. Those can be from Dex instructions, or
1489// synthesized (for example with the if-eqz instruction).
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001490class HIntConstant : public HConstant {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001491 public:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001492 explicit HIntConstant(int32_t value) : HConstant(Primitive::kPrimInt), value_(value) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001493
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001494 int32_t GetValue() const { return value_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001495
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001496 virtual bool InstructionDataEquals(HInstruction* other) const {
1497 return other->AsIntConstant()->value_ == value_;
1498 }
1499
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001500 virtual size_t ComputeHashCode() const { return GetValue(); }
1501
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001502 DECLARE_INSTRUCTION(IntConstant);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001503
1504 private:
1505 const int32_t value_;
1506
1507 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
1508};
1509
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001510class HLongConstant : public HConstant {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001511 public:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001512 explicit HLongConstant(int64_t value) : HConstant(Primitive::kPrimLong), value_(value) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001513
1514 int64_t GetValue() const { return value_; }
1515
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001516 virtual bool InstructionDataEquals(HInstruction* other) const {
1517 return other->AsLongConstant()->value_ == value_;
1518 }
1519
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001520 virtual size_t ComputeHashCode() const { return static_cast<size_t>(GetValue()); }
1521
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001522 DECLARE_INSTRUCTION(LongConstant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001523
1524 private:
1525 const int64_t value_;
1526
1527 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
1528};
1529
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001530class HInvoke : public HInstruction {
1531 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001532 HInvoke(ArenaAllocator* arena,
1533 uint32_t number_of_arguments,
1534 Primitive::Type return_type,
1535 uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001536 : HInstruction(SideEffects::All()),
1537 inputs_(arena, number_of_arguments),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001538 return_type_(return_type),
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001539 dex_pc_(dex_pc) {
1540 inputs_.SetSize(number_of_arguments);
1541 }
1542
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001543 virtual size_t InputCount() const { return inputs_.Size(); }
1544 virtual HInstruction* InputAt(size_t i) const { return inputs_.Get(i); }
1545
1546 // Runtime needs to walk the stack, so Dex -> Dex calls need to
1547 // know their environment.
1548 virtual bool NeedsEnvironment() const { return true; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001549
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001550 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001551 SetRawInputAt(index, argument);
1552 }
1553
1554 virtual void SetRawInputAt(size_t index, HInstruction* input) {
1555 inputs_.Put(index, input);
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001556 }
1557
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001558 virtual Primitive::Type GetType() const { return return_type_; }
1559
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001560 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001561
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001562 DECLARE_INSTRUCTION(Invoke);
1563
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001564 protected:
1565 GrowableArray<HInstruction*> inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001566 const Primitive::Type return_type_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001567 const uint32_t dex_pc_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001568
1569 private:
1570 DISALLOW_COPY_AND_ASSIGN(HInvoke);
1571};
1572
1573class HInvokeStatic : public HInvoke {
1574 public:
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001575 HInvokeStatic(ArenaAllocator* arena,
1576 uint32_t number_of_arguments,
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001577 Primitive::Type return_type,
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001578 uint32_t dex_pc,
1579 uint32_t index_in_dex_cache)
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001580 : HInvoke(arena, number_of_arguments, return_type, dex_pc),
1581 index_in_dex_cache_(index_in_dex_cache) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001582
1583 uint32_t GetIndexInDexCache() const { return index_in_dex_cache_; }
1584
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001585 DECLARE_INSTRUCTION(InvokeStatic);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001586
1587 private:
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001588 const uint32_t index_in_dex_cache_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001589
1590 DISALLOW_COPY_AND_ASSIGN(HInvokeStatic);
1591};
1592
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001593class HInvokeVirtual : public HInvoke {
1594 public:
1595 HInvokeVirtual(ArenaAllocator* arena,
1596 uint32_t number_of_arguments,
1597 Primitive::Type return_type,
1598 uint32_t dex_pc,
1599 uint32_t vtable_index)
1600 : HInvoke(arena, number_of_arguments, return_type, dex_pc),
1601 vtable_index_(vtable_index) {}
1602
1603 uint32_t GetVTableIndex() const { return vtable_index_; }
1604
1605 DECLARE_INSTRUCTION(InvokeVirtual);
1606
1607 private:
1608 const uint32_t vtable_index_;
1609
1610 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
1611};
1612
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001613class HInvokeInterface : public HInvoke {
1614 public:
1615 HInvokeInterface(ArenaAllocator* arena,
1616 uint32_t number_of_arguments,
1617 Primitive::Type return_type,
1618 uint32_t dex_pc,
1619 uint32_t dex_method_index,
1620 uint32_t imt_index)
1621 : HInvoke(arena, number_of_arguments, return_type, dex_pc),
1622 dex_method_index_(dex_method_index),
1623 imt_index_(imt_index) {}
1624
1625 uint32_t GetImtIndex() const { return imt_index_; }
1626 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
1627
1628 DECLARE_INSTRUCTION(InvokeInterface);
1629
1630 private:
1631 const uint32_t dex_method_index_;
1632 const uint32_t imt_index_;
1633
1634 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
1635};
1636
Dave Allison20dfc792014-06-16 20:44:29 -07001637class HNewInstance : public HExpression<0> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001638 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001639 HNewInstance(uint32_t dex_pc, uint16_t type_index)
1640 : HExpression(Primitive::kPrimNot, SideEffects::None()),
1641 dex_pc_(dex_pc),
1642 type_index_(type_index) {}
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001643
1644 uint32_t GetDexPc() const { return dex_pc_; }
1645 uint16_t GetTypeIndex() const { return type_index_; }
1646
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001647 // Calls runtime so needs an environment.
1648 virtual bool NeedsEnvironment() const { return true; }
1649
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001650 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001651
1652 private:
1653 const uint32_t dex_pc_;
1654 const uint16_t type_index_;
1655
1656 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
1657};
1658
Roland Levillain88cb1752014-10-20 16:36:47 +01001659class HNeg : public HUnaryOperation {
1660 public:
1661 explicit HNeg(Primitive::Type result_type, HInstruction* input)
1662 : HUnaryOperation(result_type, input) {}
1663
Roland Levillain9240d6a2014-10-20 16:47:04 +01001664 virtual int32_t Evaluate(int32_t x) const OVERRIDE { return -x; }
1665 virtual int64_t Evaluate(int64_t x) const OVERRIDE { return -x; }
1666
Roland Levillain88cb1752014-10-20 16:36:47 +01001667 DECLARE_INSTRUCTION(Neg);
1668
1669 private:
1670 DISALLOW_COPY_AND_ASSIGN(HNeg);
1671};
1672
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001673class HNewArray : public HExpression<1> {
1674 public:
1675 HNewArray(HInstruction* length, uint32_t dex_pc, uint16_t type_index)
1676 : HExpression(Primitive::kPrimNot, SideEffects::None()),
1677 dex_pc_(dex_pc),
1678 type_index_(type_index) {
1679 SetRawInputAt(0, length);
1680 }
1681
1682 uint32_t GetDexPc() const { return dex_pc_; }
1683 uint16_t GetTypeIndex() const { return type_index_; }
1684
1685 // Calls runtime so needs an environment.
1686 virtual bool NeedsEnvironment() const { return true; }
1687
1688 DECLARE_INSTRUCTION(NewArray);
1689
1690 private:
1691 const uint32_t dex_pc_;
1692 const uint16_t type_index_;
1693
1694 DISALLOW_COPY_AND_ASSIGN(HNewArray);
1695};
1696
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001697class HAdd : public HBinaryOperation {
1698 public:
1699 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1700 : HBinaryOperation(result_type, left, right) {}
1701
1702 virtual bool IsCommutative() { return true; }
1703
Roland Levillain93445682014-10-06 19:24:02 +01001704 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1705 return x + y;
1706 }
1707 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1708 return x + y;
1709 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001710
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001711 DECLARE_INSTRUCTION(Add);
1712
1713 private:
1714 DISALLOW_COPY_AND_ASSIGN(HAdd);
1715};
1716
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001717class HSub : public HBinaryOperation {
1718 public:
1719 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1720 : HBinaryOperation(result_type, left, right) {}
1721
Roland Levillain93445682014-10-06 19:24:02 +01001722 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1723 return x - y;
1724 }
1725 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1726 return x - y;
1727 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001728
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001729 DECLARE_INSTRUCTION(Sub);
1730
1731 private:
1732 DISALLOW_COPY_AND_ASSIGN(HSub);
1733};
1734
Calin Juravle34bacdf2014-10-07 20:23:36 +01001735class HMul : public HBinaryOperation {
1736 public:
1737 HMul(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1738 : HBinaryOperation(result_type, left, right) {}
1739
1740 virtual bool IsCommutative() { return true; }
1741
1742 virtual int32_t Evaluate(int32_t x, int32_t y) const { return x * y; }
1743 virtual int64_t Evaluate(int64_t x, int64_t y) const { return x * y; }
1744
1745 DECLARE_INSTRUCTION(Mul);
1746
1747 private:
1748 DISALLOW_COPY_AND_ASSIGN(HMul);
1749};
1750
Calin Juravle7c4954d2014-10-28 16:57:40 +00001751class HDiv : public HBinaryOperation {
1752 public:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001753 HDiv(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
1754 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00001755
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00001756 virtual int32_t Evaluate(int32_t x, int32_t y) const {
1757 // Our graph structure ensures we never have 0 for `y` during constant folding.
1758 DCHECK_NE(y, 0);
1759 // Special case -1 to avoid getting a SIGFPE on x86.
1760 return (y == -1) ? -x : x / y;
1761 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00001762 virtual int64_t Evaluate(int64_t x, int64_t y) const { return x / y; }
1763
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001764 uint32_t GetDexPc() const { return dex_pc_; }
1765
Calin Juravle7c4954d2014-10-28 16:57:40 +00001766 DECLARE_INSTRUCTION(Div);
1767
1768 private:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001769 const uint32_t dex_pc_;
1770
Calin Juravle7c4954d2014-10-28 16:57:40 +00001771 DISALLOW_COPY_AND_ASSIGN(HDiv);
1772};
1773
Calin Juravled0d48522014-11-04 16:40:20 +00001774class HDivZeroCheck : public HExpression<1> {
1775 public:
1776 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
1777 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
1778 SetRawInputAt(0, value);
1779 }
1780
1781 bool CanBeMoved() const OVERRIDE { return true; }
1782
1783 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
1784 UNUSED(other);
1785 return true;
1786 }
1787
1788 bool NeedsEnvironment() const OVERRIDE { return true; }
1789 bool CanThrow() const OVERRIDE { return true; }
1790
1791 uint32_t GetDexPc() const { return dex_pc_; }
1792
1793 DECLARE_INSTRUCTION(DivZeroCheck);
1794
1795 private:
1796 const uint32_t dex_pc_;
1797
1798 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
1799};
1800
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001801class HAnd : public HBinaryOperation {
1802 public:
1803 HAnd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1804 : HBinaryOperation(result_type, left, right) {}
1805
1806 bool IsCommutative() OVERRIDE { return true; }
1807
1808 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x & y; }
1809 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x & y; }
1810
1811 DECLARE_INSTRUCTION(And);
1812
1813 private:
1814 DISALLOW_COPY_AND_ASSIGN(HAnd);
1815};
1816
1817class HOr : public HBinaryOperation {
1818 public:
1819 HOr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1820 : HBinaryOperation(result_type, left, right) {}
1821
1822 bool IsCommutative() OVERRIDE { return true; }
1823
1824 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x | y; }
1825 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x | y; }
1826
1827 DECLARE_INSTRUCTION(Or);
1828
1829 private:
1830 DISALLOW_COPY_AND_ASSIGN(HOr);
1831};
1832
1833class HXor : public HBinaryOperation {
1834 public:
1835 HXor(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1836 : HBinaryOperation(result_type, left, right) {}
1837
1838 bool IsCommutative() OVERRIDE { return true; }
1839
1840 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x ^ y; }
1841 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x ^ y; }
1842
1843 DECLARE_INSTRUCTION(Xor);
1844
1845 private:
1846 DISALLOW_COPY_AND_ASSIGN(HXor);
1847};
1848
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001849// The value of a parameter in this method. Its location depends on
1850// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07001851class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001852 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001853 HParameterValue(uint8_t index, Primitive::Type parameter_type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001854 : HExpression(parameter_type, SideEffects::None()), index_(index) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001855
1856 uint8_t GetIndex() const { return index_; }
1857
1858 DECLARE_INSTRUCTION(ParameterValue);
1859
1860 private:
1861 // The index of this parameter in the parameters list. Must be less
1862 // than HGraph::number_of_in_vregs_;
1863 const uint8_t index_;
1864
1865 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
1866};
1867
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001868class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001869 public:
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001870 explicit HNot(Primitive::Type result_type, HInstruction* input)
1871 : HUnaryOperation(result_type, input) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001872
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001873 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001874 virtual bool InstructionDataEquals(HInstruction* other) const {
1875 UNUSED(other);
1876 return true;
1877 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001878
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001879 virtual int32_t Evaluate(int32_t x) const OVERRIDE { return ~x; }
1880 virtual int64_t Evaluate(int64_t x) const OVERRIDE { return ~x; }
1881
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001882 DECLARE_INSTRUCTION(Not);
1883
1884 private:
1885 DISALLOW_COPY_AND_ASSIGN(HNot);
1886};
1887
Roland Levillaindff1f282014-11-05 14:15:05 +00001888class HTypeConversion : public HExpression<1> {
1889 public:
1890 // Instantiate a type conversion of `input` to `result_type`.
1891 HTypeConversion(Primitive::Type result_type, HInstruction* input)
1892 : HExpression(result_type, SideEffects::None()) {
1893 SetRawInputAt(0, input);
1894 DCHECK_NE(input->GetType(), result_type);
1895 }
1896
1897 HInstruction* GetInput() const { return InputAt(0); }
1898 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
1899 Primitive::Type GetResultType() const { return GetType(); }
1900
1901 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00001902 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00001903
1904 DECLARE_INSTRUCTION(TypeConversion);
1905
1906 private:
1907 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
1908};
1909
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001910class HPhi : public HInstruction {
1911 public:
1912 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001913 : HInstruction(SideEffects::None()),
1914 inputs_(arena, number_of_inputs),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001915 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001916 type_(type),
1917 is_live_(false) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001918 inputs_.SetSize(number_of_inputs);
1919 }
1920
1921 virtual size_t InputCount() const { return inputs_.Size(); }
1922 virtual HInstruction* InputAt(size_t i) const { return inputs_.Get(i); }
1923
1924 virtual void SetRawInputAt(size_t index, HInstruction* input) {
1925 inputs_.Put(index, input);
1926 }
1927
1928 void AddInput(HInstruction* input);
1929
1930 virtual Primitive::Type GetType() const { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001931 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001932
1933 uint32_t GetRegNumber() const { return reg_number_; }
1934
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001935 void SetDead() { is_live_ = false; }
1936 void SetLive() { is_live_ = true; }
1937 bool IsDead() const { return !is_live_; }
1938 bool IsLive() const { return is_live_; }
1939
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001940 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001941
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001942 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001943 GrowableArray<HInstruction*> inputs_;
1944 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001945 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001946 bool is_live_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001947
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001948 DISALLOW_COPY_AND_ASSIGN(HPhi);
1949};
1950
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001951class HNullCheck : public HExpression<1> {
1952 public:
1953 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001954 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001955 SetRawInputAt(0, value);
1956 }
1957
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001958 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001959 virtual bool InstructionDataEquals(HInstruction* other) const {
1960 UNUSED(other);
1961 return true;
1962 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001963
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001964 virtual bool NeedsEnvironment() const { return true; }
1965
Roland Levillaine161a2a2014-10-03 12:45:18 +01001966 virtual bool CanThrow() const { return true; }
1967
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001968 uint32_t GetDexPc() const { return dex_pc_; }
1969
1970 DECLARE_INSTRUCTION(NullCheck);
1971
1972 private:
1973 const uint32_t dex_pc_;
1974
1975 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
1976};
1977
1978class FieldInfo : public ValueObject {
1979 public:
Roland Levillain5799fc02014-09-25 12:15:20 +01001980 FieldInfo(MemberOffset field_offset, Primitive::Type field_type)
Nicolas Geoffray39468442014-09-02 15:17:15 +01001981 : field_offset_(field_offset), field_type_(field_type) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001982
1983 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001984 Primitive::Type GetFieldType() const { return field_type_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001985
1986 private:
1987 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01001988 const Primitive::Type field_type_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001989};
1990
1991class HInstanceFieldGet : public HExpression<1> {
1992 public:
1993 HInstanceFieldGet(HInstruction* value,
1994 Primitive::Type field_type,
1995 MemberOffset field_offset)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001996 : HExpression(field_type, SideEffects::DependsOnSomething()),
1997 field_info_(field_offset, field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001998 SetRawInputAt(0, value);
1999 }
2000
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002001 virtual bool CanBeMoved() const { return true; }
2002 virtual bool InstructionDataEquals(HInstruction* other) const {
2003 size_t other_offset = other->AsInstanceFieldGet()->GetFieldOffset().SizeValue();
2004 return other_offset == GetFieldOffset().SizeValue();
2005 }
2006
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002007 virtual size_t ComputeHashCode() const {
2008 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
2009 }
2010
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002011 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002012 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002013
2014 DECLARE_INSTRUCTION(InstanceFieldGet);
2015
2016 private:
2017 const FieldInfo field_info_;
2018
2019 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
2020};
2021
2022class HInstanceFieldSet : public HTemplateInstruction<2> {
2023 public:
2024 HInstanceFieldSet(HInstruction* object,
2025 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01002026 Primitive::Type field_type,
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002027 MemberOffset field_offset)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002028 : HTemplateInstruction(SideEffects::ChangesSomething()),
2029 field_info_(field_offset, field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002030 SetRawInputAt(0, object);
2031 SetRawInputAt(1, value);
2032 }
2033
2034 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002035 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002036
2037 DECLARE_INSTRUCTION(InstanceFieldSet);
2038
2039 private:
2040 const FieldInfo field_info_;
2041
2042 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
2043};
2044
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002045class HArrayGet : public HExpression<2> {
2046 public:
2047 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002048 : HExpression(type, SideEffects::DependsOnSomething()) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002049 SetRawInputAt(0, array);
2050 SetRawInputAt(1, index);
2051 }
2052
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002053 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002054 virtual bool InstructionDataEquals(HInstruction* other) const {
2055 UNUSED(other);
2056 return true;
2057 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002058 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002059
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002060 DECLARE_INSTRUCTION(ArrayGet);
2061
2062 private:
2063 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
2064};
2065
2066class HArraySet : public HTemplateInstruction<3> {
2067 public:
2068 HArraySet(HInstruction* array,
2069 HInstruction* index,
2070 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002071 Primitive::Type expected_component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002072 uint32_t dex_pc)
2073 : HTemplateInstruction(SideEffects::ChangesSomething()),
2074 dex_pc_(dex_pc),
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002075 expected_component_type_(expected_component_type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002076 SetRawInputAt(0, array);
2077 SetRawInputAt(1, index);
2078 SetRawInputAt(2, value);
2079 }
2080
2081 virtual bool NeedsEnvironment() const {
2082 // We currently always call a runtime method to catch array store
2083 // exceptions.
2084 return InputAt(2)->GetType() == Primitive::kPrimNot;
2085 }
2086
2087 uint32_t GetDexPc() const { return dex_pc_; }
2088
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002089 HInstruction* GetValue() const { return InputAt(2); }
2090
2091 Primitive::Type GetComponentType() const {
2092 // The Dex format does not type floating point index operations. Since the
2093 // `expected_component_type_` is set during building and can therefore not
2094 // be correct, we also check what is the value type. If it is a floating
2095 // point type, we must use that type.
2096 Primitive::Type value_type = GetValue()->GetType();
2097 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
2098 ? value_type
2099 : expected_component_type_;
2100 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002101
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002102 DECLARE_INSTRUCTION(ArraySet);
2103
2104 private:
2105 const uint32_t dex_pc_;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002106 const Primitive::Type expected_component_type_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002107
2108 DISALLOW_COPY_AND_ASSIGN(HArraySet);
2109};
2110
2111class HArrayLength : public HExpression<1> {
2112 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002113 explicit HArrayLength(HInstruction* array)
2114 : HExpression(Primitive::kPrimInt, SideEffects::None()) {
2115 // Note that arrays do not change length, so the instruction does not
2116 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002117 SetRawInputAt(0, array);
2118 }
2119
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002120 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002121 virtual bool InstructionDataEquals(HInstruction* other) const {
2122 UNUSED(other);
2123 return true;
2124 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002125
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002126 DECLARE_INSTRUCTION(ArrayLength);
2127
2128 private:
2129 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
2130};
2131
2132class HBoundsCheck : public HExpression<2> {
2133 public:
2134 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002135 : HExpression(index->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002136 DCHECK(index->GetType() == Primitive::kPrimInt);
2137 SetRawInputAt(0, index);
2138 SetRawInputAt(1, length);
2139 }
2140
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002141 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002142 virtual bool InstructionDataEquals(HInstruction* other) const {
2143 UNUSED(other);
2144 return true;
2145 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002146
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002147 virtual bool NeedsEnvironment() const { return true; }
2148
Roland Levillaine161a2a2014-10-03 12:45:18 +01002149 virtual bool CanThrow() const { return true; }
2150
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002151 uint32_t GetDexPc() const { return dex_pc_; }
2152
2153 DECLARE_INSTRUCTION(BoundsCheck);
2154
2155 private:
2156 const uint32_t dex_pc_;
2157
2158 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
2159};
2160
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002161/**
2162 * Some DEX instructions are folded into multiple HInstructions that need
2163 * to stay live until the last HInstruction. This class
2164 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00002165 * HInstruction stays live. `index` represents the stack location index of the
2166 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002167 */
2168class HTemporary : public HTemplateInstruction<0> {
2169 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002170 explicit HTemporary(size_t index) : HTemplateInstruction(SideEffects::None()), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002171
2172 size_t GetIndex() const { return index_; }
2173
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00002174 Primitive::Type GetType() const OVERRIDE {
2175 // The previous instruction is the one that will be stored in the temporary location.
2176 DCHECK(GetPrevious() != nullptr);
2177 return GetPrevious()->GetType();
2178 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00002179
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002180 DECLARE_INSTRUCTION(Temporary);
2181
2182 private:
2183 const size_t index_;
2184
2185 DISALLOW_COPY_AND_ASSIGN(HTemporary);
2186};
2187
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002188class HSuspendCheck : public HTemplateInstruction<0> {
2189 public:
2190 explicit HSuspendCheck(uint32_t dex_pc)
Nicolas Geoffray9ebc72c2014-09-25 16:33:42 +01002191 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002192
2193 virtual bool NeedsEnvironment() const {
2194 return true;
2195 }
2196
2197 uint32_t GetDexPc() const { return dex_pc_; }
2198
2199 DECLARE_INSTRUCTION(SuspendCheck);
2200
2201 private:
2202 const uint32_t dex_pc_;
2203
2204 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
2205};
2206
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002207/**
2208 * Instruction to load a Class object.
2209 */
2210class HLoadClass : public HExpression<0> {
2211 public:
2212 HLoadClass(uint16_t type_index,
2213 bool is_referrers_class,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002214 uint32_t dex_pc)
2215 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2216 type_index_(type_index),
2217 is_referrers_class_(is_referrers_class),
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002218 dex_pc_(dex_pc),
2219 generate_clinit_check_(false) {}
2220
2221 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002222
2223 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2224 return other->AsLoadClass()->type_index_ == type_index_;
2225 }
2226
2227 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
2228
2229 uint32_t GetDexPc() const { return dex_pc_; }
2230 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002231 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002232
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002233 bool NeedsEnvironment() const OVERRIDE {
2234 // Will call runtime and load the class if the class is not loaded yet.
2235 // TODO: finer grain decision.
2236 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002237 }
2238
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002239 bool MustGenerateClinitCheck() const {
2240 return generate_clinit_check_;
2241 }
2242
2243 void SetMustGenerateClinitCheck() {
2244 generate_clinit_check_ = true;
2245 }
2246
2247 bool CanCallRuntime() const {
2248 return MustGenerateClinitCheck() || !is_referrers_class_;
2249 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002250
2251 DECLARE_INSTRUCTION(LoadClass);
2252
2253 private:
2254 const uint16_t type_index_;
2255 const bool is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002256 const uint32_t dex_pc_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002257 // Whether this instruction must generate the initialization check.
2258 // Used for code generation.
2259 bool generate_clinit_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002260
2261 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
2262};
2263
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002264class HLoadString : public HExpression<0> {
2265 public:
2266 HLoadString(uint32_t string_index, uint32_t dex_pc)
2267 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2268 string_index_(string_index),
2269 dex_pc_(dex_pc) {}
2270
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002271 bool CanBeMoved() const OVERRIDE { return true; }
2272
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002273 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2274 return other->AsLoadString()->string_index_ == string_index_;
2275 }
2276
2277 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
2278
2279 uint32_t GetDexPc() const { return dex_pc_; }
2280 uint32_t GetStringIndex() const { return string_index_; }
2281
2282 // TODO: Can we deopt or debug when we resolve a string?
2283 bool NeedsEnvironment() const OVERRIDE { return false; }
2284
2285 DECLARE_INSTRUCTION(LoadString);
2286
2287 private:
2288 const uint32_t string_index_;
2289 const uint32_t dex_pc_;
2290
2291 DISALLOW_COPY_AND_ASSIGN(HLoadString);
2292};
2293
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002294// TODO: Pass this check to HInvokeStatic nodes.
2295/**
2296 * Performs an initialization check on its Class object input.
2297 */
2298class HClinitCheck : public HExpression<1> {
2299 public:
2300 explicit HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
2301 : HExpression(Primitive::kPrimNot, SideEffects::All()),
2302 dex_pc_(dex_pc) {
2303 SetRawInputAt(0, constant);
2304 }
2305
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002306 bool CanBeMoved() const OVERRIDE { return true; }
2307 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2308 UNUSED(other);
2309 return true;
2310 }
2311
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002312 bool NeedsEnvironment() const OVERRIDE {
2313 // May call runtime to initialize the class.
2314 return true;
2315 }
2316
2317 uint32_t GetDexPc() const { return dex_pc_; }
2318
2319 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
2320
2321 DECLARE_INSTRUCTION(ClinitCheck);
2322
2323 private:
2324 const uint32_t dex_pc_;
2325
2326 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
2327};
2328
2329class HStaticFieldGet : public HExpression<1> {
2330 public:
2331 HStaticFieldGet(HInstruction* cls,
2332 Primitive::Type field_type,
2333 MemberOffset field_offset)
2334 : HExpression(field_type, SideEffects::DependsOnSomething()),
2335 field_info_(field_offset, field_type) {
2336 SetRawInputAt(0, cls);
2337 }
2338
2339 bool CanBeMoved() const OVERRIDE { return true; }
2340 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2341 size_t other_offset = other->AsStaticFieldGet()->GetFieldOffset().SizeValue();
2342 return other_offset == GetFieldOffset().SizeValue();
2343 }
2344
2345 size_t ComputeHashCode() const OVERRIDE {
2346 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
2347 }
2348
2349 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
2350 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
2351
2352 DECLARE_INSTRUCTION(StaticFieldGet);
2353
2354 private:
2355 const FieldInfo field_info_;
2356
2357 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
2358};
2359
2360class HStaticFieldSet : public HTemplateInstruction<2> {
2361 public:
2362 HStaticFieldSet(HInstruction* cls,
2363 HInstruction* value,
2364 Primitive::Type field_type,
2365 MemberOffset field_offset)
2366 : HTemplateInstruction(SideEffects::ChangesSomething()),
2367 field_info_(field_offset, field_type) {
2368 SetRawInputAt(0, cls);
2369 SetRawInputAt(1, value);
2370 }
2371
2372 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
2373 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
2374
2375 DECLARE_INSTRUCTION(StaticFieldSet);
2376
2377 private:
2378 const FieldInfo field_info_;
2379
2380 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
2381};
2382
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002383// Implement the move-exception DEX instruction.
2384class HLoadException : public HExpression<0> {
2385 public:
2386 HLoadException() : HExpression(Primitive::kPrimNot, SideEffects::None()) {}
2387
2388 DECLARE_INSTRUCTION(LoadException);
2389
2390 private:
2391 DISALLOW_COPY_AND_ASSIGN(HLoadException);
2392};
2393
2394class HThrow : public HTemplateInstruction<1> {
2395 public:
2396 HThrow(HInstruction* exception, uint32_t dex_pc)
2397 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {
2398 SetRawInputAt(0, exception);
2399 }
2400
2401 bool IsControlFlow() const OVERRIDE { return true; }
2402
2403 bool NeedsEnvironment() const OVERRIDE { return true; }
2404
2405 uint32_t GetDexPc() const { return dex_pc_; }
2406
2407 DECLARE_INSTRUCTION(Throw);
2408
2409 private:
2410 uint32_t dex_pc_;
2411
2412 DISALLOW_COPY_AND_ASSIGN(HThrow);
2413};
2414
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002415class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002416 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002417 HInstanceOf(HInstruction* object,
2418 HLoadClass* constant,
2419 bool class_is_final,
2420 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002421 : HExpression(Primitive::kPrimBoolean, SideEffects::None()),
2422 class_is_final_(class_is_final),
2423 dex_pc_(dex_pc) {
2424 SetRawInputAt(0, object);
2425 SetRawInputAt(1, constant);
2426 }
2427
2428 bool CanBeMoved() const OVERRIDE { return true; }
2429
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002430 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002431 return true;
2432 }
2433
2434 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002435 return false;
2436 }
2437
2438 uint32_t GetDexPc() const { return dex_pc_; }
2439
2440 bool IsClassFinal() const { return class_is_final_; }
2441
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002442 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002443
2444 private:
2445 const bool class_is_final_;
2446 const uint32_t dex_pc_;
2447
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002448 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
2449};
2450
2451class HCheckCast : public HTemplateInstruction<2> {
2452 public:
2453 HCheckCast(HInstruction* object,
2454 HLoadClass* constant,
2455 bool class_is_final,
2456 uint32_t dex_pc)
2457 : HTemplateInstruction(SideEffects::None()),
2458 class_is_final_(class_is_final),
2459 dex_pc_(dex_pc) {
2460 SetRawInputAt(0, object);
2461 SetRawInputAt(1, constant);
2462 }
2463
2464 bool CanBeMoved() const OVERRIDE { return true; }
2465
2466 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2467 return true;
2468 }
2469
2470 bool NeedsEnvironment() const OVERRIDE {
2471 // Instruction may throw a CheckCastError.
2472 return true;
2473 }
2474
2475 bool CanThrow() const OVERRIDE { return true; }
2476
2477 uint32_t GetDexPc() const { return dex_pc_; }
2478
2479 bool IsClassFinal() const { return class_is_final_; }
2480
2481 DECLARE_INSTRUCTION(CheckCast);
2482
2483 private:
2484 const bool class_is_final_;
2485 const uint32_t dex_pc_;
2486
2487 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002488};
2489
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00002490class HMonitorOperation : public HTemplateInstruction<1> {
2491 public:
2492 enum OperationKind {
2493 kEnter,
2494 kExit,
2495 };
2496
2497 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
2498 : HTemplateInstruction(SideEffects::None()), kind_(kind), dex_pc_(dex_pc) {
2499 SetRawInputAt(0, object);
2500 }
2501
2502 // Instruction may throw a Java exception, so we need an environment.
2503 bool NeedsEnvironment() const OVERRIDE { return true; }
2504 bool CanThrow() const OVERRIDE { return true; }
2505
2506 uint32_t GetDexPc() const { return dex_pc_; }
2507
2508 bool IsEnter() const { return kind_ == kEnter; }
2509
2510 DECLARE_INSTRUCTION(MonitorOperation);
2511
2512 protected:
2513 const OperationKind kind_;
2514 const uint32_t dex_pc_;
2515
2516 private:
2517 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
2518};
2519
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002520
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002521class MoveOperands : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002522 public:
Nicolas Geoffray740475d2014-09-29 10:33:25 +01002523 MoveOperands(Location source, Location destination, HInstruction* instruction)
2524 : source_(source), destination_(destination), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002525
2526 Location GetSource() const { return source_; }
2527 Location GetDestination() const { return destination_; }
2528
2529 void SetSource(Location value) { source_ = value; }
2530 void SetDestination(Location value) { destination_ = value; }
2531
2532 // The parallel move resolver marks moves as "in-progress" by clearing the
2533 // destination (but not the source).
2534 Location MarkPending() {
2535 DCHECK(!IsPending());
2536 Location dest = destination_;
2537 destination_ = Location::NoLocation();
2538 return dest;
2539 }
2540
2541 void ClearPending(Location dest) {
2542 DCHECK(IsPending());
2543 destination_ = dest;
2544 }
2545
2546 bool IsPending() const {
2547 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
2548 return destination_.IsInvalid() && !source_.IsInvalid();
2549 }
2550
2551 // True if this blocks a move from the given location.
2552 bool Blocks(Location loc) const {
2553 return !IsEliminated() && source_.Equals(loc);
2554 }
2555
2556 // A move is redundant if it's been eliminated, if its source and
2557 // destination are the same, or if its destination is unneeded.
2558 bool IsRedundant() const {
2559 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
2560 }
2561
2562 // We clear both operands to indicate move that's been eliminated.
2563 void Eliminate() {
2564 source_ = destination_ = Location::NoLocation();
2565 }
2566
2567 bool IsEliminated() const {
2568 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
2569 return source_.IsInvalid();
2570 }
2571
Nicolas Geoffray740475d2014-09-29 10:33:25 +01002572 HInstruction* GetInstruction() const { return instruction_; }
2573
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002574 private:
2575 Location source_;
2576 Location destination_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01002577 // The instruction this move is assocatied with. Null when this move is
2578 // for moving an input in the expected locations of user (including a phi user).
2579 // This is only used in debug mode, to ensure we do not connect interval siblings
2580 // in the same parallel move.
2581 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002582
2583 DISALLOW_COPY_AND_ASSIGN(MoveOperands);
2584};
2585
2586static constexpr size_t kDefaultNumberOfMoves = 4;
2587
2588class HParallelMove : public HTemplateInstruction<0> {
2589 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002590 explicit HParallelMove(ArenaAllocator* arena)
2591 : HTemplateInstruction(SideEffects::None()), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002592
2593 void AddMove(MoveOperands* move) {
Nicolas Geoffray740475d2014-09-29 10:33:25 +01002594 if (kIsDebugBuild && move->GetInstruction() != nullptr) {
2595 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
2596 DCHECK_NE(moves_.Get(i)->GetInstruction(), move->GetInstruction())
2597 << "Doing parallel moves for the same instruction.";
2598 }
2599 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002600 moves_.Add(move);
2601 }
2602
2603 MoveOperands* MoveOperandsAt(size_t index) const {
2604 return moves_.Get(index);
2605 }
2606
2607 size_t NumMoves() const { return moves_.Size(); }
2608
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002609 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002610
2611 private:
2612 GrowableArray<MoveOperands*> moves_;
2613
2614 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
2615};
2616
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002617class HGraphVisitor : public ValueObject {
2618 public:
Dave Allison20dfc792014-06-16 20:44:29 -07002619 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
2620 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002621
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002622 virtual void VisitInstruction(HInstruction* instruction) { UNUSED(instruction); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002623 virtual void VisitBasicBlock(HBasicBlock* block);
2624
Roland Levillain633021e2014-10-01 14:12:25 +01002625 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002626 void VisitInsertionOrder();
2627
Roland Levillain633021e2014-10-01 14:12:25 +01002628 // Visit the graph following dominator tree reverse post-order.
2629 void VisitReversePostOrder();
2630
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002631 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002632
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002633 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002634#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002635 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
2636
2637 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
2638
2639#undef DECLARE_VISIT_INSTRUCTION
2640
2641 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07002642 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002643
2644 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
2645};
2646
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002647class HGraphDelegateVisitor : public HGraphVisitor {
2648 public:
2649 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
2650 virtual ~HGraphDelegateVisitor() {}
2651
2652 // Visit functions that delegate to to super class.
2653#define DECLARE_VISIT_INSTRUCTION(name, super) \
2654 virtual void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
2655
2656 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
2657
2658#undef DECLARE_VISIT_INSTRUCTION
2659
2660 private:
2661 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
2662};
2663
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002664class HInsertionOrderIterator : public ValueObject {
2665 public:
2666 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
2667
2668 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
2669 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
2670 void Advance() { ++index_; }
2671
2672 private:
2673 const HGraph& graph_;
2674 size_t index_;
2675
2676 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
2677};
2678
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002679class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002680 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002681 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002682
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002683 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
2684 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002685 void Advance() { ++index_; }
2686
2687 private:
2688 const HGraph& graph_;
2689 size_t index_;
2690
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002691 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002692};
2693
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002694class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002695 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002696 explicit HPostOrderIterator(const HGraph& graph)
2697 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002698
2699 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002700 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002701 void Advance() { --index_; }
2702
2703 private:
2704 const HGraph& graph_;
2705 size_t index_;
2706
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002707 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002708};
2709
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002710} // namespace art
2711
2712#endif // ART_COMPILER_OPTIMIZING_NODES_H_