blob: 2cc021cccfc67d9a2a00bce96fc38dbfc1b5bc4f [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;
Jean Christophe Beyler53d9da82014-12-04 13:28:25 -0800198 void RemoveBlock(HBasicBlock* block) const;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000199
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000200 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000201
202 // List of blocks in insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000203 GrowableArray<HBasicBlock*> blocks_;
204
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100205 // List of blocks to perform a reverse post order tree traversal.
206 GrowableArray<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000207
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000208 HBasicBlock* entry_block_;
209 HBasicBlock* exit_block_;
210
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100211 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100212 uint16_t maximum_number_of_out_vregs_;
213
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100214 // The number of virtual registers in this method. Contains the parameters.
215 uint16_t number_of_vregs_;
216
217 // The number of virtual registers used by parameters of this method.
218 uint16_t number_of_in_vregs_;
219
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000220 // Number of vreg size slots that the temporaries use (used in baseline compiler).
221 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100222
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000223 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000224 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000225
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000226 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000227 DISALLOW_COPY_AND_ASSIGN(HGraph);
228};
229
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700230class HLoopInformation : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000231 public:
232 HLoopInformation(HBasicBlock* header, HGraph* graph)
233 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100234 suspend_check_(nullptr),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100235 back_edges_(graph->GetArena(), kDefaultNumberOfBackEdges),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100236 // Make bit vector growable, as the number of blocks may change.
237 blocks_(graph->GetArena(), graph->GetBlocks().Size(), true) {}
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100238
239 HBasicBlock* GetHeader() const {
240 return header_;
241 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000242
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100243 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
244 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
245 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
246
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000247 void AddBackEdge(HBasicBlock* back_edge) {
248 back_edges_.Add(back_edge);
249 }
250
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100251 void RemoveBackEdge(HBasicBlock* back_edge) {
252 back_edges_.Delete(back_edge);
253 }
254
255 bool IsBackEdge(HBasicBlock* block) {
256 for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) {
257 if (back_edges_.Get(i) == block) return true;
258 }
259 return false;
260 }
261
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000262 size_t NumberOfBackEdges() const {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000263 return back_edges_.Size();
264 }
265
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100266 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100267
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100268 const GrowableArray<HBasicBlock*>& GetBackEdges() const {
269 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100270 }
271
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100272 void ClearBackEdges() {
273 back_edges_.Reset();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100274 }
275
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100276 // Find blocks that are part of this loop. Returns whether the loop is a natural loop,
277 // that is the header dominates the back edge.
278 bool Populate();
279
280 // Returns whether this loop information contains `block`.
281 // Note that this loop information *must* be populated before entering this function.
282 bool Contains(const HBasicBlock& block) const;
283
284 // Returns whether this loop information is an inner loop of `other`.
285 // Note that `other` *must* be populated before entering this function.
286 bool IsIn(const HLoopInformation& other) const;
287
288 const ArenaBitVector& GetBlocks() const { return blocks_; }
289
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000290 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100291 // Internal recursive implementation of `Populate`.
292 void PopulateRecursive(HBasicBlock* block);
293
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000294 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100295 HSuspendCheck* suspend_check_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000296 GrowableArray<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100297 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000298
299 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
300};
301
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100302static constexpr size_t kNoLifetime = -1;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100303static constexpr uint32_t kNoDexPc = -1;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100304
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000305// A block in a method. Contains the list of instructions represented
306// as a double linked list. Each block knows its predecessors and
307// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100308
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700309class HBasicBlock : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000310 public:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100311 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000312 : graph_(graph),
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000313 predecessors_(graph->GetArena(), kDefaultNumberOfPredecessors),
314 successors_(graph->GetArena(), kDefaultNumberOfSuccessors),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000315 loop_information_(nullptr),
316 dominator_(nullptr),
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100317 dominated_blocks_(graph->GetArena(), kDefaultNumberOfDominatedBlocks),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100318 block_id_(-1),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100319 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100320 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000321 lifetime_end_(kNoLifetime),
322 is_catch_block_(false) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000323
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100324 const GrowableArray<HBasicBlock*>& GetPredecessors() const {
325 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000326 }
327
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100328 const GrowableArray<HBasicBlock*>& GetSuccessors() const {
329 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000330 }
331
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100332 const GrowableArray<HBasicBlock*>& GetDominatedBlocks() const {
333 return dominated_blocks_;
334 }
335
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100336 bool IsEntryBlock() const {
337 return graph_->GetEntryBlock() == this;
338 }
339
340 bool IsExitBlock() const {
341 return graph_->GetExitBlock() == this;
342 }
343
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000344 void AddBackEdge(HBasicBlock* back_edge) {
345 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000346 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000347 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100348 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000349 loop_information_->AddBackEdge(back_edge);
350 }
351
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000352 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000353
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000354 int GetBlockId() const { return block_id_; }
355 void SetBlockId(int id) { block_id_ = id; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000356
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000357 HBasicBlock* GetDominator() const { return dominator_; }
358 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100359 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.Add(block); }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000360
361 int NumberOfBackEdges() const {
362 return loop_information_ == nullptr
363 ? 0
364 : loop_information_->NumberOfBackEdges();
365 }
366
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100367 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
368 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100369 const HInstructionList& GetInstructions() const { return instructions_; }
370 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100371 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000372
373 void AddSuccessor(HBasicBlock* block) {
374 successors_.Add(block);
375 block->predecessors_.Add(this);
376 }
377
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100378 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
379 size_t successor_index = GetSuccessorIndexOf(existing);
380 DCHECK_NE(successor_index, static_cast<size_t>(-1));
381 existing->RemovePredecessor(this);
382 new_block->predecessors_.Add(this);
383 successors_.Put(successor_index, new_block);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000384 }
385
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100386 void RemovePredecessor(HBasicBlock* block) {
387 predecessors_.Delete(block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100388 }
389
390 void ClearAllPredecessors() {
391 predecessors_.Reset();
392 }
393
394 void AddPredecessor(HBasicBlock* block) {
395 predecessors_.Add(block);
396 block->successors_.Add(this);
397 }
398
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100399 void SwapPredecessors() {
Nicolas Geoffrayc83d4412014-09-18 16:46:20 +0100400 DCHECK_EQ(predecessors_.Size(), 2u);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100401 HBasicBlock* temp = predecessors_.Get(0);
402 predecessors_.Put(0, predecessors_.Get(1));
403 predecessors_.Put(1, temp);
404 }
405
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100406 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) {
407 for (size_t i = 0, e = predecessors_.Size(); i < e; ++i) {
408 if (predecessors_.Get(i) == predecessor) {
409 return i;
410 }
411 }
412 return -1;
413 }
414
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100415 size_t GetSuccessorIndexOf(HBasicBlock* successor) {
416 for (size_t i = 0, e = successors_.Size(); i < e; ++i) {
417 if (successors_.Get(i) == successor) {
418 return i;
419 }
420 }
421 return -1;
422 }
423
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000424 void AddInstruction(HInstruction* instruction);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100425 void RemoveInstruction(HInstruction* instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100426 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +0100427 // Replace instruction `initial` with `replacement` within this block.
428 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
429 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100430 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100431 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100432 void RemovePhi(HPhi* phi);
433
434 bool IsLoopHeader() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100435 return (loop_information_ != nullptr) && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100436 }
437
Roland Levillain6b879dd2014-09-22 17:13:44 +0100438 bool IsLoopPreHeaderFirstPredecessor() const {
439 DCHECK(IsLoopHeader());
440 DCHECK(!GetPredecessors().IsEmpty());
441 return GetPredecessors().Get(0) == GetLoopInformation()->GetPreHeader();
442 }
443
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100444 HLoopInformation* GetLoopInformation() const {
445 return loop_information_;
446 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000447
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100448 // Set the loop_information_ on this block. This method overrides the current
449 // loop_information if it is an outer loop of the passed loop information.
450 void SetInLoop(HLoopInformation* info) {
451 if (IsLoopHeader()) {
452 // Nothing to do. This just means `info` is an outer loop.
453 } else if (loop_information_ == nullptr) {
454 loop_information_ = info;
455 } else if (loop_information_->Contains(*info->GetHeader())) {
456 // Block is currently part of an outer loop. Make it part of this inner loop.
457 // Note that a non loop header having a loop information means this loop information
458 // has already been populated
459 loop_information_ = info;
460 } else {
461 // Block is part of an inner loop. Do not update the loop information.
462 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
463 // at this point, because this method is being called while populating `info`.
464 }
465 }
466
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100467 bool IsInLoop() const { return loop_information_ != nullptr; }
468
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100469 // Returns wheter this block dominates the blocked passed as parameter.
470 bool Dominates(HBasicBlock* block) const;
471
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100472 size_t GetLifetimeStart() const { return lifetime_start_; }
473 size_t GetLifetimeEnd() const { return lifetime_end_; }
474
475 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
476 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
477
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100478 uint32_t GetDexPc() const { return dex_pc_; }
479
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000480 bool IsCatchBlock() const { return is_catch_block_; }
481 void SetIsCatchBlock() { is_catch_block_ = true; }
482
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000483 private:
484 HGraph* const graph_;
485 GrowableArray<HBasicBlock*> predecessors_;
486 GrowableArray<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100487 HInstructionList instructions_;
488 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000489 HLoopInformation* loop_information_;
490 HBasicBlock* dominator_;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100491 GrowableArray<HBasicBlock*> dominated_blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000492 int block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100493 // The dex program counter of the first instruction of this block.
494 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100495 size_t lifetime_start_;
496 size_t lifetime_end_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000497 bool is_catch_block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000498
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000499 friend class HGraph;
500 friend class HInstruction;
501
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000502 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
503};
504
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100505#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
506 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000507 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000508 M(ArrayGet, Instruction) \
509 M(ArrayLength, Instruction) \
510 M(ArraySet, Instruction) \
511 M(BoundsCheck, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000512 M(CheckCast, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100513 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000514 M(Compare, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100515 M(Condition, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000516 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +0000517 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000518 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100519 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000520 M(Exit, Instruction) \
521 M(FloatConstant, Constant) \
522 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100523 M(GreaterThan, Condition) \
524 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100525 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000526 M(InstanceFieldGet, Instruction) \
527 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000528 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100529 M(IntConstant, Constant) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +0000530 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000531 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100532 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000533 M(LessThan, Condition) \
534 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000535 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000536 M(LoadException, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100537 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000538 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100539 M(Local, Instruction) \
540 M(LongConstant, Constant) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +0000541 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000542 M(Mul, BinaryOperation) \
543 M(Neg, UnaryOperation) \
544 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100545 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +0100546 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000547 M(NotEqual, Condition) \
548 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000549 M(Or, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100550 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000551 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100552 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000553 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100554 M(Return, Instruction) \
555 M(ReturnVoid, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000556 M(Shl, BinaryOperation) \
557 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100558 M(StaticFieldGet, Instruction) \
559 M(StaticFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100560 M(StoreLocal, Instruction) \
561 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100562 M(SuspendCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000563 M(Temporary, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000564 M(Throw, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +0000565 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000566 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000567 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000568
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100569#define FOR_EACH_INSTRUCTION(M) \
570 FOR_EACH_CONCRETE_INSTRUCTION(M) \
571 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +0100572 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +0100573 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100574 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -0700575
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100576#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000577FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
578#undef FORWARD_DECLARATION
579
Roland Levillainccc07a92014-09-16 14:48:16 +0100580#define DECLARE_INSTRUCTION(type) \
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100581 virtual InstructionKind GetKind() const { return k##type; } \
Roland Levillainccc07a92014-09-16 14:48:16 +0100582 virtual const char* DebugName() const { return #type; } \
583 virtual const H##type* As##type() const OVERRIDE { return this; } \
584 virtual H##type* As##type() OVERRIDE { return this; } \
585 virtual bool InstructionTypeEquals(HInstruction* other) const { \
586 return other->Is##type(); \
587 } \
588 virtual void Accept(HGraphVisitor* visitor)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000589
David Brazdiled596192015-01-23 10:39:45 +0000590template <typename T> class HUseList;
591
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100592template <typename T>
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700593class HUseListNode : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000594 public:
David Brazdiled596192015-01-23 10:39:45 +0000595 HUseListNode* GetPrevious() const { return prev_; }
596 HUseListNode* GetNext() const { return next_; }
597 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100598 size_t GetIndex() const { return index_; }
599
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000600 private:
David Brazdiled596192015-01-23 10:39:45 +0000601 HUseListNode(T user, size_t index)
602 : user_(user), index_(index), prev_(nullptr), next_(nullptr) {}
603
604 T const user_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100605 const size_t index_;
David Brazdiled596192015-01-23 10:39:45 +0000606 HUseListNode<T>* prev_;
607 HUseListNode<T>* next_;
608
609 friend class HUseList<T>;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000610
611 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
612};
613
David Brazdiled596192015-01-23 10:39:45 +0000614template <typename T>
615class HUseList : public ValueObject {
616 public:
617 HUseList() : first_(nullptr) {}
618
619 void Clear() {
620 first_ = nullptr;
621 }
622
623 // Adds a new entry at the beginning of the use list and returns
624 // the newly created node.
625 HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) {
David Brazdilea55b932015-01-27 17:12:29 +0000626 HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index);
David Brazdiled596192015-01-23 10:39:45 +0000627 if (IsEmpty()) {
628 first_ = new_node;
629 } else {
630 first_->prev_ = new_node;
631 new_node->next_ = first_;
632 first_ = new_node;
633 }
634 return new_node;
635 }
636
637 HUseListNode<T>* GetFirst() const {
638 return first_;
639 }
640
641 void Remove(HUseListNode<T>* node) {
642 if (node->prev_ != nullptr) {
643 node->prev_->next_ = node->next_;
644 }
645 if (node->next_ != nullptr) {
646 node->next_->prev_ = node->prev_;
647 }
648 if (node == first_) {
649 first_ = node->next_;
650 }
651 }
652
653 bool IsEmpty() const {
654 return first_ == nullptr;
655 }
656
657 bool HasOnlyOneUse() const {
658 return first_ != nullptr && first_->next_ == nullptr;
659 }
660
661 private:
662 HUseListNode<T>* first_;
663};
664
665template<typename T>
666class HUseIterator : public ValueObject {
667 public:
668 explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {}
669
670 bool Done() const { return current_ == nullptr; }
671
672 void Advance() {
673 DCHECK(!Done());
674 current_ = current_->GetNext();
675 }
676
677 HUseListNode<T>* Current() const {
678 DCHECK(!Done());
679 return current_;
680 }
681
682 private:
683 HUseListNode<T>* current_;
684
685 friend class HValue;
686};
687
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100688// Represents the side effects an instruction may have.
689class SideEffects : public ValueObject {
690 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100691 SideEffects() : flags_(0) {}
692
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100693 static SideEffects None() {
694 return SideEffects(0);
695 }
696
697 static SideEffects All() {
698 return SideEffects(ChangesSomething().flags_ | DependsOnSomething().flags_);
699 }
700
701 static SideEffects ChangesSomething() {
702 return SideEffects((1 << kFlagChangesCount) - 1);
703 }
704
705 static SideEffects DependsOnSomething() {
706 int count = kFlagDependsOnCount - kFlagChangesCount;
707 return SideEffects(((1 << count) - 1) << kFlagChangesCount);
708 }
709
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100710 SideEffects Union(SideEffects other) const {
711 return SideEffects(flags_ | other.flags_);
712 }
713
Roland Levillain72bceff2014-09-15 18:29:00 +0100714 bool HasSideEffects() const {
715 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
716 return (flags_ & all_bits_set) != 0;
717 }
718
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100719 bool HasAllSideEffects() const {
720 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
721 return all_bits_set == (flags_ & all_bits_set);
722 }
723
724 bool DependsOn(SideEffects other) const {
725 size_t depends_flags = other.ComputeDependsFlags();
726 return (flags_ & depends_flags) != 0;
727 }
728
729 bool HasDependencies() const {
730 int count = kFlagDependsOnCount - kFlagChangesCount;
731 size_t all_bits_set = (1 << count) - 1;
732 return ((flags_ >> kFlagChangesCount) & all_bits_set) != 0;
733 }
734
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100735 private:
736 static constexpr int kFlagChangesSomething = 0;
737 static constexpr int kFlagChangesCount = kFlagChangesSomething + 1;
738
739 static constexpr int kFlagDependsOnSomething = kFlagChangesCount;
740 static constexpr int kFlagDependsOnCount = kFlagDependsOnSomething + 1;
741
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100742 explicit SideEffects(size_t flags) : flags_(flags) {}
743
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100744 size_t ComputeDependsFlags() const {
745 return flags_ << kFlagChangesCount;
746 }
747
748 size_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100749};
750
David Brazdiled596192015-01-23 10:39:45 +0000751// A HEnvironment object contains the values of virtual registers at a given location.
752class HEnvironment : public ArenaObject<kArenaAllocMisc> {
753 public:
754 HEnvironment(ArenaAllocator* arena, size_t number_of_vregs)
755 : vregs_(arena, number_of_vregs) {
756 vregs_.SetSize(number_of_vregs);
757 for (size_t i = 0; i < number_of_vregs; i++) {
758 vregs_.Put(i, VRegInfo(nullptr, nullptr));
759 }
760 }
761
762 void CopyFrom(HEnvironment* env);
763
764 void SetRawEnvAt(size_t index, HInstruction* instruction) {
765 vregs_.Put(index, VRegInfo(instruction, nullptr));
766 }
767
768 // Record instructions' use entries of this environment for constant-time removal.
769 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
770 DCHECK(env_use->GetUser() == this);
771 size_t index = env_use->GetIndex();
772 VRegInfo info = vregs_.Get(index);
773 DCHECK(info.vreg_ != nullptr);
774 DCHECK(info.node_ == nullptr);
775 vregs_.Put(index, VRegInfo(info.vreg_, env_use));
776 }
777
778 HInstruction* GetInstructionAt(size_t index) const {
779 return vregs_.Get(index).vreg_;
780 }
781
782 HUseListNode<HEnvironment*>* GetInstructionEnvUseAt(size_t index) const {
783 return vregs_.Get(index).node_;
784 }
785
786 size_t Size() const { return vregs_.Size(); }
787
788 private:
789 struct VRegInfo {
790 HInstruction* vreg_;
791 HUseListNode<HEnvironment*>* node_;
792
793 VRegInfo(HInstruction* instruction, HUseListNode<HEnvironment*>* env_use)
794 : vreg_(instruction), node_(env_use) {}
795 };
796
797 GrowableArray<VRegInfo> vregs_;
798
799 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
800};
801
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700802class HInstruction : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000803 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100804 explicit HInstruction(SideEffects side_effects)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000805 : previous_(nullptr),
806 next_(nullptr),
807 block_(nullptr),
808 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100809 ssa_index_(-1),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100810 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100811 locations_(nullptr),
812 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100813 lifetime_position_(kNoLifetime),
814 side_effects_(side_effects) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000815
Dave Allison20dfc792014-06-16 20:44:29 -0700816 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000817
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100818#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100819 enum InstructionKind {
820 FOR_EACH_INSTRUCTION(DECLARE_KIND)
821 };
822#undef DECLARE_KIND
823
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000824 HInstruction* GetNext() const { return next_; }
825 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000826
Calin Juravle77520bc2015-01-12 18:45:46 +0000827 HInstruction* GetNextDisregardingMoves() const;
828 HInstruction* GetPreviousDisregardingMoves() const;
829
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000830 HBasicBlock* GetBlock() const { return block_; }
831 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100832 bool IsInBlock() const { return block_ != nullptr; }
833 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +0100834 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000835
Roland Levillain6b879dd2014-09-22 17:13:44 +0100836 virtual size_t InputCount() const = 0;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100837 virtual HInstruction* InputAt(size_t i) const = 0;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000838
839 virtual void Accept(HGraphVisitor* visitor) = 0;
840 virtual const char* DebugName() const = 0;
841
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100842 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100843 virtual void SetRawInputAt(size_t index, HInstruction* input) = 0;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100844
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100845 virtual bool NeedsEnvironment() const { return false; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100846 virtual bool IsControlFlow() const { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +0100847 virtual bool CanThrow() const { return false; }
Roland Levillain72bceff2014-09-15 18:29:00 +0100848 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100849
Calin Juravle77520bc2015-01-12 18:45:46 +0000850 virtual bool CanDoImplicitNullCheck() const { return false; }
851
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100852 void AddUseAt(HInstruction* user, size_t index) {
David Brazdiled596192015-01-23 10:39:45 +0000853 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000854 }
855
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100856 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +0100857 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +0000858 HUseListNode<HEnvironment*>* env_use =
859 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
860 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100861 }
862
863 void RemoveUser(HInstruction* user, size_t index);
David Brazdiled596192015-01-23 10:39:45 +0000864 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100865
David Brazdilea55b932015-01-27 17:12:29 +0000866 const HUseList<HInstruction*>& GetUses() { return uses_; }
867 const HUseList<HEnvironment*>& GetEnvUses() { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000868
David Brazdiled596192015-01-23 10:39:45 +0000869 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
870 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000871
Roland Levillain6c82d402014-10-13 16:10:27 +0100872 // Does this instruction strictly dominate `other_instruction`?
873 // Returns false if this instruction and `other_instruction` are the same.
874 // Aborts if this instruction and `other_instruction` are both phis.
875 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +0100876
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000877 int GetId() const { return id_; }
878 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000879
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100880 int GetSsaIndex() const { return ssa_index_; }
881 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
882 bool HasSsaIndex() const { return ssa_index_ != -1; }
883
884 bool HasEnvironment() const { return environment_ != nullptr; }
885 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100886 void SetEnvironment(HEnvironment* environment) { environment_ = environment; }
887
Nicolas Geoffray39468442014-09-02 15:17:15 +0100888 // Returns the number of entries in the environment. Typically, that is the
889 // number of dex registers in a method. It could be more in case of inlining.
890 size_t EnvironmentSize() const;
891
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000892 LocationSummary* GetLocations() const { return locations_; }
893 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000894
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100895 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100896 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100897
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000898 // Insert `this` instruction in `cursor`'s graph, just before `cursor`.
899 void InsertBefore(HInstruction* cursor);
900
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100901#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +0100902 bool Is##type() const { return (As##type() != nullptr); } \
903 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000904 virtual H##type* As##type() { return nullptr; }
905
906 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
907#undef INSTRUCTION_TYPE_CHECK
908
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100909 // Returns whether the instruction can be moved within the graph.
910 virtual bool CanBeMoved() const { return false; }
911
912 // Returns whether the two instructions are of the same kind.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700913 virtual bool InstructionTypeEquals(HInstruction* other) const {
914 UNUSED(other);
915 return false;
916 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100917
918 // Returns whether any data encoded in the two instructions is equal.
919 // This method does not look at the inputs. Both instructions must be
920 // of the same type, otherwise the method has undefined behavior.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700921 virtual bool InstructionDataEquals(HInstruction* other) const {
922 UNUSED(other);
923 return false;
924 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100925
926 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +0000927 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100928 // 2) Their inputs are identical.
929 bool Equals(HInstruction* other) const;
930
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100931 virtual InstructionKind GetKind() const = 0;
932
933 virtual size_t ComputeHashCode() const {
934 size_t result = GetKind();
935 for (size_t i = 0, e = InputCount(); i < e; ++i) {
936 result = (result * 31) + InputAt(i)->GetId();
937 }
938 return result;
939 }
940
941 SideEffects GetSideEffects() const { return side_effects_; }
942
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100943 size_t GetLifetimePosition() const { return lifetime_position_; }
944 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
945 LiveInterval* GetLiveInterval() const { return live_interval_; }
946 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
947 bool HasLiveInterval() const { return live_interval_ != nullptr; }
948
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000949 private:
950 HInstruction* previous_;
951 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000952 HBasicBlock* block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000953
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000954 // An instruction gets an id when it is added to the graph.
955 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +0100956 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000957 int id_;
958
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100959 // When doing liveness analysis, instructions that have uses get an SSA index.
960 int ssa_index_;
961
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100962 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +0000963 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100964
965 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +0000966 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100967
Nicolas Geoffray39468442014-09-02 15:17:15 +0100968 // The environment associated with this instruction. Not null if the instruction
969 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100970 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000971
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000972 // Set by the code generator.
973 LocationSummary* locations_;
974
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100975 // Set by the liveness analysis.
976 LiveInterval* live_interval_;
977
978 // Set by the liveness analysis, this is the position in a linear
979 // order of blocks where this instruction's live interval start.
980 size_t lifetime_position_;
981
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100982 const SideEffects side_effects_;
983
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000984 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000985 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100986 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000987
988 DISALLOW_COPY_AND_ASSIGN(HInstruction);
989};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700990std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000991
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000992class HInputIterator : public ValueObject {
993 public:
Dave Allison20dfc792014-06-16 20:44:29 -0700994 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000995
996 bool Done() const { return index_ == instruction_->InputCount(); }
997 HInstruction* Current() const { return instruction_->InputAt(index_); }
998 void Advance() { index_++; }
999
1000 private:
1001 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001002 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001003
1004 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
1005};
1006
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001007class HInstructionIterator : public ValueObject {
1008 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001009 explicit HInstructionIterator(const HInstructionList& instructions)
1010 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001011 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001012 }
1013
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001014 bool Done() const { return instruction_ == nullptr; }
1015 HInstruction* Current() const { return instruction_; }
1016 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001017 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001018 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001019 }
1020
1021 private:
1022 HInstruction* instruction_;
1023 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001024
1025 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001026};
1027
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001028class HBackwardInstructionIterator : public ValueObject {
1029 public:
1030 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
1031 : instruction_(instructions.last_instruction_) {
1032 next_ = Done() ? nullptr : instruction_->GetPrevious();
1033 }
1034
1035 bool Done() const { return instruction_ == nullptr; }
1036 HInstruction* Current() const { return instruction_; }
1037 void Advance() {
1038 instruction_ = next_;
1039 next_ = Done() ? nullptr : instruction_->GetPrevious();
1040 }
1041
1042 private:
1043 HInstruction* instruction_;
1044 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001045
1046 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001047};
1048
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001049// An embedded container with N elements of type T. Used (with partial
1050// specialization for N=0) because embedded arrays cannot have size 0.
1051template<typename T, intptr_t N>
1052class EmbeddedArray {
1053 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001054 EmbeddedArray() : elements_() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001055
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001056 intptr_t GetLength() const { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001057
1058 const T& operator[](intptr_t i) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001059 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001060 return elements_[i];
1061 }
1062
1063 T& operator[](intptr_t i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001064 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001065 return elements_[i];
1066 }
1067
1068 const T& At(intptr_t i) const {
1069 return (*this)[i];
1070 }
1071
1072 void SetAt(intptr_t i, const T& val) {
1073 (*this)[i] = val;
1074 }
1075
1076 private:
1077 T elements_[N];
1078};
1079
1080template<typename T>
1081class EmbeddedArray<T, 0> {
1082 public:
1083 intptr_t length() const { return 0; }
1084 const T& operator[](intptr_t i) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001085 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001086 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001087 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001088 }
1089 T& operator[](intptr_t i) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001090 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001091 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001092 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001093 }
1094};
1095
1096template<intptr_t N>
1097class HTemplateInstruction: public HInstruction {
1098 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001099 HTemplateInstruction<N>(SideEffects side_effects)
1100 : HInstruction(side_effects), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07001101 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001102
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001103 virtual size_t InputCount() const { return N; }
1104 virtual HInstruction* InputAt(size_t i) const { return inputs_[i]; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001105
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001106 protected:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001107 virtual void SetRawInputAt(size_t i, HInstruction* instruction) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001108 inputs_[i] = instruction;
1109 }
1110
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001111 private:
1112 EmbeddedArray<HInstruction*, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001113
1114 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001115};
1116
Dave Allison20dfc792014-06-16 20:44:29 -07001117template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001118class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07001119 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001120 HExpression<N>(Primitive::Type type, SideEffects side_effects)
1121 : HTemplateInstruction<N>(side_effects), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07001122 virtual ~HExpression() {}
1123
1124 virtual Primitive::Type GetType() const { return type_; }
1125
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001126 protected:
1127 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07001128};
1129
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001130// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
1131// instruction that branches to the exit block.
1132class HReturnVoid : public HTemplateInstruction<0> {
1133 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001134 HReturnVoid() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001135
1136 virtual bool IsControlFlow() const { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001137
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001138 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001139
1140 private:
1141 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
1142};
1143
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001144// Represents dex's RETURN opcodes. A HReturn is a control flow
1145// instruction that branches to the exit block.
1146class HReturn : public HTemplateInstruction<1> {
1147 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001148 explicit HReturn(HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001149 SetRawInputAt(0, value);
1150 }
1151
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001152 virtual bool IsControlFlow() const { return true; }
1153
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001154 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001155
1156 private:
1157 DISALLOW_COPY_AND_ASSIGN(HReturn);
1158};
1159
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001160// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001161// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001162// exit block.
1163class HExit : public HTemplateInstruction<0> {
1164 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001165 HExit() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001166
1167 virtual bool IsControlFlow() const { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001168
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001169 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001170
1171 private:
1172 DISALLOW_COPY_AND_ASSIGN(HExit);
1173};
1174
1175// Jumps from one block to another.
1176class HGoto : public HTemplateInstruction<0> {
1177 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001178 HGoto() : HTemplateInstruction(SideEffects::None()) {}
1179
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001180 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001181
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001182 HBasicBlock* GetSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001183 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001184 }
1185
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001186 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001187
1188 private:
1189 DISALLOW_COPY_AND_ASSIGN(HGoto);
1190};
1191
Dave Allison20dfc792014-06-16 20:44:29 -07001192
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001193// Conditional branch. A block ending with an HIf instruction must have
1194// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001195class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001196 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001197 explicit HIf(HInstruction* input) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001198 SetRawInputAt(0, input);
1199 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001200
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001201 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001202
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001203 HBasicBlock* IfTrueSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001204 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001205 }
1206
1207 HBasicBlock* IfFalseSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001208 return GetBlock()->GetSuccessors().Get(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001209 }
1210
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001211 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001212
Dave Allison20dfc792014-06-16 20:44:29 -07001213 virtual bool IsIfInstruction() const { return true; }
1214
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001215 private:
1216 DISALLOW_COPY_AND_ASSIGN(HIf);
1217};
1218
Roland Levillain88cb1752014-10-20 16:36:47 +01001219class HUnaryOperation : public HExpression<1> {
1220 public:
1221 HUnaryOperation(Primitive::Type result_type, HInstruction* input)
1222 : HExpression(result_type, SideEffects::None()) {
1223 SetRawInputAt(0, input);
1224 }
1225
1226 HInstruction* GetInput() const { return InputAt(0); }
1227 Primitive::Type GetResultType() const { return GetType(); }
1228
1229 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001230 virtual bool InstructionDataEquals(HInstruction* other) const {
1231 UNUSED(other);
1232 return true;
1233 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001234
Roland Levillain9240d6a2014-10-20 16:47:04 +01001235 // Try to statically evaluate `operation` and return a HConstant
1236 // containing the result of this evaluation. If `operation` cannot
1237 // be evaluated as a constant, return nullptr.
1238 HConstant* TryStaticEvaluation() const;
1239
1240 // Apply this operation to `x`.
1241 virtual int32_t Evaluate(int32_t x) const = 0;
1242 virtual int64_t Evaluate(int64_t x) const = 0;
1243
Roland Levillain88cb1752014-10-20 16:36:47 +01001244 DECLARE_INSTRUCTION(UnaryOperation);
1245
1246 private:
1247 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
1248};
1249
Dave Allison20dfc792014-06-16 20:44:29 -07001250class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001251 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001252 HBinaryOperation(Primitive::Type result_type,
1253 HInstruction* left,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001254 HInstruction* right) : HExpression(result_type, SideEffects::None()) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001255 SetRawInputAt(0, left);
1256 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001257 }
1258
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001259 HInstruction* GetLeft() const { return InputAt(0); }
1260 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07001261 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001262
1263 virtual bool IsCommutative() { return false; }
1264
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001265 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001266 virtual bool InstructionDataEquals(HInstruction* other) const {
1267 UNUSED(other);
1268 return true;
1269 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001270
Roland Levillain9240d6a2014-10-20 16:47:04 +01001271 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01001272 // containing the result of this evaluation. If `operation` cannot
1273 // be evaluated as a constant, return nullptr.
Roland Levillain9240d6a2014-10-20 16:47:04 +01001274 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01001275
1276 // Apply this operation to `x` and `y`.
1277 virtual int32_t Evaluate(int32_t x, int32_t y) const = 0;
1278 virtual int64_t Evaluate(int64_t x, int64_t y) const = 0;
1279
Roland Levillainccc07a92014-09-16 14:48:16 +01001280 DECLARE_INSTRUCTION(BinaryOperation);
1281
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001282 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001283 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
1284};
1285
Dave Allison20dfc792014-06-16 20:44:29 -07001286class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001287 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001288 HCondition(HInstruction* first, HInstruction* second)
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001289 : HBinaryOperation(Primitive::kPrimBoolean, first, second),
1290 needs_materialization_(true) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001291
1292 virtual bool IsCommutative() { return true; }
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001293
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001294 bool NeedsMaterialization() const { return needs_materialization_; }
1295 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001296
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001297 // For code generation purposes, returns whether this instruction is just before
1298 // `if_`, and disregard moves in between.
1299 bool IsBeforeWhenDisregardMoves(HIf* if_) const;
1300
Dave Allison20dfc792014-06-16 20:44:29 -07001301 DECLARE_INSTRUCTION(Condition);
1302
1303 virtual IfCondition GetCondition() const = 0;
1304
1305 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001306 // For register allocation purposes, returns whether this instruction needs to be
1307 // materialized (that is, not just be in the processor flags).
1308 bool needs_materialization_;
1309
Dave Allison20dfc792014-06-16 20:44:29 -07001310 DISALLOW_COPY_AND_ASSIGN(HCondition);
1311};
1312
1313// Instruction to check if two inputs are equal to each other.
1314class HEqual : public HCondition {
1315 public:
1316 HEqual(HInstruction* first, HInstruction* second)
1317 : HCondition(first, second) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001318
Roland Levillain93445682014-10-06 19:24:02 +01001319 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1320 return x == y ? 1 : 0;
1321 }
1322 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1323 return x == y ? 1 : 0;
1324 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001325
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001326 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001327
Dave Allison20dfc792014-06-16 20:44:29 -07001328 virtual IfCondition GetCondition() const {
1329 return kCondEQ;
1330 }
1331
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001332 private:
1333 DISALLOW_COPY_AND_ASSIGN(HEqual);
1334};
1335
Dave Allison20dfc792014-06-16 20:44:29 -07001336class HNotEqual : public HCondition {
1337 public:
1338 HNotEqual(HInstruction* first, HInstruction* second)
1339 : HCondition(first, second) {}
1340
Roland Levillain93445682014-10-06 19:24:02 +01001341 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1342 return x != y ? 1 : 0;
1343 }
1344 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1345 return x != y ? 1 : 0;
1346 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001347
Dave Allison20dfc792014-06-16 20:44:29 -07001348 DECLARE_INSTRUCTION(NotEqual);
1349
1350 virtual IfCondition GetCondition() const {
1351 return kCondNE;
1352 }
1353
1354 private:
1355 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
1356};
1357
1358class HLessThan : public HCondition {
1359 public:
1360 HLessThan(HInstruction* first, HInstruction* second)
1361 : HCondition(first, second) {}
1362
Roland Levillain93445682014-10-06 19:24:02 +01001363 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1364 return x < y ? 1 : 0;
1365 }
1366 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1367 return x < y ? 1 : 0;
1368 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001369
Dave Allison20dfc792014-06-16 20:44:29 -07001370 DECLARE_INSTRUCTION(LessThan);
1371
1372 virtual IfCondition GetCondition() const {
1373 return kCondLT;
1374 }
1375
1376 private:
1377 DISALLOW_COPY_AND_ASSIGN(HLessThan);
1378};
1379
1380class HLessThanOrEqual : public HCondition {
1381 public:
1382 HLessThanOrEqual(HInstruction* first, HInstruction* second)
1383 : HCondition(first, second) {}
1384
Roland Levillain93445682014-10-06 19:24:02 +01001385 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1386 return x <= y ? 1 : 0;
1387 }
1388 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1389 return x <= y ? 1 : 0;
1390 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001391
Dave Allison20dfc792014-06-16 20:44:29 -07001392 DECLARE_INSTRUCTION(LessThanOrEqual);
1393
1394 virtual IfCondition GetCondition() const {
1395 return kCondLE;
1396 }
1397
1398 private:
1399 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
1400};
1401
1402class HGreaterThan : public HCondition {
1403 public:
1404 HGreaterThan(HInstruction* first, HInstruction* second)
1405 : HCondition(first, second) {}
1406
Roland Levillain93445682014-10-06 19:24:02 +01001407 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1408 return x > y ? 1 : 0;
1409 }
1410 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1411 return x > y ? 1 : 0;
1412 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001413
Dave Allison20dfc792014-06-16 20:44:29 -07001414 DECLARE_INSTRUCTION(GreaterThan);
1415
1416 virtual IfCondition GetCondition() const {
1417 return kCondGT;
1418 }
1419
1420 private:
1421 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
1422};
1423
1424class HGreaterThanOrEqual : public HCondition {
1425 public:
1426 HGreaterThanOrEqual(HInstruction* first, HInstruction* second)
1427 : HCondition(first, second) {}
1428
Roland Levillain93445682014-10-06 19:24:02 +01001429 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1430 return x >= y ? 1 : 0;
1431 }
1432 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1433 return x >= y ? 1 : 0;
1434 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001435
Dave Allison20dfc792014-06-16 20:44:29 -07001436 DECLARE_INSTRUCTION(GreaterThanOrEqual);
1437
1438 virtual IfCondition GetCondition() const {
1439 return kCondGE;
1440 }
1441
1442 private:
1443 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
1444};
1445
1446
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001447// Instruction to check how two inputs compare to each other.
1448// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
1449class HCompare : public HBinaryOperation {
1450 public:
Calin Juravleddb7df22014-11-25 20:56:51 +00001451 // The bias applies for floating point operations and indicates how NaN
1452 // comparisons are treated:
1453 enum Bias {
1454 kNoBias, // bias is not applicable (i.e. for long operation)
1455 kGtBias, // return 1 for NaN comparisons
1456 kLtBias, // return -1 for NaN comparisons
1457 };
1458
1459 HCompare(Primitive::Type type, HInstruction* first, HInstruction* second, Bias bias)
1460 : HBinaryOperation(Primitive::kPrimInt, first, second), bias_(bias) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001461 DCHECK_EQ(type, first->GetType());
1462 DCHECK_EQ(type, second->GetType());
1463 }
1464
Calin Juravleddb7df22014-11-25 20:56:51 +00001465 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001466 return
1467 x == y ? 0 :
1468 x > y ? 1 :
1469 -1;
1470 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001471
1472 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001473 return
1474 x == y ? 0 :
1475 x > y ? 1 :
1476 -1;
1477 }
1478
Calin Juravleddb7df22014-11-25 20:56:51 +00001479 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
1480 return bias_ == other->AsCompare()->bias_;
1481 }
1482
1483 bool IsGtBias() { return bias_ == kGtBias; }
1484
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001485 DECLARE_INSTRUCTION(Compare);
1486
1487 private:
Calin Juravleddb7df22014-11-25 20:56:51 +00001488 const Bias bias_;
1489
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001490 DISALLOW_COPY_AND_ASSIGN(HCompare);
1491};
1492
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001493// A local in the graph. Corresponds to a Dex register.
1494class HLocal : public HTemplateInstruction<0> {
1495 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001496 explicit HLocal(uint16_t reg_number)
1497 : HTemplateInstruction(SideEffects::None()), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001498
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001499 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001500
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001501 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001502
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001503 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001504 // The Dex register number.
1505 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001506
1507 DISALLOW_COPY_AND_ASSIGN(HLocal);
1508};
1509
1510// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07001511class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001512 public:
Roland Levillain5799fc02014-09-25 12:15:20 +01001513 HLoadLocal(HLocal* local, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001514 : HExpression(type, SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001515 SetRawInputAt(0, local);
1516 }
1517
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001518 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1519
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001520 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001521
1522 private:
1523 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
1524};
1525
1526// Store a value in a given local. This instruction has two inputs: the value
1527// and the local.
1528class HStoreLocal : public HTemplateInstruction<2> {
1529 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001530 HStoreLocal(HLocal* local, HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001531 SetRawInputAt(0, local);
1532 SetRawInputAt(1, value);
1533 }
1534
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001535 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1536
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001537 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001538
1539 private:
1540 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
1541};
1542
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001543class HConstant : public HExpression<0> {
1544 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001545 explicit HConstant(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
1546
1547 virtual bool CanBeMoved() const { return true; }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001548
1549 DECLARE_INSTRUCTION(Constant);
1550
1551 private:
1552 DISALLOW_COPY_AND_ASSIGN(HConstant);
1553};
1554
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001555class HFloatConstant : public HConstant {
1556 public:
1557 explicit HFloatConstant(float value) : HConstant(Primitive::kPrimFloat), value_(value) {}
1558
1559 float GetValue() const { return value_; }
1560
1561 virtual bool InstructionDataEquals(HInstruction* other) const {
1562 return bit_cast<float, int32_t>(other->AsFloatConstant()->value_) ==
1563 bit_cast<float, int32_t>(value_);
1564 }
1565
1566 virtual size_t ComputeHashCode() const { return static_cast<size_t>(GetValue()); }
1567
1568 DECLARE_INSTRUCTION(FloatConstant);
1569
1570 private:
1571 const float value_;
1572
1573 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
1574};
1575
1576class HDoubleConstant : public HConstant {
1577 public:
1578 explicit HDoubleConstant(double value) : HConstant(Primitive::kPrimDouble), value_(value) {}
1579
1580 double GetValue() const { return value_; }
1581
1582 virtual bool InstructionDataEquals(HInstruction* other) const {
1583 return bit_cast<double, int64_t>(other->AsDoubleConstant()->value_) ==
1584 bit_cast<double, int64_t>(value_);
1585 }
1586
1587 virtual size_t ComputeHashCode() const { return static_cast<size_t>(GetValue()); }
1588
1589 DECLARE_INSTRUCTION(DoubleConstant);
1590
1591 private:
1592 const double value_;
1593
1594 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
1595};
1596
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001597// Constants of the type int. Those can be from Dex instructions, or
1598// synthesized (for example with the if-eqz instruction).
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001599class HIntConstant : public HConstant {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001600 public:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001601 explicit HIntConstant(int32_t value) : HConstant(Primitive::kPrimInt), value_(value) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001602
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001603 int32_t GetValue() const { return value_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001604
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001605 virtual bool InstructionDataEquals(HInstruction* other) const {
1606 return other->AsIntConstant()->value_ == value_;
1607 }
1608
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001609 virtual size_t ComputeHashCode() const { return GetValue(); }
1610
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001611 DECLARE_INSTRUCTION(IntConstant);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001612
1613 private:
1614 const int32_t value_;
1615
1616 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
1617};
1618
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001619class HLongConstant : public HConstant {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001620 public:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001621 explicit HLongConstant(int64_t value) : HConstant(Primitive::kPrimLong), value_(value) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001622
1623 int64_t GetValue() const { return value_; }
1624
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001625 virtual bool InstructionDataEquals(HInstruction* other) const {
1626 return other->AsLongConstant()->value_ == value_;
1627 }
1628
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001629 virtual size_t ComputeHashCode() const { return static_cast<size_t>(GetValue()); }
1630
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001631 DECLARE_INSTRUCTION(LongConstant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001632
1633 private:
1634 const int64_t value_;
1635
1636 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
1637};
1638
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001639enum class Intrinsics {
1640#define OPTIMIZING_INTRINSICS(Name, IsStatic) k ## Name,
1641#include "intrinsics_list.h"
1642 kNone,
1643 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
1644#undef INTRINSICS_LIST
1645#undef OPTIMIZING_INTRINSICS
1646};
1647std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
1648
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001649class HInvoke : public HInstruction {
1650 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001651 virtual size_t InputCount() const { return inputs_.Size(); }
1652 virtual HInstruction* InputAt(size_t i) const { return inputs_.Get(i); }
1653
1654 // Runtime needs to walk the stack, so Dex -> Dex calls need to
1655 // know their environment.
Calin Juravle77520bc2015-01-12 18:45:46 +00001656 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001657
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001658 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001659 SetRawInputAt(index, argument);
1660 }
1661
1662 virtual void SetRawInputAt(size_t index, HInstruction* input) {
1663 inputs_.Put(index, input);
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001664 }
1665
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001666 virtual Primitive::Type GetType() const { return return_type_; }
1667
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001668 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001669
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001670 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
1671
1672 Intrinsics GetIntrinsic() {
1673 return intrinsic_;
1674 }
1675
1676 void SetIntrinsic(Intrinsics intrinsic) {
1677 intrinsic_ = intrinsic;
1678 }
1679
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001680 DECLARE_INSTRUCTION(Invoke);
1681
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001682 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001683 HInvoke(ArenaAllocator* arena,
1684 uint32_t number_of_arguments,
1685 Primitive::Type return_type,
1686 uint32_t dex_pc,
1687 uint32_t dex_method_index)
1688 : HInstruction(SideEffects::All()),
1689 inputs_(arena, number_of_arguments),
1690 return_type_(return_type),
1691 dex_pc_(dex_pc),
1692 dex_method_index_(dex_method_index),
1693 intrinsic_(Intrinsics::kNone) {
1694 inputs_.SetSize(number_of_arguments);
1695 }
1696
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001697 GrowableArray<HInstruction*> inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001698 const Primitive::Type return_type_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001699 const uint32_t dex_pc_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001700 const uint32_t dex_method_index_;
1701 Intrinsics intrinsic_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001702
1703 private:
1704 DISALLOW_COPY_AND_ASSIGN(HInvoke);
1705};
1706
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001707class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001708 public:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001709 HInvokeStaticOrDirect(ArenaAllocator* arena,
1710 uint32_t number_of_arguments,
1711 Primitive::Type return_type,
1712 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001713 uint32_t dex_method_index,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001714 InvokeType invoke_type)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001715 : HInvoke(arena, number_of_arguments, return_type, dex_pc, dex_method_index),
Nicolas Geoffray4e44c822014-12-17 12:25:12 +00001716 invoke_type_(invoke_type) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001717
Calin Juravle77520bc2015-01-12 18:45:46 +00001718 bool CanDoImplicitNullCheck() const OVERRIDE {
1719 // We access the method via the dex cache so we can't do an implicit null check.
1720 // TODO: for intrinsics we can generate implicit null checks.
1721 return false;
1722 }
1723
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001724 InvokeType GetInvokeType() const { return invoke_type_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001725
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001726 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001727
1728 private:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001729 const InvokeType invoke_type_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001730
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001731 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001732};
1733
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001734class HInvokeVirtual : public HInvoke {
1735 public:
1736 HInvokeVirtual(ArenaAllocator* arena,
1737 uint32_t number_of_arguments,
1738 Primitive::Type return_type,
1739 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001740 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001741 uint32_t vtable_index)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001742 : HInvoke(arena, number_of_arguments, return_type, dex_pc, dex_method_index),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001743 vtable_index_(vtable_index) {}
1744
Calin Juravle77520bc2015-01-12 18:45:46 +00001745 bool CanDoImplicitNullCheck() const OVERRIDE {
1746 // TODO: Add implicit null checks in intrinsics.
1747 return !GetLocations()->Intrinsified();
1748 }
1749
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001750 uint32_t GetVTableIndex() const { return vtable_index_; }
1751
1752 DECLARE_INSTRUCTION(InvokeVirtual);
1753
1754 private:
1755 const uint32_t vtable_index_;
1756
1757 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
1758};
1759
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001760class HInvokeInterface : public HInvoke {
1761 public:
1762 HInvokeInterface(ArenaAllocator* arena,
1763 uint32_t number_of_arguments,
1764 Primitive::Type return_type,
1765 uint32_t dex_pc,
1766 uint32_t dex_method_index,
1767 uint32_t imt_index)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001768 : HInvoke(arena, number_of_arguments, return_type, dex_pc, dex_method_index),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001769 imt_index_(imt_index) {}
1770
Calin Juravle77520bc2015-01-12 18:45:46 +00001771 bool CanDoImplicitNullCheck() const OVERRIDE {
1772 // TODO: Add implicit null checks in intrinsics.
1773 return !GetLocations()->Intrinsified();
1774 }
1775
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001776 uint32_t GetImtIndex() const { return imt_index_; }
1777 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
1778
1779 DECLARE_INSTRUCTION(InvokeInterface);
1780
1781 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001782 const uint32_t imt_index_;
1783
1784 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
1785};
1786
Dave Allison20dfc792014-06-16 20:44:29 -07001787class HNewInstance : public HExpression<0> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001788 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001789 HNewInstance(uint32_t dex_pc, uint16_t type_index)
1790 : HExpression(Primitive::kPrimNot, SideEffects::None()),
1791 dex_pc_(dex_pc),
1792 type_index_(type_index) {}
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001793
1794 uint32_t GetDexPc() const { return dex_pc_; }
1795 uint16_t GetTypeIndex() const { return type_index_; }
1796
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001797 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00001798 bool NeedsEnvironment() const OVERRIDE { return true; }
1799 // It may throw when called on:
1800 // - interfaces
1801 // - abstract/innaccessible/unknown classes
1802 // TODO: optimize when possible.
1803 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001804
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001805 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001806
1807 private:
1808 const uint32_t dex_pc_;
1809 const uint16_t type_index_;
1810
1811 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
1812};
1813
Roland Levillain88cb1752014-10-20 16:36:47 +01001814class HNeg : public HUnaryOperation {
1815 public:
1816 explicit HNeg(Primitive::Type result_type, HInstruction* input)
1817 : HUnaryOperation(result_type, input) {}
1818
Roland Levillain9240d6a2014-10-20 16:47:04 +01001819 virtual int32_t Evaluate(int32_t x) const OVERRIDE { return -x; }
1820 virtual int64_t Evaluate(int64_t x) const OVERRIDE { return -x; }
1821
Roland Levillain88cb1752014-10-20 16:36:47 +01001822 DECLARE_INSTRUCTION(Neg);
1823
1824 private:
1825 DISALLOW_COPY_AND_ASSIGN(HNeg);
1826};
1827
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001828class HNewArray : public HExpression<1> {
1829 public:
1830 HNewArray(HInstruction* length, uint32_t dex_pc, uint16_t type_index)
1831 : HExpression(Primitive::kPrimNot, SideEffects::None()),
1832 dex_pc_(dex_pc),
1833 type_index_(type_index) {
1834 SetRawInputAt(0, length);
1835 }
1836
1837 uint32_t GetDexPc() const { return dex_pc_; }
1838 uint16_t GetTypeIndex() const { return type_index_; }
1839
1840 // Calls runtime so needs an environment.
1841 virtual bool NeedsEnvironment() const { return true; }
1842
1843 DECLARE_INSTRUCTION(NewArray);
1844
1845 private:
1846 const uint32_t dex_pc_;
1847 const uint16_t type_index_;
1848
1849 DISALLOW_COPY_AND_ASSIGN(HNewArray);
1850};
1851
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001852class HAdd : public HBinaryOperation {
1853 public:
1854 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1855 : HBinaryOperation(result_type, left, right) {}
1856
1857 virtual bool IsCommutative() { return true; }
1858
Roland Levillain93445682014-10-06 19:24:02 +01001859 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1860 return x + y;
1861 }
1862 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1863 return x + y;
1864 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001865
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001866 DECLARE_INSTRUCTION(Add);
1867
1868 private:
1869 DISALLOW_COPY_AND_ASSIGN(HAdd);
1870};
1871
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001872class HSub : public HBinaryOperation {
1873 public:
1874 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1875 : HBinaryOperation(result_type, left, right) {}
1876
Roland Levillain93445682014-10-06 19:24:02 +01001877 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1878 return x - y;
1879 }
1880 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1881 return x - y;
1882 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001883
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001884 DECLARE_INSTRUCTION(Sub);
1885
1886 private:
1887 DISALLOW_COPY_AND_ASSIGN(HSub);
1888};
1889
Calin Juravle34bacdf2014-10-07 20:23:36 +01001890class HMul : public HBinaryOperation {
1891 public:
1892 HMul(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1893 : HBinaryOperation(result_type, left, right) {}
1894
1895 virtual bool IsCommutative() { return true; }
1896
1897 virtual int32_t Evaluate(int32_t x, int32_t y) const { return x * y; }
1898 virtual int64_t Evaluate(int64_t x, int64_t y) const { return x * y; }
1899
1900 DECLARE_INSTRUCTION(Mul);
1901
1902 private:
1903 DISALLOW_COPY_AND_ASSIGN(HMul);
1904};
1905
Calin Juravle7c4954d2014-10-28 16:57:40 +00001906class HDiv : public HBinaryOperation {
1907 public:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001908 HDiv(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
1909 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00001910
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00001911 virtual int32_t Evaluate(int32_t x, int32_t y) const {
1912 // Our graph structure ensures we never have 0 for `y` during constant folding.
1913 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00001914 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00001915 return (y == -1) ? -x : x / y;
1916 }
Calin Juravlebacfec32014-11-14 15:54:36 +00001917
1918 virtual int64_t Evaluate(int64_t x, int64_t y) const {
1919 DCHECK_NE(y, 0);
1920 // Special case -1 to avoid getting a SIGFPE on x86(_64).
1921 return (y == -1) ? -x : x / y;
1922 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00001923
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001924 uint32_t GetDexPc() const { return dex_pc_; }
1925
Calin Juravle7c4954d2014-10-28 16:57:40 +00001926 DECLARE_INSTRUCTION(Div);
1927
1928 private:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001929 const uint32_t dex_pc_;
1930
Calin Juravle7c4954d2014-10-28 16:57:40 +00001931 DISALLOW_COPY_AND_ASSIGN(HDiv);
1932};
1933
Calin Juravlebacfec32014-11-14 15:54:36 +00001934class HRem : public HBinaryOperation {
1935 public:
1936 HRem(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
1937 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
1938
1939 virtual int32_t Evaluate(int32_t x, int32_t y) const {
1940 DCHECK_NE(y, 0);
1941 // Special case -1 to avoid getting a SIGFPE on x86(_64).
1942 return (y == -1) ? 0 : x % y;
1943 }
1944
1945 virtual int64_t Evaluate(int64_t x, int64_t y) const {
1946 DCHECK_NE(y, 0);
1947 // Special case -1 to avoid getting a SIGFPE on x86(_64).
1948 return (y == -1) ? 0 : x % y;
1949 }
1950
1951 uint32_t GetDexPc() const { return dex_pc_; }
1952
1953 DECLARE_INSTRUCTION(Rem);
1954
1955 private:
1956 const uint32_t dex_pc_;
1957
1958 DISALLOW_COPY_AND_ASSIGN(HRem);
1959};
1960
Calin Juravled0d48522014-11-04 16:40:20 +00001961class HDivZeroCheck : public HExpression<1> {
1962 public:
1963 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
1964 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
1965 SetRawInputAt(0, value);
1966 }
1967
1968 bool CanBeMoved() const OVERRIDE { return true; }
1969
1970 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
1971 UNUSED(other);
1972 return true;
1973 }
1974
1975 bool NeedsEnvironment() const OVERRIDE { return true; }
1976 bool CanThrow() const OVERRIDE { return true; }
1977
1978 uint32_t GetDexPc() const { return dex_pc_; }
1979
1980 DECLARE_INSTRUCTION(DivZeroCheck);
1981
1982 private:
1983 const uint32_t dex_pc_;
1984
1985 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
1986};
1987
Calin Juravle9aec02f2014-11-18 23:06:35 +00001988class HShl : public HBinaryOperation {
1989 public:
1990 HShl(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1991 : HBinaryOperation(result_type, left, right) {}
1992
1993 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x << (y & kMaxIntShiftValue); }
1994 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x << (y & kMaxLongShiftValue); }
1995
1996 DECLARE_INSTRUCTION(Shl);
1997
1998 private:
1999 DISALLOW_COPY_AND_ASSIGN(HShl);
2000};
2001
2002class HShr : public HBinaryOperation {
2003 public:
2004 HShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2005 : HBinaryOperation(result_type, left, right) {}
2006
2007 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x >> (y & kMaxIntShiftValue); }
2008 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x >> (y & kMaxLongShiftValue); }
2009
2010 DECLARE_INSTRUCTION(Shr);
2011
2012 private:
2013 DISALLOW_COPY_AND_ASSIGN(HShr);
2014};
2015
2016class HUShr : public HBinaryOperation {
2017 public:
2018 HUShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2019 : HBinaryOperation(result_type, left, right) {}
2020
2021 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
2022 uint32_t ux = static_cast<uint32_t>(x);
2023 uint32_t uy = static_cast<uint32_t>(y) & kMaxIntShiftValue;
2024 return static_cast<int32_t>(ux >> uy);
2025 }
2026
2027 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
2028 uint64_t ux = static_cast<uint64_t>(x);
2029 uint64_t uy = static_cast<uint64_t>(y) & kMaxLongShiftValue;
2030 return static_cast<int64_t>(ux >> uy);
2031 }
2032
2033 DECLARE_INSTRUCTION(UShr);
2034
2035 private:
2036 DISALLOW_COPY_AND_ASSIGN(HUShr);
2037};
2038
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002039class HAnd : public HBinaryOperation {
2040 public:
2041 HAnd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2042 : HBinaryOperation(result_type, left, right) {}
2043
2044 bool IsCommutative() OVERRIDE { return true; }
2045
2046 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x & y; }
2047 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x & y; }
2048
2049 DECLARE_INSTRUCTION(And);
2050
2051 private:
2052 DISALLOW_COPY_AND_ASSIGN(HAnd);
2053};
2054
2055class HOr : public HBinaryOperation {
2056 public:
2057 HOr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2058 : HBinaryOperation(result_type, left, right) {}
2059
2060 bool IsCommutative() OVERRIDE { return true; }
2061
2062 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x | y; }
2063 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x | y; }
2064
2065 DECLARE_INSTRUCTION(Or);
2066
2067 private:
2068 DISALLOW_COPY_AND_ASSIGN(HOr);
2069};
2070
2071class HXor : public HBinaryOperation {
2072 public:
2073 HXor(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2074 : HBinaryOperation(result_type, left, right) {}
2075
2076 bool IsCommutative() OVERRIDE { return true; }
2077
2078 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x ^ y; }
2079 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x ^ y; }
2080
2081 DECLARE_INSTRUCTION(Xor);
2082
2083 private:
2084 DISALLOW_COPY_AND_ASSIGN(HXor);
2085};
2086
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002087// The value of a parameter in this method. Its location depends on
2088// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07002089class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002090 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002091 HParameterValue(uint8_t index, Primitive::Type parameter_type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002092 : HExpression(parameter_type, SideEffects::None()), index_(index) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002093
2094 uint8_t GetIndex() const { return index_; }
2095
2096 DECLARE_INSTRUCTION(ParameterValue);
2097
2098 private:
2099 // The index of this parameter in the parameters list. Must be less
2100 // than HGraph::number_of_in_vregs_;
2101 const uint8_t index_;
2102
2103 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
2104};
2105
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002106class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002107 public:
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002108 explicit HNot(Primitive::Type result_type, HInstruction* input)
2109 : HUnaryOperation(result_type, input) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002110
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002111 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002112 virtual bool InstructionDataEquals(HInstruction* other) const {
2113 UNUSED(other);
2114 return true;
2115 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002116
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002117 virtual int32_t Evaluate(int32_t x) const OVERRIDE { return ~x; }
2118 virtual int64_t Evaluate(int64_t x) const OVERRIDE { return ~x; }
2119
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002120 DECLARE_INSTRUCTION(Not);
2121
2122 private:
2123 DISALLOW_COPY_AND_ASSIGN(HNot);
2124};
2125
Roland Levillaindff1f282014-11-05 14:15:05 +00002126class HTypeConversion : public HExpression<1> {
2127 public:
2128 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00002129 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
2130 : HExpression(result_type, SideEffects::None()), dex_pc_(dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002131 SetRawInputAt(0, input);
2132 DCHECK_NE(input->GetType(), result_type);
2133 }
2134
2135 HInstruction* GetInput() const { return InputAt(0); }
2136 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
2137 Primitive::Type GetResultType() const { return GetType(); }
2138
Roland Levillain624279f2014-12-04 11:54:28 +00002139 // Required by the x86 and ARM code generators when producing calls
2140 // to the runtime.
2141 uint32_t GetDexPc() const { return dex_pc_; }
2142
Roland Levillaindff1f282014-11-05 14:15:05 +00002143 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00002144 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00002145
2146 DECLARE_INSTRUCTION(TypeConversion);
2147
2148 private:
Roland Levillain624279f2014-12-04 11:54:28 +00002149 const uint32_t dex_pc_;
2150
Roland Levillaindff1f282014-11-05 14:15:05 +00002151 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
2152};
2153
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002154class HPhi : public HInstruction {
2155 public:
2156 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002157 : HInstruction(SideEffects::None()),
2158 inputs_(arena, number_of_inputs),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002159 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002160 type_(type),
2161 is_live_(false) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002162 inputs_.SetSize(number_of_inputs);
2163 }
2164
2165 virtual size_t InputCount() const { return inputs_.Size(); }
2166 virtual HInstruction* InputAt(size_t i) const { return inputs_.Get(i); }
2167
2168 virtual void SetRawInputAt(size_t index, HInstruction* input) {
2169 inputs_.Put(index, input);
2170 }
2171
2172 void AddInput(HInstruction* input);
2173
2174 virtual Primitive::Type GetType() const { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002175 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002176
2177 uint32_t GetRegNumber() const { return reg_number_; }
2178
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002179 void SetDead() { is_live_ = false; }
2180 void SetLive() { is_live_ = true; }
2181 bool IsDead() const { return !is_live_; }
2182 bool IsLive() const { return is_live_; }
2183
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002184 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002185
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002186 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002187 GrowableArray<HInstruction*> inputs_;
2188 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002189 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002190 bool is_live_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002191
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002192 DISALLOW_COPY_AND_ASSIGN(HPhi);
2193};
2194
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002195class HNullCheck : public HExpression<1> {
2196 public:
2197 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002198 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002199 SetRawInputAt(0, value);
2200 }
2201
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002202 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002203 virtual bool InstructionDataEquals(HInstruction* other) const {
2204 UNUSED(other);
2205 return true;
2206 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002207
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002208 virtual bool NeedsEnvironment() const { return true; }
2209
Roland Levillaine161a2a2014-10-03 12:45:18 +01002210 virtual bool CanThrow() const { return true; }
2211
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002212 uint32_t GetDexPc() const { return dex_pc_; }
2213
2214 DECLARE_INSTRUCTION(NullCheck);
2215
2216 private:
2217 const uint32_t dex_pc_;
2218
2219 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
2220};
2221
2222class FieldInfo : public ValueObject {
2223 public:
Calin Juravle52c48962014-12-16 17:02:57 +00002224 FieldInfo(MemberOffset field_offset, Primitive::Type field_type, bool is_volatile)
2225 : field_offset_(field_offset), field_type_(field_type), is_volatile_(is_volatile) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002226
2227 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002228 Primitive::Type GetFieldType() const { return field_type_; }
Calin Juravle52c48962014-12-16 17:02:57 +00002229 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002230
2231 private:
2232 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01002233 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00002234 const bool is_volatile_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002235};
2236
2237class HInstanceFieldGet : public HExpression<1> {
2238 public:
2239 HInstanceFieldGet(HInstruction* value,
2240 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00002241 MemberOffset field_offset,
2242 bool is_volatile)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002243 : HExpression(field_type, SideEffects::DependsOnSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00002244 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002245 SetRawInputAt(0, value);
2246 }
2247
Calin Juravle10c9cbe2014-12-19 10:50:19 +00002248 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002249
2250 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2251 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
2252 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002253 }
2254
Calin Juravle77520bc2015-01-12 18:45:46 +00002255 bool CanDoImplicitNullCheck() const OVERRIDE {
2256 return GetFieldOffset().Uint32Value() < kPageSize;
2257 }
2258
2259 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002260 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
2261 }
2262
Calin Juravle52c48962014-12-16 17:02:57 +00002263 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002264 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002265 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002266 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002267
2268 DECLARE_INSTRUCTION(InstanceFieldGet);
2269
2270 private:
2271 const FieldInfo field_info_;
2272
2273 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
2274};
2275
2276class HInstanceFieldSet : public HTemplateInstruction<2> {
2277 public:
2278 HInstanceFieldSet(HInstruction* object,
2279 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01002280 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00002281 MemberOffset field_offset,
2282 bool is_volatile)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002283 : HTemplateInstruction(SideEffects::ChangesSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00002284 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002285 SetRawInputAt(0, object);
2286 SetRawInputAt(1, value);
2287 }
2288
Calin Juravle77520bc2015-01-12 18:45:46 +00002289 bool CanDoImplicitNullCheck() const OVERRIDE {
2290 return GetFieldOffset().Uint32Value() < kPageSize;
2291 }
2292
Calin Juravle52c48962014-12-16 17:02:57 +00002293 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002294 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002295 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002296 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002297 HInstruction* GetValue() const { return InputAt(1); }
2298
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002299 DECLARE_INSTRUCTION(InstanceFieldSet);
2300
2301 private:
2302 const FieldInfo field_info_;
2303
2304 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
2305};
2306
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002307class HArrayGet : public HExpression<2> {
2308 public:
2309 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002310 : HExpression(type, SideEffects::DependsOnSomething()) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002311 SetRawInputAt(0, array);
2312 SetRawInputAt(1, index);
2313 }
2314
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002315 bool CanBeMoved() const OVERRIDE { return true; }
2316 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002317 UNUSED(other);
2318 return true;
2319 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002320 bool CanDoImplicitNullCheck() const OVERRIDE {
2321 // TODO: We can be smarter here.
2322 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
2323 // which generates the implicit null check. There are cases when these can be removed
2324 // to produce better code. If we ever add optimizations to do so we should allow an
2325 // implicit check here (as long as the address falls in the first page).
2326 return false;
2327 }
2328
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002329 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002330
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002331 HInstruction* GetArray() const { return InputAt(0); }
2332 HInstruction* GetIndex() const { return InputAt(1); }
2333
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002334 DECLARE_INSTRUCTION(ArrayGet);
2335
2336 private:
2337 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
2338};
2339
2340class HArraySet : public HTemplateInstruction<3> {
2341 public:
2342 HArraySet(HInstruction* array,
2343 HInstruction* index,
2344 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002345 Primitive::Type expected_component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002346 uint32_t dex_pc)
2347 : HTemplateInstruction(SideEffects::ChangesSomething()),
2348 dex_pc_(dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002349 expected_component_type_(expected_component_type),
2350 needs_type_check_(value->GetType() == Primitive::kPrimNot) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002351 SetRawInputAt(0, array);
2352 SetRawInputAt(1, index);
2353 SetRawInputAt(2, value);
2354 }
2355
Calin Juravle77520bc2015-01-12 18:45:46 +00002356 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002357 // We currently always call a runtime method to catch array store
2358 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002359 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002360 }
2361
Calin Juravle77520bc2015-01-12 18:45:46 +00002362 bool CanDoImplicitNullCheck() const OVERRIDE {
2363 // TODO: Same as for ArrayGet.
2364 return false;
2365 }
2366
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002367 void ClearNeedsTypeCheck() {
2368 needs_type_check_ = false;
2369 }
2370
2371 bool NeedsTypeCheck() const { return needs_type_check_; }
2372
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002373 uint32_t GetDexPc() const { return dex_pc_; }
2374
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002375 HInstruction* GetArray() const { return InputAt(0); }
2376 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002377 HInstruction* GetValue() const { return InputAt(2); }
2378
2379 Primitive::Type GetComponentType() const {
2380 // The Dex format does not type floating point index operations. Since the
2381 // `expected_component_type_` is set during building and can therefore not
2382 // be correct, we also check what is the value type. If it is a floating
2383 // point type, we must use that type.
2384 Primitive::Type value_type = GetValue()->GetType();
2385 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
2386 ? value_type
2387 : expected_component_type_;
2388 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002389
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002390 DECLARE_INSTRUCTION(ArraySet);
2391
2392 private:
2393 const uint32_t dex_pc_;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002394 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002395 bool needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002396
2397 DISALLOW_COPY_AND_ASSIGN(HArraySet);
2398};
2399
2400class HArrayLength : public HExpression<1> {
2401 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002402 explicit HArrayLength(HInstruction* array)
2403 : HExpression(Primitive::kPrimInt, SideEffects::None()) {
2404 // Note that arrays do not change length, so the instruction does not
2405 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002406 SetRawInputAt(0, array);
2407 }
2408
Calin Juravle77520bc2015-01-12 18:45:46 +00002409 bool CanBeMoved() const OVERRIDE { return true; }
2410 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002411 UNUSED(other);
2412 return true;
2413 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002414 bool CanDoImplicitNullCheck() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002415
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002416 DECLARE_INSTRUCTION(ArrayLength);
2417
2418 private:
2419 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
2420};
2421
2422class HBoundsCheck : public HExpression<2> {
2423 public:
2424 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002425 : HExpression(index->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002426 DCHECK(index->GetType() == Primitive::kPrimInt);
2427 SetRawInputAt(0, index);
2428 SetRawInputAt(1, length);
2429 }
2430
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002431 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002432 virtual bool InstructionDataEquals(HInstruction* other) const {
2433 UNUSED(other);
2434 return true;
2435 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002436
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002437 virtual bool NeedsEnvironment() const { return true; }
2438
Roland Levillaine161a2a2014-10-03 12:45:18 +01002439 virtual bool CanThrow() const { return true; }
2440
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002441 uint32_t GetDexPc() const { return dex_pc_; }
2442
2443 DECLARE_INSTRUCTION(BoundsCheck);
2444
2445 private:
2446 const uint32_t dex_pc_;
2447
2448 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
2449};
2450
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002451/**
2452 * Some DEX instructions are folded into multiple HInstructions that need
2453 * to stay live until the last HInstruction. This class
2454 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00002455 * HInstruction stays live. `index` represents the stack location index of the
2456 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002457 */
2458class HTemporary : public HTemplateInstruction<0> {
2459 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002460 explicit HTemporary(size_t index) : HTemplateInstruction(SideEffects::None()), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002461
2462 size_t GetIndex() const { return index_; }
2463
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00002464 Primitive::Type GetType() const OVERRIDE {
2465 // The previous instruction is the one that will be stored in the temporary location.
2466 DCHECK(GetPrevious() != nullptr);
2467 return GetPrevious()->GetType();
2468 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00002469
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002470 DECLARE_INSTRUCTION(Temporary);
2471
2472 private:
2473 const size_t index_;
2474
2475 DISALLOW_COPY_AND_ASSIGN(HTemporary);
2476};
2477
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002478class HSuspendCheck : public HTemplateInstruction<0> {
2479 public:
2480 explicit HSuspendCheck(uint32_t dex_pc)
Nicolas Geoffray9ebc72c2014-09-25 16:33:42 +01002481 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002482
2483 virtual bool NeedsEnvironment() const {
2484 return true;
2485 }
2486
2487 uint32_t GetDexPc() const { return dex_pc_; }
2488
2489 DECLARE_INSTRUCTION(SuspendCheck);
2490
2491 private:
2492 const uint32_t dex_pc_;
2493
2494 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
2495};
2496
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002497/**
2498 * Instruction to load a Class object.
2499 */
2500class HLoadClass : public HExpression<0> {
2501 public:
2502 HLoadClass(uint16_t type_index,
2503 bool is_referrers_class,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002504 uint32_t dex_pc)
2505 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2506 type_index_(type_index),
2507 is_referrers_class_(is_referrers_class),
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002508 dex_pc_(dex_pc),
2509 generate_clinit_check_(false) {}
2510
2511 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002512
2513 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2514 return other->AsLoadClass()->type_index_ == type_index_;
2515 }
2516
2517 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
2518
2519 uint32_t GetDexPc() const { return dex_pc_; }
2520 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002521 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002522
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002523 bool NeedsEnvironment() const OVERRIDE {
2524 // Will call runtime and load the class if the class is not loaded yet.
2525 // TODO: finer grain decision.
2526 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002527 }
2528
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002529 bool MustGenerateClinitCheck() const {
2530 return generate_clinit_check_;
2531 }
2532
2533 void SetMustGenerateClinitCheck() {
2534 generate_clinit_check_ = true;
2535 }
2536
2537 bool CanCallRuntime() const {
2538 return MustGenerateClinitCheck() || !is_referrers_class_;
2539 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002540
2541 DECLARE_INSTRUCTION(LoadClass);
2542
2543 private:
2544 const uint16_t type_index_;
2545 const bool is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002546 const uint32_t dex_pc_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002547 // Whether this instruction must generate the initialization check.
2548 // Used for code generation.
2549 bool generate_clinit_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002550
2551 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
2552};
2553
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002554class HLoadString : public HExpression<0> {
2555 public:
2556 HLoadString(uint32_t string_index, uint32_t dex_pc)
2557 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2558 string_index_(string_index),
2559 dex_pc_(dex_pc) {}
2560
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002561 bool CanBeMoved() const OVERRIDE { return true; }
2562
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002563 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2564 return other->AsLoadString()->string_index_ == string_index_;
2565 }
2566
2567 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
2568
2569 uint32_t GetDexPc() const { return dex_pc_; }
2570 uint32_t GetStringIndex() const { return string_index_; }
2571
2572 // TODO: Can we deopt or debug when we resolve a string?
2573 bool NeedsEnvironment() const OVERRIDE { return false; }
2574
2575 DECLARE_INSTRUCTION(LoadString);
2576
2577 private:
2578 const uint32_t string_index_;
2579 const uint32_t dex_pc_;
2580
2581 DISALLOW_COPY_AND_ASSIGN(HLoadString);
2582};
2583
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002584// TODO: Pass this check to HInvokeStaticOrDirect nodes.
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002585/**
2586 * Performs an initialization check on its Class object input.
2587 */
2588class HClinitCheck : public HExpression<1> {
2589 public:
2590 explicit HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
2591 : HExpression(Primitive::kPrimNot, SideEffects::All()),
2592 dex_pc_(dex_pc) {
2593 SetRawInputAt(0, constant);
2594 }
2595
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002596 bool CanBeMoved() const OVERRIDE { return true; }
2597 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2598 UNUSED(other);
2599 return true;
2600 }
2601
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002602 bool NeedsEnvironment() const OVERRIDE {
2603 // May call runtime to initialize the class.
2604 return true;
2605 }
2606
2607 uint32_t GetDexPc() const { return dex_pc_; }
2608
2609 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
2610
2611 DECLARE_INSTRUCTION(ClinitCheck);
2612
2613 private:
2614 const uint32_t dex_pc_;
2615
2616 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
2617};
2618
2619class HStaticFieldGet : public HExpression<1> {
2620 public:
2621 HStaticFieldGet(HInstruction* cls,
2622 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00002623 MemberOffset field_offset,
2624 bool is_volatile)
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002625 : HExpression(field_type, SideEffects::DependsOnSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00002626 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002627 SetRawInputAt(0, cls);
2628 }
2629
Calin Juravle52c48962014-12-16 17:02:57 +00002630
Calin Juravle10c9cbe2014-12-19 10:50:19 +00002631 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002632
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002633 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00002634 HStaticFieldGet* other_get = other->AsStaticFieldGet();
2635 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002636 }
2637
2638 size_t ComputeHashCode() const OVERRIDE {
2639 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
2640 }
2641
Calin Juravle52c48962014-12-16 17:02:57 +00002642 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002643 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
2644 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002645 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002646
2647 DECLARE_INSTRUCTION(StaticFieldGet);
2648
2649 private:
2650 const FieldInfo field_info_;
2651
2652 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
2653};
2654
2655class HStaticFieldSet : public HTemplateInstruction<2> {
2656 public:
2657 HStaticFieldSet(HInstruction* cls,
2658 HInstruction* value,
2659 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00002660 MemberOffset field_offset,
2661 bool is_volatile)
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002662 : HTemplateInstruction(SideEffects::ChangesSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00002663 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002664 SetRawInputAt(0, cls);
2665 SetRawInputAt(1, value);
2666 }
2667
Calin Juravle52c48962014-12-16 17:02:57 +00002668 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002669 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
2670 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002671 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002672
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002673 HInstruction* GetValue() const { return InputAt(1); }
2674
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002675 DECLARE_INSTRUCTION(StaticFieldSet);
2676
2677 private:
2678 const FieldInfo field_info_;
2679
2680 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
2681};
2682
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002683// Implement the move-exception DEX instruction.
2684class HLoadException : public HExpression<0> {
2685 public:
2686 HLoadException() : HExpression(Primitive::kPrimNot, SideEffects::None()) {}
2687
2688 DECLARE_INSTRUCTION(LoadException);
2689
2690 private:
2691 DISALLOW_COPY_AND_ASSIGN(HLoadException);
2692};
2693
2694class HThrow : public HTemplateInstruction<1> {
2695 public:
2696 HThrow(HInstruction* exception, uint32_t dex_pc)
2697 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {
2698 SetRawInputAt(0, exception);
2699 }
2700
2701 bool IsControlFlow() const OVERRIDE { return true; }
2702
2703 bool NeedsEnvironment() const OVERRIDE { return true; }
2704
2705 uint32_t GetDexPc() const { return dex_pc_; }
2706
2707 DECLARE_INSTRUCTION(Throw);
2708
2709 private:
2710 uint32_t dex_pc_;
2711
2712 DISALLOW_COPY_AND_ASSIGN(HThrow);
2713};
2714
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002715class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002716 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002717 HInstanceOf(HInstruction* object,
2718 HLoadClass* constant,
2719 bool class_is_final,
2720 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002721 : HExpression(Primitive::kPrimBoolean, SideEffects::None()),
2722 class_is_final_(class_is_final),
2723 dex_pc_(dex_pc) {
2724 SetRawInputAt(0, object);
2725 SetRawInputAt(1, constant);
2726 }
2727
2728 bool CanBeMoved() const OVERRIDE { return true; }
2729
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002730 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002731 return true;
2732 }
2733
2734 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002735 return false;
2736 }
2737
2738 uint32_t GetDexPc() const { return dex_pc_; }
2739
2740 bool IsClassFinal() const { return class_is_final_; }
2741
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002742 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002743
2744 private:
2745 const bool class_is_final_;
2746 const uint32_t dex_pc_;
2747
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002748 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
2749};
2750
2751class HCheckCast : public HTemplateInstruction<2> {
2752 public:
2753 HCheckCast(HInstruction* object,
2754 HLoadClass* constant,
2755 bool class_is_final,
2756 uint32_t dex_pc)
2757 : HTemplateInstruction(SideEffects::None()),
2758 class_is_final_(class_is_final),
2759 dex_pc_(dex_pc) {
2760 SetRawInputAt(0, object);
2761 SetRawInputAt(1, constant);
2762 }
2763
2764 bool CanBeMoved() const OVERRIDE { return true; }
2765
2766 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2767 return true;
2768 }
2769
2770 bool NeedsEnvironment() const OVERRIDE {
2771 // Instruction may throw a CheckCastError.
2772 return true;
2773 }
2774
2775 bool CanThrow() const OVERRIDE { return true; }
2776
2777 uint32_t GetDexPc() const { return dex_pc_; }
2778
2779 bool IsClassFinal() const { return class_is_final_; }
2780
2781 DECLARE_INSTRUCTION(CheckCast);
2782
2783 private:
2784 const bool class_is_final_;
2785 const uint32_t dex_pc_;
2786
2787 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002788};
2789
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00002790class HMonitorOperation : public HTemplateInstruction<1> {
2791 public:
2792 enum OperationKind {
2793 kEnter,
2794 kExit,
2795 };
2796
2797 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
2798 : HTemplateInstruction(SideEffects::None()), kind_(kind), dex_pc_(dex_pc) {
2799 SetRawInputAt(0, object);
2800 }
2801
2802 // Instruction may throw a Java exception, so we need an environment.
2803 bool NeedsEnvironment() const OVERRIDE { return true; }
2804 bool CanThrow() const OVERRIDE { return true; }
2805
2806 uint32_t GetDexPc() const { return dex_pc_; }
2807
2808 bool IsEnter() const { return kind_ == kEnter; }
2809
2810 DECLARE_INSTRUCTION(MonitorOperation);
2811
Calin Juravle52c48962014-12-16 17:02:57 +00002812 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00002813 const OperationKind kind_;
2814 const uint32_t dex_pc_;
2815
2816 private:
2817 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
2818};
2819
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002820class MoveOperands : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002821 public:
Nicolas Geoffray740475d2014-09-29 10:33:25 +01002822 MoveOperands(Location source, Location destination, HInstruction* instruction)
2823 : source_(source), destination_(destination), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002824
2825 Location GetSource() const { return source_; }
2826 Location GetDestination() const { return destination_; }
2827
2828 void SetSource(Location value) { source_ = value; }
2829 void SetDestination(Location value) { destination_ = value; }
2830
2831 // The parallel move resolver marks moves as "in-progress" by clearing the
2832 // destination (but not the source).
2833 Location MarkPending() {
2834 DCHECK(!IsPending());
2835 Location dest = destination_;
2836 destination_ = Location::NoLocation();
2837 return dest;
2838 }
2839
2840 void ClearPending(Location dest) {
2841 DCHECK(IsPending());
2842 destination_ = dest;
2843 }
2844
2845 bool IsPending() const {
2846 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
2847 return destination_.IsInvalid() && !source_.IsInvalid();
2848 }
2849
2850 // True if this blocks a move from the given location.
2851 bool Blocks(Location loc) const {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00002852 return !IsEliminated() && source_.Equals(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002853 }
2854
2855 // A move is redundant if it's been eliminated, if its source and
2856 // destination are the same, or if its destination is unneeded.
2857 bool IsRedundant() const {
2858 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
2859 }
2860
2861 // We clear both operands to indicate move that's been eliminated.
2862 void Eliminate() {
2863 source_ = destination_ = Location::NoLocation();
2864 }
2865
2866 bool IsEliminated() const {
2867 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
2868 return source_.IsInvalid();
2869 }
2870
Nicolas Geoffray740475d2014-09-29 10:33:25 +01002871 HInstruction* GetInstruction() const { return instruction_; }
2872
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002873 private:
2874 Location source_;
2875 Location destination_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01002876 // The instruction this move is assocatied with. Null when this move is
2877 // for moving an input in the expected locations of user (including a phi user).
2878 // This is only used in debug mode, to ensure we do not connect interval siblings
2879 // in the same parallel move.
2880 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002881};
2882
2883static constexpr size_t kDefaultNumberOfMoves = 4;
2884
2885class HParallelMove : public HTemplateInstruction<0> {
2886 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002887 explicit HParallelMove(ArenaAllocator* arena)
2888 : HTemplateInstruction(SideEffects::None()), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002889
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00002890 void AddMove(Location source, Location destination, HInstruction* instruction) {
2891 DCHECK(source.IsValid());
2892 DCHECK(destination.IsValid());
2893 // The parallel move resolver does not handle pairs. So we decompose the
2894 // pair locations into two moves.
2895 if (source.IsPair() && destination.IsPair()) {
2896 AddMove(source.ToLow(), destination.ToLow(), instruction);
2897 AddMove(source.ToHigh(), destination.ToHigh(), nullptr);
2898 } else if (source.IsPair()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002899 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00002900 AddMove(source.ToLow(), Location::StackSlot(destination.GetStackIndex()), instruction);
2901 AddMove(source.ToHigh(), Location::StackSlot(destination.GetHighStackIndex(4)), nullptr);
2902 } else if (destination.IsPair()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002903 if (source.IsConstant()) {
2904 // We put the same constant in the move. The code generator will handle which
2905 // low or high part to use.
2906 AddMove(source, destination.ToLow(), instruction);
2907 AddMove(source, destination.ToHigh(), nullptr);
2908 } else {
2909 DCHECK(source.IsDoubleStackSlot());
2910 AddMove(Location::StackSlot(source.GetStackIndex()), destination.ToLow(), instruction);
2911 // TODO: rewrite GetHighStackIndex to not require a word size. It's supposed to
2912 // always be 4.
2913 static constexpr int kHighOffset = 4;
2914 AddMove(Location::StackSlot(source.GetHighStackIndex(kHighOffset)),
2915 destination.ToHigh(),
2916 nullptr);
2917 }
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00002918 } else {
2919 if (kIsDebugBuild) {
2920 if (instruction != nullptr) {
2921 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
2922 DCHECK_NE(moves_.Get(i).GetInstruction(), instruction)
2923 << "Doing parallel moves for the same instruction.";
2924 }
2925 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00002926 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00002927 DCHECK(!destination.Equals(moves_.Get(i).GetDestination()))
2928 << "Same destination for two moves in a parallel move.";
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00002929 }
2930 }
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00002931 moves_.Add(MoveOperands(source, destination, instruction));
Nicolas Geoffray740475d2014-09-29 10:33:25 +01002932 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002933 }
2934
2935 MoveOperands* MoveOperandsAt(size_t index) const {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00002936 return moves_.GetRawStorage() + index;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002937 }
2938
2939 size_t NumMoves() const { return moves_.Size(); }
2940
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002941 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002942
2943 private:
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00002944 GrowableArray<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002945
2946 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
2947};
2948
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002949class HGraphVisitor : public ValueObject {
2950 public:
Dave Allison20dfc792014-06-16 20:44:29 -07002951 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
2952 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002953
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002954 virtual void VisitInstruction(HInstruction* instruction) { UNUSED(instruction); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002955 virtual void VisitBasicBlock(HBasicBlock* block);
2956
Roland Levillain633021e2014-10-01 14:12:25 +01002957 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002958 void VisitInsertionOrder();
2959
Roland Levillain633021e2014-10-01 14:12:25 +01002960 // Visit the graph following dominator tree reverse post-order.
2961 void VisitReversePostOrder();
2962
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002963 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002964
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002965 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002966#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002967 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
2968
2969 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
2970
2971#undef DECLARE_VISIT_INSTRUCTION
2972
2973 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07002974 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002975
2976 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
2977};
2978
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002979class HGraphDelegateVisitor : public HGraphVisitor {
2980 public:
2981 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
2982 virtual ~HGraphDelegateVisitor() {}
2983
2984 // Visit functions that delegate to to super class.
2985#define DECLARE_VISIT_INSTRUCTION(name, super) \
2986 virtual void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
2987
2988 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
2989
2990#undef DECLARE_VISIT_INSTRUCTION
2991
2992 private:
2993 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
2994};
2995
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002996class HInsertionOrderIterator : public ValueObject {
2997 public:
2998 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
2999
3000 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
3001 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
3002 void Advance() { ++index_; }
3003
3004 private:
3005 const HGraph& graph_;
3006 size_t index_;
3007
3008 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
3009};
3010
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003011class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003012 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003013 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003014
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003015 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
3016 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003017 void Advance() { ++index_; }
3018
3019 private:
3020 const HGraph& graph_;
3021 size_t index_;
3022
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003023 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003024};
3025
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003026class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003027 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003028 explicit HPostOrderIterator(const HGraph& graph)
3029 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003030
3031 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003032 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003033 void Advance() { --index_; }
3034
3035 private:
3036 const HGraph& graph_;
3037 size_t index_;
3038
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003039 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003040};
3041
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003042} // namespace art
3043
3044#endif // ART_COMPILER_OPTIMIZING_NODES_H_