blob: e19bfce9de38aab12cc64de5894978025dfbe256 [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
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100590template <typename T>
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700591class HUseListNode : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000592 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100593 HUseListNode(T* user, size_t index, HUseListNode* tail)
Dave Allison20dfc792014-06-16 20:44:29 -0700594 : user_(user), index_(index), tail_(tail) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000595
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000596 HUseListNode* GetTail() const { return tail_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100597 T* GetUser() const { return user_; }
598 size_t GetIndex() const { return index_; }
599
600 void SetTail(HUseListNode<T>* node) { tail_ = node; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000601
602 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100603 T* const user_;
604 const size_t index_;
605 HUseListNode<T>* tail_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000606
607 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
608};
609
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100610// Represents the side effects an instruction may have.
611class SideEffects : public ValueObject {
612 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100613 SideEffects() : flags_(0) {}
614
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100615 static SideEffects None() {
616 return SideEffects(0);
617 }
618
619 static SideEffects All() {
620 return SideEffects(ChangesSomething().flags_ | DependsOnSomething().flags_);
621 }
622
623 static SideEffects ChangesSomething() {
624 return SideEffects((1 << kFlagChangesCount) - 1);
625 }
626
627 static SideEffects DependsOnSomething() {
628 int count = kFlagDependsOnCount - kFlagChangesCount;
629 return SideEffects(((1 << count) - 1) << kFlagChangesCount);
630 }
631
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100632 SideEffects Union(SideEffects other) const {
633 return SideEffects(flags_ | other.flags_);
634 }
635
Roland Levillain72bceff2014-09-15 18:29:00 +0100636 bool HasSideEffects() const {
637 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
638 return (flags_ & all_bits_set) != 0;
639 }
640
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100641 bool HasAllSideEffects() const {
642 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
643 return all_bits_set == (flags_ & all_bits_set);
644 }
645
646 bool DependsOn(SideEffects other) const {
647 size_t depends_flags = other.ComputeDependsFlags();
648 return (flags_ & depends_flags) != 0;
649 }
650
651 bool HasDependencies() const {
652 int count = kFlagDependsOnCount - kFlagChangesCount;
653 size_t all_bits_set = (1 << count) - 1;
654 return ((flags_ >> kFlagChangesCount) & all_bits_set) != 0;
655 }
656
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100657 private:
658 static constexpr int kFlagChangesSomething = 0;
659 static constexpr int kFlagChangesCount = kFlagChangesSomething + 1;
660
661 static constexpr int kFlagDependsOnSomething = kFlagChangesCount;
662 static constexpr int kFlagDependsOnCount = kFlagDependsOnSomething + 1;
663
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100664 explicit SideEffects(size_t flags) : flags_(flags) {}
665
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100666 size_t ComputeDependsFlags() const {
667 return flags_ << kFlagChangesCount;
668 }
669
670 size_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100671};
672
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700673class HInstruction : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000674 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100675 explicit HInstruction(SideEffects side_effects)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000676 : previous_(nullptr),
677 next_(nullptr),
678 block_(nullptr),
679 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100680 ssa_index_(-1),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000681 uses_(nullptr),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100682 env_uses_(nullptr),
683 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100684 locations_(nullptr),
685 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100686 lifetime_position_(kNoLifetime),
687 side_effects_(side_effects) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000688
Dave Allison20dfc792014-06-16 20:44:29 -0700689 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000690
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100691#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100692 enum InstructionKind {
693 FOR_EACH_INSTRUCTION(DECLARE_KIND)
694 };
695#undef DECLARE_KIND
696
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000697 HInstruction* GetNext() const { return next_; }
698 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000699
Calin Juravle77520bc2015-01-12 18:45:46 +0000700 HInstruction* GetNextDisregardingMoves() const;
701 HInstruction* GetPreviousDisregardingMoves() const;
702
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000703 HBasicBlock* GetBlock() const { return block_; }
704 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100705 bool IsInBlock() const { return block_ != nullptr; }
706 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +0100707 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000708
Roland Levillain6b879dd2014-09-22 17:13:44 +0100709 virtual size_t InputCount() const = 0;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100710 virtual HInstruction* InputAt(size_t i) const = 0;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000711
712 virtual void Accept(HGraphVisitor* visitor) = 0;
713 virtual const char* DebugName() const = 0;
714
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100715 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100716 virtual void SetRawInputAt(size_t index, HInstruction* input) = 0;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100717
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100718 virtual bool NeedsEnvironment() const { return false; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100719 virtual bool IsControlFlow() const { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +0100720 virtual bool CanThrow() const { return false; }
Roland Levillain72bceff2014-09-15 18:29:00 +0100721 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100722
Calin Juravle77520bc2015-01-12 18:45:46 +0000723 virtual bool CanDoImplicitNullCheck() const { return false; }
724
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100725 void AddUseAt(HInstruction* user, size_t index) {
726 uses_ = new (block_->GetGraph()->GetArena()) HUseListNode<HInstruction>(user, index, uses_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000727 }
728
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100729 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +0100730 DCHECK(user != nullptr);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100731 env_uses_ = new (block_->GetGraph()->GetArena()) HUseListNode<HEnvironment>(
732 user, index, env_uses_);
733 }
734
735 void RemoveUser(HInstruction* user, size_t index);
Nicolas Geoffray724c9632014-09-22 12:27:27 +0100736 void RemoveEnvironmentUser(HEnvironment* user, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100737
738 HUseListNode<HInstruction>* GetUses() const { return uses_; }
739 HUseListNode<HEnvironment>* GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000740
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100741 bool HasUses() const { return uses_ != nullptr || env_uses_ != nullptr; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100742 bool HasEnvironmentUses() const { return env_uses_ != nullptr; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000743
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100744 size_t NumberOfUses() const {
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100745 // TODO: Optimize this method if it is used outside of the HGraphVisualizer.
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100746 size_t result = 0;
747 HUseListNode<HInstruction>* current = uses_;
748 while (current != nullptr) {
749 current = current->GetTail();
750 ++result;
751 }
752 return result;
753 }
754
Roland Levillain6c82d402014-10-13 16:10:27 +0100755 // Does this instruction strictly dominate `other_instruction`?
756 // Returns false if this instruction and `other_instruction` are the same.
757 // Aborts if this instruction and `other_instruction` are both phis.
758 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +0100759
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000760 int GetId() const { return id_; }
761 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000762
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100763 int GetSsaIndex() const { return ssa_index_; }
764 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
765 bool HasSsaIndex() const { return ssa_index_ != -1; }
766
767 bool HasEnvironment() const { return environment_ != nullptr; }
768 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100769 void SetEnvironment(HEnvironment* environment) { environment_ = environment; }
770
Nicolas Geoffray39468442014-09-02 15:17:15 +0100771 // Returns the number of entries in the environment. Typically, that is the
772 // number of dex registers in a method. It could be more in case of inlining.
773 size_t EnvironmentSize() const;
774
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000775 LocationSummary* GetLocations() const { return locations_; }
776 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000777
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100778 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100779 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100780
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000781 // Insert `this` instruction in `cursor`'s graph, just before `cursor`.
782 void InsertBefore(HInstruction* cursor);
783
Dave Allison20dfc792014-06-16 20:44:29 -0700784 bool HasOnlyOneUse() const {
785 return uses_ != nullptr && uses_->GetTail() == nullptr;
786 }
787
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100788#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +0100789 bool Is##type() const { return (As##type() != nullptr); } \
790 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000791 virtual H##type* As##type() { return nullptr; }
792
793 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
794#undef INSTRUCTION_TYPE_CHECK
795
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100796 // Returns whether the instruction can be moved within the graph.
797 virtual bool CanBeMoved() const { return false; }
798
799 // Returns whether the two instructions are of the same kind.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700800 virtual bool InstructionTypeEquals(HInstruction* other) const {
801 UNUSED(other);
802 return false;
803 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100804
805 // Returns whether any data encoded in the two instructions is equal.
806 // This method does not look at the inputs. Both instructions must be
807 // of the same type, otherwise the method has undefined behavior.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700808 virtual bool InstructionDataEquals(HInstruction* other) const {
809 UNUSED(other);
810 return false;
811 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100812
813 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +0000814 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100815 // 2) Their inputs are identical.
816 bool Equals(HInstruction* other) const;
817
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100818 virtual InstructionKind GetKind() const = 0;
819
820 virtual size_t ComputeHashCode() const {
821 size_t result = GetKind();
822 for (size_t i = 0, e = InputCount(); i < e; ++i) {
823 result = (result * 31) + InputAt(i)->GetId();
824 }
825 return result;
826 }
827
828 SideEffects GetSideEffects() const { return side_effects_; }
829
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100830 size_t GetLifetimePosition() const { return lifetime_position_; }
831 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
832 LiveInterval* GetLiveInterval() const { return live_interval_; }
833 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
834 bool HasLiveInterval() const { return live_interval_ != nullptr; }
835
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000836 private:
837 HInstruction* previous_;
838 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000839 HBasicBlock* block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000840
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000841 // An instruction gets an id when it is added to the graph.
842 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +0100843 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000844 int id_;
845
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100846 // When doing liveness analysis, instructions that have uses get an SSA index.
847 int ssa_index_;
848
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100849 // List of instructions that have this instruction as input.
850 HUseListNode<HInstruction>* uses_;
851
852 // List of environments that contain this instruction.
853 HUseListNode<HEnvironment>* env_uses_;
854
Nicolas Geoffray39468442014-09-02 15:17:15 +0100855 // The environment associated with this instruction. Not null if the instruction
856 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100857 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000858
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000859 // Set by the code generator.
860 LocationSummary* locations_;
861
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100862 // Set by the liveness analysis.
863 LiveInterval* live_interval_;
864
865 // Set by the liveness analysis, this is the position in a linear
866 // order of blocks where this instruction's live interval start.
867 size_t lifetime_position_;
868
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100869 const SideEffects side_effects_;
870
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000871 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000872 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100873 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000874
875 DISALLOW_COPY_AND_ASSIGN(HInstruction);
876};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700877std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000878
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100879template<typename T>
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000880class HUseIterator : public ValueObject {
881 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100882 explicit HUseIterator(HUseListNode<T>* uses) : current_(uses) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000883
884 bool Done() const { return current_ == nullptr; }
885
886 void Advance() {
887 DCHECK(!Done());
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000888 current_ = current_->GetTail();
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000889 }
890
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100891 HUseListNode<T>* Current() const {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000892 DCHECK(!Done());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100893 return current_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000894 }
895
896 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100897 HUseListNode<T>* current_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000898
899 friend class HValue;
900};
901
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100902// A HEnvironment object contains the values of virtual registers at a given location.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700903class HEnvironment : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100904 public:
905 HEnvironment(ArenaAllocator* arena, size_t number_of_vregs) : vregs_(arena, number_of_vregs) {
906 vregs_.SetSize(number_of_vregs);
907 for (size_t i = 0; i < number_of_vregs; i++) {
908 vregs_.Put(i, nullptr);
909 }
910 }
911
912 void Populate(const GrowableArray<HInstruction*>& env) {
913 for (size_t i = 0; i < env.Size(); i++) {
914 HInstruction* instruction = env.Get(i);
915 vregs_.Put(i, instruction);
916 if (instruction != nullptr) {
917 instruction->AddEnvUseAt(this, i);
918 }
919 }
920 }
921
922 void SetRawEnvAt(size_t index, HInstruction* instruction) {
923 vregs_.Put(index, instruction);
924 }
925
Nicolas Geoffray39468442014-09-02 15:17:15 +0100926 HInstruction* GetInstructionAt(size_t index) const {
927 return vregs_.Get(index);
928 }
929
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100930 GrowableArray<HInstruction*>* GetVRegs() {
931 return &vregs_;
932 }
933
Nicolas Geoffray39468442014-09-02 15:17:15 +0100934 size_t Size() const { return vregs_.Size(); }
935
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100936 private:
937 GrowableArray<HInstruction*> vregs_;
938
939 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
940};
941
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000942class HInputIterator : public ValueObject {
943 public:
Dave Allison20dfc792014-06-16 20:44:29 -0700944 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000945
946 bool Done() const { return index_ == instruction_->InputCount(); }
947 HInstruction* Current() const { return instruction_->InputAt(index_); }
948 void Advance() { index_++; }
949
950 private:
951 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100952 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000953
954 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
955};
956
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000957class HInstructionIterator : public ValueObject {
958 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100959 explicit HInstructionIterator(const HInstructionList& instructions)
960 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000961 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000962 }
963
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000964 bool Done() const { return instruction_ == nullptr; }
965 HInstruction* Current() const { return instruction_; }
966 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000967 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000968 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000969 }
970
971 private:
972 HInstruction* instruction_;
973 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100974
975 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000976};
977
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100978class HBackwardInstructionIterator : public ValueObject {
979 public:
980 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
981 : instruction_(instructions.last_instruction_) {
982 next_ = Done() ? nullptr : instruction_->GetPrevious();
983 }
984
985 bool Done() const { return instruction_ == nullptr; }
986 HInstruction* Current() const { return instruction_; }
987 void Advance() {
988 instruction_ = next_;
989 next_ = Done() ? nullptr : instruction_->GetPrevious();
990 }
991
992 private:
993 HInstruction* instruction_;
994 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100995
996 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100997};
998
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000999// An embedded container with N elements of type T. Used (with partial
1000// specialization for N=0) because embedded arrays cannot have size 0.
1001template<typename T, intptr_t N>
1002class EmbeddedArray {
1003 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001004 EmbeddedArray() : elements_() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001005
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001006 intptr_t GetLength() const { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001007
1008 const T& operator[](intptr_t i) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001009 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001010 return elements_[i];
1011 }
1012
1013 T& operator[](intptr_t i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001014 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001015 return elements_[i];
1016 }
1017
1018 const T& At(intptr_t i) const {
1019 return (*this)[i];
1020 }
1021
1022 void SetAt(intptr_t i, const T& val) {
1023 (*this)[i] = val;
1024 }
1025
1026 private:
1027 T elements_[N];
1028};
1029
1030template<typename T>
1031class EmbeddedArray<T, 0> {
1032 public:
1033 intptr_t length() const { return 0; }
1034 const T& operator[](intptr_t i) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001035 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001036 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001037 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001038 }
1039 T& operator[](intptr_t i) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001040 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001041 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001042 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001043 }
1044};
1045
1046template<intptr_t N>
1047class HTemplateInstruction: public HInstruction {
1048 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001049 HTemplateInstruction<N>(SideEffects side_effects)
1050 : HInstruction(side_effects), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07001051 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001052
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001053 virtual size_t InputCount() const { return N; }
1054 virtual HInstruction* InputAt(size_t i) const { return inputs_[i]; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001055
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001056 protected:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001057 virtual void SetRawInputAt(size_t i, HInstruction* instruction) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001058 inputs_[i] = instruction;
1059 }
1060
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001061 private:
1062 EmbeddedArray<HInstruction*, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001063
1064 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001065};
1066
Dave Allison20dfc792014-06-16 20:44:29 -07001067template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001068class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07001069 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001070 HExpression<N>(Primitive::Type type, SideEffects side_effects)
1071 : HTemplateInstruction<N>(side_effects), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07001072 virtual ~HExpression() {}
1073
1074 virtual Primitive::Type GetType() const { return type_; }
1075
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001076 protected:
1077 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07001078};
1079
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001080// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
1081// instruction that branches to the exit block.
1082class HReturnVoid : public HTemplateInstruction<0> {
1083 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001084 HReturnVoid() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001085
1086 virtual bool IsControlFlow() const { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001087
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001088 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001089
1090 private:
1091 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
1092};
1093
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001094// Represents dex's RETURN opcodes. A HReturn is a control flow
1095// instruction that branches to the exit block.
1096class HReturn : public HTemplateInstruction<1> {
1097 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001098 explicit HReturn(HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001099 SetRawInputAt(0, value);
1100 }
1101
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001102 virtual bool IsControlFlow() const { return true; }
1103
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001104 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001105
1106 private:
1107 DISALLOW_COPY_AND_ASSIGN(HReturn);
1108};
1109
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001110// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001111// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001112// exit block.
1113class HExit : public HTemplateInstruction<0> {
1114 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001115 HExit() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001116
1117 virtual bool IsControlFlow() const { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001118
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001119 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001120
1121 private:
1122 DISALLOW_COPY_AND_ASSIGN(HExit);
1123};
1124
1125// Jumps from one block to another.
1126class HGoto : public HTemplateInstruction<0> {
1127 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001128 HGoto() : HTemplateInstruction(SideEffects::None()) {}
1129
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001130 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001131
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001132 HBasicBlock* GetSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001133 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001134 }
1135
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001136 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001137
1138 private:
1139 DISALLOW_COPY_AND_ASSIGN(HGoto);
1140};
1141
Dave Allison20dfc792014-06-16 20:44:29 -07001142
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001143// Conditional branch. A block ending with an HIf instruction must have
1144// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001145class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001146 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001147 explicit HIf(HInstruction* input) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001148 SetRawInputAt(0, input);
1149 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001150
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001151 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001152
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001153 HBasicBlock* IfTrueSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001154 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001155 }
1156
1157 HBasicBlock* IfFalseSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001158 return GetBlock()->GetSuccessors().Get(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001159 }
1160
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001161 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001162
Dave Allison20dfc792014-06-16 20:44:29 -07001163 virtual bool IsIfInstruction() const { return true; }
1164
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001165 private:
1166 DISALLOW_COPY_AND_ASSIGN(HIf);
1167};
1168
Roland Levillain88cb1752014-10-20 16:36:47 +01001169class HUnaryOperation : public HExpression<1> {
1170 public:
1171 HUnaryOperation(Primitive::Type result_type, HInstruction* input)
1172 : HExpression(result_type, SideEffects::None()) {
1173 SetRawInputAt(0, input);
1174 }
1175
1176 HInstruction* GetInput() const { return InputAt(0); }
1177 Primitive::Type GetResultType() const { return GetType(); }
1178
1179 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001180 virtual bool InstructionDataEquals(HInstruction* other) const {
1181 UNUSED(other);
1182 return true;
1183 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001184
Roland Levillain9240d6a2014-10-20 16:47:04 +01001185 // Try to statically evaluate `operation` and return a HConstant
1186 // containing the result of this evaluation. If `operation` cannot
1187 // be evaluated as a constant, return nullptr.
1188 HConstant* TryStaticEvaluation() const;
1189
1190 // Apply this operation to `x`.
1191 virtual int32_t Evaluate(int32_t x) const = 0;
1192 virtual int64_t Evaluate(int64_t x) const = 0;
1193
Roland Levillain88cb1752014-10-20 16:36:47 +01001194 DECLARE_INSTRUCTION(UnaryOperation);
1195
1196 private:
1197 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
1198};
1199
Dave Allison20dfc792014-06-16 20:44:29 -07001200class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001201 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001202 HBinaryOperation(Primitive::Type result_type,
1203 HInstruction* left,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001204 HInstruction* right) : HExpression(result_type, SideEffects::None()) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001205 SetRawInputAt(0, left);
1206 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001207 }
1208
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001209 HInstruction* GetLeft() const { return InputAt(0); }
1210 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07001211 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001212
1213 virtual bool IsCommutative() { return false; }
1214
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001215 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001216 virtual bool InstructionDataEquals(HInstruction* other) const {
1217 UNUSED(other);
1218 return true;
1219 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001220
Roland Levillain9240d6a2014-10-20 16:47:04 +01001221 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01001222 // containing the result of this evaluation. If `operation` cannot
1223 // be evaluated as a constant, return nullptr.
Roland Levillain9240d6a2014-10-20 16:47:04 +01001224 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01001225
1226 // Apply this operation to `x` and `y`.
1227 virtual int32_t Evaluate(int32_t x, int32_t y) const = 0;
1228 virtual int64_t Evaluate(int64_t x, int64_t y) const = 0;
1229
Roland Levillainccc07a92014-09-16 14:48:16 +01001230 DECLARE_INSTRUCTION(BinaryOperation);
1231
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001232 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001233 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
1234};
1235
Dave Allison20dfc792014-06-16 20:44:29 -07001236class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001237 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001238 HCondition(HInstruction* first, HInstruction* second)
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001239 : HBinaryOperation(Primitive::kPrimBoolean, first, second),
1240 needs_materialization_(true) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001241
1242 virtual bool IsCommutative() { return true; }
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001243
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001244 bool NeedsMaterialization() const { return needs_materialization_; }
1245 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001246
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001247 // For code generation purposes, returns whether this instruction is just before
1248 // `if_`, and disregard moves in between.
1249 bool IsBeforeWhenDisregardMoves(HIf* if_) const;
1250
Dave Allison20dfc792014-06-16 20:44:29 -07001251 DECLARE_INSTRUCTION(Condition);
1252
1253 virtual IfCondition GetCondition() const = 0;
1254
1255 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001256 // For register allocation purposes, returns whether this instruction needs to be
1257 // materialized (that is, not just be in the processor flags).
1258 bool needs_materialization_;
1259
Dave Allison20dfc792014-06-16 20:44:29 -07001260 DISALLOW_COPY_AND_ASSIGN(HCondition);
1261};
1262
1263// Instruction to check if two inputs are equal to each other.
1264class HEqual : public HCondition {
1265 public:
1266 HEqual(HInstruction* first, HInstruction* second)
1267 : HCondition(first, second) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001268
Roland Levillain93445682014-10-06 19:24:02 +01001269 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1270 return x == y ? 1 : 0;
1271 }
1272 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1273 return x == y ? 1 : 0;
1274 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001275
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001276 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001277
Dave Allison20dfc792014-06-16 20:44:29 -07001278 virtual IfCondition GetCondition() const {
1279 return kCondEQ;
1280 }
1281
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001282 private:
1283 DISALLOW_COPY_AND_ASSIGN(HEqual);
1284};
1285
Dave Allison20dfc792014-06-16 20:44:29 -07001286class HNotEqual : public HCondition {
1287 public:
1288 HNotEqual(HInstruction* first, HInstruction* second)
1289 : HCondition(first, second) {}
1290
Roland Levillain93445682014-10-06 19:24:02 +01001291 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1292 return x != y ? 1 : 0;
1293 }
1294 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1295 return x != y ? 1 : 0;
1296 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001297
Dave Allison20dfc792014-06-16 20:44:29 -07001298 DECLARE_INSTRUCTION(NotEqual);
1299
1300 virtual IfCondition GetCondition() const {
1301 return kCondNE;
1302 }
1303
1304 private:
1305 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
1306};
1307
1308class HLessThan : public HCondition {
1309 public:
1310 HLessThan(HInstruction* first, HInstruction* second)
1311 : HCondition(first, second) {}
1312
Roland Levillain93445682014-10-06 19:24:02 +01001313 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1314 return x < y ? 1 : 0;
1315 }
1316 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1317 return x < y ? 1 : 0;
1318 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001319
Dave Allison20dfc792014-06-16 20:44:29 -07001320 DECLARE_INSTRUCTION(LessThan);
1321
1322 virtual IfCondition GetCondition() const {
1323 return kCondLT;
1324 }
1325
1326 private:
1327 DISALLOW_COPY_AND_ASSIGN(HLessThan);
1328};
1329
1330class HLessThanOrEqual : public HCondition {
1331 public:
1332 HLessThanOrEqual(HInstruction* first, HInstruction* second)
1333 : HCondition(first, second) {}
1334
Roland Levillain93445682014-10-06 19:24:02 +01001335 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1336 return x <= y ? 1 : 0;
1337 }
1338 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1339 return x <= y ? 1 : 0;
1340 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001341
Dave Allison20dfc792014-06-16 20:44:29 -07001342 DECLARE_INSTRUCTION(LessThanOrEqual);
1343
1344 virtual IfCondition GetCondition() const {
1345 return kCondLE;
1346 }
1347
1348 private:
1349 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
1350};
1351
1352class HGreaterThan : public HCondition {
1353 public:
1354 HGreaterThan(HInstruction* first, HInstruction* second)
1355 : HCondition(first, second) {}
1356
Roland Levillain93445682014-10-06 19:24:02 +01001357 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1358 return x > y ? 1 : 0;
1359 }
1360 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1361 return x > y ? 1 : 0;
1362 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001363
Dave Allison20dfc792014-06-16 20:44:29 -07001364 DECLARE_INSTRUCTION(GreaterThan);
1365
1366 virtual IfCondition GetCondition() const {
1367 return kCondGT;
1368 }
1369
1370 private:
1371 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
1372};
1373
1374class HGreaterThanOrEqual : public HCondition {
1375 public:
1376 HGreaterThanOrEqual(HInstruction* first, HInstruction* second)
1377 : HCondition(first, second) {}
1378
Roland Levillain93445682014-10-06 19:24:02 +01001379 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1380 return x >= y ? 1 : 0;
1381 }
1382 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1383 return x >= y ? 1 : 0;
1384 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001385
Dave Allison20dfc792014-06-16 20:44:29 -07001386 DECLARE_INSTRUCTION(GreaterThanOrEqual);
1387
1388 virtual IfCondition GetCondition() const {
1389 return kCondGE;
1390 }
1391
1392 private:
1393 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
1394};
1395
1396
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001397// Instruction to check how two inputs compare to each other.
1398// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
1399class HCompare : public HBinaryOperation {
1400 public:
Calin Juravleddb7df22014-11-25 20:56:51 +00001401 // The bias applies for floating point operations and indicates how NaN
1402 // comparisons are treated:
1403 enum Bias {
1404 kNoBias, // bias is not applicable (i.e. for long operation)
1405 kGtBias, // return 1 for NaN comparisons
1406 kLtBias, // return -1 for NaN comparisons
1407 };
1408
1409 HCompare(Primitive::Type type, HInstruction* first, HInstruction* second, Bias bias)
1410 : HBinaryOperation(Primitive::kPrimInt, first, second), bias_(bias) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001411 DCHECK_EQ(type, first->GetType());
1412 DCHECK_EQ(type, second->GetType());
1413 }
1414
Calin Juravleddb7df22014-11-25 20:56:51 +00001415 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001416 return
1417 x == y ? 0 :
1418 x > y ? 1 :
1419 -1;
1420 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001421
1422 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001423 return
1424 x == y ? 0 :
1425 x > y ? 1 :
1426 -1;
1427 }
1428
Calin Juravleddb7df22014-11-25 20:56:51 +00001429 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
1430 return bias_ == other->AsCompare()->bias_;
1431 }
1432
1433 bool IsGtBias() { return bias_ == kGtBias; }
1434
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001435 DECLARE_INSTRUCTION(Compare);
1436
1437 private:
Calin Juravleddb7df22014-11-25 20:56:51 +00001438 const Bias bias_;
1439
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001440 DISALLOW_COPY_AND_ASSIGN(HCompare);
1441};
1442
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001443// A local in the graph. Corresponds to a Dex register.
1444class HLocal : public HTemplateInstruction<0> {
1445 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001446 explicit HLocal(uint16_t reg_number)
1447 : HTemplateInstruction(SideEffects::None()), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001448
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001449 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001450
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001451 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001452
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001453 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001454 // The Dex register number.
1455 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001456
1457 DISALLOW_COPY_AND_ASSIGN(HLocal);
1458};
1459
1460// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07001461class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001462 public:
Roland Levillain5799fc02014-09-25 12:15:20 +01001463 HLoadLocal(HLocal* local, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001464 : HExpression(type, SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001465 SetRawInputAt(0, local);
1466 }
1467
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001468 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1469
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001470 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001471
1472 private:
1473 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
1474};
1475
1476// Store a value in a given local. This instruction has two inputs: the value
1477// and the local.
1478class HStoreLocal : public HTemplateInstruction<2> {
1479 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001480 HStoreLocal(HLocal* local, HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001481 SetRawInputAt(0, local);
1482 SetRawInputAt(1, value);
1483 }
1484
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001485 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1486
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001487 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001488
1489 private:
1490 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
1491};
1492
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001493class HConstant : public HExpression<0> {
1494 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001495 explicit HConstant(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
1496
1497 virtual bool CanBeMoved() const { return true; }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001498
1499 DECLARE_INSTRUCTION(Constant);
1500
1501 private:
1502 DISALLOW_COPY_AND_ASSIGN(HConstant);
1503};
1504
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001505class HFloatConstant : public HConstant {
1506 public:
1507 explicit HFloatConstant(float value) : HConstant(Primitive::kPrimFloat), value_(value) {}
1508
1509 float GetValue() const { return value_; }
1510
1511 virtual bool InstructionDataEquals(HInstruction* other) const {
1512 return bit_cast<float, int32_t>(other->AsFloatConstant()->value_) ==
1513 bit_cast<float, int32_t>(value_);
1514 }
1515
1516 virtual size_t ComputeHashCode() const { return static_cast<size_t>(GetValue()); }
1517
1518 DECLARE_INSTRUCTION(FloatConstant);
1519
1520 private:
1521 const float value_;
1522
1523 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
1524};
1525
1526class HDoubleConstant : public HConstant {
1527 public:
1528 explicit HDoubleConstant(double value) : HConstant(Primitive::kPrimDouble), value_(value) {}
1529
1530 double GetValue() const { return value_; }
1531
1532 virtual bool InstructionDataEquals(HInstruction* other) const {
1533 return bit_cast<double, int64_t>(other->AsDoubleConstant()->value_) ==
1534 bit_cast<double, int64_t>(value_);
1535 }
1536
1537 virtual size_t ComputeHashCode() const { return static_cast<size_t>(GetValue()); }
1538
1539 DECLARE_INSTRUCTION(DoubleConstant);
1540
1541 private:
1542 const double value_;
1543
1544 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
1545};
1546
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001547// Constants of the type int. Those can be from Dex instructions, or
1548// synthesized (for example with the if-eqz instruction).
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001549class HIntConstant : public HConstant {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001550 public:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001551 explicit HIntConstant(int32_t value) : HConstant(Primitive::kPrimInt), value_(value) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001552
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001553 int32_t GetValue() const { return value_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001554
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001555 virtual bool InstructionDataEquals(HInstruction* other) const {
1556 return other->AsIntConstant()->value_ == value_;
1557 }
1558
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001559 virtual size_t ComputeHashCode() const { return GetValue(); }
1560
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001561 DECLARE_INSTRUCTION(IntConstant);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001562
1563 private:
1564 const int32_t value_;
1565
1566 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
1567};
1568
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001569class HLongConstant : public HConstant {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001570 public:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001571 explicit HLongConstant(int64_t value) : HConstant(Primitive::kPrimLong), value_(value) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001572
1573 int64_t GetValue() const { return value_; }
1574
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001575 virtual bool InstructionDataEquals(HInstruction* other) const {
1576 return other->AsLongConstant()->value_ == value_;
1577 }
1578
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001579 virtual size_t ComputeHashCode() const { return static_cast<size_t>(GetValue()); }
1580
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001581 DECLARE_INSTRUCTION(LongConstant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001582
1583 private:
1584 const int64_t value_;
1585
1586 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
1587};
1588
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001589enum class Intrinsics {
1590#define OPTIMIZING_INTRINSICS(Name, IsStatic) k ## Name,
1591#include "intrinsics_list.h"
1592 kNone,
1593 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
1594#undef INTRINSICS_LIST
1595#undef OPTIMIZING_INTRINSICS
1596};
1597std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
1598
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001599class HInvoke : public HInstruction {
1600 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001601 virtual size_t InputCount() const { return inputs_.Size(); }
1602 virtual HInstruction* InputAt(size_t i) const { return inputs_.Get(i); }
1603
1604 // Runtime needs to walk the stack, so Dex -> Dex calls need to
1605 // know their environment.
Calin Juravle77520bc2015-01-12 18:45:46 +00001606 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001607
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001608 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001609 SetRawInputAt(index, argument);
1610 }
1611
1612 virtual void SetRawInputAt(size_t index, HInstruction* input) {
1613 inputs_.Put(index, input);
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001614 }
1615
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001616 virtual Primitive::Type GetType() const { return return_type_; }
1617
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001618 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001619
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001620 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
1621
1622 Intrinsics GetIntrinsic() {
1623 return intrinsic_;
1624 }
1625
1626 void SetIntrinsic(Intrinsics intrinsic) {
1627 intrinsic_ = intrinsic;
1628 }
1629
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001630 DECLARE_INSTRUCTION(Invoke);
1631
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001632 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001633 HInvoke(ArenaAllocator* arena,
1634 uint32_t number_of_arguments,
1635 Primitive::Type return_type,
1636 uint32_t dex_pc,
1637 uint32_t dex_method_index)
1638 : HInstruction(SideEffects::All()),
1639 inputs_(arena, number_of_arguments),
1640 return_type_(return_type),
1641 dex_pc_(dex_pc),
1642 dex_method_index_(dex_method_index),
1643 intrinsic_(Intrinsics::kNone) {
1644 inputs_.SetSize(number_of_arguments);
1645 }
1646
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001647 GrowableArray<HInstruction*> inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001648 const Primitive::Type return_type_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001649 const uint32_t dex_pc_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001650 const uint32_t dex_method_index_;
1651 Intrinsics intrinsic_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001652
1653 private:
1654 DISALLOW_COPY_AND_ASSIGN(HInvoke);
1655};
1656
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001657class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001658 public:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001659 HInvokeStaticOrDirect(ArenaAllocator* arena,
1660 uint32_t number_of_arguments,
1661 Primitive::Type return_type,
1662 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001663 uint32_t dex_method_index,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001664 InvokeType invoke_type)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001665 : HInvoke(arena, number_of_arguments, return_type, dex_pc, dex_method_index),
Nicolas Geoffray4e44c822014-12-17 12:25:12 +00001666 invoke_type_(invoke_type) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001667
Calin Juravle77520bc2015-01-12 18:45:46 +00001668 bool CanDoImplicitNullCheck() const OVERRIDE {
1669 // We access the method via the dex cache so we can't do an implicit null check.
1670 // TODO: for intrinsics we can generate implicit null checks.
1671 return false;
1672 }
1673
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001674 InvokeType GetInvokeType() const { return invoke_type_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001675
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001676 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001677
1678 private:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001679 const InvokeType invoke_type_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001680
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001681 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001682};
1683
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001684class HInvokeVirtual : public HInvoke {
1685 public:
1686 HInvokeVirtual(ArenaAllocator* arena,
1687 uint32_t number_of_arguments,
1688 Primitive::Type return_type,
1689 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001690 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001691 uint32_t vtable_index)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001692 : HInvoke(arena, number_of_arguments, return_type, dex_pc, dex_method_index),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001693 vtable_index_(vtable_index) {}
1694
Calin Juravle77520bc2015-01-12 18:45:46 +00001695 bool CanDoImplicitNullCheck() const OVERRIDE {
1696 // TODO: Add implicit null checks in intrinsics.
1697 return !GetLocations()->Intrinsified();
1698 }
1699
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001700 uint32_t GetVTableIndex() const { return vtable_index_; }
1701
1702 DECLARE_INSTRUCTION(InvokeVirtual);
1703
1704 private:
1705 const uint32_t vtable_index_;
1706
1707 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
1708};
1709
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001710class HInvokeInterface : public HInvoke {
1711 public:
1712 HInvokeInterface(ArenaAllocator* arena,
1713 uint32_t number_of_arguments,
1714 Primitive::Type return_type,
1715 uint32_t dex_pc,
1716 uint32_t dex_method_index,
1717 uint32_t imt_index)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001718 : HInvoke(arena, number_of_arguments, return_type, dex_pc, dex_method_index),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001719 imt_index_(imt_index) {}
1720
Calin Juravle77520bc2015-01-12 18:45:46 +00001721 bool CanDoImplicitNullCheck() const OVERRIDE {
1722 // TODO: Add implicit null checks in intrinsics.
1723 return !GetLocations()->Intrinsified();
1724 }
1725
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001726 uint32_t GetImtIndex() const { return imt_index_; }
1727 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
1728
1729 DECLARE_INSTRUCTION(InvokeInterface);
1730
1731 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001732 const uint32_t imt_index_;
1733
1734 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
1735};
1736
Dave Allison20dfc792014-06-16 20:44:29 -07001737class HNewInstance : public HExpression<0> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001738 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001739 HNewInstance(uint32_t dex_pc, uint16_t type_index)
1740 : HExpression(Primitive::kPrimNot, SideEffects::None()),
1741 dex_pc_(dex_pc),
1742 type_index_(type_index) {}
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001743
1744 uint32_t GetDexPc() const { return dex_pc_; }
1745 uint16_t GetTypeIndex() const { return type_index_; }
1746
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001747 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00001748 bool NeedsEnvironment() const OVERRIDE { return true; }
1749 // It may throw when called on:
1750 // - interfaces
1751 // - abstract/innaccessible/unknown classes
1752 // TODO: optimize when possible.
1753 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001754
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001755 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001756
1757 private:
1758 const uint32_t dex_pc_;
1759 const uint16_t type_index_;
1760
1761 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
1762};
1763
Roland Levillain88cb1752014-10-20 16:36:47 +01001764class HNeg : public HUnaryOperation {
1765 public:
1766 explicit HNeg(Primitive::Type result_type, HInstruction* input)
1767 : HUnaryOperation(result_type, input) {}
1768
Roland Levillain9240d6a2014-10-20 16:47:04 +01001769 virtual int32_t Evaluate(int32_t x) const OVERRIDE { return -x; }
1770 virtual int64_t Evaluate(int64_t x) const OVERRIDE { return -x; }
1771
Roland Levillain88cb1752014-10-20 16:36:47 +01001772 DECLARE_INSTRUCTION(Neg);
1773
1774 private:
1775 DISALLOW_COPY_AND_ASSIGN(HNeg);
1776};
1777
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001778class HNewArray : public HExpression<1> {
1779 public:
1780 HNewArray(HInstruction* length, uint32_t dex_pc, uint16_t type_index)
1781 : HExpression(Primitive::kPrimNot, SideEffects::None()),
1782 dex_pc_(dex_pc),
1783 type_index_(type_index) {
1784 SetRawInputAt(0, length);
1785 }
1786
1787 uint32_t GetDexPc() const { return dex_pc_; }
1788 uint16_t GetTypeIndex() const { return type_index_; }
1789
1790 // Calls runtime so needs an environment.
1791 virtual bool NeedsEnvironment() const { return true; }
1792
1793 DECLARE_INSTRUCTION(NewArray);
1794
1795 private:
1796 const uint32_t dex_pc_;
1797 const uint16_t type_index_;
1798
1799 DISALLOW_COPY_AND_ASSIGN(HNewArray);
1800};
1801
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001802class HAdd : public HBinaryOperation {
1803 public:
1804 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1805 : HBinaryOperation(result_type, left, right) {}
1806
1807 virtual bool IsCommutative() { return true; }
1808
Roland Levillain93445682014-10-06 19:24:02 +01001809 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1810 return x + y;
1811 }
1812 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1813 return x + y;
1814 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001815
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001816 DECLARE_INSTRUCTION(Add);
1817
1818 private:
1819 DISALLOW_COPY_AND_ASSIGN(HAdd);
1820};
1821
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001822class HSub : public HBinaryOperation {
1823 public:
1824 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1825 : HBinaryOperation(result_type, left, right) {}
1826
Roland Levillain93445682014-10-06 19:24:02 +01001827 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1828 return x - y;
1829 }
1830 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1831 return x - y;
1832 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001833
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001834 DECLARE_INSTRUCTION(Sub);
1835
1836 private:
1837 DISALLOW_COPY_AND_ASSIGN(HSub);
1838};
1839
Calin Juravle34bacdf2014-10-07 20:23:36 +01001840class HMul : public HBinaryOperation {
1841 public:
1842 HMul(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1843 : HBinaryOperation(result_type, left, right) {}
1844
1845 virtual bool IsCommutative() { return true; }
1846
1847 virtual int32_t Evaluate(int32_t x, int32_t y) const { return x * y; }
1848 virtual int64_t Evaluate(int64_t x, int64_t y) const { return x * y; }
1849
1850 DECLARE_INSTRUCTION(Mul);
1851
1852 private:
1853 DISALLOW_COPY_AND_ASSIGN(HMul);
1854};
1855
Calin Juravle7c4954d2014-10-28 16:57:40 +00001856class HDiv : public HBinaryOperation {
1857 public:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001858 HDiv(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
1859 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00001860
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00001861 virtual int32_t Evaluate(int32_t x, int32_t y) const {
1862 // Our graph structure ensures we never have 0 for `y` during constant folding.
1863 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00001864 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00001865 return (y == -1) ? -x : x / y;
1866 }
Calin Juravlebacfec32014-11-14 15:54:36 +00001867
1868 virtual int64_t Evaluate(int64_t x, int64_t y) const {
1869 DCHECK_NE(y, 0);
1870 // Special case -1 to avoid getting a SIGFPE on x86(_64).
1871 return (y == -1) ? -x : x / y;
1872 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00001873
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001874 uint32_t GetDexPc() const { return dex_pc_; }
1875
Calin Juravle7c4954d2014-10-28 16:57:40 +00001876 DECLARE_INSTRUCTION(Div);
1877
1878 private:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001879 const uint32_t dex_pc_;
1880
Calin Juravle7c4954d2014-10-28 16:57:40 +00001881 DISALLOW_COPY_AND_ASSIGN(HDiv);
1882};
1883
Calin Juravlebacfec32014-11-14 15:54:36 +00001884class HRem : public HBinaryOperation {
1885 public:
1886 HRem(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
1887 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
1888
1889 virtual int32_t Evaluate(int32_t x, int32_t y) const {
1890 DCHECK_NE(y, 0);
1891 // Special case -1 to avoid getting a SIGFPE on x86(_64).
1892 return (y == -1) ? 0 : x % y;
1893 }
1894
1895 virtual int64_t Evaluate(int64_t x, int64_t y) const {
1896 DCHECK_NE(y, 0);
1897 // Special case -1 to avoid getting a SIGFPE on x86(_64).
1898 return (y == -1) ? 0 : x % y;
1899 }
1900
1901 uint32_t GetDexPc() const { return dex_pc_; }
1902
1903 DECLARE_INSTRUCTION(Rem);
1904
1905 private:
1906 const uint32_t dex_pc_;
1907
1908 DISALLOW_COPY_AND_ASSIGN(HRem);
1909};
1910
Calin Juravled0d48522014-11-04 16:40:20 +00001911class HDivZeroCheck : public HExpression<1> {
1912 public:
1913 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
1914 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
1915 SetRawInputAt(0, value);
1916 }
1917
1918 bool CanBeMoved() const OVERRIDE { return true; }
1919
1920 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
1921 UNUSED(other);
1922 return true;
1923 }
1924
1925 bool NeedsEnvironment() const OVERRIDE { return true; }
1926 bool CanThrow() const OVERRIDE { return true; }
1927
1928 uint32_t GetDexPc() const { return dex_pc_; }
1929
1930 DECLARE_INSTRUCTION(DivZeroCheck);
1931
1932 private:
1933 const uint32_t dex_pc_;
1934
1935 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
1936};
1937
Calin Juravle9aec02f2014-11-18 23:06:35 +00001938class HShl : public HBinaryOperation {
1939 public:
1940 HShl(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1941 : HBinaryOperation(result_type, left, right) {}
1942
1943 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x << (y & kMaxIntShiftValue); }
1944 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x << (y & kMaxLongShiftValue); }
1945
1946 DECLARE_INSTRUCTION(Shl);
1947
1948 private:
1949 DISALLOW_COPY_AND_ASSIGN(HShl);
1950};
1951
1952class HShr : public HBinaryOperation {
1953 public:
1954 HShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1955 : HBinaryOperation(result_type, left, right) {}
1956
1957 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x >> (y & kMaxIntShiftValue); }
1958 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x >> (y & kMaxLongShiftValue); }
1959
1960 DECLARE_INSTRUCTION(Shr);
1961
1962 private:
1963 DISALLOW_COPY_AND_ASSIGN(HShr);
1964};
1965
1966class HUShr : public HBinaryOperation {
1967 public:
1968 HUShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1969 : HBinaryOperation(result_type, left, right) {}
1970
1971 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1972 uint32_t ux = static_cast<uint32_t>(x);
1973 uint32_t uy = static_cast<uint32_t>(y) & kMaxIntShiftValue;
1974 return static_cast<int32_t>(ux >> uy);
1975 }
1976
1977 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1978 uint64_t ux = static_cast<uint64_t>(x);
1979 uint64_t uy = static_cast<uint64_t>(y) & kMaxLongShiftValue;
1980 return static_cast<int64_t>(ux >> uy);
1981 }
1982
1983 DECLARE_INSTRUCTION(UShr);
1984
1985 private:
1986 DISALLOW_COPY_AND_ASSIGN(HUShr);
1987};
1988
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001989class HAnd : public HBinaryOperation {
1990 public:
1991 HAnd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1992 : HBinaryOperation(result_type, left, right) {}
1993
1994 bool IsCommutative() OVERRIDE { return true; }
1995
1996 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x & y; }
1997 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x & y; }
1998
1999 DECLARE_INSTRUCTION(And);
2000
2001 private:
2002 DISALLOW_COPY_AND_ASSIGN(HAnd);
2003};
2004
2005class HOr : public HBinaryOperation {
2006 public:
2007 HOr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2008 : HBinaryOperation(result_type, left, right) {}
2009
2010 bool IsCommutative() OVERRIDE { return true; }
2011
2012 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x | y; }
2013 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x | y; }
2014
2015 DECLARE_INSTRUCTION(Or);
2016
2017 private:
2018 DISALLOW_COPY_AND_ASSIGN(HOr);
2019};
2020
2021class HXor : public HBinaryOperation {
2022 public:
2023 HXor(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2024 : HBinaryOperation(result_type, left, right) {}
2025
2026 bool IsCommutative() OVERRIDE { return true; }
2027
2028 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x ^ y; }
2029 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x ^ y; }
2030
2031 DECLARE_INSTRUCTION(Xor);
2032
2033 private:
2034 DISALLOW_COPY_AND_ASSIGN(HXor);
2035};
2036
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002037// The value of a parameter in this method. Its location depends on
2038// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07002039class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002040 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002041 HParameterValue(uint8_t index, Primitive::Type parameter_type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002042 : HExpression(parameter_type, SideEffects::None()), index_(index) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002043
2044 uint8_t GetIndex() const { return index_; }
2045
2046 DECLARE_INSTRUCTION(ParameterValue);
2047
2048 private:
2049 // The index of this parameter in the parameters list. Must be less
2050 // than HGraph::number_of_in_vregs_;
2051 const uint8_t index_;
2052
2053 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
2054};
2055
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002056class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002057 public:
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002058 explicit HNot(Primitive::Type result_type, HInstruction* input)
2059 : HUnaryOperation(result_type, input) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002060
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002061 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002062 virtual bool InstructionDataEquals(HInstruction* other) const {
2063 UNUSED(other);
2064 return true;
2065 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002066
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002067 virtual int32_t Evaluate(int32_t x) const OVERRIDE { return ~x; }
2068 virtual int64_t Evaluate(int64_t x) const OVERRIDE { return ~x; }
2069
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002070 DECLARE_INSTRUCTION(Not);
2071
2072 private:
2073 DISALLOW_COPY_AND_ASSIGN(HNot);
2074};
2075
Roland Levillaindff1f282014-11-05 14:15:05 +00002076class HTypeConversion : public HExpression<1> {
2077 public:
2078 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00002079 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
2080 : HExpression(result_type, SideEffects::None()), dex_pc_(dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002081 SetRawInputAt(0, input);
2082 DCHECK_NE(input->GetType(), result_type);
2083 }
2084
2085 HInstruction* GetInput() const { return InputAt(0); }
2086 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
2087 Primitive::Type GetResultType() const { return GetType(); }
2088
Roland Levillain624279f2014-12-04 11:54:28 +00002089 // Required by the x86 and ARM code generators when producing calls
2090 // to the runtime.
2091 uint32_t GetDexPc() const { return dex_pc_; }
2092
Roland Levillaindff1f282014-11-05 14:15:05 +00002093 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00002094 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00002095
2096 DECLARE_INSTRUCTION(TypeConversion);
2097
2098 private:
Roland Levillain624279f2014-12-04 11:54:28 +00002099 const uint32_t dex_pc_;
2100
Roland Levillaindff1f282014-11-05 14:15:05 +00002101 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
2102};
2103
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002104class HPhi : public HInstruction {
2105 public:
2106 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002107 : HInstruction(SideEffects::None()),
2108 inputs_(arena, number_of_inputs),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002109 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002110 type_(type),
2111 is_live_(false) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002112 inputs_.SetSize(number_of_inputs);
2113 }
2114
2115 virtual size_t InputCount() const { return inputs_.Size(); }
2116 virtual HInstruction* InputAt(size_t i) const { return inputs_.Get(i); }
2117
2118 virtual void SetRawInputAt(size_t index, HInstruction* input) {
2119 inputs_.Put(index, input);
2120 }
2121
2122 void AddInput(HInstruction* input);
2123
2124 virtual Primitive::Type GetType() const { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002125 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002126
2127 uint32_t GetRegNumber() const { return reg_number_; }
2128
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002129 void SetDead() { is_live_ = false; }
2130 void SetLive() { is_live_ = true; }
2131 bool IsDead() const { return !is_live_; }
2132 bool IsLive() const { return is_live_; }
2133
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002134 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002135
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002136 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002137 GrowableArray<HInstruction*> inputs_;
2138 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002139 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002140 bool is_live_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002141
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002142 DISALLOW_COPY_AND_ASSIGN(HPhi);
2143};
2144
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002145class HNullCheck : public HExpression<1> {
2146 public:
2147 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002148 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002149 SetRawInputAt(0, value);
2150 }
2151
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002152 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002153 virtual bool InstructionDataEquals(HInstruction* other) const {
2154 UNUSED(other);
2155 return true;
2156 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002157
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002158 virtual bool NeedsEnvironment() const { return true; }
2159
Roland Levillaine161a2a2014-10-03 12:45:18 +01002160 virtual bool CanThrow() const { return true; }
2161
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002162 uint32_t GetDexPc() const { return dex_pc_; }
2163
2164 DECLARE_INSTRUCTION(NullCheck);
2165
2166 private:
2167 const uint32_t dex_pc_;
2168
2169 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
2170};
2171
2172class FieldInfo : public ValueObject {
2173 public:
Calin Juravle52c48962014-12-16 17:02:57 +00002174 FieldInfo(MemberOffset field_offset, Primitive::Type field_type, bool is_volatile)
2175 : field_offset_(field_offset), field_type_(field_type), is_volatile_(is_volatile) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002176
2177 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002178 Primitive::Type GetFieldType() const { return field_type_; }
Calin Juravle52c48962014-12-16 17:02:57 +00002179 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002180
2181 private:
2182 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01002183 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00002184 const bool is_volatile_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002185};
2186
2187class HInstanceFieldGet : public HExpression<1> {
2188 public:
2189 HInstanceFieldGet(HInstruction* value,
2190 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00002191 MemberOffset field_offset,
2192 bool is_volatile)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002193 : HExpression(field_type, SideEffects::DependsOnSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00002194 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002195 SetRawInputAt(0, value);
2196 }
2197
Calin Juravle10c9cbe2014-12-19 10:50:19 +00002198 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002199
2200 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2201 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
2202 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002203 }
2204
Calin Juravle77520bc2015-01-12 18:45:46 +00002205 bool CanDoImplicitNullCheck() const OVERRIDE {
2206 return GetFieldOffset().Uint32Value() < kPageSize;
2207 }
2208
2209 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002210 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
2211 }
2212
Calin Juravle52c48962014-12-16 17:02:57 +00002213 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002214 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002215 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002216 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002217
2218 DECLARE_INSTRUCTION(InstanceFieldGet);
2219
2220 private:
2221 const FieldInfo field_info_;
2222
2223 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
2224};
2225
2226class HInstanceFieldSet : public HTemplateInstruction<2> {
2227 public:
2228 HInstanceFieldSet(HInstruction* object,
2229 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01002230 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00002231 MemberOffset field_offset,
2232 bool is_volatile)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002233 : HTemplateInstruction(SideEffects::ChangesSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00002234 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002235 SetRawInputAt(0, object);
2236 SetRawInputAt(1, value);
2237 }
2238
Calin Juravle77520bc2015-01-12 18:45:46 +00002239 bool CanDoImplicitNullCheck() const OVERRIDE {
2240 return GetFieldOffset().Uint32Value() < kPageSize;
2241 }
2242
Calin Juravle52c48962014-12-16 17:02:57 +00002243 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002244 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002245 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002246 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002247 HInstruction* GetValue() const { return InputAt(1); }
2248
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002249 DECLARE_INSTRUCTION(InstanceFieldSet);
2250
2251 private:
2252 const FieldInfo field_info_;
2253
2254 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
2255};
2256
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002257class HArrayGet : public HExpression<2> {
2258 public:
2259 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002260 : HExpression(type, SideEffects::DependsOnSomething()) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002261 SetRawInputAt(0, array);
2262 SetRawInputAt(1, index);
2263 }
2264
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002265 bool CanBeMoved() const OVERRIDE { return true; }
2266 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002267 UNUSED(other);
2268 return true;
2269 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002270 bool CanDoImplicitNullCheck() const OVERRIDE {
2271 // TODO: We can be smarter here.
2272 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
2273 // which generates the implicit null check. There are cases when these can be removed
2274 // to produce better code. If we ever add optimizations to do so we should allow an
2275 // implicit check here (as long as the address falls in the first page).
2276 return false;
2277 }
2278
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002279 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002280
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002281 HInstruction* GetArray() const { return InputAt(0); }
2282 HInstruction* GetIndex() const { return InputAt(1); }
2283
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002284 DECLARE_INSTRUCTION(ArrayGet);
2285
2286 private:
2287 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
2288};
2289
2290class HArraySet : public HTemplateInstruction<3> {
2291 public:
2292 HArraySet(HInstruction* array,
2293 HInstruction* index,
2294 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002295 Primitive::Type expected_component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002296 uint32_t dex_pc)
2297 : HTemplateInstruction(SideEffects::ChangesSomething()),
2298 dex_pc_(dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002299 expected_component_type_(expected_component_type),
2300 needs_type_check_(value->GetType() == Primitive::kPrimNot) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002301 SetRawInputAt(0, array);
2302 SetRawInputAt(1, index);
2303 SetRawInputAt(2, value);
2304 }
2305
Calin Juravle77520bc2015-01-12 18:45:46 +00002306 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002307 // We currently always call a runtime method to catch array store
2308 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002309 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002310 }
2311
Calin Juravle77520bc2015-01-12 18:45:46 +00002312 bool CanDoImplicitNullCheck() const OVERRIDE {
2313 // TODO: Same as for ArrayGet.
2314 return false;
2315 }
2316
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002317 void ClearNeedsTypeCheck() {
2318 needs_type_check_ = false;
2319 }
2320
2321 bool NeedsTypeCheck() const { return needs_type_check_; }
2322
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002323 uint32_t GetDexPc() const { return dex_pc_; }
2324
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002325 HInstruction* GetArray() const { return InputAt(0); }
2326 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002327 HInstruction* GetValue() const { return InputAt(2); }
2328
2329 Primitive::Type GetComponentType() const {
2330 // The Dex format does not type floating point index operations. Since the
2331 // `expected_component_type_` is set during building and can therefore not
2332 // be correct, we also check what is the value type. If it is a floating
2333 // point type, we must use that type.
2334 Primitive::Type value_type = GetValue()->GetType();
2335 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
2336 ? value_type
2337 : expected_component_type_;
2338 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002339
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002340 DECLARE_INSTRUCTION(ArraySet);
2341
2342 private:
2343 const uint32_t dex_pc_;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002344 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002345 bool needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002346
2347 DISALLOW_COPY_AND_ASSIGN(HArraySet);
2348};
2349
2350class HArrayLength : public HExpression<1> {
2351 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002352 explicit HArrayLength(HInstruction* array)
2353 : HExpression(Primitive::kPrimInt, SideEffects::None()) {
2354 // Note that arrays do not change length, so the instruction does not
2355 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002356 SetRawInputAt(0, array);
2357 }
2358
Calin Juravle77520bc2015-01-12 18:45:46 +00002359 bool CanBeMoved() const OVERRIDE { return true; }
2360 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002361 UNUSED(other);
2362 return true;
2363 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002364 bool CanDoImplicitNullCheck() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002365
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002366 DECLARE_INSTRUCTION(ArrayLength);
2367
2368 private:
2369 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
2370};
2371
2372class HBoundsCheck : public HExpression<2> {
2373 public:
2374 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002375 : HExpression(index->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002376 DCHECK(index->GetType() == Primitive::kPrimInt);
2377 SetRawInputAt(0, index);
2378 SetRawInputAt(1, length);
2379 }
2380
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002381 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002382 virtual bool InstructionDataEquals(HInstruction* other) const {
2383 UNUSED(other);
2384 return true;
2385 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002386
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002387 virtual bool NeedsEnvironment() const { return true; }
2388
Roland Levillaine161a2a2014-10-03 12:45:18 +01002389 virtual bool CanThrow() const { return true; }
2390
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002391 uint32_t GetDexPc() const { return dex_pc_; }
2392
2393 DECLARE_INSTRUCTION(BoundsCheck);
2394
2395 private:
2396 const uint32_t dex_pc_;
2397
2398 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
2399};
2400
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002401/**
2402 * Some DEX instructions are folded into multiple HInstructions that need
2403 * to stay live until the last HInstruction. This class
2404 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00002405 * HInstruction stays live. `index` represents the stack location index of the
2406 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002407 */
2408class HTemporary : public HTemplateInstruction<0> {
2409 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002410 explicit HTemporary(size_t index) : HTemplateInstruction(SideEffects::None()), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002411
2412 size_t GetIndex() const { return index_; }
2413
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00002414 Primitive::Type GetType() const OVERRIDE {
2415 // The previous instruction is the one that will be stored in the temporary location.
2416 DCHECK(GetPrevious() != nullptr);
2417 return GetPrevious()->GetType();
2418 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00002419
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002420 DECLARE_INSTRUCTION(Temporary);
2421
2422 private:
2423 const size_t index_;
2424
2425 DISALLOW_COPY_AND_ASSIGN(HTemporary);
2426};
2427
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002428class HSuspendCheck : public HTemplateInstruction<0> {
2429 public:
2430 explicit HSuspendCheck(uint32_t dex_pc)
Nicolas Geoffray9ebc72c2014-09-25 16:33:42 +01002431 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002432
2433 virtual bool NeedsEnvironment() const {
2434 return true;
2435 }
2436
2437 uint32_t GetDexPc() const { return dex_pc_; }
2438
2439 DECLARE_INSTRUCTION(SuspendCheck);
2440
2441 private:
2442 const uint32_t dex_pc_;
2443
2444 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
2445};
2446
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002447/**
2448 * Instruction to load a Class object.
2449 */
2450class HLoadClass : public HExpression<0> {
2451 public:
2452 HLoadClass(uint16_t type_index,
2453 bool is_referrers_class,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002454 uint32_t dex_pc)
2455 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2456 type_index_(type_index),
2457 is_referrers_class_(is_referrers_class),
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002458 dex_pc_(dex_pc),
2459 generate_clinit_check_(false) {}
2460
2461 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002462
2463 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2464 return other->AsLoadClass()->type_index_ == type_index_;
2465 }
2466
2467 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
2468
2469 uint32_t GetDexPc() const { return dex_pc_; }
2470 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002471 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002472
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002473 bool NeedsEnvironment() const OVERRIDE {
2474 // Will call runtime and load the class if the class is not loaded yet.
2475 // TODO: finer grain decision.
2476 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002477 }
2478
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002479 bool MustGenerateClinitCheck() const {
2480 return generate_clinit_check_;
2481 }
2482
2483 void SetMustGenerateClinitCheck() {
2484 generate_clinit_check_ = true;
2485 }
2486
2487 bool CanCallRuntime() const {
2488 return MustGenerateClinitCheck() || !is_referrers_class_;
2489 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002490
2491 DECLARE_INSTRUCTION(LoadClass);
2492
2493 private:
2494 const uint16_t type_index_;
2495 const bool is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002496 const uint32_t dex_pc_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002497 // Whether this instruction must generate the initialization check.
2498 // Used for code generation.
2499 bool generate_clinit_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002500
2501 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
2502};
2503
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002504class HLoadString : public HExpression<0> {
2505 public:
2506 HLoadString(uint32_t string_index, uint32_t dex_pc)
2507 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2508 string_index_(string_index),
2509 dex_pc_(dex_pc) {}
2510
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002511 bool CanBeMoved() const OVERRIDE { return true; }
2512
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002513 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2514 return other->AsLoadString()->string_index_ == string_index_;
2515 }
2516
2517 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
2518
2519 uint32_t GetDexPc() const { return dex_pc_; }
2520 uint32_t GetStringIndex() const { return string_index_; }
2521
2522 // TODO: Can we deopt or debug when we resolve a string?
2523 bool NeedsEnvironment() const OVERRIDE { return false; }
2524
2525 DECLARE_INSTRUCTION(LoadString);
2526
2527 private:
2528 const uint32_t string_index_;
2529 const uint32_t dex_pc_;
2530
2531 DISALLOW_COPY_AND_ASSIGN(HLoadString);
2532};
2533
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002534// TODO: Pass this check to HInvokeStaticOrDirect nodes.
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002535/**
2536 * Performs an initialization check on its Class object input.
2537 */
2538class HClinitCheck : public HExpression<1> {
2539 public:
2540 explicit HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
2541 : HExpression(Primitive::kPrimNot, SideEffects::All()),
2542 dex_pc_(dex_pc) {
2543 SetRawInputAt(0, constant);
2544 }
2545
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002546 bool CanBeMoved() const OVERRIDE { return true; }
2547 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2548 UNUSED(other);
2549 return true;
2550 }
2551
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002552 bool NeedsEnvironment() const OVERRIDE {
2553 // May call runtime to initialize the class.
2554 return true;
2555 }
2556
2557 uint32_t GetDexPc() const { return dex_pc_; }
2558
2559 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
2560
2561 DECLARE_INSTRUCTION(ClinitCheck);
2562
2563 private:
2564 const uint32_t dex_pc_;
2565
2566 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
2567};
2568
2569class HStaticFieldGet : public HExpression<1> {
2570 public:
2571 HStaticFieldGet(HInstruction* cls,
2572 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00002573 MemberOffset field_offset,
2574 bool is_volatile)
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002575 : HExpression(field_type, SideEffects::DependsOnSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00002576 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002577 SetRawInputAt(0, cls);
2578 }
2579
Calin Juravle52c48962014-12-16 17:02:57 +00002580
Calin Juravle10c9cbe2014-12-19 10:50:19 +00002581 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002582
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002583 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00002584 HStaticFieldGet* other_get = other->AsStaticFieldGet();
2585 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002586 }
2587
2588 size_t ComputeHashCode() const OVERRIDE {
2589 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
2590 }
2591
Calin Juravle52c48962014-12-16 17:02:57 +00002592 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002593 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
2594 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002595 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002596
2597 DECLARE_INSTRUCTION(StaticFieldGet);
2598
2599 private:
2600 const FieldInfo field_info_;
2601
2602 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
2603};
2604
2605class HStaticFieldSet : public HTemplateInstruction<2> {
2606 public:
2607 HStaticFieldSet(HInstruction* cls,
2608 HInstruction* value,
2609 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00002610 MemberOffset field_offset,
2611 bool is_volatile)
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002612 : HTemplateInstruction(SideEffects::ChangesSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00002613 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002614 SetRawInputAt(0, cls);
2615 SetRawInputAt(1, value);
2616 }
2617
Calin Juravle52c48962014-12-16 17:02:57 +00002618 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002619 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
2620 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002621 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002622
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002623 HInstruction* GetValue() const { return InputAt(1); }
2624
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002625 DECLARE_INSTRUCTION(StaticFieldSet);
2626
2627 private:
2628 const FieldInfo field_info_;
2629
2630 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
2631};
2632
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002633// Implement the move-exception DEX instruction.
2634class HLoadException : public HExpression<0> {
2635 public:
2636 HLoadException() : HExpression(Primitive::kPrimNot, SideEffects::None()) {}
2637
2638 DECLARE_INSTRUCTION(LoadException);
2639
2640 private:
2641 DISALLOW_COPY_AND_ASSIGN(HLoadException);
2642};
2643
2644class HThrow : public HTemplateInstruction<1> {
2645 public:
2646 HThrow(HInstruction* exception, uint32_t dex_pc)
2647 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {
2648 SetRawInputAt(0, exception);
2649 }
2650
2651 bool IsControlFlow() const OVERRIDE { return true; }
2652
2653 bool NeedsEnvironment() const OVERRIDE { return true; }
2654
2655 uint32_t GetDexPc() const { return dex_pc_; }
2656
2657 DECLARE_INSTRUCTION(Throw);
2658
2659 private:
2660 uint32_t dex_pc_;
2661
2662 DISALLOW_COPY_AND_ASSIGN(HThrow);
2663};
2664
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002665class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002666 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002667 HInstanceOf(HInstruction* object,
2668 HLoadClass* constant,
2669 bool class_is_final,
2670 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002671 : HExpression(Primitive::kPrimBoolean, SideEffects::None()),
2672 class_is_final_(class_is_final),
2673 dex_pc_(dex_pc) {
2674 SetRawInputAt(0, object);
2675 SetRawInputAt(1, constant);
2676 }
2677
2678 bool CanBeMoved() const OVERRIDE { return true; }
2679
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002680 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002681 return true;
2682 }
2683
2684 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002685 return false;
2686 }
2687
2688 uint32_t GetDexPc() const { return dex_pc_; }
2689
2690 bool IsClassFinal() const { return class_is_final_; }
2691
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002692 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002693
2694 private:
2695 const bool class_is_final_;
2696 const uint32_t dex_pc_;
2697
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002698 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
2699};
2700
2701class HCheckCast : public HTemplateInstruction<2> {
2702 public:
2703 HCheckCast(HInstruction* object,
2704 HLoadClass* constant,
2705 bool class_is_final,
2706 uint32_t dex_pc)
2707 : HTemplateInstruction(SideEffects::None()),
2708 class_is_final_(class_is_final),
2709 dex_pc_(dex_pc) {
2710 SetRawInputAt(0, object);
2711 SetRawInputAt(1, constant);
2712 }
2713
2714 bool CanBeMoved() const OVERRIDE { return true; }
2715
2716 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2717 return true;
2718 }
2719
2720 bool NeedsEnvironment() const OVERRIDE {
2721 // Instruction may throw a CheckCastError.
2722 return true;
2723 }
2724
2725 bool CanThrow() const OVERRIDE { return true; }
2726
2727 uint32_t GetDexPc() const { return dex_pc_; }
2728
2729 bool IsClassFinal() const { return class_is_final_; }
2730
2731 DECLARE_INSTRUCTION(CheckCast);
2732
2733 private:
2734 const bool class_is_final_;
2735 const uint32_t dex_pc_;
2736
2737 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002738};
2739
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00002740class HMonitorOperation : public HTemplateInstruction<1> {
2741 public:
2742 enum OperationKind {
2743 kEnter,
2744 kExit,
2745 };
2746
2747 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
2748 : HTemplateInstruction(SideEffects::None()), kind_(kind), dex_pc_(dex_pc) {
2749 SetRawInputAt(0, object);
2750 }
2751
2752 // Instruction may throw a Java exception, so we need an environment.
2753 bool NeedsEnvironment() const OVERRIDE { return true; }
2754 bool CanThrow() const OVERRIDE { return true; }
2755
2756 uint32_t GetDexPc() const { return dex_pc_; }
2757
2758 bool IsEnter() const { return kind_ == kEnter; }
2759
2760 DECLARE_INSTRUCTION(MonitorOperation);
2761
Calin Juravle52c48962014-12-16 17:02:57 +00002762 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00002763 const OperationKind kind_;
2764 const uint32_t dex_pc_;
2765
2766 private:
2767 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
2768};
2769
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002770class MoveOperands : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002771 public:
Nicolas Geoffray740475d2014-09-29 10:33:25 +01002772 MoveOperands(Location source, Location destination, HInstruction* instruction)
2773 : source_(source), destination_(destination), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002774
2775 Location GetSource() const { return source_; }
2776 Location GetDestination() const { return destination_; }
2777
2778 void SetSource(Location value) { source_ = value; }
2779 void SetDestination(Location value) { destination_ = value; }
2780
2781 // The parallel move resolver marks moves as "in-progress" by clearing the
2782 // destination (but not the source).
2783 Location MarkPending() {
2784 DCHECK(!IsPending());
2785 Location dest = destination_;
2786 destination_ = Location::NoLocation();
2787 return dest;
2788 }
2789
2790 void ClearPending(Location dest) {
2791 DCHECK(IsPending());
2792 destination_ = dest;
2793 }
2794
2795 bool IsPending() const {
2796 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
2797 return destination_.IsInvalid() && !source_.IsInvalid();
2798 }
2799
2800 // True if this blocks a move from the given location.
2801 bool Blocks(Location loc) const {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00002802 return !IsEliminated() && source_.Equals(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002803 }
2804
2805 // A move is redundant if it's been eliminated, if its source and
2806 // destination are the same, or if its destination is unneeded.
2807 bool IsRedundant() const {
2808 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
2809 }
2810
2811 // We clear both operands to indicate move that's been eliminated.
2812 void Eliminate() {
2813 source_ = destination_ = Location::NoLocation();
2814 }
2815
2816 bool IsEliminated() const {
2817 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
2818 return source_.IsInvalid();
2819 }
2820
Nicolas Geoffray740475d2014-09-29 10:33:25 +01002821 HInstruction* GetInstruction() const { return instruction_; }
2822
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002823 private:
2824 Location source_;
2825 Location destination_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01002826 // The instruction this move is assocatied with. Null when this move is
2827 // for moving an input in the expected locations of user (including a phi user).
2828 // This is only used in debug mode, to ensure we do not connect interval siblings
2829 // in the same parallel move.
2830 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002831};
2832
2833static constexpr size_t kDefaultNumberOfMoves = 4;
2834
2835class HParallelMove : public HTemplateInstruction<0> {
2836 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002837 explicit HParallelMove(ArenaAllocator* arena)
2838 : HTemplateInstruction(SideEffects::None()), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002839
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00002840 void AddMove(Location source, Location destination, HInstruction* instruction) {
2841 DCHECK(source.IsValid());
2842 DCHECK(destination.IsValid());
2843 // The parallel move resolver does not handle pairs. So we decompose the
2844 // pair locations into two moves.
2845 if (source.IsPair() && destination.IsPair()) {
2846 AddMove(source.ToLow(), destination.ToLow(), instruction);
2847 AddMove(source.ToHigh(), destination.ToHigh(), nullptr);
2848 } else if (source.IsPair()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002849 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00002850 AddMove(source.ToLow(), Location::StackSlot(destination.GetStackIndex()), instruction);
2851 AddMove(source.ToHigh(), Location::StackSlot(destination.GetHighStackIndex(4)), nullptr);
2852 } else if (destination.IsPair()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002853 if (source.IsConstant()) {
2854 // We put the same constant in the move. The code generator will handle which
2855 // low or high part to use.
2856 AddMove(source, destination.ToLow(), instruction);
2857 AddMove(source, destination.ToHigh(), nullptr);
2858 } else {
2859 DCHECK(source.IsDoubleStackSlot());
2860 AddMove(Location::StackSlot(source.GetStackIndex()), destination.ToLow(), instruction);
2861 // TODO: rewrite GetHighStackIndex to not require a word size. It's supposed to
2862 // always be 4.
2863 static constexpr int kHighOffset = 4;
2864 AddMove(Location::StackSlot(source.GetHighStackIndex(kHighOffset)),
2865 destination.ToHigh(),
2866 nullptr);
2867 }
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00002868 } else {
2869 if (kIsDebugBuild) {
2870 if (instruction != nullptr) {
2871 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
2872 DCHECK_NE(moves_.Get(i).GetInstruction(), instruction)
2873 << "Doing parallel moves for the same instruction.";
2874 }
2875 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00002876 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00002877 DCHECK(!destination.Equals(moves_.Get(i).GetDestination()))
2878 << "Same destination for two moves in a parallel move.";
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00002879 }
2880 }
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00002881 moves_.Add(MoveOperands(source, destination, instruction));
Nicolas Geoffray740475d2014-09-29 10:33:25 +01002882 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002883 }
2884
2885 MoveOperands* MoveOperandsAt(size_t index) const {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00002886 return moves_.GetRawStorage() + index;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002887 }
2888
2889 size_t NumMoves() const { return moves_.Size(); }
2890
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002891 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002892
2893 private:
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00002894 GrowableArray<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002895
2896 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
2897};
2898
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002899class HGraphVisitor : public ValueObject {
2900 public:
Dave Allison20dfc792014-06-16 20:44:29 -07002901 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
2902 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002903
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002904 virtual void VisitInstruction(HInstruction* instruction) { UNUSED(instruction); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002905 virtual void VisitBasicBlock(HBasicBlock* block);
2906
Roland Levillain633021e2014-10-01 14:12:25 +01002907 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002908 void VisitInsertionOrder();
2909
Roland Levillain633021e2014-10-01 14:12:25 +01002910 // Visit the graph following dominator tree reverse post-order.
2911 void VisitReversePostOrder();
2912
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002913 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002914
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002915 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002916#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002917 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
2918
2919 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
2920
2921#undef DECLARE_VISIT_INSTRUCTION
2922
2923 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07002924 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002925
2926 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
2927};
2928
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002929class HGraphDelegateVisitor : public HGraphVisitor {
2930 public:
2931 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
2932 virtual ~HGraphDelegateVisitor() {}
2933
2934 // Visit functions that delegate to to super class.
2935#define DECLARE_VISIT_INSTRUCTION(name, super) \
2936 virtual void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
2937
2938 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
2939
2940#undef DECLARE_VISIT_INSTRUCTION
2941
2942 private:
2943 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
2944};
2945
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002946class HInsertionOrderIterator : public ValueObject {
2947 public:
2948 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
2949
2950 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
2951 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
2952 void Advance() { ++index_; }
2953
2954 private:
2955 const HGraph& graph_;
2956 size_t index_;
2957
2958 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
2959};
2960
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002961class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002962 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002963 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002964
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002965 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
2966 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002967 void Advance() { ++index_; }
2968
2969 private:
2970 const HGraph& graph_;
2971 size_t index_;
2972
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002973 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002974};
2975
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002976class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002977 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002978 explicit HPostOrderIterator(const HGraph& graph)
2979 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002980
2981 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002982 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002983 void Advance() { --index_; }
2984
2985 private:
2986 const HGraph& graph_;
2987 size_t index_;
2988
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01002989 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002990};
2991
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002992} // namespace art
2993
2994#endif // ART_COMPILER_OPTIMIZING_NODES_H_