blob: f562113e6ef6902906da2ebf44216c311ff89e40 [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 Geoffray622d9c32014-05-12 16:11:02 +0100115 // Find all natural loops in this graph. Aborts computation and returns false
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100116 // if one loop is not natural, that is the header does not dominate the back
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100117 // edge.
118 bool FindNaturalLoops() const;
119
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);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000176 void RemoveDeadBlocks(const ArenaBitVector& visited) const;
177
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000178 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000179
180 // List of blocks in insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000181 GrowableArray<HBasicBlock*> blocks_;
182
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100183 // List of blocks to perform a reverse post order tree traversal.
184 GrowableArray<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000185
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000186 HBasicBlock* entry_block_;
187 HBasicBlock* exit_block_;
188
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100189 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100190 uint16_t maximum_number_of_out_vregs_;
191
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100192 // The number of virtual registers in this method. Contains the parameters.
193 uint16_t number_of_vregs_;
194
195 // The number of virtual registers used by parameters of this method.
196 uint16_t number_of_in_vregs_;
197
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000198 // Number of vreg size slots that the temporaries use (used in baseline compiler).
199 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100200
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000201 // The current id to assign to a newly added instruction. See HInstruction.id_.
202 int current_instruction_id_;
203
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000204 DISALLOW_COPY_AND_ASSIGN(HGraph);
205};
206
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700207class HLoopInformation : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000208 public:
209 HLoopInformation(HBasicBlock* header, HGraph* graph)
210 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100211 suspend_check_(nullptr),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100212 back_edges_(graph->GetArena(), kDefaultNumberOfBackEdges),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100213 // Make bit vector growable, as the number of blocks may change.
214 blocks_(graph->GetArena(), graph->GetBlocks().Size(), true) {}
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100215
216 HBasicBlock* GetHeader() const {
217 return header_;
218 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000219
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100220 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
221 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
222 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
223
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000224 void AddBackEdge(HBasicBlock* back_edge) {
225 back_edges_.Add(back_edge);
226 }
227
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100228 void RemoveBackEdge(HBasicBlock* back_edge) {
229 back_edges_.Delete(back_edge);
230 }
231
232 bool IsBackEdge(HBasicBlock* block) {
233 for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) {
234 if (back_edges_.Get(i) == block) return true;
235 }
236 return false;
237 }
238
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000239 size_t NumberOfBackEdges() const {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000240 return back_edges_.Size();
241 }
242
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100243 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100244
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100245 const GrowableArray<HBasicBlock*>& GetBackEdges() const {
246 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100247 }
248
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100249 void ClearBackEdges() {
250 back_edges_.Reset();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100251 }
252
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100253 // Find blocks that are part of this loop. Returns whether the loop is a natural loop,
254 // that is the header dominates the back edge.
255 bool Populate();
256
257 // Returns whether this loop information contains `block`.
258 // Note that this loop information *must* be populated before entering this function.
259 bool Contains(const HBasicBlock& block) const;
260
261 // Returns whether this loop information is an inner loop of `other`.
262 // Note that `other` *must* be populated before entering this function.
263 bool IsIn(const HLoopInformation& other) const;
264
265 const ArenaBitVector& GetBlocks() const { return blocks_; }
266
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000267 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100268 // Internal recursive implementation of `Populate`.
269 void PopulateRecursive(HBasicBlock* block);
270
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000271 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100272 HSuspendCheck* suspend_check_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000273 GrowableArray<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100274 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000275
276 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
277};
278
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100279static constexpr size_t kNoLifetime = -1;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100280static constexpr uint32_t kNoDexPc = -1;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100281
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000282// A block in a method. Contains the list of instructions represented
283// as a double linked list. Each block knows its predecessors and
284// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100285
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700286class HBasicBlock : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000287 public:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100288 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000289 : graph_(graph),
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000290 predecessors_(graph->GetArena(), kDefaultNumberOfPredecessors),
291 successors_(graph->GetArena(), kDefaultNumberOfSuccessors),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000292 loop_information_(nullptr),
293 dominator_(nullptr),
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100294 dominated_blocks_(graph->GetArena(), kDefaultNumberOfDominatedBlocks),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100295 block_id_(-1),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100296 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100297 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000298 lifetime_end_(kNoLifetime),
299 is_catch_block_(false) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000300
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100301 const GrowableArray<HBasicBlock*>& GetPredecessors() const {
302 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000303 }
304
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100305 const GrowableArray<HBasicBlock*>& GetSuccessors() const {
306 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000307 }
308
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100309 const GrowableArray<HBasicBlock*>& GetDominatedBlocks() const {
310 return dominated_blocks_;
311 }
312
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100313 bool IsEntryBlock() const {
314 return graph_->GetEntryBlock() == this;
315 }
316
317 bool IsExitBlock() const {
318 return graph_->GetExitBlock() == this;
319 }
320
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000321 void AddBackEdge(HBasicBlock* back_edge) {
322 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000323 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000324 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100325 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000326 loop_information_->AddBackEdge(back_edge);
327 }
328
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000329 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000330
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000331 int GetBlockId() const { return block_id_; }
332 void SetBlockId(int id) { block_id_ = id; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000333
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000334 HBasicBlock* GetDominator() const { return dominator_; }
335 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100336 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.Add(block); }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000337
338 int NumberOfBackEdges() const {
339 return loop_information_ == nullptr
340 ? 0
341 : loop_information_->NumberOfBackEdges();
342 }
343
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100344 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
345 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100346 const HInstructionList& GetInstructions() const { return instructions_; }
347 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100348 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000349
350 void AddSuccessor(HBasicBlock* block) {
351 successors_.Add(block);
352 block->predecessors_.Add(this);
353 }
354
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100355 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
356 size_t successor_index = GetSuccessorIndexOf(existing);
357 DCHECK_NE(successor_index, static_cast<size_t>(-1));
358 existing->RemovePredecessor(this);
359 new_block->predecessors_.Add(this);
360 successors_.Put(successor_index, new_block);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000361 }
362
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100363 void RemovePredecessor(HBasicBlock* block) {
364 predecessors_.Delete(block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100365 }
366
367 void ClearAllPredecessors() {
368 predecessors_.Reset();
369 }
370
371 void AddPredecessor(HBasicBlock* block) {
372 predecessors_.Add(block);
373 block->successors_.Add(this);
374 }
375
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100376 void SwapPredecessors() {
Nicolas Geoffrayc83d4412014-09-18 16:46:20 +0100377 DCHECK_EQ(predecessors_.Size(), 2u);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100378 HBasicBlock* temp = predecessors_.Get(0);
379 predecessors_.Put(0, predecessors_.Get(1));
380 predecessors_.Put(1, temp);
381 }
382
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100383 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) {
384 for (size_t i = 0, e = predecessors_.Size(); i < e; ++i) {
385 if (predecessors_.Get(i) == predecessor) {
386 return i;
387 }
388 }
389 return -1;
390 }
391
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100392 size_t GetSuccessorIndexOf(HBasicBlock* successor) {
393 for (size_t i = 0, e = successors_.Size(); i < e; ++i) {
394 if (successors_.Get(i) == successor) {
395 return i;
396 }
397 }
398 return -1;
399 }
400
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000401 void AddInstruction(HInstruction* instruction);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100402 void RemoveInstruction(HInstruction* instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100403 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +0100404 // Replace instruction `initial` with `replacement` within this block.
405 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
406 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100407 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100408 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100409 void RemovePhi(HPhi* phi);
410
411 bool IsLoopHeader() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100412 return (loop_information_ != nullptr) && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100413 }
414
Roland Levillain6b879dd2014-09-22 17:13:44 +0100415 bool IsLoopPreHeaderFirstPredecessor() const {
416 DCHECK(IsLoopHeader());
417 DCHECK(!GetPredecessors().IsEmpty());
418 return GetPredecessors().Get(0) == GetLoopInformation()->GetPreHeader();
419 }
420
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100421 HLoopInformation* GetLoopInformation() const {
422 return loop_information_;
423 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000424
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100425 // Set the loop_information_ on this block. This method overrides the current
426 // loop_information if it is an outer loop of the passed loop information.
427 void SetInLoop(HLoopInformation* info) {
428 if (IsLoopHeader()) {
429 // Nothing to do. This just means `info` is an outer loop.
430 } else if (loop_information_ == nullptr) {
431 loop_information_ = info;
432 } else if (loop_information_->Contains(*info->GetHeader())) {
433 // Block is currently part of an outer loop. Make it part of this inner loop.
434 // Note that a non loop header having a loop information means this loop information
435 // has already been populated
436 loop_information_ = info;
437 } else {
438 // Block is part of an inner loop. Do not update the loop information.
439 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
440 // at this point, because this method is being called while populating `info`.
441 }
442 }
443
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100444 bool IsInLoop() const { return loop_information_ != nullptr; }
445
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100446 // Returns wheter this block dominates the blocked passed as parameter.
447 bool Dominates(HBasicBlock* block) const;
448
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100449 size_t GetLifetimeStart() const { return lifetime_start_; }
450 size_t GetLifetimeEnd() const { return lifetime_end_; }
451
452 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
453 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
454
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100455 uint32_t GetDexPc() const { return dex_pc_; }
456
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000457 bool IsCatchBlock() const { return is_catch_block_; }
458 void SetIsCatchBlock() { is_catch_block_ = true; }
459
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000460 private:
461 HGraph* const graph_;
462 GrowableArray<HBasicBlock*> predecessors_;
463 GrowableArray<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100464 HInstructionList instructions_;
465 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000466 HLoopInformation* loop_information_;
467 HBasicBlock* dominator_;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100468 GrowableArray<HBasicBlock*> dominated_blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000469 int block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100470 // The dex program counter of the first instruction of this block.
471 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100472 size_t lifetime_start_;
473 size_t lifetime_end_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000474 bool is_catch_block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000475
476 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
477};
478
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100479#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
480 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000481 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000482 M(ArrayGet, Instruction) \
483 M(ArrayLength, Instruction) \
484 M(ArraySet, Instruction) \
485 M(BoundsCheck, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000486 M(CheckCast, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100487 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000488 M(Compare, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100489 M(Condition, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000490 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +0000491 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000492 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100493 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000494 M(Exit, Instruction) \
495 M(FloatConstant, Constant) \
496 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100497 M(GreaterThan, Condition) \
498 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100499 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000500 M(InstanceFieldGet, Instruction) \
501 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000502 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100503 M(IntConstant, Constant) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +0000504 M(InvokeInterface, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100505 M(InvokeStatic, Invoke) \
506 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000507 M(LessThan, Condition) \
508 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000509 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000510 M(LoadException, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100511 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000512 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100513 M(Local, Instruction) \
514 M(LongConstant, Constant) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +0000515 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000516 M(Mul, BinaryOperation) \
517 M(Neg, UnaryOperation) \
518 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100519 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +0100520 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000521 M(NotEqual, Condition) \
522 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000523 M(Or, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100524 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000525 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100526 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000527 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100528 M(Return, Instruction) \
529 M(ReturnVoid, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000530 M(Shl, BinaryOperation) \
531 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100532 M(StaticFieldGet, Instruction) \
533 M(StaticFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100534 M(StoreLocal, Instruction) \
535 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100536 M(SuspendCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000537 M(Temporary, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000538 M(Throw, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +0000539 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000540 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000541 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000542
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100543#define FOR_EACH_INSTRUCTION(M) \
544 FOR_EACH_CONCRETE_INSTRUCTION(M) \
545 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +0100546 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +0100547 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100548 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -0700549
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100550#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000551FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
552#undef FORWARD_DECLARATION
553
Roland Levillainccc07a92014-09-16 14:48:16 +0100554#define DECLARE_INSTRUCTION(type) \
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100555 virtual InstructionKind GetKind() const { return k##type; } \
Roland Levillainccc07a92014-09-16 14:48:16 +0100556 virtual const char* DebugName() const { return #type; } \
557 virtual const H##type* As##type() const OVERRIDE { return this; } \
558 virtual H##type* As##type() OVERRIDE { return this; } \
559 virtual bool InstructionTypeEquals(HInstruction* other) const { \
560 return other->Is##type(); \
561 } \
562 virtual void Accept(HGraphVisitor* visitor)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000563
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100564template <typename T>
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700565class HUseListNode : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000566 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100567 HUseListNode(T* user, size_t index, HUseListNode* tail)
Dave Allison20dfc792014-06-16 20:44:29 -0700568 : user_(user), index_(index), tail_(tail) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000569
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000570 HUseListNode* GetTail() const { return tail_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100571 T* GetUser() const { return user_; }
572 size_t GetIndex() const { return index_; }
573
574 void SetTail(HUseListNode<T>* node) { tail_ = node; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000575
576 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100577 T* const user_;
578 const size_t index_;
579 HUseListNode<T>* tail_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000580
581 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
582};
583
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100584// Represents the side effects an instruction may have.
585class SideEffects : public ValueObject {
586 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100587 SideEffects() : flags_(0) {}
588
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100589 static SideEffects None() {
590 return SideEffects(0);
591 }
592
593 static SideEffects All() {
594 return SideEffects(ChangesSomething().flags_ | DependsOnSomething().flags_);
595 }
596
597 static SideEffects ChangesSomething() {
598 return SideEffects((1 << kFlagChangesCount) - 1);
599 }
600
601 static SideEffects DependsOnSomething() {
602 int count = kFlagDependsOnCount - kFlagChangesCount;
603 return SideEffects(((1 << count) - 1) << kFlagChangesCount);
604 }
605
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100606 SideEffects Union(SideEffects other) const {
607 return SideEffects(flags_ | other.flags_);
608 }
609
Roland Levillain72bceff2014-09-15 18:29:00 +0100610 bool HasSideEffects() const {
611 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
612 return (flags_ & all_bits_set) != 0;
613 }
614
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100615 bool HasAllSideEffects() const {
616 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
617 return all_bits_set == (flags_ & all_bits_set);
618 }
619
620 bool DependsOn(SideEffects other) const {
621 size_t depends_flags = other.ComputeDependsFlags();
622 return (flags_ & depends_flags) != 0;
623 }
624
625 bool HasDependencies() const {
626 int count = kFlagDependsOnCount - kFlagChangesCount;
627 size_t all_bits_set = (1 << count) - 1;
628 return ((flags_ >> kFlagChangesCount) & all_bits_set) != 0;
629 }
630
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100631 private:
632 static constexpr int kFlagChangesSomething = 0;
633 static constexpr int kFlagChangesCount = kFlagChangesSomething + 1;
634
635 static constexpr int kFlagDependsOnSomething = kFlagChangesCount;
636 static constexpr int kFlagDependsOnCount = kFlagDependsOnSomething + 1;
637
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100638 explicit SideEffects(size_t flags) : flags_(flags) {}
639
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100640 size_t ComputeDependsFlags() const {
641 return flags_ << kFlagChangesCount;
642 }
643
644 size_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100645};
646
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700647class HInstruction : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000648 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100649 explicit HInstruction(SideEffects side_effects)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000650 : previous_(nullptr),
651 next_(nullptr),
652 block_(nullptr),
653 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100654 ssa_index_(-1),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000655 uses_(nullptr),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100656 env_uses_(nullptr),
657 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100658 locations_(nullptr),
659 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100660 lifetime_position_(kNoLifetime),
661 side_effects_(side_effects) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000662
Dave Allison20dfc792014-06-16 20:44:29 -0700663 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000664
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100665#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100666 enum InstructionKind {
667 FOR_EACH_INSTRUCTION(DECLARE_KIND)
668 };
669#undef DECLARE_KIND
670
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000671 HInstruction* GetNext() const { return next_; }
672 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000673
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000674 HBasicBlock* GetBlock() const { return block_; }
675 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100676 bool IsInBlock() const { return block_ != nullptr; }
677 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +0100678 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000679
Roland Levillain6b879dd2014-09-22 17:13:44 +0100680 virtual size_t InputCount() const = 0;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100681 virtual HInstruction* InputAt(size_t i) const = 0;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000682
683 virtual void Accept(HGraphVisitor* visitor) = 0;
684 virtual const char* DebugName() const = 0;
685
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100686 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100687 virtual void SetRawInputAt(size_t index, HInstruction* input) = 0;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100688
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100689 virtual bool NeedsEnvironment() const { return false; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100690 virtual bool IsControlFlow() const { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +0100691 virtual bool CanThrow() const { return false; }
Roland Levillain72bceff2014-09-15 18:29:00 +0100692 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100693
694 void AddUseAt(HInstruction* user, size_t index) {
695 uses_ = new (block_->GetGraph()->GetArena()) HUseListNode<HInstruction>(user, index, uses_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000696 }
697
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100698 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +0100699 DCHECK(user != nullptr);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100700 env_uses_ = new (block_->GetGraph()->GetArena()) HUseListNode<HEnvironment>(
701 user, index, env_uses_);
702 }
703
704 void RemoveUser(HInstruction* user, size_t index);
Nicolas Geoffray724c9632014-09-22 12:27:27 +0100705 void RemoveEnvironmentUser(HEnvironment* user, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100706
707 HUseListNode<HInstruction>* GetUses() const { return uses_; }
708 HUseListNode<HEnvironment>* GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000709
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100710 bool HasUses() const { return uses_ != nullptr || env_uses_ != nullptr; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100711 bool HasEnvironmentUses() const { return env_uses_ != nullptr; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000712
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100713 size_t NumberOfUses() const {
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100714 // TODO: Optimize this method if it is used outside of the HGraphVisualizer.
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100715 size_t result = 0;
716 HUseListNode<HInstruction>* current = uses_;
717 while (current != nullptr) {
718 current = current->GetTail();
719 ++result;
720 }
721 return result;
722 }
723
Roland Levillain6c82d402014-10-13 16:10:27 +0100724 // Does this instruction strictly dominate `other_instruction`?
725 // Returns false if this instruction and `other_instruction` are the same.
726 // Aborts if this instruction and `other_instruction` are both phis.
727 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +0100728
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000729 int GetId() const { return id_; }
730 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000731
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100732 int GetSsaIndex() const { return ssa_index_; }
733 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
734 bool HasSsaIndex() const { return ssa_index_ != -1; }
735
736 bool HasEnvironment() const { return environment_ != nullptr; }
737 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100738 void SetEnvironment(HEnvironment* environment) { environment_ = environment; }
739
Nicolas Geoffray39468442014-09-02 15:17:15 +0100740 // Returns the number of entries in the environment. Typically, that is the
741 // number of dex registers in a method. It could be more in case of inlining.
742 size_t EnvironmentSize() const;
743
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000744 LocationSummary* GetLocations() const { return locations_; }
745 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000746
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100747 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100748 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100749
Dave Allison20dfc792014-06-16 20:44:29 -0700750 bool HasOnlyOneUse() const {
751 return uses_ != nullptr && uses_->GetTail() == nullptr;
752 }
753
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100754#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +0100755 bool Is##type() const { return (As##type() != nullptr); } \
756 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000757 virtual H##type* As##type() { return nullptr; }
758
759 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
760#undef INSTRUCTION_TYPE_CHECK
761
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100762 // Returns whether the instruction can be moved within the graph.
763 virtual bool CanBeMoved() const { return false; }
764
765 // Returns whether the two instructions are of the same kind.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700766 virtual bool InstructionTypeEquals(HInstruction* other) const {
767 UNUSED(other);
768 return false;
769 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100770
771 // Returns whether any data encoded in the two instructions is equal.
772 // This method does not look at the inputs. Both instructions must be
773 // of the same type, otherwise the method has undefined behavior.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700774 virtual bool InstructionDataEquals(HInstruction* other) const {
775 UNUSED(other);
776 return false;
777 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100778
779 // Returns whether two instructions are equal, that is:
780 // 1) They have the same type and contain the same data,
781 // 2) Their inputs are identical.
782 bool Equals(HInstruction* other) const;
783
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100784 virtual InstructionKind GetKind() const = 0;
785
786 virtual size_t ComputeHashCode() const {
787 size_t result = GetKind();
788 for (size_t i = 0, e = InputCount(); i < e; ++i) {
789 result = (result * 31) + InputAt(i)->GetId();
790 }
791 return result;
792 }
793
794 SideEffects GetSideEffects() const { return side_effects_; }
795
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100796 size_t GetLifetimePosition() const { return lifetime_position_; }
797 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
798 LiveInterval* GetLiveInterval() const { return live_interval_; }
799 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
800 bool HasLiveInterval() const { return live_interval_ != nullptr; }
801
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000802 private:
803 HInstruction* previous_;
804 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000805 HBasicBlock* block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000806
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000807 // An instruction gets an id when it is added to the graph.
808 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +0100809 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000810 int id_;
811
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100812 // When doing liveness analysis, instructions that have uses get an SSA index.
813 int ssa_index_;
814
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100815 // List of instructions that have this instruction as input.
816 HUseListNode<HInstruction>* uses_;
817
818 // List of environments that contain this instruction.
819 HUseListNode<HEnvironment>* env_uses_;
820
Nicolas Geoffray39468442014-09-02 15:17:15 +0100821 // The environment associated with this instruction. Not null if the instruction
822 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100823 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000824
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000825 // Set by the code generator.
826 LocationSummary* locations_;
827
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100828 // Set by the liveness analysis.
829 LiveInterval* live_interval_;
830
831 // Set by the liveness analysis, this is the position in a linear
832 // order of blocks where this instruction's live interval start.
833 size_t lifetime_position_;
834
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100835 const SideEffects side_effects_;
836
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000837 friend class HBasicBlock;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100838 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000839
840 DISALLOW_COPY_AND_ASSIGN(HInstruction);
841};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700842std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000843
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100844template<typename T>
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000845class HUseIterator : public ValueObject {
846 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100847 explicit HUseIterator(HUseListNode<T>* uses) : current_(uses) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000848
849 bool Done() const { return current_ == nullptr; }
850
851 void Advance() {
852 DCHECK(!Done());
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000853 current_ = current_->GetTail();
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000854 }
855
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100856 HUseListNode<T>* Current() const {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000857 DCHECK(!Done());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100858 return current_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000859 }
860
861 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100862 HUseListNode<T>* current_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000863
864 friend class HValue;
865};
866
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100867// A HEnvironment object contains the values of virtual registers at a given location.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700868class HEnvironment : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100869 public:
870 HEnvironment(ArenaAllocator* arena, size_t number_of_vregs) : vregs_(arena, number_of_vregs) {
871 vregs_.SetSize(number_of_vregs);
872 for (size_t i = 0; i < number_of_vregs; i++) {
873 vregs_.Put(i, nullptr);
874 }
875 }
876
877 void Populate(const GrowableArray<HInstruction*>& env) {
878 for (size_t i = 0; i < env.Size(); i++) {
879 HInstruction* instruction = env.Get(i);
880 vregs_.Put(i, instruction);
881 if (instruction != nullptr) {
882 instruction->AddEnvUseAt(this, i);
883 }
884 }
885 }
886
887 void SetRawEnvAt(size_t index, HInstruction* instruction) {
888 vregs_.Put(index, instruction);
889 }
890
Nicolas Geoffray39468442014-09-02 15:17:15 +0100891 HInstruction* GetInstructionAt(size_t index) const {
892 return vregs_.Get(index);
893 }
894
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100895 GrowableArray<HInstruction*>* GetVRegs() {
896 return &vregs_;
897 }
898
Nicolas Geoffray39468442014-09-02 15:17:15 +0100899 size_t Size() const { return vregs_.Size(); }
900
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100901 private:
902 GrowableArray<HInstruction*> vregs_;
903
904 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
905};
906
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000907class HInputIterator : public ValueObject {
908 public:
Dave Allison20dfc792014-06-16 20:44:29 -0700909 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000910
911 bool Done() const { return index_ == instruction_->InputCount(); }
912 HInstruction* Current() const { return instruction_->InputAt(index_); }
913 void Advance() { index_++; }
914
915 private:
916 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100917 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000918
919 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
920};
921
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000922class HInstructionIterator : public ValueObject {
923 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100924 explicit HInstructionIterator(const HInstructionList& instructions)
925 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000926 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000927 }
928
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000929 bool Done() const { return instruction_ == nullptr; }
930 HInstruction* Current() const { return instruction_; }
931 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000932 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000933 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000934 }
935
936 private:
937 HInstruction* instruction_;
938 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100939
940 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000941};
942
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100943class HBackwardInstructionIterator : public ValueObject {
944 public:
945 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
946 : instruction_(instructions.last_instruction_) {
947 next_ = Done() ? nullptr : instruction_->GetPrevious();
948 }
949
950 bool Done() const { return instruction_ == nullptr; }
951 HInstruction* Current() const { return instruction_; }
952 void Advance() {
953 instruction_ = next_;
954 next_ = Done() ? nullptr : instruction_->GetPrevious();
955 }
956
957 private:
958 HInstruction* instruction_;
959 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100960
961 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100962};
963
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000964// An embedded container with N elements of type T. Used (with partial
965// specialization for N=0) because embedded arrays cannot have size 0.
966template<typename T, intptr_t N>
967class EmbeddedArray {
968 public:
Dave Allison20dfc792014-06-16 20:44:29 -0700969 EmbeddedArray() : elements_() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000970
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000971 intptr_t GetLength() const { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000972
973 const T& operator[](intptr_t i) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000974 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000975 return elements_[i];
976 }
977
978 T& operator[](intptr_t i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000979 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000980 return elements_[i];
981 }
982
983 const T& At(intptr_t i) const {
984 return (*this)[i];
985 }
986
987 void SetAt(intptr_t i, const T& val) {
988 (*this)[i] = val;
989 }
990
991 private:
992 T elements_[N];
993};
994
995template<typename T>
996class EmbeddedArray<T, 0> {
997 public:
998 intptr_t length() const { return 0; }
999 const T& operator[](intptr_t i) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001000 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001001 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001002 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001003 }
1004 T& operator[](intptr_t i) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001005 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001006 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001007 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001008 }
1009};
1010
1011template<intptr_t N>
1012class HTemplateInstruction: public HInstruction {
1013 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001014 HTemplateInstruction<N>(SideEffects side_effects)
1015 : HInstruction(side_effects), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07001016 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001017
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001018 virtual size_t InputCount() const { return N; }
1019 virtual HInstruction* InputAt(size_t i) const { return inputs_[i]; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001020
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001021 protected:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001022 virtual void SetRawInputAt(size_t i, HInstruction* instruction) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001023 inputs_[i] = instruction;
1024 }
1025
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001026 private:
1027 EmbeddedArray<HInstruction*, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001028
1029 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001030};
1031
Dave Allison20dfc792014-06-16 20:44:29 -07001032template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001033class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07001034 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001035 HExpression<N>(Primitive::Type type, SideEffects side_effects)
1036 : HTemplateInstruction<N>(side_effects), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07001037 virtual ~HExpression() {}
1038
1039 virtual Primitive::Type GetType() const { return type_; }
1040
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001041 protected:
1042 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07001043};
1044
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001045// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
1046// instruction that branches to the exit block.
1047class HReturnVoid : public HTemplateInstruction<0> {
1048 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001049 HReturnVoid() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001050
1051 virtual bool IsControlFlow() const { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001052
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001053 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001054
1055 private:
1056 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
1057};
1058
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001059// Represents dex's RETURN opcodes. A HReturn is a control flow
1060// instruction that branches to the exit block.
1061class HReturn : public HTemplateInstruction<1> {
1062 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001063 explicit HReturn(HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001064 SetRawInputAt(0, value);
1065 }
1066
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001067 virtual bool IsControlFlow() const { return true; }
1068
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001069 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001070
1071 private:
1072 DISALLOW_COPY_AND_ASSIGN(HReturn);
1073};
1074
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001075// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001076// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001077// exit block.
1078class HExit : public HTemplateInstruction<0> {
1079 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001080 HExit() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001081
1082 virtual bool IsControlFlow() const { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001083
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001084 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001085
1086 private:
1087 DISALLOW_COPY_AND_ASSIGN(HExit);
1088};
1089
1090// Jumps from one block to another.
1091class HGoto : public HTemplateInstruction<0> {
1092 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001093 HGoto() : HTemplateInstruction(SideEffects::None()) {}
1094
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001095 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001096
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001097 HBasicBlock* GetSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001098 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001099 }
1100
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001101 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001102
1103 private:
1104 DISALLOW_COPY_AND_ASSIGN(HGoto);
1105};
1106
Dave Allison20dfc792014-06-16 20:44:29 -07001107
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001108// Conditional branch. A block ending with an HIf instruction must have
1109// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001110class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001111 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001112 explicit HIf(HInstruction* input) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001113 SetRawInputAt(0, input);
1114 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001115
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001116 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001117
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001118 HBasicBlock* IfTrueSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001119 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001120 }
1121
1122 HBasicBlock* IfFalseSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001123 return GetBlock()->GetSuccessors().Get(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001124 }
1125
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001126 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001127
Dave Allison20dfc792014-06-16 20:44:29 -07001128 virtual bool IsIfInstruction() const { return true; }
1129
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001130 private:
1131 DISALLOW_COPY_AND_ASSIGN(HIf);
1132};
1133
Roland Levillain88cb1752014-10-20 16:36:47 +01001134class HUnaryOperation : public HExpression<1> {
1135 public:
1136 HUnaryOperation(Primitive::Type result_type, HInstruction* input)
1137 : HExpression(result_type, SideEffects::None()) {
1138 SetRawInputAt(0, input);
1139 }
1140
1141 HInstruction* GetInput() const { return InputAt(0); }
1142 Primitive::Type GetResultType() const { return GetType(); }
1143
1144 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001145 virtual bool InstructionDataEquals(HInstruction* other) const {
1146 UNUSED(other);
1147 return true;
1148 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001149
Roland Levillain9240d6a2014-10-20 16:47:04 +01001150 // Try to statically evaluate `operation` and return a HConstant
1151 // containing the result of this evaluation. If `operation` cannot
1152 // be evaluated as a constant, return nullptr.
1153 HConstant* TryStaticEvaluation() const;
1154
1155 // Apply this operation to `x`.
1156 virtual int32_t Evaluate(int32_t x) const = 0;
1157 virtual int64_t Evaluate(int64_t x) const = 0;
1158
Roland Levillain88cb1752014-10-20 16:36:47 +01001159 DECLARE_INSTRUCTION(UnaryOperation);
1160
1161 private:
1162 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
1163};
1164
Dave Allison20dfc792014-06-16 20:44:29 -07001165class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001166 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001167 HBinaryOperation(Primitive::Type result_type,
1168 HInstruction* left,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001169 HInstruction* right) : HExpression(result_type, SideEffects::None()) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001170 SetRawInputAt(0, left);
1171 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001172 }
1173
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001174 HInstruction* GetLeft() const { return InputAt(0); }
1175 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07001176 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001177
1178 virtual bool IsCommutative() { return false; }
1179
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001180 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001181 virtual bool InstructionDataEquals(HInstruction* other) const {
1182 UNUSED(other);
1183 return true;
1184 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001185
Roland Levillain9240d6a2014-10-20 16:47:04 +01001186 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01001187 // containing the result of this evaluation. If `operation` cannot
1188 // be evaluated as a constant, return nullptr.
Roland Levillain9240d6a2014-10-20 16:47:04 +01001189 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01001190
1191 // Apply this operation to `x` and `y`.
1192 virtual int32_t Evaluate(int32_t x, int32_t y) const = 0;
1193 virtual int64_t Evaluate(int64_t x, int64_t y) const = 0;
1194
Roland Levillainccc07a92014-09-16 14:48:16 +01001195 DECLARE_INSTRUCTION(BinaryOperation);
1196
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001197 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001198 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
1199};
1200
Dave Allison20dfc792014-06-16 20:44:29 -07001201class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001202 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001203 HCondition(HInstruction* first, HInstruction* second)
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001204 : HBinaryOperation(Primitive::kPrimBoolean, first, second),
1205 needs_materialization_(true) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001206
1207 virtual bool IsCommutative() { return true; }
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001208
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001209 bool NeedsMaterialization() const { return needs_materialization_; }
1210 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001211
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001212 // For code generation purposes, returns whether this instruction is just before
1213 // `if_`, and disregard moves in between.
1214 bool IsBeforeWhenDisregardMoves(HIf* if_) const;
1215
Dave Allison20dfc792014-06-16 20:44:29 -07001216 DECLARE_INSTRUCTION(Condition);
1217
1218 virtual IfCondition GetCondition() const = 0;
1219
1220 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001221 // For register allocation purposes, returns whether this instruction needs to be
1222 // materialized (that is, not just be in the processor flags).
1223 bool needs_materialization_;
1224
Dave Allison20dfc792014-06-16 20:44:29 -07001225 DISALLOW_COPY_AND_ASSIGN(HCondition);
1226};
1227
1228// Instruction to check if two inputs are equal to each other.
1229class HEqual : public HCondition {
1230 public:
1231 HEqual(HInstruction* first, HInstruction* second)
1232 : HCondition(first, second) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001233
Roland Levillain93445682014-10-06 19:24:02 +01001234 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1235 return x == y ? 1 : 0;
1236 }
1237 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1238 return x == y ? 1 : 0;
1239 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001240
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001241 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001242
Dave Allison20dfc792014-06-16 20:44:29 -07001243 virtual IfCondition GetCondition() const {
1244 return kCondEQ;
1245 }
1246
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001247 private:
1248 DISALLOW_COPY_AND_ASSIGN(HEqual);
1249};
1250
Dave Allison20dfc792014-06-16 20:44:29 -07001251class HNotEqual : public HCondition {
1252 public:
1253 HNotEqual(HInstruction* first, HInstruction* second)
1254 : HCondition(first, second) {}
1255
Roland Levillain93445682014-10-06 19:24:02 +01001256 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1257 return x != y ? 1 : 0;
1258 }
1259 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1260 return x != y ? 1 : 0;
1261 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001262
Dave Allison20dfc792014-06-16 20:44:29 -07001263 DECLARE_INSTRUCTION(NotEqual);
1264
1265 virtual IfCondition GetCondition() const {
1266 return kCondNE;
1267 }
1268
1269 private:
1270 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
1271};
1272
1273class HLessThan : public HCondition {
1274 public:
1275 HLessThan(HInstruction* first, HInstruction* second)
1276 : HCondition(first, second) {}
1277
Roland Levillain93445682014-10-06 19:24:02 +01001278 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1279 return x < y ? 1 : 0;
1280 }
1281 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1282 return x < y ? 1 : 0;
1283 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001284
Dave Allison20dfc792014-06-16 20:44:29 -07001285 DECLARE_INSTRUCTION(LessThan);
1286
1287 virtual IfCondition GetCondition() const {
1288 return kCondLT;
1289 }
1290
1291 private:
1292 DISALLOW_COPY_AND_ASSIGN(HLessThan);
1293};
1294
1295class HLessThanOrEqual : public HCondition {
1296 public:
1297 HLessThanOrEqual(HInstruction* first, HInstruction* second)
1298 : HCondition(first, second) {}
1299
Roland Levillain93445682014-10-06 19:24:02 +01001300 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1301 return x <= y ? 1 : 0;
1302 }
1303 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1304 return x <= y ? 1 : 0;
1305 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001306
Dave Allison20dfc792014-06-16 20:44:29 -07001307 DECLARE_INSTRUCTION(LessThanOrEqual);
1308
1309 virtual IfCondition GetCondition() const {
1310 return kCondLE;
1311 }
1312
1313 private:
1314 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
1315};
1316
1317class HGreaterThan : public HCondition {
1318 public:
1319 HGreaterThan(HInstruction* first, HInstruction* second)
1320 : HCondition(first, second) {}
1321
Roland Levillain93445682014-10-06 19:24:02 +01001322 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1323 return x > y ? 1 : 0;
1324 }
1325 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1326 return x > y ? 1 : 0;
1327 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001328
Dave Allison20dfc792014-06-16 20:44:29 -07001329 DECLARE_INSTRUCTION(GreaterThan);
1330
1331 virtual IfCondition GetCondition() const {
1332 return kCondGT;
1333 }
1334
1335 private:
1336 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
1337};
1338
1339class HGreaterThanOrEqual : public HCondition {
1340 public:
1341 HGreaterThanOrEqual(HInstruction* first, HInstruction* second)
1342 : HCondition(first, second) {}
1343
Roland Levillain93445682014-10-06 19:24:02 +01001344 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1345 return x >= y ? 1 : 0;
1346 }
1347 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1348 return x >= y ? 1 : 0;
1349 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001350
Dave Allison20dfc792014-06-16 20:44:29 -07001351 DECLARE_INSTRUCTION(GreaterThanOrEqual);
1352
1353 virtual IfCondition GetCondition() const {
1354 return kCondGE;
1355 }
1356
1357 private:
1358 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
1359};
1360
1361
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001362// Instruction to check how two inputs compare to each other.
1363// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
1364class HCompare : public HBinaryOperation {
1365 public:
Nicolas Geoffray799f5062014-11-26 14:45:52 +00001366 HCompare(Primitive::Type type, HInstruction* first, HInstruction* second)
1367 : HBinaryOperation(Primitive::kPrimInt, first, second) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001368 DCHECK_EQ(type, first->GetType());
1369 DCHECK_EQ(type, second->GetType());
1370 }
1371
Nicolas Geoffray799f5062014-11-26 14:45:52 +00001372 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001373 return
1374 x == y ? 0 :
1375 x > y ? 1 :
1376 -1;
1377 }
Nicolas Geoffray799f5062014-11-26 14:45:52 +00001378 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001379 return
1380 x == y ? 0 :
1381 x > y ? 1 :
1382 -1;
1383 }
1384
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001385 DECLARE_INSTRUCTION(Compare);
1386
1387 private:
1388 DISALLOW_COPY_AND_ASSIGN(HCompare);
1389};
1390
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001391// A local in the graph. Corresponds to a Dex register.
1392class HLocal : public HTemplateInstruction<0> {
1393 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001394 explicit HLocal(uint16_t reg_number)
1395 : HTemplateInstruction(SideEffects::None()), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001396
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001397 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001398
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001399 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001400
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001401 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001402 // The Dex register number.
1403 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001404
1405 DISALLOW_COPY_AND_ASSIGN(HLocal);
1406};
1407
1408// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07001409class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001410 public:
Roland Levillain5799fc02014-09-25 12:15:20 +01001411 HLoadLocal(HLocal* local, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001412 : HExpression(type, SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001413 SetRawInputAt(0, local);
1414 }
1415
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001416 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1417
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001418 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001419
1420 private:
1421 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
1422};
1423
1424// Store a value in a given local. This instruction has two inputs: the value
1425// and the local.
1426class HStoreLocal : public HTemplateInstruction<2> {
1427 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001428 HStoreLocal(HLocal* local, HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001429 SetRawInputAt(0, local);
1430 SetRawInputAt(1, value);
1431 }
1432
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001433 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1434
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001435 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001436
1437 private:
1438 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
1439};
1440
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001441class HConstant : public HExpression<0> {
1442 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001443 explicit HConstant(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
1444
1445 virtual bool CanBeMoved() const { return true; }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001446
1447 DECLARE_INSTRUCTION(Constant);
1448
1449 private:
1450 DISALLOW_COPY_AND_ASSIGN(HConstant);
1451};
1452
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001453class HFloatConstant : public HConstant {
1454 public:
1455 explicit HFloatConstant(float value) : HConstant(Primitive::kPrimFloat), value_(value) {}
1456
1457 float GetValue() const { return value_; }
1458
1459 virtual bool InstructionDataEquals(HInstruction* other) const {
1460 return bit_cast<float, int32_t>(other->AsFloatConstant()->value_) ==
1461 bit_cast<float, int32_t>(value_);
1462 }
1463
1464 virtual size_t ComputeHashCode() const { return static_cast<size_t>(GetValue()); }
1465
1466 DECLARE_INSTRUCTION(FloatConstant);
1467
1468 private:
1469 const float value_;
1470
1471 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
1472};
1473
1474class HDoubleConstant : public HConstant {
1475 public:
1476 explicit HDoubleConstant(double value) : HConstant(Primitive::kPrimDouble), value_(value) {}
1477
1478 double GetValue() const { return value_; }
1479
1480 virtual bool InstructionDataEquals(HInstruction* other) const {
1481 return bit_cast<double, int64_t>(other->AsDoubleConstant()->value_) ==
1482 bit_cast<double, int64_t>(value_);
1483 }
1484
1485 virtual size_t ComputeHashCode() const { return static_cast<size_t>(GetValue()); }
1486
1487 DECLARE_INSTRUCTION(DoubleConstant);
1488
1489 private:
1490 const double value_;
1491
1492 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
1493};
1494
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001495// Constants of the type int. Those can be from Dex instructions, or
1496// synthesized (for example with the if-eqz instruction).
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001497class HIntConstant : public HConstant {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001498 public:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001499 explicit HIntConstant(int32_t value) : HConstant(Primitive::kPrimInt), value_(value) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001500
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001501 int32_t GetValue() const { return value_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001502
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001503 virtual bool InstructionDataEquals(HInstruction* other) const {
1504 return other->AsIntConstant()->value_ == value_;
1505 }
1506
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001507 virtual size_t ComputeHashCode() const { return GetValue(); }
1508
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001509 DECLARE_INSTRUCTION(IntConstant);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001510
1511 private:
1512 const int32_t value_;
1513
1514 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
1515};
1516
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001517class HLongConstant : public HConstant {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001518 public:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001519 explicit HLongConstant(int64_t value) : HConstant(Primitive::kPrimLong), value_(value) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001520
1521 int64_t GetValue() const { return value_; }
1522
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001523 virtual bool InstructionDataEquals(HInstruction* other) const {
1524 return other->AsLongConstant()->value_ == value_;
1525 }
1526
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001527 virtual size_t ComputeHashCode() const { return static_cast<size_t>(GetValue()); }
1528
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001529 DECLARE_INSTRUCTION(LongConstant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001530
1531 private:
1532 const int64_t value_;
1533
1534 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
1535};
1536
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001537class HInvoke : public HInstruction {
1538 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001539 HInvoke(ArenaAllocator* arena,
1540 uint32_t number_of_arguments,
1541 Primitive::Type return_type,
1542 uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001543 : HInstruction(SideEffects::All()),
1544 inputs_(arena, number_of_arguments),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001545 return_type_(return_type),
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001546 dex_pc_(dex_pc) {
1547 inputs_.SetSize(number_of_arguments);
1548 }
1549
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001550 virtual size_t InputCount() const { return inputs_.Size(); }
1551 virtual HInstruction* InputAt(size_t i) const { return inputs_.Get(i); }
1552
1553 // Runtime needs to walk the stack, so Dex -> Dex calls need to
1554 // know their environment.
1555 virtual bool NeedsEnvironment() const { return true; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001556
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001557 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001558 SetRawInputAt(index, argument);
1559 }
1560
1561 virtual void SetRawInputAt(size_t index, HInstruction* input) {
1562 inputs_.Put(index, input);
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001563 }
1564
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001565 virtual Primitive::Type GetType() const { return return_type_; }
1566
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001567 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001568
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001569 DECLARE_INSTRUCTION(Invoke);
1570
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001571 protected:
1572 GrowableArray<HInstruction*> inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001573 const Primitive::Type return_type_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001574 const uint32_t dex_pc_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001575
1576 private:
1577 DISALLOW_COPY_AND_ASSIGN(HInvoke);
1578};
1579
1580class HInvokeStatic : public HInvoke {
1581 public:
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001582 HInvokeStatic(ArenaAllocator* arena,
1583 uint32_t number_of_arguments,
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001584 Primitive::Type return_type,
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001585 uint32_t dex_pc,
1586 uint32_t index_in_dex_cache)
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001587 : HInvoke(arena, number_of_arguments, return_type, dex_pc),
1588 index_in_dex_cache_(index_in_dex_cache) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001589
1590 uint32_t GetIndexInDexCache() const { return index_in_dex_cache_; }
1591
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001592 DECLARE_INSTRUCTION(InvokeStatic);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001593
1594 private:
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001595 const uint32_t index_in_dex_cache_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001596
1597 DISALLOW_COPY_AND_ASSIGN(HInvokeStatic);
1598};
1599
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001600class HInvokeVirtual : public HInvoke {
1601 public:
1602 HInvokeVirtual(ArenaAllocator* arena,
1603 uint32_t number_of_arguments,
1604 Primitive::Type return_type,
1605 uint32_t dex_pc,
1606 uint32_t vtable_index)
1607 : HInvoke(arena, number_of_arguments, return_type, dex_pc),
1608 vtable_index_(vtable_index) {}
1609
1610 uint32_t GetVTableIndex() const { return vtable_index_; }
1611
1612 DECLARE_INSTRUCTION(InvokeVirtual);
1613
1614 private:
1615 const uint32_t vtable_index_;
1616
1617 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
1618};
1619
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001620class HInvokeInterface : public HInvoke {
1621 public:
1622 HInvokeInterface(ArenaAllocator* arena,
1623 uint32_t number_of_arguments,
1624 Primitive::Type return_type,
1625 uint32_t dex_pc,
1626 uint32_t dex_method_index,
1627 uint32_t imt_index)
1628 : HInvoke(arena, number_of_arguments, return_type, dex_pc),
1629 dex_method_index_(dex_method_index),
1630 imt_index_(imt_index) {}
1631
1632 uint32_t GetImtIndex() const { return imt_index_; }
1633 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
1634
1635 DECLARE_INSTRUCTION(InvokeInterface);
1636
1637 private:
1638 const uint32_t dex_method_index_;
1639 const uint32_t imt_index_;
1640
1641 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
1642};
1643
Dave Allison20dfc792014-06-16 20:44:29 -07001644class HNewInstance : public HExpression<0> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001645 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001646 HNewInstance(uint32_t dex_pc, uint16_t type_index)
1647 : HExpression(Primitive::kPrimNot, SideEffects::None()),
1648 dex_pc_(dex_pc),
1649 type_index_(type_index) {}
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001650
1651 uint32_t GetDexPc() const { return dex_pc_; }
1652 uint16_t GetTypeIndex() const { return type_index_; }
1653
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001654 // Calls runtime so needs an environment.
1655 virtual bool NeedsEnvironment() const { return true; }
1656
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001657 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001658
1659 private:
1660 const uint32_t dex_pc_;
1661 const uint16_t type_index_;
1662
1663 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
1664};
1665
Roland Levillain88cb1752014-10-20 16:36:47 +01001666class HNeg : public HUnaryOperation {
1667 public:
1668 explicit HNeg(Primitive::Type result_type, HInstruction* input)
1669 : HUnaryOperation(result_type, input) {}
1670
Roland Levillain9240d6a2014-10-20 16:47:04 +01001671 virtual int32_t Evaluate(int32_t x) const OVERRIDE { return -x; }
1672 virtual int64_t Evaluate(int64_t x) const OVERRIDE { return -x; }
1673
Roland Levillain88cb1752014-10-20 16:36:47 +01001674 DECLARE_INSTRUCTION(Neg);
1675
1676 private:
1677 DISALLOW_COPY_AND_ASSIGN(HNeg);
1678};
1679
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001680class HNewArray : public HExpression<1> {
1681 public:
1682 HNewArray(HInstruction* length, uint32_t dex_pc, uint16_t type_index)
1683 : HExpression(Primitive::kPrimNot, SideEffects::None()),
1684 dex_pc_(dex_pc),
1685 type_index_(type_index) {
1686 SetRawInputAt(0, length);
1687 }
1688
1689 uint32_t GetDexPc() const { return dex_pc_; }
1690 uint16_t GetTypeIndex() const { return type_index_; }
1691
1692 // Calls runtime so needs an environment.
1693 virtual bool NeedsEnvironment() const { return true; }
1694
1695 DECLARE_INSTRUCTION(NewArray);
1696
1697 private:
1698 const uint32_t dex_pc_;
1699 const uint16_t type_index_;
1700
1701 DISALLOW_COPY_AND_ASSIGN(HNewArray);
1702};
1703
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001704class HAdd : public HBinaryOperation {
1705 public:
1706 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1707 : HBinaryOperation(result_type, left, right) {}
1708
1709 virtual bool IsCommutative() { return true; }
1710
Roland Levillain93445682014-10-06 19:24:02 +01001711 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1712 return x + y;
1713 }
1714 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1715 return x + y;
1716 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001717
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001718 DECLARE_INSTRUCTION(Add);
1719
1720 private:
1721 DISALLOW_COPY_AND_ASSIGN(HAdd);
1722};
1723
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001724class HSub : public HBinaryOperation {
1725 public:
1726 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1727 : HBinaryOperation(result_type, left, right) {}
1728
Roland Levillain93445682014-10-06 19:24:02 +01001729 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1730 return x - y;
1731 }
1732 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1733 return x - y;
1734 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001735
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001736 DECLARE_INSTRUCTION(Sub);
1737
1738 private:
1739 DISALLOW_COPY_AND_ASSIGN(HSub);
1740};
1741
Calin Juravle34bacdf2014-10-07 20:23:36 +01001742class HMul : public HBinaryOperation {
1743 public:
1744 HMul(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1745 : HBinaryOperation(result_type, left, right) {}
1746
1747 virtual bool IsCommutative() { return true; }
1748
1749 virtual int32_t Evaluate(int32_t x, int32_t y) const { return x * y; }
1750 virtual int64_t Evaluate(int64_t x, int64_t y) const { return x * y; }
1751
1752 DECLARE_INSTRUCTION(Mul);
1753
1754 private:
1755 DISALLOW_COPY_AND_ASSIGN(HMul);
1756};
1757
Calin Juravle7c4954d2014-10-28 16:57:40 +00001758class HDiv : public HBinaryOperation {
1759 public:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001760 HDiv(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
1761 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00001762
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00001763 virtual int32_t Evaluate(int32_t x, int32_t y) const {
1764 // Our graph structure ensures we never have 0 for `y` during constant folding.
1765 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00001766 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00001767 return (y == -1) ? -x : x / y;
1768 }
Calin Juravlebacfec32014-11-14 15:54:36 +00001769
1770 virtual int64_t Evaluate(int64_t x, int64_t y) const {
1771 DCHECK_NE(y, 0);
1772 // Special case -1 to avoid getting a SIGFPE on x86(_64).
1773 return (y == -1) ? -x : x / y;
1774 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00001775
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001776 uint32_t GetDexPc() const { return dex_pc_; }
1777
Calin Juravle7c4954d2014-10-28 16:57:40 +00001778 DECLARE_INSTRUCTION(Div);
1779
1780 private:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001781 const uint32_t dex_pc_;
1782
Calin Juravle7c4954d2014-10-28 16:57:40 +00001783 DISALLOW_COPY_AND_ASSIGN(HDiv);
1784};
1785
Calin Juravlebacfec32014-11-14 15:54:36 +00001786class HRem : public HBinaryOperation {
1787 public:
1788 HRem(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
1789 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
1790
1791 virtual int32_t Evaluate(int32_t x, int32_t y) const {
1792 DCHECK_NE(y, 0);
1793 // Special case -1 to avoid getting a SIGFPE on x86(_64).
1794 return (y == -1) ? 0 : x % y;
1795 }
1796
1797 virtual int64_t Evaluate(int64_t x, int64_t y) const {
1798 DCHECK_NE(y, 0);
1799 // Special case -1 to avoid getting a SIGFPE on x86(_64).
1800 return (y == -1) ? 0 : x % y;
1801 }
1802
1803 uint32_t GetDexPc() const { return dex_pc_; }
1804
1805 DECLARE_INSTRUCTION(Rem);
1806
1807 private:
1808 const uint32_t dex_pc_;
1809
1810 DISALLOW_COPY_AND_ASSIGN(HRem);
1811};
1812
Calin Juravled0d48522014-11-04 16:40:20 +00001813class HDivZeroCheck : public HExpression<1> {
1814 public:
1815 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
1816 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
1817 SetRawInputAt(0, value);
1818 }
1819
1820 bool CanBeMoved() const OVERRIDE { return true; }
1821
1822 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
1823 UNUSED(other);
1824 return true;
1825 }
1826
1827 bool NeedsEnvironment() const OVERRIDE { return true; }
1828 bool CanThrow() const OVERRIDE { return true; }
1829
1830 uint32_t GetDexPc() const { return dex_pc_; }
1831
1832 DECLARE_INSTRUCTION(DivZeroCheck);
1833
1834 private:
1835 const uint32_t dex_pc_;
1836
1837 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
1838};
1839
Calin Juravle9aec02f2014-11-18 23:06:35 +00001840class HShl : public HBinaryOperation {
1841 public:
1842 HShl(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1843 : HBinaryOperation(result_type, left, right) {}
1844
1845 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x << (y & kMaxIntShiftValue); }
1846 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x << (y & kMaxLongShiftValue); }
1847
1848 DECLARE_INSTRUCTION(Shl);
1849
1850 private:
1851 DISALLOW_COPY_AND_ASSIGN(HShl);
1852};
1853
1854class HShr : public HBinaryOperation {
1855 public:
1856 HShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1857 : HBinaryOperation(result_type, left, right) {}
1858
1859 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x >> (y & kMaxIntShiftValue); }
1860 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x >> (y & kMaxLongShiftValue); }
1861
1862 DECLARE_INSTRUCTION(Shr);
1863
1864 private:
1865 DISALLOW_COPY_AND_ASSIGN(HShr);
1866};
1867
1868class HUShr : public HBinaryOperation {
1869 public:
1870 HUShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1871 : HBinaryOperation(result_type, left, right) {}
1872
1873 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1874 uint32_t ux = static_cast<uint32_t>(x);
1875 uint32_t uy = static_cast<uint32_t>(y) & kMaxIntShiftValue;
1876 return static_cast<int32_t>(ux >> uy);
1877 }
1878
1879 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1880 uint64_t ux = static_cast<uint64_t>(x);
1881 uint64_t uy = static_cast<uint64_t>(y) & kMaxLongShiftValue;
1882 return static_cast<int64_t>(ux >> uy);
1883 }
1884
1885 DECLARE_INSTRUCTION(UShr);
1886
1887 private:
1888 DISALLOW_COPY_AND_ASSIGN(HUShr);
1889};
1890
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001891class HAnd : public HBinaryOperation {
1892 public:
1893 HAnd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1894 : HBinaryOperation(result_type, left, right) {}
1895
1896 bool IsCommutative() OVERRIDE { return true; }
1897
1898 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x & y; }
1899 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x & y; }
1900
1901 DECLARE_INSTRUCTION(And);
1902
1903 private:
1904 DISALLOW_COPY_AND_ASSIGN(HAnd);
1905};
1906
1907class HOr : public HBinaryOperation {
1908 public:
1909 HOr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1910 : HBinaryOperation(result_type, left, right) {}
1911
1912 bool IsCommutative() OVERRIDE { return true; }
1913
1914 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x | y; }
1915 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x | y; }
1916
1917 DECLARE_INSTRUCTION(Or);
1918
1919 private:
1920 DISALLOW_COPY_AND_ASSIGN(HOr);
1921};
1922
1923class HXor : public HBinaryOperation {
1924 public:
1925 HXor(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1926 : HBinaryOperation(result_type, left, right) {}
1927
1928 bool IsCommutative() OVERRIDE { return true; }
1929
1930 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x ^ y; }
1931 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x ^ y; }
1932
1933 DECLARE_INSTRUCTION(Xor);
1934
1935 private:
1936 DISALLOW_COPY_AND_ASSIGN(HXor);
1937};
1938
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001939// The value of a parameter in this method. Its location depends on
1940// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07001941class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001942 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001943 HParameterValue(uint8_t index, Primitive::Type parameter_type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001944 : HExpression(parameter_type, SideEffects::None()), index_(index) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001945
1946 uint8_t GetIndex() const { return index_; }
1947
1948 DECLARE_INSTRUCTION(ParameterValue);
1949
1950 private:
1951 // The index of this parameter in the parameters list. Must be less
1952 // than HGraph::number_of_in_vregs_;
1953 const uint8_t index_;
1954
1955 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
1956};
1957
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001958class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001959 public:
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001960 explicit HNot(Primitive::Type result_type, HInstruction* input)
1961 : HUnaryOperation(result_type, input) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001962
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001963 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001964 virtual bool InstructionDataEquals(HInstruction* other) const {
1965 UNUSED(other);
1966 return true;
1967 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001968
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001969 virtual int32_t Evaluate(int32_t x) const OVERRIDE { return ~x; }
1970 virtual int64_t Evaluate(int64_t x) const OVERRIDE { return ~x; }
1971
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001972 DECLARE_INSTRUCTION(Not);
1973
1974 private:
1975 DISALLOW_COPY_AND_ASSIGN(HNot);
1976};
1977
Roland Levillaindff1f282014-11-05 14:15:05 +00001978class HTypeConversion : public HExpression<1> {
1979 public:
1980 // Instantiate a type conversion of `input` to `result_type`.
1981 HTypeConversion(Primitive::Type result_type, HInstruction* input)
1982 : HExpression(result_type, SideEffects::None()) {
1983 SetRawInputAt(0, input);
1984 DCHECK_NE(input->GetType(), result_type);
1985 }
1986
1987 HInstruction* GetInput() const { return InputAt(0); }
1988 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
1989 Primitive::Type GetResultType() const { return GetType(); }
1990
1991 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00001992 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00001993
1994 DECLARE_INSTRUCTION(TypeConversion);
1995
1996 private:
1997 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
1998};
1999
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002000class HPhi : public HInstruction {
2001 public:
2002 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002003 : HInstruction(SideEffects::None()),
2004 inputs_(arena, number_of_inputs),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002005 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002006 type_(type),
2007 is_live_(false) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002008 inputs_.SetSize(number_of_inputs);
2009 }
2010
2011 virtual size_t InputCount() const { return inputs_.Size(); }
2012 virtual HInstruction* InputAt(size_t i) const { return inputs_.Get(i); }
2013
2014 virtual void SetRawInputAt(size_t index, HInstruction* input) {
2015 inputs_.Put(index, input);
2016 }
2017
2018 void AddInput(HInstruction* input);
2019
2020 virtual Primitive::Type GetType() const { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002021 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002022
2023 uint32_t GetRegNumber() const { return reg_number_; }
2024
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002025 void SetDead() { is_live_ = false; }
2026 void SetLive() { is_live_ = true; }
2027 bool IsDead() const { return !is_live_; }
2028 bool IsLive() const { return is_live_; }
2029
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002030 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002031
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002032 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002033 GrowableArray<HInstruction*> inputs_;
2034 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002035 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002036 bool is_live_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002037
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002038 DISALLOW_COPY_AND_ASSIGN(HPhi);
2039};
2040
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002041class HNullCheck : public HExpression<1> {
2042 public:
2043 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002044 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002045 SetRawInputAt(0, value);
2046 }
2047
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002048 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002049 virtual bool InstructionDataEquals(HInstruction* other) const {
2050 UNUSED(other);
2051 return true;
2052 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002053
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002054 virtual bool NeedsEnvironment() const { return true; }
2055
Roland Levillaine161a2a2014-10-03 12:45:18 +01002056 virtual bool CanThrow() const { return true; }
2057
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002058 uint32_t GetDexPc() const { return dex_pc_; }
2059
2060 DECLARE_INSTRUCTION(NullCheck);
2061
2062 private:
2063 const uint32_t dex_pc_;
2064
2065 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
2066};
2067
2068class FieldInfo : public ValueObject {
2069 public:
Roland Levillain5799fc02014-09-25 12:15:20 +01002070 FieldInfo(MemberOffset field_offset, Primitive::Type field_type)
Nicolas Geoffray39468442014-09-02 15:17:15 +01002071 : field_offset_(field_offset), field_type_(field_type) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002072
2073 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002074 Primitive::Type GetFieldType() const { return field_type_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002075
2076 private:
2077 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01002078 const Primitive::Type field_type_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002079};
2080
2081class HInstanceFieldGet : public HExpression<1> {
2082 public:
2083 HInstanceFieldGet(HInstruction* value,
2084 Primitive::Type field_type,
2085 MemberOffset field_offset)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002086 : HExpression(field_type, SideEffects::DependsOnSomething()),
2087 field_info_(field_offset, field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002088 SetRawInputAt(0, value);
2089 }
2090
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002091 virtual bool CanBeMoved() const { return true; }
2092 virtual bool InstructionDataEquals(HInstruction* other) const {
2093 size_t other_offset = other->AsInstanceFieldGet()->GetFieldOffset().SizeValue();
2094 return other_offset == GetFieldOffset().SizeValue();
2095 }
2096
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002097 virtual size_t ComputeHashCode() const {
2098 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
2099 }
2100
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002101 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002102 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002103
2104 DECLARE_INSTRUCTION(InstanceFieldGet);
2105
2106 private:
2107 const FieldInfo field_info_;
2108
2109 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
2110};
2111
2112class HInstanceFieldSet : public HTemplateInstruction<2> {
2113 public:
2114 HInstanceFieldSet(HInstruction* object,
2115 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01002116 Primitive::Type field_type,
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002117 MemberOffset field_offset)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002118 : HTemplateInstruction(SideEffects::ChangesSomething()),
2119 field_info_(field_offset, field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002120 SetRawInputAt(0, object);
2121 SetRawInputAt(1, value);
2122 }
2123
2124 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002125 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002126
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002127 HInstruction* GetValue() const { return InputAt(1); }
2128
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002129 DECLARE_INSTRUCTION(InstanceFieldSet);
2130
2131 private:
2132 const FieldInfo field_info_;
2133
2134 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
2135};
2136
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002137class HArrayGet : public HExpression<2> {
2138 public:
2139 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002140 : HExpression(type, SideEffects::DependsOnSomething()) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002141 SetRawInputAt(0, array);
2142 SetRawInputAt(1, index);
2143 }
2144
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002145 bool CanBeMoved() const OVERRIDE { return true; }
2146 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002147 UNUSED(other);
2148 return true;
2149 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002150 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002151
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002152 HInstruction* GetArray() const { return InputAt(0); }
2153 HInstruction* GetIndex() const { return InputAt(1); }
2154
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002155 DECLARE_INSTRUCTION(ArrayGet);
2156
2157 private:
2158 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
2159};
2160
2161class HArraySet : public HTemplateInstruction<3> {
2162 public:
2163 HArraySet(HInstruction* array,
2164 HInstruction* index,
2165 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002166 Primitive::Type expected_component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002167 uint32_t dex_pc)
2168 : HTemplateInstruction(SideEffects::ChangesSomething()),
2169 dex_pc_(dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002170 expected_component_type_(expected_component_type),
2171 needs_type_check_(value->GetType() == Primitive::kPrimNot) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002172 SetRawInputAt(0, array);
2173 SetRawInputAt(1, index);
2174 SetRawInputAt(2, value);
2175 }
2176
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002177 bool NeedsEnvironment() const {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002178 // We currently always call a runtime method to catch array store
2179 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002180 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002181 }
2182
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002183 void ClearNeedsTypeCheck() {
2184 needs_type_check_ = false;
2185 }
2186
2187 bool NeedsTypeCheck() const { return needs_type_check_; }
2188
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002189 uint32_t GetDexPc() const { return dex_pc_; }
2190
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002191 HInstruction* GetArray() const { return InputAt(0); }
2192 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002193 HInstruction* GetValue() const { return InputAt(2); }
2194
2195 Primitive::Type GetComponentType() const {
2196 // The Dex format does not type floating point index operations. Since the
2197 // `expected_component_type_` is set during building and can therefore not
2198 // be correct, we also check what is the value type. If it is a floating
2199 // point type, we must use that type.
2200 Primitive::Type value_type = GetValue()->GetType();
2201 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
2202 ? value_type
2203 : expected_component_type_;
2204 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002205
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002206 DECLARE_INSTRUCTION(ArraySet);
2207
2208 private:
2209 const uint32_t dex_pc_;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002210 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002211 bool needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002212
2213 DISALLOW_COPY_AND_ASSIGN(HArraySet);
2214};
2215
2216class HArrayLength : public HExpression<1> {
2217 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002218 explicit HArrayLength(HInstruction* array)
2219 : HExpression(Primitive::kPrimInt, SideEffects::None()) {
2220 // Note that arrays do not change length, so the instruction does not
2221 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002222 SetRawInputAt(0, array);
2223 }
2224
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002225 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002226 virtual bool InstructionDataEquals(HInstruction* other) const {
2227 UNUSED(other);
2228 return true;
2229 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002230
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002231 DECLARE_INSTRUCTION(ArrayLength);
2232
2233 private:
2234 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
2235};
2236
2237class HBoundsCheck : public HExpression<2> {
2238 public:
2239 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002240 : HExpression(index->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002241 DCHECK(index->GetType() == Primitive::kPrimInt);
2242 SetRawInputAt(0, index);
2243 SetRawInputAt(1, length);
2244 }
2245
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002246 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002247 virtual bool InstructionDataEquals(HInstruction* other) const {
2248 UNUSED(other);
2249 return true;
2250 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002251
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002252 virtual bool NeedsEnvironment() const { return true; }
2253
Roland Levillaine161a2a2014-10-03 12:45:18 +01002254 virtual bool CanThrow() const { return true; }
2255
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002256 uint32_t GetDexPc() const { return dex_pc_; }
2257
2258 DECLARE_INSTRUCTION(BoundsCheck);
2259
2260 private:
2261 const uint32_t dex_pc_;
2262
2263 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
2264};
2265
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002266/**
2267 * Some DEX instructions are folded into multiple HInstructions that need
2268 * to stay live until the last HInstruction. This class
2269 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00002270 * HInstruction stays live. `index` represents the stack location index of the
2271 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002272 */
2273class HTemporary : public HTemplateInstruction<0> {
2274 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002275 explicit HTemporary(size_t index) : HTemplateInstruction(SideEffects::None()), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002276
2277 size_t GetIndex() const { return index_; }
2278
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00002279 Primitive::Type GetType() const OVERRIDE {
2280 // The previous instruction is the one that will be stored in the temporary location.
2281 DCHECK(GetPrevious() != nullptr);
2282 return GetPrevious()->GetType();
2283 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00002284
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002285 DECLARE_INSTRUCTION(Temporary);
2286
2287 private:
2288 const size_t index_;
2289
2290 DISALLOW_COPY_AND_ASSIGN(HTemporary);
2291};
2292
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002293class HSuspendCheck : public HTemplateInstruction<0> {
2294 public:
2295 explicit HSuspendCheck(uint32_t dex_pc)
Nicolas Geoffray9ebc72c2014-09-25 16:33:42 +01002296 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002297
2298 virtual bool NeedsEnvironment() const {
2299 return true;
2300 }
2301
2302 uint32_t GetDexPc() const { return dex_pc_; }
2303
2304 DECLARE_INSTRUCTION(SuspendCheck);
2305
2306 private:
2307 const uint32_t dex_pc_;
2308
2309 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
2310};
2311
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002312/**
2313 * Instruction to load a Class object.
2314 */
2315class HLoadClass : public HExpression<0> {
2316 public:
2317 HLoadClass(uint16_t type_index,
2318 bool is_referrers_class,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002319 uint32_t dex_pc)
2320 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2321 type_index_(type_index),
2322 is_referrers_class_(is_referrers_class),
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002323 dex_pc_(dex_pc),
2324 generate_clinit_check_(false) {}
2325
2326 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002327
2328 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2329 return other->AsLoadClass()->type_index_ == type_index_;
2330 }
2331
2332 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
2333
2334 uint32_t GetDexPc() const { return dex_pc_; }
2335 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002336 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002337
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002338 bool NeedsEnvironment() const OVERRIDE {
2339 // Will call runtime and load the class if the class is not loaded yet.
2340 // TODO: finer grain decision.
2341 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002342 }
2343
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002344 bool MustGenerateClinitCheck() const {
2345 return generate_clinit_check_;
2346 }
2347
2348 void SetMustGenerateClinitCheck() {
2349 generate_clinit_check_ = true;
2350 }
2351
2352 bool CanCallRuntime() const {
2353 return MustGenerateClinitCheck() || !is_referrers_class_;
2354 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002355
2356 DECLARE_INSTRUCTION(LoadClass);
2357
2358 private:
2359 const uint16_t type_index_;
2360 const bool is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002361 const uint32_t dex_pc_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002362 // Whether this instruction must generate the initialization check.
2363 // Used for code generation.
2364 bool generate_clinit_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002365
2366 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
2367};
2368
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002369class HLoadString : public HExpression<0> {
2370 public:
2371 HLoadString(uint32_t string_index, uint32_t dex_pc)
2372 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2373 string_index_(string_index),
2374 dex_pc_(dex_pc) {}
2375
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002376 bool CanBeMoved() const OVERRIDE { return true; }
2377
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002378 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2379 return other->AsLoadString()->string_index_ == string_index_;
2380 }
2381
2382 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
2383
2384 uint32_t GetDexPc() const { return dex_pc_; }
2385 uint32_t GetStringIndex() const { return string_index_; }
2386
2387 // TODO: Can we deopt or debug when we resolve a string?
2388 bool NeedsEnvironment() const OVERRIDE { return false; }
2389
2390 DECLARE_INSTRUCTION(LoadString);
2391
2392 private:
2393 const uint32_t string_index_;
2394 const uint32_t dex_pc_;
2395
2396 DISALLOW_COPY_AND_ASSIGN(HLoadString);
2397};
2398
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002399// TODO: Pass this check to HInvokeStatic nodes.
2400/**
2401 * Performs an initialization check on its Class object input.
2402 */
2403class HClinitCheck : public HExpression<1> {
2404 public:
2405 explicit HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
2406 : HExpression(Primitive::kPrimNot, SideEffects::All()),
2407 dex_pc_(dex_pc) {
2408 SetRawInputAt(0, constant);
2409 }
2410
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002411 bool CanBeMoved() const OVERRIDE { return true; }
2412 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2413 UNUSED(other);
2414 return true;
2415 }
2416
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002417 bool NeedsEnvironment() const OVERRIDE {
2418 // May call runtime to initialize the class.
2419 return true;
2420 }
2421
2422 uint32_t GetDexPc() const { return dex_pc_; }
2423
2424 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
2425
2426 DECLARE_INSTRUCTION(ClinitCheck);
2427
2428 private:
2429 const uint32_t dex_pc_;
2430
2431 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
2432};
2433
2434class HStaticFieldGet : public HExpression<1> {
2435 public:
2436 HStaticFieldGet(HInstruction* cls,
2437 Primitive::Type field_type,
2438 MemberOffset field_offset)
2439 : HExpression(field_type, SideEffects::DependsOnSomething()),
2440 field_info_(field_offset, field_type) {
2441 SetRawInputAt(0, cls);
2442 }
2443
2444 bool CanBeMoved() const OVERRIDE { return true; }
2445 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2446 size_t other_offset = other->AsStaticFieldGet()->GetFieldOffset().SizeValue();
2447 return other_offset == GetFieldOffset().SizeValue();
2448 }
2449
2450 size_t ComputeHashCode() const OVERRIDE {
2451 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
2452 }
2453
2454 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
2455 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
2456
2457 DECLARE_INSTRUCTION(StaticFieldGet);
2458
2459 private:
2460 const FieldInfo field_info_;
2461
2462 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
2463};
2464
2465class HStaticFieldSet : public HTemplateInstruction<2> {
2466 public:
2467 HStaticFieldSet(HInstruction* cls,
2468 HInstruction* value,
2469 Primitive::Type field_type,
2470 MemberOffset field_offset)
2471 : HTemplateInstruction(SideEffects::ChangesSomething()),
2472 field_info_(field_offset, field_type) {
2473 SetRawInputAt(0, cls);
2474 SetRawInputAt(1, value);
2475 }
2476
2477 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
2478 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
2479
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002480 HInstruction* GetValue() const { return InputAt(1); }
2481
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002482 DECLARE_INSTRUCTION(StaticFieldSet);
2483
2484 private:
2485 const FieldInfo field_info_;
2486
2487 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
2488};
2489
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002490// Implement the move-exception DEX instruction.
2491class HLoadException : public HExpression<0> {
2492 public:
2493 HLoadException() : HExpression(Primitive::kPrimNot, SideEffects::None()) {}
2494
2495 DECLARE_INSTRUCTION(LoadException);
2496
2497 private:
2498 DISALLOW_COPY_AND_ASSIGN(HLoadException);
2499};
2500
2501class HThrow : public HTemplateInstruction<1> {
2502 public:
2503 HThrow(HInstruction* exception, uint32_t dex_pc)
2504 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {
2505 SetRawInputAt(0, exception);
2506 }
2507
2508 bool IsControlFlow() const OVERRIDE { return true; }
2509
2510 bool NeedsEnvironment() const OVERRIDE { return true; }
2511
2512 uint32_t GetDexPc() const { return dex_pc_; }
2513
2514 DECLARE_INSTRUCTION(Throw);
2515
2516 private:
2517 uint32_t dex_pc_;
2518
2519 DISALLOW_COPY_AND_ASSIGN(HThrow);
2520};
2521
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002522class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002523 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002524 HInstanceOf(HInstruction* object,
2525 HLoadClass* constant,
2526 bool class_is_final,
2527 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002528 : HExpression(Primitive::kPrimBoolean, SideEffects::None()),
2529 class_is_final_(class_is_final),
2530 dex_pc_(dex_pc) {
2531 SetRawInputAt(0, object);
2532 SetRawInputAt(1, constant);
2533 }
2534
2535 bool CanBeMoved() const OVERRIDE { return true; }
2536
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002537 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002538 return true;
2539 }
2540
2541 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002542 return false;
2543 }
2544
2545 uint32_t GetDexPc() const { return dex_pc_; }
2546
2547 bool IsClassFinal() const { return class_is_final_; }
2548
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002549 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002550
2551 private:
2552 const bool class_is_final_;
2553 const uint32_t dex_pc_;
2554
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002555 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
2556};
2557
2558class HCheckCast : public HTemplateInstruction<2> {
2559 public:
2560 HCheckCast(HInstruction* object,
2561 HLoadClass* constant,
2562 bool class_is_final,
2563 uint32_t dex_pc)
2564 : HTemplateInstruction(SideEffects::None()),
2565 class_is_final_(class_is_final),
2566 dex_pc_(dex_pc) {
2567 SetRawInputAt(0, object);
2568 SetRawInputAt(1, constant);
2569 }
2570
2571 bool CanBeMoved() const OVERRIDE { return true; }
2572
2573 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2574 return true;
2575 }
2576
2577 bool NeedsEnvironment() const OVERRIDE {
2578 // Instruction may throw a CheckCastError.
2579 return true;
2580 }
2581
2582 bool CanThrow() const OVERRIDE { return true; }
2583
2584 uint32_t GetDexPc() const { return dex_pc_; }
2585
2586 bool IsClassFinal() const { return class_is_final_; }
2587
2588 DECLARE_INSTRUCTION(CheckCast);
2589
2590 private:
2591 const bool class_is_final_;
2592 const uint32_t dex_pc_;
2593
2594 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002595};
2596
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00002597class HMonitorOperation : public HTemplateInstruction<1> {
2598 public:
2599 enum OperationKind {
2600 kEnter,
2601 kExit,
2602 };
2603
2604 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
2605 : HTemplateInstruction(SideEffects::None()), kind_(kind), dex_pc_(dex_pc) {
2606 SetRawInputAt(0, object);
2607 }
2608
2609 // Instruction may throw a Java exception, so we need an environment.
2610 bool NeedsEnvironment() const OVERRIDE { return true; }
2611 bool CanThrow() const OVERRIDE { return true; }
2612
2613 uint32_t GetDexPc() const { return dex_pc_; }
2614
2615 bool IsEnter() const { return kind_ == kEnter; }
2616
2617 DECLARE_INSTRUCTION(MonitorOperation);
2618
2619 protected:
2620 const OperationKind kind_;
2621 const uint32_t dex_pc_;
2622
2623 private:
2624 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
2625};
2626
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002627
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002628class MoveOperands : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002629 public:
Nicolas Geoffray740475d2014-09-29 10:33:25 +01002630 MoveOperands(Location source, Location destination, HInstruction* instruction)
2631 : source_(source), destination_(destination), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002632
2633 Location GetSource() const { return source_; }
2634 Location GetDestination() const { return destination_; }
2635
2636 void SetSource(Location value) { source_ = value; }
2637 void SetDestination(Location value) { destination_ = value; }
2638
2639 // The parallel move resolver marks moves as "in-progress" by clearing the
2640 // destination (but not the source).
2641 Location MarkPending() {
2642 DCHECK(!IsPending());
2643 Location dest = destination_;
2644 destination_ = Location::NoLocation();
2645 return dest;
2646 }
2647
2648 void ClearPending(Location dest) {
2649 DCHECK(IsPending());
2650 destination_ = dest;
2651 }
2652
2653 bool IsPending() const {
2654 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
2655 return destination_.IsInvalid() && !source_.IsInvalid();
2656 }
2657
2658 // True if this blocks a move from the given location.
2659 bool Blocks(Location loc) const {
2660 return !IsEliminated() && source_.Equals(loc);
2661 }
2662
2663 // A move is redundant if it's been eliminated, if its source and
2664 // destination are the same, or if its destination is unneeded.
2665 bool IsRedundant() const {
2666 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
2667 }
2668
2669 // We clear both operands to indicate move that's been eliminated.
2670 void Eliminate() {
2671 source_ = destination_ = Location::NoLocation();
2672 }
2673
2674 bool IsEliminated() const {
2675 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
2676 return source_.IsInvalid();
2677 }
2678
Nicolas Geoffray740475d2014-09-29 10:33:25 +01002679 HInstruction* GetInstruction() const { return instruction_; }
2680
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002681 private:
2682 Location source_;
2683 Location destination_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01002684 // The instruction this move is assocatied with. Null when this move is
2685 // for moving an input in the expected locations of user (including a phi user).
2686 // This is only used in debug mode, to ensure we do not connect interval siblings
2687 // in the same parallel move.
2688 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002689
2690 DISALLOW_COPY_AND_ASSIGN(MoveOperands);
2691};
2692
2693static constexpr size_t kDefaultNumberOfMoves = 4;
2694
2695class HParallelMove : public HTemplateInstruction<0> {
2696 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002697 explicit HParallelMove(ArenaAllocator* arena)
2698 : HTemplateInstruction(SideEffects::None()), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002699
2700 void AddMove(MoveOperands* move) {
Nicolas Geoffray740475d2014-09-29 10:33:25 +01002701 if (kIsDebugBuild && move->GetInstruction() != nullptr) {
2702 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
2703 DCHECK_NE(moves_.Get(i)->GetInstruction(), move->GetInstruction())
2704 << "Doing parallel moves for the same instruction.";
2705 }
2706 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002707 moves_.Add(move);
2708 }
2709
2710 MoveOperands* MoveOperandsAt(size_t index) const {
2711 return moves_.Get(index);
2712 }
2713
2714 size_t NumMoves() const { return moves_.Size(); }
2715
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002716 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002717
2718 private:
2719 GrowableArray<MoveOperands*> moves_;
2720
2721 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
2722};
2723
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002724class HGraphVisitor : public ValueObject {
2725 public:
Dave Allison20dfc792014-06-16 20:44:29 -07002726 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
2727 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002728
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002729 virtual void VisitInstruction(HInstruction* instruction) { UNUSED(instruction); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002730 virtual void VisitBasicBlock(HBasicBlock* block);
2731
Roland Levillain633021e2014-10-01 14:12:25 +01002732 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002733 void VisitInsertionOrder();
2734
Roland Levillain633021e2014-10-01 14:12:25 +01002735 // Visit the graph following dominator tree reverse post-order.
2736 void VisitReversePostOrder();
2737
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002738 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002739
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002740 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002741#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002742 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
2743
2744 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
2745
2746#undef DECLARE_VISIT_INSTRUCTION
2747
2748 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07002749 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002750
2751 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
2752};
2753
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002754class HGraphDelegateVisitor : public HGraphVisitor {
2755 public:
2756 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
2757 virtual ~HGraphDelegateVisitor() {}
2758
2759 // Visit functions that delegate to to super class.
2760#define DECLARE_VISIT_INSTRUCTION(name, super) \
2761 virtual void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
2762
2763 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
2764
2765#undef DECLARE_VISIT_INSTRUCTION
2766
2767 private:
2768 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
2769};
2770
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002771class HInsertionOrderIterator : public ValueObject {
2772 public:
2773 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
2774
2775 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
2776 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
2777 void Advance() { ++index_; }
2778
2779 private:
2780 const HGraph& graph_;
2781 size_t index_;
2782
2783 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
2784};
2785
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002786class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002787 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002788 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002789
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002790 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
2791 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002792 void Advance() { ++index_; }
2793
2794 private:
2795 const HGraph& graph_;
2796 size_t index_;
2797
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002798 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002799};
2800
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002801class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002802 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002803 explicit HPostOrderIterator(const HGraph& graph)
2804 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002805
2806 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002807 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002808 void Advance() { --index_; }
2809
2810 private:
2811 const HGraph& graph_;
2812 size_t index_;
2813
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002814 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002815};
2816
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002817} // namespace art
2818
2819#endif // ART_COMPILER_OPTIMIZING_NODES_H_