blob: 0bd571a1fbe7a6264b8a877b58b2cc95680e0f2e [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 Geoffraye53798a2014-12-01 10:31:54 +000020#include "invoke_type.h"
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010021#include "locations.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010022#include "offsets.h"
Ian Rogerse63db272014-07-15 15:36:11 -070023#include "primitive.h"
Ian Rogers0279ebb2014-10-08 17:27:48 -070024#include "utils/arena_object.h"
Nicolas Geoffray0e336432014-02-26 18:24:38 +000025#include "utils/arena_bit_vector.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000026#include "utils/growable_array.h"
27
28namespace art {
29
30class HBasicBlock;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010031class HEnvironment;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000032class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000033class HIntConstant;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000034class HInvoke;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000035class HGraphVisitor;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010036class HPhi;
Nicolas Geoffray3c049742014-09-24 18:10:46 +010037class HSuspendCheck;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010038class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000039class LocationSummary;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000040
41static const int kDefaultNumberOfBlocks = 8;
42static const int kDefaultNumberOfSuccessors = 2;
43static const int kDefaultNumberOfPredecessors = 2;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010044static const int kDefaultNumberOfDominatedBlocks = 1;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000045static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000046
Calin Juravle9aec02f2014-11-18 23:06:35 +000047static constexpr uint32_t kMaxIntShiftValue = 0x1f;
48static constexpr uint64_t kMaxLongShiftValue = 0x3f;
49
Dave Allison20dfc792014-06-16 20:44:29 -070050enum IfCondition {
51 kCondEQ,
52 kCondNE,
53 kCondLT,
54 kCondLE,
55 kCondGT,
56 kCondGE,
57};
58
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010059class HInstructionList {
60 public:
61 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
62
63 void AddInstruction(HInstruction* instruction);
64 void RemoveInstruction(HInstruction* instruction);
65
Roland Levillain6b469232014-09-25 10:10:38 +010066 // Return true if this list contains `instruction`.
67 bool Contains(HInstruction* instruction) const;
68
Roland Levillainccc07a92014-09-16 14:48:16 +010069 // Return true if `instruction1` is found before `instruction2` in
70 // this instruction list and false otherwise. Abort if none
71 // of these instructions is found.
72 bool FoundBefore(const HInstruction* instruction1,
73 const HInstruction* instruction2) const;
74
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010075 private:
76 HInstruction* first_instruction_;
77 HInstruction* last_instruction_;
78
79 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000080 friend class HGraph;
81 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010082 friend class HInstructionIterator;
Nicolas Geoffray804d0932014-05-02 08:46:00 +010083 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010084
85 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
86};
87
Nicolas Geoffray818f2102014-02-18 16:43:35 +000088// Control-flow graph of a method. Contains a list of basic blocks.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070089class HGraph : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +000090 public:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000091 HGraph(ArenaAllocator* arena, int start_instruction_id = 0)
Nicolas Geoffray818f2102014-02-18 16:43:35 +000092 : arena_(arena),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000093 blocks_(arena, kDefaultNumberOfBlocks),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +010094 reverse_post_order_(arena, kDefaultNumberOfBlocks),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070095 entry_block_(nullptr),
96 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +010097 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +010098 number_of_vregs_(0),
99 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000100 temporaries_vreg_slots_(0),
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000101 current_instruction_id_(start_instruction_id) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000102
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000103 ArenaAllocator* GetArena() const { return arena_; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100104 const GrowableArray<HBasicBlock*>& GetBlocks() const { return blocks_; }
Roland Levillain93445682014-10-06 19:24:02 +0100105 HBasicBlock* GetBlock(size_t id) const { return blocks_.Get(id); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000106
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000107 HBasicBlock* GetEntryBlock() const { return entry_block_; }
108 HBasicBlock* GetExitBlock() const { return exit_block_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000109
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000110 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
111 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000112
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000113 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100114
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000115 // Try building the SSA form of this graph, with dominance computation and loop
116 // recognition. Returns whether it was successful in doing all these steps.
117 bool TryBuildingSsa() {
118 BuildDominatorTree();
119 TransformToSsa();
120 return AnalyzeNaturalLoops();
121 }
122
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000123 void BuildDominatorTree();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000124 void TransformToSsa();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100125 void SimplifyCFG();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000126
Nicolas Geoffrayf5370122014-12-02 11:51:19 +0000127 // Analyze all natural loops in this graph. Returns false if one
128 // loop is not natural, that is the header does not dominate the
129 // back edge.
130 bool AnalyzeNaturalLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100131
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000132 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
133 void InlineInto(HGraph* outer_graph, HInvoke* invoke);
134
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100135 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
136 void SimplifyLoop(HBasicBlock* header);
137
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000138 int32_t GetNextInstructionId() {
139 DCHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000140 return current_instruction_id_++;
141 }
142
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000143 int32_t GetCurrentInstructionId() const {
144 return current_instruction_id_;
145 }
146
147 void SetCurrentInstructionId(int32_t id) {
148 current_instruction_id_ = id;
149 }
150
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100151 uint16_t GetMaximumNumberOfOutVRegs() const {
152 return maximum_number_of_out_vregs_;
153 }
154
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000155 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
156 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100157 }
158
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000159 void UpdateTemporariesVRegSlots(size_t slots) {
160 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100161 }
162
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000163 size_t GetTemporariesVRegSlots() const {
164 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100165 }
166
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100167 void SetNumberOfVRegs(uint16_t number_of_vregs) {
168 number_of_vregs_ = number_of_vregs;
169 }
170
171 uint16_t GetNumberOfVRegs() const {
172 return number_of_vregs_;
173 }
174
175 void SetNumberOfInVRegs(uint16_t value) {
176 number_of_in_vregs_ = value;
177 }
178
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100179 uint16_t GetNumberOfLocalVRegs() const {
180 return number_of_vregs_ - number_of_in_vregs_;
181 }
182
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100183 const GrowableArray<HBasicBlock*>& GetReversePostOrder() const {
184 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100185 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100186
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000187 private:
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000188 HBasicBlock* FindCommonDominator(HBasicBlock* first, HBasicBlock* second) const;
189 void VisitBlockForDominatorTree(HBasicBlock* block,
190 HBasicBlock* predecessor,
191 GrowableArray<size_t>* visits);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100192 void FindBackEdges(ArenaBitVector* visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000193 void VisitBlockForBackEdges(HBasicBlock* block,
194 ArenaBitVector* visited,
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100195 ArenaBitVector* visiting);
Roland Levillainfc600dc2014-12-02 17:16:31 +0000196 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000197 void RemoveDeadBlocks(const ArenaBitVector& visited) const;
198
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000199 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000200
201 // List of blocks in insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000202 GrowableArray<HBasicBlock*> blocks_;
203
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100204 // List of blocks to perform a reverse post order tree traversal.
205 GrowableArray<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000206
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000207 HBasicBlock* entry_block_;
208 HBasicBlock* exit_block_;
209
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100210 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100211 uint16_t maximum_number_of_out_vregs_;
212
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100213 // The number of virtual registers in this method. Contains the parameters.
214 uint16_t number_of_vregs_;
215
216 // The number of virtual registers used by parameters of this method.
217 uint16_t number_of_in_vregs_;
218
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000219 // Number of vreg size slots that the temporaries use (used in baseline compiler).
220 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100221
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000222 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000223 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000224
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000225 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000226 DISALLOW_COPY_AND_ASSIGN(HGraph);
227};
228
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700229class HLoopInformation : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000230 public:
231 HLoopInformation(HBasicBlock* header, HGraph* graph)
232 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100233 suspend_check_(nullptr),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100234 back_edges_(graph->GetArena(), kDefaultNumberOfBackEdges),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100235 // Make bit vector growable, as the number of blocks may change.
236 blocks_(graph->GetArena(), graph->GetBlocks().Size(), true) {}
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100237
238 HBasicBlock* GetHeader() const {
239 return header_;
240 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000241
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100242 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
243 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
244 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
245
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000246 void AddBackEdge(HBasicBlock* back_edge) {
247 back_edges_.Add(back_edge);
248 }
249
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100250 void RemoveBackEdge(HBasicBlock* back_edge) {
251 back_edges_.Delete(back_edge);
252 }
253
254 bool IsBackEdge(HBasicBlock* block) {
255 for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) {
256 if (back_edges_.Get(i) == block) return true;
257 }
258 return false;
259 }
260
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000261 size_t NumberOfBackEdges() const {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000262 return back_edges_.Size();
263 }
264
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100265 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100266
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100267 const GrowableArray<HBasicBlock*>& GetBackEdges() const {
268 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100269 }
270
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100271 void ClearBackEdges() {
272 back_edges_.Reset();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100273 }
274
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100275 // Find blocks that are part of this loop. Returns whether the loop is a natural loop,
276 // that is the header dominates the back edge.
277 bool Populate();
278
279 // Returns whether this loop information contains `block`.
280 // Note that this loop information *must* be populated before entering this function.
281 bool Contains(const HBasicBlock& block) const;
282
283 // Returns whether this loop information is an inner loop of `other`.
284 // Note that `other` *must* be populated before entering this function.
285 bool IsIn(const HLoopInformation& other) const;
286
287 const ArenaBitVector& GetBlocks() const { return blocks_; }
288
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000289 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100290 // Internal recursive implementation of `Populate`.
291 void PopulateRecursive(HBasicBlock* block);
292
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000293 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100294 HSuspendCheck* suspend_check_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000295 GrowableArray<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100296 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000297
298 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
299};
300
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100301static constexpr size_t kNoLifetime = -1;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100302static constexpr uint32_t kNoDexPc = -1;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100303
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000304// A block in a method. Contains the list of instructions represented
305// as a double linked list. Each block knows its predecessors and
306// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100307
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700308class HBasicBlock : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000309 public:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100310 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000311 : graph_(graph),
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000312 predecessors_(graph->GetArena(), kDefaultNumberOfPredecessors),
313 successors_(graph->GetArena(), kDefaultNumberOfSuccessors),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000314 loop_information_(nullptr),
315 dominator_(nullptr),
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100316 dominated_blocks_(graph->GetArena(), kDefaultNumberOfDominatedBlocks),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100317 block_id_(-1),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100318 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100319 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000320 lifetime_end_(kNoLifetime),
321 is_catch_block_(false) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000322
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100323 const GrowableArray<HBasicBlock*>& GetPredecessors() const {
324 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000325 }
326
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100327 const GrowableArray<HBasicBlock*>& GetSuccessors() const {
328 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000329 }
330
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100331 const GrowableArray<HBasicBlock*>& GetDominatedBlocks() const {
332 return dominated_blocks_;
333 }
334
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100335 bool IsEntryBlock() const {
336 return graph_->GetEntryBlock() == this;
337 }
338
339 bool IsExitBlock() const {
340 return graph_->GetExitBlock() == this;
341 }
342
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000343 void AddBackEdge(HBasicBlock* back_edge) {
344 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000345 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000346 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100347 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000348 loop_information_->AddBackEdge(back_edge);
349 }
350
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000351 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000352
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000353 int GetBlockId() const { return block_id_; }
354 void SetBlockId(int id) { block_id_ = id; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000355
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000356 HBasicBlock* GetDominator() const { return dominator_; }
357 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100358 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.Add(block); }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000359
360 int NumberOfBackEdges() const {
361 return loop_information_ == nullptr
362 ? 0
363 : loop_information_->NumberOfBackEdges();
364 }
365
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100366 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
367 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100368 const HInstructionList& GetInstructions() const { return instructions_; }
369 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100370 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000371
372 void AddSuccessor(HBasicBlock* block) {
373 successors_.Add(block);
374 block->predecessors_.Add(this);
375 }
376
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100377 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
378 size_t successor_index = GetSuccessorIndexOf(existing);
379 DCHECK_NE(successor_index, static_cast<size_t>(-1));
380 existing->RemovePredecessor(this);
381 new_block->predecessors_.Add(this);
382 successors_.Put(successor_index, new_block);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000383 }
384
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100385 void RemovePredecessor(HBasicBlock* block) {
386 predecessors_.Delete(block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100387 }
388
389 void ClearAllPredecessors() {
390 predecessors_.Reset();
391 }
392
393 void AddPredecessor(HBasicBlock* block) {
394 predecessors_.Add(block);
395 block->successors_.Add(this);
396 }
397
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100398 void SwapPredecessors() {
Nicolas Geoffrayc83d4412014-09-18 16:46:20 +0100399 DCHECK_EQ(predecessors_.Size(), 2u);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100400 HBasicBlock* temp = predecessors_.Get(0);
401 predecessors_.Put(0, predecessors_.Get(1));
402 predecessors_.Put(1, temp);
403 }
404
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100405 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) {
406 for (size_t i = 0, e = predecessors_.Size(); i < e; ++i) {
407 if (predecessors_.Get(i) == predecessor) {
408 return i;
409 }
410 }
411 return -1;
412 }
413
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100414 size_t GetSuccessorIndexOf(HBasicBlock* successor) {
415 for (size_t i = 0, e = successors_.Size(); i < e; ++i) {
416 if (successors_.Get(i) == successor) {
417 return i;
418 }
419 }
420 return -1;
421 }
422
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000423 void AddInstruction(HInstruction* instruction);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100424 void RemoveInstruction(HInstruction* instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100425 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +0100426 // Replace instruction `initial` with `replacement` within this block.
427 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
428 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100429 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100430 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100431 void RemovePhi(HPhi* phi);
432
433 bool IsLoopHeader() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100434 return (loop_information_ != nullptr) && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100435 }
436
Roland Levillain6b879dd2014-09-22 17:13:44 +0100437 bool IsLoopPreHeaderFirstPredecessor() const {
438 DCHECK(IsLoopHeader());
439 DCHECK(!GetPredecessors().IsEmpty());
440 return GetPredecessors().Get(0) == GetLoopInformation()->GetPreHeader();
441 }
442
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100443 HLoopInformation* GetLoopInformation() const {
444 return loop_information_;
445 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000446
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100447 // Set the loop_information_ on this block. This method overrides the current
448 // loop_information if it is an outer loop of the passed loop information.
449 void SetInLoop(HLoopInformation* info) {
450 if (IsLoopHeader()) {
451 // Nothing to do. This just means `info` is an outer loop.
452 } else if (loop_information_ == nullptr) {
453 loop_information_ = info;
454 } else if (loop_information_->Contains(*info->GetHeader())) {
455 // Block is currently part of an outer loop. Make it part of this inner loop.
456 // Note that a non loop header having a loop information means this loop information
457 // has already been populated
458 loop_information_ = info;
459 } else {
460 // Block is part of an inner loop. Do not update the loop information.
461 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
462 // at this point, because this method is being called while populating `info`.
463 }
464 }
465
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100466 bool IsInLoop() const { return loop_information_ != nullptr; }
467
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100468 // Returns wheter this block dominates the blocked passed as parameter.
469 bool Dominates(HBasicBlock* block) const;
470
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100471 size_t GetLifetimeStart() const { return lifetime_start_; }
472 size_t GetLifetimeEnd() const { return lifetime_end_; }
473
474 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
475 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
476
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100477 uint32_t GetDexPc() const { return dex_pc_; }
478
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000479 bool IsCatchBlock() const { return is_catch_block_; }
480 void SetIsCatchBlock() { is_catch_block_ = true; }
481
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000482 private:
483 HGraph* const graph_;
484 GrowableArray<HBasicBlock*> predecessors_;
485 GrowableArray<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100486 HInstructionList instructions_;
487 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000488 HLoopInformation* loop_information_;
489 HBasicBlock* dominator_;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100490 GrowableArray<HBasicBlock*> dominated_blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000491 int block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100492 // The dex program counter of the first instruction of this block.
493 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100494 size_t lifetime_start_;
495 size_t lifetime_end_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000496 bool is_catch_block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000497
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000498 friend class HGraph;
499 friend class HInstruction;
500
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000501 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
502};
503
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100504#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
505 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000506 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000507 M(ArrayGet, Instruction) \
508 M(ArrayLength, Instruction) \
509 M(ArraySet, Instruction) \
510 M(BoundsCheck, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000511 M(CheckCast, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100512 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000513 M(Compare, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100514 M(Condition, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000515 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +0000516 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000517 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100518 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000519 M(Exit, Instruction) \
520 M(FloatConstant, Constant) \
521 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100522 M(GreaterThan, Condition) \
523 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100524 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000525 M(InstanceFieldGet, Instruction) \
526 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000527 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100528 M(IntConstant, Constant) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +0000529 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000530 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100531 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000532 M(LessThan, Condition) \
533 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000534 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000535 M(LoadException, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100536 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000537 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100538 M(Local, Instruction) \
539 M(LongConstant, Constant) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +0000540 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000541 M(Mul, BinaryOperation) \
542 M(Neg, UnaryOperation) \
543 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100544 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +0100545 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000546 M(NotEqual, Condition) \
547 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000548 M(Or, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100549 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000550 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100551 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000552 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100553 M(Return, Instruction) \
554 M(ReturnVoid, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000555 M(Shl, BinaryOperation) \
556 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100557 M(StaticFieldGet, Instruction) \
558 M(StaticFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100559 M(StoreLocal, Instruction) \
560 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100561 M(SuspendCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000562 M(Temporary, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000563 M(Throw, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +0000564 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000565 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000566 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000567
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100568#define FOR_EACH_INSTRUCTION(M) \
569 FOR_EACH_CONCRETE_INSTRUCTION(M) \
570 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +0100571 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +0100572 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100573 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -0700574
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100575#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000576FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
577#undef FORWARD_DECLARATION
578
Roland Levillainccc07a92014-09-16 14:48:16 +0100579#define DECLARE_INSTRUCTION(type) \
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100580 virtual InstructionKind GetKind() const { return k##type; } \
Roland Levillainccc07a92014-09-16 14:48:16 +0100581 virtual const char* DebugName() const { return #type; } \
582 virtual const H##type* As##type() const OVERRIDE { return this; } \
583 virtual H##type* As##type() OVERRIDE { return this; } \
584 virtual bool InstructionTypeEquals(HInstruction* other) const { \
585 return other->Is##type(); \
586 } \
587 virtual void Accept(HGraphVisitor* visitor)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000588
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100589template <typename T>
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700590class HUseListNode : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000591 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100592 HUseListNode(T* user, size_t index, HUseListNode* tail)
Dave Allison20dfc792014-06-16 20:44:29 -0700593 : user_(user), index_(index), tail_(tail) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000594
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000595 HUseListNode* GetTail() const { return tail_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100596 T* GetUser() const { return user_; }
597 size_t GetIndex() const { return index_; }
598
599 void SetTail(HUseListNode<T>* node) { tail_ = node; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000600
601 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100602 T* const user_;
603 const size_t index_;
604 HUseListNode<T>* tail_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000605
606 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
607};
608
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100609// Represents the side effects an instruction may have.
610class SideEffects : public ValueObject {
611 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100612 SideEffects() : flags_(0) {}
613
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100614 static SideEffects None() {
615 return SideEffects(0);
616 }
617
618 static SideEffects All() {
619 return SideEffects(ChangesSomething().flags_ | DependsOnSomething().flags_);
620 }
621
622 static SideEffects ChangesSomething() {
623 return SideEffects((1 << kFlagChangesCount) - 1);
624 }
625
626 static SideEffects DependsOnSomething() {
627 int count = kFlagDependsOnCount - kFlagChangesCount;
628 return SideEffects(((1 << count) - 1) << kFlagChangesCount);
629 }
630
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100631 SideEffects Union(SideEffects other) const {
632 return SideEffects(flags_ | other.flags_);
633 }
634
Roland Levillain72bceff2014-09-15 18:29:00 +0100635 bool HasSideEffects() const {
636 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
637 return (flags_ & all_bits_set) != 0;
638 }
639
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100640 bool HasAllSideEffects() const {
641 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
642 return all_bits_set == (flags_ & all_bits_set);
643 }
644
645 bool DependsOn(SideEffects other) const {
646 size_t depends_flags = other.ComputeDependsFlags();
647 return (flags_ & depends_flags) != 0;
648 }
649
650 bool HasDependencies() const {
651 int count = kFlagDependsOnCount - kFlagChangesCount;
652 size_t all_bits_set = (1 << count) - 1;
653 return ((flags_ >> kFlagChangesCount) & all_bits_set) != 0;
654 }
655
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100656 private:
657 static constexpr int kFlagChangesSomething = 0;
658 static constexpr int kFlagChangesCount = kFlagChangesSomething + 1;
659
660 static constexpr int kFlagDependsOnSomething = kFlagChangesCount;
661 static constexpr int kFlagDependsOnCount = kFlagDependsOnSomething + 1;
662
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100663 explicit SideEffects(size_t flags) : flags_(flags) {}
664
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100665 size_t ComputeDependsFlags() const {
666 return flags_ << kFlagChangesCount;
667 }
668
669 size_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100670};
671
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700672class HInstruction : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000673 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100674 explicit HInstruction(SideEffects side_effects)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000675 : previous_(nullptr),
676 next_(nullptr),
677 block_(nullptr),
678 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100679 ssa_index_(-1),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000680 uses_(nullptr),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100681 env_uses_(nullptr),
682 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100683 locations_(nullptr),
684 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100685 lifetime_position_(kNoLifetime),
686 side_effects_(side_effects) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000687
Dave Allison20dfc792014-06-16 20:44:29 -0700688 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000689
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100690#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100691 enum InstructionKind {
692 FOR_EACH_INSTRUCTION(DECLARE_KIND)
693 };
694#undef DECLARE_KIND
695
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000696 HInstruction* GetNext() const { return next_; }
697 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000698
Calin Juravle77520bc2015-01-12 18:45:46 +0000699 HInstruction* GetNextDisregardingMoves() const;
700 HInstruction* GetPreviousDisregardingMoves() const;
701
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000702 HBasicBlock* GetBlock() const { return block_; }
703 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100704 bool IsInBlock() const { return block_ != nullptr; }
705 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +0100706 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000707
Roland Levillain6b879dd2014-09-22 17:13:44 +0100708 virtual size_t InputCount() const = 0;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100709 virtual HInstruction* InputAt(size_t i) const = 0;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000710
711 virtual void Accept(HGraphVisitor* visitor) = 0;
712 virtual const char* DebugName() const = 0;
713
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100714 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100715 virtual void SetRawInputAt(size_t index, HInstruction* input) = 0;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100716
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100717 virtual bool NeedsEnvironment() const { return false; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100718 virtual bool IsControlFlow() const { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +0100719 virtual bool CanThrow() const { return false; }
Roland Levillain72bceff2014-09-15 18:29:00 +0100720 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100721
Calin Juravle77520bc2015-01-12 18:45:46 +0000722 virtual bool CanDoImplicitNullCheck() const { return false; }
723
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100724 void AddUseAt(HInstruction* user, size_t index) {
725 uses_ = new (block_->GetGraph()->GetArena()) HUseListNode<HInstruction>(user, index, uses_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000726 }
727
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100728 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +0100729 DCHECK(user != nullptr);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100730 env_uses_ = new (block_->GetGraph()->GetArena()) HUseListNode<HEnvironment>(
731 user, index, env_uses_);
732 }
733
734 void RemoveUser(HInstruction* user, size_t index);
Nicolas Geoffray724c9632014-09-22 12:27:27 +0100735 void RemoveEnvironmentUser(HEnvironment* user, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100736
737 HUseListNode<HInstruction>* GetUses() const { return uses_; }
738 HUseListNode<HEnvironment>* GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000739
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100740 bool HasUses() const { return uses_ != nullptr || env_uses_ != nullptr; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100741 bool HasEnvironmentUses() const { return env_uses_ != nullptr; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000742
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100743 size_t NumberOfUses() const {
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100744 // TODO: Optimize this method if it is used outside of the HGraphVisualizer.
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100745 size_t result = 0;
746 HUseListNode<HInstruction>* current = uses_;
747 while (current != nullptr) {
748 current = current->GetTail();
749 ++result;
750 }
751 return result;
752 }
753
Roland Levillain6c82d402014-10-13 16:10:27 +0100754 // Does this instruction strictly dominate `other_instruction`?
755 // Returns false if this instruction and `other_instruction` are the same.
756 // Aborts if this instruction and `other_instruction` are both phis.
757 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +0100758
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000759 int GetId() const { return id_; }
760 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000761
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100762 int GetSsaIndex() const { return ssa_index_; }
763 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
764 bool HasSsaIndex() const { return ssa_index_ != -1; }
765
766 bool HasEnvironment() const { return environment_ != nullptr; }
767 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100768 void SetEnvironment(HEnvironment* environment) { environment_ = environment; }
769
Nicolas Geoffray39468442014-09-02 15:17:15 +0100770 // Returns the number of entries in the environment. Typically, that is the
771 // number of dex registers in a method. It could be more in case of inlining.
772 size_t EnvironmentSize() const;
773
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000774 LocationSummary* GetLocations() const { return locations_; }
775 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000776
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100777 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100778 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100779
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000780 // Insert `this` instruction in `cursor`'s graph, just before `cursor`.
781 void InsertBefore(HInstruction* cursor);
782
Dave Allison20dfc792014-06-16 20:44:29 -0700783 bool HasOnlyOneUse() const {
784 return uses_ != nullptr && uses_->GetTail() == nullptr;
785 }
786
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100787#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +0100788 bool Is##type() const { return (As##type() != nullptr); } \
789 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000790 virtual H##type* As##type() { return nullptr; }
791
792 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
793#undef INSTRUCTION_TYPE_CHECK
794
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100795 // Returns whether the instruction can be moved within the graph.
796 virtual bool CanBeMoved() const { return false; }
797
798 // Returns whether the two instructions are of the same kind.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700799 virtual bool InstructionTypeEquals(HInstruction* other) const {
800 UNUSED(other);
801 return false;
802 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100803
804 // Returns whether any data encoded in the two instructions is equal.
805 // This method does not look at the inputs. Both instructions must be
806 // of the same type, otherwise the method has undefined behavior.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700807 virtual bool InstructionDataEquals(HInstruction* other) const {
808 UNUSED(other);
809 return false;
810 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100811
812 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +0000813 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100814 // 2) Their inputs are identical.
815 bool Equals(HInstruction* other) const;
816
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100817 virtual InstructionKind GetKind() const = 0;
818
819 virtual size_t ComputeHashCode() const {
820 size_t result = GetKind();
821 for (size_t i = 0, e = InputCount(); i < e; ++i) {
822 result = (result * 31) + InputAt(i)->GetId();
823 }
824 return result;
825 }
826
827 SideEffects GetSideEffects() const { return side_effects_; }
828
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100829 size_t GetLifetimePosition() const { return lifetime_position_; }
830 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
831 LiveInterval* GetLiveInterval() const { return live_interval_; }
832 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
833 bool HasLiveInterval() const { return live_interval_ != nullptr; }
834
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000835 private:
836 HInstruction* previous_;
837 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000838 HBasicBlock* block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000839
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000840 // An instruction gets an id when it is added to the graph.
841 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +0100842 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000843 int id_;
844
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100845 // When doing liveness analysis, instructions that have uses get an SSA index.
846 int ssa_index_;
847
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100848 // List of instructions that have this instruction as input.
849 HUseListNode<HInstruction>* uses_;
850
851 // List of environments that contain this instruction.
852 HUseListNode<HEnvironment>* env_uses_;
853
Nicolas Geoffray39468442014-09-02 15:17:15 +0100854 // The environment associated with this instruction. Not null if the instruction
855 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100856 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000857
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000858 // Set by the code generator.
859 LocationSummary* locations_;
860
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100861 // Set by the liveness analysis.
862 LiveInterval* live_interval_;
863
864 // Set by the liveness analysis, this is the position in a linear
865 // order of blocks where this instruction's live interval start.
866 size_t lifetime_position_;
867
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100868 const SideEffects side_effects_;
869
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000870 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000871 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100872 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000873
874 DISALLOW_COPY_AND_ASSIGN(HInstruction);
875};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700876std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000877
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100878template<typename T>
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000879class HUseIterator : public ValueObject {
880 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100881 explicit HUseIterator(HUseListNode<T>* uses) : current_(uses) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000882
883 bool Done() const { return current_ == nullptr; }
884
885 void Advance() {
886 DCHECK(!Done());
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000887 current_ = current_->GetTail();
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000888 }
889
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100890 HUseListNode<T>* Current() const {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000891 DCHECK(!Done());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100892 return current_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000893 }
894
895 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100896 HUseListNode<T>* current_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000897
898 friend class HValue;
899};
900
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100901// A HEnvironment object contains the values of virtual registers at a given location.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700902class HEnvironment : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100903 public:
904 HEnvironment(ArenaAllocator* arena, size_t number_of_vregs) : vregs_(arena, number_of_vregs) {
905 vregs_.SetSize(number_of_vregs);
906 for (size_t i = 0; i < number_of_vregs; i++) {
907 vregs_.Put(i, nullptr);
908 }
909 }
910
911 void Populate(const GrowableArray<HInstruction*>& env) {
912 for (size_t i = 0; i < env.Size(); i++) {
913 HInstruction* instruction = env.Get(i);
914 vregs_.Put(i, instruction);
915 if (instruction != nullptr) {
916 instruction->AddEnvUseAt(this, i);
917 }
918 }
919 }
920
921 void SetRawEnvAt(size_t index, HInstruction* instruction) {
922 vregs_.Put(index, instruction);
923 }
924
Nicolas Geoffray39468442014-09-02 15:17:15 +0100925 HInstruction* GetInstructionAt(size_t index) const {
926 return vregs_.Get(index);
927 }
928
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100929 GrowableArray<HInstruction*>* GetVRegs() {
930 return &vregs_;
931 }
932
Nicolas Geoffray39468442014-09-02 15:17:15 +0100933 size_t Size() const { return vregs_.Size(); }
934
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100935 private:
936 GrowableArray<HInstruction*> vregs_;
937
938 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
939};
940
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000941class HInputIterator : public ValueObject {
942 public:
Dave Allison20dfc792014-06-16 20:44:29 -0700943 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000944
945 bool Done() const { return index_ == instruction_->InputCount(); }
946 HInstruction* Current() const { return instruction_->InputAt(index_); }
947 void Advance() { index_++; }
948
949 private:
950 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100951 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000952
953 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
954};
955
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000956class HInstructionIterator : public ValueObject {
957 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100958 explicit HInstructionIterator(const HInstructionList& instructions)
959 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000960 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000961 }
962
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000963 bool Done() const { return instruction_ == nullptr; }
964 HInstruction* Current() const { return instruction_; }
965 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000966 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000967 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000968 }
969
970 private:
971 HInstruction* instruction_;
972 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100973
974 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000975};
976
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100977class HBackwardInstructionIterator : public ValueObject {
978 public:
979 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
980 : instruction_(instructions.last_instruction_) {
981 next_ = Done() ? nullptr : instruction_->GetPrevious();
982 }
983
984 bool Done() const { return instruction_ == nullptr; }
985 HInstruction* Current() const { return instruction_; }
986 void Advance() {
987 instruction_ = next_;
988 next_ = Done() ? nullptr : instruction_->GetPrevious();
989 }
990
991 private:
992 HInstruction* instruction_;
993 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100994
995 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100996};
997
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000998// An embedded container with N elements of type T. Used (with partial
999// specialization for N=0) because embedded arrays cannot have size 0.
1000template<typename T, intptr_t N>
1001class EmbeddedArray {
1002 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001003 EmbeddedArray() : elements_() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001004
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001005 intptr_t GetLength() const { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001006
1007 const T& operator[](intptr_t i) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001008 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001009 return elements_[i];
1010 }
1011
1012 T& operator[](intptr_t i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001013 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001014 return elements_[i];
1015 }
1016
1017 const T& At(intptr_t i) const {
1018 return (*this)[i];
1019 }
1020
1021 void SetAt(intptr_t i, const T& val) {
1022 (*this)[i] = val;
1023 }
1024
1025 private:
1026 T elements_[N];
1027};
1028
1029template<typename T>
1030class EmbeddedArray<T, 0> {
1031 public:
1032 intptr_t length() const { return 0; }
1033 const T& operator[](intptr_t i) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001034 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001035 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001036 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001037 }
1038 T& operator[](intptr_t i) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001039 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001040 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001041 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001042 }
1043};
1044
1045template<intptr_t N>
1046class HTemplateInstruction: public HInstruction {
1047 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001048 HTemplateInstruction<N>(SideEffects side_effects)
1049 : HInstruction(side_effects), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07001050 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001051
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001052 virtual size_t InputCount() const { return N; }
1053 virtual HInstruction* InputAt(size_t i) const { return inputs_[i]; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001054
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001055 protected:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001056 virtual void SetRawInputAt(size_t i, HInstruction* instruction) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001057 inputs_[i] = instruction;
1058 }
1059
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001060 private:
1061 EmbeddedArray<HInstruction*, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001062
1063 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001064};
1065
Dave Allison20dfc792014-06-16 20:44:29 -07001066template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001067class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07001068 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001069 HExpression<N>(Primitive::Type type, SideEffects side_effects)
1070 : HTemplateInstruction<N>(side_effects), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07001071 virtual ~HExpression() {}
1072
1073 virtual Primitive::Type GetType() const { return type_; }
1074
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001075 protected:
1076 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07001077};
1078
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001079// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
1080// instruction that branches to the exit block.
1081class HReturnVoid : public HTemplateInstruction<0> {
1082 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001083 HReturnVoid() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001084
1085 virtual bool IsControlFlow() const { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001086
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001087 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001088
1089 private:
1090 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
1091};
1092
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001093// Represents dex's RETURN opcodes. A HReturn is a control flow
1094// instruction that branches to the exit block.
1095class HReturn : public HTemplateInstruction<1> {
1096 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001097 explicit HReturn(HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001098 SetRawInputAt(0, value);
1099 }
1100
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001101 virtual bool IsControlFlow() const { return true; }
1102
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001103 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001104
1105 private:
1106 DISALLOW_COPY_AND_ASSIGN(HReturn);
1107};
1108
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001109// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001110// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001111// exit block.
1112class HExit : public HTemplateInstruction<0> {
1113 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001114 HExit() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001115
1116 virtual bool IsControlFlow() const { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001117
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001118 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001119
1120 private:
1121 DISALLOW_COPY_AND_ASSIGN(HExit);
1122};
1123
1124// Jumps from one block to another.
1125class HGoto : public HTemplateInstruction<0> {
1126 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001127 HGoto() : HTemplateInstruction(SideEffects::None()) {}
1128
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001129 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001130
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001131 HBasicBlock* GetSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001132 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001133 }
1134
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001135 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001136
1137 private:
1138 DISALLOW_COPY_AND_ASSIGN(HGoto);
1139};
1140
Dave Allison20dfc792014-06-16 20:44:29 -07001141
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001142// Conditional branch. A block ending with an HIf instruction must have
1143// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001144class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001145 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001146 explicit HIf(HInstruction* input) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001147 SetRawInputAt(0, input);
1148 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001149
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001150 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001151
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001152 HBasicBlock* IfTrueSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001153 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001154 }
1155
1156 HBasicBlock* IfFalseSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001157 return GetBlock()->GetSuccessors().Get(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001158 }
1159
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001160 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001161
Dave Allison20dfc792014-06-16 20:44:29 -07001162 virtual bool IsIfInstruction() const { return true; }
1163
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001164 private:
1165 DISALLOW_COPY_AND_ASSIGN(HIf);
1166};
1167
Roland Levillain88cb1752014-10-20 16:36:47 +01001168class HUnaryOperation : public HExpression<1> {
1169 public:
1170 HUnaryOperation(Primitive::Type result_type, HInstruction* input)
1171 : HExpression(result_type, SideEffects::None()) {
1172 SetRawInputAt(0, input);
1173 }
1174
1175 HInstruction* GetInput() const { return InputAt(0); }
1176 Primitive::Type GetResultType() const { return GetType(); }
1177
1178 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001179 virtual bool InstructionDataEquals(HInstruction* other) const {
1180 UNUSED(other);
1181 return true;
1182 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001183
Roland Levillain9240d6a2014-10-20 16:47:04 +01001184 // Try to statically evaluate `operation` and return a HConstant
1185 // containing the result of this evaluation. If `operation` cannot
1186 // be evaluated as a constant, return nullptr.
1187 HConstant* TryStaticEvaluation() const;
1188
1189 // Apply this operation to `x`.
1190 virtual int32_t Evaluate(int32_t x) const = 0;
1191 virtual int64_t Evaluate(int64_t x) const = 0;
1192
Roland Levillain88cb1752014-10-20 16:36:47 +01001193 DECLARE_INSTRUCTION(UnaryOperation);
1194
1195 private:
1196 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
1197};
1198
Dave Allison20dfc792014-06-16 20:44:29 -07001199class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001200 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001201 HBinaryOperation(Primitive::Type result_type,
1202 HInstruction* left,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001203 HInstruction* right) : HExpression(result_type, SideEffects::None()) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001204 SetRawInputAt(0, left);
1205 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001206 }
1207
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001208 HInstruction* GetLeft() const { return InputAt(0); }
1209 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07001210 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001211
1212 virtual bool IsCommutative() { return false; }
1213
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001214 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001215 virtual bool InstructionDataEquals(HInstruction* other) const {
1216 UNUSED(other);
1217 return true;
1218 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001219
Roland Levillain9240d6a2014-10-20 16:47:04 +01001220 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01001221 // containing the result of this evaluation. If `operation` cannot
1222 // be evaluated as a constant, return nullptr.
Roland Levillain9240d6a2014-10-20 16:47:04 +01001223 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01001224
1225 // Apply this operation to `x` and `y`.
1226 virtual int32_t Evaluate(int32_t x, int32_t y) const = 0;
1227 virtual int64_t Evaluate(int64_t x, int64_t y) const = 0;
1228
Roland Levillainccc07a92014-09-16 14:48:16 +01001229 DECLARE_INSTRUCTION(BinaryOperation);
1230
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001231 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001232 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
1233};
1234
Dave Allison20dfc792014-06-16 20:44:29 -07001235class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001236 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001237 HCondition(HInstruction* first, HInstruction* second)
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001238 : HBinaryOperation(Primitive::kPrimBoolean, first, second),
1239 needs_materialization_(true) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001240
1241 virtual bool IsCommutative() { return true; }
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001242
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001243 bool NeedsMaterialization() const { return needs_materialization_; }
1244 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001245
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001246 // For code generation purposes, returns whether this instruction is just before
1247 // `if_`, and disregard moves in between.
1248 bool IsBeforeWhenDisregardMoves(HIf* if_) const;
1249
Dave Allison20dfc792014-06-16 20:44:29 -07001250 DECLARE_INSTRUCTION(Condition);
1251
1252 virtual IfCondition GetCondition() const = 0;
1253
1254 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001255 // For register allocation purposes, returns whether this instruction needs to be
1256 // materialized (that is, not just be in the processor flags).
1257 bool needs_materialization_;
1258
Dave Allison20dfc792014-06-16 20:44:29 -07001259 DISALLOW_COPY_AND_ASSIGN(HCondition);
1260};
1261
1262// Instruction to check if two inputs are equal to each other.
1263class HEqual : public HCondition {
1264 public:
1265 HEqual(HInstruction* first, HInstruction* second)
1266 : HCondition(first, second) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001267
Roland Levillain93445682014-10-06 19:24:02 +01001268 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1269 return x == y ? 1 : 0;
1270 }
1271 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1272 return x == y ? 1 : 0;
1273 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001274
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001275 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001276
Dave Allison20dfc792014-06-16 20:44:29 -07001277 virtual IfCondition GetCondition() const {
1278 return kCondEQ;
1279 }
1280
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001281 private:
1282 DISALLOW_COPY_AND_ASSIGN(HEqual);
1283};
1284
Dave Allison20dfc792014-06-16 20:44:29 -07001285class HNotEqual : public HCondition {
1286 public:
1287 HNotEqual(HInstruction* first, HInstruction* second)
1288 : HCondition(first, second) {}
1289
Roland Levillain93445682014-10-06 19:24:02 +01001290 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1291 return x != y ? 1 : 0;
1292 }
1293 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1294 return x != y ? 1 : 0;
1295 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001296
Dave Allison20dfc792014-06-16 20:44:29 -07001297 DECLARE_INSTRUCTION(NotEqual);
1298
1299 virtual IfCondition GetCondition() const {
1300 return kCondNE;
1301 }
1302
1303 private:
1304 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
1305};
1306
1307class HLessThan : public HCondition {
1308 public:
1309 HLessThan(HInstruction* first, HInstruction* second)
1310 : HCondition(first, second) {}
1311
Roland Levillain93445682014-10-06 19:24:02 +01001312 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1313 return x < y ? 1 : 0;
1314 }
1315 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1316 return x < y ? 1 : 0;
1317 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001318
Dave Allison20dfc792014-06-16 20:44:29 -07001319 DECLARE_INSTRUCTION(LessThan);
1320
1321 virtual IfCondition GetCondition() const {
1322 return kCondLT;
1323 }
1324
1325 private:
1326 DISALLOW_COPY_AND_ASSIGN(HLessThan);
1327};
1328
1329class HLessThanOrEqual : public HCondition {
1330 public:
1331 HLessThanOrEqual(HInstruction* first, HInstruction* second)
1332 : HCondition(first, second) {}
1333
Roland Levillain93445682014-10-06 19:24:02 +01001334 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1335 return x <= y ? 1 : 0;
1336 }
1337 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1338 return x <= y ? 1 : 0;
1339 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001340
Dave Allison20dfc792014-06-16 20:44:29 -07001341 DECLARE_INSTRUCTION(LessThanOrEqual);
1342
1343 virtual IfCondition GetCondition() const {
1344 return kCondLE;
1345 }
1346
1347 private:
1348 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
1349};
1350
1351class HGreaterThan : public HCondition {
1352 public:
1353 HGreaterThan(HInstruction* first, HInstruction* second)
1354 : HCondition(first, second) {}
1355
Roland Levillain93445682014-10-06 19:24:02 +01001356 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1357 return x > y ? 1 : 0;
1358 }
1359 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1360 return x > y ? 1 : 0;
1361 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001362
Dave Allison20dfc792014-06-16 20:44:29 -07001363 DECLARE_INSTRUCTION(GreaterThan);
1364
1365 virtual IfCondition GetCondition() const {
1366 return kCondGT;
1367 }
1368
1369 private:
1370 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
1371};
1372
1373class HGreaterThanOrEqual : public HCondition {
1374 public:
1375 HGreaterThanOrEqual(HInstruction* first, HInstruction* second)
1376 : HCondition(first, second) {}
1377
Roland Levillain93445682014-10-06 19:24:02 +01001378 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1379 return x >= y ? 1 : 0;
1380 }
1381 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1382 return x >= y ? 1 : 0;
1383 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001384
Dave Allison20dfc792014-06-16 20:44:29 -07001385 DECLARE_INSTRUCTION(GreaterThanOrEqual);
1386
1387 virtual IfCondition GetCondition() const {
1388 return kCondGE;
1389 }
1390
1391 private:
1392 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
1393};
1394
1395
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001396// Instruction to check how two inputs compare to each other.
1397// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
1398class HCompare : public HBinaryOperation {
1399 public:
Calin Juravleddb7df22014-11-25 20:56:51 +00001400 // The bias applies for floating point operations and indicates how NaN
1401 // comparisons are treated:
1402 enum Bias {
1403 kNoBias, // bias is not applicable (i.e. for long operation)
1404 kGtBias, // return 1 for NaN comparisons
1405 kLtBias, // return -1 for NaN comparisons
1406 };
1407
1408 HCompare(Primitive::Type type, HInstruction* first, HInstruction* second, Bias bias)
1409 : HBinaryOperation(Primitive::kPrimInt, first, second), bias_(bias) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001410 DCHECK_EQ(type, first->GetType());
1411 DCHECK_EQ(type, second->GetType());
1412 }
1413
Calin Juravleddb7df22014-11-25 20:56:51 +00001414 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001415 return
1416 x == y ? 0 :
1417 x > y ? 1 :
1418 -1;
1419 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001420
1421 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001422 return
1423 x == y ? 0 :
1424 x > y ? 1 :
1425 -1;
1426 }
1427
Calin Juravleddb7df22014-11-25 20:56:51 +00001428 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
1429 return bias_ == other->AsCompare()->bias_;
1430 }
1431
1432 bool IsGtBias() { return bias_ == kGtBias; }
1433
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001434 DECLARE_INSTRUCTION(Compare);
1435
1436 private:
Calin Juravleddb7df22014-11-25 20:56:51 +00001437 const Bias bias_;
1438
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001439 DISALLOW_COPY_AND_ASSIGN(HCompare);
1440};
1441
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001442// A local in the graph. Corresponds to a Dex register.
1443class HLocal : public HTemplateInstruction<0> {
1444 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001445 explicit HLocal(uint16_t reg_number)
1446 : HTemplateInstruction(SideEffects::None()), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001447
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001448 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001449
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001450 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001451
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001452 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001453 // The Dex register number.
1454 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001455
1456 DISALLOW_COPY_AND_ASSIGN(HLocal);
1457};
1458
1459// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07001460class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001461 public:
Roland Levillain5799fc02014-09-25 12:15:20 +01001462 HLoadLocal(HLocal* local, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001463 : HExpression(type, SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001464 SetRawInputAt(0, local);
1465 }
1466
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001467 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1468
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001469 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001470
1471 private:
1472 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
1473};
1474
1475// Store a value in a given local. This instruction has two inputs: the value
1476// and the local.
1477class HStoreLocal : public HTemplateInstruction<2> {
1478 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001479 HStoreLocal(HLocal* local, HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001480 SetRawInputAt(0, local);
1481 SetRawInputAt(1, value);
1482 }
1483
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001484 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1485
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001486 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001487
1488 private:
1489 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
1490};
1491
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001492class HConstant : public HExpression<0> {
1493 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001494 explicit HConstant(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
1495
1496 virtual bool CanBeMoved() const { return true; }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001497
1498 DECLARE_INSTRUCTION(Constant);
1499
1500 private:
1501 DISALLOW_COPY_AND_ASSIGN(HConstant);
1502};
1503
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001504class HFloatConstant : public HConstant {
1505 public:
1506 explicit HFloatConstant(float value) : HConstant(Primitive::kPrimFloat), value_(value) {}
1507
1508 float GetValue() const { return value_; }
1509
1510 virtual bool InstructionDataEquals(HInstruction* other) const {
1511 return bit_cast<float, int32_t>(other->AsFloatConstant()->value_) ==
1512 bit_cast<float, int32_t>(value_);
1513 }
1514
1515 virtual size_t ComputeHashCode() const { return static_cast<size_t>(GetValue()); }
1516
1517 DECLARE_INSTRUCTION(FloatConstant);
1518
1519 private:
1520 const float value_;
1521
1522 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
1523};
1524
1525class HDoubleConstant : public HConstant {
1526 public:
1527 explicit HDoubleConstant(double value) : HConstant(Primitive::kPrimDouble), value_(value) {}
1528
1529 double GetValue() const { return value_; }
1530
1531 virtual bool InstructionDataEquals(HInstruction* other) const {
1532 return bit_cast<double, int64_t>(other->AsDoubleConstant()->value_) ==
1533 bit_cast<double, int64_t>(value_);
1534 }
1535
1536 virtual size_t ComputeHashCode() const { return static_cast<size_t>(GetValue()); }
1537
1538 DECLARE_INSTRUCTION(DoubleConstant);
1539
1540 private:
1541 const double value_;
1542
1543 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
1544};
1545
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001546// Constants of the type int. Those can be from Dex instructions, or
1547// synthesized (for example with the if-eqz instruction).
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001548class HIntConstant : public HConstant {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001549 public:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001550 explicit HIntConstant(int32_t value) : HConstant(Primitive::kPrimInt), value_(value) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001551
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001552 int32_t GetValue() const { return value_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001553
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001554 virtual bool InstructionDataEquals(HInstruction* other) const {
1555 return other->AsIntConstant()->value_ == value_;
1556 }
1557
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001558 virtual size_t ComputeHashCode() const { return GetValue(); }
1559
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001560 DECLARE_INSTRUCTION(IntConstant);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001561
1562 private:
1563 const int32_t value_;
1564
1565 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
1566};
1567
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001568class HLongConstant : public HConstant {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001569 public:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001570 explicit HLongConstant(int64_t value) : HConstant(Primitive::kPrimLong), value_(value) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001571
1572 int64_t GetValue() const { return value_; }
1573
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001574 virtual bool InstructionDataEquals(HInstruction* other) const {
1575 return other->AsLongConstant()->value_ == value_;
1576 }
1577
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001578 virtual size_t ComputeHashCode() const { return static_cast<size_t>(GetValue()); }
1579
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001580 DECLARE_INSTRUCTION(LongConstant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001581
1582 private:
1583 const int64_t value_;
1584
1585 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
1586};
1587
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001588enum class Intrinsics {
1589#define OPTIMIZING_INTRINSICS(Name, IsStatic) k ## Name,
1590#include "intrinsics_list.h"
1591 kNone,
1592 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
1593#undef INTRINSICS_LIST
1594#undef OPTIMIZING_INTRINSICS
1595};
1596std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
1597
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001598class HInvoke : public HInstruction {
1599 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001600 virtual size_t InputCount() const { return inputs_.Size(); }
1601 virtual HInstruction* InputAt(size_t i) const { return inputs_.Get(i); }
1602
1603 // Runtime needs to walk the stack, so Dex -> Dex calls need to
1604 // know their environment.
Calin Juravle77520bc2015-01-12 18:45:46 +00001605 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001606
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001607 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001608 SetRawInputAt(index, argument);
1609 }
1610
1611 virtual void SetRawInputAt(size_t index, HInstruction* input) {
1612 inputs_.Put(index, input);
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001613 }
1614
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001615 virtual Primitive::Type GetType() const { return return_type_; }
1616
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001617 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001618
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001619 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
1620
1621 Intrinsics GetIntrinsic() {
1622 return intrinsic_;
1623 }
1624
1625 void SetIntrinsic(Intrinsics intrinsic) {
1626 intrinsic_ = intrinsic;
1627 }
1628
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001629 DECLARE_INSTRUCTION(Invoke);
1630
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001631 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001632 HInvoke(ArenaAllocator* arena,
1633 uint32_t number_of_arguments,
1634 Primitive::Type return_type,
1635 uint32_t dex_pc,
1636 uint32_t dex_method_index)
1637 : HInstruction(SideEffects::All()),
1638 inputs_(arena, number_of_arguments),
1639 return_type_(return_type),
1640 dex_pc_(dex_pc),
1641 dex_method_index_(dex_method_index),
1642 intrinsic_(Intrinsics::kNone) {
1643 inputs_.SetSize(number_of_arguments);
1644 }
1645
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001646 GrowableArray<HInstruction*> inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001647 const Primitive::Type return_type_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001648 const uint32_t dex_pc_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001649 const uint32_t dex_method_index_;
1650 Intrinsics intrinsic_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001651
1652 private:
1653 DISALLOW_COPY_AND_ASSIGN(HInvoke);
1654};
1655
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001656class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001657 public:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001658 HInvokeStaticOrDirect(ArenaAllocator* arena,
1659 uint32_t number_of_arguments,
1660 Primitive::Type return_type,
1661 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001662 uint32_t dex_method_index,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001663 InvokeType invoke_type)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001664 : HInvoke(arena, number_of_arguments, return_type, dex_pc, dex_method_index),
Nicolas Geoffray4e44c822014-12-17 12:25:12 +00001665 invoke_type_(invoke_type) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001666
Calin Juravle77520bc2015-01-12 18:45:46 +00001667 bool CanDoImplicitNullCheck() const OVERRIDE {
1668 // We access the method via the dex cache so we can't do an implicit null check.
1669 // TODO: for intrinsics we can generate implicit null checks.
1670 return false;
1671 }
1672
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001673 InvokeType GetInvokeType() const { return invoke_type_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001674
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001675 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001676
1677 private:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001678 const InvokeType invoke_type_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001679
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001680 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001681};
1682
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001683class HInvokeVirtual : public HInvoke {
1684 public:
1685 HInvokeVirtual(ArenaAllocator* arena,
1686 uint32_t number_of_arguments,
1687 Primitive::Type return_type,
1688 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001689 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001690 uint32_t vtable_index)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001691 : HInvoke(arena, number_of_arguments, return_type, dex_pc, dex_method_index),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001692 vtable_index_(vtable_index) {}
1693
Calin Juravle77520bc2015-01-12 18:45:46 +00001694 bool CanDoImplicitNullCheck() const OVERRIDE {
1695 // TODO: Add implicit null checks in intrinsics.
1696 return !GetLocations()->Intrinsified();
1697 }
1698
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001699 uint32_t GetVTableIndex() const { return vtable_index_; }
1700
1701 DECLARE_INSTRUCTION(InvokeVirtual);
1702
1703 private:
1704 const uint32_t vtable_index_;
1705
1706 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
1707};
1708
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001709class HInvokeInterface : public HInvoke {
1710 public:
1711 HInvokeInterface(ArenaAllocator* arena,
1712 uint32_t number_of_arguments,
1713 Primitive::Type return_type,
1714 uint32_t dex_pc,
1715 uint32_t dex_method_index,
1716 uint32_t imt_index)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001717 : HInvoke(arena, number_of_arguments, return_type, dex_pc, dex_method_index),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001718 imt_index_(imt_index) {}
1719
Calin Juravle77520bc2015-01-12 18:45:46 +00001720 bool CanDoImplicitNullCheck() const OVERRIDE {
1721 // TODO: Add implicit null checks in intrinsics.
1722 return !GetLocations()->Intrinsified();
1723 }
1724
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001725 uint32_t GetImtIndex() const { return imt_index_; }
1726 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
1727
1728 DECLARE_INSTRUCTION(InvokeInterface);
1729
1730 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001731 const uint32_t imt_index_;
1732
1733 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
1734};
1735
Dave Allison20dfc792014-06-16 20:44:29 -07001736class HNewInstance : public HExpression<0> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001737 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001738 HNewInstance(uint32_t dex_pc, uint16_t type_index)
1739 : HExpression(Primitive::kPrimNot, SideEffects::None()),
1740 dex_pc_(dex_pc),
1741 type_index_(type_index) {}
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001742
1743 uint32_t GetDexPc() const { return dex_pc_; }
1744 uint16_t GetTypeIndex() const { return type_index_; }
1745
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001746 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00001747 bool NeedsEnvironment() const OVERRIDE { return true; }
1748 // It may throw when called on:
1749 // - interfaces
1750 // - abstract/innaccessible/unknown classes
1751 // TODO: optimize when possible.
1752 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001753
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001754 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001755
1756 private:
1757 const uint32_t dex_pc_;
1758 const uint16_t type_index_;
1759
1760 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
1761};
1762
Roland Levillain88cb1752014-10-20 16:36:47 +01001763class HNeg : public HUnaryOperation {
1764 public:
1765 explicit HNeg(Primitive::Type result_type, HInstruction* input)
1766 : HUnaryOperation(result_type, input) {}
1767
Roland Levillain9240d6a2014-10-20 16:47:04 +01001768 virtual int32_t Evaluate(int32_t x) const OVERRIDE { return -x; }
1769 virtual int64_t Evaluate(int64_t x) const OVERRIDE { return -x; }
1770
Roland Levillain88cb1752014-10-20 16:36:47 +01001771 DECLARE_INSTRUCTION(Neg);
1772
1773 private:
1774 DISALLOW_COPY_AND_ASSIGN(HNeg);
1775};
1776
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001777class HNewArray : public HExpression<1> {
1778 public:
1779 HNewArray(HInstruction* length, uint32_t dex_pc, uint16_t type_index)
1780 : HExpression(Primitive::kPrimNot, SideEffects::None()),
1781 dex_pc_(dex_pc),
1782 type_index_(type_index) {
1783 SetRawInputAt(0, length);
1784 }
1785
1786 uint32_t GetDexPc() const { return dex_pc_; }
1787 uint16_t GetTypeIndex() const { return type_index_; }
1788
1789 // Calls runtime so needs an environment.
1790 virtual bool NeedsEnvironment() const { return true; }
1791
1792 DECLARE_INSTRUCTION(NewArray);
1793
1794 private:
1795 const uint32_t dex_pc_;
1796 const uint16_t type_index_;
1797
1798 DISALLOW_COPY_AND_ASSIGN(HNewArray);
1799};
1800
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001801class HAdd : public HBinaryOperation {
1802 public:
1803 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1804 : HBinaryOperation(result_type, left, right) {}
1805
1806 virtual bool IsCommutative() { return true; }
1807
Roland Levillain93445682014-10-06 19:24:02 +01001808 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1809 return x + y;
1810 }
1811 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1812 return x + y;
1813 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001814
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001815 DECLARE_INSTRUCTION(Add);
1816
1817 private:
1818 DISALLOW_COPY_AND_ASSIGN(HAdd);
1819};
1820
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001821class HSub : public HBinaryOperation {
1822 public:
1823 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1824 : HBinaryOperation(result_type, left, right) {}
1825
Roland Levillain93445682014-10-06 19:24:02 +01001826 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1827 return x - y;
1828 }
1829 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1830 return x - y;
1831 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001832
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001833 DECLARE_INSTRUCTION(Sub);
1834
1835 private:
1836 DISALLOW_COPY_AND_ASSIGN(HSub);
1837};
1838
Calin Juravle34bacdf2014-10-07 20:23:36 +01001839class HMul : public HBinaryOperation {
1840 public:
1841 HMul(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1842 : HBinaryOperation(result_type, left, right) {}
1843
1844 virtual bool IsCommutative() { return true; }
1845
1846 virtual int32_t Evaluate(int32_t x, int32_t y) const { return x * y; }
1847 virtual int64_t Evaluate(int64_t x, int64_t y) const { return x * y; }
1848
1849 DECLARE_INSTRUCTION(Mul);
1850
1851 private:
1852 DISALLOW_COPY_AND_ASSIGN(HMul);
1853};
1854
Calin Juravle7c4954d2014-10-28 16:57:40 +00001855class HDiv : public HBinaryOperation {
1856 public:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001857 HDiv(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
1858 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00001859
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00001860 virtual int32_t Evaluate(int32_t x, int32_t y) const {
1861 // Our graph structure ensures we never have 0 for `y` during constant folding.
1862 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00001863 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00001864 return (y == -1) ? -x : x / y;
1865 }
Calin Juravlebacfec32014-11-14 15:54:36 +00001866
1867 virtual int64_t Evaluate(int64_t x, int64_t y) const {
1868 DCHECK_NE(y, 0);
1869 // Special case -1 to avoid getting a SIGFPE on x86(_64).
1870 return (y == -1) ? -x : x / y;
1871 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00001872
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001873 uint32_t GetDexPc() const { return dex_pc_; }
1874
Calin Juravle7c4954d2014-10-28 16:57:40 +00001875 DECLARE_INSTRUCTION(Div);
1876
1877 private:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001878 const uint32_t dex_pc_;
1879
Calin Juravle7c4954d2014-10-28 16:57:40 +00001880 DISALLOW_COPY_AND_ASSIGN(HDiv);
1881};
1882
Calin Juravlebacfec32014-11-14 15:54:36 +00001883class HRem : public HBinaryOperation {
1884 public:
1885 HRem(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
1886 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
1887
1888 virtual int32_t Evaluate(int32_t x, int32_t y) const {
1889 DCHECK_NE(y, 0);
1890 // Special case -1 to avoid getting a SIGFPE on x86(_64).
1891 return (y == -1) ? 0 : x % y;
1892 }
1893
1894 virtual int64_t Evaluate(int64_t x, int64_t y) const {
1895 DCHECK_NE(y, 0);
1896 // Special case -1 to avoid getting a SIGFPE on x86(_64).
1897 return (y == -1) ? 0 : x % y;
1898 }
1899
1900 uint32_t GetDexPc() const { return dex_pc_; }
1901
1902 DECLARE_INSTRUCTION(Rem);
1903
1904 private:
1905 const uint32_t dex_pc_;
1906
1907 DISALLOW_COPY_AND_ASSIGN(HRem);
1908};
1909
Calin Juravled0d48522014-11-04 16:40:20 +00001910class HDivZeroCheck : public HExpression<1> {
1911 public:
1912 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
1913 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
1914 SetRawInputAt(0, value);
1915 }
1916
1917 bool CanBeMoved() const OVERRIDE { return true; }
1918
1919 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
1920 UNUSED(other);
1921 return true;
1922 }
1923
1924 bool NeedsEnvironment() const OVERRIDE { return true; }
1925 bool CanThrow() const OVERRIDE { return true; }
1926
1927 uint32_t GetDexPc() const { return dex_pc_; }
1928
1929 DECLARE_INSTRUCTION(DivZeroCheck);
1930
1931 private:
1932 const uint32_t dex_pc_;
1933
1934 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
1935};
1936
Calin Juravle9aec02f2014-11-18 23:06:35 +00001937class HShl : public HBinaryOperation {
1938 public:
1939 HShl(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1940 : HBinaryOperation(result_type, left, right) {}
1941
1942 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x << (y & kMaxIntShiftValue); }
1943 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x << (y & kMaxLongShiftValue); }
1944
1945 DECLARE_INSTRUCTION(Shl);
1946
1947 private:
1948 DISALLOW_COPY_AND_ASSIGN(HShl);
1949};
1950
1951class HShr : public HBinaryOperation {
1952 public:
1953 HShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1954 : HBinaryOperation(result_type, left, right) {}
1955
1956 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x >> (y & kMaxIntShiftValue); }
1957 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x >> (y & kMaxLongShiftValue); }
1958
1959 DECLARE_INSTRUCTION(Shr);
1960
1961 private:
1962 DISALLOW_COPY_AND_ASSIGN(HShr);
1963};
1964
1965class HUShr : public HBinaryOperation {
1966 public:
1967 HUShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1968 : HBinaryOperation(result_type, left, right) {}
1969
1970 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1971 uint32_t ux = static_cast<uint32_t>(x);
1972 uint32_t uy = static_cast<uint32_t>(y) & kMaxIntShiftValue;
1973 return static_cast<int32_t>(ux >> uy);
1974 }
1975
1976 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1977 uint64_t ux = static_cast<uint64_t>(x);
1978 uint64_t uy = static_cast<uint64_t>(y) & kMaxLongShiftValue;
1979 return static_cast<int64_t>(ux >> uy);
1980 }
1981
1982 DECLARE_INSTRUCTION(UShr);
1983
1984 private:
1985 DISALLOW_COPY_AND_ASSIGN(HUShr);
1986};
1987
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001988class HAnd : public HBinaryOperation {
1989 public:
1990 HAnd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1991 : HBinaryOperation(result_type, left, right) {}
1992
1993 bool IsCommutative() OVERRIDE { return true; }
1994
1995 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x & y; }
1996 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x & y; }
1997
1998 DECLARE_INSTRUCTION(And);
1999
2000 private:
2001 DISALLOW_COPY_AND_ASSIGN(HAnd);
2002};
2003
2004class HOr : public HBinaryOperation {
2005 public:
2006 HOr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2007 : HBinaryOperation(result_type, left, right) {}
2008
2009 bool IsCommutative() OVERRIDE { return true; }
2010
2011 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x | y; }
2012 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x | y; }
2013
2014 DECLARE_INSTRUCTION(Or);
2015
2016 private:
2017 DISALLOW_COPY_AND_ASSIGN(HOr);
2018};
2019
2020class HXor : public HBinaryOperation {
2021 public:
2022 HXor(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2023 : HBinaryOperation(result_type, left, right) {}
2024
2025 bool IsCommutative() OVERRIDE { return true; }
2026
2027 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x ^ y; }
2028 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x ^ y; }
2029
2030 DECLARE_INSTRUCTION(Xor);
2031
2032 private:
2033 DISALLOW_COPY_AND_ASSIGN(HXor);
2034};
2035
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002036// The value of a parameter in this method. Its location depends on
2037// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07002038class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002039 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002040 HParameterValue(uint8_t index, Primitive::Type parameter_type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002041 : HExpression(parameter_type, SideEffects::None()), index_(index) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002042
2043 uint8_t GetIndex() const { return index_; }
2044
2045 DECLARE_INSTRUCTION(ParameterValue);
2046
2047 private:
2048 // The index of this parameter in the parameters list. Must be less
2049 // than HGraph::number_of_in_vregs_;
2050 const uint8_t index_;
2051
2052 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
2053};
2054
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002055class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002056 public:
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002057 explicit HNot(Primitive::Type result_type, HInstruction* input)
2058 : HUnaryOperation(result_type, input) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002059
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002060 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002061 virtual bool InstructionDataEquals(HInstruction* other) const {
2062 UNUSED(other);
2063 return true;
2064 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002065
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002066 virtual int32_t Evaluate(int32_t x) const OVERRIDE { return ~x; }
2067 virtual int64_t Evaluate(int64_t x) const OVERRIDE { return ~x; }
2068
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002069 DECLARE_INSTRUCTION(Not);
2070
2071 private:
2072 DISALLOW_COPY_AND_ASSIGN(HNot);
2073};
2074
Roland Levillaindff1f282014-11-05 14:15:05 +00002075class HTypeConversion : public HExpression<1> {
2076 public:
2077 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00002078 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
2079 : HExpression(result_type, SideEffects::None()), dex_pc_(dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002080 SetRawInputAt(0, input);
2081 DCHECK_NE(input->GetType(), result_type);
2082 }
2083
2084 HInstruction* GetInput() const { return InputAt(0); }
2085 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
2086 Primitive::Type GetResultType() const { return GetType(); }
2087
Roland Levillain624279f2014-12-04 11:54:28 +00002088 // Required by the x86 and ARM code generators when producing calls
2089 // to the runtime.
2090 uint32_t GetDexPc() const { return dex_pc_; }
2091
Roland Levillaindff1f282014-11-05 14:15:05 +00002092 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00002093 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00002094
2095 DECLARE_INSTRUCTION(TypeConversion);
2096
2097 private:
Roland Levillain624279f2014-12-04 11:54:28 +00002098 const uint32_t dex_pc_;
2099
Roland Levillaindff1f282014-11-05 14:15:05 +00002100 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
2101};
2102
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002103class HPhi : public HInstruction {
2104 public:
2105 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002106 : HInstruction(SideEffects::None()),
2107 inputs_(arena, number_of_inputs),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002108 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002109 type_(type),
2110 is_live_(false) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002111 inputs_.SetSize(number_of_inputs);
2112 }
2113
2114 virtual size_t InputCount() const { return inputs_.Size(); }
2115 virtual HInstruction* InputAt(size_t i) const { return inputs_.Get(i); }
2116
2117 virtual void SetRawInputAt(size_t index, HInstruction* input) {
2118 inputs_.Put(index, input);
2119 }
2120
2121 void AddInput(HInstruction* input);
2122
2123 virtual Primitive::Type GetType() const { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002124 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002125
2126 uint32_t GetRegNumber() const { return reg_number_; }
2127
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002128 void SetDead() { is_live_ = false; }
2129 void SetLive() { is_live_ = true; }
2130 bool IsDead() const { return !is_live_; }
2131 bool IsLive() const { return is_live_; }
2132
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002133 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002134
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002135 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002136 GrowableArray<HInstruction*> inputs_;
2137 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002138 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002139 bool is_live_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002140
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002141 DISALLOW_COPY_AND_ASSIGN(HPhi);
2142};
2143
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002144class HNullCheck : public HExpression<1> {
2145 public:
2146 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002147 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002148 SetRawInputAt(0, value);
2149 }
2150
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002151 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002152 virtual bool InstructionDataEquals(HInstruction* other) const {
2153 UNUSED(other);
2154 return true;
2155 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002156
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002157 virtual bool NeedsEnvironment() const { return true; }
2158
Roland Levillaine161a2a2014-10-03 12:45:18 +01002159 virtual bool CanThrow() const { return true; }
2160
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002161 uint32_t GetDexPc() const { return dex_pc_; }
2162
2163 DECLARE_INSTRUCTION(NullCheck);
2164
2165 private:
2166 const uint32_t dex_pc_;
2167
2168 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
2169};
2170
2171class FieldInfo : public ValueObject {
2172 public:
Calin Juravle52c48962014-12-16 17:02:57 +00002173 FieldInfo(MemberOffset field_offset, Primitive::Type field_type, bool is_volatile)
2174 : field_offset_(field_offset), field_type_(field_type), is_volatile_(is_volatile) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002175
2176 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002177 Primitive::Type GetFieldType() const { return field_type_; }
Calin Juravle52c48962014-12-16 17:02:57 +00002178 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002179
2180 private:
2181 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01002182 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00002183 const bool is_volatile_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002184};
2185
2186class HInstanceFieldGet : public HExpression<1> {
2187 public:
2188 HInstanceFieldGet(HInstruction* value,
2189 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00002190 MemberOffset field_offset,
2191 bool is_volatile)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002192 : HExpression(field_type, SideEffects::DependsOnSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00002193 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002194 SetRawInputAt(0, value);
2195 }
2196
Calin Juravle10c9cbe2014-12-19 10:50:19 +00002197 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002198
2199 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2200 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
2201 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002202 }
2203
Calin Juravle77520bc2015-01-12 18:45:46 +00002204 bool CanDoImplicitNullCheck() const OVERRIDE {
2205 return GetFieldOffset().Uint32Value() < kPageSize;
2206 }
2207
2208 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002209 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
2210 }
2211
Calin Juravle52c48962014-12-16 17:02:57 +00002212 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002213 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002214 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002215 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002216
2217 DECLARE_INSTRUCTION(InstanceFieldGet);
2218
2219 private:
2220 const FieldInfo field_info_;
2221
2222 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
2223};
2224
2225class HInstanceFieldSet : public HTemplateInstruction<2> {
2226 public:
2227 HInstanceFieldSet(HInstruction* object,
2228 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01002229 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00002230 MemberOffset field_offset,
2231 bool is_volatile)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002232 : HTemplateInstruction(SideEffects::ChangesSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00002233 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002234 SetRawInputAt(0, object);
2235 SetRawInputAt(1, value);
2236 }
2237
Calin Juravle77520bc2015-01-12 18:45:46 +00002238 bool CanDoImplicitNullCheck() const OVERRIDE {
2239 return GetFieldOffset().Uint32Value() < kPageSize;
2240 }
2241
Calin Juravle52c48962014-12-16 17:02:57 +00002242 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002243 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002244 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002245 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002246 HInstruction* GetValue() const { return InputAt(1); }
2247
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002248 DECLARE_INSTRUCTION(InstanceFieldSet);
2249
2250 private:
2251 const FieldInfo field_info_;
2252
2253 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
2254};
2255
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002256class HArrayGet : public HExpression<2> {
2257 public:
2258 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002259 : HExpression(type, SideEffects::DependsOnSomething()) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002260 SetRawInputAt(0, array);
2261 SetRawInputAt(1, index);
2262 }
2263
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002264 bool CanBeMoved() const OVERRIDE { return true; }
2265 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002266 UNUSED(other);
2267 return true;
2268 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002269 bool CanDoImplicitNullCheck() const OVERRIDE {
2270 // TODO: We can be smarter here.
2271 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
2272 // which generates the implicit null check. There are cases when these can be removed
2273 // to produce better code. If we ever add optimizations to do so we should allow an
2274 // implicit check here (as long as the address falls in the first page).
2275 return false;
2276 }
2277
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002278 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002279
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002280 HInstruction* GetArray() const { return InputAt(0); }
2281 HInstruction* GetIndex() const { return InputAt(1); }
2282
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002283 DECLARE_INSTRUCTION(ArrayGet);
2284
2285 private:
2286 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
2287};
2288
2289class HArraySet : public HTemplateInstruction<3> {
2290 public:
2291 HArraySet(HInstruction* array,
2292 HInstruction* index,
2293 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002294 Primitive::Type expected_component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002295 uint32_t dex_pc)
2296 : HTemplateInstruction(SideEffects::ChangesSomething()),
2297 dex_pc_(dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002298 expected_component_type_(expected_component_type),
2299 needs_type_check_(value->GetType() == Primitive::kPrimNot) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002300 SetRawInputAt(0, array);
2301 SetRawInputAt(1, index);
2302 SetRawInputAt(2, value);
2303 }
2304
Calin Juravle77520bc2015-01-12 18:45:46 +00002305 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002306 // We currently always call a runtime method to catch array store
2307 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002308 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002309 }
2310
Calin Juravle77520bc2015-01-12 18:45:46 +00002311 bool CanDoImplicitNullCheck() const OVERRIDE {
2312 // TODO: Same as for ArrayGet.
2313 return false;
2314 }
2315
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002316 void ClearNeedsTypeCheck() {
2317 needs_type_check_ = false;
2318 }
2319
2320 bool NeedsTypeCheck() const { return needs_type_check_; }
2321
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002322 uint32_t GetDexPc() const { return dex_pc_; }
2323
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002324 HInstruction* GetArray() const { return InputAt(0); }
2325 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002326 HInstruction* GetValue() const { return InputAt(2); }
2327
2328 Primitive::Type GetComponentType() const {
2329 // The Dex format does not type floating point index operations. Since the
2330 // `expected_component_type_` is set during building and can therefore not
2331 // be correct, we also check what is the value type. If it is a floating
2332 // point type, we must use that type.
2333 Primitive::Type value_type = GetValue()->GetType();
2334 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
2335 ? value_type
2336 : expected_component_type_;
2337 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002338
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002339 DECLARE_INSTRUCTION(ArraySet);
2340
2341 private:
2342 const uint32_t dex_pc_;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002343 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002344 bool needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002345
2346 DISALLOW_COPY_AND_ASSIGN(HArraySet);
2347};
2348
2349class HArrayLength : public HExpression<1> {
2350 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002351 explicit HArrayLength(HInstruction* array)
2352 : HExpression(Primitive::kPrimInt, SideEffects::None()) {
2353 // Note that arrays do not change length, so the instruction does not
2354 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002355 SetRawInputAt(0, array);
2356 }
2357
Calin Juravle77520bc2015-01-12 18:45:46 +00002358 bool CanBeMoved() const OVERRIDE { return true; }
2359 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002360 UNUSED(other);
2361 return true;
2362 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002363 bool CanDoImplicitNullCheck() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002364
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002365 DECLARE_INSTRUCTION(ArrayLength);
2366
2367 private:
2368 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
2369};
2370
2371class HBoundsCheck : public HExpression<2> {
2372 public:
2373 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002374 : HExpression(index->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002375 DCHECK(index->GetType() == Primitive::kPrimInt);
2376 SetRawInputAt(0, index);
2377 SetRawInputAt(1, length);
2378 }
2379
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002380 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002381 virtual bool InstructionDataEquals(HInstruction* other) const {
2382 UNUSED(other);
2383 return true;
2384 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002385
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002386 virtual bool NeedsEnvironment() const { return true; }
2387
Roland Levillaine161a2a2014-10-03 12:45:18 +01002388 virtual bool CanThrow() const { return true; }
2389
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002390 uint32_t GetDexPc() const { return dex_pc_; }
2391
2392 DECLARE_INSTRUCTION(BoundsCheck);
2393
2394 private:
2395 const uint32_t dex_pc_;
2396
2397 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
2398};
2399
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002400/**
2401 * Some DEX instructions are folded into multiple HInstructions that need
2402 * to stay live until the last HInstruction. This class
2403 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00002404 * HInstruction stays live. `index` represents the stack location index of the
2405 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002406 */
2407class HTemporary : public HTemplateInstruction<0> {
2408 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002409 explicit HTemporary(size_t index) : HTemplateInstruction(SideEffects::None()), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002410
2411 size_t GetIndex() const { return index_; }
2412
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00002413 Primitive::Type GetType() const OVERRIDE {
2414 // The previous instruction is the one that will be stored in the temporary location.
2415 DCHECK(GetPrevious() != nullptr);
2416 return GetPrevious()->GetType();
2417 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00002418
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002419 DECLARE_INSTRUCTION(Temporary);
2420
2421 private:
2422 const size_t index_;
2423
2424 DISALLOW_COPY_AND_ASSIGN(HTemporary);
2425};
2426
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002427class HSuspendCheck : public HTemplateInstruction<0> {
2428 public:
2429 explicit HSuspendCheck(uint32_t dex_pc)
Nicolas Geoffray9ebc72c2014-09-25 16:33:42 +01002430 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002431
2432 virtual bool NeedsEnvironment() const {
2433 return true;
2434 }
2435
2436 uint32_t GetDexPc() const { return dex_pc_; }
2437
2438 DECLARE_INSTRUCTION(SuspendCheck);
2439
2440 private:
2441 const uint32_t dex_pc_;
2442
2443 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
2444};
2445
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002446/**
2447 * Instruction to load a Class object.
2448 */
2449class HLoadClass : public HExpression<0> {
2450 public:
2451 HLoadClass(uint16_t type_index,
2452 bool is_referrers_class,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002453 uint32_t dex_pc)
2454 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2455 type_index_(type_index),
2456 is_referrers_class_(is_referrers_class),
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002457 dex_pc_(dex_pc),
2458 generate_clinit_check_(false) {}
2459
2460 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002461
2462 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2463 return other->AsLoadClass()->type_index_ == type_index_;
2464 }
2465
2466 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
2467
2468 uint32_t GetDexPc() const { return dex_pc_; }
2469 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002470 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002471
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002472 bool NeedsEnvironment() const OVERRIDE {
2473 // Will call runtime and load the class if the class is not loaded yet.
2474 // TODO: finer grain decision.
2475 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002476 }
2477
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002478 bool MustGenerateClinitCheck() const {
2479 return generate_clinit_check_;
2480 }
2481
2482 void SetMustGenerateClinitCheck() {
2483 generate_clinit_check_ = true;
2484 }
2485
2486 bool CanCallRuntime() const {
2487 return MustGenerateClinitCheck() || !is_referrers_class_;
2488 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002489
2490 DECLARE_INSTRUCTION(LoadClass);
2491
2492 private:
2493 const uint16_t type_index_;
2494 const bool is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002495 const uint32_t dex_pc_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002496 // Whether this instruction must generate the initialization check.
2497 // Used for code generation.
2498 bool generate_clinit_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002499
2500 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
2501};
2502
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002503class HLoadString : public HExpression<0> {
2504 public:
2505 HLoadString(uint32_t string_index, uint32_t dex_pc)
2506 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2507 string_index_(string_index),
2508 dex_pc_(dex_pc) {}
2509
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002510 bool CanBeMoved() const OVERRIDE { return true; }
2511
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002512 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2513 return other->AsLoadString()->string_index_ == string_index_;
2514 }
2515
2516 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
2517
2518 uint32_t GetDexPc() const { return dex_pc_; }
2519 uint32_t GetStringIndex() const { return string_index_; }
2520
2521 // TODO: Can we deopt or debug when we resolve a string?
2522 bool NeedsEnvironment() const OVERRIDE { return false; }
2523
2524 DECLARE_INSTRUCTION(LoadString);
2525
2526 private:
2527 const uint32_t string_index_;
2528 const uint32_t dex_pc_;
2529
2530 DISALLOW_COPY_AND_ASSIGN(HLoadString);
2531};
2532
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002533// TODO: Pass this check to HInvokeStaticOrDirect nodes.
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002534/**
2535 * Performs an initialization check on its Class object input.
2536 */
2537class HClinitCheck : public HExpression<1> {
2538 public:
2539 explicit HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
2540 : HExpression(Primitive::kPrimNot, SideEffects::All()),
2541 dex_pc_(dex_pc) {
2542 SetRawInputAt(0, constant);
2543 }
2544
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002545 bool CanBeMoved() const OVERRIDE { return true; }
2546 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2547 UNUSED(other);
2548 return true;
2549 }
2550
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002551 bool NeedsEnvironment() const OVERRIDE {
2552 // May call runtime to initialize the class.
2553 return true;
2554 }
2555
2556 uint32_t GetDexPc() const { return dex_pc_; }
2557
2558 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
2559
2560 DECLARE_INSTRUCTION(ClinitCheck);
2561
2562 private:
2563 const uint32_t dex_pc_;
2564
2565 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
2566};
2567
2568class HStaticFieldGet : public HExpression<1> {
2569 public:
2570 HStaticFieldGet(HInstruction* cls,
2571 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00002572 MemberOffset field_offset,
2573 bool is_volatile)
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002574 : HExpression(field_type, SideEffects::DependsOnSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00002575 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002576 SetRawInputAt(0, cls);
2577 }
2578
Calin Juravle52c48962014-12-16 17:02:57 +00002579
Calin Juravle10c9cbe2014-12-19 10:50:19 +00002580 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002581
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002582 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00002583 HStaticFieldGet* other_get = other->AsStaticFieldGet();
2584 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002585 }
2586
2587 size_t ComputeHashCode() const OVERRIDE {
2588 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
2589 }
2590
Calin Juravle52c48962014-12-16 17:02:57 +00002591 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002592 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
2593 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002594 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002595
2596 DECLARE_INSTRUCTION(StaticFieldGet);
2597
2598 private:
2599 const FieldInfo field_info_;
2600
2601 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
2602};
2603
2604class HStaticFieldSet : public HTemplateInstruction<2> {
2605 public:
2606 HStaticFieldSet(HInstruction* cls,
2607 HInstruction* value,
2608 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00002609 MemberOffset field_offset,
2610 bool is_volatile)
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002611 : HTemplateInstruction(SideEffects::ChangesSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00002612 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002613 SetRawInputAt(0, cls);
2614 SetRawInputAt(1, value);
2615 }
2616
Calin Juravle52c48962014-12-16 17:02:57 +00002617 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002618 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
2619 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002620 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002621
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002622 HInstruction* GetValue() const { return InputAt(1); }
2623
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002624 DECLARE_INSTRUCTION(StaticFieldSet);
2625
2626 private:
2627 const FieldInfo field_info_;
2628
2629 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
2630};
2631
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002632// Implement the move-exception DEX instruction.
2633class HLoadException : public HExpression<0> {
2634 public:
2635 HLoadException() : HExpression(Primitive::kPrimNot, SideEffects::None()) {}
2636
2637 DECLARE_INSTRUCTION(LoadException);
2638
2639 private:
2640 DISALLOW_COPY_AND_ASSIGN(HLoadException);
2641};
2642
2643class HThrow : public HTemplateInstruction<1> {
2644 public:
2645 HThrow(HInstruction* exception, uint32_t dex_pc)
2646 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {
2647 SetRawInputAt(0, exception);
2648 }
2649
2650 bool IsControlFlow() const OVERRIDE { return true; }
2651
2652 bool NeedsEnvironment() const OVERRIDE { return true; }
2653
2654 uint32_t GetDexPc() const { return dex_pc_; }
2655
2656 DECLARE_INSTRUCTION(Throw);
2657
2658 private:
2659 uint32_t dex_pc_;
2660
2661 DISALLOW_COPY_AND_ASSIGN(HThrow);
2662};
2663
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002664class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002665 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002666 HInstanceOf(HInstruction* object,
2667 HLoadClass* constant,
2668 bool class_is_final,
2669 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002670 : HExpression(Primitive::kPrimBoolean, SideEffects::None()),
2671 class_is_final_(class_is_final),
2672 dex_pc_(dex_pc) {
2673 SetRawInputAt(0, object);
2674 SetRawInputAt(1, constant);
2675 }
2676
2677 bool CanBeMoved() const OVERRIDE { return true; }
2678
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002679 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002680 return true;
2681 }
2682
2683 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002684 return false;
2685 }
2686
2687 uint32_t GetDexPc() const { return dex_pc_; }
2688
2689 bool IsClassFinal() const { return class_is_final_; }
2690
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002691 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002692
2693 private:
2694 const bool class_is_final_;
2695 const uint32_t dex_pc_;
2696
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002697 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
2698};
2699
2700class HCheckCast : public HTemplateInstruction<2> {
2701 public:
2702 HCheckCast(HInstruction* object,
2703 HLoadClass* constant,
2704 bool class_is_final,
2705 uint32_t dex_pc)
2706 : HTemplateInstruction(SideEffects::None()),
2707 class_is_final_(class_is_final),
2708 dex_pc_(dex_pc) {
2709 SetRawInputAt(0, object);
2710 SetRawInputAt(1, constant);
2711 }
2712
2713 bool CanBeMoved() const OVERRIDE { return true; }
2714
2715 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2716 return true;
2717 }
2718
2719 bool NeedsEnvironment() const OVERRIDE {
2720 // Instruction may throw a CheckCastError.
2721 return true;
2722 }
2723
2724 bool CanThrow() const OVERRIDE { return true; }
2725
2726 uint32_t GetDexPc() const { return dex_pc_; }
2727
2728 bool IsClassFinal() const { return class_is_final_; }
2729
2730 DECLARE_INSTRUCTION(CheckCast);
2731
2732 private:
2733 const bool class_is_final_;
2734 const uint32_t dex_pc_;
2735
2736 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002737};
2738
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00002739class HMonitorOperation : public HTemplateInstruction<1> {
2740 public:
2741 enum OperationKind {
2742 kEnter,
2743 kExit,
2744 };
2745
2746 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
2747 : HTemplateInstruction(SideEffects::None()), kind_(kind), dex_pc_(dex_pc) {
2748 SetRawInputAt(0, object);
2749 }
2750
2751 // Instruction may throw a Java exception, so we need an environment.
2752 bool NeedsEnvironment() const OVERRIDE { return true; }
2753 bool CanThrow() const OVERRIDE { return true; }
2754
2755 uint32_t GetDexPc() const { return dex_pc_; }
2756
2757 bool IsEnter() const { return kind_ == kEnter; }
2758
2759 DECLARE_INSTRUCTION(MonitorOperation);
2760
Calin Juravle52c48962014-12-16 17:02:57 +00002761 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00002762 const OperationKind kind_;
2763 const uint32_t dex_pc_;
2764
2765 private:
2766 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
2767};
2768
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002769class MoveOperands : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002770 public:
Nicolas Geoffray740475d2014-09-29 10:33:25 +01002771 MoveOperands(Location source, Location destination, HInstruction* instruction)
2772 : source_(source), destination_(destination), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002773
2774 Location GetSource() const { return source_; }
2775 Location GetDestination() const { return destination_; }
2776
2777 void SetSource(Location value) { source_ = value; }
2778 void SetDestination(Location value) { destination_ = value; }
2779
2780 // The parallel move resolver marks moves as "in-progress" by clearing the
2781 // destination (but not the source).
2782 Location MarkPending() {
2783 DCHECK(!IsPending());
2784 Location dest = destination_;
2785 destination_ = Location::NoLocation();
2786 return dest;
2787 }
2788
2789 void ClearPending(Location dest) {
2790 DCHECK(IsPending());
2791 destination_ = dest;
2792 }
2793
2794 bool IsPending() const {
2795 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
2796 return destination_.IsInvalid() && !source_.IsInvalid();
2797 }
2798
2799 // True if this blocks a move from the given location.
2800 bool Blocks(Location loc) const {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00002801 return !IsEliminated() && source_.Equals(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002802 }
2803
2804 // A move is redundant if it's been eliminated, if its source and
2805 // destination are the same, or if its destination is unneeded.
2806 bool IsRedundant() const {
2807 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
2808 }
2809
2810 // We clear both operands to indicate move that's been eliminated.
2811 void Eliminate() {
2812 source_ = destination_ = Location::NoLocation();
2813 }
2814
2815 bool IsEliminated() const {
2816 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
2817 return source_.IsInvalid();
2818 }
2819
Nicolas Geoffray740475d2014-09-29 10:33:25 +01002820 HInstruction* GetInstruction() const { return instruction_; }
2821
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002822 private:
2823 Location source_;
2824 Location destination_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01002825 // The instruction this move is assocatied with. Null when this move is
2826 // for moving an input in the expected locations of user (including a phi user).
2827 // This is only used in debug mode, to ensure we do not connect interval siblings
2828 // in the same parallel move.
2829 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002830};
2831
2832static constexpr size_t kDefaultNumberOfMoves = 4;
2833
2834class HParallelMove : public HTemplateInstruction<0> {
2835 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002836 explicit HParallelMove(ArenaAllocator* arena)
2837 : HTemplateInstruction(SideEffects::None()), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002838
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00002839 void AddMove(Location source, Location destination, HInstruction* instruction) {
2840 DCHECK(source.IsValid());
2841 DCHECK(destination.IsValid());
2842 // The parallel move resolver does not handle pairs. So we decompose the
2843 // pair locations into two moves.
2844 if (source.IsPair() && destination.IsPair()) {
2845 AddMove(source.ToLow(), destination.ToLow(), instruction);
2846 AddMove(source.ToHigh(), destination.ToHigh(), nullptr);
2847 } else if (source.IsPair()) {
Nicolas Geoffrayc399fdc2015-01-21 12:42:57 +00002848 DCHECK(destination.IsDoubleStackSlot());
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00002849 AddMove(source.ToLow(), Location::StackSlot(destination.GetStackIndex()), instruction);
2850 AddMove(source.ToHigh(), Location::StackSlot(destination.GetHighStackIndex(4)), nullptr);
2851 } else if (destination.IsPair()) {
Nicolas Geoffrayc399fdc2015-01-21 12:42:57 +00002852 DCHECK(source.IsDoubleStackSlot());
2853 AddMove(Location::StackSlot(source.GetStackIndex()), destination.ToLow(), instruction);
2854 // TODO: rewrite GetHighStackIndex to not require a word size. It's supposed to
2855 // always be 4.
2856 static constexpr int kHighOffset = 4;
2857 AddMove(Location::StackSlot(source.GetHighStackIndex(kHighOffset)),
2858 destination.ToHigh(),
2859 nullptr);
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00002860 } else {
2861 if (kIsDebugBuild) {
2862 if (instruction != nullptr) {
2863 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
2864 DCHECK_NE(moves_.Get(i).GetInstruction(), instruction)
2865 << "Doing parallel moves for the same instruction.";
2866 }
2867 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00002868 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00002869 DCHECK(!destination.Equals(moves_.Get(i).GetDestination()))
2870 << "Same destination for two moves in a parallel move.";
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00002871 }
2872 }
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00002873 moves_.Add(MoveOperands(source, destination, instruction));
Nicolas Geoffray740475d2014-09-29 10:33:25 +01002874 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002875 }
2876
2877 MoveOperands* MoveOperandsAt(size_t index) const {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00002878 return moves_.GetRawStorage() + index;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002879 }
2880
2881 size_t NumMoves() const { return moves_.Size(); }
2882
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002883 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002884
2885 private:
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00002886 GrowableArray<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002887
2888 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
2889};
2890
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002891class HGraphVisitor : public ValueObject {
2892 public:
Dave Allison20dfc792014-06-16 20:44:29 -07002893 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
2894 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002895
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002896 virtual void VisitInstruction(HInstruction* instruction) { UNUSED(instruction); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002897 virtual void VisitBasicBlock(HBasicBlock* block);
2898
Roland Levillain633021e2014-10-01 14:12:25 +01002899 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002900 void VisitInsertionOrder();
2901
Roland Levillain633021e2014-10-01 14:12:25 +01002902 // Visit the graph following dominator tree reverse post-order.
2903 void VisitReversePostOrder();
2904
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002905 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002906
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002907 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002908#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002909 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
2910
2911 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
2912
2913#undef DECLARE_VISIT_INSTRUCTION
2914
2915 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07002916 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002917
2918 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
2919};
2920
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002921class HGraphDelegateVisitor : public HGraphVisitor {
2922 public:
2923 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
2924 virtual ~HGraphDelegateVisitor() {}
2925
2926 // Visit functions that delegate to to super class.
2927#define DECLARE_VISIT_INSTRUCTION(name, super) \
2928 virtual void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
2929
2930 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
2931
2932#undef DECLARE_VISIT_INSTRUCTION
2933
2934 private:
2935 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
2936};
2937
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002938class HInsertionOrderIterator : public ValueObject {
2939 public:
2940 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
2941
2942 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
2943 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
2944 void Advance() { ++index_; }
2945
2946 private:
2947 const HGraph& graph_;
2948 size_t index_;
2949
2950 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
2951};
2952
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002953class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002954 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002955 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002956
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002957 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
2958 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002959 void Advance() { ++index_; }
2960
2961 private:
2962 const HGraph& graph_;
2963 size_t index_;
2964
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002965 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002966};
2967
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002968class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002969 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002970 explicit HPostOrderIterator(const HGraph& graph)
2971 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002972
2973 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002974 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002975 void Advance() { --index_; }
2976
2977 private:
2978 const HGraph& graph_;
2979 size_t index_;
2980
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002981 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002982};
2983
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002984} // namespace art
2985
2986#endif // ART_COMPILER_OPTIMIZING_NODES_H_