blob: dfb03c31cc3cd796450eb2108216a870c7573c6c [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 Juravle10e244f2015-01-26 18:54:32 +0000850 // Does not apply for all instructions, but having this at top level greatly
851 // simplifies the null check elimination.
852 virtual bool CanBeNull() const { return true; }
853
Calin Juravle77520bc2015-01-12 18:45:46 +0000854 virtual bool CanDoImplicitNullCheck() const { return false; }
855
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100856 void AddUseAt(HInstruction* user, size_t index) {
David Brazdiled596192015-01-23 10:39:45 +0000857 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000858 }
859
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100860 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +0100861 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +0000862 HUseListNode<HEnvironment*>* env_use =
863 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
864 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100865 }
866
867 void RemoveUser(HInstruction* user, size_t index);
David Brazdiled596192015-01-23 10:39:45 +0000868 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100869
David Brazdilea55b932015-01-27 17:12:29 +0000870 const HUseList<HInstruction*>& GetUses() { return uses_; }
871 const HUseList<HEnvironment*>& GetEnvUses() { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000872
David Brazdiled596192015-01-23 10:39:45 +0000873 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
874 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000875
Roland Levillain6c82d402014-10-13 16:10:27 +0100876 // Does this instruction strictly dominate `other_instruction`?
877 // Returns false if this instruction and `other_instruction` are the same.
878 // Aborts if this instruction and `other_instruction` are both phis.
879 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +0100880
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000881 int GetId() const { return id_; }
882 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000883
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100884 int GetSsaIndex() const { return ssa_index_; }
885 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
886 bool HasSsaIndex() const { return ssa_index_ != -1; }
887
888 bool HasEnvironment() const { return environment_ != nullptr; }
889 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100890 void SetEnvironment(HEnvironment* environment) { environment_ = environment; }
891
Nicolas Geoffray39468442014-09-02 15:17:15 +0100892 // Returns the number of entries in the environment. Typically, that is the
893 // number of dex registers in a method. It could be more in case of inlining.
894 size_t EnvironmentSize() const;
895
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000896 LocationSummary* GetLocations() const { return locations_; }
897 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000898
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100899 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100900 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100901
Nicolas Geoffray82091da2015-01-26 10:02:45 +0000902 // Move `this` instruction before `cursor`.
903 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000904
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100905#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +0100906 bool Is##type() const { return (As##type() != nullptr); } \
907 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000908 virtual H##type* As##type() { return nullptr; }
909
910 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
911#undef INSTRUCTION_TYPE_CHECK
912
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100913 // Returns whether the instruction can be moved within the graph.
914 virtual bool CanBeMoved() const { return false; }
915
916 // Returns whether the two instructions are of the same kind.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700917 virtual bool InstructionTypeEquals(HInstruction* other) const {
918 UNUSED(other);
919 return false;
920 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100921
922 // Returns whether any data encoded in the two instructions is equal.
923 // This method does not look at the inputs. Both instructions must be
924 // of the same type, otherwise the method has undefined behavior.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700925 virtual bool InstructionDataEquals(HInstruction* other) const {
926 UNUSED(other);
927 return false;
928 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100929
930 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +0000931 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100932 // 2) Their inputs are identical.
933 bool Equals(HInstruction* other) const;
934
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100935 virtual InstructionKind GetKind() const = 0;
936
937 virtual size_t ComputeHashCode() const {
938 size_t result = GetKind();
939 for (size_t i = 0, e = InputCount(); i < e; ++i) {
940 result = (result * 31) + InputAt(i)->GetId();
941 }
942 return result;
943 }
944
945 SideEffects GetSideEffects() const { return side_effects_; }
946
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100947 size_t GetLifetimePosition() const { return lifetime_position_; }
948 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
949 LiveInterval* GetLiveInterval() const { return live_interval_; }
950 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
951 bool HasLiveInterval() const { return live_interval_ != nullptr; }
952
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000953 private:
954 HInstruction* previous_;
955 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000956 HBasicBlock* block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000957
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000958 // An instruction gets an id when it is added to the graph.
959 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +0100960 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000961 int id_;
962
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100963 // When doing liveness analysis, instructions that have uses get an SSA index.
964 int ssa_index_;
965
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100966 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +0000967 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100968
969 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +0000970 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100971
Nicolas Geoffray39468442014-09-02 15:17:15 +0100972 // The environment associated with this instruction. Not null if the instruction
973 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100974 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000975
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000976 // Set by the code generator.
977 LocationSummary* locations_;
978
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100979 // Set by the liveness analysis.
980 LiveInterval* live_interval_;
981
982 // Set by the liveness analysis, this is the position in a linear
983 // order of blocks where this instruction's live interval start.
984 size_t lifetime_position_;
985
Nicolas Geoffray065bf772014-09-03 14:51:22 +0100986 const SideEffects side_effects_;
987
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000988 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000989 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100990 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000991
992 DISALLOW_COPY_AND_ASSIGN(HInstruction);
993};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700994std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000995
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000996class HInputIterator : public ValueObject {
997 public:
Dave Allison20dfc792014-06-16 20:44:29 -0700998 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000999
1000 bool Done() const { return index_ == instruction_->InputCount(); }
1001 HInstruction* Current() const { return instruction_->InputAt(index_); }
1002 void Advance() { index_++; }
1003
1004 private:
1005 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001006 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001007
1008 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
1009};
1010
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001011class HInstructionIterator : public ValueObject {
1012 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001013 explicit HInstructionIterator(const HInstructionList& instructions)
1014 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001015 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001016 }
1017
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001018 bool Done() const { return instruction_ == nullptr; }
1019 HInstruction* Current() const { return instruction_; }
1020 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001021 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001022 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001023 }
1024
1025 private:
1026 HInstruction* instruction_;
1027 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001028
1029 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001030};
1031
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001032class HBackwardInstructionIterator : public ValueObject {
1033 public:
1034 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
1035 : instruction_(instructions.last_instruction_) {
1036 next_ = Done() ? nullptr : instruction_->GetPrevious();
1037 }
1038
1039 bool Done() const { return instruction_ == nullptr; }
1040 HInstruction* Current() const { return instruction_; }
1041 void Advance() {
1042 instruction_ = next_;
1043 next_ = Done() ? nullptr : instruction_->GetPrevious();
1044 }
1045
1046 private:
1047 HInstruction* instruction_;
1048 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001049
1050 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001051};
1052
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001053// An embedded container with N elements of type T. Used (with partial
1054// specialization for N=0) because embedded arrays cannot have size 0.
1055template<typename T, intptr_t N>
1056class EmbeddedArray {
1057 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001058 EmbeddedArray() : elements_() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001059
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001060 intptr_t GetLength() const { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001061
1062 const T& operator[](intptr_t i) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001063 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001064 return elements_[i];
1065 }
1066
1067 T& operator[](intptr_t i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001068 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001069 return elements_[i];
1070 }
1071
1072 const T& At(intptr_t i) const {
1073 return (*this)[i];
1074 }
1075
1076 void SetAt(intptr_t i, const T& val) {
1077 (*this)[i] = val;
1078 }
1079
1080 private:
1081 T elements_[N];
1082};
1083
1084template<typename T>
1085class EmbeddedArray<T, 0> {
1086 public:
1087 intptr_t length() const { return 0; }
1088 const T& operator[](intptr_t i) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001089 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001090 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001091 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001092 }
1093 T& operator[](intptr_t i) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001094 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001095 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001096 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001097 }
1098};
1099
1100template<intptr_t N>
1101class HTemplateInstruction: public HInstruction {
1102 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001103 HTemplateInstruction<N>(SideEffects side_effects)
1104 : HInstruction(side_effects), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07001105 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001106
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001107 virtual size_t InputCount() const { return N; }
1108 virtual HInstruction* InputAt(size_t i) const { return inputs_[i]; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001109
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001110 protected:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001111 virtual void SetRawInputAt(size_t i, HInstruction* instruction) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001112 inputs_[i] = instruction;
1113 }
1114
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001115 private:
1116 EmbeddedArray<HInstruction*, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001117
1118 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001119};
1120
Dave Allison20dfc792014-06-16 20:44:29 -07001121template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001122class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07001123 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001124 HExpression<N>(Primitive::Type type, SideEffects side_effects)
1125 : HTemplateInstruction<N>(side_effects), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07001126 virtual ~HExpression() {}
1127
1128 virtual Primitive::Type GetType() const { return type_; }
1129
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001130 protected:
1131 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07001132};
1133
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001134// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
1135// instruction that branches to the exit block.
1136class HReturnVoid : public HTemplateInstruction<0> {
1137 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001138 HReturnVoid() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001139
1140 virtual bool IsControlFlow() const { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001141
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001142 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001143
1144 private:
1145 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
1146};
1147
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001148// Represents dex's RETURN opcodes. A HReturn is a control flow
1149// instruction that branches to the exit block.
1150class HReturn : public HTemplateInstruction<1> {
1151 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001152 explicit HReturn(HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001153 SetRawInputAt(0, value);
1154 }
1155
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001156 virtual bool IsControlFlow() const { return true; }
1157
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001158 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001159
1160 private:
1161 DISALLOW_COPY_AND_ASSIGN(HReturn);
1162};
1163
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001164// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001165// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001166// exit block.
1167class HExit : public HTemplateInstruction<0> {
1168 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001169 HExit() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001170
1171 virtual bool IsControlFlow() const { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001172
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001173 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001174
1175 private:
1176 DISALLOW_COPY_AND_ASSIGN(HExit);
1177};
1178
1179// Jumps from one block to another.
1180class HGoto : public HTemplateInstruction<0> {
1181 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001182 HGoto() : HTemplateInstruction(SideEffects::None()) {}
1183
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001184 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001185
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001186 HBasicBlock* GetSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001187 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001188 }
1189
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001190 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001191
1192 private:
1193 DISALLOW_COPY_AND_ASSIGN(HGoto);
1194};
1195
Dave Allison20dfc792014-06-16 20:44:29 -07001196
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001197// Conditional branch. A block ending with an HIf instruction must have
1198// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001199class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001200 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001201 explicit HIf(HInstruction* input) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001202 SetRawInputAt(0, input);
1203 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001204
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001205 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001206
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001207 HBasicBlock* IfTrueSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001208 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001209 }
1210
1211 HBasicBlock* IfFalseSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001212 return GetBlock()->GetSuccessors().Get(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001213 }
1214
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001215 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001216
Dave Allison20dfc792014-06-16 20:44:29 -07001217 virtual bool IsIfInstruction() const { return true; }
1218
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001219 private:
1220 DISALLOW_COPY_AND_ASSIGN(HIf);
1221};
1222
Roland Levillain88cb1752014-10-20 16:36:47 +01001223class HUnaryOperation : public HExpression<1> {
1224 public:
1225 HUnaryOperation(Primitive::Type result_type, HInstruction* input)
1226 : HExpression(result_type, SideEffects::None()) {
1227 SetRawInputAt(0, input);
1228 }
1229
1230 HInstruction* GetInput() const { return InputAt(0); }
1231 Primitive::Type GetResultType() const { return GetType(); }
1232
1233 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001234 virtual bool InstructionDataEquals(HInstruction* other) const {
1235 UNUSED(other);
1236 return true;
1237 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001238
Roland Levillain9240d6a2014-10-20 16:47:04 +01001239 // Try to statically evaluate `operation` and return a HConstant
1240 // containing the result of this evaluation. If `operation` cannot
1241 // be evaluated as a constant, return nullptr.
1242 HConstant* TryStaticEvaluation() const;
1243
1244 // Apply this operation to `x`.
1245 virtual int32_t Evaluate(int32_t x) const = 0;
1246 virtual int64_t Evaluate(int64_t x) const = 0;
1247
Roland Levillain88cb1752014-10-20 16:36:47 +01001248 DECLARE_INSTRUCTION(UnaryOperation);
1249
1250 private:
1251 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
1252};
1253
Dave Allison20dfc792014-06-16 20:44:29 -07001254class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001255 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001256 HBinaryOperation(Primitive::Type result_type,
1257 HInstruction* left,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001258 HInstruction* right) : HExpression(result_type, SideEffects::None()) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001259 SetRawInputAt(0, left);
1260 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001261 }
1262
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001263 HInstruction* GetLeft() const { return InputAt(0); }
1264 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07001265 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001266
1267 virtual bool IsCommutative() { return false; }
1268
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001269 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001270 virtual bool InstructionDataEquals(HInstruction* other) const {
1271 UNUSED(other);
1272 return true;
1273 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001274
Roland Levillain9240d6a2014-10-20 16:47:04 +01001275 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01001276 // containing the result of this evaluation. If `operation` cannot
1277 // be evaluated as a constant, return nullptr.
Roland Levillain9240d6a2014-10-20 16:47:04 +01001278 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01001279
1280 // Apply this operation to `x` and `y`.
1281 virtual int32_t Evaluate(int32_t x, int32_t y) const = 0;
1282 virtual int64_t Evaluate(int64_t x, int64_t y) const = 0;
1283
Roland Levillainccc07a92014-09-16 14:48:16 +01001284 DECLARE_INSTRUCTION(BinaryOperation);
1285
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001286 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001287 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
1288};
1289
Dave Allison20dfc792014-06-16 20:44:29 -07001290class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001291 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001292 HCondition(HInstruction* first, HInstruction* second)
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001293 : HBinaryOperation(Primitive::kPrimBoolean, first, second),
1294 needs_materialization_(true) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001295
1296 virtual bool IsCommutative() { return true; }
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001297
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001298 bool NeedsMaterialization() const { return needs_materialization_; }
1299 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001300
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001301 // For code generation purposes, returns whether this instruction is just before
1302 // `if_`, and disregard moves in between.
1303 bool IsBeforeWhenDisregardMoves(HIf* if_) const;
1304
Dave Allison20dfc792014-06-16 20:44:29 -07001305 DECLARE_INSTRUCTION(Condition);
1306
1307 virtual IfCondition GetCondition() const = 0;
1308
1309 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001310 // For register allocation purposes, returns whether this instruction needs to be
1311 // materialized (that is, not just be in the processor flags).
1312 bool needs_materialization_;
1313
Dave Allison20dfc792014-06-16 20:44:29 -07001314 DISALLOW_COPY_AND_ASSIGN(HCondition);
1315};
1316
1317// Instruction to check if two inputs are equal to each other.
1318class HEqual : public HCondition {
1319 public:
1320 HEqual(HInstruction* first, HInstruction* second)
1321 : HCondition(first, second) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001322
Roland Levillain93445682014-10-06 19:24:02 +01001323 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1324 return x == y ? 1 : 0;
1325 }
1326 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1327 return x == y ? 1 : 0;
1328 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001329
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001330 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001331
Dave Allison20dfc792014-06-16 20:44:29 -07001332 virtual IfCondition GetCondition() const {
1333 return kCondEQ;
1334 }
1335
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001336 private:
1337 DISALLOW_COPY_AND_ASSIGN(HEqual);
1338};
1339
Dave Allison20dfc792014-06-16 20:44:29 -07001340class HNotEqual : public HCondition {
1341 public:
1342 HNotEqual(HInstruction* first, HInstruction* second)
1343 : HCondition(first, second) {}
1344
Roland Levillain93445682014-10-06 19:24:02 +01001345 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1346 return x != y ? 1 : 0;
1347 }
1348 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1349 return x != y ? 1 : 0;
1350 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001351
Dave Allison20dfc792014-06-16 20:44:29 -07001352 DECLARE_INSTRUCTION(NotEqual);
1353
1354 virtual IfCondition GetCondition() const {
1355 return kCondNE;
1356 }
1357
1358 private:
1359 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
1360};
1361
1362class HLessThan : public HCondition {
1363 public:
1364 HLessThan(HInstruction* first, HInstruction* second)
1365 : HCondition(first, second) {}
1366
Roland Levillain93445682014-10-06 19:24:02 +01001367 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1368 return x < y ? 1 : 0;
1369 }
1370 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1371 return x < y ? 1 : 0;
1372 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001373
Dave Allison20dfc792014-06-16 20:44:29 -07001374 DECLARE_INSTRUCTION(LessThan);
1375
1376 virtual IfCondition GetCondition() const {
1377 return kCondLT;
1378 }
1379
1380 private:
1381 DISALLOW_COPY_AND_ASSIGN(HLessThan);
1382};
1383
1384class HLessThanOrEqual : public HCondition {
1385 public:
1386 HLessThanOrEqual(HInstruction* first, HInstruction* second)
1387 : HCondition(first, second) {}
1388
Roland Levillain93445682014-10-06 19:24:02 +01001389 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1390 return x <= y ? 1 : 0;
1391 }
1392 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1393 return x <= y ? 1 : 0;
1394 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001395
Dave Allison20dfc792014-06-16 20:44:29 -07001396 DECLARE_INSTRUCTION(LessThanOrEqual);
1397
1398 virtual IfCondition GetCondition() const {
1399 return kCondLE;
1400 }
1401
1402 private:
1403 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
1404};
1405
1406class HGreaterThan : public HCondition {
1407 public:
1408 HGreaterThan(HInstruction* first, HInstruction* second)
1409 : HCondition(first, second) {}
1410
Roland Levillain93445682014-10-06 19:24:02 +01001411 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1412 return x > y ? 1 : 0;
1413 }
1414 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1415 return x > y ? 1 : 0;
1416 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001417
Dave Allison20dfc792014-06-16 20:44:29 -07001418 DECLARE_INSTRUCTION(GreaterThan);
1419
1420 virtual IfCondition GetCondition() const {
1421 return kCondGT;
1422 }
1423
1424 private:
1425 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
1426};
1427
1428class HGreaterThanOrEqual : public HCondition {
1429 public:
1430 HGreaterThanOrEqual(HInstruction* first, HInstruction* second)
1431 : HCondition(first, second) {}
1432
Roland Levillain93445682014-10-06 19:24:02 +01001433 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1434 return x >= y ? 1 : 0;
1435 }
1436 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1437 return x >= y ? 1 : 0;
1438 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001439
Dave Allison20dfc792014-06-16 20:44:29 -07001440 DECLARE_INSTRUCTION(GreaterThanOrEqual);
1441
1442 virtual IfCondition GetCondition() const {
1443 return kCondGE;
1444 }
1445
1446 private:
1447 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
1448};
1449
1450
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001451// Instruction to check how two inputs compare to each other.
1452// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
1453class HCompare : public HBinaryOperation {
1454 public:
Calin Juravleddb7df22014-11-25 20:56:51 +00001455 // The bias applies for floating point operations and indicates how NaN
1456 // comparisons are treated:
1457 enum Bias {
1458 kNoBias, // bias is not applicable (i.e. for long operation)
1459 kGtBias, // return 1 for NaN comparisons
1460 kLtBias, // return -1 for NaN comparisons
1461 };
1462
1463 HCompare(Primitive::Type type, HInstruction* first, HInstruction* second, Bias bias)
1464 : HBinaryOperation(Primitive::kPrimInt, first, second), bias_(bias) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001465 DCHECK_EQ(type, first->GetType());
1466 DCHECK_EQ(type, second->GetType());
1467 }
1468
Calin Juravleddb7df22014-11-25 20:56:51 +00001469 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001470 return
1471 x == y ? 0 :
1472 x > y ? 1 :
1473 -1;
1474 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001475
1476 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01001477 return
1478 x == y ? 0 :
1479 x > y ? 1 :
1480 -1;
1481 }
1482
Calin Juravleddb7df22014-11-25 20:56:51 +00001483 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
1484 return bias_ == other->AsCompare()->bias_;
1485 }
1486
1487 bool IsGtBias() { return bias_ == kGtBias; }
1488
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001489 DECLARE_INSTRUCTION(Compare);
1490
1491 private:
Calin Juravleddb7df22014-11-25 20:56:51 +00001492 const Bias bias_;
1493
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001494 DISALLOW_COPY_AND_ASSIGN(HCompare);
1495};
1496
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001497// A local in the graph. Corresponds to a Dex register.
1498class HLocal : public HTemplateInstruction<0> {
1499 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001500 explicit HLocal(uint16_t reg_number)
1501 : HTemplateInstruction(SideEffects::None()), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001502
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001503 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001504
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001505 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001506
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001507 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001508 // The Dex register number.
1509 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001510
1511 DISALLOW_COPY_AND_ASSIGN(HLocal);
1512};
1513
1514// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07001515class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001516 public:
Roland Levillain5799fc02014-09-25 12:15:20 +01001517 HLoadLocal(HLocal* local, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001518 : HExpression(type, SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001519 SetRawInputAt(0, local);
1520 }
1521
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001522 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1523
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001524 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001525
1526 private:
1527 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
1528};
1529
1530// Store a value in a given local. This instruction has two inputs: the value
1531// and the local.
1532class HStoreLocal : public HTemplateInstruction<2> {
1533 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001534 HStoreLocal(HLocal* local, HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001535 SetRawInputAt(0, local);
1536 SetRawInputAt(1, value);
1537 }
1538
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001539 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
1540
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001541 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001542
1543 private:
1544 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
1545};
1546
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001547class HConstant : public HExpression<0> {
1548 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001549 explicit HConstant(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
1550
1551 virtual bool CanBeMoved() const { return true; }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001552
1553 DECLARE_INSTRUCTION(Constant);
1554
1555 private:
1556 DISALLOW_COPY_AND_ASSIGN(HConstant);
1557};
1558
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001559class HFloatConstant : public HConstant {
1560 public:
1561 explicit HFloatConstant(float value) : HConstant(Primitive::kPrimFloat), value_(value) {}
1562
1563 float GetValue() const { return value_; }
1564
1565 virtual bool InstructionDataEquals(HInstruction* other) const {
1566 return bit_cast<float, int32_t>(other->AsFloatConstant()->value_) ==
1567 bit_cast<float, int32_t>(value_);
1568 }
1569
1570 virtual size_t ComputeHashCode() const { return static_cast<size_t>(GetValue()); }
1571
1572 DECLARE_INSTRUCTION(FloatConstant);
1573
1574 private:
1575 const float value_;
1576
1577 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
1578};
1579
1580class HDoubleConstant : public HConstant {
1581 public:
1582 explicit HDoubleConstant(double value) : HConstant(Primitive::kPrimDouble), value_(value) {}
1583
1584 double GetValue() const { return value_; }
1585
1586 virtual bool InstructionDataEquals(HInstruction* other) const {
1587 return bit_cast<double, int64_t>(other->AsDoubleConstant()->value_) ==
1588 bit_cast<double, int64_t>(value_);
1589 }
1590
1591 virtual size_t ComputeHashCode() const { return static_cast<size_t>(GetValue()); }
1592
1593 DECLARE_INSTRUCTION(DoubleConstant);
1594
1595 private:
1596 const double value_;
1597
1598 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
1599};
1600
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001601// Constants of the type int. Those can be from Dex instructions, or
1602// synthesized (for example with the if-eqz instruction).
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001603class HIntConstant : public HConstant {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001604 public:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001605 explicit HIntConstant(int32_t value) : HConstant(Primitive::kPrimInt), value_(value) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001606
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001607 int32_t GetValue() const { return value_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001608
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001609 virtual bool InstructionDataEquals(HInstruction* other) const {
1610 return other->AsIntConstant()->value_ == value_;
1611 }
1612
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001613 virtual size_t ComputeHashCode() const { return GetValue(); }
1614
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001615 DECLARE_INSTRUCTION(IntConstant);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001616
1617 private:
1618 const int32_t value_;
1619
1620 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
1621};
1622
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001623class HLongConstant : public HConstant {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001624 public:
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001625 explicit HLongConstant(int64_t value) : HConstant(Primitive::kPrimLong), value_(value) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001626
1627 int64_t GetValue() const { return value_; }
1628
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001629 virtual bool InstructionDataEquals(HInstruction* other) const {
1630 return other->AsLongConstant()->value_ == value_;
1631 }
1632
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001633 virtual size_t ComputeHashCode() const { return static_cast<size_t>(GetValue()); }
1634
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001635 DECLARE_INSTRUCTION(LongConstant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001636
1637 private:
1638 const int64_t value_;
1639
1640 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
1641};
1642
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001643enum class Intrinsics {
1644#define OPTIMIZING_INTRINSICS(Name, IsStatic) k ## Name,
1645#include "intrinsics_list.h"
1646 kNone,
1647 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
1648#undef INTRINSICS_LIST
1649#undef OPTIMIZING_INTRINSICS
1650};
1651std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
1652
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001653class HInvoke : public HInstruction {
1654 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001655 virtual size_t InputCount() const { return inputs_.Size(); }
1656 virtual HInstruction* InputAt(size_t i) const { return inputs_.Get(i); }
1657
1658 // Runtime needs to walk the stack, so Dex -> Dex calls need to
1659 // know their environment.
Calin Juravle77520bc2015-01-12 18:45:46 +00001660 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001661
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001662 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001663 SetRawInputAt(index, argument);
1664 }
1665
1666 virtual void SetRawInputAt(size_t index, HInstruction* input) {
1667 inputs_.Put(index, input);
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001668 }
1669
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001670 virtual Primitive::Type GetType() const { return return_type_; }
1671
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001672 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001673
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001674 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
1675
1676 Intrinsics GetIntrinsic() {
1677 return intrinsic_;
1678 }
1679
1680 void SetIntrinsic(Intrinsics intrinsic) {
1681 intrinsic_ = intrinsic;
1682 }
1683
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001684 DECLARE_INSTRUCTION(Invoke);
1685
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001686 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001687 HInvoke(ArenaAllocator* arena,
1688 uint32_t number_of_arguments,
1689 Primitive::Type return_type,
1690 uint32_t dex_pc,
1691 uint32_t dex_method_index)
1692 : HInstruction(SideEffects::All()),
1693 inputs_(arena, number_of_arguments),
1694 return_type_(return_type),
1695 dex_pc_(dex_pc),
1696 dex_method_index_(dex_method_index),
1697 intrinsic_(Intrinsics::kNone) {
1698 inputs_.SetSize(number_of_arguments);
1699 }
1700
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001701 GrowableArray<HInstruction*> inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001702 const Primitive::Type return_type_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001703 const uint32_t dex_pc_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001704 const uint32_t dex_method_index_;
1705 Intrinsics intrinsic_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001706
1707 private:
1708 DISALLOW_COPY_AND_ASSIGN(HInvoke);
1709};
1710
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001711class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001712 public:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001713 HInvokeStaticOrDirect(ArenaAllocator* arena,
1714 uint32_t number_of_arguments,
1715 Primitive::Type return_type,
1716 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001717 uint32_t dex_method_index,
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001718 bool is_recursive,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001719 InvokeType invoke_type)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001720 : HInvoke(arena, number_of_arguments, return_type, dex_pc, dex_method_index),
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001721 invoke_type_(invoke_type),
1722 is_recursive_(is_recursive) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001723
Calin Juravle77520bc2015-01-12 18:45:46 +00001724 bool CanDoImplicitNullCheck() const OVERRIDE {
1725 // We access the method via the dex cache so we can't do an implicit null check.
1726 // TODO: for intrinsics we can generate implicit null checks.
1727 return false;
1728 }
1729
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001730 InvokeType GetInvokeType() const { return invoke_type_; }
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001731 bool IsRecursive() const { return is_recursive_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001732
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001733 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001734
1735 private:
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001736 const InvokeType invoke_type_;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001737 const bool is_recursive_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001738
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001739 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001740};
1741
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001742class HInvokeVirtual : public HInvoke {
1743 public:
1744 HInvokeVirtual(ArenaAllocator* arena,
1745 uint32_t number_of_arguments,
1746 Primitive::Type return_type,
1747 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001748 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001749 uint32_t vtable_index)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001750 : HInvoke(arena, number_of_arguments, return_type, dex_pc, dex_method_index),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001751 vtable_index_(vtable_index) {}
1752
Calin Juravle77520bc2015-01-12 18:45:46 +00001753 bool CanDoImplicitNullCheck() const OVERRIDE {
1754 // TODO: Add implicit null checks in intrinsics.
1755 return !GetLocations()->Intrinsified();
1756 }
1757
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001758 uint32_t GetVTableIndex() const { return vtable_index_; }
1759
1760 DECLARE_INSTRUCTION(InvokeVirtual);
1761
1762 private:
1763 const uint32_t vtable_index_;
1764
1765 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
1766};
1767
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001768class HInvokeInterface : public HInvoke {
1769 public:
1770 HInvokeInterface(ArenaAllocator* arena,
1771 uint32_t number_of_arguments,
1772 Primitive::Type return_type,
1773 uint32_t dex_pc,
1774 uint32_t dex_method_index,
1775 uint32_t imt_index)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001776 : HInvoke(arena, number_of_arguments, return_type, dex_pc, dex_method_index),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001777 imt_index_(imt_index) {}
1778
Calin Juravle77520bc2015-01-12 18:45:46 +00001779 bool CanDoImplicitNullCheck() const OVERRIDE {
1780 // TODO: Add implicit null checks in intrinsics.
1781 return !GetLocations()->Intrinsified();
1782 }
1783
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001784 uint32_t GetImtIndex() const { return imt_index_; }
1785 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
1786
1787 DECLARE_INSTRUCTION(InvokeInterface);
1788
1789 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001790 const uint32_t imt_index_;
1791
1792 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
1793};
1794
Dave Allison20dfc792014-06-16 20:44:29 -07001795class HNewInstance : public HExpression<0> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001796 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001797 HNewInstance(uint32_t dex_pc, uint16_t type_index)
1798 : HExpression(Primitive::kPrimNot, SideEffects::None()),
1799 dex_pc_(dex_pc),
1800 type_index_(type_index) {}
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001801
1802 uint32_t GetDexPc() const { return dex_pc_; }
1803 uint16_t GetTypeIndex() const { return type_index_; }
1804
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001805 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00001806 bool NeedsEnvironment() const OVERRIDE { return true; }
1807 // It may throw when called on:
1808 // - interfaces
1809 // - abstract/innaccessible/unknown classes
1810 // TODO: optimize when possible.
1811 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001812
Calin Juravle10e244f2015-01-26 18:54:32 +00001813 bool CanBeNull() const OVERRIDE { return false; }
1814
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001815 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01001816
1817 private:
1818 const uint32_t dex_pc_;
1819 const uint16_t type_index_;
1820
1821 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
1822};
1823
Roland Levillain88cb1752014-10-20 16:36:47 +01001824class HNeg : public HUnaryOperation {
1825 public:
1826 explicit HNeg(Primitive::Type result_type, HInstruction* input)
1827 : HUnaryOperation(result_type, input) {}
1828
Roland Levillain9240d6a2014-10-20 16:47:04 +01001829 virtual int32_t Evaluate(int32_t x) const OVERRIDE { return -x; }
1830 virtual int64_t Evaluate(int64_t x) const OVERRIDE { return -x; }
1831
Roland Levillain88cb1752014-10-20 16:36:47 +01001832 DECLARE_INSTRUCTION(Neg);
1833
1834 private:
1835 DISALLOW_COPY_AND_ASSIGN(HNeg);
1836};
1837
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001838class HNewArray : public HExpression<1> {
1839 public:
1840 HNewArray(HInstruction* length, uint32_t dex_pc, uint16_t type_index)
1841 : HExpression(Primitive::kPrimNot, SideEffects::None()),
1842 dex_pc_(dex_pc),
1843 type_index_(type_index) {
1844 SetRawInputAt(0, length);
1845 }
1846
1847 uint32_t GetDexPc() const { return dex_pc_; }
1848 uint16_t GetTypeIndex() const { return type_index_; }
1849
1850 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00001851 bool NeedsEnvironment() const OVERRIDE { return true; }
1852
1853 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01001854
1855 DECLARE_INSTRUCTION(NewArray);
1856
1857 private:
1858 const uint32_t dex_pc_;
1859 const uint16_t type_index_;
1860
1861 DISALLOW_COPY_AND_ASSIGN(HNewArray);
1862};
1863
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001864class HAdd : public HBinaryOperation {
1865 public:
1866 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1867 : HBinaryOperation(result_type, left, right) {}
1868
1869 virtual bool IsCommutative() { return true; }
1870
Roland Levillain93445682014-10-06 19:24:02 +01001871 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1872 return x + y;
1873 }
1874 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1875 return x + y;
1876 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001877
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001878 DECLARE_INSTRUCTION(Add);
1879
1880 private:
1881 DISALLOW_COPY_AND_ASSIGN(HAdd);
1882};
1883
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001884class HSub : public HBinaryOperation {
1885 public:
1886 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1887 : HBinaryOperation(result_type, left, right) {}
1888
Roland Levillain93445682014-10-06 19:24:02 +01001889 virtual int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
1890 return x - y;
1891 }
1892 virtual int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
1893 return x - y;
1894 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001895
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01001896 DECLARE_INSTRUCTION(Sub);
1897
1898 private:
1899 DISALLOW_COPY_AND_ASSIGN(HSub);
1900};
1901
Calin Juravle34bacdf2014-10-07 20:23:36 +01001902class HMul : public HBinaryOperation {
1903 public:
1904 HMul(Primitive::Type result_type, HInstruction* left, HInstruction* right)
1905 : HBinaryOperation(result_type, left, right) {}
1906
1907 virtual bool IsCommutative() { return true; }
1908
1909 virtual int32_t Evaluate(int32_t x, int32_t y) const { return x * y; }
1910 virtual int64_t Evaluate(int64_t x, int64_t y) const { return x * y; }
1911
1912 DECLARE_INSTRUCTION(Mul);
1913
1914 private:
1915 DISALLOW_COPY_AND_ASSIGN(HMul);
1916};
1917
Calin Juravle7c4954d2014-10-28 16:57:40 +00001918class HDiv : public HBinaryOperation {
1919 public:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001920 HDiv(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
1921 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00001922
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00001923 virtual int32_t Evaluate(int32_t x, int32_t y) const {
1924 // Our graph structure ensures we never have 0 for `y` during constant folding.
1925 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00001926 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00001927 return (y == -1) ? -x : x / y;
1928 }
Calin Juravlebacfec32014-11-14 15:54:36 +00001929
1930 virtual int64_t Evaluate(int64_t x, int64_t y) const {
1931 DCHECK_NE(y, 0);
1932 // Special case -1 to avoid getting a SIGFPE on x86(_64).
1933 return (y == -1) ? -x : x / y;
1934 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00001935
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001936 uint32_t GetDexPc() const { return dex_pc_; }
1937
Calin Juravle7c4954d2014-10-28 16:57:40 +00001938 DECLARE_INSTRUCTION(Div);
1939
1940 private:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001941 const uint32_t dex_pc_;
1942
Calin Juravle7c4954d2014-10-28 16:57:40 +00001943 DISALLOW_COPY_AND_ASSIGN(HDiv);
1944};
1945
Calin Juravlebacfec32014-11-14 15:54:36 +00001946class HRem : public HBinaryOperation {
1947 public:
1948 HRem(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
1949 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
1950
1951 virtual int32_t Evaluate(int32_t x, int32_t y) const {
1952 DCHECK_NE(y, 0);
1953 // Special case -1 to avoid getting a SIGFPE on x86(_64).
1954 return (y == -1) ? 0 : x % y;
1955 }
1956
1957 virtual int64_t Evaluate(int64_t x, int64_t y) const {
1958 DCHECK_NE(y, 0);
1959 // Special case -1 to avoid getting a SIGFPE on x86(_64).
1960 return (y == -1) ? 0 : x % y;
1961 }
1962
1963 uint32_t GetDexPc() const { return dex_pc_; }
1964
1965 DECLARE_INSTRUCTION(Rem);
1966
1967 private:
1968 const uint32_t dex_pc_;
1969
1970 DISALLOW_COPY_AND_ASSIGN(HRem);
1971};
1972
Calin Juravled0d48522014-11-04 16:40:20 +00001973class HDivZeroCheck : public HExpression<1> {
1974 public:
1975 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
1976 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
1977 SetRawInputAt(0, value);
1978 }
1979
1980 bool CanBeMoved() const OVERRIDE { return true; }
1981
1982 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
1983 UNUSED(other);
1984 return true;
1985 }
1986
1987 bool NeedsEnvironment() const OVERRIDE { return true; }
1988 bool CanThrow() const OVERRIDE { return true; }
1989
1990 uint32_t GetDexPc() const { return dex_pc_; }
1991
1992 DECLARE_INSTRUCTION(DivZeroCheck);
1993
1994 private:
1995 const uint32_t dex_pc_;
1996
1997 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
1998};
1999
Calin Juravle9aec02f2014-11-18 23:06:35 +00002000class HShl : public HBinaryOperation {
2001 public:
2002 HShl(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2003 : HBinaryOperation(result_type, left, right) {}
2004
2005 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x << (y & kMaxIntShiftValue); }
2006 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x << (y & kMaxLongShiftValue); }
2007
2008 DECLARE_INSTRUCTION(Shl);
2009
2010 private:
2011 DISALLOW_COPY_AND_ASSIGN(HShl);
2012};
2013
2014class HShr : public HBinaryOperation {
2015 public:
2016 HShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2017 : HBinaryOperation(result_type, left, right) {}
2018
2019 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x >> (y & kMaxIntShiftValue); }
2020 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x >> (y & kMaxLongShiftValue); }
2021
2022 DECLARE_INSTRUCTION(Shr);
2023
2024 private:
2025 DISALLOW_COPY_AND_ASSIGN(HShr);
2026};
2027
2028class HUShr : public HBinaryOperation {
2029 public:
2030 HUShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2031 : HBinaryOperation(result_type, left, right) {}
2032
2033 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
2034 uint32_t ux = static_cast<uint32_t>(x);
2035 uint32_t uy = static_cast<uint32_t>(y) & kMaxIntShiftValue;
2036 return static_cast<int32_t>(ux >> uy);
2037 }
2038
2039 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
2040 uint64_t ux = static_cast<uint64_t>(x);
2041 uint64_t uy = static_cast<uint64_t>(y) & kMaxLongShiftValue;
2042 return static_cast<int64_t>(ux >> uy);
2043 }
2044
2045 DECLARE_INSTRUCTION(UShr);
2046
2047 private:
2048 DISALLOW_COPY_AND_ASSIGN(HUShr);
2049};
2050
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002051class HAnd : public HBinaryOperation {
2052 public:
2053 HAnd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2054 : HBinaryOperation(result_type, left, right) {}
2055
2056 bool IsCommutative() OVERRIDE { return true; }
2057
2058 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x & y; }
2059 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x & y; }
2060
2061 DECLARE_INSTRUCTION(And);
2062
2063 private:
2064 DISALLOW_COPY_AND_ASSIGN(HAnd);
2065};
2066
2067class HOr : public HBinaryOperation {
2068 public:
2069 HOr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2070 : HBinaryOperation(result_type, left, right) {}
2071
2072 bool IsCommutative() OVERRIDE { return true; }
2073
2074 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x | y; }
2075 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x | y; }
2076
2077 DECLARE_INSTRUCTION(Or);
2078
2079 private:
2080 DISALLOW_COPY_AND_ASSIGN(HOr);
2081};
2082
2083class HXor : public HBinaryOperation {
2084 public:
2085 HXor(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2086 : HBinaryOperation(result_type, left, right) {}
2087
2088 bool IsCommutative() OVERRIDE { return true; }
2089
2090 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x ^ y; }
2091 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x ^ y; }
2092
2093 DECLARE_INSTRUCTION(Xor);
2094
2095 private:
2096 DISALLOW_COPY_AND_ASSIGN(HXor);
2097};
2098
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002099// The value of a parameter in this method. Its location depends on
2100// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07002101class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002102 public:
Calin Juravle10e244f2015-01-26 18:54:32 +00002103 HParameterValue(uint8_t index, Primitive::Type parameter_type, bool is_this = false)
2104 : HExpression(parameter_type, SideEffects::None()), index_(index), is_this_(is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002105
2106 uint8_t GetIndex() const { return index_; }
2107
Calin Juravle10e244f2015-01-26 18:54:32 +00002108 bool CanBeNull() const OVERRIDE { return !is_this_; }
2109
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002110 DECLARE_INSTRUCTION(ParameterValue);
2111
2112 private:
2113 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00002114 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002115 const uint8_t index_;
2116
Calin Juravle10e244f2015-01-26 18:54:32 +00002117 // Whether or not the parameter value corresponds to 'this' argument.
2118 const bool is_this_;
2119
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002120 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
2121};
2122
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002123class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002124 public:
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002125 explicit HNot(Primitive::Type result_type, HInstruction* input)
2126 : HUnaryOperation(result_type, input) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002127
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002128 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002129 virtual bool InstructionDataEquals(HInstruction* other) const {
2130 UNUSED(other);
2131 return true;
2132 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002133
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002134 virtual int32_t Evaluate(int32_t x) const OVERRIDE { return ~x; }
2135 virtual int64_t Evaluate(int64_t x) const OVERRIDE { return ~x; }
2136
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002137 DECLARE_INSTRUCTION(Not);
2138
2139 private:
2140 DISALLOW_COPY_AND_ASSIGN(HNot);
2141};
2142
Roland Levillaindff1f282014-11-05 14:15:05 +00002143class HTypeConversion : public HExpression<1> {
2144 public:
2145 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00002146 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
2147 : HExpression(result_type, SideEffects::None()), dex_pc_(dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002148 SetRawInputAt(0, input);
2149 DCHECK_NE(input->GetType(), result_type);
2150 }
2151
2152 HInstruction* GetInput() const { return InputAt(0); }
2153 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
2154 Primitive::Type GetResultType() const { return GetType(); }
2155
Roland Levillain624279f2014-12-04 11:54:28 +00002156 // Required by the x86 and ARM code generators when producing calls
2157 // to the runtime.
2158 uint32_t GetDexPc() const { return dex_pc_; }
2159
Roland Levillaindff1f282014-11-05 14:15:05 +00002160 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00002161 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00002162
2163 DECLARE_INSTRUCTION(TypeConversion);
2164
2165 private:
Roland Levillain624279f2014-12-04 11:54:28 +00002166 const uint32_t dex_pc_;
2167
Roland Levillaindff1f282014-11-05 14:15:05 +00002168 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
2169};
2170
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002171class HPhi : public HInstruction {
2172 public:
2173 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002174 : HInstruction(SideEffects::None()),
2175 inputs_(arena, number_of_inputs),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002176 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002177 type_(type),
Calin Juravle10e244f2015-01-26 18:54:32 +00002178 is_live_(false),
2179 can_be_null_(true) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002180 inputs_.SetSize(number_of_inputs);
2181 }
2182
Calin Juravle10e244f2015-01-26 18:54:32 +00002183 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
2184 HInstruction* InputAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002185
Calin Juravle10e244f2015-01-26 18:54:32 +00002186 void SetRawInputAt(size_t index, HInstruction* input) OVERRIDE {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002187 inputs_.Put(index, input);
2188 }
2189
2190 void AddInput(HInstruction* input);
2191
Calin Juravle10e244f2015-01-26 18:54:32 +00002192 Primitive::Type GetType() const OVERRIDE { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002193 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002194
Calin Juravle10e244f2015-01-26 18:54:32 +00002195 bool CanBeNull() const OVERRIDE { return can_be_null_; }
2196 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
2197
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002198 uint32_t GetRegNumber() const { return reg_number_; }
2199
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002200 void SetDead() { is_live_ = false; }
2201 void SetLive() { is_live_ = true; }
2202 bool IsDead() const { return !is_live_; }
2203 bool IsLive() const { return is_live_; }
2204
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002205 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002206
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002207 private:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002208 GrowableArray<HInstruction*> inputs_;
2209 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002210 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002211 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00002212 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002213
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002214 DISALLOW_COPY_AND_ASSIGN(HPhi);
2215};
2216
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002217class HNullCheck : public HExpression<1> {
2218 public:
2219 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002220 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002221 SetRawInputAt(0, value);
2222 }
2223
Calin Juravle10e244f2015-01-26 18:54:32 +00002224 bool CanBeMoved() const OVERRIDE { return true; }
2225 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002226 UNUSED(other);
2227 return true;
2228 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002229
Calin Juravle10e244f2015-01-26 18:54:32 +00002230 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002231
Calin Juravle10e244f2015-01-26 18:54:32 +00002232 bool CanThrow() const OVERRIDE { return true; }
2233
2234 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01002235
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002236 uint32_t GetDexPc() const { return dex_pc_; }
2237
2238 DECLARE_INSTRUCTION(NullCheck);
2239
2240 private:
2241 const uint32_t dex_pc_;
2242
2243 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
2244};
2245
2246class FieldInfo : public ValueObject {
2247 public:
Calin Juravle52c48962014-12-16 17:02:57 +00002248 FieldInfo(MemberOffset field_offset, Primitive::Type field_type, bool is_volatile)
2249 : field_offset_(field_offset), field_type_(field_type), is_volatile_(is_volatile) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002250
2251 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002252 Primitive::Type GetFieldType() const { return field_type_; }
Calin Juravle52c48962014-12-16 17:02:57 +00002253 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002254
2255 private:
2256 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01002257 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00002258 const bool is_volatile_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002259};
2260
2261class HInstanceFieldGet : public HExpression<1> {
2262 public:
2263 HInstanceFieldGet(HInstruction* value,
2264 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00002265 MemberOffset field_offset,
2266 bool is_volatile)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002267 : HExpression(field_type, SideEffects::DependsOnSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00002268 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002269 SetRawInputAt(0, value);
2270 }
2271
Calin Juravle10c9cbe2014-12-19 10:50:19 +00002272 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002273
2274 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2275 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
2276 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002277 }
2278
Calin Juravle77520bc2015-01-12 18:45:46 +00002279 bool CanDoImplicitNullCheck() const OVERRIDE {
2280 return GetFieldOffset().Uint32Value() < kPageSize;
2281 }
2282
2283 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002284 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
2285 }
2286
Calin Juravle52c48962014-12-16 17:02:57 +00002287 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002288 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002289 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002290 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002291
2292 DECLARE_INSTRUCTION(InstanceFieldGet);
2293
2294 private:
2295 const FieldInfo field_info_;
2296
2297 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
2298};
2299
2300class HInstanceFieldSet : public HTemplateInstruction<2> {
2301 public:
2302 HInstanceFieldSet(HInstruction* object,
2303 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01002304 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00002305 MemberOffset field_offset,
2306 bool is_volatile)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002307 : HTemplateInstruction(SideEffects::ChangesSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00002308 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002309 SetRawInputAt(0, object);
2310 SetRawInputAt(1, value);
2311 }
2312
Calin Juravle77520bc2015-01-12 18:45:46 +00002313 bool CanDoImplicitNullCheck() const OVERRIDE {
2314 return GetFieldOffset().Uint32Value() < kPageSize;
2315 }
2316
Calin Juravle52c48962014-12-16 17:02:57 +00002317 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002318 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002319 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002320 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002321 HInstruction* GetValue() const { return InputAt(1); }
2322
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002323 DECLARE_INSTRUCTION(InstanceFieldSet);
2324
2325 private:
2326 const FieldInfo field_info_;
2327
2328 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
2329};
2330
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002331class HArrayGet : public HExpression<2> {
2332 public:
2333 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002334 : HExpression(type, SideEffects::DependsOnSomething()) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002335 SetRawInputAt(0, array);
2336 SetRawInputAt(1, index);
2337 }
2338
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002339 bool CanBeMoved() const OVERRIDE { return true; }
2340 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002341 UNUSED(other);
2342 return true;
2343 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002344 bool CanDoImplicitNullCheck() const OVERRIDE {
2345 // TODO: We can be smarter here.
2346 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
2347 // which generates the implicit null check. There are cases when these can be removed
2348 // to produce better code. If we ever add optimizations to do so we should allow an
2349 // implicit check here (as long as the address falls in the first page).
2350 return false;
2351 }
2352
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002353 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002354
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002355 HInstruction* GetArray() const { return InputAt(0); }
2356 HInstruction* GetIndex() const { return InputAt(1); }
2357
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002358 DECLARE_INSTRUCTION(ArrayGet);
2359
2360 private:
2361 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
2362};
2363
2364class HArraySet : public HTemplateInstruction<3> {
2365 public:
2366 HArraySet(HInstruction* array,
2367 HInstruction* index,
2368 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002369 Primitive::Type expected_component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002370 uint32_t dex_pc)
2371 : HTemplateInstruction(SideEffects::ChangesSomething()),
2372 dex_pc_(dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002373 expected_component_type_(expected_component_type),
2374 needs_type_check_(value->GetType() == Primitive::kPrimNot) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002375 SetRawInputAt(0, array);
2376 SetRawInputAt(1, index);
2377 SetRawInputAt(2, value);
2378 }
2379
Calin Juravle77520bc2015-01-12 18:45:46 +00002380 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002381 // We currently always call a runtime method to catch array store
2382 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002383 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002384 }
2385
Calin Juravle77520bc2015-01-12 18:45:46 +00002386 bool CanDoImplicitNullCheck() const OVERRIDE {
2387 // TODO: Same as for ArrayGet.
2388 return false;
2389 }
2390
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002391 void ClearNeedsTypeCheck() {
2392 needs_type_check_ = false;
2393 }
2394
2395 bool NeedsTypeCheck() const { return needs_type_check_; }
2396
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002397 uint32_t GetDexPc() const { return dex_pc_; }
2398
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002399 HInstruction* GetArray() const { return InputAt(0); }
2400 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002401 HInstruction* GetValue() const { return InputAt(2); }
2402
2403 Primitive::Type GetComponentType() const {
2404 // The Dex format does not type floating point index operations. Since the
2405 // `expected_component_type_` is set during building and can therefore not
2406 // be correct, we also check what is the value type. If it is a floating
2407 // point type, we must use that type.
2408 Primitive::Type value_type = GetValue()->GetType();
2409 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
2410 ? value_type
2411 : expected_component_type_;
2412 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01002413
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002414 DECLARE_INSTRUCTION(ArraySet);
2415
2416 private:
2417 const uint32_t dex_pc_;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002418 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002419 bool needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002420
2421 DISALLOW_COPY_AND_ASSIGN(HArraySet);
2422};
2423
2424class HArrayLength : public HExpression<1> {
2425 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002426 explicit HArrayLength(HInstruction* array)
2427 : HExpression(Primitive::kPrimInt, SideEffects::None()) {
2428 // Note that arrays do not change length, so the instruction does not
2429 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002430 SetRawInputAt(0, array);
2431 }
2432
Calin Juravle77520bc2015-01-12 18:45:46 +00002433 bool CanBeMoved() const OVERRIDE { return true; }
2434 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002435 UNUSED(other);
2436 return true;
2437 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002438 bool CanDoImplicitNullCheck() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002439
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002440 DECLARE_INSTRUCTION(ArrayLength);
2441
2442 private:
2443 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
2444};
2445
2446class HBoundsCheck : public HExpression<2> {
2447 public:
2448 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002449 : HExpression(index->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002450 DCHECK(index->GetType() == Primitive::kPrimInt);
2451 SetRawInputAt(0, index);
2452 SetRawInputAt(1, length);
2453 }
2454
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002455 virtual bool CanBeMoved() const { return true; }
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002456 virtual bool InstructionDataEquals(HInstruction* other) const {
2457 UNUSED(other);
2458 return true;
2459 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002460
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002461 virtual bool NeedsEnvironment() const { return true; }
2462
Roland Levillaine161a2a2014-10-03 12:45:18 +01002463 virtual bool CanThrow() const { return true; }
2464
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01002465 uint32_t GetDexPc() const { return dex_pc_; }
2466
2467 DECLARE_INSTRUCTION(BoundsCheck);
2468
2469 private:
2470 const uint32_t dex_pc_;
2471
2472 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
2473};
2474
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002475/**
2476 * Some DEX instructions are folded into multiple HInstructions that need
2477 * to stay live until the last HInstruction. This class
2478 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00002479 * HInstruction stays live. `index` represents the stack location index of the
2480 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002481 */
2482class HTemporary : public HTemplateInstruction<0> {
2483 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002484 explicit HTemporary(size_t index) : HTemplateInstruction(SideEffects::None()), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002485
2486 size_t GetIndex() const { return index_; }
2487
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00002488 Primitive::Type GetType() const OVERRIDE {
2489 // The previous instruction is the one that will be stored in the temporary location.
2490 DCHECK(GetPrevious() != nullptr);
2491 return GetPrevious()->GetType();
2492 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00002493
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002494 DECLARE_INSTRUCTION(Temporary);
2495
2496 private:
2497 const size_t index_;
2498
2499 DISALLOW_COPY_AND_ASSIGN(HTemporary);
2500};
2501
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002502class HSuspendCheck : public HTemplateInstruction<0> {
2503 public:
2504 explicit HSuspendCheck(uint32_t dex_pc)
Nicolas Geoffray9ebc72c2014-09-25 16:33:42 +01002505 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00002506
2507 virtual bool NeedsEnvironment() const {
2508 return true;
2509 }
2510
2511 uint32_t GetDexPc() const { return dex_pc_; }
2512
2513 DECLARE_INSTRUCTION(SuspendCheck);
2514
2515 private:
2516 const uint32_t dex_pc_;
2517
2518 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
2519};
2520
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002521/**
2522 * Instruction to load a Class object.
2523 */
2524class HLoadClass : public HExpression<0> {
2525 public:
2526 HLoadClass(uint16_t type_index,
2527 bool is_referrers_class,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002528 uint32_t dex_pc)
2529 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2530 type_index_(type_index),
2531 is_referrers_class_(is_referrers_class),
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002532 dex_pc_(dex_pc),
2533 generate_clinit_check_(false) {}
2534
2535 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002536
2537 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2538 return other->AsLoadClass()->type_index_ == type_index_;
2539 }
2540
2541 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
2542
2543 uint32_t GetDexPc() const { return dex_pc_; }
2544 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002545 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002546
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002547 bool NeedsEnvironment() const OVERRIDE {
2548 // Will call runtime and load the class if the class is not loaded yet.
2549 // TODO: finer grain decision.
2550 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002551 }
2552
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002553 bool MustGenerateClinitCheck() const {
2554 return generate_clinit_check_;
2555 }
2556
2557 void SetMustGenerateClinitCheck() {
2558 generate_clinit_check_ = true;
2559 }
2560
2561 bool CanCallRuntime() const {
2562 return MustGenerateClinitCheck() || !is_referrers_class_;
2563 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002564
Nicolas Geoffray82091da2015-01-26 10:02:45 +00002565 bool CanThrow() const OVERRIDE {
2566 // May call runtime and and therefore can throw.
2567 // TODO: finer grain decision.
2568 return !is_referrers_class_;
2569 }
2570
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002571 DECLARE_INSTRUCTION(LoadClass);
2572
2573 private:
2574 const uint16_t type_index_;
2575 const bool is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002576 const uint32_t dex_pc_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002577 // Whether this instruction must generate the initialization check.
2578 // Used for code generation.
2579 bool generate_clinit_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002580
2581 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
2582};
2583
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002584class HLoadString : public HExpression<0> {
2585 public:
2586 HLoadString(uint32_t string_index, uint32_t dex_pc)
2587 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2588 string_index_(string_index),
2589 dex_pc_(dex_pc) {}
2590
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002591 bool CanBeMoved() const OVERRIDE { return true; }
2592
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00002593 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2594 return other->AsLoadString()->string_index_ == string_index_;
2595 }
2596
2597 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
2598
2599 uint32_t GetDexPc() const { return dex_pc_; }
2600 uint32_t GetStringIndex() const { return string_index_; }
2601
2602 // TODO: Can we deopt or debug when we resolve a string?
2603 bool NeedsEnvironment() const OVERRIDE { return false; }
2604
2605 DECLARE_INSTRUCTION(LoadString);
2606
2607 private:
2608 const uint32_t string_index_;
2609 const uint32_t dex_pc_;
2610
2611 DISALLOW_COPY_AND_ASSIGN(HLoadString);
2612};
2613
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002614// TODO: Pass this check to HInvokeStaticOrDirect nodes.
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002615/**
2616 * Performs an initialization check on its Class object input.
2617 */
2618class HClinitCheck : public HExpression<1> {
2619 public:
2620 explicit HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
2621 : HExpression(Primitive::kPrimNot, SideEffects::All()),
2622 dex_pc_(dex_pc) {
2623 SetRawInputAt(0, constant);
2624 }
2625
Nicolas Geoffray424f6762014-11-03 14:51:25 +00002626 bool CanBeMoved() const OVERRIDE { return true; }
2627 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2628 UNUSED(other);
2629 return true;
2630 }
2631
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002632 bool NeedsEnvironment() const OVERRIDE {
2633 // May call runtime to initialize the class.
2634 return true;
2635 }
2636
2637 uint32_t GetDexPc() const { return dex_pc_; }
2638
2639 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
2640
2641 DECLARE_INSTRUCTION(ClinitCheck);
2642
2643 private:
2644 const uint32_t dex_pc_;
2645
2646 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
2647};
2648
2649class HStaticFieldGet : public HExpression<1> {
2650 public:
2651 HStaticFieldGet(HInstruction* cls,
2652 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00002653 MemberOffset field_offset,
2654 bool is_volatile)
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002655 : HExpression(field_type, SideEffects::DependsOnSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00002656 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002657 SetRawInputAt(0, cls);
2658 }
2659
Calin Juravle52c48962014-12-16 17:02:57 +00002660
Calin Juravle10c9cbe2014-12-19 10:50:19 +00002661 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002662
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002663 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00002664 HStaticFieldGet* other_get = other->AsStaticFieldGet();
2665 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002666 }
2667
2668 size_t ComputeHashCode() const OVERRIDE {
2669 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
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
2677 DECLARE_INSTRUCTION(StaticFieldGet);
2678
2679 private:
2680 const FieldInfo field_info_;
2681
2682 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
2683};
2684
2685class HStaticFieldSet : public HTemplateInstruction<2> {
2686 public:
2687 HStaticFieldSet(HInstruction* cls,
2688 HInstruction* value,
2689 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00002690 MemberOffset field_offset,
2691 bool is_volatile)
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002692 : HTemplateInstruction(SideEffects::ChangesSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00002693 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002694 SetRawInputAt(0, cls);
2695 SetRawInputAt(1, value);
2696 }
2697
Calin Juravle52c48962014-12-16 17:02:57 +00002698 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002699 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
2700 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00002701 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002702
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00002703 HInstruction* GetValue() const { return InputAt(1); }
2704
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002705 DECLARE_INSTRUCTION(StaticFieldSet);
2706
2707 private:
2708 const FieldInfo field_info_;
2709
2710 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
2711};
2712
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002713// Implement the move-exception DEX instruction.
2714class HLoadException : public HExpression<0> {
2715 public:
2716 HLoadException() : HExpression(Primitive::kPrimNot, SideEffects::None()) {}
2717
2718 DECLARE_INSTRUCTION(LoadException);
2719
2720 private:
2721 DISALLOW_COPY_AND_ASSIGN(HLoadException);
2722};
2723
2724class HThrow : public HTemplateInstruction<1> {
2725 public:
2726 HThrow(HInstruction* exception, uint32_t dex_pc)
2727 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {
2728 SetRawInputAt(0, exception);
2729 }
2730
2731 bool IsControlFlow() const OVERRIDE { return true; }
2732
2733 bool NeedsEnvironment() const OVERRIDE { return true; }
2734
Nicolas Geoffray82091da2015-01-26 10:02:45 +00002735 bool CanThrow() const OVERRIDE { return true; }
2736
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002737 uint32_t GetDexPc() const { return dex_pc_; }
2738
2739 DECLARE_INSTRUCTION(Throw);
2740
2741 private:
Nicolas Geoffray82091da2015-01-26 10:02:45 +00002742 const uint32_t dex_pc_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002743
2744 DISALLOW_COPY_AND_ASSIGN(HThrow);
2745};
2746
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002747class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002748 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002749 HInstanceOf(HInstruction* object,
2750 HLoadClass* constant,
2751 bool class_is_final,
2752 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002753 : HExpression(Primitive::kPrimBoolean, SideEffects::None()),
2754 class_is_final_(class_is_final),
2755 dex_pc_(dex_pc) {
2756 SetRawInputAt(0, object);
2757 SetRawInputAt(1, constant);
2758 }
2759
2760 bool CanBeMoved() const OVERRIDE { return true; }
2761
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002762 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002763 return true;
2764 }
2765
2766 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002767 return false;
2768 }
2769
2770 uint32_t GetDexPc() const { return dex_pc_; }
2771
2772 bool IsClassFinal() const { return class_is_final_; }
2773
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002774 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002775
2776 private:
2777 const bool class_is_final_;
2778 const uint32_t dex_pc_;
2779
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00002780 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
2781};
2782
2783class HCheckCast : public HTemplateInstruction<2> {
2784 public:
2785 HCheckCast(HInstruction* object,
2786 HLoadClass* constant,
2787 bool class_is_final,
2788 uint32_t dex_pc)
2789 : HTemplateInstruction(SideEffects::None()),
2790 class_is_final_(class_is_final),
2791 dex_pc_(dex_pc) {
2792 SetRawInputAt(0, object);
2793 SetRawInputAt(1, constant);
2794 }
2795
2796 bool CanBeMoved() const OVERRIDE { return true; }
2797
2798 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2799 return true;
2800 }
2801
2802 bool NeedsEnvironment() const OVERRIDE {
2803 // Instruction may throw a CheckCastError.
2804 return true;
2805 }
2806
2807 bool CanThrow() const OVERRIDE { return true; }
2808
2809 uint32_t GetDexPc() const { return dex_pc_; }
2810
2811 bool IsClassFinal() const { return class_is_final_; }
2812
2813 DECLARE_INSTRUCTION(CheckCast);
2814
2815 private:
2816 const bool class_is_final_;
2817 const uint32_t dex_pc_;
2818
2819 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00002820};
2821
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00002822class HMonitorOperation : public HTemplateInstruction<1> {
2823 public:
2824 enum OperationKind {
2825 kEnter,
2826 kExit,
2827 };
2828
2829 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
2830 : HTemplateInstruction(SideEffects::None()), kind_(kind), dex_pc_(dex_pc) {
2831 SetRawInputAt(0, object);
2832 }
2833
2834 // Instruction may throw a Java exception, so we need an environment.
2835 bool NeedsEnvironment() const OVERRIDE { return true; }
2836 bool CanThrow() const OVERRIDE { return true; }
2837
2838 uint32_t GetDexPc() const { return dex_pc_; }
2839
2840 bool IsEnter() const { return kind_ == kEnter; }
2841
2842 DECLARE_INSTRUCTION(MonitorOperation);
2843
Calin Juravle52c48962014-12-16 17:02:57 +00002844 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00002845 const OperationKind kind_;
2846 const uint32_t dex_pc_;
2847
2848 private:
2849 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
2850};
2851
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002852class MoveOperands : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002853 public:
Nicolas Geoffray740475d2014-09-29 10:33:25 +01002854 MoveOperands(Location source, Location destination, HInstruction* instruction)
2855 : source_(source), destination_(destination), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002856
2857 Location GetSource() const { return source_; }
2858 Location GetDestination() const { return destination_; }
2859
2860 void SetSource(Location value) { source_ = value; }
2861 void SetDestination(Location value) { destination_ = value; }
2862
2863 // The parallel move resolver marks moves as "in-progress" by clearing the
2864 // destination (but not the source).
2865 Location MarkPending() {
2866 DCHECK(!IsPending());
2867 Location dest = destination_;
2868 destination_ = Location::NoLocation();
2869 return dest;
2870 }
2871
2872 void ClearPending(Location dest) {
2873 DCHECK(IsPending());
2874 destination_ = dest;
2875 }
2876
2877 bool IsPending() const {
2878 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
2879 return destination_.IsInvalid() && !source_.IsInvalid();
2880 }
2881
2882 // True if this blocks a move from the given location.
2883 bool Blocks(Location loc) const {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00002884 return !IsEliminated() && source_.Equals(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002885 }
2886
2887 // A move is redundant if it's been eliminated, if its source and
2888 // destination are the same, or if its destination is unneeded.
2889 bool IsRedundant() const {
2890 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
2891 }
2892
2893 // We clear both operands to indicate move that's been eliminated.
2894 void Eliminate() {
2895 source_ = destination_ = Location::NoLocation();
2896 }
2897
2898 bool IsEliminated() const {
2899 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
2900 return source_.IsInvalid();
2901 }
2902
Nicolas Geoffray740475d2014-09-29 10:33:25 +01002903 HInstruction* GetInstruction() const { return instruction_; }
2904
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002905 private:
2906 Location source_;
2907 Location destination_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01002908 // The instruction this move is assocatied with. Null when this move is
2909 // for moving an input in the expected locations of user (including a phi user).
2910 // This is only used in debug mode, to ensure we do not connect interval siblings
2911 // in the same parallel move.
2912 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002913};
2914
2915static constexpr size_t kDefaultNumberOfMoves = 4;
2916
2917class HParallelMove : public HTemplateInstruction<0> {
2918 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002919 explicit HParallelMove(ArenaAllocator* arena)
2920 : HTemplateInstruction(SideEffects::None()), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002921
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00002922 void AddMove(Location source, Location destination, HInstruction* instruction) {
2923 DCHECK(source.IsValid());
2924 DCHECK(destination.IsValid());
2925 // The parallel move resolver does not handle pairs. So we decompose the
2926 // pair locations into two moves.
2927 if (source.IsPair() && destination.IsPair()) {
2928 AddMove(source.ToLow(), destination.ToLow(), instruction);
2929 AddMove(source.ToHigh(), destination.ToHigh(), nullptr);
2930 } else if (source.IsPair()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002931 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00002932 AddMove(source.ToLow(), Location::StackSlot(destination.GetStackIndex()), instruction);
2933 AddMove(source.ToHigh(), Location::StackSlot(destination.GetHighStackIndex(4)), nullptr);
2934 } else if (destination.IsPair()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002935 if (source.IsConstant()) {
2936 // We put the same constant in the move. The code generator will handle which
2937 // low or high part to use.
2938 AddMove(source, destination.ToLow(), instruction);
2939 AddMove(source, destination.ToHigh(), nullptr);
2940 } else {
2941 DCHECK(source.IsDoubleStackSlot());
2942 AddMove(Location::StackSlot(source.GetStackIndex()), destination.ToLow(), instruction);
2943 // TODO: rewrite GetHighStackIndex to not require a word size. It's supposed to
2944 // always be 4.
2945 static constexpr int kHighOffset = 4;
2946 AddMove(Location::StackSlot(source.GetHighStackIndex(kHighOffset)),
2947 destination.ToHigh(),
2948 nullptr);
2949 }
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00002950 } else {
2951 if (kIsDebugBuild) {
2952 if (instruction != nullptr) {
2953 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
2954 DCHECK_NE(moves_.Get(i).GetInstruction(), instruction)
2955 << "Doing parallel moves for the same instruction.";
2956 }
2957 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00002958 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00002959 DCHECK(!destination.Equals(moves_.Get(i).GetDestination()))
2960 << "Same destination for two moves in a parallel move.";
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00002961 }
2962 }
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00002963 moves_.Add(MoveOperands(source, destination, instruction));
Nicolas Geoffray740475d2014-09-29 10:33:25 +01002964 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002965 }
2966
2967 MoveOperands* MoveOperandsAt(size_t index) const {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00002968 return moves_.GetRawStorage() + index;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002969 }
2970
2971 size_t NumMoves() const { return moves_.Size(); }
2972
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002973 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002974
2975 private:
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00002976 GrowableArray<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01002977
2978 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
2979};
2980
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002981class HGraphVisitor : public ValueObject {
2982 public:
Dave Allison20dfc792014-06-16 20:44:29 -07002983 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
2984 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002985
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002986 virtual void VisitInstruction(HInstruction* instruction) { UNUSED(instruction); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002987 virtual void VisitBasicBlock(HBasicBlock* block);
2988
Roland Levillain633021e2014-10-01 14:12:25 +01002989 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002990 void VisitInsertionOrder();
2991
Roland Levillain633021e2014-10-01 14:12:25 +01002992 // Visit the graph following dominator tree reverse post-order.
2993 void VisitReversePostOrder();
2994
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002995 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002996
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002997 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002998#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002999 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
3000
3001 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
3002
3003#undef DECLARE_VISIT_INSTRUCTION
3004
3005 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07003006 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003007
3008 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
3009};
3010
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003011class HGraphDelegateVisitor : public HGraphVisitor {
3012 public:
3013 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
3014 virtual ~HGraphDelegateVisitor() {}
3015
3016 // Visit functions that delegate to to super class.
3017#define DECLARE_VISIT_INSTRUCTION(name, super) \
3018 virtual void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
3019
3020 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
3021
3022#undef DECLARE_VISIT_INSTRUCTION
3023
3024 private:
3025 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
3026};
3027
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003028class HInsertionOrderIterator : public ValueObject {
3029 public:
3030 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
3031
3032 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
3033 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
3034 void Advance() { ++index_; }
3035
3036 private:
3037 const HGraph& graph_;
3038 size_t index_;
3039
3040 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
3041};
3042
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003043class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003044 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003045 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003046
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003047 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
3048 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003049 void Advance() { ++index_; }
3050
3051 private:
3052 const HGraph& graph_;
3053 size_t index_;
3054
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003055 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003056};
3057
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003058class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003059 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003060 explicit HPostOrderIterator(const HGraph& graph)
3061 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {}
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003062
3063 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003064 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003065 void Advance() { --index_; }
3066
3067 private:
3068 const HGraph& graph_;
3069 size_t index_;
3070
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003071 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003072};
3073
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003074// Iterator over the blocks that art part of the loop. Includes blocks part
3075// of an inner loop. The order in which the blocks are iterated is on their
3076// block id.
3077class HBlocksInLoopIterator : public ValueObject {
3078 public:
3079 explicit HBlocksInLoopIterator(const HLoopInformation& info)
3080 : blocks_in_loop_(info.GetBlocks()),
3081 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
3082 index_(0) {
3083 if (!blocks_in_loop_.IsBitSet(index_)) {
3084 Advance();
3085 }
3086 }
3087
3088 bool Done() const { return index_ == blocks_.Size(); }
3089 HBasicBlock* Current() const { return blocks_.Get(index_); }
3090 void Advance() {
3091 ++index_;
3092 for (size_t e = blocks_.Size(); index_ < e; ++index_) {
3093 if (blocks_in_loop_.IsBitSet(index_)) {
3094 break;
3095 }
3096 }
3097 }
3098
3099 private:
3100 const BitVector& blocks_in_loop_;
3101 const GrowableArray<HBasicBlock*>& blocks_;
3102 size_t index_;
3103
3104 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
3105};
3106
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003107} // namespace art
3108
3109#endif // ART_COMPILER_OPTIMIZING_NODES_H_