blob: 5e2dec1623ba8cb86f6bd6734633f8777862cfce [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"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000023#include "utils/allocation.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 Geoffrayddb311f2014-05-16 09:28:54 +010035class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000036class LocationSummary;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000037
38static const int kDefaultNumberOfBlocks = 8;
39static const int kDefaultNumberOfSuccessors = 2;
40static const int kDefaultNumberOfPredecessors = 2;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010041static const int kDefaultNumberOfDominatedBlocks = 1;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000042static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000043
Dave Allison20dfc792014-06-16 20:44:29 -070044enum IfCondition {
45 kCondEQ,
46 kCondNE,
47 kCondLT,
48 kCondLE,
49 kCondGT,
50 kCondGE,
51};
52
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010053class HInstructionList {
54 public:
55 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
56
57 void AddInstruction(HInstruction* instruction);
58 void RemoveInstruction(HInstruction* instruction);
59
Roland Levillain6b469232014-09-25 10:10:38 +010060 // Return true if this list contains `instruction`.
61 bool Contains(HInstruction* instruction) const;
62
Roland Levillainccc07a92014-09-16 14:48:16 +010063 // Return true if `instruction1` is found before `instruction2` in
64 // this instruction list and false otherwise. Abort if none
65 // of these instructions is found.
66 bool FoundBefore(const HInstruction* instruction1,
67 const HInstruction* instruction2) const;
68
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010069 private:
70 HInstruction* first_instruction_;
71 HInstruction* last_instruction_;
72
73 friend class HBasicBlock;
74 friend class HInstructionIterator;
Nicolas Geoffray804d0932014-05-02 08:46:00 +010075 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010076
77 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
78};
79
Nicolas Geoffray818f2102014-02-18 16:43:35 +000080// Control-flow graph of a method. Contains a list of basic blocks.
81class HGraph : public ArenaObject {
82 public:
83 explicit HGraph(ArenaAllocator* arena)
84 : arena_(arena),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000085 blocks_(arena, kDefaultNumberOfBlocks),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +010086 reverse_post_order_(arena, kDefaultNumberOfBlocks),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +010087 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +010088 number_of_vregs_(0),
89 number_of_in_vregs_(0),
Nicolas Geoffraye5038322014-07-04 09:41:32 +010090 number_of_temporaries_(0),
Dave Allison20dfc792014-06-16 20:44:29 -070091 current_instruction_id_(0) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +000092
Nicolas Geoffray787c3072014-03-17 10:20:19 +000093 ArenaAllocator* GetArena() const { return arena_; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +010094 const GrowableArray<HBasicBlock*>& GetBlocks() const { return blocks_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +000095
Nicolas Geoffray787c3072014-03-17 10:20:19 +000096 HBasicBlock* GetEntryBlock() const { return entry_block_; }
97 HBasicBlock* GetExitBlock() const { return exit_block_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000098
Nicolas Geoffray787c3072014-03-17 10:20:19 +000099 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
100 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000101
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000102 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100103
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000104 void BuildDominatorTree();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100105 void TransformToSSA();
106 void SimplifyCFG();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000107
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100108 // Find all natural loops in this graph. Aborts computation and returns false
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100109 // if one loop is not natural, that is the header does not dominate the back
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100110 // edge.
111 bool FindNaturalLoops() const;
112
113 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
114 void SimplifyLoop(HBasicBlock* header);
115
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000116 int GetNextInstructionId() {
117 return current_instruction_id_++;
118 }
119
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100120 uint16_t GetMaximumNumberOfOutVRegs() const {
121 return maximum_number_of_out_vregs_;
122 }
123
124 void UpdateMaximumNumberOfOutVRegs(uint16_t new_value) {
125 maximum_number_of_out_vregs_ = std::max(new_value, maximum_number_of_out_vregs_);
126 }
127
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100128 void UpdateNumberOfTemporaries(size_t count) {
129 number_of_temporaries_ = std::max(count, number_of_temporaries_);
130 }
131
132 size_t GetNumberOfTemporaries() const {
133 return number_of_temporaries_;
134 }
135
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100136 void SetNumberOfVRegs(uint16_t number_of_vregs) {
137 number_of_vregs_ = number_of_vregs;
138 }
139
140 uint16_t GetNumberOfVRegs() const {
141 return number_of_vregs_;
142 }
143
144 void SetNumberOfInVRegs(uint16_t value) {
145 number_of_in_vregs_ = value;
146 }
147
148 uint16_t GetNumberOfInVRegs() const {
149 return number_of_in_vregs_;
150 }
151
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100152 uint16_t GetNumberOfLocalVRegs() const {
153 return number_of_vregs_ - number_of_in_vregs_;
154 }
155
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100156 const GrowableArray<HBasicBlock*>& GetReversePostOrder() const {
157 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100158 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100159
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000160 private:
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000161 HBasicBlock* FindCommonDominator(HBasicBlock* first, HBasicBlock* second) const;
162 void VisitBlockForDominatorTree(HBasicBlock* block,
163 HBasicBlock* predecessor,
164 GrowableArray<size_t>* visits);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100165 void FindBackEdges(ArenaBitVector* visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000166 void VisitBlockForBackEdges(HBasicBlock* block,
167 ArenaBitVector* visited,
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100168 ArenaBitVector* visiting);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000169 void RemoveDeadBlocks(const ArenaBitVector& visited) const;
170
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000171 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000172
173 // List of blocks in insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000174 GrowableArray<HBasicBlock*> blocks_;
175
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100176 // List of blocks to perform a reverse post order tree traversal.
177 GrowableArray<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000178
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000179 HBasicBlock* entry_block_;
180 HBasicBlock* exit_block_;
181
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100182 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100183 uint16_t maximum_number_of_out_vregs_;
184
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100185 // The number of virtual registers in this method. Contains the parameters.
186 uint16_t number_of_vregs_;
187
188 // The number of virtual registers used by parameters of this method.
189 uint16_t number_of_in_vregs_;
190
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100191 // The number of temporaries that will be needed for the baseline compiler.
192 size_t number_of_temporaries_;
193
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000194 // The current id to assign to a newly added instruction. See HInstruction.id_.
195 int current_instruction_id_;
196
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000197 DISALLOW_COPY_AND_ASSIGN(HGraph);
198};
199
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000200class HLoopInformation : public ArenaObject {
201 public:
202 HLoopInformation(HBasicBlock* header, HGraph* graph)
203 : header_(header),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100204 back_edges_(graph->GetArena(), kDefaultNumberOfBackEdges),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100205 // Make bit vector growable, as the number of blocks may change.
206 blocks_(graph->GetArena(), graph->GetBlocks().Size(), true) {}
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100207
208 HBasicBlock* GetHeader() const {
209 return header_;
210 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000211
212 void AddBackEdge(HBasicBlock* back_edge) {
213 back_edges_.Add(back_edge);
214 }
215
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100216 void RemoveBackEdge(HBasicBlock* back_edge) {
217 back_edges_.Delete(back_edge);
218 }
219
220 bool IsBackEdge(HBasicBlock* block) {
221 for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) {
222 if (back_edges_.Get(i) == block) return true;
223 }
224 return false;
225 }
226
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000227 int NumberOfBackEdges() const {
228 return back_edges_.Size();
229 }
230
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100231 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100232
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100233 const GrowableArray<HBasicBlock*>& GetBackEdges() const {
234 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100235 }
236
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100237 void ClearBackEdges() {
238 back_edges_.Reset();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100239 }
240
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100241 // Find blocks that are part of this loop. Returns whether the loop is a natural loop,
242 // that is the header dominates the back edge.
243 bool Populate();
244
245 // Returns whether this loop information contains `block`.
246 // Note that this loop information *must* be populated before entering this function.
247 bool Contains(const HBasicBlock& block) const;
248
249 // Returns whether this loop information is an inner loop of `other`.
250 // Note that `other` *must* be populated before entering this function.
251 bool IsIn(const HLoopInformation& other) const;
252
253 const ArenaBitVector& GetBlocks() const { return blocks_; }
254
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000255 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100256 // Internal recursive implementation of `Populate`.
257 void PopulateRecursive(HBasicBlock* block);
258
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000259 HBasicBlock* header_;
260 GrowableArray<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100261 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000262
263 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
264};
265
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100266static constexpr size_t kNoLifetime = -1;
267
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000268// A block in a method. Contains the list of instructions represented
269// as a double linked list. Each block knows its predecessors and
270// successors.
271class HBasicBlock : public ArenaObject {
272 public:
273 explicit HBasicBlock(HGraph* graph)
274 : graph_(graph),
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000275 predecessors_(graph->GetArena(), kDefaultNumberOfPredecessors),
276 successors_(graph->GetArena(), kDefaultNumberOfSuccessors),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000277 loop_information_(nullptr),
278 dominator_(nullptr),
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100279 dominated_blocks_(graph->GetArena(), kDefaultNumberOfDominatedBlocks),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100280 block_id_(-1),
281 lifetime_start_(kNoLifetime),
282 lifetime_end_(kNoLifetime) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000283
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100284 const GrowableArray<HBasicBlock*>& GetPredecessors() const {
285 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000286 }
287
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100288 const GrowableArray<HBasicBlock*>& GetSuccessors() const {
289 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000290 }
291
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100292 const GrowableArray<HBasicBlock*>& GetDominatedBlocks() const {
293 return dominated_blocks_;
294 }
295
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000296 void AddBackEdge(HBasicBlock* back_edge) {
297 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000298 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000299 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100300 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000301 loop_information_->AddBackEdge(back_edge);
302 }
303
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000304 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000305
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000306 int GetBlockId() const { return block_id_; }
307 void SetBlockId(int id) { block_id_ = id; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000308
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000309 HBasicBlock* GetDominator() const { return dominator_; }
310 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100311 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.Add(block); }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000312
313 int NumberOfBackEdges() const {
314 return loop_information_ == nullptr
315 ? 0
316 : loop_information_->NumberOfBackEdges();
317 }
318
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100319 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
320 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100321 const HInstructionList& GetInstructions() const { return instructions_; }
322 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100323 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000324
325 void AddSuccessor(HBasicBlock* block) {
326 successors_.Add(block);
327 block->predecessors_.Add(this);
328 }
329
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100330 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
331 size_t successor_index = GetSuccessorIndexOf(existing);
332 DCHECK_NE(successor_index, static_cast<size_t>(-1));
333 existing->RemovePredecessor(this);
334 new_block->predecessors_.Add(this);
335 successors_.Put(successor_index, new_block);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000336 }
337
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100338 void RemovePredecessor(HBasicBlock* block) {
339 predecessors_.Delete(block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100340 }
341
342 void ClearAllPredecessors() {
343 predecessors_.Reset();
344 }
345
346 void AddPredecessor(HBasicBlock* block) {
347 predecessors_.Add(block);
348 block->successors_.Add(this);
349 }
350
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100351 void SwapPredecessors() {
Nicolas Geoffrayc83d4412014-09-18 16:46:20 +0100352 DCHECK_EQ(predecessors_.Size(), 2u);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100353 HBasicBlock* temp = predecessors_.Get(0);
354 predecessors_.Put(0, predecessors_.Get(1));
355 predecessors_.Put(1, temp);
356 }
357
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100358 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) {
359 for (size_t i = 0, e = predecessors_.Size(); i < e; ++i) {
360 if (predecessors_.Get(i) == predecessor) {
361 return i;
362 }
363 }
364 return -1;
365 }
366
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100367 size_t GetSuccessorIndexOf(HBasicBlock* successor) {
368 for (size_t i = 0, e = successors_.Size(); i < e; ++i) {
369 if (successors_.Get(i) == successor) {
370 return i;
371 }
372 }
373 return -1;
374 }
375
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000376 void AddInstruction(HInstruction* instruction);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100377 void RemoveInstruction(HInstruction* instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100378 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +0100379 // Replace instruction `initial` with `replacement` within this block.
380 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
381 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100382 void AddPhi(HPhi* phi);
383 void RemovePhi(HPhi* phi);
384
385 bool IsLoopHeader() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100386 return (loop_information_ != nullptr) && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100387 }
388
Roland Levillain6b879dd2014-09-22 17:13:44 +0100389 bool IsLoopPreHeaderFirstPredecessor() const {
390 DCHECK(IsLoopHeader());
391 DCHECK(!GetPredecessors().IsEmpty());
392 return GetPredecessors().Get(0) == GetLoopInformation()->GetPreHeader();
393 }
394
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100395 HLoopInformation* GetLoopInformation() const {
396 return loop_information_;
397 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000398
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100399 // Set the loop_information_ on this block. This method overrides the current
400 // loop_information if it is an outer loop of the passed loop information.
401 void SetInLoop(HLoopInformation* info) {
402 if (IsLoopHeader()) {
403 // Nothing to do. This just means `info` is an outer loop.
404 } else if (loop_information_ == nullptr) {
405 loop_information_ = info;
406 } else if (loop_information_->Contains(*info->GetHeader())) {
407 // Block is currently part of an outer loop. Make it part of this inner loop.
408 // Note that a non loop header having a loop information means this loop information
409 // has already been populated
410 loop_information_ = info;
411 } else {
412 // Block is part of an inner loop. Do not update the loop information.
413 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
414 // at this point, because this method is being called while populating `info`.
415 }
416 }
417
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100418 bool IsInLoop() const { return loop_information_ != nullptr; }
419
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100420 // Returns wheter this block dominates the blocked passed as parameter.
421 bool Dominates(HBasicBlock* block) const;
422
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100423 size_t GetLifetimeStart() const { return lifetime_start_; }
424 size_t GetLifetimeEnd() const { return lifetime_end_; }
425
426 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
427 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
428
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000429 private:
430 HGraph* const graph_;
431 GrowableArray<HBasicBlock*> predecessors_;
432 GrowableArray<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100433 HInstructionList instructions_;
434 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000435 HLoopInformation* loop_information_;
436 HBasicBlock* dominator_;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100437 GrowableArray<HBasicBlock*> dominated_blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000438 int block_id_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100439 size_t lifetime_start_;
440 size_t lifetime_end_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000441
442 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
443};
444
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100445#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000446 M(Add) \
Dave Allison20dfc792014-06-16 20:44:29 -0700447 M(Condition) \
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000448 M(Equal) \
Dave Allison20dfc792014-06-16 20:44:29 -0700449 M(NotEqual) \
450 M(LessThan) \
451 M(LessThanOrEqual) \
452 M(GreaterThan) \
453 M(GreaterThanOrEqual) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000454 M(Exit) \
455 M(Goto) \
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000456 M(If) \
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000457 M(IntConstant) \
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000458 M(InvokeStatic) \
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100459 M(InvokeVirtual) \
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000460 M(LoadLocal) \
461 M(Local) \
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100462 M(LongConstant) \
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +0100463 M(NewInstance) \
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100464 M(Not) \
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100465 M(ParameterValue) \
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100466 M(ParallelMove) \
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100467 M(Phi) \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000468 M(Return) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000469 M(ReturnVoid) \
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000470 M(StoreLocal) \
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100471 M(Sub) \
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100472 M(Compare) \
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100473 M(InstanceFieldGet) \
474 M(InstanceFieldSet) \
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100475 M(ArrayGet) \
476 M(ArraySet) \
477 M(ArrayLength) \
478 M(BoundsCheck) \
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100479 M(NullCheck) \
480 M(Temporary) \
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000481 M(SuspendCheck) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000482
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100483#define FOR_EACH_INSTRUCTION(M) \
484 FOR_EACH_CONCRETE_INSTRUCTION(M) \
Roland Levillainccc07a92014-09-16 14:48:16 +0100485 M(Constant) \
486 M(BinaryOperation)
Dave Allison20dfc792014-06-16 20:44:29 -0700487
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000488#define FORWARD_DECLARATION(type) class H##type;
489FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
490#undef FORWARD_DECLARATION
491
Roland Levillainccc07a92014-09-16 14:48:16 +0100492#define DECLARE_INSTRUCTION(type) \
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100493 virtual InstructionKind GetKind() const { return k##type; } \
Roland Levillainccc07a92014-09-16 14:48:16 +0100494 virtual const char* DebugName() const { return #type; } \
495 virtual const H##type* As##type() const OVERRIDE { return this; } \
496 virtual H##type* As##type() OVERRIDE { return this; } \
497 virtual bool InstructionTypeEquals(HInstruction* other) const { \
498 return other->Is##type(); \
499 } \
500 virtual void Accept(HGraphVisitor* visitor)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000501
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100502template <typename T>
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000503class HUseListNode : public ArenaObject {
504 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100505 HUseListNode(T* user, size_t index, HUseListNode* tail)
Dave Allison20dfc792014-06-16 20:44:29 -0700506 : user_(user), index_(index), tail_(tail) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000507
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000508 HUseListNode* GetTail() const { return tail_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100509 T* GetUser() const { return user_; }
510 size_t GetIndex() const { return index_; }
511
512 void SetTail(HUseListNode<T>* node) { tail_ = node; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000513
514 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100515 T* const user_;
516 const size_t index_;
517 HUseListNode<T>* tail_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000518
519 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
520};
521
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100522// Represents the side effects an instruction may have.
523class SideEffects : public ValueObject {
524 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100525 SideEffects() : flags_(0) {}
526
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100527 static SideEffects None() {
528 return SideEffects(0);
529 }
530
531 static SideEffects All() {
532 return SideEffects(ChangesSomething().flags_ | DependsOnSomething().flags_);
533 }
534
535 static SideEffects ChangesSomething() {
536 return SideEffects((1 << kFlagChangesCount) - 1);
537 }
538
539 static SideEffects DependsOnSomething() {
540 int count = kFlagDependsOnCount - kFlagChangesCount;
541 return SideEffects(((1 << count) - 1) << kFlagChangesCount);
542 }
543
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100544 SideEffects Union(SideEffects other) const {
545 return SideEffects(flags_ | other.flags_);
546 }
547
Roland Levillain72bceff2014-09-15 18:29:00 +0100548 bool HasSideEffects() const {
549 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
550 return (flags_ & all_bits_set) != 0;
551 }
552
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100553 bool HasAllSideEffects() const {
554 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
555 return all_bits_set == (flags_ & all_bits_set);
556 }
557
558 bool DependsOn(SideEffects other) const {
559 size_t depends_flags = other.ComputeDependsFlags();
560 return (flags_ & depends_flags) != 0;
561 }
562
563 bool HasDependencies() const {
564 int count = kFlagDependsOnCount - kFlagChangesCount;
565 size_t all_bits_set = (1 << count) - 1;
566 return ((flags_ >> kFlagChangesCount) & all_bits_set) != 0;
567 }
568
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100569 private:
570 static constexpr int kFlagChangesSomething = 0;
571 static constexpr int kFlagChangesCount = kFlagChangesSomething + 1;
572
573 static constexpr int kFlagDependsOnSomething = kFlagChangesCount;
574 static constexpr int kFlagDependsOnCount = kFlagDependsOnSomething + 1;
575
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100576 explicit SideEffects(size_t flags) : flags_(flags) {}
577
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100578 size_t ComputeDependsFlags() const {
579 return flags_ << kFlagChangesCount;
580 }
581
582 size_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100583};
584
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000585class HInstruction : public ArenaObject {
586 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100587 explicit HInstruction(SideEffects side_effects)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000588 : previous_(nullptr),
589 next_(nullptr),
590 block_(nullptr),
591 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100592 ssa_index_(-1),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000593 uses_(nullptr),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100594 env_uses_(nullptr),
595 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100596 locations_(nullptr),
597 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100598 lifetime_position_(kNoLifetime),
599 side_effects_(side_effects) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000600
Dave Allison20dfc792014-06-16 20:44:29 -0700601 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000602
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100603#define DECLARE_KIND(type) k##type,
604 enum InstructionKind {
605 FOR_EACH_INSTRUCTION(DECLARE_KIND)
606 };
607#undef DECLARE_KIND
608
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000609 HInstruction* GetNext() const { return next_; }
610 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000611
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000612 HBasicBlock* GetBlock() const { return block_; }
613 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100614 bool IsInBlock() const { return block_ != nullptr; }
615 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +0100616 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000617
Roland Levillain6b879dd2014-09-22 17:13:44 +0100618 virtual size_t InputCount() const = 0;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100619 virtual HInstruction* InputAt(size_t i) const = 0;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000620
621 virtual void Accept(HGraphVisitor* visitor) = 0;
622 virtual const char* DebugName() const = 0;
623
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100624 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100625 virtual void SetRawInputAt(size_t index, HInstruction* input) = 0;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100626
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100627 virtual bool NeedsEnvironment() const { return false; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100628 virtual bool IsControlFlow() const { return false; }
Roland Levillain72bceff2014-09-15 18:29:00 +0100629 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100630
631 void AddUseAt(HInstruction* user, size_t index) {
632 uses_ = new (block_->GetGraph()->GetArena()) HUseListNode<HInstruction>(user, index, uses_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000633 }
634
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100635 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +0100636 DCHECK(user != nullptr);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100637 env_uses_ = new (block_->GetGraph()->GetArena()) HUseListNode<HEnvironment>(
638 user, index, env_uses_);
639 }
640
641 void RemoveUser(HInstruction* user, size_t index);
Nicolas Geoffray724c9632014-09-22 12:27:27 +0100642 void RemoveEnvironmentUser(HEnvironment* user, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100643
644 HUseListNode<HInstruction>* GetUses() const { return uses_; }
645 HUseListNode<HEnvironment>* GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000646
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100647 bool HasUses() const { return uses_ != nullptr || env_uses_ != nullptr; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100648 bool HasEnvironmentUses() const { return env_uses_ != nullptr; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000649
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100650 size_t NumberOfUses() const {
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100651 // TODO: Optimize this method if it is used outside of the HGraphVisualizer.
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100652 size_t result = 0;
653 HUseListNode<HInstruction>* current = uses_;
654 while (current != nullptr) {
655 current = current->GetTail();
656 ++result;
657 }
658 return result;
659 }
660
Roland Levillainccc07a92014-09-16 14:48:16 +0100661 // Does this instruction dominate `other_instruction`? Aborts if
662 // this instruction and `other_instruction` are both phis.
663 bool Dominates(HInstruction* other_instruction) const;
664
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000665 int GetId() const { return id_; }
666 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000667
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100668 int GetSsaIndex() const { return ssa_index_; }
669 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
670 bool HasSsaIndex() const { return ssa_index_ != -1; }
671
672 bool HasEnvironment() const { return environment_ != nullptr; }
673 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100674 void SetEnvironment(HEnvironment* environment) { environment_ = environment; }
675
Nicolas Geoffray39468442014-09-02 15:17:15 +0100676 // Returns the number of entries in the environment. Typically, that is the
677 // number of dex registers in a method. It could be more in case of inlining.
678 size_t EnvironmentSize() const;
679
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000680 LocationSummary* GetLocations() const { return locations_; }
681 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000682
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100683 void ReplaceWith(HInstruction* instruction);
684
Dave Allison20dfc792014-06-16 20:44:29 -0700685 bool HasOnlyOneUse() const {
686 return uses_ != nullptr && uses_->GetTail() == nullptr;
687 }
688
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000689#define INSTRUCTION_TYPE_CHECK(type) \
Roland Levillainccc07a92014-09-16 14:48:16 +0100690 bool Is##type() const { return (As##type() != nullptr); } \
691 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000692 virtual H##type* As##type() { return nullptr; }
693
694 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
695#undef INSTRUCTION_TYPE_CHECK
696
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100697 // Returns whether the instruction can be moved within the graph.
698 virtual bool CanBeMoved() const { return false; }
699
700 // Returns whether the two instructions are of the same kind.
701 virtual bool InstructionTypeEquals(HInstruction* other) const { return false; }
702
703 // Returns whether any data encoded in the two instructions is equal.
704 // This method does not look at the inputs. Both instructions must be
705 // of the same type, otherwise the method has undefined behavior.
706 virtual bool InstructionDataEquals(HInstruction* other) const { return false; }
707
708 // Returns whether two instructions are equal, that is:
709 // 1) They have the same type and contain the same data,
710 // 2) Their inputs are identical.
711 bool Equals(HInstruction* other) const;
712
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100713 virtual InstructionKind GetKind() const = 0;
714
715 virtual size_t ComputeHashCode() const {
716 size_t result = GetKind();
717 for (size_t i = 0, e = InputCount(); i < e; ++i) {
718 result = (result * 31) + InputAt(i)->GetId();
719 }
720 return result;
721 }
722
723 SideEffects GetSideEffects() const { return side_effects_; }
724
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100725 size_t GetLifetimePosition() const { return lifetime_position_; }
726 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
727 LiveInterval* GetLiveInterval() const { return live_interval_; }
728 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
729 bool HasLiveInterval() const { return live_interval_ != nullptr; }
730
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000731 private:
732 HInstruction* previous_;
733 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000734 HBasicBlock* block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000735
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000736 // An instruction gets an id when it is added to the graph.
737 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +0100738 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000739 int id_;
740
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100741 // When doing liveness analysis, instructions that have uses get an SSA index.
742 int ssa_index_;
743
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100744 // List of instructions that have this instruction as input.
745 HUseListNode<HInstruction>* uses_;
746
747 // List of environments that contain this instruction.
748 HUseListNode<HEnvironment>* env_uses_;
749
Nicolas Geoffray39468442014-09-02 15:17:15 +0100750 // The environment associated with this instruction. Not null if the instruction
751 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100752 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000753
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000754 // Set by the code generator.
755 LocationSummary* locations_;
756
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100757 // Set by the liveness analysis.
758 LiveInterval* live_interval_;
759
760 // Set by the liveness analysis, this is the position in a linear
761 // order of blocks where this instruction's live interval start.
762 size_t lifetime_position_;
763
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100764 const SideEffects side_effects_;
765
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000766 friend class HBasicBlock;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100767 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000768
769 DISALLOW_COPY_AND_ASSIGN(HInstruction);
770};
771
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100772template<typename T>
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000773class HUseIterator : public ValueObject {
774 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100775 explicit HUseIterator(HUseListNode<T>* uses) : current_(uses) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000776
777 bool Done() const { return current_ == nullptr; }
778
779 void Advance() {
780 DCHECK(!Done());
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000781 current_ = current_->GetTail();
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000782 }
783
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100784 HUseListNode<T>* Current() const {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000785 DCHECK(!Done());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100786 return current_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000787 }
788
789 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100790 HUseListNode<T>* current_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000791
792 friend class HValue;
793};
794
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100795// A HEnvironment object contains the values of virtual registers at a given location.
796class HEnvironment : public ArenaObject {
797 public:
798 HEnvironment(ArenaAllocator* arena, size_t number_of_vregs) : vregs_(arena, number_of_vregs) {
799 vregs_.SetSize(number_of_vregs);
800 for (size_t i = 0; i < number_of_vregs; i++) {
801 vregs_.Put(i, nullptr);
802 }
803 }
804
805 void Populate(const GrowableArray<HInstruction*>& env) {
806 for (size_t i = 0; i < env.Size(); i++) {
807 HInstruction* instruction = env.Get(i);
808 vregs_.Put(i, instruction);
809 if (instruction != nullptr) {
810 instruction->AddEnvUseAt(this, i);
811 }
812 }
813 }
814
815 void SetRawEnvAt(size_t index, HInstruction* instruction) {
816 vregs_.Put(index, instruction);
817 }
818
Nicolas Geoffray39468442014-09-02 15:17:15 +0100819 HInstruction* GetInstructionAt(size_t index) const {
820 return vregs_.Get(index);
821 }
822
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100823 GrowableArray<HInstruction*>* GetVRegs() {
824 return &vregs_;
825 }
826
Nicolas Geoffray39468442014-09-02 15:17:15 +0100827 size_t Size() const { return vregs_.Size(); }
828
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100829 private:
830 GrowableArray<HInstruction*> vregs_;
831
832 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
833};
834
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000835class HInputIterator : public ValueObject {
836 public:
Dave Allison20dfc792014-06-16 20:44:29 -0700837 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000838
839 bool Done() const { return index_ == instruction_->InputCount(); }
840 HInstruction* Current() const { return instruction_->InputAt(index_); }
841 void Advance() { index_++; }
842
843 private:
844 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100845 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000846
847 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
848};
849
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000850class HInstructionIterator : public ValueObject {
851 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100852 explicit HInstructionIterator(const HInstructionList& instructions)
853 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000854 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000855 }
856
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000857 bool Done() const { return instruction_ == nullptr; }
858 HInstruction* Current() const { return instruction_; }
859 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000860 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000861 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000862 }
863
864 private:
865 HInstruction* instruction_;
866 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100867
868 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000869};
870
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100871class HBackwardInstructionIterator : public ValueObject {
872 public:
873 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
874 : instruction_(instructions.last_instruction_) {
875 next_ = Done() ? nullptr : instruction_->GetPrevious();
876 }
877
878 bool Done() const { return instruction_ == nullptr; }
879 HInstruction* Current() const { return instruction_; }
880 void Advance() {
881 instruction_ = next_;
882 next_ = Done() ? nullptr : instruction_->GetPrevious();
883 }
884
885 private:
886 HInstruction* instruction_;
887 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100888
889 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100890};
891
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000892// An embedded container with N elements of type T. Used (with partial
893// specialization for N=0) because embedded arrays cannot have size 0.
894template<typename T, intptr_t N>
895class EmbeddedArray {
896 public:
Dave Allison20dfc792014-06-16 20:44:29 -0700897 EmbeddedArray() : elements_() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000898
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000899 intptr_t GetLength() const { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000900
901 const T& operator[](intptr_t i) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000902 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000903 return elements_[i];
904 }
905
906 T& operator[](intptr_t i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000907 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000908 return elements_[i];
909 }
910
911 const T& At(intptr_t i) const {
912 return (*this)[i];
913 }
914
915 void SetAt(intptr_t i, const T& val) {
916 (*this)[i] = val;
917 }
918
919 private:
920 T elements_[N];
921};
922
923template<typename T>
924class EmbeddedArray<T, 0> {
925 public:
926 intptr_t length() const { return 0; }
927 const T& operator[](intptr_t i) const {
928 LOG(FATAL) << "Unreachable";
929 static T sentinel = 0;
930 return sentinel;
931 }
932 T& operator[](intptr_t i) {
933 LOG(FATAL) << "Unreachable";
934 static T sentinel = 0;
935 return sentinel;
936 }
937};
938
939template<intptr_t N>
940class HTemplateInstruction: public HInstruction {
941 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100942 HTemplateInstruction<N>(SideEffects side_effects)
943 : HInstruction(side_effects), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -0700944 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000945
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100946 virtual size_t InputCount() const { return N; }
947 virtual HInstruction* InputAt(size_t i) const { return inputs_[i]; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000948
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000949 protected:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100950 virtual void SetRawInputAt(size_t i, HInstruction* instruction) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000951 inputs_[i] = instruction;
952 }
953
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000954 private:
955 EmbeddedArray<HInstruction*, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100956
957 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000958};
959
Dave Allison20dfc792014-06-16 20:44:29 -0700960template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100961class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -0700962 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100963 HExpression<N>(Primitive::Type type, SideEffects side_effects)
964 : HTemplateInstruction<N>(side_effects), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -0700965 virtual ~HExpression() {}
966
967 virtual Primitive::Type GetType() const { return type_; }
968
969 private:
970 const Primitive::Type type_;
971};
972
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000973// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
974// instruction that branches to the exit block.
975class HReturnVoid : public HTemplateInstruction<0> {
976 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100977 HReturnVoid() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100978
979 virtual bool IsControlFlow() const { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000980
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100981 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000982
983 private:
984 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
985};
986
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000987// Represents dex's RETURN opcodes. A HReturn is a control flow
988// instruction that branches to the exit block.
989class HReturn : public HTemplateInstruction<1> {
990 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100991 explicit HReturn(HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000992 SetRawInputAt(0, value);
993 }
994
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100995 virtual bool IsControlFlow() const { return true; }
996
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100997 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000998
999 private:
1000 DISALLOW_COPY_AND_ASSIGN(HReturn);
1001};
1002
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001003// The exit instruction is the only instruction of the exit block.
1004// Instructions aborting the method (HTrow and HReturn) must branch to the
1005// exit block.
1006class HExit : public HTemplateInstruction<0> {
1007 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001008 HExit() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001009
1010 virtual bool IsControlFlow() const { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001011
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001012 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001013
1014 private:
1015 DISALLOW_COPY_AND_ASSIGN(HExit);
1016};
1017
1018// Jumps from one block to another.
1019class HGoto : public HTemplateInstruction<0> {
1020 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001021 HGoto() : HTemplateInstruction(SideEffects::None()) {}
1022
1023 virtual bool IsControlFlow() const { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001024
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001025 HBasicBlock* GetSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001026 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001027 }
1028
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001029 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001030
1031 private:
1032 DISALLOW_COPY_AND_ASSIGN(HGoto);
1033};
1034
Dave Allison20dfc792014-06-16 20:44:29 -07001035
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001036// Conditional branch. A block ending with an HIf instruction must have
1037// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001038class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001039 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001040 explicit HIf(HInstruction* input) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001041 SetRawInputAt(0, input);
1042 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001043
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001044 virtual bool IsControlFlow() const { return true; }
1045
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001046 HBasicBlock* IfTrueSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001047 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001048 }
1049
1050 HBasicBlock* IfFalseSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001051 return GetBlock()->GetSuccessors().Get(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001052 }
1053
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001054 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001055
Dave Allison20dfc792014-06-16 20:44:29 -07001056 virtual bool IsIfInstruction() const { return true; }
1057
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001058 private:
1059 DISALLOW_COPY_AND_ASSIGN(HIf);
1060};
1061
Dave Allison20dfc792014-06-16 20:44:29 -07001062class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001063 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001064 HBinaryOperation(Primitive::Type result_type,
1065 HInstruction* left,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001066 HInstruction* right) : HExpression(result_type, SideEffects::None()) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001067 SetRawInputAt(0, left);
1068 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001069 }
1070
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001071 HInstruction* GetLeft() const { return InputAt(0); }
1072 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07001073 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001074
1075 virtual bool IsCommutative() { return false; }
1076
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001077 virtual bool CanBeMoved() const { return true; }
1078 virtual bool InstructionDataEquals(HInstruction* other) const { return true; }
1079
Roland Levillain556c3d12014-09-18 15:25:07 +01001080 // Try to statically evaluate `operation` and return an HConstant
1081 // containing the result of this evaluation. If `operation` cannot
1082 // be evaluated as a constant, return nullptr.
1083 HConstant* TryStaticEvaluation(ArenaAllocator* allocator) const;
1084
1085 // Apply this operation to `x` and `y`.
1086 virtual int32_t Evaluate(int32_t x, int32_t y) const = 0;
1087 virtual int64_t Evaluate(int64_t x, int64_t y) const = 0;
1088
Roland Levillainccc07a92014-09-16 14:48:16 +01001089 DECLARE_INSTRUCTION(BinaryOperation);
1090
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001091 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001092 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
1093};
1094
Dave Allison20dfc792014-06-16 20:44:29 -07001095class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001096 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001097 HCondition(HInstruction* first, HInstruction* second)
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001098 : HBinaryOperation(Primitive::kPrimBoolean, first, second) {}
1099
1100 virtual bool IsCommutative() { return true; }
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001101
1102 // For register allocation purposes, returns whether this instruction needs to be
1103 // materialized (that is, not just be in the processor flags).
Dave Allison20dfc792014-06-16 20:44:29 -07001104 bool NeedsMaterialization() const;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001105
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001106 // For code generation purposes, returns whether this instruction is just before
1107 // `if_`, and disregard moves in between.
1108 bool IsBeforeWhenDisregardMoves(HIf* if_) const;
1109
Dave Allison20dfc792014-06-16 20:44:29 -07001110 DECLARE_INSTRUCTION(Condition);
1111
1112 virtual IfCondition GetCondition() const = 0;
1113
1114 private:
1115 DISALLOW_COPY_AND_ASSIGN(HCondition);
1116};
1117
1118// Instruction to check if two inputs are equal to each other.
1119class HEqual : public HCondition {
1120 public:
1121 HEqual(HInstruction* first, HInstruction* second)
1122 : HCondition(first, second) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001123
Roland Levillain556c3d12014-09-18 15:25:07 +01001124 virtual int32_t Evaluate(int32_t x, int32_t y) const { return x == y; }
1125 virtual int64_t Evaluate(int64_t x, int64_t y) const { return x == y; }
1126
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001127 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001128
Dave Allison20dfc792014-06-16 20:44:29 -07001129 virtual IfCondition GetCondition() const {
1130 return kCondEQ;
1131 }
1132
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001133 private:
1134 DISALLOW_COPY_AND_ASSIGN(HEqual);
1135};
1136
Dave Allison20dfc792014-06-16 20:44:29 -07001137class HNotEqual : public HCondition {
1138 public:
1139 HNotEqual(HInstruction* first, HInstruction* second)
1140 : HCondition(first, second) {}
1141
Roland Levillain556c3d12014-09-18 15:25:07 +01001142 virtual int32_t Evaluate(int32_t x, int32_t y) const { return x != y; }
1143 virtual int64_t Evaluate(int64_t x, int64_t y) const { return x != y; }
1144
Dave Allison20dfc792014-06-16 20:44:29 -07001145 DECLARE_INSTRUCTION(NotEqual);
1146
1147 virtual IfCondition GetCondition() const {
1148 return kCondNE;
1149 }
1150
1151 private:
1152 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
1153};
1154
1155class HLessThan : public HCondition {
1156 public:
1157 HLessThan(HInstruction* first, HInstruction* second)
1158 : HCondition(first, second) {}
1159
Roland Levillain556c3d12014-09-18 15:25:07 +01001160 virtual int32_t Evaluate(int32_t x, int32_t y) const { return x < y; }
1161 virtual int64_t Evaluate(int64_t x, int64_t y) const { return x < y; }
1162
Dave Allison20dfc792014-06-16 20:44:29 -07001163 DECLARE_INSTRUCTION(LessThan);
1164
1165 virtual IfCondition GetCondition() const {
1166 return kCondLT;
1167 }
1168
1169 private:
1170 DISALLOW_COPY_AND_ASSIGN(HLessThan);
1171};
1172
1173class HLessThanOrEqual : public HCondition {
1174 public:
1175 HLessThanOrEqual(HInstruction* first, HInstruction* second)
1176 : HCondition(first, second) {}
1177
Roland Levillain556c3d12014-09-18 15:25:07 +01001178 virtual int32_t Evaluate(int32_t x, int32_t y) const { return x <= y; }
1179 virtual int64_t Evaluate(int64_t x, int64_t y) const { return x <= y; }
1180
Dave Allison20dfc792014-06-16 20:44:29 -07001181 DECLARE_INSTRUCTION(LessThanOrEqual);
1182
1183 virtual IfCondition GetCondition() const {
1184 return kCondLE;
1185 }
1186
1187 private:
1188 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
1189};
1190
1191class HGreaterThan : public HCondition {
1192 public:
1193 HGreaterThan(HInstruction* first, HInstruction* second)
1194 : HCondition(first, second) {}
1195
Roland Levillain556c3d12014-09-18 15:25:07 +01001196 virtual int32_t Evaluate(int32_t x, int32_t y) const { return x > y; }
1197 virtual int64_t Evaluate(int64_t x, int64_t y) const { return x > y; }
1198
Dave Allison20dfc792014-06-16 20:44:29 -07001199 DECLARE_INSTRUCTION(GreaterThan);
1200
1201 virtual IfCondition GetCondition() const {
1202 return kCondGT;
1203 }
1204
1205 private:
1206 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
1207};
1208
1209class HGreaterThanOrEqual : public HCondition {
1210 public:
1211 HGreaterThanOrEqual(HInstruction* first, HInstruction* second)
1212 : HCondition(first, second) {}
1213
Roland Levillain556c3d12014-09-18 15:25:07 +01001214 virtual int32_t Evaluate(int32_t x, int32_t y) const { return x >= y; }
1215 virtual int64_t Evaluate(int64_t x, int64_t y) const { return x >= y; }
1216
Dave Allison20dfc792014-06-16 20:44:29 -07001217 DECLARE_INSTRUCTION(GreaterThanOrEqual);
1218
1219 virtual IfCondition GetCondition() const {
1220 return kCondGE;
1221 }
1222
1223 private:
1224 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
1225};
1226
1227
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001228// Instruction to check how two inputs compare to each other.
1229// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
1230class HCompare : public HBinaryOperation {
1231 public:
1232 HCompare(Primitive::Type type, HInstruction* first, HInstruction* second)
1233 : HBinaryOperation(Primitive::kPrimInt, first, second) {
1234 DCHECK_EQ(type, first->GetType());
1235 DCHECK_EQ(type, second->GetType());
1236 }
1237
Roland Levillain556c3d12014-09-18 15:25:07 +01001238 virtual int32_t Evaluate(int32_t x, int32_t y) const {
1239 return
1240 x == y ? 0 :
1241 x > y ? 1 :
1242 -1;
1243 }
1244 virtual int64_t Evaluate(int64_t x, int64_t y) const {
1245 return
1246 x == y ? 0 :
1247 x > y ? 1 :
1248 -1;
1249 }
1250
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001251 DECLARE_INSTRUCTION(Compare);
1252
1253 private:
1254 DISALLOW_COPY_AND_ASSIGN(HCompare);
1255};
1256
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001257// A local in the graph. Corresponds to a Dex register.
1258class HLocal : public HTemplateInstruction<0> {
1259 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001260 explicit HLocal(uint16_t reg_number)
1261 : HTemplateInstruction(SideEffects::None()), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001262
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001263 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001264
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001265 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001266
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001267 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001268 // The Dex register number.
1269 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001270
1271 DISALLOW_COPY_AND_ASSIGN(HLocal);
1272};
1273
1274// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07001275class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001276 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001277 explicit HLoadLocal(HLocal* local, Primitive::Type type)
1278 : HExpression(type, SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001279 SetRawInputAt(0, local);
1280 }
1281
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001282 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1283
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001284 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001285
1286 private:
1287 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
1288};
1289
1290// Store a value in a given local. This instruction has two inputs: the value
1291// and the local.
1292class HStoreLocal : public HTemplateInstruction<2> {
1293 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001294 HStoreLocal(HLocal* local, HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001295 SetRawInputAt(0, local);
1296 SetRawInputAt(1, value);
1297 }
1298
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001299 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1300
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001301 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001302
1303 private:
1304 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
1305};
1306
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001307class HConstant : public HExpression<0> {
1308 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001309 explicit HConstant(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
1310
1311 virtual bool CanBeMoved() const { return true; }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001312
1313 DECLARE_INSTRUCTION(Constant);
1314
1315 private:
1316 DISALLOW_COPY_AND_ASSIGN(HConstant);
1317};
1318
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001319// Constants of the type int. Those can be from Dex instructions, or
1320// synthesized (for example with the if-eqz instruction).
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001321class HIntConstant : public HConstant {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001322 public:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001323 explicit HIntConstant(int32_t value) : HConstant(Primitive::kPrimInt), value_(value) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001324
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001325 int32_t GetValue() const { return value_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001326
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001327 virtual bool InstructionDataEquals(HInstruction* other) const {
1328 return other->AsIntConstant()->value_ == value_;
1329 }
1330
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001331 virtual size_t ComputeHashCode() const { return GetValue(); }
1332
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001333 DECLARE_INSTRUCTION(IntConstant);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001334
1335 private:
1336 const int32_t value_;
1337
1338 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
1339};
1340
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001341class HLongConstant : public HConstant {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001342 public:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001343 explicit HLongConstant(int64_t value) : HConstant(Primitive::kPrimLong), value_(value) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001344
1345 int64_t GetValue() const { return value_; }
1346
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001347 virtual bool InstructionDataEquals(HInstruction* other) const {
1348 return other->AsLongConstant()->value_ == value_;
1349 }
1350
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001351 virtual size_t ComputeHashCode() const { return static_cast<size_t>(GetValue()); }
1352
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001353 DECLARE_INSTRUCTION(LongConstant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001354
1355 private:
1356 const int64_t value_;
1357
1358 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
1359};
1360
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001361class HInvoke : public HInstruction {
1362 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001363 HInvoke(ArenaAllocator* arena,
1364 uint32_t number_of_arguments,
1365 Primitive::Type return_type,
1366 uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001367 : HInstruction(SideEffects::All()),
1368 inputs_(arena, number_of_arguments),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001369 return_type_(return_type),
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001370 dex_pc_(dex_pc) {
1371 inputs_.SetSize(number_of_arguments);
1372 }
1373
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001374 virtual size_t InputCount() const { return inputs_.Size(); }
1375 virtual HInstruction* InputAt(size_t i) const { return inputs_.Get(i); }
1376
1377 // Runtime needs to walk the stack, so Dex -> Dex calls need to
1378 // know their environment.
1379 virtual bool NeedsEnvironment() const { return true; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001380
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001381 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001382 SetRawInputAt(index, argument);
1383 }
1384
1385 virtual void SetRawInputAt(size_t index, HInstruction* input) {
1386 inputs_.Put(index, input);
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001387 }
1388
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001389 virtual Primitive::Type GetType() const { return return_type_; }
1390
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001391 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001392
1393 protected:
1394 GrowableArray<HInstruction*> inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001395 const Primitive::Type return_type_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001396 const uint32_t dex_pc_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001397
1398 private:
1399 DISALLOW_COPY_AND_ASSIGN(HInvoke);
1400};
1401
1402class HInvokeStatic : public HInvoke {
1403 public:
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001404 HInvokeStatic(ArenaAllocator* arena,
1405 uint32_t number_of_arguments,
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001406 Primitive::Type return_type,
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001407 uint32_t dex_pc,
1408 uint32_t index_in_dex_cache)
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001409 : HInvoke(arena, number_of_arguments, return_type, dex_pc),
1410 index_in_dex_cache_(index_in_dex_cache) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001411
1412 uint32_t GetIndexInDexCache() const { return index_in_dex_cache_; }
1413
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001414 DECLARE_INSTRUCTION(InvokeStatic);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001415
1416 private:
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001417 const uint32_t index_in_dex_cache_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001418
1419 DISALLOW_COPY_AND_ASSIGN(HInvokeStatic);
1420};
1421
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001422class HInvokeVirtual : public HInvoke {
1423 public:
1424 HInvokeVirtual(ArenaAllocator* arena,
1425 uint32_t number_of_arguments,
1426 Primitive::Type return_type,
1427 uint32_t dex_pc,
1428 uint32_t vtable_index)
1429 : HInvoke(arena, number_of_arguments, return_type, dex_pc),
1430 vtable_index_(vtable_index) {}
1431
1432 uint32_t GetVTableIndex() const { return vtable_index_; }
1433
1434 DECLARE_INSTRUCTION(InvokeVirtual);
1435
1436 private:
1437 const uint32_t vtable_index_;
1438
1439 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
1440};
1441
Dave Allison20dfc792014-06-16 20:44:29 -07001442class HNewInstance : public HExpression<0> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001443 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001444 HNewInstance(uint32_t dex_pc, uint16_t type_index)
1445 : HExpression(Primitive::kPrimNot, SideEffects::None()),
1446 dex_pc_(dex_pc),
1447 type_index_(type_index) {}
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001448
1449 uint32_t GetDexPc() const { return dex_pc_; }
1450 uint16_t GetTypeIndex() const { return type_index_; }
1451
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001452 // Calls runtime so needs an environment.
1453 virtual bool NeedsEnvironment() const { return true; }
1454
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001455 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001456
1457 private:
1458 const uint32_t dex_pc_;
1459 const uint16_t type_index_;
1460
1461 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
1462};
1463
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001464class HAdd : public HBinaryOperation {
1465 public:
1466 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1467 : HBinaryOperation(result_type, left, right) {}
1468
1469 virtual bool IsCommutative() { return true; }
1470
Roland Levillain556c3d12014-09-18 15:25:07 +01001471 virtual int32_t Evaluate(int32_t x, int32_t y) const { return x + y; }
1472 virtual int64_t Evaluate(int64_t x, int64_t y) const { return x + y; }
1473
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001474 DECLARE_INSTRUCTION(Add);
1475
1476 private:
1477 DISALLOW_COPY_AND_ASSIGN(HAdd);
1478};
1479
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001480class HSub : public HBinaryOperation {
1481 public:
1482 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1483 : HBinaryOperation(result_type, left, right) {}
1484
1485 virtual bool IsCommutative() { return false; }
1486
Roland Levillain556c3d12014-09-18 15:25:07 +01001487 virtual int32_t Evaluate(int32_t x, int32_t y) const { return x + y; }
1488 virtual int64_t Evaluate(int64_t x, int64_t y) const { return x + y; }
1489
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001490 DECLARE_INSTRUCTION(Sub);
1491
1492 private:
1493 DISALLOW_COPY_AND_ASSIGN(HSub);
1494};
1495
1496// The value of a parameter in this method. Its location depends on
1497// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07001498class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001499 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001500 HParameterValue(uint8_t index, Primitive::Type parameter_type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001501 : HExpression(parameter_type, SideEffects::None()), index_(index) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001502
1503 uint8_t GetIndex() const { return index_; }
1504
1505 DECLARE_INSTRUCTION(ParameterValue);
1506
1507 private:
1508 // The index of this parameter in the parameters list. Must be less
1509 // than HGraph::number_of_in_vregs_;
1510 const uint8_t index_;
1511
1512 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
1513};
1514
Dave Allison20dfc792014-06-16 20:44:29 -07001515class HNot : public HExpression<1> {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001516 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001517 explicit HNot(HInstruction* input) : HExpression(Primitive::kPrimBoolean, SideEffects::None()) {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001518 SetRawInputAt(0, input);
1519 }
1520
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001521 virtual bool CanBeMoved() const { return true; }
1522 virtual bool InstructionDataEquals(HInstruction* other) const { return true; }
1523
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001524 DECLARE_INSTRUCTION(Not);
1525
1526 private:
1527 DISALLOW_COPY_AND_ASSIGN(HNot);
1528};
1529
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001530class HPhi : public HInstruction {
1531 public:
1532 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001533 : HInstruction(SideEffects::None()),
1534 inputs_(arena, number_of_inputs),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001535 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001536 type_(type),
1537 is_live_(false) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001538 inputs_.SetSize(number_of_inputs);
1539 }
1540
1541 virtual size_t InputCount() const { return inputs_.Size(); }
1542 virtual HInstruction* InputAt(size_t i) const { return inputs_.Get(i); }
1543
1544 virtual void SetRawInputAt(size_t index, HInstruction* input) {
1545 inputs_.Put(index, input);
1546 }
1547
1548 void AddInput(HInstruction* input);
1549
1550 virtual Primitive::Type GetType() const { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001551 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001552
1553 uint32_t GetRegNumber() const { return reg_number_; }
1554
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001555 void SetDead() { is_live_ = false; }
1556 void SetLive() { is_live_ = true; }
1557 bool IsDead() const { return !is_live_; }
1558 bool IsLive() const { return is_live_; }
1559
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001560 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001561
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001562 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001563 GrowableArray<HInstruction*> inputs_;
1564 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001565 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001566 bool is_live_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001567
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001568 DISALLOW_COPY_AND_ASSIGN(HPhi);
1569};
1570
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001571class HNullCheck : public HExpression<1> {
1572 public:
1573 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001574 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001575 SetRawInputAt(0, value);
1576 }
1577
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001578 virtual bool CanBeMoved() const { return true; }
1579 virtual bool InstructionDataEquals(HInstruction* other) const { return true; }
1580
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001581 virtual bool NeedsEnvironment() const { return true; }
1582
1583 uint32_t GetDexPc() const { return dex_pc_; }
1584
1585 DECLARE_INSTRUCTION(NullCheck);
1586
1587 private:
1588 const uint32_t dex_pc_;
1589
1590 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
1591};
1592
1593class FieldInfo : public ValueObject {
1594 public:
Nicolas Geoffray39468442014-09-02 15:17:15 +01001595 explicit FieldInfo(MemberOffset field_offset, Primitive::Type field_type)
1596 : field_offset_(field_offset), field_type_(field_type) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001597
1598 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001599 Primitive::Type GetFieldType() const { return field_type_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001600
1601 private:
1602 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01001603 const Primitive::Type field_type_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001604};
1605
1606class HInstanceFieldGet : public HExpression<1> {
1607 public:
1608 HInstanceFieldGet(HInstruction* value,
1609 Primitive::Type field_type,
1610 MemberOffset field_offset)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001611 : HExpression(field_type, SideEffects::DependsOnSomething()),
1612 field_info_(field_offset, field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001613 SetRawInputAt(0, value);
1614 }
1615
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001616 virtual bool CanBeMoved() const { return true; }
1617 virtual bool InstructionDataEquals(HInstruction* other) const {
1618 size_t other_offset = other->AsInstanceFieldGet()->GetFieldOffset().SizeValue();
1619 return other_offset == GetFieldOffset().SizeValue();
1620 }
1621
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001622 virtual size_t ComputeHashCode() const {
1623 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
1624 }
1625
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001626 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001627 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001628
1629 DECLARE_INSTRUCTION(InstanceFieldGet);
1630
1631 private:
1632 const FieldInfo field_info_;
1633
1634 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
1635};
1636
1637class HInstanceFieldSet : public HTemplateInstruction<2> {
1638 public:
1639 HInstanceFieldSet(HInstruction* object,
1640 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01001641 Primitive::Type field_type,
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001642 MemberOffset field_offset)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001643 : HTemplateInstruction(SideEffects::ChangesSomething()),
1644 field_info_(field_offset, field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001645 SetRawInputAt(0, object);
1646 SetRawInputAt(1, value);
1647 }
1648
1649 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001650 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001651
1652 DECLARE_INSTRUCTION(InstanceFieldSet);
1653
1654 private:
1655 const FieldInfo field_info_;
1656
1657 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
1658};
1659
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001660class HArrayGet : public HExpression<2> {
1661 public:
1662 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001663 : HExpression(type, SideEffects::DependsOnSomething()) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001664 SetRawInputAt(0, array);
1665 SetRawInputAt(1, index);
1666 }
1667
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001668 virtual bool CanBeMoved() const { return true; }
1669 virtual bool InstructionDataEquals(HInstruction* other) const { return true; }
1670
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001671 DECLARE_INSTRUCTION(ArrayGet);
1672
1673 private:
1674 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
1675};
1676
1677class HArraySet : public HTemplateInstruction<3> {
1678 public:
1679 HArraySet(HInstruction* array,
1680 HInstruction* index,
1681 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01001682 Primitive::Type component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001683 uint32_t dex_pc)
1684 : HTemplateInstruction(SideEffects::ChangesSomething()),
1685 dex_pc_(dex_pc),
1686 component_type_(component_type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001687 SetRawInputAt(0, array);
1688 SetRawInputAt(1, index);
1689 SetRawInputAt(2, value);
1690 }
1691
1692 virtual bool NeedsEnvironment() const {
1693 // We currently always call a runtime method to catch array store
1694 // exceptions.
1695 return InputAt(2)->GetType() == Primitive::kPrimNot;
1696 }
1697
1698 uint32_t GetDexPc() const { return dex_pc_; }
1699
Nicolas Geoffray39468442014-09-02 15:17:15 +01001700 Primitive::Type GetComponentType() const { return component_type_; }
1701
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001702 DECLARE_INSTRUCTION(ArraySet);
1703
1704 private:
1705 const uint32_t dex_pc_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01001706 const Primitive::Type component_type_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001707
1708 DISALLOW_COPY_AND_ASSIGN(HArraySet);
1709};
1710
1711class HArrayLength : public HExpression<1> {
1712 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001713 explicit HArrayLength(HInstruction* array)
1714 : HExpression(Primitive::kPrimInt, SideEffects::None()) {
1715 // Note that arrays do not change length, so the instruction does not
1716 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001717 SetRawInputAt(0, array);
1718 }
1719
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001720 virtual bool CanBeMoved() const { return true; }
1721 virtual bool InstructionDataEquals(HInstruction* other) const { return true; }
1722
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001723 DECLARE_INSTRUCTION(ArrayLength);
1724
1725 private:
1726 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
1727};
1728
1729class HBoundsCheck : public HExpression<2> {
1730 public:
1731 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001732 : HExpression(index->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001733 DCHECK(index->GetType() == Primitive::kPrimInt);
1734 SetRawInputAt(0, index);
1735 SetRawInputAt(1, length);
1736 }
1737
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001738 virtual bool CanBeMoved() const { return true; }
1739 virtual bool InstructionDataEquals(HInstruction* other) const { return true; }
1740
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001741 virtual bool NeedsEnvironment() const { return true; }
1742
1743 uint32_t GetDexPc() const { return dex_pc_; }
1744
1745 DECLARE_INSTRUCTION(BoundsCheck);
1746
1747 private:
1748 const uint32_t dex_pc_;
1749
1750 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
1751};
1752
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001753/**
1754 * Some DEX instructions are folded into multiple HInstructions that need
1755 * to stay live until the last HInstruction. This class
1756 * is used as a marker for the baseline compiler to ensure its preceding
1757 * HInstruction stays live. `index` is the temporary number that is used
1758 * for knowing the stack offset where to store the instruction.
1759 */
1760class HTemporary : public HTemplateInstruction<0> {
1761 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001762 explicit HTemporary(size_t index) : HTemplateInstruction(SideEffects::None()), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001763
1764 size_t GetIndex() const { return index_; }
1765
1766 DECLARE_INSTRUCTION(Temporary);
1767
1768 private:
1769 const size_t index_;
1770
1771 DISALLOW_COPY_AND_ASSIGN(HTemporary);
1772};
1773
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001774class HSuspendCheck : public HTemplateInstruction<0> {
1775 public:
1776 explicit HSuspendCheck(uint32_t dex_pc)
1777 : HTemplateInstruction(SideEffects::ChangesSomething()), dex_pc_(dex_pc) {}
1778
1779 virtual bool NeedsEnvironment() const {
1780 return true;
1781 }
1782
1783 uint32_t GetDexPc() const { return dex_pc_; }
1784
1785 DECLARE_INSTRUCTION(SuspendCheck);
1786
1787 private:
1788 const uint32_t dex_pc_;
1789
1790 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
1791};
1792
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001793class MoveOperands : public ArenaObject {
1794 public:
1795 MoveOperands(Location source, Location destination)
1796 : source_(source), destination_(destination) {}
1797
1798 Location GetSource() const { return source_; }
1799 Location GetDestination() const { return destination_; }
1800
1801 void SetSource(Location value) { source_ = value; }
1802 void SetDestination(Location value) { destination_ = value; }
1803
1804 // The parallel move resolver marks moves as "in-progress" by clearing the
1805 // destination (but not the source).
1806 Location MarkPending() {
1807 DCHECK(!IsPending());
1808 Location dest = destination_;
1809 destination_ = Location::NoLocation();
1810 return dest;
1811 }
1812
1813 void ClearPending(Location dest) {
1814 DCHECK(IsPending());
1815 destination_ = dest;
1816 }
1817
1818 bool IsPending() const {
1819 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
1820 return destination_.IsInvalid() && !source_.IsInvalid();
1821 }
1822
1823 // True if this blocks a move from the given location.
1824 bool Blocks(Location loc) const {
1825 return !IsEliminated() && source_.Equals(loc);
1826 }
1827
1828 // A move is redundant if it's been eliminated, if its source and
1829 // destination are the same, or if its destination is unneeded.
1830 bool IsRedundant() const {
1831 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
1832 }
1833
1834 // We clear both operands to indicate move that's been eliminated.
1835 void Eliminate() {
1836 source_ = destination_ = Location::NoLocation();
1837 }
1838
1839 bool IsEliminated() const {
1840 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
1841 return source_.IsInvalid();
1842 }
1843
1844 private:
1845 Location source_;
1846 Location destination_;
1847
1848 DISALLOW_COPY_AND_ASSIGN(MoveOperands);
1849};
1850
1851static constexpr size_t kDefaultNumberOfMoves = 4;
1852
1853class HParallelMove : public HTemplateInstruction<0> {
1854 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001855 explicit HParallelMove(ArenaAllocator* arena)
1856 : HTemplateInstruction(SideEffects::None()), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001857
1858 void AddMove(MoveOperands* move) {
1859 moves_.Add(move);
1860 }
1861
1862 MoveOperands* MoveOperandsAt(size_t index) const {
1863 return moves_.Get(index);
1864 }
1865
1866 size_t NumMoves() const { return moves_.Size(); }
1867
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001868 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001869
1870 private:
1871 GrowableArray<MoveOperands*> moves_;
1872
1873 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
1874};
1875
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001876class HGraphVisitor : public ValueObject {
1877 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001878 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
1879 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001880
Dave Allison20dfc792014-06-16 20:44:29 -07001881 virtual void VisitInstruction(HInstruction* instruction) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001882 virtual void VisitBasicBlock(HBasicBlock* block);
1883
1884 void VisitInsertionOrder();
1885
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001886 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001887
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001888 // Visit functions for instruction classes.
1889#define DECLARE_VISIT_INSTRUCTION(name) \
1890 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
1891
1892 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
1893
1894#undef DECLARE_VISIT_INSTRUCTION
1895
1896 private:
1897 HGraph* graph_;
1898
1899 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
1900};
1901
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001902class HInsertionOrderIterator : public ValueObject {
1903 public:
1904 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
1905
1906 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
1907 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
1908 void Advance() { ++index_; }
1909
1910 private:
1911 const HGraph& graph_;
1912 size_t index_;
1913
1914 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
1915};
1916
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001917class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001918 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001919 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001920
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001921 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
1922 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001923 void Advance() { ++index_; }
1924
1925 private:
1926 const HGraph& graph_;
1927 size_t index_;
1928
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001929 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001930};
1931
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001932class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001933 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001934 explicit HPostOrderIterator(const HGraph& graph)
1935 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001936
1937 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001938 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001939 void Advance() { --index_; }
1940
1941 private:
1942 const HGraph& graph_;
1943 size_t index_;
1944
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001945 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001946};
1947
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001948} // namespace art
1949
1950#endif // ART_COMPILER_OPTIMIZING_NODES_H_