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