blob: 7c8e7e2bdd77d48a5a4eb9d507a7a2b21b754a78 [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 Geoffray3c049742014-09-24 18:10:46 +010035class HSuspendCheck;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010036class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000037class LocationSummary;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000038
39static const int kDefaultNumberOfBlocks = 8;
40static const int kDefaultNumberOfSuccessors = 2;
41static const int kDefaultNumberOfPredecessors = 2;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010042static const int kDefaultNumberOfDominatedBlocks = 1;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000043static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000044
Dave Allison20dfc792014-06-16 20:44:29 -070045enum IfCondition {
46 kCondEQ,
47 kCondNE,
48 kCondLT,
49 kCondLE,
50 kCondGT,
51 kCondGE,
52};
53
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010054class HInstructionList {
55 public:
56 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
57
58 void AddInstruction(HInstruction* instruction);
59 void RemoveInstruction(HInstruction* instruction);
60
Roland Levillainccc07a92014-09-16 14:48:16 +010061 // Return true if `instruction1` is found before `instruction2` in
62 // this instruction list and false otherwise. Abort if none
63 // of these instructions is found.
64 bool FoundBefore(const HInstruction* instruction1,
65 const HInstruction* instruction2) const;
66
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010067 private:
68 HInstruction* first_instruction_;
69 HInstruction* last_instruction_;
70
71 friend class HBasicBlock;
72 friend class HInstructionIterator;
Nicolas Geoffray804d0932014-05-02 08:46:00 +010073 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010074
75 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
76};
77
Nicolas Geoffray818f2102014-02-18 16:43:35 +000078// Control-flow graph of a method. Contains a list of basic blocks.
79class HGraph : public ArenaObject {
80 public:
81 explicit HGraph(ArenaAllocator* arena)
82 : arena_(arena),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000083 blocks_(arena, kDefaultNumberOfBlocks),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +010084 reverse_post_order_(arena, kDefaultNumberOfBlocks),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +010085 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +010086 number_of_vregs_(0),
87 number_of_in_vregs_(0),
Nicolas Geoffraye5038322014-07-04 09:41:32 +010088 number_of_temporaries_(0),
Dave Allison20dfc792014-06-16 20:44:29 -070089 current_instruction_id_(0) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +000090
Nicolas Geoffray787c3072014-03-17 10:20:19 +000091 ArenaAllocator* GetArena() const { return arena_; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +010092 const GrowableArray<HBasicBlock*>& GetBlocks() const { return blocks_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +000093
Nicolas Geoffray787c3072014-03-17 10:20:19 +000094 HBasicBlock* GetEntryBlock() const { return entry_block_; }
95 HBasicBlock* GetExitBlock() const { return exit_block_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000096
Nicolas Geoffray787c3072014-03-17 10:20:19 +000097 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
98 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000099
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000100 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100101
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000102 void BuildDominatorTree();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100103 void TransformToSSA();
104 void SimplifyCFG();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000105
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100106 // Find all natural loops in this graph. Aborts computation and returns false
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100107 // if one loop is not natural, that is the header does not dominate the back
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100108 // edge.
109 bool FindNaturalLoops() const;
110
111 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
112 void SimplifyLoop(HBasicBlock* header);
113
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000114 int GetNextInstructionId() {
115 return current_instruction_id_++;
116 }
117
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100118 uint16_t GetMaximumNumberOfOutVRegs() const {
119 return maximum_number_of_out_vregs_;
120 }
121
122 void UpdateMaximumNumberOfOutVRegs(uint16_t new_value) {
123 maximum_number_of_out_vregs_ = std::max(new_value, maximum_number_of_out_vregs_);
124 }
125
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100126 void UpdateNumberOfTemporaries(size_t count) {
127 number_of_temporaries_ = std::max(count, number_of_temporaries_);
128 }
129
130 size_t GetNumberOfTemporaries() const {
131 return number_of_temporaries_;
132 }
133
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100134 void SetNumberOfVRegs(uint16_t number_of_vregs) {
135 number_of_vregs_ = number_of_vregs;
136 }
137
138 uint16_t GetNumberOfVRegs() const {
139 return number_of_vregs_;
140 }
141
142 void SetNumberOfInVRegs(uint16_t value) {
143 number_of_in_vregs_ = value;
144 }
145
146 uint16_t GetNumberOfInVRegs() const {
147 return number_of_in_vregs_;
148 }
149
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100150 uint16_t GetNumberOfLocalVRegs() const {
151 return number_of_vregs_ - number_of_in_vregs_;
152 }
153
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100154 const GrowableArray<HBasicBlock*>& GetReversePostOrder() const {
155 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100156 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100157
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000158 private:
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000159 HBasicBlock* FindCommonDominator(HBasicBlock* first, HBasicBlock* second) const;
160 void VisitBlockForDominatorTree(HBasicBlock* block,
161 HBasicBlock* predecessor,
162 GrowableArray<size_t>* visits);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100163 void FindBackEdges(ArenaBitVector* visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000164 void VisitBlockForBackEdges(HBasicBlock* block,
165 ArenaBitVector* visited,
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100166 ArenaBitVector* visiting);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000167 void RemoveDeadBlocks(const ArenaBitVector& visited) const;
168
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000169 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000170
171 // List of blocks in insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000172 GrowableArray<HBasicBlock*> blocks_;
173
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100174 // List of blocks to perform a reverse post order tree traversal.
175 GrowableArray<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000176
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000177 HBasicBlock* entry_block_;
178 HBasicBlock* exit_block_;
179
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100180 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100181 uint16_t maximum_number_of_out_vregs_;
182
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100183 // The number of virtual registers in this method. Contains the parameters.
184 uint16_t number_of_vregs_;
185
186 // The number of virtual registers used by parameters of this method.
187 uint16_t number_of_in_vregs_;
188
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100189 // The number of temporaries that will be needed for the baseline compiler.
190 size_t number_of_temporaries_;
191
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000192 // The current id to assign to a newly added instruction. See HInstruction.id_.
193 int current_instruction_id_;
194
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000195 DISALLOW_COPY_AND_ASSIGN(HGraph);
196};
197
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000198class HLoopInformation : public ArenaObject {
199 public:
200 HLoopInformation(HBasicBlock* header, HGraph* graph)
201 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100202 suspend_check_(nullptr),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100203 back_edges_(graph->GetArena(), kDefaultNumberOfBackEdges),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100204 // Make bit vector growable, as the number of blocks may change.
205 blocks_(graph->GetArena(), graph->GetBlocks().Size(), true) {}
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100206
207 HBasicBlock* GetHeader() const {
208 return header_;
209 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000210
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100211 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
212 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
213 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
214
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000215 void AddBackEdge(HBasicBlock* back_edge) {
216 back_edges_.Add(back_edge);
217 }
218
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100219 void RemoveBackEdge(HBasicBlock* back_edge) {
220 back_edges_.Delete(back_edge);
221 }
222
223 bool IsBackEdge(HBasicBlock* block) {
224 for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) {
225 if (back_edges_.Get(i) == block) return true;
226 }
227 return false;
228 }
229
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000230 int NumberOfBackEdges() const {
231 return back_edges_.Size();
232 }
233
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100234 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100235
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100236 const GrowableArray<HBasicBlock*>& GetBackEdges() const {
237 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100238 }
239
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100240 void ClearBackEdges() {
241 back_edges_.Reset();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100242 }
243
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100244 // Find blocks that are part of this loop. Returns whether the loop is a natural loop,
245 // that is the header dominates the back edge.
246 bool Populate();
247
248 // Returns whether this loop information contains `block`.
249 // Note that this loop information *must* be populated before entering this function.
250 bool Contains(const HBasicBlock& block) const;
251
252 // Returns whether this loop information is an inner loop of `other`.
253 // Note that `other` *must* be populated before entering this function.
254 bool IsIn(const HLoopInformation& other) const;
255
256 const ArenaBitVector& GetBlocks() const { return blocks_; }
257
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000258 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100259 // Internal recursive implementation of `Populate`.
260 void PopulateRecursive(HBasicBlock* block);
261
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000262 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100263 HSuspendCheck* suspend_check_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000264 GrowableArray<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100265 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000266
267 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
268};
269
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100270static constexpr size_t kNoLifetime = -1;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100271static constexpr uint32_t kNoDexPc = -1;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100272
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000273// A block in a method. Contains the list of instructions represented
274// as a double linked list. Each block knows its predecessors and
275// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100276
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000277class HBasicBlock : public ArenaObject {
278 public:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100279 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000280 : graph_(graph),
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000281 predecessors_(graph->GetArena(), kDefaultNumberOfPredecessors),
282 successors_(graph->GetArena(), kDefaultNumberOfSuccessors),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000283 loop_information_(nullptr),
284 dominator_(nullptr),
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100285 dominated_blocks_(graph->GetArena(), kDefaultNumberOfDominatedBlocks),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100286 block_id_(-1),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100287 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100288 lifetime_start_(kNoLifetime),
289 lifetime_end_(kNoLifetime) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000290
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100291 const GrowableArray<HBasicBlock*>& GetPredecessors() const {
292 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000293 }
294
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100295 const GrowableArray<HBasicBlock*>& GetSuccessors() const {
296 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000297 }
298
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100299 const GrowableArray<HBasicBlock*>& GetDominatedBlocks() const {
300 return dominated_blocks_;
301 }
302
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100303 bool IsEntryBlock() const {
304 return graph_->GetEntryBlock() == this;
305 }
306
307 bool IsExitBlock() const {
308 return graph_->GetExitBlock() == this;
309 }
310
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000311 void AddBackEdge(HBasicBlock* back_edge) {
312 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000313 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000314 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100315 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000316 loop_information_->AddBackEdge(back_edge);
317 }
318
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000319 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000320
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000321 int GetBlockId() const { return block_id_; }
322 void SetBlockId(int id) { block_id_ = id; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000323
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000324 HBasicBlock* GetDominator() const { return dominator_; }
325 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100326 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.Add(block); }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000327
328 int NumberOfBackEdges() const {
329 return loop_information_ == nullptr
330 ? 0
331 : loop_information_->NumberOfBackEdges();
332 }
333
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100334 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
335 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100336 const HInstructionList& GetInstructions() const { return instructions_; }
337 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100338 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000339
340 void AddSuccessor(HBasicBlock* block) {
341 successors_.Add(block);
342 block->predecessors_.Add(this);
343 }
344
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100345 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
346 size_t successor_index = GetSuccessorIndexOf(existing);
347 DCHECK_NE(successor_index, static_cast<size_t>(-1));
348 existing->RemovePredecessor(this);
349 new_block->predecessors_.Add(this);
350 successors_.Put(successor_index, new_block);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000351 }
352
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100353 void RemovePredecessor(HBasicBlock* block) {
354 predecessors_.Delete(block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100355 }
356
357 void ClearAllPredecessors() {
358 predecessors_.Reset();
359 }
360
361 void AddPredecessor(HBasicBlock* block) {
362 predecessors_.Add(block);
363 block->successors_.Add(this);
364 }
365
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100366 void SwapPredecessors() {
Nicolas Geoffrayc83d4412014-09-18 16:46:20 +0100367 DCHECK_EQ(predecessors_.Size(), 2u);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100368 HBasicBlock* temp = predecessors_.Get(0);
369 predecessors_.Put(0, predecessors_.Get(1));
370 predecessors_.Put(1, temp);
371 }
372
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100373 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) {
374 for (size_t i = 0, e = predecessors_.Size(); i < e; ++i) {
375 if (predecessors_.Get(i) == predecessor) {
376 return i;
377 }
378 }
379 return -1;
380 }
381
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100382 size_t GetSuccessorIndexOf(HBasicBlock* successor) {
383 for (size_t i = 0, e = successors_.Size(); i < e; ++i) {
384 if (successors_.Get(i) == successor) {
385 return i;
386 }
387 }
388 return -1;
389 }
390
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000391 void AddInstruction(HInstruction* instruction);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100392 void RemoveInstruction(HInstruction* instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100393 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +0100394 // Replace instruction `initial` with `replacement` within this block.
395 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
396 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100397 void AddPhi(HPhi* phi);
398 void RemovePhi(HPhi* phi);
399
400 bool IsLoopHeader() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100401 return (loop_information_ != nullptr) && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100402 }
403
Roland Levillain6b879dd2014-09-22 17:13:44 +0100404 bool IsLoopPreHeaderFirstPredecessor() const {
405 DCHECK(IsLoopHeader());
406 DCHECK(!GetPredecessors().IsEmpty());
407 return GetPredecessors().Get(0) == GetLoopInformation()->GetPreHeader();
408 }
409
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100410 HLoopInformation* GetLoopInformation() const {
411 return loop_information_;
412 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000413
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100414 // Set the loop_information_ on this block. This method overrides the current
415 // loop_information if it is an outer loop of the passed loop information.
416 void SetInLoop(HLoopInformation* info) {
417 if (IsLoopHeader()) {
418 // Nothing to do. This just means `info` is an outer loop.
419 } else if (loop_information_ == nullptr) {
420 loop_information_ = info;
421 } else if (loop_information_->Contains(*info->GetHeader())) {
422 // Block is currently part of an outer loop. Make it part of this inner loop.
423 // Note that a non loop header having a loop information means this loop information
424 // has already been populated
425 loop_information_ = info;
426 } else {
427 // Block is part of an inner loop. Do not update the loop information.
428 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
429 // at this point, because this method is being called while populating `info`.
430 }
431 }
432
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100433 bool IsInLoop() const { return loop_information_ != nullptr; }
434
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100435 // Returns wheter this block dominates the blocked passed as parameter.
436 bool Dominates(HBasicBlock* block) const;
437
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100438 size_t GetLifetimeStart() const { return lifetime_start_; }
439 size_t GetLifetimeEnd() const { return lifetime_end_; }
440
441 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
442 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
443
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100444 uint32_t GetDexPc() const { return dex_pc_; }
445
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000446 private:
447 HGraph* const graph_;
448 GrowableArray<HBasicBlock*> predecessors_;
449 GrowableArray<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100450 HInstructionList instructions_;
451 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000452 HLoopInformation* loop_information_;
453 HBasicBlock* dominator_;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100454 GrowableArray<HBasicBlock*> dominated_blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000455 int block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100456 // The dex program counter of the first instruction of this block.
457 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100458 size_t lifetime_start_;
459 size_t lifetime_end_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000460
461 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
462};
463
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100464#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +0000465 M(Add) \
Dave Allison20dfc792014-06-16 20:44:29 -0700466 M(Condition) \
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000467 M(Equal) \
Dave Allison20dfc792014-06-16 20:44:29 -0700468 M(NotEqual) \
469 M(LessThan) \
470 M(LessThanOrEqual) \
471 M(GreaterThan) \
472 M(GreaterThanOrEqual) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000473 M(Exit) \
474 M(Goto) \
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000475 M(If) \
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000476 M(IntConstant) \
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000477 M(InvokeStatic) \
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +0100478 M(InvokeVirtual) \
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000479 M(LoadLocal) \
480 M(Local) \
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100481 M(LongConstant) \
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +0100482 M(NewInstance) \
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +0100483 M(Not) \
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100484 M(ParameterValue) \
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100485 M(ParallelMove) \
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100486 M(Phi) \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000487 M(Return) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000488 M(ReturnVoid) \
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000489 M(StoreLocal) \
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100490 M(Sub) \
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100491 M(Compare) \
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100492 M(InstanceFieldGet) \
493 M(InstanceFieldSet) \
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100494 M(ArrayGet) \
495 M(ArraySet) \
496 M(ArrayLength) \
497 M(BoundsCheck) \
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100498 M(NullCheck) \
499 M(Temporary) \
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000500 M(SuspendCheck) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000501
Nicolas Geoffray96f89a22014-07-11 10:57:49 +0100502#define FOR_EACH_INSTRUCTION(M) \
503 FOR_EACH_CONCRETE_INSTRUCTION(M) \
Roland Levillainccc07a92014-09-16 14:48:16 +0100504 M(Constant) \
505 M(BinaryOperation)
Dave Allison20dfc792014-06-16 20:44:29 -0700506
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000507#define FORWARD_DECLARATION(type) class H##type;
508FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
509#undef FORWARD_DECLARATION
510
Roland Levillainccc07a92014-09-16 14:48:16 +0100511#define DECLARE_INSTRUCTION(type) \
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100512 virtual InstructionKind GetKind() const { return k##type; } \
Roland Levillainccc07a92014-09-16 14:48:16 +0100513 virtual const char* DebugName() const { return #type; } \
514 virtual const H##type* As##type() const OVERRIDE { return this; } \
515 virtual H##type* As##type() OVERRIDE { return this; } \
516 virtual bool InstructionTypeEquals(HInstruction* other) const { \
517 return other->Is##type(); \
518 } \
519 virtual void Accept(HGraphVisitor* visitor)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000520
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100521template <typename T>
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000522class HUseListNode : public ArenaObject {
523 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100524 HUseListNode(T* user, size_t index, HUseListNode* tail)
Dave Allison20dfc792014-06-16 20:44:29 -0700525 : user_(user), index_(index), tail_(tail) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000526
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000527 HUseListNode* GetTail() const { return tail_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100528 T* GetUser() const { return user_; }
529 size_t GetIndex() const { return index_; }
530
531 void SetTail(HUseListNode<T>* node) { tail_ = node; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000532
533 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100534 T* const user_;
535 const size_t index_;
536 HUseListNode<T>* tail_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000537
538 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
539};
540
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100541// Represents the side effects an instruction may have.
542class SideEffects : public ValueObject {
543 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100544 SideEffects() : flags_(0) {}
545
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100546 static SideEffects None() {
547 return SideEffects(0);
548 }
549
550 static SideEffects All() {
551 return SideEffects(ChangesSomething().flags_ | DependsOnSomething().flags_);
552 }
553
554 static SideEffects ChangesSomething() {
555 return SideEffects((1 << kFlagChangesCount) - 1);
556 }
557
558 static SideEffects DependsOnSomething() {
559 int count = kFlagDependsOnCount - kFlagChangesCount;
560 return SideEffects(((1 << count) - 1) << kFlagChangesCount);
561 }
562
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100563 SideEffects Union(SideEffects other) const {
564 return SideEffects(flags_ | other.flags_);
565 }
566
Roland Levillain72bceff2014-09-15 18:29:00 +0100567 bool HasSideEffects() const {
568 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
569 return (flags_ & all_bits_set) != 0;
570 }
571
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100572 bool HasAllSideEffects() const {
573 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
574 return all_bits_set == (flags_ & all_bits_set);
575 }
576
577 bool DependsOn(SideEffects other) const {
578 size_t depends_flags = other.ComputeDependsFlags();
579 return (flags_ & depends_flags) != 0;
580 }
581
582 bool HasDependencies() const {
583 int count = kFlagDependsOnCount - kFlagChangesCount;
584 size_t all_bits_set = (1 << count) - 1;
585 return ((flags_ >> kFlagChangesCount) & all_bits_set) != 0;
586 }
587
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100588 private:
589 static constexpr int kFlagChangesSomething = 0;
590 static constexpr int kFlagChangesCount = kFlagChangesSomething + 1;
591
592 static constexpr int kFlagDependsOnSomething = kFlagChangesCount;
593 static constexpr int kFlagDependsOnCount = kFlagDependsOnSomething + 1;
594
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100595 explicit SideEffects(size_t flags) : flags_(flags) {}
596
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100597 size_t ComputeDependsFlags() const {
598 return flags_ << kFlagChangesCount;
599 }
600
601 size_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100602};
603
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000604class HInstruction : public ArenaObject {
605 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100606 explicit HInstruction(SideEffects side_effects)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000607 : previous_(nullptr),
608 next_(nullptr),
609 block_(nullptr),
610 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100611 ssa_index_(-1),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000612 uses_(nullptr),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100613 env_uses_(nullptr),
614 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100615 locations_(nullptr),
616 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100617 lifetime_position_(kNoLifetime),
618 side_effects_(side_effects) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000619
Dave Allison20dfc792014-06-16 20:44:29 -0700620 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000621
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100622#define DECLARE_KIND(type) k##type,
623 enum InstructionKind {
624 FOR_EACH_INSTRUCTION(DECLARE_KIND)
625 };
626#undef DECLARE_KIND
627
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000628 HInstruction* GetNext() const { return next_; }
629 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000630
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000631 HBasicBlock* GetBlock() const { return block_; }
632 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100633 bool IsInBlock() const { return block_ != nullptr; }
634 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +0100635 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000636
Roland Levillain6b879dd2014-09-22 17:13:44 +0100637 virtual size_t InputCount() const = 0;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100638 virtual HInstruction* InputAt(size_t i) const = 0;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000639
640 virtual void Accept(HGraphVisitor* visitor) = 0;
641 virtual const char* DebugName() const = 0;
642
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100643 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100644 virtual void SetRawInputAt(size_t index, HInstruction* input) = 0;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100645
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100646 virtual bool NeedsEnvironment() const { return false; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100647 virtual bool IsControlFlow() const { return false; }
Roland Levillain72bceff2014-09-15 18:29:00 +0100648 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100649
650 void AddUseAt(HInstruction* user, size_t index) {
651 uses_ = new (block_->GetGraph()->GetArena()) HUseListNode<HInstruction>(user, index, uses_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000652 }
653
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100654 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +0100655 DCHECK(user != nullptr);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100656 env_uses_ = new (block_->GetGraph()->GetArena()) HUseListNode<HEnvironment>(
657 user, index, env_uses_);
658 }
659
660 void RemoveUser(HInstruction* user, size_t index);
Nicolas Geoffray724c9632014-09-22 12:27:27 +0100661 void RemoveEnvironmentUser(HEnvironment* user, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100662
663 HUseListNode<HInstruction>* GetUses() const { return uses_; }
664 HUseListNode<HEnvironment>* GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000665
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100666 bool HasUses() const { return uses_ != nullptr || env_uses_ != nullptr; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100667 bool HasEnvironmentUses() const { return env_uses_ != nullptr; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000668
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100669 size_t NumberOfUses() const {
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100670 // TODO: Optimize this method if it is used outside of the HGraphVisualizer.
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100671 size_t result = 0;
672 HUseListNode<HInstruction>* current = uses_;
673 while (current != nullptr) {
674 current = current->GetTail();
675 ++result;
676 }
677 return result;
678 }
679
Roland Levillainccc07a92014-09-16 14:48:16 +0100680 // Does this instruction dominate `other_instruction`? Aborts if
681 // this instruction and `other_instruction` are both phis.
682 bool Dominates(HInstruction* other_instruction) const;
683
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000684 int GetId() const { return id_; }
685 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000686
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100687 int GetSsaIndex() const { return ssa_index_; }
688 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
689 bool HasSsaIndex() const { return ssa_index_ != -1; }
690
691 bool HasEnvironment() const { return environment_ != nullptr; }
692 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100693 void SetEnvironment(HEnvironment* environment) { environment_ = environment; }
694
Nicolas Geoffray39468442014-09-02 15:17:15 +0100695 // Returns the number of entries in the environment. Typically, that is the
696 // number of dex registers in a method. It could be more in case of inlining.
697 size_t EnvironmentSize() const;
698
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000699 LocationSummary* GetLocations() const { return locations_; }
700 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000701
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100702 void ReplaceWith(HInstruction* instruction);
703
Dave Allison20dfc792014-06-16 20:44:29 -0700704 bool HasOnlyOneUse() const {
705 return uses_ != nullptr && uses_->GetTail() == nullptr;
706 }
707
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000708#define INSTRUCTION_TYPE_CHECK(type) \
Roland Levillainccc07a92014-09-16 14:48:16 +0100709 bool Is##type() const { return (As##type() != nullptr); } \
710 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000711 virtual H##type* As##type() { return nullptr; }
712
713 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
714#undef INSTRUCTION_TYPE_CHECK
715
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100716 // Returns whether the instruction can be moved within the graph.
717 virtual bool CanBeMoved() const { return false; }
718
719 // Returns whether the two instructions are of the same kind.
720 virtual bool InstructionTypeEquals(HInstruction* other) const { return false; }
721
722 // Returns whether any data encoded in the two instructions is equal.
723 // This method does not look at the inputs. Both instructions must be
724 // of the same type, otherwise the method has undefined behavior.
725 virtual bool InstructionDataEquals(HInstruction* other) const { return false; }
726
727 // Returns whether two instructions are equal, that is:
728 // 1) They have the same type and contain the same data,
729 // 2) Their inputs are identical.
730 bool Equals(HInstruction* other) const;
731
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100732 virtual InstructionKind GetKind() const = 0;
733
734 virtual size_t ComputeHashCode() const {
735 size_t result = GetKind();
736 for (size_t i = 0, e = InputCount(); i < e; ++i) {
737 result = (result * 31) + InputAt(i)->GetId();
738 }
739 return result;
740 }
741
742 SideEffects GetSideEffects() const { return side_effects_; }
743
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100744 size_t GetLifetimePosition() const { return lifetime_position_; }
745 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
746 LiveInterval* GetLiveInterval() const { return live_interval_; }
747 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
748 bool HasLiveInterval() const { return live_interval_ != nullptr; }
749
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000750 private:
751 HInstruction* previous_;
752 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000753 HBasicBlock* block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000754
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000755 // An instruction gets an id when it is added to the graph.
756 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +0100757 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000758 int id_;
759
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100760 // When doing liveness analysis, instructions that have uses get an SSA index.
761 int ssa_index_;
762
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100763 // List of instructions that have this instruction as input.
764 HUseListNode<HInstruction>* uses_;
765
766 // List of environments that contain this instruction.
767 HUseListNode<HEnvironment>* env_uses_;
768
Nicolas Geoffray39468442014-09-02 15:17:15 +0100769 // The environment associated with this instruction. Not null if the instruction
770 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100771 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000772
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000773 // Set by the code generator.
774 LocationSummary* locations_;
775
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100776 // Set by the liveness analysis.
777 LiveInterval* live_interval_;
778
779 // Set by the liveness analysis, this is the position in a linear
780 // order of blocks where this instruction's live interval start.
781 size_t lifetime_position_;
782
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100783 const SideEffects side_effects_;
784
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000785 friend class HBasicBlock;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100786 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000787
788 DISALLOW_COPY_AND_ASSIGN(HInstruction);
789};
790
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100791template<typename T>
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000792class HUseIterator : public ValueObject {
793 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100794 explicit HUseIterator(HUseListNode<T>* uses) : current_(uses) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000795
796 bool Done() const { return current_ == nullptr; }
797
798 void Advance() {
799 DCHECK(!Done());
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000800 current_ = current_->GetTail();
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000801 }
802
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100803 HUseListNode<T>* Current() const {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000804 DCHECK(!Done());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100805 return current_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000806 }
807
808 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100809 HUseListNode<T>* current_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000810
811 friend class HValue;
812};
813
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100814// A HEnvironment object contains the values of virtual registers at a given location.
815class HEnvironment : public ArenaObject {
816 public:
817 HEnvironment(ArenaAllocator* arena, size_t number_of_vregs) : vregs_(arena, number_of_vregs) {
818 vregs_.SetSize(number_of_vregs);
819 for (size_t i = 0; i < number_of_vregs; i++) {
820 vregs_.Put(i, nullptr);
821 }
822 }
823
824 void Populate(const GrowableArray<HInstruction*>& env) {
825 for (size_t i = 0; i < env.Size(); i++) {
826 HInstruction* instruction = env.Get(i);
827 vregs_.Put(i, instruction);
828 if (instruction != nullptr) {
829 instruction->AddEnvUseAt(this, i);
830 }
831 }
832 }
833
834 void SetRawEnvAt(size_t index, HInstruction* instruction) {
835 vregs_.Put(index, instruction);
836 }
837
Nicolas Geoffray39468442014-09-02 15:17:15 +0100838 HInstruction* GetInstructionAt(size_t index) const {
839 return vregs_.Get(index);
840 }
841
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100842 GrowableArray<HInstruction*>* GetVRegs() {
843 return &vregs_;
844 }
845
Nicolas Geoffray39468442014-09-02 15:17:15 +0100846 size_t Size() const { return vregs_.Size(); }
847
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100848 private:
849 GrowableArray<HInstruction*> vregs_;
850
851 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
852};
853
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000854class HInputIterator : public ValueObject {
855 public:
Dave Allison20dfc792014-06-16 20:44:29 -0700856 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000857
858 bool Done() const { return index_ == instruction_->InputCount(); }
859 HInstruction* Current() const { return instruction_->InputAt(index_); }
860 void Advance() { index_++; }
861
862 private:
863 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100864 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000865
866 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
867};
868
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000869class HInstructionIterator : public ValueObject {
870 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100871 explicit HInstructionIterator(const HInstructionList& instructions)
872 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000873 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000874 }
875
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000876 bool Done() const { return instruction_ == nullptr; }
877 HInstruction* Current() const { return instruction_; }
878 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000879 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000880 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000881 }
882
883 private:
884 HInstruction* instruction_;
885 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100886
887 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000888};
889
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100890class HBackwardInstructionIterator : public ValueObject {
891 public:
892 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
893 : instruction_(instructions.last_instruction_) {
894 next_ = Done() ? nullptr : instruction_->GetPrevious();
895 }
896
897 bool Done() const { return instruction_ == nullptr; }
898 HInstruction* Current() const { return instruction_; }
899 void Advance() {
900 instruction_ = next_;
901 next_ = Done() ? nullptr : instruction_->GetPrevious();
902 }
903
904 private:
905 HInstruction* instruction_;
906 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100907
908 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100909};
910
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000911// An embedded container with N elements of type T. Used (with partial
912// specialization for N=0) because embedded arrays cannot have size 0.
913template<typename T, intptr_t N>
914class EmbeddedArray {
915 public:
Dave Allison20dfc792014-06-16 20:44:29 -0700916 EmbeddedArray() : elements_() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000917
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000918 intptr_t GetLength() const { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000919
920 const T& operator[](intptr_t i) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000921 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000922 return elements_[i];
923 }
924
925 T& operator[](intptr_t i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000926 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000927 return elements_[i];
928 }
929
930 const T& At(intptr_t i) const {
931 return (*this)[i];
932 }
933
934 void SetAt(intptr_t i, const T& val) {
935 (*this)[i] = val;
936 }
937
938 private:
939 T elements_[N];
940};
941
942template<typename T>
943class EmbeddedArray<T, 0> {
944 public:
945 intptr_t length() const { return 0; }
946 const T& operator[](intptr_t i) const {
947 LOG(FATAL) << "Unreachable";
948 static T sentinel = 0;
949 return sentinel;
950 }
951 T& operator[](intptr_t i) {
952 LOG(FATAL) << "Unreachable";
953 static T sentinel = 0;
954 return sentinel;
955 }
956};
957
958template<intptr_t N>
959class HTemplateInstruction: public HInstruction {
960 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100961 HTemplateInstruction<N>(SideEffects side_effects)
962 : HInstruction(side_effects), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -0700963 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000964
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100965 virtual size_t InputCount() const { return N; }
966 virtual HInstruction* InputAt(size_t i) const { return inputs_[i]; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000967
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000968 protected:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100969 virtual void SetRawInputAt(size_t i, HInstruction* instruction) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000970 inputs_[i] = instruction;
971 }
972
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000973 private:
974 EmbeddedArray<HInstruction*, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100975
976 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000977};
978
Dave Allison20dfc792014-06-16 20:44:29 -0700979template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100980class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -0700981 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100982 HExpression<N>(Primitive::Type type, SideEffects side_effects)
983 : HTemplateInstruction<N>(side_effects), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -0700984 virtual ~HExpression() {}
985
986 virtual Primitive::Type GetType() const { return type_; }
987
988 private:
989 const Primitive::Type type_;
990};
991
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000992// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
993// instruction that branches to the exit block.
994class HReturnVoid : public HTemplateInstruction<0> {
995 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100996 HReturnVoid() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100997
998 virtual bool IsControlFlow() const { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000999
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001000 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001001
1002 private:
1003 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
1004};
1005
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001006// Represents dex's RETURN opcodes. A HReturn is a control flow
1007// instruction that branches to the exit block.
1008class HReturn : public HTemplateInstruction<1> {
1009 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001010 explicit HReturn(HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001011 SetRawInputAt(0, value);
1012 }
1013
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001014 virtual bool IsControlFlow() const { return true; }
1015
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001016 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001017
1018 private:
1019 DISALLOW_COPY_AND_ASSIGN(HReturn);
1020};
1021
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001022// The exit instruction is the only instruction of the exit block.
1023// Instructions aborting the method (HTrow and HReturn) must branch to the
1024// exit block.
1025class HExit : public HTemplateInstruction<0> {
1026 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001027 HExit() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001028
1029 virtual bool IsControlFlow() const { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001030
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001031 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001032
1033 private:
1034 DISALLOW_COPY_AND_ASSIGN(HExit);
1035};
1036
1037// Jumps from one block to another.
1038class HGoto : public HTemplateInstruction<0> {
1039 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001040 HGoto() : HTemplateInstruction(SideEffects::None()) {}
1041
1042 virtual bool IsControlFlow() const { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001043
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001044 HBasicBlock* GetSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001045 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001046 }
1047
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001048 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001049
1050 private:
1051 DISALLOW_COPY_AND_ASSIGN(HGoto);
1052};
1053
Dave Allison20dfc792014-06-16 20:44:29 -07001054
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001055// Conditional branch. A block ending with an HIf instruction must have
1056// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001057class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001058 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001059 explicit HIf(HInstruction* input) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001060 SetRawInputAt(0, input);
1061 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001062
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001063 virtual bool IsControlFlow() const { return true; }
1064
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001065 HBasicBlock* IfTrueSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001066 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001067 }
1068
1069 HBasicBlock* IfFalseSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001070 return GetBlock()->GetSuccessors().Get(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001071 }
1072
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001073 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001074
Dave Allison20dfc792014-06-16 20:44:29 -07001075 virtual bool IsIfInstruction() const { return true; }
1076
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001077 private:
1078 DISALLOW_COPY_AND_ASSIGN(HIf);
1079};
1080
Dave Allison20dfc792014-06-16 20:44:29 -07001081class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001082 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001083 HBinaryOperation(Primitive::Type result_type,
1084 HInstruction* left,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001085 HInstruction* right) : HExpression(result_type, SideEffects::None()) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001086 SetRawInputAt(0, left);
1087 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001088 }
1089
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001090 HInstruction* GetLeft() const { return InputAt(0); }
1091 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07001092 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001093
1094 virtual bool IsCommutative() { return false; }
1095
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001096 virtual bool CanBeMoved() const { return true; }
1097 virtual bool InstructionDataEquals(HInstruction* other) const { return true; }
1098
Roland Levillain556c3d12014-09-18 15:25:07 +01001099 // Try to statically evaluate `operation` and return an HConstant
1100 // containing the result of this evaluation. If `operation` cannot
1101 // be evaluated as a constant, return nullptr.
1102 HConstant* TryStaticEvaluation(ArenaAllocator* allocator) const;
1103
1104 // Apply this operation to `x` and `y`.
1105 virtual int32_t Evaluate(int32_t x, int32_t y) const = 0;
1106 virtual int64_t Evaluate(int64_t x, int64_t y) const = 0;
1107
Roland Levillainccc07a92014-09-16 14:48:16 +01001108 DECLARE_INSTRUCTION(BinaryOperation);
1109
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001110 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001111 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
1112};
1113
Dave Allison20dfc792014-06-16 20:44:29 -07001114class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001115 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001116 HCondition(HInstruction* first, HInstruction* second)
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001117 : HBinaryOperation(Primitive::kPrimBoolean, first, second) {}
1118
1119 virtual bool IsCommutative() { return true; }
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001120
1121 // For register allocation purposes, returns whether this instruction needs to be
1122 // materialized (that is, not just be in the processor flags).
Dave Allison20dfc792014-06-16 20:44:29 -07001123 bool NeedsMaterialization() const;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001124
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001125 // For code generation purposes, returns whether this instruction is just before
1126 // `if_`, and disregard moves in between.
1127 bool IsBeforeWhenDisregardMoves(HIf* if_) const;
1128
Dave Allison20dfc792014-06-16 20:44:29 -07001129 DECLARE_INSTRUCTION(Condition);
1130
1131 virtual IfCondition GetCondition() const = 0;
1132
1133 private:
1134 DISALLOW_COPY_AND_ASSIGN(HCondition);
1135};
1136
1137// Instruction to check if two inputs are equal to each other.
1138class HEqual : public HCondition {
1139 public:
1140 HEqual(HInstruction* first, HInstruction* second)
1141 : HCondition(first, second) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001142
Roland Levillain556c3d12014-09-18 15:25:07 +01001143 virtual int32_t Evaluate(int32_t x, int32_t y) const { return x == y; }
1144 virtual int64_t Evaluate(int64_t x, int64_t y) const { return x == y; }
1145
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001146 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001147
Dave Allison20dfc792014-06-16 20:44:29 -07001148 virtual IfCondition GetCondition() const {
1149 return kCondEQ;
1150 }
1151
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001152 private:
1153 DISALLOW_COPY_AND_ASSIGN(HEqual);
1154};
1155
Dave Allison20dfc792014-06-16 20:44:29 -07001156class HNotEqual : public HCondition {
1157 public:
1158 HNotEqual(HInstruction* first, HInstruction* second)
1159 : HCondition(first, second) {}
1160
Roland Levillain556c3d12014-09-18 15:25:07 +01001161 virtual int32_t Evaluate(int32_t x, int32_t y) const { return x != y; }
1162 virtual int64_t Evaluate(int64_t x, int64_t y) const { return x != y; }
1163
Dave Allison20dfc792014-06-16 20:44:29 -07001164 DECLARE_INSTRUCTION(NotEqual);
1165
1166 virtual IfCondition GetCondition() const {
1167 return kCondNE;
1168 }
1169
1170 private:
1171 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
1172};
1173
1174class HLessThan : public HCondition {
1175 public:
1176 HLessThan(HInstruction* first, HInstruction* second)
1177 : HCondition(first, second) {}
1178
Roland Levillain556c3d12014-09-18 15:25:07 +01001179 virtual int32_t Evaluate(int32_t x, int32_t y) const { return x < y; }
1180 virtual int64_t Evaluate(int64_t x, int64_t y) const { return x < y; }
1181
Dave Allison20dfc792014-06-16 20:44:29 -07001182 DECLARE_INSTRUCTION(LessThan);
1183
1184 virtual IfCondition GetCondition() const {
1185 return kCondLT;
1186 }
1187
1188 private:
1189 DISALLOW_COPY_AND_ASSIGN(HLessThan);
1190};
1191
1192class HLessThanOrEqual : public HCondition {
1193 public:
1194 HLessThanOrEqual(HInstruction* first, HInstruction* second)
1195 : HCondition(first, second) {}
1196
Roland Levillain556c3d12014-09-18 15:25:07 +01001197 virtual int32_t Evaluate(int32_t x, int32_t y) const { return x <= y; }
1198 virtual int64_t Evaluate(int64_t x, int64_t y) const { return x <= y; }
1199
Dave Allison20dfc792014-06-16 20:44:29 -07001200 DECLARE_INSTRUCTION(LessThanOrEqual);
1201
1202 virtual IfCondition GetCondition() const {
1203 return kCondLE;
1204 }
1205
1206 private:
1207 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
1208};
1209
1210class HGreaterThan : public HCondition {
1211 public:
1212 HGreaterThan(HInstruction* first, HInstruction* second)
1213 : HCondition(first, second) {}
1214
Roland Levillain556c3d12014-09-18 15:25:07 +01001215 virtual int32_t Evaluate(int32_t x, int32_t y) const { return x > y; }
1216 virtual int64_t Evaluate(int64_t x, int64_t y) const { return x > y; }
1217
Dave Allison20dfc792014-06-16 20:44:29 -07001218 DECLARE_INSTRUCTION(GreaterThan);
1219
1220 virtual IfCondition GetCondition() const {
1221 return kCondGT;
1222 }
1223
1224 private:
1225 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
1226};
1227
1228class HGreaterThanOrEqual : public HCondition {
1229 public:
1230 HGreaterThanOrEqual(HInstruction* first, HInstruction* second)
1231 : HCondition(first, second) {}
1232
Roland Levillain556c3d12014-09-18 15:25:07 +01001233 virtual int32_t Evaluate(int32_t x, int32_t y) const { return x >= y; }
1234 virtual int64_t Evaluate(int64_t x, int64_t y) const { return x >= y; }
1235
Dave Allison20dfc792014-06-16 20:44:29 -07001236 DECLARE_INSTRUCTION(GreaterThanOrEqual);
1237
1238 virtual IfCondition GetCondition() const {
1239 return kCondGE;
1240 }
1241
1242 private:
1243 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
1244};
1245
1246
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001247// Instruction to check how two inputs compare to each other.
1248// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
1249class HCompare : public HBinaryOperation {
1250 public:
1251 HCompare(Primitive::Type type, HInstruction* first, HInstruction* second)
1252 : HBinaryOperation(Primitive::kPrimInt, first, second) {
1253 DCHECK_EQ(type, first->GetType());
1254 DCHECK_EQ(type, second->GetType());
1255 }
1256
Roland Levillain556c3d12014-09-18 15:25:07 +01001257 virtual int32_t Evaluate(int32_t x, int32_t y) const {
1258 return
1259 x == y ? 0 :
1260 x > y ? 1 :
1261 -1;
1262 }
1263 virtual int64_t Evaluate(int64_t x, int64_t y) const {
1264 return
1265 x == y ? 0 :
1266 x > y ? 1 :
1267 -1;
1268 }
1269
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001270 DECLARE_INSTRUCTION(Compare);
1271
1272 private:
1273 DISALLOW_COPY_AND_ASSIGN(HCompare);
1274};
1275
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001276// A local in the graph. Corresponds to a Dex register.
1277class HLocal : public HTemplateInstruction<0> {
1278 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001279 explicit HLocal(uint16_t reg_number)
1280 : HTemplateInstruction(SideEffects::None()), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001281
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001282 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001283
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001284 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001285
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001286 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001287 // The Dex register number.
1288 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001289
1290 DISALLOW_COPY_AND_ASSIGN(HLocal);
1291};
1292
1293// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07001294class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001295 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001296 explicit HLoadLocal(HLocal* local, Primitive::Type type)
1297 : HExpression(type, SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001298 SetRawInputAt(0, local);
1299 }
1300
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001301 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1302
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001303 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001304
1305 private:
1306 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
1307};
1308
1309// Store a value in a given local. This instruction has two inputs: the value
1310// and the local.
1311class HStoreLocal : public HTemplateInstruction<2> {
1312 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001313 HStoreLocal(HLocal* local, HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001314 SetRawInputAt(0, local);
1315 SetRawInputAt(1, value);
1316 }
1317
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001318 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1319
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001320 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001321
1322 private:
1323 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
1324};
1325
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001326class HConstant : public HExpression<0> {
1327 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001328 explicit HConstant(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
1329
1330 virtual bool CanBeMoved() const { return true; }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001331
1332 DECLARE_INSTRUCTION(Constant);
1333
1334 private:
1335 DISALLOW_COPY_AND_ASSIGN(HConstant);
1336};
1337
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001338// Constants of the type int. Those can be from Dex instructions, or
1339// synthesized (for example with the if-eqz instruction).
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001340class HIntConstant : public HConstant {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001341 public:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001342 explicit HIntConstant(int32_t value) : HConstant(Primitive::kPrimInt), value_(value) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001343
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001344 int32_t GetValue() const { return value_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001345
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001346 virtual bool InstructionDataEquals(HInstruction* other) const {
1347 return other->AsIntConstant()->value_ == value_;
1348 }
1349
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001350 virtual size_t ComputeHashCode() const { return GetValue(); }
1351
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001352 DECLARE_INSTRUCTION(IntConstant);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001353
1354 private:
1355 const int32_t value_;
1356
1357 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
1358};
1359
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001360class HLongConstant : public HConstant {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001361 public:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001362 explicit HLongConstant(int64_t value) : HConstant(Primitive::kPrimLong), value_(value) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001363
1364 int64_t GetValue() const { return value_; }
1365
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001366 virtual bool InstructionDataEquals(HInstruction* other) const {
1367 return other->AsLongConstant()->value_ == value_;
1368 }
1369
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001370 virtual size_t ComputeHashCode() const { return static_cast<size_t>(GetValue()); }
1371
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001372 DECLARE_INSTRUCTION(LongConstant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001373
1374 private:
1375 const int64_t value_;
1376
1377 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
1378};
1379
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001380class HInvoke : public HInstruction {
1381 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001382 HInvoke(ArenaAllocator* arena,
1383 uint32_t number_of_arguments,
1384 Primitive::Type return_type,
1385 uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001386 : HInstruction(SideEffects::All()),
1387 inputs_(arena, number_of_arguments),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001388 return_type_(return_type),
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001389 dex_pc_(dex_pc) {
1390 inputs_.SetSize(number_of_arguments);
1391 }
1392
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001393 virtual size_t InputCount() const { return inputs_.Size(); }
1394 virtual HInstruction* InputAt(size_t i) const { return inputs_.Get(i); }
1395
1396 // Runtime needs to walk the stack, so Dex -> Dex calls need to
1397 // know their environment.
1398 virtual bool NeedsEnvironment() const { return true; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001399
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001400 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001401 SetRawInputAt(index, argument);
1402 }
1403
1404 virtual void SetRawInputAt(size_t index, HInstruction* input) {
1405 inputs_.Put(index, input);
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001406 }
1407
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001408 virtual Primitive::Type GetType() const { return return_type_; }
1409
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001410 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001411
1412 protected:
1413 GrowableArray<HInstruction*> inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001414 const Primitive::Type return_type_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001415 const uint32_t dex_pc_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001416
1417 private:
1418 DISALLOW_COPY_AND_ASSIGN(HInvoke);
1419};
1420
1421class HInvokeStatic : public HInvoke {
1422 public:
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001423 HInvokeStatic(ArenaAllocator* arena,
1424 uint32_t number_of_arguments,
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001425 Primitive::Type return_type,
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001426 uint32_t dex_pc,
1427 uint32_t index_in_dex_cache)
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001428 : HInvoke(arena, number_of_arguments, return_type, dex_pc),
1429 index_in_dex_cache_(index_in_dex_cache) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001430
1431 uint32_t GetIndexInDexCache() const { return index_in_dex_cache_; }
1432
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001433 DECLARE_INSTRUCTION(InvokeStatic);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001434
1435 private:
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001436 const uint32_t index_in_dex_cache_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001437
1438 DISALLOW_COPY_AND_ASSIGN(HInvokeStatic);
1439};
1440
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001441class HInvokeVirtual : public HInvoke {
1442 public:
1443 HInvokeVirtual(ArenaAllocator* arena,
1444 uint32_t number_of_arguments,
1445 Primitive::Type return_type,
1446 uint32_t dex_pc,
1447 uint32_t vtable_index)
1448 : HInvoke(arena, number_of_arguments, return_type, dex_pc),
1449 vtable_index_(vtable_index) {}
1450
1451 uint32_t GetVTableIndex() const { return vtable_index_; }
1452
1453 DECLARE_INSTRUCTION(InvokeVirtual);
1454
1455 private:
1456 const uint32_t vtable_index_;
1457
1458 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
1459};
1460
Dave Allison20dfc792014-06-16 20:44:29 -07001461class HNewInstance : public HExpression<0> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001462 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001463 HNewInstance(uint32_t dex_pc, uint16_t type_index)
1464 : HExpression(Primitive::kPrimNot, SideEffects::None()),
1465 dex_pc_(dex_pc),
1466 type_index_(type_index) {}
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001467
1468 uint32_t GetDexPc() const { return dex_pc_; }
1469 uint16_t GetTypeIndex() const { return type_index_; }
1470
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001471 // Calls runtime so needs an environment.
1472 virtual bool NeedsEnvironment() const { return true; }
1473
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001474 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001475
1476 private:
1477 const uint32_t dex_pc_;
1478 const uint16_t type_index_;
1479
1480 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
1481};
1482
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001483class HAdd : public HBinaryOperation {
1484 public:
1485 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1486 : HBinaryOperation(result_type, left, right) {}
1487
1488 virtual bool IsCommutative() { return true; }
1489
Roland Levillain556c3d12014-09-18 15:25:07 +01001490 virtual int32_t Evaluate(int32_t x, int32_t y) const { return x + y; }
1491 virtual int64_t Evaluate(int64_t x, int64_t y) const { return x + y; }
1492
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001493 DECLARE_INSTRUCTION(Add);
1494
1495 private:
1496 DISALLOW_COPY_AND_ASSIGN(HAdd);
1497};
1498
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001499class HSub : public HBinaryOperation {
1500 public:
1501 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1502 : HBinaryOperation(result_type, left, right) {}
1503
1504 virtual bool IsCommutative() { return false; }
1505
Roland Levillain556c3d12014-09-18 15:25:07 +01001506 virtual int32_t Evaluate(int32_t x, int32_t y) const { return x + y; }
1507 virtual int64_t Evaluate(int64_t x, int64_t y) const { return x + y; }
1508
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001509 DECLARE_INSTRUCTION(Sub);
1510
1511 private:
1512 DISALLOW_COPY_AND_ASSIGN(HSub);
1513};
1514
1515// The value of a parameter in this method. Its location depends on
1516// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07001517class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001518 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001519 HParameterValue(uint8_t index, Primitive::Type parameter_type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001520 : HExpression(parameter_type, SideEffects::None()), index_(index) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001521
1522 uint8_t GetIndex() const { return index_; }
1523
1524 DECLARE_INSTRUCTION(ParameterValue);
1525
1526 private:
1527 // The index of this parameter in the parameters list. Must be less
1528 // than HGraph::number_of_in_vregs_;
1529 const uint8_t index_;
1530
1531 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
1532};
1533
Dave Allison20dfc792014-06-16 20:44:29 -07001534class HNot : public HExpression<1> {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001535 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001536 explicit HNot(HInstruction* input) : HExpression(Primitive::kPrimBoolean, SideEffects::None()) {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001537 SetRawInputAt(0, input);
1538 }
1539
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001540 virtual bool CanBeMoved() const { return true; }
1541 virtual bool InstructionDataEquals(HInstruction* other) const { return true; }
1542
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001543 DECLARE_INSTRUCTION(Not);
1544
1545 private:
1546 DISALLOW_COPY_AND_ASSIGN(HNot);
1547};
1548
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001549class HPhi : public HInstruction {
1550 public:
1551 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001552 : HInstruction(SideEffects::None()),
1553 inputs_(arena, number_of_inputs),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001554 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001555 type_(type),
1556 is_live_(false) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001557 inputs_.SetSize(number_of_inputs);
1558 }
1559
1560 virtual size_t InputCount() const { return inputs_.Size(); }
1561 virtual HInstruction* InputAt(size_t i) const { return inputs_.Get(i); }
1562
1563 virtual void SetRawInputAt(size_t index, HInstruction* input) {
1564 inputs_.Put(index, input);
1565 }
1566
1567 void AddInput(HInstruction* input);
1568
1569 virtual Primitive::Type GetType() const { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001570 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001571
1572 uint32_t GetRegNumber() const { return reg_number_; }
1573
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001574 void SetDead() { is_live_ = false; }
1575 void SetLive() { is_live_ = true; }
1576 bool IsDead() const { return !is_live_; }
1577 bool IsLive() const { return is_live_; }
1578
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001579 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001580
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001581 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001582 GrowableArray<HInstruction*> inputs_;
1583 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001584 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001585 bool is_live_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001586
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001587 DISALLOW_COPY_AND_ASSIGN(HPhi);
1588};
1589
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001590class HNullCheck : public HExpression<1> {
1591 public:
1592 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001593 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001594 SetRawInputAt(0, value);
1595 }
1596
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001597 virtual bool CanBeMoved() const { return true; }
1598 virtual bool InstructionDataEquals(HInstruction* other) const { return true; }
1599
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001600 virtual bool NeedsEnvironment() const { return true; }
1601
1602 uint32_t GetDexPc() const { return dex_pc_; }
1603
1604 DECLARE_INSTRUCTION(NullCheck);
1605
1606 private:
1607 const uint32_t dex_pc_;
1608
1609 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
1610};
1611
1612class FieldInfo : public ValueObject {
1613 public:
Nicolas Geoffray39468442014-09-02 15:17:15 +01001614 explicit FieldInfo(MemberOffset field_offset, Primitive::Type field_type)
1615 : field_offset_(field_offset), field_type_(field_type) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001616
1617 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001618 Primitive::Type GetFieldType() const { return field_type_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001619
1620 private:
1621 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01001622 const Primitive::Type field_type_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001623};
1624
1625class HInstanceFieldGet : public HExpression<1> {
1626 public:
1627 HInstanceFieldGet(HInstruction* value,
1628 Primitive::Type field_type,
1629 MemberOffset field_offset)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001630 : HExpression(field_type, SideEffects::DependsOnSomething()),
1631 field_info_(field_offset, field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001632 SetRawInputAt(0, value);
1633 }
1634
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001635 virtual bool CanBeMoved() const { return true; }
1636 virtual bool InstructionDataEquals(HInstruction* other) const {
1637 size_t other_offset = other->AsInstanceFieldGet()->GetFieldOffset().SizeValue();
1638 return other_offset == GetFieldOffset().SizeValue();
1639 }
1640
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001641 virtual size_t ComputeHashCode() const {
1642 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
1643 }
1644
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001645 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001646 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001647
1648 DECLARE_INSTRUCTION(InstanceFieldGet);
1649
1650 private:
1651 const FieldInfo field_info_;
1652
1653 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
1654};
1655
1656class HInstanceFieldSet : public HTemplateInstruction<2> {
1657 public:
1658 HInstanceFieldSet(HInstruction* object,
1659 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01001660 Primitive::Type field_type,
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001661 MemberOffset field_offset)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001662 : HTemplateInstruction(SideEffects::ChangesSomething()),
1663 field_info_(field_offset, field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001664 SetRawInputAt(0, object);
1665 SetRawInputAt(1, value);
1666 }
1667
1668 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001669 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001670
1671 DECLARE_INSTRUCTION(InstanceFieldSet);
1672
1673 private:
1674 const FieldInfo field_info_;
1675
1676 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
1677};
1678
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001679class HArrayGet : public HExpression<2> {
1680 public:
1681 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001682 : HExpression(type, SideEffects::DependsOnSomething()) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001683 SetRawInputAt(0, array);
1684 SetRawInputAt(1, index);
1685 }
1686
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001687 virtual bool CanBeMoved() const { return true; }
1688 virtual bool InstructionDataEquals(HInstruction* other) const { return true; }
1689
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001690 DECLARE_INSTRUCTION(ArrayGet);
1691
1692 private:
1693 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
1694};
1695
1696class HArraySet : public HTemplateInstruction<3> {
1697 public:
1698 HArraySet(HInstruction* array,
1699 HInstruction* index,
1700 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01001701 Primitive::Type component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001702 uint32_t dex_pc)
1703 : HTemplateInstruction(SideEffects::ChangesSomething()),
1704 dex_pc_(dex_pc),
1705 component_type_(component_type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001706 SetRawInputAt(0, array);
1707 SetRawInputAt(1, index);
1708 SetRawInputAt(2, value);
1709 }
1710
1711 virtual bool NeedsEnvironment() const {
1712 // We currently always call a runtime method to catch array store
1713 // exceptions.
1714 return InputAt(2)->GetType() == Primitive::kPrimNot;
1715 }
1716
1717 uint32_t GetDexPc() const { return dex_pc_; }
1718
Nicolas Geoffray39468442014-09-02 15:17:15 +01001719 Primitive::Type GetComponentType() const { return component_type_; }
1720
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001721 DECLARE_INSTRUCTION(ArraySet);
1722
1723 private:
1724 const uint32_t dex_pc_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01001725 const Primitive::Type component_type_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001726
1727 DISALLOW_COPY_AND_ASSIGN(HArraySet);
1728};
1729
1730class HArrayLength : public HExpression<1> {
1731 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001732 explicit HArrayLength(HInstruction* array)
1733 : HExpression(Primitive::kPrimInt, SideEffects::None()) {
1734 // Note that arrays do not change length, so the instruction does not
1735 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001736 SetRawInputAt(0, array);
1737 }
1738
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001739 virtual bool CanBeMoved() const { return true; }
1740 virtual bool InstructionDataEquals(HInstruction* other) const { return true; }
1741
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001742 DECLARE_INSTRUCTION(ArrayLength);
1743
1744 private:
1745 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
1746};
1747
1748class HBoundsCheck : public HExpression<2> {
1749 public:
1750 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001751 : HExpression(index->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001752 DCHECK(index->GetType() == Primitive::kPrimInt);
1753 SetRawInputAt(0, index);
1754 SetRawInputAt(1, length);
1755 }
1756
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001757 virtual bool CanBeMoved() const { return true; }
1758 virtual bool InstructionDataEquals(HInstruction* other) const { return true; }
1759
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001760 virtual bool NeedsEnvironment() const { return true; }
1761
1762 uint32_t GetDexPc() const { return dex_pc_; }
1763
1764 DECLARE_INSTRUCTION(BoundsCheck);
1765
1766 private:
1767 const uint32_t dex_pc_;
1768
1769 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
1770};
1771
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001772/**
1773 * Some DEX instructions are folded into multiple HInstructions that need
1774 * to stay live until the last HInstruction. This class
1775 * is used as a marker for the baseline compiler to ensure its preceding
1776 * HInstruction stays live. `index` is the temporary number that is used
1777 * for knowing the stack offset where to store the instruction.
1778 */
1779class HTemporary : public HTemplateInstruction<0> {
1780 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001781 explicit HTemporary(size_t index) : HTemplateInstruction(SideEffects::None()), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001782
1783 size_t GetIndex() const { return index_; }
1784
1785 DECLARE_INSTRUCTION(Temporary);
1786
1787 private:
1788 const size_t index_;
1789
1790 DISALLOW_COPY_AND_ASSIGN(HTemporary);
1791};
1792
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00001793class HSuspendCheck : public HTemplateInstruction<0> {
1794 public:
1795 explicit HSuspendCheck(uint32_t dex_pc)
1796 : HTemplateInstruction(SideEffects::ChangesSomething()), dex_pc_(dex_pc) {}
1797
1798 virtual bool NeedsEnvironment() const {
1799 return true;
1800 }
1801
1802 uint32_t GetDexPc() const { return dex_pc_; }
1803
1804 DECLARE_INSTRUCTION(SuspendCheck);
1805
1806 private:
1807 const uint32_t dex_pc_;
1808
1809 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
1810};
1811
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001812class MoveOperands : public ArenaObject {
1813 public:
1814 MoveOperands(Location source, Location destination)
1815 : source_(source), destination_(destination) {}
1816
1817 Location GetSource() const { return source_; }
1818 Location GetDestination() const { return destination_; }
1819
1820 void SetSource(Location value) { source_ = value; }
1821 void SetDestination(Location value) { destination_ = value; }
1822
1823 // The parallel move resolver marks moves as "in-progress" by clearing the
1824 // destination (but not the source).
1825 Location MarkPending() {
1826 DCHECK(!IsPending());
1827 Location dest = destination_;
1828 destination_ = Location::NoLocation();
1829 return dest;
1830 }
1831
1832 void ClearPending(Location dest) {
1833 DCHECK(IsPending());
1834 destination_ = dest;
1835 }
1836
1837 bool IsPending() const {
1838 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
1839 return destination_.IsInvalid() && !source_.IsInvalid();
1840 }
1841
1842 // True if this blocks a move from the given location.
1843 bool Blocks(Location loc) const {
1844 return !IsEliminated() && source_.Equals(loc);
1845 }
1846
1847 // A move is redundant if it's been eliminated, if its source and
1848 // destination are the same, or if its destination is unneeded.
1849 bool IsRedundant() const {
1850 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
1851 }
1852
1853 // We clear both operands to indicate move that's been eliminated.
1854 void Eliminate() {
1855 source_ = destination_ = Location::NoLocation();
1856 }
1857
1858 bool IsEliminated() const {
1859 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
1860 return source_.IsInvalid();
1861 }
1862
1863 private:
1864 Location source_;
1865 Location destination_;
1866
1867 DISALLOW_COPY_AND_ASSIGN(MoveOperands);
1868};
1869
1870static constexpr size_t kDefaultNumberOfMoves = 4;
1871
1872class HParallelMove : public HTemplateInstruction<0> {
1873 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001874 explicit HParallelMove(ArenaAllocator* arena)
1875 : HTemplateInstruction(SideEffects::None()), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001876
1877 void AddMove(MoveOperands* move) {
1878 moves_.Add(move);
1879 }
1880
1881 MoveOperands* MoveOperandsAt(size_t index) const {
1882 return moves_.Get(index);
1883 }
1884
1885 size_t NumMoves() const { return moves_.Size(); }
1886
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001887 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001888
1889 private:
1890 GrowableArray<MoveOperands*> moves_;
1891
1892 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
1893};
1894
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001895class HGraphVisitor : public ValueObject {
1896 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001897 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
1898 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001899
Dave Allison20dfc792014-06-16 20:44:29 -07001900 virtual void VisitInstruction(HInstruction* instruction) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001901 virtual void VisitBasicBlock(HBasicBlock* block);
1902
1903 void VisitInsertionOrder();
1904
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001905 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001906
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001907 // Visit functions for instruction classes.
1908#define DECLARE_VISIT_INSTRUCTION(name) \
1909 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
1910
1911 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
1912
1913#undef DECLARE_VISIT_INSTRUCTION
1914
1915 private:
1916 HGraph* graph_;
1917
1918 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
1919};
1920
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001921class HInsertionOrderIterator : public ValueObject {
1922 public:
1923 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
1924
1925 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
1926 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
1927 void Advance() { ++index_; }
1928
1929 private:
1930 const HGraph& graph_;
1931 size_t index_;
1932
1933 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
1934};
1935
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001936class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001937 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001938 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001939
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001940 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
1941 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001942 void Advance() { ++index_; }
1943
1944 private:
1945 const HGraph& graph_;
1946 size_t index_;
1947
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001948 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001949};
1950
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001951class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001952 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001953 explicit HPostOrderIterator(const HGraph& graph)
1954 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001955
1956 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001957 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001958 void Advance() { --index_; }
1959
1960 private:
1961 const HGraph& graph_;
1962 size_t index_;
1963
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001964 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001965};
1966
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001967} // namespace art
1968
1969#endif // ART_COMPILER_OPTIMIZING_NODES_H_