blob: 9253f0b7403cdb1c02ede8781f1ead7b980044d7 [file] [log] [blame]
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ART_COMPILER_OPTIMIZING_NODES_H_
18#define ART_COMPILER_OPTIMIZING_NODES_H_
19
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010020#include "locations.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010021#include "offsets.h"
Ian Rogerse63db272014-07-15 15:36:11 -070022#include "primitive.h"
Ian Rogers0279ebb2014-10-08 17:27:48 -070023#include "utils/arena_object.h"
Nicolas Geoffray0e336432014-02-26 18:24:38 +000024#include "utils/arena_bit_vector.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000025#include "utils/growable_array.h"
26
27namespace art {
28
29class HBasicBlock;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010030class HEnvironment;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000031class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000032class HIntConstant;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000033class HGraphVisitor;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010034class HPhi;
Nicolas Geoffray3c049742014-09-24 18:10:46 +010035class HSuspendCheck;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010036class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000037class LocationSummary;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000038
39static const int kDefaultNumberOfBlocks = 8;
40static const int kDefaultNumberOfSuccessors = 2;
41static const int kDefaultNumberOfPredecessors = 2;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010042static const int kDefaultNumberOfDominatedBlocks = 1;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000043static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000044
Dave Allison20dfc792014-06-16 20:44:29 -070045enum IfCondition {
46 kCondEQ,
47 kCondNE,
48 kCondLT,
49 kCondLE,
50 kCondGT,
51 kCondGE,
52};
53
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010054class HInstructionList {
55 public:
56 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
57
58 void AddInstruction(HInstruction* instruction);
59 void RemoveInstruction(HInstruction* instruction);
60
Roland Levillain6b469232014-09-25 10:10:38 +010061 // Return true if this list contains `instruction`.
62 bool Contains(HInstruction* instruction) const;
63
Roland Levillainccc07a92014-09-16 14:48:16 +010064 // Return true if `instruction1` is found before `instruction2` in
65 // this instruction list and false otherwise. Abort if none
66 // of these instructions is found.
67 bool FoundBefore(const HInstruction* instruction1,
68 const HInstruction* instruction2) const;
69
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010070 private:
71 HInstruction* first_instruction_;
72 HInstruction* last_instruction_;
73
74 friend class HBasicBlock;
75 friend class HInstructionIterator;
Nicolas Geoffray804d0932014-05-02 08:46:00 +010076 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010077
78 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
79};
80
Nicolas Geoffray818f2102014-02-18 16:43:35 +000081// Control-flow graph of a method. Contains a list of basic blocks.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070082class HGraph : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +000083 public:
84 explicit HGraph(ArenaAllocator* arena)
85 : arena_(arena),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000086 blocks_(arena, kDefaultNumberOfBlocks),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +010087 reverse_post_order_(arena, kDefaultNumberOfBlocks),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070088 entry_block_(nullptr),
89 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +010090 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +010091 number_of_vregs_(0),
92 number_of_in_vregs_(0),
Nicolas Geoffraye5038322014-07-04 09:41:32 +010093 number_of_temporaries_(0),
Dave Allison20dfc792014-06-16 20:44:29 -070094 current_instruction_id_(0) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +000095
Nicolas Geoffray787c3072014-03-17 10:20:19 +000096 ArenaAllocator* GetArena() const { return arena_; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +010097 const GrowableArray<HBasicBlock*>& GetBlocks() const { return blocks_; }
Roland Levillain93445682014-10-06 19:24:02 +010098 HBasicBlock* GetBlock(size_t id) const { return blocks_.Get(id); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +000099
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000100 HBasicBlock* GetEntryBlock() const { return entry_block_; }
101 HBasicBlock* GetExitBlock() const { return exit_block_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000102
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000103 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
104 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000105
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000106 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100107
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000108 void BuildDominatorTree();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100109 void TransformToSSA();
110 void SimplifyCFG();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000111
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100112 // Find all natural loops in this graph. Aborts computation and returns false
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100113 // if one loop is not natural, that is the header does not dominate the back
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100114 // edge.
115 bool FindNaturalLoops() const;
116
117 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
118 void SimplifyLoop(HBasicBlock* header);
119
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000120 int GetNextInstructionId() {
121 return current_instruction_id_++;
122 }
123
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100124 uint16_t GetMaximumNumberOfOutVRegs() const {
125 return maximum_number_of_out_vregs_;
126 }
127
128 void UpdateMaximumNumberOfOutVRegs(uint16_t new_value) {
129 maximum_number_of_out_vregs_ = std::max(new_value, maximum_number_of_out_vregs_);
130 }
131
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100132 void UpdateNumberOfTemporaries(size_t count) {
133 number_of_temporaries_ = std::max(count, number_of_temporaries_);
134 }
135
136 size_t GetNumberOfTemporaries() const {
137 return number_of_temporaries_;
138 }
139
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100140 void SetNumberOfVRegs(uint16_t number_of_vregs) {
141 number_of_vregs_ = number_of_vregs;
142 }
143
144 uint16_t GetNumberOfVRegs() const {
145 return number_of_vregs_;
146 }
147
148 void SetNumberOfInVRegs(uint16_t value) {
149 number_of_in_vregs_ = value;
150 }
151
152 uint16_t GetNumberOfInVRegs() const {
153 return number_of_in_vregs_;
154 }
155
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100156 uint16_t GetNumberOfLocalVRegs() const {
157 return number_of_vregs_ - number_of_in_vregs_;
158 }
159
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100160 const GrowableArray<HBasicBlock*>& GetReversePostOrder() const {
161 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100162 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100163
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000164 private:
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000165 HBasicBlock* FindCommonDominator(HBasicBlock* first, HBasicBlock* second) const;
166 void VisitBlockForDominatorTree(HBasicBlock* block,
167 HBasicBlock* predecessor,
168 GrowableArray<size_t>* visits);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100169 void FindBackEdges(ArenaBitVector* visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000170 void VisitBlockForBackEdges(HBasicBlock* block,
171 ArenaBitVector* visited,
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100172 ArenaBitVector* visiting);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000173 void RemoveDeadBlocks(const ArenaBitVector& visited) const;
174
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000175 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000176
177 // List of blocks in insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000178 GrowableArray<HBasicBlock*> blocks_;
179
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100180 // List of blocks to perform a reverse post order tree traversal.
181 GrowableArray<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000182
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000183 HBasicBlock* entry_block_;
184 HBasicBlock* exit_block_;
185
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100186 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100187 uint16_t maximum_number_of_out_vregs_;
188
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100189 // The number of virtual registers in this method. Contains the parameters.
190 uint16_t number_of_vregs_;
191
192 // The number of virtual registers used by parameters of this method.
193 uint16_t number_of_in_vregs_;
194
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100195 // The number of temporaries that will be needed for the baseline compiler.
196 size_t number_of_temporaries_;
197
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000198 // The current id to assign to a newly added instruction. See HInstruction.id_.
199 int current_instruction_id_;
200
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000201 DISALLOW_COPY_AND_ASSIGN(HGraph);
202};
203
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700204class HLoopInformation : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000205 public:
206 HLoopInformation(HBasicBlock* header, HGraph* graph)
207 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100208 suspend_check_(nullptr),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100209 back_edges_(graph->GetArena(), kDefaultNumberOfBackEdges),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100210 // Make bit vector growable, as the number of blocks may change.
211 blocks_(graph->GetArena(), graph->GetBlocks().Size(), true) {}
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100212
213 HBasicBlock* GetHeader() const {
214 return header_;
215 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000216
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100217 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
218 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
219 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
220
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000221 void AddBackEdge(HBasicBlock* back_edge) {
222 back_edges_.Add(back_edge);
223 }
224
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100225 void RemoveBackEdge(HBasicBlock* back_edge) {
226 back_edges_.Delete(back_edge);
227 }
228
229 bool IsBackEdge(HBasicBlock* block) {
230 for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) {
231 if (back_edges_.Get(i) == block) return true;
232 }
233 return false;
234 }
235
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000236 int NumberOfBackEdges() const {
237 return back_edges_.Size();
238 }
239
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100240 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100241
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100242 const GrowableArray<HBasicBlock*>& GetBackEdges() const {
243 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100244 }
245
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100246 void ClearBackEdges() {
247 back_edges_.Reset();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100248 }
249
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100250 // Find blocks that are part of this loop. Returns whether the loop is a natural loop,
251 // that is the header dominates the back edge.
252 bool Populate();
253
254 // Returns whether this loop information contains `block`.
255 // Note that this loop information *must* be populated before entering this function.
256 bool Contains(const HBasicBlock& block) const;
257
258 // Returns whether this loop information is an inner loop of `other`.
259 // Note that `other` *must* be populated before entering this function.
260 bool IsIn(const HLoopInformation& other) const;
261
262 const ArenaBitVector& GetBlocks() const { return blocks_; }
263
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000264 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100265 // Internal recursive implementation of `Populate`.
266 void PopulateRecursive(HBasicBlock* block);
267
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000268 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100269 HSuspendCheck* suspend_check_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000270 GrowableArray<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100271 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000272
273 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
274};
275
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100276static constexpr size_t kNoLifetime = -1;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100277static constexpr uint32_t kNoDexPc = -1;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100278
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000279// A block in a method. Contains the list of instructions represented
280// as a double linked list. Each block knows its predecessors and
281// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100282
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700283class HBasicBlock : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000284 public:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100285 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000286 : graph_(graph),
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000287 predecessors_(graph->GetArena(), kDefaultNumberOfPredecessors),
288 successors_(graph->GetArena(), kDefaultNumberOfSuccessors),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000289 loop_information_(nullptr),
290 dominator_(nullptr),
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100291 dominated_blocks_(graph->GetArena(), kDefaultNumberOfDominatedBlocks),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100292 block_id_(-1),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100293 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100294 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000295 lifetime_end_(kNoLifetime),
296 is_catch_block_(false) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000297
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100298 const GrowableArray<HBasicBlock*>& GetPredecessors() const {
299 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000300 }
301
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100302 const GrowableArray<HBasicBlock*>& GetSuccessors() const {
303 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000304 }
305
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100306 const GrowableArray<HBasicBlock*>& GetDominatedBlocks() const {
307 return dominated_blocks_;
308 }
309
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100310 bool IsEntryBlock() const {
311 return graph_->GetEntryBlock() == this;
312 }
313
314 bool IsExitBlock() const {
315 return graph_->GetExitBlock() == this;
316 }
317
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000318 void AddBackEdge(HBasicBlock* back_edge) {
319 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000320 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000321 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100322 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000323 loop_information_->AddBackEdge(back_edge);
324 }
325
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000326 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000327
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000328 int GetBlockId() const { return block_id_; }
329 void SetBlockId(int id) { block_id_ = id; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000330
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000331 HBasicBlock* GetDominator() const { return dominator_; }
332 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100333 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.Add(block); }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000334
335 int NumberOfBackEdges() const {
336 return loop_information_ == nullptr
337 ? 0
338 : loop_information_->NumberOfBackEdges();
339 }
340
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100341 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
342 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100343 const HInstructionList& GetInstructions() const { return instructions_; }
344 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100345 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000346
347 void AddSuccessor(HBasicBlock* block) {
348 successors_.Add(block);
349 block->predecessors_.Add(this);
350 }
351
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100352 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
353 size_t successor_index = GetSuccessorIndexOf(existing);
354 DCHECK_NE(successor_index, static_cast<size_t>(-1));
355 existing->RemovePredecessor(this);
356 new_block->predecessors_.Add(this);
357 successors_.Put(successor_index, new_block);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000358 }
359
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100360 void RemovePredecessor(HBasicBlock* block) {
361 predecessors_.Delete(block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100362 }
363
364 void ClearAllPredecessors() {
365 predecessors_.Reset();
366 }
367
368 void AddPredecessor(HBasicBlock* block) {
369 predecessors_.Add(block);
370 block->successors_.Add(this);
371 }
372
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100373 void SwapPredecessors() {
Nicolas Geoffrayc83d4412014-09-18 16:46:20 +0100374 DCHECK_EQ(predecessors_.Size(), 2u);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100375 HBasicBlock* temp = predecessors_.Get(0);
376 predecessors_.Put(0, predecessors_.Get(1));
377 predecessors_.Put(1, temp);
378 }
379
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100380 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) {
381 for (size_t i = 0, e = predecessors_.Size(); i < e; ++i) {
382 if (predecessors_.Get(i) == predecessor) {
383 return i;
384 }
385 }
386 return -1;
387 }
388
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100389 size_t GetSuccessorIndexOf(HBasicBlock* successor) {
390 for (size_t i = 0, e = successors_.Size(); i < e; ++i) {
391 if (successors_.Get(i) == successor) {
392 return i;
393 }
394 }
395 return -1;
396 }
397
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000398 void AddInstruction(HInstruction* instruction);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100399 void RemoveInstruction(HInstruction* instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100400 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +0100401 // Replace instruction `initial` with `replacement` within this block.
402 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
403 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100404 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100405 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100406 void RemovePhi(HPhi* phi);
407
408 bool IsLoopHeader() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100409 return (loop_information_ != nullptr) && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100410 }
411
Roland Levillain6b879dd2014-09-22 17:13:44 +0100412 bool IsLoopPreHeaderFirstPredecessor() const {
413 DCHECK(IsLoopHeader());
414 DCHECK(!GetPredecessors().IsEmpty());
415 return GetPredecessors().Get(0) == GetLoopInformation()->GetPreHeader();
416 }
417
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100418 HLoopInformation* GetLoopInformation() const {
419 return loop_information_;
420 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000421
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100422 // Set the loop_information_ on this block. This method overrides the current
423 // loop_information if it is an outer loop of the passed loop information.
424 void SetInLoop(HLoopInformation* info) {
425 if (IsLoopHeader()) {
426 // Nothing to do. This just means `info` is an outer loop.
427 } else if (loop_information_ == nullptr) {
428 loop_information_ = info;
429 } else if (loop_information_->Contains(*info->GetHeader())) {
430 // Block is currently part of an outer loop. Make it part of this inner loop.
431 // Note that a non loop header having a loop information means this loop information
432 // has already been populated
433 loop_information_ = info;
434 } else {
435 // Block is part of an inner loop. Do not update the loop information.
436 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
437 // at this point, because this method is being called while populating `info`.
438 }
439 }
440
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100441 bool IsInLoop() const { return loop_information_ != nullptr; }
442
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100443 // Returns wheter this block dominates the blocked passed as parameter.
444 bool Dominates(HBasicBlock* block) const;
445
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100446 size_t GetLifetimeStart() const { return lifetime_start_; }
447 size_t GetLifetimeEnd() const { return lifetime_end_; }
448
449 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
450 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
451
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100452 uint32_t GetDexPc() const { return dex_pc_; }
453
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000454 bool IsCatchBlock() const { return is_catch_block_; }
455 void SetIsCatchBlock() { is_catch_block_ = true; }
456
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000457 private:
458 HGraph* const graph_;
459 GrowableArray<HBasicBlock*> predecessors_;
460 GrowableArray<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100461 HInstructionList instructions_;
462 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000463 HLoopInformation* loop_information_;
464 HBasicBlock* dominator_;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100465 GrowableArray<HBasicBlock*> dominated_blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000466 int block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100467 // The dex program counter of the first instruction of this block.
468 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100469 size_t lifetime_start_;
470 size_t lifetime_end_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000471 bool is_catch_block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000472
473 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
474};
475
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100476#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
477 M(Add, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000478 M(ArrayGet, Instruction) \
479 M(ArrayLength, Instruction) \
480 M(ArraySet, Instruction) \
481 M(BoundsCheck, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100482 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000483 M(Compare, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100484 M(Condition, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000485 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +0000486 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000487 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100488 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000489 M(Exit, Instruction) \
490 M(FloatConstant, Constant) \
491 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100492 M(GreaterThan, Condition) \
493 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100494 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000495 M(InstanceFieldGet, Instruction) \
496 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100497 M(IntConstant, Constant) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +0000498 M(InvokeInterface, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100499 M(InvokeStatic, Invoke) \
500 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000501 M(LessThan, Condition) \
502 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000503 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000504 M(LoadException, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100505 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000506 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100507 M(Local, Instruction) \
508 M(LongConstant, Constant) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000509 M(Mul, BinaryOperation) \
510 M(Neg, UnaryOperation) \
511 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100512 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +0100513 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000514 M(NotEqual, Condition) \
515 M(NullCheck, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100516 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000517 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100518 M(Phi, Instruction) \
519 M(Return, Instruction) \
520 M(ReturnVoid, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100521 M(StaticFieldGet, Instruction) \
522 M(StaticFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100523 M(StoreLocal, Instruction) \
524 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100525 M(SuspendCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000526 M(Temporary, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000527 M(Throw, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +0000528 M(TypeConversion, Instruction) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000529
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100530#define FOR_EACH_INSTRUCTION(M) \
531 FOR_EACH_CONCRETE_INSTRUCTION(M) \
532 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +0100533 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +0100534 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100535 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -0700536
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100537#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000538FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
539#undef FORWARD_DECLARATION
540
Roland Levillainccc07a92014-09-16 14:48:16 +0100541#define DECLARE_INSTRUCTION(type) \
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100542 virtual InstructionKind GetKind() const { return k##type; } \
Roland Levillainccc07a92014-09-16 14:48:16 +0100543 virtual const char* DebugName() const { return #type; } \
544 virtual const H##type* As##type() const OVERRIDE { return this; } \
545 virtual H##type* As##type() OVERRIDE { return this; } \
546 virtual bool InstructionTypeEquals(HInstruction* other) const { \
547 return other->Is##type(); \
548 } \
549 virtual void Accept(HGraphVisitor* visitor)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000550
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100551template <typename T>
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700552class HUseListNode : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000553 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100554 HUseListNode(T* user, size_t index, HUseListNode* tail)
Dave Allison20dfc792014-06-16 20:44:29 -0700555 : user_(user), index_(index), tail_(tail) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000556
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000557 HUseListNode* GetTail() const { return tail_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100558 T* GetUser() const { return user_; }
559 size_t GetIndex() const { return index_; }
560
561 void SetTail(HUseListNode<T>* node) { tail_ = node; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000562
563 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100564 T* const user_;
565 const size_t index_;
566 HUseListNode<T>* tail_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000567
568 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
569};
570
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100571// Represents the side effects an instruction may have.
572class SideEffects : public ValueObject {
573 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100574 SideEffects() : flags_(0) {}
575
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100576 static SideEffects None() {
577 return SideEffects(0);
578 }
579
580 static SideEffects All() {
581 return SideEffects(ChangesSomething().flags_ | DependsOnSomething().flags_);
582 }
583
584 static SideEffects ChangesSomething() {
585 return SideEffects((1 << kFlagChangesCount) - 1);
586 }
587
588 static SideEffects DependsOnSomething() {
589 int count = kFlagDependsOnCount - kFlagChangesCount;
590 return SideEffects(((1 << count) - 1) << kFlagChangesCount);
591 }
592
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100593 SideEffects Union(SideEffects other) const {
594 return SideEffects(flags_ | other.flags_);
595 }
596
Roland Levillain72bceff2014-09-15 18:29:00 +0100597 bool HasSideEffects() const {
598 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
599 return (flags_ & all_bits_set) != 0;
600 }
601
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100602 bool HasAllSideEffects() const {
603 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
604 return all_bits_set == (flags_ & all_bits_set);
605 }
606
607 bool DependsOn(SideEffects other) const {
608 size_t depends_flags = other.ComputeDependsFlags();
609 return (flags_ & depends_flags) != 0;
610 }
611
612 bool HasDependencies() const {
613 int count = kFlagDependsOnCount - kFlagChangesCount;
614 size_t all_bits_set = (1 << count) - 1;
615 return ((flags_ >> kFlagChangesCount) & all_bits_set) != 0;
616 }
617
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100618 private:
619 static constexpr int kFlagChangesSomething = 0;
620 static constexpr int kFlagChangesCount = kFlagChangesSomething + 1;
621
622 static constexpr int kFlagDependsOnSomething = kFlagChangesCount;
623 static constexpr int kFlagDependsOnCount = kFlagDependsOnSomething + 1;
624
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100625 explicit SideEffects(size_t flags) : flags_(flags) {}
626
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100627 size_t ComputeDependsFlags() const {
628 return flags_ << kFlagChangesCount;
629 }
630
631 size_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100632};
633
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700634class HInstruction : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000635 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100636 explicit HInstruction(SideEffects side_effects)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000637 : previous_(nullptr),
638 next_(nullptr),
639 block_(nullptr),
640 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100641 ssa_index_(-1),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000642 uses_(nullptr),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100643 env_uses_(nullptr),
644 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100645 locations_(nullptr),
646 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100647 lifetime_position_(kNoLifetime),
648 side_effects_(side_effects) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000649
Dave Allison20dfc792014-06-16 20:44:29 -0700650 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000651
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100652#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100653 enum InstructionKind {
654 FOR_EACH_INSTRUCTION(DECLARE_KIND)
655 };
656#undef DECLARE_KIND
657
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000658 HInstruction* GetNext() const { return next_; }
659 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000660
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000661 HBasicBlock* GetBlock() const { return block_; }
662 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100663 bool IsInBlock() const { return block_ != nullptr; }
664 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +0100665 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000666
Roland Levillain6b879dd2014-09-22 17:13:44 +0100667 virtual size_t InputCount() const = 0;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100668 virtual HInstruction* InputAt(size_t i) const = 0;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000669
670 virtual void Accept(HGraphVisitor* visitor) = 0;
671 virtual const char* DebugName() const = 0;
672
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100673 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100674 virtual void SetRawInputAt(size_t index, HInstruction* input) = 0;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100675
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100676 virtual bool NeedsEnvironment() const { return false; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100677 virtual bool IsControlFlow() const { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +0100678 virtual bool CanThrow() const { return false; }
Roland Levillain72bceff2014-09-15 18:29:00 +0100679 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100680
681 void AddUseAt(HInstruction* user, size_t index) {
682 uses_ = new (block_->GetGraph()->GetArena()) HUseListNode<HInstruction>(user, index, uses_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000683 }
684
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100685 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +0100686 DCHECK(user != nullptr);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100687 env_uses_ = new (block_->GetGraph()->GetArena()) HUseListNode<HEnvironment>(
688 user, index, env_uses_);
689 }
690
691 void RemoveUser(HInstruction* user, size_t index);
Nicolas Geoffray724c9632014-09-22 12:27:27 +0100692 void RemoveEnvironmentUser(HEnvironment* user, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100693
694 HUseListNode<HInstruction>* GetUses() const { return uses_; }
695 HUseListNode<HEnvironment>* GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000696
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100697 bool HasUses() const { return uses_ != nullptr || env_uses_ != nullptr; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100698 bool HasEnvironmentUses() const { return env_uses_ != nullptr; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000699
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100700 size_t NumberOfUses() const {
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100701 // TODO: Optimize this method if it is used outside of the HGraphVisualizer.
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100702 size_t result = 0;
703 HUseListNode<HInstruction>* current = uses_;
704 while (current != nullptr) {
705 current = current->GetTail();
706 ++result;
707 }
708 return result;
709 }
710
Roland Levillain6c82d402014-10-13 16:10:27 +0100711 // Does this instruction strictly dominate `other_instruction`?
712 // Returns false if this instruction and `other_instruction` are the same.
713 // Aborts if this instruction and `other_instruction` are both phis.
714 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +0100715
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000716 int GetId() const { return id_; }
717 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000718
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100719 int GetSsaIndex() const { return ssa_index_; }
720 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
721 bool HasSsaIndex() const { return ssa_index_ != -1; }
722
723 bool HasEnvironment() const { return environment_ != nullptr; }
724 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100725 void SetEnvironment(HEnvironment* environment) { environment_ = environment; }
726
Nicolas Geoffray39468442014-09-02 15:17:15 +0100727 // Returns the number of entries in the environment. Typically, that is the
728 // number of dex registers in a method. It could be more in case of inlining.
729 size_t EnvironmentSize() const;
730
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000731 LocationSummary* GetLocations() const { return locations_; }
732 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000733
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100734 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100735 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100736
Dave Allison20dfc792014-06-16 20:44:29 -0700737 bool HasOnlyOneUse() const {
738 return uses_ != nullptr && uses_->GetTail() == nullptr;
739 }
740
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100741#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +0100742 bool Is##type() const { return (As##type() != nullptr); } \
743 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000744 virtual H##type* As##type() { return nullptr; }
745
746 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
747#undef INSTRUCTION_TYPE_CHECK
748
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100749 // Returns whether the instruction can be moved within the graph.
750 virtual bool CanBeMoved() const { return false; }
751
752 // Returns whether the two instructions are of the same kind.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700753 virtual bool InstructionTypeEquals(HInstruction* other) const {
754 UNUSED(other);
755 return false;
756 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100757
758 // Returns whether any data encoded in the two instructions is equal.
759 // This method does not look at the inputs. Both instructions must be
760 // of the same type, otherwise the method has undefined behavior.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700761 virtual bool InstructionDataEquals(HInstruction* other) const {
762 UNUSED(other);
763 return false;
764 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100765
766 // Returns whether two instructions are equal, that is:
767 // 1) They have the same type and contain the same data,
768 // 2) Their inputs are identical.
769 bool Equals(HInstruction* other) const;
770
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100771 virtual InstructionKind GetKind() const = 0;
772
773 virtual size_t ComputeHashCode() const {
774 size_t result = GetKind();
775 for (size_t i = 0, e = InputCount(); i < e; ++i) {
776 result = (result * 31) + InputAt(i)->GetId();
777 }
778 return result;
779 }
780
781 SideEffects GetSideEffects() const { return side_effects_; }
782
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100783 size_t GetLifetimePosition() const { return lifetime_position_; }
784 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
785 LiveInterval* GetLiveInterval() const { return live_interval_; }
786 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
787 bool HasLiveInterval() const { return live_interval_ != nullptr; }
788
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000789 private:
790 HInstruction* previous_;
791 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000792 HBasicBlock* block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000793
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000794 // An instruction gets an id when it is added to the graph.
795 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +0100796 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000797 int id_;
798
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100799 // When doing liveness analysis, instructions that have uses get an SSA index.
800 int ssa_index_;
801
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100802 // List of instructions that have this instruction as input.
803 HUseListNode<HInstruction>* uses_;
804
805 // List of environments that contain this instruction.
806 HUseListNode<HEnvironment>* env_uses_;
807
Nicolas Geoffray39468442014-09-02 15:17:15 +0100808 // The environment associated with this instruction. Not null if the instruction
809 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100810 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000811
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000812 // Set by the code generator.
813 LocationSummary* locations_;
814
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100815 // Set by the liveness analysis.
816 LiveInterval* live_interval_;
817
818 // Set by the liveness analysis, this is the position in a linear
819 // order of blocks where this instruction's live interval start.
820 size_t lifetime_position_;
821
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100822 const SideEffects side_effects_;
823
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000824 friend class HBasicBlock;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100825 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000826
827 DISALLOW_COPY_AND_ASSIGN(HInstruction);
828};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700829std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000830
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100831template<typename T>
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000832class HUseIterator : public ValueObject {
833 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100834 explicit HUseIterator(HUseListNode<T>* uses) : current_(uses) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000835
836 bool Done() const { return current_ == nullptr; }
837
838 void Advance() {
839 DCHECK(!Done());
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000840 current_ = current_->GetTail();
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000841 }
842
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100843 HUseListNode<T>* Current() const {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000844 DCHECK(!Done());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100845 return current_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000846 }
847
848 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100849 HUseListNode<T>* current_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000850
851 friend class HValue;
852};
853
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100854// A HEnvironment object contains the values of virtual registers at a given location.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700855class HEnvironment : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100856 public:
857 HEnvironment(ArenaAllocator* arena, size_t number_of_vregs) : vregs_(arena, number_of_vregs) {
858 vregs_.SetSize(number_of_vregs);
859 for (size_t i = 0; i < number_of_vregs; i++) {
860 vregs_.Put(i, nullptr);
861 }
862 }
863
864 void Populate(const GrowableArray<HInstruction*>& env) {
865 for (size_t i = 0; i < env.Size(); i++) {
866 HInstruction* instruction = env.Get(i);
867 vregs_.Put(i, instruction);
868 if (instruction != nullptr) {
869 instruction->AddEnvUseAt(this, i);
870 }
871 }
872 }
873
874 void SetRawEnvAt(size_t index, HInstruction* instruction) {
875 vregs_.Put(index, instruction);
876 }
877
Nicolas Geoffray39468442014-09-02 15:17:15 +0100878 HInstruction* GetInstructionAt(size_t index) const {
879 return vregs_.Get(index);
880 }
881
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100882 GrowableArray<HInstruction*>* GetVRegs() {
883 return &vregs_;
884 }
885
Nicolas Geoffray39468442014-09-02 15:17:15 +0100886 size_t Size() const { return vregs_.Size(); }
887
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100888 private:
889 GrowableArray<HInstruction*> vregs_;
890
891 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
892};
893
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000894class HInputIterator : public ValueObject {
895 public:
Dave Allison20dfc792014-06-16 20:44:29 -0700896 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000897
898 bool Done() const { return index_ == instruction_->InputCount(); }
899 HInstruction* Current() const { return instruction_->InputAt(index_); }
900 void Advance() { index_++; }
901
902 private:
903 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100904 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000905
906 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
907};
908
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000909class HInstructionIterator : public ValueObject {
910 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100911 explicit HInstructionIterator(const HInstructionList& instructions)
912 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000913 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000914 }
915
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000916 bool Done() const { return instruction_ == nullptr; }
917 HInstruction* Current() const { return instruction_; }
918 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000919 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000920 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000921 }
922
923 private:
924 HInstruction* instruction_;
925 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100926
927 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000928};
929
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100930class HBackwardInstructionIterator : public ValueObject {
931 public:
932 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
933 : instruction_(instructions.last_instruction_) {
934 next_ = Done() ? nullptr : instruction_->GetPrevious();
935 }
936
937 bool Done() const { return instruction_ == nullptr; }
938 HInstruction* Current() const { return instruction_; }
939 void Advance() {
940 instruction_ = next_;
941 next_ = Done() ? nullptr : instruction_->GetPrevious();
942 }
943
944 private:
945 HInstruction* instruction_;
946 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100947
948 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100949};
950
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000951// An embedded container with N elements of type T. Used (with partial
952// specialization for N=0) because embedded arrays cannot have size 0.
953template<typename T, intptr_t N>
954class EmbeddedArray {
955 public:
Dave Allison20dfc792014-06-16 20:44:29 -0700956 EmbeddedArray() : elements_() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000957
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000958 intptr_t GetLength() const { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000959
960 const T& operator[](intptr_t i) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000961 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000962 return elements_[i];
963 }
964
965 T& operator[](intptr_t i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000966 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000967 return elements_[i];
968 }
969
970 const T& At(intptr_t i) const {
971 return (*this)[i];
972 }
973
974 void SetAt(intptr_t i, const T& val) {
975 (*this)[i] = val;
976 }
977
978 private:
979 T elements_[N];
980};
981
982template<typename T>
983class EmbeddedArray<T, 0> {
984 public:
985 intptr_t length() const { return 0; }
986 const T& operator[](intptr_t i) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700987 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000988 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700989 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000990 }
991 T& operator[](intptr_t i) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700992 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000993 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700994 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000995 }
996};
997
998template<intptr_t N>
999class HTemplateInstruction: public HInstruction {
1000 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001001 HTemplateInstruction<N>(SideEffects side_effects)
1002 : HInstruction(side_effects), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07001003 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001004
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001005 virtual size_t InputCount() const { return N; }
1006 virtual HInstruction* InputAt(size_t i) const { return inputs_[i]; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001007
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001008 protected:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001009 virtual void SetRawInputAt(size_t i, HInstruction* instruction) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001010 inputs_[i] = instruction;
1011 }
1012
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001013 private:
1014 EmbeddedArray<HInstruction*, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001015
1016 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001017};
1018
Dave Allison20dfc792014-06-16 20:44:29 -07001019template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001020class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07001021 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001022 HExpression<N>(Primitive::Type type, SideEffects side_effects)
1023 : HTemplateInstruction<N>(side_effects), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07001024 virtual ~HExpression() {}
1025
1026 virtual Primitive::Type GetType() const { return type_; }
1027
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001028 protected:
1029 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07001030};
1031
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001032// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
1033// instruction that branches to the exit block.
1034class HReturnVoid : public HTemplateInstruction<0> {
1035 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001036 HReturnVoid() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001037
1038 virtual bool IsControlFlow() const { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001039
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001040 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001041
1042 private:
1043 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
1044};
1045
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001046// Represents dex's RETURN opcodes. A HReturn is a control flow
1047// instruction that branches to the exit block.
1048class HReturn : public HTemplateInstruction<1> {
1049 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001050 explicit HReturn(HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001051 SetRawInputAt(0, value);
1052 }
1053
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001054 virtual bool IsControlFlow() const { return true; }
1055
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001056 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001057
1058 private:
1059 DISALLOW_COPY_AND_ASSIGN(HReturn);
1060};
1061
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001062// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001063// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001064// exit block.
1065class HExit : public HTemplateInstruction<0> {
1066 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001067 HExit() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001068
1069 virtual bool IsControlFlow() const { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001070
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001071 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001072
1073 private:
1074 DISALLOW_COPY_AND_ASSIGN(HExit);
1075};
1076
1077// Jumps from one block to another.
1078class HGoto : public HTemplateInstruction<0> {
1079 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001080 HGoto() : HTemplateInstruction(SideEffects::None()) {}
1081
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001082 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001083
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001084 HBasicBlock* GetSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001085 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001086 }
1087
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001088 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001089
1090 private:
1091 DISALLOW_COPY_AND_ASSIGN(HGoto);
1092};
1093
Dave Allison20dfc792014-06-16 20:44:29 -07001094
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001095// Conditional branch. A block ending with an HIf instruction must have
1096// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001097class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001098 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001099 explicit HIf(HInstruction* input) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001100 SetRawInputAt(0, input);
1101 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001102
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001103 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001104
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001105 HBasicBlock* IfTrueSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001106 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001107 }
1108
1109 HBasicBlock* IfFalseSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001110 return GetBlock()->GetSuccessors().Get(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001111 }
1112
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001113 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001114
Dave Allison20dfc792014-06-16 20:44:29 -07001115 virtual bool IsIfInstruction() const { return true; }
1116
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001117 private:
1118 DISALLOW_COPY_AND_ASSIGN(HIf);
1119};
1120
Roland Levillain88cb1752014-10-20 16:36:47 +01001121class HUnaryOperation : public HExpression<1> {
1122 public:
1123 HUnaryOperation(Primitive::Type result_type, HInstruction* input)
1124 : HExpression(result_type, SideEffects::None()) {
1125 SetRawInputAt(0, input);
1126 }
1127
1128 HInstruction* GetInput() const { return InputAt(0); }
1129 Primitive::Type GetResultType() const { return GetType(); }
1130
1131 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001132 virtual bool InstructionDataEquals(HInstruction* other) const {
1133 UNUSED(other);
1134 return true;
1135 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001136
Roland Levillain9240d6a2014-10-20 16:47:04 +01001137 // Try to statically evaluate `operation` and return a HConstant
1138 // containing the result of this evaluation. If `operation` cannot
1139 // be evaluated as a constant, return nullptr.
1140 HConstant* TryStaticEvaluation() const;
1141
1142 // Apply this operation to `x`.
1143 virtual int32_t Evaluate(int32_t x) const = 0;
1144 virtual int64_t Evaluate(int64_t x) const = 0;
1145
Roland Levillain88cb1752014-10-20 16:36:47 +01001146 DECLARE_INSTRUCTION(UnaryOperation);
1147
1148 private:
1149 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
1150};
1151
Dave Allison20dfc792014-06-16 20:44:29 -07001152class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001153 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001154 HBinaryOperation(Primitive::Type result_type,
1155 HInstruction* left,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001156 HInstruction* right) : HExpression(result_type, SideEffects::None()) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001157 SetRawInputAt(0, left);
1158 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001159 }
1160
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001161 HInstruction* GetLeft() const { return InputAt(0); }
1162 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07001163 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001164
1165 virtual bool IsCommutative() { return false; }
1166
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001167 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001168 virtual bool InstructionDataEquals(HInstruction* other) const {
1169 UNUSED(other);
1170 return true;
1171 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001172
Roland Levillain9240d6a2014-10-20 16:47:04 +01001173 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01001174 // containing the result of this evaluation. If `operation` cannot
1175 // be evaluated as a constant, return nullptr.
Roland Levillain9240d6a2014-10-20 16:47:04 +01001176 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01001177
1178 // Apply this operation to `x` and `y`.
1179 virtual int32_t Evaluate(int32_t x, int32_t y) const = 0;
1180 virtual int64_t Evaluate(int64_t x, int64_t y) const = 0;
1181
Roland Levillainccc07a92014-09-16 14:48:16 +01001182 DECLARE_INSTRUCTION(BinaryOperation);
1183
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001184 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001185 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
1186};
1187
Dave Allison20dfc792014-06-16 20:44:29 -07001188class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001189 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001190 HCondition(HInstruction* first, HInstruction* second)
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001191 : HBinaryOperation(Primitive::kPrimBoolean, first, second),
1192 needs_materialization_(true) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001193
1194 virtual bool IsCommutative() { return true; }
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001195
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001196 bool NeedsMaterialization() const { return needs_materialization_; }
1197 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001198
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001199 // For code generation purposes, returns whether this instruction is just before
1200 // `if_`, and disregard moves in between.
1201 bool IsBeforeWhenDisregardMoves(HIf* if_) const;
1202
Dave Allison20dfc792014-06-16 20:44:29 -07001203 DECLARE_INSTRUCTION(Condition);
1204
1205 virtual IfCondition GetCondition() const = 0;
1206
1207 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001208 // For register allocation purposes, returns whether this instruction needs to be
1209 // materialized (that is, not just be in the processor flags).
1210 bool needs_materialization_;
1211
Dave Allison20dfc792014-06-16 20:44:29 -07001212 DISALLOW_COPY_AND_ASSIGN(HCondition);
1213};
1214
1215// Instruction to check if two inputs are equal to each other.
1216class HEqual : public HCondition {
1217 public:
1218 HEqual(HInstruction* first, HInstruction* second)
1219 : HCondition(first, second) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001220
Roland Levillain93445682014-10-06 19:24:02 +01001221 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1222 return x == y ? 1 : 0;
1223 }
1224 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1225 return x == y ? 1 : 0;
1226 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001227
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001228 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001229
Dave Allison20dfc792014-06-16 20:44:29 -07001230 virtual IfCondition GetCondition() const {
1231 return kCondEQ;
1232 }
1233
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001234 private:
1235 DISALLOW_COPY_AND_ASSIGN(HEqual);
1236};
1237
Dave Allison20dfc792014-06-16 20:44:29 -07001238class HNotEqual : public HCondition {
1239 public:
1240 HNotEqual(HInstruction* first, HInstruction* second)
1241 : HCondition(first, second) {}
1242
Roland Levillain93445682014-10-06 19:24:02 +01001243 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1244 return x != y ? 1 : 0;
1245 }
1246 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1247 return x != y ? 1 : 0;
1248 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001249
Dave Allison20dfc792014-06-16 20:44:29 -07001250 DECLARE_INSTRUCTION(NotEqual);
1251
1252 virtual IfCondition GetCondition() const {
1253 return kCondNE;
1254 }
1255
1256 private:
1257 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
1258};
1259
1260class HLessThan : public HCondition {
1261 public:
1262 HLessThan(HInstruction* first, HInstruction* second)
1263 : HCondition(first, second) {}
1264
Roland Levillain93445682014-10-06 19:24:02 +01001265 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1266 return x < y ? 1 : 0;
1267 }
1268 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1269 return x < y ? 1 : 0;
1270 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001271
Dave Allison20dfc792014-06-16 20:44:29 -07001272 DECLARE_INSTRUCTION(LessThan);
1273
1274 virtual IfCondition GetCondition() const {
1275 return kCondLT;
1276 }
1277
1278 private:
1279 DISALLOW_COPY_AND_ASSIGN(HLessThan);
1280};
1281
1282class HLessThanOrEqual : public HCondition {
1283 public:
1284 HLessThanOrEqual(HInstruction* first, HInstruction* second)
1285 : HCondition(first, second) {}
1286
Roland Levillain93445682014-10-06 19:24:02 +01001287 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1288 return x <= y ? 1 : 0;
1289 }
1290 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1291 return x <= y ? 1 : 0;
1292 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001293
Dave Allison20dfc792014-06-16 20:44:29 -07001294 DECLARE_INSTRUCTION(LessThanOrEqual);
1295
1296 virtual IfCondition GetCondition() const {
1297 return kCondLE;
1298 }
1299
1300 private:
1301 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
1302};
1303
1304class HGreaterThan : public HCondition {
1305 public:
1306 HGreaterThan(HInstruction* first, HInstruction* second)
1307 : HCondition(first, second) {}
1308
Roland Levillain93445682014-10-06 19:24:02 +01001309 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1310 return x > y ? 1 : 0;
1311 }
1312 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1313 return x > y ? 1 : 0;
1314 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001315
Dave Allison20dfc792014-06-16 20:44:29 -07001316 DECLARE_INSTRUCTION(GreaterThan);
1317
1318 virtual IfCondition GetCondition() const {
1319 return kCondGT;
1320 }
1321
1322 private:
1323 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
1324};
1325
1326class HGreaterThanOrEqual : public HCondition {
1327 public:
1328 HGreaterThanOrEqual(HInstruction* first, HInstruction* second)
1329 : HCondition(first, second) {}
1330
Roland Levillain93445682014-10-06 19:24:02 +01001331 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1332 return x >= y ? 1 : 0;
1333 }
1334 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1335 return x >= y ? 1 : 0;
1336 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001337
Dave Allison20dfc792014-06-16 20:44:29 -07001338 DECLARE_INSTRUCTION(GreaterThanOrEqual);
1339
1340 virtual IfCondition GetCondition() const {
1341 return kCondGE;
1342 }
1343
1344 private:
1345 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
1346};
1347
1348
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001349// Instruction to check how two inputs compare to each other.
1350// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
1351class HCompare : public HBinaryOperation {
1352 public:
1353 HCompare(Primitive::Type type, HInstruction* first, HInstruction* second)
1354 : HBinaryOperation(Primitive::kPrimInt, first, second) {
1355 DCHECK_EQ(type, first->GetType());
1356 DCHECK_EQ(type, second->GetType());
1357 }
1358
Roland Levillain93445682014-10-06 19:24:02 +01001359 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001360 return
1361 x == y ? 0 :
1362 x > y ? 1 :
1363 -1;
1364 }
Roland Levillain93445682014-10-06 19:24:02 +01001365 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001366 return
1367 x == y ? 0 :
1368 x > y ? 1 :
1369 -1;
1370 }
1371
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001372 DECLARE_INSTRUCTION(Compare);
1373
1374 private:
1375 DISALLOW_COPY_AND_ASSIGN(HCompare);
1376};
1377
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001378// A local in the graph. Corresponds to a Dex register.
1379class HLocal : public HTemplateInstruction<0> {
1380 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001381 explicit HLocal(uint16_t reg_number)
1382 : HTemplateInstruction(SideEffects::None()), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001383
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001384 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001385
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001386 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001387
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001388 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001389 // The Dex register number.
1390 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001391
1392 DISALLOW_COPY_AND_ASSIGN(HLocal);
1393};
1394
1395// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07001396class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001397 public:
Roland Levillain5799fc02014-09-25 12:15:20 +01001398 HLoadLocal(HLocal* local, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001399 : HExpression(type, SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001400 SetRawInputAt(0, local);
1401 }
1402
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001403 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1404
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001405 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001406
1407 private:
1408 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
1409};
1410
1411// Store a value in a given local. This instruction has two inputs: the value
1412// and the local.
1413class HStoreLocal : public HTemplateInstruction<2> {
1414 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001415 HStoreLocal(HLocal* local, HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001416 SetRawInputAt(0, local);
1417 SetRawInputAt(1, value);
1418 }
1419
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001420 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1421
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001422 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001423
1424 private:
1425 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
1426};
1427
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001428class HConstant : public HExpression<0> {
1429 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001430 explicit HConstant(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
1431
1432 virtual bool CanBeMoved() const { return true; }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001433
1434 DECLARE_INSTRUCTION(Constant);
1435
1436 private:
1437 DISALLOW_COPY_AND_ASSIGN(HConstant);
1438};
1439
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001440class HFloatConstant : public HConstant {
1441 public:
1442 explicit HFloatConstant(float value) : HConstant(Primitive::kPrimFloat), value_(value) {}
1443
1444 float GetValue() const { return value_; }
1445
1446 virtual bool InstructionDataEquals(HInstruction* other) const {
1447 return bit_cast<float, int32_t>(other->AsFloatConstant()->value_) ==
1448 bit_cast<float, int32_t>(value_);
1449 }
1450
1451 virtual size_t ComputeHashCode() const { return static_cast<size_t>(GetValue()); }
1452
1453 DECLARE_INSTRUCTION(FloatConstant);
1454
1455 private:
1456 const float value_;
1457
1458 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
1459};
1460
1461class HDoubleConstant : public HConstant {
1462 public:
1463 explicit HDoubleConstant(double value) : HConstant(Primitive::kPrimDouble), value_(value) {}
1464
1465 double GetValue() const { return value_; }
1466
1467 virtual bool InstructionDataEquals(HInstruction* other) const {
1468 return bit_cast<double, int64_t>(other->AsDoubleConstant()->value_) ==
1469 bit_cast<double, int64_t>(value_);
1470 }
1471
1472 virtual size_t ComputeHashCode() const { return static_cast<size_t>(GetValue()); }
1473
1474 DECLARE_INSTRUCTION(DoubleConstant);
1475
1476 private:
1477 const double value_;
1478
1479 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
1480};
1481
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001482// Constants of the type int. Those can be from Dex instructions, or
1483// synthesized (for example with the if-eqz instruction).
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001484class HIntConstant : public HConstant {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001485 public:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001486 explicit HIntConstant(int32_t value) : HConstant(Primitive::kPrimInt), value_(value) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001487
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001488 int32_t GetValue() const { return value_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001489
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001490 virtual bool InstructionDataEquals(HInstruction* other) const {
1491 return other->AsIntConstant()->value_ == value_;
1492 }
1493
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001494 virtual size_t ComputeHashCode() const { return GetValue(); }
1495
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001496 DECLARE_INSTRUCTION(IntConstant);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001497
1498 private:
1499 const int32_t value_;
1500
1501 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
1502};
1503
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001504class HLongConstant : public HConstant {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001505 public:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001506 explicit HLongConstant(int64_t value) : HConstant(Primitive::kPrimLong), value_(value) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001507
1508 int64_t GetValue() const { return value_; }
1509
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001510 virtual bool InstructionDataEquals(HInstruction* other) const {
1511 return other->AsLongConstant()->value_ == value_;
1512 }
1513
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001514 virtual size_t ComputeHashCode() const { return static_cast<size_t>(GetValue()); }
1515
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001516 DECLARE_INSTRUCTION(LongConstant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001517
1518 private:
1519 const int64_t value_;
1520
1521 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
1522};
1523
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001524class HInvoke : public HInstruction {
1525 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001526 HInvoke(ArenaAllocator* arena,
1527 uint32_t number_of_arguments,
1528 Primitive::Type return_type,
1529 uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001530 : HInstruction(SideEffects::All()),
1531 inputs_(arena, number_of_arguments),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001532 return_type_(return_type),
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001533 dex_pc_(dex_pc) {
1534 inputs_.SetSize(number_of_arguments);
1535 }
1536
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001537 virtual size_t InputCount() const { return inputs_.Size(); }
1538 virtual HInstruction* InputAt(size_t i) const { return inputs_.Get(i); }
1539
1540 // Runtime needs to walk the stack, so Dex -> Dex calls need to
1541 // know their environment.
1542 virtual bool NeedsEnvironment() const { return true; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001543
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001544 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001545 SetRawInputAt(index, argument);
1546 }
1547
1548 virtual void SetRawInputAt(size_t index, HInstruction* input) {
1549 inputs_.Put(index, input);
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001550 }
1551
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001552 virtual Primitive::Type GetType() const { return return_type_; }
1553
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001554 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001555
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001556 DECLARE_INSTRUCTION(Invoke);
1557
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001558 protected:
1559 GrowableArray<HInstruction*> inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001560 const Primitive::Type return_type_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001561 const uint32_t dex_pc_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001562
1563 private:
1564 DISALLOW_COPY_AND_ASSIGN(HInvoke);
1565};
1566
1567class HInvokeStatic : public HInvoke {
1568 public:
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001569 HInvokeStatic(ArenaAllocator* arena,
1570 uint32_t number_of_arguments,
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001571 Primitive::Type return_type,
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001572 uint32_t dex_pc,
1573 uint32_t index_in_dex_cache)
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001574 : HInvoke(arena, number_of_arguments, return_type, dex_pc),
1575 index_in_dex_cache_(index_in_dex_cache) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001576
1577 uint32_t GetIndexInDexCache() const { return index_in_dex_cache_; }
1578
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001579 DECLARE_INSTRUCTION(InvokeStatic);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001580
1581 private:
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001582 const uint32_t index_in_dex_cache_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001583
1584 DISALLOW_COPY_AND_ASSIGN(HInvokeStatic);
1585};
1586
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001587class HInvokeVirtual : public HInvoke {
1588 public:
1589 HInvokeVirtual(ArenaAllocator* arena,
1590 uint32_t number_of_arguments,
1591 Primitive::Type return_type,
1592 uint32_t dex_pc,
1593 uint32_t vtable_index)
1594 : HInvoke(arena, number_of_arguments, return_type, dex_pc),
1595 vtable_index_(vtable_index) {}
1596
1597 uint32_t GetVTableIndex() const { return vtable_index_; }
1598
1599 DECLARE_INSTRUCTION(InvokeVirtual);
1600
1601 private:
1602 const uint32_t vtable_index_;
1603
1604 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
1605};
1606
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001607class HInvokeInterface : public HInvoke {
1608 public:
1609 HInvokeInterface(ArenaAllocator* arena,
1610 uint32_t number_of_arguments,
1611 Primitive::Type return_type,
1612 uint32_t dex_pc,
1613 uint32_t dex_method_index,
1614 uint32_t imt_index)
1615 : HInvoke(arena, number_of_arguments, return_type, dex_pc),
1616 dex_method_index_(dex_method_index),
1617 imt_index_(imt_index) {}
1618
1619 uint32_t GetImtIndex() const { return imt_index_; }
1620 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
1621
1622 DECLARE_INSTRUCTION(InvokeInterface);
1623
1624 private:
1625 const uint32_t dex_method_index_;
1626 const uint32_t imt_index_;
1627
1628 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
1629};
1630
Dave Allison20dfc792014-06-16 20:44:29 -07001631class HNewInstance : public HExpression<0> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001632 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001633 HNewInstance(uint32_t dex_pc, uint16_t type_index)
1634 : HExpression(Primitive::kPrimNot, SideEffects::None()),
1635 dex_pc_(dex_pc),
1636 type_index_(type_index) {}
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001637
1638 uint32_t GetDexPc() const { return dex_pc_; }
1639 uint16_t GetTypeIndex() const { return type_index_; }
1640
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001641 // Calls runtime so needs an environment.
1642 virtual bool NeedsEnvironment() const { return true; }
1643
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001644 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001645
1646 private:
1647 const uint32_t dex_pc_;
1648 const uint16_t type_index_;
1649
1650 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
1651};
1652
Roland Levillain88cb1752014-10-20 16:36:47 +01001653class HNeg : public HUnaryOperation {
1654 public:
1655 explicit HNeg(Primitive::Type result_type, HInstruction* input)
1656 : HUnaryOperation(result_type, input) {}
1657
Roland Levillain9240d6a2014-10-20 16:47:04 +01001658 virtual int32_t Evaluate(int32_t x) const OVERRIDE { return -x; }
1659 virtual int64_t Evaluate(int64_t x) const OVERRIDE { return -x; }
1660
Roland Levillain88cb1752014-10-20 16:36:47 +01001661 DECLARE_INSTRUCTION(Neg);
1662
1663 private:
1664 DISALLOW_COPY_AND_ASSIGN(HNeg);
1665};
1666
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001667class HNewArray : public HExpression<1> {
1668 public:
1669 HNewArray(HInstruction* length, uint32_t dex_pc, uint16_t type_index)
1670 : HExpression(Primitive::kPrimNot, SideEffects::None()),
1671 dex_pc_(dex_pc),
1672 type_index_(type_index) {
1673 SetRawInputAt(0, length);
1674 }
1675
1676 uint32_t GetDexPc() const { return dex_pc_; }
1677 uint16_t GetTypeIndex() const { return type_index_; }
1678
1679 // Calls runtime so needs an environment.
1680 virtual bool NeedsEnvironment() const { return true; }
1681
1682 DECLARE_INSTRUCTION(NewArray);
1683
1684 private:
1685 const uint32_t dex_pc_;
1686 const uint16_t type_index_;
1687
1688 DISALLOW_COPY_AND_ASSIGN(HNewArray);
1689};
1690
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001691class HAdd : public HBinaryOperation {
1692 public:
1693 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1694 : HBinaryOperation(result_type, left, right) {}
1695
1696 virtual bool IsCommutative() { return true; }
1697
Roland Levillain93445682014-10-06 19:24:02 +01001698 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1699 return x + y;
1700 }
1701 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1702 return x + y;
1703 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001704
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001705 DECLARE_INSTRUCTION(Add);
1706
1707 private:
1708 DISALLOW_COPY_AND_ASSIGN(HAdd);
1709};
1710
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001711class HSub : public HBinaryOperation {
1712 public:
1713 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1714 : HBinaryOperation(result_type, left, right) {}
1715
Roland Levillain93445682014-10-06 19:24:02 +01001716 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1717 return x - y;
1718 }
1719 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1720 return x - y;
1721 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001722
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001723 DECLARE_INSTRUCTION(Sub);
1724
1725 private:
1726 DISALLOW_COPY_AND_ASSIGN(HSub);
1727};
1728
Calin Juravle34bacdf2014-10-07 20:23:36 +01001729class HMul : public HBinaryOperation {
1730 public:
1731 HMul(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1732 : HBinaryOperation(result_type, left, right) {}
1733
1734 virtual bool IsCommutative() { return true; }
1735
1736 virtual int32_t Evaluate(int32_t x, int32_t y) const { return x * y; }
1737 virtual int64_t Evaluate(int64_t x, int64_t y) const { return x * y; }
1738
1739 DECLARE_INSTRUCTION(Mul);
1740
1741 private:
1742 DISALLOW_COPY_AND_ASSIGN(HMul);
1743};
1744
Calin Juravle7c4954d2014-10-28 16:57:40 +00001745class HDiv : public HBinaryOperation {
1746 public:
1747 HDiv(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1748 : HBinaryOperation(result_type, left, right) {}
1749
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00001750 virtual int32_t Evaluate(int32_t x, int32_t y) const {
1751 // Our graph structure ensures we never have 0 for `y` during constant folding.
1752 DCHECK_NE(y, 0);
1753 // Special case -1 to avoid getting a SIGFPE on x86.
1754 return (y == -1) ? -x : x / y;
1755 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00001756 virtual int64_t Evaluate(int64_t x, int64_t y) const { return x / y; }
1757
1758 DECLARE_INSTRUCTION(Div);
1759
1760 private:
1761 DISALLOW_COPY_AND_ASSIGN(HDiv);
1762};
1763
Calin Juravled0d48522014-11-04 16:40:20 +00001764class HDivZeroCheck : public HExpression<1> {
1765 public:
1766 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
1767 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
1768 SetRawInputAt(0, value);
1769 }
1770
1771 bool CanBeMoved() const OVERRIDE { return true; }
1772
1773 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
1774 UNUSED(other);
1775 return true;
1776 }
1777
1778 bool NeedsEnvironment() const OVERRIDE { return true; }
1779 bool CanThrow() const OVERRIDE { return true; }
1780
1781 uint32_t GetDexPc() const { return dex_pc_; }
1782
1783 DECLARE_INSTRUCTION(DivZeroCheck);
1784
1785 private:
1786 const uint32_t dex_pc_;
1787
1788 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
1789};
1790
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001791// The value of a parameter in this method. Its location depends on
1792// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07001793class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001794 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001795 HParameterValue(uint8_t index, Primitive::Type parameter_type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001796 : HExpression(parameter_type, SideEffects::None()), index_(index) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001797
1798 uint8_t GetIndex() const { return index_; }
1799
1800 DECLARE_INSTRUCTION(ParameterValue);
1801
1802 private:
1803 // The index of this parameter in the parameters list. Must be less
1804 // than HGraph::number_of_in_vregs_;
1805 const uint8_t index_;
1806
1807 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
1808};
1809
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001810class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001811 public:
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001812 explicit HNot(Primitive::Type result_type, HInstruction* input)
1813 : HUnaryOperation(result_type, input) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001814
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001815 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001816 virtual bool InstructionDataEquals(HInstruction* other) const {
1817 UNUSED(other);
1818 return true;
1819 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001820
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001821 virtual int32_t Evaluate(int32_t x) const OVERRIDE { return ~x; }
1822 virtual int64_t Evaluate(int64_t x) const OVERRIDE { return ~x; }
1823
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001824 DECLARE_INSTRUCTION(Not);
1825
1826 private:
1827 DISALLOW_COPY_AND_ASSIGN(HNot);
1828};
1829
Roland Levillaindff1f282014-11-05 14:15:05 +00001830class HTypeConversion : public HExpression<1> {
1831 public:
1832 // Instantiate a type conversion of `input` to `result_type`.
1833 HTypeConversion(Primitive::Type result_type, HInstruction* input)
1834 : HExpression(result_type, SideEffects::None()) {
1835 SetRawInputAt(0, input);
1836 DCHECK_NE(input->GetType(), result_type);
1837 }
1838
1839 HInstruction* GetInput() const { return InputAt(0); }
1840 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
1841 Primitive::Type GetResultType() const { return GetType(); }
1842
1843 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00001844 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00001845
1846 DECLARE_INSTRUCTION(TypeConversion);
1847
1848 private:
1849 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
1850};
1851
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001852class HPhi : public HInstruction {
1853 public:
1854 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001855 : HInstruction(SideEffects::None()),
1856 inputs_(arena, number_of_inputs),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001857 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001858 type_(type),
1859 is_live_(false) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001860 inputs_.SetSize(number_of_inputs);
1861 }
1862
1863 virtual size_t InputCount() const { return inputs_.Size(); }
1864 virtual HInstruction* InputAt(size_t i) const { return inputs_.Get(i); }
1865
1866 virtual void SetRawInputAt(size_t index, HInstruction* input) {
1867 inputs_.Put(index, input);
1868 }
1869
1870 void AddInput(HInstruction* input);
1871
1872 virtual Primitive::Type GetType() const { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001873 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001874
1875 uint32_t GetRegNumber() const { return reg_number_; }
1876
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001877 void SetDead() { is_live_ = false; }
1878 void SetLive() { is_live_ = true; }
1879 bool IsDead() const { return !is_live_; }
1880 bool IsLive() const { return is_live_; }
1881
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001882 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001883
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001884 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001885 GrowableArray<HInstruction*> inputs_;
1886 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001887 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001888 bool is_live_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001889
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001890 DISALLOW_COPY_AND_ASSIGN(HPhi);
1891};
1892
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001893class HNullCheck : public HExpression<1> {
1894 public:
1895 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001896 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001897 SetRawInputAt(0, value);
1898 }
1899
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001900 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001901 virtual bool InstructionDataEquals(HInstruction* other) const {
1902 UNUSED(other);
1903 return true;
1904 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001905
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001906 virtual bool NeedsEnvironment() const { return true; }
1907
Roland Levillaine161a2a2014-10-03 12:45:18 +01001908 virtual bool CanThrow() const { return true; }
1909
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001910 uint32_t GetDexPc() const { return dex_pc_; }
1911
1912 DECLARE_INSTRUCTION(NullCheck);
1913
1914 private:
1915 const uint32_t dex_pc_;
1916
1917 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
1918};
1919
1920class FieldInfo : public ValueObject {
1921 public:
Roland Levillain5799fc02014-09-25 12:15:20 +01001922 FieldInfo(MemberOffset field_offset, Primitive::Type field_type)
Nicolas Geoffray39468442014-09-02 15:17:15 +01001923 : field_offset_(field_offset), field_type_(field_type) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001924
1925 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001926 Primitive::Type GetFieldType() const { return field_type_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001927
1928 private:
1929 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01001930 const Primitive::Type field_type_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001931};
1932
1933class HInstanceFieldGet : public HExpression<1> {
1934 public:
1935 HInstanceFieldGet(HInstruction* value,
1936 Primitive::Type field_type,
1937 MemberOffset field_offset)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001938 : HExpression(field_type, SideEffects::DependsOnSomething()),
1939 field_info_(field_offset, field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001940 SetRawInputAt(0, value);
1941 }
1942
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001943 virtual bool CanBeMoved() const { return true; }
1944 virtual bool InstructionDataEquals(HInstruction* other) const {
1945 size_t other_offset = other->AsInstanceFieldGet()->GetFieldOffset().SizeValue();
1946 return other_offset == GetFieldOffset().SizeValue();
1947 }
1948
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001949 virtual size_t ComputeHashCode() const {
1950 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
1951 }
1952
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001953 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001954 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001955
1956 DECLARE_INSTRUCTION(InstanceFieldGet);
1957
1958 private:
1959 const FieldInfo field_info_;
1960
1961 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
1962};
1963
1964class HInstanceFieldSet : public HTemplateInstruction<2> {
1965 public:
1966 HInstanceFieldSet(HInstruction* object,
1967 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01001968 Primitive::Type field_type,
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001969 MemberOffset field_offset)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001970 : HTemplateInstruction(SideEffects::ChangesSomething()),
1971 field_info_(field_offset, field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001972 SetRawInputAt(0, object);
1973 SetRawInputAt(1, value);
1974 }
1975
1976 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001977 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001978
1979 DECLARE_INSTRUCTION(InstanceFieldSet);
1980
1981 private:
1982 const FieldInfo field_info_;
1983
1984 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
1985};
1986
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001987class HArrayGet : public HExpression<2> {
1988 public:
1989 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001990 : HExpression(type, SideEffects::DependsOnSomething()) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001991 SetRawInputAt(0, array);
1992 SetRawInputAt(1, index);
1993 }
1994
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001995 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001996 virtual bool InstructionDataEquals(HInstruction* other) const {
1997 UNUSED(other);
1998 return true;
1999 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002000 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002001
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002002 DECLARE_INSTRUCTION(ArrayGet);
2003
2004 private:
2005 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
2006};
2007
2008class HArraySet : public HTemplateInstruction<3> {
2009 public:
2010 HArraySet(HInstruction* array,
2011 HInstruction* index,
2012 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002013 Primitive::Type expected_component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002014 uint32_t dex_pc)
2015 : HTemplateInstruction(SideEffects::ChangesSomething()),
2016 dex_pc_(dex_pc),
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002017 expected_component_type_(expected_component_type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002018 SetRawInputAt(0, array);
2019 SetRawInputAt(1, index);
2020 SetRawInputAt(2, value);
2021 }
2022
2023 virtual bool NeedsEnvironment() const {
2024 // We currently always call a runtime method to catch array store
2025 // exceptions.
2026 return InputAt(2)->GetType() == Primitive::kPrimNot;
2027 }
2028
2029 uint32_t GetDexPc() const { return dex_pc_; }
2030
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002031 HInstruction* GetValue() const { return InputAt(2); }
2032
2033 Primitive::Type GetComponentType() const {
2034 // The Dex format does not type floating point index operations. Since the
2035 // `expected_component_type_` is set during building and can therefore not
2036 // be correct, we also check what is the value type. If it is a floating
2037 // point type, we must use that type.
2038 Primitive::Type value_type = GetValue()->GetType();
2039 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
2040 ? value_type
2041 : expected_component_type_;
2042 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002043
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002044 DECLARE_INSTRUCTION(ArraySet);
2045
2046 private:
2047 const uint32_t dex_pc_;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002048 const Primitive::Type expected_component_type_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002049
2050 DISALLOW_COPY_AND_ASSIGN(HArraySet);
2051};
2052
2053class HArrayLength : public HExpression<1> {
2054 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002055 explicit HArrayLength(HInstruction* array)
2056 : HExpression(Primitive::kPrimInt, SideEffects::None()) {
2057 // Note that arrays do not change length, so the instruction does not
2058 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002059 SetRawInputAt(0, array);
2060 }
2061
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002062 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002063 virtual bool InstructionDataEquals(HInstruction* other) const {
2064 UNUSED(other);
2065 return true;
2066 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002067
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002068 DECLARE_INSTRUCTION(ArrayLength);
2069
2070 private:
2071 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
2072};
2073
2074class HBoundsCheck : public HExpression<2> {
2075 public:
2076 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002077 : HExpression(index->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002078 DCHECK(index->GetType() == Primitive::kPrimInt);
2079 SetRawInputAt(0, index);
2080 SetRawInputAt(1, length);
2081 }
2082
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002083 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002084 virtual bool InstructionDataEquals(HInstruction* other) const {
2085 UNUSED(other);
2086 return true;
2087 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002088
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002089 virtual bool NeedsEnvironment() const { return true; }
2090
Roland Levillaine161a2a2014-10-03 12:45:18 +01002091 virtual bool CanThrow() const { return true; }
2092
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002093 uint32_t GetDexPc() const { return dex_pc_; }
2094
2095 DECLARE_INSTRUCTION(BoundsCheck);
2096
2097 private:
2098 const uint32_t dex_pc_;
2099
2100 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
2101};
2102
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002103/**
2104 * Some DEX instructions are folded into multiple HInstructions that need
2105 * to stay live until the last HInstruction. This class
2106 * is used as a marker for the baseline compiler to ensure its preceding
2107 * HInstruction stays live. `index` is the temporary number that is used
2108 * for knowing the stack offset where to store the instruction.
2109 */
2110class HTemporary : public HTemplateInstruction<0> {
2111 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002112 explicit HTemporary(size_t index) : HTemplateInstruction(SideEffects::None()), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002113
2114 size_t GetIndex() const { return index_; }
2115
2116 DECLARE_INSTRUCTION(Temporary);
2117
2118 private:
2119 const size_t index_;
2120
2121 DISALLOW_COPY_AND_ASSIGN(HTemporary);
2122};
2123
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002124class HSuspendCheck : public HTemplateInstruction<0> {
2125 public:
2126 explicit HSuspendCheck(uint32_t dex_pc)
Nicolas Geoffray9ebc72c2014-09-25 16:33:42 +01002127 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002128
2129 virtual bool NeedsEnvironment() const {
2130 return true;
2131 }
2132
2133 uint32_t GetDexPc() const { return dex_pc_; }
2134
2135 DECLARE_INSTRUCTION(SuspendCheck);
2136
2137 private:
2138 const uint32_t dex_pc_;
2139
2140 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
2141};
2142
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002143/**
2144 * Instruction to load a Class object.
2145 */
2146class HLoadClass : public HExpression<0> {
2147 public:
2148 HLoadClass(uint16_t type_index,
2149 bool is_referrers_class,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002150 uint32_t dex_pc)
2151 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2152 type_index_(type_index),
2153 is_referrers_class_(is_referrers_class),
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002154 dex_pc_(dex_pc),
2155 generate_clinit_check_(false) {}
2156
2157 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002158
2159 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2160 return other->AsLoadClass()->type_index_ == type_index_;
2161 }
2162
2163 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
2164
2165 uint32_t GetDexPc() const { return dex_pc_; }
2166 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002167 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002168
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002169 bool NeedsEnvironment() const OVERRIDE {
2170 // Will call runtime and load the class if the class is not loaded yet.
2171 // TODO: finer grain decision.
2172 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002173 }
2174
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002175 bool MustGenerateClinitCheck() const {
2176 return generate_clinit_check_;
2177 }
2178
2179 void SetMustGenerateClinitCheck() {
2180 generate_clinit_check_ = true;
2181 }
2182
2183 bool CanCallRuntime() const {
2184 return MustGenerateClinitCheck() || !is_referrers_class_;
2185 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002186
2187 DECLARE_INSTRUCTION(LoadClass);
2188
2189 private:
2190 const uint16_t type_index_;
2191 const bool is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002192 const uint32_t dex_pc_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002193 // Whether this instruction must generate the initialization check.
2194 // Used for code generation.
2195 bool generate_clinit_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002196
2197 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
2198};
2199
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002200class HLoadString : public HExpression<0> {
2201 public:
2202 HLoadString(uint32_t string_index, uint32_t dex_pc)
2203 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2204 string_index_(string_index),
2205 dex_pc_(dex_pc) {}
2206
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002207 bool CanBeMoved() const OVERRIDE { return true; }
2208
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002209 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2210 return other->AsLoadString()->string_index_ == string_index_;
2211 }
2212
2213 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
2214
2215 uint32_t GetDexPc() const { return dex_pc_; }
2216 uint32_t GetStringIndex() const { return string_index_; }
2217
2218 // TODO: Can we deopt or debug when we resolve a string?
2219 bool NeedsEnvironment() const OVERRIDE { return false; }
2220
2221 DECLARE_INSTRUCTION(LoadString);
2222
2223 private:
2224 const uint32_t string_index_;
2225 const uint32_t dex_pc_;
2226
2227 DISALLOW_COPY_AND_ASSIGN(HLoadString);
2228};
2229
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002230// TODO: Pass this check to HInvokeStatic nodes.
2231/**
2232 * Performs an initialization check on its Class object input.
2233 */
2234class HClinitCheck : public HExpression<1> {
2235 public:
2236 explicit HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
2237 : HExpression(Primitive::kPrimNot, SideEffects::All()),
2238 dex_pc_(dex_pc) {
2239 SetRawInputAt(0, constant);
2240 }
2241
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002242 bool CanBeMoved() const OVERRIDE { return true; }
2243 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2244 UNUSED(other);
2245 return true;
2246 }
2247
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002248 bool NeedsEnvironment() const OVERRIDE {
2249 // May call runtime to initialize the class.
2250 return true;
2251 }
2252
2253 uint32_t GetDexPc() const { return dex_pc_; }
2254
2255 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
2256
2257 DECLARE_INSTRUCTION(ClinitCheck);
2258
2259 private:
2260 const uint32_t dex_pc_;
2261
2262 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
2263};
2264
2265class HStaticFieldGet : public HExpression<1> {
2266 public:
2267 HStaticFieldGet(HInstruction* cls,
2268 Primitive::Type field_type,
2269 MemberOffset field_offset)
2270 : HExpression(field_type, SideEffects::DependsOnSomething()),
2271 field_info_(field_offset, field_type) {
2272 SetRawInputAt(0, cls);
2273 }
2274
2275 bool CanBeMoved() const OVERRIDE { return true; }
2276 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2277 size_t other_offset = other->AsStaticFieldGet()->GetFieldOffset().SizeValue();
2278 return other_offset == GetFieldOffset().SizeValue();
2279 }
2280
2281 size_t ComputeHashCode() const OVERRIDE {
2282 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
2283 }
2284
2285 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
2286 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
2287
2288 DECLARE_INSTRUCTION(StaticFieldGet);
2289
2290 private:
2291 const FieldInfo field_info_;
2292
2293 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
2294};
2295
2296class HStaticFieldSet : public HTemplateInstruction<2> {
2297 public:
2298 HStaticFieldSet(HInstruction* cls,
2299 HInstruction* value,
2300 Primitive::Type field_type,
2301 MemberOffset field_offset)
2302 : HTemplateInstruction(SideEffects::ChangesSomething()),
2303 field_info_(field_offset, field_type) {
2304 SetRawInputAt(0, cls);
2305 SetRawInputAt(1, value);
2306 }
2307
2308 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
2309 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
2310
2311 DECLARE_INSTRUCTION(StaticFieldSet);
2312
2313 private:
2314 const FieldInfo field_info_;
2315
2316 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
2317};
2318
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002319// Implement the move-exception DEX instruction.
2320class HLoadException : public HExpression<0> {
2321 public:
2322 HLoadException() : HExpression(Primitive::kPrimNot, SideEffects::None()) {}
2323
2324 DECLARE_INSTRUCTION(LoadException);
2325
2326 private:
2327 DISALLOW_COPY_AND_ASSIGN(HLoadException);
2328};
2329
2330class HThrow : public HTemplateInstruction<1> {
2331 public:
2332 HThrow(HInstruction* exception, uint32_t dex_pc)
2333 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {
2334 SetRawInputAt(0, exception);
2335 }
2336
2337 bool IsControlFlow() const OVERRIDE { return true; }
2338
2339 bool NeedsEnvironment() const OVERRIDE { return true; }
2340
2341 uint32_t GetDexPc() const { return dex_pc_; }
2342
2343 DECLARE_INSTRUCTION(Throw);
2344
2345 private:
2346 uint32_t dex_pc_;
2347
2348 DISALLOW_COPY_AND_ASSIGN(HThrow);
2349};
2350
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002351class MoveOperands : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002352 public:
Nicolas Geoffray740475d2014-09-29 10:33:25 +01002353 MoveOperands(Location source, Location destination, HInstruction* instruction)
2354 : source_(source), destination_(destination), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002355
2356 Location GetSource() const { return source_; }
2357 Location GetDestination() const { return destination_; }
2358
2359 void SetSource(Location value) { source_ = value; }
2360 void SetDestination(Location value) { destination_ = value; }
2361
2362 // The parallel move resolver marks moves as "in-progress" by clearing the
2363 // destination (but not the source).
2364 Location MarkPending() {
2365 DCHECK(!IsPending());
2366 Location dest = destination_;
2367 destination_ = Location::NoLocation();
2368 return dest;
2369 }
2370
2371 void ClearPending(Location dest) {
2372 DCHECK(IsPending());
2373 destination_ = dest;
2374 }
2375
2376 bool IsPending() const {
2377 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
2378 return destination_.IsInvalid() && !source_.IsInvalid();
2379 }
2380
2381 // True if this blocks a move from the given location.
2382 bool Blocks(Location loc) const {
2383 return !IsEliminated() && source_.Equals(loc);
2384 }
2385
2386 // A move is redundant if it's been eliminated, if its source and
2387 // destination are the same, or if its destination is unneeded.
2388 bool IsRedundant() const {
2389 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
2390 }
2391
2392 // We clear both operands to indicate move that's been eliminated.
2393 void Eliminate() {
2394 source_ = destination_ = Location::NoLocation();
2395 }
2396
2397 bool IsEliminated() const {
2398 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
2399 return source_.IsInvalid();
2400 }
2401
Nicolas Geoffray740475d2014-09-29 10:33:25 +01002402 HInstruction* GetInstruction() const { return instruction_; }
2403
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002404 private:
2405 Location source_;
2406 Location destination_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01002407 // The instruction this move is assocatied with. Null when this move is
2408 // for moving an input in the expected locations of user (including a phi user).
2409 // This is only used in debug mode, to ensure we do not connect interval siblings
2410 // in the same parallel move.
2411 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002412
2413 DISALLOW_COPY_AND_ASSIGN(MoveOperands);
2414};
2415
2416static constexpr size_t kDefaultNumberOfMoves = 4;
2417
2418class HParallelMove : public HTemplateInstruction<0> {
2419 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002420 explicit HParallelMove(ArenaAllocator* arena)
2421 : HTemplateInstruction(SideEffects::None()), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002422
2423 void AddMove(MoveOperands* move) {
Nicolas Geoffray740475d2014-09-29 10:33:25 +01002424 if (kIsDebugBuild && move->GetInstruction() != nullptr) {
2425 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
2426 DCHECK_NE(moves_.Get(i)->GetInstruction(), move->GetInstruction())
2427 << "Doing parallel moves for the same instruction.";
2428 }
2429 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002430 moves_.Add(move);
2431 }
2432
2433 MoveOperands* MoveOperandsAt(size_t index) const {
2434 return moves_.Get(index);
2435 }
2436
2437 size_t NumMoves() const { return moves_.Size(); }
2438
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002439 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002440
2441 private:
2442 GrowableArray<MoveOperands*> moves_;
2443
2444 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
2445};
2446
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002447class HGraphVisitor : public ValueObject {
2448 public:
Dave Allison20dfc792014-06-16 20:44:29 -07002449 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
2450 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002451
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002452 virtual void VisitInstruction(HInstruction* instruction) { UNUSED(instruction); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002453 virtual void VisitBasicBlock(HBasicBlock* block);
2454
Roland Levillain633021e2014-10-01 14:12:25 +01002455 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002456 void VisitInsertionOrder();
2457
Roland Levillain633021e2014-10-01 14:12:25 +01002458 // Visit the graph following dominator tree reverse post-order.
2459 void VisitReversePostOrder();
2460
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002461 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002462
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002463 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002464#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002465 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
2466
2467 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
2468
2469#undef DECLARE_VISIT_INSTRUCTION
2470
2471 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07002472 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002473
2474 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
2475};
2476
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002477class HGraphDelegateVisitor : public HGraphVisitor {
2478 public:
2479 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
2480 virtual ~HGraphDelegateVisitor() {}
2481
2482 // Visit functions that delegate to to super class.
2483#define DECLARE_VISIT_INSTRUCTION(name, super) \
2484 virtual void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
2485
2486 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
2487
2488#undef DECLARE_VISIT_INSTRUCTION
2489
2490 private:
2491 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
2492};
2493
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002494class HInsertionOrderIterator : public ValueObject {
2495 public:
2496 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
2497
2498 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
2499 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
2500 void Advance() { ++index_; }
2501
2502 private:
2503 const HGraph& graph_;
2504 size_t index_;
2505
2506 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
2507};
2508
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002509class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002510 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002511 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002512
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002513 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
2514 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002515 void Advance() { ++index_; }
2516
2517 private:
2518 const HGraph& graph_;
2519 size_t index_;
2520
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002521 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002522};
2523
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002524class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002525 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002526 explicit HPostOrderIterator(const HGraph& graph)
2527 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002528
2529 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002530 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002531 void Advance() { --index_; }
2532
2533 private:
2534 const HGraph& graph_;
2535 size_t index_;
2536
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002537 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002538};
2539
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002540} // namespace art
2541
2542#endif // ART_COMPILER_OPTIMIZING_NODES_H_