blob: 4706b3beabb56c8e550b728019a4840a3c2eacee [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);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000176 void RemoveDeadBlocks(const ArenaBitVector& visited) const;
177
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000178 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000179
180 // List of blocks in insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000181 GrowableArray<HBasicBlock*> blocks_;
182
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100183 // List of blocks to perform a reverse post order tree traversal.
184 GrowableArray<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000185
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000186 HBasicBlock* entry_block_;
187 HBasicBlock* exit_block_;
188
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100189 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100190 uint16_t maximum_number_of_out_vregs_;
191
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100192 // The number of virtual registers in this method. Contains the parameters.
193 uint16_t number_of_vregs_;
194
195 // The number of virtual registers used by parameters of this method.
196 uint16_t number_of_in_vregs_;
197
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000198 // Number of vreg size slots that the temporaries use (used in baseline compiler).
199 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100200
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000201 // The current id to assign to a newly added instruction. See HInstruction.id_.
202 int current_instruction_id_;
203
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000204 DISALLOW_COPY_AND_ASSIGN(HGraph);
205};
206
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700207class HLoopInformation : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000208 public:
209 HLoopInformation(HBasicBlock* header, HGraph* graph)
210 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100211 suspend_check_(nullptr),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100212 back_edges_(graph->GetArena(), kDefaultNumberOfBackEdges),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100213 // Make bit vector growable, as the number of blocks may change.
214 blocks_(graph->GetArena(), graph->GetBlocks().Size(), true) {}
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100215
216 HBasicBlock* GetHeader() const {
217 return header_;
218 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000219
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100220 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
221 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
222 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
223
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000224 void AddBackEdge(HBasicBlock* back_edge) {
225 back_edges_.Add(back_edge);
226 }
227
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100228 void RemoveBackEdge(HBasicBlock* back_edge) {
229 back_edges_.Delete(back_edge);
230 }
231
232 bool IsBackEdge(HBasicBlock* block) {
233 for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) {
234 if (back_edges_.Get(i) == block) return true;
235 }
236 return false;
237 }
238
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000239 size_t NumberOfBackEdges() const {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000240 return back_edges_.Size();
241 }
242
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100243 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100244
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100245 const GrowableArray<HBasicBlock*>& GetBackEdges() const {
246 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100247 }
248
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100249 void ClearBackEdges() {
250 back_edges_.Reset();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100251 }
252
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100253 // Find blocks that are part of this loop. Returns whether the loop is a natural loop,
254 // that is the header dominates the back edge.
255 bool Populate();
256
257 // Returns whether this loop information contains `block`.
258 // Note that this loop information *must* be populated before entering this function.
259 bool Contains(const HBasicBlock& block) const;
260
261 // Returns whether this loop information is an inner loop of `other`.
262 // Note that `other` *must* be populated before entering this function.
263 bool IsIn(const HLoopInformation& other) const;
264
265 const ArenaBitVector& GetBlocks() const { return blocks_; }
266
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000267 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100268 // Internal recursive implementation of `Populate`.
269 void PopulateRecursive(HBasicBlock* block);
270
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000271 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100272 HSuspendCheck* suspend_check_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000273 GrowableArray<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100274 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000275
276 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
277};
278
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100279static constexpr size_t kNoLifetime = -1;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100280static constexpr uint32_t kNoDexPc = -1;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100281
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000282// A block in a method. Contains the list of instructions represented
283// as a double linked list. Each block knows its predecessors and
284// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100285
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700286class HBasicBlock : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000287 public:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100288 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000289 : graph_(graph),
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000290 predecessors_(graph->GetArena(), kDefaultNumberOfPredecessors),
291 successors_(graph->GetArena(), kDefaultNumberOfSuccessors),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000292 loop_information_(nullptr),
293 dominator_(nullptr),
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100294 dominated_blocks_(graph->GetArena(), kDefaultNumberOfDominatedBlocks),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100295 block_id_(-1),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100296 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100297 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000298 lifetime_end_(kNoLifetime),
299 is_catch_block_(false) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000300
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100301 const GrowableArray<HBasicBlock*>& GetPredecessors() const {
302 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000303 }
304
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100305 const GrowableArray<HBasicBlock*>& GetSuccessors() const {
306 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000307 }
308
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100309 const GrowableArray<HBasicBlock*>& GetDominatedBlocks() const {
310 return dominated_blocks_;
311 }
312
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100313 bool IsEntryBlock() const {
314 return graph_->GetEntryBlock() == this;
315 }
316
317 bool IsExitBlock() const {
318 return graph_->GetExitBlock() == this;
319 }
320
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000321 void AddBackEdge(HBasicBlock* back_edge) {
322 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000323 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000324 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100325 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000326 loop_information_->AddBackEdge(back_edge);
327 }
328
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000329 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000330
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000331 int GetBlockId() const { return block_id_; }
332 void SetBlockId(int id) { block_id_ = id; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000333
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000334 HBasicBlock* GetDominator() const { return dominator_; }
335 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100336 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.Add(block); }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000337
338 int NumberOfBackEdges() const {
339 return loop_information_ == nullptr
340 ? 0
341 : loop_information_->NumberOfBackEdges();
342 }
343
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100344 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
345 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100346 const HInstructionList& GetInstructions() const { return instructions_; }
347 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100348 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000349
350 void AddSuccessor(HBasicBlock* block) {
351 successors_.Add(block);
352 block->predecessors_.Add(this);
353 }
354
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100355 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
356 size_t successor_index = GetSuccessorIndexOf(existing);
357 DCHECK_NE(successor_index, static_cast<size_t>(-1));
358 existing->RemovePredecessor(this);
359 new_block->predecessors_.Add(this);
360 successors_.Put(successor_index, new_block);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000361 }
362
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100363 void RemovePredecessor(HBasicBlock* block) {
364 predecessors_.Delete(block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100365 }
366
367 void ClearAllPredecessors() {
368 predecessors_.Reset();
369 }
370
371 void AddPredecessor(HBasicBlock* block) {
372 predecessors_.Add(block);
373 block->successors_.Add(this);
374 }
375
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100376 void SwapPredecessors() {
Nicolas Geoffrayc83d4412014-09-18 16:46:20 +0100377 DCHECK_EQ(predecessors_.Size(), 2u);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100378 HBasicBlock* temp = predecessors_.Get(0);
379 predecessors_.Put(0, predecessors_.Get(1));
380 predecessors_.Put(1, temp);
381 }
382
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100383 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) {
384 for (size_t i = 0, e = predecessors_.Size(); i < e; ++i) {
385 if (predecessors_.Get(i) == predecessor) {
386 return i;
387 }
388 }
389 return -1;
390 }
391
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100392 size_t GetSuccessorIndexOf(HBasicBlock* successor) {
393 for (size_t i = 0, e = successors_.Size(); i < e; ++i) {
394 if (successors_.Get(i) == successor) {
395 return i;
396 }
397 }
398 return -1;
399 }
400
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000401 void AddInstruction(HInstruction* instruction);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100402 void RemoveInstruction(HInstruction* instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100403 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +0100404 // Replace instruction `initial` with `replacement` within this block.
405 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
406 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100407 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100408 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100409 void RemovePhi(HPhi* phi);
410
411 bool IsLoopHeader() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100412 return (loop_information_ != nullptr) && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100413 }
414
Roland Levillain6b879dd2014-09-22 17:13:44 +0100415 bool IsLoopPreHeaderFirstPredecessor() const {
416 DCHECK(IsLoopHeader());
417 DCHECK(!GetPredecessors().IsEmpty());
418 return GetPredecessors().Get(0) == GetLoopInformation()->GetPreHeader();
419 }
420
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100421 HLoopInformation* GetLoopInformation() const {
422 return loop_information_;
423 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000424
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100425 // Set the loop_information_ on this block. This method overrides the current
426 // loop_information if it is an outer loop of the passed loop information.
427 void SetInLoop(HLoopInformation* info) {
428 if (IsLoopHeader()) {
429 // Nothing to do. This just means `info` is an outer loop.
430 } else if (loop_information_ == nullptr) {
431 loop_information_ = info;
432 } else if (loop_information_->Contains(*info->GetHeader())) {
433 // Block is currently part of an outer loop. Make it part of this inner loop.
434 // Note that a non loop header having a loop information means this loop information
435 // has already been populated
436 loop_information_ = info;
437 } else {
438 // Block is part of an inner loop. Do not update the loop information.
439 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
440 // at this point, because this method is being called while populating `info`.
441 }
442 }
443
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100444 bool IsInLoop() const { return loop_information_ != nullptr; }
445
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100446 // Returns wheter this block dominates the blocked passed as parameter.
447 bool Dominates(HBasicBlock* block) const;
448
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100449 size_t GetLifetimeStart() const { return lifetime_start_; }
450 size_t GetLifetimeEnd() const { return lifetime_end_; }
451
452 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
453 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
454
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100455 uint32_t GetDexPc() const { return dex_pc_; }
456
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000457 bool IsCatchBlock() const { return is_catch_block_; }
458 void SetIsCatchBlock() { is_catch_block_ = true; }
459
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000460 private:
461 HGraph* const graph_;
462 GrowableArray<HBasicBlock*> predecessors_;
463 GrowableArray<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100464 HInstructionList instructions_;
465 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000466 HLoopInformation* loop_information_;
467 HBasicBlock* dominator_;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100468 GrowableArray<HBasicBlock*> dominated_blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000469 int block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100470 // The dex program counter of the first instruction of this block.
471 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100472 size_t lifetime_start_;
473 size_t lifetime_end_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000474 bool is_catch_block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000475
476 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
477};
478
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100479#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
480 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000481 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000482 M(ArrayGet, Instruction) \
483 M(ArrayLength, Instruction) \
484 M(ArraySet, Instruction) \
485 M(BoundsCheck, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000486 M(CheckCast, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100487 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000488 M(Compare, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100489 M(Condition, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000490 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +0000491 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000492 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100493 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000494 M(Exit, Instruction) \
495 M(FloatConstant, Constant) \
496 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100497 M(GreaterThan, Condition) \
498 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100499 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000500 M(InstanceFieldGet, Instruction) \
501 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000502 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100503 M(IntConstant, Constant) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +0000504 M(InvokeInterface, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100505 M(InvokeStatic, Invoke) \
506 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000507 M(LessThan, Condition) \
508 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000509 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000510 M(LoadException, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100511 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000512 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100513 M(Local, Instruction) \
514 M(LongConstant, Constant) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +0000515 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000516 M(Mul, BinaryOperation) \
517 M(Neg, UnaryOperation) \
518 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100519 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +0100520 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000521 M(NotEqual, Condition) \
522 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000523 M(Or, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100524 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000525 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100526 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000527 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100528 M(Return, Instruction) \
529 M(ReturnVoid, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000530 M(Shl, BinaryOperation) \
531 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100532 M(StaticFieldGet, Instruction) \
533 M(StaticFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100534 M(StoreLocal, Instruction) \
535 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100536 M(SuspendCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000537 M(Temporary, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000538 M(Throw, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +0000539 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000540 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000541 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000542
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100543#define FOR_EACH_INSTRUCTION(M) \
544 FOR_EACH_CONCRETE_INSTRUCTION(M) \
545 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +0100546 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +0100547 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100548 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -0700549
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100550#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000551FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
552#undef FORWARD_DECLARATION
553
Roland Levillainccc07a92014-09-16 14:48:16 +0100554#define DECLARE_INSTRUCTION(type) \
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100555 virtual InstructionKind GetKind() const { return k##type; } \
Roland Levillainccc07a92014-09-16 14:48:16 +0100556 virtual const char* DebugName() const { return #type; } \
557 virtual const H##type* As##type() const OVERRIDE { return this; } \
558 virtual H##type* As##type() OVERRIDE { return this; } \
559 virtual bool InstructionTypeEquals(HInstruction* other) const { \
560 return other->Is##type(); \
561 } \
562 virtual void Accept(HGraphVisitor* visitor)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000563
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100564template <typename T>
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700565class HUseListNode : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000566 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100567 HUseListNode(T* user, size_t index, HUseListNode* tail)
Dave Allison20dfc792014-06-16 20:44:29 -0700568 : user_(user), index_(index), tail_(tail) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000569
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000570 HUseListNode* GetTail() const { return tail_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100571 T* GetUser() const { return user_; }
572 size_t GetIndex() const { return index_; }
573
574 void SetTail(HUseListNode<T>* node) { tail_ = node; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000575
576 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100577 T* const user_;
578 const size_t index_;
579 HUseListNode<T>* tail_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000580
581 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
582};
583
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100584// Represents the side effects an instruction may have.
585class SideEffects : public ValueObject {
586 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100587 SideEffects() : flags_(0) {}
588
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100589 static SideEffects None() {
590 return SideEffects(0);
591 }
592
593 static SideEffects All() {
594 return SideEffects(ChangesSomething().flags_ | DependsOnSomething().flags_);
595 }
596
597 static SideEffects ChangesSomething() {
598 return SideEffects((1 << kFlagChangesCount) - 1);
599 }
600
601 static SideEffects DependsOnSomething() {
602 int count = kFlagDependsOnCount - kFlagChangesCount;
603 return SideEffects(((1 << count) - 1) << kFlagChangesCount);
604 }
605
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100606 SideEffects Union(SideEffects other) const {
607 return SideEffects(flags_ | other.flags_);
608 }
609
Roland Levillain72bceff2014-09-15 18:29:00 +0100610 bool HasSideEffects() const {
611 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
612 return (flags_ & all_bits_set) != 0;
613 }
614
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100615 bool HasAllSideEffects() const {
616 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
617 return all_bits_set == (flags_ & all_bits_set);
618 }
619
620 bool DependsOn(SideEffects other) const {
621 size_t depends_flags = other.ComputeDependsFlags();
622 return (flags_ & depends_flags) != 0;
623 }
624
625 bool HasDependencies() const {
626 int count = kFlagDependsOnCount - kFlagChangesCount;
627 size_t all_bits_set = (1 << count) - 1;
628 return ((flags_ >> kFlagChangesCount) & all_bits_set) != 0;
629 }
630
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100631 private:
632 static constexpr int kFlagChangesSomething = 0;
633 static constexpr int kFlagChangesCount = kFlagChangesSomething + 1;
634
635 static constexpr int kFlagDependsOnSomething = kFlagChangesCount;
636 static constexpr int kFlagDependsOnCount = kFlagDependsOnSomething + 1;
637
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100638 explicit SideEffects(size_t flags) : flags_(flags) {}
639
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100640 size_t ComputeDependsFlags() const {
641 return flags_ << kFlagChangesCount;
642 }
643
644 size_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100645};
646
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700647class HInstruction : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000648 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100649 explicit HInstruction(SideEffects side_effects)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000650 : previous_(nullptr),
651 next_(nullptr),
652 block_(nullptr),
653 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100654 ssa_index_(-1),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000655 uses_(nullptr),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100656 env_uses_(nullptr),
657 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100658 locations_(nullptr),
659 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100660 lifetime_position_(kNoLifetime),
661 side_effects_(side_effects) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000662
Dave Allison20dfc792014-06-16 20:44:29 -0700663 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000664
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100665#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100666 enum InstructionKind {
667 FOR_EACH_INSTRUCTION(DECLARE_KIND)
668 };
669#undef DECLARE_KIND
670
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000671 HInstruction* GetNext() const { return next_; }
672 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000673
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000674 HBasicBlock* GetBlock() const { return block_; }
675 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100676 bool IsInBlock() const { return block_ != nullptr; }
677 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +0100678 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000679
Roland Levillain6b879dd2014-09-22 17:13:44 +0100680 virtual size_t InputCount() const = 0;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100681 virtual HInstruction* InputAt(size_t i) const = 0;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000682
683 virtual void Accept(HGraphVisitor* visitor) = 0;
684 virtual const char* DebugName() const = 0;
685
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100686 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100687 virtual void SetRawInputAt(size_t index, HInstruction* input) = 0;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100688
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100689 virtual bool NeedsEnvironment() const { return false; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100690 virtual bool IsControlFlow() const { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +0100691 virtual bool CanThrow() const { return false; }
Roland Levillain72bceff2014-09-15 18:29:00 +0100692 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100693
694 void AddUseAt(HInstruction* user, size_t index) {
695 uses_ = new (block_->GetGraph()->GetArena()) HUseListNode<HInstruction>(user, index, uses_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000696 }
697
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100698 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +0100699 DCHECK(user != nullptr);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100700 env_uses_ = new (block_->GetGraph()->GetArena()) HUseListNode<HEnvironment>(
701 user, index, env_uses_);
702 }
703
704 void RemoveUser(HInstruction* user, size_t index);
Nicolas Geoffray724c9632014-09-22 12:27:27 +0100705 void RemoveEnvironmentUser(HEnvironment* user, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100706
707 HUseListNode<HInstruction>* GetUses() const { return uses_; }
708 HUseListNode<HEnvironment>* GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000709
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100710 bool HasUses() const { return uses_ != nullptr || env_uses_ != nullptr; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100711 bool HasEnvironmentUses() const { return env_uses_ != nullptr; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000712
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100713 size_t NumberOfUses() const {
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100714 // TODO: Optimize this method if it is used outside of the HGraphVisualizer.
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100715 size_t result = 0;
716 HUseListNode<HInstruction>* current = uses_;
717 while (current != nullptr) {
718 current = current->GetTail();
719 ++result;
720 }
721 return result;
722 }
723
Roland Levillain6c82d402014-10-13 16:10:27 +0100724 // Does this instruction strictly dominate `other_instruction`?
725 // Returns false if this instruction and `other_instruction` are the same.
726 // Aborts if this instruction and `other_instruction` are both phis.
727 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +0100728
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000729 int GetId() const { return id_; }
730 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000731
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100732 int GetSsaIndex() const { return ssa_index_; }
733 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
734 bool HasSsaIndex() const { return ssa_index_ != -1; }
735
736 bool HasEnvironment() const { return environment_ != nullptr; }
737 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100738 void SetEnvironment(HEnvironment* environment) { environment_ = environment; }
739
Nicolas Geoffray39468442014-09-02 15:17:15 +0100740 // Returns the number of entries in the environment. Typically, that is the
741 // number of dex registers in a method. It could be more in case of inlining.
742 size_t EnvironmentSize() const;
743
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000744 LocationSummary* GetLocations() const { return locations_; }
745 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000746
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100747 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100748 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100749
Dave Allison20dfc792014-06-16 20:44:29 -0700750 bool HasOnlyOneUse() const {
751 return uses_ != nullptr && uses_->GetTail() == nullptr;
752 }
753
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100754#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +0100755 bool Is##type() const { return (As##type() != nullptr); } \
756 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000757 virtual H##type* As##type() { return nullptr; }
758
759 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
760#undef INSTRUCTION_TYPE_CHECK
761
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100762 // Returns whether the instruction can be moved within the graph.
763 virtual bool CanBeMoved() const { return false; }
764
765 // Returns whether the two instructions are of the same kind.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700766 virtual bool InstructionTypeEquals(HInstruction* other) const {
767 UNUSED(other);
768 return false;
769 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100770
771 // Returns whether any data encoded in the two instructions is equal.
772 // This method does not look at the inputs. Both instructions must be
773 // of the same type, otherwise the method has undefined behavior.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700774 virtual bool InstructionDataEquals(HInstruction* other) const {
775 UNUSED(other);
776 return false;
777 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100778
779 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +0000780 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100781 // 2) Their inputs are identical.
782 bool Equals(HInstruction* other) const;
783
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100784 virtual InstructionKind GetKind() const = 0;
785
786 virtual size_t ComputeHashCode() const {
787 size_t result = GetKind();
788 for (size_t i = 0, e = InputCount(); i < e; ++i) {
789 result = (result * 31) + InputAt(i)->GetId();
790 }
791 return result;
792 }
793
794 SideEffects GetSideEffects() const { return side_effects_; }
795
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100796 size_t GetLifetimePosition() const { return lifetime_position_; }
797 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
798 LiveInterval* GetLiveInterval() const { return live_interval_; }
799 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
800 bool HasLiveInterval() const { return live_interval_ != nullptr; }
801
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000802 private:
803 HInstruction* previous_;
804 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000805 HBasicBlock* block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000806
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000807 // An instruction gets an id when it is added to the graph.
808 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +0100809 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000810 int id_;
811
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100812 // When doing liveness analysis, instructions that have uses get an SSA index.
813 int ssa_index_;
814
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100815 // List of instructions that have this instruction as input.
816 HUseListNode<HInstruction>* uses_;
817
818 // List of environments that contain this instruction.
819 HUseListNode<HEnvironment>* env_uses_;
820
Nicolas Geoffray39468442014-09-02 15:17:15 +0100821 // The environment associated with this instruction. Not null if the instruction
822 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100823 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000824
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000825 // Set by the code generator.
826 LocationSummary* locations_;
827
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100828 // Set by the liveness analysis.
829 LiveInterval* live_interval_;
830
831 // Set by the liveness analysis, this is the position in a linear
832 // order of blocks where this instruction's live interval start.
833 size_t lifetime_position_;
834
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100835 const SideEffects side_effects_;
836
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000837 friend class HBasicBlock;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100838 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000839
840 DISALLOW_COPY_AND_ASSIGN(HInstruction);
841};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700842std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000843
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100844template<typename T>
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000845class HUseIterator : public ValueObject {
846 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100847 explicit HUseIterator(HUseListNode<T>* uses) : current_(uses) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000848
849 bool Done() const { return current_ == nullptr; }
850
851 void Advance() {
852 DCHECK(!Done());
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000853 current_ = current_->GetTail();
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000854 }
855
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100856 HUseListNode<T>* Current() const {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000857 DCHECK(!Done());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100858 return current_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000859 }
860
861 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100862 HUseListNode<T>* current_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000863
864 friend class HValue;
865};
866
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100867// A HEnvironment object contains the values of virtual registers at a given location.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700868class HEnvironment : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100869 public:
870 HEnvironment(ArenaAllocator* arena, size_t number_of_vregs) : vregs_(arena, number_of_vregs) {
871 vregs_.SetSize(number_of_vregs);
872 for (size_t i = 0; i < number_of_vregs; i++) {
873 vregs_.Put(i, nullptr);
874 }
875 }
876
877 void Populate(const GrowableArray<HInstruction*>& env) {
878 for (size_t i = 0; i < env.Size(); i++) {
879 HInstruction* instruction = env.Get(i);
880 vregs_.Put(i, instruction);
881 if (instruction != nullptr) {
882 instruction->AddEnvUseAt(this, i);
883 }
884 }
885 }
886
887 void SetRawEnvAt(size_t index, HInstruction* instruction) {
888 vregs_.Put(index, instruction);
889 }
890
Nicolas Geoffray39468442014-09-02 15:17:15 +0100891 HInstruction* GetInstructionAt(size_t index) const {
892 return vregs_.Get(index);
893 }
894
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100895 GrowableArray<HInstruction*>* GetVRegs() {
896 return &vregs_;
897 }
898
Nicolas Geoffray39468442014-09-02 15:17:15 +0100899 size_t Size() const { return vregs_.Size(); }
900
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100901 private:
902 GrowableArray<HInstruction*> vregs_;
903
904 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
905};
906
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000907class HInputIterator : public ValueObject {
908 public:
Dave Allison20dfc792014-06-16 20:44:29 -0700909 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000910
911 bool Done() const { return index_ == instruction_->InputCount(); }
912 HInstruction* Current() const { return instruction_->InputAt(index_); }
913 void Advance() { index_++; }
914
915 private:
916 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100917 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000918
919 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
920};
921
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000922class HInstructionIterator : public ValueObject {
923 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100924 explicit HInstructionIterator(const HInstructionList& instructions)
925 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000926 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000927 }
928
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000929 bool Done() const { return instruction_ == nullptr; }
930 HInstruction* Current() const { return instruction_; }
931 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000932 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000933 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000934 }
935
936 private:
937 HInstruction* instruction_;
938 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100939
940 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000941};
942
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100943class HBackwardInstructionIterator : public ValueObject {
944 public:
945 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
946 : instruction_(instructions.last_instruction_) {
947 next_ = Done() ? nullptr : instruction_->GetPrevious();
948 }
949
950 bool Done() const { return instruction_ == nullptr; }
951 HInstruction* Current() const { return instruction_; }
952 void Advance() {
953 instruction_ = next_;
954 next_ = Done() ? nullptr : instruction_->GetPrevious();
955 }
956
957 private:
958 HInstruction* instruction_;
959 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100960
961 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100962};
963
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000964// An embedded container with N elements of type T. Used (with partial
965// specialization for N=0) because embedded arrays cannot have size 0.
966template<typename T, intptr_t N>
967class EmbeddedArray {
968 public:
Dave Allison20dfc792014-06-16 20:44:29 -0700969 EmbeddedArray() : elements_() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000970
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000971 intptr_t GetLength() const { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000972
973 const T& operator[](intptr_t i) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000974 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000975 return elements_[i];
976 }
977
978 T& operator[](intptr_t i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000979 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000980 return elements_[i];
981 }
982
983 const T& At(intptr_t i) const {
984 return (*this)[i];
985 }
986
987 void SetAt(intptr_t i, const T& val) {
988 (*this)[i] = val;
989 }
990
991 private:
992 T elements_[N];
993};
994
995template<typename T>
996class EmbeddedArray<T, 0> {
997 public:
998 intptr_t length() const { return 0; }
999 const T& operator[](intptr_t i) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001000 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001001 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001002 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001003 }
1004 T& operator[](intptr_t i) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001005 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001006 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001007 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001008 }
1009};
1010
1011template<intptr_t N>
1012class HTemplateInstruction: public HInstruction {
1013 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001014 HTemplateInstruction<N>(SideEffects side_effects)
1015 : HInstruction(side_effects), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07001016 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001017
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001018 virtual size_t InputCount() const { return N; }
1019 virtual HInstruction* InputAt(size_t i) const { return inputs_[i]; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001020
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001021 protected:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001022 virtual void SetRawInputAt(size_t i, HInstruction* instruction) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001023 inputs_[i] = instruction;
1024 }
1025
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001026 private:
1027 EmbeddedArray<HInstruction*, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001028
1029 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001030};
1031
Dave Allison20dfc792014-06-16 20:44:29 -07001032template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001033class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07001034 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001035 HExpression<N>(Primitive::Type type, SideEffects side_effects)
1036 : HTemplateInstruction<N>(side_effects), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07001037 virtual ~HExpression() {}
1038
1039 virtual Primitive::Type GetType() const { return type_; }
1040
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001041 protected:
1042 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07001043};
1044
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001045// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
1046// instruction that branches to the exit block.
1047class HReturnVoid : public HTemplateInstruction<0> {
1048 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001049 HReturnVoid() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001050
1051 virtual bool IsControlFlow() const { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001052
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001053 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001054
1055 private:
1056 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
1057};
1058
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001059// Represents dex's RETURN opcodes. A HReturn is a control flow
1060// instruction that branches to the exit block.
1061class HReturn : public HTemplateInstruction<1> {
1062 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001063 explicit HReturn(HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001064 SetRawInputAt(0, value);
1065 }
1066
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001067 virtual bool IsControlFlow() const { return true; }
1068
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001069 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001070
1071 private:
1072 DISALLOW_COPY_AND_ASSIGN(HReturn);
1073};
1074
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001075// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001076// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001077// exit block.
1078class HExit : public HTemplateInstruction<0> {
1079 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001080 HExit() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001081
1082 virtual bool IsControlFlow() const { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001083
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001084 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001085
1086 private:
1087 DISALLOW_COPY_AND_ASSIGN(HExit);
1088};
1089
1090// Jumps from one block to another.
1091class HGoto : public HTemplateInstruction<0> {
1092 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001093 HGoto() : HTemplateInstruction(SideEffects::None()) {}
1094
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001095 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001096
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001097 HBasicBlock* GetSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001098 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001099 }
1100
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001101 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001102
1103 private:
1104 DISALLOW_COPY_AND_ASSIGN(HGoto);
1105};
1106
Dave Allison20dfc792014-06-16 20:44:29 -07001107
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001108// Conditional branch. A block ending with an HIf instruction must have
1109// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001110class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001111 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001112 explicit HIf(HInstruction* input) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001113 SetRawInputAt(0, input);
1114 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001115
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001116 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001117
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001118 HBasicBlock* IfTrueSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001119 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001120 }
1121
1122 HBasicBlock* IfFalseSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001123 return GetBlock()->GetSuccessors().Get(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001124 }
1125
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001126 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001127
Dave Allison20dfc792014-06-16 20:44:29 -07001128 virtual bool IsIfInstruction() const { return true; }
1129
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001130 private:
1131 DISALLOW_COPY_AND_ASSIGN(HIf);
1132};
1133
Roland Levillain88cb1752014-10-20 16:36:47 +01001134class HUnaryOperation : public HExpression<1> {
1135 public:
1136 HUnaryOperation(Primitive::Type result_type, HInstruction* input)
1137 : HExpression(result_type, SideEffects::None()) {
1138 SetRawInputAt(0, input);
1139 }
1140
1141 HInstruction* GetInput() const { return InputAt(0); }
1142 Primitive::Type GetResultType() const { return GetType(); }
1143
1144 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001145 virtual bool InstructionDataEquals(HInstruction* other) const {
1146 UNUSED(other);
1147 return true;
1148 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001149
Roland Levillain9240d6a2014-10-20 16:47:04 +01001150 // Try to statically evaluate `operation` and return a HConstant
1151 // containing the result of this evaluation. If `operation` cannot
1152 // be evaluated as a constant, return nullptr.
1153 HConstant* TryStaticEvaluation() const;
1154
1155 // Apply this operation to `x`.
1156 virtual int32_t Evaluate(int32_t x) const = 0;
1157 virtual int64_t Evaluate(int64_t x) const = 0;
1158
Roland Levillain88cb1752014-10-20 16:36:47 +01001159 DECLARE_INSTRUCTION(UnaryOperation);
1160
1161 private:
1162 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
1163};
1164
Dave Allison20dfc792014-06-16 20:44:29 -07001165class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001166 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001167 HBinaryOperation(Primitive::Type result_type,
1168 HInstruction* left,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001169 HInstruction* right) : HExpression(result_type, SideEffects::None()) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001170 SetRawInputAt(0, left);
1171 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001172 }
1173
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001174 HInstruction* GetLeft() const { return InputAt(0); }
1175 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07001176 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001177
1178 virtual bool IsCommutative() { return false; }
1179
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001180 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001181 virtual bool InstructionDataEquals(HInstruction* other) const {
1182 UNUSED(other);
1183 return true;
1184 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001185
Roland Levillain9240d6a2014-10-20 16:47:04 +01001186 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01001187 // containing the result of this evaluation. If `operation` cannot
1188 // be evaluated as a constant, return nullptr.
Roland Levillain9240d6a2014-10-20 16:47:04 +01001189 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01001190
1191 // Apply this operation to `x` and `y`.
1192 virtual int32_t Evaluate(int32_t x, int32_t y) const = 0;
1193 virtual int64_t Evaluate(int64_t x, int64_t y) const = 0;
1194
Roland Levillainccc07a92014-09-16 14:48:16 +01001195 DECLARE_INSTRUCTION(BinaryOperation);
1196
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001197 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001198 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
1199};
1200
Dave Allison20dfc792014-06-16 20:44:29 -07001201class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001202 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001203 HCondition(HInstruction* first, HInstruction* second)
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001204 : HBinaryOperation(Primitive::kPrimBoolean, first, second),
1205 needs_materialization_(true) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001206
1207 virtual bool IsCommutative() { return true; }
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001208
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001209 bool NeedsMaterialization() const { return needs_materialization_; }
1210 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001211
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001212 // For code generation purposes, returns whether this instruction is just before
1213 // `if_`, and disregard moves in between.
1214 bool IsBeforeWhenDisregardMoves(HIf* if_) const;
1215
Dave Allison20dfc792014-06-16 20:44:29 -07001216 DECLARE_INSTRUCTION(Condition);
1217
1218 virtual IfCondition GetCondition() const = 0;
1219
1220 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001221 // For register allocation purposes, returns whether this instruction needs to be
1222 // materialized (that is, not just be in the processor flags).
1223 bool needs_materialization_;
1224
Dave Allison20dfc792014-06-16 20:44:29 -07001225 DISALLOW_COPY_AND_ASSIGN(HCondition);
1226};
1227
1228// Instruction to check if two inputs are equal to each other.
1229class HEqual : public HCondition {
1230 public:
1231 HEqual(HInstruction* first, HInstruction* second)
1232 : HCondition(first, second) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001233
Roland Levillain93445682014-10-06 19:24:02 +01001234 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1235 return x == y ? 1 : 0;
1236 }
1237 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1238 return x == y ? 1 : 0;
1239 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001240
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001241 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001242
Dave Allison20dfc792014-06-16 20:44:29 -07001243 virtual IfCondition GetCondition() const {
1244 return kCondEQ;
1245 }
1246
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001247 private:
1248 DISALLOW_COPY_AND_ASSIGN(HEqual);
1249};
1250
Dave Allison20dfc792014-06-16 20:44:29 -07001251class HNotEqual : public HCondition {
1252 public:
1253 HNotEqual(HInstruction* first, HInstruction* second)
1254 : HCondition(first, second) {}
1255
Roland Levillain93445682014-10-06 19:24:02 +01001256 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1257 return x != y ? 1 : 0;
1258 }
1259 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1260 return x != y ? 1 : 0;
1261 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001262
Dave Allison20dfc792014-06-16 20:44:29 -07001263 DECLARE_INSTRUCTION(NotEqual);
1264
1265 virtual IfCondition GetCondition() const {
1266 return kCondNE;
1267 }
1268
1269 private:
1270 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
1271};
1272
1273class HLessThan : public HCondition {
1274 public:
1275 HLessThan(HInstruction* first, HInstruction* second)
1276 : HCondition(first, second) {}
1277
Roland Levillain93445682014-10-06 19:24:02 +01001278 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1279 return x < y ? 1 : 0;
1280 }
1281 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1282 return x < y ? 1 : 0;
1283 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001284
Dave Allison20dfc792014-06-16 20:44:29 -07001285 DECLARE_INSTRUCTION(LessThan);
1286
1287 virtual IfCondition GetCondition() const {
1288 return kCondLT;
1289 }
1290
1291 private:
1292 DISALLOW_COPY_AND_ASSIGN(HLessThan);
1293};
1294
1295class HLessThanOrEqual : public HCondition {
1296 public:
1297 HLessThanOrEqual(HInstruction* first, HInstruction* second)
1298 : HCondition(first, second) {}
1299
Roland Levillain93445682014-10-06 19:24:02 +01001300 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1301 return x <= y ? 1 : 0;
1302 }
1303 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1304 return x <= y ? 1 : 0;
1305 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001306
Dave Allison20dfc792014-06-16 20:44:29 -07001307 DECLARE_INSTRUCTION(LessThanOrEqual);
1308
1309 virtual IfCondition GetCondition() const {
1310 return kCondLE;
1311 }
1312
1313 private:
1314 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
1315};
1316
1317class HGreaterThan : public HCondition {
1318 public:
1319 HGreaterThan(HInstruction* first, HInstruction* second)
1320 : HCondition(first, second) {}
1321
Roland Levillain93445682014-10-06 19:24:02 +01001322 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1323 return x > y ? 1 : 0;
1324 }
1325 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1326 return x > y ? 1 : 0;
1327 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001328
Dave Allison20dfc792014-06-16 20:44:29 -07001329 DECLARE_INSTRUCTION(GreaterThan);
1330
1331 virtual IfCondition GetCondition() const {
1332 return kCondGT;
1333 }
1334
1335 private:
1336 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
1337};
1338
1339class HGreaterThanOrEqual : public HCondition {
1340 public:
1341 HGreaterThanOrEqual(HInstruction* first, HInstruction* second)
1342 : HCondition(first, second) {}
1343
Roland Levillain93445682014-10-06 19:24:02 +01001344 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1345 return x >= y ? 1 : 0;
1346 }
1347 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1348 return x >= y ? 1 : 0;
1349 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001350
Dave Allison20dfc792014-06-16 20:44:29 -07001351 DECLARE_INSTRUCTION(GreaterThanOrEqual);
1352
1353 virtual IfCondition GetCondition() const {
1354 return kCondGE;
1355 }
1356
1357 private:
1358 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
1359};
1360
1361
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001362// Instruction to check how two inputs compare to each other.
1363// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
1364class HCompare : public HBinaryOperation {
1365 public:
Calin Juravleddb7df22014-11-25 20:56:51 +00001366 // The bias applies for floating point operations and indicates how NaN
1367 // comparisons are treated:
1368 enum Bias {
1369 kNoBias, // bias is not applicable (i.e. for long operation)
1370 kGtBias, // return 1 for NaN comparisons
1371 kLtBias, // return -1 for NaN comparisons
1372 };
1373
1374 HCompare(Primitive::Type type, HInstruction* first, HInstruction* second, Bias bias)
1375 : HBinaryOperation(Primitive::kPrimInt, first, second), bias_(bias) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001376 DCHECK_EQ(type, first->GetType());
1377 DCHECK_EQ(type, second->GetType());
1378 }
1379
Calin Juravleddb7df22014-11-25 20:56:51 +00001380 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001381 return
1382 x == y ? 0 :
1383 x > y ? 1 :
1384 -1;
1385 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001386
1387 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001388 return
1389 x == y ? 0 :
1390 x > y ? 1 :
1391 -1;
1392 }
1393
Calin Juravleddb7df22014-11-25 20:56:51 +00001394 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
1395 return bias_ == other->AsCompare()->bias_;
1396 }
1397
1398 bool IsGtBias() { return bias_ == kGtBias; }
1399
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001400 DECLARE_INSTRUCTION(Compare);
1401
1402 private:
Calin Juravleddb7df22014-11-25 20:56:51 +00001403 const Bias bias_;
1404
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001405 DISALLOW_COPY_AND_ASSIGN(HCompare);
1406};
1407
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001408// A local in the graph. Corresponds to a Dex register.
1409class HLocal : public HTemplateInstruction<0> {
1410 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001411 explicit HLocal(uint16_t reg_number)
1412 : HTemplateInstruction(SideEffects::None()), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001413
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001414 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001415
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001416 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001417
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001418 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001419 // The Dex register number.
1420 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001421
1422 DISALLOW_COPY_AND_ASSIGN(HLocal);
1423};
1424
1425// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07001426class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001427 public:
Roland Levillain5799fc02014-09-25 12:15:20 +01001428 HLoadLocal(HLocal* local, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001429 : HExpression(type, SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001430 SetRawInputAt(0, local);
1431 }
1432
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001433 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1434
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001435 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001436
1437 private:
1438 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
1439};
1440
1441// Store a value in a given local. This instruction has two inputs: the value
1442// and the local.
1443class HStoreLocal : public HTemplateInstruction<2> {
1444 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001445 HStoreLocal(HLocal* local, HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001446 SetRawInputAt(0, local);
1447 SetRawInputAt(1, value);
1448 }
1449
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001450 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1451
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001452 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001453
1454 private:
1455 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
1456};
1457
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001458class HConstant : public HExpression<0> {
1459 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001460 explicit HConstant(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
1461
1462 virtual bool CanBeMoved() const { return true; }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001463
1464 DECLARE_INSTRUCTION(Constant);
1465
1466 private:
1467 DISALLOW_COPY_AND_ASSIGN(HConstant);
1468};
1469
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001470class HFloatConstant : public HConstant {
1471 public:
1472 explicit HFloatConstant(float value) : HConstant(Primitive::kPrimFloat), value_(value) {}
1473
1474 float GetValue() const { return value_; }
1475
1476 virtual bool InstructionDataEquals(HInstruction* other) const {
1477 return bit_cast<float, int32_t>(other->AsFloatConstant()->value_) ==
1478 bit_cast<float, int32_t>(value_);
1479 }
1480
1481 virtual size_t ComputeHashCode() const { return static_cast<size_t>(GetValue()); }
1482
1483 DECLARE_INSTRUCTION(FloatConstant);
1484
1485 private:
1486 const float value_;
1487
1488 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
1489};
1490
1491class HDoubleConstant : public HConstant {
1492 public:
1493 explicit HDoubleConstant(double value) : HConstant(Primitive::kPrimDouble), value_(value) {}
1494
1495 double GetValue() const { return value_; }
1496
1497 virtual bool InstructionDataEquals(HInstruction* other) const {
1498 return bit_cast<double, int64_t>(other->AsDoubleConstant()->value_) ==
1499 bit_cast<double, int64_t>(value_);
1500 }
1501
1502 virtual size_t ComputeHashCode() const { return static_cast<size_t>(GetValue()); }
1503
1504 DECLARE_INSTRUCTION(DoubleConstant);
1505
1506 private:
1507 const double value_;
1508
1509 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
1510};
1511
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001512// Constants of the type int. Those can be from Dex instructions, or
1513// synthesized (for example with the if-eqz instruction).
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001514class HIntConstant : public HConstant {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001515 public:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001516 explicit HIntConstant(int32_t value) : HConstant(Primitive::kPrimInt), value_(value) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001517
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001518 int32_t GetValue() const { return value_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001519
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001520 virtual bool InstructionDataEquals(HInstruction* other) const {
1521 return other->AsIntConstant()->value_ == value_;
1522 }
1523
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001524 virtual size_t ComputeHashCode() const { return GetValue(); }
1525
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001526 DECLARE_INSTRUCTION(IntConstant);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001527
1528 private:
1529 const int32_t value_;
1530
1531 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
1532};
1533
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001534class HLongConstant : public HConstant {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001535 public:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001536 explicit HLongConstant(int64_t value) : HConstant(Primitive::kPrimLong), value_(value) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001537
1538 int64_t GetValue() const { return value_; }
1539
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001540 virtual bool InstructionDataEquals(HInstruction* other) const {
1541 return other->AsLongConstant()->value_ == value_;
1542 }
1543
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001544 virtual size_t ComputeHashCode() const { return static_cast<size_t>(GetValue()); }
1545
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001546 DECLARE_INSTRUCTION(LongConstant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001547
1548 private:
1549 const int64_t value_;
1550
1551 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
1552};
1553
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001554class HInvoke : public HInstruction {
1555 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001556 HInvoke(ArenaAllocator* arena,
1557 uint32_t number_of_arguments,
1558 Primitive::Type return_type,
1559 uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001560 : HInstruction(SideEffects::All()),
1561 inputs_(arena, number_of_arguments),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001562 return_type_(return_type),
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001563 dex_pc_(dex_pc) {
1564 inputs_.SetSize(number_of_arguments);
1565 }
1566
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001567 virtual size_t InputCount() const { return inputs_.Size(); }
1568 virtual HInstruction* InputAt(size_t i) const { return inputs_.Get(i); }
1569
1570 // Runtime needs to walk the stack, so Dex -> Dex calls need to
1571 // know their environment.
1572 virtual bool NeedsEnvironment() const { return true; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001573
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001574 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001575 SetRawInputAt(index, argument);
1576 }
1577
1578 virtual void SetRawInputAt(size_t index, HInstruction* input) {
1579 inputs_.Put(index, input);
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001580 }
1581
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001582 virtual Primitive::Type GetType() const { return return_type_; }
1583
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001584 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001585
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001586 DECLARE_INSTRUCTION(Invoke);
1587
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001588 protected:
1589 GrowableArray<HInstruction*> inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001590 const Primitive::Type return_type_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001591 const uint32_t dex_pc_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001592
1593 private:
1594 DISALLOW_COPY_AND_ASSIGN(HInvoke);
1595};
1596
1597class HInvokeStatic : public HInvoke {
1598 public:
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001599 HInvokeStatic(ArenaAllocator* arena,
1600 uint32_t number_of_arguments,
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001601 Primitive::Type return_type,
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001602 uint32_t dex_pc,
1603 uint32_t index_in_dex_cache)
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001604 : HInvoke(arena, number_of_arguments, return_type, dex_pc),
1605 index_in_dex_cache_(index_in_dex_cache) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001606
1607 uint32_t GetIndexInDexCache() const { return index_in_dex_cache_; }
1608
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001609 DECLARE_INSTRUCTION(InvokeStatic);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001610
1611 private:
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001612 const uint32_t index_in_dex_cache_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001613
1614 DISALLOW_COPY_AND_ASSIGN(HInvokeStatic);
1615};
1616
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001617class HInvokeVirtual : public HInvoke {
1618 public:
1619 HInvokeVirtual(ArenaAllocator* arena,
1620 uint32_t number_of_arguments,
1621 Primitive::Type return_type,
1622 uint32_t dex_pc,
1623 uint32_t vtable_index)
1624 : HInvoke(arena, number_of_arguments, return_type, dex_pc),
1625 vtable_index_(vtable_index) {}
1626
1627 uint32_t GetVTableIndex() const { return vtable_index_; }
1628
1629 DECLARE_INSTRUCTION(InvokeVirtual);
1630
1631 private:
1632 const uint32_t vtable_index_;
1633
1634 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
1635};
1636
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001637class HInvokeInterface : public HInvoke {
1638 public:
1639 HInvokeInterface(ArenaAllocator* arena,
1640 uint32_t number_of_arguments,
1641 Primitive::Type return_type,
1642 uint32_t dex_pc,
1643 uint32_t dex_method_index,
1644 uint32_t imt_index)
1645 : HInvoke(arena, number_of_arguments, return_type, dex_pc),
1646 dex_method_index_(dex_method_index),
1647 imt_index_(imt_index) {}
1648
1649 uint32_t GetImtIndex() const { return imt_index_; }
1650 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
1651
1652 DECLARE_INSTRUCTION(InvokeInterface);
1653
1654 private:
1655 const uint32_t dex_method_index_;
1656 const uint32_t imt_index_;
1657
1658 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
1659};
1660
Dave Allison20dfc792014-06-16 20:44:29 -07001661class HNewInstance : public HExpression<0> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001662 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001663 HNewInstance(uint32_t dex_pc, uint16_t type_index)
1664 : HExpression(Primitive::kPrimNot, SideEffects::None()),
1665 dex_pc_(dex_pc),
1666 type_index_(type_index) {}
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001667
1668 uint32_t GetDexPc() const { return dex_pc_; }
1669 uint16_t GetTypeIndex() const { return type_index_; }
1670
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001671 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00001672 bool NeedsEnvironment() const OVERRIDE { return true; }
1673 // It may throw when called on:
1674 // - interfaces
1675 // - abstract/innaccessible/unknown classes
1676 // TODO: optimize when possible.
1677 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001678
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001679 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001680
1681 private:
1682 const uint32_t dex_pc_;
1683 const uint16_t type_index_;
1684
1685 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
1686};
1687
Roland Levillain88cb1752014-10-20 16:36:47 +01001688class HNeg : public HUnaryOperation {
1689 public:
1690 explicit HNeg(Primitive::Type result_type, HInstruction* input)
1691 : HUnaryOperation(result_type, input) {}
1692
Roland Levillain9240d6a2014-10-20 16:47:04 +01001693 virtual int32_t Evaluate(int32_t x) const OVERRIDE { return -x; }
1694 virtual int64_t Evaluate(int64_t x) const OVERRIDE { return -x; }
1695
Roland Levillain88cb1752014-10-20 16:36:47 +01001696 DECLARE_INSTRUCTION(Neg);
1697
1698 private:
1699 DISALLOW_COPY_AND_ASSIGN(HNeg);
1700};
1701
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001702class HNewArray : public HExpression<1> {
1703 public:
1704 HNewArray(HInstruction* length, uint32_t dex_pc, uint16_t type_index)
1705 : HExpression(Primitive::kPrimNot, SideEffects::None()),
1706 dex_pc_(dex_pc),
1707 type_index_(type_index) {
1708 SetRawInputAt(0, length);
1709 }
1710
1711 uint32_t GetDexPc() const { return dex_pc_; }
1712 uint16_t GetTypeIndex() const { return type_index_; }
1713
1714 // Calls runtime so needs an environment.
1715 virtual bool NeedsEnvironment() const { return true; }
1716
1717 DECLARE_INSTRUCTION(NewArray);
1718
1719 private:
1720 const uint32_t dex_pc_;
1721 const uint16_t type_index_;
1722
1723 DISALLOW_COPY_AND_ASSIGN(HNewArray);
1724};
1725
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001726class HAdd : public HBinaryOperation {
1727 public:
1728 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1729 : HBinaryOperation(result_type, left, right) {}
1730
1731 virtual bool IsCommutative() { return true; }
1732
Roland Levillain93445682014-10-06 19:24:02 +01001733 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1734 return x + y;
1735 }
1736 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1737 return x + y;
1738 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001739
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001740 DECLARE_INSTRUCTION(Add);
1741
1742 private:
1743 DISALLOW_COPY_AND_ASSIGN(HAdd);
1744};
1745
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001746class HSub : public HBinaryOperation {
1747 public:
1748 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1749 : HBinaryOperation(result_type, left, right) {}
1750
Roland Levillain93445682014-10-06 19:24:02 +01001751 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1752 return x - y;
1753 }
1754 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1755 return x - y;
1756 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001757
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001758 DECLARE_INSTRUCTION(Sub);
1759
1760 private:
1761 DISALLOW_COPY_AND_ASSIGN(HSub);
1762};
1763
Calin Juravle34bacdf2014-10-07 20:23:36 +01001764class HMul : public HBinaryOperation {
1765 public:
1766 HMul(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1767 : HBinaryOperation(result_type, left, right) {}
1768
1769 virtual bool IsCommutative() { return true; }
1770
1771 virtual int32_t Evaluate(int32_t x, int32_t y) const { return x * y; }
1772 virtual int64_t Evaluate(int64_t x, int64_t y) const { return x * y; }
1773
1774 DECLARE_INSTRUCTION(Mul);
1775
1776 private:
1777 DISALLOW_COPY_AND_ASSIGN(HMul);
1778};
1779
Calin Juravle7c4954d2014-10-28 16:57:40 +00001780class HDiv : public HBinaryOperation {
1781 public:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001782 HDiv(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
1783 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00001784
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00001785 virtual int32_t Evaluate(int32_t x, int32_t y) const {
1786 // Our graph structure ensures we never have 0 for `y` during constant folding.
1787 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00001788 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00001789 return (y == -1) ? -x : x / y;
1790 }
Calin Juravlebacfec32014-11-14 15:54:36 +00001791
1792 virtual int64_t Evaluate(int64_t x, int64_t y) const {
1793 DCHECK_NE(y, 0);
1794 // Special case -1 to avoid getting a SIGFPE on x86(_64).
1795 return (y == -1) ? -x : x / y;
1796 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00001797
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001798 uint32_t GetDexPc() const { return dex_pc_; }
1799
Calin Juravle7c4954d2014-10-28 16:57:40 +00001800 DECLARE_INSTRUCTION(Div);
1801
1802 private:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001803 const uint32_t dex_pc_;
1804
Calin Juravle7c4954d2014-10-28 16:57:40 +00001805 DISALLOW_COPY_AND_ASSIGN(HDiv);
1806};
1807
Calin Juravlebacfec32014-11-14 15:54:36 +00001808class HRem : public HBinaryOperation {
1809 public:
1810 HRem(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
1811 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
1812
1813 virtual int32_t Evaluate(int32_t x, int32_t y) const {
1814 DCHECK_NE(y, 0);
1815 // Special case -1 to avoid getting a SIGFPE on x86(_64).
1816 return (y == -1) ? 0 : x % y;
1817 }
1818
1819 virtual int64_t Evaluate(int64_t x, int64_t y) const {
1820 DCHECK_NE(y, 0);
1821 // Special case -1 to avoid getting a SIGFPE on x86(_64).
1822 return (y == -1) ? 0 : x % y;
1823 }
1824
1825 uint32_t GetDexPc() const { return dex_pc_; }
1826
1827 DECLARE_INSTRUCTION(Rem);
1828
1829 private:
1830 const uint32_t dex_pc_;
1831
1832 DISALLOW_COPY_AND_ASSIGN(HRem);
1833};
1834
Calin Juravled0d48522014-11-04 16:40:20 +00001835class HDivZeroCheck : public HExpression<1> {
1836 public:
1837 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
1838 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
1839 SetRawInputAt(0, value);
1840 }
1841
1842 bool CanBeMoved() const OVERRIDE { return true; }
1843
1844 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
1845 UNUSED(other);
1846 return true;
1847 }
1848
1849 bool NeedsEnvironment() const OVERRIDE { return true; }
1850 bool CanThrow() const OVERRIDE { return true; }
1851
1852 uint32_t GetDexPc() const { return dex_pc_; }
1853
1854 DECLARE_INSTRUCTION(DivZeroCheck);
1855
1856 private:
1857 const uint32_t dex_pc_;
1858
1859 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
1860};
1861
Calin Juravle9aec02f2014-11-18 23:06:35 +00001862class HShl : public HBinaryOperation {
1863 public:
1864 HShl(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1865 : HBinaryOperation(result_type, left, right) {}
1866
1867 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x << (y & kMaxIntShiftValue); }
1868 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x << (y & kMaxLongShiftValue); }
1869
1870 DECLARE_INSTRUCTION(Shl);
1871
1872 private:
1873 DISALLOW_COPY_AND_ASSIGN(HShl);
1874};
1875
1876class HShr : public HBinaryOperation {
1877 public:
1878 HShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1879 : HBinaryOperation(result_type, left, right) {}
1880
1881 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x >> (y & kMaxIntShiftValue); }
1882 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x >> (y & kMaxLongShiftValue); }
1883
1884 DECLARE_INSTRUCTION(Shr);
1885
1886 private:
1887 DISALLOW_COPY_AND_ASSIGN(HShr);
1888};
1889
1890class HUShr : public HBinaryOperation {
1891 public:
1892 HUShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1893 : HBinaryOperation(result_type, left, right) {}
1894
1895 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1896 uint32_t ux = static_cast<uint32_t>(x);
1897 uint32_t uy = static_cast<uint32_t>(y) & kMaxIntShiftValue;
1898 return static_cast<int32_t>(ux >> uy);
1899 }
1900
1901 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1902 uint64_t ux = static_cast<uint64_t>(x);
1903 uint64_t uy = static_cast<uint64_t>(y) & kMaxLongShiftValue;
1904 return static_cast<int64_t>(ux >> uy);
1905 }
1906
1907 DECLARE_INSTRUCTION(UShr);
1908
1909 private:
1910 DISALLOW_COPY_AND_ASSIGN(HUShr);
1911};
1912
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001913class HAnd : public HBinaryOperation {
1914 public:
1915 HAnd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1916 : HBinaryOperation(result_type, left, right) {}
1917
1918 bool IsCommutative() OVERRIDE { return true; }
1919
1920 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x & y; }
1921 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x & y; }
1922
1923 DECLARE_INSTRUCTION(And);
1924
1925 private:
1926 DISALLOW_COPY_AND_ASSIGN(HAnd);
1927};
1928
1929class HOr : public HBinaryOperation {
1930 public:
1931 HOr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1932 : HBinaryOperation(result_type, left, right) {}
1933
1934 bool IsCommutative() OVERRIDE { return true; }
1935
1936 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x | y; }
1937 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x | y; }
1938
1939 DECLARE_INSTRUCTION(Or);
1940
1941 private:
1942 DISALLOW_COPY_AND_ASSIGN(HOr);
1943};
1944
1945class HXor : public HBinaryOperation {
1946 public:
1947 HXor(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1948 : HBinaryOperation(result_type, left, right) {}
1949
1950 bool IsCommutative() OVERRIDE { return true; }
1951
1952 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x ^ y; }
1953 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x ^ y; }
1954
1955 DECLARE_INSTRUCTION(Xor);
1956
1957 private:
1958 DISALLOW_COPY_AND_ASSIGN(HXor);
1959};
1960
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001961// The value of a parameter in this method. Its location depends on
1962// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07001963class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001964 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001965 HParameterValue(uint8_t index, Primitive::Type parameter_type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001966 : HExpression(parameter_type, SideEffects::None()), index_(index) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001967
1968 uint8_t GetIndex() const { return index_; }
1969
1970 DECLARE_INSTRUCTION(ParameterValue);
1971
1972 private:
1973 // The index of this parameter in the parameters list. Must be less
1974 // than HGraph::number_of_in_vregs_;
1975 const uint8_t index_;
1976
1977 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
1978};
1979
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001980class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001981 public:
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001982 explicit HNot(Primitive::Type result_type, HInstruction* input)
1983 : HUnaryOperation(result_type, input) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001984
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001985 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001986 virtual bool InstructionDataEquals(HInstruction* other) const {
1987 UNUSED(other);
1988 return true;
1989 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001990
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001991 virtual int32_t Evaluate(int32_t x) const OVERRIDE { return ~x; }
1992 virtual int64_t Evaluate(int64_t x) const OVERRIDE { return ~x; }
1993
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001994 DECLARE_INSTRUCTION(Not);
1995
1996 private:
1997 DISALLOW_COPY_AND_ASSIGN(HNot);
1998};
1999
Roland Levillaindff1f282014-11-05 14:15:05 +00002000class HTypeConversion : public HExpression<1> {
2001 public:
2002 // Instantiate a type conversion of `input` to `result_type`.
2003 HTypeConversion(Primitive::Type result_type, HInstruction* input)
2004 : HExpression(result_type, SideEffects::None()) {
2005 SetRawInputAt(0, input);
2006 DCHECK_NE(input->GetType(), result_type);
2007 }
2008
2009 HInstruction* GetInput() const { return InputAt(0); }
2010 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
2011 Primitive::Type GetResultType() const { return GetType(); }
2012
2013 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00002014 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00002015
2016 DECLARE_INSTRUCTION(TypeConversion);
2017
2018 private:
2019 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
2020};
2021
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002022class HPhi : public HInstruction {
2023 public:
2024 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002025 : HInstruction(SideEffects::None()),
2026 inputs_(arena, number_of_inputs),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002027 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002028 type_(type),
2029 is_live_(false) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002030 inputs_.SetSize(number_of_inputs);
2031 }
2032
2033 virtual size_t InputCount() const { return inputs_.Size(); }
2034 virtual HInstruction* InputAt(size_t i) const { return inputs_.Get(i); }
2035
2036 virtual void SetRawInputAt(size_t index, HInstruction* input) {
2037 inputs_.Put(index, input);
2038 }
2039
2040 void AddInput(HInstruction* input);
2041
2042 virtual Primitive::Type GetType() const { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002043 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002044
2045 uint32_t GetRegNumber() const { return reg_number_; }
2046
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002047 void SetDead() { is_live_ = false; }
2048 void SetLive() { is_live_ = true; }
2049 bool IsDead() const { return !is_live_; }
2050 bool IsLive() const { return is_live_; }
2051
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002052 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002053
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002054 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002055 GrowableArray<HInstruction*> inputs_;
2056 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002057 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002058 bool is_live_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002059
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002060 DISALLOW_COPY_AND_ASSIGN(HPhi);
2061};
2062
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002063class HNullCheck : public HExpression<1> {
2064 public:
2065 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002066 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002067 SetRawInputAt(0, value);
2068 }
2069
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002070 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002071 virtual bool InstructionDataEquals(HInstruction* other) const {
2072 UNUSED(other);
2073 return true;
2074 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002075
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002076 virtual bool NeedsEnvironment() const { return true; }
2077
Roland Levillaine161a2a2014-10-03 12:45:18 +01002078 virtual bool CanThrow() const { return true; }
2079
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002080 uint32_t GetDexPc() const { return dex_pc_; }
2081
2082 DECLARE_INSTRUCTION(NullCheck);
2083
2084 private:
2085 const uint32_t dex_pc_;
2086
2087 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
2088};
2089
2090class FieldInfo : public ValueObject {
2091 public:
Roland Levillain5799fc02014-09-25 12:15:20 +01002092 FieldInfo(MemberOffset field_offset, Primitive::Type field_type)
Nicolas Geoffray39468442014-09-02 15:17:15 +01002093 : field_offset_(field_offset), field_type_(field_type) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002094
2095 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002096 Primitive::Type GetFieldType() const { return field_type_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002097
2098 private:
2099 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01002100 const Primitive::Type field_type_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002101};
2102
2103class HInstanceFieldGet : public HExpression<1> {
2104 public:
2105 HInstanceFieldGet(HInstruction* value,
2106 Primitive::Type field_type,
2107 MemberOffset field_offset)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002108 : HExpression(field_type, SideEffects::DependsOnSomething()),
2109 field_info_(field_offset, field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002110 SetRawInputAt(0, value);
2111 }
2112
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002113 virtual bool CanBeMoved() const { return true; }
2114 virtual bool InstructionDataEquals(HInstruction* other) const {
2115 size_t other_offset = other->AsInstanceFieldGet()->GetFieldOffset().SizeValue();
2116 return other_offset == GetFieldOffset().SizeValue();
2117 }
2118
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002119 virtual size_t ComputeHashCode() const {
2120 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
2121 }
2122
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002123 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002124 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002125
2126 DECLARE_INSTRUCTION(InstanceFieldGet);
2127
2128 private:
2129 const FieldInfo field_info_;
2130
2131 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
2132};
2133
2134class HInstanceFieldSet : public HTemplateInstruction<2> {
2135 public:
2136 HInstanceFieldSet(HInstruction* object,
2137 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01002138 Primitive::Type field_type,
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002139 MemberOffset field_offset)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002140 : HTemplateInstruction(SideEffects::ChangesSomething()),
2141 field_info_(field_offset, field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002142 SetRawInputAt(0, object);
2143 SetRawInputAt(1, value);
2144 }
2145
2146 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002147 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002148
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002149 HInstruction* GetValue() const { return InputAt(1); }
2150
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002151 DECLARE_INSTRUCTION(InstanceFieldSet);
2152
2153 private:
2154 const FieldInfo field_info_;
2155
2156 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
2157};
2158
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002159class HArrayGet : public HExpression<2> {
2160 public:
2161 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002162 : HExpression(type, SideEffects::DependsOnSomething()) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002163 SetRawInputAt(0, array);
2164 SetRawInputAt(1, index);
2165 }
2166
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002167 bool CanBeMoved() const OVERRIDE { return true; }
2168 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002169 UNUSED(other);
2170 return true;
2171 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002172 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002173
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002174 HInstruction* GetArray() const { return InputAt(0); }
2175 HInstruction* GetIndex() const { return InputAt(1); }
2176
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002177 DECLARE_INSTRUCTION(ArrayGet);
2178
2179 private:
2180 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
2181};
2182
2183class HArraySet : public HTemplateInstruction<3> {
2184 public:
2185 HArraySet(HInstruction* array,
2186 HInstruction* index,
2187 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002188 Primitive::Type expected_component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002189 uint32_t dex_pc)
2190 : HTemplateInstruction(SideEffects::ChangesSomething()),
2191 dex_pc_(dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002192 expected_component_type_(expected_component_type),
2193 needs_type_check_(value->GetType() == Primitive::kPrimNot) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002194 SetRawInputAt(0, array);
2195 SetRawInputAt(1, index);
2196 SetRawInputAt(2, value);
2197 }
2198
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002199 bool NeedsEnvironment() const {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002200 // We currently always call a runtime method to catch array store
2201 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002202 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002203 }
2204
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002205 void ClearNeedsTypeCheck() {
2206 needs_type_check_ = false;
2207 }
2208
2209 bool NeedsTypeCheck() const { return needs_type_check_; }
2210
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002211 uint32_t GetDexPc() const { return dex_pc_; }
2212
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002213 HInstruction* GetArray() const { return InputAt(0); }
2214 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002215 HInstruction* GetValue() const { return InputAt(2); }
2216
2217 Primitive::Type GetComponentType() const {
2218 // The Dex format does not type floating point index operations. Since the
2219 // `expected_component_type_` is set during building and can therefore not
2220 // be correct, we also check what is the value type. If it is a floating
2221 // point type, we must use that type.
2222 Primitive::Type value_type = GetValue()->GetType();
2223 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
2224 ? value_type
2225 : expected_component_type_;
2226 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002227
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002228 DECLARE_INSTRUCTION(ArraySet);
2229
2230 private:
2231 const uint32_t dex_pc_;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002232 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002233 bool needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002234
2235 DISALLOW_COPY_AND_ASSIGN(HArraySet);
2236};
2237
2238class HArrayLength : public HExpression<1> {
2239 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002240 explicit HArrayLength(HInstruction* array)
2241 : HExpression(Primitive::kPrimInt, SideEffects::None()) {
2242 // Note that arrays do not change length, so the instruction does not
2243 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002244 SetRawInputAt(0, array);
2245 }
2246
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002247 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002248 virtual bool InstructionDataEquals(HInstruction* other) const {
2249 UNUSED(other);
2250 return true;
2251 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002252
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002253 DECLARE_INSTRUCTION(ArrayLength);
2254
2255 private:
2256 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
2257};
2258
2259class HBoundsCheck : public HExpression<2> {
2260 public:
2261 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002262 : HExpression(index->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002263 DCHECK(index->GetType() == Primitive::kPrimInt);
2264 SetRawInputAt(0, index);
2265 SetRawInputAt(1, length);
2266 }
2267
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002268 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002269 virtual bool InstructionDataEquals(HInstruction* other) const {
2270 UNUSED(other);
2271 return true;
2272 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002273
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002274 virtual bool NeedsEnvironment() const { return true; }
2275
Roland Levillaine161a2a2014-10-03 12:45:18 +01002276 virtual bool CanThrow() const { return true; }
2277
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002278 uint32_t GetDexPc() const { return dex_pc_; }
2279
2280 DECLARE_INSTRUCTION(BoundsCheck);
2281
2282 private:
2283 const uint32_t dex_pc_;
2284
2285 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
2286};
2287
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002288/**
2289 * Some DEX instructions are folded into multiple HInstructions that need
2290 * to stay live until the last HInstruction. This class
2291 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00002292 * HInstruction stays live. `index` represents the stack location index of the
2293 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002294 */
2295class HTemporary : public HTemplateInstruction<0> {
2296 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002297 explicit HTemporary(size_t index) : HTemplateInstruction(SideEffects::None()), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002298
2299 size_t GetIndex() const { return index_; }
2300
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00002301 Primitive::Type GetType() const OVERRIDE {
2302 // The previous instruction is the one that will be stored in the temporary location.
2303 DCHECK(GetPrevious() != nullptr);
2304 return GetPrevious()->GetType();
2305 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00002306
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002307 DECLARE_INSTRUCTION(Temporary);
2308
2309 private:
2310 const size_t index_;
2311
2312 DISALLOW_COPY_AND_ASSIGN(HTemporary);
2313};
2314
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002315class HSuspendCheck : public HTemplateInstruction<0> {
2316 public:
2317 explicit HSuspendCheck(uint32_t dex_pc)
Nicolas Geoffray9ebc72c2014-09-25 16:33:42 +01002318 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002319
2320 virtual bool NeedsEnvironment() const {
2321 return true;
2322 }
2323
2324 uint32_t GetDexPc() const { return dex_pc_; }
2325
2326 DECLARE_INSTRUCTION(SuspendCheck);
2327
2328 private:
2329 const uint32_t dex_pc_;
2330
2331 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
2332};
2333
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002334/**
2335 * Instruction to load a Class object.
2336 */
2337class HLoadClass : public HExpression<0> {
2338 public:
2339 HLoadClass(uint16_t type_index,
2340 bool is_referrers_class,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002341 uint32_t dex_pc)
2342 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2343 type_index_(type_index),
2344 is_referrers_class_(is_referrers_class),
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002345 dex_pc_(dex_pc),
2346 generate_clinit_check_(false) {}
2347
2348 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002349
2350 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2351 return other->AsLoadClass()->type_index_ == type_index_;
2352 }
2353
2354 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
2355
2356 uint32_t GetDexPc() const { return dex_pc_; }
2357 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002358 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002359
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002360 bool NeedsEnvironment() const OVERRIDE {
2361 // Will call runtime and load the class if the class is not loaded yet.
2362 // TODO: finer grain decision.
2363 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002364 }
2365
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002366 bool MustGenerateClinitCheck() const {
2367 return generate_clinit_check_;
2368 }
2369
2370 void SetMustGenerateClinitCheck() {
2371 generate_clinit_check_ = true;
2372 }
2373
2374 bool CanCallRuntime() const {
2375 return MustGenerateClinitCheck() || !is_referrers_class_;
2376 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002377
2378 DECLARE_INSTRUCTION(LoadClass);
2379
2380 private:
2381 const uint16_t type_index_;
2382 const bool is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002383 const uint32_t dex_pc_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002384 // Whether this instruction must generate the initialization check.
2385 // Used for code generation.
2386 bool generate_clinit_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002387
2388 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
2389};
2390
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002391class HLoadString : public HExpression<0> {
2392 public:
2393 HLoadString(uint32_t string_index, uint32_t dex_pc)
2394 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2395 string_index_(string_index),
2396 dex_pc_(dex_pc) {}
2397
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002398 bool CanBeMoved() const OVERRIDE { return true; }
2399
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002400 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2401 return other->AsLoadString()->string_index_ == string_index_;
2402 }
2403
2404 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
2405
2406 uint32_t GetDexPc() const { return dex_pc_; }
2407 uint32_t GetStringIndex() const { return string_index_; }
2408
2409 // TODO: Can we deopt or debug when we resolve a string?
2410 bool NeedsEnvironment() const OVERRIDE { return false; }
2411
2412 DECLARE_INSTRUCTION(LoadString);
2413
2414 private:
2415 const uint32_t string_index_;
2416 const uint32_t dex_pc_;
2417
2418 DISALLOW_COPY_AND_ASSIGN(HLoadString);
2419};
2420
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002421// TODO: Pass this check to HInvokeStatic nodes.
2422/**
2423 * Performs an initialization check on its Class object input.
2424 */
2425class HClinitCheck : public HExpression<1> {
2426 public:
2427 explicit HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
2428 : HExpression(Primitive::kPrimNot, SideEffects::All()),
2429 dex_pc_(dex_pc) {
2430 SetRawInputAt(0, constant);
2431 }
2432
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002433 bool CanBeMoved() const OVERRIDE { return true; }
2434 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2435 UNUSED(other);
2436 return true;
2437 }
2438
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002439 bool NeedsEnvironment() const OVERRIDE {
2440 // May call runtime to initialize the class.
2441 return true;
2442 }
2443
2444 uint32_t GetDexPc() const { return dex_pc_; }
2445
2446 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
2447
2448 DECLARE_INSTRUCTION(ClinitCheck);
2449
2450 private:
2451 const uint32_t dex_pc_;
2452
2453 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
2454};
2455
2456class HStaticFieldGet : public HExpression<1> {
2457 public:
2458 HStaticFieldGet(HInstruction* cls,
2459 Primitive::Type field_type,
2460 MemberOffset field_offset)
2461 : HExpression(field_type, SideEffects::DependsOnSomething()),
2462 field_info_(field_offset, field_type) {
2463 SetRawInputAt(0, cls);
2464 }
2465
2466 bool CanBeMoved() const OVERRIDE { return true; }
2467 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2468 size_t other_offset = other->AsStaticFieldGet()->GetFieldOffset().SizeValue();
2469 return other_offset == GetFieldOffset().SizeValue();
2470 }
2471
2472 size_t ComputeHashCode() const OVERRIDE {
2473 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
2474 }
2475
2476 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
2477 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
2478
2479 DECLARE_INSTRUCTION(StaticFieldGet);
2480
2481 private:
2482 const FieldInfo field_info_;
2483
2484 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
2485};
2486
2487class HStaticFieldSet : public HTemplateInstruction<2> {
2488 public:
2489 HStaticFieldSet(HInstruction* cls,
2490 HInstruction* value,
2491 Primitive::Type field_type,
2492 MemberOffset field_offset)
2493 : HTemplateInstruction(SideEffects::ChangesSomething()),
2494 field_info_(field_offset, field_type) {
2495 SetRawInputAt(0, cls);
2496 SetRawInputAt(1, value);
2497 }
2498
2499 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
2500 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
2501
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002502 HInstruction* GetValue() const { return InputAt(1); }
2503
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002504 DECLARE_INSTRUCTION(StaticFieldSet);
2505
2506 private:
2507 const FieldInfo field_info_;
2508
2509 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
2510};
2511
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002512// Implement the move-exception DEX instruction.
2513class HLoadException : public HExpression<0> {
2514 public:
2515 HLoadException() : HExpression(Primitive::kPrimNot, SideEffects::None()) {}
2516
2517 DECLARE_INSTRUCTION(LoadException);
2518
2519 private:
2520 DISALLOW_COPY_AND_ASSIGN(HLoadException);
2521};
2522
2523class HThrow : public HTemplateInstruction<1> {
2524 public:
2525 HThrow(HInstruction* exception, uint32_t dex_pc)
2526 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {
2527 SetRawInputAt(0, exception);
2528 }
2529
2530 bool IsControlFlow() const OVERRIDE { return true; }
2531
2532 bool NeedsEnvironment() const OVERRIDE { return true; }
2533
2534 uint32_t GetDexPc() const { return dex_pc_; }
2535
2536 DECLARE_INSTRUCTION(Throw);
2537
2538 private:
2539 uint32_t dex_pc_;
2540
2541 DISALLOW_COPY_AND_ASSIGN(HThrow);
2542};
2543
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002544class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002545 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002546 HInstanceOf(HInstruction* object,
2547 HLoadClass* constant,
2548 bool class_is_final,
2549 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002550 : HExpression(Primitive::kPrimBoolean, SideEffects::None()),
2551 class_is_final_(class_is_final),
2552 dex_pc_(dex_pc) {
2553 SetRawInputAt(0, object);
2554 SetRawInputAt(1, constant);
2555 }
2556
2557 bool CanBeMoved() const OVERRIDE { return true; }
2558
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002559 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002560 return true;
2561 }
2562
2563 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002564 return false;
2565 }
2566
2567 uint32_t GetDexPc() const { return dex_pc_; }
2568
2569 bool IsClassFinal() const { return class_is_final_; }
2570
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002571 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002572
2573 private:
2574 const bool class_is_final_;
2575 const uint32_t dex_pc_;
2576
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002577 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
2578};
2579
2580class HCheckCast : public HTemplateInstruction<2> {
2581 public:
2582 HCheckCast(HInstruction* object,
2583 HLoadClass* constant,
2584 bool class_is_final,
2585 uint32_t dex_pc)
2586 : HTemplateInstruction(SideEffects::None()),
2587 class_is_final_(class_is_final),
2588 dex_pc_(dex_pc) {
2589 SetRawInputAt(0, object);
2590 SetRawInputAt(1, constant);
2591 }
2592
2593 bool CanBeMoved() const OVERRIDE { return true; }
2594
2595 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2596 return true;
2597 }
2598
2599 bool NeedsEnvironment() const OVERRIDE {
2600 // Instruction may throw a CheckCastError.
2601 return true;
2602 }
2603
2604 bool CanThrow() const OVERRIDE { return true; }
2605
2606 uint32_t GetDexPc() const { return dex_pc_; }
2607
2608 bool IsClassFinal() const { return class_is_final_; }
2609
2610 DECLARE_INSTRUCTION(CheckCast);
2611
2612 private:
2613 const bool class_is_final_;
2614 const uint32_t dex_pc_;
2615
2616 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002617};
2618
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00002619class HMonitorOperation : public HTemplateInstruction<1> {
2620 public:
2621 enum OperationKind {
2622 kEnter,
2623 kExit,
2624 };
2625
2626 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
2627 : HTemplateInstruction(SideEffects::None()), kind_(kind), dex_pc_(dex_pc) {
2628 SetRawInputAt(0, object);
2629 }
2630
2631 // Instruction may throw a Java exception, so we need an environment.
2632 bool NeedsEnvironment() const OVERRIDE { return true; }
2633 bool CanThrow() const OVERRIDE { return true; }
2634
2635 uint32_t GetDexPc() const { return dex_pc_; }
2636
2637 bool IsEnter() const { return kind_ == kEnter; }
2638
2639 DECLARE_INSTRUCTION(MonitorOperation);
2640
2641 protected:
2642 const OperationKind kind_;
2643 const uint32_t dex_pc_;
2644
2645 private:
2646 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
2647};
2648
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002649
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002650class MoveOperands : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002651 public:
Nicolas Geoffray740475d2014-09-29 10:33:25 +01002652 MoveOperands(Location source, Location destination, HInstruction* instruction)
2653 : source_(source), destination_(destination), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002654
2655 Location GetSource() const { return source_; }
2656 Location GetDestination() const { return destination_; }
2657
2658 void SetSource(Location value) { source_ = value; }
2659 void SetDestination(Location value) { destination_ = value; }
2660
2661 // The parallel move resolver marks moves as "in-progress" by clearing the
2662 // destination (but not the source).
2663 Location MarkPending() {
2664 DCHECK(!IsPending());
2665 Location dest = destination_;
2666 destination_ = Location::NoLocation();
2667 return dest;
2668 }
2669
2670 void ClearPending(Location dest) {
2671 DCHECK(IsPending());
2672 destination_ = dest;
2673 }
2674
2675 bool IsPending() const {
2676 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
2677 return destination_.IsInvalid() && !source_.IsInvalid();
2678 }
2679
2680 // True if this blocks a move from the given location.
2681 bool Blocks(Location loc) const {
2682 return !IsEliminated() && source_.Equals(loc);
2683 }
2684
2685 // A move is redundant if it's been eliminated, if its source and
2686 // destination are the same, or if its destination is unneeded.
2687 bool IsRedundant() const {
2688 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
2689 }
2690
2691 // We clear both operands to indicate move that's been eliminated.
2692 void Eliminate() {
2693 source_ = destination_ = Location::NoLocation();
2694 }
2695
2696 bool IsEliminated() const {
2697 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
2698 return source_.IsInvalid();
2699 }
2700
Nicolas Geoffray740475d2014-09-29 10:33:25 +01002701 HInstruction* GetInstruction() const { return instruction_; }
2702
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002703 private:
2704 Location source_;
2705 Location destination_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01002706 // The instruction this move is assocatied with. Null when this move is
2707 // for moving an input in the expected locations of user (including a phi user).
2708 // This is only used in debug mode, to ensure we do not connect interval siblings
2709 // in the same parallel move.
2710 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002711
2712 DISALLOW_COPY_AND_ASSIGN(MoveOperands);
2713};
2714
2715static constexpr size_t kDefaultNumberOfMoves = 4;
2716
2717class HParallelMove : public HTemplateInstruction<0> {
2718 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002719 explicit HParallelMove(ArenaAllocator* arena)
2720 : HTemplateInstruction(SideEffects::None()), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002721
2722 void AddMove(MoveOperands* move) {
Nicolas Geoffray740475d2014-09-29 10:33:25 +01002723 if (kIsDebugBuild && move->GetInstruction() != nullptr) {
2724 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
2725 DCHECK_NE(moves_.Get(i)->GetInstruction(), move->GetInstruction())
2726 << "Doing parallel moves for the same instruction.";
2727 }
2728 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002729 moves_.Add(move);
2730 }
2731
2732 MoveOperands* MoveOperandsAt(size_t index) const {
2733 return moves_.Get(index);
2734 }
2735
2736 size_t NumMoves() const { return moves_.Size(); }
2737
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002738 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002739
2740 private:
2741 GrowableArray<MoveOperands*> moves_;
2742
2743 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
2744};
2745
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002746class HGraphVisitor : public ValueObject {
2747 public:
Dave Allison20dfc792014-06-16 20:44:29 -07002748 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
2749 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002750
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002751 virtual void VisitInstruction(HInstruction* instruction) { UNUSED(instruction); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002752 virtual void VisitBasicBlock(HBasicBlock* block);
2753
Roland Levillain633021e2014-10-01 14:12:25 +01002754 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002755 void VisitInsertionOrder();
2756
Roland Levillain633021e2014-10-01 14:12:25 +01002757 // Visit the graph following dominator tree reverse post-order.
2758 void VisitReversePostOrder();
2759
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002760 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002761
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002762 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002763#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002764 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
2765
2766 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
2767
2768#undef DECLARE_VISIT_INSTRUCTION
2769
2770 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07002771 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002772
2773 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
2774};
2775
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002776class HGraphDelegateVisitor : public HGraphVisitor {
2777 public:
2778 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
2779 virtual ~HGraphDelegateVisitor() {}
2780
2781 // Visit functions that delegate to to super class.
2782#define DECLARE_VISIT_INSTRUCTION(name, super) \
2783 virtual void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
2784
2785 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
2786
2787#undef DECLARE_VISIT_INSTRUCTION
2788
2789 private:
2790 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
2791};
2792
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002793class HInsertionOrderIterator : public ValueObject {
2794 public:
2795 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
2796
2797 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
2798 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
2799 void Advance() { ++index_; }
2800
2801 private:
2802 const HGraph& graph_;
2803 size_t index_;
2804
2805 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
2806};
2807
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002808class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002809 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002810 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002811
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002812 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
2813 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002814 void Advance() { ++index_; }
2815
2816 private:
2817 const HGraph& graph_;
2818 size_t index_;
2819
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002820 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002821};
2822
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002823class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002824 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002825 explicit HPostOrderIterator(const HGraph& graph)
2826 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002827
2828 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002829 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002830 void Advance() { --index_; }
2831
2832 private:
2833 const HGraph& graph_;
2834 size_t index_;
2835
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002836 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002837};
2838
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002839} // namespace art
2840
2841#endif // ART_COMPILER_OPTIMIZING_NODES_H_