blob: 1a0ebe562dacee65d1ba41468bb29089208e3e07 [file] [log] [blame]
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ART_COMPILER_OPTIMIZING_NODES_H_
18#define ART_COMPILER_OPTIMIZING_NODES_H_
19
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000020#include "invoke_type.h"
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010021#include "locations.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010022#include "offsets.h"
Ian Rogerse63db272014-07-15 15:36:11 -070023#include "primitive.h"
Ian Rogers0279ebb2014-10-08 17:27:48 -070024#include "utils/arena_object.h"
Nicolas Geoffray0e336432014-02-26 18:24:38 +000025#include "utils/arena_bit_vector.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000026#include "utils/growable_array.h"
27
28namespace art {
29
30class HBasicBlock;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010031class HEnvironment;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000032class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000033class HIntConstant;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000034class HInvoke;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000035class HGraphVisitor;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010036class HPhi;
Nicolas Geoffray3c049742014-09-24 18:10:46 +010037class HSuspendCheck;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010038class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000039class LocationSummary;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000040
41static const int kDefaultNumberOfBlocks = 8;
42static const int kDefaultNumberOfSuccessors = 2;
43static const int kDefaultNumberOfPredecessors = 2;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010044static const int kDefaultNumberOfDominatedBlocks = 1;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000045static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000046
Calin Juravle9aec02f2014-11-18 23:06:35 +000047static constexpr uint32_t kMaxIntShiftValue = 0x1f;
48static constexpr uint64_t kMaxLongShiftValue = 0x3f;
49
Dave Allison20dfc792014-06-16 20:44:29 -070050enum IfCondition {
51 kCondEQ,
52 kCondNE,
53 kCondLT,
54 kCondLE,
55 kCondGT,
56 kCondGE,
57};
58
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010059class HInstructionList {
60 public:
61 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
62
63 void AddInstruction(HInstruction* instruction);
64 void RemoveInstruction(HInstruction* instruction);
65
Roland Levillain6b469232014-09-25 10:10:38 +010066 // Return true if this list contains `instruction`.
67 bool Contains(HInstruction* instruction) const;
68
Roland Levillainccc07a92014-09-16 14:48:16 +010069 // Return true if `instruction1` is found before `instruction2` in
70 // this instruction list and false otherwise. Abort if none
71 // of these instructions is found.
72 bool FoundBefore(const HInstruction* instruction1,
73 const HInstruction* instruction2) const;
74
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010075 private:
76 HInstruction* first_instruction_;
77 HInstruction* last_instruction_;
78
79 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000080 friend class HGraph;
81 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010082 friend class HInstructionIterator;
Nicolas Geoffray804d0932014-05-02 08:46:00 +010083 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010084
85 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
86};
87
Nicolas Geoffray818f2102014-02-18 16:43:35 +000088// Control-flow graph of a method. Contains a list of basic blocks.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070089class HGraph : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +000090 public:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000091 HGraph(ArenaAllocator* arena, int start_instruction_id = 0)
Nicolas Geoffray818f2102014-02-18 16:43:35 +000092 : arena_(arena),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000093 blocks_(arena, kDefaultNumberOfBlocks),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +010094 reverse_post_order_(arena, kDefaultNumberOfBlocks),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070095 entry_block_(nullptr),
96 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +010097 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +010098 number_of_vregs_(0),
99 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000100 temporaries_vreg_slots_(0),
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000101 current_instruction_id_(start_instruction_id) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000102
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000103 ArenaAllocator* GetArena() const { return arena_; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100104 const GrowableArray<HBasicBlock*>& GetBlocks() const { return blocks_; }
Roland Levillain93445682014-10-06 19:24:02 +0100105 HBasicBlock* GetBlock(size_t id) const { return blocks_.Get(id); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000106
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000107 HBasicBlock* GetEntryBlock() const { return entry_block_; }
108 HBasicBlock* GetExitBlock() const { return exit_block_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000109
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000110 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
111 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000112
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000113 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100114
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000115 // Try building the SSA form of this graph, with dominance computation and loop
116 // recognition. Returns whether it was successful in doing all these steps.
117 bool TryBuildingSsa() {
118 BuildDominatorTree();
119 TransformToSsa();
120 return AnalyzeNaturalLoops();
121 }
122
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000123 void BuildDominatorTree();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000124 void TransformToSsa();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100125 void SimplifyCFG();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000126
Nicolas Geoffrayf5370122014-12-02 11:51:19 +0000127 // Analyze all natural loops in this graph. Returns false if one
128 // loop is not natural, that is the header does not dominate the
129 // back edge.
130 bool AnalyzeNaturalLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100131
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000132 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
133 void InlineInto(HGraph* outer_graph, HInvoke* invoke);
134
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100135 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
136 void SimplifyLoop(HBasicBlock* header);
137
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000138 int32_t GetNextInstructionId() {
139 DCHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000140 return current_instruction_id_++;
141 }
142
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000143 int32_t GetCurrentInstructionId() const {
144 return current_instruction_id_;
145 }
146
147 void SetCurrentInstructionId(int32_t id) {
148 current_instruction_id_ = id;
149 }
150
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100151 uint16_t GetMaximumNumberOfOutVRegs() const {
152 return maximum_number_of_out_vregs_;
153 }
154
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000155 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
156 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100157 }
158
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000159 void UpdateTemporariesVRegSlots(size_t slots) {
160 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100161 }
162
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000163 size_t GetTemporariesVRegSlots() const {
164 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100165 }
166
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100167 void SetNumberOfVRegs(uint16_t number_of_vregs) {
168 number_of_vregs_ = number_of_vregs;
169 }
170
171 uint16_t GetNumberOfVRegs() const {
172 return number_of_vregs_;
173 }
174
175 void SetNumberOfInVRegs(uint16_t value) {
176 number_of_in_vregs_ = value;
177 }
178
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100179 uint16_t GetNumberOfLocalVRegs() const {
180 return number_of_vregs_ - number_of_in_vregs_;
181 }
182
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100183 const GrowableArray<HBasicBlock*>& GetReversePostOrder() const {
184 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100185 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100186
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000187 private:
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000188 HBasicBlock* FindCommonDominator(HBasicBlock* first, HBasicBlock* second) const;
189 void VisitBlockForDominatorTree(HBasicBlock* block,
190 HBasicBlock* predecessor,
191 GrowableArray<size_t>* visits);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100192 void FindBackEdges(ArenaBitVector* visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000193 void VisitBlockForBackEdges(HBasicBlock* block,
194 ArenaBitVector* visited,
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100195 ArenaBitVector* visiting);
Roland Levillainfc600dc2014-12-02 17:16:31 +0000196 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000197 void RemoveDeadBlocks(const ArenaBitVector& visited) const;
Jean Christophe Beyler53d9da82014-12-04 13:28:25 -0800198 void RemoveBlock(HBasicBlock* block) const;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000199
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000200 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000201
202 // List of blocks in insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000203 GrowableArray<HBasicBlock*> blocks_;
204
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100205 // List of blocks to perform a reverse post order tree traversal.
206 GrowableArray<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000207
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000208 HBasicBlock* entry_block_;
209 HBasicBlock* exit_block_;
210
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100211 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100212 uint16_t maximum_number_of_out_vregs_;
213
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100214 // The number of virtual registers in this method. Contains the parameters.
215 uint16_t number_of_vregs_;
216
217 // The number of virtual registers used by parameters of this method.
218 uint16_t number_of_in_vregs_;
219
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000220 // Number of vreg size slots that the temporaries use (used in baseline compiler).
221 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100222
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000223 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000224 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000225
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000226 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000227 DISALLOW_COPY_AND_ASSIGN(HGraph);
228};
229
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700230class HLoopInformation : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000231 public:
232 HLoopInformation(HBasicBlock* header, HGraph* graph)
233 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100234 suspend_check_(nullptr),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100235 back_edges_(graph->GetArena(), kDefaultNumberOfBackEdges),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100236 // Make bit vector growable, as the number of blocks may change.
237 blocks_(graph->GetArena(), graph->GetBlocks().Size(), true) {}
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100238
239 HBasicBlock* GetHeader() const {
240 return header_;
241 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000242
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100243 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
244 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
245 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
246
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000247 void AddBackEdge(HBasicBlock* back_edge) {
248 back_edges_.Add(back_edge);
249 }
250
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100251 void RemoveBackEdge(HBasicBlock* back_edge) {
252 back_edges_.Delete(back_edge);
253 }
254
255 bool IsBackEdge(HBasicBlock* block) {
256 for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) {
257 if (back_edges_.Get(i) == block) return true;
258 }
259 return false;
260 }
261
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000262 size_t NumberOfBackEdges() const {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000263 return back_edges_.Size();
264 }
265
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100266 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100267
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100268 const GrowableArray<HBasicBlock*>& GetBackEdges() const {
269 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100270 }
271
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100272 void ClearBackEdges() {
273 back_edges_.Reset();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100274 }
275
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100276 // Find blocks that are part of this loop. Returns whether the loop is a natural loop,
277 // that is the header dominates the back edge.
278 bool Populate();
279
280 // Returns whether this loop information contains `block`.
281 // Note that this loop information *must* be populated before entering this function.
282 bool Contains(const HBasicBlock& block) const;
283
284 // Returns whether this loop information is an inner loop of `other`.
285 // Note that `other` *must* be populated before entering this function.
286 bool IsIn(const HLoopInformation& other) const;
287
288 const ArenaBitVector& GetBlocks() const { return blocks_; }
289
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000290 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100291 // Internal recursive implementation of `Populate`.
292 void PopulateRecursive(HBasicBlock* block);
293
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000294 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100295 HSuspendCheck* suspend_check_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000296 GrowableArray<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100297 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000298
299 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
300};
301
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100302static constexpr size_t kNoLifetime = -1;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100303static constexpr uint32_t kNoDexPc = -1;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100304
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000305// A block in a method. Contains the list of instructions represented
306// as a double linked list. Each block knows its predecessors and
307// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100308
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700309class HBasicBlock : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000310 public:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100311 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000312 : graph_(graph),
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000313 predecessors_(graph->GetArena(), kDefaultNumberOfPredecessors),
314 successors_(graph->GetArena(), kDefaultNumberOfSuccessors),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000315 loop_information_(nullptr),
316 dominator_(nullptr),
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100317 dominated_blocks_(graph->GetArena(), kDefaultNumberOfDominatedBlocks),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100318 block_id_(-1),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100319 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100320 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000321 lifetime_end_(kNoLifetime),
322 is_catch_block_(false) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000323
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100324 const GrowableArray<HBasicBlock*>& GetPredecessors() const {
325 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000326 }
327
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100328 const GrowableArray<HBasicBlock*>& GetSuccessors() const {
329 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000330 }
331
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100332 const GrowableArray<HBasicBlock*>& GetDominatedBlocks() const {
333 return dominated_blocks_;
334 }
335
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100336 bool IsEntryBlock() const {
337 return graph_->GetEntryBlock() == this;
338 }
339
340 bool IsExitBlock() const {
341 return graph_->GetExitBlock() == this;
342 }
343
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000344 void AddBackEdge(HBasicBlock* back_edge) {
345 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000346 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000347 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100348 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000349 loop_information_->AddBackEdge(back_edge);
350 }
351
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000352 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000353
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000354 int GetBlockId() const { return block_id_; }
355 void SetBlockId(int id) { block_id_ = id; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000356
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000357 HBasicBlock* GetDominator() const { return dominator_; }
358 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100359 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.Add(block); }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000360
361 int NumberOfBackEdges() const {
362 return loop_information_ == nullptr
363 ? 0
364 : loop_information_->NumberOfBackEdges();
365 }
366
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100367 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
368 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100369 const HInstructionList& GetInstructions() const { return instructions_; }
370 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100371 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000372
373 void AddSuccessor(HBasicBlock* block) {
374 successors_.Add(block);
375 block->predecessors_.Add(this);
376 }
377
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100378 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
379 size_t successor_index = GetSuccessorIndexOf(existing);
380 DCHECK_NE(successor_index, static_cast<size_t>(-1));
381 existing->RemovePredecessor(this);
382 new_block->predecessors_.Add(this);
383 successors_.Put(successor_index, new_block);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000384 }
385
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100386 void RemovePredecessor(HBasicBlock* block) {
387 predecessors_.Delete(block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100388 }
389
390 void ClearAllPredecessors() {
391 predecessors_.Reset();
392 }
393
394 void AddPredecessor(HBasicBlock* block) {
395 predecessors_.Add(block);
396 block->successors_.Add(this);
397 }
398
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100399 void SwapPredecessors() {
Nicolas Geoffrayc83d4412014-09-18 16:46:20 +0100400 DCHECK_EQ(predecessors_.Size(), 2u);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100401 HBasicBlock* temp = predecessors_.Get(0);
402 predecessors_.Put(0, predecessors_.Get(1));
403 predecessors_.Put(1, temp);
404 }
405
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100406 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) {
407 for (size_t i = 0, e = predecessors_.Size(); i < e; ++i) {
408 if (predecessors_.Get(i) == predecessor) {
409 return i;
410 }
411 }
412 return -1;
413 }
414
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100415 size_t GetSuccessorIndexOf(HBasicBlock* successor) {
416 for (size_t i = 0, e = successors_.Size(); i < e; ++i) {
417 if (successors_.Get(i) == successor) {
418 return i;
419 }
420 }
421 return -1;
422 }
423
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000424 void AddInstruction(HInstruction* instruction);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100425 void RemoveInstruction(HInstruction* instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100426 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +0100427 // Replace instruction `initial` with `replacement` within this block.
428 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
429 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100430 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100431 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100432 void RemovePhi(HPhi* phi);
433
434 bool IsLoopHeader() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100435 return (loop_information_ != nullptr) && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100436 }
437
Roland Levillain6b879dd2014-09-22 17:13:44 +0100438 bool IsLoopPreHeaderFirstPredecessor() const {
439 DCHECK(IsLoopHeader());
440 DCHECK(!GetPredecessors().IsEmpty());
441 return GetPredecessors().Get(0) == GetLoopInformation()->GetPreHeader();
442 }
443
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100444 HLoopInformation* GetLoopInformation() const {
445 return loop_information_;
446 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000447
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100448 // Set the loop_information_ on this block. This method overrides the current
449 // loop_information if it is an outer loop of the passed loop information.
450 void SetInLoop(HLoopInformation* info) {
451 if (IsLoopHeader()) {
452 // Nothing to do. This just means `info` is an outer loop.
453 } else if (loop_information_ == nullptr) {
454 loop_information_ = info;
455 } else if (loop_information_->Contains(*info->GetHeader())) {
456 // Block is currently part of an outer loop. Make it part of this inner loop.
457 // Note that a non loop header having a loop information means this loop information
458 // has already been populated
459 loop_information_ = info;
460 } else {
461 // Block is part of an inner loop. Do not update the loop information.
462 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
463 // at this point, because this method is being called while populating `info`.
464 }
465 }
466
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100467 bool IsInLoop() const { return loop_information_ != nullptr; }
468
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100469 // Returns wheter this block dominates the blocked passed as parameter.
470 bool Dominates(HBasicBlock* block) const;
471
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100472 size_t GetLifetimeStart() const { return lifetime_start_; }
473 size_t GetLifetimeEnd() const { return lifetime_end_; }
474
475 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
476 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
477
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100478 uint32_t GetDexPc() const { return dex_pc_; }
479
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000480 bool IsCatchBlock() const { return is_catch_block_; }
481 void SetIsCatchBlock() { is_catch_block_ = true; }
482
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000483 private:
484 HGraph* const graph_;
485 GrowableArray<HBasicBlock*> predecessors_;
486 GrowableArray<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100487 HInstructionList instructions_;
488 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000489 HLoopInformation* loop_information_;
490 HBasicBlock* dominator_;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100491 GrowableArray<HBasicBlock*> dominated_blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000492 int block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100493 // The dex program counter of the first instruction of this block.
494 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100495 size_t lifetime_start_;
496 size_t lifetime_end_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000497 bool is_catch_block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000498
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000499 friend class HGraph;
500 friend class HInstruction;
501
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000502 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
503};
504
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100505#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
506 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000507 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000508 M(ArrayGet, Instruction) \
509 M(ArrayLength, Instruction) \
510 M(ArraySet, Instruction) \
511 M(BoundsCheck, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000512 M(CheckCast, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100513 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000514 M(Compare, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100515 M(Condition, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000516 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +0000517 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000518 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100519 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000520 M(Exit, Instruction) \
521 M(FloatConstant, Constant) \
522 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100523 M(GreaterThan, Condition) \
524 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100525 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000526 M(InstanceFieldGet, Instruction) \
527 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000528 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100529 M(IntConstant, Constant) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +0000530 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000531 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100532 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000533 M(LessThan, Condition) \
534 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000535 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000536 M(LoadException, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100537 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000538 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100539 M(Local, Instruction) \
540 M(LongConstant, Constant) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +0000541 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000542 M(Mul, BinaryOperation) \
543 M(Neg, UnaryOperation) \
544 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100545 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +0100546 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000547 M(NotEqual, Condition) \
548 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000549 M(Or, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100550 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000551 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100552 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000553 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100554 M(Return, Instruction) \
555 M(ReturnVoid, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000556 M(Shl, BinaryOperation) \
557 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100558 M(StaticFieldGet, Instruction) \
559 M(StaticFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100560 M(StoreLocal, Instruction) \
561 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100562 M(SuspendCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000563 M(Temporary, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000564 M(Throw, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +0000565 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000566 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000567 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000568
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100569#define FOR_EACH_INSTRUCTION(M) \
570 FOR_EACH_CONCRETE_INSTRUCTION(M) \
571 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +0100572 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +0100573 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100574 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -0700575
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100576#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000577FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
578#undef FORWARD_DECLARATION
579
Roland Levillainccc07a92014-09-16 14:48:16 +0100580#define DECLARE_INSTRUCTION(type) \
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100581 virtual InstructionKind GetKind() const { return k##type; } \
Roland Levillainccc07a92014-09-16 14:48:16 +0100582 virtual const char* DebugName() const { return #type; } \
583 virtual const H##type* As##type() const OVERRIDE { return this; } \
584 virtual H##type* As##type() OVERRIDE { return this; } \
585 virtual bool InstructionTypeEquals(HInstruction* other) const { \
586 return other->Is##type(); \
587 } \
588 virtual void Accept(HGraphVisitor* visitor)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000589
David Brazdiled596192015-01-23 10:39:45 +0000590template <typename T> class HUseList;
591
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100592template <typename T>
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700593class HUseListNode : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000594 public:
David Brazdiled596192015-01-23 10:39:45 +0000595 HUseListNode* GetPrevious() const { return prev_; }
596 HUseListNode* GetNext() const { return next_; }
597 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100598 size_t GetIndex() const { return index_; }
599
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000600 private:
David Brazdiled596192015-01-23 10:39:45 +0000601 HUseListNode(T user, size_t index)
602 : user_(user), index_(index), prev_(nullptr), next_(nullptr) {}
603
604 T const user_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100605 const size_t index_;
David Brazdiled596192015-01-23 10:39:45 +0000606 HUseListNode<T>* prev_;
607 HUseListNode<T>* next_;
608
609 friend class HUseList<T>;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000610
611 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
612};
613
David Brazdiled596192015-01-23 10:39:45 +0000614template <typename T>
615class HUseList : public ValueObject {
616 public:
617 HUseList() : first_(nullptr) {}
618
619 void Clear() {
620 first_ = nullptr;
621 }
622
623 // Adds a new entry at the beginning of the use list and returns
624 // the newly created node.
625 HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) {
David Brazdilea55b932015-01-27 17:12:29 +0000626 HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index);
David Brazdiled596192015-01-23 10:39:45 +0000627 if (IsEmpty()) {
628 first_ = new_node;
629 } else {
630 first_->prev_ = new_node;
631 new_node->next_ = first_;
632 first_ = new_node;
633 }
634 return new_node;
635 }
636
637 HUseListNode<T>* GetFirst() const {
638 return first_;
639 }
640
641 void Remove(HUseListNode<T>* node) {
642 if (node->prev_ != nullptr) {
643 node->prev_->next_ = node->next_;
644 }
645 if (node->next_ != nullptr) {
646 node->next_->prev_ = node->prev_;
647 }
648 if (node == first_) {
649 first_ = node->next_;
650 }
651 }
652
653 bool IsEmpty() const {
654 return first_ == nullptr;
655 }
656
657 bool HasOnlyOneUse() const {
658 return first_ != nullptr && first_->next_ == nullptr;
659 }
660
661 private:
662 HUseListNode<T>* first_;
663};
664
665template<typename T>
666class HUseIterator : public ValueObject {
667 public:
668 explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {}
669
670 bool Done() const { return current_ == nullptr; }
671
672 void Advance() {
673 DCHECK(!Done());
674 current_ = current_->GetNext();
675 }
676
677 HUseListNode<T>* Current() const {
678 DCHECK(!Done());
679 return current_;
680 }
681
682 private:
683 HUseListNode<T>* current_;
684
685 friend class HValue;
686};
687
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100688// Represents the side effects an instruction may have.
689class SideEffects : public ValueObject {
690 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100691 SideEffects() : flags_(0) {}
692
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100693 static SideEffects None() {
694 return SideEffects(0);
695 }
696
697 static SideEffects All() {
698 return SideEffects(ChangesSomething().flags_ | DependsOnSomething().flags_);
699 }
700
701 static SideEffects ChangesSomething() {
702 return SideEffects((1 << kFlagChangesCount) - 1);
703 }
704
705 static SideEffects DependsOnSomething() {
706 int count = kFlagDependsOnCount - kFlagChangesCount;
707 return SideEffects(((1 << count) - 1) << kFlagChangesCount);
708 }
709
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100710 SideEffects Union(SideEffects other) const {
711 return SideEffects(flags_ | other.flags_);
712 }
713
Roland Levillain72bceff2014-09-15 18:29:00 +0100714 bool HasSideEffects() const {
715 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
716 return (flags_ & all_bits_set) != 0;
717 }
718
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100719 bool HasAllSideEffects() const {
720 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
721 return all_bits_set == (flags_ & all_bits_set);
722 }
723
724 bool DependsOn(SideEffects other) const {
725 size_t depends_flags = other.ComputeDependsFlags();
726 return (flags_ & depends_flags) != 0;
727 }
728
729 bool HasDependencies() const {
730 int count = kFlagDependsOnCount - kFlagChangesCount;
731 size_t all_bits_set = (1 << count) - 1;
732 return ((flags_ >> kFlagChangesCount) & all_bits_set) != 0;
733 }
734
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100735 private:
736 static constexpr int kFlagChangesSomething = 0;
737 static constexpr int kFlagChangesCount = kFlagChangesSomething + 1;
738
739 static constexpr int kFlagDependsOnSomething = kFlagChangesCount;
740 static constexpr int kFlagDependsOnCount = kFlagDependsOnSomething + 1;
741
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100742 explicit SideEffects(size_t flags) : flags_(flags) {}
743
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100744 size_t ComputeDependsFlags() const {
745 return flags_ << kFlagChangesCount;
746 }
747
748 size_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100749};
750
David Brazdiled596192015-01-23 10:39:45 +0000751// A HEnvironment object contains the values of virtual registers at a given location.
752class HEnvironment : public ArenaObject<kArenaAllocMisc> {
753 public:
754 HEnvironment(ArenaAllocator* arena, size_t number_of_vregs)
755 : vregs_(arena, number_of_vregs) {
756 vregs_.SetSize(number_of_vregs);
757 for (size_t i = 0; i < number_of_vregs; i++) {
758 vregs_.Put(i, VRegInfo(nullptr, nullptr));
759 }
760 }
761
762 void CopyFrom(HEnvironment* env);
763
764 void SetRawEnvAt(size_t index, HInstruction* instruction) {
765 vregs_.Put(index, VRegInfo(instruction, nullptr));
766 }
767
768 // Record instructions' use entries of this environment for constant-time removal.
769 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
770 DCHECK(env_use->GetUser() == this);
771 size_t index = env_use->GetIndex();
772 VRegInfo info = vregs_.Get(index);
773 DCHECK(info.vreg_ != nullptr);
774 DCHECK(info.node_ == nullptr);
775 vregs_.Put(index, VRegInfo(info.vreg_, env_use));
776 }
777
778 HInstruction* GetInstructionAt(size_t index) const {
779 return vregs_.Get(index).vreg_;
780 }
781
782 HUseListNode<HEnvironment*>* GetInstructionEnvUseAt(size_t index) const {
783 return vregs_.Get(index).node_;
784 }
785
786 size_t Size() const { return vregs_.Size(); }
787
788 private:
789 struct VRegInfo {
790 HInstruction* vreg_;
791 HUseListNode<HEnvironment*>* node_;
792
793 VRegInfo(HInstruction* instruction, HUseListNode<HEnvironment*>* env_use)
794 : vreg_(instruction), node_(env_use) {}
795 };
796
797 GrowableArray<VRegInfo> vregs_;
798
799 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
800};
801
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700802class HInstruction : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000803 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100804 explicit HInstruction(SideEffects side_effects)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000805 : previous_(nullptr),
806 next_(nullptr),
807 block_(nullptr),
808 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100809 ssa_index_(-1),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100810 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100811 locations_(nullptr),
812 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100813 lifetime_position_(kNoLifetime),
814 side_effects_(side_effects) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000815
Dave Allison20dfc792014-06-16 20:44:29 -0700816 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000817
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100818#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100819 enum InstructionKind {
820 FOR_EACH_INSTRUCTION(DECLARE_KIND)
821 };
822#undef DECLARE_KIND
823
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000824 HInstruction* GetNext() const { return next_; }
825 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000826
Calin Juravle77520bc2015-01-12 18:45:46 +0000827 HInstruction* GetNextDisregardingMoves() const;
828 HInstruction* GetPreviousDisregardingMoves() const;
829
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000830 HBasicBlock* GetBlock() const { return block_; }
831 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100832 bool IsInBlock() const { return block_ != nullptr; }
833 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +0100834 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000835
Roland Levillain6b879dd2014-09-22 17:13:44 +0100836 virtual size_t InputCount() const = 0;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100837 virtual HInstruction* InputAt(size_t i) const = 0;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000838
839 virtual void Accept(HGraphVisitor* visitor) = 0;
840 virtual const char* DebugName() const = 0;
841
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100842 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100843 virtual void SetRawInputAt(size_t index, HInstruction* input) = 0;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100844
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100845 virtual bool NeedsEnvironment() const { return false; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100846 virtual bool IsControlFlow() const { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +0100847 virtual bool CanThrow() const { return false; }
Roland Levillain72bceff2014-09-15 18:29:00 +0100848 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100849
Calin Juravle77520bc2015-01-12 18:45:46 +0000850 virtual bool CanDoImplicitNullCheck() const { return false; }
851
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100852 void AddUseAt(HInstruction* user, size_t index) {
David Brazdiled596192015-01-23 10:39:45 +0000853 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000854 }
855
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100856 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +0100857 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +0000858 HUseListNode<HEnvironment*>* env_use =
859 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
860 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100861 }
862
863 void RemoveUser(HInstruction* user, size_t index);
David Brazdiled596192015-01-23 10:39:45 +0000864 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100865
David Brazdilea55b932015-01-27 17:12:29 +0000866 const HUseList<HInstruction*>& GetUses() { return uses_; }
867 const HUseList<HEnvironment*>& GetEnvUses() { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000868
David Brazdiled596192015-01-23 10:39:45 +0000869 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
870 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000871
Roland Levillain6c82d402014-10-13 16:10:27 +0100872 // Does this instruction strictly dominate `other_instruction`?
873 // Returns false if this instruction and `other_instruction` are the same.
874 // Aborts if this instruction and `other_instruction` are both phis.
875 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +0100876
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000877 int GetId() const { return id_; }
878 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000879
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100880 int GetSsaIndex() const { return ssa_index_; }
881 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
882 bool HasSsaIndex() const { return ssa_index_ != -1; }
883
884 bool HasEnvironment() const { return environment_ != nullptr; }
885 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100886 void SetEnvironment(HEnvironment* environment) { environment_ = environment; }
887
Nicolas Geoffray39468442014-09-02 15:17:15 +0100888 // Returns the number of entries in the environment. Typically, that is the
889 // number of dex registers in a method. It could be more in case of inlining.
890 size_t EnvironmentSize() const;
891
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000892 LocationSummary* GetLocations() const { return locations_; }
893 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000894
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100895 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100896 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100897
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000898 // Insert `this` instruction in `cursor`'s graph, just before `cursor`.
899 void InsertBefore(HInstruction* cursor);
900
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100901#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +0100902 bool Is##type() const { return (As##type() != nullptr); } \
903 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000904 virtual H##type* As##type() { return nullptr; }
905
906 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
907#undef INSTRUCTION_TYPE_CHECK
908
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100909 // Returns whether the instruction can be moved within the graph.
910 virtual bool CanBeMoved() const { return false; }
911
912 // Returns whether the two instructions are of the same kind.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700913 virtual bool InstructionTypeEquals(HInstruction* other) const {
914 UNUSED(other);
915 return false;
916 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100917
918 // Returns whether any data encoded in the two instructions is equal.
919 // This method does not look at the inputs. Both instructions must be
920 // of the same type, otherwise the method has undefined behavior.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700921 virtual bool InstructionDataEquals(HInstruction* other) const {
922 UNUSED(other);
923 return false;
924 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100925
926 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +0000927 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100928 // 2) Their inputs are identical.
929 bool Equals(HInstruction* other) const;
930
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100931 virtual InstructionKind GetKind() const = 0;
932
933 virtual size_t ComputeHashCode() const {
934 size_t result = GetKind();
935 for (size_t i = 0, e = InputCount(); i < e; ++i) {
936 result = (result * 31) + InputAt(i)->GetId();
937 }
938 return result;
939 }
940
941 SideEffects GetSideEffects() const { return side_effects_; }
942
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100943 size_t GetLifetimePosition() const { return lifetime_position_; }
944 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
945 LiveInterval* GetLiveInterval() const { return live_interval_; }
946 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
947 bool HasLiveInterval() const { return live_interval_ != nullptr; }
948
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000949 private:
950 HInstruction* previous_;
951 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000952 HBasicBlock* block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000953
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000954 // An instruction gets an id when it is added to the graph.
955 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +0100956 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000957 int id_;
958
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100959 // When doing liveness analysis, instructions that have uses get an SSA index.
960 int ssa_index_;
961
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100962 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +0000963 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100964
965 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +0000966 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100967
Nicolas Geoffray39468442014-09-02 15:17:15 +0100968 // The environment associated with this instruction. Not null if the instruction
969 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100970 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000971
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000972 // Set by the code generator.
973 LocationSummary* locations_;
974
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100975 // Set by the liveness analysis.
976 LiveInterval* live_interval_;
977
978 // Set by the liveness analysis, this is the position in a linear
979 // order of blocks where this instruction's live interval start.
980 size_t lifetime_position_;
981
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100982 const SideEffects side_effects_;
983
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000984 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000985 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100986 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000987
988 DISALLOW_COPY_AND_ASSIGN(HInstruction);
989};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700990std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000991
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000992class HInputIterator : public ValueObject {
993 public:
Dave Allison20dfc792014-06-16 20:44:29 -0700994 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000995
996 bool Done() const { return index_ == instruction_->InputCount(); }
997 HInstruction* Current() const { return instruction_->InputAt(index_); }
998 void Advance() { index_++; }
999
1000 private:
1001 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001002 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001003
1004 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
1005};
1006
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001007class HInstructionIterator : public ValueObject {
1008 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001009 explicit HInstructionIterator(const HInstructionList& instructions)
1010 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001011 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001012 }
1013
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001014 bool Done() const { return instruction_ == nullptr; }
1015 HInstruction* Current() const { return instruction_; }
1016 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001017 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001018 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001019 }
1020
1021 private:
1022 HInstruction* instruction_;
1023 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001024
1025 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001026};
1027
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001028class HBackwardInstructionIterator : public ValueObject {
1029 public:
1030 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
1031 : instruction_(instructions.last_instruction_) {
1032 next_ = Done() ? nullptr : instruction_->GetPrevious();
1033 }
1034
1035 bool Done() const { return instruction_ == nullptr; }
1036 HInstruction* Current() const { return instruction_; }
1037 void Advance() {
1038 instruction_ = next_;
1039 next_ = Done() ? nullptr : instruction_->GetPrevious();
1040 }
1041
1042 private:
1043 HInstruction* instruction_;
1044 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001045
1046 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001047};
1048
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001049// An embedded container with N elements of type T. Used (with partial
1050// specialization for N=0) because embedded arrays cannot have size 0.
1051template<typename T, intptr_t N>
1052class EmbeddedArray {
1053 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001054 EmbeddedArray() : elements_() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001055
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001056 intptr_t GetLength() const { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001057
1058 const T& operator[](intptr_t i) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001059 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001060 return elements_[i];
1061 }
1062
1063 T& operator[](intptr_t i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001064 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001065 return elements_[i];
1066 }
1067
1068 const T& At(intptr_t i) const {
1069 return (*this)[i];
1070 }
1071
1072 void SetAt(intptr_t i, const T& val) {
1073 (*this)[i] = val;
1074 }
1075
1076 private:
1077 T elements_[N];
1078};
1079
1080template<typename T>
1081class EmbeddedArray<T, 0> {
1082 public:
1083 intptr_t length() const { return 0; }
1084 const T& operator[](intptr_t i) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001085 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001086 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001087 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001088 }
1089 T& operator[](intptr_t i) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001090 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001091 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001092 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001093 }
1094};
1095
1096template<intptr_t N>
1097class HTemplateInstruction: public HInstruction {
1098 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001099 HTemplateInstruction<N>(SideEffects side_effects)
1100 : HInstruction(side_effects), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07001101 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001102
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001103 virtual size_t InputCount() const { return N; }
1104 virtual HInstruction* InputAt(size_t i) const { return inputs_[i]; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001105
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001106 protected:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001107 virtual void SetRawInputAt(size_t i, HInstruction* instruction) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001108 inputs_[i] = instruction;
1109 }
1110
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001111 private:
1112 EmbeddedArray<HInstruction*, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001113
1114 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001115};
1116
Dave Allison20dfc792014-06-16 20:44:29 -07001117template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001118class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07001119 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001120 HExpression<N>(Primitive::Type type, SideEffects side_effects)
1121 : HTemplateInstruction<N>(side_effects), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07001122 virtual ~HExpression() {}
1123
1124 virtual Primitive::Type GetType() const { return type_; }
1125
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001126 protected:
1127 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07001128};
1129
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001130// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
1131// instruction that branches to the exit block.
1132class HReturnVoid : public HTemplateInstruction<0> {
1133 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001134 HReturnVoid() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001135
1136 virtual bool IsControlFlow() const { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001137
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001138 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001139
1140 private:
1141 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
1142};
1143
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001144// Represents dex's RETURN opcodes. A HReturn is a control flow
1145// instruction that branches to the exit block.
1146class HReturn : public HTemplateInstruction<1> {
1147 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001148 explicit HReturn(HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001149 SetRawInputAt(0, value);
1150 }
1151
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001152 virtual bool IsControlFlow() const { return true; }
1153
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001154 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001155
1156 private:
1157 DISALLOW_COPY_AND_ASSIGN(HReturn);
1158};
1159
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001160// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001161// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001162// exit block.
1163class HExit : public HTemplateInstruction<0> {
1164 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001165 HExit() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001166
1167 virtual bool IsControlFlow() const { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001168
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001169 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001170
1171 private:
1172 DISALLOW_COPY_AND_ASSIGN(HExit);
1173};
1174
1175// Jumps from one block to another.
1176class HGoto : public HTemplateInstruction<0> {
1177 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001178 HGoto() : HTemplateInstruction(SideEffects::None()) {}
1179
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001180 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001181
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001182 HBasicBlock* GetSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001183 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001184 }
1185
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001186 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001187
1188 private:
1189 DISALLOW_COPY_AND_ASSIGN(HGoto);
1190};
1191
Dave Allison20dfc792014-06-16 20:44:29 -07001192
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001193// Conditional branch. A block ending with an HIf instruction must have
1194// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001195class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001196 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001197 explicit HIf(HInstruction* input) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001198 SetRawInputAt(0, input);
1199 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001200
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001201 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001202
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001203 HBasicBlock* IfTrueSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001204 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001205 }
1206
1207 HBasicBlock* IfFalseSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001208 return GetBlock()->GetSuccessors().Get(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001209 }
1210
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001211 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001212
Dave Allison20dfc792014-06-16 20:44:29 -07001213 virtual bool IsIfInstruction() const { return true; }
1214
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001215 private:
1216 DISALLOW_COPY_AND_ASSIGN(HIf);
1217};
1218
Roland Levillain88cb1752014-10-20 16:36:47 +01001219class HUnaryOperation : public HExpression<1> {
1220 public:
1221 HUnaryOperation(Primitive::Type result_type, HInstruction* input)
1222 : HExpression(result_type, SideEffects::None()) {
1223 SetRawInputAt(0, input);
1224 }
1225
1226 HInstruction* GetInput() const { return InputAt(0); }
1227 Primitive::Type GetResultType() const { return GetType(); }
1228
1229 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001230 virtual bool InstructionDataEquals(HInstruction* other) const {
1231 UNUSED(other);
1232 return true;
1233 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001234
Roland Levillain9240d6a2014-10-20 16:47:04 +01001235 // Try to statically evaluate `operation` and return a HConstant
1236 // containing the result of this evaluation. If `operation` cannot
1237 // be evaluated as a constant, return nullptr.
1238 HConstant* TryStaticEvaluation() const;
1239
1240 // Apply this operation to `x`.
1241 virtual int32_t Evaluate(int32_t x) const = 0;
1242 virtual int64_t Evaluate(int64_t x) const = 0;
1243
Roland Levillain88cb1752014-10-20 16:36:47 +01001244 DECLARE_INSTRUCTION(UnaryOperation);
1245
1246 private:
1247 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
1248};
1249
Dave Allison20dfc792014-06-16 20:44:29 -07001250class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001251 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001252 HBinaryOperation(Primitive::Type result_type,
1253 HInstruction* left,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001254 HInstruction* right) : HExpression(result_type, SideEffects::None()) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001255 SetRawInputAt(0, left);
1256 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001257 }
1258
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001259 HInstruction* GetLeft() const { return InputAt(0); }
1260 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07001261 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001262
1263 virtual bool IsCommutative() { return false; }
1264
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001265 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001266 virtual bool InstructionDataEquals(HInstruction* other) const {
1267 UNUSED(other);
1268 return true;
1269 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001270
Roland Levillain9240d6a2014-10-20 16:47:04 +01001271 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01001272 // containing the result of this evaluation. If `operation` cannot
1273 // be evaluated as a constant, return nullptr.
Roland Levillain9240d6a2014-10-20 16:47:04 +01001274 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01001275
1276 // Apply this operation to `x` and `y`.
1277 virtual int32_t Evaluate(int32_t x, int32_t y) const = 0;
1278 virtual int64_t Evaluate(int64_t x, int64_t y) const = 0;
1279
Roland Levillainccc07a92014-09-16 14:48:16 +01001280 DECLARE_INSTRUCTION(BinaryOperation);
1281
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001282 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001283 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
1284};
1285
Dave Allison20dfc792014-06-16 20:44:29 -07001286class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001287 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001288 HCondition(HInstruction* first, HInstruction* second)
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001289 : HBinaryOperation(Primitive::kPrimBoolean, first, second),
1290 needs_materialization_(true) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001291
1292 virtual bool IsCommutative() { return true; }
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001293
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001294 bool NeedsMaterialization() const { return needs_materialization_; }
1295 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001296
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001297 // For code generation purposes, returns whether this instruction is just before
1298 // `if_`, and disregard moves in between.
1299 bool IsBeforeWhenDisregardMoves(HIf* if_) const;
1300
Dave Allison20dfc792014-06-16 20:44:29 -07001301 DECLARE_INSTRUCTION(Condition);
1302
1303 virtual IfCondition GetCondition() const = 0;
1304
1305 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001306 // For register allocation purposes, returns whether this instruction needs to be
1307 // materialized (that is, not just be in the processor flags).
1308 bool needs_materialization_;
1309
Dave Allison20dfc792014-06-16 20:44:29 -07001310 DISALLOW_COPY_AND_ASSIGN(HCondition);
1311};
1312
1313// Instruction to check if two inputs are equal to each other.
1314class HEqual : public HCondition {
1315 public:
1316 HEqual(HInstruction* first, HInstruction* second)
1317 : HCondition(first, second) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001318
Roland Levillain93445682014-10-06 19:24:02 +01001319 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1320 return x == y ? 1 : 0;
1321 }
1322 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1323 return x == y ? 1 : 0;
1324 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001325
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001326 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001327
Dave Allison20dfc792014-06-16 20:44:29 -07001328 virtual IfCondition GetCondition() const {
1329 return kCondEQ;
1330 }
1331
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001332 private:
1333 DISALLOW_COPY_AND_ASSIGN(HEqual);
1334};
1335
Dave Allison20dfc792014-06-16 20:44:29 -07001336class HNotEqual : public HCondition {
1337 public:
1338 HNotEqual(HInstruction* first, HInstruction* second)
1339 : HCondition(first, second) {}
1340
Roland Levillain93445682014-10-06 19:24:02 +01001341 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1342 return x != y ? 1 : 0;
1343 }
1344 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1345 return x != y ? 1 : 0;
1346 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001347
Dave Allison20dfc792014-06-16 20:44:29 -07001348 DECLARE_INSTRUCTION(NotEqual);
1349
1350 virtual IfCondition GetCondition() const {
1351 return kCondNE;
1352 }
1353
1354 private:
1355 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
1356};
1357
1358class HLessThan : public HCondition {
1359 public:
1360 HLessThan(HInstruction* first, HInstruction* second)
1361 : HCondition(first, second) {}
1362
Roland Levillain93445682014-10-06 19:24:02 +01001363 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1364 return x < y ? 1 : 0;
1365 }
1366 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1367 return x < y ? 1 : 0;
1368 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001369
Dave Allison20dfc792014-06-16 20:44:29 -07001370 DECLARE_INSTRUCTION(LessThan);
1371
1372 virtual IfCondition GetCondition() const {
1373 return kCondLT;
1374 }
1375
1376 private:
1377 DISALLOW_COPY_AND_ASSIGN(HLessThan);
1378};
1379
1380class HLessThanOrEqual : public HCondition {
1381 public:
1382 HLessThanOrEqual(HInstruction* first, HInstruction* second)
1383 : HCondition(first, second) {}
1384
Roland Levillain93445682014-10-06 19:24:02 +01001385 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1386 return x <= y ? 1 : 0;
1387 }
1388 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1389 return x <= y ? 1 : 0;
1390 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001391
Dave Allison20dfc792014-06-16 20:44:29 -07001392 DECLARE_INSTRUCTION(LessThanOrEqual);
1393
1394 virtual IfCondition GetCondition() const {
1395 return kCondLE;
1396 }
1397
1398 private:
1399 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
1400};
1401
1402class HGreaterThan : public HCondition {
1403 public:
1404 HGreaterThan(HInstruction* first, HInstruction* second)
1405 : HCondition(first, second) {}
1406
Roland Levillain93445682014-10-06 19:24:02 +01001407 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1408 return x > y ? 1 : 0;
1409 }
1410 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1411 return x > y ? 1 : 0;
1412 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001413
Dave Allison20dfc792014-06-16 20:44:29 -07001414 DECLARE_INSTRUCTION(GreaterThan);
1415
1416 virtual IfCondition GetCondition() const {
1417 return kCondGT;
1418 }
1419
1420 private:
1421 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
1422};
1423
1424class HGreaterThanOrEqual : public HCondition {
1425 public:
1426 HGreaterThanOrEqual(HInstruction* first, HInstruction* second)
1427 : HCondition(first, second) {}
1428
Roland Levillain93445682014-10-06 19:24:02 +01001429 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1430 return x >= y ? 1 : 0;
1431 }
1432 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1433 return x >= y ? 1 : 0;
1434 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001435
Dave Allison20dfc792014-06-16 20:44:29 -07001436 DECLARE_INSTRUCTION(GreaterThanOrEqual);
1437
1438 virtual IfCondition GetCondition() const {
1439 return kCondGE;
1440 }
1441
1442 private:
1443 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
1444};
1445
1446
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001447// Instruction to check how two inputs compare to each other.
1448// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
1449class HCompare : public HBinaryOperation {
1450 public:
Calin Juravleddb7df22014-11-25 20:56:51 +00001451 // The bias applies for floating point operations and indicates how NaN
1452 // comparisons are treated:
1453 enum Bias {
1454 kNoBias, // bias is not applicable (i.e. for long operation)
1455 kGtBias, // return 1 for NaN comparisons
1456 kLtBias, // return -1 for NaN comparisons
1457 };
1458
1459 HCompare(Primitive::Type type, HInstruction* first, HInstruction* second, Bias bias)
1460 : HBinaryOperation(Primitive::kPrimInt, first, second), bias_(bias) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001461 DCHECK_EQ(type, first->GetType());
1462 DCHECK_EQ(type, second->GetType());
1463 }
1464
Calin Juravleddb7df22014-11-25 20:56:51 +00001465 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001466 return
1467 x == y ? 0 :
1468 x > y ? 1 :
1469 -1;
1470 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001471
1472 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001473 return
1474 x == y ? 0 :
1475 x > y ? 1 :
1476 -1;
1477 }
1478
Calin Juravleddb7df22014-11-25 20:56:51 +00001479 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
1480 return bias_ == other->AsCompare()->bias_;
1481 }
1482
1483 bool IsGtBias() { return bias_ == kGtBias; }
1484
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001485 DECLARE_INSTRUCTION(Compare);
1486
1487 private:
Calin Juravleddb7df22014-11-25 20:56:51 +00001488 const Bias bias_;
1489
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001490 DISALLOW_COPY_AND_ASSIGN(HCompare);
1491};
1492
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001493// A local in the graph. Corresponds to a Dex register.
1494class HLocal : public HTemplateInstruction<0> {
1495 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001496 explicit HLocal(uint16_t reg_number)
1497 : HTemplateInstruction(SideEffects::None()), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001498
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001499 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001500
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001501 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001502
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001503 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001504 // The Dex register number.
1505 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001506
1507 DISALLOW_COPY_AND_ASSIGN(HLocal);
1508};
1509
1510// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07001511class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001512 public:
Roland Levillain5799fc02014-09-25 12:15:20 +01001513 HLoadLocal(HLocal* local, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001514 : HExpression(type, SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001515 SetRawInputAt(0, local);
1516 }
1517
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001518 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1519
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001520 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001521
1522 private:
1523 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
1524};
1525
1526// Store a value in a given local. This instruction has two inputs: the value
1527// and the local.
1528class HStoreLocal : public HTemplateInstruction<2> {
1529 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001530 HStoreLocal(HLocal* local, HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001531 SetRawInputAt(0, local);
1532 SetRawInputAt(1, value);
1533 }
1534
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001535 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1536
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001537 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001538
1539 private:
1540 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
1541};
1542
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001543class HConstant : public HExpression<0> {
1544 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001545 explicit HConstant(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
1546
1547 virtual bool CanBeMoved() const { return true; }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001548
1549 DECLARE_INSTRUCTION(Constant);
1550
1551 private:
1552 DISALLOW_COPY_AND_ASSIGN(HConstant);
1553};
1554
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001555class HFloatConstant : public HConstant {
1556 public:
1557 explicit HFloatConstant(float value) : HConstant(Primitive::kPrimFloat), value_(value) {}
1558
1559 float GetValue() const { return value_; }
1560
1561 virtual bool InstructionDataEquals(HInstruction* other) const {
1562 return bit_cast<float, int32_t>(other->AsFloatConstant()->value_) ==
1563 bit_cast<float, int32_t>(value_);
1564 }
1565
1566 virtual size_t ComputeHashCode() const { return static_cast<size_t>(GetValue()); }
1567
1568 DECLARE_INSTRUCTION(FloatConstant);
1569
1570 private:
1571 const float value_;
1572
1573 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
1574};
1575
1576class HDoubleConstant : public HConstant {
1577 public:
1578 explicit HDoubleConstant(double value) : HConstant(Primitive::kPrimDouble), value_(value) {}
1579
1580 double GetValue() const { return value_; }
1581
1582 virtual bool InstructionDataEquals(HInstruction* other) const {
1583 return bit_cast<double, int64_t>(other->AsDoubleConstant()->value_) ==
1584 bit_cast<double, int64_t>(value_);
1585 }
1586
1587 virtual size_t ComputeHashCode() const { return static_cast<size_t>(GetValue()); }
1588
1589 DECLARE_INSTRUCTION(DoubleConstant);
1590
1591 private:
1592 const double value_;
1593
1594 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
1595};
1596
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001597// Constants of the type int. Those can be from Dex instructions, or
1598// synthesized (for example with the if-eqz instruction).
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001599class HIntConstant : public HConstant {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001600 public:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001601 explicit HIntConstant(int32_t value) : HConstant(Primitive::kPrimInt), value_(value) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001602
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001603 int32_t GetValue() const { return value_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001604
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001605 virtual bool InstructionDataEquals(HInstruction* other) const {
1606 return other->AsIntConstant()->value_ == value_;
1607 }
1608
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001609 virtual size_t ComputeHashCode() const { return GetValue(); }
1610
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001611 DECLARE_INSTRUCTION(IntConstant);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001612
1613 private:
1614 const int32_t value_;
1615
1616 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
1617};
1618
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001619class HLongConstant : public HConstant {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001620 public:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001621 explicit HLongConstant(int64_t value) : HConstant(Primitive::kPrimLong), value_(value) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001622
1623 int64_t GetValue() const { return value_; }
1624
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001625 virtual bool InstructionDataEquals(HInstruction* other) const {
1626 return other->AsLongConstant()->value_ == value_;
1627 }
1628
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001629 virtual size_t ComputeHashCode() const { return static_cast<size_t>(GetValue()); }
1630
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001631 DECLARE_INSTRUCTION(LongConstant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001632
1633 private:
1634 const int64_t value_;
1635
1636 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
1637};
1638
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001639enum class Intrinsics {
1640#define OPTIMIZING_INTRINSICS(Name, IsStatic) k ## Name,
1641#include "intrinsics_list.h"
1642 kNone,
1643 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
1644#undef INTRINSICS_LIST
1645#undef OPTIMIZING_INTRINSICS
1646};
1647std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
1648
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001649class HInvoke : public HInstruction {
1650 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001651 virtual size_t InputCount() const { return inputs_.Size(); }
1652 virtual HInstruction* InputAt(size_t i) const { return inputs_.Get(i); }
1653
1654 // Runtime needs to walk the stack, so Dex -> Dex calls need to
1655 // know their environment.
Calin Juravle77520bc2015-01-12 18:45:46 +00001656 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001657
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001658 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001659 SetRawInputAt(index, argument);
1660 }
1661
1662 virtual void SetRawInputAt(size_t index, HInstruction* input) {
1663 inputs_.Put(index, input);
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001664 }
1665
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001666 virtual Primitive::Type GetType() const { return return_type_; }
1667
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001668 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001669
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001670 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
1671
1672 Intrinsics GetIntrinsic() {
1673 return intrinsic_;
1674 }
1675
1676 void SetIntrinsic(Intrinsics intrinsic) {
1677 intrinsic_ = intrinsic;
1678 }
1679
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001680 DECLARE_INSTRUCTION(Invoke);
1681
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001682 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001683 HInvoke(ArenaAllocator* arena,
1684 uint32_t number_of_arguments,
1685 Primitive::Type return_type,
1686 uint32_t dex_pc,
1687 uint32_t dex_method_index)
1688 : HInstruction(SideEffects::All()),
1689 inputs_(arena, number_of_arguments),
1690 return_type_(return_type),
1691 dex_pc_(dex_pc),
1692 dex_method_index_(dex_method_index),
1693 intrinsic_(Intrinsics::kNone) {
1694 inputs_.SetSize(number_of_arguments);
1695 }
1696
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001697 GrowableArray<HInstruction*> inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001698 const Primitive::Type return_type_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001699 const uint32_t dex_pc_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001700 const uint32_t dex_method_index_;
1701 Intrinsics intrinsic_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001702
1703 private:
1704 DISALLOW_COPY_AND_ASSIGN(HInvoke);
1705};
1706
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001707class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001708 public:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001709 HInvokeStaticOrDirect(ArenaAllocator* arena,
1710 uint32_t number_of_arguments,
1711 Primitive::Type return_type,
1712 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001713 uint32_t dex_method_index,
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001714 bool is_recursive,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001715 InvokeType invoke_type)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001716 : HInvoke(arena, number_of_arguments, return_type, dex_pc, dex_method_index),
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001717 invoke_type_(invoke_type),
1718 is_recursive_(is_recursive) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001719
Calin Juravle77520bc2015-01-12 18:45:46 +00001720 bool CanDoImplicitNullCheck() const OVERRIDE {
1721 // We access the method via the dex cache so we can't do an implicit null check.
1722 // TODO: for intrinsics we can generate implicit null checks.
1723 return false;
1724 }
1725
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001726 InvokeType GetInvokeType() const { return invoke_type_; }
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001727 bool IsRecursive() const { return is_recursive_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001728
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001729 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001730
1731 private:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001732 const InvokeType invoke_type_;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001733 const bool is_recursive_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001734
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001735 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001736};
1737
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001738class HInvokeVirtual : public HInvoke {
1739 public:
1740 HInvokeVirtual(ArenaAllocator* arena,
1741 uint32_t number_of_arguments,
1742 Primitive::Type return_type,
1743 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001744 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001745 uint32_t vtable_index)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001746 : HInvoke(arena, number_of_arguments, return_type, dex_pc, dex_method_index),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001747 vtable_index_(vtable_index) {}
1748
Calin Juravle77520bc2015-01-12 18:45:46 +00001749 bool CanDoImplicitNullCheck() const OVERRIDE {
1750 // TODO: Add implicit null checks in intrinsics.
1751 return !GetLocations()->Intrinsified();
1752 }
1753
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001754 uint32_t GetVTableIndex() const { return vtable_index_; }
1755
1756 DECLARE_INSTRUCTION(InvokeVirtual);
1757
1758 private:
1759 const uint32_t vtable_index_;
1760
1761 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
1762};
1763
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001764class HInvokeInterface : public HInvoke {
1765 public:
1766 HInvokeInterface(ArenaAllocator* arena,
1767 uint32_t number_of_arguments,
1768 Primitive::Type return_type,
1769 uint32_t dex_pc,
1770 uint32_t dex_method_index,
1771 uint32_t imt_index)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001772 : HInvoke(arena, number_of_arguments, return_type, dex_pc, dex_method_index),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001773 imt_index_(imt_index) {}
1774
Calin Juravle77520bc2015-01-12 18:45:46 +00001775 bool CanDoImplicitNullCheck() const OVERRIDE {
1776 // TODO: Add implicit null checks in intrinsics.
1777 return !GetLocations()->Intrinsified();
1778 }
1779
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001780 uint32_t GetImtIndex() const { return imt_index_; }
1781 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
1782
1783 DECLARE_INSTRUCTION(InvokeInterface);
1784
1785 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001786 const uint32_t imt_index_;
1787
1788 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
1789};
1790
Dave Allison20dfc792014-06-16 20:44:29 -07001791class HNewInstance : public HExpression<0> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001792 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001793 HNewInstance(uint32_t dex_pc, uint16_t type_index)
1794 : HExpression(Primitive::kPrimNot, SideEffects::None()),
1795 dex_pc_(dex_pc),
1796 type_index_(type_index) {}
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001797
1798 uint32_t GetDexPc() const { return dex_pc_; }
1799 uint16_t GetTypeIndex() const { return type_index_; }
1800
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001801 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00001802 bool NeedsEnvironment() const OVERRIDE { return true; }
1803 // It may throw when called on:
1804 // - interfaces
1805 // - abstract/innaccessible/unknown classes
1806 // TODO: optimize when possible.
1807 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001808
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001809 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001810
1811 private:
1812 const uint32_t dex_pc_;
1813 const uint16_t type_index_;
1814
1815 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
1816};
1817
Roland Levillain88cb1752014-10-20 16:36:47 +01001818class HNeg : public HUnaryOperation {
1819 public:
1820 explicit HNeg(Primitive::Type result_type, HInstruction* input)
1821 : HUnaryOperation(result_type, input) {}
1822
Roland Levillain9240d6a2014-10-20 16:47:04 +01001823 virtual int32_t Evaluate(int32_t x) const OVERRIDE { return -x; }
1824 virtual int64_t Evaluate(int64_t x) const OVERRIDE { return -x; }
1825
Roland Levillain88cb1752014-10-20 16:36:47 +01001826 DECLARE_INSTRUCTION(Neg);
1827
1828 private:
1829 DISALLOW_COPY_AND_ASSIGN(HNeg);
1830};
1831
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001832class HNewArray : public HExpression<1> {
1833 public:
1834 HNewArray(HInstruction* length, uint32_t dex_pc, uint16_t type_index)
1835 : HExpression(Primitive::kPrimNot, SideEffects::None()),
1836 dex_pc_(dex_pc),
1837 type_index_(type_index) {
1838 SetRawInputAt(0, length);
1839 }
1840
1841 uint32_t GetDexPc() const { return dex_pc_; }
1842 uint16_t GetTypeIndex() const { return type_index_; }
1843
1844 // Calls runtime so needs an environment.
1845 virtual bool NeedsEnvironment() const { return true; }
1846
1847 DECLARE_INSTRUCTION(NewArray);
1848
1849 private:
1850 const uint32_t dex_pc_;
1851 const uint16_t type_index_;
1852
1853 DISALLOW_COPY_AND_ASSIGN(HNewArray);
1854};
1855
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001856class HAdd : public HBinaryOperation {
1857 public:
1858 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1859 : HBinaryOperation(result_type, left, right) {}
1860
1861 virtual bool IsCommutative() { return true; }
1862
Roland Levillain93445682014-10-06 19:24:02 +01001863 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1864 return x + y;
1865 }
1866 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1867 return x + y;
1868 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001869
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001870 DECLARE_INSTRUCTION(Add);
1871
1872 private:
1873 DISALLOW_COPY_AND_ASSIGN(HAdd);
1874};
1875
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001876class HSub : public HBinaryOperation {
1877 public:
1878 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1879 : HBinaryOperation(result_type, left, right) {}
1880
Roland Levillain93445682014-10-06 19:24:02 +01001881 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1882 return x - y;
1883 }
1884 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1885 return x - y;
1886 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001887
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001888 DECLARE_INSTRUCTION(Sub);
1889
1890 private:
1891 DISALLOW_COPY_AND_ASSIGN(HSub);
1892};
1893
Calin Juravle34bacdf2014-10-07 20:23:36 +01001894class HMul : public HBinaryOperation {
1895 public:
1896 HMul(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1897 : HBinaryOperation(result_type, left, right) {}
1898
1899 virtual bool IsCommutative() { return true; }
1900
1901 virtual int32_t Evaluate(int32_t x, int32_t y) const { return x * y; }
1902 virtual int64_t Evaluate(int64_t x, int64_t y) const { return x * y; }
1903
1904 DECLARE_INSTRUCTION(Mul);
1905
1906 private:
1907 DISALLOW_COPY_AND_ASSIGN(HMul);
1908};
1909
Calin Juravle7c4954d2014-10-28 16:57:40 +00001910class HDiv : public HBinaryOperation {
1911 public:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001912 HDiv(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
1913 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00001914
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00001915 virtual int32_t Evaluate(int32_t x, int32_t y) const {
1916 // Our graph structure ensures we never have 0 for `y` during constant folding.
1917 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00001918 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00001919 return (y == -1) ? -x : x / y;
1920 }
Calin Juravlebacfec32014-11-14 15:54:36 +00001921
1922 virtual int64_t Evaluate(int64_t x, int64_t y) const {
1923 DCHECK_NE(y, 0);
1924 // Special case -1 to avoid getting a SIGFPE on x86(_64).
1925 return (y == -1) ? -x : x / y;
1926 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00001927
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001928 uint32_t GetDexPc() const { return dex_pc_; }
1929
Calin Juravle7c4954d2014-10-28 16:57:40 +00001930 DECLARE_INSTRUCTION(Div);
1931
1932 private:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001933 const uint32_t dex_pc_;
1934
Calin Juravle7c4954d2014-10-28 16:57:40 +00001935 DISALLOW_COPY_AND_ASSIGN(HDiv);
1936};
1937
Calin Juravlebacfec32014-11-14 15:54:36 +00001938class HRem : public HBinaryOperation {
1939 public:
1940 HRem(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
1941 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
1942
1943 virtual int32_t Evaluate(int32_t x, int32_t y) const {
1944 DCHECK_NE(y, 0);
1945 // Special case -1 to avoid getting a SIGFPE on x86(_64).
1946 return (y == -1) ? 0 : x % y;
1947 }
1948
1949 virtual int64_t Evaluate(int64_t x, int64_t y) const {
1950 DCHECK_NE(y, 0);
1951 // Special case -1 to avoid getting a SIGFPE on x86(_64).
1952 return (y == -1) ? 0 : x % y;
1953 }
1954
1955 uint32_t GetDexPc() const { return dex_pc_; }
1956
1957 DECLARE_INSTRUCTION(Rem);
1958
1959 private:
1960 const uint32_t dex_pc_;
1961
1962 DISALLOW_COPY_AND_ASSIGN(HRem);
1963};
1964
Calin Juravled0d48522014-11-04 16:40:20 +00001965class HDivZeroCheck : public HExpression<1> {
1966 public:
1967 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
1968 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
1969 SetRawInputAt(0, value);
1970 }
1971
1972 bool CanBeMoved() const OVERRIDE { return true; }
1973
1974 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
1975 UNUSED(other);
1976 return true;
1977 }
1978
1979 bool NeedsEnvironment() const OVERRIDE { return true; }
1980 bool CanThrow() const OVERRIDE { return true; }
1981
1982 uint32_t GetDexPc() const { return dex_pc_; }
1983
1984 DECLARE_INSTRUCTION(DivZeroCheck);
1985
1986 private:
1987 const uint32_t dex_pc_;
1988
1989 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
1990};
1991
Calin Juravle9aec02f2014-11-18 23:06:35 +00001992class HShl : public HBinaryOperation {
1993 public:
1994 HShl(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1995 : HBinaryOperation(result_type, left, right) {}
1996
1997 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x << (y & kMaxIntShiftValue); }
1998 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x << (y & kMaxLongShiftValue); }
1999
2000 DECLARE_INSTRUCTION(Shl);
2001
2002 private:
2003 DISALLOW_COPY_AND_ASSIGN(HShl);
2004};
2005
2006class HShr : public HBinaryOperation {
2007 public:
2008 HShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2009 : HBinaryOperation(result_type, left, right) {}
2010
2011 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x >> (y & kMaxIntShiftValue); }
2012 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x >> (y & kMaxLongShiftValue); }
2013
2014 DECLARE_INSTRUCTION(Shr);
2015
2016 private:
2017 DISALLOW_COPY_AND_ASSIGN(HShr);
2018};
2019
2020class HUShr : public HBinaryOperation {
2021 public:
2022 HUShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2023 : HBinaryOperation(result_type, left, right) {}
2024
2025 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
2026 uint32_t ux = static_cast<uint32_t>(x);
2027 uint32_t uy = static_cast<uint32_t>(y) & kMaxIntShiftValue;
2028 return static_cast<int32_t>(ux >> uy);
2029 }
2030
2031 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
2032 uint64_t ux = static_cast<uint64_t>(x);
2033 uint64_t uy = static_cast<uint64_t>(y) & kMaxLongShiftValue;
2034 return static_cast<int64_t>(ux >> uy);
2035 }
2036
2037 DECLARE_INSTRUCTION(UShr);
2038
2039 private:
2040 DISALLOW_COPY_AND_ASSIGN(HUShr);
2041};
2042
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002043class HAnd : public HBinaryOperation {
2044 public:
2045 HAnd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2046 : HBinaryOperation(result_type, left, right) {}
2047
2048 bool IsCommutative() OVERRIDE { return true; }
2049
2050 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x & y; }
2051 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x & y; }
2052
2053 DECLARE_INSTRUCTION(And);
2054
2055 private:
2056 DISALLOW_COPY_AND_ASSIGN(HAnd);
2057};
2058
2059class HOr : public HBinaryOperation {
2060 public:
2061 HOr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2062 : HBinaryOperation(result_type, left, right) {}
2063
2064 bool IsCommutative() OVERRIDE { return true; }
2065
2066 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x | y; }
2067 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x | y; }
2068
2069 DECLARE_INSTRUCTION(Or);
2070
2071 private:
2072 DISALLOW_COPY_AND_ASSIGN(HOr);
2073};
2074
2075class HXor : public HBinaryOperation {
2076 public:
2077 HXor(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2078 : HBinaryOperation(result_type, left, right) {}
2079
2080 bool IsCommutative() OVERRIDE { return true; }
2081
2082 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x ^ y; }
2083 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x ^ y; }
2084
2085 DECLARE_INSTRUCTION(Xor);
2086
2087 private:
2088 DISALLOW_COPY_AND_ASSIGN(HXor);
2089};
2090
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002091// The value of a parameter in this method. Its location depends on
2092// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07002093class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002094 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002095 HParameterValue(uint8_t index, Primitive::Type parameter_type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002096 : HExpression(parameter_type, SideEffects::None()), index_(index) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002097
2098 uint8_t GetIndex() const { return index_; }
2099
2100 DECLARE_INSTRUCTION(ParameterValue);
2101
2102 private:
2103 // The index of this parameter in the parameters list. Must be less
2104 // than HGraph::number_of_in_vregs_;
2105 const uint8_t index_;
2106
2107 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
2108};
2109
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002110class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002111 public:
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002112 explicit HNot(Primitive::Type result_type, HInstruction* input)
2113 : HUnaryOperation(result_type, input) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002114
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002115 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002116 virtual bool InstructionDataEquals(HInstruction* other) const {
2117 UNUSED(other);
2118 return true;
2119 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002120
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002121 virtual int32_t Evaluate(int32_t x) const OVERRIDE { return ~x; }
2122 virtual int64_t Evaluate(int64_t x) const OVERRIDE { return ~x; }
2123
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002124 DECLARE_INSTRUCTION(Not);
2125
2126 private:
2127 DISALLOW_COPY_AND_ASSIGN(HNot);
2128};
2129
Roland Levillaindff1f282014-11-05 14:15:05 +00002130class HTypeConversion : public HExpression<1> {
2131 public:
2132 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00002133 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
2134 : HExpression(result_type, SideEffects::None()), dex_pc_(dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002135 SetRawInputAt(0, input);
2136 DCHECK_NE(input->GetType(), result_type);
2137 }
2138
2139 HInstruction* GetInput() const { return InputAt(0); }
2140 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
2141 Primitive::Type GetResultType() const { return GetType(); }
2142
Roland Levillain624279f2014-12-04 11:54:28 +00002143 // Required by the x86 and ARM code generators when producing calls
2144 // to the runtime.
2145 uint32_t GetDexPc() const { return dex_pc_; }
2146
Roland Levillaindff1f282014-11-05 14:15:05 +00002147 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00002148 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00002149
2150 DECLARE_INSTRUCTION(TypeConversion);
2151
2152 private:
Roland Levillain624279f2014-12-04 11:54:28 +00002153 const uint32_t dex_pc_;
2154
Roland Levillaindff1f282014-11-05 14:15:05 +00002155 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
2156};
2157
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002158class HPhi : public HInstruction {
2159 public:
2160 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002161 : HInstruction(SideEffects::None()),
2162 inputs_(arena, number_of_inputs),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002163 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002164 type_(type),
2165 is_live_(false) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002166 inputs_.SetSize(number_of_inputs);
2167 }
2168
2169 virtual size_t InputCount() const { return inputs_.Size(); }
2170 virtual HInstruction* InputAt(size_t i) const { return inputs_.Get(i); }
2171
2172 virtual void SetRawInputAt(size_t index, HInstruction* input) {
2173 inputs_.Put(index, input);
2174 }
2175
2176 void AddInput(HInstruction* input);
2177
2178 virtual Primitive::Type GetType() const { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002179 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002180
2181 uint32_t GetRegNumber() const { return reg_number_; }
2182
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002183 void SetDead() { is_live_ = false; }
2184 void SetLive() { is_live_ = true; }
2185 bool IsDead() const { return !is_live_; }
2186 bool IsLive() const { return is_live_; }
2187
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002188 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002189
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002190 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002191 GrowableArray<HInstruction*> inputs_;
2192 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002193 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002194 bool is_live_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002195
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002196 DISALLOW_COPY_AND_ASSIGN(HPhi);
2197};
2198
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002199class HNullCheck : public HExpression<1> {
2200 public:
2201 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002202 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002203 SetRawInputAt(0, value);
2204 }
2205
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002206 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002207 virtual bool InstructionDataEquals(HInstruction* other) const {
2208 UNUSED(other);
2209 return true;
2210 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002211
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002212 virtual bool NeedsEnvironment() const { return true; }
2213
Roland Levillaine161a2a2014-10-03 12:45:18 +01002214 virtual bool CanThrow() const { return true; }
2215
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002216 uint32_t GetDexPc() const { return dex_pc_; }
2217
2218 DECLARE_INSTRUCTION(NullCheck);
2219
2220 private:
2221 const uint32_t dex_pc_;
2222
2223 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
2224};
2225
2226class FieldInfo : public ValueObject {
2227 public:
Calin Juravle52c48962014-12-16 17:02:57 +00002228 FieldInfo(MemberOffset field_offset, Primitive::Type field_type, bool is_volatile)
2229 : field_offset_(field_offset), field_type_(field_type), is_volatile_(is_volatile) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002230
2231 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002232 Primitive::Type GetFieldType() const { return field_type_; }
Calin Juravle52c48962014-12-16 17:02:57 +00002233 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002234
2235 private:
2236 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01002237 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00002238 const bool is_volatile_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002239};
2240
2241class HInstanceFieldGet : public HExpression<1> {
2242 public:
2243 HInstanceFieldGet(HInstruction* value,
2244 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00002245 MemberOffset field_offset,
2246 bool is_volatile)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002247 : HExpression(field_type, SideEffects::DependsOnSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00002248 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002249 SetRawInputAt(0, value);
2250 }
2251
Calin Juravle10c9cbe2014-12-19 10:50:19 +00002252 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002253
2254 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2255 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
2256 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002257 }
2258
Calin Juravle77520bc2015-01-12 18:45:46 +00002259 bool CanDoImplicitNullCheck() const OVERRIDE {
2260 return GetFieldOffset().Uint32Value() < kPageSize;
2261 }
2262
2263 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002264 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
2265 }
2266
Calin Juravle52c48962014-12-16 17:02:57 +00002267 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002268 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002269 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002270 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002271
2272 DECLARE_INSTRUCTION(InstanceFieldGet);
2273
2274 private:
2275 const FieldInfo field_info_;
2276
2277 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
2278};
2279
2280class HInstanceFieldSet : public HTemplateInstruction<2> {
2281 public:
2282 HInstanceFieldSet(HInstruction* object,
2283 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01002284 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00002285 MemberOffset field_offset,
2286 bool is_volatile)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002287 : HTemplateInstruction(SideEffects::ChangesSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00002288 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002289 SetRawInputAt(0, object);
2290 SetRawInputAt(1, value);
2291 }
2292
Calin Juravle77520bc2015-01-12 18:45:46 +00002293 bool CanDoImplicitNullCheck() const OVERRIDE {
2294 return GetFieldOffset().Uint32Value() < kPageSize;
2295 }
2296
Calin Juravle52c48962014-12-16 17:02:57 +00002297 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002298 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002299 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002300 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002301 HInstruction* GetValue() const { return InputAt(1); }
2302
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002303 DECLARE_INSTRUCTION(InstanceFieldSet);
2304
2305 private:
2306 const FieldInfo field_info_;
2307
2308 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
2309};
2310
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002311class HArrayGet : public HExpression<2> {
2312 public:
2313 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002314 : HExpression(type, SideEffects::DependsOnSomething()) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002315 SetRawInputAt(0, array);
2316 SetRawInputAt(1, index);
2317 }
2318
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002319 bool CanBeMoved() const OVERRIDE { return true; }
2320 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002321 UNUSED(other);
2322 return true;
2323 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002324 bool CanDoImplicitNullCheck() const OVERRIDE {
2325 // TODO: We can be smarter here.
2326 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
2327 // which generates the implicit null check. There are cases when these can be removed
2328 // to produce better code. If we ever add optimizations to do so we should allow an
2329 // implicit check here (as long as the address falls in the first page).
2330 return false;
2331 }
2332
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002333 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002334
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002335 HInstruction* GetArray() const { return InputAt(0); }
2336 HInstruction* GetIndex() const { return InputAt(1); }
2337
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002338 DECLARE_INSTRUCTION(ArrayGet);
2339
2340 private:
2341 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
2342};
2343
2344class HArraySet : public HTemplateInstruction<3> {
2345 public:
2346 HArraySet(HInstruction* array,
2347 HInstruction* index,
2348 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002349 Primitive::Type expected_component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002350 uint32_t dex_pc)
2351 : HTemplateInstruction(SideEffects::ChangesSomething()),
2352 dex_pc_(dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002353 expected_component_type_(expected_component_type),
2354 needs_type_check_(value->GetType() == Primitive::kPrimNot) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002355 SetRawInputAt(0, array);
2356 SetRawInputAt(1, index);
2357 SetRawInputAt(2, value);
2358 }
2359
Calin Juravle77520bc2015-01-12 18:45:46 +00002360 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002361 // We currently always call a runtime method to catch array store
2362 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002363 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002364 }
2365
Calin Juravle77520bc2015-01-12 18:45:46 +00002366 bool CanDoImplicitNullCheck() const OVERRIDE {
2367 // TODO: Same as for ArrayGet.
2368 return false;
2369 }
2370
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002371 void ClearNeedsTypeCheck() {
2372 needs_type_check_ = false;
2373 }
2374
2375 bool NeedsTypeCheck() const { return needs_type_check_; }
2376
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002377 uint32_t GetDexPc() const { return dex_pc_; }
2378
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002379 HInstruction* GetArray() const { return InputAt(0); }
2380 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002381 HInstruction* GetValue() const { return InputAt(2); }
2382
2383 Primitive::Type GetComponentType() const {
2384 // The Dex format does not type floating point index operations. Since the
2385 // `expected_component_type_` is set during building and can therefore not
2386 // be correct, we also check what is the value type. If it is a floating
2387 // point type, we must use that type.
2388 Primitive::Type value_type = GetValue()->GetType();
2389 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
2390 ? value_type
2391 : expected_component_type_;
2392 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002393
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002394 DECLARE_INSTRUCTION(ArraySet);
2395
2396 private:
2397 const uint32_t dex_pc_;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002398 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002399 bool needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002400
2401 DISALLOW_COPY_AND_ASSIGN(HArraySet);
2402};
2403
2404class HArrayLength : public HExpression<1> {
2405 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002406 explicit HArrayLength(HInstruction* array)
2407 : HExpression(Primitive::kPrimInt, SideEffects::None()) {
2408 // Note that arrays do not change length, so the instruction does not
2409 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002410 SetRawInputAt(0, array);
2411 }
2412
Calin Juravle77520bc2015-01-12 18:45:46 +00002413 bool CanBeMoved() const OVERRIDE { return true; }
2414 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002415 UNUSED(other);
2416 return true;
2417 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002418 bool CanDoImplicitNullCheck() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002419
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002420 DECLARE_INSTRUCTION(ArrayLength);
2421
2422 private:
2423 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
2424};
2425
2426class HBoundsCheck : public HExpression<2> {
2427 public:
2428 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002429 : HExpression(index->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002430 DCHECK(index->GetType() == Primitive::kPrimInt);
2431 SetRawInputAt(0, index);
2432 SetRawInputAt(1, length);
2433 }
2434
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002435 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002436 virtual bool InstructionDataEquals(HInstruction* other) const {
2437 UNUSED(other);
2438 return true;
2439 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002440
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002441 virtual bool NeedsEnvironment() const { return true; }
2442
Roland Levillaine161a2a2014-10-03 12:45:18 +01002443 virtual bool CanThrow() const { return true; }
2444
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002445 uint32_t GetDexPc() const { return dex_pc_; }
2446
2447 DECLARE_INSTRUCTION(BoundsCheck);
2448
2449 private:
2450 const uint32_t dex_pc_;
2451
2452 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
2453};
2454
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002455/**
2456 * Some DEX instructions are folded into multiple HInstructions that need
2457 * to stay live until the last HInstruction. This class
2458 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00002459 * HInstruction stays live. `index` represents the stack location index of the
2460 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002461 */
2462class HTemporary : public HTemplateInstruction<0> {
2463 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002464 explicit HTemporary(size_t index) : HTemplateInstruction(SideEffects::None()), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002465
2466 size_t GetIndex() const { return index_; }
2467
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00002468 Primitive::Type GetType() const OVERRIDE {
2469 // The previous instruction is the one that will be stored in the temporary location.
2470 DCHECK(GetPrevious() != nullptr);
2471 return GetPrevious()->GetType();
2472 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00002473
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002474 DECLARE_INSTRUCTION(Temporary);
2475
2476 private:
2477 const size_t index_;
2478
2479 DISALLOW_COPY_AND_ASSIGN(HTemporary);
2480};
2481
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002482class HSuspendCheck : public HTemplateInstruction<0> {
2483 public:
2484 explicit HSuspendCheck(uint32_t dex_pc)
Nicolas Geoffray9ebc72c2014-09-25 16:33:42 +01002485 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002486
2487 virtual bool NeedsEnvironment() const {
2488 return true;
2489 }
2490
2491 uint32_t GetDexPc() const { return dex_pc_; }
2492
2493 DECLARE_INSTRUCTION(SuspendCheck);
2494
2495 private:
2496 const uint32_t dex_pc_;
2497
2498 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
2499};
2500
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002501/**
2502 * Instruction to load a Class object.
2503 */
2504class HLoadClass : public HExpression<0> {
2505 public:
2506 HLoadClass(uint16_t type_index,
2507 bool is_referrers_class,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002508 uint32_t dex_pc)
2509 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2510 type_index_(type_index),
2511 is_referrers_class_(is_referrers_class),
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002512 dex_pc_(dex_pc),
2513 generate_clinit_check_(false) {}
2514
2515 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002516
2517 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2518 return other->AsLoadClass()->type_index_ == type_index_;
2519 }
2520
2521 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
2522
2523 uint32_t GetDexPc() const { return dex_pc_; }
2524 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002525 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002526
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002527 bool NeedsEnvironment() const OVERRIDE {
2528 // Will call runtime and load the class if the class is not loaded yet.
2529 // TODO: finer grain decision.
2530 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002531 }
2532
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002533 bool MustGenerateClinitCheck() const {
2534 return generate_clinit_check_;
2535 }
2536
2537 void SetMustGenerateClinitCheck() {
2538 generate_clinit_check_ = true;
2539 }
2540
2541 bool CanCallRuntime() const {
2542 return MustGenerateClinitCheck() || !is_referrers_class_;
2543 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002544
2545 DECLARE_INSTRUCTION(LoadClass);
2546
2547 private:
2548 const uint16_t type_index_;
2549 const bool is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002550 const uint32_t dex_pc_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002551 // Whether this instruction must generate the initialization check.
2552 // Used for code generation.
2553 bool generate_clinit_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002554
2555 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
2556};
2557
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002558class HLoadString : public HExpression<0> {
2559 public:
2560 HLoadString(uint32_t string_index, uint32_t dex_pc)
2561 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2562 string_index_(string_index),
2563 dex_pc_(dex_pc) {}
2564
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002565 bool CanBeMoved() const OVERRIDE { return true; }
2566
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002567 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2568 return other->AsLoadString()->string_index_ == string_index_;
2569 }
2570
2571 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
2572
2573 uint32_t GetDexPc() const { return dex_pc_; }
2574 uint32_t GetStringIndex() const { return string_index_; }
2575
2576 // TODO: Can we deopt or debug when we resolve a string?
2577 bool NeedsEnvironment() const OVERRIDE { return false; }
2578
2579 DECLARE_INSTRUCTION(LoadString);
2580
2581 private:
2582 const uint32_t string_index_;
2583 const uint32_t dex_pc_;
2584
2585 DISALLOW_COPY_AND_ASSIGN(HLoadString);
2586};
2587
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002588// TODO: Pass this check to HInvokeStaticOrDirect nodes.
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002589/**
2590 * Performs an initialization check on its Class object input.
2591 */
2592class HClinitCheck : public HExpression<1> {
2593 public:
2594 explicit HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
2595 : HExpression(Primitive::kPrimNot, SideEffects::All()),
2596 dex_pc_(dex_pc) {
2597 SetRawInputAt(0, constant);
2598 }
2599
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002600 bool CanBeMoved() const OVERRIDE { return true; }
2601 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2602 UNUSED(other);
2603 return true;
2604 }
2605
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002606 bool NeedsEnvironment() const OVERRIDE {
2607 // May call runtime to initialize the class.
2608 return true;
2609 }
2610
2611 uint32_t GetDexPc() const { return dex_pc_; }
2612
2613 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
2614
2615 DECLARE_INSTRUCTION(ClinitCheck);
2616
2617 private:
2618 const uint32_t dex_pc_;
2619
2620 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
2621};
2622
2623class HStaticFieldGet : public HExpression<1> {
2624 public:
2625 HStaticFieldGet(HInstruction* cls,
2626 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00002627 MemberOffset field_offset,
2628 bool is_volatile)
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002629 : HExpression(field_type, SideEffects::DependsOnSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00002630 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002631 SetRawInputAt(0, cls);
2632 }
2633
Calin Juravle52c48962014-12-16 17:02:57 +00002634
Calin Juravle10c9cbe2014-12-19 10:50:19 +00002635 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002636
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002637 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00002638 HStaticFieldGet* other_get = other->AsStaticFieldGet();
2639 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002640 }
2641
2642 size_t ComputeHashCode() const OVERRIDE {
2643 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
2644 }
2645
Calin Juravle52c48962014-12-16 17:02:57 +00002646 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002647 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
2648 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002649 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002650
2651 DECLARE_INSTRUCTION(StaticFieldGet);
2652
2653 private:
2654 const FieldInfo field_info_;
2655
2656 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
2657};
2658
2659class HStaticFieldSet : public HTemplateInstruction<2> {
2660 public:
2661 HStaticFieldSet(HInstruction* cls,
2662 HInstruction* value,
2663 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00002664 MemberOffset field_offset,
2665 bool is_volatile)
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002666 : HTemplateInstruction(SideEffects::ChangesSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00002667 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002668 SetRawInputAt(0, cls);
2669 SetRawInputAt(1, value);
2670 }
2671
Calin Juravle52c48962014-12-16 17:02:57 +00002672 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002673 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
2674 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002675 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002676
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002677 HInstruction* GetValue() const { return InputAt(1); }
2678
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002679 DECLARE_INSTRUCTION(StaticFieldSet);
2680
2681 private:
2682 const FieldInfo field_info_;
2683
2684 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
2685};
2686
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002687// Implement the move-exception DEX instruction.
2688class HLoadException : public HExpression<0> {
2689 public:
2690 HLoadException() : HExpression(Primitive::kPrimNot, SideEffects::None()) {}
2691
2692 DECLARE_INSTRUCTION(LoadException);
2693
2694 private:
2695 DISALLOW_COPY_AND_ASSIGN(HLoadException);
2696};
2697
2698class HThrow : public HTemplateInstruction<1> {
2699 public:
2700 HThrow(HInstruction* exception, uint32_t dex_pc)
2701 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {
2702 SetRawInputAt(0, exception);
2703 }
2704
2705 bool IsControlFlow() const OVERRIDE { return true; }
2706
2707 bool NeedsEnvironment() const OVERRIDE { return true; }
2708
2709 uint32_t GetDexPc() const { return dex_pc_; }
2710
2711 DECLARE_INSTRUCTION(Throw);
2712
2713 private:
2714 uint32_t dex_pc_;
2715
2716 DISALLOW_COPY_AND_ASSIGN(HThrow);
2717};
2718
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002719class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002720 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002721 HInstanceOf(HInstruction* object,
2722 HLoadClass* constant,
2723 bool class_is_final,
2724 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002725 : HExpression(Primitive::kPrimBoolean, SideEffects::None()),
2726 class_is_final_(class_is_final),
2727 dex_pc_(dex_pc) {
2728 SetRawInputAt(0, object);
2729 SetRawInputAt(1, constant);
2730 }
2731
2732 bool CanBeMoved() const OVERRIDE { return true; }
2733
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002734 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002735 return true;
2736 }
2737
2738 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002739 return false;
2740 }
2741
2742 uint32_t GetDexPc() const { return dex_pc_; }
2743
2744 bool IsClassFinal() const { return class_is_final_; }
2745
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002746 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002747
2748 private:
2749 const bool class_is_final_;
2750 const uint32_t dex_pc_;
2751
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002752 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
2753};
2754
2755class HCheckCast : public HTemplateInstruction<2> {
2756 public:
2757 HCheckCast(HInstruction* object,
2758 HLoadClass* constant,
2759 bool class_is_final,
2760 uint32_t dex_pc)
2761 : HTemplateInstruction(SideEffects::None()),
2762 class_is_final_(class_is_final),
2763 dex_pc_(dex_pc) {
2764 SetRawInputAt(0, object);
2765 SetRawInputAt(1, constant);
2766 }
2767
2768 bool CanBeMoved() const OVERRIDE { return true; }
2769
2770 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2771 return true;
2772 }
2773
2774 bool NeedsEnvironment() const OVERRIDE {
2775 // Instruction may throw a CheckCastError.
2776 return true;
2777 }
2778
2779 bool CanThrow() const OVERRIDE { return true; }
2780
2781 uint32_t GetDexPc() const { return dex_pc_; }
2782
2783 bool IsClassFinal() const { return class_is_final_; }
2784
2785 DECLARE_INSTRUCTION(CheckCast);
2786
2787 private:
2788 const bool class_is_final_;
2789 const uint32_t dex_pc_;
2790
2791 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002792};
2793
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00002794class HMonitorOperation : public HTemplateInstruction<1> {
2795 public:
2796 enum OperationKind {
2797 kEnter,
2798 kExit,
2799 };
2800
2801 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
2802 : HTemplateInstruction(SideEffects::None()), kind_(kind), dex_pc_(dex_pc) {
2803 SetRawInputAt(0, object);
2804 }
2805
2806 // Instruction may throw a Java exception, so we need an environment.
2807 bool NeedsEnvironment() const OVERRIDE { return true; }
2808 bool CanThrow() const OVERRIDE { return true; }
2809
2810 uint32_t GetDexPc() const { return dex_pc_; }
2811
2812 bool IsEnter() const { return kind_ == kEnter; }
2813
2814 DECLARE_INSTRUCTION(MonitorOperation);
2815
Calin Juravle52c48962014-12-16 17:02:57 +00002816 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00002817 const OperationKind kind_;
2818 const uint32_t dex_pc_;
2819
2820 private:
2821 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
2822};
2823
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002824class MoveOperands : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002825 public:
Nicolas Geoffray740475d2014-09-29 10:33:25 +01002826 MoveOperands(Location source, Location destination, HInstruction* instruction)
2827 : source_(source), destination_(destination), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002828
2829 Location GetSource() const { return source_; }
2830 Location GetDestination() const { return destination_; }
2831
2832 void SetSource(Location value) { source_ = value; }
2833 void SetDestination(Location value) { destination_ = value; }
2834
2835 // The parallel move resolver marks moves as "in-progress" by clearing the
2836 // destination (but not the source).
2837 Location MarkPending() {
2838 DCHECK(!IsPending());
2839 Location dest = destination_;
2840 destination_ = Location::NoLocation();
2841 return dest;
2842 }
2843
2844 void ClearPending(Location dest) {
2845 DCHECK(IsPending());
2846 destination_ = dest;
2847 }
2848
2849 bool IsPending() const {
2850 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
2851 return destination_.IsInvalid() && !source_.IsInvalid();
2852 }
2853
2854 // True if this blocks a move from the given location.
2855 bool Blocks(Location loc) const {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00002856 return !IsEliminated() && source_.Equals(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002857 }
2858
2859 // A move is redundant if it's been eliminated, if its source and
2860 // destination are the same, or if its destination is unneeded.
2861 bool IsRedundant() const {
2862 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
2863 }
2864
2865 // We clear both operands to indicate move that's been eliminated.
2866 void Eliminate() {
2867 source_ = destination_ = Location::NoLocation();
2868 }
2869
2870 bool IsEliminated() const {
2871 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
2872 return source_.IsInvalid();
2873 }
2874
Nicolas Geoffray740475d2014-09-29 10:33:25 +01002875 HInstruction* GetInstruction() const { return instruction_; }
2876
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002877 private:
2878 Location source_;
2879 Location destination_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01002880 // The instruction this move is assocatied with. Null when this move is
2881 // for moving an input in the expected locations of user (including a phi user).
2882 // This is only used in debug mode, to ensure we do not connect interval siblings
2883 // in the same parallel move.
2884 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002885};
2886
2887static constexpr size_t kDefaultNumberOfMoves = 4;
2888
2889class HParallelMove : public HTemplateInstruction<0> {
2890 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002891 explicit HParallelMove(ArenaAllocator* arena)
2892 : HTemplateInstruction(SideEffects::None()), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002893
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00002894 void AddMove(Location source, Location destination, HInstruction* instruction) {
2895 DCHECK(source.IsValid());
2896 DCHECK(destination.IsValid());
2897 // The parallel move resolver does not handle pairs. So we decompose the
2898 // pair locations into two moves.
2899 if (source.IsPair() && destination.IsPair()) {
2900 AddMove(source.ToLow(), destination.ToLow(), instruction);
2901 AddMove(source.ToHigh(), destination.ToHigh(), nullptr);
2902 } else if (source.IsPair()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002903 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00002904 AddMove(source.ToLow(), Location::StackSlot(destination.GetStackIndex()), instruction);
2905 AddMove(source.ToHigh(), Location::StackSlot(destination.GetHighStackIndex(4)), nullptr);
2906 } else if (destination.IsPair()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002907 if (source.IsConstant()) {
2908 // We put the same constant in the move. The code generator will handle which
2909 // low or high part to use.
2910 AddMove(source, destination.ToLow(), instruction);
2911 AddMove(source, destination.ToHigh(), nullptr);
2912 } else {
2913 DCHECK(source.IsDoubleStackSlot());
2914 AddMove(Location::StackSlot(source.GetStackIndex()), destination.ToLow(), instruction);
2915 // TODO: rewrite GetHighStackIndex to not require a word size. It's supposed to
2916 // always be 4.
2917 static constexpr int kHighOffset = 4;
2918 AddMove(Location::StackSlot(source.GetHighStackIndex(kHighOffset)),
2919 destination.ToHigh(),
2920 nullptr);
2921 }
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00002922 } else {
2923 if (kIsDebugBuild) {
2924 if (instruction != nullptr) {
2925 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
2926 DCHECK_NE(moves_.Get(i).GetInstruction(), instruction)
2927 << "Doing parallel moves for the same instruction.";
2928 }
2929 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00002930 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00002931 DCHECK(!destination.Equals(moves_.Get(i).GetDestination()))
2932 << "Same destination for two moves in a parallel move.";
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00002933 }
2934 }
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00002935 moves_.Add(MoveOperands(source, destination, instruction));
Nicolas Geoffray740475d2014-09-29 10:33:25 +01002936 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002937 }
2938
2939 MoveOperands* MoveOperandsAt(size_t index) const {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00002940 return moves_.GetRawStorage() + index;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002941 }
2942
2943 size_t NumMoves() const { return moves_.Size(); }
2944
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002945 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002946
2947 private:
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00002948 GrowableArray<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002949
2950 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
2951};
2952
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002953class HGraphVisitor : public ValueObject {
2954 public:
Dave Allison20dfc792014-06-16 20:44:29 -07002955 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
2956 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002957
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002958 virtual void VisitInstruction(HInstruction* instruction) { UNUSED(instruction); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002959 virtual void VisitBasicBlock(HBasicBlock* block);
2960
Roland Levillain633021e2014-10-01 14:12:25 +01002961 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002962 void VisitInsertionOrder();
2963
Roland Levillain633021e2014-10-01 14:12:25 +01002964 // Visit the graph following dominator tree reverse post-order.
2965 void VisitReversePostOrder();
2966
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002967 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002968
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002969 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002970#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002971 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
2972
2973 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
2974
2975#undef DECLARE_VISIT_INSTRUCTION
2976
2977 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07002978 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002979
2980 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
2981};
2982
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002983class HGraphDelegateVisitor : public HGraphVisitor {
2984 public:
2985 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
2986 virtual ~HGraphDelegateVisitor() {}
2987
2988 // Visit functions that delegate to to super class.
2989#define DECLARE_VISIT_INSTRUCTION(name, super) \
2990 virtual void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
2991
2992 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
2993
2994#undef DECLARE_VISIT_INSTRUCTION
2995
2996 private:
2997 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
2998};
2999
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003000class HInsertionOrderIterator : public ValueObject {
3001 public:
3002 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
3003
3004 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
3005 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
3006 void Advance() { ++index_; }
3007
3008 private:
3009 const HGraph& graph_;
3010 size_t index_;
3011
3012 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
3013};
3014
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003015class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003016 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003017 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003018
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003019 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
3020 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003021 void Advance() { ++index_; }
3022
3023 private:
3024 const HGraph& graph_;
3025 size_t index_;
3026
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003027 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003028};
3029
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003030class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003031 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003032 explicit HPostOrderIterator(const HGraph& graph)
3033 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003034
3035 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003036 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003037 void Advance() { --index_; }
3038
3039 private:
3040 const HGraph& graph_;
3041 size_t index_;
3042
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003043 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003044};
3045
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003046} // namespace art
3047
3048#endif // ART_COMPILER_OPTIMIZING_NODES_H_