blob: 9d0b4a971e54edf653bf7e55bb2fcbe39bfed950 [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.
1672 virtual bool NeedsEnvironment() const { return true; }
1673
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001674 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001675
1676 private:
1677 const uint32_t dex_pc_;
1678 const uint16_t type_index_;
1679
1680 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
1681};
1682
Roland Levillain88cb1752014-10-20 16:36:47 +01001683class HNeg : public HUnaryOperation {
1684 public:
1685 explicit HNeg(Primitive::Type result_type, HInstruction* input)
1686 : HUnaryOperation(result_type, input) {}
1687
Roland Levillain9240d6a2014-10-20 16:47:04 +01001688 virtual int32_t Evaluate(int32_t x) const OVERRIDE { return -x; }
1689 virtual int64_t Evaluate(int64_t x) const OVERRIDE { return -x; }
1690
Roland Levillain88cb1752014-10-20 16:36:47 +01001691 DECLARE_INSTRUCTION(Neg);
1692
1693 private:
1694 DISALLOW_COPY_AND_ASSIGN(HNeg);
1695};
1696
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001697class HNewArray : public HExpression<1> {
1698 public:
1699 HNewArray(HInstruction* length, uint32_t dex_pc, uint16_t type_index)
1700 : HExpression(Primitive::kPrimNot, SideEffects::None()),
1701 dex_pc_(dex_pc),
1702 type_index_(type_index) {
1703 SetRawInputAt(0, length);
1704 }
1705
1706 uint32_t GetDexPc() const { return dex_pc_; }
1707 uint16_t GetTypeIndex() const { return type_index_; }
1708
1709 // Calls runtime so needs an environment.
1710 virtual bool NeedsEnvironment() const { return true; }
1711
1712 DECLARE_INSTRUCTION(NewArray);
1713
1714 private:
1715 const uint32_t dex_pc_;
1716 const uint16_t type_index_;
1717
1718 DISALLOW_COPY_AND_ASSIGN(HNewArray);
1719};
1720
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001721class HAdd : public HBinaryOperation {
1722 public:
1723 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1724 : HBinaryOperation(result_type, left, right) {}
1725
1726 virtual bool IsCommutative() { return true; }
1727
Roland Levillain93445682014-10-06 19:24:02 +01001728 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1729 return x + y;
1730 }
1731 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1732 return x + y;
1733 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001734
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001735 DECLARE_INSTRUCTION(Add);
1736
1737 private:
1738 DISALLOW_COPY_AND_ASSIGN(HAdd);
1739};
1740
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001741class HSub : public HBinaryOperation {
1742 public:
1743 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1744 : HBinaryOperation(result_type, left, right) {}
1745
Roland Levillain93445682014-10-06 19:24:02 +01001746 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1747 return x - y;
1748 }
1749 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1750 return x - y;
1751 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001752
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001753 DECLARE_INSTRUCTION(Sub);
1754
1755 private:
1756 DISALLOW_COPY_AND_ASSIGN(HSub);
1757};
1758
Calin Juravle34bacdf2014-10-07 20:23:36 +01001759class HMul : public HBinaryOperation {
1760 public:
1761 HMul(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1762 : HBinaryOperation(result_type, left, right) {}
1763
1764 virtual bool IsCommutative() { return true; }
1765
1766 virtual int32_t Evaluate(int32_t x, int32_t y) const { return x * y; }
1767 virtual int64_t Evaluate(int64_t x, int64_t y) const { return x * y; }
1768
1769 DECLARE_INSTRUCTION(Mul);
1770
1771 private:
1772 DISALLOW_COPY_AND_ASSIGN(HMul);
1773};
1774
Calin Juravle7c4954d2014-10-28 16:57:40 +00001775class HDiv : public HBinaryOperation {
1776 public:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001777 HDiv(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
1778 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00001779
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00001780 virtual int32_t Evaluate(int32_t x, int32_t y) const {
1781 // Our graph structure ensures we never have 0 for `y` during constant folding.
1782 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00001783 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00001784 return (y == -1) ? -x : x / y;
1785 }
Calin Juravlebacfec32014-11-14 15:54:36 +00001786
1787 virtual int64_t Evaluate(int64_t x, int64_t y) const {
1788 DCHECK_NE(y, 0);
1789 // Special case -1 to avoid getting a SIGFPE on x86(_64).
1790 return (y == -1) ? -x : x / y;
1791 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00001792
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001793 uint32_t GetDexPc() const { return dex_pc_; }
1794
Calin Juravle7c4954d2014-10-28 16:57:40 +00001795 DECLARE_INSTRUCTION(Div);
1796
1797 private:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001798 const uint32_t dex_pc_;
1799
Calin Juravle7c4954d2014-10-28 16:57:40 +00001800 DISALLOW_COPY_AND_ASSIGN(HDiv);
1801};
1802
Calin Juravlebacfec32014-11-14 15:54:36 +00001803class HRem : public HBinaryOperation {
1804 public:
1805 HRem(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
1806 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
1807
1808 virtual int32_t Evaluate(int32_t x, int32_t y) const {
1809 DCHECK_NE(y, 0);
1810 // Special case -1 to avoid getting a SIGFPE on x86(_64).
1811 return (y == -1) ? 0 : x % y;
1812 }
1813
1814 virtual int64_t Evaluate(int64_t x, int64_t y) const {
1815 DCHECK_NE(y, 0);
1816 // Special case -1 to avoid getting a SIGFPE on x86(_64).
1817 return (y == -1) ? 0 : x % y;
1818 }
1819
1820 uint32_t GetDexPc() const { return dex_pc_; }
1821
1822 DECLARE_INSTRUCTION(Rem);
1823
1824 private:
1825 const uint32_t dex_pc_;
1826
1827 DISALLOW_COPY_AND_ASSIGN(HRem);
1828};
1829
Calin Juravled0d48522014-11-04 16:40:20 +00001830class HDivZeroCheck : public HExpression<1> {
1831 public:
1832 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
1833 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
1834 SetRawInputAt(0, value);
1835 }
1836
1837 bool CanBeMoved() const OVERRIDE { return true; }
1838
1839 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
1840 UNUSED(other);
1841 return true;
1842 }
1843
1844 bool NeedsEnvironment() const OVERRIDE { return true; }
1845 bool CanThrow() const OVERRIDE { return true; }
1846
1847 uint32_t GetDexPc() const { return dex_pc_; }
1848
1849 DECLARE_INSTRUCTION(DivZeroCheck);
1850
1851 private:
1852 const uint32_t dex_pc_;
1853
1854 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
1855};
1856
Calin Juravle9aec02f2014-11-18 23:06:35 +00001857class HShl : public HBinaryOperation {
1858 public:
1859 HShl(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1860 : HBinaryOperation(result_type, left, right) {}
1861
1862 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x << (y & kMaxIntShiftValue); }
1863 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x << (y & kMaxLongShiftValue); }
1864
1865 DECLARE_INSTRUCTION(Shl);
1866
1867 private:
1868 DISALLOW_COPY_AND_ASSIGN(HShl);
1869};
1870
1871class HShr : public HBinaryOperation {
1872 public:
1873 HShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1874 : HBinaryOperation(result_type, left, right) {}
1875
1876 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x >> (y & kMaxIntShiftValue); }
1877 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x >> (y & kMaxLongShiftValue); }
1878
1879 DECLARE_INSTRUCTION(Shr);
1880
1881 private:
1882 DISALLOW_COPY_AND_ASSIGN(HShr);
1883};
1884
1885class HUShr : public HBinaryOperation {
1886 public:
1887 HUShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1888 : HBinaryOperation(result_type, left, right) {}
1889
1890 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1891 uint32_t ux = static_cast<uint32_t>(x);
1892 uint32_t uy = static_cast<uint32_t>(y) & kMaxIntShiftValue;
1893 return static_cast<int32_t>(ux >> uy);
1894 }
1895
1896 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1897 uint64_t ux = static_cast<uint64_t>(x);
1898 uint64_t uy = static_cast<uint64_t>(y) & kMaxLongShiftValue;
1899 return static_cast<int64_t>(ux >> uy);
1900 }
1901
1902 DECLARE_INSTRUCTION(UShr);
1903
1904 private:
1905 DISALLOW_COPY_AND_ASSIGN(HUShr);
1906};
1907
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001908class HAnd : public HBinaryOperation {
1909 public:
1910 HAnd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1911 : HBinaryOperation(result_type, left, right) {}
1912
1913 bool IsCommutative() OVERRIDE { return true; }
1914
1915 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x & y; }
1916 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x & y; }
1917
1918 DECLARE_INSTRUCTION(And);
1919
1920 private:
1921 DISALLOW_COPY_AND_ASSIGN(HAnd);
1922};
1923
1924class HOr : public HBinaryOperation {
1925 public:
1926 HOr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1927 : HBinaryOperation(result_type, left, right) {}
1928
1929 bool IsCommutative() OVERRIDE { return true; }
1930
1931 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x | y; }
1932 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x | y; }
1933
1934 DECLARE_INSTRUCTION(Or);
1935
1936 private:
1937 DISALLOW_COPY_AND_ASSIGN(HOr);
1938};
1939
1940class HXor : public HBinaryOperation {
1941 public:
1942 HXor(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1943 : HBinaryOperation(result_type, left, right) {}
1944
1945 bool IsCommutative() OVERRIDE { return true; }
1946
1947 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x ^ y; }
1948 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x ^ y; }
1949
1950 DECLARE_INSTRUCTION(Xor);
1951
1952 private:
1953 DISALLOW_COPY_AND_ASSIGN(HXor);
1954};
1955
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001956// The value of a parameter in this method. Its location depends on
1957// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07001958class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001959 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001960 HParameterValue(uint8_t index, Primitive::Type parameter_type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001961 : HExpression(parameter_type, SideEffects::None()), index_(index) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001962
1963 uint8_t GetIndex() const { return index_; }
1964
1965 DECLARE_INSTRUCTION(ParameterValue);
1966
1967 private:
1968 // The index of this parameter in the parameters list. Must be less
1969 // than HGraph::number_of_in_vregs_;
1970 const uint8_t index_;
1971
1972 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
1973};
1974
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001975class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001976 public:
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001977 explicit HNot(Primitive::Type result_type, HInstruction* input)
1978 : HUnaryOperation(result_type, input) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001979
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001980 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001981 virtual bool InstructionDataEquals(HInstruction* other) const {
1982 UNUSED(other);
1983 return true;
1984 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001985
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001986 virtual int32_t Evaluate(int32_t x) const OVERRIDE { return ~x; }
1987 virtual int64_t Evaluate(int64_t x) const OVERRIDE { return ~x; }
1988
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01001989 DECLARE_INSTRUCTION(Not);
1990
1991 private:
1992 DISALLOW_COPY_AND_ASSIGN(HNot);
1993};
1994
Roland Levillaindff1f282014-11-05 14:15:05 +00001995class HTypeConversion : public HExpression<1> {
1996 public:
1997 // Instantiate a type conversion of `input` to `result_type`.
1998 HTypeConversion(Primitive::Type result_type, HInstruction* input)
1999 : HExpression(result_type, SideEffects::None()) {
2000 SetRawInputAt(0, input);
2001 DCHECK_NE(input->GetType(), result_type);
2002 }
2003
2004 HInstruction* GetInput() const { return InputAt(0); }
2005 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
2006 Primitive::Type GetResultType() const { return GetType(); }
2007
2008 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00002009 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00002010
2011 DECLARE_INSTRUCTION(TypeConversion);
2012
2013 private:
2014 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
2015};
2016
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002017class HPhi : public HInstruction {
2018 public:
2019 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002020 : HInstruction(SideEffects::None()),
2021 inputs_(arena, number_of_inputs),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002022 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002023 type_(type),
2024 is_live_(false) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002025 inputs_.SetSize(number_of_inputs);
2026 }
2027
2028 virtual size_t InputCount() const { return inputs_.Size(); }
2029 virtual HInstruction* InputAt(size_t i) const { return inputs_.Get(i); }
2030
2031 virtual void SetRawInputAt(size_t index, HInstruction* input) {
2032 inputs_.Put(index, input);
2033 }
2034
2035 void AddInput(HInstruction* input);
2036
2037 virtual Primitive::Type GetType() const { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002038 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002039
2040 uint32_t GetRegNumber() const { return reg_number_; }
2041
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002042 void SetDead() { is_live_ = false; }
2043 void SetLive() { is_live_ = true; }
2044 bool IsDead() const { return !is_live_; }
2045 bool IsLive() const { return is_live_; }
2046
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002047 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002048
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002049 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002050 GrowableArray<HInstruction*> inputs_;
2051 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002052 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002053 bool is_live_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002054
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002055 DISALLOW_COPY_AND_ASSIGN(HPhi);
2056};
2057
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002058class HNullCheck : public HExpression<1> {
2059 public:
2060 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002061 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002062 SetRawInputAt(0, value);
2063 }
2064
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002065 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002066 virtual bool InstructionDataEquals(HInstruction* other) const {
2067 UNUSED(other);
2068 return true;
2069 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002070
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002071 virtual bool NeedsEnvironment() const { return true; }
2072
Roland Levillaine161a2a2014-10-03 12:45:18 +01002073 virtual bool CanThrow() const { return true; }
2074
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002075 uint32_t GetDexPc() const { return dex_pc_; }
2076
2077 DECLARE_INSTRUCTION(NullCheck);
2078
2079 private:
2080 const uint32_t dex_pc_;
2081
2082 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
2083};
2084
2085class FieldInfo : public ValueObject {
2086 public:
Roland Levillain5799fc02014-09-25 12:15:20 +01002087 FieldInfo(MemberOffset field_offset, Primitive::Type field_type)
Nicolas Geoffray39468442014-09-02 15:17:15 +01002088 : field_offset_(field_offset), field_type_(field_type) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002089
2090 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002091 Primitive::Type GetFieldType() const { return field_type_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002092
2093 private:
2094 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01002095 const Primitive::Type field_type_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002096};
2097
2098class HInstanceFieldGet : public HExpression<1> {
2099 public:
2100 HInstanceFieldGet(HInstruction* value,
2101 Primitive::Type field_type,
2102 MemberOffset field_offset)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002103 : HExpression(field_type, SideEffects::DependsOnSomething()),
2104 field_info_(field_offset, field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002105 SetRawInputAt(0, value);
2106 }
2107
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002108 virtual bool CanBeMoved() const { return true; }
2109 virtual bool InstructionDataEquals(HInstruction* other) const {
2110 size_t other_offset = other->AsInstanceFieldGet()->GetFieldOffset().SizeValue();
2111 return other_offset == GetFieldOffset().SizeValue();
2112 }
2113
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002114 virtual size_t ComputeHashCode() const {
2115 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
2116 }
2117
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002118 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002119 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002120
2121 DECLARE_INSTRUCTION(InstanceFieldGet);
2122
2123 private:
2124 const FieldInfo field_info_;
2125
2126 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
2127};
2128
2129class HInstanceFieldSet : public HTemplateInstruction<2> {
2130 public:
2131 HInstanceFieldSet(HInstruction* object,
2132 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01002133 Primitive::Type field_type,
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002134 MemberOffset field_offset)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002135 : HTemplateInstruction(SideEffects::ChangesSomething()),
2136 field_info_(field_offset, field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002137 SetRawInputAt(0, object);
2138 SetRawInputAt(1, value);
2139 }
2140
2141 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002142 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002143
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002144 HInstruction* GetValue() const { return InputAt(1); }
2145
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002146 DECLARE_INSTRUCTION(InstanceFieldSet);
2147
2148 private:
2149 const FieldInfo field_info_;
2150
2151 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
2152};
2153
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002154class HArrayGet : public HExpression<2> {
2155 public:
2156 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002157 : HExpression(type, SideEffects::DependsOnSomething()) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002158 SetRawInputAt(0, array);
2159 SetRawInputAt(1, index);
2160 }
2161
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002162 bool CanBeMoved() const OVERRIDE { return true; }
2163 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002164 UNUSED(other);
2165 return true;
2166 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002167 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002168
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002169 HInstruction* GetArray() const { return InputAt(0); }
2170 HInstruction* GetIndex() const { return InputAt(1); }
2171
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002172 DECLARE_INSTRUCTION(ArrayGet);
2173
2174 private:
2175 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
2176};
2177
2178class HArraySet : public HTemplateInstruction<3> {
2179 public:
2180 HArraySet(HInstruction* array,
2181 HInstruction* index,
2182 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002183 Primitive::Type expected_component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002184 uint32_t dex_pc)
2185 : HTemplateInstruction(SideEffects::ChangesSomething()),
2186 dex_pc_(dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002187 expected_component_type_(expected_component_type),
2188 needs_type_check_(value->GetType() == Primitive::kPrimNot) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002189 SetRawInputAt(0, array);
2190 SetRawInputAt(1, index);
2191 SetRawInputAt(2, value);
2192 }
2193
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002194 bool NeedsEnvironment() const {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002195 // We currently always call a runtime method to catch array store
2196 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002197 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002198 }
2199
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002200 void ClearNeedsTypeCheck() {
2201 needs_type_check_ = false;
2202 }
2203
2204 bool NeedsTypeCheck() const { return needs_type_check_; }
2205
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002206 uint32_t GetDexPc() const { return dex_pc_; }
2207
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002208 HInstruction* GetArray() const { return InputAt(0); }
2209 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002210 HInstruction* GetValue() const { return InputAt(2); }
2211
2212 Primitive::Type GetComponentType() const {
2213 // The Dex format does not type floating point index operations. Since the
2214 // `expected_component_type_` is set during building and can therefore not
2215 // be correct, we also check what is the value type. If it is a floating
2216 // point type, we must use that type.
2217 Primitive::Type value_type = GetValue()->GetType();
2218 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
2219 ? value_type
2220 : expected_component_type_;
2221 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002222
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002223 DECLARE_INSTRUCTION(ArraySet);
2224
2225 private:
2226 const uint32_t dex_pc_;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002227 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002228 bool needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002229
2230 DISALLOW_COPY_AND_ASSIGN(HArraySet);
2231};
2232
2233class HArrayLength : public HExpression<1> {
2234 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002235 explicit HArrayLength(HInstruction* array)
2236 : HExpression(Primitive::kPrimInt, SideEffects::None()) {
2237 // Note that arrays do not change length, so the instruction does not
2238 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002239 SetRawInputAt(0, array);
2240 }
2241
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002242 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002243 virtual bool InstructionDataEquals(HInstruction* other) const {
2244 UNUSED(other);
2245 return true;
2246 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002247
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002248 DECLARE_INSTRUCTION(ArrayLength);
2249
2250 private:
2251 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
2252};
2253
2254class HBoundsCheck : public HExpression<2> {
2255 public:
2256 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002257 : HExpression(index->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002258 DCHECK(index->GetType() == Primitive::kPrimInt);
2259 SetRawInputAt(0, index);
2260 SetRawInputAt(1, length);
2261 }
2262
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002263 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002264 virtual bool InstructionDataEquals(HInstruction* other) const {
2265 UNUSED(other);
2266 return true;
2267 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002268
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002269 virtual bool NeedsEnvironment() const { return true; }
2270
Roland Levillaine161a2a2014-10-03 12:45:18 +01002271 virtual bool CanThrow() const { return true; }
2272
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002273 uint32_t GetDexPc() const { return dex_pc_; }
2274
2275 DECLARE_INSTRUCTION(BoundsCheck);
2276
2277 private:
2278 const uint32_t dex_pc_;
2279
2280 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
2281};
2282
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002283/**
2284 * Some DEX instructions are folded into multiple HInstructions that need
2285 * to stay live until the last HInstruction. This class
2286 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00002287 * HInstruction stays live. `index` represents the stack location index of the
2288 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002289 */
2290class HTemporary : public HTemplateInstruction<0> {
2291 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002292 explicit HTemporary(size_t index) : HTemplateInstruction(SideEffects::None()), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002293
2294 size_t GetIndex() const { return index_; }
2295
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00002296 Primitive::Type GetType() const OVERRIDE {
2297 // The previous instruction is the one that will be stored in the temporary location.
2298 DCHECK(GetPrevious() != nullptr);
2299 return GetPrevious()->GetType();
2300 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00002301
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002302 DECLARE_INSTRUCTION(Temporary);
2303
2304 private:
2305 const size_t index_;
2306
2307 DISALLOW_COPY_AND_ASSIGN(HTemporary);
2308};
2309
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002310class HSuspendCheck : public HTemplateInstruction<0> {
2311 public:
2312 explicit HSuspendCheck(uint32_t dex_pc)
Nicolas Geoffray9ebc72c2014-09-25 16:33:42 +01002313 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002314
2315 virtual bool NeedsEnvironment() const {
2316 return true;
2317 }
2318
2319 uint32_t GetDexPc() const { return dex_pc_; }
2320
2321 DECLARE_INSTRUCTION(SuspendCheck);
2322
2323 private:
2324 const uint32_t dex_pc_;
2325
2326 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
2327};
2328
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002329/**
2330 * Instruction to load a Class object.
2331 */
2332class HLoadClass : public HExpression<0> {
2333 public:
2334 HLoadClass(uint16_t type_index,
2335 bool is_referrers_class,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002336 uint32_t dex_pc)
2337 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2338 type_index_(type_index),
2339 is_referrers_class_(is_referrers_class),
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002340 dex_pc_(dex_pc),
2341 generate_clinit_check_(false) {}
2342
2343 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002344
2345 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2346 return other->AsLoadClass()->type_index_ == type_index_;
2347 }
2348
2349 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
2350
2351 uint32_t GetDexPc() const { return dex_pc_; }
2352 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002353 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002354
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002355 bool NeedsEnvironment() const OVERRIDE {
2356 // Will call runtime and load the class if the class is not loaded yet.
2357 // TODO: finer grain decision.
2358 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002359 }
2360
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002361 bool MustGenerateClinitCheck() const {
2362 return generate_clinit_check_;
2363 }
2364
2365 void SetMustGenerateClinitCheck() {
2366 generate_clinit_check_ = true;
2367 }
2368
2369 bool CanCallRuntime() const {
2370 return MustGenerateClinitCheck() || !is_referrers_class_;
2371 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002372
2373 DECLARE_INSTRUCTION(LoadClass);
2374
2375 private:
2376 const uint16_t type_index_;
2377 const bool is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002378 const uint32_t dex_pc_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002379 // Whether this instruction must generate the initialization check.
2380 // Used for code generation.
2381 bool generate_clinit_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002382
2383 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
2384};
2385
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002386class HLoadString : public HExpression<0> {
2387 public:
2388 HLoadString(uint32_t string_index, uint32_t dex_pc)
2389 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2390 string_index_(string_index),
2391 dex_pc_(dex_pc) {}
2392
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002393 bool CanBeMoved() const OVERRIDE { return true; }
2394
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002395 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2396 return other->AsLoadString()->string_index_ == string_index_;
2397 }
2398
2399 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
2400
2401 uint32_t GetDexPc() const { return dex_pc_; }
2402 uint32_t GetStringIndex() const { return string_index_; }
2403
2404 // TODO: Can we deopt or debug when we resolve a string?
2405 bool NeedsEnvironment() const OVERRIDE { return false; }
2406
2407 DECLARE_INSTRUCTION(LoadString);
2408
2409 private:
2410 const uint32_t string_index_;
2411 const uint32_t dex_pc_;
2412
2413 DISALLOW_COPY_AND_ASSIGN(HLoadString);
2414};
2415
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002416// TODO: Pass this check to HInvokeStatic nodes.
2417/**
2418 * Performs an initialization check on its Class object input.
2419 */
2420class HClinitCheck : public HExpression<1> {
2421 public:
2422 explicit HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
2423 : HExpression(Primitive::kPrimNot, SideEffects::All()),
2424 dex_pc_(dex_pc) {
2425 SetRawInputAt(0, constant);
2426 }
2427
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002428 bool CanBeMoved() const OVERRIDE { return true; }
2429 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2430 UNUSED(other);
2431 return true;
2432 }
2433
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002434 bool NeedsEnvironment() const OVERRIDE {
2435 // May call runtime to initialize the class.
2436 return true;
2437 }
2438
2439 uint32_t GetDexPc() const { return dex_pc_; }
2440
2441 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
2442
2443 DECLARE_INSTRUCTION(ClinitCheck);
2444
2445 private:
2446 const uint32_t dex_pc_;
2447
2448 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
2449};
2450
2451class HStaticFieldGet : public HExpression<1> {
2452 public:
2453 HStaticFieldGet(HInstruction* cls,
2454 Primitive::Type field_type,
2455 MemberOffset field_offset)
2456 : HExpression(field_type, SideEffects::DependsOnSomething()),
2457 field_info_(field_offset, field_type) {
2458 SetRawInputAt(0, cls);
2459 }
2460
2461 bool CanBeMoved() const OVERRIDE { return true; }
2462 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2463 size_t other_offset = other->AsStaticFieldGet()->GetFieldOffset().SizeValue();
2464 return other_offset == GetFieldOffset().SizeValue();
2465 }
2466
2467 size_t ComputeHashCode() const OVERRIDE {
2468 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
2469 }
2470
2471 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
2472 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
2473
2474 DECLARE_INSTRUCTION(StaticFieldGet);
2475
2476 private:
2477 const FieldInfo field_info_;
2478
2479 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
2480};
2481
2482class HStaticFieldSet : public HTemplateInstruction<2> {
2483 public:
2484 HStaticFieldSet(HInstruction* cls,
2485 HInstruction* value,
2486 Primitive::Type field_type,
2487 MemberOffset field_offset)
2488 : HTemplateInstruction(SideEffects::ChangesSomething()),
2489 field_info_(field_offset, field_type) {
2490 SetRawInputAt(0, cls);
2491 SetRawInputAt(1, value);
2492 }
2493
2494 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
2495 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
2496
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002497 HInstruction* GetValue() const { return InputAt(1); }
2498
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002499 DECLARE_INSTRUCTION(StaticFieldSet);
2500
2501 private:
2502 const FieldInfo field_info_;
2503
2504 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
2505};
2506
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002507// Implement the move-exception DEX instruction.
2508class HLoadException : public HExpression<0> {
2509 public:
2510 HLoadException() : HExpression(Primitive::kPrimNot, SideEffects::None()) {}
2511
2512 DECLARE_INSTRUCTION(LoadException);
2513
2514 private:
2515 DISALLOW_COPY_AND_ASSIGN(HLoadException);
2516};
2517
2518class HThrow : public HTemplateInstruction<1> {
2519 public:
2520 HThrow(HInstruction* exception, uint32_t dex_pc)
2521 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {
2522 SetRawInputAt(0, exception);
2523 }
2524
2525 bool IsControlFlow() const OVERRIDE { return true; }
2526
2527 bool NeedsEnvironment() const OVERRIDE { return true; }
2528
2529 uint32_t GetDexPc() const { return dex_pc_; }
2530
2531 DECLARE_INSTRUCTION(Throw);
2532
2533 private:
2534 uint32_t dex_pc_;
2535
2536 DISALLOW_COPY_AND_ASSIGN(HThrow);
2537};
2538
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002539class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002540 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002541 HInstanceOf(HInstruction* object,
2542 HLoadClass* constant,
2543 bool class_is_final,
2544 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002545 : HExpression(Primitive::kPrimBoolean, SideEffects::None()),
2546 class_is_final_(class_is_final),
2547 dex_pc_(dex_pc) {
2548 SetRawInputAt(0, object);
2549 SetRawInputAt(1, constant);
2550 }
2551
2552 bool CanBeMoved() const OVERRIDE { return true; }
2553
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002554 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002555 return true;
2556 }
2557
2558 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002559 return false;
2560 }
2561
2562 uint32_t GetDexPc() const { return dex_pc_; }
2563
2564 bool IsClassFinal() const { return class_is_final_; }
2565
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002566 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002567
2568 private:
2569 const bool class_is_final_;
2570 const uint32_t dex_pc_;
2571
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002572 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
2573};
2574
2575class HCheckCast : public HTemplateInstruction<2> {
2576 public:
2577 HCheckCast(HInstruction* object,
2578 HLoadClass* constant,
2579 bool class_is_final,
2580 uint32_t dex_pc)
2581 : HTemplateInstruction(SideEffects::None()),
2582 class_is_final_(class_is_final),
2583 dex_pc_(dex_pc) {
2584 SetRawInputAt(0, object);
2585 SetRawInputAt(1, constant);
2586 }
2587
2588 bool CanBeMoved() const OVERRIDE { return true; }
2589
2590 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2591 return true;
2592 }
2593
2594 bool NeedsEnvironment() const OVERRIDE {
2595 // Instruction may throw a CheckCastError.
2596 return true;
2597 }
2598
2599 bool CanThrow() const OVERRIDE { return true; }
2600
2601 uint32_t GetDexPc() const { return dex_pc_; }
2602
2603 bool IsClassFinal() const { return class_is_final_; }
2604
2605 DECLARE_INSTRUCTION(CheckCast);
2606
2607 private:
2608 const bool class_is_final_;
2609 const uint32_t dex_pc_;
2610
2611 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002612};
2613
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00002614class HMonitorOperation : public HTemplateInstruction<1> {
2615 public:
2616 enum OperationKind {
2617 kEnter,
2618 kExit,
2619 };
2620
2621 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
2622 : HTemplateInstruction(SideEffects::None()), kind_(kind), dex_pc_(dex_pc) {
2623 SetRawInputAt(0, object);
2624 }
2625
2626 // Instruction may throw a Java exception, so we need an environment.
2627 bool NeedsEnvironment() const OVERRIDE { return true; }
2628 bool CanThrow() const OVERRIDE { return true; }
2629
2630 uint32_t GetDexPc() const { return dex_pc_; }
2631
2632 bool IsEnter() const { return kind_ == kEnter; }
2633
2634 DECLARE_INSTRUCTION(MonitorOperation);
2635
2636 protected:
2637 const OperationKind kind_;
2638 const uint32_t dex_pc_;
2639
2640 private:
2641 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
2642};
2643
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002644
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002645class MoveOperands : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002646 public:
Nicolas Geoffray740475d2014-09-29 10:33:25 +01002647 MoveOperands(Location source, Location destination, HInstruction* instruction)
2648 : source_(source), destination_(destination), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002649
2650 Location GetSource() const { return source_; }
2651 Location GetDestination() const { return destination_; }
2652
2653 void SetSource(Location value) { source_ = value; }
2654 void SetDestination(Location value) { destination_ = value; }
2655
2656 // The parallel move resolver marks moves as "in-progress" by clearing the
2657 // destination (but not the source).
2658 Location MarkPending() {
2659 DCHECK(!IsPending());
2660 Location dest = destination_;
2661 destination_ = Location::NoLocation();
2662 return dest;
2663 }
2664
2665 void ClearPending(Location dest) {
2666 DCHECK(IsPending());
2667 destination_ = dest;
2668 }
2669
2670 bool IsPending() const {
2671 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
2672 return destination_.IsInvalid() && !source_.IsInvalid();
2673 }
2674
2675 // True if this blocks a move from the given location.
2676 bool Blocks(Location loc) const {
2677 return !IsEliminated() && source_.Equals(loc);
2678 }
2679
2680 // A move is redundant if it's been eliminated, if its source and
2681 // destination are the same, or if its destination is unneeded.
2682 bool IsRedundant() const {
2683 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
2684 }
2685
2686 // We clear both operands to indicate move that's been eliminated.
2687 void Eliminate() {
2688 source_ = destination_ = Location::NoLocation();
2689 }
2690
2691 bool IsEliminated() const {
2692 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
2693 return source_.IsInvalid();
2694 }
2695
Nicolas Geoffray740475d2014-09-29 10:33:25 +01002696 HInstruction* GetInstruction() const { return instruction_; }
2697
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002698 private:
2699 Location source_;
2700 Location destination_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01002701 // The instruction this move is assocatied with. Null when this move is
2702 // for moving an input in the expected locations of user (including a phi user).
2703 // This is only used in debug mode, to ensure we do not connect interval siblings
2704 // in the same parallel move.
2705 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002706
2707 DISALLOW_COPY_AND_ASSIGN(MoveOperands);
2708};
2709
2710static constexpr size_t kDefaultNumberOfMoves = 4;
2711
2712class HParallelMove : public HTemplateInstruction<0> {
2713 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002714 explicit HParallelMove(ArenaAllocator* arena)
2715 : HTemplateInstruction(SideEffects::None()), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002716
2717 void AddMove(MoveOperands* move) {
Nicolas Geoffray740475d2014-09-29 10:33:25 +01002718 if (kIsDebugBuild && move->GetInstruction() != nullptr) {
2719 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
2720 DCHECK_NE(moves_.Get(i)->GetInstruction(), move->GetInstruction())
2721 << "Doing parallel moves for the same instruction.";
2722 }
2723 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002724 moves_.Add(move);
2725 }
2726
2727 MoveOperands* MoveOperandsAt(size_t index) const {
2728 return moves_.Get(index);
2729 }
2730
2731 size_t NumMoves() const { return moves_.Size(); }
2732
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002733 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002734
2735 private:
2736 GrowableArray<MoveOperands*> moves_;
2737
2738 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
2739};
2740
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002741class HGraphVisitor : public ValueObject {
2742 public:
Dave Allison20dfc792014-06-16 20:44:29 -07002743 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
2744 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002745
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002746 virtual void VisitInstruction(HInstruction* instruction) { UNUSED(instruction); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002747 virtual void VisitBasicBlock(HBasicBlock* block);
2748
Roland Levillain633021e2014-10-01 14:12:25 +01002749 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002750 void VisitInsertionOrder();
2751
Roland Levillain633021e2014-10-01 14:12:25 +01002752 // Visit the graph following dominator tree reverse post-order.
2753 void VisitReversePostOrder();
2754
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002755 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002756
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002757 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002758#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002759 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
2760
2761 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
2762
2763#undef DECLARE_VISIT_INSTRUCTION
2764
2765 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07002766 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002767
2768 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
2769};
2770
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002771class HGraphDelegateVisitor : public HGraphVisitor {
2772 public:
2773 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
2774 virtual ~HGraphDelegateVisitor() {}
2775
2776 // Visit functions that delegate to to super class.
2777#define DECLARE_VISIT_INSTRUCTION(name, super) \
2778 virtual void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
2779
2780 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
2781
2782#undef DECLARE_VISIT_INSTRUCTION
2783
2784 private:
2785 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
2786};
2787
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002788class HInsertionOrderIterator : public ValueObject {
2789 public:
2790 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
2791
2792 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
2793 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
2794 void Advance() { ++index_; }
2795
2796 private:
2797 const HGraph& graph_;
2798 size_t index_;
2799
2800 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
2801};
2802
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002803class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002804 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002805 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002806
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002807 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
2808 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002809 void Advance() { ++index_; }
2810
2811 private:
2812 const HGraph& graph_;
2813 size_t index_;
2814
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002815 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002816};
2817
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002818class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002819 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002820 explicit HPostOrderIterator(const HGraph& graph)
2821 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002822
2823 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002824 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002825 void Advance() { --index_; }
2826
2827 private:
2828 const HGraph& graph_;
2829 size_t index_;
2830
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002831 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002832};
2833
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002834} // namespace art
2835
2836#endif // ART_COMPILER_OPTIMIZING_NODES_H_