blob: cac78f602c718c2fa4f5cb0bc9000302398ffc8c [file] [log] [blame]
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ART_COMPILER_OPTIMIZING_NODES_H_
18#define ART_COMPILER_OPTIMIZING_NODES_H_
19
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000020#include "invoke_type.h"
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010021#include "locations.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010022#include "offsets.h"
Ian Rogerse63db272014-07-15 15:36:11 -070023#include "primitive.h"
Ian Rogers0279ebb2014-10-08 17:27:48 -070024#include "utils/arena_object.h"
Nicolas Geoffray0e336432014-02-26 18:24:38 +000025#include "utils/arena_bit_vector.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000026#include "utils/growable_array.h"
27
28namespace art {
29
30class HBasicBlock;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010031class HEnvironment;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000032class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000033class HIntConstant;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000034class HInvoke;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000035class HGraphVisitor;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010036class HPhi;
Nicolas Geoffray3c049742014-09-24 18:10:46 +010037class HSuspendCheck;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010038class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000039class LocationSummary;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000040
41static const int kDefaultNumberOfBlocks = 8;
42static const int kDefaultNumberOfSuccessors = 2;
43static const int kDefaultNumberOfPredecessors = 2;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010044static const int kDefaultNumberOfDominatedBlocks = 1;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000045static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000046
Calin Juravle9aec02f2014-11-18 23:06:35 +000047static constexpr uint32_t kMaxIntShiftValue = 0x1f;
48static constexpr uint64_t kMaxLongShiftValue = 0x3f;
49
Dave Allison20dfc792014-06-16 20:44:29 -070050enum IfCondition {
51 kCondEQ,
52 kCondNE,
53 kCondLT,
54 kCondLE,
55 kCondGT,
56 kCondGE,
57};
58
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010059class HInstructionList {
60 public:
61 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
62
63 void AddInstruction(HInstruction* instruction);
64 void RemoveInstruction(HInstruction* instruction);
65
Roland Levillain6b469232014-09-25 10:10:38 +010066 // Return true if this list contains `instruction`.
67 bool Contains(HInstruction* instruction) const;
68
Roland Levillainccc07a92014-09-16 14:48:16 +010069 // Return true if `instruction1` is found before `instruction2` in
70 // this instruction list and false otherwise. Abort if none
71 // of these instructions is found.
72 bool FoundBefore(const HInstruction* instruction1,
73 const HInstruction* instruction2) const;
74
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010075 private:
76 HInstruction* first_instruction_;
77 HInstruction* last_instruction_;
78
79 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000080 friend class HGraph;
81 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010082 friend class HInstructionIterator;
Nicolas Geoffray804d0932014-05-02 08:46:00 +010083 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010084
85 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
86};
87
Nicolas Geoffray818f2102014-02-18 16:43:35 +000088// Control-flow graph of a method. Contains a list of basic blocks.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070089class HGraph : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +000090 public:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000091 HGraph(ArenaAllocator* arena, int start_instruction_id = 0)
Nicolas Geoffray818f2102014-02-18 16:43:35 +000092 : arena_(arena),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000093 blocks_(arena, kDefaultNumberOfBlocks),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +010094 reverse_post_order_(arena, kDefaultNumberOfBlocks),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070095 entry_block_(nullptr),
96 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +010097 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +010098 number_of_vregs_(0),
99 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000100 temporaries_vreg_slots_(0),
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000101 current_instruction_id_(start_instruction_id) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000102
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000103 ArenaAllocator* GetArena() const { return arena_; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100104 const GrowableArray<HBasicBlock*>& GetBlocks() const { return blocks_; }
Roland Levillain93445682014-10-06 19:24:02 +0100105 HBasicBlock* GetBlock(size_t id) const { return blocks_.Get(id); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000106
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000107 HBasicBlock* GetEntryBlock() const { return entry_block_; }
108 HBasicBlock* GetExitBlock() const { return exit_block_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000109
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000110 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
111 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000112
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000113 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100114
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000115 // Try building the SSA form of this graph, with dominance computation and loop
116 // recognition. Returns whether it was successful in doing all these steps.
117 bool TryBuildingSsa() {
118 BuildDominatorTree();
119 TransformToSsa();
120 return AnalyzeNaturalLoops();
121 }
122
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000123 void BuildDominatorTree();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000124 void TransformToSsa();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100125 void SimplifyCFG();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000126
Nicolas Geoffrayf5370122014-12-02 11:51:19 +0000127 // Analyze all natural loops in this graph. Returns false if one
128 // loop is not natural, that is the header does not dominate the
129 // back edge.
130 bool AnalyzeNaturalLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100131
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000132 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
133 void InlineInto(HGraph* outer_graph, HInvoke* invoke);
134
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100135 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
136 void SimplifyLoop(HBasicBlock* header);
137
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000138 int32_t GetNextInstructionId() {
139 DCHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000140 return current_instruction_id_++;
141 }
142
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000143 int32_t GetCurrentInstructionId() const {
144 return current_instruction_id_;
145 }
146
147 void SetCurrentInstructionId(int32_t id) {
148 current_instruction_id_ = id;
149 }
150
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100151 uint16_t GetMaximumNumberOfOutVRegs() const {
152 return maximum_number_of_out_vregs_;
153 }
154
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000155 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
156 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100157 }
158
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000159 void UpdateTemporariesVRegSlots(size_t slots) {
160 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100161 }
162
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000163 size_t GetTemporariesVRegSlots() const {
164 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100165 }
166
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100167 void SetNumberOfVRegs(uint16_t number_of_vregs) {
168 number_of_vregs_ = number_of_vregs;
169 }
170
171 uint16_t GetNumberOfVRegs() const {
172 return number_of_vregs_;
173 }
174
175 void SetNumberOfInVRegs(uint16_t value) {
176 number_of_in_vregs_ = value;
177 }
178
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100179 uint16_t GetNumberOfLocalVRegs() const {
180 return number_of_vregs_ - number_of_in_vregs_;
181 }
182
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100183 const GrowableArray<HBasicBlock*>& GetReversePostOrder() const {
184 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100185 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100186
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000187 private:
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000188 HBasicBlock* FindCommonDominator(HBasicBlock* first, HBasicBlock* second) const;
189 void VisitBlockForDominatorTree(HBasicBlock* block,
190 HBasicBlock* predecessor,
191 GrowableArray<size_t>* visits);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100192 void FindBackEdges(ArenaBitVector* visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000193 void VisitBlockForBackEdges(HBasicBlock* block,
194 ArenaBitVector* visited,
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100195 ArenaBitVector* visiting);
Roland Levillainfc600dc2014-12-02 17:16:31 +0000196 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000197 void RemoveDeadBlocks(const ArenaBitVector& visited) const;
Jean Christophe Beyler53d9da82014-12-04 13:28:25 -0800198 void RemoveBlock(HBasicBlock* block) const;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000199
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000200 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000201
202 // List of blocks in insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000203 GrowableArray<HBasicBlock*> blocks_;
204
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100205 // List of blocks to perform a reverse post order tree traversal.
206 GrowableArray<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000207
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000208 HBasicBlock* entry_block_;
209 HBasicBlock* exit_block_;
210
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100211 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100212 uint16_t maximum_number_of_out_vregs_;
213
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100214 // The number of virtual registers in this method. Contains the parameters.
215 uint16_t number_of_vregs_;
216
217 // The number of virtual registers used by parameters of this method.
218 uint16_t number_of_in_vregs_;
219
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000220 // Number of vreg size slots that the temporaries use (used in baseline compiler).
221 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100222
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000223 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000224 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000225
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000226 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000227 DISALLOW_COPY_AND_ASSIGN(HGraph);
228};
229
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700230class HLoopInformation : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000231 public:
232 HLoopInformation(HBasicBlock* header, HGraph* graph)
233 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100234 suspend_check_(nullptr),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100235 back_edges_(graph->GetArena(), kDefaultNumberOfBackEdges),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100236 // Make bit vector growable, as the number of blocks may change.
237 blocks_(graph->GetArena(), graph->GetBlocks().Size(), true) {}
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100238
239 HBasicBlock* GetHeader() const {
240 return header_;
241 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000242
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100243 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
244 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
245 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
246
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000247 void AddBackEdge(HBasicBlock* back_edge) {
248 back_edges_.Add(back_edge);
249 }
250
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100251 void RemoveBackEdge(HBasicBlock* back_edge) {
252 back_edges_.Delete(back_edge);
253 }
254
255 bool IsBackEdge(HBasicBlock* block) {
256 for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) {
257 if (back_edges_.Get(i) == block) return true;
258 }
259 return false;
260 }
261
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000262 size_t NumberOfBackEdges() const {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000263 return back_edges_.Size();
264 }
265
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100266 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100267
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100268 const GrowableArray<HBasicBlock*>& GetBackEdges() const {
269 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100270 }
271
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100272 void ClearBackEdges() {
273 back_edges_.Reset();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100274 }
275
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100276 // Find blocks that are part of this loop. Returns whether the loop is a natural loop,
277 // that is the header dominates the back edge.
278 bool Populate();
279
280 // Returns whether this loop information contains `block`.
281 // Note that this loop information *must* be populated before entering this function.
282 bool Contains(const HBasicBlock& block) const;
283
284 // Returns whether this loop information is an inner loop of `other`.
285 // Note that `other` *must* be populated before entering this function.
286 bool IsIn(const HLoopInformation& other) const;
287
288 const ArenaBitVector& GetBlocks() const { return blocks_; }
289
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000290 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100291 // Internal recursive implementation of `Populate`.
292 void PopulateRecursive(HBasicBlock* block);
293
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000294 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100295 HSuspendCheck* suspend_check_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000296 GrowableArray<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100297 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000298
299 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
300};
301
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100302static constexpr size_t kNoLifetime = -1;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100303static constexpr uint32_t kNoDexPc = -1;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100304
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000305// A block in a method. Contains the list of instructions represented
306// as a double linked list. Each block knows its predecessors and
307// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100308
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700309class HBasicBlock : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000310 public:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100311 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000312 : graph_(graph),
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000313 predecessors_(graph->GetArena(), kDefaultNumberOfPredecessors),
314 successors_(graph->GetArena(), kDefaultNumberOfSuccessors),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000315 loop_information_(nullptr),
316 dominator_(nullptr),
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100317 dominated_blocks_(graph->GetArena(), kDefaultNumberOfDominatedBlocks),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100318 block_id_(-1),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100319 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100320 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000321 lifetime_end_(kNoLifetime),
322 is_catch_block_(false) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000323
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100324 const GrowableArray<HBasicBlock*>& GetPredecessors() const {
325 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000326 }
327
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100328 const GrowableArray<HBasicBlock*>& GetSuccessors() const {
329 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000330 }
331
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100332 const GrowableArray<HBasicBlock*>& GetDominatedBlocks() const {
333 return dominated_blocks_;
334 }
335
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100336 bool IsEntryBlock() const {
337 return graph_->GetEntryBlock() == this;
338 }
339
340 bool IsExitBlock() const {
341 return graph_->GetExitBlock() == this;
342 }
343
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000344 void AddBackEdge(HBasicBlock* back_edge) {
345 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000346 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000347 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100348 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000349 loop_information_->AddBackEdge(back_edge);
350 }
351
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000352 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000353
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000354 int GetBlockId() const { return block_id_; }
355 void SetBlockId(int id) { block_id_ = id; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000356
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000357 HBasicBlock* GetDominator() const { return dominator_; }
358 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100359 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.Add(block); }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000360
361 int NumberOfBackEdges() const {
362 return loop_information_ == nullptr
363 ? 0
364 : loop_information_->NumberOfBackEdges();
365 }
366
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100367 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
368 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100369 const HInstructionList& GetInstructions() const { return instructions_; }
370 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100371 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000372
373 void AddSuccessor(HBasicBlock* block) {
374 successors_.Add(block);
375 block->predecessors_.Add(this);
376 }
377
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100378 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
379 size_t successor_index = GetSuccessorIndexOf(existing);
380 DCHECK_NE(successor_index, static_cast<size_t>(-1));
381 existing->RemovePredecessor(this);
382 new_block->predecessors_.Add(this);
383 successors_.Put(successor_index, new_block);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000384 }
385
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100386 void RemovePredecessor(HBasicBlock* block) {
387 predecessors_.Delete(block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100388 }
389
390 void ClearAllPredecessors() {
391 predecessors_.Reset();
392 }
393
394 void AddPredecessor(HBasicBlock* block) {
395 predecessors_.Add(block);
396 block->successors_.Add(this);
397 }
398
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100399 void SwapPredecessors() {
Nicolas Geoffrayc83d4412014-09-18 16:46:20 +0100400 DCHECK_EQ(predecessors_.Size(), 2u);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100401 HBasicBlock* temp = predecessors_.Get(0);
402 predecessors_.Put(0, predecessors_.Get(1));
403 predecessors_.Put(1, temp);
404 }
405
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100406 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) {
407 for (size_t i = 0, e = predecessors_.Size(); i < e; ++i) {
408 if (predecessors_.Get(i) == predecessor) {
409 return i;
410 }
411 }
412 return -1;
413 }
414
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100415 size_t GetSuccessorIndexOf(HBasicBlock* successor) {
416 for (size_t i = 0, e = successors_.Size(); i < e; ++i) {
417 if (successors_.Get(i) == successor) {
418 return i;
419 }
420 }
421 return -1;
422 }
423
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000424 void AddInstruction(HInstruction* instruction);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100425 void RemoveInstruction(HInstruction* instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100426 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +0100427 // Replace instruction `initial` with `replacement` within this block.
428 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
429 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100430 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100431 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100432 void RemovePhi(HPhi* phi);
433
434 bool IsLoopHeader() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100435 return (loop_information_ != nullptr) && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100436 }
437
Roland Levillain6b879dd2014-09-22 17:13:44 +0100438 bool IsLoopPreHeaderFirstPredecessor() const {
439 DCHECK(IsLoopHeader());
440 DCHECK(!GetPredecessors().IsEmpty());
441 return GetPredecessors().Get(0) == GetLoopInformation()->GetPreHeader();
442 }
443
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100444 HLoopInformation* GetLoopInformation() const {
445 return loop_information_;
446 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000447
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100448 // Set the loop_information_ on this block. This method overrides the current
449 // loop_information if it is an outer loop of the passed loop information.
450 void SetInLoop(HLoopInformation* info) {
451 if (IsLoopHeader()) {
452 // Nothing to do. This just means `info` is an outer loop.
453 } else if (loop_information_ == nullptr) {
454 loop_information_ = info;
455 } else if (loop_information_->Contains(*info->GetHeader())) {
456 // Block is currently part of an outer loop. Make it part of this inner loop.
457 // Note that a non loop header having a loop information means this loop information
458 // has already been populated
459 loop_information_ = info;
460 } else {
461 // Block is part of an inner loop. Do not update the loop information.
462 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
463 // at this point, because this method is being called while populating `info`.
464 }
465 }
466
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100467 bool IsInLoop() const { return loop_information_ != nullptr; }
468
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100469 // Returns wheter this block dominates the blocked passed as parameter.
470 bool Dominates(HBasicBlock* block) const;
471
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100472 size_t GetLifetimeStart() const { return lifetime_start_; }
473 size_t GetLifetimeEnd() const { return lifetime_end_; }
474
475 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
476 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
477
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100478 uint32_t GetDexPc() const { return dex_pc_; }
479
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000480 bool IsCatchBlock() const { return is_catch_block_; }
481 void SetIsCatchBlock() { is_catch_block_ = true; }
482
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000483 private:
484 HGraph* const graph_;
485 GrowableArray<HBasicBlock*> predecessors_;
486 GrowableArray<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100487 HInstructionList instructions_;
488 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000489 HLoopInformation* loop_information_;
490 HBasicBlock* dominator_;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100491 GrowableArray<HBasicBlock*> dominated_blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000492 int block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100493 // The dex program counter of the first instruction of this block.
494 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100495 size_t lifetime_start_;
496 size_t lifetime_end_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000497 bool is_catch_block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000498
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000499 friend class HGraph;
500 friend class HInstruction;
501
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000502 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
503};
504
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100505#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
506 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000507 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000508 M(ArrayGet, Instruction) \
509 M(ArrayLength, Instruction) \
510 M(ArraySet, Instruction) \
511 M(BoundsCheck, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000512 M(CheckCast, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100513 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000514 M(Compare, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100515 M(Condition, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000516 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +0000517 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000518 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100519 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000520 M(Exit, Instruction) \
521 M(FloatConstant, Constant) \
522 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100523 M(GreaterThan, Condition) \
524 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100525 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000526 M(InstanceFieldGet, Instruction) \
527 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000528 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100529 M(IntConstant, Constant) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +0000530 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000531 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100532 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000533 M(LessThan, Condition) \
534 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000535 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000536 M(LoadException, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100537 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000538 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100539 M(Local, Instruction) \
540 M(LongConstant, Constant) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +0000541 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000542 M(Mul, BinaryOperation) \
543 M(Neg, UnaryOperation) \
544 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100545 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +0100546 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000547 M(NotEqual, Condition) \
548 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000549 M(Or, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100550 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000551 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100552 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000553 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100554 M(Return, Instruction) \
555 M(ReturnVoid, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000556 M(Shl, BinaryOperation) \
557 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100558 M(StaticFieldGet, Instruction) \
559 M(StaticFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100560 M(StoreLocal, Instruction) \
561 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100562 M(SuspendCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000563 M(Temporary, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000564 M(Throw, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +0000565 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000566 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000567 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000568
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100569#define FOR_EACH_INSTRUCTION(M) \
570 FOR_EACH_CONCRETE_INSTRUCTION(M) \
571 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +0100572 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +0100573 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100574 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -0700575
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100576#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000577FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
578#undef FORWARD_DECLARATION
579
Roland Levillainccc07a92014-09-16 14:48:16 +0100580#define DECLARE_INSTRUCTION(type) \
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100581 virtual InstructionKind GetKind() const { return k##type; } \
Roland Levillainccc07a92014-09-16 14:48:16 +0100582 virtual const char* DebugName() const { return #type; } \
583 virtual const H##type* As##type() const OVERRIDE { return this; } \
584 virtual H##type* As##type() OVERRIDE { return this; } \
585 virtual bool InstructionTypeEquals(HInstruction* other) const { \
586 return other->Is##type(); \
587 } \
588 virtual void Accept(HGraphVisitor* visitor)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000589
David Brazdiled596192015-01-23 10:39:45 +0000590template <typename T> class HUseList;
591
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100592template <typename T>
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700593class HUseListNode : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000594 public:
David Brazdiled596192015-01-23 10:39:45 +0000595 HUseListNode* GetPrevious() const { return prev_; }
596 HUseListNode* GetNext() const { return next_; }
597 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100598 size_t GetIndex() const { return index_; }
599
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000600 private:
David Brazdiled596192015-01-23 10:39:45 +0000601 HUseListNode(T user, size_t index)
602 : user_(user), index_(index), prev_(nullptr), next_(nullptr) {}
603
604 T const user_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100605 const size_t index_;
David Brazdiled596192015-01-23 10:39:45 +0000606 HUseListNode<T>* prev_;
607 HUseListNode<T>* next_;
608
609 friend class HUseList<T>;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000610
611 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
612};
613
David Brazdiled596192015-01-23 10:39:45 +0000614template <typename T>
615class HUseList : public ValueObject {
616 public:
617 HUseList() : first_(nullptr) {}
618
619 void Clear() {
620 first_ = nullptr;
621 }
622
623 // Adds a new entry at the beginning of the use list and returns
624 // the newly created node.
625 HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) {
626 HUseListNode<T>* new_node = new(arena) HUseListNode<T>(user, index);
627 if (IsEmpty()) {
628 first_ = new_node;
629 } else {
630 first_->prev_ = new_node;
631 new_node->next_ = first_;
632 first_ = new_node;
633 }
634 return new_node;
635 }
636
637 HUseListNode<T>* GetFirst() const {
638 return first_;
639 }
640
641 void Remove(HUseListNode<T>* node) {
642 if (node->prev_ != nullptr) {
643 node->prev_->next_ = node->next_;
644 }
645 if (node->next_ != nullptr) {
646 node->next_->prev_ = node->prev_;
647 }
648 if (node == first_) {
649 first_ = node->next_;
650 }
651 }
652
653 bool IsEmpty() const {
654 return first_ == nullptr;
655 }
656
657 bool HasOnlyOneUse() const {
658 return first_ != nullptr && first_->next_ == nullptr;
659 }
660
661 private:
662 HUseListNode<T>* first_;
663};
664
665template<typename T>
666class HUseIterator : public ValueObject {
667 public:
668 explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {}
669
670 bool Done() const { return current_ == nullptr; }
671
672 void Advance() {
673 DCHECK(!Done());
674 current_ = current_->GetNext();
675 }
676
677 HUseListNode<T>* Current() const {
678 DCHECK(!Done());
679 return current_;
680 }
681
682 private:
683 HUseListNode<T>* current_;
684
685 friend class HValue;
686};
687
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100688// Represents the side effects an instruction may have.
689class SideEffects : public ValueObject {
690 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100691 SideEffects() : flags_(0) {}
692
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100693 static SideEffects None() {
694 return SideEffects(0);
695 }
696
697 static SideEffects All() {
698 return SideEffects(ChangesSomething().flags_ | DependsOnSomething().flags_);
699 }
700
701 static SideEffects ChangesSomething() {
702 return SideEffects((1 << kFlagChangesCount) - 1);
703 }
704
705 static SideEffects DependsOnSomething() {
706 int count = kFlagDependsOnCount - kFlagChangesCount;
707 return SideEffects(((1 << count) - 1) << kFlagChangesCount);
708 }
709
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100710 SideEffects Union(SideEffects other) const {
711 return SideEffects(flags_ | other.flags_);
712 }
713
Roland Levillain72bceff2014-09-15 18:29:00 +0100714 bool HasSideEffects() const {
715 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
716 return (flags_ & all_bits_set) != 0;
717 }
718
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100719 bool HasAllSideEffects() const {
720 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
721 return all_bits_set == (flags_ & all_bits_set);
722 }
723
724 bool DependsOn(SideEffects other) const {
725 size_t depends_flags = other.ComputeDependsFlags();
726 return (flags_ & depends_flags) != 0;
727 }
728
729 bool HasDependencies() const {
730 int count = kFlagDependsOnCount - kFlagChangesCount;
731 size_t all_bits_set = (1 << count) - 1;
732 return ((flags_ >> kFlagChangesCount) & all_bits_set) != 0;
733 }
734
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100735 private:
736 static constexpr int kFlagChangesSomething = 0;
737 static constexpr int kFlagChangesCount = kFlagChangesSomething + 1;
738
739 static constexpr int kFlagDependsOnSomething = kFlagChangesCount;
740 static constexpr int kFlagDependsOnCount = kFlagDependsOnSomething + 1;
741
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100742 explicit SideEffects(size_t flags) : flags_(flags) {}
743
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100744 size_t ComputeDependsFlags() const {
745 return flags_ << kFlagChangesCount;
746 }
747
748 size_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100749};
750
David Brazdiled596192015-01-23 10:39:45 +0000751// A HEnvironment object contains the values of virtual registers at a given location.
752class HEnvironment : public ArenaObject<kArenaAllocMisc> {
753 public:
754 HEnvironment(ArenaAllocator* arena, size_t number_of_vregs)
755 : vregs_(arena, number_of_vregs) {
756 vregs_.SetSize(number_of_vregs);
757 for (size_t i = 0; i < number_of_vregs; i++) {
758 vregs_.Put(i, VRegInfo(nullptr, nullptr));
759 }
760 }
761
762 void CopyFrom(HEnvironment* env);
763
764 void SetRawEnvAt(size_t index, HInstruction* instruction) {
765 vregs_.Put(index, VRegInfo(instruction, nullptr));
766 }
767
768 // Record instructions' use entries of this environment for constant-time removal.
769 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
770 DCHECK(env_use->GetUser() == this);
771 size_t index = env_use->GetIndex();
772 VRegInfo info = vregs_.Get(index);
773 DCHECK(info.vreg_ != nullptr);
774 DCHECK(info.node_ == nullptr);
775 vregs_.Put(index, VRegInfo(info.vreg_, env_use));
776 }
777
778 HInstruction* GetInstructionAt(size_t index) const {
779 return vregs_.Get(index).vreg_;
780 }
781
782 HUseListNode<HEnvironment*>* GetInstructionEnvUseAt(size_t index) const {
783 return vregs_.Get(index).node_;
784 }
785
786 size_t Size() const { return vregs_.Size(); }
787
788 private:
789 struct VRegInfo {
790 HInstruction* vreg_;
791 HUseListNode<HEnvironment*>* node_;
792
793 VRegInfo(HInstruction* instruction, HUseListNode<HEnvironment*>* env_use)
794 : vreg_(instruction), node_(env_use) {}
795 };
796
797 GrowableArray<VRegInfo> vregs_;
798
799 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
800};
801
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700802class HInstruction : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000803 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100804 explicit HInstruction(SideEffects side_effects)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000805 : previous_(nullptr),
806 next_(nullptr),
807 block_(nullptr),
808 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100809 ssa_index_(-1),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100810 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100811 locations_(nullptr),
812 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100813 lifetime_position_(kNoLifetime),
814 side_effects_(side_effects) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000815
Dave Allison20dfc792014-06-16 20:44:29 -0700816 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000817
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100818#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100819 enum InstructionKind {
820 FOR_EACH_INSTRUCTION(DECLARE_KIND)
821 };
822#undef DECLARE_KIND
823
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000824 HInstruction* GetNext() const { return next_; }
825 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000826
Calin Juravle77520bc2015-01-12 18:45:46 +0000827 HInstruction* GetNextDisregardingMoves() const;
828 HInstruction* GetPreviousDisregardingMoves() const;
829
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000830 HBasicBlock* GetBlock() const { return block_; }
831 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100832 bool IsInBlock() const { return block_ != nullptr; }
833 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +0100834 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000835
Roland Levillain6b879dd2014-09-22 17:13:44 +0100836 virtual size_t InputCount() const = 0;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100837 virtual HInstruction* InputAt(size_t i) const = 0;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000838
839 virtual void Accept(HGraphVisitor* visitor) = 0;
840 virtual const char* DebugName() const = 0;
841
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100842 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100843 virtual void SetRawInputAt(size_t index, HInstruction* input) = 0;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100844
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100845 virtual bool NeedsEnvironment() const { return false; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100846 virtual bool IsControlFlow() const { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +0100847 virtual bool CanThrow() const { return false; }
Roland Levillain72bceff2014-09-15 18:29:00 +0100848 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100849
Calin Juravle77520bc2015-01-12 18:45:46 +0000850 virtual bool CanDoImplicitNullCheck() const { return false; }
851
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100852 void AddUseAt(HInstruction* user, size_t index) {
David Brazdiled596192015-01-23 10:39:45 +0000853 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000854 }
855
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100856 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +0100857 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +0000858 HUseListNode<HEnvironment*>* env_use =
859 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
860 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100861 }
862
863 void RemoveUser(HInstruction* user, size_t index);
David Brazdiled596192015-01-23 10:39:45 +0000864 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100865
David Brazdiled596192015-01-23 10:39:45 +0000866 HUseList<HInstruction*>& GetUses() { return uses_; }
867 HUseList<HEnvironment*>& GetEnvUses() { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000868
David Brazdiled596192015-01-23 10:39:45 +0000869 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
870 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000871
David Brazdiled596192015-01-23 10:39:45 +0000872 size_t ExpensiveComputeNumberOfUses() const {
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100873 // TODO: Optimize this method if it is used outside of the HGraphVisualizer.
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100874 size_t result = 0;
David Brazdiled596192015-01-23 10:39:45 +0000875 for (HUseIterator<HInstruction*> it(uses_); !it.Done(); it.Advance()) {
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100876 ++result;
877 }
878 return result;
879 }
880
Roland Levillain6c82d402014-10-13 16:10:27 +0100881 // Does this instruction strictly dominate `other_instruction`?
882 // Returns false if this instruction and `other_instruction` are the same.
883 // Aborts if this instruction and `other_instruction` are both phis.
884 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +0100885
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000886 int GetId() const { return id_; }
887 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000888
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100889 int GetSsaIndex() const { return ssa_index_; }
890 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
891 bool HasSsaIndex() const { return ssa_index_ != -1; }
892
893 bool HasEnvironment() const { return environment_ != nullptr; }
894 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100895 void SetEnvironment(HEnvironment* environment) { environment_ = environment; }
896
Nicolas Geoffray39468442014-09-02 15:17:15 +0100897 // Returns the number of entries in the environment. Typically, that is the
898 // number of dex registers in a method. It could be more in case of inlining.
899 size_t EnvironmentSize() const;
900
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000901 LocationSummary* GetLocations() const { return locations_; }
902 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000903
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100904 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100905 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100906
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000907 // Insert `this` instruction in `cursor`'s graph, just before `cursor`.
908 void InsertBefore(HInstruction* cursor);
909
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100910#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +0100911 bool Is##type() const { return (As##type() != nullptr); } \
912 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000913 virtual H##type* As##type() { return nullptr; }
914
915 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
916#undef INSTRUCTION_TYPE_CHECK
917
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100918 // Returns whether the instruction can be moved within the graph.
919 virtual bool CanBeMoved() const { return false; }
920
921 // Returns whether the two instructions are of the same kind.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700922 virtual bool InstructionTypeEquals(HInstruction* other) const {
923 UNUSED(other);
924 return false;
925 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100926
927 // Returns whether any data encoded in the two instructions is equal.
928 // This method does not look at the inputs. Both instructions must be
929 // of the same type, otherwise the method has undefined behavior.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700930 virtual bool InstructionDataEquals(HInstruction* other) const {
931 UNUSED(other);
932 return false;
933 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100934
935 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +0000936 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100937 // 2) Their inputs are identical.
938 bool Equals(HInstruction* other) const;
939
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100940 virtual InstructionKind GetKind() const = 0;
941
942 virtual size_t ComputeHashCode() const {
943 size_t result = GetKind();
944 for (size_t i = 0, e = InputCount(); i < e; ++i) {
945 result = (result * 31) + InputAt(i)->GetId();
946 }
947 return result;
948 }
949
950 SideEffects GetSideEffects() const { return side_effects_; }
951
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100952 size_t GetLifetimePosition() const { return lifetime_position_; }
953 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
954 LiveInterval* GetLiveInterval() const { return live_interval_; }
955 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
956 bool HasLiveInterval() const { return live_interval_ != nullptr; }
957
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000958 private:
959 HInstruction* previous_;
960 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000961 HBasicBlock* block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000962
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000963 // An instruction gets an id when it is added to the graph.
964 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +0100965 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000966 int id_;
967
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100968 // When doing liveness analysis, instructions that have uses get an SSA index.
969 int ssa_index_;
970
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100971 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +0000972 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100973
974 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +0000975 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100976
Nicolas Geoffray39468442014-09-02 15:17:15 +0100977 // The environment associated with this instruction. Not null if the instruction
978 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100979 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000980
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000981 // Set by the code generator.
982 LocationSummary* locations_;
983
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100984 // Set by the liveness analysis.
985 LiveInterval* live_interval_;
986
987 // Set by the liveness analysis, this is the position in a linear
988 // order of blocks where this instruction's live interval start.
989 size_t lifetime_position_;
990
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100991 const SideEffects side_effects_;
992
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000993 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000994 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100995 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000996
997 DISALLOW_COPY_AND_ASSIGN(HInstruction);
998};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700999std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001000
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001001class HInputIterator : public ValueObject {
1002 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001003 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001004
1005 bool Done() const { return index_ == instruction_->InputCount(); }
1006 HInstruction* Current() const { return instruction_->InputAt(index_); }
1007 void Advance() { index_++; }
1008
1009 private:
1010 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001011 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001012
1013 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
1014};
1015
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001016class HInstructionIterator : public ValueObject {
1017 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001018 explicit HInstructionIterator(const HInstructionList& instructions)
1019 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001020 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001021 }
1022
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001023 bool Done() const { return instruction_ == nullptr; }
1024 HInstruction* Current() const { return instruction_; }
1025 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001026 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001027 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001028 }
1029
1030 private:
1031 HInstruction* instruction_;
1032 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001033
1034 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001035};
1036
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001037class HBackwardInstructionIterator : public ValueObject {
1038 public:
1039 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
1040 : instruction_(instructions.last_instruction_) {
1041 next_ = Done() ? nullptr : instruction_->GetPrevious();
1042 }
1043
1044 bool Done() const { return instruction_ == nullptr; }
1045 HInstruction* Current() const { return instruction_; }
1046 void Advance() {
1047 instruction_ = next_;
1048 next_ = Done() ? nullptr : instruction_->GetPrevious();
1049 }
1050
1051 private:
1052 HInstruction* instruction_;
1053 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001054
1055 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001056};
1057
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001058// An embedded container with N elements of type T. Used (with partial
1059// specialization for N=0) because embedded arrays cannot have size 0.
1060template<typename T, intptr_t N>
1061class EmbeddedArray {
1062 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001063 EmbeddedArray() : elements_() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001064
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001065 intptr_t GetLength() const { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001066
1067 const T& operator[](intptr_t i) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001068 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001069 return elements_[i];
1070 }
1071
1072 T& operator[](intptr_t i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001073 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001074 return elements_[i];
1075 }
1076
1077 const T& At(intptr_t i) const {
1078 return (*this)[i];
1079 }
1080
1081 void SetAt(intptr_t i, const T& val) {
1082 (*this)[i] = val;
1083 }
1084
1085 private:
1086 T elements_[N];
1087};
1088
1089template<typename T>
1090class EmbeddedArray<T, 0> {
1091 public:
1092 intptr_t length() const { return 0; }
1093 const T& operator[](intptr_t i) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001094 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001095 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001096 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001097 }
1098 T& operator[](intptr_t i) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001099 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001100 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001101 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001102 }
1103};
1104
1105template<intptr_t N>
1106class HTemplateInstruction: public HInstruction {
1107 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001108 HTemplateInstruction<N>(SideEffects side_effects)
1109 : HInstruction(side_effects), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07001110 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001111
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001112 virtual size_t InputCount() const { return N; }
1113 virtual HInstruction* InputAt(size_t i) const { return inputs_[i]; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001114
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001115 protected:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001116 virtual void SetRawInputAt(size_t i, HInstruction* instruction) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001117 inputs_[i] = instruction;
1118 }
1119
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001120 private:
1121 EmbeddedArray<HInstruction*, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001122
1123 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001124};
1125
Dave Allison20dfc792014-06-16 20:44:29 -07001126template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001127class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07001128 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001129 HExpression<N>(Primitive::Type type, SideEffects side_effects)
1130 : HTemplateInstruction<N>(side_effects), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07001131 virtual ~HExpression() {}
1132
1133 virtual Primitive::Type GetType() const { return type_; }
1134
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001135 protected:
1136 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07001137};
1138
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001139// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
1140// instruction that branches to the exit block.
1141class HReturnVoid : public HTemplateInstruction<0> {
1142 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001143 HReturnVoid() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001144
1145 virtual bool IsControlFlow() const { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001146
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001147 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001148
1149 private:
1150 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
1151};
1152
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001153// Represents dex's RETURN opcodes. A HReturn is a control flow
1154// instruction that branches to the exit block.
1155class HReturn : public HTemplateInstruction<1> {
1156 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001157 explicit HReturn(HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001158 SetRawInputAt(0, value);
1159 }
1160
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001161 virtual bool IsControlFlow() const { return true; }
1162
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001163 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001164
1165 private:
1166 DISALLOW_COPY_AND_ASSIGN(HReturn);
1167};
1168
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001169// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001170// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001171// exit block.
1172class HExit : public HTemplateInstruction<0> {
1173 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001174 HExit() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001175
1176 virtual bool IsControlFlow() const { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001177
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001178 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001179
1180 private:
1181 DISALLOW_COPY_AND_ASSIGN(HExit);
1182};
1183
1184// Jumps from one block to another.
1185class HGoto : public HTemplateInstruction<0> {
1186 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001187 HGoto() : HTemplateInstruction(SideEffects::None()) {}
1188
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001189 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001190
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001191 HBasicBlock* GetSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001192 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001193 }
1194
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001195 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001196
1197 private:
1198 DISALLOW_COPY_AND_ASSIGN(HGoto);
1199};
1200
Dave Allison20dfc792014-06-16 20:44:29 -07001201
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001202// Conditional branch. A block ending with an HIf instruction must have
1203// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001204class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001205 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001206 explicit HIf(HInstruction* input) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001207 SetRawInputAt(0, input);
1208 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001209
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001210 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001211
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001212 HBasicBlock* IfTrueSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001213 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001214 }
1215
1216 HBasicBlock* IfFalseSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001217 return GetBlock()->GetSuccessors().Get(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001218 }
1219
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001220 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001221
Dave Allison20dfc792014-06-16 20:44:29 -07001222 virtual bool IsIfInstruction() const { return true; }
1223
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001224 private:
1225 DISALLOW_COPY_AND_ASSIGN(HIf);
1226};
1227
Roland Levillain88cb1752014-10-20 16:36:47 +01001228class HUnaryOperation : public HExpression<1> {
1229 public:
1230 HUnaryOperation(Primitive::Type result_type, HInstruction* input)
1231 : HExpression(result_type, SideEffects::None()) {
1232 SetRawInputAt(0, input);
1233 }
1234
1235 HInstruction* GetInput() const { return InputAt(0); }
1236 Primitive::Type GetResultType() const { return GetType(); }
1237
1238 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001239 virtual bool InstructionDataEquals(HInstruction* other) const {
1240 UNUSED(other);
1241 return true;
1242 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001243
Roland Levillain9240d6a2014-10-20 16:47:04 +01001244 // Try to statically evaluate `operation` and return a HConstant
1245 // containing the result of this evaluation. If `operation` cannot
1246 // be evaluated as a constant, return nullptr.
1247 HConstant* TryStaticEvaluation() const;
1248
1249 // Apply this operation to `x`.
1250 virtual int32_t Evaluate(int32_t x) const = 0;
1251 virtual int64_t Evaluate(int64_t x) const = 0;
1252
Roland Levillain88cb1752014-10-20 16:36:47 +01001253 DECLARE_INSTRUCTION(UnaryOperation);
1254
1255 private:
1256 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
1257};
1258
Dave Allison20dfc792014-06-16 20:44:29 -07001259class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001260 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001261 HBinaryOperation(Primitive::Type result_type,
1262 HInstruction* left,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001263 HInstruction* right) : HExpression(result_type, SideEffects::None()) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001264 SetRawInputAt(0, left);
1265 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001266 }
1267
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001268 HInstruction* GetLeft() const { return InputAt(0); }
1269 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07001270 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001271
1272 virtual bool IsCommutative() { return false; }
1273
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001274 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001275 virtual bool InstructionDataEquals(HInstruction* other) const {
1276 UNUSED(other);
1277 return true;
1278 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001279
Roland Levillain9240d6a2014-10-20 16:47:04 +01001280 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01001281 // containing the result of this evaluation. If `operation` cannot
1282 // be evaluated as a constant, return nullptr.
Roland Levillain9240d6a2014-10-20 16:47:04 +01001283 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01001284
1285 // Apply this operation to `x` and `y`.
1286 virtual int32_t Evaluate(int32_t x, int32_t y) const = 0;
1287 virtual int64_t Evaluate(int64_t x, int64_t y) const = 0;
1288
Roland Levillainccc07a92014-09-16 14:48:16 +01001289 DECLARE_INSTRUCTION(BinaryOperation);
1290
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001291 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001292 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
1293};
1294
Dave Allison20dfc792014-06-16 20:44:29 -07001295class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001296 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001297 HCondition(HInstruction* first, HInstruction* second)
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001298 : HBinaryOperation(Primitive::kPrimBoolean, first, second),
1299 needs_materialization_(true) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001300
1301 virtual bool IsCommutative() { return true; }
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001302
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001303 bool NeedsMaterialization() const { return needs_materialization_; }
1304 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001305
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001306 // For code generation purposes, returns whether this instruction is just before
1307 // `if_`, and disregard moves in between.
1308 bool IsBeforeWhenDisregardMoves(HIf* if_) const;
1309
Dave Allison20dfc792014-06-16 20:44:29 -07001310 DECLARE_INSTRUCTION(Condition);
1311
1312 virtual IfCondition GetCondition() const = 0;
1313
1314 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001315 // For register allocation purposes, returns whether this instruction needs to be
1316 // materialized (that is, not just be in the processor flags).
1317 bool needs_materialization_;
1318
Dave Allison20dfc792014-06-16 20:44:29 -07001319 DISALLOW_COPY_AND_ASSIGN(HCondition);
1320};
1321
1322// Instruction to check if two inputs are equal to each other.
1323class HEqual : public HCondition {
1324 public:
1325 HEqual(HInstruction* first, HInstruction* second)
1326 : HCondition(first, second) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001327
Roland Levillain93445682014-10-06 19:24:02 +01001328 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1329 return x == y ? 1 : 0;
1330 }
1331 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1332 return x == y ? 1 : 0;
1333 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001334
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001335 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001336
Dave Allison20dfc792014-06-16 20:44:29 -07001337 virtual IfCondition GetCondition() const {
1338 return kCondEQ;
1339 }
1340
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001341 private:
1342 DISALLOW_COPY_AND_ASSIGN(HEqual);
1343};
1344
Dave Allison20dfc792014-06-16 20:44:29 -07001345class HNotEqual : public HCondition {
1346 public:
1347 HNotEqual(HInstruction* first, HInstruction* second)
1348 : HCondition(first, second) {}
1349
Roland Levillain93445682014-10-06 19:24:02 +01001350 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1351 return x != y ? 1 : 0;
1352 }
1353 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1354 return x != y ? 1 : 0;
1355 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001356
Dave Allison20dfc792014-06-16 20:44:29 -07001357 DECLARE_INSTRUCTION(NotEqual);
1358
1359 virtual IfCondition GetCondition() const {
1360 return kCondNE;
1361 }
1362
1363 private:
1364 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
1365};
1366
1367class HLessThan : public HCondition {
1368 public:
1369 HLessThan(HInstruction* first, HInstruction* second)
1370 : HCondition(first, second) {}
1371
Roland Levillain93445682014-10-06 19:24:02 +01001372 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1373 return x < y ? 1 : 0;
1374 }
1375 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1376 return x < y ? 1 : 0;
1377 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001378
Dave Allison20dfc792014-06-16 20:44:29 -07001379 DECLARE_INSTRUCTION(LessThan);
1380
1381 virtual IfCondition GetCondition() const {
1382 return kCondLT;
1383 }
1384
1385 private:
1386 DISALLOW_COPY_AND_ASSIGN(HLessThan);
1387};
1388
1389class HLessThanOrEqual : public HCondition {
1390 public:
1391 HLessThanOrEqual(HInstruction* first, HInstruction* second)
1392 : HCondition(first, second) {}
1393
Roland Levillain93445682014-10-06 19:24:02 +01001394 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1395 return x <= y ? 1 : 0;
1396 }
1397 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1398 return x <= y ? 1 : 0;
1399 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001400
Dave Allison20dfc792014-06-16 20:44:29 -07001401 DECLARE_INSTRUCTION(LessThanOrEqual);
1402
1403 virtual IfCondition GetCondition() const {
1404 return kCondLE;
1405 }
1406
1407 private:
1408 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
1409};
1410
1411class HGreaterThan : public HCondition {
1412 public:
1413 HGreaterThan(HInstruction* first, HInstruction* second)
1414 : HCondition(first, second) {}
1415
Roland Levillain93445682014-10-06 19:24:02 +01001416 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1417 return x > y ? 1 : 0;
1418 }
1419 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1420 return x > y ? 1 : 0;
1421 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001422
Dave Allison20dfc792014-06-16 20:44:29 -07001423 DECLARE_INSTRUCTION(GreaterThan);
1424
1425 virtual IfCondition GetCondition() const {
1426 return kCondGT;
1427 }
1428
1429 private:
1430 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
1431};
1432
1433class HGreaterThanOrEqual : public HCondition {
1434 public:
1435 HGreaterThanOrEqual(HInstruction* first, HInstruction* second)
1436 : HCondition(first, second) {}
1437
Roland Levillain93445682014-10-06 19:24:02 +01001438 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1439 return x >= y ? 1 : 0;
1440 }
1441 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1442 return x >= y ? 1 : 0;
1443 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001444
Dave Allison20dfc792014-06-16 20:44:29 -07001445 DECLARE_INSTRUCTION(GreaterThanOrEqual);
1446
1447 virtual IfCondition GetCondition() const {
1448 return kCondGE;
1449 }
1450
1451 private:
1452 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
1453};
1454
1455
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001456// Instruction to check how two inputs compare to each other.
1457// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
1458class HCompare : public HBinaryOperation {
1459 public:
Calin Juravleddb7df22014-11-25 20:56:51 +00001460 // The bias applies for floating point operations and indicates how NaN
1461 // comparisons are treated:
1462 enum Bias {
1463 kNoBias, // bias is not applicable (i.e. for long operation)
1464 kGtBias, // return 1 for NaN comparisons
1465 kLtBias, // return -1 for NaN comparisons
1466 };
1467
1468 HCompare(Primitive::Type type, HInstruction* first, HInstruction* second, Bias bias)
1469 : HBinaryOperation(Primitive::kPrimInt, first, second), bias_(bias) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001470 DCHECK_EQ(type, first->GetType());
1471 DCHECK_EQ(type, second->GetType());
1472 }
1473
Calin Juravleddb7df22014-11-25 20:56:51 +00001474 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001475 return
1476 x == y ? 0 :
1477 x > y ? 1 :
1478 -1;
1479 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001480
1481 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001482 return
1483 x == y ? 0 :
1484 x > y ? 1 :
1485 -1;
1486 }
1487
Calin Juravleddb7df22014-11-25 20:56:51 +00001488 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
1489 return bias_ == other->AsCompare()->bias_;
1490 }
1491
1492 bool IsGtBias() { return bias_ == kGtBias; }
1493
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001494 DECLARE_INSTRUCTION(Compare);
1495
1496 private:
Calin Juravleddb7df22014-11-25 20:56:51 +00001497 const Bias bias_;
1498
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001499 DISALLOW_COPY_AND_ASSIGN(HCompare);
1500};
1501
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001502// A local in the graph. Corresponds to a Dex register.
1503class HLocal : public HTemplateInstruction<0> {
1504 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001505 explicit HLocal(uint16_t reg_number)
1506 : HTemplateInstruction(SideEffects::None()), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001507
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001508 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001509
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001510 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001511
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001512 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001513 // The Dex register number.
1514 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001515
1516 DISALLOW_COPY_AND_ASSIGN(HLocal);
1517};
1518
1519// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07001520class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001521 public:
Roland Levillain5799fc02014-09-25 12:15:20 +01001522 HLoadLocal(HLocal* local, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001523 : HExpression(type, SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001524 SetRawInputAt(0, local);
1525 }
1526
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001527 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1528
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001529 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001530
1531 private:
1532 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
1533};
1534
1535// Store a value in a given local. This instruction has two inputs: the value
1536// and the local.
1537class HStoreLocal : public HTemplateInstruction<2> {
1538 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001539 HStoreLocal(HLocal* local, HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001540 SetRawInputAt(0, local);
1541 SetRawInputAt(1, value);
1542 }
1543
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001544 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1545
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001546 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001547
1548 private:
1549 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
1550};
1551
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001552class HConstant : public HExpression<0> {
1553 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001554 explicit HConstant(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
1555
1556 virtual bool CanBeMoved() const { return true; }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001557
1558 DECLARE_INSTRUCTION(Constant);
1559
1560 private:
1561 DISALLOW_COPY_AND_ASSIGN(HConstant);
1562};
1563
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001564class HFloatConstant : public HConstant {
1565 public:
1566 explicit HFloatConstant(float value) : HConstant(Primitive::kPrimFloat), value_(value) {}
1567
1568 float GetValue() const { return value_; }
1569
1570 virtual bool InstructionDataEquals(HInstruction* other) const {
1571 return bit_cast<float, int32_t>(other->AsFloatConstant()->value_) ==
1572 bit_cast<float, int32_t>(value_);
1573 }
1574
1575 virtual size_t ComputeHashCode() const { return static_cast<size_t>(GetValue()); }
1576
1577 DECLARE_INSTRUCTION(FloatConstant);
1578
1579 private:
1580 const float value_;
1581
1582 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
1583};
1584
1585class HDoubleConstant : public HConstant {
1586 public:
1587 explicit HDoubleConstant(double value) : HConstant(Primitive::kPrimDouble), value_(value) {}
1588
1589 double GetValue() const { return value_; }
1590
1591 virtual bool InstructionDataEquals(HInstruction* other) const {
1592 return bit_cast<double, int64_t>(other->AsDoubleConstant()->value_) ==
1593 bit_cast<double, int64_t>(value_);
1594 }
1595
1596 virtual size_t ComputeHashCode() const { return static_cast<size_t>(GetValue()); }
1597
1598 DECLARE_INSTRUCTION(DoubleConstant);
1599
1600 private:
1601 const double value_;
1602
1603 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
1604};
1605
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001606// Constants of the type int. Those can be from Dex instructions, or
1607// synthesized (for example with the if-eqz instruction).
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001608class HIntConstant : public HConstant {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001609 public:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001610 explicit HIntConstant(int32_t value) : HConstant(Primitive::kPrimInt), value_(value) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001611
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001612 int32_t GetValue() const { return value_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001613
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001614 virtual bool InstructionDataEquals(HInstruction* other) const {
1615 return other->AsIntConstant()->value_ == value_;
1616 }
1617
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001618 virtual size_t ComputeHashCode() const { return GetValue(); }
1619
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001620 DECLARE_INSTRUCTION(IntConstant);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001621
1622 private:
1623 const int32_t value_;
1624
1625 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
1626};
1627
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001628class HLongConstant : public HConstant {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001629 public:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001630 explicit HLongConstant(int64_t value) : HConstant(Primitive::kPrimLong), value_(value) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001631
1632 int64_t GetValue() const { return value_; }
1633
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001634 virtual bool InstructionDataEquals(HInstruction* other) const {
1635 return other->AsLongConstant()->value_ == value_;
1636 }
1637
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001638 virtual size_t ComputeHashCode() const { return static_cast<size_t>(GetValue()); }
1639
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001640 DECLARE_INSTRUCTION(LongConstant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001641
1642 private:
1643 const int64_t value_;
1644
1645 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
1646};
1647
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001648enum class Intrinsics {
1649#define OPTIMIZING_INTRINSICS(Name, IsStatic) k ## Name,
1650#include "intrinsics_list.h"
1651 kNone,
1652 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
1653#undef INTRINSICS_LIST
1654#undef OPTIMIZING_INTRINSICS
1655};
1656std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
1657
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001658class HInvoke : public HInstruction {
1659 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001660 virtual size_t InputCount() const { return inputs_.Size(); }
1661 virtual HInstruction* InputAt(size_t i) const { return inputs_.Get(i); }
1662
1663 // Runtime needs to walk the stack, so Dex -> Dex calls need to
1664 // know their environment.
Calin Juravle77520bc2015-01-12 18:45:46 +00001665 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001666
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001667 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001668 SetRawInputAt(index, argument);
1669 }
1670
1671 virtual void SetRawInputAt(size_t index, HInstruction* input) {
1672 inputs_.Put(index, input);
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001673 }
1674
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001675 virtual Primitive::Type GetType() const { return return_type_; }
1676
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001677 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001678
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001679 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
1680
1681 Intrinsics GetIntrinsic() {
1682 return intrinsic_;
1683 }
1684
1685 void SetIntrinsic(Intrinsics intrinsic) {
1686 intrinsic_ = intrinsic;
1687 }
1688
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001689 DECLARE_INSTRUCTION(Invoke);
1690
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001691 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001692 HInvoke(ArenaAllocator* arena,
1693 uint32_t number_of_arguments,
1694 Primitive::Type return_type,
1695 uint32_t dex_pc,
1696 uint32_t dex_method_index)
1697 : HInstruction(SideEffects::All()),
1698 inputs_(arena, number_of_arguments),
1699 return_type_(return_type),
1700 dex_pc_(dex_pc),
1701 dex_method_index_(dex_method_index),
1702 intrinsic_(Intrinsics::kNone) {
1703 inputs_.SetSize(number_of_arguments);
1704 }
1705
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001706 GrowableArray<HInstruction*> inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001707 const Primitive::Type return_type_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001708 const uint32_t dex_pc_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001709 const uint32_t dex_method_index_;
1710 Intrinsics intrinsic_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001711
1712 private:
1713 DISALLOW_COPY_AND_ASSIGN(HInvoke);
1714};
1715
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001716class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001717 public:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001718 HInvokeStaticOrDirect(ArenaAllocator* arena,
1719 uint32_t number_of_arguments,
1720 Primitive::Type return_type,
1721 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001722 uint32_t dex_method_index,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001723 InvokeType invoke_type)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001724 : HInvoke(arena, number_of_arguments, return_type, dex_pc, dex_method_index),
Nicolas Geoffray4e44c822014-12-17 12:25:12 +00001725 invoke_type_(invoke_type) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001726
Calin Juravle77520bc2015-01-12 18:45:46 +00001727 bool CanDoImplicitNullCheck() const OVERRIDE {
1728 // We access the method via the dex cache so we can't do an implicit null check.
1729 // TODO: for intrinsics we can generate implicit null checks.
1730 return false;
1731 }
1732
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001733 InvokeType GetInvokeType() const { return invoke_type_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001734
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001735 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001736
1737 private:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001738 const InvokeType invoke_type_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001739
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001740 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001741};
1742
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001743class HInvokeVirtual : public HInvoke {
1744 public:
1745 HInvokeVirtual(ArenaAllocator* arena,
1746 uint32_t number_of_arguments,
1747 Primitive::Type return_type,
1748 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001749 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001750 uint32_t vtable_index)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001751 : HInvoke(arena, number_of_arguments, return_type, dex_pc, dex_method_index),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001752 vtable_index_(vtable_index) {}
1753
Calin Juravle77520bc2015-01-12 18:45:46 +00001754 bool CanDoImplicitNullCheck() const OVERRIDE {
1755 // TODO: Add implicit null checks in intrinsics.
1756 return !GetLocations()->Intrinsified();
1757 }
1758
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001759 uint32_t GetVTableIndex() const { return vtable_index_; }
1760
1761 DECLARE_INSTRUCTION(InvokeVirtual);
1762
1763 private:
1764 const uint32_t vtable_index_;
1765
1766 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
1767};
1768
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001769class HInvokeInterface : public HInvoke {
1770 public:
1771 HInvokeInterface(ArenaAllocator* arena,
1772 uint32_t number_of_arguments,
1773 Primitive::Type return_type,
1774 uint32_t dex_pc,
1775 uint32_t dex_method_index,
1776 uint32_t imt_index)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001777 : HInvoke(arena, number_of_arguments, return_type, dex_pc, dex_method_index),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001778 imt_index_(imt_index) {}
1779
Calin Juravle77520bc2015-01-12 18:45:46 +00001780 bool CanDoImplicitNullCheck() const OVERRIDE {
1781 // TODO: Add implicit null checks in intrinsics.
1782 return !GetLocations()->Intrinsified();
1783 }
1784
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001785 uint32_t GetImtIndex() const { return imt_index_; }
1786 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
1787
1788 DECLARE_INSTRUCTION(InvokeInterface);
1789
1790 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001791 const uint32_t imt_index_;
1792
1793 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
1794};
1795
Dave Allison20dfc792014-06-16 20:44:29 -07001796class HNewInstance : public HExpression<0> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001797 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001798 HNewInstance(uint32_t dex_pc, uint16_t type_index)
1799 : HExpression(Primitive::kPrimNot, SideEffects::None()),
1800 dex_pc_(dex_pc),
1801 type_index_(type_index) {}
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001802
1803 uint32_t GetDexPc() const { return dex_pc_; }
1804 uint16_t GetTypeIndex() const { return type_index_; }
1805
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001806 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00001807 bool NeedsEnvironment() const OVERRIDE { return true; }
1808 // It may throw when called on:
1809 // - interfaces
1810 // - abstract/innaccessible/unknown classes
1811 // TODO: optimize when possible.
1812 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001813
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001814 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001815
1816 private:
1817 const uint32_t dex_pc_;
1818 const uint16_t type_index_;
1819
1820 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
1821};
1822
Roland Levillain88cb1752014-10-20 16:36:47 +01001823class HNeg : public HUnaryOperation {
1824 public:
1825 explicit HNeg(Primitive::Type result_type, HInstruction* input)
1826 : HUnaryOperation(result_type, input) {}
1827
Roland Levillain9240d6a2014-10-20 16:47:04 +01001828 virtual int32_t Evaluate(int32_t x) const OVERRIDE { return -x; }
1829 virtual int64_t Evaluate(int64_t x) const OVERRIDE { return -x; }
1830
Roland Levillain88cb1752014-10-20 16:36:47 +01001831 DECLARE_INSTRUCTION(Neg);
1832
1833 private:
1834 DISALLOW_COPY_AND_ASSIGN(HNeg);
1835};
1836
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001837class HNewArray : public HExpression<1> {
1838 public:
1839 HNewArray(HInstruction* length, uint32_t dex_pc, uint16_t type_index)
1840 : HExpression(Primitive::kPrimNot, SideEffects::None()),
1841 dex_pc_(dex_pc),
1842 type_index_(type_index) {
1843 SetRawInputAt(0, length);
1844 }
1845
1846 uint32_t GetDexPc() const { return dex_pc_; }
1847 uint16_t GetTypeIndex() const { return type_index_; }
1848
1849 // Calls runtime so needs an environment.
1850 virtual bool NeedsEnvironment() const { return true; }
1851
1852 DECLARE_INSTRUCTION(NewArray);
1853
1854 private:
1855 const uint32_t dex_pc_;
1856 const uint16_t type_index_;
1857
1858 DISALLOW_COPY_AND_ASSIGN(HNewArray);
1859};
1860
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001861class HAdd : public HBinaryOperation {
1862 public:
1863 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1864 : HBinaryOperation(result_type, left, right) {}
1865
1866 virtual bool IsCommutative() { return true; }
1867
Roland Levillain93445682014-10-06 19:24:02 +01001868 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1869 return x + y;
1870 }
1871 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1872 return x + y;
1873 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001874
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001875 DECLARE_INSTRUCTION(Add);
1876
1877 private:
1878 DISALLOW_COPY_AND_ASSIGN(HAdd);
1879};
1880
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001881class HSub : public HBinaryOperation {
1882 public:
1883 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1884 : HBinaryOperation(result_type, left, right) {}
1885
Roland Levillain93445682014-10-06 19:24:02 +01001886 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1887 return x - y;
1888 }
1889 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1890 return x - y;
1891 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001892
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001893 DECLARE_INSTRUCTION(Sub);
1894
1895 private:
1896 DISALLOW_COPY_AND_ASSIGN(HSub);
1897};
1898
Calin Juravle34bacdf2014-10-07 20:23:36 +01001899class HMul : public HBinaryOperation {
1900 public:
1901 HMul(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1902 : HBinaryOperation(result_type, left, right) {}
1903
1904 virtual bool IsCommutative() { return true; }
1905
1906 virtual int32_t Evaluate(int32_t x, int32_t y) const { return x * y; }
1907 virtual int64_t Evaluate(int64_t x, int64_t y) const { return x * y; }
1908
1909 DECLARE_INSTRUCTION(Mul);
1910
1911 private:
1912 DISALLOW_COPY_AND_ASSIGN(HMul);
1913};
1914
Calin Juravle7c4954d2014-10-28 16:57:40 +00001915class HDiv : public HBinaryOperation {
1916 public:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001917 HDiv(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
1918 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00001919
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00001920 virtual int32_t Evaluate(int32_t x, int32_t y) const {
1921 // Our graph structure ensures we never have 0 for `y` during constant folding.
1922 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00001923 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00001924 return (y == -1) ? -x : x / y;
1925 }
Calin Juravlebacfec32014-11-14 15:54:36 +00001926
1927 virtual int64_t Evaluate(int64_t x, int64_t y) const {
1928 DCHECK_NE(y, 0);
1929 // Special case -1 to avoid getting a SIGFPE on x86(_64).
1930 return (y == -1) ? -x : x / y;
1931 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00001932
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001933 uint32_t GetDexPc() const { return dex_pc_; }
1934
Calin Juravle7c4954d2014-10-28 16:57:40 +00001935 DECLARE_INSTRUCTION(Div);
1936
1937 private:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001938 const uint32_t dex_pc_;
1939
Calin Juravle7c4954d2014-10-28 16:57:40 +00001940 DISALLOW_COPY_AND_ASSIGN(HDiv);
1941};
1942
Calin Juravlebacfec32014-11-14 15:54:36 +00001943class HRem : public HBinaryOperation {
1944 public:
1945 HRem(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
1946 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
1947
1948 virtual int32_t Evaluate(int32_t x, int32_t y) const {
1949 DCHECK_NE(y, 0);
1950 // Special case -1 to avoid getting a SIGFPE on x86(_64).
1951 return (y == -1) ? 0 : x % y;
1952 }
1953
1954 virtual int64_t Evaluate(int64_t x, int64_t y) const {
1955 DCHECK_NE(y, 0);
1956 // Special case -1 to avoid getting a SIGFPE on x86(_64).
1957 return (y == -1) ? 0 : x % y;
1958 }
1959
1960 uint32_t GetDexPc() const { return dex_pc_; }
1961
1962 DECLARE_INSTRUCTION(Rem);
1963
1964 private:
1965 const uint32_t dex_pc_;
1966
1967 DISALLOW_COPY_AND_ASSIGN(HRem);
1968};
1969
Calin Juravled0d48522014-11-04 16:40:20 +00001970class HDivZeroCheck : public HExpression<1> {
1971 public:
1972 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
1973 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
1974 SetRawInputAt(0, value);
1975 }
1976
1977 bool CanBeMoved() const OVERRIDE { return true; }
1978
1979 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
1980 UNUSED(other);
1981 return true;
1982 }
1983
1984 bool NeedsEnvironment() const OVERRIDE { return true; }
1985 bool CanThrow() const OVERRIDE { return true; }
1986
1987 uint32_t GetDexPc() const { return dex_pc_; }
1988
1989 DECLARE_INSTRUCTION(DivZeroCheck);
1990
1991 private:
1992 const uint32_t dex_pc_;
1993
1994 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
1995};
1996
Calin Juravle9aec02f2014-11-18 23:06:35 +00001997class HShl : public HBinaryOperation {
1998 public:
1999 HShl(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2000 : HBinaryOperation(result_type, left, right) {}
2001
2002 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x << (y & kMaxIntShiftValue); }
2003 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x << (y & kMaxLongShiftValue); }
2004
2005 DECLARE_INSTRUCTION(Shl);
2006
2007 private:
2008 DISALLOW_COPY_AND_ASSIGN(HShl);
2009};
2010
2011class HShr : public HBinaryOperation {
2012 public:
2013 HShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2014 : HBinaryOperation(result_type, left, right) {}
2015
2016 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x >> (y & kMaxIntShiftValue); }
2017 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x >> (y & kMaxLongShiftValue); }
2018
2019 DECLARE_INSTRUCTION(Shr);
2020
2021 private:
2022 DISALLOW_COPY_AND_ASSIGN(HShr);
2023};
2024
2025class HUShr : public HBinaryOperation {
2026 public:
2027 HUShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2028 : HBinaryOperation(result_type, left, right) {}
2029
2030 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
2031 uint32_t ux = static_cast<uint32_t>(x);
2032 uint32_t uy = static_cast<uint32_t>(y) & kMaxIntShiftValue;
2033 return static_cast<int32_t>(ux >> uy);
2034 }
2035
2036 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
2037 uint64_t ux = static_cast<uint64_t>(x);
2038 uint64_t uy = static_cast<uint64_t>(y) & kMaxLongShiftValue;
2039 return static_cast<int64_t>(ux >> uy);
2040 }
2041
2042 DECLARE_INSTRUCTION(UShr);
2043
2044 private:
2045 DISALLOW_COPY_AND_ASSIGN(HUShr);
2046};
2047
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002048class HAnd : public HBinaryOperation {
2049 public:
2050 HAnd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2051 : HBinaryOperation(result_type, left, right) {}
2052
2053 bool IsCommutative() OVERRIDE { return true; }
2054
2055 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x & y; }
2056 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x & y; }
2057
2058 DECLARE_INSTRUCTION(And);
2059
2060 private:
2061 DISALLOW_COPY_AND_ASSIGN(HAnd);
2062};
2063
2064class HOr : public HBinaryOperation {
2065 public:
2066 HOr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2067 : HBinaryOperation(result_type, left, right) {}
2068
2069 bool IsCommutative() OVERRIDE { return true; }
2070
2071 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x | y; }
2072 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x | y; }
2073
2074 DECLARE_INSTRUCTION(Or);
2075
2076 private:
2077 DISALLOW_COPY_AND_ASSIGN(HOr);
2078};
2079
2080class HXor : public HBinaryOperation {
2081 public:
2082 HXor(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2083 : HBinaryOperation(result_type, left, right) {}
2084
2085 bool IsCommutative() OVERRIDE { return true; }
2086
2087 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x ^ y; }
2088 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x ^ y; }
2089
2090 DECLARE_INSTRUCTION(Xor);
2091
2092 private:
2093 DISALLOW_COPY_AND_ASSIGN(HXor);
2094};
2095
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002096// The value of a parameter in this method. Its location depends on
2097// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07002098class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002099 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002100 HParameterValue(uint8_t index, Primitive::Type parameter_type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002101 : HExpression(parameter_type, SideEffects::None()), index_(index) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002102
2103 uint8_t GetIndex() const { return index_; }
2104
2105 DECLARE_INSTRUCTION(ParameterValue);
2106
2107 private:
2108 // The index of this parameter in the parameters list. Must be less
2109 // than HGraph::number_of_in_vregs_;
2110 const uint8_t index_;
2111
2112 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
2113};
2114
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002115class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002116 public:
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002117 explicit HNot(Primitive::Type result_type, HInstruction* input)
2118 : HUnaryOperation(result_type, input) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002119
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002120 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002121 virtual bool InstructionDataEquals(HInstruction* other) const {
2122 UNUSED(other);
2123 return true;
2124 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002125
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002126 virtual int32_t Evaluate(int32_t x) const OVERRIDE { return ~x; }
2127 virtual int64_t Evaluate(int64_t x) const OVERRIDE { return ~x; }
2128
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002129 DECLARE_INSTRUCTION(Not);
2130
2131 private:
2132 DISALLOW_COPY_AND_ASSIGN(HNot);
2133};
2134
Roland Levillaindff1f282014-11-05 14:15:05 +00002135class HTypeConversion : public HExpression<1> {
2136 public:
2137 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00002138 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
2139 : HExpression(result_type, SideEffects::None()), dex_pc_(dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002140 SetRawInputAt(0, input);
2141 DCHECK_NE(input->GetType(), result_type);
2142 }
2143
2144 HInstruction* GetInput() const { return InputAt(0); }
2145 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
2146 Primitive::Type GetResultType() const { return GetType(); }
2147
Roland Levillain624279f2014-12-04 11:54:28 +00002148 // Required by the x86 and ARM code generators when producing calls
2149 // to the runtime.
2150 uint32_t GetDexPc() const { return dex_pc_; }
2151
Roland Levillaindff1f282014-11-05 14:15:05 +00002152 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00002153 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00002154
2155 DECLARE_INSTRUCTION(TypeConversion);
2156
2157 private:
Roland Levillain624279f2014-12-04 11:54:28 +00002158 const uint32_t dex_pc_;
2159
Roland Levillaindff1f282014-11-05 14:15:05 +00002160 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
2161};
2162
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002163class HPhi : public HInstruction {
2164 public:
2165 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002166 : HInstruction(SideEffects::None()),
2167 inputs_(arena, number_of_inputs),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002168 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002169 type_(type),
2170 is_live_(false) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002171 inputs_.SetSize(number_of_inputs);
2172 }
2173
2174 virtual size_t InputCount() const { return inputs_.Size(); }
2175 virtual HInstruction* InputAt(size_t i) const { return inputs_.Get(i); }
2176
2177 virtual void SetRawInputAt(size_t index, HInstruction* input) {
2178 inputs_.Put(index, input);
2179 }
2180
2181 void AddInput(HInstruction* input);
2182
2183 virtual Primitive::Type GetType() const { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002184 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002185
2186 uint32_t GetRegNumber() const { return reg_number_; }
2187
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002188 void SetDead() { is_live_ = false; }
2189 void SetLive() { is_live_ = true; }
2190 bool IsDead() const { return !is_live_; }
2191 bool IsLive() const { return is_live_; }
2192
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002193 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002194
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002195 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002196 GrowableArray<HInstruction*> inputs_;
2197 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002198 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002199 bool is_live_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002200
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002201 DISALLOW_COPY_AND_ASSIGN(HPhi);
2202};
2203
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002204class HNullCheck : public HExpression<1> {
2205 public:
2206 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002207 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002208 SetRawInputAt(0, value);
2209 }
2210
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002211 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002212 virtual bool InstructionDataEquals(HInstruction* other) const {
2213 UNUSED(other);
2214 return true;
2215 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002216
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002217 virtual bool NeedsEnvironment() const { return true; }
2218
Roland Levillaine161a2a2014-10-03 12:45:18 +01002219 virtual bool CanThrow() const { return true; }
2220
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002221 uint32_t GetDexPc() const { return dex_pc_; }
2222
2223 DECLARE_INSTRUCTION(NullCheck);
2224
2225 private:
2226 const uint32_t dex_pc_;
2227
2228 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
2229};
2230
2231class FieldInfo : public ValueObject {
2232 public:
Calin Juravle52c48962014-12-16 17:02:57 +00002233 FieldInfo(MemberOffset field_offset, Primitive::Type field_type, bool is_volatile)
2234 : field_offset_(field_offset), field_type_(field_type), is_volatile_(is_volatile) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002235
2236 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002237 Primitive::Type GetFieldType() const { return field_type_; }
Calin Juravle52c48962014-12-16 17:02:57 +00002238 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002239
2240 private:
2241 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01002242 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00002243 const bool is_volatile_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002244};
2245
2246class HInstanceFieldGet : public HExpression<1> {
2247 public:
2248 HInstanceFieldGet(HInstruction* value,
2249 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00002250 MemberOffset field_offset,
2251 bool is_volatile)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002252 : HExpression(field_type, SideEffects::DependsOnSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00002253 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002254 SetRawInputAt(0, value);
2255 }
2256
Calin Juravle10c9cbe2014-12-19 10:50:19 +00002257 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002258
2259 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2260 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
2261 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002262 }
2263
Calin Juravle77520bc2015-01-12 18:45:46 +00002264 bool CanDoImplicitNullCheck() const OVERRIDE {
2265 return GetFieldOffset().Uint32Value() < kPageSize;
2266 }
2267
2268 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002269 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
2270 }
2271
Calin Juravle52c48962014-12-16 17:02:57 +00002272 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002273 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002274 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002275 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002276
2277 DECLARE_INSTRUCTION(InstanceFieldGet);
2278
2279 private:
2280 const FieldInfo field_info_;
2281
2282 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
2283};
2284
2285class HInstanceFieldSet : public HTemplateInstruction<2> {
2286 public:
2287 HInstanceFieldSet(HInstruction* object,
2288 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01002289 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00002290 MemberOffset field_offset,
2291 bool is_volatile)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002292 : HTemplateInstruction(SideEffects::ChangesSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00002293 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002294 SetRawInputAt(0, object);
2295 SetRawInputAt(1, value);
2296 }
2297
Calin Juravle77520bc2015-01-12 18:45:46 +00002298 bool CanDoImplicitNullCheck() const OVERRIDE {
2299 return GetFieldOffset().Uint32Value() < kPageSize;
2300 }
2301
Calin Juravle52c48962014-12-16 17:02:57 +00002302 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002303 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002304 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002305 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002306 HInstruction* GetValue() const { return InputAt(1); }
2307
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002308 DECLARE_INSTRUCTION(InstanceFieldSet);
2309
2310 private:
2311 const FieldInfo field_info_;
2312
2313 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
2314};
2315
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002316class HArrayGet : public HExpression<2> {
2317 public:
2318 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002319 : HExpression(type, SideEffects::DependsOnSomething()) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002320 SetRawInputAt(0, array);
2321 SetRawInputAt(1, index);
2322 }
2323
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002324 bool CanBeMoved() const OVERRIDE { return true; }
2325 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002326 UNUSED(other);
2327 return true;
2328 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002329 bool CanDoImplicitNullCheck() const OVERRIDE {
2330 // TODO: We can be smarter here.
2331 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
2332 // which generates the implicit null check. There are cases when these can be removed
2333 // to produce better code. If we ever add optimizations to do so we should allow an
2334 // implicit check here (as long as the address falls in the first page).
2335 return false;
2336 }
2337
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002338 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002339
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002340 HInstruction* GetArray() const { return InputAt(0); }
2341 HInstruction* GetIndex() const { return InputAt(1); }
2342
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002343 DECLARE_INSTRUCTION(ArrayGet);
2344
2345 private:
2346 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
2347};
2348
2349class HArraySet : public HTemplateInstruction<3> {
2350 public:
2351 HArraySet(HInstruction* array,
2352 HInstruction* index,
2353 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002354 Primitive::Type expected_component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002355 uint32_t dex_pc)
2356 : HTemplateInstruction(SideEffects::ChangesSomething()),
2357 dex_pc_(dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002358 expected_component_type_(expected_component_type),
2359 needs_type_check_(value->GetType() == Primitive::kPrimNot) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002360 SetRawInputAt(0, array);
2361 SetRawInputAt(1, index);
2362 SetRawInputAt(2, value);
2363 }
2364
Calin Juravle77520bc2015-01-12 18:45:46 +00002365 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002366 // We currently always call a runtime method to catch array store
2367 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002368 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002369 }
2370
Calin Juravle77520bc2015-01-12 18:45:46 +00002371 bool CanDoImplicitNullCheck() const OVERRIDE {
2372 // TODO: Same as for ArrayGet.
2373 return false;
2374 }
2375
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002376 void ClearNeedsTypeCheck() {
2377 needs_type_check_ = false;
2378 }
2379
2380 bool NeedsTypeCheck() const { return needs_type_check_; }
2381
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002382 uint32_t GetDexPc() const { return dex_pc_; }
2383
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002384 HInstruction* GetArray() const { return InputAt(0); }
2385 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002386 HInstruction* GetValue() const { return InputAt(2); }
2387
2388 Primitive::Type GetComponentType() const {
2389 // The Dex format does not type floating point index operations. Since the
2390 // `expected_component_type_` is set during building and can therefore not
2391 // be correct, we also check what is the value type. If it is a floating
2392 // point type, we must use that type.
2393 Primitive::Type value_type = GetValue()->GetType();
2394 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
2395 ? value_type
2396 : expected_component_type_;
2397 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002398
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002399 DECLARE_INSTRUCTION(ArraySet);
2400
2401 private:
2402 const uint32_t dex_pc_;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002403 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002404 bool needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002405
2406 DISALLOW_COPY_AND_ASSIGN(HArraySet);
2407};
2408
2409class HArrayLength : public HExpression<1> {
2410 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002411 explicit HArrayLength(HInstruction* array)
2412 : HExpression(Primitive::kPrimInt, SideEffects::None()) {
2413 // Note that arrays do not change length, so the instruction does not
2414 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002415 SetRawInputAt(0, array);
2416 }
2417
Calin Juravle77520bc2015-01-12 18:45:46 +00002418 bool CanBeMoved() const OVERRIDE { return true; }
2419 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002420 UNUSED(other);
2421 return true;
2422 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002423 bool CanDoImplicitNullCheck() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002424
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002425 DECLARE_INSTRUCTION(ArrayLength);
2426
2427 private:
2428 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
2429};
2430
2431class HBoundsCheck : public HExpression<2> {
2432 public:
2433 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002434 : HExpression(index->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002435 DCHECK(index->GetType() == Primitive::kPrimInt);
2436 SetRawInputAt(0, index);
2437 SetRawInputAt(1, length);
2438 }
2439
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002440 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002441 virtual bool InstructionDataEquals(HInstruction* other) const {
2442 UNUSED(other);
2443 return true;
2444 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002445
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002446 virtual bool NeedsEnvironment() const { return true; }
2447
Roland Levillaine161a2a2014-10-03 12:45:18 +01002448 virtual bool CanThrow() const { return true; }
2449
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002450 uint32_t GetDexPc() const { return dex_pc_; }
2451
2452 DECLARE_INSTRUCTION(BoundsCheck);
2453
2454 private:
2455 const uint32_t dex_pc_;
2456
2457 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
2458};
2459
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002460/**
2461 * Some DEX instructions are folded into multiple HInstructions that need
2462 * to stay live until the last HInstruction. This class
2463 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00002464 * HInstruction stays live. `index` represents the stack location index of the
2465 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002466 */
2467class HTemporary : public HTemplateInstruction<0> {
2468 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002469 explicit HTemporary(size_t index) : HTemplateInstruction(SideEffects::None()), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002470
2471 size_t GetIndex() const { return index_; }
2472
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00002473 Primitive::Type GetType() const OVERRIDE {
2474 // The previous instruction is the one that will be stored in the temporary location.
2475 DCHECK(GetPrevious() != nullptr);
2476 return GetPrevious()->GetType();
2477 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00002478
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002479 DECLARE_INSTRUCTION(Temporary);
2480
2481 private:
2482 const size_t index_;
2483
2484 DISALLOW_COPY_AND_ASSIGN(HTemporary);
2485};
2486
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002487class HSuspendCheck : public HTemplateInstruction<0> {
2488 public:
2489 explicit HSuspendCheck(uint32_t dex_pc)
Nicolas Geoffray9ebc72c2014-09-25 16:33:42 +01002490 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002491
2492 virtual bool NeedsEnvironment() const {
2493 return true;
2494 }
2495
2496 uint32_t GetDexPc() const { return dex_pc_; }
2497
2498 DECLARE_INSTRUCTION(SuspendCheck);
2499
2500 private:
2501 const uint32_t dex_pc_;
2502
2503 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
2504};
2505
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002506/**
2507 * Instruction to load a Class object.
2508 */
2509class HLoadClass : public HExpression<0> {
2510 public:
2511 HLoadClass(uint16_t type_index,
2512 bool is_referrers_class,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002513 uint32_t dex_pc)
2514 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2515 type_index_(type_index),
2516 is_referrers_class_(is_referrers_class),
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002517 dex_pc_(dex_pc),
2518 generate_clinit_check_(false) {}
2519
2520 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002521
2522 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2523 return other->AsLoadClass()->type_index_ == type_index_;
2524 }
2525
2526 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
2527
2528 uint32_t GetDexPc() const { return dex_pc_; }
2529 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002530 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002531
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002532 bool NeedsEnvironment() const OVERRIDE {
2533 // Will call runtime and load the class if the class is not loaded yet.
2534 // TODO: finer grain decision.
2535 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002536 }
2537
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002538 bool MustGenerateClinitCheck() const {
2539 return generate_clinit_check_;
2540 }
2541
2542 void SetMustGenerateClinitCheck() {
2543 generate_clinit_check_ = true;
2544 }
2545
2546 bool CanCallRuntime() const {
2547 return MustGenerateClinitCheck() || !is_referrers_class_;
2548 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002549
2550 DECLARE_INSTRUCTION(LoadClass);
2551
2552 private:
2553 const uint16_t type_index_;
2554 const bool is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002555 const uint32_t dex_pc_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002556 // Whether this instruction must generate the initialization check.
2557 // Used for code generation.
2558 bool generate_clinit_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002559
2560 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
2561};
2562
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002563class HLoadString : public HExpression<0> {
2564 public:
2565 HLoadString(uint32_t string_index, uint32_t dex_pc)
2566 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2567 string_index_(string_index),
2568 dex_pc_(dex_pc) {}
2569
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002570 bool CanBeMoved() const OVERRIDE { return true; }
2571
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002572 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2573 return other->AsLoadString()->string_index_ == string_index_;
2574 }
2575
2576 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
2577
2578 uint32_t GetDexPc() const { return dex_pc_; }
2579 uint32_t GetStringIndex() const { return string_index_; }
2580
2581 // TODO: Can we deopt or debug when we resolve a string?
2582 bool NeedsEnvironment() const OVERRIDE { return false; }
2583
2584 DECLARE_INSTRUCTION(LoadString);
2585
2586 private:
2587 const uint32_t string_index_;
2588 const uint32_t dex_pc_;
2589
2590 DISALLOW_COPY_AND_ASSIGN(HLoadString);
2591};
2592
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002593// TODO: Pass this check to HInvokeStaticOrDirect nodes.
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002594/**
2595 * Performs an initialization check on its Class object input.
2596 */
2597class HClinitCheck : public HExpression<1> {
2598 public:
2599 explicit HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
2600 : HExpression(Primitive::kPrimNot, SideEffects::All()),
2601 dex_pc_(dex_pc) {
2602 SetRawInputAt(0, constant);
2603 }
2604
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002605 bool CanBeMoved() const OVERRIDE { return true; }
2606 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2607 UNUSED(other);
2608 return true;
2609 }
2610
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002611 bool NeedsEnvironment() const OVERRIDE {
2612 // May call runtime to initialize the class.
2613 return true;
2614 }
2615
2616 uint32_t GetDexPc() const { return dex_pc_; }
2617
2618 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
2619
2620 DECLARE_INSTRUCTION(ClinitCheck);
2621
2622 private:
2623 const uint32_t dex_pc_;
2624
2625 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
2626};
2627
2628class HStaticFieldGet : public HExpression<1> {
2629 public:
2630 HStaticFieldGet(HInstruction* cls,
2631 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00002632 MemberOffset field_offset,
2633 bool is_volatile)
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002634 : HExpression(field_type, SideEffects::DependsOnSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00002635 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002636 SetRawInputAt(0, cls);
2637 }
2638
Calin Juravle52c48962014-12-16 17:02:57 +00002639
Calin Juravle10c9cbe2014-12-19 10:50:19 +00002640 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002641
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002642 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00002643 HStaticFieldGet* other_get = other->AsStaticFieldGet();
2644 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002645 }
2646
2647 size_t ComputeHashCode() const OVERRIDE {
2648 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
2649 }
2650
Calin Juravle52c48962014-12-16 17:02:57 +00002651 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002652 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
2653 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002654 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002655
2656 DECLARE_INSTRUCTION(StaticFieldGet);
2657
2658 private:
2659 const FieldInfo field_info_;
2660
2661 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
2662};
2663
2664class HStaticFieldSet : public HTemplateInstruction<2> {
2665 public:
2666 HStaticFieldSet(HInstruction* cls,
2667 HInstruction* value,
2668 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00002669 MemberOffset field_offset,
2670 bool is_volatile)
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002671 : HTemplateInstruction(SideEffects::ChangesSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00002672 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002673 SetRawInputAt(0, cls);
2674 SetRawInputAt(1, value);
2675 }
2676
Calin Juravle52c48962014-12-16 17:02:57 +00002677 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002678 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
2679 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002680 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002681
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002682 HInstruction* GetValue() const { return InputAt(1); }
2683
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002684 DECLARE_INSTRUCTION(StaticFieldSet);
2685
2686 private:
2687 const FieldInfo field_info_;
2688
2689 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
2690};
2691
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002692// Implement the move-exception DEX instruction.
2693class HLoadException : public HExpression<0> {
2694 public:
2695 HLoadException() : HExpression(Primitive::kPrimNot, SideEffects::None()) {}
2696
2697 DECLARE_INSTRUCTION(LoadException);
2698
2699 private:
2700 DISALLOW_COPY_AND_ASSIGN(HLoadException);
2701};
2702
2703class HThrow : public HTemplateInstruction<1> {
2704 public:
2705 HThrow(HInstruction* exception, uint32_t dex_pc)
2706 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {
2707 SetRawInputAt(0, exception);
2708 }
2709
2710 bool IsControlFlow() const OVERRIDE { return true; }
2711
2712 bool NeedsEnvironment() const OVERRIDE { return true; }
2713
2714 uint32_t GetDexPc() const { return dex_pc_; }
2715
2716 DECLARE_INSTRUCTION(Throw);
2717
2718 private:
2719 uint32_t dex_pc_;
2720
2721 DISALLOW_COPY_AND_ASSIGN(HThrow);
2722};
2723
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002724class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002725 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002726 HInstanceOf(HInstruction* object,
2727 HLoadClass* constant,
2728 bool class_is_final,
2729 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002730 : HExpression(Primitive::kPrimBoolean, SideEffects::None()),
2731 class_is_final_(class_is_final),
2732 dex_pc_(dex_pc) {
2733 SetRawInputAt(0, object);
2734 SetRawInputAt(1, constant);
2735 }
2736
2737 bool CanBeMoved() const OVERRIDE { return true; }
2738
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002739 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002740 return true;
2741 }
2742
2743 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002744 return false;
2745 }
2746
2747 uint32_t GetDexPc() const { return dex_pc_; }
2748
2749 bool IsClassFinal() const { return class_is_final_; }
2750
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002751 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002752
2753 private:
2754 const bool class_is_final_;
2755 const uint32_t dex_pc_;
2756
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002757 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
2758};
2759
2760class HCheckCast : public HTemplateInstruction<2> {
2761 public:
2762 HCheckCast(HInstruction* object,
2763 HLoadClass* constant,
2764 bool class_is_final,
2765 uint32_t dex_pc)
2766 : HTemplateInstruction(SideEffects::None()),
2767 class_is_final_(class_is_final),
2768 dex_pc_(dex_pc) {
2769 SetRawInputAt(0, object);
2770 SetRawInputAt(1, constant);
2771 }
2772
2773 bool CanBeMoved() const OVERRIDE { return true; }
2774
2775 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2776 return true;
2777 }
2778
2779 bool NeedsEnvironment() const OVERRIDE {
2780 // Instruction may throw a CheckCastError.
2781 return true;
2782 }
2783
2784 bool CanThrow() const OVERRIDE { return true; }
2785
2786 uint32_t GetDexPc() const { return dex_pc_; }
2787
2788 bool IsClassFinal() const { return class_is_final_; }
2789
2790 DECLARE_INSTRUCTION(CheckCast);
2791
2792 private:
2793 const bool class_is_final_;
2794 const uint32_t dex_pc_;
2795
2796 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002797};
2798
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00002799class HMonitorOperation : public HTemplateInstruction<1> {
2800 public:
2801 enum OperationKind {
2802 kEnter,
2803 kExit,
2804 };
2805
2806 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
2807 : HTemplateInstruction(SideEffects::None()), kind_(kind), dex_pc_(dex_pc) {
2808 SetRawInputAt(0, object);
2809 }
2810
2811 // Instruction may throw a Java exception, so we need an environment.
2812 bool NeedsEnvironment() const OVERRIDE { return true; }
2813 bool CanThrow() const OVERRIDE { return true; }
2814
2815 uint32_t GetDexPc() const { return dex_pc_; }
2816
2817 bool IsEnter() const { return kind_ == kEnter; }
2818
2819 DECLARE_INSTRUCTION(MonitorOperation);
2820
Calin Juravle52c48962014-12-16 17:02:57 +00002821 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00002822 const OperationKind kind_;
2823 const uint32_t dex_pc_;
2824
2825 private:
2826 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
2827};
2828
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002829class MoveOperands : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002830 public:
Nicolas Geoffray740475d2014-09-29 10:33:25 +01002831 MoveOperands(Location source, Location destination, HInstruction* instruction)
2832 : source_(source), destination_(destination), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002833
2834 Location GetSource() const { return source_; }
2835 Location GetDestination() const { return destination_; }
2836
2837 void SetSource(Location value) { source_ = value; }
2838 void SetDestination(Location value) { destination_ = value; }
2839
2840 // The parallel move resolver marks moves as "in-progress" by clearing the
2841 // destination (but not the source).
2842 Location MarkPending() {
2843 DCHECK(!IsPending());
2844 Location dest = destination_;
2845 destination_ = Location::NoLocation();
2846 return dest;
2847 }
2848
2849 void ClearPending(Location dest) {
2850 DCHECK(IsPending());
2851 destination_ = dest;
2852 }
2853
2854 bool IsPending() const {
2855 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
2856 return destination_.IsInvalid() && !source_.IsInvalid();
2857 }
2858
2859 // True if this blocks a move from the given location.
2860 bool Blocks(Location loc) const {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00002861 return !IsEliminated() && source_.Equals(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002862 }
2863
2864 // A move is redundant if it's been eliminated, if its source and
2865 // destination are the same, or if its destination is unneeded.
2866 bool IsRedundant() const {
2867 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
2868 }
2869
2870 // We clear both operands to indicate move that's been eliminated.
2871 void Eliminate() {
2872 source_ = destination_ = Location::NoLocation();
2873 }
2874
2875 bool IsEliminated() const {
2876 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
2877 return source_.IsInvalid();
2878 }
2879
Nicolas Geoffray740475d2014-09-29 10:33:25 +01002880 HInstruction* GetInstruction() const { return instruction_; }
2881
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002882 private:
2883 Location source_;
2884 Location destination_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01002885 // The instruction this move is assocatied with. Null when this move is
2886 // for moving an input in the expected locations of user (including a phi user).
2887 // This is only used in debug mode, to ensure we do not connect interval siblings
2888 // in the same parallel move.
2889 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002890};
2891
2892static constexpr size_t kDefaultNumberOfMoves = 4;
2893
2894class HParallelMove : public HTemplateInstruction<0> {
2895 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002896 explicit HParallelMove(ArenaAllocator* arena)
2897 : HTemplateInstruction(SideEffects::None()), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002898
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00002899 void AddMove(Location source, Location destination, HInstruction* instruction) {
2900 DCHECK(source.IsValid());
2901 DCHECK(destination.IsValid());
2902 // The parallel move resolver does not handle pairs. So we decompose the
2903 // pair locations into two moves.
2904 if (source.IsPair() && destination.IsPair()) {
2905 AddMove(source.ToLow(), destination.ToLow(), instruction);
2906 AddMove(source.ToHigh(), destination.ToHigh(), nullptr);
2907 } else if (source.IsPair()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002908 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00002909 AddMove(source.ToLow(), Location::StackSlot(destination.GetStackIndex()), instruction);
2910 AddMove(source.ToHigh(), Location::StackSlot(destination.GetHighStackIndex(4)), nullptr);
2911 } else if (destination.IsPair()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002912 if (source.IsConstant()) {
2913 // We put the same constant in the move. The code generator will handle which
2914 // low or high part to use.
2915 AddMove(source, destination.ToLow(), instruction);
2916 AddMove(source, destination.ToHigh(), nullptr);
2917 } else {
2918 DCHECK(source.IsDoubleStackSlot());
2919 AddMove(Location::StackSlot(source.GetStackIndex()), destination.ToLow(), instruction);
2920 // TODO: rewrite GetHighStackIndex to not require a word size. It's supposed to
2921 // always be 4.
2922 static constexpr int kHighOffset = 4;
2923 AddMove(Location::StackSlot(source.GetHighStackIndex(kHighOffset)),
2924 destination.ToHigh(),
2925 nullptr);
2926 }
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00002927 } else {
2928 if (kIsDebugBuild) {
2929 if (instruction != nullptr) {
2930 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
2931 DCHECK_NE(moves_.Get(i).GetInstruction(), instruction)
2932 << "Doing parallel moves for the same instruction.";
2933 }
2934 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00002935 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00002936 DCHECK(!destination.Equals(moves_.Get(i).GetDestination()))
2937 << "Same destination for two moves in a parallel move.";
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00002938 }
2939 }
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00002940 moves_.Add(MoveOperands(source, destination, instruction));
Nicolas Geoffray740475d2014-09-29 10:33:25 +01002941 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002942 }
2943
2944 MoveOperands* MoveOperandsAt(size_t index) const {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00002945 return moves_.GetRawStorage() + index;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002946 }
2947
2948 size_t NumMoves() const { return moves_.Size(); }
2949
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002950 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002951
2952 private:
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00002953 GrowableArray<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002954
2955 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
2956};
2957
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002958class HGraphVisitor : public ValueObject {
2959 public:
Dave Allison20dfc792014-06-16 20:44:29 -07002960 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
2961 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002962
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002963 virtual void VisitInstruction(HInstruction* instruction) { UNUSED(instruction); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002964 virtual void VisitBasicBlock(HBasicBlock* block);
2965
Roland Levillain633021e2014-10-01 14:12:25 +01002966 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002967 void VisitInsertionOrder();
2968
Roland Levillain633021e2014-10-01 14:12:25 +01002969 // Visit the graph following dominator tree reverse post-order.
2970 void VisitReversePostOrder();
2971
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002972 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002973
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002974 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002975#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002976 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
2977
2978 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
2979
2980#undef DECLARE_VISIT_INSTRUCTION
2981
2982 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07002983 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002984
2985 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
2986};
2987
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002988class HGraphDelegateVisitor : public HGraphVisitor {
2989 public:
2990 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
2991 virtual ~HGraphDelegateVisitor() {}
2992
2993 // Visit functions that delegate to to super class.
2994#define DECLARE_VISIT_INSTRUCTION(name, super) \
2995 virtual void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
2996
2997 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
2998
2999#undef DECLARE_VISIT_INSTRUCTION
3000
3001 private:
3002 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
3003};
3004
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003005class HInsertionOrderIterator : public ValueObject {
3006 public:
3007 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
3008
3009 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
3010 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
3011 void Advance() { ++index_; }
3012
3013 private:
3014 const HGraph& graph_;
3015 size_t index_;
3016
3017 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
3018};
3019
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003020class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003021 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003022 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003023
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003024 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
3025 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003026 void Advance() { ++index_; }
3027
3028 private:
3029 const HGraph& graph_;
3030 size_t index_;
3031
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003032 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003033};
3034
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003035class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003036 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003037 explicit HPostOrderIterator(const HGraph& graph)
3038 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003039
3040 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003041 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003042 void Advance() { --index_; }
3043
3044 private:
3045 const HGraph& graph_;
3046 size_t index_;
3047
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003048 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003049};
3050
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003051} // namespace art
3052
3053#endif // ART_COMPILER_OPTIMIZING_NODES_H_