blob: e87b044cc9fc6cf079968559afabff3ce6c829b5 [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 Geoffraybe9a92a2014-02-25 14:22:56 +000041static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000042
Dave Allison20dfc792014-06-16 20:44:29 -070043enum IfCondition {
44 kCondEQ,
45 kCondNE,
46 kCondLT,
47 kCondLE,
48 kCondGT,
49 kCondGE,
50};
51
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010052class HInstructionList {
53 public:
54 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
55
56 void AddInstruction(HInstruction* instruction);
57 void RemoveInstruction(HInstruction* instruction);
58
59 private:
60 HInstruction* first_instruction_;
61 HInstruction* last_instruction_;
62
63 friend class HBasicBlock;
64 friend class HInstructionIterator;
Nicolas Geoffray804d0932014-05-02 08:46:00 +010065 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010066
67 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
68};
69
Nicolas Geoffray818f2102014-02-18 16:43:35 +000070// Control-flow graph of a method. Contains a list of basic blocks.
71class HGraph : public ArenaObject {
72 public:
73 explicit HGraph(ArenaAllocator* arena)
74 : arena_(arena),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000075 blocks_(arena, kDefaultNumberOfBlocks),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +010076 reverse_post_order_(arena, kDefaultNumberOfBlocks),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +010077 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +010078 number_of_vregs_(0),
79 number_of_in_vregs_(0),
Nicolas Geoffraye5038322014-07-04 09:41:32 +010080 number_of_temporaries_(0),
Dave Allison20dfc792014-06-16 20:44:29 -070081 current_instruction_id_(0) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +000082
Nicolas Geoffray787c3072014-03-17 10:20:19 +000083 ArenaAllocator* GetArena() const { return arena_; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +010084 const GrowableArray<HBasicBlock*>& GetBlocks() const { return blocks_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +000085
Nicolas Geoffray787c3072014-03-17 10:20:19 +000086 HBasicBlock* GetEntryBlock() const { return entry_block_; }
87 HBasicBlock* GetExitBlock() const { return exit_block_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000088
Nicolas Geoffray787c3072014-03-17 10:20:19 +000089 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
90 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000091
Nicolas Geoffray818f2102014-02-18 16:43:35 +000092 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010093
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000094 void BuildDominatorTree();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010095 void TransformToSSA();
96 void SimplifyCFG();
Nicolas Geoffray818f2102014-02-18 16:43:35 +000097
Nicolas Geoffray622d9c32014-05-12 16:11:02 +010098 // Find all natural loops in this graph. Aborts computation and returns false
Nicolas Geoffrayf635e632014-05-14 09:43:38 +010099 // if one loop is not natural, that is the header does not dominate the back
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100100 // edge.
101 bool FindNaturalLoops() const;
102
103 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
104 void SimplifyLoop(HBasicBlock* header);
105
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000106 int GetNextInstructionId() {
107 return current_instruction_id_++;
108 }
109
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100110 uint16_t GetMaximumNumberOfOutVRegs() const {
111 return maximum_number_of_out_vregs_;
112 }
113
114 void UpdateMaximumNumberOfOutVRegs(uint16_t new_value) {
115 maximum_number_of_out_vregs_ = std::max(new_value, maximum_number_of_out_vregs_);
116 }
117
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100118 void UpdateNumberOfTemporaries(size_t count) {
119 number_of_temporaries_ = std::max(count, number_of_temporaries_);
120 }
121
122 size_t GetNumberOfTemporaries() const {
123 return number_of_temporaries_;
124 }
125
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100126 void SetNumberOfVRegs(uint16_t number_of_vregs) {
127 number_of_vregs_ = number_of_vregs;
128 }
129
130 uint16_t GetNumberOfVRegs() const {
131 return number_of_vregs_;
132 }
133
134 void SetNumberOfInVRegs(uint16_t value) {
135 number_of_in_vregs_ = value;
136 }
137
138 uint16_t GetNumberOfInVRegs() const {
139 return number_of_in_vregs_;
140 }
141
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100142 uint16_t GetNumberOfLocalVRegs() const {
143 return number_of_vregs_ - number_of_in_vregs_;
144 }
145
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100146 const GrowableArray<HBasicBlock*>& GetReversePostOrder() const {
147 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100148 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100149
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000150 private:
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000151 HBasicBlock* FindCommonDominator(HBasicBlock* first, HBasicBlock* second) const;
152 void VisitBlockForDominatorTree(HBasicBlock* block,
153 HBasicBlock* predecessor,
154 GrowableArray<size_t>* visits);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100155 void FindBackEdges(ArenaBitVector* visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000156 void VisitBlockForBackEdges(HBasicBlock* block,
157 ArenaBitVector* visited,
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100158 ArenaBitVector* visiting);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000159 void RemoveDeadBlocks(const ArenaBitVector& visited) const;
160
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000161 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000162
163 // List of blocks in insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000164 GrowableArray<HBasicBlock*> blocks_;
165
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100166 // List of blocks to perform a reverse post order tree traversal.
167 GrowableArray<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000168
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000169 HBasicBlock* entry_block_;
170 HBasicBlock* exit_block_;
171
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100172 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100173 uint16_t maximum_number_of_out_vregs_;
174
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100175 // The number of virtual registers in this method. Contains the parameters.
176 uint16_t number_of_vregs_;
177
178 // The number of virtual registers used by parameters of this method.
179 uint16_t number_of_in_vregs_;
180
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100181 // The number of temporaries that will be needed for the baseline compiler.
182 size_t number_of_temporaries_;
183
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000184 // The current id to assign to a newly added instruction. See HInstruction.id_.
185 int current_instruction_id_;
186
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000187 DISALLOW_COPY_AND_ASSIGN(HGraph);
188};
189
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000190class HLoopInformation : public ArenaObject {
191 public:
192 HLoopInformation(HBasicBlock* header, HGraph* graph)
193 : header_(header),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100194 back_edges_(graph->GetArena(), kDefaultNumberOfBackEdges),
195 blocks_(graph->GetArena(), graph->GetBlocks().Size(), false) {}
196
197 HBasicBlock* GetHeader() const {
198 return header_;
199 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000200
201 void AddBackEdge(HBasicBlock* back_edge) {
202 back_edges_.Add(back_edge);
203 }
204
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100205 void RemoveBackEdge(HBasicBlock* back_edge) {
206 back_edges_.Delete(back_edge);
207 }
208
209 bool IsBackEdge(HBasicBlock* block) {
210 for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) {
211 if (back_edges_.Get(i) == block) return true;
212 }
213 return false;
214 }
215
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000216 int NumberOfBackEdges() const {
217 return back_edges_.Size();
218 }
219
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100220 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100221
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100222 const GrowableArray<HBasicBlock*>& GetBackEdges() const {
223 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100224 }
225
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100226 void ClearBackEdges() {
227 back_edges_.Reset();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100228 }
229
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100230 // Find blocks that are part of this loop. Returns whether the loop is a natural loop,
231 // that is the header dominates the back edge.
232 bool Populate();
233
234 // Returns whether this loop information contains `block`.
235 // Note that this loop information *must* be populated before entering this function.
236 bool Contains(const HBasicBlock& block) const;
237
238 // Returns whether this loop information is an inner loop of `other`.
239 // Note that `other` *must* be populated before entering this function.
240 bool IsIn(const HLoopInformation& other) const;
241
242 const ArenaBitVector& GetBlocks() const { return blocks_; }
243
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000244 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100245 // Internal recursive implementation of `Populate`.
246 void PopulateRecursive(HBasicBlock* block);
247
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000248 HBasicBlock* header_;
249 GrowableArray<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100250 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000251
252 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
253};
254
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100255static constexpr size_t kNoLifetime = -1;
256
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000257// A block in a method. Contains the list of instructions represented
258// as a double linked list. Each block knows its predecessors and
259// successors.
260class HBasicBlock : public ArenaObject {
261 public:
262 explicit HBasicBlock(HGraph* graph)
263 : graph_(graph),
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000264 predecessors_(graph->GetArena(), kDefaultNumberOfPredecessors),
265 successors_(graph->GetArena(), kDefaultNumberOfSuccessors),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000266 loop_information_(nullptr),
267 dominator_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100268 block_id_(-1),
269 lifetime_start_(kNoLifetime),
270 lifetime_end_(kNoLifetime) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000271
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100272 const GrowableArray<HBasicBlock*>& GetPredecessors() const {
273 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000274 }
275
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100276 const GrowableArray<HBasicBlock*>& GetSuccessors() const {
277 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000278 }
279
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000280 void AddBackEdge(HBasicBlock* back_edge) {
281 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000282 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000283 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100284 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000285 loop_information_->AddBackEdge(back_edge);
286 }
287
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000288 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000289
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000290 int GetBlockId() const { return block_id_; }
291 void SetBlockId(int id) { block_id_ = id; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000292
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000293 HBasicBlock* GetDominator() const { return dominator_; }
294 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000295
296 int NumberOfBackEdges() const {
297 return loop_information_ == nullptr
298 ? 0
299 : loop_information_->NumberOfBackEdges();
300 }
301
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100302 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
303 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100304 const HInstructionList& GetInstructions() const { return instructions_; }
305 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100306 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000307
308 void AddSuccessor(HBasicBlock* block) {
309 successors_.Add(block);
310 block->predecessors_.Add(this);
311 }
312
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100313 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
314 size_t successor_index = GetSuccessorIndexOf(existing);
315 DCHECK_NE(successor_index, static_cast<size_t>(-1));
316 existing->RemovePredecessor(this);
317 new_block->predecessors_.Add(this);
318 successors_.Put(successor_index, new_block);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000319 }
320
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100321 void RemovePredecessor(HBasicBlock* block) {
322 predecessors_.Delete(block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100323 }
324
325 void ClearAllPredecessors() {
326 predecessors_.Reset();
327 }
328
329 void AddPredecessor(HBasicBlock* block) {
330 predecessors_.Add(block);
331 block->successors_.Add(this);
332 }
333
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100334 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) {
335 for (size_t i = 0, e = predecessors_.Size(); i < e; ++i) {
336 if (predecessors_.Get(i) == predecessor) {
337 return i;
338 }
339 }
340 return -1;
341 }
342
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100343 size_t GetSuccessorIndexOf(HBasicBlock* successor) {
344 for (size_t i = 0, e = successors_.Size(); i < e; ++i) {
345 if (successors_.Get(i) == successor) {
346 return i;
347 }
348 }
349 return -1;
350 }
351
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000352 void AddInstruction(HInstruction* instruction);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100353 void RemoveInstruction(HInstruction* instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100354 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100355 void AddPhi(HPhi* phi);
356 void RemovePhi(HPhi* phi);
357
358 bool IsLoopHeader() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100359 return (loop_information_ != nullptr) && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100360 }
361
362 HLoopInformation* GetLoopInformation() const {
363 return loop_information_;
364 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000365
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100366 // Set the loop_information_ on this block. This method overrides the current
367 // loop_information if it is an outer loop of the passed loop information.
368 void SetInLoop(HLoopInformation* info) {
369 if (IsLoopHeader()) {
370 // Nothing to do. This just means `info` is an outer loop.
371 } else if (loop_information_ == nullptr) {
372 loop_information_ = info;
373 } else if (loop_information_->Contains(*info->GetHeader())) {
374 // Block is currently part of an outer loop. Make it part of this inner loop.
375 // Note that a non loop header having a loop information means this loop information
376 // has already been populated
377 loop_information_ = info;
378 } else {
379 // Block is part of an inner loop. Do not update the loop information.
380 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
381 // at this point, because this method is being called while populating `info`.
382 }
383 }
384
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100385 bool IsInLoop() const { return loop_information_ != nullptr; }
386
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100387 // Returns wheter this block dominates the blocked passed as parameter.
388 bool Dominates(HBasicBlock* block) const;
389
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100390 size_t GetLifetimeStart() const { return lifetime_start_; }
391 size_t GetLifetimeEnd() const { return lifetime_end_; }
392
393 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
394 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
395
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000396 private:
397 HGraph* const graph_;
398 GrowableArray<HBasicBlock*> predecessors_;
399 GrowableArray<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100400 HInstructionList instructions_;
401 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000402 HLoopInformation* loop_information_;
403 HBasicBlock* dominator_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000404 int block_id_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100405 size_t lifetime_start_;
406 size_t lifetime_end_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000407
408 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
409};
410
411#define FOR_EACH_INSTRUCTION(M) \
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000412 M(Add) \
Dave Allison20dfc792014-06-16 20:44:29 -0700413 M(Condition) \
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000414 M(Equal) \
Dave Allison20dfc792014-06-16 20:44:29 -0700415 M(NotEqual) \
416 M(LessThan) \
417 M(LessThanOrEqual) \
418 M(GreaterThan) \
419 M(GreaterThanOrEqual) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000420 M(Exit) \
421 M(Goto) \
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000422 M(If) \
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000423 M(IntConstant) \
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000424 M(InvokeStatic) \
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000425 M(LoadLocal) \
426 M(Local) \
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100427 M(LongConstant) \
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +0100428 M(NewInstance) \
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100429 M(Not) \
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100430 M(ParameterValue) \
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100431 M(ParallelMove) \
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100432 M(Phi) \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000433 M(Return) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000434 M(ReturnVoid) \
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000435 M(StoreLocal) \
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100436 M(Sub) \
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100437 M(Compare) \
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100438 M(InstanceFieldGet) \
439 M(InstanceFieldSet) \
440 M(NullCheck) \
441 M(Temporary) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000442
Dave Allison20dfc792014-06-16 20:44:29 -0700443
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000444#define FORWARD_DECLARATION(type) class H##type;
445FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
446#undef FORWARD_DECLARATION
447
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000448#define DECLARE_INSTRUCTION(type) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000449 virtual const char* DebugName() const { return #type; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000450 virtual H##type* As##type() { return this; } \
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100451 virtual void Accept(HGraphVisitor* visitor) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000452
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100453template <typename T>
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000454class HUseListNode : public ArenaObject {
455 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100456 HUseListNode(T* user, size_t index, HUseListNode* tail)
Dave Allison20dfc792014-06-16 20:44:29 -0700457 : user_(user), index_(index), tail_(tail) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000458
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000459 HUseListNode* GetTail() const { return tail_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100460 T* GetUser() const { return user_; }
461 size_t GetIndex() const { return index_; }
462
463 void SetTail(HUseListNode<T>* node) { tail_ = node; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000464
465 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100466 T* const user_;
467 const size_t index_;
468 HUseListNode<T>* tail_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000469
470 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
471};
472
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000473class HInstruction : public ArenaObject {
474 public:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000475 HInstruction()
476 : previous_(nullptr),
477 next_(nullptr),
478 block_(nullptr),
479 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100480 ssa_index_(-1),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000481 uses_(nullptr),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100482 env_uses_(nullptr),
483 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100484 locations_(nullptr),
485 live_interval_(nullptr),
486 lifetime_position_(kNoLifetime) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000487
Dave Allison20dfc792014-06-16 20:44:29 -0700488 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000489
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000490 HInstruction* GetNext() const { return next_; }
491 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000492
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000493 HBasicBlock* GetBlock() const { return block_; }
494 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100495 bool IsInBlock() const { return block_ != nullptr; }
496 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000497
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100498 virtual size_t InputCount() const = 0;
499 virtual HInstruction* InputAt(size_t i) const = 0;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000500
501 virtual void Accept(HGraphVisitor* visitor) = 0;
502 virtual const char* DebugName() const = 0;
503
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100504 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100505 virtual void SetRawInputAt(size_t index, HInstruction* input) = 0;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100506
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100507 virtual bool NeedsEnvironment() const { return false; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100508 virtual bool IsControlFlow() const { return false; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100509
510 void AddUseAt(HInstruction* user, size_t index) {
511 uses_ = new (block_->GetGraph()->GetArena()) HUseListNode<HInstruction>(user, index, uses_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000512 }
513
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100514 void AddEnvUseAt(HEnvironment* user, size_t index) {
515 env_uses_ = new (block_->GetGraph()->GetArena()) HUseListNode<HEnvironment>(
516 user, index, env_uses_);
517 }
518
519 void RemoveUser(HInstruction* user, size_t index);
520
521 HUseListNode<HInstruction>* GetUses() const { return uses_; }
522 HUseListNode<HEnvironment>* GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000523
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100524 bool HasUses() const { return uses_ != nullptr || env_uses_ != nullptr; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100525 bool HasEnvironmentUses() const { return env_uses_ != nullptr; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000526
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100527 size_t NumberOfUses() const {
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100528 // TODO: Optimize this method if it is used outside of the HGraphVisualizer.
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100529 size_t result = 0;
530 HUseListNode<HInstruction>* current = uses_;
531 while (current != nullptr) {
532 current = current->GetTail();
533 ++result;
534 }
535 return result;
536 }
537
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000538 int GetId() const { return id_; }
539 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000540
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100541 int GetSsaIndex() const { return ssa_index_; }
542 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
543 bool HasSsaIndex() const { return ssa_index_ != -1; }
544
545 bool HasEnvironment() const { return environment_ != nullptr; }
546 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100547 void SetEnvironment(HEnvironment* environment) { environment_ = environment; }
548
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000549 LocationSummary* GetLocations() const { return locations_; }
550 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000551
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100552 void ReplaceWith(HInstruction* instruction);
553
Dave Allison20dfc792014-06-16 20:44:29 -0700554 bool HasOnlyOneUse() const {
555 return uses_ != nullptr && uses_->GetTail() == nullptr;
556 }
557
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000558#define INSTRUCTION_TYPE_CHECK(type) \
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100559 bool Is##type() { return (As##type() != nullptr); } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000560 virtual H##type* As##type() { return nullptr; }
561
562 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
563#undef INSTRUCTION_TYPE_CHECK
564
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100565 size_t GetLifetimePosition() const { return lifetime_position_; }
566 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
567 LiveInterval* GetLiveInterval() const { return live_interval_; }
568 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
569 bool HasLiveInterval() const { return live_interval_ != nullptr; }
570
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000571 private:
572 HInstruction* previous_;
573 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000574 HBasicBlock* block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000575
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000576 // An instruction gets an id when it is added to the graph.
577 // It reflects creation order. A negative id means the instruction
578 // has not beed added to the graph.
579 int id_;
580
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100581 // When doing liveness analysis, instructions that have uses get an SSA index.
582 int ssa_index_;
583
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100584 // List of instructions that have this instruction as input.
585 HUseListNode<HInstruction>* uses_;
586
587 // List of environments that contain this instruction.
588 HUseListNode<HEnvironment>* env_uses_;
589
590 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000591
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000592 // Set by the code generator.
593 LocationSummary* locations_;
594
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100595 // Set by the liveness analysis.
596 LiveInterval* live_interval_;
597
598 // Set by the liveness analysis, this is the position in a linear
599 // order of blocks where this instruction's live interval start.
600 size_t lifetime_position_;
601
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000602 friend class HBasicBlock;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100603 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000604
605 DISALLOW_COPY_AND_ASSIGN(HInstruction);
606};
607
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100608template<typename T>
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000609class HUseIterator : public ValueObject {
610 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100611 explicit HUseIterator(HUseListNode<T>* uses) : current_(uses) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000612
613 bool Done() const { return current_ == nullptr; }
614
615 void Advance() {
616 DCHECK(!Done());
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000617 current_ = current_->GetTail();
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000618 }
619
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100620 HUseListNode<T>* Current() const {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000621 DCHECK(!Done());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100622 return current_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000623 }
624
625 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100626 HUseListNode<T>* current_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000627
628 friend class HValue;
629};
630
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100631// A HEnvironment object contains the values of virtual registers at a given location.
632class HEnvironment : public ArenaObject {
633 public:
634 HEnvironment(ArenaAllocator* arena, size_t number_of_vregs) : vregs_(arena, number_of_vregs) {
635 vregs_.SetSize(number_of_vregs);
636 for (size_t i = 0; i < number_of_vregs; i++) {
637 vregs_.Put(i, nullptr);
638 }
639 }
640
641 void Populate(const GrowableArray<HInstruction*>& env) {
642 for (size_t i = 0; i < env.Size(); i++) {
643 HInstruction* instruction = env.Get(i);
644 vregs_.Put(i, instruction);
645 if (instruction != nullptr) {
646 instruction->AddEnvUseAt(this, i);
647 }
648 }
649 }
650
651 void SetRawEnvAt(size_t index, HInstruction* instruction) {
652 vregs_.Put(index, instruction);
653 }
654
655 GrowableArray<HInstruction*>* GetVRegs() {
656 return &vregs_;
657 }
658
659 private:
660 GrowableArray<HInstruction*> vregs_;
661
662 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
663};
664
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000665class HInputIterator : public ValueObject {
666 public:
Dave Allison20dfc792014-06-16 20:44:29 -0700667 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000668
669 bool Done() const { return index_ == instruction_->InputCount(); }
670 HInstruction* Current() const { return instruction_->InputAt(index_); }
671 void Advance() { index_++; }
672
673 private:
674 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100675 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000676
677 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
678};
679
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000680class HInstructionIterator : public ValueObject {
681 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100682 explicit HInstructionIterator(const HInstructionList& instructions)
683 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000684 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000685 }
686
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000687 bool Done() const { return instruction_ == nullptr; }
688 HInstruction* Current() const { return instruction_; }
689 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000690 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000691 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000692 }
693
694 private:
695 HInstruction* instruction_;
696 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100697
698 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000699};
700
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100701class HBackwardInstructionIterator : public ValueObject {
702 public:
703 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
704 : instruction_(instructions.last_instruction_) {
705 next_ = Done() ? nullptr : instruction_->GetPrevious();
706 }
707
708 bool Done() const { return instruction_ == nullptr; }
709 HInstruction* Current() const { return instruction_; }
710 void Advance() {
711 instruction_ = next_;
712 next_ = Done() ? nullptr : instruction_->GetPrevious();
713 }
714
715 private:
716 HInstruction* instruction_;
717 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100718
719 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100720};
721
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000722// An embedded container with N elements of type T. Used (with partial
723// specialization for N=0) because embedded arrays cannot have size 0.
724template<typename T, intptr_t N>
725class EmbeddedArray {
726 public:
Dave Allison20dfc792014-06-16 20:44:29 -0700727 EmbeddedArray() : elements_() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000728
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000729 intptr_t GetLength() const { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000730
731 const T& operator[](intptr_t i) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000732 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000733 return elements_[i];
734 }
735
736 T& operator[](intptr_t i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000737 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000738 return elements_[i];
739 }
740
741 const T& At(intptr_t i) const {
742 return (*this)[i];
743 }
744
745 void SetAt(intptr_t i, const T& val) {
746 (*this)[i] = val;
747 }
748
749 private:
750 T elements_[N];
751};
752
753template<typename T>
754class EmbeddedArray<T, 0> {
755 public:
756 intptr_t length() const { return 0; }
757 const T& operator[](intptr_t i) const {
758 LOG(FATAL) << "Unreachable";
759 static T sentinel = 0;
760 return sentinel;
761 }
762 T& operator[](intptr_t i) {
763 LOG(FATAL) << "Unreachable";
764 static T sentinel = 0;
765 return sentinel;
766 }
767};
768
769template<intptr_t N>
770class HTemplateInstruction: public HInstruction {
771 public:
Dave Allison20dfc792014-06-16 20:44:29 -0700772 HTemplateInstruction<N>() : inputs_() {}
773 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000774
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100775 virtual size_t InputCount() const { return N; }
776 virtual HInstruction* InputAt(size_t i) const { return inputs_[i]; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000777
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000778 protected:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100779 virtual void SetRawInputAt(size_t i, HInstruction* instruction) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000780 inputs_[i] = instruction;
781 }
782
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000783 private:
784 EmbeddedArray<HInstruction*, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100785
786 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000787};
788
Dave Allison20dfc792014-06-16 20:44:29 -0700789template<intptr_t N>
790class HExpression: public HTemplateInstruction<N> {
791 public:
792 explicit HExpression<N>(Primitive::Type type) : type_(type) {}
793 virtual ~HExpression() {}
794
795 virtual Primitive::Type GetType() const { return type_; }
796
797 private:
798 const Primitive::Type type_;
799};
800
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000801// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
802// instruction that branches to the exit block.
803class HReturnVoid : public HTemplateInstruction<0> {
804 public:
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100805 HReturnVoid() {}
806
807 virtual bool IsControlFlow() const { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000808
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100809 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000810
811 private:
812 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
813};
814
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000815// Represents dex's RETURN opcodes. A HReturn is a control flow
816// instruction that branches to the exit block.
817class HReturn : public HTemplateInstruction<1> {
818 public:
819 explicit HReturn(HInstruction* value) {
820 SetRawInputAt(0, value);
821 }
822
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100823 virtual bool IsControlFlow() const { return true; }
824
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100825 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000826
827 private:
828 DISALLOW_COPY_AND_ASSIGN(HReturn);
829};
830
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000831// The exit instruction is the only instruction of the exit block.
832// Instructions aborting the method (HTrow and HReturn) must branch to the
833// exit block.
834class HExit : public HTemplateInstruction<0> {
835 public:
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100836 HExit() {}
837
838 virtual bool IsControlFlow() const { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000839
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100840 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000841
842 private:
843 DISALLOW_COPY_AND_ASSIGN(HExit);
844};
845
846// Jumps from one block to another.
847class HGoto : public HTemplateInstruction<0> {
848 public:
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100849 HGoto() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000850
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000851 HBasicBlock* GetSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100852 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000853 }
854
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100855 virtual bool IsControlFlow() const { return true; }
856
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100857 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000858
859 private:
860 DISALLOW_COPY_AND_ASSIGN(HGoto);
861};
862
Dave Allison20dfc792014-06-16 20:44:29 -0700863
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000864// Conditional branch. A block ending with an HIf instruction must have
865// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000866class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000867 public:
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000868 explicit HIf(HInstruction* input) {
869 SetRawInputAt(0, input);
870 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000871
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000872 HBasicBlock* IfTrueSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100873 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000874 }
875
876 HBasicBlock* IfFalseSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100877 return GetBlock()->GetSuccessors().Get(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000878 }
879
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100880 virtual bool IsControlFlow() const { return true; }
881
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100882 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000883
Dave Allison20dfc792014-06-16 20:44:29 -0700884 virtual bool IsIfInstruction() const { return true; }
885
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000886 private:
887 DISALLOW_COPY_AND_ASSIGN(HIf);
888};
889
Dave Allison20dfc792014-06-16 20:44:29 -0700890class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000891 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000892 HBinaryOperation(Primitive::Type result_type,
893 HInstruction* left,
Dave Allison20dfc792014-06-16 20:44:29 -0700894 HInstruction* right) : HExpression(result_type) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000895 SetRawInputAt(0, left);
896 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000897 }
898
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000899 HInstruction* GetLeft() const { return InputAt(0); }
900 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -0700901 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000902
903 virtual bool IsCommutative() { return false; }
904
905 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000906 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
907};
908
Dave Allison20dfc792014-06-16 20:44:29 -0700909class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000910 public:
Dave Allison20dfc792014-06-16 20:44:29 -0700911 HCondition(HInstruction* first, HInstruction* second)
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000912 : HBinaryOperation(Primitive::kPrimBoolean, first, second) {}
913
914 virtual bool IsCommutative() { return true; }
Dave Allison20dfc792014-06-16 20:44:29 -0700915 bool NeedsMaterialization() const;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000916
Dave Allison20dfc792014-06-16 20:44:29 -0700917 DECLARE_INSTRUCTION(Condition);
918
919 virtual IfCondition GetCondition() const = 0;
920
921 private:
922 DISALLOW_COPY_AND_ASSIGN(HCondition);
923};
924
925// Instruction to check if two inputs are equal to each other.
926class HEqual : public HCondition {
927 public:
928 HEqual(HInstruction* first, HInstruction* second)
929 : HCondition(first, second) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100930
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100931 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000932
Dave Allison20dfc792014-06-16 20:44:29 -0700933 virtual IfCondition GetCondition() const {
934 return kCondEQ;
935 }
936
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000937 private:
938 DISALLOW_COPY_AND_ASSIGN(HEqual);
939};
940
Dave Allison20dfc792014-06-16 20:44:29 -0700941class HNotEqual : public HCondition {
942 public:
943 HNotEqual(HInstruction* first, HInstruction* second)
944 : HCondition(first, second) {}
945
946 DECLARE_INSTRUCTION(NotEqual);
947
948 virtual IfCondition GetCondition() const {
949 return kCondNE;
950 }
951
952 private:
953 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
954};
955
956class HLessThan : public HCondition {
957 public:
958 HLessThan(HInstruction* first, HInstruction* second)
959 : HCondition(first, second) {}
960
961 DECLARE_INSTRUCTION(LessThan);
962
963 virtual IfCondition GetCondition() const {
964 return kCondLT;
965 }
966
967 private:
968 DISALLOW_COPY_AND_ASSIGN(HLessThan);
969};
970
971class HLessThanOrEqual : public HCondition {
972 public:
973 HLessThanOrEqual(HInstruction* first, HInstruction* second)
974 : HCondition(first, second) {}
975
976 DECLARE_INSTRUCTION(LessThanOrEqual);
977
978 virtual IfCondition GetCondition() const {
979 return kCondLE;
980 }
981
982 private:
983 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
984};
985
986class HGreaterThan : public HCondition {
987 public:
988 HGreaterThan(HInstruction* first, HInstruction* second)
989 : HCondition(first, second) {}
990
991 DECLARE_INSTRUCTION(GreaterThan);
992
993 virtual IfCondition GetCondition() const {
994 return kCondGT;
995 }
996
997 private:
998 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
999};
1000
1001class HGreaterThanOrEqual : public HCondition {
1002 public:
1003 HGreaterThanOrEqual(HInstruction* first, HInstruction* second)
1004 : HCondition(first, second) {}
1005
1006 DECLARE_INSTRUCTION(GreaterThanOrEqual);
1007
1008 virtual IfCondition GetCondition() const {
1009 return kCondGE;
1010 }
1011
1012 private:
1013 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
1014};
1015
1016
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001017// Instruction to check how two inputs compare to each other.
1018// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
1019class HCompare : public HBinaryOperation {
1020 public:
1021 HCompare(Primitive::Type type, HInstruction* first, HInstruction* second)
1022 : HBinaryOperation(Primitive::kPrimInt, first, second) {
1023 DCHECK_EQ(type, first->GetType());
1024 DCHECK_EQ(type, second->GetType());
1025 }
1026
1027 DECLARE_INSTRUCTION(Compare);
1028
1029 private:
1030 DISALLOW_COPY_AND_ASSIGN(HCompare);
1031};
1032
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001033// A local in the graph. Corresponds to a Dex register.
1034class HLocal : public HTemplateInstruction<0> {
1035 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001036 explicit HLocal(uint16_t reg_number) : reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001037
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001038 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001039
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001040 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001041
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001042 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001043 // The Dex register number.
1044 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001045
1046 DISALLOW_COPY_AND_ASSIGN(HLocal);
1047};
1048
1049// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07001050class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001051 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001052 explicit HLoadLocal(HLocal* local, Primitive::Type type) : HExpression(type) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001053 SetRawInputAt(0, local);
1054 }
1055
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001056 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1057
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001058 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001059
1060 private:
1061 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
1062};
1063
1064// Store a value in a given local. This instruction has two inputs: the value
1065// and the local.
1066class HStoreLocal : public HTemplateInstruction<2> {
1067 public:
1068 HStoreLocal(HLocal* local, HInstruction* value) {
1069 SetRawInputAt(0, local);
1070 SetRawInputAt(1, value);
1071 }
1072
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001073 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1074
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001075 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001076
1077 private:
1078 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
1079};
1080
1081// Constants of the type int. Those can be from Dex instructions, or
1082// synthesized (for example with the if-eqz instruction).
Dave Allison20dfc792014-06-16 20:44:29 -07001083class HIntConstant : public HExpression<0> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001084 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001085 explicit HIntConstant(int32_t value) : HExpression(Primitive::kPrimInt), value_(value) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001086
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001087 int32_t GetValue() const { return value_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001088
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001089 DECLARE_INSTRUCTION(IntConstant);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001090
1091 private:
1092 const int32_t value_;
1093
1094 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
1095};
1096
Dave Allison20dfc792014-06-16 20:44:29 -07001097class HLongConstant : public HExpression<0> {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001098 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001099 explicit HLongConstant(int64_t value) : HExpression(Primitive::kPrimLong), value_(value) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001100
1101 int64_t GetValue() const { return value_; }
1102
1103 virtual Primitive::Type GetType() const { return Primitive::kPrimLong; }
1104
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001105 DECLARE_INSTRUCTION(LongConstant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001106
1107 private:
1108 const int64_t value_;
1109
1110 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
1111};
1112
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001113class HInvoke : public HInstruction {
1114 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001115 HInvoke(ArenaAllocator* arena,
1116 uint32_t number_of_arguments,
1117 Primitive::Type return_type,
1118 uint32_t dex_pc)
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001119 : inputs_(arena, number_of_arguments),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001120 return_type_(return_type),
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001121 dex_pc_(dex_pc) {
1122 inputs_.SetSize(number_of_arguments);
1123 }
1124
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001125 virtual size_t InputCount() const { return inputs_.Size(); }
1126 virtual HInstruction* InputAt(size_t i) const { return inputs_.Get(i); }
1127
1128 // Runtime needs to walk the stack, so Dex -> Dex calls need to
1129 // know their environment.
1130 virtual bool NeedsEnvironment() const { return true; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001131
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001132 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001133 SetRawInputAt(index, argument);
1134 }
1135
1136 virtual void SetRawInputAt(size_t index, HInstruction* input) {
1137 inputs_.Put(index, input);
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001138 }
1139
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001140 virtual Primitive::Type GetType() const { return return_type_; }
1141
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001142 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001143
1144 protected:
1145 GrowableArray<HInstruction*> inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001146 const Primitive::Type return_type_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001147 const uint32_t dex_pc_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001148
1149 private:
1150 DISALLOW_COPY_AND_ASSIGN(HInvoke);
1151};
1152
1153class HInvokeStatic : public HInvoke {
1154 public:
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001155 HInvokeStatic(ArenaAllocator* arena,
1156 uint32_t number_of_arguments,
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001157 Primitive::Type return_type,
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001158 uint32_t dex_pc,
1159 uint32_t index_in_dex_cache)
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001160 : HInvoke(arena, number_of_arguments, return_type, dex_pc),
1161 index_in_dex_cache_(index_in_dex_cache) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001162
1163 uint32_t GetIndexInDexCache() const { return index_in_dex_cache_; }
1164
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001165 DECLARE_INSTRUCTION(InvokeStatic);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001166
1167 private:
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001168 const uint32_t index_in_dex_cache_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001169
1170 DISALLOW_COPY_AND_ASSIGN(HInvokeStatic);
1171};
1172
Dave Allison20dfc792014-06-16 20:44:29 -07001173class HNewInstance : public HExpression<0> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001174 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001175 HNewInstance(uint32_t dex_pc, uint16_t type_index) : HExpression(Primitive::kPrimNot),
1176 dex_pc_(dex_pc), type_index_(type_index) {}
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001177
1178 uint32_t GetDexPc() const { return dex_pc_; }
1179 uint16_t GetTypeIndex() const { return type_index_; }
1180
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001181 // Calls runtime so needs an environment.
1182 virtual bool NeedsEnvironment() const { return true; }
1183
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001184 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001185
1186 private:
1187 const uint32_t dex_pc_;
1188 const uint16_t type_index_;
1189
1190 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
1191};
1192
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001193class HAdd : public HBinaryOperation {
1194 public:
1195 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1196 : HBinaryOperation(result_type, left, right) {}
1197
1198 virtual bool IsCommutative() { return true; }
1199
1200 DECLARE_INSTRUCTION(Add);
1201
1202 private:
1203 DISALLOW_COPY_AND_ASSIGN(HAdd);
1204};
1205
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001206class HSub : public HBinaryOperation {
1207 public:
1208 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1209 : HBinaryOperation(result_type, left, right) {}
1210
1211 virtual bool IsCommutative() { return false; }
1212
1213 DECLARE_INSTRUCTION(Sub);
1214
1215 private:
1216 DISALLOW_COPY_AND_ASSIGN(HSub);
1217};
1218
1219// The value of a parameter in this method. Its location depends on
1220// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07001221class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001222 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001223 HParameterValue(uint8_t index, Primitive::Type parameter_type)
Dave Allison20dfc792014-06-16 20:44:29 -07001224 : HExpression(parameter_type), index_(index) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001225
1226 uint8_t GetIndex() const { return index_; }
1227
1228 DECLARE_INSTRUCTION(ParameterValue);
1229
1230 private:
1231 // The index of this parameter in the parameters list. Must be less
1232 // than HGraph::number_of_in_vregs_;
1233 const uint8_t index_;
1234
1235 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
1236};
1237
Dave Allison20dfc792014-06-16 20:44:29 -07001238class HNot : public HExpression<1> {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001239 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001240 explicit HNot(HInstruction* input) : HExpression(Primitive::kPrimBoolean) {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001241 SetRawInputAt(0, input);
1242 }
1243
1244 DECLARE_INSTRUCTION(Not);
1245
1246 private:
1247 DISALLOW_COPY_AND_ASSIGN(HNot);
1248};
1249
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001250class HPhi : public HInstruction {
1251 public:
1252 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
1253 : inputs_(arena, number_of_inputs),
1254 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001255 type_(type),
1256 is_live_(false) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001257 inputs_.SetSize(number_of_inputs);
1258 }
1259
1260 virtual size_t InputCount() const { return inputs_.Size(); }
1261 virtual HInstruction* InputAt(size_t i) const { return inputs_.Get(i); }
1262
1263 virtual void SetRawInputAt(size_t index, HInstruction* input) {
1264 inputs_.Put(index, input);
1265 }
1266
1267 void AddInput(HInstruction* input);
1268
1269 virtual Primitive::Type GetType() const { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001270 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001271
1272 uint32_t GetRegNumber() const { return reg_number_; }
1273
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001274 void SetDead() { is_live_ = false; }
1275 void SetLive() { is_live_ = true; }
1276 bool IsDead() const { return !is_live_; }
1277 bool IsLive() const { return is_live_; }
1278
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001279 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001280
1281 protected:
1282 GrowableArray<HInstruction*> inputs_;
1283 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001284 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001285 bool is_live_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001286
1287 private:
1288 DISALLOW_COPY_AND_ASSIGN(HPhi);
1289};
1290
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001291class HNullCheck : public HExpression<1> {
1292 public:
1293 HNullCheck(HInstruction* value, uint32_t dex_pc)
1294 : HExpression(value->GetType()), dex_pc_(dex_pc) {
1295 SetRawInputAt(0, value);
1296 }
1297
1298 virtual bool NeedsEnvironment() const { return true; }
1299
1300 uint32_t GetDexPc() const { return dex_pc_; }
1301
1302 DECLARE_INSTRUCTION(NullCheck);
1303
1304 private:
1305 const uint32_t dex_pc_;
1306
1307 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
1308};
1309
1310class FieldInfo : public ValueObject {
1311 public:
1312 explicit FieldInfo(MemberOffset field_offset)
1313 : field_offset_(field_offset) {}
1314
1315 MemberOffset GetFieldOffset() const { return field_offset_; }
1316
1317 private:
1318 const MemberOffset field_offset_;
1319};
1320
1321class HInstanceFieldGet : public HExpression<1> {
1322 public:
1323 HInstanceFieldGet(HInstruction* value,
1324 Primitive::Type field_type,
1325 MemberOffset field_offset)
1326 : HExpression(field_type), field_info_(field_offset) {
1327 SetRawInputAt(0, value);
1328 }
1329
1330 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
1331
1332 DECLARE_INSTRUCTION(InstanceFieldGet);
1333
1334 private:
1335 const FieldInfo field_info_;
1336
1337 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
1338};
1339
1340class HInstanceFieldSet : public HTemplateInstruction<2> {
1341 public:
1342 HInstanceFieldSet(HInstruction* object,
1343 HInstruction* value,
1344 MemberOffset field_offset)
1345 : field_info_(field_offset) {
1346 SetRawInputAt(0, object);
1347 SetRawInputAt(1, value);
1348 }
1349
1350 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
1351
1352 DECLARE_INSTRUCTION(InstanceFieldSet);
1353
1354 private:
1355 const FieldInfo field_info_;
1356
1357 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
1358};
1359
1360/**
1361 * Some DEX instructions are folded into multiple HInstructions that need
1362 * to stay live until the last HInstruction. This class
1363 * is used as a marker for the baseline compiler to ensure its preceding
1364 * HInstruction stays live. `index` is the temporary number that is used
1365 * for knowing the stack offset where to store the instruction.
1366 */
1367class HTemporary : public HTemplateInstruction<0> {
1368 public:
1369 explicit HTemporary(size_t index) : index_(index) {}
1370
1371 size_t GetIndex() const { return index_; }
1372
1373 DECLARE_INSTRUCTION(Temporary);
1374
1375 private:
1376 const size_t index_;
1377
1378 DISALLOW_COPY_AND_ASSIGN(HTemporary);
1379};
1380
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001381class MoveOperands : public ArenaObject {
1382 public:
1383 MoveOperands(Location source, Location destination)
1384 : source_(source), destination_(destination) {}
1385
1386 Location GetSource() const { return source_; }
1387 Location GetDestination() const { return destination_; }
1388
1389 void SetSource(Location value) { source_ = value; }
1390 void SetDestination(Location value) { destination_ = value; }
1391
1392 // The parallel move resolver marks moves as "in-progress" by clearing the
1393 // destination (but not the source).
1394 Location MarkPending() {
1395 DCHECK(!IsPending());
1396 Location dest = destination_;
1397 destination_ = Location::NoLocation();
1398 return dest;
1399 }
1400
1401 void ClearPending(Location dest) {
1402 DCHECK(IsPending());
1403 destination_ = dest;
1404 }
1405
1406 bool IsPending() const {
1407 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
1408 return destination_.IsInvalid() && !source_.IsInvalid();
1409 }
1410
1411 // True if this blocks a move from the given location.
1412 bool Blocks(Location loc) const {
1413 return !IsEliminated() && source_.Equals(loc);
1414 }
1415
1416 // A move is redundant if it's been eliminated, if its source and
1417 // destination are the same, or if its destination is unneeded.
1418 bool IsRedundant() const {
1419 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
1420 }
1421
1422 // We clear both operands to indicate move that's been eliminated.
1423 void Eliminate() {
1424 source_ = destination_ = Location::NoLocation();
1425 }
1426
1427 bool IsEliminated() const {
1428 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
1429 return source_.IsInvalid();
1430 }
1431
1432 private:
1433 Location source_;
1434 Location destination_;
1435
1436 DISALLOW_COPY_AND_ASSIGN(MoveOperands);
1437};
1438
1439static constexpr size_t kDefaultNumberOfMoves = 4;
1440
1441class HParallelMove : public HTemplateInstruction<0> {
1442 public:
1443 explicit HParallelMove(ArenaAllocator* arena) : moves_(arena, kDefaultNumberOfMoves) {}
1444
1445 void AddMove(MoveOperands* move) {
1446 moves_.Add(move);
1447 }
1448
1449 MoveOperands* MoveOperandsAt(size_t index) const {
1450 return moves_.Get(index);
1451 }
1452
1453 size_t NumMoves() const { return moves_.Size(); }
1454
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001455 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001456
1457 private:
1458 GrowableArray<MoveOperands*> moves_;
1459
1460 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
1461};
1462
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001463class HGraphVisitor : public ValueObject {
1464 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001465 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
1466 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001467
Dave Allison20dfc792014-06-16 20:44:29 -07001468 virtual void VisitInstruction(HInstruction* instruction) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001469 virtual void VisitBasicBlock(HBasicBlock* block);
1470
1471 void VisitInsertionOrder();
1472
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001473 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001474
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001475 // Visit functions for instruction classes.
1476#define DECLARE_VISIT_INSTRUCTION(name) \
1477 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
1478
1479 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
1480
1481#undef DECLARE_VISIT_INSTRUCTION
1482
1483 private:
1484 HGraph* graph_;
1485
1486 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
1487};
1488
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001489class HInsertionOrderIterator : public ValueObject {
1490 public:
1491 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
1492
1493 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
1494 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
1495 void Advance() { ++index_; }
1496
1497 private:
1498 const HGraph& graph_;
1499 size_t index_;
1500
1501 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
1502};
1503
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001504class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001505 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001506 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001507
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001508 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
1509 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001510 void Advance() { ++index_; }
1511
1512 private:
1513 const HGraph& graph_;
1514 size_t index_;
1515
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001516 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001517};
1518
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001519class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001520 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001521 explicit HPostOrderIterator(const HGraph& graph)
1522 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001523
1524 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001525 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001526 void Advance() { --index_; }
1527
1528 private:
1529 const HGraph& graph_;
1530 size_t index_;
1531
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001532 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001533};
1534
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001535} // namespace art
1536
1537#endif // ART_COMPILER_OPTIMIZING_NODES_H_