blob: d7069ca3125d2d9aab1d53f53ae7e654cea28e7d [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) \
481 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100482 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000483 M(Exit, Instruction) \
484 M(FloatConstant, Constant) \
485 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100486 M(GreaterThan, Condition) \
487 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100488 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000489 M(InstanceFieldGet, Instruction) \
490 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100491 M(IntConstant, Constant) \
492 M(InvokeStatic, Invoke) \
493 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000494 M(LessThan, Condition) \
495 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000496 M(LoadClass, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100497 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000498 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100499 M(Local, Instruction) \
500 M(LongConstant, Constant) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000501 M(Mul, BinaryOperation) \
502 M(Neg, UnaryOperation) \
503 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100504 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +0100505 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000506 M(NotEqual, Condition) \
507 M(NullCheck, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100508 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000509 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100510 M(Phi, Instruction) \
511 M(Return, Instruction) \
512 M(ReturnVoid, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100513 M(StaticFieldGet, Instruction) \
514 M(StaticFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100515 M(StoreLocal, Instruction) \
516 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100517 M(SuspendCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000518 M(Temporary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +0000519 M(TypeConversion, Instruction) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000520
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100521#define FOR_EACH_INSTRUCTION(M) \
522 FOR_EACH_CONCRETE_INSTRUCTION(M) \
523 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +0100524 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +0100525 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100526 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -0700527
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100528#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000529FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
530#undef FORWARD_DECLARATION
531
Roland Levillainccc07a92014-09-16 14:48:16 +0100532#define DECLARE_INSTRUCTION(type) \
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100533 virtual InstructionKind GetKind() const { return k##type; } \
Roland Levillainccc07a92014-09-16 14:48:16 +0100534 virtual const char* DebugName() const { return #type; } \
535 virtual const H##type* As##type() const OVERRIDE { return this; } \
536 virtual H##type* As##type() OVERRIDE { return this; } \
537 virtual bool InstructionTypeEquals(HInstruction* other) const { \
538 return other->Is##type(); \
539 } \
540 virtual void Accept(HGraphVisitor* visitor)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000541
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100542template <typename T>
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700543class HUseListNode : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000544 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100545 HUseListNode(T* user, size_t index, HUseListNode* tail)
Dave Allison20dfc792014-06-16 20:44:29 -0700546 : user_(user), index_(index), tail_(tail) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000547
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000548 HUseListNode* GetTail() const { return tail_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100549 T* GetUser() const { return user_; }
550 size_t GetIndex() const { return index_; }
551
552 void SetTail(HUseListNode<T>* node) { tail_ = node; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000553
554 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100555 T* const user_;
556 const size_t index_;
557 HUseListNode<T>* tail_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000558
559 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
560};
561
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100562// Represents the side effects an instruction may have.
563class SideEffects : public ValueObject {
564 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100565 SideEffects() : flags_(0) {}
566
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100567 static SideEffects None() {
568 return SideEffects(0);
569 }
570
571 static SideEffects All() {
572 return SideEffects(ChangesSomething().flags_ | DependsOnSomething().flags_);
573 }
574
575 static SideEffects ChangesSomething() {
576 return SideEffects((1 << kFlagChangesCount) - 1);
577 }
578
579 static SideEffects DependsOnSomething() {
580 int count = kFlagDependsOnCount - kFlagChangesCount;
581 return SideEffects(((1 << count) - 1) << kFlagChangesCount);
582 }
583
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100584 SideEffects Union(SideEffects other) const {
585 return SideEffects(flags_ | other.flags_);
586 }
587
Roland Levillain72bceff2014-09-15 18:29:00 +0100588 bool HasSideEffects() const {
589 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
590 return (flags_ & all_bits_set) != 0;
591 }
592
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100593 bool HasAllSideEffects() const {
594 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
595 return all_bits_set == (flags_ & all_bits_set);
596 }
597
598 bool DependsOn(SideEffects other) const {
599 size_t depends_flags = other.ComputeDependsFlags();
600 return (flags_ & depends_flags) != 0;
601 }
602
603 bool HasDependencies() const {
604 int count = kFlagDependsOnCount - kFlagChangesCount;
605 size_t all_bits_set = (1 << count) - 1;
606 return ((flags_ >> kFlagChangesCount) & all_bits_set) != 0;
607 }
608
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100609 private:
610 static constexpr int kFlagChangesSomething = 0;
611 static constexpr int kFlagChangesCount = kFlagChangesSomething + 1;
612
613 static constexpr int kFlagDependsOnSomething = kFlagChangesCount;
614 static constexpr int kFlagDependsOnCount = kFlagDependsOnSomething + 1;
615
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100616 explicit SideEffects(size_t flags) : flags_(flags) {}
617
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100618 size_t ComputeDependsFlags() const {
619 return flags_ << kFlagChangesCount;
620 }
621
622 size_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100623};
624
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700625class HInstruction : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000626 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100627 explicit HInstruction(SideEffects side_effects)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000628 : previous_(nullptr),
629 next_(nullptr),
630 block_(nullptr),
631 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100632 ssa_index_(-1),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000633 uses_(nullptr),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100634 env_uses_(nullptr),
635 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100636 locations_(nullptr),
637 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100638 lifetime_position_(kNoLifetime),
639 side_effects_(side_effects) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000640
Dave Allison20dfc792014-06-16 20:44:29 -0700641 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000642
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100643#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100644 enum InstructionKind {
645 FOR_EACH_INSTRUCTION(DECLARE_KIND)
646 };
647#undef DECLARE_KIND
648
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000649 HInstruction* GetNext() const { return next_; }
650 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000651
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000652 HBasicBlock* GetBlock() const { return block_; }
653 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100654 bool IsInBlock() const { return block_ != nullptr; }
655 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +0100656 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000657
Roland Levillain6b879dd2014-09-22 17:13:44 +0100658 virtual size_t InputCount() const = 0;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100659 virtual HInstruction* InputAt(size_t i) const = 0;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000660
661 virtual void Accept(HGraphVisitor* visitor) = 0;
662 virtual const char* DebugName() const = 0;
663
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100664 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100665 virtual void SetRawInputAt(size_t index, HInstruction* input) = 0;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100666
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100667 virtual bool NeedsEnvironment() const { return false; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100668 virtual bool IsControlFlow() const { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +0100669 virtual bool CanThrow() const { return false; }
Roland Levillain72bceff2014-09-15 18:29:00 +0100670 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100671
672 void AddUseAt(HInstruction* user, size_t index) {
673 uses_ = new (block_->GetGraph()->GetArena()) HUseListNode<HInstruction>(user, index, uses_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000674 }
675
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100676 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +0100677 DCHECK(user != nullptr);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100678 env_uses_ = new (block_->GetGraph()->GetArena()) HUseListNode<HEnvironment>(
679 user, index, env_uses_);
680 }
681
682 void RemoveUser(HInstruction* user, size_t index);
Nicolas Geoffray724c9632014-09-22 12:27:27 +0100683 void RemoveEnvironmentUser(HEnvironment* user, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100684
685 HUseListNode<HInstruction>* GetUses() const { return uses_; }
686 HUseListNode<HEnvironment>* GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000687
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100688 bool HasUses() const { return uses_ != nullptr || env_uses_ != nullptr; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100689 bool HasEnvironmentUses() const { return env_uses_ != nullptr; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000690
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100691 size_t NumberOfUses() const {
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100692 // TODO: Optimize this method if it is used outside of the HGraphVisualizer.
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100693 size_t result = 0;
694 HUseListNode<HInstruction>* current = uses_;
695 while (current != nullptr) {
696 current = current->GetTail();
697 ++result;
698 }
699 return result;
700 }
701
Roland Levillain6c82d402014-10-13 16:10:27 +0100702 // Does this instruction strictly dominate `other_instruction`?
703 // Returns false if this instruction and `other_instruction` are the same.
704 // Aborts if this instruction and `other_instruction` are both phis.
705 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +0100706
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000707 int GetId() const { return id_; }
708 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000709
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100710 int GetSsaIndex() const { return ssa_index_; }
711 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
712 bool HasSsaIndex() const { return ssa_index_ != -1; }
713
714 bool HasEnvironment() const { return environment_ != nullptr; }
715 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100716 void SetEnvironment(HEnvironment* environment) { environment_ = environment; }
717
Nicolas Geoffray39468442014-09-02 15:17:15 +0100718 // Returns the number of entries in the environment. Typically, that is the
719 // number of dex registers in a method. It could be more in case of inlining.
720 size_t EnvironmentSize() const;
721
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000722 LocationSummary* GetLocations() const { return locations_; }
723 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000724
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100725 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100726 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100727
Dave Allison20dfc792014-06-16 20:44:29 -0700728 bool HasOnlyOneUse() const {
729 return uses_ != nullptr && uses_->GetTail() == nullptr;
730 }
731
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100732#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +0100733 bool Is##type() const { return (As##type() != nullptr); } \
734 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000735 virtual H##type* As##type() { return nullptr; }
736
737 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
738#undef INSTRUCTION_TYPE_CHECK
739
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100740 // Returns whether the instruction can be moved within the graph.
741 virtual bool CanBeMoved() const { return false; }
742
743 // Returns whether the two instructions are of the same kind.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700744 virtual bool InstructionTypeEquals(HInstruction* other) const {
745 UNUSED(other);
746 return false;
747 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100748
749 // Returns whether any data encoded in the two instructions is equal.
750 // This method does not look at the inputs. Both instructions must be
751 // of the same type, otherwise the method has undefined behavior.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700752 virtual bool InstructionDataEquals(HInstruction* other) const {
753 UNUSED(other);
754 return false;
755 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100756
757 // Returns whether two instructions are equal, that is:
758 // 1) They have the same type and contain the same data,
759 // 2) Their inputs are identical.
760 bool Equals(HInstruction* other) const;
761
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100762 virtual InstructionKind GetKind() const = 0;
763
764 virtual size_t ComputeHashCode() const {
765 size_t result = GetKind();
766 for (size_t i = 0, e = InputCount(); i < e; ++i) {
767 result = (result * 31) + InputAt(i)->GetId();
768 }
769 return result;
770 }
771
772 SideEffects GetSideEffects() const { return side_effects_; }
773
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100774 size_t GetLifetimePosition() const { return lifetime_position_; }
775 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
776 LiveInterval* GetLiveInterval() const { return live_interval_; }
777 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
778 bool HasLiveInterval() const { return live_interval_ != nullptr; }
779
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000780 private:
781 HInstruction* previous_;
782 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000783 HBasicBlock* block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000784
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000785 // An instruction gets an id when it is added to the graph.
786 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +0100787 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000788 int id_;
789
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100790 // When doing liveness analysis, instructions that have uses get an SSA index.
791 int ssa_index_;
792
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100793 // List of instructions that have this instruction as input.
794 HUseListNode<HInstruction>* uses_;
795
796 // List of environments that contain this instruction.
797 HUseListNode<HEnvironment>* env_uses_;
798
Nicolas Geoffray39468442014-09-02 15:17:15 +0100799 // The environment associated with this instruction. Not null if the instruction
800 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100801 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000802
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000803 // Set by the code generator.
804 LocationSummary* locations_;
805
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100806 // Set by the liveness analysis.
807 LiveInterval* live_interval_;
808
809 // Set by the liveness analysis, this is the position in a linear
810 // order of blocks where this instruction's live interval start.
811 size_t lifetime_position_;
812
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100813 const SideEffects side_effects_;
814
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000815 friend class HBasicBlock;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100816 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000817
818 DISALLOW_COPY_AND_ASSIGN(HInstruction);
819};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700820std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000821
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100822template<typename T>
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000823class HUseIterator : public ValueObject {
824 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100825 explicit HUseIterator(HUseListNode<T>* uses) : current_(uses) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000826
827 bool Done() const { return current_ == nullptr; }
828
829 void Advance() {
830 DCHECK(!Done());
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000831 current_ = current_->GetTail();
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000832 }
833
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100834 HUseListNode<T>* Current() const {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000835 DCHECK(!Done());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100836 return current_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000837 }
838
839 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100840 HUseListNode<T>* current_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000841
842 friend class HValue;
843};
844
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100845// A HEnvironment object contains the values of virtual registers at a given location.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700846class HEnvironment : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100847 public:
848 HEnvironment(ArenaAllocator* arena, size_t number_of_vregs) : vregs_(arena, number_of_vregs) {
849 vregs_.SetSize(number_of_vregs);
850 for (size_t i = 0; i < number_of_vregs; i++) {
851 vregs_.Put(i, nullptr);
852 }
853 }
854
855 void Populate(const GrowableArray<HInstruction*>& env) {
856 for (size_t i = 0; i < env.Size(); i++) {
857 HInstruction* instruction = env.Get(i);
858 vregs_.Put(i, instruction);
859 if (instruction != nullptr) {
860 instruction->AddEnvUseAt(this, i);
861 }
862 }
863 }
864
865 void SetRawEnvAt(size_t index, HInstruction* instruction) {
866 vregs_.Put(index, instruction);
867 }
868
Nicolas Geoffray39468442014-09-02 15:17:15 +0100869 HInstruction* GetInstructionAt(size_t index) const {
870 return vregs_.Get(index);
871 }
872
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100873 GrowableArray<HInstruction*>* GetVRegs() {
874 return &vregs_;
875 }
876
Nicolas Geoffray39468442014-09-02 15:17:15 +0100877 size_t Size() const { return vregs_.Size(); }
878
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100879 private:
880 GrowableArray<HInstruction*> vregs_;
881
882 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
883};
884
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000885class HInputIterator : public ValueObject {
886 public:
Dave Allison20dfc792014-06-16 20:44:29 -0700887 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000888
889 bool Done() const { return index_ == instruction_->InputCount(); }
890 HInstruction* Current() const { return instruction_->InputAt(index_); }
891 void Advance() { index_++; }
892
893 private:
894 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100895 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000896
897 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
898};
899
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000900class HInstructionIterator : public ValueObject {
901 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100902 explicit HInstructionIterator(const HInstructionList& instructions)
903 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000904 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000905 }
906
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000907 bool Done() const { return instruction_ == nullptr; }
908 HInstruction* Current() const { return instruction_; }
909 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000910 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000911 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000912 }
913
914 private:
915 HInstruction* instruction_;
916 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100917
918 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000919};
920
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100921class HBackwardInstructionIterator : public ValueObject {
922 public:
923 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
924 : instruction_(instructions.last_instruction_) {
925 next_ = Done() ? nullptr : instruction_->GetPrevious();
926 }
927
928 bool Done() const { return instruction_ == nullptr; }
929 HInstruction* Current() const { return instruction_; }
930 void Advance() {
931 instruction_ = next_;
932 next_ = Done() ? nullptr : instruction_->GetPrevious();
933 }
934
935 private:
936 HInstruction* instruction_;
937 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100938
939 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100940};
941
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000942// An embedded container with N elements of type T. Used (with partial
943// specialization for N=0) because embedded arrays cannot have size 0.
944template<typename T, intptr_t N>
945class EmbeddedArray {
946 public:
Dave Allison20dfc792014-06-16 20:44:29 -0700947 EmbeddedArray() : elements_() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000948
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000949 intptr_t GetLength() const { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000950
951 const T& operator[](intptr_t i) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000952 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000953 return elements_[i];
954 }
955
956 T& operator[](intptr_t i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000957 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000958 return elements_[i];
959 }
960
961 const T& At(intptr_t i) const {
962 return (*this)[i];
963 }
964
965 void SetAt(intptr_t i, const T& val) {
966 (*this)[i] = val;
967 }
968
969 private:
970 T elements_[N];
971};
972
973template<typename T>
974class EmbeddedArray<T, 0> {
975 public:
976 intptr_t length() const { return 0; }
977 const T& operator[](intptr_t i) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700978 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000979 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700980 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000981 }
982 T& operator[](intptr_t i) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700983 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000984 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700985 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000986 }
987};
988
989template<intptr_t N>
990class HTemplateInstruction: public HInstruction {
991 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100992 HTemplateInstruction<N>(SideEffects side_effects)
993 : HInstruction(side_effects), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -0700994 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000995
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100996 virtual size_t InputCount() const { return N; }
997 virtual HInstruction* InputAt(size_t i) const { return inputs_[i]; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000998
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000999 protected:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001000 virtual void SetRawInputAt(size_t i, HInstruction* instruction) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001001 inputs_[i] = instruction;
1002 }
1003
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001004 private:
1005 EmbeddedArray<HInstruction*, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001006
1007 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001008};
1009
Dave Allison20dfc792014-06-16 20:44:29 -07001010template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001011class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07001012 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001013 HExpression<N>(Primitive::Type type, SideEffects side_effects)
1014 : HTemplateInstruction<N>(side_effects), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07001015 virtual ~HExpression() {}
1016
1017 virtual Primitive::Type GetType() const { return type_; }
1018
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001019 protected:
1020 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07001021};
1022
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001023// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
1024// instruction that branches to the exit block.
1025class HReturnVoid : public HTemplateInstruction<0> {
1026 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001027 HReturnVoid() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001028
1029 virtual bool IsControlFlow() const { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001030
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001031 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001032
1033 private:
1034 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
1035};
1036
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001037// Represents dex's RETURN opcodes. A HReturn is a control flow
1038// instruction that branches to the exit block.
1039class HReturn : public HTemplateInstruction<1> {
1040 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001041 explicit HReturn(HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001042 SetRawInputAt(0, value);
1043 }
1044
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001045 virtual bool IsControlFlow() const { return true; }
1046
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001047 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001048
1049 private:
1050 DISALLOW_COPY_AND_ASSIGN(HReturn);
1051};
1052
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001053// The exit instruction is the only instruction of the exit block.
1054// Instructions aborting the method (HTrow and HReturn) must branch to the
1055// exit block.
1056class HExit : public HTemplateInstruction<0> {
1057 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001058 HExit() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001059
1060 virtual bool IsControlFlow() const { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001061
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001062 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001063
1064 private:
1065 DISALLOW_COPY_AND_ASSIGN(HExit);
1066};
1067
1068// Jumps from one block to another.
1069class HGoto : public HTemplateInstruction<0> {
1070 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001071 HGoto() : HTemplateInstruction(SideEffects::None()) {}
1072
1073 virtual bool IsControlFlow() const { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001074
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001075 HBasicBlock* GetSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001076 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001077 }
1078
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001079 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001080
1081 private:
1082 DISALLOW_COPY_AND_ASSIGN(HGoto);
1083};
1084
Dave Allison20dfc792014-06-16 20:44:29 -07001085
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001086// Conditional branch. A block ending with an HIf instruction must have
1087// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001088class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001089 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001090 explicit HIf(HInstruction* input) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001091 SetRawInputAt(0, input);
1092 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001093
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001094 virtual bool IsControlFlow() const { return true; }
1095
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001096 HBasicBlock* IfTrueSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001097 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001098 }
1099
1100 HBasicBlock* IfFalseSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001101 return GetBlock()->GetSuccessors().Get(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001102 }
1103
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001104 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001105
Dave Allison20dfc792014-06-16 20:44:29 -07001106 virtual bool IsIfInstruction() const { return true; }
1107
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001108 private:
1109 DISALLOW_COPY_AND_ASSIGN(HIf);
1110};
1111
Roland Levillain88cb1752014-10-20 16:36:47 +01001112class HUnaryOperation : public HExpression<1> {
1113 public:
1114 HUnaryOperation(Primitive::Type result_type, HInstruction* input)
1115 : HExpression(result_type, SideEffects::None()) {
1116 SetRawInputAt(0, input);
1117 }
1118
1119 HInstruction* GetInput() const { return InputAt(0); }
1120 Primitive::Type GetResultType() const { return GetType(); }
1121
1122 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001123 virtual bool InstructionDataEquals(HInstruction* other) const {
1124 UNUSED(other);
1125 return true;
1126 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001127
Roland Levillain9240d6a2014-10-20 16:47:04 +01001128 // Try to statically evaluate `operation` and return a HConstant
1129 // containing the result of this evaluation. If `operation` cannot
1130 // be evaluated as a constant, return nullptr.
1131 HConstant* TryStaticEvaluation() const;
1132
1133 // Apply this operation to `x`.
1134 virtual int32_t Evaluate(int32_t x) const = 0;
1135 virtual int64_t Evaluate(int64_t x) const = 0;
1136
Roland Levillain88cb1752014-10-20 16:36:47 +01001137 DECLARE_INSTRUCTION(UnaryOperation);
1138
1139 private:
1140 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
1141};
1142
Dave Allison20dfc792014-06-16 20:44:29 -07001143class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001144 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001145 HBinaryOperation(Primitive::Type result_type,
1146 HInstruction* left,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001147 HInstruction* right) : HExpression(result_type, SideEffects::None()) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001148 SetRawInputAt(0, left);
1149 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001150 }
1151
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001152 HInstruction* GetLeft() const { return InputAt(0); }
1153 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07001154 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001155
1156 virtual bool IsCommutative() { return false; }
1157
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001158 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001159 virtual bool InstructionDataEquals(HInstruction* other) const {
1160 UNUSED(other);
1161 return true;
1162 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001163
Roland Levillain9240d6a2014-10-20 16:47:04 +01001164 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01001165 // containing the result of this evaluation. If `operation` cannot
1166 // be evaluated as a constant, return nullptr.
Roland Levillain9240d6a2014-10-20 16:47:04 +01001167 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01001168
1169 // Apply this operation to `x` and `y`.
1170 virtual int32_t Evaluate(int32_t x, int32_t y) const = 0;
1171 virtual int64_t Evaluate(int64_t x, int64_t y) const = 0;
1172
Roland Levillainccc07a92014-09-16 14:48:16 +01001173 DECLARE_INSTRUCTION(BinaryOperation);
1174
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001175 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001176 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
1177};
1178
Dave Allison20dfc792014-06-16 20:44:29 -07001179class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001180 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001181 HCondition(HInstruction* first, HInstruction* second)
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001182 : HBinaryOperation(Primitive::kPrimBoolean, first, second),
1183 needs_materialization_(true) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001184
1185 virtual bool IsCommutative() { return true; }
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001186
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001187 bool NeedsMaterialization() const { return needs_materialization_; }
1188 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001189
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001190 // For code generation purposes, returns whether this instruction is just before
1191 // `if_`, and disregard moves in between.
1192 bool IsBeforeWhenDisregardMoves(HIf* if_) const;
1193
Dave Allison20dfc792014-06-16 20:44:29 -07001194 DECLARE_INSTRUCTION(Condition);
1195
1196 virtual IfCondition GetCondition() const = 0;
1197
1198 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001199 // For register allocation purposes, returns whether this instruction needs to be
1200 // materialized (that is, not just be in the processor flags).
1201 bool needs_materialization_;
1202
Dave Allison20dfc792014-06-16 20:44:29 -07001203 DISALLOW_COPY_AND_ASSIGN(HCondition);
1204};
1205
1206// Instruction to check if two inputs are equal to each other.
1207class HEqual : public HCondition {
1208 public:
1209 HEqual(HInstruction* first, HInstruction* second)
1210 : HCondition(first, second) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001211
Roland Levillain93445682014-10-06 19:24:02 +01001212 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1213 return x == y ? 1 : 0;
1214 }
1215 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1216 return x == y ? 1 : 0;
1217 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001218
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001219 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001220
Dave Allison20dfc792014-06-16 20:44:29 -07001221 virtual IfCondition GetCondition() const {
1222 return kCondEQ;
1223 }
1224
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001225 private:
1226 DISALLOW_COPY_AND_ASSIGN(HEqual);
1227};
1228
Dave Allison20dfc792014-06-16 20:44:29 -07001229class HNotEqual : public HCondition {
1230 public:
1231 HNotEqual(HInstruction* first, HInstruction* second)
1232 : HCondition(first, second) {}
1233
Roland Levillain93445682014-10-06 19:24:02 +01001234 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1235 return x != y ? 1 : 0;
1236 }
1237 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1238 return x != y ? 1 : 0;
1239 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001240
Dave Allison20dfc792014-06-16 20:44:29 -07001241 DECLARE_INSTRUCTION(NotEqual);
1242
1243 virtual IfCondition GetCondition() const {
1244 return kCondNE;
1245 }
1246
1247 private:
1248 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
1249};
1250
1251class HLessThan : public HCondition {
1252 public:
1253 HLessThan(HInstruction* first, HInstruction* second)
1254 : HCondition(first, second) {}
1255
Roland Levillain93445682014-10-06 19:24:02 +01001256 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1257 return x < y ? 1 : 0;
1258 }
1259 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1260 return x < y ? 1 : 0;
1261 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001262
Dave Allison20dfc792014-06-16 20:44:29 -07001263 DECLARE_INSTRUCTION(LessThan);
1264
1265 virtual IfCondition GetCondition() const {
1266 return kCondLT;
1267 }
1268
1269 private:
1270 DISALLOW_COPY_AND_ASSIGN(HLessThan);
1271};
1272
1273class HLessThanOrEqual : public HCondition {
1274 public:
1275 HLessThanOrEqual(HInstruction* first, HInstruction* second)
1276 : HCondition(first, second) {}
1277
Roland Levillain93445682014-10-06 19:24:02 +01001278 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1279 return x <= y ? 1 : 0;
1280 }
1281 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1282 return x <= y ? 1 : 0;
1283 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001284
Dave Allison20dfc792014-06-16 20:44:29 -07001285 DECLARE_INSTRUCTION(LessThanOrEqual);
1286
1287 virtual IfCondition GetCondition() const {
1288 return kCondLE;
1289 }
1290
1291 private:
1292 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
1293};
1294
1295class HGreaterThan : public HCondition {
1296 public:
1297 HGreaterThan(HInstruction* first, HInstruction* second)
1298 : HCondition(first, second) {}
1299
Roland Levillain93445682014-10-06 19:24:02 +01001300 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1301 return x > y ? 1 : 0;
1302 }
1303 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1304 return x > y ? 1 : 0;
1305 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001306
Dave Allison20dfc792014-06-16 20:44:29 -07001307 DECLARE_INSTRUCTION(GreaterThan);
1308
1309 virtual IfCondition GetCondition() const {
1310 return kCondGT;
1311 }
1312
1313 private:
1314 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
1315};
1316
1317class HGreaterThanOrEqual : public HCondition {
1318 public:
1319 HGreaterThanOrEqual(HInstruction* first, HInstruction* second)
1320 : HCondition(first, second) {}
1321
Roland Levillain93445682014-10-06 19:24:02 +01001322 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1323 return x >= y ? 1 : 0;
1324 }
1325 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1326 return x >= y ? 1 : 0;
1327 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001328
Dave Allison20dfc792014-06-16 20:44:29 -07001329 DECLARE_INSTRUCTION(GreaterThanOrEqual);
1330
1331 virtual IfCondition GetCondition() const {
1332 return kCondGE;
1333 }
1334
1335 private:
1336 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
1337};
1338
1339
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001340// Instruction to check how two inputs compare to each other.
1341// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
1342class HCompare : public HBinaryOperation {
1343 public:
1344 HCompare(Primitive::Type type, HInstruction* first, HInstruction* second)
1345 : HBinaryOperation(Primitive::kPrimInt, first, second) {
1346 DCHECK_EQ(type, first->GetType());
1347 DCHECK_EQ(type, second->GetType());
1348 }
1349
Roland Levillain93445682014-10-06 19:24:02 +01001350 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001351 return
1352 x == y ? 0 :
1353 x > y ? 1 :
1354 -1;
1355 }
Roland Levillain93445682014-10-06 19:24:02 +01001356 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001357 return
1358 x == y ? 0 :
1359 x > y ? 1 :
1360 -1;
1361 }
1362
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001363 DECLARE_INSTRUCTION(Compare);
1364
1365 private:
1366 DISALLOW_COPY_AND_ASSIGN(HCompare);
1367};
1368
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001369// A local in the graph. Corresponds to a Dex register.
1370class HLocal : public HTemplateInstruction<0> {
1371 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001372 explicit HLocal(uint16_t reg_number)
1373 : HTemplateInstruction(SideEffects::None()), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001374
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001375 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001376
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001377 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001378
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001379 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001380 // The Dex register number.
1381 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001382
1383 DISALLOW_COPY_AND_ASSIGN(HLocal);
1384};
1385
1386// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07001387class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001388 public:
Roland Levillain5799fc02014-09-25 12:15:20 +01001389 HLoadLocal(HLocal* local, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001390 : HExpression(type, SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001391 SetRawInputAt(0, local);
1392 }
1393
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001394 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1395
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001396 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001397
1398 private:
1399 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
1400};
1401
1402// Store a value in a given local. This instruction has two inputs: the value
1403// and the local.
1404class HStoreLocal : public HTemplateInstruction<2> {
1405 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001406 HStoreLocal(HLocal* local, HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001407 SetRawInputAt(0, local);
1408 SetRawInputAt(1, value);
1409 }
1410
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001411 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1412
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001413 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001414
1415 private:
1416 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
1417};
1418
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001419class HConstant : public HExpression<0> {
1420 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001421 explicit HConstant(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
1422
1423 virtual bool CanBeMoved() const { return true; }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001424
1425 DECLARE_INSTRUCTION(Constant);
1426
1427 private:
1428 DISALLOW_COPY_AND_ASSIGN(HConstant);
1429};
1430
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001431class HFloatConstant : public HConstant {
1432 public:
1433 explicit HFloatConstant(float value) : HConstant(Primitive::kPrimFloat), value_(value) {}
1434
1435 float GetValue() const { return value_; }
1436
1437 virtual bool InstructionDataEquals(HInstruction* other) const {
1438 return bit_cast<float, int32_t>(other->AsFloatConstant()->value_) ==
1439 bit_cast<float, int32_t>(value_);
1440 }
1441
1442 virtual size_t ComputeHashCode() const { return static_cast<size_t>(GetValue()); }
1443
1444 DECLARE_INSTRUCTION(FloatConstant);
1445
1446 private:
1447 const float value_;
1448
1449 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
1450};
1451
1452class HDoubleConstant : public HConstant {
1453 public:
1454 explicit HDoubleConstant(double value) : HConstant(Primitive::kPrimDouble), value_(value) {}
1455
1456 double GetValue() const { return value_; }
1457
1458 virtual bool InstructionDataEquals(HInstruction* other) const {
1459 return bit_cast<double, int64_t>(other->AsDoubleConstant()->value_) ==
1460 bit_cast<double, int64_t>(value_);
1461 }
1462
1463 virtual size_t ComputeHashCode() const { return static_cast<size_t>(GetValue()); }
1464
1465 DECLARE_INSTRUCTION(DoubleConstant);
1466
1467 private:
1468 const double value_;
1469
1470 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
1471};
1472
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001473// Constants of the type int. Those can be from Dex instructions, or
1474// synthesized (for example with the if-eqz instruction).
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001475class HIntConstant : public HConstant {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001476 public:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001477 explicit HIntConstant(int32_t value) : HConstant(Primitive::kPrimInt), value_(value) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001478
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001479 int32_t GetValue() const { return value_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001480
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001481 virtual bool InstructionDataEquals(HInstruction* other) const {
1482 return other->AsIntConstant()->value_ == value_;
1483 }
1484
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001485 virtual size_t ComputeHashCode() const { return GetValue(); }
1486
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001487 DECLARE_INSTRUCTION(IntConstant);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001488
1489 private:
1490 const int32_t value_;
1491
1492 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
1493};
1494
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001495class HLongConstant : public HConstant {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001496 public:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001497 explicit HLongConstant(int64_t value) : HConstant(Primitive::kPrimLong), value_(value) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001498
1499 int64_t GetValue() const { return value_; }
1500
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001501 virtual bool InstructionDataEquals(HInstruction* other) const {
1502 return other->AsLongConstant()->value_ == value_;
1503 }
1504
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001505 virtual size_t ComputeHashCode() const { return static_cast<size_t>(GetValue()); }
1506
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001507 DECLARE_INSTRUCTION(LongConstant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001508
1509 private:
1510 const int64_t value_;
1511
1512 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
1513};
1514
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001515class HInvoke : public HInstruction {
1516 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001517 HInvoke(ArenaAllocator* arena,
1518 uint32_t number_of_arguments,
1519 Primitive::Type return_type,
1520 uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001521 : HInstruction(SideEffects::All()),
1522 inputs_(arena, number_of_arguments),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001523 return_type_(return_type),
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001524 dex_pc_(dex_pc) {
1525 inputs_.SetSize(number_of_arguments);
1526 }
1527
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001528 virtual size_t InputCount() const { return inputs_.Size(); }
1529 virtual HInstruction* InputAt(size_t i) const { return inputs_.Get(i); }
1530
1531 // Runtime needs to walk the stack, so Dex -> Dex calls need to
1532 // know their environment.
1533 virtual bool NeedsEnvironment() const { return true; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001534
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001535 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001536 SetRawInputAt(index, argument);
1537 }
1538
1539 virtual void SetRawInputAt(size_t index, HInstruction* input) {
1540 inputs_.Put(index, input);
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001541 }
1542
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001543 virtual Primitive::Type GetType() const { return return_type_; }
1544
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001545 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001546
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001547 DECLARE_INSTRUCTION(Invoke);
1548
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001549 protected:
1550 GrowableArray<HInstruction*> inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001551 const Primitive::Type return_type_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001552 const uint32_t dex_pc_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001553
1554 private:
1555 DISALLOW_COPY_AND_ASSIGN(HInvoke);
1556};
1557
1558class HInvokeStatic : public HInvoke {
1559 public:
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001560 HInvokeStatic(ArenaAllocator* arena,
1561 uint32_t number_of_arguments,
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001562 Primitive::Type return_type,
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001563 uint32_t dex_pc,
1564 uint32_t index_in_dex_cache)
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001565 : HInvoke(arena, number_of_arguments, return_type, dex_pc),
1566 index_in_dex_cache_(index_in_dex_cache) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001567
1568 uint32_t GetIndexInDexCache() const { return index_in_dex_cache_; }
1569
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001570 DECLARE_INSTRUCTION(InvokeStatic);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001571
1572 private:
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001573 const uint32_t index_in_dex_cache_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001574
1575 DISALLOW_COPY_AND_ASSIGN(HInvokeStatic);
1576};
1577
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001578class HInvokeVirtual : public HInvoke {
1579 public:
1580 HInvokeVirtual(ArenaAllocator* arena,
1581 uint32_t number_of_arguments,
1582 Primitive::Type return_type,
1583 uint32_t dex_pc,
1584 uint32_t vtable_index)
1585 : HInvoke(arena, number_of_arguments, return_type, dex_pc),
1586 vtable_index_(vtable_index) {}
1587
1588 uint32_t GetVTableIndex() const { return vtable_index_; }
1589
1590 DECLARE_INSTRUCTION(InvokeVirtual);
1591
1592 private:
1593 const uint32_t vtable_index_;
1594
1595 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
1596};
1597
Dave Allison20dfc792014-06-16 20:44:29 -07001598class HNewInstance : public HExpression<0> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001599 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001600 HNewInstance(uint32_t dex_pc, uint16_t type_index)
1601 : HExpression(Primitive::kPrimNot, SideEffects::None()),
1602 dex_pc_(dex_pc),
1603 type_index_(type_index) {}
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001604
1605 uint32_t GetDexPc() const { return dex_pc_; }
1606 uint16_t GetTypeIndex() const { return type_index_; }
1607
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001608 // Calls runtime so needs an environment.
1609 virtual bool NeedsEnvironment() const { return true; }
1610
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001611 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001612
1613 private:
1614 const uint32_t dex_pc_;
1615 const uint16_t type_index_;
1616
1617 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
1618};
1619
Roland Levillain88cb1752014-10-20 16:36:47 +01001620class HNeg : public HUnaryOperation {
1621 public:
1622 explicit HNeg(Primitive::Type result_type, HInstruction* input)
1623 : HUnaryOperation(result_type, input) {}
1624
Roland Levillain9240d6a2014-10-20 16:47:04 +01001625 virtual int32_t Evaluate(int32_t x) const OVERRIDE { return -x; }
1626 virtual int64_t Evaluate(int64_t x) const OVERRIDE { return -x; }
1627
Roland Levillain88cb1752014-10-20 16:36:47 +01001628 DECLARE_INSTRUCTION(Neg);
1629
1630 private:
1631 DISALLOW_COPY_AND_ASSIGN(HNeg);
1632};
1633
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001634class HNewArray : public HExpression<1> {
1635 public:
1636 HNewArray(HInstruction* length, uint32_t dex_pc, uint16_t type_index)
1637 : HExpression(Primitive::kPrimNot, SideEffects::None()),
1638 dex_pc_(dex_pc),
1639 type_index_(type_index) {
1640 SetRawInputAt(0, length);
1641 }
1642
1643 uint32_t GetDexPc() const { return dex_pc_; }
1644 uint16_t GetTypeIndex() const { return type_index_; }
1645
1646 // Calls runtime so needs an environment.
1647 virtual bool NeedsEnvironment() const { return true; }
1648
1649 DECLARE_INSTRUCTION(NewArray);
1650
1651 private:
1652 const uint32_t dex_pc_;
1653 const uint16_t type_index_;
1654
1655 DISALLOW_COPY_AND_ASSIGN(HNewArray);
1656};
1657
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001658class HAdd : public HBinaryOperation {
1659 public:
1660 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1661 : HBinaryOperation(result_type, left, right) {}
1662
1663 virtual bool IsCommutative() { return true; }
1664
Roland Levillain93445682014-10-06 19:24:02 +01001665 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1666 return x + y;
1667 }
1668 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1669 return x + y;
1670 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001671
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001672 DECLARE_INSTRUCTION(Add);
1673
1674 private:
1675 DISALLOW_COPY_AND_ASSIGN(HAdd);
1676};
1677
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001678class HSub : public HBinaryOperation {
1679 public:
1680 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1681 : HBinaryOperation(result_type, left, right) {}
1682
Roland Levillain93445682014-10-06 19:24:02 +01001683 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1684 return x - y;
1685 }
1686 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1687 return x - y;
1688 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001689
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001690 DECLARE_INSTRUCTION(Sub);
1691
1692 private:
1693 DISALLOW_COPY_AND_ASSIGN(HSub);
1694};
1695
Calin Juravle34bacdf2014-10-07 20:23:36 +01001696class HMul : public HBinaryOperation {
1697 public:
1698 HMul(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1699 : HBinaryOperation(result_type, left, right) {}
1700
1701 virtual bool IsCommutative() { return true; }
1702
1703 virtual int32_t Evaluate(int32_t x, int32_t y) const { return x * y; }
1704 virtual int64_t Evaluate(int64_t x, int64_t y) const { return x * y; }
1705
1706 DECLARE_INSTRUCTION(Mul);
1707
1708 private:
1709 DISALLOW_COPY_AND_ASSIGN(HMul);
1710};
1711
Calin Juravle7c4954d2014-10-28 16:57:40 +00001712class HDiv : public HBinaryOperation {
1713 public:
1714 HDiv(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1715 : HBinaryOperation(result_type, left, right) {}
1716
1717 virtual int32_t Evaluate(int32_t x, int32_t y) const { return x / y; }
1718 virtual int64_t Evaluate(int64_t x, int64_t y) const { return x / y; }
1719
1720 DECLARE_INSTRUCTION(Div);
1721
1722 private:
1723 DISALLOW_COPY_AND_ASSIGN(HDiv);
1724};
1725
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001726// The value of a parameter in this method. Its location depends on
1727// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07001728class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001729 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001730 HParameterValue(uint8_t index, Primitive::Type parameter_type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001731 : HExpression(parameter_type, SideEffects::None()), index_(index) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001732
1733 uint8_t GetIndex() const { return index_; }
1734
1735 DECLARE_INSTRUCTION(ParameterValue);
1736
1737 private:
1738 // The index of this parameter in the parameters list. Must be less
1739 // than HGraph::number_of_in_vregs_;
1740 const uint8_t index_;
1741
1742 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
1743};
1744
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001745class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001746 public:
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001747 explicit HNot(Primitive::Type result_type, HInstruction* input)
1748 : HUnaryOperation(result_type, input) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001749
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001750 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001751 virtual bool InstructionDataEquals(HInstruction* other) const {
1752 UNUSED(other);
1753 return true;
1754 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001755
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001756 virtual int32_t Evaluate(int32_t x) const OVERRIDE { return ~x; }
1757 virtual int64_t Evaluate(int64_t x) const OVERRIDE { return ~x; }
1758
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001759 DECLARE_INSTRUCTION(Not);
1760
1761 private:
1762 DISALLOW_COPY_AND_ASSIGN(HNot);
1763};
1764
Roland Levillaindff1f282014-11-05 14:15:05 +00001765class HTypeConversion : public HExpression<1> {
1766 public:
1767 // Instantiate a type conversion of `input` to `result_type`.
1768 HTypeConversion(Primitive::Type result_type, HInstruction* input)
1769 : HExpression(result_type, SideEffects::None()) {
1770 SetRawInputAt(0, input);
1771 DCHECK_NE(input->GetType(), result_type);
1772 }
1773
1774 HInstruction* GetInput() const { return InputAt(0); }
1775 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
1776 Primitive::Type GetResultType() const { return GetType(); }
1777
1778 bool CanBeMoved() const OVERRIDE { return true; }
1779 bool InstructionDataEquals(HInstruction* /* other */) const OVERRIDE { return true; }
1780
1781 DECLARE_INSTRUCTION(TypeConversion);
1782
1783 private:
1784 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
1785};
1786
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001787class HPhi : public HInstruction {
1788 public:
1789 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001790 : HInstruction(SideEffects::None()),
1791 inputs_(arena, number_of_inputs),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001792 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001793 type_(type),
1794 is_live_(false) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001795 inputs_.SetSize(number_of_inputs);
1796 }
1797
1798 virtual size_t InputCount() const { return inputs_.Size(); }
1799 virtual HInstruction* InputAt(size_t i) const { return inputs_.Get(i); }
1800
1801 virtual void SetRawInputAt(size_t index, HInstruction* input) {
1802 inputs_.Put(index, input);
1803 }
1804
1805 void AddInput(HInstruction* input);
1806
1807 virtual Primitive::Type GetType() const { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001808 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001809
1810 uint32_t GetRegNumber() const { return reg_number_; }
1811
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001812 void SetDead() { is_live_ = false; }
1813 void SetLive() { is_live_ = true; }
1814 bool IsDead() const { return !is_live_; }
1815 bool IsLive() const { return is_live_; }
1816
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001817 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001818
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001819 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001820 GrowableArray<HInstruction*> inputs_;
1821 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001822 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001823 bool is_live_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001824
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001825 DISALLOW_COPY_AND_ASSIGN(HPhi);
1826};
1827
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001828class HNullCheck : public HExpression<1> {
1829 public:
1830 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001831 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001832 SetRawInputAt(0, value);
1833 }
1834
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001835 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001836 virtual bool InstructionDataEquals(HInstruction* other) const {
1837 UNUSED(other);
1838 return true;
1839 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001840
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001841 virtual bool NeedsEnvironment() const { return true; }
1842
Roland Levillaine161a2a2014-10-03 12:45:18 +01001843 virtual bool CanThrow() const { return true; }
1844
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001845 uint32_t GetDexPc() const { return dex_pc_; }
1846
1847 DECLARE_INSTRUCTION(NullCheck);
1848
1849 private:
1850 const uint32_t dex_pc_;
1851
1852 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
1853};
1854
1855class FieldInfo : public ValueObject {
1856 public:
Roland Levillain5799fc02014-09-25 12:15:20 +01001857 FieldInfo(MemberOffset field_offset, Primitive::Type field_type)
Nicolas Geoffray39468442014-09-02 15:17:15 +01001858 : field_offset_(field_offset), field_type_(field_type) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001859
1860 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001861 Primitive::Type GetFieldType() const { return field_type_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001862
1863 private:
1864 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01001865 const Primitive::Type field_type_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001866};
1867
1868class HInstanceFieldGet : public HExpression<1> {
1869 public:
1870 HInstanceFieldGet(HInstruction* value,
1871 Primitive::Type field_type,
1872 MemberOffset field_offset)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001873 : HExpression(field_type, SideEffects::DependsOnSomething()),
1874 field_info_(field_offset, field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001875 SetRawInputAt(0, value);
1876 }
1877
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001878 virtual bool CanBeMoved() const { return true; }
1879 virtual bool InstructionDataEquals(HInstruction* other) const {
1880 size_t other_offset = other->AsInstanceFieldGet()->GetFieldOffset().SizeValue();
1881 return other_offset == GetFieldOffset().SizeValue();
1882 }
1883
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001884 virtual size_t ComputeHashCode() const {
1885 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
1886 }
1887
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001888 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001889 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001890
1891 DECLARE_INSTRUCTION(InstanceFieldGet);
1892
1893 private:
1894 const FieldInfo field_info_;
1895
1896 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
1897};
1898
1899class HInstanceFieldSet : public HTemplateInstruction<2> {
1900 public:
1901 HInstanceFieldSet(HInstruction* object,
1902 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01001903 Primitive::Type field_type,
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001904 MemberOffset field_offset)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001905 : HTemplateInstruction(SideEffects::ChangesSomething()),
1906 field_info_(field_offset, field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001907 SetRawInputAt(0, object);
1908 SetRawInputAt(1, value);
1909 }
1910
1911 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001912 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001913
1914 DECLARE_INSTRUCTION(InstanceFieldSet);
1915
1916 private:
1917 const FieldInfo field_info_;
1918
1919 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
1920};
1921
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001922class HArrayGet : public HExpression<2> {
1923 public:
1924 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001925 : HExpression(type, SideEffects::DependsOnSomething()) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001926 SetRawInputAt(0, array);
1927 SetRawInputAt(1, index);
1928 }
1929
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001930 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001931 virtual bool InstructionDataEquals(HInstruction* other) const {
1932 UNUSED(other);
1933 return true;
1934 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001935 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001936
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001937 DECLARE_INSTRUCTION(ArrayGet);
1938
1939 private:
1940 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
1941};
1942
1943class HArraySet : public HTemplateInstruction<3> {
1944 public:
1945 HArraySet(HInstruction* array,
1946 HInstruction* index,
1947 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001948 Primitive::Type expected_component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001949 uint32_t dex_pc)
1950 : HTemplateInstruction(SideEffects::ChangesSomething()),
1951 dex_pc_(dex_pc),
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001952 expected_component_type_(expected_component_type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001953 SetRawInputAt(0, array);
1954 SetRawInputAt(1, index);
1955 SetRawInputAt(2, value);
1956 }
1957
1958 virtual bool NeedsEnvironment() const {
1959 // We currently always call a runtime method to catch array store
1960 // exceptions.
1961 return InputAt(2)->GetType() == Primitive::kPrimNot;
1962 }
1963
1964 uint32_t GetDexPc() const { return dex_pc_; }
1965
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001966 HInstruction* GetValue() const { return InputAt(2); }
1967
1968 Primitive::Type GetComponentType() const {
1969 // The Dex format does not type floating point index operations. Since the
1970 // `expected_component_type_` is set during building and can therefore not
1971 // be correct, we also check what is the value type. If it is a floating
1972 // point type, we must use that type.
1973 Primitive::Type value_type = GetValue()->GetType();
1974 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
1975 ? value_type
1976 : expected_component_type_;
1977 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001978
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001979 DECLARE_INSTRUCTION(ArraySet);
1980
1981 private:
1982 const uint32_t dex_pc_;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001983 const Primitive::Type expected_component_type_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001984
1985 DISALLOW_COPY_AND_ASSIGN(HArraySet);
1986};
1987
1988class HArrayLength : public HExpression<1> {
1989 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001990 explicit HArrayLength(HInstruction* array)
1991 : HExpression(Primitive::kPrimInt, SideEffects::None()) {
1992 // Note that arrays do not change length, so the instruction does not
1993 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001994 SetRawInputAt(0, array);
1995 }
1996
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001997 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001998 virtual bool InstructionDataEquals(HInstruction* other) const {
1999 UNUSED(other);
2000 return true;
2001 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002002
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002003 DECLARE_INSTRUCTION(ArrayLength);
2004
2005 private:
2006 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
2007};
2008
2009class HBoundsCheck : public HExpression<2> {
2010 public:
2011 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002012 : HExpression(index->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002013 DCHECK(index->GetType() == Primitive::kPrimInt);
2014 SetRawInputAt(0, index);
2015 SetRawInputAt(1, length);
2016 }
2017
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002018 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002019 virtual bool InstructionDataEquals(HInstruction* other) const {
2020 UNUSED(other);
2021 return true;
2022 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002023
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002024 virtual bool NeedsEnvironment() const { return true; }
2025
Roland Levillaine161a2a2014-10-03 12:45:18 +01002026 virtual bool CanThrow() const { return true; }
2027
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002028 uint32_t GetDexPc() const { return dex_pc_; }
2029
2030 DECLARE_INSTRUCTION(BoundsCheck);
2031
2032 private:
2033 const uint32_t dex_pc_;
2034
2035 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
2036};
2037
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002038/**
2039 * Some DEX instructions are folded into multiple HInstructions that need
2040 * to stay live until the last HInstruction. This class
2041 * is used as a marker for the baseline compiler to ensure its preceding
2042 * HInstruction stays live. `index` is the temporary number that is used
2043 * for knowing the stack offset where to store the instruction.
2044 */
2045class HTemporary : public HTemplateInstruction<0> {
2046 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002047 explicit HTemporary(size_t index) : HTemplateInstruction(SideEffects::None()), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002048
2049 size_t GetIndex() const { return index_; }
2050
2051 DECLARE_INSTRUCTION(Temporary);
2052
2053 private:
2054 const size_t index_;
2055
2056 DISALLOW_COPY_AND_ASSIGN(HTemporary);
2057};
2058
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002059class HSuspendCheck : public HTemplateInstruction<0> {
2060 public:
2061 explicit HSuspendCheck(uint32_t dex_pc)
Nicolas Geoffray9ebc72c2014-09-25 16:33:42 +01002062 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002063
2064 virtual bool NeedsEnvironment() const {
2065 return true;
2066 }
2067
2068 uint32_t GetDexPc() const { return dex_pc_; }
2069
2070 DECLARE_INSTRUCTION(SuspendCheck);
2071
2072 private:
2073 const uint32_t dex_pc_;
2074
2075 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
2076};
2077
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002078/**
2079 * Instruction to load a Class object.
2080 */
2081class HLoadClass : public HExpression<0> {
2082 public:
2083 HLoadClass(uint16_t type_index,
2084 bool is_referrers_class,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002085 uint32_t dex_pc)
2086 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2087 type_index_(type_index),
2088 is_referrers_class_(is_referrers_class),
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002089 dex_pc_(dex_pc),
2090 generate_clinit_check_(false) {}
2091
2092 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002093
2094 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2095 return other->AsLoadClass()->type_index_ == type_index_;
2096 }
2097
2098 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
2099
2100 uint32_t GetDexPc() const { return dex_pc_; }
2101 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002102 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002103
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002104 bool NeedsEnvironment() const OVERRIDE {
2105 // Will call runtime and load the class if the class is not loaded yet.
2106 // TODO: finer grain decision.
2107 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002108 }
2109
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002110 bool MustGenerateClinitCheck() const {
2111 return generate_clinit_check_;
2112 }
2113
2114 void SetMustGenerateClinitCheck() {
2115 generate_clinit_check_ = true;
2116 }
2117
2118 bool CanCallRuntime() const {
2119 return MustGenerateClinitCheck() || !is_referrers_class_;
2120 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002121
2122 DECLARE_INSTRUCTION(LoadClass);
2123
2124 private:
2125 const uint16_t type_index_;
2126 const bool is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002127 const uint32_t dex_pc_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002128 // Whether this instruction must generate the initialization check.
2129 // Used for code generation.
2130 bool generate_clinit_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002131
2132 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
2133};
2134
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002135class HLoadString : public HExpression<0> {
2136 public:
2137 HLoadString(uint32_t string_index, uint32_t dex_pc)
2138 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2139 string_index_(string_index),
2140 dex_pc_(dex_pc) {}
2141
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002142 bool CanBeMoved() const OVERRIDE { return true; }
2143
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002144 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2145 return other->AsLoadString()->string_index_ == string_index_;
2146 }
2147
2148 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
2149
2150 uint32_t GetDexPc() const { return dex_pc_; }
2151 uint32_t GetStringIndex() const { return string_index_; }
2152
2153 // TODO: Can we deopt or debug when we resolve a string?
2154 bool NeedsEnvironment() const OVERRIDE { return false; }
2155
2156 DECLARE_INSTRUCTION(LoadString);
2157
2158 private:
2159 const uint32_t string_index_;
2160 const uint32_t dex_pc_;
2161
2162 DISALLOW_COPY_AND_ASSIGN(HLoadString);
2163};
2164
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002165// TODO: Pass this check to HInvokeStatic nodes.
2166/**
2167 * Performs an initialization check on its Class object input.
2168 */
2169class HClinitCheck : public HExpression<1> {
2170 public:
2171 explicit HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
2172 : HExpression(Primitive::kPrimNot, SideEffects::All()),
2173 dex_pc_(dex_pc) {
2174 SetRawInputAt(0, constant);
2175 }
2176
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002177 bool CanBeMoved() const OVERRIDE { return true; }
2178 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2179 UNUSED(other);
2180 return true;
2181 }
2182
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002183 bool NeedsEnvironment() const OVERRIDE {
2184 // May call runtime to initialize the class.
2185 return true;
2186 }
2187
2188 uint32_t GetDexPc() const { return dex_pc_; }
2189
2190 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
2191
2192 DECLARE_INSTRUCTION(ClinitCheck);
2193
2194 private:
2195 const uint32_t dex_pc_;
2196
2197 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
2198};
2199
2200class HStaticFieldGet : public HExpression<1> {
2201 public:
2202 HStaticFieldGet(HInstruction* cls,
2203 Primitive::Type field_type,
2204 MemberOffset field_offset)
2205 : HExpression(field_type, SideEffects::DependsOnSomething()),
2206 field_info_(field_offset, field_type) {
2207 SetRawInputAt(0, cls);
2208 }
2209
2210 bool CanBeMoved() const OVERRIDE { return true; }
2211 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2212 size_t other_offset = other->AsStaticFieldGet()->GetFieldOffset().SizeValue();
2213 return other_offset == GetFieldOffset().SizeValue();
2214 }
2215
2216 size_t ComputeHashCode() const OVERRIDE {
2217 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
2218 }
2219
2220 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
2221 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
2222
2223 DECLARE_INSTRUCTION(StaticFieldGet);
2224
2225 private:
2226 const FieldInfo field_info_;
2227
2228 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
2229};
2230
2231class HStaticFieldSet : public HTemplateInstruction<2> {
2232 public:
2233 HStaticFieldSet(HInstruction* cls,
2234 HInstruction* value,
2235 Primitive::Type field_type,
2236 MemberOffset field_offset)
2237 : HTemplateInstruction(SideEffects::ChangesSomething()),
2238 field_info_(field_offset, field_type) {
2239 SetRawInputAt(0, cls);
2240 SetRawInputAt(1, value);
2241 }
2242
2243 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
2244 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
2245
2246 DECLARE_INSTRUCTION(StaticFieldSet);
2247
2248 private:
2249 const FieldInfo field_info_;
2250
2251 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
2252};
2253
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002254class MoveOperands : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002255 public:
Nicolas Geoffray740475d2014-09-29 10:33:25 +01002256 MoveOperands(Location source, Location destination, HInstruction* instruction)
2257 : source_(source), destination_(destination), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002258
2259 Location GetSource() const { return source_; }
2260 Location GetDestination() const { return destination_; }
2261
2262 void SetSource(Location value) { source_ = value; }
2263 void SetDestination(Location value) { destination_ = value; }
2264
2265 // The parallel move resolver marks moves as "in-progress" by clearing the
2266 // destination (but not the source).
2267 Location MarkPending() {
2268 DCHECK(!IsPending());
2269 Location dest = destination_;
2270 destination_ = Location::NoLocation();
2271 return dest;
2272 }
2273
2274 void ClearPending(Location dest) {
2275 DCHECK(IsPending());
2276 destination_ = dest;
2277 }
2278
2279 bool IsPending() const {
2280 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
2281 return destination_.IsInvalid() && !source_.IsInvalid();
2282 }
2283
2284 // True if this blocks a move from the given location.
2285 bool Blocks(Location loc) const {
2286 return !IsEliminated() && source_.Equals(loc);
2287 }
2288
2289 // A move is redundant if it's been eliminated, if its source and
2290 // destination are the same, or if its destination is unneeded.
2291 bool IsRedundant() const {
2292 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
2293 }
2294
2295 // We clear both operands to indicate move that's been eliminated.
2296 void Eliminate() {
2297 source_ = destination_ = Location::NoLocation();
2298 }
2299
2300 bool IsEliminated() const {
2301 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
2302 return source_.IsInvalid();
2303 }
2304
Nicolas Geoffray740475d2014-09-29 10:33:25 +01002305 HInstruction* GetInstruction() const { return instruction_; }
2306
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002307 private:
2308 Location source_;
2309 Location destination_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01002310 // The instruction this move is assocatied with. Null when this move is
2311 // for moving an input in the expected locations of user (including a phi user).
2312 // This is only used in debug mode, to ensure we do not connect interval siblings
2313 // in the same parallel move.
2314 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002315
2316 DISALLOW_COPY_AND_ASSIGN(MoveOperands);
2317};
2318
2319static constexpr size_t kDefaultNumberOfMoves = 4;
2320
2321class HParallelMove : public HTemplateInstruction<0> {
2322 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002323 explicit HParallelMove(ArenaAllocator* arena)
2324 : HTemplateInstruction(SideEffects::None()), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002325
2326 void AddMove(MoveOperands* move) {
Nicolas Geoffray740475d2014-09-29 10:33:25 +01002327 if (kIsDebugBuild && move->GetInstruction() != nullptr) {
2328 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
2329 DCHECK_NE(moves_.Get(i)->GetInstruction(), move->GetInstruction())
2330 << "Doing parallel moves for the same instruction.";
2331 }
2332 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002333 moves_.Add(move);
2334 }
2335
2336 MoveOperands* MoveOperandsAt(size_t index) const {
2337 return moves_.Get(index);
2338 }
2339
2340 size_t NumMoves() const { return moves_.Size(); }
2341
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002342 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002343
2344 private:
2345 GrowableArray<MoveOperands*> moves_;
2346
2347 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
2348};
2349
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002350class HGraphVisitor : public ValueObject {
2351 public:
Dave Allison20dfc792014-06-16 20:44:29 -07002352 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
2353 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002354
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002355 virtual void VisitInstruction(HInstruction* instruction) { UNUSED(instruction); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002356 virtual void VisitBasicBlock(HBasicBlock* block);
2357
Roland Levillain633021e2014-10-01 14:12:25 +01002358 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002359 void VisitInsertionOrder();
2360
Roland Levillain633021e2014-10-01 14:12:25 +01002361 // Visit the graph following dominator tree reverse post-order.
2362 void VisitReversePostOrder();
2363
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002364 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002365
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002366 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002367#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002368 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
2369
2370 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
2371
2372#undef DECLARE_VISIT_INSTRUCTION
2373
2374 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07002375 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002376
2377 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
2378};
2379
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002380class HGraphDelegateVisitor : public HGraphVisitor {
2381 public:
2382 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
2383 virtual ~HGraphDelegateVisitor() {}
2384
2385 // Visit functions that delegate to to super class.
2386#define DECLARE_VISIT_INSTRUCTION(name, super) \
2387 virtual void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
2388
2389 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
2390
2391#undef DECLARE_VISIT_INSTRUCTION
2392
2393 private:
2394 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
2395};
2396
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002397class HInsertionOrderIterator : public ValueObject {
2398 public:
2399 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
2400
2401 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
2402 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
2403 void Advance() { ++index_; }
2404
2405 private:
2406 const HGraph& graph_;
2407 size_t index_;
2408
2409 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
2410};
2411
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002412class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002413 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002414 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002415
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002416 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
2417 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002418 void Advance() { ++index_; }
2419
2420 private:
2421 const HGraph& graph_;
2422 size_t index_;
2423
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002424 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002425};
2426
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002427class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002428 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002429 explicit HPostOrderIterator(const HGraph& graph)
2430 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002431
2432 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002433 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002434 void Advance() { --index_; }
2435
2436 private:
2437 const HGraph& graph_;
2438 size_t index_;
2439
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002440 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002441};
2442
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002443} // namespace art
2444
2445#endif // ART_COMPILER_OPTIMIZING_NODES_H_