blob: 8a25de19d9ddf9dda243438f0adc1836e38f0e0e [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
Calin Juravle9aec02f2014-11-18 23:06:35 +000045static constexpr uint32_t kMaxIntShiftValue = 0x1f;
46static constexpr uint64_t kMaxLongShiftValue = 0x3f;
47
Dave Allison20dfc792014-06-16 20:44:29 -070048enum IfCondition {
49 kCondEQ,
50 kCondNE,
51 kCondLT,
52 kCondLE,
53 kCondGT,
54 kCondGE,
55};
56
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010057class HInstructionList {
58 public:
59 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
60
61 void AddInstruction(HInstruction* instruction);
62 void RemoveInstruction(HInstruction* instruction);
63
Roland Levillain6b469232014-09-25 10:10:38 +010064 // Return true if this list contains `instruction`.
65 bool Contains(HInstruction* instruction) const;
66
Roland Levillainccc07a92014-09-16 14:48:16 +010067 // Return true if `instruction1` is found before `instruction2` in
68 // this instruction list and false otherwise. Abort if none
69 // of these instructions is found.
70 bool FoundBefore(const HInstruction* instruction1,
71 const HInstruction* instruction2) const;
72
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010073 private:
74 HInstruction* first_instruction_;
75 HInstruction* last_instruction_;
76
77 friend class HBasicBlock;
78 friend class HInstructionIterator;
Nicolas Geoffray804d0932014-05-02 08:46:00 +010079 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010080
81 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
82};
83
Nicolas Geoffray818f2102014-02-18 16:43:35 +000084// Control-flow graph of a method. Contains a list of basic blocks.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070085class HGraph : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +000086 public:
87 explicit HGraph(ArenaAllocator* arena)
88 : arena_(arena),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000089 blocks_(arena, kDefaultNumberOfBlocks),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +010090 reverse_post_order_(arena, kDefaultNumberOfBlocks),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070091 entry_block_(nullptr),
92 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +010093 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +010094 number_of_vregs_(0),
95 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +000096 temporaries_vreg_slots_(0),
Dave Allison20dfc792014-06-16 20:44:29 -070097 current_instruction_id_(0) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +000098
Nicolas Geoffray787c3072014-03-17 10:20:19 +000099 ArenaAllocator* GetArena() const { return arena_; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100100 const GrowableArray<HBasicBlock*>& GetBlocks() const { return blocks_; }
Roland Levillain93445682014-10-06 19:24:02 +0100101 HBasicBlock* GetBlock(size_t id) const { return blocks_.Get(id); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000102
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000103 HBasicBlock* GetEntryBlock() const { return entry_block_; }
104 HBasicBlock* GetExitBlock() const { return exit_block_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000105
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000106 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
107 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000108
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000109 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100110
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000111 void BuildDominatorTree();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100112 void TransformToSSA();
113 void SimplifyCFG();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000114
Nicolas Geoffrayf5370122014-12-02 11:51:19 +0000115 // Analyze all natural loops in this graph. Returns false if one
116 // loop is not natural, that is the header does not dominate the
117 // back edge.
118 bool AnalyzeNaturalLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100119
120 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
121 void SimplifyLoop(HBasicBlock* header);
122
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000123 int GetNextInstructionId() {
124 return current_instruction_id_++;
125 }
126
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100127 uint16_t GetMaximumNumberOfOutVRegs() const {
128 return maximum_number_of_out_vregs_;
129 }
130
131 void UpdateMaximumNumberOfOutVRegs(uint16_t new_value) {
132 maximum_number_of_out_vregs_ = std::max(new_value, maximum_number_of_out_vregs_);
133 }
134
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000135 void UpdateTemporariesVRegSlots(size_t slots) {
136 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100137 }
138
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000139 size_t GetTemporariesVRegSlots() const {
140 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100141 }
142
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100143 void SetNumberOfVRegs(uint16_t number_of_vregs) {
144 number_of_vregs_ = number_of_vregs;
145 }
146
147 uint16_t GetNumberOfVRegs() const {
148 return number_of_vregs_;
149 }
150
151 void SetNumberOfInVRegs(uint16_t value) {
152 number_of_in_vregs_ = value;
153 }
154
155 uint16_t GetNumberOfInVRegs() const {
156 return number_of_in_vregs_;
157 }
158
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100159 uint16_t GetNumberOfLocalVRegs() const {
160 return number_of_vregs_ - number_of_in_vregs_;
161 }
162
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100163 const GrowableArray<HBasicBlock*>& GetReversePostOrder() const {
164 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100165 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100166
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000167 private:
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000168 HBasicBlock* FindCommonDominator(HBasicBlock* first, HBasicBlock* second) const;
169 void VisitBlockForDominatorTree(HBasicBlock* block,
170 HBasicBlock* predecessor,
171 GrowableArray<size_t>* visits);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100172 void FindBackEdges(ArenaBitVector* visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000173 void VisitBlockForBackEdges(HBasicBlock* block,
174 ArenaBitVector* visited,
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100175 ArenaBitVector* visiting);
Roland Levillainfc600dc2014-12-02 17:16:31 +0000176 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000177 void RemoveDeadBlocks(const ArenaBitVector& visited) const;
178
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000179 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000180
181 // List of blocks in insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000182 GrowableArray<HBasicBlock*> blocks_;
183
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100184 // List of blocks to perform a reverse post order tree traversal.
185 GrowableArray<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000186
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000187 HBasicBlock* entry_block_;
188 HBasicBlock* exit_block_;
189
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100190 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100191 uint16_t maximum_number_of_out_vregs_;
192
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100193 // The number of virtual registers in this method. Contains the parameters.
194 uint16_t number_of_vregs_;
195
196 // The number of virtual registers used by parameters of this method.
197 uint16_t number_of_in_vregs_;
198
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000199 // Number of vreg size slots that the temporaries use (used in baseline compiler).
200 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100201
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000202 // The current id to assign to a newly added instruction. See HInstruction.id_.
203 int current_instruction_id_;
204
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000205 DISALLOW_COPY_AND_ASSIGN(HGraph);
206};
207
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700208class HLoopInformation : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000209 public:
210 HLoopInformation(HBasicBlock* header, HGraph* graph)
211 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100212 suspend_check_(nullptr),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100213 back_edges_(graph->GetArena(), kDefaultNumberOfBackEdges),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100214 // Make bit vector growable, as the number of blocks may change.
215 blocks_(graph->GetArena(), graph->GetBlocks().Size(), true) {}
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100216
217 HBasicBlock* GetHeader() const {
218 return header_;
219 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000220
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100221 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
222 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
223 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
224
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000225 void AddBackEdge(HBasicBlock* back_edge) {
226 back_edges_.Add(back_edge);
227 }
228
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100229 void RemoveBackEdge(HBasicBlock* back_edge) {
230 back_edges_.Delete(back_edge);
231 }
232
233 bool IsBackEdge(HBasicBlock* block) {
234 for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) {
235 if (back_edges_.Get(i) == block) return true;
236 }
237 return false;
238 }
239
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000240 size_t NumberOfBackEdges() const {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000241 return back_edges_.Size();
242 }
243
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100244 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100245
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100246 const GrowableArray<HBasicBlock*>& GetBackEdges() const {
247 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100248 }
249
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100250 void ClearBackEdges() {
251 back_edges_.Reset();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100252 }
253
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100254 // Find blocks that are part of this loop. Returns whether the loop is a natural loop,
255 // that is the header dominates the back edge.
256 bool Populate();
257
258 // Returns whether this loop information contains `block`.
259 // Note that this loop information *must* be populated before entering this function.
260 bool Contains(const HBasicBlock& block) const;
261
262 // Returns whether this loop information is an inner loop of `other`.
263 // Note that `other` *must* be populated before entering this function.
264 bool IsIn(const HLoopInformation& other) const;
265
266 const ArenaBitVector& GetBlocks() const { return blocks_; }
267
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000268 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100269 // Internal recursive implementation of `Populate`.
270 void PopulateRecursive(HBasicBlock* block);
271
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000272 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100273 HSuspendCheck* suspend_check_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000274 GrowableArray<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100275 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000276
277 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
278};
279
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100280static constexpr size_t kNoLifetime = -1;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100281static constexpr uint32_t kNoDexPc = -1;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100282
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000283// A block in a method. Contains the list of instructions represented
284// as a double linked list. Each block knows its predecessors and
285// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100286
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700287class HBasicBlock : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000288 public:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100289 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000290 : graph_(graph),
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000291 predecessors_(graph->GetArena(), kDefaultNumberOfPredecessors),
292 successors_(graph->GetArena(), kDefaultNumberOfSuccessors),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000293 loop_information_(nullptr),
294 dominator_(nullptr),
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100295 dominated_blocks_(graph->GetArena(), kDefaultNumberOfDominatedBlocks),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100296 block_id_(-1),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100297 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100298 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000299 lifetime_end_(kNoLifetime),
300 is_catch_block_(false) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000301
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100302 const GrowableArray<HBasicBlock*>& GetPredecessors() const {
303 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000304 }
305
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100306 const GrowableArray<HBasicBlock*>& GetSuccessors() const {
307 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000308 }
309
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100310 const GrowableArray<HBasicBlock*>& GetDominatedBlocks() const {
311 return dominated_blocks_;
312 }
313
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100314 bool IsEntryBlock() const {
315 return graph_->GetEntryBlock() == this;
316 }
317
318 bool IsExitBlock() const {
319 return graph_->GetExitBlock() == this;
320 }
321
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000322 void AddBackEdge(HBasicBlock* back_edge) {
323 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000324 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000325 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100326 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000327 loop_information_->AddBackEdge(back_edge);
328 }
329
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000330 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000331
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000332 int GetBlockId() const { return block_id_; }
333 void SetBlockId(int id) { block_id_ = id; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000334
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000335 HBasicBlock* GetDominator() const { return dominator_; }
336 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100337 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.Add(block); }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000338
339 int NumberOfBackEdges() const {
340 return loop_information_ == nullptr
341 ? 0
342 : loop_information_->NumberOfBackEdges();
343 }
344
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100345 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
346 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100347 const HInstructionList& GetInstructions() const { return instructions_; }
348 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100349 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000350
351 void AddSuccessor(HBasicBlock* block) {
352 successors_.Add(block);
353 block->predecessors_.Add(this);
354 }
355
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100356 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
357 size_t successor_index = GetSuccessorIndexOf(existing);
358 DCHECK_NE(successor_index, static_cast<size_t>(-1));
359 existing->RemovePredecessor(this);
360 new_block->predecessors_.Add(this);
361 successors_.Put(successor_index, new_block);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000362 }
363
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100364 void RemovePredecessor(HBasicBlock* block) {
365 predecessors_.Delete(block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100366 }
367
368 void ClearAllPredecessors() {
369 predecessors_.Reset();
370 }
371
372 void AddPredecessor(HBasicBlock* block) {
373 predecessors_.Add(block);
374 block->successors_.Add(this);
375 }
376
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100377 void SwapPredecessors() {
Nicolas Geoffrayc83d4412014-09-18 16:46:20 +0100378 DCHECK_EQ(predecessors_.Size(), 2u);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100379 HBasicBlock* temp = predecessors_.Get(0);
380 predecessors_.Put(0, predecessors_.Get(1));
381 predecessors_.Put(1, temp);
382 }
383
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100384 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) {
385 for (size_t i = 0, e = predecessors_.Size(); i < e; ++i) {
386 if (predecessors_.Get(i) == predecessor) {
387 return i;
388 }
389 }
390 return -1;
391 }
392
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100393 size_t GetSuccessorIndexOf(HBasicBlock* successor) {
394 for (size_t i = 0, e = successors_.Size(); i < e; ++i) {
395 if (successors_.Get(i) == successor) {
396 return i;
397 }
398 }
399 return -1;
400 }
401
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000402 void AddInstruction(HInstruction* instruction);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100403 void RemoveInstruction(HInstruction* instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100404 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +0100405 // Replace instruction `initial` with `replacement` within this block.
406 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
407 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100408 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100409 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100410 void RemovePhi(HPhi* phi);
411
412 bool IsLoopHeader() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100413 return (loop_information_ != nullptr) && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100414 }
415
Roland Levillain6b879dd2014-09-22 17:13:44 +0100416 bool IsLoopPreHeaderFirstPredecessor() const {
417 DCHECK(IsLoopHeader());
418 DCHECK(!GetPredecessors().IsEmpty());
419 return GetPredecessors().Get(0) == GetLoopInformation()->GetPreHeader();
420 }
421
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100422 HLoopInformation* GetLoopInformation() const {
423 return loop_information_;
424 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000425
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100426 // Set the loop_information_ on this block. This method overrides the current
427 // loop_information if it is an outer loop of the passed loop information.
428 void SetInLoop(HLoopInformation* info) {
429 if (IsLoopHeader()) {
430 // Nothing to do. This just means `info` is an outer loop.
431 } else if (loop_information_ == nullptr) {
432 loop_information_ = info;
433 } else if (loop_information_->Contains(*info->GetHeader())) {
434 // Block is currently part of an outer loop. Make it part of this inner loop.
435 // Note that a non loop header having a loop information means this loop information
436 // has already been populated
437 loop_information_ = info;
438 } else {
439 // Block is part of an inner loop. Do not update the loop information.
440 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
441 // at this point, because this method is being called while populating `info`.
442 }
443 }
444
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100445 bool IsInLoop() const { return loop_information_ != nullptr; }
446
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100447 // Returns wheter this block dominates the blocked passed as parameter.
448 bool Dominates(HBasicBlock* block) const;
449
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100450 size_t GetLifetimeStart() const { return lifetime_start_; }
451 size_t GetLifetimeEnd() const { return lifetime_end_; }
452
453 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
454 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
455
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100456 uint32_t GetDexPc() const { return dex_pc_; }
457
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000458 bool IsCatchBlock() const { return is_catch_block_; }
459 void SetIsCatchBlock() { is_catch_block_ = true; }
460
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000461 private:
462 HGraph* const graph_;
463 GrowableArray<HBasicBlock*> predecessors_;
464 GrowableArray<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100465 HInstructionList instructions_;
466 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000467 HLoopInformation* loop_information_;
468 HBasicBlock* dominator_;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100469 GrowableArray<HBasicBlock*> dominated_blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000470 int block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100471 // The dex program counter of the first instruction of this block.
472 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100473 size_t lifetime_start_;
474 size_t lifetime_end_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000475 bool is_catch_block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000476
477 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
478};
479
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100480#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
481 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000482 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000483 M(ArrayGet, Instruction) \
484 M(ArrayLength, Instruction) \
485 M(ArraySet, Instruction) \
486 M(BoundsCheck, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000487 M(CheckCast, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100488 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000489 M(Compare, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100490 M(Condition, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000491 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +0000492 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000493 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100494 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000495 M(Exit, Instruction) \
496 M(FloatConstant, Constant) \
497 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100498 M(GreaterThan, Condition) \
499 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100500 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000501 M(InstanceFieldGet, Instruction) \
502 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000503 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100504 M(IntConstant, Constant) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +0000505 M(InvokeInterface, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100506 M(InvokeStatic, Invoke) \
507 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000508 M(LessThan, Condition) \
509 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000510 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000511 M(LoadException, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100512 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000513 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100514 M(Local, Instruction) \
515 M(LongConstant, Constant) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +0000516 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000517 M(Mul, BinaryOperation) \
518 M(Neg, UnaryOperation) \
519 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100520 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +0100521 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000522 M(NotEqual, Condition) \
523 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000524 M(Or, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100525 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000526 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100527 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000528 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100529 M(Return, Instruction) \
530 M(ReturnVoid, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000531 M(Shl, BinaryOperation) \
532 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100533 M(StaticFieldGet, Instruction) \
534 M(StaticFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100535 M(StoreLocal, Instruction) \
536 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100537 M(SuspendCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000538 M(Temporary, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000539 M(Throw, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +0000540 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000541 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000542 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000543
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100544#define FOR_EACH_INSTRUCTION(M) \
545 FOR_EACH_CONCRETE_INSTRUCTION(M) \
546 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +0100547 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +0100548 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100549 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -0700550
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100551#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000552FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
553#undef FORWARD_DECLARATION
554
Roland Levillainccc07a92014-09-16 14:48:16 +0100555#define DECLARE_INSTRUCTION(type) \
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100556 virtual InstructionKind GetKind() const { return k##type; } \
Roland Levillainccc07a92014-09-16 14:48:16 +0100557 virtual const char* DebugName() const { return #type; } \
558 virtual const H##type* As##type() const OVERRIDE { return this; } \
559 virtual H##type* As##type() OVERRIDE { return this; } \
560 virtual bool InstructionTypeEquals(HInstruction* other) const { \
561 return other->Is##type(); \
562 } \
563 virtual void Accept(HGraphVisitor* visitor)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000564
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100565template <typename T>
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700566class HUseListNode : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000567 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100568 HUseListNode(T* user, size_t index, HUseListNode* tail)
Dave Allison20dfc792014-06-16 20:44:29 -0700569 : user_(user), index_(index), tail_(tail) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000570
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000571 HUseListNode* GetTail() const { return tail_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100572 T* GetUser() const { return user_; }
573 size_t GetIndex() const { return index_; }
574
575 void SetTail(HUseListNode<T>* node) { tail_ = node; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000576
577 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100578 T* const user_;
579 const size_t index_;
580 HUseListNode<T>* tail_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000581
582 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
583};
584
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100585// Represents the side effects an instruction may have.
586class SideEffects : public ValueObject {
587 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100588 SideEffects() : flags_(0) {}
589
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100590 static SideEffects None() {
591 return SideEffects(0);
592 }
593
594 static SideEffects All() {
595 return SideEffects(ChangesSomething().flags_ | DependsOnSomething().flags_);
596 }
597
598 static SideEffects ChangesSomething() {
599 return SideEffects((1 << kFlagChangesCount) - 1);
600 }
601
602 static SideEffects DependsOnSomething() {
603 int count = kFlagDependsOnCount - kFlagChangesCount;
604 return SideEffects(((1 << count) - 1) << kFlagChangesCount);
605 }
606
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100607 SideEffects Union(SideEffects other) const {
608 return SideEffects(flags_ | other.flags_);
609 }
610
Roland Levillain72bceff2014-09-15 18:29:00 +0100611 bool HasSideEffects() const {
612 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
613 return (flags_ & all_bits_set) != 0;
614 }
615
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100616 bool HasAllSideEffects() const {
617 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
618 return all_bits_set == (flags_ & all_bits_set);
619 }
620
621 bool DependsOn(SideEffects other) const {
622 size_t depends_flags = other.ComputeDependsFlags();
623 return (flags_ & depends_flags) != 0;
624 }
625
626 bool HasDependencies() const {
627 int count = kFlagDependsOnCount - kFlagChangesCount;
628 size_t all_bits_set = (1 << count) - 1;
629 return ((flags_ >> kFlagChangesCount) & all_bits_set) != 0;
630 }
631
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100632 private:
633 static constexpr int kFlagChangesSomething = 0;
634 static constexpr int kFlagChangesCount = kFlagChangesSomething + 1;
635
636 static constexpr int kFlagDependsOnSomething = kFlagChangesCount;
637 static constexpr int kFlagDependsOnCount = kFlagDependsOnSomething + 1;
638
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100639 explicit SideEffects(size_t flags) : flags_(flags) {}
640
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100641 size_t ComputeDependsFlags() const {
642 return flags_ << kFlagChangesCount;
643 }
644
645 size_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100646};
647
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700648class HInstruction : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000649 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100650 explicit HInstruction(SideEffects side_effects)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000651 : previous_(nullptr),
652 next_(nullptr),
653 block_(nullptr),
654 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100655 ssa_index_(-1),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000656 uses_(nullptr),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100657 env_uses_(nullptr),
658 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100659 locations_(nullptr),
660 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100661 lifetime_position_(kNoLifetime),
662 side_effects_(side_effects) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000663
Dave Allison20dfc792014-06-16 20:44:29 -0700664 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000665
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100666#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100667 enum InstructionKind {
668 FOR_EACH_INSTRUCTION(DECLARE_KIND)
669 };
670#undef DECLARE_KIND
671
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000672 HInstruction* GetNext() const { return next_; }
673 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000674
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000675 HBasicBlock* GetBlock() const { return block_; }
676 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100677 bool IsInBlock() const { return block_ != nullptr; }
678 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +0100679 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000680
Roland Levillain6b879dd2014-09-22 17:13:44 +0100681 virtual size_t InputCount() const = 0;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100682 virtual HInstruction* InputAt(size_t i) const = 0;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000683
684 virtual void Accept(HGraphVisitor* visitor) = 0;
685 virtual const char* DebugName() const = 0;
686
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100687 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100688 virtual void SetRawInputAt(size_t index, HInstruction* input) = 0;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100689
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100690 virtual bool NeedsEnvironment() const { return false; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100691 virtual bool IsControlFlow() const { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +0100692 virtual bool CanThrow() const { return false; }
Roland Levillain72bceff2014-09-15 18:29:00 +0100693 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100694
695 void AddUseAt(HInstruction* user, size_t index) {
696 uses_ = new (block_->GetGraph()->GetArena()) HUseListNode<HInstruction>(user, index, uses_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000697 }
698
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100699 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +0100700 DCHECK(user != nullptr);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100701 env_uses_ = new (block_->GetGraph()->GetArena()) HUseListNode<HEnvironment>(
702 user, index, env_uses_);
703 }
704
705 void RemoveUser(HInstruction* user, size_t index);
Nicolas Geoffray724c9632014-09-22 12:27:27 +0100706 void RemoveEnvironmentUser(HEnvironment* user, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100707
708 HUseListNode<HInstruction>* GetUses() const { return uses_; }
709 HUseListNode<HEnvironment>* GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000710
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100711 bool HasUses() const { return uses_ != nullptr || env_uses_ != nullptr; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100712 bool HasEnvironmentUses() const { return env_uses_ != nullptr; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000713
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100714 size_t NumberOfUses() const {
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100715 // TODO: Optimize this method if it is used outside of the HGraphVisualizer.
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100716 size_t result = 0;
717 HUseListNode<HInstruction>* current = uses_;
718 while (current != nullptr) {
719 current = current->GetTail();
720 ++result;
721 }
722 return result;
723 }
724
Roland Levillain6c82d402014-10-13 16:10:27 +0100725 // Does this instruction strictly dominate `other_instruction`?
726 // Returns false if this instruction and `other_instruction` are the same.
727 // Aborts if this instruction and `other_instruction` are both phis.
728 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +0100729
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000730 int GetId() const { return id_; }
731 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000732
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100733 int GetSsaIndex() const { return ssa_index_; }
734 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
735 bool HasSsaIndex() const { return ssa_index_ != -1; }
736
737 bool HasEnvironment() const { return environment_ != nullptr; }
738 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100739 void SetEnvironment(HEnvironment* environment) { environment_ = environment; }
740
Nicolas Geoffray39468442014-09-02 15:17:15 +0100741 // Returns the number of entries in the environment. Typically, that is the
742 // number of dex registers in a method. It could be more in case of inlining.
743 size_t EnvironmentSize() const;
744
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000745 LocationSummary* GetLocations() const { return locations_; }
746 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000747
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100748 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100749 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100750
Dave Allison20dfc792014-06-16 20:44:29 -0700751 bool HasOnlyOneUse() const {
752 return uses_ != nullptr && uses_->GetTail() == nullptr;
753 }
754
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100755#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +0100756 bool Is##type() const { return (As##type() != nullptr); } \
757 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000758 virtual H##type* As##type() { return nullptr; }
759
760 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
761#undef INSTRUCTION_TYPE_CHECK
762
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100763 // Returns whether the instruction can be moved within the graph.
764 virtual bool CanBeMoved() const { return false; }
765
766 // Returns whether the two instructions are of the same kind.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700767 virtual bool InstructionTypeEquals(HInstruction* other) const {
768 UNUSED(other);
769 return false;
770 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100771
772 // Returns whether any data encoded in the two instructions is equal.
773 // This method does not look at the inputs. Both instructions must be
774 // of the same type, otherwise the method has undefined behavior.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700775 virtual bool InstructionDataEquals(HInstruction* other) const {
776 UNUSED(other);
777 return false;
778 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100779
780 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +0000781 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100782 // 2) Their inputs are identical.
783 bool Equals(HInstruction* other) const;
784
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100785 virtual InstructionKind GetKind() const = 0;
786
787 virtual size_t ComputeHashCode() const {
788 size_t result = GetKind();
789 for (size_t i = 0, e = InputCount(); i < e; ++i) {
790 result = (result * 31) + InputAt(i)->GetId();
791 }
792 return result;
793 }
794
795 SideEffects GetSideEffects() const { return side_effects_; }
796
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100797 size_t GetLifetimePosition() const { return lifetime_position_; }
798 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
799 LiveInterval* GetLiveInterval() const { return live_interval_; }
800 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
801 bool HasLiveInterval() const { return live_interval_ != nullptr; }
802
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000803 private:
804 HInstruction* previous_;
805 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000806 HBasicBlock* block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000807
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000808 // An instruction gets an id when it is added to the graph.
809 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +0100810 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000811 int id_;
812
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100813 // When doing liveness analysis, instructions that have uses get an SSA index.
814 int ssa_index_;
815
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100816 // List of instructions that have this instruction as input.
817 HUseListNode<HInstruction>* uses_;
818
819 // List of environments that contain this instruction.
820 HUseListNode<HEnvironment>* env_uses_;
821
Nicolas Geoffray39468442014-09-02 15:17:15 +0100822 // The environment associated with this instruction. Not null if the instruction
823 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100824 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000825
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000826 // Set by the code generator.
827 LocationSummary* locations_;
828
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100829 // Set by the liveness analysis.
830 LiveInterval* live_interval_;
831
832 // Set by the liveness analysis, this is the position in a linear
833 // order of blocks where this instruction's live interval start.
834 size_t lifetime_position_;
835
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100836 const SideEffects side_effects_;
837
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000838 friend class HBasicBlock;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100839 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000840
841 DISALLOW_COPY_AND_ASSIGN(HInstruction);
842};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700843std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000844
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100845template<typename T>
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000846class HUseIterator : public ValueObject {
847 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100848 explicit HUseIterator(HUseListNode<T>* uses) : current_(uses) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000849
850 bool Done() const { return current_ == nullptr; }
851
852 void Advance() {
853 DCHECK(!Done());
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000854 current_ = current_->GetTail();
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000855 }
856
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100857 HUseListNode<T>* Current() const {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000858 DCHECK(!Done());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100859 return current_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000860 }
861
862 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100863 HUseListNode<T>* current_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000864
865 friend class HValue;
866};
867
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100868// A HEnvironment object contains the values of virtual registers at a given location.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700869class HEnvironment : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100870 public:
871 HEnvironment(ArenaAllocator* arena, size_t number_of_vregs) : vregs_(arena, number_of_vregs) {
872 vregs_.SetSize(number_of_vregs);
873 for (size_t i = 0; i < number_of_vregs; i++) {
874 vregs_.Put(i, nullptr);
875 }
876 }
877
878 void Populate(const GrowableArray<HInstruction*>& env) {
879 for (size_t i = 0; i < env.Size(); i++) {
880 HInstruction* instruction = env.Get(i);
881 vregs_.Put(i, instruction);
882 if (instruction != nullptr) {
883 instruction->AddEnvUseAt(this, i);
884 }
885 }
886 }
887
888 void SetRawEnvAt(size_t index, HInstruction* instruction) {
889 vregs_.Put(index, instruction);
890 }
891
Nicolas Geoffray39468442014-09-02 15:17:15 +0100892 HInstruction* GetInstructionAt(size_t index) const {
893 return vregs_.Get(index);
894 }
895
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100896 GrowableArray<HInstruction*>* GetVRegs() {
897 return &vregs_;
898 }
899
Nicolas Geoffray39468442014-09-02 15:17:15 +0100900 size_t Size() const { return vregs_.Size(); }
901
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100902 private:
903 GrowableArray<HInstruction*> vregs_;
904
905 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
906};
907
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000908class HInputIterator : public ValueObject {
909 public:
Dave Allison20dfc792014-06-16 20:44:29 -0700910 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000911
912 bool Done() const { return index_ == instruction_->InputCount(); }
913 HInstruction* Current() const { return instruction_->InputAt(index_); }
914 void Advance() { index_++; }
915
916 private:
917 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100918 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000919
920 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
921};
922
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000923class HInstructionIterator : public ValueObject {
924 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100925 explicit HInstructionIterator(const HInstructionList& instructions)
926 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000927 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000928 }
929
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000930 bool Done() const { return instruction_ == nullptr; }
931 HInstruction* Current() const { return instruction_; }
932 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000933 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000934 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000935 }
936
937 private:
938 HInstruction* instruction_;
939 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100940
941 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000942};
943
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100944class HBackwardInstructionIterator : public ValueObject {
945 public:
946 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
947 : instruction_(instructions.last_instruction_) {
948 next_ = Done() ? nullptr : instruction_->GetPrevious();
949 }
950
951 bool Done() const { return instruction_ == nullptr; }
952 HInstruction* Current() const { return instruction_; }
953 void Advance() {
954 instruction_ = next_;
955 next_ = Done() ? nullptr : instruction_->GetPrevious();
956 }
957
958 private:
959 HInstruction* instruction_;
960 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100961
962 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100963};
964
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000965// An embedded container with N elements of type T. Used (with partial
966// specialization for N=0) because embedded arrays cannot have size 0.
967template<typename T, intptr_t N>
968class EmbeddedArray {
969 public:
Dave Allison20dfc792014-06-16 20:44:29 -0700970 EmbeddedArray() : elements_() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000971
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000972 intptr_t GetLength() const { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000973
974 const T& operator[](intptr_t i) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000975 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000976 return elements_[i];
977 }
978
979 T& operator[](intptr_t i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000980 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000981 return elements_[i];
982 }
983
984 const T& At(intptr_t i) const {
985 return (*this)[i];
986 }
987
988 void SetAt(intptr_t i, const T& val) {
989 (*this)[i] = val;
990 }
991
992 private:
993 T elements_[N];
994};
995
996template<typename T>
997class EmbeddedArray<T, 0> {
998 public:
999 intptr_t length() const { return 0; }
1000 const T& operator[](intptr_t i) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001001 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001002 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001003 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001004 }
1005 T& operator[](intptr_t i) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001006 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001007 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001008 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001009 }
1010};
1011
1012template<intptr_t N>
1013class HTemplateInstruction: public HInstruction {
1014 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001015 HTemplateInstruction<N>(SideEffects side_effects)
1016 : HInstruction(side_effects), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07001017 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001018
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001019 virtual size_t InputCount() const { return N; }
1020 virtual HInstruction* InputAt(size_t i) const { return inputs_[i]; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001021
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001022 protected:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001023 virtual void SetRawInputAt(size_t i, HInstruction* instruction) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001024 inputs_[i] = instruction;
1025 }
1026
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001027 private:
1028 EmbeddedArray<HInstruction*, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001029
1030 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001031};
1032
Dave Allison20dfc792014-06-16 20:44:29 -07001033template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001034class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07001035 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001036 HExpression<N>(Primitive::Type type, SideEffects side_effects)
1037 : HTemplateInstruction<N>(side_effects), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07001038 virtual ~HExpression() {}
1039
1040 virtual Primitive::Type GetType() const { return type_; }
1041
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001042 protected:
1043 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07001044};
1045
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001046// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
1047// instruction that branches to the exit block.
1048class HReturnVoid : public HTemplateInstruction<0> {
1049 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001050 HReturnVoid() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001051
1052 virtual bool IsControlFlow() const { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001053
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001054 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001055
1056 private:
1057 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
1058};
1059
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001060// Represents dex's RETURN opcodes. A HReturn is a control flow
1061// instruction that branches to the exit block.
1062class HReturn : public HTemplateInstruction<1> {
1063 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001064 explicit HReturn(HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001065 SetRawInputAt(0, value);
1066 }
1067
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001068 virtual bool IsControlFlow() const { return true; }
1069
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001070 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001071
1072 private:
1073 DISALLOW_COPY_AND_ASSIGN(HReturn);
1074};
1075
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001076// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001077// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001078// exit block.
1079class HExit : public HTemplateInstruction<0> {
1080 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001081 HExit() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001082
1083 virtual bool IsControlFlow() const { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001084
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001085 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001086
1087 private:
1088 DISALLOW_COPY_AND_ASSIGN(HExit);
1089};
1090
1091// Jumps from one block to another.
1092class HGoto : public HTemplateInstruction<0> {
1093 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001094 HGoto() : HTemplateInstruction(SideEffects::None()) {}
1095
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001096 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001097
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001098 HBasicBlock* GetSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001099 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001100 }
1101
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001102 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001103
1104 private:
1105 DISALLOW_COPY_AND_ASSIGN(HGoto);
1106};
1107
Dave Allison20dfc792014-06-16 20:44:29 -07001108
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001109// Conditional branch. A block ending with an HIf instruction must have
1110// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001111class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001112 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001113 explicit HIf(HInstruction* input) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001114 SetRawInputAt(0, input);
1115 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001116
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001117 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001118
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001119 HBasicBlock* IfTrueSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001120 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001121 }
1122
1123 HBasicBlock* IfFalseSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001124 return GetBlock()->GetSuccessors().Get(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001125 }
1126
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001127 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001128
Dave Allison20dfc792014-06-16 20:44:29 -07001129 virtual bool IsIfInstruction() const { return true; }
1130
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001131 private:
1132 DISALLOW_COPY_AND_ASSIGN(HIf);
1133};
1134
Roland Levillain88cb1752014-10-20 16:36:47 +01001135class HUnaryOperation : public HExpression<1> {
1136 public:
1137 HUnaryOperation(Primitive::Type result_type, HInstruction* input)
1138 : HExpression(result_type, SideEffects::None()) {
1139 SetRawInputAt(0, input);
1140 }
1141
1142 HInstruction* GetInput() const { return InputAt(0); }
1143 Primitive::Type GetResultType() const { return GetType(); }
1144
1145 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001146 virtual bool InstructionDataEquals(HInstruction* other) const {
1147 UNUSED(other);
1148 return true;
1149 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001150
Roland Levillain9240d6a2014-10-20 16:47:04 +01001151 // Try to statically evaluate `operation` and return a HConstant
1152 // containing the result of this evaluation. If `operation` cannot
1153 // be evaluated as a constant, return nullptr.
1154 HConstant* TryStaticEvaluation() const;
1155
1156 // Apply this operation to `x`.
1157 virtual int32_t Evaluate(int32_t x) const = 0;
1158 virtual int64_t Evaluate(int64_t x) const = 0;
1159
Roland Levillain88cb1752014-10-20 16:36:47 +01001160 DECLARE_INSTRUCTION(UnaryOperation);
1161
1162 private:
1163 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
1164};
1165
Dave Allison20dfc792014-06-16 20:44:29 -07001166class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001167 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001168 HBinaryOperation(Primitive::Type result_type,
1169 HInstruction* left,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001170 HInstruction* right) : HExpression(result_type, SideEffects::None()) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001171 SetRawInputAt(0, left);
1172 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001173 }
1174
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001175 HInstruction* GetLeft() const { return InputAt(0); }
1176 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07001177 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001178
1179 virtual bool IsCommutative() { return false; }
1180
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001181 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001182 virtual bool InstructionDataEquals(HInstruction* other) const {
1183 UNUSED(other);
1184 return true;
1185 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001186
Roland Levillain9240d6a2014-10-20 16:47:04 +01001187 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01001188 // containing the result of this evaluation. If `operation` cannot
1189 // be evaluated as a constant, return nullptr.
Roland Levillain9240d6a2014-10-20 16:47:04 +01001190 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01001191
1192 // Apply this operation to `x` and `y`.
1193 virtual int32_t Evaluate(int32_t x, int32_t y) const = 0;
1194 virtual int64_t Evaluate(int64_t x, int64_t y) const = 0;
1195
Roland Levillainccc07a92014-09-16 14:48:16 +01001196 DECLARE_INSTRUCTION(BinaryOperation);
1197
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001198 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001199 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
1200};
1201
Dave Allison20dfc792014-06-16 20:44:29 -07001202class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001203 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001204 HCondition(HInstruction* first, HInstruction* second)
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001205 : HBinaryOperation(Primitive::kPrimBoolean, first, second),
1206 needs_materialization_(true) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001207
1208 virtual bool IsCommutative() { return true; }
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001209
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001210 bool NeedsMaterialization() const { return needs_materialization_; }
1211 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001212
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001213 // For code generation purposes, returns whether this instruction is just before
1214 // `if_`, and disregard moves in between.
1215 bool IsBeforeWhenDisregardMoves(HIf* if_) const;
1216
Dave Allison20dfc792014-06-16 20:44:29 -07001217 DECLARE_INSTRUCTION(Condition);
1218
1219 virtual IfCondition GetCondition() const = 0;
1220
1221 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001222 // For register allocation purposes, returns whether this instruction needs to be
1223 // materialized (that is, not just be in the processor flags).
1224 bool needs_materialization_;
1225
Dave Allison20dfc792014-06-16 20:44:29 -07001226 DISALLOW_COPY_AND_ASSIGN(HCondition);
1227};
1228
1229// Instruction to check if two inputs are equal to each other.
1230class HEqual : public HCondition {
1231 public:
1232 HEqual(HInstruction* first, HInstruction* second)
1233 : HCondition(first, second) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001234
Roland Levillain93445682014-10-06 19:24:02 +01001235 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1236 return x == y ? 1 : 0;
1237 }
1238 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1239 return x == y ? 1 : 0;
1240 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001241
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001242 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001243
Dave Allison20dfc792014-06-16 20:44:29 -07001244 virtual IfCondition GetCondition() const {
1245 return kCondEQ;
1246 }
1247
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001248 private:
1249 DISALLOW_COPY_AND_ASSIGN(HEqual);
1250};
1251
Dave Allison20dfc792014-06-16 20:44:29 -07001252class HNotEqual : public HCondition {
1253 public:
1254 HNotEqual(HInstruction* first, HInstruction* second)
1255 : HCondition(first, second) {}
1256
Roland Levillain93445682014-10-06 19:24:02 +01001257 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1258 return x != y ? 1 : 0;
1259 }
1260 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1261 return x != y ? 1 : 0;
1262 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001263
Dave Allison20dfc792014-06-16 20:44:29 -07001264 DECLARE_INSTRUCTION(NotEqual);
1265
1266 virtual IfCondition GetCondition() const {
1267 return kCondNE;
1268 }
1269
1270 private:
1271 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
1272};
1273
1274class HLessThan : public HCondition {
1275 public:
1276 HLessThan(HInstruction* first, HInstruction* second)
1277 : HCondition(first, second) {}
1278
Roland Levillain93445682014-10-06 19:24:02 +01001279 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1280 return x < y ? 1 : 0;
1281 }
1282 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1283 return x < y ? 1 : 0;
1284 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001285
Dave Allison20dfc792014-06-16 20:44:29 -07001286 DECLARE_INSTRUCTION(LessThan);
1287
1288 virtual IfCondition GetCondition() const {
1289 return kCondLT;
1290 }
1291
1292 private:
1293 DISALLOW_COPY_AND_ASSIGN(HLessThan);
1294};
1295
1296class HLessThanOrEqual : public HCondition {
1297 public:
1298 HLessThanOrEqual(HInstruction* first, HInstruction* second)
1299 : HCondition(first, second) {}
1300
Roland Levillain93445682014-10-06 19:24:02 +01001301 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1302 return x <= y ? 1 : 0;
1303 }
1304 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1305 return x <= y ? 1 : 0;
1306 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001307
Dave Allison20dfc792014-06-16 20:44:29 -07001308 DECLARE_INSTRUCTION(LessThanOrEqual);
1309
1310 virtual IfCondition GetCondition() const {
1311 return kCondLE;
1312 }
1313
1314 private:
1315 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
1316};
1317
1318class HGreaterThan : public HCondition {
1319 public:
1320 HGreaterThan(HInstruction* first, HInstruction* second)
1321 : HCondition(first, second) {}
1322
Roland Levillain93445682014-10-06 19:24:02 +01001323 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1324 return x > y ? 1 : 0;
1325 }
1326 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1327 return x > y ? 1 : 0;
1328 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001329
Dave Allison20dfc792014-06-16 20:44:29 -07001330 DECLARE_INSTRUCTION(GreaterThan);
1331
1332 virtual IfCondition GetCondition() const {
1333 return kCondGT;
1334 }
1335
1336 private:
1337 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
1338};
1339
1340class HGreaterThanOrEqual : public HCondition {
1341 public:
1342 HGreaterThanOrEqual(HInstruction* first, HInstruction* second)
1343 : HCondition(first, second) {}
1344
Roland Levillain93445682014-10-06 19:24:02 +01001345 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1346 return x >= y ? 1 : 0;
1347 }
1348 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1349 return x >= y ? 1 : 0;
1350 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001351
Dave Allison20dfc792014-06-16 20:44:29 -07001352 DECLARE_INSTRUCTION(GreaterThanOrEqual);
1353
1354 virtual IfCondition GetCondition() const {
1355 return kCondGE;
1356 }
1357
1358 private:
1359 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
1360};
1361
1362
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001363// Instruction to check how two inputs compare to each other.
1364// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
1365class HCompare : public HBinaryOperation {
1366 public:
Calin Juravleddb7df22014-11-25 20:56:51 +00001367 // The bias applies for floating point operations and indicates how NaN
1368 // comparisons are treated:
1369 enum Bias {
1370 kNoBias, // bias is not applicable (i.e. for long operation)
1371 kGtBias, // return 1 for NaN comparisons
1372 kLtBias, // return -1 for NaN comparisons
1373 };
1374
1375 HCompare(Primitive::Type type, HInstruction* first, HInstruction* second, Bias bias)
1376 : HBinaryOperation(Primitive::kPrimInt, first, second), bias_(bias) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001377 DCHECK_EQ(type, first->GetType());
1378 DCHECK_EQ(type, second->GetType());
1379 }
1380
Calin Juravleddb7df22014-11-25 20:56:51 +00001381 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001382 return
1383 x == y ? 0 :
1384 x > y ? 1 :
1385 -1;
1386 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001387
1388 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001389 return
1390 x == y ? 0 :
1391 x > y ? 1 :
1392 -1;
1393 }
1394
Calin Juravleddb7df22014-11-25 20:56:51 +00001395 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
1396 return bias_ == other->AsCompare()->bias_;
1397 }
1398
1399 bool IsGtBias() { return bias_ == kGtBias; }
1400
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001401 DECLARE_INSTRUCTION(Compare);
1402
1403 private:
Calin Juravleddb7df22014-11-25 20:56:51 +00001404 const Bias bias_;
1405
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001406 DISALLOW_COPY_AND_ASSIGN(HCompare);
1407};
1408
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001409// A local in the graph. Corresponds to a Dex register.
1410class HLocal : public HTemplateInstruction<0> {
1411 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001412 explicit HLocal(uint16_t reg_number)
1413 : HTemplateInstruction(SideEffects::None()), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001414
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001415 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001416
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001417 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001418
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001419 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001420 // The Dex register number.
1421 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001422
1423 DISALLOW_COPY_AND_ASSIGN(HLocal);
1424};
1425
1426// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07001427class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001428 public:
Roland Levillain5799fc02014-09-25 12:15:20 +01001429 HLoadLocal(HLocal* local, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001430 : HExpression(type, SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001431 SetRawInputAt(0, local);
1432 }
1433
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001434 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1435
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001436 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001437
1438 private:
1439 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
1440};
1441
1442// Store a value in a given local. This instruction has two inputs: the value
1443// and the local.
1444class HStoreLocal : public HTemplateInstruction<2> {
1445 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001446 HStoreLocal(HLocal* local, HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001447 SetRawInputAt(0, local);
1448 SetRawInputAt(1, value);
1449 }
1450
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001451 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1452
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001453 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001454
1455 private:
1456 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
1457};
1458
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001459class HConstant : public HExpression<0> {
1460 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001461 explicit HConstant(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
1462
1463 virtual bool CanBeMoved() const { return true; }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001464
1465 DECLARE_INSTRUCTION(Constant);
1466
1467 private:
1468 DISALLOW_COPY_AND_ASSIGN(HConstant);
1469};
1470
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001471class HFloatConstant : public HConstant {
1472 public:
1473 explicit HFloatConstant(float value) : HConstant(Primitive::kPrimFloat), value_(value) {}
1474
1475 float GetValue() const { return value_; }
1476
1477 virtual bool InstructionDataEquals(HInstruction* other) const {
1478 return bit_cast<float, int32_t>(other->AsFloatConstant()->value_) ==
1479 bit_cast<float, int32_t>(value_);
1480 }
1481
1482 virtual size_t ComputeHashCode() const { return static_cast<size_t>(GetValue()); }
1483
1484 DECLARE_INSTRUCTION(FloatConstant);
1485
1486 private:
1487 const float value_;
1488
1489 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
1490};
1491
1492class HDoubleConstant : public HConstant {
1493 public:
1494 explicit HDoubleConstant(double value) : HConstant(Primitive::kPrimDouble), value_(value) {}
1495
1496 double GetValue() const { return value_; }
1497
1498 virtual bool InstructionDataEquals(HInstruction* other) const {
1499 return bit_cast<double, int64_t>(other->AsDoubleConstant()->value_) ==
1500 bit_cast<double, int64_t>(value_);
1501 }
1502
1503 virtual size_t ComputeHashCode() const { return static_cast<size_t>(GetValue()); }
1504
1505 DECLARE_INSTRUCTION(DoubleConstant);
1506
1507 private:
1508 const double value_;
1509
1510 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
1511};
1512
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001513// Constants of the type int. Those can be from Dex instructions, or
1514// synthesized (for example with the if-eqz instruction).
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001515class HIntConstant : public HConstant {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001516 public:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001517 explicit HIntConstant(int32_t value) : HConstant(Primitive::kPrimInt), value_(value) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001518
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001519 int32_t GetValue() const { return value_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001520
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001521 virtual bool InstructionDataEquals(HInstruction* other) const {
1522 return other->AsIntConstant()->value_ == value_;
1523 }
1524
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001525 virtual size_t ComputeHashCode() const { return GetValue(); }
1526
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001527 DECLARE_INSTRUCTION(IntConstant);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001528
1529 private:
1530 const int32_t value_;
1531
1532 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
1533};
1534
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001535class HLongConstant : public HConstant {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001536 public:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001537 explicit HLongConstant(int64_t value) : HConstant(Primitive::kPrimLong), value_(value) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001538
1539 int64_t GetValue() const { return value_; }
1540
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001541 virtual bool InstructionDataEquals(HInstruction* other) const {
1542 return other->AsLongConstant()->value_ == value_;
1543 }
1544
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001545 virtual size_t ComputeHashCode() const { return static_cast<size_t>(GetValue()); }
1546
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001547 DECLARE_INSTRUCTION(LongConstant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001548
1549 private:
1550 const int64_t value_;
1551
1552 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
1553};
1554
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001555class HInvoke : public HInstruction {
1556 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001557 HInvoke(ArenaAllocator* arena,
1558 uint32_t number_of_arguments,
1559 Primitive::Type return_type,
1560 uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001561 : HInstruction(SideEffects::All()),
1562 inputs_(arena, number_of_arguments),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001563 return_type_(return_type),
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001564 dex_pc_(dex_pc) {
1565 inputs_.SetSize(number_of_arguments);
1566 }
1567
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001568 virtual size_t InputCount() const { return inputs_.Size(); }
1569 virtual HInstruction* InputAt(size_t i) const { return inputs_.Get(i); }
1570
1571 // Runtime needs to walk the stack, so Dex -> Dex calls need to
1572 // know their environment.
1573 virtual bool NeedsEnvironment() const { return true; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001574
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001575 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001576 SetRawInputAt(index, argument);
1577 }
1578
1579 virtual void SetRawInputAt(size_t index, HInstruction* input) {
1580 inputs_.Put(index, input);
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001581 }
1582
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001583 virtual Primitive::Type GetType() const { return return_type_; }
1584
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001585 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001586
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001587 DECLARE_INSTRUCTION(Invoke);
1588
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001589 protected:
1590 GrowableArray<HInstruction*> inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001591 const Primitive::Type return_type_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001592 const uint32_t dex_pc_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001593
1594 private:
1595 DISALLOW_COPY_AND_ASSIGN(HInvoke);
1596};
1597
1598class HInvokeStatic : public HInvoke {
1599 public:
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001600 HInvokeStatic(ArenaAllocator* arena,
1601 uint32_t number_of_arguments,
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001602 Primitive::Type return_type,
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001603 uint32_t dex_pc,
1604 uint32_t index_in_dex_cache)
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001605 : HInvoke(arena, number_of_arguments, return_type, dex_pc),
1606 index_in_dex_cache_(index_in_dex_cache) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001607
1608 uint32_t GetIndexInDexCache() const { return index_in_dex_cache_; }
1609
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001610 DECLARE_INSTRUCTION(InvokeStatic);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001611
1612 private:
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001613 const uint32_t index_in_dex_cache_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001614
1615 DISALLOW_COPY_AND_ASSIGN(HInvokeStatic);
1616};
1617
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001618class HInvokeVirtual : public HInvoke {
1619 public:
1620 HInvokeVirtual(ArenaAllocator* arena,
1621 uint32_t number_of_arguments,
1622 Primitive::Type return_type,
1623 uint32_t dex_pc,
1624 uint32_t vtable_index)
1625 : HInvoke(arena, number_of_arguments, return_type, dex_pc),
1626 vtable_index_(vtable_index) {}
1627
1628 uint32_t GetVTableIndex() const { return vtable_index_; }
1629
1630 DECLARE_INSTRUCTION(InvokeVirtual);
1631
1632 private:
1633 const uint32_t vtable_index_;
1634
1635 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
1636};
1637
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001638class HInvokeInterface : public HInvoke {
1639 public:
1640 HInvokeInterface(ArenaAllocator* arena,
1641 uint32_t number_of_arguments,
1642 Primitive::Type return_type,
1643 uint32_t dex_pc,
1644 uint32_t dex_method_index,
1645 uint32_t imt_index)
1646 : HInvoke(arena, number_of_arguments, return_type, dex_pc),
1647 dex_method_index_(dex_method_index),
1648 imt_index_(imt_index) {}
1649
1650 uint32_t GetImtIndex() const { return imt_index_; }
1651 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
1652
1653 DECLARE_INSTRUCTION(InvokeInterface);
1654
1655 private:
1656 const uint32_t dex_method_index_;
1657 const uint32_t imt_index_;
1658
1659 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
1660};
1661
Dave Allison20dfc792014-06-16 20:44:29 -07001662class HNewInstance : public HExpression<0> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001663 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001664 HNewInstance(uint32_t dex_pc, uint16_t type_index)
1665 : HExpression(Primitive::kPrimNot, SideEffects::None()),
1666 dex_pc_(dex_pc),
1667 type_index_(type_index) {}
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001668
1669 uint32_t GetDexPc() const { return dex_pc_; }
1670 uint16_t GetTypeIndex() const { return type_index_; }
1671
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001672 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00001673 bool NeedsEnvironment() const OVERRIDE { return true; }
1674 // It may throw when called on:
1675 // - interfaces
1676 // - abstract/innaccessible/unknown classes
1677 // TODO: optimize when possible.
1678 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001679
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001680 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001681
1682 private:
1683 const uint32_t dex_pc_;
1684 const uint16_t type_index_;
1685
1686 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
1687};
1688
Roland Levillain88cb1752014-10-20 16:36:47 +01001689class HNeg : public HUnaryOperation {
1690 public:
1691 explicit HNeg(Primitive::Type result_type, HInstruction* input)
1692 : HUnaryOperation(result_type, input) {}
1693
Roland Levillain9240d6a2014-10-20 16:47:04 +01001694 virtual int32_t Evaluate(int32_t x) const OVERRIDE { return -x; }
1695 virtual int64_t Evaluate(int64_t x) const OVERRIDE { return -x; }
1696
Roland Levillain88cb1752014-10-20 16:36:47 +01001697 DECLARE_INSTRUCTION(Neg);
1698
1699 private:
1700 DISALLOW_COPY_AND_ASSIGN(HNeg);
1701};
1702
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001703class HNewArray : public HExpression<1> {
1704 public:
1705 HNewArray(HInstruction* length, uint32_t dex_pc, uint16_t type_index)
1706 : HExpression(Primitive::kPrimNot, SideEffects::None()),
1707 dex_pc_(dex_pc),
1708 type_index_(type_index) {
1709 SetRawInputAt(0, length);
1710 }
1711
1712 uint32_t GetDexPc() const { return dex_pc_; }
1713 uint16_t GetTypeIndex() const { return type_index_; }
1714
1715 // Calls runtime so needs an environment.
1716 virtual bool NeedsEnvironment() const { return true; }
1717
1718 DECLARE_INSTRUCTION(NewArray);
1719
1720 private:
1721 const uint32_t dex_pc_;
1722 const uint16_t type_index_;
1723
1724 DISALLOW_COPY_AND_ASSIGN(HNewArray);
1725};
1726
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001727class HAdd : public HBinaryOperation {
1728 public:
1729 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1730 : HBinaryOperation(result_type, left, right) {}
1731
1732 virtual bool IsCommutative() { return true; }
1733
Roland Levillain93445682014-10-06 19:24:02 +01001734 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1735 return x + y;
1736 }
1737 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1738 return x + y;
1739 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001740
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001741 DECLARE_INSTRUCTION(Add);
1742
1743 private:
1744 DISALLOW_COPY_AND_ASSIGN(HAdd);
1745};
1746
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001747class HSub : public HBinaryOperation {
1748 public:
1749 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1750 : HBinaryOperation(result_type, left, right) {}
1751
Roland Levillain93445682014-10-06 19:24:02 +01001752 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1753 return x - y;
1754 }
1755 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1756 return x - y;
1757 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001758
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001759 DECLARE_INSTRUCTION(Sub);
1760
1761 private:
1762 DISALLOW_COPY_AND_ASSIGN(HSub);
1763};
1764
Calin Juravle34bacdf2014-10-07 20:23:36 +01001765class HMul : public HBinaryOperation {
1766 public:
1767 HMul(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1768 : HBinaryOperation(result_type, left, right) {}
1769
1770 virtual bool IsCommutative() { return true; }
1771
1772 virtual int32_t Evaluate(int32_t x, int32_t y) const { return x * y; }
1773 virtual int64_t Evaluate(int64_t x, int64_t y) const { return x * y; }
1774
1775 DECLARE_INSTRUCTION(Mul);
1776
1777 private:
1778 DISALLOW_COPY_AND_ASSIGN(HMul);
1779};
1780
Calin Juravle7c4954d2014-10-28 16:57:40 +00001781class HDiv : public HBinaryOperation {
1782 public:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001783 HDiv(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
1784 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00001785
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00001786 virtual int32_t Evaluate(int32_t x, int32_t y) const {
1787 // Our graph structure ensures we never have 0 for `y` during constant folding.
1788 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00001789 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00001790 return (y == -1) ? -x : x / y;
1791 }
Calin Juravlebacfec32014-11-14 15:54:36 +00001792
1793 virtual int64_t Evaluate(int64_t x, int64_t y) const {
1794 DCHECK_NE(y, 0);
1795 // Special case -1 to avoid getting a SIGFPE on x86(_64).
1796 return (y == -1) ? -x : x / y;
1797 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00001798
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001799 uint32_t GetDexPc() const { return dex_pc_; }
1800
Calin Juravle7c4954d2014-10-28 16:57:40 +00001801 DECLARE_INSTRUCTION(Div);
1802
1803 private:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001804 const uint32_t dex_pc_;
1805
Calin Juravle7c4954d2014-10-28 16:57:40 +00001806 DISALLOW_COPY_AND_ASSIGN(HDiv);
1807};
1808
Calin Juravlebacfec32014-11-14 15:54:36 +00001809class HRem : public HBinaryOperation {
1810 public:
1811 HRem(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
1812 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
1813
1814 virtual int32_t Evaluate(int32_t x, int32_t y) const {
1815 DCHECK_NE(y, 0);
1816 // Special case -1 to avoid getting a SIGFPE on x86(_64).
1817 return (y == -1) ? 0 : x % y;
1818 }
1819
1820 virtual int64_t Evaluate(int64_t x, int64_t y) const {
1821 DCHECK_NE(y, 0);
1822 // Special case -1 to avoid getting a SIGFPE on x86(_64).
1823 return (y == -1) ? 0 : x % y;
1824 }
1825
1826 uint32_t GetDexPc() const { return dex_pc_; }
1827
1828 DECLARE_INSTRUCTION(Rem);
1829
1830 private:
1831 const uint32_t dex_pc_;
1832
1833 DISALLOW_COPY_AND_ASSIGN(HRem);
1834};
1835
Calin Juravled0d48522014-11-04 16:40:20 +00001836class HDivZeroCheck : public HExpression<1> {
1837 public:
1838 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
1839 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
1840 SetRawInputAt(0, value);
1841 }
1842
1843 bool CanBeMoved() const OVERRIDE { return true; }
1844
1845 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
1846 UNUSED(other);
1847 return true;
1848 }
1849
1850 bool NeedsEnvironment() const OVERRIDE { return true; }
1851 bool CanThrow() const OVERRIDE { return true; }
1852
1853 uint32_t GetDexPc() const { return dex_pc_; }
1854
1855 DECLARE_INSTRUCTION(DivZeroCheck);
1856
1857 private:
1858 const uint32_t dex_pc_;
1859
1860 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
1861};
1862
Calin Juravle9aec02f2014-11-18 23:06:35 +00001863class HShl : public HBinaryOperation {
1864 public:
1865 HShl(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1866 : HBinaryOperation(result_type, left, right) {}
1867
1868 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x << (y & kMaxIntShiftValue); }
1869 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x << (y & kMaxLongShiftValue); }
1870
1871 DECLARE_INSTRUCTION(Shl);
1872
1873 private:
1874 DISALLOW_COPY_AND_ASSIGN(HShl);
1875};
1876
1877class HShr : public HBinaryOperation {
1878 public:
1879 HShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1880 : HBinaryOperation(result_type, left, right) {}
1881
1882 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x >> (y & kMaxIntShiftValue); }
1883 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x >> (y & kMaxLongShiftValue); }
1884
1885 DECLARE_INSTRUCTION(Shr);
1886
1887 private:
1888 DISALLOW_COPY_AND_ASSIGN(HShr);
1889};
1890
1891class HUShr : public HBinaryOperation {
1892 public:
1893 HUShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1894 : HBinaryOperation(result_type, left, right) {}
1895
1896 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1897 uint32_t ux = static_cast<uint32_t>(x);
1898 uint32_t uy = static_cast<uint32_t>(y) & kMaxIntShiftValue;
1899 return static_cast<int32_t>(ux >> uy);
1900 }
1901
1902 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1903 uint64_t ux = static_cast<uint64_t>(x);
1904 uint64_t uy = static_cast<uint64_t>(y) & kMaxLongShiftValue;
1905 return static_cast<int64_t>(ux >> uy);
1906 }
1907
1908 DECLARE_INSTRUCTION(UShr);
1909
1910 private:
1911 DISALLOW_COPY_AND_ASSIGN(HUShr);
1912};
1913
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001914class HAnd : public HBinaryOperation {
1915 public:
1916 HAnd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1917 : HBinaryOperation(result_type, left, right) {}
1918
1919 bool IsCommutative() OVERRIDE { return true; }
1920
1921 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x & y; }
1922 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x & y; }
1923
1924 DECLARE_INSTRUCTION(And);
1925
1926 private:
1927 DISALLOW_COPY_AND_ASSIGN(HAnd);
1928};
1929
1930class HOr : public HBinaryOperation {
1931 public:
1932 HOr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1933 : HBinaryOperation(result_type, left, right) {}
1934
1935 bool IsCommutative() OVERRIDE { return true; }
1936
1937 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x | y; }
1938 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x | y; }
1939
1940 DECLARE_INSTRUCTION(Or);
1941
1942 private:
1943 DISALLOW_COPY_AND_ASSIGN(HOr);
1944};
1945
1946class HXor : public HBinaryOperation {
1947 public:
1948 HXor(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1949 : HBinaryOperation(result_type, left, right) {}
1950
1951 bool IsCommutative() OVERRIDE { return true; }
1952
1953 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x ^ y; }
1954 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x ^ y; }
1955
1956 DECLARE_INSTRUCTION(Xor);
1957
1958 private:
1959 DISALLOW_COPY_AND_ASSIGN(HXor);
1960};
1961
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001962// The value of a parameter in this method. Its location depends on
1963// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07001964class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001965 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001966 HParameterValue(uint8_t index, Primitive::Type parameter_type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001967 : HExpression(parameter_type, SideEffects::None()), index_(index) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001968
1969 uint8_t GetIndex() const { return index_; }
1970
1971 DECLARE_INSTRUCTION(ParameterValue);
1972
1973 private:
1974 // The index of this parameter in the parameters list. Must be less
1975 // than HGraph::number_of_in_vregs_;
1976 const uint8_t index_;
1977
1978 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
1979};
1980
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001981class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001982 public:
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001983 explicit HNot(Primitive::Type result_type, HInstruction* input)
1984 : HUnaryOperation(result_type, input) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001985
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001986 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001987 virtual bool InstructionDataEquals(HInstruction* other) const {
1988 UNUSED(other);
1989 return true;
1990 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001991
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001992 virtual int32_t Evaluate(int32_t x) const OVERRIDE { return ~x; }
1993 virtual int64_t Evaluate(int64_t x) const OVERRIDE { return ~x; }
1994
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001995 DECLARE_INSTRUCTION(Not);
1996
1997 private:
1998 DISALLOW_COPY_AND_ASSIGN(HNot);
1999};
2000
Roland Levillaindff1f282014-11-05 14:15:05 +00002001class HTypeConversion : public HExpression<1> {
2002 public:
2003 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00002004 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
2005 : HExpression(result_type, SideEffects::None()), dex_pc_(dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002006 SetRawInputAt(0, input);
2007 DCHECK_NE(input->GetType(), result_type);
2008 }
2009
2010 HInstruction* GetInput() const { return InputAt(0); }
2011 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
2012 Primitive::Type GetResultType() const { return GetType(); }
2013
Roland Levillain624279f2014-12-04 11:54:28 +00002014 // Required by the x86 and ARM code generators when producing calls
2015 // to the runtime.
2016 uint32_t GetDexPc() const { return dex_pc_; }
2017
Roland Levillaindff1f282014-11-05 14:15:05 +00002018 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00002019 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00002020
2021 DECLARE_INSTRUCTION(TypeConversion);
2022
2023 private:
Roland Levillain624279f2014-12-04 11:54:28 +00002024 const uint32_t dex_pc_;
2025
Roland Levillaindff1f282014-11-05 14:15:05 +00002026 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
2027};
2028
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002029class HPhi : public HInstruction {
2030 public:
2031 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002032 : HInstruction(SideEffects::None()),
2033 inputs_(arena, number_of_inputs),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002034 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002035 type_(type),
2036 is_live_(false) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002037 inputs_.SetSize(number_of_inputs);
2038 }
2039
2040 virtual size_t InputCount() const { return inputs_.Size(); }
2041 virtual HInstruction* InputAt(size_t i) const { return inputs_.Get(i); }
2042
2043 virtual void SetRawInputAt(size_t index, HInstruction* input) {
2044 inputs_.Put(index, input);
2045 }
2046
2047 void AddInput(HInstruction* input);
2048
2049 virtual Primitive::Type GetType() const { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002050 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002051
2052 uint32_t GetRegNumber() const { return reg_number_; }
2053
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002054 void SetDead() { is_live_ = false; }
2055 void SetLive() { is_live_ = true; }
2056 bool IsDead() const { return !is_live_; }
2057 bool IsLive() const { return is_live_; }
2058
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002059 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002060
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002061 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002062 GrowableArray<HInstruction*> inputs_;
2063 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002064 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002065 bool is_live_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002066
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002067 DISALLOW_COPY_AND_ASSIGN(HPhi);
2068};
2069
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002070class HNullCheck : public HExpression<1> {
2071 public:
2072 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002073 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002074 SetRawInputAt(0, value);
2075 }
2076
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002077 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002078 virtual bool InstructionDataEquals(HInstruction* other) const {
2079 UNUSED(other);
2080 return true;
2081 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002082
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002083 virtual bool NeedsEnvironment() const { return true; }
2084
Roland Levillaine161a2a2014-10-03 12:45:18 +01002085 virtual bool CanThrow() const { return true; }
2086
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002087 uint32_t GetDexPc() const { return dex_pc_; }
2088
2089 DECLARE_INSTRUCTION(NullCheck);
2090
2091 private:
2092 const uint32_t dex_pc_;
2093
2094 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
2095};
2096
2097class FieldInfo : public ValueObject {
2098 public:
Roland Levillain5799fc02014-09-25 12:15:20 +01002099 FieldInfo(MemberOffset field_offset, Primitive::Type field_type)
Nicolas Geoffray39468442014-09-02 15:17:15 +01002100 : field_offset_(field_offset), field_type_(field_type) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002101
2102 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002103 Primitive::Type GetFieldType() const { return field_type_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002104
2105 private:
2106 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01002107 const Primitive::Type field_type_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002108};
2109
2110class HInstanceFieldGet : public HExpression<1> {
2111 public:
2112 HInstanceFieldGet(HInstruction* value,
2113 Primitive::Type field_type,
2114 MemberOffset field_offset)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002115 : HExpression(field_type, SideEffects::DependsOnSomething()),
2116 field_info_(field_offset, field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002117 SetRawInputAt(0, value);
2118 }
2119
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002120 virtual bool CanBeMoved() const { return true; }
2121 virtual bool InstructionDataEquals(HInstruction* other) const {
2122 size_t other_offset = other->AsInstanceFieldGet()->GetFieldOffset().SizeValue();
2123 return other_offset == GetFieldOffset().SizeValue();
2124 }
2125
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002126 virtual size_t ComputeHashCode() const {
2127 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
2128 }
2129
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002130 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002131 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002132
2133 DECLARE_INSTRUCTION(InstanceFieldGet);
2134
2135 private:
2136 const FieldInfo field_info_;
2137
2138 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
2139};
2140
2141class HInstanceFieldSet : public HTemplateInstruction<2> {
2142 public:
2143 HInstanceFieldSet(HInstruction* object,
2144 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01002145 Primitive::Type field_type,
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002146 MemberOffset field_offset)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002147 : HTemplateInstruction(SideEffects::ChangesSomething()),
2148 field_info_(field_offset, field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002149 SetRawInputAt(0, object);
2150 SetRawInputAt(1, value);
2151 }
2152
2153 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002154 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002155
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002156 HInstruction* GetValue() const { return InputAt(1); }
2157
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002158 DECLARE_INSTRUCTION(InstanceFieldSet);
2159
2160 private:
2161 const FieldInfo field_info_;
2162
2163 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
2164};
2165
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002166class HArrayGet : public HExpression<2> {
2167 public:
2168 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002169 : HExpression(type, SideEffects::DependsOnSomething()) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002170 SetRawInputAt(0, array);
2171 SetRawInputAt(1, index);
2172 }
2173
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002174 bool CanBeMoved() const OVERRIDE { return true; }
2175 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002176 UNUSED(other);
2177 return true;
2178 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002179 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002180
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002181 HInstruction* GetArray() const { return InputAt(0); }
2182 HInstruction* GetIndex() const { return InputAt(1); }
2183
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002184 DECLARE_INSTRUCTION(ArrayGet);
2185
2186 private:
2187 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
2188};
2189
2190class HArraySet : public HTemplateInstruction<3> {
2191 public:
2192 HArraySet(HInstruction* array,
2193 HInstruction* index,
2194 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002195 Primitive::Type expected_component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002196 uint32_t dex_pc)
2197 : HTemplateInstruction(SideEffects::ChangesSomething()),
2198 dex_pc_(dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002199 expected_component_type_(expected_component_type),
2200 needs_type_check_(value->GetType() == Primitive::kPrimNot) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002201 SetRawInputAt(0, array);
2202 SetRawInputAt(1, index);
2203 SetRawInputAt(2, value);
2204 }
2205
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002206 bool NeedsEnvironment() const {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002207 // We currently always call a runtime method to catch array store
2208 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002209 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002210 }
2211
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002212 void ClearNeedsTypeCheck() {
2213 needs_type_check_ = false;
2214 }
2215
2216 bool NeedsTypeCheck() const { return needs_type_check_; }
2217
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002218 uint32_t GetDexPc() const { return dex_pc_; }
2219
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002220 HInstruction* GetArray() const { return InputAt(0); }
2221 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002222 HInstruction* GetValue() const { return InputAt(2); }
2223
2224 Primitive::Type GetComponentType() const {
2225 // The Dex format does not type floating point index operations. Since the
2226 // `expected_component_type_` is set during building and can therefore not
2227 // be correct, we also check what is the value type. If it is a floating
2228 // point type, we must use that type.
2229 Primitive::Type value_type = GetValue()->GetType();
2230 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
2231 ? value_type
2232 : expected_component_type_;
2233 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002234
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002235 DECLARE_INSTRUCTION(ArraySet);
2236
2237 private:
2238 const uint32_t dex_pc_;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002239 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002240 bool needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002241
2242 DISALLOW_COPY_AND_ASSIGN(HArraySet);
2243};
2244
2245class HArrayLength : public HExpression<1> {
2246 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002247 explicit HArrayLength(HInstruction* array)
2248 : HExpression(Primitive::kPrimInt, SideEffects::None()) {
2249 // Note that arrays do not change length, so the instruction does not
2250 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002251 SetRawInputAt(0, array);
2252 }
2253
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002254 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002255 virtual bool InstructionDataEquals(HInstruction* other) const {
2256 UNUSED(other);
2257 return true;
2258 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002259
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002260 DECLARE_INSTRUCTION(ArrayLength);
2261
2262 private:
2263 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
2264};
2265
2266class HBoundsCheck : public HExpression<2> {
2267 public:
2268 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002269 : HExpression(index->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002270 DCHECK(index->GetType() == Primitive::kPrimInt);
2271 SetRawInputAt(0, index);
2272 SetRawInputAt(1, length);
2273 }
2274
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002275 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002276 virtual bool InstructionDataEquals(HInstruction* other) const {
2277 UNUSED(other);
2278 return true;
2279 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002280
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002281 virtual bool NeedsEnvironment() const { return true; }
2282
Roland Levillaine161a2a2014-10-03 12:45:18 +01002283 virtual bool CanThrow() const { return true; }
2284
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002285 uint32_t GetDexPc() const { return dex_pc_; }
2286
2287 DECLARE_INSTRUCTION(BoundsCheck);
2288
2289 private:
2290 const uint32_t dex_pc_;
2291
2292 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
2293};
2294
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002295/**
2296 * Some DEX instructions are folded into multiple HInstructions that need
2297 * to stay live until the last HInstruction. This class
2298 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00002299 * HInstruction stays live. `index` represents the stack location index of the
2300 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002301 */
2302class HTemporary : public HTemplateInstruction<0> {
2303 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002304 explicit HTemporary(size_t index) : HTemplateInstruction(SideEffects::None()), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002305
2306 size_t GetIndex() const { return index_; }
2307
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00002308 Primitive::Type GetType() const OVERRIDE {
2309 // The previous instruction is the one that will be stored in the temporary location.
2310 DCHECK(GetPrevious() != nullptr);
2311 return GetPrevious()->GetType();
2312 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00002313
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002314 DECLARE_INSTRUCTION(Temporary);
2315
2316 private:
2317 const size_t index_;
2318
2319 DISALLOW_COPY_AND_ASSIGN(HTemporary);
2320};
2321
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002322class HSuspendCheck : public HTemplateInstruction<0> {
2323 public:
2324 explicit HSuspendCheck(uint32_t dex_pc)
Nicolas Geoffray9ebc72c2014-09-25 16:33:42 +01002325 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002326
2327 virtual bool NeedsEnvironment() const {
2328 return true;
2329 }
2330
2331 uint32_t GetDexPc() const { return dex_pc_; }
2332
2333 DECLARE_INSTRUCTION(SuspendCheck);
2334
2335 private:
2336 const uint32_t dex_pc_;
2337
2338 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
2339};
2340
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002341/**
2342 * Instruction to load a Class object.
2343 */
2344class HLoadClass : public HExpression<0> {
2345 public:
2346 HLoadClass(uint16_t type_index,
2347 bool is_referrers_class,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002348 uint32_t dex_pc)
2349 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2350 type_index_(type_index),
2351 is_referrers_class_(is_referrers_class),
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002352 dex_pc_(dex_pc),
2353 generate_clinit_check_(false) {}
2354
2355 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002356
2357 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2358 return other->AsLoadClass()->type_index_ == type_index_;
2359 }
2360
2361 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
2362
2363 uint32_t GetDexPc() const { return dex_pc_; }
2364 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002365 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002366
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002367 bool NeedsEnvironment() const OVERRIDE {
2368 // Will call runtime and load the class if the class is not loaded yet.
2369 // TODO: finer grain decision.
2370 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002371 }
2372
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002373 bool MustGenerateClinitCheck() const {
2374 return generate_clinit_check_;
2375 }
2376
2377 void SetMustGenerateClinitCheck() {
2378 generate_clinit_check_ = true;
2379 }
2380
2381 bool CanCallRuntime() const {
2382 return MustGenerateClinitCheck() || !is_referrers_class_;
2383 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002384
2385 DECLARE_INSTRUCTION(LoadClass);
2386
2387 private:
2388 const uint16_t type_index_;
2389 const bool is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002390 const uint32_t dex_pc_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002391 // Whether this instruction must generate the initialization check.
2392 // Used for code generation.
2393 bool generate_clinit_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002394
2395 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
2396};
2397
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002398class HLoadString : public HExpression<0> {
2399 public:
2400 HLoadString(uint32_t string_index, uint32_t dex_pc)
2401 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2402 string_index_(string_index),
2403 dex_pc_(dex_pc) {}
2404
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002405 bool CanBeMoved() const OVERRIDE { return true; }
2406
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002407 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2408 return other->AsLoadString()->string_index_ == string_index_;
2409 }
2410
2411 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
2412
2413 uint32_t GetDexPc() const { return dex_pc_; }
2414 uint32_t GetStringIndex() const { return string_index_; }
2415
2416 // TODO: Can we deopt or debug when we resolve a string?
2417 bool NeedsEnvironment() const OVERRIDE { return false; }
2418
2419 DECLARE_INSTRUCTION(LoadString);
2420
2421 private:
2422 const uint32_t string_index_;
2423 const uint32_t dex_pc_;
2424
2425 DISALLOW_COPY_AND_ASSIGN(HLoadString);
2426};
2427
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002428// TODO: Pass this check to HInvokeStatic nodes.
2429/**
2430 * Performs an initialization check on its Class object input.
2431 */
2432class HClinitCheck : public HExpression<1> {
2433 public:
2434 explicit HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
2435 : HExpression(Primitive::kPrimNot, SideEffects::All()),
2436 dex_pc_(dex_pc) {
2437 SetRawInputAt(0, constant);
2438 }
2439
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002440 bool CanBeMoved() const OVERRIDE { return true; }
2441 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2442 UNUSED(other);
2443 return true;
2444 }
2445
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002446 bool NeedsEnvironment() const OVERRIDE {
2447 // May call runtime to initialize the class.
2448 return true;
2449 }
2450
2451 uint32_t GetDexPc() const { return dex_pc_; }
2452
2453 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
2454
2455 DECLARE_INSTRUCTION(ClinitCheck);
2456
2457 private:
2458 const uint32_t dex_pc_;
2459
2460 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
2461};
2462
2463class HStaticFieldGet : public HExpression<1> {
2464 public:
2465 HStaticFieldGet(HInstruction* cls,
2466 Primitive::Type field_type,
2467 MemberOffset field_offset)
2468 : HExpression(field_type, SideEffects::DependsOnSomething()),
2469 field_info_(field_offset, field_type) {
2470 SetRawInputAt(0, cls);
2471 }
2472
2473 bool CanBeMoved() const OVERRIDE { return true; }
2474 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2475 size_t other_offset = other->AsStaticFieldGet()->GetFieldOffset().SizeValue();
2476 return other_offset == GetFieldOffset().SizeValue();
2477 }
2478
2479 size_t ComputeHashCode() const OVERRIDE {
2480 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
2481 }
2482
2483 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
2484 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
2485
2486 DECLARE_INSTRUCTION(StaticFieldGet);
2487
2488 private:
2489 const FieldInfo field_info_;
2490
2491 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
2492};
2493
2494class HStaticFieldSet : public HTemplateInstruction<2> {
2495 public:
2496 HStaticFieldSet(HInstruction* cls,
2497 HInstruction* value,
2498 Primitive::Type field_type,
2499 MemberOffset field_offset)
2500 : HTemplateInstruction(SideEffects::ChangesSomething()),
2501 field_info_(field_offset, field_type) {
2502 SetRawInputAt(0, cls);
2503 SetRawInputAt(1, value);
2504 }
2505
2506 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
2507 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
2508
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002509 HInstruction* GetValue() const { return InputAt(1); }
2510
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002511 DECLARE_INSTRUCTION(StaticFieldSet);
2512
2513 private:
2514 const FieldInfo field_info_;
2515
2516 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
2517};
2518
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002519// Implement the move-exception DEX instruction.
2520class HLoadException : public HExpression<0> {
2521 public:
2522 HLoadException() : HExpression(Primitive::kPrimNot, SideEffects::None()) {}
2523
2524 DECLARE_INSTRUCTION(LoadException);
2525
2526 private:
2527 DISALLOW_COPY_AND_ASSIGN(HLoadException);
2528};
2529
2530class HThrow : public HTemplateInstruction<1> {
2531 public:
2532 HThrow(HInstruction* exception, uint32_t dex_pc)
2533 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {
2534 SetRawInputAt(0, exception);
2535 }
2536
2537 bool IsControlFlow() const OVERRIDE { return true; }
2538
2539 bool NeedsEnvironment() const OVERRIDE { return true; }
2540
2541 uint32_t GetDexPc() const { return dex_pc_; }
2542
2543 DECLARE_INSTRUCTION(Throw);
2544
2545 private:
2546 uint32_t dex_pc_;
2547
2548 DISALLOW_COPY_AND_ASSIGN(HThrow);
2549};
2550
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002551class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002552 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002553 HInstanceOf(HInstruction* object,
2554 HLoadClass* constant,
2555 bool class_is_final,
2556 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002557 : HExpression(Primitive::kPrimBoolean, SideEffects::None()),
2558 class_is_final_(class_is_final),
2559 dex_pc_(dex_pc) {
2560 SetRawInputAt(0, object);
2561 SetRawInputAt(1, constant);
2562 }
2563
2564 bool CanBeMoved() const OVERRIDE { return true; }
2565
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002566 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002567 return true;
2568 }
2569
2570 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002571 return false;
2572 }
2573
2574 uint32_t GetDexPc() const { return dex_pc_; }
2575
2576 bool IsClassFinal() const { return class_is_final_; }
2577
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002578 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002579
2580 private:
2581 const bool class_is_final_;
2582 const uint32_t dex_pc_;
2583
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002584 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
2585};
2586
2587class HCheckCast : public HTemplateInstruction<2> {
2588 public:
2589 HCheckCast(HInstruction* object,
2590 HLoadClass* constant,
2591 bool class_is_final,
2592 uint32_t dex_pc)
2593 : HTemplateInstruction(SideEffects::None()),
2594 class_is_final_(class_is_final),
2595 dex_pc_(dex_pc) {
2596 SetRawInputAt(0, object);
2597 SetRawInputAt(1, constant);
2598 }
2599
2600 bool CanBeMoved() const OVERRIDE { return true; }
2601
2602 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2603 return true;
2604 }
2605
2606 bool NeedsEnvironment() const OVERRIDE {
2607 // Instruction may throw a CheckCastError.
2608 return true;
2609 }
2610
2611 bool CanThrow() const OVERRIDE { return true; }
2612
2613 uint32_t GetDexPc() const { return dex_pc_; }
2614
2615 bool IsClassFinal() const { return class_is_final_; }
2616
2617 DECLARE_INSTRUCTION(CheckCast);
2618
2619 private:
2620 const bool class_is_final_;
2621 const uint32_t dex_pc_;
2622
2623 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002624};
2625
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00002626class HMonitorOperation : public HTemplateInstruction<1> {
2627 public:
2628 enum OperationKind {
2629 kEnter,
2630 kExit,
2631 };
2632
2633 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
2634 : HTemplateInstruction(SideEffects::None()), kind_(kind), dex_pc_(dex_pc) {
2635 SetRawInputAt(0, object);
2636 }
2637
2638 // Instruction may throw a Java exception, so we need an environment.
2639 bool NeedsEnvironment() const OVERRIDE { return true; }
2640 bool CanThrow() const OVERRIDE { return true; }
2641
2642 uint32_t GetDexPc() const { return dex_pc_; }
2643
2644 bool IsEnter() const { return kind_ == kEnter; }
2645
2646 DECLARE_INSTRUCTION(MonitorOperation);
2647
2648 protected:
2649 const OperationKind kind_;
2650 const uint32_t dex_pc_;
2651
2652 private:
2653 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
2654};
2655
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002656
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002657class MoveOperands : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002658 public:
Nicolas Geoffray740475d2014-09-29 10:33:25 +01002659 MoveOperands(Location source, Location destination, HInstruction* instruction)
2660 : source_(source), destination_(destination), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002661
2662 Location GetSource() const { return source_; }
2663 Location GetDestination() const { return destination_; }
2664
2665 void SetSource(Location value) { source_ = value; }
2666 void SetDestination(Location value) { destination_ = value; }
2667
2668 // The parallel move resolver marks moves as "in-progress" by clearing the
2669 // destination (but not the source).
2670 Location MarkPending() {
2671 DCHECK(!IsPending());
2672 Location dest = destination_;
2673 destination_ = Location::NoLocation();
2674 return dest;
2675 }
2676
2677 void ClearPending(Location dest) {
2678 DCHECK(IsPending());
2679 destination_ = dest;
2680 }
2681
2682 bool IsPending() const {
2683 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
2684 return destination_.IsInvalid() && !source_.IsInvalid();
2685 }
2686
2687 // True if this blocks a move from the given location.
2688 bool Blocks(Location loc) const {
2689 return !IsEliminated() && source_.Equals(loc);
2690 }
2691
2692 // A move is redundant if it's been eliminated, if its source and
2693 // destination are the same, or if its destination is unneeded.
2694 bool IsRedundant() const {
2695 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
2696 }
2697
2698 // We clear both operands to indicate move that's been eliminated.
2699 void Eliminate() {
2700 source_ = destination_ = Location::NoLocation();
2701 }
2702
2703 bool IsEliminated() const {
2704 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
2705 return source_.IsInvalid();
2706 }
2707
Nicolas Geoffray740475d2014-09-29 10:33:25 +01002708 HInstruction* GetInstruction() const { return instruction_; }
2709
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002710 private:
2711 Location source_;
2712 Location destination_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01002713 // The instruction this move is assocatied with. Null when this move is
2714 // for moving an input in the expected locations of user (including a phi user).
2715 // This is only used in debug mode, to ensure we do not connect interval siblings
2716 // in the same parallel move.
2717 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002718
2719 DISALLOW_COPY_AND_ASSIGN(MoveOperands);
2720};
2721
2722static constexpr size_t kDefaultNumberOfMoves = 4;
2723
2724class HParallelMove : public HTemplateInstruction<0> {
2725 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002726 explicit HParallelMove(ArenaAllocator* arena)
2727 : HTemplateInstruction(SideEffects::None()), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002728
2729 void AddMove(MoveOperands* move) {
Nicolas Geoffray740475d2014-09-29 10:33:25 +01002730 if (kIsDebugBuild && move->GetInstruction() != nullptr) {
2731 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
2732 DCHECK_NE(moves_.Get(i)->GetInstruction(), move->GetInstruction())
2733 << "Doing parallel moves for the same instruction.";
2734 }
2735 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002736 moves_.Add(move);
2737 }
2738
2739 MoveOperands* MoveOperandsAt(size_t index) const {
2740 return moves_.Get(index);
2741 }
2742
2743 size_t NumMoves() const { return moves_.Size(); }
2744
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002745 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002746
2747 private:
2748 GrowableArray<MoveOperands*> moves_;
2749
2750 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
2751};
2752
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002753class HGraphVisitor : public ValueObject {
2754 public:
Dave Allison20dfc792014-06-16 20:44:29 -07002755 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
2756 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002757
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002758 virtual void VisitInstruction(HInstruction* instruction) { UNUSED(instruction); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002759 virtual void VisitBasicBlock(HBasicBlock* block);
2760
Roland Levillain633021e2014-10-01 14:12:25 +01002761 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002762 void VisitInsertionOrder();
2763
Roland Levillain633021e2014-10-01 14:12:25 +01002764 // Visit the graph following dominator tree reverse post-order.
2765 void VisitReversePostOrder();
2766
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002767 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002768
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002769 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002770#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002771 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
2772
2773 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
2774
2775#undef DECLARE_VISIT_INSTRUCTION
2776
2777 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07002778 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002779
2780 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
2781};
2782
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002783class HGraphDelegateVisitor : public HGraphVisitor {
2784 public:
2785 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
2786 virtual ~HGraphDelegateVisitor() {}
2787
2788 // Visit functions that delegate to to super class.
2789#define DECLARE_VISIT_INSTRUCTION(name, super) \
2790 virtual void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
2791
2792 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
2793
2794#undef DECLARE_VISIT_INSTRUCTION
2795
2796 private:
2797 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
2798};
2799
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002800class HInsertionOrderIterator : public ValueObject {
2801 public:
2802 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
2803
2804 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
2805 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
2806 void Advance() { ++index_; }
2807
2808 private:
2809 const HGraph& graph_;
2810 size_t index_;
2811
2812 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
2813};
2814
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002815class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002816 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002817 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002818
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002819 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
2820 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002821 void Advance() { ++index_; }
2822
2823 private:
2824 const HGraph& graph_;
2825 size_t index_;
2826
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002827 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002828};
2829
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002830class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002831 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002832 explicit HPostOrderIterator(const HGraph& graph)
2833 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002834
2835 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002836 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002837 void Advance() { --index_; }
2838
2839 private:
2840 const HGraph& graph_;
2841 size_t index_;
2842
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002843 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002844};
2845
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002846} // namespace art
2847
2848#endif // ART_COMPILER_OPTIMIZING_NODES_H_