blob: ca846b32a33998ae7ca1958791d8359c37eefcf6 [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"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000022#include "utils/allocation.h"
Nicolas Geoffray0e336432014-02-26 18:24:38 +000023#include "utils/arena_bit_vector.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000024#include "utils/growable_array.h"
25
26namespace art {
27
28class HBasicBlock;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010029class HEnvironment;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000030class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000031class HIntConstant;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000032class HGraphVisitor;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010033class HPhi;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010034class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000035class LocationSummary;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000036
37static const int kDefaultNumberOfBlocks = 8;
38static const int kDefaultNumberOfSuccessors = 2;
39static const int kDefaultNumberOfPredecessors = 2;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000040static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000041
Dave Allison20dfc792014-06-16 20:44:29 -070042enum IfCondition {
43 kCondEQ,
44 kCondNE,
45 kCondLT,
46 kCondLE,
47 kCondGT,
48 kCondGE,
49};
50
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010051class HInstructionList {
52 public:
53 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
54
55 void AddInstruction(HInstruction* instruction);
56 void RemoveInstruction(HInstruction* instruction);
57
58 private:
59 HInstruction* first_instruction_;
60 HInstruction* last_instruction_;
61
62 friend class HBasicBlock;
63 friend class HInstructionIterator;
Nicolas Geoffray804d0932014-05-02 08:46:00 +010064 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010065
66 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
67};
68
Nicolas Geoffray818f2102014-02-18 16:43:35 +000069// Control-flow graph of a method. Contains a list of basic blocks.
70class HGraph : public ArenaObject {
71 public:
72 explicit HGraph(ArenaAllocator* arena)
73 : arena_(arena),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000074 blocks_(arena, kDefaultNumberOfBlocks),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +010075 reverse_post_order_(arena, kDefaultNumberOfBlocks),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +010076 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +010077 number_of_vregs_(0),
78 number_of_in_vregs_(0),
Nicolas Geoffraye5038322014-07-04 09:41:32 +010079 number_of_temporaries_(0),
Dave Allison20dfc792014-06-16 20:44:29 -070080 current_instruction_id_(0) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +000081
Nicolas Geoffray787c3072014-03-17 10:20:19 +000082 ArenaAllocator* GetArena() const { return arena_; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +010083 const GrowableArray<HBasicBlock*>& GetBlocks() const { return blocks_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +000084
Nicolas Geoffray787c3072014-03-17 10:20:19 +000085 HBasicBlock* GetEntryBlock() const { return entry_block_; }
86 HBasicBlock* GetExitBlock() const { return exit_block_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000087
Nicolas Geoffray787c3072014-03-17 10:20:19 +000088 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
89 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000090
Nicolas Geoffray818f2102014-02-18 16:43:35 +000091 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010092
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000093 void BuildDominatorTree();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010094 void TransformToSSA();
95 void SimplifyCFG();
Nicolas Geoffray818f2102014-02-18 16:43:35 +000096
Nicolas Geoffray622d9c32014-05-12 16:11:02 +010097 // Find all natural loops in this graph. Aborts computation and returns false
Nicolas Geoffrayf635e632014-05-14 09:43:38 +010098 // if one loop is not natural, that is the header does not dominate the back
Nicolas Geoffray622d9c32014-05-12 16:11:02 +010099 // edge.
100 bool FindNaturalLoops() const;
101
102 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
103 void SimplifyLoop(HBasicBlock* header);
104
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000105 int GetNextInstructionId() {
106 return current_instruction_id_++;
107 }
108
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100109 uint16_t GetMaximumNumberOfOutVRegs() const {
110 return maximum_number_of_out_vregs_;
111 }
112
113 void UpdateMaximumNumberOfOutVRegs(uint16_t new_value) {
114 maximum_number_of_out_vregs_ = std::max(new_value, maximum_number_of_out_vregs_);
115 }
116
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100117 void UpdateNumberOfTemporaries(size_t count) {
118 number_of_temporaries_ = std::max(count, number_of_temporaries_);
119 }
120
121 size_t GetNumberOfTemporaries() const {
122 return number_of_temporaries_;
123 }
124
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100125 void SetNumberOfVRegs(uint16_t number_of_vregs) {
126 number_of_vregs_ = number_of_vregs;
127 }
128
129 uint16_t GetNumberOfVRegs() const {
130 return number_of_vregs_;
131 }
132
133 void SetNumberOfInVRegs(uint16_t value) {
134 number_of_in_vregs_ = value;
135 }
136
137 uint16_t GetNumberOfInVRegs() const {
138 return number_of_in_vregs_;
139 }
140
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100141 uint16_t GetNumberOfLocalVRegs() const {
142 return number_of_vregs_ - number_of_in_vregs_;
143 }
144
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100145 const GrowableArray<HBasicBlock*>& GetReversePostOrder() const {
146 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100147 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100148
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000149 private:
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000150 HBasicBlock* FindCommonDominator(HBasicBlock* first, HBasicBlock* second) const;
151 void VisitBlockForDominatorTree(HBasicBlock* block,
152 HBasicBlock* predecessor,
153 GrowableArray<size_t>* visits);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100154 void FindBackEdges(ArenaBitVector* visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000155 void VisitBlockForBackEdges(HBasicBlock* block,
156 ArenaBitVector* visited,
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100157 ArenaBitVector* visiting);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000158 void RemoveDeadBlocks(const ArenaBitVector& visited) const;
159
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000160 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000161
162 // List of blocks in insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000163 GrowableArray<HBasicBlock*> blocks_;
164
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100165 // List of blocks to perform a reverse post order tree traversal.
166 GrowableArray<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000167
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000168 HBasicBlock* entry_block_;
169 HBasicBlock* exit_block_;
170
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100171 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100172 uint16_t maximum_number_of_out_vregs_;
173
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100174 // The number of virtual registers in this method. Contains the parameters.
175 uint16_t number_of_vregs_;
176
177 // The number of virtual registers used by parameters of this method.
178 uint16_t number_of_in_vregs_;
179
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100180 // The number of temporaries that will be needed for the baseline compiler.
181 size_t number_of_temporaries_;
182
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000183 // The current id to assign to a newly added instruction. See HInstruction.id_.
184 int current_instruction_id_;
185
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000186 DISALLOW_COPY_AND_ASSIGN(HGraph);
187};
188
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000189class HLoopInformation : public ArenaObject {
190 public:
191 HLoopInformation(HBasicBlock* header, HGraph* graph)
192 : header_(header),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100193 back_edges_(graph->GetArena(), kDefaultNumberOfBackEdges),
194 blocks_(graph->GetArena(), graph->GetBlocks().Size(), false) {}
195
196 HBasicBlock* GetHeader() const {
197 return header_;
198 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000199
200 void AddBackEdge(HBasicBlock* back_edge) {
201 back_edges_.Add(back_edge);
202 }
203
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100204 void RemoveBackEdge(HBasicBlock* back_edge) {
205 back_edges_.Delete(back_edge);
206 }
207
208 bool IsBackEdge(HBasicBlock* block) {
209 for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) {
210 if (back_edges_.Get(i) == block) return true;
211 }
212 return false;
213 }
214
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000215 int NumberOfBackEdges() const {
216 return back_edges_.Size();
217 }
218
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100219 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100220
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100221 const GrowableArray<HBasicBlock*>& GetBackEdges() const {
222 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100223 }
224
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100225 void ClearBackEdges() {
226 back_edges_.Reset();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100227 }
228
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100229 // Find blocks that are part of this loop. Returns whether the loop is a natural loop,
230 // that is the header dominates the back edge.
231 bool Populate();
232
233 // Returns whether this loop information contains `block`.
234 // Note that this loop information *must* be populated before entering this function.
235 bool Contains(const HBasicBlock& block) const;
236
237 // Returns whether this loop information is an inner loop of `other`.
238 // Note that `other` *must* be populated before entering this function.
239 bool IsIn(const HLoopInformation& other) const;
240
241 const ArenaBitVector& GetBlocks() const { return blocks_; }
242
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000243 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100244 // Internal recursive implementation of `Populate`.
245 void PopulateRecursive(HBasicBlock* block);
246
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000247 HBasicBlock* header_;
248 GrowableArray<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100249 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000250
251 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
252};
253
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100254static constexpr size_t kNoLifetime = -1;
255
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000256// A block in a method. Contains the list of instructions represented
257// as a double linked list. Each block knows its predecessors and
258// successors.
259class HBasicBlock : public ArenaObject {
260 public:
261 explicit HBasicBlock(HGraph* graph)
262 : graph_(graph),
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000263 predecessors_(graph->GetArena(), kDefaultNumberOfPredecessors),
264 successors_(graph->GetArena(), kDefaultNumberOfSuccessors),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000265 loop_information_(nullptr),
266 dominator_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100267 block_id_(-1),
268 lifetime_start_(kNoLifetime),
269 lifetime_end_(kNoLifetime) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000270
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100271 const GrowableArray<HBasicBlock*>& GetPredecessors() const {
272 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000273 }
274
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100275 const GrowableArray<HBasicBlock*>& GetSuccessors() const {
276 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000277 }
278
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000279 void AddBackEdge(HBasicBlock* back_edge) {
280 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000281 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000282 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100283 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000284 loop_information_->AddBackEdge(back_edge);
285 }
286
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000287 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000288
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000289 int GetBlockId() const { return block_id_; }
290 void SetBlockId(int id) { block_id_ = id; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000291
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000292 HBasicBlock* GetDominator() const { return dominator_; }
293 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000294
295 int NumberOfBackEdges() const {
296 return loop_information_ == nullptr
297 ? 0
298 : loop_information_->NumberOfBackEdges();
299 }
300
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100301 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
302 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100303 const HInstructionList& GetInstructions() const { return instructions_; }
304 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100305 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000306
307 void AddSuccessor(HBasicBlock* block) {
308 successors_.Add(block);
309 block->predecessors_.Add(this);
310 }
311
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100312 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
313 size_t successor_index = GetSuccessorIndexOf(existing);
314 DCHECK_NE(successor_index, static_cast<size_t>(-1));
315 existing->RemovePredecessor(this);
316 new_block->predecessors_.Add(this);
317 successors_.Put(successor_index, new_block);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000318 }
319
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100320 void RemovePredecessor(HBasicBlock* block) {
321 predecessors_.Delete(block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100322 }
323
324 void ClearAllPredecessors() {
325 predecessors_.Reset();
326 }
327
328 void AddPredecessor(HBasicBlock* block) {
329 predecessors_.Add(block);
330 block->successors_.Add(this);
331 }
332
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100333 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) {
334 for (size_t i = 0, e = predecessors_.Size(); i < e; ++i) {
335 if (predecessors_.Get(i) == predecessor) {
336 return i;
337 }
338 }
339 return -1;
340 }
341
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100342 size_t GetSuccessorIndexOf(HBasicBlock* successor) {
343 for (size_t i = 0, e = successors_.Size(); i < e; ++i) {
344 if (successors_.Get(i) == successor) {
345 return i;
346 }
347 }
348 return -1;
349 }
350
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000351 void AddInstruction(HInstruction* instruction);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100352 void RemoveInstruction(HInstruction* instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100353 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100354 void AddPhi(HPhi* phi);
355 void RemovePhi(HPhi* phi);
356
357 bool IsLoopHeader() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100358 return (loop_information_ != nullptr) && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100359 }
360
361 HLoopInformation* GetLoopInformation() const {
362 return loop_information_;
363 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000364
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100365 // Set the loop_information_ on this block. This method overrides the current
366 // loop_information if it is an outer loop of the passed loop information.
367 void SetInLoop(HLoopInformation* info) {
368 if (IsLoopHeader()) {
369 // Nothing to do. This just means `info` is an outer loop.
370 } else if (loop_information_ == nullptr) {
371 loop_information_ = info;
372 } else if (loop_information_->Contains(*info->GetHeader())) {
373 // Block is currently part of an outer loop. Make it part of this inner loop.
374 // Note that a non loop header having a loop information means this loop information
375 // has already been populated
376 loop_information_ = info;
377 } else {
378 // Block is part of an inner loop. Do not update the loop information.
379 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
380 // at this point, because this method is being called while populating `info`.
381 }
382 }
383
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100384 bool IsInLoop() const { return loop_information_ != nullptr; }
385
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100386 // Returns wheter this block dominates the blocked passed as parameter.
387 bool Dominates(HBasicBlock* block) const;
388
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100389 size_t GetLifetimeStart() const { return lifetime_start_; }
390 size_t GetLifetimeEnd() const { return lifetime_end_; }
391
392 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
393 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
394
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000395 private:
396 HGraph* const graph_;
397 GrowableArray<HBasicBlock*> predecessors_;
398 GrowableArray<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100399 HInstructionList instructions_;
400 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000401 HLoopInformation* loop_information_;
402 HBasicBlock* dominator_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000403 int block_id_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100404 size_t lifetime_start_;
405 size_t lifetime_end_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000406
407 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
408};
409
410#define FOR_EACH_INSTRUCTION(M) \
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000411 M(Add) \
Dave Allison20dfc792014-06-16 20:44:29 -0700412 M(Condition) \
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000413 M(Equal) \
Dave Allison20dfc792014-06-16 20:44:29 -0700414 M(NotEqual) \
415 M(LessThan) \
416 M(LessThanOrEqual) \
417 M(GreaterThan) \
418 M(GreaterThanOrEqual) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000419 M(Exit) \
420 M(Goto) \
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000421 M(If) \
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000422 M(IntConstant) \
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000423 M(InvokeStatic) \
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000424 M(LoadLocal) \
425 M(Local) \
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100426 M(LongConstant) \
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +0100427 M(NewInstance) \
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100428 M(Not) \
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100429 M(ParameterValue) \
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100430 M(ParallelMove) \
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100431 M(Phi) \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000432 M(Return) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000433 M(ReturnVoid) \
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000434 M(StoreLocal) \
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100435 M(Sub) \
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100436 M(Compare) \
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100437 M(InstanceFieldGet) \
438 M(InstanceFieldSet) \
439 M(NullCheck) \
440 M(Temporary) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000441
Dave Allison20dfc792014-06-16 20:44:29 -0700442
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000443#define FORWARD_DECLARATION(type) class H##type;
444FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
445#undef FORWARD_DECLARATION
446
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000447#define DECLARE_INSTRUCTION(type) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000448 virtual const char* DebugName() const { return #type; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000449 virtual H##type* As##type() { return this; } \
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100450 virtual void Accept(HGraphVisitor* visitor) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000451
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100452template <typename T>
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000453class HUseListNode : public ArenaObject {
454 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100455 HUseListNode(T* user, size_t index, HUseListNode* tail)
Dave Allison20dfc792014-06-16 20:44:29 -0700456 : user_(user), index_(index), tail_(tail) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000457
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000458 HUseListNode* GetTail() const { return tail_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100459 T* GetUser() const { return user_; }
460 size_t GetIndex() const { return index_; }
461
462 void SetTail(HUseListNode<T>* node) { tail_ = node; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000463
464 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100465 T* const user_;
466 const size_t index_;
467 HUseListNode<T>* tail_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000468
469 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
470};
471
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000472class HInstruction : public ArenaObject {
473 public:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000474 HInstruction()
475 : previous_(nullptr),
476 next_(nullptr),
477 block_(nullptr),
478 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100479 ssa_index_(-1),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000480 uses_(nullptr),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100481 env_uses_(nullptr),
482 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100483 locations_(nullptr),
484 live_interval_(nullptr),
485 lifetime_position_(kNoLifetime) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000486
Dave Allison20dfc792014-06-16 20:44:29 -0700487 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000488
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000489 HInstruction* GetNext() const { return next_; }
490 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000491
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000492 HBasicBlock* GetBlock() const { return block_; }
493 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100494 bool IsInBlock() const { return block_ != nullptr; }
495 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000496
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100497 virtual size_t InputCount() const = 0;
498 virtual HInstruction* InputAt(size_t i) const = 0;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000499
500 virtual void Accept(HGraphVisitor* visitor) = 0;
501 virtual const char* DebugName() const = 0;
502
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100503 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100504 virtual void SetRawInputAt(size_t index, HInstruction* input) = 0;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100505
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100506 virtual bool NeedsEnvironment() const { return false; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100507 virtual bool IsControlFlow() const { return false; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100508
509 void AddUseAt(HInstruction* user, size_t index) {
510 uses_ = new (block_->GetGraph()->GetArena()) HUseListNode<HInstruction>(user, index, uses_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000511 }
512
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100513 void AddEnvUseAt(HEnvironment* user, size_t index) {
514 env_uses_ = new (block_->GetGraph()->GetArena()) HUseListNode<HEnvironment>(
515 user, index, env_uses_);
516 }
517
518 void RemoveUser(HInstruction* user, size_t index);
519
520 HUseListNode<HInstruction>* GetUses() const { return uses_; }
521 HUseListNode<HEnvironment>* GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000522
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100523 bool HasUses() const { return uses_ != nullptr || env_uses_ != nullptr; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100524 bool HasEnvironmentUses() const { return env_uses_ != nullptr; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000525
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100526 size_t NumberOfUses() const {
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100527 // TODO: Optimize this method if it is used outside of the HGraphVisualizer.
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100528 size_t result = 0;
529 HUseListNode<HInstruction>* current = uses_;
530 while (current != nullptr) {
531 current = current->GetTail();
532 ++result;
533 }
534 return result;
535 }
536
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000537 int GetId() const { return id_; }
538 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000539
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100540 int GetSsaIndex() const { return ssa_index_; }
541 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
542 bool HasSsaIndex() const { return ssa_index_ != -1; }
543
544 bool HasEnvironment() const { return environment_ != nullptr; }
545 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100546 void SetEnvironment(HEnvironment* environment) { environment_ = environment; }
547
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000548 LocationSummary* GetLocations() const { return locations_; }
549 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000550
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100551 void ReplaceWith(HInstruction* instruction);
552
Dave Allison20dfc792014-06-16 20:44:29 -0700553 bool HasOnlyOneUse() const {
554 return uses_ != nullptr && uses_->GetTail() == nullptr;
555 }
556
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000557#define INSTRUCTION_TYPE_CHECK(type) \
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100558 bool Is##type() { return (As##type() != nullptr); } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000559 virtual H##type* As##type() { return nullptr; }
560
561 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
562#undef INSTRUCTION_TYPE_CHECK
563
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100564 size_t GetLifetimePosition() const { return lifetime_position_; }
565 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
566 LiveInterval* GetLiveInterval() const { return live_interval_; }
567 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
568 bool HasLiveInterval() const { return live_interval_ != nullptr; }
569
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000570 private:
571 HInstruction* previous_;
572 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000573 HBasicBlock* block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000574
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000575 // An instruction gets an id when it is added to the graph.
576 // It reflects creation order. A negative id means the instruction
577 // has not beed added to the graph.
578 int id_;
579
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100580 // When doing liveness analysis, instructions that have uses get an SSA index.
581 int ssa_index_;
582
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100583 // List of instructions that have this instruction as input.
584 HUseListNode<HInstruction>* uses_;
585
586 // List of environments that contain this instruction.
587 HUseListNode<HEnvironment>* env_uses_;
588
589 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000590
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000591 // Set by the code generator.
592 LocationSummary* locations_;
593
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100594 // Set by the liveness analysis.
595 LiveInterval* live_interval_;
596
597 // Set by the liveness analysis, this is the position in a linear
598 // order of blocks where this instruction's live interval start.
599 size_t lifetime_position_;
600
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000601 friend class HBasicBlock;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100602 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000603
604 DISALLOW_COPY_AND_ASSIGN(HInstruction);
605};
606
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100607template<typename T>
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000608class HUseIterator : public ValueObject {
609 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100610 explicit HUseIterator(HUseListNode<T>* uses) : current_(uses) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000611
612 bool Done() const { return current_ == nullptr; }
613
614 void Advance() {
615 DCHECK(!Done());
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000616 current_ = current_->GetTail();
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000617 }
618
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100619 HUseListNode<T>* Current() const {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000620 DCHECK(!Done());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100621 return current_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000622 }
623
624 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100625 HUseListNode<T>* current_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000626
627 friend class HValue;
628};
629
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100630// A HEnvironment object contains the values of virtual registers at a given location.
631class HEnvironment : public ArenaObject {
632 public:
633 HEnvironment(ArenaAllocator* arena, size_t number_of_vregs) : vregs_(arena, number_of_vregs) {
634 vregs_.SetSize(number_of_vregs);
635 for (size_t i = 0; i < number_of_vregs; i++) {
636 vregs_.Put(i, nullptr);
637 }
638 }
639
640 void Populate(const GrowableArray<HInstruction*>& env) {
641 for (size_t i = 0; i < env.Size(); i++) {
642 HInstruction* instruction = env.Get(i);
643 vregs_.Put(i, instruction);
644 if (instruction != nullptr) {
645 instruction->AddEnvUseAt(this, i);
646 }
647 }
648 }
649
650 void SetRawEnvAt(size_t index, HInstruction* instruction) {
651 vregs_.Put(index, instruction);
652 }
653
654 GrowableArray<HInstruction*>* GetVRegs() {
655 return &vregs_;
656 }
657
658 private:
659 GrowableArray<HInstruction*> vregs_;
660
661 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
662};
663
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000664class HInputIterator : public ValueObject {
665 public:
Dave Allison20dfc792014-06-16 20:44:29 -0700666 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000667
668 bool Done() const { return index_ == instruction_->InputCount(); }
669 HInstruction* Current() const { return instruction_->InputAt(index_); }
670 void Advance() { index_++; }
671
672 private:
673 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100674 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000675
676 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
677};
678
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000679class HInstructionIterator : public ValueObject {
680 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100681 explicit HInstructionIterator(const HInstructionList& instructions)
682 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000683 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000684 }
685
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000686 bool Done() const { return instruction_ == nullptr; }
687 HInstruction* Current() const { return instruction_; }
688 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000689 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000690 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000691 }
692
693 private:
694 HInstruction* instruction_;
695 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100696
697 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000698};
699
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100700class HBackwardInstructionIterator : public ValueObject {
701 public:
702 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
703 : instruction_(instructions.last_instruction_) {
704 next_ = Done() ? nullptr : instruction_->GetPrevious();
705 }
706
707 bool Done() const { return instruction_ == nullptr; }
708 HInstruction* Current() const { return instruction_; }
709 void Advance() {
710 instruction_ = next_;
711 next_ = Done() ? nullptr : instruction_->GetPrevious();
712 }
713
714 private:
715 HInstruction* instruction_;
716 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100717
718 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100719};
720
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000721// An embedded container with N elements of type T. Used (with partial
722// specialization for N=0) because embedded arrays cannot have size 0.
723template<typename T, intptr_t N>
724class EmbeddedArray {
725 public:
Dave Allison20dfc792014-06-16 20:44:29 -0700726 EmbeddedArray() : elements_() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000727
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000728 intptr_t GetLength() const { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000729
730 const T& operator[](intptr_t i) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000731 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000732 return elements_[i];
733 }
734
735 T& operator[](intptr_t i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000736 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000737 return elements_[i];
738 }
739
740 const T& At(intptr_t i) const {
741 return (*this)[i];
742 }
743
744 void SetAt(intptr_t i, const T& val) {
745 (*this)[i] = val;
746 }
747
748 private:
749 T elements_[N];
750};
751
752template<typename T>
753class EmbeddedArray<T, 0> {
754 public:
755 intptr_t length() const { return 0; }
756 const T& operator[](intptr_t i) const {
757 LOG(FATAL) << "Unreachable";
758 static T sentinel = 0;
759 return sentinel;
760 }
761 T& operator[](intptr_t i) {
762 LOG(FATAL) << "Unreachable";
763 static T sentinel = 0;
764 return sentinel;
765 }
766};
767
768template<intptr_t N>
769class HTemplateInstruction: public HInstruction {
770 public:
Dave Allison20dfc792014-06-16 20:44:29 -0700771 HTemplateInstruction<N>() : inputs_() {}
772 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000773
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100774 virtual size_t InputCount() const { return N; }
775 virtual HInstruction* InputAt(size_t i) const { return inputs_[i]; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000776
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000777 protected:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100778 virtual void SetRawInputAt(size_t i, HInstruction* instruction) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000779 inputs_[i] = instruction;
780 }
781
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000782 private:
783 EmbeddedArray<HInstruction*, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100784
785 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000786};
787
Dave Allison20dfc792014-06-16 20:44:29 -0700788template<intptr_t N>
789class HExpression: public HTemplateInstruction<N> {
790 public:
791 explicit HExpression<N>(Primitive::Type type) : type_(type) {}
792 virtual ~HExpression() {}
793
794 virtual Primitive::Type GetType() const { return type_; }
795
796 private:
797 const Primitive::Type type_;
798};
799
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000800// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
801// instruction that branches to the exit block.
802class HReturnVoid : public HTemplateInstruction<0> {
803 public:
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100804 HReturnVoid() {}
805
806 virtual bool IsControlFlow() const { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000807
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100808 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000809
810 private:
811 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
812};
813
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000814// Represents dex's RETURN opcodes. A HReturn is a control flow
815// instruction that branches to the exit block.
816class HReturn : public HTemplateInstruction<1> {
817 public:
818 explicit HReturn(HInstruction* value) {
819 SetRawInputAt(0, value);
820 }
821
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100822 virtual bool IsControlFlow() const { return true; }
823
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100824 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000825
826 private:
827 DISALLOW_COPY_AND_ASSIGN(HReturn);
828};
829
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000830// The exit instruction is the only instruction of the exit block.
831// Instructions aborting the method (HTrow and HReturn) must branch to the
832// exit block.
833class HExit : public HTemplateInstruction<0> {
834 public:
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100835 HExit() {}
836
837 virtual bool IsControlFlow() const { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000838
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100839 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000840
841 private:
842 DISALLOW_COPY_AND_ASSIGN(HExit);
843};
844
845// Jumps from one block to another.
846class HGoto : public HTemplateInstruction<0> {
847 public:
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100848 HGoto() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000849
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000850 HBasicBlock* GetSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100851 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000852 }
853
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100854 virtual bool IsControlFlow() const { return true; }
855
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100856 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000857
858 private:
859 DISALLOW_COPY_AND_ASSIGN(HGoto);
860};
861
Dave Allison20dfc792014-06-16 20:44:29 -0700862
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000863// Conditional branch. A block ending with an HIf instruction must have
864// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000865class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000866 public:
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000867 explicit HIf(HInstruction* input) {
868 SetRawInputAt(0, input);
869 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000870
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000871 HBasicBlock* IfTrueSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100872 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000873 }
874
875 HBasicBlock* IfFalseSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100876 return GetBlock()->GetSuccessors().Get(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000877 }
878
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100879 virtual bool IsControlFlow() const { return true; }
880
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100881 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000882
Dave Allison20dfc792014-06-16 20:44:29 -0700883 virtual bool IsIfInstruction() const { return true; }
884
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000885 private:
886 DISALLOW_COPY_AND_ASSIGN(HIf);
887};
888
Dave Allison20dfc792014-06-16 20:44:29 -0700889class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000890 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000891 HBinaryOperation(Primitive::Type result_type,
892 HInstruction* left,
Dave Allison20dfc792014-06-16 20:44:29 -0700893 HInstruction* right) : HExpression(result_type) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000894 SetRawInputAt(0, left);
895 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000896 }
897
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000898 HInstruction* GetLeft() const { return InputAt(0); }
899 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -0700900 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000901
902 virtual bool IsCommutative() { return false; }
903
904 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000905 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
906};
907
Dave Allison20dfc792014-06-16 20:44:29 -0700908class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000909 public:
Dave Allison20dfc792014-06-16 20:44:29 -0700910 HCondition(HInstruction* first, HInstruction* second)
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000911 : HBinaryOperation(Primitive::kPrimBoolean, first, second) {}
912
913 virtual bool IsCommutative() { return true; }
Dave Allison20dfc792014-06-16 20:44:29 -0700914 bool NeedsMaterialization() const;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000915
Dave Allison20dfc792014-06-16 20:44:29 -0700916 DECLARE_INSTRUCTION(Condition);
917
918 virtual IfCondition GetCondition() const = 0;
919
920 private:
921 DISALLOW_COPY_AND_ASSIGN(HCondition);
922};
923
924// Instruction to check if two inputs are equal to each other.
925class HEqual : public HCondition {
926 public:
927 HEqual(HInstruction* first, HInstruction* second)
928 : HCondition(first, second) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100929
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100930 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000931
Dave Allison20dfc792014-06-16 20:44:29 -0700932 virtual IfCondition GetCondition() const {
933 return kCondEQ;
934 }
935
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000936 private:
937 DISALLOW_COPY_AND_ASSIGN(HEqual);
938};
939
Dave Allison20dfc792014-06-16 20:44:29 -0700940class HNotEqual : public HCondition {
941 public:
942 HNotEqual(HInstruction* first, HInstruction* second)
943 : HCondition(first, second) {}
944
945 DECLARE_INSTRUCTION(NotEqual);
946
947 virtual IfCondition GetCondition() const {
948 return kCondNE;
949 }
950
951 private:
952 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
953};
954
955class HLessThan : public HCondition {
956 public:
957 HLessThan(HInstruction* first, HInstruction* second)
958 : HCondition(first, second) {}
959
960 DECLARE_INSTRUCTION(LessThan);
961
962 virtual IfCondition GetCondition() const {
963 return kCondLT;
964 }
965
966 private:
967 DISALLOW_COPY_AND_ASSIGN(HLessThan);
968};
969
970class HLessThanOrEqual : public HCondition {
971 public:
972 HLessThanOrEqual(HInstruction* first, HInstruction* second)
973 : HCondition(first, second) {}
974
975 DECLARE_INSTRUCTION(LessThanOrEqual);
976
977 virtual IfCondition GetCondition() const {
978 return kCondLE;
979 }
980
981 private:
982 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
983};
984
985class HGreaterThan : public HCondition {
986 public:
987 HGreaterThan(HInstruction* first, HInstruction* second)
988 : HCondition(first, second) {}
989
990 DECLARE_INSTRUCTION(GreaterThan);
991
992 virtual IfCondition GetCondition() const {
993 return kCondGT;
994 }
995
996 private:
997 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
998};
999
1000class HGreaterThanOrEqual : public HCondition {
1001 public:
1002 HGreaterThanOrEqual(HInstruction* first, HInstruction* second)
1003 : HCondition(first, second) {}
1004
1005 DECLARE_INSTRUCTION(GreaterThanOrEqual);
1006
1007 virtual IfCondition GetCondition() const {
1008 return kCondGE;
1009 }
1010
1011 private:
1012 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
1013};
1014
1015
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001016// Instruction to check how two inputs compare to each other.
1017// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
1018class HCompare : public HBinaryOperation {
1019 public:
1020 HCompare(Primitive::Type type, HInstruction* first, HInstruction* second)
1021 : HBinaryOperation(Primitive::kPrimInt, first, second) {
1022 DCHECK_EQ(type, first->GetType());
1023 DCHECK_EQ(type, second->GetType());
1024 }
1025
1026 DECLARE_INSTRUCTION(Compare);
1027
1028 private:
1029 DISALLOW_COPY_AND_ASSIGN(HCompare);
1030};
1031
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001032// A local in the graph. Corresponds to a Dex register.
1033class HLocal : public HTemplateInstruction<0> {
1034 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001035 explicit HLocal(uint16_t reg_number) : reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001036
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001037 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001038
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001039 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001040
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001041 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001042 // The Dex register number.
1043 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001044
1045 DISALLOW_COPY_AND_ASSIGN(HLocal);
1046};
1047
1048// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07001049class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001050 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001051 explicit HLoadLocal(HLocal* local, Primitive::Type type) : HExpression(type) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001052 SetRawInputAt(0, local);
1053 }
1054
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001055 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1056
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001057 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001058
1059 private:
1060 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
1061};
1062
1063// Store a value in a given local. This instruction has two inputs: the value
1064// and the local.
1065class HStoreLocal : public HTemplateInstruction<2> {
1066 public:
1067 HStoreLocal(HLocal* local, HInstruction* value) {
1068 SetRawInputAt(0, local);
1069 SetRawInputAt(1, value);
1070 }
1071
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001072 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1073
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001074 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001075
1076 private:
1077 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
1078};
1079
1080// Constants of the type int. Those can be from Dex instructions, or
1081// synthesized (for example with the if-eqz instruction).
Dave Allison20dfc792014-06-16 20:44:29 -07001082class HIntConstant : public HExpression<0> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001083 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001084 explicit HIntConstant(int32_t value) : HExpression(Primitive::kPrimInt), value_(value) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001085
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001086 int32_t GetValue() const { return value_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001087
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001088 DECLARE_INSTRUCTION(IntConstant);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001089
1090 private:
1091 const int32_t value_;
1092
1093 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
1094};
1095
Dave Allison20dfc792014-06-16 20:44:29 -07001096class HLongConstant : public HExpression<0> {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001097 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001098 explicit HLongConstant(int64_t value) : HExpression(Primitive::kPrimLong), value_(value) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001099
1100 int64_t GetValue() const { return value_; }
1101
1102 virtual Primitive::Type GetType() const { return Primitive::kPrimLong; }
1103
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001104 DECLARE_INSTRUCTION(LongConstant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001105
1106 private:
1107 const int64_t value_;
1108
1109 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
1110};
1111
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001112class HInvoke : public HInstruction {
1113 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001114 HInvoke(ArenaAllocator* arena,
1115 uint32_t number_of_arguments,
1116 Primitive::Type return_type,
1117 uint32_t dex_pc)
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001118 : inputs_(arena, number_of_arguments),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001119 return_type_(return_type),
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001120 dex_pc_(dex_pc) {
1121 inputs_.SetSize(number_of_arguments);
1122 }
1123
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001124 virtual size_t InputCount() const { return inputs_.Size(); }
1125 virtual HInstruction* InputAt(size_t i) const { return inputs_.Get(i); }
1126
1127 // Runtime needs to walk the stack, so Dex -> Dex calls need to
1128 // know their environment.
1129 virtual bool NeedsEnvironment() const { return true; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001130
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001131 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001132 SetRawInputAt(index, argument);
1133 }
1134
1135 virtual void SetRawInputAt(size_t index, HInstruction* input) {
1136 inputs_.Put(index, input);
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001137 }
1138
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001139 virtual Primitive::Type GetType() const { return return_type_; }
1140
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001141 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001142
1143 protected:
1144 GrowableArray<HInstruction*> inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001145 const Primitive::Type return_type_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001146 const uint32_t dex_pc_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001147
1148 private:
1149 DISALLOW_COPY_AND_ASSIGN(HInvoke);
1150};
1151
1152class HInvokeStatic : public HInvoke {
1153 public:
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001154 HInvokeStatic(ArenaAllocator* arena,
1155 uint32_t number_of_arguments,
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001156 Primitive::Type return_type,
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001157 uint32_t dex_pc,
1158 uint32_t index_in_dex_cache)
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001159 : HInvoke(arena, number_of_arguments, return_type, dex_pc),
1160 index_in_dex_cache_(index_in_dex_cache) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001161
1162 uint32_t GetIndexInDexCache() const { return index_in_dex_cache_; }
1163
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001164 DECLARE_INSTRUCTION(InvokeStatic);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001165
1166 private:
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001167 const uint32_t index_in_dex_cache_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001168
1169 DISALLOW_COPY_AND_ASSIGN(HInvokeStatic);
1170};
1171
Dave Allison20dfc792014-06-16 20:44:29 -07001172class HNewInstance : public HExpression<0> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001173 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001174 HNewInstance(uint32_t dex_pc, uint16_t type_index) : HExpression(Primitive::kPrimNot),
1175 dex_pc_(dex_pc), type_index_(type_index) {}
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001176
1177 uint32_t GetDexPc() const { return dex_pc_; }
1178 uint16_t GetTypeIndex() const { return type_index_; }
1179
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001180 // Calls runtime so needs an environment.
1181 virtual bool NeedsEnvironment() const { return true; }
1182
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001183 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001184
1185 private:
1186 const uint32_t dex_pc_;
1187 const uint16_t type_index_;
1188
1189 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
1190};
1191
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001192class HAdd : public HBinaryOperation {
1193 public:
1194 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1195 : HBinaryOperation(result_type, left, right) {}
1196
1197 virtual bool IsCommutative() { return true; }
1198
1199 DECLARE_INSTRUCTION(Add);
1200
1201 private:
1202 DISALLOW_COPY_AND_ASSIGN(HAdd);
1203};
1204
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001205class HSub : public HBinaryOperation {
1206 public:
1207 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1208 : HBinaryOperation(result_type, left, right) {}
1209
1210 virtual bool IsCommutative() { return false; }
1211
1212 DECLARE_INSTRUCTION(Sub);
1213
1214 private:
1215 DISALLOW_COPY_AND_ASSIGN(HSub);
1216};
1217
1218// The value of a parameter in this method. Its location depends on
1219// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07001220class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001221 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001222 HParameterValue(uint8_t index, Primitive::Type parameter_type)
Dave Allison20dfc792014-06-16 20:44:29 -07001223 : HExpression(parameter_type), index_(index) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001224
1225 uint8_t GetIndex() const { return index_; }
1226
1227 DECLARE_INSTRUCTION(ParameterValue);
1228
1229 private:
1230 // The index of this parameter in the parameters list. Must be less
1231 // than HGraph::number_of_in_vregs_;
1232 const uint8_t index_;
1233
1234 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
1235};
1236
Dave Allison20dfc792014-06-16 20:44:29 -07001237class HNot : public HExpression<1> {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001238 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001239 explicit HNot(HInstruction* input) : HExpression(Primitive::kPrimBoolean) {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001240 SetRawInputAt(0, input);
1241 }
1242
1243 DECLARE_INSTRUCTION(Not);
1244
1245 private:
1246 DISALLOW_COPY_AND_ASSIGN(HNot);
1247};
1248
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001249class HPhi : public HInstruction {
1250 public:
1251 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
1252 : inputs_(arena, number_of_inputs),
1253 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001254 type_(type),
1255 is_live_(false) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001256 inputs_.SetSize(number_of_inputs);
1257 }
1258
1259 virtual size_t InputCount() const { return inputs_.Size(); }
1260 virtual HInstruction* InputAt(size_t i) const { return inputs_.Get(i); }
1261
1262 virtual void SetRawInputAt(size_t index, HInstruction* input) {
1263 inputs_.Put(index, input);
1264 }
1265
1266 void AddInput(HInstruction* input);
1267
1268 virtual Primitive::Type GetType() const { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001269 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001270
1271 uint32_t GetRegNumber() const { return reg_number_; }
1272
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001273 void SetDead() { is_live_ = false; }
1274 void SetLive() { is_live_ = true; }
1275 bool IsDead() const { return !is_live_; }
1276 bool IsLive() const { return is_live_; }
1277
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001278 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001279
1280 protected:
1281 GrowableArray<HInstruction*> inputs_;
1282 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001283 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001284 bool is_live_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001285
1286 private:
1287 DISALLOW_COPY_AND_ASSIGN(HPhi);
1288};
1289
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001290class HNullCheck : public HExpression<1> {
1291 public:
1292 HNullCheck(HInstruction* value, uint32_t dex_pc)
1293 : HExpression(value->GetType()), dex_pc_(dex_pc) {
1294 SetRawInputAt(0, value);
1295 }
1296
1297 virtual bool NeedsEnvironment() const { return true; }
1298
1299 uint32_t GetDexPc() const { return dex_pc_; }
1300
1301 DECLARE_INSTRUCTION(NullCheck);
1302
1303 private:
1304 const uint32_t dex_pc_;
1305
1306 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
1307};
1308
1309class FieldInfo : public ValueObject {
1310 public:
1311 explicit FieldInfo(MemberOffset field_offset)
1312 : field_offset_(field_offset) {}
1313
1314 MemberOffset GetFieldOffset() const { return field_offset_; }
1315
1316 private:
1317 const MemberOffset field_offset_;
1318};
1319
1320class HInstanceFieldGet : public HExpression<1> {
1321 public:
1322 HInstanceFieldGet(HInstruction* value,
1323 Primitive::Type field_type,
1324 MemberOffset field_offset)
1325 : HExpression(field_type), field_info_(field_offset) {
1326 SetRawInputAt(0, value);
1327 }
1328
1329 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
1330
1331 DECLARE_INSTRUCTION(InstanceFieldGet);
1332
1333 private:
1334 const FieldInfo field_info_;
1335
1336 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
1337};
1338
1339class HInstanceFieldSet : public HTemplateInstruction<2> {
1340 public:
1341 HInstanceFieldSet(HInstruction* object,
1342 HInstruction* value,
1343 MemberOffset field_offset)
1344 : field_info_(field_offset) {
1345 SetRawInputAt(0, object);
1346 SetRawInputAt(1, value);
1347 }
1348
1349 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
1350
1351 DECLARE_INSTRUCTION(InstanceFieldSet);
1352
1353 private:
1354 const FieldInfo field_info_;
1355
1356 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
1357};
1358
1359/**
1360 * Some DEX instructions are folded into multiple HInstructions that need
1361 * to stay live until the last HInstruction. This class
1362 * is used as a marker for the baseline compiler to ensure its preceding
1363 * HInstruction stays live. `index` is the temporary number that is used
1364 * for knowing the stack offset where to store the instruction.
1365 */
1366class HTemporary : public HTemplateInstruction<0> {
1367 public:
1368 explicit HTemporary(size_t index) : index_(index) {}
1369
1370 size_t GetIndex() const { return index_; }
1371
1372 DECLARE_INSTRUCTION(Temporary);
1373
1374 private:
1375 const size_t index_;
1376
1377 DISALLOW_COPY_AND_ASSIGN(HTemporary);
1378};
1379
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001380class MoveOperands : public ArenaObject {
1381 public:
1382 MoveOperands(Location source, Location destination)
1383 : source_(source), destination_(destination) {}
1384
1385 Location GetSource() const { return source_; }
1386 Location GetDestination() const { return destination_; }
1387
1388 void SetSource(Location value) { source_ = value; }
1389 void SetDestination(Location value) { destination_ = value; }
1390
1391 // The parallel move resolver marks moves as "in-progress" by clearing the
1392 // destination (but not the source).
1393 Location MarkPending() {
1394 DCHECK(!IsPending());
1395 Location dest = destination_;
1396 destination_ = Location::NoLocation();
1397 return dest;
1398 }
1399
1400 void ClearPending(Location dest) {
1401 DCHECK(IsPending());
1402 destination_ = dest;
1403 }
1404
1405 bool IsPending() const {
1406 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
1407 return destination_.IsInvalid() && !source_.IsInvalid();
1408 }
1409
1410 // True if this blocks a move from the given location.
1411 bool Blocks(Location loc) const {
1412 return !IsEliminated() && source_.Equals(loc);
1413 }
1414
1415 // A move is redundant if it's been eliminated, if its source and
1416 // destination are the same, or if its destination is unneeded.
1417 bool IsRedundant() const {
1418 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
1419 }
1420
1421 // We clear both operands to indicate move that's been eliminated.
1422 void Eliminate() {
1423 source_ = destination_ = Location::NoLocation();
1424 }
1425
1426 bool IsEliminated() const {
1427 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
1428 return source_.IsInvalid();
1429 }
1430
1431 private:
1432 Location source_;
1433 Location destination_;
1434
1435 DISALLOW_COPY_AND_ASSIGN(MoveOperands);
1436};
1437
1438static constexpr size_t kDefaultNumberOfMoves = 4;
1439
1440class HParallelMove : public HTemplateInstruction<0> {
1441 public:
1442 explicit HParallelMove(ArenaAllocator* arena) : moves_(arena, kDefaultNumberOfMoves) {}
1443
1444 void AddMove(MoveOperands* move) {
1445 moves_.Add(move);
1446 }
1447
1448 MoveOperands* MoveOperandsAt(size_t index) const {
1449 return moves_.Get(index);
1450 }
1451
1452 size_t NumMoves() const { return moves_.Size(); }
1453
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001454 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001455
1456 private:
1457 GrowableArray<MoveOperands*> moves_;
1458
1459 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
1460};
1461
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001462class HGraphVisitor : public ValueObject {
1463 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001464 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
1465 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001466
Dave Allison20dfc792014-06-16 20:44:29 -07001467 virtual void VisitInstruction(HInstruction* instruction) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001468 virtual void VisitBasicBlock(HBasicBlock* block);
1469
1470 void VisitInsertionOrder();
1471
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001472 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001473
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001474 // Visit functions for instruction classes.
1475#define DECLARE_VISIT_INSTRUCTION(name) \
1476 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
1477
1478 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
1479
1480#undef DECLARE_VISIT_INSTRUCTION
1481
1482 private:
1483 HGraph* graph_;
1484
1485 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
1486};
1487
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001488class HInsertionOrderIterator : public ValueObject {
1489 public:
1490 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
1491
1492 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
1493 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
1494 void Advance() { ++index_; }
1495
1496 private:
1497 const HGraph& graph_;
1498 size_t index_;
1499
1500 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
1501};
1502
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001503class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001504 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001505 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001506
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001507 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
1508 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001509 void Advance() { ++index_; }
1510
1511 private:
1512 const HGraph& graph_;
1513 size_t index_;
1514
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001515 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001516};
1517
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001518class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001519 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001520 explicit HPostOrderIterator(const HGraph& graph)
1521 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001522
1523 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001524 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001525 void Advance() { --index_; }
1526
1527 private:
1528 const HGraph& graph_;
1529 size_t index_;
1530
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001531 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001532};
1533
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001534} // namespace art
1535
1536#endif // ART_COMPILER_OPTIMIZING_NODES_H_