blob: 61a6f6b1b6dc0fe483dc42042893f144b336831c [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
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100411#define FOR_EACH_CONCRETE_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
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100443#define FOR_EACH_INSTRUCTION(M) \
444 FOR_EACH_CONCRETE_INSTRUCTION(M) \
445 M(Constant)
Dave Allison20dfc792014-06-16 20:44:29 -0700446
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000447#define FORWARD_DECLARATION(type) class H##type;
448FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
449#undef FORWARD_DECLARATION
450
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000451#define DECLARE_INSTRUCTION(type) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000452 virtual const char* DebugName() const { return #type; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000453 virtual H##type* As##type() { return this; } \
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100454 virtual void Accept(HGraphVisitor* visitor) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000455
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100456template <typename T>
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000457class HUseListNode : public ArenaObject {
458 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100459 HUseListNode(T* user, size_t index, HUseListNode* tail)
Dave Allison20dfc792014-06-16 20:44:29 -0700460 : user_(user), index_(index), tail_(tail) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000461
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000462 HUseListNode* GetTail() const { return tail_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100463 T* GetUser() const { return user_; }
464 size_t GetIndex() const { return index_; }
465
466 void SetTail(HUseListNode<T>* node) { tail_ = node; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000467
468 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100469 T* const user_;
470 const size_t index_;
471 HUseListNode<T>* tail_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000472
473 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
474};
475
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000476class HInstruction : public ArenaObject {
477 public:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000478 HInstruction()
479 : previous_(nullptr),
480 next_(nullptr),
481 block_(nullptr),
482 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100483 ssa_index_(-1),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000484 uses_(nullptr),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100485 env_uses_(nullptr),
486 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100487 locations_(nullptr),
488 live_interval_(nullptr),
489 lifetime_position_(kNoLifetime) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000490
Dave Allison20dfc792014-06-16 20:44:29 -0700491 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000492
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000493 HInstruction* GetNext() const { return next_; }
494 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000495
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000496 HBasicBlock* GetBlock() const { return block_; }
497 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100498 bool IsInBlock() const { return block_ != nullptr; }
499 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000500
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100501 virtual size_t InputCount() const = 0;
502 virtual HInstruction* InputAt(size_t i) const = 0;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000503
504 virtual void Accept(HGraphVisitor* visitor) = 0;
505 virtual const char* DebugName() const = 0;
506
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100507 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100508 virtual void SetRawInputAt(size_t index, HInstruction* input) = 0;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100509
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100510 virtual bool NeedsEnvironment() const { return false; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100511 virtual bool IsControlFlow() const { return false; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100512
513 void AddUseAt(HInstruction* user, size_t index) {
514 uses_ = new (block_->GetGraph()->GetArena()) HUseListNode<HInstruction>(user, index, uses_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000515 }
516
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100517 void AddEnvUseAt(HEnvironment* user, size_t index) {
518 env_uses_ = new (block_->GetGraph()->GetArena()) HUseListNode<HEnvironment>(
519 user, index, env_uses_);
520 }
521
522 void RemoveUser(HInstruction* user, size_t index);
523
524 HUseListNode<HInstruction>* GetUses() const { return uses_; }
525 HUseListNode<HEnvironment>* GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000526
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100527 bool HasUses() const { return uses_ != nullptr || env_uses_ != nullptr; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100528 bool HasEnvironmentUses() const { return env_uses_ != nullptr; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000529
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100530 size_t NumberOfUses() const {
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100531 // TODO: Optimize this method if it is used outside of the HGraphVisualizer.
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100532 size_t result = 0;
533 HUseListNode<HInstruction>* current = uses_;
534 while (current != nullptr) {
535 current = current->GetTail();
536 ++result;
537 }
538 return result;
539 }
540
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000541 int GetId() const { return id_; }
542 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000543
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100544 int GetSsaIndex() const { return ssa_index_; }
545 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
546 bool HasSsaIndex() const { return ssa_index_ != -1; }
547
548 bool HasEnvironment() const { return environment_ != nullptr; }
549 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100550 void SetEnvironment(HEnvironment* environment) { environment_ = environment; }
551
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000552 LocationSummary* GetLocations() const { return locations_; }
553 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000554
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100555 void ReplaceWith(HInstruction* instruction);
556
Dave Allison20dfc792014-06-16 20:44:29 -0700557 bool HasOnlyOneUse() const {
558 return uses_ != nullptr && uses_->GetTail() == nullptr;
559 }
560
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000561#define INSTRUCTION_TYPE_CHECK(type) \
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100562 bool Is##type() { return (As##type() != nullptr); } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000563 virtual H##type* As##type() { return nullptr; }
564
565 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
566#undef INSTRUCTION_TYPE_CHECK
567
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100568 size_t GetLifetimePosition() const { return lifetime_position_; }
569 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
570 LiveInterval* GetLiveInterval() const { return live_interval_; }
571 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
572 bool HasLiveInterval() const { return live_interval_ != nullptr; }
573
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000574 private:
575 HInstruction* previous_;
576 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000577 HBasicBlock* block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000578
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000579 // An instruction gets an id when it is added to the graph.
580 // It reflects creation order. A negative id means the instruction
581 // has not beed added to the graph.
582 int id_;
583
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100584 // When doing liveness analysis, instructions that have uses get an SSA index.
585 int ssa_index_;
586
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100587 // List of instructions that have this instruction as input.
588 HUseListNode<HInstruction>* uses_;
589
590 // List of environments that contain this instruction.
591 HUseListNode<HEnvironment>* env_uses_;
592
593 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000594
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000595 // Set by the code generator.
596 LocationSummary* locations_;
597
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100598 // Set by the liveness analysis.
599 LiveInterval* live_interval_;
600
601 // Set by the liveness analysis, this is the position in a linear
602 // order of blocks where this instruction's live interval start.
603 size_t lifetime_position_;
604
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000605 friend class HBasicBlock;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100606 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000607
608 DISALLOW_COPY_AND_ASSIGN(HInstruction);
609};
610
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100611template<typename T>
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000612class HUseIterator : public ValueObject {
613 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100614 explicit HUseIterator(HUseListNode<T>* uses) : current_(uses) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000615
616 bool Done() const { return current_ == nullptr; }
617
618 void Advance() {
619 DCHECK(!Done());
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000620 current_ = current_->GetTail();
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000621 }
622
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100623 HUseListNode<T>* Current() const {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000624 DCHECK(!Done());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100625 return current_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000626 }
627
628 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100629 HUseListNode<T>* current_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000630
631 friend class HValue;
632};
633
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100634// A HEnvironment object contains the values of virtual registers at a given location.
635class HEnvironment : public ArenaObject {
636 public:
637 HEnvironment(ArenaAllocator* arena, size_t number_of_vregs) : vregs_(arena, number_of_vregs) {
638 vregs_.SetSize(number_of_vregs);
639 for (size_t i = 0; i < number_of_vregs; i++) {
640 vregs_.Put(i, nullptr);
641 }
642 }
643
644 void Populate(const GrowableArray<HInstruction*>& env) {
645 for (size_t i = 0; i < env.Size(); i++) {
646 HInstruction* instruction = env.Get(i);
647 vregs_.Put(i, instruction);
648 if (instruction != nullptr) {
649 instruction->AddEnvUseAt(this, i);
650 }
651 }
652 }
653
654 void SetRawEnvAt(size_t index, HInstruction* instruction) {
655 vregs_.Put(index, instruction);
656 }
657
658 GrowableArray<HInstruction*>* GetVRegs() {
659 return &vregs_;
660 }
661
662 private:
663 GrowableArray<HInstruction*> vregs_;
664
665 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
666};
667
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000668class HInputIterator : public ValueObject {
669 public:
Dave Allison20dfc792014-06-16 20:44:29 -0700670 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000671
672 bool Done() const { return index_ == instruction_->InputCount(); }
673 HInstruction* Current() const { return instruction_->InputAt(index_); }
674 void Advance() { index_++; }
675
676 private:
677 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100678 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000679
680 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
681};
682
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000683class HInstructionIterator : public ValueObject {
684 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100685 explicit HInstructionIterator(const HInstructionList& instructions)
686 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000687 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000688 }
689
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000690 bool Done() const { return instruction_ == nullptr; }
691 HInstruction* Current() const { return instruction_; }
692 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000693 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000694 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000695 }
696
697 private:
698 HInstruction* instruction_;
699 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100700
701 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000702};
703
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100704class HBackwardInstructionIterator : public ValueObject {
705 public:
706 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
707 : instruction_(instructions.last_instruction_) {
708 next_ = Done() ? nullptr : instruction_->GetPrevious();
709 }
710
711 bool Done() const { return instruction_ == nullptr; }
712 HInstruction* Current() const { return instruction_; }
713 void Advance() {
714 instruction_ = next_;
715 next_ = Done() ? nullptr : instruction_->GetPrevious();
716 }
717
718 private:
719 HInstruction* instruction_;
720 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100721
722 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100723};
724
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000725// An embedded container with N elements of type T. Used (with partial
726// specialization for N=0) because embedded arrays cannot have size 0.
727template<typename T, intptr_t N>
728class EmbeddedArray {
729 public:
Dave Allison20dfc792014-06-16 20:44:29 -0700730 EmbeddedArray() : elements_() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000731
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000732 intptr_t GetLength() const { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000733
734 const T& operator[](intptr_t i) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000735 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000736 return elements_[i];
737 }
738
739 T& operator[](intptr_t i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000740 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000741 return elements_[i];
742 }
743
744 const T& At(intptr_t i) const {
745 return (*this)[i];
746 }
747
748 void SetAt(intptr_t i, const T& val) {
749 (*this)[i] = val;
750 }
751
752 private:
753 T elements_[N];
754};
755
756template<typename T>
757class EmbeddedArray<T, 0> {
758 public:
759 intptr_t length() const { return 0; }
760 const T& operator[](intptr_t i) const {
761 LOG(FATAL) << "Unreachable";
762 static T sentinel = 0;
763 return sentinel;
764 }
765 T& operator[](intptr_t i) {
766 LOG(FATAL) << "Unreachable";
767 static T sentinel = 0;
768 return sentinel;
769 }
770};
771
772template<intptr_t N>
773class HTemplateInstruction: public HInstruction {
774 public:
Dave Allison20dfc792014-06-16 20:44:29 -0700775 HTemplateInstruction<N>() : inputs_() {}
776 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000777
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100778 virtual size_t InputCount() const { return N; }
779 virtual HInstruction* InputAt(size_t i) const { return inputs_[i]; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000780
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000781 protected:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100782 virtual void SetRawInputAt(size_t i, HInstruction* instruction) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000783 inputs_[i] = instruction;
784 }
785
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000786 private:
787 EmbeddedArray<HInstruction*, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100788
789 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000790};
791
Dave Allison20dfc792014-06-16 20:44:29 -0700792template<intptr_t N>
793class HExpression: public HTemplateInstruction<N> {
794 public:
795 explicit HExpression<N>(Primitive::Type type) : type_(type) {}
796 virtual ~HExpression() {}
797
798 virtual Primitive::Type GetType() const { return type_; }
799
800 private:
801 const Primitive::Type type_;
802};
803
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000804// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
805// instruction that branches to the exit block.
806class HReturnVoid : public HTemplateInstruction<0> {
807 public:
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100808 HReturnVoid() {}
809
810 virtual bool IsControlFlow() const { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000811
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100812 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000813
814 private:
815 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
816};
817
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000818// Represents dex's RETURN opcodes. A HReturn is a control flow
819// instruction that branches to the exit block.
820class HReturn : public HTemplateInstruction<1> {
821 public:
822 explicit HReturn(HInstruction* value) {
823 SetRawInputAt(0, value);
824 }
825
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100826 virtual bool IsControlFlow() const { return true; }
827
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100828 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000829
830 private:
831 DISALLOW_COPY_AND_ASSIGN(HReturn);
832};
833
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000834// The exit instruction is the only instruction of the exit block.
835// Instructions aborting the method (HTrow and HReturn) must branch to the
836// exit block.
837class HExit : public HTemplateInstruction<0> {
838 public:
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100839 HExit() {}
840
841 virtual bool IsControlFlow() const { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000842
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100843 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000844
845 private:
846 DISALLOW_COPY_AND_ASSIGN(HExit);
847};
848
849// Jumps from one block to another.
850class HGoto : public HTemplateInstruction<0> {
851 public:
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100852 HGoto() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000853
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000854 HBasicBlock* GetSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100855 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000856 }
857
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100858 virtual bool IsControlFlow() const { return true; }
859
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100860 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000861
862 private:
863 DISALLOW_COPY_AND_ASSIGN(HGoto);
864};
865
Dave Allison20dfc792014-06-16 20:44:29 -0700866
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000867// Conditional branch. A block ending with an HIf instruction must have
868// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000869class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000870 public:
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000871 explicit HIf(HInstruction* input) {
872 SetRawInputAt(0, input);
873 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000874
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000875 HBasicBlock* IfTrueSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100876 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000877 }
878
879 HBasicBlock* IfFalseSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100880 return GetBlock()->GetSuccessors().Get(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000881 }
882
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100883 virtual bool IsControlFlow() const { return true; }
884
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100885 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000886
Dave Allison20dfc792014-06-16 20:44:29 -0700887 virtual bool IsIfInstruction() const { return true; }
888
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000889 private:
890 DISALLOW_COPY_AND_ASSIGN(HIf);
891};
892
Dave Allison20dfc792014-06-16 20:44:29 -0700893class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000894 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000895 HBinaryOperation(Primitive::Type result_type,
896 HInstruction* left,
Dave Allison20dfc792014-06-16 20:44:29 -0700897 HInstruction* right) : HExpression(result_type) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000898 SetRawInputAt(0, left);
899 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000900 }
901
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000902 HInstruction* GetLeft() const { return InputAt(0); }
903 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -0700904 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000905
906 virtual bool IsCommutative() { return false; }
907
908 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000909 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
910};
911
Dave Allison20dfc792014-06-16 20:44:29 -0700912class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000913 public:
Dave Allison20dfc792014-06-16 20:44:29 -0700914 HCondition(HInstruction* first, HInstruction* second)
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000915 : HBinaryOperation(Primitive::kPrimBoolean, first, second) {}
916
917 virtual bool IsCommutative() { return true; }
Dave Allison20dfc792014-06-16 20:44:29 -0700918 bool NeedsMaterialization() const;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000919
Dave Allison20dfc792014-06-16 20:44:29 -0700920 DECLARE_INSTRUCTION(Condition);
921
922 virtual IfCondition GetCondition() const = 0;
923
924 private:
925 DISALLOW_COPY_AND_ASSIGN(HCondition);
926};
927
928// Instruction to check if two inputs are equal to each other.
929class HEqual : public HCondition {
930 public:
931 HEqual(HInstruction* first, HInstruction* second)
932 : HCondition(first, second) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100933
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100934 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000935
Dave Allison20dfc792014-06-16 20:44:29 -0700936 virtual IfCondition GetCondition() const {
937 return kCondEQ;
938 }
939
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000940 private:
941 DISALLOW_COPY_AND_ASSIGN(HEqual);
942};
943
Dave Allison20dfc792014-06-16 20:44:29 -0700944class HNotEqual : public HCondition {
945 public:
946 HNotEqual(HInstruction* first, HInstruction* second)
947 : HCondition(first, second) {}
948
949 DECLARE_INSTRUCTION(NotEqual);
950
951 virtual IfCondition GetCondition() const {
952 return kCondNE;
953 }
954
955 private:
956 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
957};
958
959class HLessThan : public HCondition {
960 public:
961 HLessThan(HInstruction* first, HInstruction* second)
962 : HCondition(first, second) {}
963
964 DECLARE_INSTRUCTION(LessThan);
965
966 virtual IfCondition GetCondition() const {
967 return kCondLT;
968 }
969
970 private:
971 DISALLOW_COPY_AND_ASSIGN(HLessThan);
972};
973
974class HLessThanOrEqual : public HCondition {
975 public:
976 HLessThanOrEqual(HInstruction* first, HInstruction* second)
977 : HCondition(first, second) {}
978
979 DECLARE_INSTRUCTION(LessThanOrEqual);
980
981 virtual IfCondition GetCondition() const {
982 return kCondLE;
983 }
984
985 private:
986 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
987};
988
989class HGreaterThan : public HCondition {
990 public:
991 HGreaterThan(HInstruction* first, HInstruction* second)
992 : HCondition(first, second) {}
993
994 DECLARE_INSTRUCTION(GreaterThan);
995
996 virtual IfCondition GetCondition() const {
997 return kCondGT;
998 }
999
1000 private:
1001 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
1002};
1003
1004class HGreaterThanOrEqual : public HCondition {
1005 public:
1006 HGreaterThanOrEqual(HInstruction* first, HInstruction* second)
1007 : HCondition(first, second) {}
1008
1009 DECLARE_INSTRUCTION(GreaterThanOrEqual);
1010
1011 virtual IfCondition GetCondition() const {
1012 return kCondGE;
1013 }
1014
1015 private:
1016 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
1017};
1018
1019
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001020// Instruction to check how two inputs compare to each other.
1021// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
1022class HCompare : public HBinaryOperation {
1023 public:
1024 HCompare(Primitive::Type type, HInstruction* first, HInstruction* second)
1025 : HBinaryOperation(Primitive::kPrimInt, first, second) {
1026 DCHECK_EQ(type, first->GetType());
1027 DCHECK_EQ(type, second->GetType());
1028 }
1029
1030 DECLARE_INSTRUCTION(Compare);
1031
1032 private:
1033 DISALLOW_COPY_AND_ASSIGN(HCompare);
1034};
1035
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001036// A local in the graph. Corresponds to a Dex register.
1037class HLocal : public HTemplateInstruction<0> {
1038 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001039 explicit HLocal(uint16_t reg_number) : reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001040
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001041 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001042
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001043 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001044
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001045 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001046 // The Dex register number.
1047 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001048
1049 DISALLOW_COPY_AND_ASSIGN(HLocal);
1050};
1051
1052// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07001053class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001054 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001055 explicit HLoadLocal(HLocal* local, Primitive::Type type) : HExpression(type) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001056 SetRawInputAt(0, local);
1057 }
1058
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001059 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1060
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001061 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001062
1063 private:
1064 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
1065};
1066
1067// Store a value in a given local. This instruction has two inputs: the value
1068// and the local.
1069class HStoreLocal : public HTemplateInstruction<2> {
1070 public:
1071 HStoreLocal(HLocal* local, HInstruction* value) {
1072 SetRawInputAt(0, local);
1073 SetRawInputAt(1, value);
1074 }
1075
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001076 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1077
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001078 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001079
1080 private:
1081 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
1082};
1083
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001084class HConstant : public HExpression<0> {
1085 public:
1086 explicit HConstant(Primitive::Type type) : HExpression(type) {}
1087
1088 DECLARE_INSTRUCTION(Constant);
1089
1090 private:
1091 DISALLOW_COPY_AND_ASSIGN(HConstant);
1092};
1093
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001094// Constants of the type int. Those can be from Dex instructions, or
1095// synthesized (for example with the if-eqz instruction).
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001096class HIntConstant : public HConstant {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001097 public:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001098 explicit HIntConstant(int32_t value) : HConstant(Primitive::kPrimInt), value_(value) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001099
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001100 int32_t GetValue() const { return value_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001101
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001102 DECLARE_INSTRUCTION(IntConstant);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001103
1104 private:
1105 const int32_t value_;
1106
1107 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
1108};
1109
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001110class HLongConstant : public HConstant {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001111 public:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001112 explicit HLongConstant(int64_t value) : HConstant(Primitive::kPrimLong), value_(value) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001113
1114 int64_t GetValue() const { return value_; }
1115
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001116 DECLARE_INSTRUCTION(LongConstant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001117
1118 private:
1119 const int64_t value_;
1120
1121 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
1122};
1123
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001124class HInvoke : public HInstruction {
1125 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001126 HInvoke(ArenaAllocator* arena,
1127 uint32_t number_of_arguments,
1128 Primitive::Type return_type,
1129 uint32_t dex_pc)
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001130 : inputs_(arena, number_of_arguments),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001131 return_type_(return_type),
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001132 dex_pc_(dex_pc) {
1133 inputs_.SetSize(number_of_arguments);
1134 }
1135
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001136 virtual size_t InputCount() const { return inputs_.Size(); }
1137 virtual HInstruction* InputAt(size_t i) const { return inputs_.Get(i); }
1138
1139 // Runtime needs to walk the stack, so Dex -> Dex calls need to
1140 // know their environment.
1141 virtual bool NeedsEnvironment() const { return true; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001142
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001143 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001144 SetRawInputAt(index, argument);
1145 }
1146
1147 virtual void SetRawInputAt(size_t index, HInstruction* input) {
1148 inputs_.Put(index, input);
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001149 }
1150
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001151 virtual Primitive::Type GetType() const { return return_type_; }
1152
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001153 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001154
1155 protected:
1156 GrowableArray<HInstruction*> inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001157 const Primitive::Type return_type_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001158 const uint32_t dex_pc_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001159
1160 private:
1161 DISALLOW_COPY_AND_ASSIGN(HInvoke);
1162};
1163
1164class HInvokeStatic : public HInvoke {
1165 public:
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001166 HInvokeStatic(ArenaAllocator* arena,
1167 uint32_t number_of_arguments,
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001168 Primitive::Type return_type,
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001169 uint32_t dex_pc,
1170 uint32_t index_in_dex_cache)
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001171 : HInvoke(arena, number_of_arguments, return_type, dex_pc),
1172 index_in_dex_cache_(index_in_dex_cache) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001173
1174 uint32_t GetIndexInDexCache() const { return index_in_dex_cache_; }
1175
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001176 DECLARE_INSTRUCTION(InvokeStatic);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001177
1178 private:
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001179 const uint32_t index_in_dex_cache_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001180
1181 DISALLOW_COPY_AND_ASSIGN(HInvokeStatic);
1182};
1183
Dave Allison20dfc792014-06-16 20:44:29 -07001184class HNewInstance : public HExpression<0> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001185 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001186 HNewInstance(uint32_t dex_pc, uint16_t type_index) : HExpression(Primitive::kPrimNot),
1187 dex_pc_(dex_pc), type_index_(type_index) {}
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001188
1189 uint32_t GetDexPc() const { return dex_pc_; }
1190 uint16_t GetTypeIndex() const { return type_index_; }
1191
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001192 // Calls runtime so needs an environment.
1193 virtual bool NeedsEnvironment() const { return true; }
1194
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001195 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001196
1197 private:
1198 const uint32_t dex_pc_;
1199 const uint16_t type_index_;
1200
1201 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
1202};
1203
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001204class HAdd : public HBinaryOperation {
1205 public:
1206 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1207 : HBinaryOperation(result_type, left, right) {}
1208
1209 virtual bool IsCommutative() { return true; }
1210
1211 DECLARE_INSTRUCTION(Add);
1212
1213 private:
1214 DISALLOW_COPY_AND_ASSIGN(HAdd);
1215};
1216
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001217class HSub : public HBinaryOperation {
1218 public:
1219 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1220 : HBinaryOperation(result_type, left, right) {}
1221
1222 virtual bool IsCommutative() { return false; }
1223
1224 DECLARE_INSTRUCTION(Sub);
1225
1226 private:
1227 DISALLOW_COPY_AND_ASSIGN(HSub);
1228};
1229
1230// The value of a parameter in this method. Its location depends on
1231// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07001232class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001233 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001234 HParameterValue(uint8_t index, Primitive::Type parameter_type)
Dave Allison20dfc792014-06-16 20:44:29 -07001235 : HExpression(parameter_type), index_(index) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001236
1237 uint8_t GetIndex() const { return index_; }
1238
1239 DECLARE_INSTRUCTION(ParameterValue);
1240
1241 private:
1242 // The index of this parameter in the parameters list. Must be less
1243 // than HGraph::number_of_in_vregs_;
1244 const uint8_t index_;
1245
1246 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
1247};
1248
Dave Allison20dfc792014-06-16 20:44:29 -07001249class HNot : public HExpression<1> {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001250 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001251 explicit HNot(HInstruction* input) : HExpression(Primitive::kPrimBoolean) {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001252 SetRawInputAt(0, input);
1253 }
1254
1255 DECLARE_INSTRUCTION(Not);
1256
1257 private:
1258 DISALLOW_COPY_AND_ASSIGN(HNot);
1259};
1260
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001261class HPhi : public HInstruction {
1262 public:
1263 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
1264 : inputs_(arena, number_of_inputs),
1265 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001266 type_(type),
1267 is_live_(false) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001268 inputs_.SetSize(number_of_inputs);
1269 }
1270
1271 virtual size_t InputCount() const { return inputs_.Size(); }
1272 virtual HInstruction* InputAt(size_t i) const { return inputs_.Get(i); }
1273
1274 virtual void SetRawInputAt(size_t index, HInstruction* input) {
1275 inputs_.Put(index, input);
1276 }
1277
1278 void AddInput(HInstruction* input);
1279
1280 virtual Primitive::Type GetType() const { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001281 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001282
1283 uint32_t GetRegNumber() const { return reg_number_; }
1284
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001285 void SetDead() { is_live_ = false; }
1286 void SetLive() { is_live_ = true; }
1287 bool IsDead() const { return !is_live_; }
1288 bool IsLive() const { return is_live_; }
1289
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001290 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001291
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001292 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001293 GrowableArray<HInstruction*> inputs_;
1294 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001295 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001296 bool is_live_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001297
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001298 DISALLOW_COPY_AND_ASSIGN(HPhi);
1299};
1300
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001301class HNullCheck : public HExpression<1> {
1302 public:
1303 HNullCheck(HInstruction* value, uint32_t dex_pc)
1304 : HExpression(value->GetType()), dex_pc_(dex_pc) {
1305 SetRawInputAt(0, value);
1306 }
1307
1308 virtual bool NeedsEnvironment() const { return true; }
1309
1310 uint32_t GetDexPc() const { return dex_pc_; }
1311
1312 DECLARE_INSTRUCTION(NullCheck);
1313
1314 private:
1315 const uint32_t dex_pc_;
1316
1317 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
1318};
1319
1320class FieldInfo : public ValueObject {
1321 public:
1322 explicit FieldInfo(MemberOffset field_offset)
1323 : field_offset_(field_offset) {}
1324
1325 MemberOffset GetFieldOffset() const { return field_offset_; }
1326
1327 private:
1328 const MemberOffset field_offset_;
1329};
1330
1331class HInstanceFieldGet : public HExpression<1> {
1332 public:
1333 HInstanceFieldGet(HInstruction* value,
1334 Primitive::Type field_type,
1335 MemberOffset field_offset)
1336 : HExpression(field_type), field_info_(field_offset) {
1337 SetRawInputAt(0, value);
1338 }
1339
1340 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
1341
1342 DECLARE_INSTRUCTION(InstanceFieldGet);
1343
1344 private:
1345 const FieldInfo field_info_;
1346
1347 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
1348};
1349
1350class HInstanceFieldSet : public HTemplateInstruction<2> {
1351 public:
1352 HInstanceFieldSet(HInstruction* object,
1353 HInstruction* value,
1354 MemberOffset field_offset)
1355 : field_info_(field_offset) {
1356 SetRawInputAt(0, object);
1357 SetRawInputAt(1, value);
1358 }
1359
1360 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
1361
1362 DECLARE_INSTRUCTION(InstanceFieldSet);
1363
1364 private:
1365 const FieldInfo field_info_;
1366
1367 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
1368};
1369
1370/**
1371 * Some DEX instructions are folded into multiple HInstructions that need
1372 * to stay live until the last HInstruction. This class
1373 * is used as a marker for the baseline compiler to ensure its preceding
1374 * HInstruction stays live. `index` is the temporary number that is used
1375 * for knowing the stack offset where to store the instruction.
1376 */
1377class HTemporary : public HTemplateInstruction<0> {
1378 public:
1379 explicit HTemporary(size_t index) : index_(index) {}
1380
1381 size_t GetIndex() const { return index_; }
1382
1383 DECLARE_INSTRUCTION(Temporary);
1384
1385 private:
1386 const size_t index_;
1387
1388 DISALLOW_COPY_AND_ASSIGN(HTemporary);
1389};
1390
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001391class MoveOperands : public ArenaObject {
1392 public:
1393 MoveOperands(Location source, Location destination)
1394 : source_(source), destination_(destination) {}
1395
1396 Location GetSource() const { return source_; }
1397 Location GetDestination() const { return destination_; }
1398
1399 void SetSource(Location value) { source_ = value; }
1400 void SetDestination(Location value) { destination_ = value; }
1401
1402 // The parallel move resolver marks moves as "in-progress" by clearing the
1403 // destination (but not the source).
1404 Location MarkPending() {
1405 DCHECK(!IsPending());
1406 Location dest = destination_;
1407 destination_ = Location::NoLocation();
1408 return dest;
1409 }
1410
1411 void ClearPending(Location dest) {
1412 DCHECK(IsPending());
1413 destination_ = dest;
1414 }
1415
1416 bool IsPending() const {
1417 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
1418 return destination_.IsInvalid() && !source_.IsInvalid();
1419 }
1420
1421 // True if this blocks a move from the given location.
1422 bool Blocks(Location loc) const {
1423 return !IsEliminated() && source_.Equals(loc);
1424 }
1425
1426 // A move is redundant if it's been eliminated, if its source and
1427 // destination are the same, or if its destination is unneeded.
1428 bool IsRedundant() const {
1429 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
1430 }
1431
1432 // We clear both operands to indicate move that's been eliminated.
1433 void Eliminate() {
1434 source_ = destination_ = Location::NoLocation();
1435 }
1436
1437 bool IsEliminated() const {
1438 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
1439 return source_.IsInvalid();
1440 }
1441
1442 private:
1443 Location source_;
1444 Location destination_;
1445
1446 DISALLOW_COPY_AND_ASSIGN(MoveOperands);
1447};
1448
1449static constexpr size_t kDefaultNumberOfMoves = 4;
1450
1451class HParallelMove : public HTemplateInstruction<0> {
1452 public:
1453 explicit HParallelMove(ArenaAllocator* arena) : moves_(arena, kDefaultNumberOfMoves) {}
1454
1455 void AddMove(MoveOperands* move) {
1456 moves_.Add(move);
1457 }
1458
1459 MoveOperands* MoveOperandsAt(size_t index) const {
1460 return moves_.Get(index);
1461 }
1462
1463 size_t NumMoves() const { return moves_.Size(); }
1464
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001465 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001466
1467 private:
1468 GrowableArray<MoveOperands*> moves_;
1469
1470 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
1471};
1472
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001473class HGraphVisitor : public ValueObject {
1474 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001475 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
1476 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001477
Dave Allison20dfc792014-06-16 20:44:29 -07001478 virtual void VisitInstruction(HInstruction* instruction) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001479 virtual void VisitBasicBlock(HBasicBlock* block);
1480
1481 void VisitInsertionOrder();
1482
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001483 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001484
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001485 // Visit functions for instruction classes.
1486#define DECLARE_VISIT_INSTRUCTION(name) \
1487 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
1488
1489 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
1490
1491#undef DECLARE_VISIT_INSTRUCTION
1492
1493 private:
1494 HGraph* graph_;
1495
1496 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
1497};
1498
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001499class HInsertionOrderIterator : public ValueObject {
1500 public:
1501 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
1502
1503 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
1504 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
1505 void Advance() { ++index_; }
1506
1507 private:
1508 const HGraph& graph_;
1509 size_t index_;
1510
1511 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
1512};
1513
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001514class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001515 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001516 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001517
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001518 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
1519 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001520 void Advance() { ++index_; }
1521
1522 private:
1523 const HGraph& graph_;
1524 size_t index_;
1525
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001526 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001527};
1528
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001529class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001530 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001531 explicit HPostOrderIterator(const HGraph& graph)
1532 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001533
1534 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001535 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001536 void Advance() { --index_; }
1537
1538 private:
1539 const HGraph& graph_;
1540 size_t index_;
1541
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001542 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001543};
1544
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001545} // namespace art
1546
1547#endif // ART_COMPILER_OPTIMIZING_NODES_H_