blob: 6ee4dfc1ebf97ed5d26a64093093185cc64f5605 [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),
295 lifetime_end_(kNoLifetime) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000296
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100297 const GrowableArray<HBasicBlock*>& GetPredecessors() const {
298 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000299 }
300
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100301 const GrowableArray<HBasicBlock*>& GetSuccessors() const {
302 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000303 }
304
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100305 const GrowableArray<HBasicBlock*>& GetDominatedBlocks() const {
306 return dominated_blocks_;
307 }
308
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100309 bool IsEntryBlock() const {
310 return graph_->GetEntryBlock() == this;
311 }
312
313 bool IsExitBlock() const {
314 return graph_->GetExitBlock() == this;
315 }
316
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000317 void AddBackEdge(HBasicBlock* back_edge) {
318 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000319 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000320 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100321 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000322 loop_information_->AddBackEdge(back_edge);
323 }
324
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000325 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000326
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000327 int GetBlockId() const { return block_id_; }
328 void SetBlockId(int id) { block_id_ = id; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000329
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000330 HBasicBlock* GetDominator() const { return dominator_; }
331 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100332 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.Add(block); }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000333
334 int NumberOfBackEdges() const {
335 return loop_information_ == nullptr
336 ? 0
337 : loop_information_->NumberOfBackEdges();
338 }
339
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100340 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
341 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100342 const HInstructionList& GetInstructions() const { return instructions_; }
343 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100344 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000345
346 void AddSuccessor(HBasicBlock* block) {
347 successors_.Add(block);
348 block->predecessors_.Add(this);
349 }
350
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100351 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
352 size_t successor_index = GetSuccessorIndexOf(existing);
353 DCHECK_NE(successor_index, static_cast<size_t>(-1));
354 existing->RemovePredecessor(this);
355 new_block->predecessors_.Add(this);
356 successors_.Put(successor_index, new_block);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000357 }
358
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100359 void RemovePredecessor(HBasicBlock* block) {
360 predecessors_.Delete(block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100361 }
362
363 void ClearAllPredecessors() {
364 predecessors_.Reset();
365 }
366
367 void AddPredecessor(HBasicBlock* block) {
368 predecessors_.Add(block);
369 block->successors_.Add(this);
370 }
371
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100372 void SwapPredecessors() {
Nicolas Geoffrayc83d4412014-09-18 16:46:20 +0100373 DCHECK_EQ(predecessors_.Size(), 2u);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100374 HBasicBlock* temp = predecessors_.Get(0);
375 predecessors_.Put(0, predecessors_.Get(1));
376 predecessors_.Put(1, temp);
377 }
378
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100379 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) {
380 for (size_t i = 0, e = predecessors_.Size(); i < e; ++i) {
381 if (predecessors_.Get(i) == predecessor) {
382 return i;
383 }
384 }
385 return -1;
386 }
387
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100388 size_t GetSuccessorIndexOf(HBasicBlock* successor) {
389 for (size_t i = 0, e = successors_.Size(); i < e; ++i) {
390 if (successors_.Get(i) == successor) {
391 return i;
392 }
393 }
394 return -1;
395 }
396
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000397 void AddInstruction(HInstruction* instruction);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100398 void RemoveInstruction(HInstruction* instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100399 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +0100400 // Replace instruction `initial` with `replacement` within this block.
401 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
402 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100403 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100404 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100405 void RemovePhi(HPhi* phi);
406
407 bool IsLoopHeader() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100408 return (loop_information_ != nullptr) && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100409 }
410
Roland Levillain6b879dd2014-09-22 17:13:44 +0100411 bool IsLoopPreHeaderFirstPredecessor() const {
412 DCHECK(IsLoopHeader());
413 DCHECK(!GetPredecessors().IsEmpty());
414 return GetPredecessors().Get(0) == GetLoopInformation()->GetPreHeader();
415 }
416
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100417 HLoopInformation* GetLoopInformation() const {
418 return loop_information_;
419 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000420
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100421 // Set the loop_information_ on this block. This method overrides the current
422 // loop_information if it is an outer loop of the passed loop information.
423 void SetInLoop(HLoopInformation* info) {
424 if (IsLoopHeader()) {
425 // Nothing to do. This just means `info` is an outer loop.
426 } else if (loop_information_ == nullptr) {
427 loop_information_ = info;
428 } else if (loop_information_->Contains(*info->GetHeader())) {
429 // Block is currently part of an outer loop. Make it part of this inner loop.
430 // Note that a non loop header having a loop information means this loop information
431 // has already been populated
432 loop_information_ = info;
433 } else {
434 // Block is part of an inner loop. Do not update the loop information.
435 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
436 // at this point, because this method is being called while populating `info`.
437 }
438 }
439
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100440 bool IsInLoop() const { return loop_information_ != nullptr; }
441
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100442 // Returns wheter this block dominates the blocked passed as parameter.
443 bool Dominates(HBasicBlock* block) const;
444
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100445 size_t GetLifetimeStart() const { return lifetime_start_; }
446 size_t GetLifetimeEnd() const { return lifetime_end_; }
447
448 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
449 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
450
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100451 uint32_t GetDexPc() const { return dex_pc_; }
452
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000453 private:
454 HGraph* const graph_;
455 GrowableArray<HBasicBlock*> predecessors_;
456 GrowableArray<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100457 HInstructionList instructions_;
458 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000459 HLoopInformation* loop_information_;
460 HBasicBlock* dominator_;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100461 GrowableArray<HBasicBlock*> dominated_blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000462 int block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100463 // The dex program counter of the first instruction of this block.
464 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100465 size_t lifetime_start_;
466 size_t lifetime_end_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000467
468 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
469};
470
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100471#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
472 M(Add, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000473 M(ArrayGet, Instruction) \
474 M(ArrayLength, Instruction) \
475 M(ArraySet, Instruction) \
476 M(BoundsCheck, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100477 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000478 M(Compare, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100479 M(Condition, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000480 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +0000481 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000482 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100483 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000484 M(Exit, Instruction) \
485 M(FloatConstant, Constant) \
486 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100487 M(GreaterThan, Condition) \
488 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100489 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000490 M(InstanceFieldGet, Instruction) \
491 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100492 M(IntConstant, Constant) \
493 M(InvokeStatic, Invoke) \
494 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000495 M(LessThan, Condition) \
496 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000497 M(LoadClass, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100498 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000499 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100500 M(Local, Instruction) \
501 M(LongConstant, Constant) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000502 M(Mul, BinaryOperation) \
503 M(Neg, UnaryOperation) \
504 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100505 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +0100506 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000507 M(NotEqual, Condition) \
508 M(NullCheck, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100509 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000510 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100511 M(Phi, Instruction) \
512 M(Return, Instruction) \
513 M(ReturnVoid, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100514 M(StaticFieldGet, Instruction) \
515 M(StaticFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100516 M(StoreLocal, Instruction) \
517 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100518 M(SuspendCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000519 M(Temporary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +0000520 M(TypeConversion, Instruction) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000521
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100522#define FOR_EACH_INSTRUCTION(M) \
523 FOR_EACH_CONCRETE_INSTRUCTION(M) \
524 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +0100525 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +0100526 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100527 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -0700528
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100529#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000530FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
531#undef FORWARD_DECLARATION
532
Roland Levillainccc07a92014-09-16 14:48:16 +0100533#define DECLARE_INSTRUCTION(type) \
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100534 virtual InstructionKind GetKind() const { return k##type; } \
Roland Levillainccc07a92014-09-16 14:48:16 +0100535 virtual const char* DebugName() const { return #type; } \
536 virtual const H##type* As##type() const OVERRIDE { return this; } \
537 virtual H##type* As##type() OVERRIDE { return this; } \
538 virtual bool InstructionTypeEquals(HInstruction* other) const { \
539 return other->Is##type(); \
540 } \
541 virtual void Accept(HGraphVisitor* visitor)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000542
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100543template <typename T>
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700544class HUseListNode : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000545 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100546 HUseListNode(T* user, size_t index, HUseListNode* tail)
Dave Allison20dfc792014-06-16 20:44:29 -0700547 : user_(user), index_(index), tail_(tail) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000548
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000549 HUseListNode* GetTail() const { return tail_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100550 T* GetUser() const { return user_; }
551 size_t GetIndex() const { return index_; }
552
553 void SetTail(HUseListNode<T>* node) { tail_ = node; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000554
555 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100556 T* const user_;
557 const size_t index_;
558 HUseListNode<T>* tail_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000559
560 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
561};
562
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100563// Represents the side effects an instruction may have.
564class SideEffects : public ValueObject {
565 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100566 SideEffects() : flags_(0) {}
567
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100568 static SideEffects None() {
569 return SideEffects(0);
570 }
571
572 static SideEffects All() {
573 return SideEffects(ChangesSomething().flags_ | DependsOnSomething().flags_);
574 }
575
576 static SideEffects ChangesSomething() {
577 return SideEffects((1 << kFlagChangesCount) - 1);
578 }
579
580 static SideEffects DependsOnSomething() {
581 int count = kFlagDependsOnCount - kFlagChangesCount;
582 return SideEffects(((1 << count) - 1) << kFlagChangesCount);
583 }
584
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100585 SideEffects Union(SideEffects other) const {
586 return SideEffects(flags_ | other.flags_);
587 }
588
Roland Levillain72bceff2014-09-15 18:29:00 +0100589 bool HasSideEffects() const {
590 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
591 return (flags_ & all_bits_set) != 0;
592 }
593
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100594 bool HasAllSideEffects() const {
595 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
596 return all_bits_set == (flags_ & all_bits_set);
597 }
598
599 bool DependsOn(SideEffects other) const {
600 size_t depends_flags = other.ComputeDependsFlags();
601 return (flags_ & depends_flags) != 0;
602 }
603
604 bool HasDependencies() const {
605 int count = kFlagDependsOnCount - kFlagChangesCount;
606 size_t all_bits_set = (1 << count) - 1;
607 return ((flags_ >> kFlagChangesCount) & all_bits_set) != 0;
608 }
609
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100610 private:
611 static constexpr int kFlagChangesSomething = 0;
612 static constexpr int kFlagChangesCount = kFlagChangesSomething + 1;
613
614 static constexpr int kFlagDependsOnSomething = kFlagChangesCount;
615 static constexpr int kFlagDependsOnCount = kFlagDependsOnSomething + 1;
616
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100617 explicit SideEffects(size_t flags) : flags_(flags) {}
618
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100619 size_t ComputeDependsFlags() const {
620 return flags_ << kFlagChangesCount;
621 }
622
623 size_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100624};
625
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700626class HInstruction : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000627 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100628 explicit HInstruction(SideEffects side_effects)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000629 : previous_(nullptr),
630 next_(nullptr),
631 block_(nullptr),
632 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100633 ssa_index_(-1),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000634 uses_(nullptr),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100635 env_uses_(nullptr),
636 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100637 locations_(nullptr),
638 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100639 lifetime_position_(kNoLifetime),
640 side_effects_(side_effects) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000641
Dave Allison20dfc792014-06-16 20:44:29 -0700642 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000643
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100644#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100645 enum InstructionKind {
646 FOR_EACH_INSTRUCTION(DECLARE_KIND)
647 };
648#undef DECLARE_KIND
649
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000650 HInstruction* GetNext() const { return next_; }
651 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000652
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000653 HBasicBlock* GetBlock() const { return block_; }
654 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100655 bool IsInBlock() const { return block_ != nullptr; }
656 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +0100657 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000658
Roland Levillain6b879dd2014-09-22 17:13:44 +0100659 virtual size_t InputCount() const = 0;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100660 virtual HInstruction* InputAt(size_t i) const = 0;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000661
662 virtual void Accept(HGraphVisitor* visitor) = 0;
663 virtual const char* DebugName() const = 0;
664
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100665 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100666 virtual void SetRawInputAt(size_t index, HInstruction* input) = 0;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100667
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100668 virtual bool NeedsEnvironment() const { return false; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100669 virtual bool IsControlFlow() const { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +0100670 virtual bool CanThrow() const { return false; }
Roland Levillain72bceff2014-09-15 18:29:00 +0100671 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100672
673 void AddUseAt(HInstruction* user, size_t index) {
674 uses_ = new (block_->GetGraph()->GetArena()) HUseListNode<HInstruction>(user, index, uses_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000675 }
676
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100677 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +0100678 DCHECK(user != nullptr);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100679 env_uses_ = new (block_->GetGraph()->GetArena()) HUseListNode<HEnvironment>(
680 user, index, env_uses_);
681 }
682
683 void RemoveUser(HInstruction* user, size_t index);
Nicolas Geoffray724c9632014-09-22 12:27:27 +0100684 void RemoveEnvironmentUser(HEnvironment* user, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100685
686 HUseListNode<HInstruction>* GetUses() const { return uses_; }
687 HUseListNode<HEnvironment>* GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000688
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100689 bool HasUses() const { return uses_ != nullptr || env_uses_ != nullptr; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100690 bool HasEnvironmentUses() const { return env_uses_ != nullptr; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000691
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100692 size_t NumberOfUses() const {
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100693 // TODO: Optimize this method if it is used outside of the HGraphVisualizer.
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100694 size_t result = 0;
695 HUseListNode<HInstruction>* current = uses_;
696 while (current != nullptr) {
697 current = current->GetTail();
698 ++result;
699 }
700 return result;
701 }
702
Roland Levillain6c82d402014-10-13 16:10:27 +0100703 // Does this instruction strictly dominate `other_instruction`?
704 // Returns false if this instruction and `other_instruction` are the same.
705 // Aborts if this instruction and `other_instruction` are both phis.
706 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +0100707
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000708 int GetId() const { return id_; }
709 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000710
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100711 int GetSsaIndex() const { return ssa_index_; }
712 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
713 bool HasSsaIndex() const { return ssa_index_ != -1; }
714
715 bool HasEnvironment() const { return environment_ != nullptr; }
716 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100717 void SetEnvironment(HEnvironment* environment) { environment_ = environment; }
718
Nicolas Geoffray39468442014-09-02 15:17:15 +0100719 // Returns the number of entries in the environment. Typically, that is the
720 // number of dex registers in a method. It could be more in case of inlining.
721 size_t EnvironmentSize() const;
722
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000723 LocationSummary* GetLocations() const { return locations_; }
724 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000725
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100726 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100727 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100728
Dave Allison20dfc792014-06-16 20:44:29 -0700729 bool HasOnlyOneUse() const {
730 return uses_ != nullptr && uses_->GetTail() == nullptr;
731 }
732
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100733#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +0100734 bool Is##type() const { return (As##type() != nullptr); } \
735 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000736 virtual H##type* As##type() { return nullptr; }
737
738 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
739#undef INSTRUCTION_TYPE_CHECK
740
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100741 // Returns whether the instruction can be moved within the graph.
742 virtual bool CanBeMoved() const { return false; }
743
744 // Returns whether the two instructions are of the same kind.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700745 virtual bool InstructionTypeEquals(HInstruction* other) const {
746 UNUSED(other);
747 return false;
748 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100749
750 // Returns whether any data encoded in the two instructions is equal.
751 // This method does not look at the inputs. Both instructions must be
752 // of the same type, otherwise the method has undefined behavior.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700753 virtual bool InstructionDataEquals(HInstruction* other) const {
754 UNUSED(other);
755 return false;
756 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100757
758 // Returns whether two instructions are equal, that is:
759 // 1) They have the same type and contain the same data,
760 // 2) Their inputs are identical.
761 bool Equals(HInstruction* other) const;
762
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100763 virtual InstructionKind GetKind() const = 0;
764
765 virtual size_t ComputeHashCode() const {
766 size_t result = GetKind();
767 for (size_t i = 0, e = InputCount(); i < e; ++i) {
768 result = (result * 31) + InputAt(i)->GetId();
769 }
770 return result;
771 }
772
773 SideEffects GetSideEffects() const { return side_effects_; }
774
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100775 size_t GetLifetimePosition() const { return lifetime_position_; }
776 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
777 LiveInterval* GetLiveInterval() const { return live_interval_; }
778 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
779 bool HasLiveInterval() const { return live_interval_ != nullptr; }
780
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000781 private:
782 HInstruction* previous_;
783 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000784 HBasicBlock* block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000785
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000786 // An instruction gets an id when it is added to the graph.
787 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +0100788 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000789 int id_;
790
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100791 // When doing liveness analysis, instructions that have uses get an SSA index.
792 int ssa_index_;
793
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100794 // List of instructions that have this instruction as input.
795 HUseListNode<HInstruction>* uses_;
796
797 // List of environments that contain this instruction.
798 HUseListNode<HEnvironment>* env_uses_;
799
Nicolas Geoffray39468442014-09-02 15:17:15 +0100800 // The environment associated with this instruction. Not null if the instruction
801 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100802 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000803
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000804 // Set by the code generator.
805 LocationSummary* locations_;
806
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100807 // Set by the liveness analysis.
808 LiveInterval* live_interval_;
809
810 // Set by the liveness analysis, this is the position in a linear
811 // order of blocks where this instruction's live interval start.
812 size_t lifetime_position_;
813
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100814 const SideEffects side_effects_;
815
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000816 friend class HBasicBlock;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100817 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000818
819 DISALLOW_COPY_AND_ASSIGN(HInstruction);
820};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700821std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000822
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100823template<typename T>
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000824class HUseIterator : public ValueObject {
825 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100826 explicit HUseIterator(HUseListNode<T>* uses) : current_(uses) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000827
828 bool Done() const { return current_ == nullptr; }
829
830 void Advance() {
831 DCHECK(!Done());
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000832 current_ = current_->GetTail();
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000833 }
834
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100835 HUseListNode<T>* Current() const {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000836 DCHECK(!Done());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100837 return current_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000838 }
839
840 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100841 HUseListNode<T>* current_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000842
843 friend class HValue;
844};
845
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100846// A HEnvironment object contains the values of virtual registers at a given location.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700847class HEnvironment : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100848 public:
849 HEnvironment(ArenaAllocator* arena, size_t number_of_vregs) : vregs_(arena, number_of_vregs) {
850 vregs_.SetSize(number_of_vregs);
851 for (size_t i = 0; i < number_of_vregs; i++) {
852 vregs_.Put(i, nullptr);
853 }
854 }
855
856 void Populate(const GrowableArray<HInstruction*>& env) {
857 for (size_t i = 0; i < env.Size(); i++) {
858 HInstruction* instruction = env.Get(i);
859 vregs_.Put(i, instruction);
860 if (instruction != nullptr) {
861 instruction->AddEnvUseAt(this, i);
862 }
863 }
864 }
865
866 void SetRawEnvAt(size_t index, HInstruction* instruction) {
867 vregs_.Put(index, instruction);
868 }
869
Nicolas Geoffray39468442014-09-02 15:17:15 +0100870 HInstruction* GetInstructionAt(size_t index) const {
871 return vregs_.Get(index);
872 }
873
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100874 GrowableArray<HInstruction*>* GetVRegs() {
875 return &vregs_;
876 }
877
Nicolas Geoffray39468442014-09-02 15:17:15 +0100878 size_t Size() const { return vregs_.Size(); }
879
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100880 private:
881 GrowableArray<HInstruction*> vregs_;
882
883 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
884};
885
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000886class HInputIterator : public ValueObject {
887 public:
Dave Allison20dfc792014-06-16 20:44:29 -0700888 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000889
890 bool Done() const { return index_ == instruction_->InputCount(); }
891 HInstruction* Current() const { return instruction_->InputAt(index_); }
892 void Advance() { index_++; }
893
894 private:
895 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100896 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000897
898 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
899};
900
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000901class HInstructionIterator : public ValueObject {
902 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100903 explicit HInstructionIterator(const HInstructionList& instructions)
904 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000905 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000906 }
907
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000908 bool Done() const { return instruction_ == nullptr; }
909 HInstruction* Current() const { return instruction_; }
910 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000911 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000912 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000913 }
914
915 private:
916 HInstruction* instruction_;
917 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100918
919 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000920};
921
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100922class HBackwardInstructionIterator : public ValueObject {
923 public:
924 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
925 : instruction_(instructions.last_instruction_) {
926 next_ = Done() ? nullptr : instruction_->GetPrevious();
927 }
928
929 bool Done() const { return instruction_ == nullptr; }
930 HInstruction* Current() const { return instruction_; }
931 void Advance() {
932 instruction_ = next_;
933 next_ = Done() ? nullptr : instruction_->GetPrevious();
934 }
935
936 private:
937 HInstruction* instruction_;
938 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100939
940 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100941};
942
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000943// An embedded container with N elements of type T. Used (with partial
944// specialization for N=0) because embedded arrays cannot have size 0.
945template<typename T, intptr_t N>
946class EmbeddedArray {
947 public:
Dave Allison20dfc792014-06-16 20:44:29 -0700948 EmbeddedArray() : elements_() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000949
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000950 intptr_t GetLength() const { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000951
952 const T& operator[](intptr_t i) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000953 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000954 return elements_[i];
955 }
956
957 T& operator[](intptr_t i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000958 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000959 return elements_[i];
960 }
961
962 const T& At(intptr_t i) const {
963 return (*this)[i];
964 }
965
966 void SetAt(intptr_t i, const T& val) {
967 (*this)[i] = val;
968 }
969
970 private:
971 T elements_[N];
972};
973
974template<typename T>
975class EmbeddedArray<T, 0> {
976 public:
977 intptr_t length() const { return 0; }
978 const T& operator[](intptr_t i) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700979 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000980 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700981 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000982 }
983 T& operator[](intptr_t i) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700984 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000985 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700986 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000987 }
988};
989
990template<intptr_t N>
991class HTemplateInstruction: public HInstruction {
992 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100993 HTemplateInstruction<N>(SideEffects side_effects)
994 : HInstruction(side_effects), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -0700995 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000996
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100997 virtual size_t InputCount() const { return N; }
998 virtual HInstruction* InputAt(size_t i) const { return inputs_[i]; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000999
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001000 protected:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001001 virtual void SetRawInputAt(size_t i, HInstruction* instruction) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001002 inputs_[i] = instruction;
1003 }
1004
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001005 private:
1006 EmbeddedArray<HInstruction*, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001007
1008 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001009};
1010
Dave Allison20dfc792014-06-16 20:44:29 -07001011template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001012class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07001013 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001014 HExpression<N>(Primitive::Type type, SideEffects side_effects)
1015 : HTemplateInstruction<N>(side_effects), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07001016 virtual ~HExpression() {}
1017
1018 virtual Primitive::Type GetType() const { return type_; }
1019
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001020 protected:
1021 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07001022};
1023
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001024// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
1025// instruction that branches to the exit block.
1026class HReturnVoid : public HTemplateInstruction<0> {
1027 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001028 HReturnVoid() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001029
1030 virtual bool IsControlFlow() const { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001031
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001032 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001033
1034 private:
1035 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
1036};
1037
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001038// Represents dex's RETURN opcodes. A HReturn is a control flow
1039// instruction that branches to the exit block.
1040class HReturn : public HTemplateInstruction<1> {
1041 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001042 explicit HReturn(HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001043 SetRawInputAt(0, value);
1044 }
1045
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001046 virtual bool IsControlFlow() const { return true; }
1047
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001048 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001049
1050 private:
1051 DISALLOW_COPY_AND_ASSIGN(HReturn);
1052};
1053
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001054// The exit instruction is the only instruction of the exit block.
1055// Instructions aborting the method (HTrow and HReturn) must branch to the
1056// exit block.
1057class HExit : public HTemplateInstruction<0> {
1058 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001059 HExit() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001060
1061 virtual bool IsControlFlow() const { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001062
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001063 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001064
1065 private:
1066 DISALLOW_COPY_AND_ASSIGN(HExit);
1067};
1068
1069// Jumps from one block to another.
1070class HGoto : public HTemplateInstruction<0> {
1071 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001072 HGoto() : HTemplateInstruction(SideEffects::None()) {}
1073
1074 virtual bool IsControlFlow() const { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001075
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001076 HBasicBlock* GetSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001077 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001078 }
1079
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001080 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001081
1082 private:
1083 DISALLOW_COPY_AND_ASSIGN(HGoto);
1084};
1085
Dave Allison20dfc792014-06-16 20:44:29 -07001086
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001087// Conditional branch. A block ending with an HIf instruction must have
1088// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001089class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001090 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001091 explicit HIf(HInstruction* input) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001092 SetRawInputAt(0, input);
1093 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001094
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001095 virtual bool IsControlFlow() const { return true; }
1096
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001097 HBasicBlock* IfTrueSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001098 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001099 }
1100
1101 HBasicBlock* IfFalseSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001102 return GetBlock()->GetSuccessors().Get(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001103 }
1104
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001105 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001106
Dave Allison20dfc792014-06-16 20:44:29 -07001107 virtual bool IsIfInstruction() const { return true; }
1108
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001109 private:
1110 DISALLOW_COPY_AND_ASSIGN(HIf);
1111};
1112
Roland Levillain88cb1752014-10-20 16:36:47 +01001113class HUnaryOperation : public HExpression<1> {
1114 public:
1115 HUnaryOperation(Primitive::Type result_type, HInstruction* input)
1116 : HExpression(result_type, SideEffects::None()) {
1117 SetRawInputAt(0, input);
1118 }
1119
1120 HInstruction* GetInput() const { return InputAt(0); }
1121 Primitive::Type GetResultType() const { return GetType(); }
1122
1123 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001124 virtual bool InstructionDataEquals(HInstruction* other) const {
1125 UNUSED(other);
1126 return true;
1127 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001128
Roland Levillain9240d6a2014-10-20 16:47:04 +01001129 // Try to statically evaluate `operation` and return a HConstant
1130 // containing the result of this evaluation. If `operation` cannot
1131 // be evaluated as a constant, return nullptr.
1132 HConstant* TryStaticEvaluation() const;
1133
1134 // Apply this operation to `x`.
1135 virtual int32_t Evaluate(int32_t x) const = 0;
1136 virtual int64_t Evaluate(int64_t x) const = 0;
1137
Roland Levillain88cb1752014-10-20 16:36:47 +01001138 DECLARE_INSTRUCTION(UnaryOperation);
1139
1140 private:
1141 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
1142};
1143
Dave Allison20dfc792014-06-16 20:44:29 -07001144class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001145 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001146 HBinaryOperation(Primitive::Type result_type,
1147 HInstruction* left,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001148 HInstruction* right) : HExpression(result_type, SideEffects::None()) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001149 SetRawInputAt(0, left);
1150 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001151 }
1152
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001153 HInstruction* GetLeft() const { return InputAt(0); }
1154 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07001155 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001156
1157 virtual bool IsCommutative() { return false; }
1158
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001159 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001160 virtual bool InstructionDataEquals(HInstruction* other) const {
1161 UNUSED(other);
1162 return true;
1163 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001164
Roland Levillain9240d6a2014-10-20 16:47:04 +01001165 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01001166 // containing the result of this evaluation. If `operation` cannot
1167 // be evaluated as a constant, return nullptr.
Roland Levillain9240d6a2014-10-20 16:47:04 +01001168 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01001169
1170 // Apply this operation to `x` and `y`.
1171 virtual int32_t Evaluate(int32_t x, int32_t y) const = 0;
1172 virtual int64_t Evaluate(int64_t x, int64_t y) const = 0;
1173
Roland Levillainccc07a92014-09-16 14:48:16 +01001174 DECLARE_INSTRUCTION(BinaryOperation);
1175
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001176 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001177 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
1178};
1179
Dave Allison20dfc792014-06-16 20:44:29 -07001180class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001181 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001182 HCondition(HInstruction* first, HInstruction* second)
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001183 : HBinaryOperation(Primitive::kPrimBoolean, first, second),
1184 needs_materialization_(true) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001185
1186 virtual bool IsCommutative() { return true; }
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001187
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001188 bool NeedsMaterialization() const { return needs_materialization_; }
1189 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001190
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001191 // For code generation purposes, returns whether this instruction is just before
1192 // `if_`, and disregard moves in between.
1193 bool IsBeforeWhenDisregardMoves(HIf* if_) const;
1194
Dave Allison20dfc792014-06-16 20:44:29 -07001195 DECLARE_INSTRUCTION(Condition);
1196
1197 virtual IfCondition GetCondition() const = 0;
1198
1199 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001200 // For register allocation purposes, returns whether this instruction needs to be
1201 // materialized (that is, not just be in the processor flags).
1202 bool needs_materialization_;
1203
Dave Allison20dfc792014-06-16 20:44:29 -07001204 DISALLOW_COPY_AND_ASSIGN(HCondition);
1205};
1206
1207// Instruction to check if two inputs are equal to each other.
1208class HEqual : public HCondition {
1209 public:
1210 HEqual(HInstruction* first, HInstruction* second)
1211 : HCondition(first, second) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001212
Roland Levillain93445682014-10-06 19:24:02 +01001213 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1214 return x == y ? 1 : 0;
1215 }
1216 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1217 return x == y ? 1 : 0;
1218 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001219
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001220 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001221
Dave Allison20dfc792014-06-16 20:44:29 -07001222 virtual IfCondition GetCondition() const {
1223 return kCondEQ;
1224 }
1225
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001226 private:
1227 DISALLOW_COPY_AND_ASSIGN(HEqual);
1228};
1229
Dave Allison20dfc792014-06-16 20:44:29 -07001230class HNotEqual : public HCondition {
1231 public:
1232 HNotEqual(HInstruction* first, HInstruction* second)
1233 : HCondition(first, second) {}
1234
Roland Levillain93445682014-10-06 19:24:02 +01001235 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1236 return x != y ? 1 : 0;
1237 }
1238 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1239 return x != y ? 1 : 0;
1240 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001241
Dave Allison20dfc792014-06-16 20:44:29 -07001242 DECLARE_INSTRUCTION(NotEqual);
1243
1244 virtual IfCondition GetCondition() const {
1245 return kCondNE;
1246 }
1247
1248 private:
1249 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
1250};
1251
1252class HLessThan : public HCondition {
1253 public:
1254 HLessThan(HInstruction* first, HInstruction* second)
1255 : HCondition(first, second) {}
1256
Roland Levillain93445682014-10-06 19:24:02 +01001257 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1258 return x < y ? 1 : 0;
1259 }
1260 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1261 return x < y ? 1 : 0;
1262 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001263
Dave Allison20dfc792014-06-16 20:44:29 -07001264 DECLARE_INSTRUCTION(LessThan);
1265
1266 virtual IfCondition GetCondition() const {
1267 return kCondLT;
1268 }
1269
1270 private:
1271 DISALLOW_COPY_AND_ASSIGN(HLessThan);
1272};
1273
1274class HLessThanOrEqual : public HCondition {
1275 public:
1276 HLessThanOrEqual(HInstruction* first, HInstruction* second)
1277 : HCondition(first, second) {}
1278
Roland Levillain93445682014-10-06 19:24:02 +01001279 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1280 return x <= y ? 1 : 0;
1281 }
1282 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1283 return x <= y ? 1 : 0;
1284 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001285
Dave Allison20dfc792014-06-16 20:44:29 -07001286 DECLARE_INSTRUCTION(LessThanOrEqual);
1287
1288 virtual IfCondition GetCondition() const {
1289 return kCondLE;
1290 }
1291
1292 private:
1293 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
1294};
1295
1296class HGreaterThan : public HCondition {
1297 public:
1298 HGreaterThan(HInstruction* first, HInstruction* second)
1299 : HCondition(first, second) {}
1300
Roland Levillain93445682014-10-06 19:24:02 +01001301 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1302 return x > y ? 1 : 0;
1303 }
1304 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1305 return x > y ? 1 : 0;
1306 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001307
Dave Allison20dfc792014-06-16 20:44:29 -07001308 DECLARE_INSTRUCTION(GreaterThan);
1309
1310 virtual IfCondition GetCondition() const {
1311 return kCondGT;
1312 }
1313
1314 private:
1315 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
1316};
1317
1318class HGreaterThanOrEqual : public HCondition {
1319 public:
1320 HGreaterThanOrEqual(HInstruction* first, HInstruction* second)
1321 : HCondition(first, second) {}
1322
Roland Levillain93445682014-10-06 19:24:02 +01001323 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1324 return x >= y ? 1 : 0;
1325 }
1326 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1327 return x >= y ? 1 : 0;
1328 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001329
Dave Allison20dfc792014-06-16 20:44:29 -07001330 DECLARE_INSTRUCTION(GreaterThanOrEqual);
1331
1332 virtual IfCondition GetCondition() const {
1333 return kCondGE;
1334 }
1335
1336 private:
1337 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
1338};
1339
1340
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001341// Instruction to check how two inputs compare to each other.
1342// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
1343class HCompare : public HBinaryOperation {
1344 public:
1345 HCompare(Primitive::Type type, HInstruction* first, HInstruction* second)
1346 : HBinaryOperation(Primitive::kPrimInt, first, second) {
1347 DCHECK_EQ(type, first->GetType());
1348 DCHECK_EQ(type, second->GetType());
1349 }
1350
Roland Levillain93445682014-10-06 19:24:02 +01001351 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001352 return
1353 x == y ? 0 :
1354 x > y ? 1 :
1355 -1;
1356 }
Roland Levillain93445682014-10-06 19:24:02 +01001357 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001358 return
1359 x == y ? 0 :
1360 x > y ? 1 :
1361 -1;
1362 }
1363
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001364 DECLARE_INSTRUCTION(Compare);
1365
1366 private:
1367 DISALLOW_COPY_AND_ASSIGN(HCompare);
1368};
1369
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001370// A local in the graph. Corresponds to a Dex register.
1371class HLocal : public HTemplateInstruction<0> {
1372 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001373 explicit HLocal(uint16_t reg_number)
1374 : HTemplateInstruction(SideEffects::None()), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001375
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001376 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001377
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001378 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001379
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001380 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001381 // The Dex register number.
1382 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001383
1384 DISALLOW_COPY_AND_ASSIGN(HLocal);
1385};
1386
1387// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07001388class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001389 public:
Roland Levillain5799fc02014-09-25 12:15:20 +01001390 HLoadLocal(HLocal* local, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001391 : HExpression(type, SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001392 SetRawInputAt(0, local);
1393 }
1394
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001395 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1396
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001397 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001398
1399 private:
1400 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
1401};
1402
1403// Store a value in a given local. This instruction has two inputs: the value
1404// and the local.
1405class HStoreLocal : public HTemplateInstruction<2> {
1406 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001407 HStoreLocal(HLocal* local, HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001408 SetRawInputAt(0, local);
1409 SetRawInputAt(1, value);
1410 }
1411
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001412 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1413
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001414 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001415
1416 private:
1417 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
1418};
1419
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001420class HConstant : public HExpression<0> {
1421 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001422 explicit HConstant(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
1423
1424 virtual bool CanBeMoved() const { return true; }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001425
1426 DECLARE_INSTRUCTION(Constant);
1427
1428 private:
1429 DISALLOW_COPY_AND_ASSIGN(HConstant);
1430};
1431
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001432class HFloatConstant : public HConstant {
1433 public:
1434 explicit HFloatConstant(float value) : HConstant(Primitive::kPrimFloat), value_(value) {}
1435
1436 float GetValue() const { return value_; }
1437
1438 virtual bool InstructionDataEquals(HInstruction* other) const {
1439 return bit_cast<float, int32_t>(other->AsFloatConstant()->value_) ==
1440 bit_cast<float, int32_t>(value_);
1441 }
1442
1443 virtual size_t ComputeHashCode() const { return static_cast<size_t>(GetValue()); }
1444
1445 DECLARE_INSTRUCTION(FloatConstant);
1446
1447 private:
1448 const float value_;
1449
1450 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
1451};
1452
1453class HDoubleConstant : public HConstant {
1454 public:
1455 explicit HDoubleConstant(double value) : HConstant(Primitive::kPrimDouble), value_(value) {}
1456
1457 double GetValue() const { return value_; }
1458
1459 virtual bool InstructionDataEquals(HInstruction* other) const {
1460 return bit_cast<double, int64_t>(other->AsDoubleConstant()->value_) ==
1461 bit_cast<double, int64_t>(value_);
1462 }
1463
1464 virtual size_t ComputeHashCode() const { return static_cast<size_t>(GetValue()); }
1465
1466 DECLARE_INSTRUCTION(DoubleConstant);
1467
1468 private:
1469 const double value_;
1470
1471 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
1472};
1473
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001474// Constants of the type int. Those can be from Dex instructions, or
1475// synthesized (for example with the if-eqz instruction).
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001476class HIntConstant : public HConstant {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001477 public:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001478 explicit HIntConstant(int32_t value) : HConstant(Primitive::kPrimInt), value_(value) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001479
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001480 int32_t GetValue() const { return value_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001481
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001482 virtual bool InstructionDataEquals(HInstruction* other) const {
1483 return other->AsIntConstant()->value_ == value_;
1484 }
1485
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001486 virtual size_t ComputeHashCode() const { return GetValue(); }
1487
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001488 DECLARE_INSTRUCTION(IntConstant);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001489
1490 private:
1491 const int32_t value_;
1492
1493 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
1494};
1495
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001496class HLongConstant : public HConstant {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001497 public:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001498 explicit HLongConstant(int64_t value) : HConstant(Primitive::kPrimLong), value_(value) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001499
1500 int64_t GetValue() const { return value_; }
1501
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001502 virtual bool InstructionDataEquals(HInstruction* other) const {
1503 return other->AsLongConstant()->value_ == value_;
1504 }
1505
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001506 virtual size_t ComputeHashCode() const { return static_cast<size_t>(GetValue()); }
1507
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001508 DECLARE_INSTRUCTION(LongConstant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001509
1510 private:
1511 const int64_t value_;
1512
1513 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
1514};
1515
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001516class HInvoke : public HInstruction {
1517 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001518 HInvoke(ArenaAllocator* arena,
1519 uint32_t number_of_arguments,
1520 Primitive::Type return_type,
1521 uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001522 : HInstruction(SideEffects::All()),
1523 inputs_(arena, number_of_arguments),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001524 return_type_(return_type),
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001525 dex_pc_(dex_pc) {
1526 inputs_.SetSize(number_of_arguments);
1527 }
1528
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001529 virtual size_t InputCount() const { return inputs_.Size(); }
1530 virtual HInstruction* InputAt(size_t i) const { return inputs_.Get(i); }
1531
1532 // Runtime needs to walk the stack, so Dex -> Dex calls need to
1533 // know their environment.
1534 virtual bool NeedsEnvironment() const { return true; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001535
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001536 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001537 SetRawInputAt(index, argument);
1538 }
1539
1540 virtual void SetRawInputAt(size_t index, HInstruction* input) {
1541 inputs_.Put(index, input);
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001542 }
1543
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001544 virtual Primitive::Type GetType() const { return return_type_; }
1545
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001546 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001547
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001548 DECLARE_INSTRUCTION(Invoke);
1549
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001550 protected:
1551 GrowableArray<HInstruction*> inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001552 const Primitive::Type return_type_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001553 const uint32_t dex_pc_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001554
1555 private:
1556 DISALLOW_COPY_AND_ASSIGN(HInvoke);
1557};
1558
1559class HInvokeStatic : public HInvoke {
1560 public:
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001561 HInvokeStatic(ArenaAllocator* arena,
1562 uint32_t number_of_arguments,
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001563 Primitive::Type return_type,
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001564 uint32_t dex_pc,
1565 uint32_t index_in_dex_cache)
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001566 : HInvoke(arena, number_of_arguments, return_type, dex_pc),
1567 index_in_dex_cache_(index_in_dex_cache) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001568
1569 uint32_t GetIndexInDexCache() const { return index_in_dex_cache_; }
1570
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001571 DECLARE_INSTRUCTION(InvokeStatic);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001572
1573 private:
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001574 const uint32_t index_in_dex_cache_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001575
1576 DISALLOW_COPY_AND_ASSIGN(HInvokeStatic);
1577};
1578
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001579class HInvokeVirtual : public HInvoke {
1580 public:
1581 HInvokeVirtual(ArenaAllocator* arena,
1582 uint32_t number_of_arguments,
1583 Primitive::Type return_type,
1584 uint32_t dex_pc,
1585 uint32_t vtable_index)
1586 : HInvoke(arena, number_of_arguments, return_type, dex_pc),
1587 vtable_index_(vtable_index) {}
1588
1589 uint32_t GetVTableIndex() const { return vtable_index_; }
1590
1591 DECLARE_INSTRUCTION(InvokeVirtual);
1592
1593 private:
1594 const uint32_t vtable_index_;
1595
1596 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
1597};
1598
Dave Allison20dfc792014-06-16 20:44:29 -07001599class HNewInstance : public HExpression<0> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001600 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001601 HNewInstance(uint32_t dex_pc, uint16_t type_index)
1602 : HExpression(Primitive::kPrimNot, SideEffects::None()),
1603 dex_pc_(dex_pc),
1604 type_index_(type_index) {}
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001605
1606 uint32_t GetDexPc() const { return dex_pc_; }
1607 uint16_t GetTypeIndex() const { return type_index_; }
1608
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001609 // Calls runtime so needs an environment.
1610 virtual bool NeedsEnvironment() const { return true; }
1611
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001612 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001613
1614 private:
1615 const uint32_t dex_pc_;
1616 const uint16_t type_index_;
1617
1618 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
1619};
1620
Roland Levillain88cb1752014-10-20 16:36:47 +01001621class HNeg : public HUnaryOperation {
1622 public:
1623 explicit HNeg(Primitive::Type result_type, HInstruction* input)
1624 : HUnaryOperation(result_type, input) {}
1625
Roland Levillain9240d6a2014-10-20 16:47:04 +01001626 virtual int32_t Evaluate(int32_t x) const OVERRIDE { return -x; }
1627 virtual int64_t Evaluate(int64_t x) const OVERRIDE { return -x; }
1628
Roland Levillain88cb1752014-10-20 16:36:47 +01001629 DECLARE_INSTRUCTION(Neg);
1630
1631 private:
1632 DISALLOW_COPY_AND_ASSIGN(HNeg);
1633};
1634
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001635class HNewArray : public HExpression<1> {
1636 public:
1637 HNewArray(HInstruction* length, uint32_t dex_pc, uint16_t type_index)
1638 : HExpression(Primitive::kPrimNot, SideEffects::None()),
1639 dex_pc_(dex_pc),
1640 type_index_(type_index) {
1641 SetRawInputAt(0, length);
1642 }
1643
1644 uint32_t GetDexPc() const { return dex_pc_; }
1645 uint16_t GetTypeIndex() const { return type_index_; }
1646
1647 // Calls runtime so needs an environment.
1648 virtual bool NeedsEnvironment() const { return true; }
1649
1650 DECLARE_INSTRUCTION(NewArray);
1651
1652 private:
1653 const uint32_t dex_pc_;
1654 const uint16_t type_index_;
1655
1656 DISALLOW_COPY_AND_ASSIGN(HNewArray);
1657};
1658
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001659class HAdd : public HBinaryOperation {
1660 public:
1661 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1662 : HBinaryOperation(result_type, left, right) {}
1663
1664 virtual bool IsCommutative() { return true; }
1665
Roland Levillain93445682014-10-06 19:24:02 +01001666 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1667 return x + y;
1668 }
1669 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1670 return x + y;
1671 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001672
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001673 DECLARE_INSTRUCTION(Add);
1674
1675 private:
1676 DISALLOW_COPY_AND_ASSIGN(HAdd);
1677};
1678
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001679class HSub : public HBinaryOperation {
1680 public:
1681 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1682 : HBinaryOperation(result_type, left, right) {}
1683
Roland Levillain93445682014-10-06 19:24:02 +01001684 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1685 return x - y;
1686 }
1687 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1688 return x - y;
1689 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001690
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001691 DECLARE_INSTRUCTION(Sub);
1692
1693 private:
1694 DISALLOW_COPY_AND_ASSIGN(HSub);
1695};
1696
Calin Juravle34bacdf2014-10-07 20:23:36 +01001697class HMul : public HBinaryOperation {
1698 public:
1699 HMul(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1700 : HBinaryOperation(result_type, left, right) {}
1701
1702 virtual bool IsCommutative() { return true; }
1703
1704 virtual int32_t Evaluate(int32_t x, int32_t y) const { return x * y; }
1705 virtual int64_t Evaluate(int64_t x, int64_t y) const { return x * y; }
1706
1707 DECLARE_INSTRUCTION(Mul);
1708
1709 private:
1710 DISALLOW_COPY_AND_ASSIGN(HMul);
1711};
1712
Calin Juravle7c4954d2014-10-28 16:57:40 +00001713class HDiv : public HBinaryOperation {
1714 public:
1715 HDiv(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1716 : HBinaryOperation(result_type, left, right) {}
1717
1718 virtual int32_t Evaluate(int32_t x, int32_t y) const { return x / y; }
1719 virtual int64_t Evaluate(int64_t x, int64_t y) const { return x / y; }
1720
1721 DECLARE_INSTRUCTION(Div);
1722
1723 private:
1724 DISALLOW_COPY_AND_ASSIGN(HDiv);
1725};
1726
Calin Juravled0d48522014-11-04 16:40:20 +00001727class HDivZeroCheck : public HExpression<1> {
1728 public:
1729 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
1730 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
1731 SetRawInputAt(0, value);
1732 }
1733
1734 bool CanBeMoved() const OVERRIDE { return true; }
1735
1736 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
1737 UNUSED(other);
1738 return true;
1739 }
1740
1741 bool NeedsEnvironment() const OVERRIDE { return true; }
1742 bool CanThrow() const OVERRIDE { return true; }
1743
1744 uint32_t GetDexPc() const { return dex_pc_; }
1745
1746 DECLARE_INSTRUCTION(DivZeroCheck);
1747
1748 private:
1749 const uint32_t dex_pc_;
1750
1751 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
1752};
1753
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001754// The value of a parameter in this method. Its location depends on
1755// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07001756class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001757 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001758 HParameterValue(uint8_t index, Primitive::Type parameter_type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001759 : HExpression(parameter_type, SideEffects::None()), index_(index) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001760
1761 uint8_t GetIndex() const { return index_; }
1762
1763 DECLARE_INSTRUCTION(ParameterValue);
1764
1765 private:
1766 // The index of this parameter in the parameters list. Must be less
1767 // than HGraph::number_of_in_vregs_;
1768 const uint8_t index_;
1769
1770 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
1771};
1772
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001773class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001774 public:
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001775 explicit HNot(Primitive::Type result_type, HInstruction* input)
1776 : HUnaryOperation(result_type, input) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001777
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001778 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001779 virtual bool InstructionDataEquals(HInstruction* other) const {
1780 UNUSED(other);
1781 return true;
1782 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001783
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001784 virtual int32_t Evaluate(int32_t x) const OVERRIDE { return ~x; }
1785 virtual int64_t Evaluate(int64_t x) const OVERRIDE { return ~x; }
1786
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001787 DECLARE_INSTRUCTION(Not);
1788
1789 private:
1790 DISALLOW_COPY_AND_ASSIGN(HNot);
1791};
1792
Roland Levillaindff1f282014-11-05 14:15:05 +00001793class HTypeConversion : public HExpression<1> {
1794 public:
1795 // Instantiate a type conversion of `input` to `result_type`.
1796 HTypeConversion(Primitive::Type result_type, HInstruction* input)
1797 : HExpression(result_type, SideEffects::None()) {
1798 SetRawInputAt(0, input);
1799 DCHECK_NE(input->GetType(), result_type);
1800 }
1801
1802 HInstruction* GetInput() const { return InputAt(0); }
1803 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
1804 Primitive::Type GetResultType() const { return GetType(); }
1805
1806 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00001807 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00001808
1809 DECLARE_INSTRUCTION(TypeConversion);
1810
1811 private:
1812 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
1813};
1814
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001815class HPhi : public HInstruction {
1816 public:
1817 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001818 : HInstruction(SideEffects::None()),
1819 inputs_(arena, number_of_inputs),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001820 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001821 type_(type),
1822 is_live_(false) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001823 inputs_.SetSize(number_of_inputs);
1824 }
1825
1826 virtual size_t InputCount() const { return inputs_.Size(); }
1827 virtual HInstruction* InputAt(size_t i) const { return inputs_.Get(i); }
1828
1829 virtual void SetRawInputAt(size_t index, HInstruction* input) {
1830 inputs_.Put(index, input);
1831 }
1832
1833 void AddInput(HInstruction* input);
1834
1835 virtual Primitive::Type GetType() const { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001836 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001837
1838 uint32_t GetRegNumber() const { return reg_number_; }
1839
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001840 void SetDead() { is_live_ = false; }
1841 void SetLive() { is_live_ = true; }
1842 bool IsDead() const { return !is_live_; }
1843 bool IsLive() const { return is_live_; }
1844
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001845 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001846
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001847 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001848 GrowableArray<HInstruction*> inputs_;
1849 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001850 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001851 bool is_live_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001852
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001853 DISALLOW_COPY_AND_ASSIGN(HPhi);
1854};
1855
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001856class HNullCheck : public HExpression<1> {
1857 public:
1858 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001859 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001860 SetRawInputAt(0, value);
1861 }
1862
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001863 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001864 virtual bool InstructionDataEquals(HInstruction* other) const {
1865 UNUSED(other);
1866 return true;
1867 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001868
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001869 virtual bool NeedsEnvironment() const { return true; }
1870
Roland Levillaine161a2a2014-10-03 12:45:18 +01001871 virtual bool CanThrow() const { return true; }
1872
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001873 uint32_t GetDexPc() const { return dex_pc_; }
1874
1875 DECLARE_INSTRUCTION(NullCheck);
1876
1877 private:
1878 const uint32_t dex_pc_;
1879
1880 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
1881};
1882
1883class FieldInfo : public ValueObject {
1884 public:
Roland Levillain5799fc02014-09-25 12:15:20 +01001885 FieldInfo(MemberOffset field_offset, Primitive::Type field_type)
Nicolas Geoffray39468442014-09-02 15:17:15 +01001886 : field_offset_(field_offset), field_type_(field_type) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001887
1888 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001889 Primitive::Type GetFieldType() const { return field_type_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001890
1891 private:
1892 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01001893 const Primitive::Type field_type_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001894};
1895
1896class HInstanceFieldGet : public HExpression<1> {
1897 public:
1898 HInstanceFieldGet(HInstruction* value,
1899 Primitive::Type field_type,
1900 MemberOffset field_offset)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001901 : HExpression(field_type, SideEffects::DependsOnSomething()),
1902 field_info_(field_offset, field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001903 SetRawInputAt(0, value);
1904 }
1905
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001906 virtual bool CanBeMoved() const { return true; }
1907 virtual bool InstructionDataEquals(HInstruction* other) const {
1908 size_t other_offset = other->AsInstanceFieldGet()->GetFieldOffset().SizeValue();
1909 return other_offset == GetFieldOffset().SizeValue();
1910 }
1911
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001912 virtual size_t ComputeHashCode() const {
1913 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
1914 }
1915
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001916 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001917 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001918
1919 DECLARE_INSTRUCTION(InstanceFieldGet);
1920
1921 private:
1922 const FieldInfo field_info_;
1923
1924 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
1925};
1926
1927class HInstanceFieldSet : public HTemplateInstruction<2> {
1928 public:
1929 HInstanceFieldSet(HInstruction* object,
1930 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01001931 Primitive::Type field_type,
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001932 MemberOffset field_offset)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001933 : HTemplateInstruction(SideEffects::ChangesSomething()),
1934 field_info_(field_offset, field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001935 SetRawInputAt(0, object);
1936 SetRawInputAt(1, value);
1937 }
1938
1939 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001940 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001941
1942 DECLARE_INSTRUCTION(InstanceFieldSet);
1943
1944 private:
1945 const FieldInfo field_info_;
1946
1947 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
1948};
1949
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001950class HArrayGet : public HExpression<2> {
1951 public:
1952 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001953 : HExpression(type, SideEffects::DependsOnSomething()) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001954 SetRawInputAt(0, array);
1955 SetRawInputAt(1, index);
1956 }
1957
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001958 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001959 virtual bool InstructionDataEquals(HInstruction* other) const {
1960 UNUSED(other);
1961 return true;
1962 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001963 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001964
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001965 DECLARE_INSTRUCTION(ArrayGet);
1966
1967 private:
1968 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
1969};
1970
1971class HArraySet : public HTemplateInstruction<3> {
1972 public:
1973 HArraySet(HInstruction* array,
1974 HInstruction* index,
1975 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001976 Primitive::Type expected_component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001977 uint32_t dex_pc)
1978 : HTemplateInstruction(SideEffects::ChangesSomething()),
1979 dex_pc_(dex_pc),
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001980 expected_component_type_(expected_component_type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001981 SetRawInputAt(0, array);
1982 SetRawInputAt(1, index);
1983 SetRawInputAt(2, value);
1984 }
1985
1986 virtual bool NeedsEnvironment() const {
1987 // We currently always call a runtime method to catch array store
1988 // exceptions.
1989 return InputAt(2)->GetType() == Primitive::kPrimNot;
1990 }
1991
1992 uint32_t GetDexPc() const { return dex_pc_; }
1993
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001994 HInstruction* GetValue() const { return InputAt(2); }
1995
1996 Primitive::Type GetComponentType() const {
1997 // The Dex format does not type floating point index operations. Since the
1998 // `expected_component_type_` is set during building and can therefore not
1999 // be correct, we also check what is the value type. If it is a floating
2000 // point type, we must use that type.
2001 Primitive::Type value_type = GetValue()->GetType();
2002 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
2003 ? value_type
2004 : expected_component_type_;
2005 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002006
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002007 DECLARE_INSTRUCTION(ArraySet);
2008
2009 private:
2010 const uint32_t dex_pc_;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002011 const Primitive::Type expected_component_type_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002012
2013 DISALLOW_COPY_AND_ASSIGN(HArraySet);
2014};
2015
2016class HArrayLength : public HExpression<1> {
2017 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002018 explicit HArrayLength(HInstruction* array)
2019 : HExpression(Primitive::kPrimInt, SideEffects::None()) {
2020 // Note that arrays do not change length, so the instruction does not
2021 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002022 SetRawInputAt(0, array);
2023 }
2024
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002025 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002026 virtual bool InstructionDataEquals(HInstruction* other) const {
2027 UNUSED(other);
2028 return true;
2029 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002030
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002031 DECLARE_INSTRUCTION(ArrayLength);
2032
2033 private:
2034 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
2035};
2036
2037class HBoundsCheck : public HExpression<2> {
2038 public:
2039 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002040 : HExpression(index->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002041 DCHECK(index->GetType() == Primitive::kPrimInt);
2042 SetRawInputAt(0, index);
2043 SetRawInputAt(1, length);
2044 }
2045
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002046 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002047 virtual bool InstructionDataEquals(HInstruction* other) const {
2048 UNUSED(other);
2049 return true;
2050 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002051
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002052 virtual bool NeedsEnvironment() const { return true; }
2053
Roland Levillaine161a2a2014-10-03 12:45:18 +01002054 virtual bool CanThrow() const { return true; }
2055
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002056 uint32_t GetDexPc() const { return dex_pc_; }
2057
2058 DECLARE_INSTRUCTION(BoundsCheck);
2059
2060 private:
2061 const uint32_t dex_pc_;
2062
2063 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
2064};
2065
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002066/**
2067 * Some DEX instructions are folded into multiple HInstructions that need
2068 * to stay live until the last HInstruction. This class
2069 * is used as a marker for the baseline compiler to ensure its preceding
2070 * HInstruction stays live. `index` is the temporary number that is used
2071 * for knowing the stack offset where to store the instruction.
2072 */
2073class HTemporary : public HTemplateInstruction<0> {
2074 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002075 explicit HTemporary(size_t index) : HTemplateInstruction(SideEffects::None()), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002076
2077 size_t GetIndex() const { return index_; }
2078
2079 DECLARE_INSTRUCTION(Temporary);
2080
2081 private:
2082 const size_t index_;
2083
2084 DISALLOW_COPY_AND_ASSIGN(HTemporary);
2085};
2086
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002087class HSuspendCheck : public HTemplateInstruction<0> {
2088 public:
2089 explicit HSuspendCheck(uint32_t dex_pc)
Nicolas Geoffray9ebc72c2014-09-25 16:33:42 +01002090 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002091
2092 virtual bool NeedsEnvironment() const {
2093 return true;
2094 }
2095
2096 uint32_t GetDexPc() const { return dex_pc_; }
2097
2098 DECLARE_INSTRUCTION(SuspendCheck);
2099
2100 private:
2101 const uint32_t dex_pc_;
2102
2103 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
2104};
2105
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002106/**
2107 * Instruction to load a Class object.
2108 */
2109class HLoadClass : public HExpression<0> {
2110 public:
2111 HLoadClass(uint16_t type_index,
2112 bool is_referrers_class,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002113 uint32_t dex_pc)
2114 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2115 type_index_(type_index),
2116 is_referrers_class_(is_referrers_class),
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002117 dex_pc_(dex_pc),
2118 generate_clinit_check_(false) {}
2119
2120 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002121
2122 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2123 return other->AsLoadClass()->type_index_ == type_index_;
2124 }
2125
2126 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
2127
2128 uint32_t GetDexPc() const { return dex_pc_; }
2129 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002130 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002131
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002132 bool NeedsEnvironment() const OVERRIDE {
2133 // Will call runtime and load the class if the class is not loaded yet.
2134 // TODO: finer grain decision.
2135 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002136 }
2137
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002138 bool MustGenerateClinitCheck() const {
2139 return generate_clinit_check_;
2140 }
2141
2142 void SetMustGenerateClinitCheck() {
2143 generate_clinit_check_ = true;
2144 }
2145
2146 bool CanCallRuntime() const {
2147 return MustGenerateClinitCheck() || !is_referrers_class_;
2148 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002149
2150 DECLARE_INSTRUCTION(LoadClass);
2151
2152 private:
2153 const uint16_t type_index_;
2154 const bool is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002155 const uint32_t dex_pc_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002156 // Whether this instruction must generate the initialization check.
2157 // Used for code generation.
2158 bool generate_clinit_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002159
2160 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
2161};
2162
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002163class HLoadString : public HExpression<0> {
2164 public:
2165 HLoadString(uint32_t string_index, uint32_t dex_pc)
2166 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2167 string_index_(string_index),
2168 dex_pc_(dex_pc) {}
2169
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002170 bool CanBeMoved() const OVERRIDE { return true; }
2171
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002172 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2173 return other->AsLoadString()->string_index_ == string_index_;
2174 }
2175
2176 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
2177
2178 uint32_t GetDexPc() const { return dex_pc_; }
2179 uint32_t GetStringIndex() const { return string_index_; }
2180
2181 // TODO: Can we deopt or debug when we resolve a string?
2182 bool NeedsEnvironment() const OVERRIDE { return false; }
2183
2184 DECLARE_INSTRUCTION(LoadString);
2185
2186 private:
2187 const uint32_t string_index_;
2188 const uint32_t dex_pc_;
2189
2190 DISALLOW_COPY_AND_ASSIGN(HLoadString);
2191};
2192
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002193// TODO: Pass this check to HInvokeStatic nodes.
2194/**
2195 * Performs an initialization check on its Class object input.
2196 */
2197class HClinitCheck : public HExpression<1> {
2198 public:
2199 explicit HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
2200 : HExpression(Primitive::kPrimNot, SideEffects::All()),
2201 dex_pc_(dex_pc) {
2202 SetRawInputAt(0, constant);
2203 }
2204
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002205 bool CanBeMoved() const OVERRIDE { return true; }
2206 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2207 UNUSED(other);
2208 return true;
2209 }
2210
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002211 bool NeedsEnvironment() const OVERRIDE {
2212 // May call runtime to initialize the class.
2213 return true;
2214 }
2215
2216 uint32_t GetDexPc() const { return dex_pc_; }
2217
2218 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
2219
2220 DECLARE_INSTRUCTION(ClinitCheck);
2221
2222 private:
2223 const uint32_t dex_pc_;
2224
2225 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
2226};
2227
2228class HStaticFieldGet : public HExpression<1> {
2229 public:
2230 HStaticFieldGet(HInstruction* cls,
2231 Primitive::Type field_type,
2232 MemberOffset field_offset)
2233 : HExpression(field_type, SideEffects::DependsOnSomething()),
2234 field_info_(field_offset, field_type) {
2235 SetRawInputAt(0, cls);
2236 }
2237
2238 bool CanBeMoved() const OVERRIDE { return true; }
2239 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2240 size_t other_offset = other->AsStaticFieldGet()->GetFieldOffset().SizeValue();
2241 return other_offset == GetFieldOffset().SizeValue();
2242 }
2243
2244 size_t ComputeHashCode() const OVERRIDE {
2245 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
2246 }
2247
2248 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
2249 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
2250
2251 DECLARE_INSTRUCTION(StaticFieldGet);
2252
2253 private:
2254 const FieldInfo field_info_;
2255
2256 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
2257};
2258
2259class HStaticFieldSet : public HTemplateInstruction<2> {
2260 public:
2261 HStaticFieldSet(HInstruction* cls,
2262 HInstruction* value,
2263 Primitive::Type field_type,
2264 MemberOffset field_offset)
2265 : HTemplateInstruction(SideEffects::ChangesSomething()),
2266 field_info_(field_offset, field_type) {
2267 SetRawInputAt(0, cls);
2268 SetRawInputAt(1, value);
2269 }
2270
2271 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
2272 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
2273
2274 DECLARE_INSTRUCTION(StaticFieldSet);
2275
2276 private:
2277 const FieldInfo field_info_;
2278
2279 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
2280};
2281
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002282class MoveOperands : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002283 public:
Nicolas Geoffray740475d2014-09-29 10:33:25 +01002284 MoveOperands(Location source, Location destination, HInstruction* instruction)
2285 : source_(source), destination_(destination), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002286
2287 Location GetSource() const { return source_; }
2288 Location GetDestination() const { return destination_; }
2289
2290 void SetSource(Location value) { source_ = value; }
2291 void SetDestination(Location value) { destination_ = value; }
2292
2293 // The parallel move resolver marks moves as "in-progress" by clearing the
2294 // destination (but not the source).
2295 Location MarkPending() {
2296 DCHECK(!IsPending());
2297 Location dest = destination_;
2298 destination_ = Location::NoLocation();
2299 return dest;
2300 }
2301
2302 void ClearPending(Location dest) {
2303 DCHECK(IsPending());
2304 destination_ = dest;
2305 }
2306
2307 bool IsPending() const {
2308 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
2309 return destination_.IsInvalid() && !source_.IsInvalid();
2310 }
2311
2312 // True if this blocks a move from the given location.
2313 bool Blocks(Location loc) const {
2314 return !IsEliminated() && source_.Equals(loc);
2315 }
2316
2317 // A move is redundant if it's been eliminated, if its source and
2318 // destination are the same, or if its destination is unneeded.
2319 bool IsRedundant() const {
2320 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
2321 }
2322
2323 // We clear both operands to indicate move that's been eliminated.
2324 void Eliminate() {
2325 source_ = destination_ = Location::NoLocation();
2326 }
2327
2328 bool IsEliminated() const {
2329 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
2330 return source_.IsInvalid();
2331 }
2332
Nicolas Geoffray740475d2014-09-29 10:33:25 +01002333 HInstruction* GetInstruction() const { return instruction_; }
2334
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002335 private:
2336 Location source_;
2337 Location destination_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01002338 // The instruction this move is assocatied with. Null when this move is
2339 // for moving an input in the expected locations of user (including a phi user).
2340 // This is only used in debug mode, to ensure we do not connect interval siblings
2341 // in the same parallel move.
2342 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002343
2344 DISALLOW_COPY_AND_ASSIGN(MoveOperands);
2345};
2346
2347static constexpr size_t kDefaultNumberOfMoves = 4;
2348
2349class HParallelMove : public HTemplateInstruction<0> {
2350 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002351 explicit HParallelMove(ArenaAllocator* arena)
2352 : HTemplateInstruction(SideEffects::None()), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002353
2354 void AddMove(MoveOperands* move) {
Nicolas Geoffray740475d2014-09-29 10:33:25 +01002355 if (kIsDebugBuild && move->GetInstruction() != nullptr) {
2356 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
2357 DCHECK_NE(moves_.Get(i)->GetInstruction(), move->GetInstruction())
2358 << "Doing parallel moves for the same instruction.";
2359 }
2360 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002361 moves_.Add(move);
2362 }
2363
2364 MoveOperands* MoveOperandsAt(size_t index) const {
2365 return moves_.Get(index);
2366 }
2367
2368 size_t NumMoves() const { return moves_.Size(); }
2369
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002370 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002371
2372 private:
2373 GrowableArray<MoveOperands*> moves_;
2374
2375 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
2376};
2377
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002378class HGraphVisitor : public ValueObject {
2379 public:
Dave Allison20dfc792014-06-16 20:44:29 -07002380 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
2381 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002382
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002383 virtual void VisitInstruction(HInstruction* instruction) { UNUSED(instruction); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002384 virtual void VisitBasicBlock(HBasicBlock* block);
2385
Roland Levillain633021e2014-10-01 14:12:25 +01002386 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002387 void VisitInsertionOrder();
2388
Roland Levillain633021e2014-10-01 14:12:25 +01002389 // Visit the graph following dominator tree reverse post-order.
2390 void VisitReversePostOrder();
2391
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002392 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002393
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002394 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002395#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002396 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
2397
2398 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
2399
2400#undef DECLARE_VISIT_INSTRUCTION
2401
2402 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07002403 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002404
2405 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
2406};
2407
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002408class HGraphDelegateVisitor : public HGraphVisitor {
2409 public:
2410 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
2411 virtual ~HGraphDelegateVisitor() {}
2412
2413 // Visit functions that delegate to to super class.
2414#define DECLARE_VISIT_INSTRUCTION(name, super) \
2415 virtual void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
2416
2417 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
2418
2419#undef DECLARE_VISIT_INSTRUCTION
2420
2421 private:
2422 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
2423};
2424
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002425class HInsertionOrderIterator : public ValueObject {
2426 public:
2427 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
2428
2429 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
2430 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
2431 void Advance() { ++index_; }
2432
2433 private:
2434 const HGraph& graph_;
2435 size_t index_;
2436
2437 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
2438};
2439
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002440class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002441 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002442 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002443
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002444 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
2445 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002446 void Advance() { ++index_; }
2447
2448 private:
2449 const HGraph& graph_;
2450 size_t index_;
2451
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002452 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002453};
2454
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002455class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002456 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002457 explicit HPostOrderIterator(const HGraph& graph)
2458 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002459
2460 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002461 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002462 void Advance() { --index_; }
2463
2464 private:
2465 const HGraph& graph_;
2466 size_t index_;
2467
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002468 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002469};
2470
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002471} // namespace art
2472
2473#endif // ART_COMPILER_OPTIMIZING_NODES_H_