blob: 7549ebfbe42da4d06aad370550ee6b71e3fe473f [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) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000519
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100520#define FOR_EACH_INSTRUCTION(M) \
521 FOR_EACH_CONCRETE_INSTRUCTION(M) \
522 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +0100523 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +0100524 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100525 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -0700526
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100527#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000528FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
529#undef FORWARD_DECLARATION
530
Roland Levillainccc07a92014-09-16 14:48:16 +0100531#define DECLARE_INSTRUCTION(type) \
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100532 virtual InstructionKind GetKind() const { return k##type; } \
Roland Levillainccc07a92014-09-16 14:48:16 +0100533 virtual const char* DebugName() const { return #type; } \
534 virtual const H##type* As##type() const OVERRIDE { return this; } \
535 virtual H##type* As##type() OVERRIDE { return this; } \
536 virtual bool InstructionTypeEquals(HInstruction* other) const { \
537 return other->Is##type(); \
538 } \
539 virtual void Accept(HGraphVisitor* visitor)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000540
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100541template <typename T>
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700542class HUseListNode : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000543 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100544 HUseListNode(T* user, size_t index, HUseListNode* tail)
Dave Allison20dfc792014-06-16 20:44:29 -0700545 : user_(user), index_(index), tail_(tail) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000546
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000547 HUseListNode* GetTail() const { return tail_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100548 T* GetUser() const { return user_; }
549 size_t GetIndex() const { return index_; }
550
551 void SetTail(HUseListNode<T>* node) { tail_ = node; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000552
553 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100554 T* const user_;
555 const size_t index_;
556 HUseListNode<T>* tail_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000557
558 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
559};
560
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100561// Represents the side effects an instruction may have.
562class SideEffects : public ValueObject {
563 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100564 SideEffects() : flags_(0) {}
565
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100566 static SideEffects None() {
567 return SideEffects(0);
568 }
569
570 static SideEffects All() {
571 return SideEffects(ChangesSomething().flags_ | DependsOnSomething().flags_);
572 }
573
574 static SideEffects ChangesSomething() {
575 return SideEffects((1 << kFlagChangesCount) - 1);
576 }
577
578 static SideEffects DependsOnSomething() {
579 int count = kFlagDependsOnCount - kFlagChangesCount;
580 return SideEffects(((1 << count) - 1) << kFlagChangesCount);
581 }
582
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100583 SideEffects Union(SideEffects other) const {
584 return SideEffects(flags_ | other.flags_);
585 }
586
Roland Levillain72bceff2014-09-15 18:29:00 +0100587 bool HasSideEffects() const {
588 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
589 return (flags_ & all_bits_set) != 0;
590 }
591
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100592 bool HasAllSideEffects() const {
593 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
594 return all_bits_set == (flags_ & all_bits_set);
595 }
596
597 bool DependsOn(SideEffects other) const {
598 size_t depends_flags = other.ComputeDependsFlags();
599 return (flags_ & depends_flags) != 0;
600 }
601
602 bool HasDependencies() const {
603 int count = kFlagDependsOnCount - kFlagChangesCount;
604 size_t all_bits_set = (1 << count) - 1;
605 return ((flags_ >> kFlagChangesCount) & all_bits_set) != 0;
606 }
607
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100608 private:
609 static constexpr int kFlagChangesSomething = 0;
610 static constexpr int kFlagChangesCount = kFlagChangesSomething + 1;
611
612 static constexpr int kFlagDependsOnSomething = kFlagChangesCount;
613 static constexpr int kFlagDependsOnCount = kFlagDependsOnSomething + 1;
614
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100615 explicit SideEffects(size_t flags) : flags_(flags) {}
616
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100617 size_t ComputeDependsFlags() const {
618 return flags_ << kFlagChangesCount;
619 }
620
621 size_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100622};
623
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700624class HInstruction : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000625 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100626 explicit HInstruction(SideEffects side_effects)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000627 : previous_(nullptr),
628 next_(nullptr),
629 block_(nullptr),
630 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100631 ssa_index_(-1),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000632 uses_(nullptr),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100633 env_uses_(nullptr),
634 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100635 locations_(nullptr),
636 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100637 lifetime_position_(kNoLifetime),
638 side_effects_(side_effects) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000639
Dave Allison20dfc792014-06-16 20:44:29 -0700640 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000641
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100642#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100643 enum InstructionKind {
644 FOR_EACH_INSTRUCTION(DECLARE_KIND)
645 };
646#undef DECLARE_KIND
647
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000648 HInstruction* GetNext() const { return next_; }
649 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000650
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000651 HBasicBlock* GetBlock() const { return block_; }
652 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100653 bool IsInBlock() const { return block_ != nullptr; }
654 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +0100655 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000656
Roland Levillain6b879dd2014-09-22 17:13:44 +0100657 virtual size_t InputCount() const = 0;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100658 virtual HInstruction* InputAt(size_t i) const = 0;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000659
660 virtual void Accept(HGraphVisitor* visitor) = 0;
661 virtual const char* DebugName() const = 0;
662
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100663 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100664 virtual void SetRawInputAt(size_t index, HInstruction* input) = 0;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100665
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100666 virtual bool NeedsEnvironment() const { return false; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100667 virtual bool IsControlFlow() const { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +0100668 virtual bool CanThrow() const { return false; }
Roland Levillain72bceff2014-09-15 18:29:00 +0100669 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100670
671 void AddUseAt(HInstruction* user, size_t index) {
672 uses_ = new (block_->GetGraph()->GetArena()) HUseListNode<HInstruction>(user, index, uses_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000673 }
674
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100675 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +0100676 DCHECK(user != nullptr);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100677 env_uses_ = new (block_->GetGraph()->GetArena()) HUseListNode<HEnvironment>(
678 user, index, env_uses_);
679 }
680
681 void RemoveUser(HInstruction* user, size_t index);
Nicolas Geoffray724c9632014-09-22 12:27:27 +0100682 void RemoveEnvironmentUser(HEnvironment* user, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100683
684 HUseListNode<HInstruction>* GetUses() const { return uses_; }
685 HUseListNode<HEnvironment>* GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000686
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100687 bool HasUses() const { return uses_ != nullptr || env_uses_ != nullptr; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100688 bool HasEnvironmentUses() const { return env_uses_ != nullptr; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000689
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100690 size_t NumberOfUses() const {
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100691 // TODO: Optimize this method if it is used outside of the HGraphVisualizer.
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100692 size_t result = 0;
693 HUseListNode<HInstruction>* current = uses_;
694 while (current != nullptr) {
695 current = current->GetTail();
696 ++result;
697 }
698 return result;
699 }
700
Roland Levillain6c82d402014-10-13 16:10:27 +0100701 // Does this instruction strictly dominate `other_instruction`?
702 // Returns false if this instruction and `other_instruction` are the same.
703 // Aborts if this instruction and `other_instruction` are both phis.
704 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +0100705
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000706 int GetId() const { return id_; }
707 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000708
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100709 int GetSsaIndex() const { return ssa_index_; }
710 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
711 bool HasSsaIndex() const { return ssa_index_ != -1; }
712
713 bool HasEnvironment() const { return environment_ != nullptr; }
714 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100715 void SetEnvironment(HEnvironment* environment) { environment_ = environment; }
716
Nicolas Geoffray39468442014-09-02 15:17:15 +0100717 // Returns the number of entries in the environment. Typically, that is the
718 // number of dex registers in a method. It could be more in case of inlining.
719 size_t EnvironmentSize() const;
720
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000721 LocationSummary* GetLocations() const { return locations_; }
722 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000723
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100724 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100725 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100726
Dave Allison20dfc792014-06-16 20:44:29 -0700727 bool HasOnlyOneUse() const {
728 return uses_ != nullptr && uses_->GetTail() == nullptr;
729 }
730
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100731#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +0100732 bool Is##type() const { return (As##type() != nullptr); } \
733 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000734 virtual H##type* As##type() { return nullptr; }
735
736 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
737#undef INSTRUCTION_TYPE_CHECK
738
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100739 // Returns whether the instruction can be moved within the graph.
740 virtual bool CanBeMoved() const { return false; }
741
742 // Returns whether the two instructions are of the same kind.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700743 virtual bool InstructionTypeEquals(HInstruction* other) const {
744 UNUSED(other);
745 return false;
746 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100747
748 // Returns whether any data encoded in the two instructions is equal.
749 // This method does not look at the inputs. Both instructions must be
750 // of the same type, otherwise the method has undefined behavior.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700751 virtual bool InstructionDataEquals(HInstruction* other) const {
752 UNUSED(other);
753 return false;
754 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100755
756 // Returns whether two instructions are equal, that is:
757 // 1) They have the same type and contain the same data,
758 // 2) Their inputs are identical.
759 bool Equals(HInstruction* other) const;
760
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100761 virtual InstructionKind GetKind() const = 0;
762
763 virtual size_t ComputeHashCode() const {
764 size_t result = GetKind();
765 for (size_t i = 0, e = InputCount(); i < e; ++i) {
766 result = (result * 31) + InputAt(i)->GetId();
767 }
768 return result;
769 }
770
771 SideEffects GetSideEffects() const { return side_effects_; }
772
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100773 size_t GetLifetimePosition() const { return lifetime_position_; }
774 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
775 LiveInterval* GetLiveInterval() const { return live_interval_; }
776 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
777 bool HasLiveInterval() const { return live_interval_ != nullptr; }
778
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000779 private:
780 HInstruction* previous_;
781 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000782 HBasicBlock* block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000783
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000784 // An instruction gets an id when it is added to the graph.
785 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +0100786 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000787 int id_;
788
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100789 // When doing liveness analysis, instructions that have uses get an SSA index.
790 int ssa_index_;
791
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100792 // List of instructions that have this instruction as input.
793 HUseListNode<HInstruction>* uses_;
794
795 // List of environments that contain this instruction.
796 HUseListNode<HEnvironment>* env_uses_;
797
Nicolas Geoffray39468442014-09-02 15:17:15 +0100798 // The environment associated with this instruction. Not null if the instruction
799 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100800 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000801
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000802 // Set by the code generator.
803 LocationSummary* locations_;
804
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100805 // Set by the liveness analysis.
806 LiveInterval* live_interval_;
807
808 // Set by the liveness analysis, this is the position in a linear
809 // order of blocks where this instruction's live interval start.
810 size_t lifetime_position_;
811
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100812 const SideEffects side_effects_;
813
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000814 friend class HBasicBlock;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100815 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000816
817 DISALLOW_COPY_AND_ASSIGN(HInstruction);
818};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700819std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000820
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100821template<typename T>
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000822class HUseIterator : public ValueObject {
823 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100824 explicit HUseIterator(HUseListNode<T>* uses) : current_(uses) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000825
826 bool Done() const { return current_ == nullptr; }
827
828 void Advance() {
829 DCHECK(!Done());
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000830 current_ = current_->GetTail();
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000831 }
832
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100833 HUseListNode<T>* Current() const {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000834 DCHECK(!Done());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100835 return current_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000836 }
837
838 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100839 HUseListNode<T>* current_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000840
841 friend class HValue;
842};
843
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100844// A HEnvironment object contains the values of virtual registers at a given location.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700845class HEnvironment : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100846 public:
847 HEnvironment(ArenaAllocator* arena, size_t number_of_vregs) : vregs_(arena, number_of_vregs) {
848 vregs_.SetSize(number_of_vregs);
849 for (size_t i = 0; i < number_of_vregs; i++) {
850 vregs_.Put(i, nullptr);
851 }
852 }
853
854 void Populate(const GrowableArray<HInstruction*>& env) {
855 for (size_t i = 0; i < env.Size(); i++) {
856 HInstruction* instruction = env.Get(i);
857 vregs_.Put(i, instruction);
858 if (instruction != nullptr) {
859 instruction->AddEnvUseAt(this, i);
860 }
861 }
862 }
863
864 void SetRawEnvAt(size_t index, HInstruction* instruction) {
865 vregs_.Put(index, instruction);
866 }
867
Nicolas Geoffray39468442014-09-02 15:17:15 +0100868 HInstruction* GetInstructionAt(size_t index) const {
869 return vregs_.Get(index);
870 }
871
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100872 GrowableArray<HInstruction*>* GetVRegs() {
873 return &vregs_;
874 }
875
Nicolas Geoffray39468442014-09-02 15:17:15 +0100876 size_t Size() const { return vregs_.Size(); }
877
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100878 private:
879 GrowableArray<HInstruction*> vregs_;
880
881 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
882};
883
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000884class HInputIterator : public ValueObject {
885 public:
Dave Allison20dfc792014-06-16 20:44:29 -0700886 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000887
888 bool Done() const { return index_ == instruction_->InputCount(); }
889 HInstruction* Current() const { return instruction_->InputAt(index_); }
890 void Advance() { index_++; }
891
892 private:
893 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100894 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000895
896 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
897};
898
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000899class HInstructionIterator : public ValueObject {
900 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100901 explicit HInstructionIterator(const HInstructionList& instructions)
902 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000903 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000904 }
905
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000906 bool Done() const { return instruction_ == nullptr; }
907 HInstruction* Current() const { return instruction_; }
908 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000909 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000910 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000911 }
912
913 private:
914 HInstruction* instruction_;
915 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100916
917 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000918};
919
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100920class HBackwardInstructionIterator : public ValueObject {
921 public:
922 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
923 : instruction_(instructions.last_instruction_) {
924 next_ = Done() ? nullptr : instruction_->GetPrevious();
925 }
926
927 bool Done() const { return instruction_ == nullptr; }
928 HInstruction* Current() const { return instruction_; }
929 void Advance() {
930 instruction_ = next_;
931 next_ = Done() ? nullptr : instruction_->GetPrevious();
932 }
933
934 private:
935 HInstruction* instruction_;
936 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100937
938 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100939};
940
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000941// An embedded container with N elements of type T. Used (with partial
942// specialization for N=0) because embedded arrays cannot have size 0.
943template<typename T, intptr_t N>
944class EmbeddedArray {
945 public:
Dave Allison20dfc792014-06-16 20:44:29 -0700946 EmbeddedArray() : elements_() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000947
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000948 intptr_t GetLength() const { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000949
950 const T& operator[](intptr_t i) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000951 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000952 return elements_[i];
953 }
954
955 T& operator[](intptr_t i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000956 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000957 return elements_[i];
958 }
959
960 const T& At(intptr_t i) const {
961 return (*this)[i];
962 }
963
964 void SetAt(intptr_t i, const T& val) {
965 (*this)[i] = val;
966 }
967
968 private:
969 T elements_[N];
970};
971
972template<typename T>
973class EmbeddedArray<T, 0> {
974 public:
975 intptr_t length() const { return 0; }
976 const T& operator[](intptr_t i) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700977 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000978 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700979 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000980 }
981 T& operator[](intptr_t i) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700982 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000983 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700984 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000985 }
986};
987
988template<intptr_t N>
989class HTemplateInstruction: public HInstruction {
990 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100991 HTemplateInstruction<N>(SideEffects side_effects)
992 : HInstruction(side_effects), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -0700993 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000994
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100995 virtual size_t InputCount() const { return N; }
996 virtual HInstruction* InputAt(size_t i) const { return inputs_[i]; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000997
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000998 protected:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100999 virtual void SetRawInputAt(size_t i, HInstruction* instruction) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001000 inputs_[i] = instruction;
1001 }
1002
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001003 private:
1004 EmbeddedArray<HInstruction*, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001005
1006 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001007};
1008
Dave Allison20dfc792014-06-16 20:44:29 -07001009template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001010class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07001011 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001012 HExpression<N>(Primitive::Type type, SideEffects side_effects)
1013 : HTemplateInstruction<N>(side_effects), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07001014 virtual ~HExpression() {}
1015
1016 virtual Primitive::Type GetType() const { return type_; }
1017
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001018 protected:
1019 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07001020};
1021
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001022// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
1023// instruction that branches to the exit block.
1024class HReturnVoid : public HTemplateInstruction<0> {
1025 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001026 HReturnVoid() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001027
1028 virtual bool IsControlFlow() const { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001029
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001030 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001031
1032 private:
1033 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
1034};
1035
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001036// Represents dex's RETURN opcodes. A HReturn is a control flow
1037// instruction that branches to the exit block.
1038class HReturn : public HTemplateInstruction<1> {
1039 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001040 explicit HReturn(HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001041 SetRawInputAt(0, value);
1042 }
1043
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001044 virtual bool IsControlFlow() const { return true; }
1045
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001046 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001047
1048 private:
1049 DISALLOW_COPY_AND_ASSIGN(HReturn);
1050};
1051
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001052// The exit instruction is the only instruction of the exit block.
1053// Instructions aborting the method (HTrow and HReturn) must branch to the
1054// exit block.
1055class HExit : public HTemplateInstruction<0> {
1056 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001057 HExit() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001058
1059 virtual bool IsControlFlow() const { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001060
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001061 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001062
1063 private:
1064 DISALLOW_COPY_AND_ASSIGN(HExit);
1065};
1066
1067// Jumps from one block to another.
1068class HGoto : public HTemplateInstruction<0> {
1069 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001070 HGoto() : HTemplateInstruction(SideEffects::None()) {}
1071
1072 virtual bool IsControlFlow() const { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001073
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001074 HBasicBlock* GetSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001075 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001076 }
1077
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001078 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001079
1080 private:
1081 DISALLOW_COPY_AND_ASSIGN(HGoto);
1082};
1083
Dave Allison20dfc792014-06-16 20:44:29 -07001084
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001085// Conditional branch. A block ending with an HIf instruction must have
1086// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001087class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001088 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001089 explicit HIf(HInstruction* input) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001090 SetRawInputAt(0, input);
1091 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001092
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001093 virtual bool IsControlFlow() const { return true; }
1094
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001095 HBasicBlock* IfTrueSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001096 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001097 }
1098
1099 HBasicBlock* IfFalseSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001100 return GetBlock()->GetSuccessors().Get(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001101 }
1102
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001103 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001104
Dave Allison20dfc792014-06-16 20:44:29 -07001105 virtual bool IsIfInstruction() const { return true; }
1106
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001107 private:
1108 DISALLOW_COPY_AND_ASSIGN(HIf);
1109};
1110
Roland Levillain88cb1752014-10-20 16:36:47 +01001111class HUnaryOperation : public HExpression<1> {
1112 public:
1113 HUnaryOperation(Primitive::Type result_type, HInstruction* input)
1114 : HExpression(result_type, SideEffects::None()) {
1115 SetRawInputAt(0, input);
1116 }
1117
1118 HInstruction* GetInput() const { return InputAt(0); }
1119 Primitive::Type GetResultType() const { return GetType(); }
1120
1121 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001122 virtual bool InstructionDataEquals(HInstruction* other) const {
1123 UNUSED(other);
1124 return true;
1125 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001126
Roland Levillain9240d6a2014-10-20 16:47:04 +01001127 // Try to statically evaluate `operation` and return a HConstant
1128 // containing the result of this evaluation. If `operation` cannot
1129 // be evaluated as a constant, return nullptr.
1130 HConstant* TryStaticEvaluation() const;
1131
1132 // Apply this operation to `x`.
1133 virtual int32_t Evaluate(int32_t x) const = 0;
1134 virtual int64_t Evaluate(int64_t x) const = 0;
1135
Roland Levillain88cb1752014-10-20 16:36:47 +01001136 DECLARE_INSTRUCTION(UnaryOperation);
1137
1138 private:
1139 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
1140};
1141
Dave Allison20dfc792014-06-16 20:44:29 -07001142class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001143 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001144 HBinaryOperation(Primitive::Type result_type,
1145 HInstruction* left,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001146 HInstruction* right) : HExpression(result_type, SideEffects::None()) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001147 SetRawInputAt(0, left);
1148 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001149 }
1150
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001151 HInstruction* GetLeft() const { return InputAt(0); }
1152 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07001153 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001154
1155 virtual bool IsCommutative() { return false; }
1156
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001157 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001158 virtual bool InstructionDataEquals(HInstruction* other) const {
1159 UNUSED(other);
1160 return true;
1161 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001162
Roland Levillain9240d6a2014-10-20 16:47:04 +01001163 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01001164 // containing the result of this evaluation. If `operation` cannot
1165 // be evaluated as a constant, return nullptr.
Roland Levillain9240d6a2014-10-20 16:47:04 +01001166 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01001167
1168 // Apply this operation to `x` and `y`.
1169 virtual int32_t Evaluate(int32_t x, int32_t y) const = 0;
1170 virtual int64_t Evaluate(int64_t x, int64_t y) const = 0;
1171
Roland Levillainccc07a92014-09-16 14:48:16 +01001172 DECLARE_INSTRUCTION(BinaryOperation);
1173
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001174 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001175 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
1176};
1177
Dave Allison20dfc792014-06-16 20:44:29 -07001178class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001179 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001180 HCondition(HInstruction* first, HInstruction* second)
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001181 : HBinaryOperation(Primitive::kPrimBoolean, first, second),
1182 needs_materialization_(true) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001183
1184 virtual bool IsCommutative() { return true; }
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001185
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001186 bool NeedsMaterialization() const { return needs_materialization_; }
1187 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001188
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001189 // For code generation purposes, returns whether this instruction is just before
1190 // `if_`, and disregard moves in between.
1191 bool IsBeforeWhenDisregardMoves(HIf* if_) const;
1192
Dave Allison20dfc792014-06-16 20:44:29 -07001193 DECLARE_INSTRUCTION(Condition);
1194
1195 virtual IfCondition GetCondition() const = 0;
1196
1197 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001198 // For register allocation purposes, returns whether this instruction needs to be
1199 // materialized (that is, not just be in the processor flags).
1200 bool needs_materialization_;
1201
Dave Allison20dfc792014-06-16 20:44:29 -07001202 DISALLOW_COPY_AND_ASSIGN(HCondition);
1203};
1204
1205// Instruction to check if two inputs are equal to each other.
1206class HEqual : public HCondition {
1207 public:
1208 HEqual(HInstruction* first, HInstruction* second)
1209 : HCondition(first, second) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001210
Roland Levillain93445682014-10-06 19:24:02 +01001211 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1212 return x == y ? 1 : 0;
1213 }
1214 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1215 return x == y ? 1 : 0;
1216 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001217
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001218 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001219
Dave Allison20dfc792014-06-16 20:44:29 -07001220 virtual IfCondition GetCondition() const {
1221 return kCondEQ;
1222 }
1223
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001224 private:
1225 DISALLOW_COPY_AND_ASSIGN(HEqual);
1226};
1227
Dave Allison20dfc792014-06-16 20:44:29 -07001228class HNotEqual : public HCondition {
1229 public:
1230 HNotEqual(HInstruction* first, HInstruction* second)
1231 : HCondition(first, second) {}
1232
Roland Levillain93445682014-10-06 19:24:02 +01001233 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1234 return x != y ? 1 : 0;
1235 }
1236 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1237 return x != y ? 1 : 0;
1238 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001239
Dave Allison20dfc792014-06-16 20:44:29 -07001240 DECLARE_INSTRUCTION(NotEqual);
1241
1242 virtual IfCondition GetCondition() const {
1243 return kCondNE;
1244 }
1245
1246 private:
1247 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
1248};
1249
1250class HLessThan : public HCondition {
1251 public:
1252 HLessThan(HInstruction* first, HInstruction* second)
1253 : HCondition(first, second) {}
1254
Roland Levillain93445682014-10-06 19:24:02 +01001255 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1256 return x < y ? 1 : 0;
1257 }
1258 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1259 return x < y ? 1 : 0;
1260 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001261
Dave Allison20dfc792014-06-16 20:44:29 -07001262 DECLARE_INSTRUCTION(LessThan);
1263
1264 virtual IfCondition GetCondition() const {
1265 return kCondLT;
1266 }
1267
1268 private:
1269 DISALLOW_COPY_AND_ASSIGN(HLessThan);
1270};
1271
1272class HLessThanOrEqual : public HCondition {
1273 public:
1274 HLessThanOrEqual(HInstruction* first, HInstruction* second)
1275 : HCondition(first, second) {}
1276
Roland Levillain93445682014-10-06 19:24:02 +01001277 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1278 return x <= y ? 1 : 0;
1279 }
1280 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1281 return x <= y ? 1 : 0;
1282 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001283
Dave Allison20dfc792014-06-16 20:44:29 -07001284 DECLARE_INSTRUCTION(LessThanOrEqual);
1285
1286 virtual IfCondition GetCondition() const {
1287 return kCondLE;
1288 }
1289
1290 private:
1291 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
1292};
1293
1294class HGreaterThan : public HCondition {
1295 public:
1296 HGreaterThan(HInstruction* first, HInstruction* second)
1297 : HCondition(first, second) {}
1298
Roland Levillain93445682014-10-06 19:24:02 +01001299 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1300 return x > y ? 1 : 0;
1301 }
1302 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1303 return x > y ? 1 : 0;
1304 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001305
Dave Allison20dfc792014-06-16 20:44:29 -07001306 DECLARE_INSTRUCTION(GreaterThan);
1307
1308 virtual IfCondition GetCondition() const {
1309 return kCondGT;
1310 }
1311
1312 private:
1313 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
1314};
1315
1316class HGreaterThanOrEqual : public HCondition {
1317 public:
1318 HGreaterThanOrEqual(HInstruction* first, HInstruction* second)
1319 : HCondition(first, second) {}
1320
Roland Levillain93445682014-10-06 19:24:02 +01001321 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1322 return x >= y ? 1 : 0;
1323 }
1324 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1325 return x >= y ? 1 : 0;
1326 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001327
Dave Allison20dfc792014-06-16 20:44:29 -07001328 DECLARE_INSTRUCTION(GreaterThanOrEqual);
1329
1330 virtual IfCondition GetCondition() const {
1331 return kCondGE;
1332 }
1333
1334 private:
1335 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
1336};
1337
1338
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001339// Instruction to check how two inputs compare to each other.
1340// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
1341class HCompare : public HBinaryOperation {
1342 public:
1343 HCompare(Primitive::Type type, HInstruction* first, HInstruction* second)
1344 : HBinaryOperation(Primitive::kPrimInt, first, second) {
1345 DCHECK_EQ(type, first->GetType());
1346 DCHECK_EQ(type, second->GetType());
1347 }
1348
Roland Levillain93445682014-10-06 19:24:02 +01001349 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001350 return
1351 x == y ? 0 :
1352 x > y ? 1 :
1353 -1;
1354 }
Roland Levillain93445682014-10-06 19:24:02 +01001355 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001356 return
1357 x == y ? 0 :
1358 x > y ? 1 :
1359 -1;
1360 }
1361
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001362 DECLARE_INSTRUCTION(Compare);
1363
1364 private:
1365 DISALLOW_COPY_AND_ASSIGN(HCompare);
1366};
1367
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001368// A local in the graph. Corresponds to a Dex register.
1369class HLocal : public HTemplateInstruction<0> {
1370 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001371 explicit HLocal(uint16_t reg_number)
1372 : HTemplateInstruction(SideEffects::None()), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001373
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001374 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001375
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001376 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001377
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001378 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001379 // The Dex register number.
1380 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001381
1382 DISALLOW_COPY_AND_ASSIGN(HLocal);
1383};
1384
1385// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07001386class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001387 public:
Roland Levillain5799fc02014-09-25 12:15:20 +01001388 HLoadLocal(HLocal* local, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001389 : HExpression(type, SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001390 SetRawInputAt(0, local);
1391 }
1392
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001393 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1394
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001395 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001396
1397 private:
1398 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
1399};
1400
1401// Store a value in a given local. This instruction has two inputs: the value
1402// and the local.
1403class HStoreLocal : public HTemplateInstruction<2> {
1404 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001405 HStoreLocal(HLocal* local, HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001406 SetRawInputAt(0, local);
1407 SetRawInputAt(1, value);
1408 }
1409
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001410 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1411
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001412 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001413
1414 private:
1415 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
1416};
1417
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001418class HConstant : public HExpression<0> {
1419 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001420 explicit HConstant(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
1421
1422 virtual bool CanBeMoved() const { return true; }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001423
1424 DECLARE_INSTRUCTION(Constant);
1425
1426 private:
1427 DISALLOW_COPY_AND_ASSIGN(HConstant);
1428};
1429
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001430class HFloatConstant : public HConstant {
1431 public:
1432 explicit HFloatConstant(float value) : HConstant(Primitive::kPrimFloat), value_(value) {}
1433
1434 float GetValue() const { return value_; }
1435
1436 virtual bool InstructionDataEquals(HInstruction* other) const {
1437 return bit_cast<float, int32_t>(other->AsFloatConstant()->value_) ==
1438 bit_cast<float, int32_t>(value_);
1439 }
1440
1441 virtual size_t ComputeHashCode() const { return static_cast<size_t>(GetValue()); }
1442
1443 DECLARE_INSTRUCTION(FloatConstant);
1444
1445 private:
1446 const float value_;
1447
1448 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
1449};
1450
1451class HDoubleConstant : public HConstant {
1452 public:
1453 explicit HDoubleConstant(double value) : HConstant(Primitive::kPrimDouble), value_(value) {}
1454
1455 double GetValue() const { return value_; }
1456
1457 virtual bool InstructionDataEquals(HInstruction* other) const {
1458 return bit_cast<double, int64_t>(other->AsDoubleConstant()->value_) ==
1459 bit_cast<double, int64_t>(value_);
1460 }
1461
1462 virtual size_t ComputeHashCode() const { return static_cast<size_t>(GetValue()); }
1463
1464 DECLARE_INSTRUCTION(DoubleConstant);
1465
1466 private:
1467 const double value_;
1468
1469 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
1470};
1471
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001472// Constants of the type int. Those can be from Dex instructions, or
1473// synthesized (for example with the if-eqz instruction).
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001474class HIntConstant : public HConstant {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001475 public:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001476 explicit HIntConstant(int32_t value) : HConstant(Primitive::kPrimInt), value_(value) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001477
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001478 int32_t GetValue() const { return value_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001479
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001480 virtual bool InstructionDataEquals(HInstruction* other) const {
1481 return other->AsIntConstant()->value_ == value_;
1482 }
1483
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001484 virtual size_t ComputeHashCode() const { return GetValue(); }
1485
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001486 DECLARE_INSTRUCTION(IntConstant);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001487
1488 private:
1489 const int32_t value_;
1490
1491 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
1492};
1493
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001494class HLongConstant : public HConstant {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001495 public:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001496 explicit HLongConstant(int64_t value) : HConstant(Primitive::kPrimLong), value_(value) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001497
1498 int64_t GetValue() const { return value_; }
1499
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001500 virtual bool InstructionDataEquals(HInstruction* other) const {
1501 return other->AsLongConstant()->value_ == value_;
1502 }
1503
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001504 virtual size_t ComputeHashCode() const { return static_cast<size_t>(GetValue()); }
1505
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001506 DECLARE_INSTRUCTION(LongConstant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001507
1508 private:
1509 const int64_t value_;
1510
1511 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
1512};
1513
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001514class HInvoke : public HInstruction {
1515 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001516 HInvoke(ArenaAllocator* arena,
1517 uint32_t number_of_arguments,
1518 Primitive::Type return_type,
1519 uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001520 : HInstruction(SideEffects::All()),
1521 inputs_(arena, number_of_arguments),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001522 return_type_(return_type),
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001523 dex_pc_(dex_pc) {
1524 inputs_.SetSize(number_of_arguments);
1525 }
1526
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001527 virtual size_t InputCount() const { return inputs_.Size(); }
1528 virtual HInstruction* InputAt(size_t i) const { return inputs_.Get(i); }
1529
1530 // Runtime needs to walk the stack, so Dex -> Dex calls need to
1531 // know their environment.
1532 virtual bool NeedsEnvironment() const { return true; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001533
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001534 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001535 SetRawInputAt(index, argument);
1536 }
1537
1538 virtual void SetRawInputAt(size_t index, HInstruction* input) {
1539 inputs_.Put(index, input);
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001540 }
1541
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001542 virtual Primitive::Type GetType() const { return return_type_; }
1543
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001544 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001545
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001546 DECLARE_INSTRUCTION(Invoke);
1547
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001548 protected:
1549 GrowableArray<HInstruction*> inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001550 const Primitive::Type return_type_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001551 const uint32_t dex_pc_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001552
1553 private:
1554 DISALLOW_COPY_AND_ASSIGN(HInvoke);
1555};
1556
1557class HInvokeStatic : public HInvoke {
1558 public:
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001559 HInvokeStatic(ArenaAllocator* arena,
1560 uint32_t number_of_arguments,
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001561 Primitive::Type return_type,
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001562 uint32_t dex_pc,
1563 uint32_t index_in_dex_cache)
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001564 : HInvoke(arena, number_of_arguments, return_type, dex_pc),
1565 index_in_dex_cache_(index_in_dex_cache) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001566
1567 uint32_t GetIndexInDexCache() const { return index_in_dex_cache_; }
1568
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001569 DECLARE_INSTRUCTION(InvokeStatic);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001570
1571 private:
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001572 const uint32_t index_in_dex_cache_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001573
1574 DISALLOW_COPY_AND_ASSIGN(HInvokeStatic);
1575};
1576
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001577class HInvokeVirtual : public HInvoke {
1578 public:
1579 HInvokeVirtual(ArenaAllocator* arena,
1580 uint32_t number_of_arguments,
1581 Primitive::Type return_type,
1582 uint32_t dex_pc,
1583 uint32_t vtable_index)
1584 : HInvoke(arena, number_of_arguments, return_type, dex_pc),
1585 vtable_index_(vtable_index) {}
1586
1587 uint32_t GetVTableIndex() const { return vtable_index_; }
1588
1589 DECLARE_INSTRUCTION(InvokeVirtual);
1590
1591 private:
1592 const uint32_t vtable_index_;
1593
1594 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
1595};
1596
Dave Allison20dfc792014-06-16 20:44:29 -07001597class HNewInstance : public HExpression<0> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001598 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001599 HNewInstance(uint32_t dex_pc, uint16_t type_index)
1600 : HExpression(Primitive::kPrimNot, SideEffects::None()),
1601 dex_pc_(dex_pc),
1602 type_index_(type_index) {}
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001603
1604 uint32_t GetDexPc() const { return dex_pc_; }
1605 uint16_t GetTypeIndex() const { return type_index_; }
1606
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001607 // Calls runtime so needs an environment.
1608 virtual bool NeedsEnvironment() const { return true; }
1609
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001610 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001611
1612 private:
1613 const uint32_t dex_pc_;
1614 const uint16_t type_index_;
1615
1616 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
1617};
1618
Roland Levillain88cb1752014-10-20 16:36:47 +01001619class HNeg : public HUnaryOperation {
1620 public:
1621 explicit HNeg(Primitive::Type result_type, HInstruction* input)
1622 : HUnaryOperation(result_type, input) {}
1623
Roland Levillain9240d6a2014-10-20 16:47:04 +01001624 virtual int32_t Evaluate(int32_t x) const OVERRIDE { return -x; }
1625 virtual int64_t Evaluate(int64_t x) const OVERRIDE { return -x; }
1626
Roland Levillain88cb1752014-10-20 16:36:47 +01001627 DECLARE_INSTRUCTION(Neg);
1628
1629 private:
1630 DISALLOW_COPY_AND_ASSIGN(HNeg);
1631};
1632
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001633class HNewArray : public HExpression<1> {
1634 public:
1635 HNewArray(HInstruction* length, uint32_t dex_pc, uint16_t type_index)
1636 : HExpression(Primitive::kPrimNot, SideEffects::None()),
1637 dex_pc_(dex_pc),
1638 type_index_(type_index) {
1639 SetRawInputAt(0, length);
1640 }
1641
1642 uint32_t GetDexPc() const { return dex_pc_; }
1643 uint16_t GetTypeIndex() const { return type_index_; }
1644
1645 // Calls runtime so needs an environment.
1646 virtual bool NeedsEnvironment() const { return true; }
1647
1648 DECLARE_INSTRUCTION(NewArray);
1649
1650 private:
1651 const uint32_t dex_pc_;
1652 const uint16_t type_index_;
1653
1654 DISALLOW_COPY_AND_ASSIGN(HNewArray);
1655};
1656
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001657class HAdd : public HBinaryOperation {
1658 public:
1659 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1660 : HBinaryOperation(result_type, left, right) {}
1661
1662 virtual bool IsCommutative() { return true; }
1663
Roland Levillain93445682014-10-06 19:24:02 +01001664 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1665 return x + y;
1666 }
1667 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1668 return x + y;
1669 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001670
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001671 DECLARE_INSTRUCTION(Add);
1672
1673 private:
1674 DISALLOW_COPY_AND_ASSIGN(HAdd);
1675};
1676
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001677class HSub : public HBinaryOperation {
1678 public:
1679 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1680 : HBinaryOperation(result_type, left, right) {}
1681
Roland Levillain93445682014-10-06 19:24:02 +01001682 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1683 return x - y;
1684 }
1685 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1686 return x - y;
1687 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001688
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001689 DECLARE_INSTRUCTION(Sub);
1690
1691 private:
1692 DISALLOW_COPY_AND_ASSIGN(HSub);
1693};
1694
Calin Juravle34bacdf2014-10-07 20:23:36 +01001695class HMul : public HBinaryOperation {
1696 public:
1697 HMul(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1698 : HBinaryOperation(result_type, left, right) {}
1699
1700 virtual bool IsCommutative() { return true; }
1701
1702 virtual int32_t Evaluate(int32_t x, int32_t y) const { return x * y; }
1703 virtual int64_t Evaluate(int64_t x, int64_t y) const { return x * y; }
1704
1705 DECLARE_INSTRUCTION(Mul);
1706
1707 private:
1708 DISALLOW_COPY_AND_ASSIGN(HMul);
1709};
1710
Calin Juravle7c4954d2014-10-28 16:57:40 +00001711class HDiv : public HBinaryOperation {
1712 public:
1713 HDiv(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1714 : HBinaryOperation(result_type, left, right) {}
1715
1716 virtual int32_t Evaluate(int32_t x, int32_t y) const { return x / y; }
1717 virtual int64_t Evaluate(int64_t x, int64_t y) const { return x / y; }
1718
1719 DECLARE_INSTRUCTION(Div);
1720
1721 private:
1722 DISALLOW_COPY_AND_ASSIGN(HDiv);
1723};
1724
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001725// The value of a parameter in this method. Its location depends on
1726// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07001727class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001728 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001729 HParameterValue(uint8_t index, Primitive::Type parameter_type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001730 : HExpression(parameter_type, SideEffects::None()), index_(index) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001731
1732 uint8_t GetIndex() const { return index_; }
1733
1734 DECLARE_INSTRUCTION(ParameterValue);
1735
1736 private:
1737 // The index of this parameter in the parameters list. Must be less
1738 // than HGraph::number_of_in_vregs_;
1739 const uint8_t index_;
1740
1741 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
1742};
1743
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001744class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001745 public:
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001746 explicit HNot(Primitive::Type result_type, HInstruction* input)
1747 : HUnaryOperation(result_type, input) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001748
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001749 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001750 virtual bool InstructionDataEquals(HInstruction* other) const {
1751 UNUSED(other);
1752 return true;
1753 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001754
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001755 virtual int32_t Evaluate(int32_t x) const OVERRIDE { return ~x; }
1756 virtual int64_t Evaluate(int64_t x) const OVERRIDE { return ~x; }
1757
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001758 DECLARE_INSTRUCTION(Not);
1759
1760 private:
1761 DISALLOW_COPY_AND_ASSIGN(HNot);
1762};
1763
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001764class HPhi : public HInstruction {
1765 public:
1766 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001767 : HInstruction(SideEffects::None()),
1768 inputs_(arena, number_of_inputs),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001769 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001770 type_(type),
1771 is_live_(false) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001772 inputs_.SetSize(number_of_inputs);
1773 }
1774
1775 virtual size_t InputCount() const { return inputs_.Size(); }
1776 virtual HInstruction* InputAt(size_t i) const { return inputs_.Get(i); }
1777
1778 virtual void SetRawInputAt(size_t index, HInstruction* input) {
1779 inputs_.Put(index, input);
1780 }
1781
1782 void AddInput(HInstruction* input);
1783
1784 virtual Primitive::Type GetType() const { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001785 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001786
1787 uint32_t GetRegNumber() const { return reg_number_; }
1788
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001789 void SetDead() { is_live_ = false; }
1790 void SetLive() { is_live_ = true; }
1791 bool IsDead() const { return !is_live_; }
1792 bool IsLive() const { return is_live_; }
1793
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001794 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001795
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001796 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001797 GrowableArray<HInstruction*> inputs_;
1798 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001799 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001800 bool is_live_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001801
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001802 DISALLOW_COPY_AND_ASSIGN(HPhi);
1803};
1804
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001805class HNullCheck : public HExpression<1> {
1806 public:
1807 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001808 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001809 SetRawInputAt(0, value);
1810 }
1811
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001812 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001813 virtual bool InstructionDataEquals(HInstruction* other) const {
1814 UNUSED(other);
1815 return true;
1816 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001817
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001818 virtual bool NeedsEnvironment() const { return true; }
1819
Roland Levillaine161a2a2014-10-03 12:45:18 +01001820 virtual bool CanThrow() const { return true; }
1821
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001822 uint32_t GetDexPc() const { return dex_pc_; }
1823
1824 DECLARE_INSTRUCTION(NullCheck);
1825
1826 private:
1827 const uint32_t dex_pc_;
1828
1829 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
1830};
1831
1832class FieldInfo : public ValueObject {
1833 public:
Roland Levillain5799fc02014-09-25 12:15:20 +01001834 FieldInfo(MemberOffset field_offset, Primitive::Type field_type)
Nicolas Geoffray39468442014-09-02 15:17:15 +01001835 : field_offset_(field_offset), field_type_(field_type) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001836
1837 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001838 Primitive::Type GetFieldType() const { return field_type_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001839
1840 private:
1841 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01001842 const Primitive::Type field_type_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001843};
1844
1845class HInstanceFieldGet : public HExpression<1> {
1846 public:
1847 HInstanceFieldGet(HInstruction* value,
1848 Primitive::Type field_type,
1849 MemberOffset field_offset)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001850 : HExpression(field_type, SideEffects::DependsOnSomething()),
1851 field_info_(field_offset, field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001852 SetRawInputAt(0, value);
1853 }
1854
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001855 virtual bool CanBeMoved() const { return true; }
1856 virtual bool InstructionDataEquals(HInstruction* other) const {
1857 size_t other_offset = other->AsInstanceFieldGet()->GetFieldOffset().SizeValue();
1858 return other_offset == GetFieldOffset().SizeValue();
1859 }
1860
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001861 virtual size_t ComputeHashCode() const {
1862 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
1863 }
1864
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001865 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001866 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001867
1868 DECLARE_INSTRUCTION(InstanceFieldGet);
1869
1870 private:
1871 const FieldInfo field_info_;
1872
1873 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
1874};
1875
1876class HInstanceFieldSet : public HTemplateInstruction<2> {
1877 public:
1878 HInstanceFieldSet(HInstruction* object,
1879 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01001880 Primitive::Type field_type,
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001881 MemberOffset field_offset)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001882 : HTemplateInstruction(SideEffects::ChangesSomething()),
1883 field_info_(field_offset, field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001884 SetRawInputAt(0, object);
1885 SetRawInputAt(1, value);
1886 }
1887
1888 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(InstanceFieldSet);
1892
1893 private:
1894 const FieldInfo field_info_;
1895
1896 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
1897};
1898
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001899class HArrayGet : public HExpression<2> {
1900 public:
1901 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001902 : HExpression(type, SideEffects::DependsOnSomething()) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001903 SetRawInputAt(0, array);
1904 SetRawInputAt(1, index);
1905 }
1906
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001907 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001908 virtual bool InstructionDataEquals(HInstruction* other) const {
1909 UNUSED(other);
1910 return true;
1911 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001912 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001913
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001914 DECLARE_INSTRUCTION(ArrayGet);
1915
1916 private:
1917 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
1918};
1919
1920class HArraySet : public HTemplateInstruction<3> {
1921 public:
1922 HArraySet(HInstruction* array,
1923 HInstruction* index,
1924 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001925 Primitive::Type expected_component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001926 uint32_t dex_pc)
1927 : HTemplateInstruction(SideEffects::ChangesSomething()),
1928 dex_pc_(dex_pc),
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001929 expected_component_type_(expected_component_type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001930 SetRawInputAt(0, array);
1931 SetRawInputAt(1, index);
1932 SetRawInputAt(2, value);
1933 }
1934
1935 virtual bool NeedsEnvironment() const {
1936 // We currently always call a runtime method to catch array store
1937 // exceptions.
1938 return InputAt(2)->GetType() == Primitive::kPrimNot;
1939 }
1940
1941 uint32_t GetDexPc() const { return dex_pc_; }
1942
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001943 HInstruction* GetValue() const { return InputAt(2); }
1944
1945 Primitive::Type GetComponentType() const {
1946 // The Dex format does not type floating point index operations. Since the
1947 // `expected_component_type_` is set during building and can therefore not
1948 // be correct, we also check what is the value type. If it is a floating
1949 // point type, we must use that type.
1950 Primitive::Type value_type = GetValue()->GetType();
1951 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
1952 ? value_type
1953 : expected_component_type_;
1954 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001955
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001956 DECLARE_INSTRUCTION(ArraySet);
1957
1958 private:
1959 const uint32_t dex_pc_;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001960 const Primitive::Type expected_component_type_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001961
1962 DISALLOW_COPY_AND_ASSIGN(HArraySet);
1963};
1964
1965class HArrayLength : public HExpression<1> {
1966 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001967 explicit HArrayLength(HInstruction* array)
1968 : HExpression(Primitive::kPrimInt, SideEffects::None()) {
1969 // Note that arrays do not change length, so the instruction does not
1970 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001971 SetRawInputAt(0, array);
1972 }
1973
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001974 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001975 virtual bool InstructionDataEquals(HInstruction* other) const {
1976 UNUSED(other);
1977 return true;
1978 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001979
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001980 DECLARE_INSTRUCTION(ArrayLength);
1981
1982 private:
1983 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
1984};
1985
1986class HBoundsCheck : public HExpression<2> {
1987 public:
1988 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001989 : HExpression(index->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001990 DCHECK(index->GetType() == Primitive::kPrimInt);
1991 SetRawInputAt(0, index);
1992 SetRawInputAt(1, length);
1993 }
1994
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001995 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001996 virtual bool InstructionDataEquals(HInstruction* other) const {
1997 UNUSED(other);
1998 return true;
1999 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002000
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002001 virtual bool NeedsEnvironment() const { return true; }
2002
Roland Levillaine161a2a2014-10-03 12:45:18 +01002003 virtual bool CanThrow() const { return true; }
2004
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002005 uint32_t GetDexPc() const { return dex_pc_; }
2006
2007 DECLARE_INSTRUCTION(BoundsCheck);
2008
2009 private:
2010 const uint32_t dex_pc_;
2011
2012 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
2013};
2014
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002015/**
2016 * Some DEX instructions are folded into multiple HInstructions that need
2017 * to stay live until the last HInstruction. This class
2018 * is used as a marker for the baseline compiler to ensure its preceding
2019 * HInstruction stays live. `index` is the temporary number that is used
2020 * for knowing the stack offset where to store the instruction.
2021 */
2022class HTemporary : public HTemplateInstruction<0> {
2023 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002024 explicit HTemporary(size_t index) : HTemplateInstruction(SideEffects::None()), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002025
2026 size_t GetIndex() const { return index_; }
2027
2028 DECLARE_INSTRUCTION(Temporary);
2029
2030 private:
2031 const size_t index_;
2032
2033 DISALLOW_COPY_AND_ASSIGN(HTemporary);
2034};
2035
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002036class HSuspendCheck : public HTemplateInstruction<0> {
2037 public:
2038 explicit HSuspendCheck(uint32_t dex_pc)
Nicolas Geoffray9ebc72c2014-09-25 16:33:42 +01002039 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002040
2041 virtual bool NeedsEnvironment() const {
2042 return true;
2043 }
2044
2045 uint32_t GetDexPc() const { return dex_pc_; }
2046
2047 DECLARE_INSTRUCTION(SuspendCheck);
2048
2049 private:
2050 const uint32_t dex_pc_;
2051
2052 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
2053};
2054
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002055// TODO: Make this class handle the case the load is null (dex cache
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002056// is null). This will be required when using it for other things than
2057// initialization check.
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002058/**
2059 * Instruction to load a Class object.
2060 */
2061class HLoadClass : public HExpression<0> {
2062 public:
2063 HLoadClass(uint16_t type_index,
2064 bool is_referrers_class,
2065 bool is_initialized,
2066 uint32_t dex_pc)
2067 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2068 type_index_(type_index),
2069 is_referrers_class_(is_referrers_class),
2070 is_initialized_(is_initialized),
2071 dex_pc_(dex_pc) {}
2072
2073 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2074 return other->AsLoadClass()->type_index_ == type_index_;
2075 }
2076
2077 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
2078
2079 uint32_t GetDexPc() const { return dex_pc_; }
2080 uint16_t GetTypeIndex() const { return type_index_; }
2081
2082 bool NeedsInitialization() const {
2083 return !is_initialized_ && !is_referrers_class_;
2084 }
2085
2086 bool IsReferrersClass() const { return is_referrers_class_; }
2087
2088 DECLARE_INSTRUCTION(LoadClass);
2089
2090 private:
2091 const uint16_t type_index_;
2092 const bool is_referrers_class_;
2093 const bool is_initialized_;
2094 const uint32_t dex_pc_;
2095
2096 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
2097};
2098
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002099class HLoadString : public HExpression<0> {
2100 public:
2101 HLoadString(uint32_t string_index, uint32_t dex_pc)
2102 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2103 string_index_(string_index),
2104 dex_pc_(dex_pc) {}
2105
2106 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2107 return other->AsLoadString()->string_index_ == string_index_;
2108 }
2109
2110 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
2111
2112 uint32_t GetDexPc() const { return dex_pc_; }
2113 uint32_t GetStringIndex() const { return string_index_; }
2114
2115 // TODO: Can we deopt or debug when we resolve a string?
2116 bool NeedsEnvironment() const OVERRIDE { return false; }
2117
2118 DECLARE_INSTRUCTION(LoadString);
2119
2120 private:
2121 const uint32_t string_index_;
2122 const uint32_t dex_pc_;
2123
2124 DISALLOW_COPY_AND_ASSIGN(HLoadString);
2125};
2126
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002127// TODO: Pass this check to HInvokeStatic nodes.
2128/**
2129 * Performs an initialization check on its Class object input.
2130 */
2131class HClinitCheck : public HExpression<1> {
2132 public:
2133 explicit HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
2134 : HExpression(Primitive::kPrimNot, SideEffects::All()),
2135 dex_pc_(dex_pc) {
2136 SetRawInputAt(0, constant);
2137 }
2138
2139 bool NeedsEnvironment() const OVERRIDE {
2140 // May call runtime to initialize the class.
2141 return true;
2142 }
2143
2144 uint32_t GetDexPc() const { return dex_pc_; }
2145
2146 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
2147
2148 DECLARE_INSTRUCTION(ClinitCheck);
2149
2150 private:
2151 const uint32_t dex_pc_;
2152
2153 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
2154};
2155
2156class HStaticFieldGet : public HExpression<1> {
2157 public:
2158 HStaticFieldGet(HInstruction* cls,
2159 Primitive::Type field_type,
2160 MemberOffset field_offset)
2161 : HExpression(field_type, SideEffects::DependsOnSomething()),
2162 field_info_(field_offset, field_type) {
2163 SetRawInputAt(0, cls);
2164 }
2165
2166 bool CanBeMoved() const OVERRIDE { return true; }
2167 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2168 size_t other_offset = other->AsStaticFieldGet()->GetFieldOffset().SizeValue();
2169 return other_offset == GetFieldOffset().SizeValue();
2170 }
2171
2172 size_t ComputeHashCode() const OVERRIDE {
2173 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
2174 }
2175
2176 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
2177 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
2178
2179 DECLARE_INSTRUCTION(StaticFieldGet);
2180
2181 private:
2182 const FieldInfo field_info_;
2183
2184 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
2185};
2186
2187class HStaticFieldSet : public HTemplateInstruction<2> {
2188 public:
2189 HStaticFieldSet(HInstruction* cls,
2190 HInstruction* value,
2191 Primitive::Type field_type,
2192 MemberOffset field_offset)
2193 : HTemplateInstruction(SideEffects::ChangesSomething()),
2194 field_info_(field_offset, field_type) {
2195 SetRawInputAt(0, cls);
2196 SetRawInputAt(1, value);
2197 }
2198
2199 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
2200 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
2201
2202 DECLARE_INSTRUCTION(StaticFieldSet);
2203
2204 private:
2205 const FieldInfo field_info_;
2206
2207 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
2208};
2209
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002210class MoveOperands : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002211 public:
Nicolas Geoffray740475d2014-09-29 10:33:25 +01002212 MoveOperands(Location source, Location destination, HInstruction* instruction)
2213 : source_(source), destination_(destination), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002214
2215 Location GetSource() const { return source_; }
2216 Location GetDestination() const { return destination_; }
2217
2218 void SetSource(Location value) { source_ = value; }
2219 void SetDestination(Location value) { destination_ = value; }
2220
2221 // The parallel move resolver marks moves as "in-progress" by clearing the
2222 // destination (but not the source).
2223 Location MarkPending() {
2224 DCHECK(!IsPending());
2225 Location dest = destination_;
2226 destination_ = Location::NoLocation();
2227 return dest;
2228 }
2229
2230 void ClearPending(Location dest) {
2231 DCHECK(IsPending());
2232 destination_ = dest;
2233 }
2234
2235 bool IsPending() const {
2236 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
2237 return destination_.IsInvalid() && !source_.IsInvalid();
2238 }
2239
2240 // True if this blocks a move from the given location.
2241 bool Blocks(Location loc) const {
2242 return !IsEliminated() && source_.Equals(loc);
2243 }
2244
2245 // A move is redundant if it's been eliminated, if its source and
2246 // destination are the same, or if its destination is unneeded.
2247 bool IsRedundant() const {
2248 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
2249 }
2250
2251 // We clear both operands to indicate move that's been eliminated.
2252 void Eliminate() {
2253 source_ = destination_ = Location::NoLocation();
2254 }
2255
2256 bool IsEliminated() const {
2257 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
2258 return source_.IsInvalid();
2259 }
2260
Nicolas Geoffray740475d2014-09-29 10:33:25 +01002261 HInstruction* GetInstruction() const { return instruction_; }
2262
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002263 private:
2264 Location source_;
2265 Location destination_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01002266 // The instruction this move is assocatied with. Null when this move is
2267 // for moving an input in the expected locations of user (including a phi user).
2268 // This is only used in debug mode, to ensure we do not connect interval siblings
2269 // in the same parallel move.
2270 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002271
2272 DISALLOW_COPY_AND_ASSIGN(MoveOperands);
2273};
2274
2275static constexpr size_t kDefaultNumberOfMoves = 4;
2276
2277class HParallelMove : public HTemplateInstruction<0> {
2278 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002279 explicit HParallelMove(ArenaAllocator* arena)
2280 : HTemplateInstruction(SideEffects::None()), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002281
2282 void AddMove(MoveOperands* move) {
Nicolas Geoffray740475d2014-09-29 10:33:25 +01002283 if (kIsDebugBuild && move->GetInstruction() != nullptr) {
2284 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
2285 DCHECK_NE(moves_.Get(i)->GetInstruction(), move->GetInstruction())
2286 << "Doing parallel moves for the same instruction.";
2287 }
2288 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002289 moves_.Add(move);
2290 }
2291
2292 MoveOperands* MoveOperandsAt(size_t index) const {
2293 return moves_.Get(index);
2294 }
2295
2296 size_t NumMoves() const { return moves_.Size(); }
2297
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002298 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002299
2300 private:
2301 GrowableArray<MoveOperands*> moves_;
2302
2303 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
2304};
2305
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002306class HGraphVisitor : public ValueObject {
2307 public:
Dave Allison20dfc792014-06-16 20:44:29 -07002308 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
2309 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002310
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002311 virtual void VisitInstruction(HInstruction* instruction) { UNUSED(instruction); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002312 virtual void VisitBasicBlock(HBasicBlock* block);
2313
Roland Levillain633021e2014-10-01 14:12:25 +01002314 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002315 void VisitInsertionOrder();
2316
Roland Levillain633021e2014-10-01 14:12:25 +01002317 // Visit the graph following dominator tree reverse post-order.
2318 void VisitReversePostOrder();
2319
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002320 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002321
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002322 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002323#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002324 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
2325
2326 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
2327
2328#undef DECLARE_VISIT_INSTRUCTION
2329
2330 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07002331 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002332
2333 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
2334};
2335
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002336class HGraphDelegateVisitor : public HGraphVisitor {
2337 public:
2338 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
2339 virtual ~HGraphDelegateVisitor() {}
2340
2341 // Visit functions that delegate to to super class.
2342#define DECLARE_VISIT_INSTRUCTION(name, super) \
2343 virtual void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
2344
2345 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
2346
2347#undef DECLARE_VISIT_INSTRUCTION
2348
2349 private:
2350 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
2351};
2352
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002353class HInsertionOrderIterator : public ValueObject {
2354 public:
2355 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
2356
2357 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
2358 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
2359 void Advance() { ++index_; }
2360
2361 private:
2362 const HGraph& graph_;
2363 size_t index_;
2364
2365 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
2366};
2367
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002368class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002369 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002370 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002371
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002372 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
2373 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002374 void Advance() { ++index_; }
2375
2376 private:
2377 const HGraph& graph_;
2378 size_t index_;
2379
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002380 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002381};
2382
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002383class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002384 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002385 explicit HPostOrderIterator(const HGraph& graph)
2386 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002387
2388 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002389 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002390 void Advance() { --index_; }
2391
2392 private:
2393 const HGraph& graph_;
2394 size_t index_;
2395
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002396 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002397};
2398
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002399} // namespace art
2400
2401#endif // ART_COMPILER_OPTIMIZING_NODES_H_