blob: 33bfe190818783a50f54cedcaa8dee00bea891d9 [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"
Ian Rogers0279ebb2014-10-08 17:27:48 -070023#include "utils/arena_object.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 Levillain6b469232014-09-25 10:10:38 +010061 // Return true if this list contains `instruction`.
62 bool Contains(HInstruction* instruction) const;
63
Roland Levillainccc07a92014-09-16 14:48:16 +010064 // Return true if `instruction1` is found before `instruction2` in
65 // this instruction list and false otherwise. Abort if none
66 // of these instructions is found.
67 bool FoundBefore(const HInstruction* instruction1,
68 const HInstruction* instruction2) const;
69
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010070 private:
71 HInstruction* first_instruction_;
72 HInstruction* last_instruction_;
73
74 friend class HBasicBlock;
75 friend class HInstructionIterator;
Nicolas Geoffray804d0932014-05-02 08:46:00 +010076 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010077
78 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
79};
80
Nicolas Geoffray818f2102014-02-18 16:43:35 +000081// Control-flow graph of a method. Contains a list of basic blocks.
82class HGraph : public ArenaObject {
83 public:
84 explicit HGraph(ArenaAllocator* arena)
85 : arena_(arena),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000086 blocks_(arena, kDefaultNumberOfBlocks),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +010087 reverse_post_order_(arena, kDefaultNumberOfBlocks),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +010088 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +010089 number_of_vregs_(0),
90 number_of_in_vregs_(0),
Nicolas Geoffraye5038322014-07-04 09:41:32 +010091 number_of_temporaries_(0),
Dave Allison20dfc792014-06-16 20:44:29 -070092 current_instruction_id_(0) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +000093
Nicolas Geoffray787c3072014-03-17 10:20:19 +000094 ArenaAllocator* GetArena() const { return arena_; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +010095 const GrowableArray<HBasicBlock*>& GetBlocks() const { return blocks_; }
Roland Levillain93445682014-10-06 19:24:02 +010096 HBasicBlock* GetBlock(size_t id) const { return blocks_.Get(id); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +000097
Nicolas Geoffray787c3072014-03-17 10:20:19 +000098 HBasicBlock* GetEntryBlock() const { return entry_block_; }
99 HBasicBlock* GetExitBlock() const { return exit_block_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000100
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000101 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
102 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000103
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000104 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100105
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000106 void BuildDominatorTree();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100107 void TransformToSSA();
108 void SimplifyCFG();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000109
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100110 // Find all natural loops in this graph. Aborts computation and returns false
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100111 // if one loop is not natural, that is the header does not dominate the back
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100112 // edge.
113 bool FindNaturalLoops() const;
114
115 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
116 void SimplifyLoop(HBasicBlock* header);
117
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000118 int GetNextInstructionId() {
119 return current_instruction_id_++;
120 }
121
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100122 uint16_t GetMaximumNumberOfOutVRegs() const {
123 return maximum_number_of_out_vregs_;
124 }
125
126 void UpdateMaximumNumberOfOutVRegs(uint16_t new_value) {
127 maximum_number_of_out_vregs_ = std::max(new_value, maximum_number_of_out_vregs_);
128 }
129
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100130 void UpdateNumberOfTemporaries(size_t count) {
131 number_of_temporaries_ = std::max(count, number_of_temporaries_);
132 }
133
134 size_t GetNumberOfTemporaries() const {
135 return number_of_temporaries_;
136 }
137
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100138 void SetNumberOfVRegs(uint16_t number_of_vregs) {
139 number_of_vregs_ = number_of_vregs;
140 }
141
142 uint16_t GetNumberOfVRegs() const {
143 return number_of_vregs_;
144 }
145
146 void SetNumberOfInVRegs(uint16_t value) {
147 number_of_in_vregs_ = value;
148 }
149
150 uint16_t GetNumberOfInVRegs() const {
151 return number_of_in_vregs_;
152 }
153
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100154 uint16_t GetNumberOfLocalVRegs() const {
155 return number_of_vregs_ - number_of_in_vregs_;
156 }
157
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100158 const GrowableArray<HBasicBlock*>& GetReversePostOrder() const {
159 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100160 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100161
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000162 private:
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000163 HBasicBlock* FindCommonDominator(HBasicBlock* first, HBasicBlock* second) const;
164 void VisitBlockForDominatorTree(HBasicBlock* block,
165 HBasicBlock* predecessor,
166 GrowableArray<size_t>* visits);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100167 void FindBackEdges(ArenaBitVector* visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000168 void VisitBlockForBackEdges(HBasicBlock* block,
169 ArenaBitVector* visited,
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100170 ArenaBitVector* visiting);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000171 void RemoveDeadBlocks(const ArenaBitVector& visited) const;
172
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000173 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000174
175 // List of blocks in insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000176 GrowableArray<HBasicBlock*> blocks_;
177
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100178 // List of blocks to perform a reverse post order tree traversal.
179 GrowableArray<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000180
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000181 HBasicBlock* entry_block_;
182 HBasicBlock* exit_block_;
183
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100184 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100185 uint16_t maximum_number_of_out_vregs_;
186
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100187 // The number of virtual registers in this method. Contains the parameters.
188 uint16_t number_of_vregs_;
189
190 // The number of virtual registers used by parameters of this method.
191 uint16_t number_of_in_vregs_;
192
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100193 // The number of temporaries that will be needed for the baseline compiler.
194 size_t number_of_temporaries_;
195
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000196 // The current id to assign to a newly added instruction. See HInstruction.id_.
197 int current_instruction_id_;
198
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000199 DISALLOW_COPY_AND_ASSIGN(HGraph);
200};
201
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000202class HLoopInformation : public ArenaObject {
203 public:
204 HLoopInformation(HBasicBlock* header, HGraph* graph)
205 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100206 suspend_check_(nullptr),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100207 back_edges_(graph->GetArena(), kDefaultNumberOfBackEdges),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100208 // Make bit vector growable, as the number of blocks may change.
209 blocks_(graph->GetArena(), graph->GetBlocks().Size(), true) {}
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100210
211 HBasicBlock* GetHeader() const {
212 return header_;
213 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000214
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100215 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
216 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
217 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
218
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000219 void AddBackEdge(HBasicBlock* back_edge) {
220 back_edges_.Add(back_edge);
221 }
222
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100223 void RemoveBackEdge(HBasicBlock* back_edge) {
224 back_edges_.Delete(back_edge);
225 }
226
227 bool IsBackEdge(HBasicBlock* block) {
228 for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) {
229 if (back_edges_.Get(i) == block) return true;
230 }
231 return false;
232 }
233
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000234 int NumberOfBackEdges() const {
235 return back_edges_.Size();
236 }
237
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100238 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100239
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100240 const GrowableArray<HBasicBlock*>& GetBackEdges() const {
241 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100242 }
243
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100244 void ClearBackEdges() {
245 back_edges_.Reset();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100246 }
247
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100248 // Find blocks that are part of this loop. Returns whether the loop is a natural loop,
249 // that is the header dominates the back edge.
250 bool Populate();
251
252 // Returns whether this loop information contains `block`.
253 // Note that this loop information *must* be populated before entering this function.
254 bool Contains(const HBasicBlock& block) const;
255
256 // Returns whether this loop information is an inner loop of `other`.
257 // Note that `other` *must* be populated before entering this function.
258 bool IsIn(const HLoopInformation& other) const;
259
260 const ArenaBitVector& GetBlocks() const { return blocks_; }
261
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000262 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100263 // Internal recursive implementation of `Populate`.
264 void PopulateRecursive(HBasicBlock* block);
265
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000266 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100267 HSuspendCheck* suspend_check_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000268 GrowableArray<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100269 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000270
271 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
272};
273
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100274static constexpr size_t kNoLifetime = -1;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100275static constexpr uint32_t kNoDexPc = -1;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100276
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000277// A block in a method. Contains the list of instructions represented
278// as a double linked list. Each block knows its predecessors and
279// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100280
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000281class HBasicBlock : public ArenaObject {
282 public:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100283 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000284 : graph_(graph),
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000285 predecessors_(graph->GetArena(), kDefaultNumberOfPredecessors),
286 successors_(graph->GetArena(), kDefaultNumberOfSuccessors),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000287 loop_information_(nullptr),
288 dominator_(nullptr),
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100289 dominated_blocks_(graph->GetArena(), kDefaultNumberOfDominatedBlocks),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100290 block_id_(-1),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100291 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100292 lifetime_start_(kNoLifetime),
293 lifetime_end_(kNoLifetime) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000294
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100295 const GrowableArray<HBasicBlock*>& GetPredecessors() const {
296 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000297 }
298
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100299 const GrowableArray<HBasicBlock*>& GetSuccessors() const {
300 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000301 }
302
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100303 const GrowableArray<HBasicBlock*>& GetDominatedBlocks() const {
304 return dominated_blocks_;
305 }
306
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100307 bool IsEntryBlock() const {
308 return graph_->GetEntryBlock() == this;
309 }
310
311 bool IsExitBlock() const {
312 return graph_->GetExitBlock() == this;
313 }
314
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000315 void AddBackEdge(HBasicBlock* back_edge) {
316 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000317 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000318 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100319 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000320 loop_information_->AddBackEdge(back_edge);
321 }
322
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000323 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000324
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000325 int GetBlockId() const { return block_id_; }
326 void SetBlockId(int id) { block_id_ = id; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000327
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000328 HBasicBlock* GetDominator() const { return dominator_; }
329 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100330 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.Add(block); }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000331
332 int NumberOfBackEdges() const {
333 return loop_information_ == nullptr
334 ? 0
335 : loop_information_->NumberOfBackEdges();
336 }
337
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100338 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
339 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100340 const HInstructionList& GetInstructions() const { return instructions_; }
341 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100342 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000343
344 void AddSuccessor(HBasicBlock* block) {
345 successors_.Add(block);
346 block->predecessors_.Add(this);
347 }
348
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100349 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
350 size_t successor_index = GetSuccessorIndexOf(existing);
351 DCHECK_NE(successor_index, static_cast<size_t>(-1));
352 existing->RemovePredecessor(this);
353 new_block->predecessors_.Add(this);
354 successors_.Put(successor_index, new_block);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000355 }
356
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100357 void RemovePredecessor(HBasicBlock* block) {
358 predecessors_.Delete(block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100359 }
360
361 void ClearAllPredecessors() {
362 predecessors_.Reset();
363 }
364
365 void AddPredecessor(HBasicBlock* block) {
366 predecessors_.Add(block);
367 block->successors_.Add(this);
368 }
369
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100370 void SwapPredecessors() {
Nicolas Geoffrayc83d4412014-09-18 16:46:20 +0100371 DCHECK_EQ(predecessors_.Size(), 2u);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100372 HBasicBlock* temp = predecessors_.Get(0);
373 predecessors_.Put(0, predecessors_.Get(1));
374 predecessors_.Put(1, temp);
375 }
376
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100377 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) {
378 for (size_t i = 0, e = predecessors_.Size(); i < e; ++i) {
379 if (predecessors_.Get(i) == predecessor) {
380 return i;
381 }
382 }
383 return -1;
384 }
385
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100386 size_t GetSuccessorIndexOf(HBasicBlock* successor) {
387 for (size_t i = 0, e = successors_.Size(); i < e; ++i) {
388 if (successors_.Get(i) == successor) {
389 return i;
390 }
391 }
392 return -1;
393 }
394
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000395 void AddInstruction(HInstruction* instruction);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100396 void RemoveInstruction(HInstruction* instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100397 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +0100398 // Replace instruction `initial` with `replacement` within this block.
399 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
400 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100401 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100402 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100403 void RemovePhi(HPhi* phi);
404
405 bool IsLoopHeader() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100406 return (loop_information_ != nullptr) && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100407 }
408
Roland Levillain6b879dd2014-09-22 17:13:44 +0100409 bool IsLoopPreHeaderFirstPredecessor() const {
410 DCHECK(IsLoopHeader());
411 DCHECK(!GetPredecessors().IsEmpty());
412 return GetPredecessors().Get(0) == GetLoopInformation()->GetPreHeader();
413 }
414
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100415 HLoopInformation* GetLoopInformation() const {
416 return loop_information_;
417 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000418
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100419 // Set the loop_information_ on this block. This method overrides the current
420 // loop_information if it is an outer loop of the passed loop information.
421 void SetInLoop(HLoopInformation* info) {
422 if (IsLoopHeader()) {
423 // Nothing to do. This just means `info` is an outer loop.
424 } else if (loop_information_ == nullptr) {
425 loop_information_ = info;
426 } else if (loop_information_->Contains(*info->GetHeader())) {
427 // Block is currently part of an outer loop. Make it part of this inner loop.
428 // Note that a non loop header having a loop information means this loop information
429 // has already been populated
430 loop_information_ = info;
431 } else {
432 // Block is part of an inner loop. Do not update the loop information.
433 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
434 // at this point, because this method is being called while populating `info`.
435 }
436 }
437
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100438 bool IsInLoop() const { return loop_information_ != nullptr; }
439
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100440 // Returns wheter this block dominates the blocked passed as parameter.
441 bool Dominates(HBasicBlock* block) const;
442
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100443 size_t GetLifetimeStart() const { return lifetime_start_; }
444 size_t GetLifetimeEnd() const { return lifetime_end_; }
445
446 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
447 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
448
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100449 uint32_t GetDexPc() const { return dex_pc_; }
450
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000451 private:
452 HGraph* const graph_;
453 GrowableArray<HBasicBlock*> predecessors_;
454 GrowableArray<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100455 HInstructionList instructions_;
456 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000457 HLoopInformation* loop_information_;
458 HBasicBlock* dominator_;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100459 GrowableArray<HBasicBlock*> dominated_blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000460 int block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100461 // The dex program counter of the first instruction of this block.
462 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100463 size_t lifetime_start_;
464 size_t lifetime_end_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000465
466 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
467};
468
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100469#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
470 M(Add, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000471 M(ArrayGet, Instruction) \
472 M(ArrayLength, Instruction) \
473 M(ArraySet, Instruction) \
474 M(BoundsCheck, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100475 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000476 M(Compare, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100477 M(Condition, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000478 M(Div, BinaryOperation) \
479 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100480 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000481 M(Exit, Instruction) \
482 M(FloatConstant, Constant) \
483 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100484 M(GreaterThan, Condition) \
485 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100486 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000487 M(InstanceFieldGet, Instruction) \
488 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100489 M(IntConstant, Constant) \
490 M(InvokeStatic, Invoke) \
491 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000492 M(LessThan, Condition) \
493 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000494 M(LoadClass, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100495 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000496 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100497 M(Local, Instruction) \
498 M(LongConstant, Constant) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000499 M(Mul, BinaryOperation) \
500 M(Neg, UnaryOperation) \
501 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100502 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +0100503 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000504 M(NotEqual, Condition) \
505 M(NullCheck, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100506 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000507 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100508 M(Phi, Instruction) \
509 M(Return, Instruction) \
510 M(ReturnVoid, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100511 M(StaticFieldGet, Instruction) \
512 M(StaticFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100513 M(StoreLocal, Instruction) \
514 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100515 M(SuspendCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000516 M(Temporary, Instruction) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000517
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100518#define FOR_EACH_INSTRUCTION(M) \
519 FOR_EACH_CONCRETE_INSTRUCTION(M) \
520 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +0100521 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +0100522 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100523 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -0700524
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100525#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000526FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
527#undef FORWARD_DECLARATION
528
Roland Levillainccc07a92014-09-16 14:48:16 +0100529#define DECLARE_INSTRUCTION(type) \
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100530 virtual InstructionKind GetKind() const { return k##type; } \
Roland Levillainccc07a92014-09-16 14:48:16 +0100531 virtual const char* DebugName() const { return #type; } \
532 virtual const H##type* As##type() const OVERRIDE { return this; } \
533 virtual H##type* As##type() OVERRIDE { return this; } \
534 virtual bool InstructionTypeEquals(HInstruction* other) const { \
535 return other->Is##type(); \
536 } \
537 virtual void Accept(HGraphVisitor* visitor)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000538
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100539template <typename T>
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000540class HUseListNode : public ArenaObject {
541 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100542 HUseListNode(T* user, size_t index, HUseListNode* tail)
Dave Allison20dfc792014-06-16 20:44:29 -0700543 : user_(user), index_(index), tail_(tail) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000544
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000545 HUseListNode* GetTail() const { return tail_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100546 T* GetUser() const { return user_; }
547 size_t GetIndex() const { return index_; }
548
549 void SetTail(HUseListNode<T>* node) { tail_ = node; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000550
551 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100552 T* const user_;
553 const size_t index_;
554 HUseListNode<T>* tail_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000555
556 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
557};
558
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100559// Represents the side effects an instruction may have.
560class SideEffects : public ValueObject {
561 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100562 SideEffects() : flags_(0) {}
563
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100564 static SideEffects None() {
565 return SideEffects(0);
566 }
567
568 static SideEffects All() {
569 return SideEffects(ChangesSomething().flags_ | DependsOnSomething().flags_);
570 }
571
572 static SideEffects ChangesSomething() {
573 return SideEffects((1 << kFlagChangesCount) - 1);
574 }
575
576 static SideEffects DependsOnSomething() {
577 int count = kFlagDependsOnCount - kFlagChangesCount;
578 return SideEffects(((1 << count) - 1) << kFlagChangesCount);
579 }
580
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100581 SideEffects Union(SideEffects other) const {
582 return SideEffects(flags_ | other.flags_);
583 }
584
Roland Levillain72bceff2014-09-15 18:29:00 +0100585 bool HasSideEffects() const {
586 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
587 return (flags_ & all_bits_set) != 0;
588 }
589
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100590 bool HasAllSideEffects() const {
591 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
592 return all_bits_set == (flags_ & all_bits_set);
593 }
594
595 bool DependsOn(SideEffects other) const {
596 size_t depends_flags = other.ComputeDependsFlags();
597 return (flags_ & depends_flags) != 0;
598 }
599
600 bool HasDependencies() const {
601 int count = kFlagDependsOnCount - kFlagChangesCount;
602 size_t all_bits_set = (1 << count) - 1;
603 return ((flags_ >> kFlagChangesCount) & all_bits_set) != 0;
604 }
605
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100606 private:
607 static constexpr int kFlagChangesSomething = 0;
608 static constexpr int kFlagChangesCount = kFlagChangesSomething + 1;
609
610 static constexpr int kFlagDependsOnSomething = kFlagChangesCount;
611 static constexpr int kFlagDependsOnCount = kFlagDependsOnSomething + 1;
612
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100613 explicit SideEffects(size_t flags) : flags_(flags) {}
614
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100615 size_t ComputeDependsFlags() const {
616 return flags_ << kFlagChangesCount;
617 }
618
619 size_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100620};
621
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000622class HInstruction : public ArenaObject {
623 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100624 explicit HInstruction(SideEffects side_effects)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000625 : previous_(nullptr),
626 next_(nullptr),
627 block_(nullptr),
628 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100629 ssa_index_(-1),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000630 uses_(nullptr),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100631 env_uses_(nullptr),
632 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100633 locations_(nullptr),
634 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100635 lifetime_position_(kNoLifetime),
636 side_effects_(side_effects) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000637
Dave Allison20dfc792014-06-16 20:44:29 -0700638 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000639
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100640#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100641 enum InstructionKind {
642 FOR_EACH_INSTRUCTION(DECLARE_KIND)
643 };
644#undef DECLARE_KIND
645
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000646 HInstruction* GetNext() const { return next_; }
647 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000648
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000649 HBasicBlock* GetBlock() const { return block_; }
650 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100651 bool IsInBlock() const { return block_ != nullptr; }
652 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +0100653 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000654
Roland Levillain6b879dd2014-09-22 17:13:44 +0100655 virtual size_t InputCount() const = 0;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100656 virtual HInstruction* InputAt(size_t i) const = 0;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000657
658 virtual void Accept(HGraphVisitor* visitor) = 0;
659 virtual const char* DebugName() const = 0;
660
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100661 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100662 virtual void SetRawInputAt(size_t index, HInstruction* input) = 0;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100663
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100664 virtual bool NeedsEnvironment() const { return false; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100665 virtual bool IsControlFlow() const { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +0100666 virtual bool CanThrow() const { return false; }
Roland Levillain72bceff2014-09-15 18:29:00 +0100667 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100668
669 void AddUseAt(HInstruction* user, size_t index) {
670 uses_ = new (block_->GetGraph()->GetArena()) HUseListNode<HInstruction>(user, index, uses_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000671 }
672
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100673 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +0100674 DCHECK(user != nullptr);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100675 env_uses_ = new (block_->GetGraph()->GetArena()) HUseListNode<HEnvironment>(
676 user, index, env_uses_);
677 }
678
679 void RemoveUser(HInstruction* user, size_t index);
Nicolas Geoffray724c9632014-09-22 12:27:27 +0100680 void RemoveEnvironmentUser(HEnvironment* user, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100681
682 HUseListNode<HInstruction>* GetUses() const { return uses_; }
683 HUseListNode<HEnvironment>* GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000684
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100685 bool HasUses() const { return uses_ != nullptr || env_uses_ != nullptr; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100686 bool HasEnvironmentUses() const { return env_uses_ != nullptr; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000687
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100688 size_t NumberOfUses() const {
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100689 // TODO: Optimize this method if it is used outside of the HGraphVisualizer.
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100690 size_t result = 0;
691 HUseListNode<HInstruction>* current = uses_;
692 while (current != nullptr) {
693 current = current->GetTail();
694 ++result;
695 }
696 return result;
697 }
698
Roland Levillain6c82d402014-10-13 16:10:27 +0100699 // Does this instruction strictly dominate `other_instruction`?
700 // Returns false if this instruction and `other_instruction` are the same.
701 // Aborts if this instruction and `other_instruction` are both phis.
702 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +0100703
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000704 int GetId() const { return id_; }
705 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000706
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100707 int GetSsaIndex() const { return ssa_index_; }
708 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
709 bool HasSsaIndex() const { return ssa_index_ != -1; }
710
711 bool HasEnvironment() const { return environment_ != nullptr; }
712 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100713 void SetEnvironment(HEnvironment* environment) { environment_ = environment; }
714
Nicolas Geoffray39468442014-09-02 15:17:15 +0100715 // Returns the number of entries in the environment. Typically, that is the
716 // number of dex registers in a method. It could be more in case of inlining.
717 size_t EnvironmentSize() const;
718
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000719 LocationSummary* GetLocations() const { return locations_; }
720 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000721
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100722 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100723 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100724
Dave Allison20dfc792014-06-16 20:44:29 -0700725 bool HasOnlyOneUse() const {
726 return uses_ != nullptr && uses_->GetTail() == nullptr;
727 }
728
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100729#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +0100730 bool Is##type() const { return (As##type() != nullptr); } \
731 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000732 virtual H##type* As##type() { return nullptr; }
733
734 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
735#undef INSTRUCTION_TYPE_CHECK
736
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100737 // Returns whether the instruction can be moved within the graph.
738 virtual bool CanBeMoved() const { return false; }
739
740 // Returns whether the two instructions are of the same kind.
741 virtual bool InstructionTypeEquals(HInstruction* other) const { return false; }
742
743 // Returns whether any data encoded in the two instructions is equal.
744 // This method does not look at the inputs. Both instructions must be
745 // of the same type, otherwise the method has undefined behavior.
746 virtual bool InstructionDataEquals(HInstruction* other) const { return false; }
747
748 // Returns whether two instructions are equal, that is:
749 // 1) They have the same type and contain the same data,
750 // 2) Their inputs are identical.
751 bool Equals(HInstruction* other) const;
752
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100753 virtual InstructionKind GetKind() const = 0;
754
755 virtual size_t ComputeHashCode() const {
756 size_t result = GetKind();
757 for (size_t i = 0, e = InputCount(); i < e; ++i) {
758 result = (result * 31) + InputAt(i)->GetId();
759 }
760 return result;
761 }
762
763 SideEffects GetSideEffects() const { return side_effects_; }
764
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100765 size_t GetLifetimePosition() const { return lifetime_position_; }
766 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
767 LiveInterval* GetLiveInterval() const { return live_interval_; }
768 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
769 bool HasLiveInterval() const { return live_interval_ != nullptr; }
770
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000771 private:
772 HInstruction* previous_;
773 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000774 HBasicBlock* block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000775
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000776 // An instruction gets an id when it is added to the graph.
777 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +0100778 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000779 int id_;
780
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100781 // When doing liveness analysis, instructions that have uses get an SSA index.
782 int ssa_index_;
783
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100784 // List of instructions that have this instruction as input.
785 HUseListNode<HInstruction>* uses_;
786
787 // List of environments that contain this instruction.
788 HUseListNode<HEnvironment>* env_uses_;
789
Nicolas Geoffray39468442014-09-02 15:17:15 +0100790 // The environment associated with this instruction. Not null if the instruction
791 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100792 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000793
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000794 // Set by the code generator.
795 LocationSummary* locations_;
796
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100797 // Set by the liveness analysis.
798 LiveInterval* live_interval_;
799
800 // Set by the liveness analysis, this is the position in a linear
801 // order of blocks where this instruction's live interval start.
802 size_t lifetime_position_;
803
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100804 const SideEffects side_effects_;
805
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000806 friend class HBasicBlock;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100807 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000808
809 DISALLOW_COPY_AND_ASSIGN(HInstruction);
810};
811
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100812template<typename T>
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000813class HUseIterator : public ValueObject {
814 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100815 explicit HUseIterator(HUseListNode<T>* uses) : current_(uses) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000816
817 bool Done() const { return current_ == nullptr; }
818
819 void Advance() {
820 DCHECK(!Done());
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000821 current_ = current_->GetTail();
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000822 }
823
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100824 HUseListNode<T>* Current() const {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000825 DCHECK(!Done());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100826 return current_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000827 }
828
829 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100830 HUseListNode<T>* current_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000831
832 friend class HValue;
833};
834
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100835// A HEnvironment object contains the values of virtual registers at a given location.
836class HEnvironment : public ArenaObject {
837 public:
838 HEnvironment(ArenaAllocator* arena, size_t number_of_vregs) : vregs_(arena, number_of_vregs) {
839 vregs_.SetSize(number_of_vregs);
840 for (size_t i = 0; i < number_of_vregs; i++) {
841 vregs_.Put(i, nullptr);
842 }
843 }
844
845 void Populate(const GrowableArray<HInstruction*>& env) {
846 for (size_t i = 0; i < env.Size(); i++) {
847 HInstruction* instruction = env.Get(i);
848 vregs_.Put(i, instruction);
849 if (instruction != nullptr) {
850 instruction->AddEnvUseAt(this, i);
851 }
852 }
853 }
854
855 void SetRawEnvAt(size_t index, HInstruction* instruction) {
856 vregs_.Put(index, instruction);
857 }
858
Nicolas Geoffray39468442014-09-02 15:17:15 +0100859 HInstruction* GetInstructionAt(size_t index) const {
860 return vregs_.Get(index);
861 }
862
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100863 GrowableArray<HInstruction*>* GetVRegs() {
864 return &vregs_;
865 }
866
Nicolas Geoffray39468442014-09-02 15:17:15 +0100867 size_t Size() const { return vregs_.Size(); }
868
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100869 private:
870 GrowableArray<HInstruction*> vregs_;
871
872 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
873};
874
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000875class HInputIterator : public ValueObject {
876 public:
Dave Allison20dfc792014-06-16 20:44:29 -0700877 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000878
879 bool Done() const { return index_ == instruction_->InputCount(); }
880 HInstruction* Current() const { return instruction_->InputAt(index_); }
881 void Advance() { index_++; }
882
883 private:
884 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100885 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000886
887 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
888};
889
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000890class HInstructionIterator : public ValueObject {
891 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100892 explicit HInstructionIterator(const HInstructionList& instructions)
893 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000894 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000895 }
896
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000897 bool Done() const { return instruction_ == nullptr; }
898 HInstruction* Current() const { return instruction_; }
899 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000900 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000901 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000902 }
903
904 private:
905 HInstruction* instruction_;
906 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100907
908 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000909};
910
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100911class HBackwardInstructionIterator : public ValueObject {
912 public:
913 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
914 : instruction_(instructions.last_instruction_) {
915 next_ = Done() ? nullptr : instruction_->GetPrevious();
916 }
917
918 bool Done() const { return instruction_ == nullptr; }
919 HInstruction* Current() const { return instruction_; }
920 void Advance() {
921 instruction_ = next_;
922 next_ = Done() ? nullptr : instruction_->GetPrevious();
923 }
924
925 private:
926 HInstruction* instruction_;
927 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100928
929 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100930};
931
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000932// An embedded container with N elements of type T. Used (with partial
933// specialization for N=0) because embedded arrays cannot have size 0.
934template<typename T, intptr_t N>
935class EmbeddedArray {
936 public:
Dave Allison20dfc792014-06-16 20:44:29 -0700937 EmbeddedArray() : elements_() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000938
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000939 intptr_t GetLength() const { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000940
941 const T& operator[](intptr_t i) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000942 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000943 return elements_[i];
944 }
945
946 T& operator[](intptr_t i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000947 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000948 return elements_[i];
949 }
950
951 const T& At(intptr_t i) const {
952 return (*this)[i];
953 }
954
955 void SetAt(intptr_t i, const T& val) {
956 (*this)[i] = val;
957 }
958
959 private:
960 T elements_[N];
961};
962
963template<typename T>
964class EmbeddedArray<T, 0> {
965 public:
966 intptr_t length() const { return 0; }
967 const T& operator[](intptr_t i) const {
968 LOG(FATAL) << "Unreachable";
969 static T sentinel = 0;
970 return sentinel;
971 }
972 T& operator[](intptr_t i) {
973 LOG(FATAL) << "Unreachable";
974 static T sentinel = 0;
975 return sentinel;
976 }
977};
978
979template<intptr_t N>
980class HTemplateInstruction: public HInstruction {
981 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100982 HTemplateInstruction<N>(SideEffects side_effects)
983 : HInstruction(side_effects), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -0700984 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000985
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100986 virtual size_t InputCount() const { return N; }
987 virtual HInstruction* InputAt(size_t i) const { return inputs_[i]; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000988
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000989 protected:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100990 virtual void SetRawInputAt(size_t i, HInstruction* instruction) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000991 inputs_[i] = instruction;
992 }
993
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000994 private:
995 EmbeddedArray<HInstruction*, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100996
997 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000998};
999
Dave Allison20dfc792014-06-16 20:44:29 -07001000template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001001class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07001002 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001003 HExpression<N>(Primitive::Type type, SideEffects side_effects)
1004 : HTemplateInstruction<N>(side_effects), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07001005 virtual ~HExpression() {}
1006
1007 virtual Primitive::Type GetType() const { return type_; }
1008
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001009 protected:
1010 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07001011};
1012
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001013// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
1014// instruction that branches to the exit block.
1015class HReturnVoid : public HTemplateInstruction<0> {
1016 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001017 HReturnVoid() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001018
1019 virtual bool IsControlFlow() const { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001020
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001021 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001022
1023 private:
1024 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
1025};
1026
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001027// Represents dex's RETURN opcodes. A HReturn is a control flow
1028// instruction that branches to the exit block.
1029class HReturn : public HTemplateInstruction<1> {
1030 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001031 explicit HReturn(HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001032 SetRawInputAt(0, value);
1033 }
1034
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001035 virtual bool IsControlFlow() const { return true; }
1036
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001037 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001038
1039 private:
1040 DISALLOW_COPY_AND_ASSIGN(HReturn);
1041};
1042
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001043// The exit instruction is the only instruction of the exit block.
1044// Instructions aborting the method (HTrow and HReturn) must branch to the
1045// exit block.
1046class HExit : public HTemplateInstruction<0> {
1047 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001048 HExit() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001049
1050 virtual bool IsControlFlow() const { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001051
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001052 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001053
1054 private:
1055 DISALLOW_COPY_AND_ASSIGN(HExit);
1056};
1057
1058// Jumps from one block to another.
1059class HGoto : public HTemplateInstruction<0> {
1060 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001061 HGoto() : HTemplateInstruction(SideEffects::None()) {}
1062
1063 virtual bool IsControlFlow() const { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001064
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001065 HBasicBlock* GetSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001066 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001067 }
1068
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001069 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001070
1071 private:
1072 DISALLOW_COPY_AND_ASSIGN(HGoto);
1073};
1074
Dave Allison20dfc792014-06-16 20:44:29 -07001075
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001076// Conditional branch. A block ending with an HIf instruction must have
1077// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001078class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001079 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001080 explicit HIf(HInstruction* input) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001081 SetRawInputAt(0, input);
1082 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001083
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001084 virtual bool IsControlFlow() const { return true; }
1085
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001086 HBasicBlock* IfTrueSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001087 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001088 }
1089
1090 HBasicBlock* IfFalseSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001091 return GetBlock()->GetSuccessors().Get(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001092 }
1093
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001094 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001095
Dave Allison20dfc792014-06-16 20:44:29 -07001096 virtual bool IsIfInstruction() const { return true; }
1097
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001098 private:
1099 DISALLOW_COPY_AND_ASSIGN(HIf);
1100};
1101
Roland Levillain88cb1752014-10-20 16:36:47 +01001102class HUnaryOperation : public HExpression<1> {
1103 public:
1104 HUnaryOperation(Primitive::Type result_type, HInstruction* input)
1105 : HExpression(result_type, SideEffects::None()) {
1106 SetRawInputAt(0, input);
1107 }
1108
1109 HInstruction* GetInput() const { return InputAt(0); }
1110 Primitive::Type GetResultType() const { return GetType(); }
1111
1112 virtual bool CanBeMoved() const { return true; }
1113 virtual bool InstructionDataEquals(HInstruction* other) const { return true; }
1114
Roland Levillain9240d6a2014-10-20 16:47:04 +01001115 // Try to statically evaluate `operation` and return a HConstant
1116 // containing the result of this evaluation. If `operation` cannot
1117 // be evaluated as a constant, return nullptr.
1118 HConstant* TryStaticEvaluation() const;
1119
1120 // Apply this operation to `x`.
1121 virtual int32_t Evaluate(int32_t x) const = 0;
1122 virtual int64_t Evaluate(int64_t x) const = 0;
1123
Roland Levillain88cb1752014-10-20 16:36:47 +01001124 DECLARE_INSTRUCTION(UnaryOperation);
1125
1126 private:
1127 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
1128};
1129
Dave Allison20dfc792014-06-16 20:44:29 -07001130class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001131 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001132 HBinaryOperation(Primitive::Type result_type,
1133 HInstruction* left,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001134 HInstruction* right) : HExpression(result_type, SideEffects::None()) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001135 SetRawInputAt(0, left);
1136 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001137 }
1138
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001139 HInstruction* GetLeft() const { return InputAt(0); }
1140 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07001141 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001142
1143 virtual bool IsCommutative() { return false; }
1144
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001145 virtual bool CanBeMoved() const { return true; }
1146 virtual bool InstructionDataEquals(HInstruction* other) const { return true; }
1147
Roland Levillain9240d6a2014-10-20 16:47:04 +01001148 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01001149 // containing the result of this evaluation. If `operation` cannot
1150 // be evaluated as a constant, return nullptr.
Roland Levillain9240d6a2014-10-20 16:47:04 +01001151 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01001152
1153 // Apply this operation to `x` and `y`.
1154 virtual int32_t Evaluate(int32_t x, int32_t y) const = 0;
1155 virtual int64_t Evaluate(int64_t x, int64_t y) const = 0;
1156
Roland Levillainccc07a92014-09-16 14:48:16 +01001157 DECLARE_INSTRUCTION(BinaryOperation);
1158
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001159 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001160 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
1161};
1162
Dave Allison20dfc792014-06-16 20:44:29 -07001163class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001164 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001165 HCondition(HInstruction* first, HInstruction* second)
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001166 : HBinaryOperation(Primitive::kPrimBoolean, first, second),
1167 needs_materialization_(true) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001168
1169 virtual bool IsCommutative() { return true; }
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001170
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001171 bool NeedsMaterialization() const { return needs_materialization_; }
1172 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001173
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001174 // For code generation purposes, returns whether this instruction is just before
1175 // `if_`, and disregard moves in between.
1176 bool IsBeforeWhenDisregardMoves(HIf* if_) const;
1177
Dave Allison20dfc792014-06-16 20:44:29 -07001178 DECLARE_INSTRUCTION(Condition);
1179
1180 virtual IfCondition GetCondition() const = 0;
1181
1182 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001183 // For register allocation purposes, returns whether this instruction needs to be
1184 // materialized (that is, not just be in the processor flags).
1185 bool needs_materialization_;
1186
Dave Allison20dfc792014-06-16 20:44:29 -07001187 DISALLOW_COPY_AND_ASSIGN(HCondition);
1188};
1189
1190// Instruction to check if two inputs are equal to each other.
1191class HEqual : public HCondition {
1192 public:
1193 HEqual(HInstruction* first, HInstruction* second)
1194 : HCondition(first, second) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001195
Roland Levillain93445682014-10-06 19:24:02 +01001196 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1197 return x == y ? 1 : 0;
1198 }
1199 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1200 return x == y ? 1 : 0;
1201 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001202
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001203 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001204
Dave Allison20dfc792014-06-16 20:44:29 -07001205 virtual IfCondition GetCondition() const {
1206 return kCondEQ;
1207 }
1208
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001209 private:
1210 DISALLOW_COPY_AND_ASSIGN(HEqual);
1211};
1212
Dave Allison20dfc792014-06-16 20:44:29 -07001213class HNotEqual : public HCondition {
1214 public:
1215 HNotEqual(HInstruction* first, HInstruction* second)
1216 : HCondition(first, second) {}
1217
Roland Levillain93445682014-10-06 19:24:02 +01001218 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1219 return x != y ? 1 : 0;
1220 }
1221 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1222 return x != y ? 1 : 0;
1223 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001224
Dave Allison20dfc792014-06-16 20:44:29 -07001225 DECLARE_INSTRUCTION(NotEqual);
1226
1227 virtual IfCondition GetCondition() const {
1228 return kCondNE;
1229 }
1230
1231 private:
1232 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
1233};
1234
1235class HLessThan : public HCondition {
1236 public:
1237 HLessThan(HInstruction* first, HInstruction* second)
1238 : HCondition(first, second) {}
1239
Roland Levillain93445682014-10-06 19:24:02 +01001240 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1241 return x < y ? 1 : 0;
1242 }
1243 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1244 return x < y ? 1 : 0;
1245 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001246
Dave Allison20dfc792014-06-16 20:44:29 -07001247 DECLARE_INSTRUCTION(LessThan);
1248
1249 virtual IfCondition GetCondition() const {
1250 return kCondLT;
1251 }
1252
1253 private:
1254 DISALLOW_COPY_AND_ASSIGN(HLessThan);
1255};
1256
1257class HLessThanOrEqual : public HCondition {
1258 public:
1259 HLessThanOrEqual(HInstruction* first, HInstruction* second)
1260 : HCondition(first, second) {}
1261
Roland Levillain93445682014-10-06 19:24:02 +01001262 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1263 return x <= y ? 1 : 0;
1264 }
1265 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1266 return x <= y ? 1 : 0;
1267 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001268
Dave Allison20dfc792014-06-16 20:44:29 -07001269 DECLARE_INSTRUCTION(LessThanOrEqual);
1270
1271 virtual IfCondition GetCondition() const {
1272 return kCondLE;
1273 }
1274
1275 private:
1276 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
1277};
1278
1279class HGreaterThan : public HCondition {
1280 public:
1281 HGreaterThan(HInstruction* first, HInstruction* second)
1282 : HCondition(first, second) {}
1283
Roland Levillain93445682014-10-06 19:24:02 +01001284 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1285 return x > y ? 1 : 0;
1286 }
1287 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1288 return x > y ? 1 : 0;
1289 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001290
Dave Allison20dfc792014-06-16 20:44:29 -07001291 DECLARE_INSTRUCTION(GreaterThan);
1292
1293 virtual IfCondition GetCondition() const {
1294 return kCondGT;
1295 }
1296
1297 private:
1298 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
1299};
1300
1301class HGreaterThanOrEqual : public HCondition {
1302 public:
1303 HGreaterThanOrEqual(HInstruction* first, HInstruction* second)
1304 : HCondition(first, second) {}
1305
Roland Levillain93445682014-10-06 19:24:02 +01001306 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1307 return x >= y ? 1 : 0;
1308 }
1309 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1310 return x >= y ? 1 : 0;
1311 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001312
Dave Allison20dfc792014-06-16 20:44:29 -07001313 DECLARE_INSTRUCTION(GreaterThanOrEqual);
1314
1315 virtual IfCondition GetCondition() const {
1316 return kCondGE;
1317 }
1318
1319 private:
1320 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
1321};
1322
1323
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001324// Instruction to check how two inputs compare to each other.
1325// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
1326class HCompare : public HBinaryOperation {
1327 public:
1328 HCompare(Primitive::Type type, HInstruction* first, HInstruction* second)
1329 : HBinaryOperation(Primitive::kPrimInt, first, second) {
1330 DCHECK_EQ(type, first->GetType());
1331 DCHECK_EQ(type, second->GetType());
1332 }
1333
Roland Levillain93445682014-10-06 19:24:02 +01001334 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001335 return
1336 x == y ? 0 :
1337 x > y ? 1 :
1338 -1;
1339 }
Roland Levillain93445682014-10-06 19:24:02 +01001340 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001341 return
1342 x == y ? 0 :
1343 x > y ? 1 :
1344 -1;
1345 }
1346
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001347 DECLARE_INSTRUCTION(Compare);
1348
1349 private:
1350 DISALLOW_COPY_AND_ASSIGN(HCompare);
1351};
1352
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001353// A local in the graph. Corresponds to a Dex register.
1354class HLocal : public HTemplateInstruction<0> {
1355 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001356 explicit HLocal(uint16_t reg_number)
1357 : HTemplateInstruction(SideEffects::None()), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001358
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001359 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001360
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001361 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001362
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001363 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001364 // The Dex register number.
1365 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001366
1367 DISALLOW_COPY_AND_ASSIGN(HLocal);
1368};
1369
1370// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07001371class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001372 public:
Roland Levillain5799fc02014-09-25 12:15:20 +01001373 HLoadLocal(HLocal* local, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001374 : HExpression(type, SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001375 SetRawInputAt(0, local);
1376 }
1377
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001378 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1379
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001380 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001381
1382 private:
1383 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
1384};
1385
1386// Store a value in a given local. This instruction has two inputs: the value
1387// and the local.
1388class HStoreLocal : public HTemplateInstruction<2> {
1389 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001390 HStoreLocal(HLocal* local, HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001391 SetRawInputAt(0, local);
1392 SetRawInputAt(1, value);
1393 }
1394
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001395 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1396
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001397 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001398
1399 private:
1400 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
1401};
1402
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001403class HConstant : public HExpression<0> {
1404 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001405 explicit HConstant(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
1406
1407 virtual bool CanBeMoved() const { return true; }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001408
1409 DECLARE_INSTRUCTION(Constant);
1410
1411 private:
1412 DISALLOW_COPY_AND_ASSIGN(HConstant);
1413};
1414
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001415class HFloatConstant : public HConstant {
1416 public:
1417 explicit HFloatConstant(float value) : HConstant(Primitive::kPrimFloat), value_(value) {}
1418
1419 float GetValue() const { return value_; }
1420
1421 virtual bool InstructionDataEquals(HInstruction* other) const {
1422 return bit_cast<float, int32_t>(other->AsFloatConstant()->value_) ==
1423 bit_cast<float, int32_t>(value_);
1424 }
1425
1426 virtual size_t ComputeHashCode() const { return static_cast<size_t>(GetValue()); }
1427
1428 DECLARE_INSTRUCTION(FloatConstant);
1429
1430 private:
1431 const float value_;
1432
1433 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
1434};
1435
1436class HDoubleConstant : public HConstant {
1437 public:
1438 explicit HDoubleConstant(double value) : HConstant(Primitive::kPrimDouble), value_(value) {}
1439
1440 double GetValue() const { return value_; }
1441
1442 virtual bool InstructionDataEquals(HInstruction* other) const {
1443 return bit_cast<double, int64_t>(other->AsDoubleConstant()->value_) ==
1444 bit_cast<double, int64_t>(value_);
1445 }
1446
1447 virtual size_t ComputeHashCode() const { return static_cast<size_t>(GetValue()); }
1448
1449 DECLARE_INSTRUCTION(DoubleConstant);
1450
1451 private:
1452 const double value_;
1453
1454 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
1455};
1456
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001457// Constants of the type int. Those can be from Dex instructions, or
1458// synthesized (for example with the if-eqz instruction).
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001459class HIntConstant : public HConstant {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001460 public:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001461 explicit HIntConstant(int32_t value) : HConstant(Primitive::kPrimInt), value_(value) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001462
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001463 int32_t GetValue() const { return value_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001464
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001465 virtual bool InstructionDataEquals(HInstruction* other) const {
1466 return other->AsIntConstant()->value_ == value_;
1467 }
1468
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001469 virtual size_t ComputeHashCode() const { return GetValue(); }
1470
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001471 DECLARE_INSTRUCTION(IntConstant);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001472
1473 private:
1474 const int32_t value_;
1475
1476 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
1477};
1478
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001479class HLongConstant : public HConstant {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001480 public:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001481 explicit HLongConstant(int64_t value) : HConstant(Primitive::kPrimLong), value_(value) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001482
1483 int64_t GetValue() const { return value_; }
1484
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001485 virtual bool InstructionDataEquals(HInstruction* other) const {
1486 return other->AsLongConstant()->value_ == value_;
1487 }
1488
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001489 virtual size_t ComputeHashCode() const { return static_cast<size_t>(GetValue()); }
1490
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001491 DECLARE_INSTRUCTION(LongConstant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001492
1493 private:
1494 const int64_t value_;
1495
1496 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
1497};
1498
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001499class HInvoke : public HInstruction {
1500 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001501 HInvoke(ArenaAllocator* arena,
1502 uint32_t number_of_arguments,
1503 Primitive::Type return_type,
1504 uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001505 : HInstruction(SideEffects::All()),
1506 inputs_(arena, number_of_arguments),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001507 return_type_(return_type),
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001508 dex_pc_(dex_pc) {
1509 inputs_.SetSize(number_of_arguments);
1510 }
1511
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001512 virtual size_t InputCount() const { return inputs_.Size(); }
1513 virtual HInstruction* InputAt(size_t i) const { return inputs_.Get(i); }
1514
1515 // Runtime needs to walk the stack, so Dex -> Dex calls need to
1516 // know their environment.
1517 virtual bool NeedsEnvironment() const { return true; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001518
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001519 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001520 SetRawInputAt(index, argument);
1521 }
1522
1523 virtual void SetRawInputAt(size_t index, HInstruction* input) {
1524 inputs_.Put(index, input);
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001525 }
1526
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001527 virtual Primitive::Type GetType() const { return return_type_; }
1528
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001529 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001530
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001531 DECLARE_INSTRUCTION(Invoke);
1532
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001533 protected:
1534 GrowableArray<HInstruction*> inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001535 const Primitive::Type return_type_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001536 const uint32_t dex_pc_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001537
1538 private:
1539 DISALLOW_COPY_AND_ASSIGN(HInvoke);
1540};
1541
1542class HInvokeStatic : public HInvoke {
1543 public:
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001544 HInvokeStatic(ArenaAllocator* arena,
1545 uint32_t number_of_arguments,
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001546 Primitive::Type return_type,
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001547 uint32_t dex_pc,
1548 uint32_t index_in_dex_cache)
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001549 : HInvoke(arena, number_of_arguments, return_type, dex_pc),
1550 index_in_dex_cache_(index_in_dex_cache) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001551
1552 uint32_t GetIndexInDexCache() const { return index_in_dex_cache_; }
1553
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001554 DECLARE_INSTRUCTION(InvokeStatic);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001555
1556 private:
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001557 const uint32_t index_in_dex_cache_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001558
1559 DISALLOW_COPY_AND_ASSIGN(HInvokeStatic);
1560};
1561
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001562class HInvokeVirtual : public HInvoke {
1563 public:
1564 HInvokeVirtual(ArenaAllocator* arena,
1565 uint32_t number_of_arguments,
1566 Primitive::Type return_type,
1567 uint32_t dex_pc,
1568 uint32_t vtable_index)
1569 : HInvoke(arena, number_of_arguments, return_type, dex_pc),
1570 vtable_index_(vtable_index) {}
1571
1572 uint32_t GetVTableIndex() const { return vtable_index_; }
1573
1574 DECLARE_INSTRUCTION(InvokeVirtual);
1575
1576 private:
1577 const uint32_t vtable_index_;
1578
1579 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
1580};
1581
Dave Allison20dfc792014-06-16 20:44:29 -07001582class HNewInstance : public HExpression<0> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001583 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001584 HNewInstance(uint32_t dex_pc, uint16_t type_index)
1585 : HExpression(Primitive::kPrimNot, SideEffects::None()),
1586 dex_pc_(dex_pc),
1587 type_index_(type_index) {}
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001588
1589 uint32_t GetDexPc() const { return dex_pc_; }
1590 uint16_t GetTypeIndex() const { return type_index_; }
1591
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001592 // Calls runtime so needs an environment.
1593 virtual bool NeedsEnvironment() const { return true; }
1594
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001595 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001596
1597 private:
1598 const uint32_t dex_pc_;
1599 const uint16_t type_index_;
1600
1601 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
1602};
1603
Roland Levillain88cb1752014-10-20 16:36:47 +01001604class HNeg : public HUnaryOperation {
1605 public:
1606 explicit HNeg(Primitive::Type result_type, HInstruction* input)
1607 : HUnaryOperation(result_type, input) {}
1608
Roland Levillain9240d6a2014-10-20 16:47:04 +01001609 virtual int32_t Evaluate(int32_t x) const OVERRIDE { return -x; }
1610 virtual int64_t Evaluate(int64_t x) const OVERRIDE { return -x; }
1611
Roland Levillain88cb1752014-10-20 16:36:47 +01001612 DECLARE_INSTRUCTION(Neg);
1613
1614 private:
1615 DISALLOW_COPY_AND_ASSIGN(HNeg);
1616};
1617
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001618class HNewArray : public HExpression<1> {
1619 public:
1620 HNewArray(HInstruction* length, uint32_t dex_pc, uint16_t type_index)
1621 : HExpression(Primitive::kPrimNot, SideEffects::None()),
1622 dex_pc_(dex_pc),
1623 type_index_(type_index) {
1624 SetRawInputAt(0, length);
1625 }
1626
1627 uint32_t GetDexPc() const { return dex_pc_; }
1628 uint16_t GetTypeIndex() const { return type_index_; }
1629
1630 // Calls runtime so needs an environment.
1631 virtual bool NeedsEnvironment() const { return true; }
1632
1633 DECLARE_INSTRUCTION(NewArray);
1634
1635 private:
1636 const uint32_t dex_pc_;
1637 const uint16_t type_index_;
1638
1639 DISALLOW_COPY_AND_ASSIGN(HNewArray);
1640};
1641
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001642class HAdd : public HBinaryOperation {
1643 public:
1644 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1645 : HBinaryOperation(result_type, left, right) {}
1646
1647 virtual bool IsCommutative() { return true; }
1648
Roland Levillain93445682014-10-06 19:24:02 +01001649 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1650 return x + y;
1651 }
1652 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1653 return x + y;
1654 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001655
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001656 DECLARE_INSTRUCTION(Add);
1657
1658 private:
1659 DISALLOW_COPY_AND_ASSIGN(HAdd);
1660};
1661
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001662class HSub : public HBinaryOperation {
1663 public:
1664 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1665 : HBinaryOperation(result_type, left, right) {}
1666
Roland Levillain93445682014-10-06 19:24:02 +01001667 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1668 return x - y;
1669 }
1670 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1671 return x - y;
1672 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001673
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001674 DECLARE_INSTRUCTION(Sub);
1675
1676 private:
1677 DISALLOW_COPY_AND_ASSIGN(HSub);
1678};
1679
Calin Juravle34bacdf2014-10-07 20:23:36 +01001680class HMul : public HBinaryOperation {
1681 public:
1682 HMul(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1683 : HBinaryOperation(result_type, left, right) {}
1684
1685 virtual bool IsCommutative() { return true; }
1686
1687 virtual int32_t Evaluate(int32_t x, int32_t y) const { return x * y; }
1688 virtual int64_t Evaluate(int64_t x, int64_t y) const { return x * y; }
1689
1690 DECLARE_INSTRUCTION(Mul);
1691
1692 private:
1693 DISALLOW_COPY_AND_ASSIGN(HMul);
1694};
1695
Calin Juravle7c4954d2014-10-28 16:57:40 +00001696class HDiv : public HBinaryOperation {
1697 public:
1698 HDiv(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1699 : HBinaryOperation(result_type, left, right) {}
1700
1701 virtual int32_t Evaluate(int32_t x, int32_t y) const { return x / y; }
1702 virtual int64_t Evaluate(int64_t x, int64_t y) const { return x / y; }
1703
1704 DECLARE_INSTRUCTION(Div);
1705
1706 private:
1707 DISALLOW_COPY_AND_ASSIGN(HDiv);
1708};
1709
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001710// The value of a parameter in this method. Its location depends on
1711// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07001712class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001713 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001714 HParameterValue(uint8_t index, Primitive::Type parameter_type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001715 : HExpression(parameter_type, SideEffects::None()), index_(index) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001716
1717 uint8_t GetIndex() const { return index_; }
1718
1719 DECLARE_INSTRUCTION(ParameterValue);
1720
1721 private:
1722 // The index of this parameter in the parameters list. Must be less
1723 // than HGraph::number_of_in_vregs_;
1724 const uint8_t index_;
1725
1726 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
1727};
1728
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001729class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001730 public:
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001731 explicit HNot(Primitive::Type result_type, HInstruction* input)
1732 : HUnaryOperation(result_type, input) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001733
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001734 virtual bool CanBeMoved() const { return true; }
1735 virtual bool InstructionDataEquals(HInstruction* other) const { return true; }
1736
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001737 virtual int32_t Evaluate(int32_t x) const OVERRIDE { return ~x; }
1738 virtual int64_t Evaluate(int64_t x) const OVERRIDE { return ~x; }
1739
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001740 DECLARE_INSTRUCTION(Not);
1741
1742 private:
1743 DISALLOW_COPY_AND_ASSIGN(HNot);
1744};
1745
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001746class HPhi : public HInstruction {
1747 public:
1748 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001749 : HInstruction(SideEffects::None()),
1750 inputs_(arena, number_of_inputs),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001751 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001752 type_(type),
1753 is_live_(false) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001754 inputs_.SetSize(number_of_inputs);
1755 }
1756
1757 virtual size_t InputCount() const { return inputs_.Size(); }
1758 virtual HInstruction* InputAt(size_t i) const { return inputs_.Get(i); }
1759
1760 virtual void SetRawInputAt(size_t index, HInstruction* input) {
1761 inputs_.Put(index, input);
1762 }
1763
1764 void AddInput(HInstruction* input);
1765
1766 virtual Primitive::Type GetType() const { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001767 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001768
1769 uint32_t GetRegNumber() const { return reg_number_; }
1770
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001771 void SetDead() { is_live_ = false; }
1772 void SetLive() { is_live_ = true; }
1773 bool IsDead() const { return !is_live_; }
1774 bool IsLive() const { return is_live_; }
1775
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001776 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001777
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001778 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001779 GrowableArray<HInstruction*> inputs_;
1780 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001781 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001782 bool is_live_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001783
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001784 DISALLOW_COPY_AND_ASSIGN(HPhi);
1785};
1786
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001787class HNullCheck : public HExpression<1> {
1788 public:
1789 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001790 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001791 SetRawInputAt(0, value);
1792 }
1793
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001794 virtual bool CanBeMoved() const { return true; }
1795 virtual bool InstructionDataEquals(HInstruction* other) const { return true; }
1796
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001797 virtual bool NeedsEnvironment() const { return true; }
1798
Roland Levillaine161a2a2014-10-03 12:45:18 +01001799 virtual bool CanThrow() const { return true; }
1800
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001801 uint32_t GetDexPc() const { return dex_pc_; }
1802
1803 DECLARE_INSTRUCTION(NullCheck);
1804
1805 private:
1806 const uint32_t dex_pc_;
1807
1808 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
1809};
1810
1811class FieldInfo : public ValueObject {
1812 public:
Roland Levillain5799fc02014-09-25 12:15:20 +01001813 FieldInfo(MemberOffset field_offset, Primitive::Type field_type)
Nicolas Geoffray39468442014-09-02 15:17:15 +01001814 : field_offset_(field_offset), field_type_(field_type) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001815
1816 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001817 Primitive::Type GetFieldType() const { return field_type_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001818
1819 private:
1820 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01001821 const Primitive::Type field_type_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001822};
1823
1824class HInstanceFieldGet : public HExpression<1> {
1825 public:
1826 HInstanceFieldGet(HInstruction* value,
1827 Primitive::Type field_type,
1828 MemberOffset field_offset)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001829 : HExpression(field_type, SideEffects::DependsOnSomething()),
1830 field_info_(field_offset, field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001831 SetRawInputAt(0, value);
1832 }
1833
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001834 virtual bool CanBeMoved() const { return true; }
1835 virtual bool InstructionDataEquals(HInstruction* other) const {
1836 size_t other_offset = other->AsInstanceFieldGet()->GetFieldOffset().SizeValue();
1837 return other_offset == GetFieldOffset().SizeValue();
1838 }
1839
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001840 virtual size_t ComputeHashCode() const {
1841 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
1842 }
1843
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001844 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001845 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001846
1847 DECLARE_INSTRUCTION(InstanceFieldGet);
1848
1849 private:
1850 const FieldInfo field_info_;
1851
1852 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
1853};
1854
1855class HInstanceFieldSet : public HTemplateInstruction<2> {
1856 public:
1857 HInstanceFieldSet(HInstruction* object,
1858 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01001859 Primitive::Type field_type,
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001860 MemberOffset field_offset)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001861 : HTemplateInstruction(SideEffects::ChangesSomething()),
1862 field_info_(field_offset, field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001863 SetRawInputAt(0, object);
1864 SetRawInputAt(1, value);
1865 }
1866
1867 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001868 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001869
1870 DECLARE_INSTRUCTION(InstanceFieldSet);
1871
1872 private:
1873 const FieldInfo field_info_;
1874
1875 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
1876};
1877
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001878class HArrayGet : public HExpression<2> {
1879 public:
1880 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001881 : HExpression(type, SideEffects::DependsOnSomething()) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001882 SetRawInputAt(0, array);
1883 SetRawInputAt(1, index);
1884 }
1885
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001886 virtual bool CanBeMoved() const { return true; }
1887 virtual bool InstructionDataEquals(HInstruction* other) const { return true; }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001888 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001889
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001890 DECLARE_INSTRUCTION(ArrayGet);
1891
1892 private:
1893 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
1894};
1895
1896class HArraySet : public HTemplateInstruction<3> {
1897 public:
1898 HArraySet(HInstruction* array,
1899 HInstruction* index,
1900 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001901 Primitive::Type expected_component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001902 uint32_t dex_pc)
1903 : HTemplateInstruction(SideEffects::ChangesSomething()),
1904 dex_pc_(dex_pc),
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001905 expected_component_type_(expected_component_type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001906 SetRawInputAt(0, array);
1907 SetRawInputAt(1, index);
1908 SetRawInputAt(2, value);
1909 }
1910
1911 virtual bool NeedsEnvironment() const {
1912 // We currently always call a runtime method to catch array store
1913 // exceptions.
1914 return InputAt(2)->GetType() == Primitive::kPrimNot;
1915 }
1916
1917 uint32_t GetDexPc() const { return dex_pc_; }
1918
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001919 HInstruction* GetValue() const { return InputAt(2); }
1920
1921 Primitive::Type GetComponentType() const {
1922 // The Dex format does not type floating point index operations. Since the
1923 // `expected_component_type_` is set during building and can therefore not
1924 // be correct, we also check what is the value type. If it is a floating
1925 // point type, we must use that type.
1926 Primitive::Type value_type = GetValue()->GetType();
1927 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
1928 ? value_type
1929 : expected_component_type_;
1930 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001931
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001932 DECLARE_INSTRUCTION(ArraySet);
1933
1934 private:
1935 const uint32_t dex_pc_;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001936 const Primitive::Type expected_component_type_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001937
1938 DISALLOW_COPY_AND_ASSIGN(HArraySet);
1939};
1940
1941class HArrayLength : public HExpression<1> {
1942 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001943 explicit HArrayLength(HInstruction* array)
1944 : HExpression(Primitive::kPrimInt, SideEffects::None()) {
1945 // Note that arrays do not change length, so the instruction does not
1946 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001947 SetRawInputAt(0, array);
1948 }
1949
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001950 virtual bool CanBeMoved() const { return true; }
1951 virtual bool InstructionDataEquals(HInstruction* other) const { return true; }
1952
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001953 DECLARE_INSTRUCTION(ArrayLength);
1954
1955 private:
1956 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
1957};
1958
1959class HBoundsCheck : public HExpression<2> {
1960 public:
1961 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001962 : HExpression(index->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001963 DCHECK(index->GetType() == Primitive::kPrimInt);
1964 SetRawInputAt(0, index);
1965 SetRawInputAt(1, length);
1966 }
1967
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001968 virtual bool CanBeMoved() const { return true; }
1969 virtual bool InstructionDataEquals(HInstruction* other) const { return true; }
1970
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001971 virtual bool NeedsEnvironment() const { return true; }
1972
Roland Levillaine161a2a2014-10-03 12:45:18 +01001973 virtual bool CanThrow() const { return true; }
1974
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01001975 uint32_t GetDexPc() const { return dex_pc_; }
1976
1977 DECLARE_INSTRUCTION(BoundsCheck);
1978
1979 private:
1980 const uint32_t dex_pc_;
1981
1982 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
1983};
1984
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001985/**
1986 * Some DEX instructions are folded into multiple HInstructions that need
1987 * to stay live until the last HInstruction. This class
1988 * is used as a marker for the baseline compiler to ensure its preceding
1989 * HInstruction stays live. `index` is the temporary number that is used
1990 * for knowing the stack offset where to store the instruction.
1991 */
1992class HTemporary : public HTemplateInstruction<0> {
1993 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001994 explicit HTemporary(size_t index) : HTemplateInstruction(SideEffects::None()), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001995
1996 size_t GetIndex() const { return index_; }
1997
1998 DECLARE_INSTRUCTION(Temporary);
1999
2000 private:
2001 const size_t index_;
2002
2003 DISALLOW_COPY_AND_ASSIGN(HTemporary);
2004};
2005
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002006class HSuspendCheck : public HTemplateInstruction<0> {
2007 public:
2008 explicit HSuspendCheck(uint32_t dex_pc)
Nicolas Geoffray9ebc72c2014-09-25 16:33:42 +01002009 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002010
2011 virtual bool NeedsEnvironment() const {
2012 return true;
2013 }
2014
2015 uint32_t GetDexPc() const { return dex_pc_; }
2016
2017 DECLARE_INSTRUCTION(SuspendCheck);
2018
2019 private:
2020 const uint32_t dex_pc_;
2021
2022 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
2023};
2024
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002025// TODO: Make this class handle the case the load is null (dex cache
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002026// is null). This will be required when using it for other things than
2027// initialization check.
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002028/**
2029 * Instruction to load a Class object.
2030 */
2031class HLoadClass : public HExpression<0> {
2032 public:
2033 HLoadClass(uint16_t type_index,
2034 bool is_referrers_class,
2035 bool is_initialized,
2036 uint32_t dex_pc)
2037 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2038 type_index_(type_index),
2039 is_referrers_class_(is_referrers_class),
2040 is_initialized_(is_initialized),
2041 dex_pc_(dex_pc) {}
2042
2043 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2044 return other->AsLoadClass()->type_index_ == type_index_;
2045 }
2046
2047 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
2048
2049 uint32_t GetDexPc() const { return dex_pc_; }
2050 uint16_t GetTypeIndex() const { return type_index_; }
2051
2052 bool NeedsInitialization() const {
2053 return !is_initialized_ && !is_referrers_class_;
2054 }
2055
2056 bool IsReferrersClass() const { return is_referrers_class_; }
2057
2058 DECLARE_INSTRUCTION(LoadClass);
2059
2060 private:
2061 const uint16_t type_index_;
2062 const bool is_referrers_class_;
2063 const bool is_initialized_;
2064 const uint32_t dex_pc_;
2065
2066 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
2067};
2068
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002069class HLoadString : public HExpression<0> {
2070 public:
2071 HLoadString(uint32_t string_index, uint32_t dex_pc)
2072 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2073 string_index_(string_index),
2074 dex_pc_(dex_pc) {}
2075
2076 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2077 return other->AsLoadString()->string_index_ == string_index_;
2078 }
2079
2080 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
2081
2082 uint32_t GetDexPc() const { return dex_pc_; }
2083 uint32_t GetStringIndex() const { return string_index_; }
2084
2085 // TODO: Can we deopt or debug when we resolve a string?
2086 bool NeedsEnvironment() const OVERRIDE { return false; }
2087
2088 DECLARE_INSTRUCTION(LoadString);
2089
2090 private:
2091 const uint32_t string_index_;
2092 const uint32_t dex_pc_;
2093
2094 DISALLOW_COPY_AND_ASSIGN(HLoadString);
2095};
2096
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002097// TODO: Pass this check to HInvokeStatic nodes.
2098/**
2099 * Performs an initialization check on its Class object input.
2100 */
2101class HClinitCheck : public HExpression<1> {
2102 public:
2103 explicit HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
2104 : HExpression(Primitive::kPrimNot, SideEffects::All()),
2105 dex_pc_(dex_pc) {
2106 SetRawInputAt(0, constant);
2107 }
2108
2109 bool NeedsEnvironment() const OVERRIDE {
2110 // May call runtime to initialize the class.
2111 return true;
2112 }
2113
2114 uint32_t GetDexPc() const { return dex_pc_; }
2115
2116 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
2117
2118 DECLARE_INSTRUCTION(ClinitCheck);
2119
2120 private:
2121 const uint32_t dex_pc_;
2122
2123 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
2124};
2125
2126class HStaticFieldGet : public HExpression<1> {
2127 public:
2128 HStaticFieldGet(HInstruction* cls,
2129 Primitive::Type field_type,
2130 MemberOffset field_offset)
2131 : HExpression(field_type, SideEffects::DependsOnSomething()),
2132 field_info_(field_offset, field_type) {
2133 SetRawInputAt(0, cls);
2134 }
2135
2136 bool CanBeMoved() const OVERRIDE { return true; }
2137 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2138 size_t other_offset = other->AsStaticFieldGet()->GetFieldOffset().SizeValue();
2139 return other_offset == GetFieldOffset().SizeValue();
2140 }
2141
2142 size_t ComputeHashCode() const OVERRIDE {
2143 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
2144 }
2145
2146 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
2147 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
2148
2149 DECLARE_INSTRUCTION(StaticFieldGet);
2150
2151 private:
2152 const FieldInfo field_info_;
2153
2154 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
2155};
2156
2157class HStaticFieldSet : public HTemplateInstruction<2> {
2158 public:
2159 HStaticFieldSet(HInstruction* cls,
2160 HInstruction* value,
2161 Primitive::Type field_type,
2162 MemberOffset field_offset)
2163 : HTemplateInstruction(SideEffects::ChangesSomething()),
2164 field_info_(field_offset, field_type) {
2165 SetRawInputAt(0, cls);
2166 SetRawInputAt(1, value);
2167 }
2168
2169 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
2170 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
2171
2172 DECLARE_INSTRUCTION(StaticFieldSet);
2173
2174 private:
2175 const FieldInfo field_info_;
2176
2177 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
2178};
2179
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002180class MoveOperands : public ArenaObject {
2181 public:
Nicolas Geoffray740475d2014-09-29 10:33:25 +01002182 MoveOperands(Location source, Location destination, HInstruction* instruction)
2183 : source_(source), destination_(destination), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002184
2185 Location GetSource() const { return source_; }
2186 Location GetDestination() const { return destination_; }
2187
2188 void SetSource(Location value) { source_ = value; }
2189 void SetDestination(Location value) { destination_ = value; }
2190
2191 // The parallel move resolver marks moves as "in-progress" by clearing the
2192 // destination (but not the source).
2193 Location MarkPending() {
2194 DCHECK(!IsPending());
2195 Location dest = destination_;
2196 destination_ = Location::NoLocation();
2197 return dest;
2198 }
2199
2200 void ClearPending(Location dest) {
2201 DCHECK(IsPending());
2202 destination_ = dest;
2203 }
2204
2205 bool IsPending() const {
2206 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
2207 return destination_.IsInvalid() && !source_.IsInvalid();
2208 }
2209
2210 // True if this blocks a move from the given location.
2211 bool Blocks(Location loc) const {
2212 return !IsEliminated() && source_.Equals(loc);
2213 }
2214
2215 // A move is redundant if it's been eliminated, if its source and
2216 // destination are the same, or if its destination is unneeded.
2217 bool IsRedundant() const {
2218 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
2219 }
2220
2221 // We clear both operands to indicate move that's been eliminated.
2222 void Eliminate() {
2223 source_ = destination_ = Location::NoLocation();
2224 }
2225
2226 bool IsEliminated() const {
2227 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
2228 return source_.IsInvalid();
2229 }
2230
Nicolas Geoffray740475d2014-09-29 10:33:25 +01002231 HInstruction* GetInstruction() const { return instruction_; }
2232
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002233 private:
2234 Location source_;
2235 Location destination_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01002236 // The instruction this move is assocatied with. Null when this move is
2237 // for moving an input in the expected locations of user (including a phi user).
2238 // This is only used in debug mode, to ensure we do not connect interval siblings
2239 // in the same parallel move.
2240 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002241
2242 DISALLOW_COPY_AND_ASSIGN(MoveOperands);
2243};
2244
2245static constexpr size_t kDefaultNumberOfMoves = 4;
2246
2247class HParallelMove : public HTemplateInstruction<0> {
2248 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002249 explicit HParallelMove(ArenaAllocator* arena)
2250 : HTemplateInstruction(SideEffects::None()), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002251
2252 void AddMove(MoveOperands* move) {
Nicolas Geoffray740475d2014-09-29 10:33:25 +01002253 if (kIsDebugBuild && move->GetInstruction() != nullptr) {
2254 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
2255 DCHECK_NE(moves_.Get(i)->GetInstruction(), move->GetInstruction())
2256 << "Doing parallel moves for the same instruction.";
2257 }
2258 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002259 moves_.Add(move);
2260 }
2261
2262 MoveOperands* MoveOperandsAt(size_t index) const {
2263 return moves_.Get(index);
2264 }
2265
2266 size_t NumMoves() const { return moves_.Size(); }
2267
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002268 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002269
2270 private:
2271 GrowableArray<MoveOperands*> moves_;
2272
2273 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
2274};
2275
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002276class HGraphVisitor : public ValueObject {
2277 public:
Dave Allison20dfc792014-06-16 20:44:29 -07002278 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
2279 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002280
Dave Allison20dfc792014-06-16 20:44:29 -07002281 virtual void VisitInstruction(HInstruction* instruction) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002282 virtual void VisitBasicBlock(HBasicBlock* block);
2283
Roland Levillain633021e2014-10-01 14:12:25 +01002284 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002285 void VisitInsertionOrder();
2286
Roland Levillain633021e2014-10-01 14:12:25 +01002287 // Visit the graph following dominator tree reverse post-order.
2288 void VisitReversePostOrder();
2289
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002290 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002291
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002292 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002293#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002294 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
2295
2296 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
2297
2298#undef DECLARE_VISIT_INSTRUCTION
2299
2300 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07002301 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002302
2303 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
2304};
2305
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002306class HGraphDelegateVisitor : public HGraphVisitor {
2307 public:
2308 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
2309 virtual ~HGraphDelegateVisitor() {}
2310
2311 // Visit functions that delegate to to super class.
2312#define DECLARE_VISIT_INSTRUCTION(name, super) \
2313 virtual void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
2314
2315 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
2316
2317#undef DECLARE_VISIT_INSTRUCTION
2318
2319 private:
2320 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
2321};
2322
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002323class HInsertionOrderIterator : public ValueObject {
2324 public:
2325 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
2326
2327 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
2328 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
2329 void Advance() { ++index_; }
2330
2331 private:
2332 const HGraph& graph_;
2333 size_t index_;
2334
2335 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
2336};
2337
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002338class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002339 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002340 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002341
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002342 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
2343 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002344 void Advance() { ++index_; }
2345
2346 private:
2347 const HGraph& graph_;
2348 size_t index_;
2349
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002350 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002351};
2352
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002353class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002354 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002355 explicit HPostOrderIterator(const HGraph& graph)
2356 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002357
2358 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002359 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002360 void Advance() { --index_; }
2361
2362 private:
2363 const HGraph& graph_;
2364 size_t index_;
2365
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002366 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002367};
2368
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002369} // namespace art
2370
2371#endif // ART_COMPILER_OPTIMIZING_NODES_H_