blob: cb3dd0f69fc3506e9b757c25364679d9d0571ea6 [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) \
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100440 M(ArrayGet) \
441 M(ArraySet) \
442 M(ArrayLength) \
443 M(BoundsCheck) \
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100444 M(NullCheck) \
445 M(Temporary) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000446
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100447#define FOR_EACH_INSTRUCTION(M) \
448 FOR_EACH_CONCRETE_INSTRUCTION(M) \
449 M(Constant)
Dave Allison20dfc792014-06-16 20:44:29 -0700450
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000451#define FORWARD_DECLARATION(type) class H##type;
452FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
453#undef FORWARD_DECLARATION
454
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000455#define DECLARE_INSTRUCTION(type) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000456 virtual const char* DebugName() const { return #type; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000457 virtual H##type* As##type() { return this; } \
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100458 virtual void Accept(HGraphVisitor* visitor) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000459
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100460template <typename T>
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000461class HUseListNode : public ArenaObject {
462 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100463 HUseListNode(T* user, size_t index, HUseListNode* tail)
Dave Allison20dfc792014-06-16 20:44:29 -0700464 : user_(user), index_(index), tail_(tail) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000465
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000466 HUseListNode* GetTail() const { return tail_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100467 T* GetUser() const { return user_; }
468 size_t GetIndex() const { return index_; }
469
470 void SetTail(HUseListNode<T>* node) { tail_ = node; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000471
472 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100473 T* const user_;
474 const size_t index_;
475 HUseListNode<T>* tail_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000476
477 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
478};
479
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000480class HInstruction : public ArenaObject {
481 public:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000482 HInstruction()
483 : previous_(nullptr),
484 next_(nullptr),
485 block_(nullptr),
486 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100487 ssa_index_(-1),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000488 uses_(nullptr),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100489 env_uses_(nullptr),
490 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100491 locations_(nullptr),
492 live_interval_(nullptr),
493 lifetime_position_(kNoLifetime) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000494
Dave Allison20dfc792014-06-16 20:44:29 -0700495 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000496
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000497 HInstruction* GetNext() const { return next_; }
498 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000499
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000500 HBasicBlock* GetBlock() const { return block_; }
501 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100502 bool IsInBlock() const { return block_ != nullptr; }
503 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000504
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100505 virtual size_t InputCount() const = 0;
506 virtual HInstruction* InputAt(size_t i) const = 0;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000507
508 virtual void Accept(HGraphVisitor* visitor) = 0;
509 virtual const char* DebugName() const = 0;
510
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100511 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100512 virtual void SetRawInputAt(size_t index, HInstruction* input) = 0;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100513
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100514 virtual bool NeedsEnvironment() const { return false; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100515 virtual bool IsControlFlow() const { return false; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100516
517 void AddUseAt(HInstruction* user, size_t index) {
518 uses_ = new (block_->GetGraph()->GetArena()) HUseListNode<HInstruction>(user, index, uses_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000519 }
520
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100521 void AddEnvUseAt(HEnvironment* user, size_t index) {
522 env_uses_ = new (block_->GetGraph()->GetArena()) HUseListNode<HEnvironment>(
523 user, index, env_uses_);
524 }
525
526 void RemoveUser(HInstruction* user, size_t index);
527
528 HUseListNode<HInstruction>* GetUses() const { return uses_; }
529 HUseListNode<HEnvironment>* GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000530
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100531 bool HasUses() const { return uses_ != nullptr || env_uses_ != nullptr; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100532 bool HasEnvironmentUses() const { return env_uses_ != nullptr; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000533
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100534 size_t NumberOfUses() const {
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100535 // TODO: Optimize this method if it is used outside of the HGraphVisualizer.
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100536 size_t result = 0;
537 HUseListNode<HInstruction>* current = uses_;
538 while (current != nullptr) {
539 current = current->GetTail();
540 ++result;
541 }
542 return result;
543 }
544
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000545 int GetId() const { return id_; }
546 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000547
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100548 int GetSsaIndex() const { return ssa_index_; }
549 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
550 bool HasSsaIndex() const { return ssa_index_ != -1; }
551
552 bool HasEnvironment() const { return environment_ != nullptr; }
553 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100554 void SetEnvironment(HEnvironment* environment) { environment_ = environment; }
555
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000556 LocationSummary* GetLocations() const { return locations_; }
557 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000558
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100559 void ReplaceWith(HInstruction* instruction);
560
Dave Allison20dfc792014-06-16 20:44:29 -0700561 bool HasOnlyOneUse() const {
562 return uses_ != nullptr && uses_->GetTail() == nullptr;
563 }
564
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000565#define INSTRUCTION_TYPE_CHECK(type) \
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100566 bool Is##type() { return (As##type() != nullptr); } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000567 virtual H##type* As##type() { return nullptr; }
568
569 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
570#undef INSTRUCTION_TYPE_CHECK
571
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100572 size_t GetLifetimePosition() const { return lifetime_position_; }
573 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
574 LiveInterval* GetLiveInterval() const { return live_interval_; }
575 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
576 bool HasLiveInterval() const { return live_interval_ != nullptr; }
577
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000578 private:
579 HInstruction* previous_;
580 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000581 HBasicBlock* block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000582
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000583 // An instruction gets an id when it is added to the graph.
584 // It reflects creation order. A negative id means the instruction
585 // has not beed added to the graph.
586 int id_;
587
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100588 // When doing liveness analysis, instructions that have uses get an SSA index.
589 int ssa_index_;
590
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100591 // List of instructions that have this instruction as input.
592 HUseListNode<HInstruction>* uses_;
593
594 // List of environments that contain this instruction.
595 HUseListNode<HEnvironment>* env_uses_;
596
597 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000598
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000599 // Set by the code generator.
600 LocationSummary* locations_;
601
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100602 // Set by the liveness analysis.
603 LiveInterval* live_interval_;
604
605 // Set by the liveness analysis, this is the position in a linear
606 // order of blocks where this instruction's live interval start.
607 size_t lifetime_position_;
608
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000609 friend class HBasicBlock;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100610 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000611
612 DISALLOW_COPY_AND_ASSIGN(HInstruction);
613};
614
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100615template<typename T>
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000616class HUseIterator : public ValueObject {
617 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100618 explicit HUseIterator(HUseListNode<T>* uses) : current_(uses) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000619
620 bool Done() const { return current_ == nullptr; }
621
622 void Advance() {
623 DCHECK(!Done());
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000624 current_ = current_->GetTail();
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000625 }
626
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100627 HUseListNode<T>* Current() const {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000628 DCHECK(!Done());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100629 return current_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000630 }
631
632 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100633 HUseListNode<T>* current_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000634
635 friend class HValue;
636};
637
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100638// A HEnvironment object contains the values of virtual registers at a given location.
639class HEnvironment : public ArenaObject {
640 public:
641 HEnvironment(ArenaAllocator* arena, size_t number_of_vregs) : vregs_(arena, number_of_vregs) {
642 vregs_.SetSize(number_of_vregs);
643 for (size_t i = 0; i < number_of_vregs; i++) {
644 vregs_.Put(i, nullptr);
645 }
646 }
647
648 void Populate(const GrowableArray<HInstruction*>& env) {
649 for (size_t i = 0; i < env.Size(); i++) {
650 HInstruction* instruction = env.Get(i);
651 vregs_.Put(i, instruction);
652 if (instruction != nullptr) {
653 instruction->AddEnvUseAt(this, i);
654 }
655 }
656 }
657
658 void SetRawEnvAt(size_t index, HInstruction* instruction) {
659 vregs_.Put(index, instruction);
660 }
661
662 GrowableArray<HInstruction*>* GetVRegs() {
663 return &vregs_;
664 }
665
666 private:
667 GrowableArray<HInstruction*> vregs_;
668
669 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
670};
671
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000672class HInputIterator : public ValueObject {
673 public:
Dave Allison20dfc792014-06-16 20:44:29 -0700674 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000675
676 bool Done() const { return index_ == instruction_->InputCount(); }
677 HInstruction* Current() const { return instruction_->InputAt(index_); }
678 void Advance() { index_++; }
679
680 private:
681 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100682 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000683
684 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
685};
686
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000687class HInstructionIterator : public ValueObject {
688 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100689 explicit HInstructionIterator(const HInstructionList& instructions)
690 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000691 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000692 }
693
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000694 bool Done() const { return instruction_ == nullptr; }
695 HInstruction* Current() const { return instruction_; }
696 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000697 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000698 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000699 }
700
701 private:
702 HInstruction* instruction_;
703 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100704
705 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000706};
707
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100708class HBackwardInstructionIterator : public ValueObject {
709 public:
710 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
711 : instruction_(instructions.last_instruction_) {
712 next_ = Done() ? nullptr : instruction_->GetPrevious();
713 }
714
715 bool Done() const { return instruction_ == nullptr; }
716 HInstruction* Current() const { return instruction_; }
717 void Advance() {
718 instruction_ = next_;
719 next_ = Done() ? nullptr : instruction_->GetPrevious();
720 }
721
722 private:
723 HInstruction* instruction_;
724 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100725
726 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100727};
728
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000729// An embedded container with N elements of type T. Used (with partial
730// specialization for N=0) because embedded arrays cannot have size 0.
731template<typename T, intptr_t N>
732class EmbeddedArray {
733 public:
Dave Allison20dfc792014-06-16 20:44:29 -0700734 EmbeddedArray() : elements_() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000735
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000736 intptr_t GetLength() const { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000737
738 const T& operator[](intptr_t i) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000739 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000740 return elements_[i];
741 }
742
743 T& operator[](intptr_t i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000744 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000745 return elements_[i];
746 }
747
748 const T& At(intptr_t i) const {
749 return (*this)[i];
750 }
751
752 void SetAt(intptr_t i, const T& val) {
753 (*this)[i] = val;
754 }
755
756 private:
757 T elements_[N];
758};
759
760template<typename T>
761class EmbeddedArray<T, 0> {
762 public:
763 intptr_t length() const { return 0; }
764 const T& operator[](intptr_t i) const {
765 LOG(FATAL) << "Unreachable";
766 static T sentinel = 0;
767 return sentinel;
768 }
769 T& operator[](intptr_t i) {
770 LOG(FATAL) << "Unreachable";
771 static T sentinel = 0;
772 return sentinel;
773 }
774};
775
776template<intptr_t N>
777class HTemplateInstruction: public HInstruction {
778 public:
Dave Allison20dfc792014-06-16 20:44:29 -0700779 HTemplateInstruction<N>() : inputs_() {}
780 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000781
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100782 virtual size_t InputCount() const { return N; }
783 virtual HInstruction* InputAt(size_t i) const { return inputs_[i]; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000784
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000785 protected:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100786 virtual void SetRawInputAt(size_t i, HInstruction* instruction) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000787 inputs_[i] = instruction;
788 }
789
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000790 private:
791 EmbeddedArray<HInstruction*, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100792
793 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000794};
795
Dave Allison20dfc792014-06-16 20:44:29 -0700796template<intptr_t N>
797class HExpression: public HTemplateInstruction<N> {
798 public:
799 explicit HExpression<N>(Primitive::Type type) : type_(type) {}
800 virtual ~HExpression() {}
801
802 virtual Primitive::Type GetType() const { return type_; }
803
804 private:
805 const Primitive::Type type_;
806};
807
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000808// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
809// instruction that branches to the exit block.
810class HReturnVoid : public HTemplateInstruction<0> {
811 public:
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100812 HReturnVoid() {}
813
814 virtual bool IsControlFlow() const { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000815
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100816 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000817
818 private:
819 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
820};
821
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000822// Represents dex's RETURN opcodes. A HReturn is a control flow
823// instruction that branches to the exit block.
824class HReturn : public HTemplateInstruction<1> {
825 public:
826 explicit HReturn(HInstruction* value) {
827 SetRawInputAt(0, value);
828 }
829
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100830 virtual bool IsControlFlow() const { return true; }
831
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100832 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000833
834 private:
835 DISALLOW_COPY_AND_ASSIGN(HReturn);
836};
837
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000838// The exit instruction is the only instruction of the exit block.
839// Instructions aborting the method (HTrow and HReturn) must branch to the
840// exit block.
841class HExit : public HTemplateInstruction<0> {
842 public:
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100843 HExit() {}
844
845 virtual bool IsControlFlow() const { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000846
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100847 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000848
849 private:
850 DISALLOW_COPY_AND_ASSIGN(HExit);
851};
852
853// Jumps from one block to another.
854class HGoto : public HTemplateInstruction<0> {
855 public:
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100856 HGoto() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000857
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000858 HBasicBlock* GetSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100859 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000860 }
861
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100862 virtual bool IsControlFlow() const { return true; }
863
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100864 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000865
866 private:
867 DISALLOW_COPY_AND_ASSIGN(HGoto);
868};
869
Dave Allison20dfc792014-06-16 20:44:29 -0700870
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000871// Conditional branch. A block ending with an HIf instruction must have
872// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000873class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000874 public:
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000875 explicit HIf(HInstruction* input) {
876 SetRawInputAt(0, input);
877 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000878
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000879 HBasicBlock* IfTrueSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100880 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000881 }
882
883 HBasicBlock* IfFalseSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100884 return GetBlock()->GetSuccessors().Get(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000885 }
886
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100887 virtual bool IsControlFlow() const { return true; }
888
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100889 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000890
Dave Allison20dfc792014-06-16 20:44:29 -0700891 virtual bool IsIfInstruction() const { return true; }
892
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000893 private:
894 DISALLOW_COPY_AND_ASSIGN(HIf);
895};
896
Dave Allison20dfc792014-06-16 20:44:29 -0700897class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000898 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000899 HBinaryOperation(Primitive::Type result_type,
900 HInstruction* left,
Dave Allison20dfc792014-06-16 20:44:29 -0700901 HInstruction* right) : HExpression(result_type) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000902 SetRawInputAt(0, left);
903 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000904 }
905
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000906 HInstruction* GetLeft() const { return InputAt(0); }
907 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -0700908 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000909
910 virtual bool IsCommutative() { return false; }
911
912 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000913 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
914};
915
Dave Allison20dfc792014-06-16 20:44:29 -0700916class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000917 public:
Dave Allison20dfc792014-06-16 20:44:29 -0700918 HCondition(HInstruction* first, HInstruction* second)
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000919 : HBinaryOperation(Primitive::kPrimBoolean, first, second) {}
920
921 virtual bool IsCommutative() { return true; }
Dave Allison20dfc792014-06-16 20:44:29 -0700922 bool NeedsMaterialization() const;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000923
Dave Allison20dfc792014-06-16 20:44:29 -0700924 DECLARE_INSTRUCTION(Condition);
925
926 virtual IfCondition GetCondition() const = 0;
927
928 private:
929 DISALLOW_COPY_AND_ASSIGN(HCondition);
930};
931
932// Instruction to check if two inputs are equal to each other.
933class HEqual : public HCondition {
934 public:
935 HEqual(HInstruction* first, HInstruction* second)
936 : HCondition(first, second) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100937
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100938 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000939
Dave Allison20dfc792014-06-16 20:44:29 -0700940 virtual IfCondition GetCondition() const {
941 return kCondEQ;
942 }
943
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000944 private:
945 DISALLOW_COPY_AND_ASSIGN(HEqual);
946};
947
Dave Allison20dfc792014-06-16 20:44:29 -0700948class HNotEqual : public HCondition {
949 public:
950 HNotEqual(HInstruction* first, HInstruction* second)
951 : HCondition(first, second) {}
952
953 DECLARE_INSTRUCTION(NotEqual);
954
955 virtual IfCondition GetCondition() const {
956 return kCondNE;
957 }
958
959 private:
960 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
961};
962
963class HLessThan : public HCondition {
964 public:
965 HLessThan(HInstruction* first, HInstruction* second)
966 : HCondition(first, second) {}
967
968 DECLARE_INSTRUCTION(LessThan);
969
970 virtual IfCondition GetCondition() const {
971 return kCondLT;
972 }
973
974 private:
975 DISALLOW_COPY_AND_ASSIGN(HLessThan);
976};
977
978class HLessThanOrEqual : public HCondition {
979 public:
980 HLessThanOrEqual(HInstruction* first, HInstruction* second)
981 : HCondition(first, second) {}
982
983 DECLARE_INSTRUCTION(LessThanOrEqual);
984
985 virtual IfCondition GetCondition() const {
986 return kCondLE;
987 }
988
989 private:
990 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
991};
992
993class HGreaterThan : public HCondition {
994 public:
995 HGreaterThan(HInstruction* first, HInstruction* second)
996 : HCondition(first, second) {}
997
998 DECLARE_INSTRUCTION(GreaterThan);
999
1000 virtual IfCondition GetCondition() const {
1001 return kCondGT;
1002 }
1003
1004 private:
1005 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
1006};
1007
1008class HGreaterThanOrEqual : public HCondition {
1009 public:
1010 HGreaterThanOrEqual(HInstruction* first, HInstruction* second)
1011 : HCondition(first, second) {}
1012
1013 DECLARE_INSTRUCTION(GreaterThanOrEqual);
1014
1015 virtual IfCondition GetCondition() const {
1016 return kCondGE;
1017 }
1018
1019 private:
1020 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
1021};
1022
1023
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001024// Instruction to check how two inputs compare to each other.
1025// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
1026class HCompare : public HBinaryOperation {
1027 public:
1028 HCompare(Primitive::Type type, HInstruction* first, HInstruction* second)
1029 : HBinaryOperation(Primitive::kPrimInt, first, second) {
1030 DCHECK_EQ(type, first->GetType());
1031 DCHECK_EQ(type, second->GetType());
1032 }
1033
1034 DECLARE_INSTRUCTION(Compare);
1035
1036 private:
1037 DISALLOW_COPY_AND_ASSIGN(HCompare);
1038};
1039
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001040// A local in the graph. Corresponds to a Dex register.
1041class HLocal : public HTemplateInstruction<0> {
1042 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001043 explicit HLocal(uint16_t reg_number) : reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001044
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001045 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001046
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001047 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001048
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001049 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001050 // The Dex register number.
1051 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001052
1053 DISALLOW_COPY_AND_ASSIGN(HLocal);
1054};
1055
1056// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07001057class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001058 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001059 explicit HLoadLocal(HLocal* local, Primitive::Type type) : HExpression(type) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001060 SetRawInputAt(0, local);
1061 }
1062
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001063 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1064
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001065 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001066
1067 private:
1068 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
1069};
1070
1071// Store a value in a given local. This instruction has two inputs: the value
1072// and the local.
1073class HStoreLocal : public HTemplateInstruction<2> {
1074 public:
1075 HStoreLocal(HLocal* local, HInstruction* value) {
1076 SetRawInputAt(0, local);
1077 SetRawInputAt(1, value);
1078 }
1079
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001080 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1081
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001082 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001083
1084 private:
1085 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
1086};
1087
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001088class HConstant : public HExpression<0> {
1089 public:
1090 explicit HConstant(Primitive::Type type) : HExpression(type) {}
1091
1092 DECLARE_INSTRUCTION(Constant);
1093
1094 private:
1095 DISALLOW_COPY_AND_ASSIGN(HConstant);
1096};
1097
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001098// Constants of the type int. Those can be from Dex instructions, or
1099// synthesized (for example with the if-eqz instruction).
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001100class HIntConstant : public HConstant {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001101 public:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001102 explicit HIntConstant(int32_t value) : HConstant(Primitive::kPrimInt), value_(value) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001103
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001104 int32_t GetValue() const { return value_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001105
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001106 DECLARE_INSTRUCTION(IntConstant);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001107
1108 private:
1109 const int32_t value_;
1110
1111 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
1112};
1113
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001114class HLongConstant : public HConstant {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001115 public:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001116 explicit HLongConstant(int64_t value) : HConstant(Primitive::kPrimLong), value_(value) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001117
1118 int64_t GetValue() const { return value_; }
1119
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001120 DECLARE_INSTRUCTION(LongConstant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001121
1122 private:
1123 const int64_t value_;
1124
1125 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
1126};
1127
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001128class HInvoke : public HInstruction {
1129 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001130 HInvoke(ArenaAllocator* arena,
1131 uint32_t number_of_arguments,
1132 Primitive::Type return_type,
1133 uint32_t dex_pc)
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001134 : inputs_(arena, number_of_arguments),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001135 return_type_(return_type),
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001136 dex_pc_(dex_pc) {
1137 inputs_.SetSize(number_of_arguments);
1138 }
1139
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001140 virtual size_t InputCount() const { return inputs_.Size(); }
1141 virtual HInstruction* InputAt(size_t i) const { return inputs_.Get(i); }
1142
1143 // Runtime needs to walk the stack, so Dex -> Dex calls need to
1144 // know their environment.
1145 virtual bool NeedsEnvironment() const { return true; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001146
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001147 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001148 SetRawInputAt(index, argument);
1149 }
1150
1151 virtual void SetRawInputAt(size_t index, HInstruction* input) {
1152 inputs_.Put(index, input);
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001153 }
1154
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001155 virtual Primitive::Type GetType() const { return return_type_; }
1156
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001157 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001158
1159 protected:
1160 GrowableArray<HInstruction*> inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001161 const Primitive::Type return_type_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001162 const uint32_t dex_pc_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001163
1164 private:
1165 DISALLOW_COPY_AND_ASSIGN(HInvoke);
1166};
1167
1168class HInvokeStatic : public HInvoke {
1169 public:
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001170 HInvokeStatic(ArenaAllocator* arena,
1171 uint32_t number_of_arguments,
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001172 Primitive::Type return_type,
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001173 uint32_t dex_pc,
1174 uint32_t index_in_dex_cache)
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001175 : HInvoke(arena, number_of_arguments, return_type, dex_pc),
1176 index_in_dex_cache_(index_in_dex_cache) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001177
1178 uint32_t GetIndexInDexCache() const { return index_in_dex_cache_; }
1179
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001180 DECLARE_INSTRUCTION(InvokeStatic);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001181
1182 private:
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001183 const uint32_t index_in_dex_cache_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001184
1185 DISALLOW_COPY_AND_ASSIGN(HInvokeStatic);
1186};
1187
Dave Allison20dfc792014-06-16 20:44:29 -07001188class HNewInstance : public HExpression<0> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001189 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001190 HNewInstance(uint32_t dex_pc, uint16_t type_index) : HExpression(Primitive::kPrimNot),
1191 dex_pc_(dex_pc), type_index_(type_index) {}
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001192
1193 uint32_t GetDexPc() const { return dex_pc_; }
1194 uint16_t GetTypeIndex() const { return type_index_; }
1195
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001196 // Calls runtime so needs an environment.
1197 virtual bool NeedsEnvironment() const { return true; }
1198
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001199 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001200
1201 private:
1202 const uint32_t dex_pc_;
1203 const uint16_t type_index_;
1204
1205 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
1206};
1207
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001208class HAdd : public HBinaryOperation {
1209 public:
1210 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1211 : HBinaryOperation(result_type, left, right) {}
1212
1213 virtual bool IsCommutative() { return true; }
1214
1215 DECLARE_INSTRUCTION(Add);
1216
1217 private:
1218 DISALLOW_COPY_AND_ASSIGN(HAdd);
1219};
1220
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001221class HSub : public HBinaryOperation {
1222 public:
1223 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1224 : HBinaryOperation(result_type, left, right) {}
1225
1226 virtual bool IsCommutative() { return false; }
1227
1228 DECLARE_INSTRUCTION(Sub);
1229
1230 private:
1231 DISALLOW_COPY_AND_ASSIGN(HSub);
1232};
1233
1234// The value of a parameter in this method. Its location depends on
1235// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07001236class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001237 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001238 HParameterValue(uint8_t index, Primitive::Type parameter_type)
Dave Allison20dfc792014-06-16 20:44:29 -07001239 : HExpression(parameter_type), index_(index) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001240
1241 uint8_t GetIndex() const { return index_; }
1242
1243 DECLARE_INSTRUCTION(ParameterValue);
1244
1245 private:
1246 // The index of this parameter in the parameters list. Must be less
1247 // than HGraph::number_of_in_vregs_;
1248 const uint8_t index_;
1249
1250 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
1251};
1252
Dave Allison20dfc792014-06-16 20:44:29 -07001253class HNot : public HExpression<1> {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001254 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001255 explicit HNot(HInstruction* input) : HExpression(Primitive::kPrimBoolean) {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001256 SetRawInputAt(0, input);
1257 }
1258
1259 DECLARE_INSTRUCTION(Not);
1260
1261 private:
1262 DISALLOW_COPY_AND_ASSIGN(HNot);
1263};
1264
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001265class HPhi : public HInstruction {
1266 public:
1267 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
1268 : inputs_(arena, number_of_inputs),
1269 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001270 type_(type),
1271 is_live_(false) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001272 inputs_.SetSize(number_of_inputs);
1273 }
1274
1275 virtual size_t InputCount() const { return inputs_.Size(); }
1276 virtual HInstruction* InputAt(size_t i) const { return inputs_.Get(i); }
1277
1278 virtual void SetRawInputAt(size_t index, HInstruction* input) {
1279 inputs_.Put(index, input);
1280 }
1281
1282 void AddInput(HInstruction* input);
1283
1284 virtual Primitive::Type GetType() const { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001285 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001286
1287 uint32_t GetRegNumber() const { return reg_number_; }
1288
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001289 void SetDead() { is_live_ = false; }
1290 void SetLive() { is_live_ = true; }
1291 bool IsDead() const { return !is_live_; }
1292 bool IsLive() const { return is_live_; }
1293
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001294 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001295
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001296 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001297 GrowableArray<HInstruction*> inputs_;
1298 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001299 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001300 bool is_live_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001301
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001302 DISALLOW_COPY_AND_ASSIGN(HPhi);
1303};
1304
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001305class HNullCheck : public HExpression<1> {
1306 public:
1307 HNullCheck(HInstruction* value, uint32_t dex_pc)
1308 : HExpression(value->GetType()), dex_pc_(dex_pc) {
1309 SetRawInputAt(0, value);
1310 }
1311
1312 virtual bool NeedsEnvironment() const { return true; }
1313
1314 uint32_t GetDexPc() const { return dex_pc_; }
1315
1316 DECLARE_INSTRUCTION(NullCheck);
1317
1318 private:
1319 const uint32_t dex_pc_;
1320
1321 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
1322};
1323
1324class FieldInfo : public ValueObject {
1325 public:
1326 explicit FieldInfo(MemberOffset field_offset)
1327 : field_offset_(field_offset) {}
1328
1329 MemberOffset GetFieldOffset() const { return field_offset_; }
1330
1331 private:
1332 const MemberOffset field_offset_;
1333};
1334
1335class HInstanceFieldGet : public HExpression<1> {
1336 public:
1337 HInstanceFieldGet(HInstruction* value,
1338 Primitive::Type field_type,
1339 MemberOffset field_offset)
1340 : HExpression(field_type), field_info_(field_offset) {
1341 SetRawInputAt(0, value);
1342 }
1343
1344 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
1345
1346 DECLARE_INSTRUCTION(InstanceFieldGet);
1347
1348 private:
1349 const FieldInfo field_info_;
1350
1351 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
1352};
1353
1354class HInstanceFieldSet : public HTemplateInstruction<2> {
1355 public:
1356 HInstanceFieldSet(HInstruction* object,
1357 HInstruction* value,
1358 MemberOffset field_offset)
1359 : field_info_(field_offset) {
1360 SetRawInputAt(0, object);
1361 SetRawInputAt(1, value);
1362 }
1363
1364 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
1365
1366 DECLARE_INSTRUCTION(InstanceFieldSet);
1367
1368 private:
1369 const FieldInfo field_info_;
1370
1371 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
1372};
1373
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001374class HArrayGet : public HExpression<2> {
1375 public:
1376 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type)
1377 : HExpression(type) {
1378 SetRawInputAt(0, array);
1379 SetRawInputAt(1, index);
1380 }
1381
1382 DECLARE_INSTRUCTION(ArrayGet);
1383
1384 private:
1385 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
1386};
1387
1388class HArraySet : public HTemplateInstruction<3> {
1389 public:
1390 HArraySet(HInstruction* array,
1391 HInstruction* index,
1392 HInstruction* value,
1393 uint32_t dex_pc) : dex_pc_(dex_pc) {
1394 SetRawInputAt(0, array);
1395 SetRawInputAt(1, index);
1396 SetRawInputAt(2, value);
1397 }
1398
1399 virtual bool NeedsEnvironment() const {
1400 // We currently always call a runtime method to catch array store
1401 // exceptions.
1402 return InputAt(2)->GetType() == Primitive::kPrimNot;
1403 }
1404
1405 uint32_t GetDexPc() const { return dex_pc_; }
1406
1407 DECLARE_INSTRUCTION(ArraySet);
1408
1409 private:
1410 const uint32_t dex_pc_;
1411
1412 DISALLOW_COPY_AND_ASSIGN(HArraySet);
1413};
1414
1415class HArrayLength : public HExpression<1> {
1416 public:
1417 explicit HArrayLength(HInstruction* array) : HExpression(Primitive::kPrimInt) {
1418 SetRawInputAt(0, array);
1419 }
1420
1421 DECLARE_INSTRUCTION(ArrayLength);
1422
1423 private:
1424 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
1425};
1426
1427class HBoundsCheck : public HExpression<2> {
1428 public:
1429 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
1430 : HExpression(index->GetType()), dex_pc_(dex_pc) {
1431 DCHECK(index->GetType() == Primitive::kPrimInt);
1432 SetRawInputAt(0, index);
1433 SetRawInputAt(1, length);
1434 }
1435
1436 virtual bool NeedsEnvironment() const { return true; }
1437
1438 uint32_t GetDexPc() const { return dex_pc_; }
1439
1440 DECLARE_INSTRUCTION(BoundsCheck);
1441
1442 private:
1443 const uint32_t dex_pc_;
1444
1445 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
1446};
1447
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001448/**
1449 * Some DEX instructions are folded into multiple HInstructions that need
1450 * to stay live until the last HInstruction. This class
1451 * is used as a marker for the baseline compiler to ensure its preceding
1452 * HInstruction stays live. `index` is the temporary number that is used
1453 * for knowing the stack offset where to store the instruction.
1454 */
1455class HTemporary : public HTemplateInstruction<0> {
1456 public:
1457 explicit HTemporary(size_t index) : index_(index) {}
1458
1459 size_t GetIndex() const { return index_; }
1460
1461 DECLARE_INSTRUCTION(Temporary);
1462
1463 private:
1464 const size_t index_;
1465
1466 DISALLOW_COPY_AND_ASSIGN(HTemporary);
1467};
1468
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001469class MoveOperands : public ArenaObject {
1470 public:
1471 MoveOperands(Location source, Location destination)
1472 : source_(source), destination_(destination) {}
1473
1474 Location GetSource() const { return source_; }
1475 Location GetDestination() const { return destination_; }
1476
1477 void SetSource(Location value) { source_ = value; }
1478 void SetDestination(Location value) { destination_ = value; }
1479
1480 // The parallel move resolver marks moves as "in-progress" by clearing the
1481 // destination (but not the source).
1482 Location MarkPending() {
1483 DCHECK(!IsPending());
1484 Location dest = destination_;
1485 destination_ = Location::NoLocation();
1486 return dest;
1487 }
1488
1489 void ClearPending(Location dest) {
1490 DCHECK(IsPending());
1491 destination_ = dest;
1492 }
1493
1494 bool IsPending() const {
1495 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
1496 return destination_.IsInvalid() && !source_.IsInvalid();
1497 }
1498
1499 // True if this blocks a move from the given location.
1500 bool Blocks(Location loc) const {
1501 return !IsEliminated() && source_.Equals(loc);
1502 }
1503
1504 // A move is redundant if it's been eliminated, if its source and
1505 // destination are the same, or if its destination is unneeded.
1506 bool IsRedundant() const {
1507 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
1508 }
1509
1510 // We clear both operands to indicate move that's been eliminated.
1511 void Eliminate() {
1512 source_ = destination_ = Location::NoLocation();
1513 }
1514
1515 bool IsEliminated() const {
1516 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
1517 return source_.IsInvalid();
1518 }
1519
1520 private:
1521 Location source_;
1522 Location destination_;
1523
1524 DISALLOW_COPY_AND_ASSIGN(MoveOperands);
1525};
1526
1527static constexpr size_t kDefaultNumberOfMoves = 4;
1528
1529class HParallelMove : public HTemplateInstruction<0> {
1530 public:
1531 explicit HParallelMove(ArenaAllocator* arena) : moves_(arena, kDefaultNumberOfMoves) {}
1532
1533 void AddMove(MoveOperands* move) {
1534 moves_.Add(move);
1535 }
1536
1537 MoveOperands* MoveOperandsAt(size_t index) const {
1538 return moves_.Get(index);
1539 }
1540
1541 size_t NumMoves() const { return moves_.Size(); }
1542
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001543 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001544
1545 private:
1546 GrowableArray<MoveOperands*> moves_;
1547
1548 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
1549};
1550
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001551class HGraphVisitor : public ValueObject {
1552 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001553 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
1554 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001555
Dave Allison20dfc792014-06-16 20:44:29 -07001556 virtual void VisitInstruction(HInstruction* instruction) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001557 virtual void VisitBasicBlock(HBasicBlock* block);
1558
1559 void VisitInsertionOrder();
1560
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001561 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001562
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001563 // Visit functions for instruction classes.
1564#define DECLARE_VISIT_INSTRUCTION(name) \
1565 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
1566
1567 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
1568
1569#undef DECLARE_VISIT_INSTRUCTION
1570
1571 private:
1572 HGraph* graph_;
1573
1574 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
1575};
1576
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001577class HInsertionOrderIterator : public ValueObject {
1578 public:
1579 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
1580
1581 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
1582 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
1583 void Advance() { ++index_; }
1584
1585 private:
1586 const HGraph& graph_;
1587 size_t index_;
1588
1589 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
1590};
1591
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001592class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001593 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001594 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001595
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001596 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
1597 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001598 void Advance() { ++index_; }
1599
1600 private:
1601 const HGraph& graph_;
1602 size_t index_;
1603
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001604 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001605};
1606
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001607class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001608 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001609 explicit HPostOrderIterator(const HGraph& graph)
1610 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001611
1612 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001613 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001614 void Advance() { --index_; }
1615
1616 private:
1617 const HGraph& graph_;
1618 size_t index_;
1619
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001620 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001621};
1622
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001623} // namespace art
1624
1625#endif // ART_COMPILER_OPTIMIZING_NODES_H_