blob: a1d2256817e839137d29b81e34b46b4bdb5e83fe [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
David Brazdil8d5b8b22015-03-24 10:51:52 +000020#include "base/arena_containers.h"
Mathieu Chartierb666f482015-02-18 14:33:14 -080021#include "base/arena_object.h"
Calin Juravle27df7582015-04-17 19:12:31 +010022#include "dex/compiler_enums.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000023#include "entrypoints/quick/quick_entrypoints_enum.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000024#include "handle.h"
25#include "handle_scope.h"
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000026#include "invoke_type.h"
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010027#include "locations.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000028#include "mirror/class.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010029#include "offsets.h"
Ian Rogerse63db272014-07-15 15:36:11 -070030#include "primitive.h"
Nicolas Geoffray0e336432014-02-26 18:24:38 +000031#include "utils/arena_bit_vector.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000032#include "utils/growable_array.h"
33
34namespace art {
35
David Brazdil1abb4192015-02-17 18:33:36 +000036class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000037class HBasicBlock;
David Brazdil8d5b8b22015-03-24 10:51:52 +000038class HDoubleConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010039class HEnvironment;
David Brazdil8d5b8b22015-03-24 10:51:52 +000040class HFloatConstant;
41class HGraphVisitor;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000042class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000043class HIntConstant;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000044class HInvoke;
David Brazdil8d5b8b22015-03-24 10:51:52 +000045class HLongConstant;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +000046class HNullConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010047class HPhi;
Nicolas Geoffray3c049742014-09-24 18:10:46 +010048class HSuspendCheck;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010049class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000050class LocationSummary;
Nicolas Geoffraydb216f42015-05-05 17:02:20 +010051class SlowPathCode;
David Brazdil8d5b8b22015-03-24 10:51:52 +000052class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000053
54static const int kDefaultNumberOfBlocks = 8;
55static const int kDefaultNumberOfSuccessors = 2;
56static const int kDefaultNumberOfPredecessors = 2;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010057static const int kDefaultNumberOfDominatedBlocks = 1;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000058static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000059
Calin Juravle9aec02f2014-11-18 23:06:35 +000060static constexpr uint32_t kMaxIntShiftValue = 0x1f;
61static constexpr uint64_t kMaxLongShiftValue = 0x3f;
62
Dave Allison20dfc792014-06-16 20:44:29 -070063enum IfCondition {
64 kCondEQ,
65 kCondNE,
66 kCondLT,
67 kCondLE,
68 kCondGT,
69 kCondGE,
70};
71
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010072class HInstructionList {
73 public:
74 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
75
76 void AddInstruction(HInstruction* instruction);
77 void RemoveInstruction(HInstruction* instruction);
78
David Brazdilc3d743f2015-04-22 13:40:50 +010079 // Insert `instruction` before/after an existing instruction `cursor`.
80 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
81 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
82
Roland Levillain6b469232014-09-25 10:10:38 +010083 // Return true if this list contains `instruction`.
84 bool Contains(HInstruction* instruction) const;
85
Roland Levillainccc07a92014-09-16 14:48:16 +010086 // Return true if `instruction1` is found before `instruction2` in
87 // this instruction list and false otherwise. Abort if none
88 // of these instructions is found.
89 bool FoundBefore(const HInstruction* instruction1,
90 const HInstruction* instruction2) const;
91
Nicolas Geoffray276d9da2015-02-02 18:24:11 +000092 bool IsEmpty() const { return first_instruction_ == nullptr; }
93 void Clear() { first_instruction_ = last_instruction_ = nullptr; }
94
95 // Update the block of all instructions to be `block`.
96 void SetBlockOfInstructions(HBasicBlock* block) const;
97
98 void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list);
99 void Add(const HInstructionList& instruction_list);
100
David Brazdil2d7352b2015-04-20 14:52:42 +0100101 // Return the number of instructions in the list. This is an expensive operation.
102 size_t CountSize() const;
103
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100104 private:
105 HInstruction* first_instruction_;
106 HInstruction* last_instruction_;
107
108 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000109 friend class HGraph;
110 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100111 friend class HInstructionIterator;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100112 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100113
114 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
115};
116
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000117// Control-flow graph of a method. Contains a list of basic blocks.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700118class HGraph : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000119 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100120 HGraph(ArenaAllocator* arena,
121 const DexFile& dex_file,
122 uint32_t method_idx,
123 bool debuggable = false,
124 int start_instruction_id = 0)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000125 : arena_(arena),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000126 blocks_(arena, kDefaultNumberOfBlocks),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100127 reverse_post_order_(arena, kDefaultNumberOfBlocks),
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100128 linear_order_(arena, kDefaultNumberOfBlocks),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700129 entry_block_(nullptr),
130 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100131 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100132 number_of_vregs_(0),
133 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000134 temporaries_vreg_slots_(0),
Mark Mendell1152c922015-04-24 17:06:35 -0400135 has_bounds_checks_(false),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000136 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000137 current_instruction_id_(start_instruction_id),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100138 dex_file_(dex_file),
139 method_idx_(method_idx),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000140 cached_null_constant_(nullptr),
141 cached_int_constants_(std::less<int32_t>(), arena->Adapter()),
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000142 cached_float_constants_(std::less<int32_t>(), arena->Adapter()),
143 cached_long_constants_(std::less<int64_t>(), arena->Adapter()),
144 cached_double_constants_(std::less<int64_t>(), arena->Adapter()) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000145
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000146 ArenaAllocator* GetArena() const { return arena_; }
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100147 const GrowableArray<HBasicBlock*>& GetBlocks() const { return blocks_; }
Roland Levillain93445682014-10-06 19:24:02 +0100148 HBasicBlock* GetBlock(size_t id) const { return blocks_.Get(id); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000149
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000150 HBasicBlock* GetEntryBlock() const { return entry_block_; }
151 HBasicBlock* GetExitBlock() const { return exit_block_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000152
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000153 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
154 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000155
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000156 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100157
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000158 // Try building the SSA form of this graph, with dominance computation and loop
159 // recognition. Returns whether it was successful in doing all these steps.
160 bool TryBuildingSsa() {
161 BuildDominatorTree();
Nicolas Geoffrayd3350832015-03-12 11:16:23 +0000162 // The SSA builder requires loops to all be natural. Specifically, the dead phi
163 // elimination phase checks the consistency of the graph when doing a post-order
164 // visit for eliminating dead phis: a dead phi can only have loop header phi
165 // users remaining when being visited.
166 if (!AnalyzeNaturalLoops()) return false;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000167 TransformToSsa();
Nicolas Geoffrayd3350832015-03-12 11:16:23 +0000168 return true;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000169 }
170
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000171 void BuildDominatorTree();
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000172 void TransformToSsa();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100173 void SimplifyCFG();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000174
Nicolas Geoffrayf5370122014-12-02 11:51:19 +0000175 // Analyze all natural loops in this graph. Returns false if one
176 // loop is not natural, that is the header does not dominate the
177 // back edge.
178 bool AnalyzeNaturalLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100179
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000180 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
181 void InlineInto(HGraph* outer_graph, HInvoke* invoke);
182
David Brazdil2d7352b2015-04-20 14:52:42 +0100183 // Removes `block` from the graph.
184 void DeleteDeadBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000185
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100186 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
187 void SimplifyLoop(HBasicBlock* header);
188
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000189 int32_t GetNextInstructionId() {
190 DCHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000191 return current_instruction_id_++;
192 }
193
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000194 int32_t GetCurrentInstructionId() const {
195 return current_instruction_id_;
196 }
197
198 void SetCurrentInstructionId(int32_t id) {
199 current_instruction_id_ = id;
200 }
201
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100202 uint16_t GetMaximumNumberOfOutVRegs() const {
203 return maximum_number_of_out_vregs_;
204 }
205
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000206 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
207 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100208 }
209
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000210 void UpdateTemporariesVRegSlots(size_t slots) {
211 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100212 }
213
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000214 size_t GetTemporariesVRegSlots() const {
215 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100216 }
217
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100218 void SetNumberOfVRegs(uint16_t number_of_vregs) {
219 number_of_vregs_ = number_of_vregs;
220 }
221
222 uint16_t GetNumberOfVRegs() const {
223 return number_of_vregs_;
224 }
225
226 void SetNumberOfInVRegs(uint16_t value) {
227 number_of_in_vregs_ = value;
228 }
229
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100230 uint16_t GetNumberOfLocalVRegs() const {
231 return number_of_vregs_ - number_of_in_vregs_;
232 }
233
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100234 const GrowableArray<HBasicBlock*>& GetReversePostOrder() const {
235 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100236 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100237
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100238 const GrowableArray<HBasicBlock*>& GetLinearOrder() const {
239 return linear_order_;
240 }
241
Mark Mendell1152c922015-04-24 17:06:35 -0400242 bool HasBoundsChecks() const {
243 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800244 }
245
Mark Mendell1152c922015-04-24 17:06:35 -0400246 void SetHasBoundsChecks(bool value) {
247 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800248 }
249
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000250 bool IsDebuggable() const { return debuggable_; }
251
David Brazdil8d5b8b22015-03-24 10:51:52 +0000252 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000253 // already, it is created and inserted into the graph. This method is only for
254 // integral types.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000255 HConstant* GetConstant(Primitive::Type type, int64_t value);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000256 HNullConstant* GetNullConstant();
David Brazdil8d5b8b22015-03-24 10:51:52 +0000257 HIntConstant* GetIntConstant(int32_t value) {
258 return CreateConstant(value, &cached_int_constants_);
259 }
260 HLongConstant* GetLongConstant(int64_t value) {
261 return CreateConstant(value, &cached_long_constants_);
262 }
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000263 HFloatConstant* GetFloatConstant(float value) {
264 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_);
265 }
266 HDoubleConstant* GetDoubleConstant(double value) {
267 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_);
268 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000269
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000270 HBasicBlock* FindCommonDominator(HBasicBlock* first, HBasicBlock* second) const;
David Brazdil2d7352b2015-04-20 14:52:42 +0100271
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100272 const DexFile& GetDexFile() const {
273 return dex_file_;
274 }
275
276 uint32_t GetMethodIdx() const {
277 return method_idx_;
278 }
279
David Brazdil2d7352b2015-04-20 14:52:42 +0100280 private:
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000281 void VisitBlockForDominatorTree(HBasicBlock* block,
282 HBasicBlock* predecessor,
283 GrowableArray<size_t>* visits);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100284 void FindBackEdges(ArenaBitVector* visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000285 void VisitBlockForBackEdges(HBasicBlock* block,
286 ArenaBitVector* visited,
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100287 ArenaBitVector* visiting);
Roland Levillainfc600dc2014-12-02 17:16:31 +0000288 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100289 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000290
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000291 template <class InstructionType, typename ValueType>
292 InstructionType* CreateConstant(ValueType value,
293 ArenaSafeMap<ValueType, InstructionType*>* cache) {
294 // Try to find an existing constant of the given value.
295 InstructionType* constant = nullptr;
296 auto cached_constant = cache->find(value);
297 if (cached_constant != cache->end()) {
298 constant = cached_constant->second;
299 }
300
301 // If not found or previously deleted, create and cache a new instruction.
302 if (constant == nullptr || constant->GetBlock() == nullptr) {
303 constant = new (arena_) InstructionType(value);
304 cache->Overwrite(value, constant);
305 InsertConstant(constant);
306 }
307 return constant;
308 }
309
David Brazdil8d5b8b22015-03-24 10:51:52 +0000310 void InsertConstant(HConstant* instruction);
311
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000312 // Cache a float constant into the graph. This method should only be
313 // called by the SsaBuilder when creating "equivalent" instructions.
314 void CacheFloatConstant(HFloatConstant* constant);
315
316 // See CacheFloatConstant comment.
317 void CacheDoubleConstant(HDoubleConstant* constant);
318
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000319 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000320
321 // List of blocks in insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000322 GrowableArray<HBasicBlock*> blocks_;
323
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100324 // List of blocks to perform a reverse post order tree traversal.
325 GrowableArray<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000326
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100327 // List of blocks to perform a linear order tree traversal.
328 GrowableArray<HBasicBlock*> linear_order_;
329
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000330 HBasicBlock* entry_block_;
331 HBasicBlock* exit_block_;
332
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100333 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100334 uint16_t maximum_number_of_out_vregs_;
335
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100336 // The number of virtual registers in this method. Contains the parameters.
337 uint16_t number_of_vregs_;
338
339 // The number of virtual registers used by parameters of this method.
340 uint16_t number_of_in_vregs_;
341
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000342 // Number of vreg size slots that the temporaries use (used in baseline compiler).
343 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100344
Mark Mendell1152c922015-04-24 17:06:35 -0400345 // Has bounds checks. We can totally skip BCE if it's false.
346 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800347
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000348 // Indicates whether the graph should be compiled in a way that
349 // ensures full debuggability. If false, we can apply more
350 // aggressive optimizations that may limit the level of debugging.
351 const bool debuggable_;
352
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000353 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000354 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000355
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100356 // The dex file from which the method is from.
357 const DexFile& dex_file_;
358
359 // The method index in the dex file.
360 const uint32_t method_idx_;
361
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000362 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000363 HNullConstant* cached_null_constant_;
364 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000365 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000366 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000367 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000368
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000369 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100370 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000371 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000372 DISALLOW_COPY_AND_ASSIGN(HGraph);
373};
374
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700375class HLoopInformation : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000376 public:
377 HLoopInformation(HBasicBlock* header, HGraph* graph)
378 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100379 suspend_check_(nullptr),
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100380 back_edges_(graph->GetArena(), kDefaultNumberOfBackEdges),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100381 // Make bit vector growable, as the number of blocks may change.
382 blocks_(graph->GetArena(), graph->GetBlocks().Size(), true) {}
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100383
384 HBasicBlock* GetHeader() const {
385 return header_;
386 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000387
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000388 void SetHeader(HBasicBlock* block) {
389 header_ = block;
390 }
391
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100392 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
393 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
394 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
395
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000396 void AddBackEdge(HBasicBlock* back_edge) {
397 back_edges_.Add(back_edge);
398 }
399
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100400 void RemoveBackEdge(HBasicBlock* back_edge) {
401 back_edges_.Delete(back_edge);
402 }
403
David Brazdil46e2a392015-03-16 17:31:52 +0000404 bool IsBackEdge(const HBasicBlock& block) const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100405 for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) {
David Brazdil46e2a392015-03-16 17:31:52 +0000406 if (back_edges_.Get(i) == &block) return true;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100407 }
408 return false;
409 }
410
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000411 size_t NumberOfBackEdges() const {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000412 return back_edges_.Size();
413 }
414
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100415 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100416
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100417 const GrowableArray<HBasicBlock*>& GetBackEdges() const {
418 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100419 }
420
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100421 // Returns the lifetime position of the back edge that has the
422 // greatest lifetime position.
423 size_t GetLifetimeEnd() const;
424
425 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
426 for (size_t i = 0, e = back_edges_.Size(); i < e; ++i) {
427 if (back_edges_.Get(i) == existing) {
428 back_edges_.Put(i, new_back_edge);
429 return;
430 }
431 }
432 UNREACHABLE();
Nicolas Geoffray57902602015-04-21 14:28:41 +0100433 }
434
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100435 // Finds blocks that are part of this loop. Returns whether the loop is a natural loop,
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100436 // that is the header dominates the back edge.
437 bool Populate();
438
David Brazdila4b8c212015-05-07 09:59:30 +0100439 // Reanalyzes the loop by removing loop info from its blocks and re-running
440 // Populate(). If there are no back edges left, the loop info is completely
441 // removed as well as its SuspendCheck instruction. It must be run on nested
442 // inner loops first.
443 void Update();
444
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100445 // Returns whether this loop information contains `block`.
446 // Note that this loop information *must* be populated before entering this function.
447 bool Contains(const HBasicBlock& block) const;
448
449 // Returns whether this loop information is an inner loop of `other`.
450 // Note that `other` *must* be populated before entering this function.
451 bool IsIn(const HLoopInformation& other) const;
452
453 const ArenaBitVector& GetBlocks() const { return blocks_; }
454
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000455 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000456 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000457
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000458 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100459 // Internal recursive implementation of `Populate`.
460 void PopulateRecursive(HBasicBlock* block);
461
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000462 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100463 HSuspendCheck* suspend_check_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000464 GrowableArray<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100465 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000466
467 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
468};
469
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100470static constexpr size_t kNoLifetime = -1;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100471static constexpr uint32_t kNoDexPc = -1;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100472
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000473// A block in a method. Contains the list of instructions represented
474// as a double linked list. Each block knows its predecessors and
475// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100476
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700477class HBasicBlock : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000478 public:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100479 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000480 : graph_(graph),
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000481 predecessors_(graph->GetArena(), kDefaultNumberOfPredecessors),
482 successors_(graph->GetArena(), kDefaultNumberOfSuccessors),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000483 loop_information_(nullptr),
484 dominator_(nullptr),
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100485 dominated_blocks_(graph->GetArena(), kDefaultNumberOfDominatedBlocks),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100486 block_id_(-1),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100487 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100488 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000489 lifetime_end_(kNoLifetime),
490 is_catch_block_(false) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000491
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100492 const GrowableArray<HBasicBlock*>& GetPredecessors() const {
493 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000494 }
495
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100496 const GrowableArray<HBasicBlock*>& GetSuccessors() const {
497 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000498 }
499
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100500 const GrowableArray<HBasicBlock*>& GetDominatedBlocks() const {
501 return dominated_blocks_;
502 }
503
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100504 bool IsEntryBlock() const {
505 return graph_->GetEntryBlock() == this;
506 }
507
508 bool IsExitBlock() const {
509 return graph_->GetExitBlock() == this;
510 }
511
David Brazdil46e2a392015-03-16 17:31:52 +0000512 bool IsSingleGoto() const;
513
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000514 void AddBackEdge(HBasicBlock* back_edge) {
515 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000516 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000517 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100518 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000519 loop_information_->AddBackEdge(back_edge);
520 }
521
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000522 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000523 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000524
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000525 int GetBlockId() const { return block_id_; }
526 void SetBlockId(int id) { block_id_ = id; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000527
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000528 HBasicBlock* GetDominator() const { return dominator_; }
529 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100530 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.Add(block); }
David Brazdil2d7352b2015-04-20 14:52:42 +0100531 void RemoveDominatedBlock(HBasicBlock* block) { dominated_blocks_.Delete(block); }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000532 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
533 for (size_t i = 0, e = dominated_blocks_.Size(); i < e; ++i) {
534 if (dominated_blocks_.Get(i) == existing) {
535 dominated_blocks_.Put(i, new_block);
536 return;
537 }
538 }
539 LOG(FATAL) << "Unreachable";
540 UNREACHABLE();
541 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000542
543 int NumberOfBackEdges() const {
544 return loop_information_ == nullptr
545 ? 0
546 : loop_information_->NumberOfBackEdges();
547 }
548
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100549 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
550 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100551 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100552 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +0100553 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
554 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000555
556 void AddSuccessor(HBasicBlock* block) {
557 successors_.Add(block);
558 block->predecessors_.Add(this);
559 }
560
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100561 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
562 size_t successor_index = GetSuccessorIndexOf(existing);
563 DCHECK_NE(successor_index, static_cast<size_t>(-1));
564 existing->RemovePredecessor(this);
565 new_block->predecessors_.Add(this);
566 successors_.Put(successor_index, new_block);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000567 }
568
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000569 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
570 size_t predecessor_index = GetPredecessorIndexOf(existing);
571 DCHECK_NE(predecessor_index, static_cast<size_t>(-1));
572 existing->RemoveSuccessor(this);
573 new_block->successors_.Add(this);
574 predecessors_.Put(predecessor_index, new_block);
575 }
576
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100577 void RemovePredecessor(HBasicBlock* block) {
578 predecessors_.Delete(block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100579 }
580
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000581 void RemoveSuccessor(HBasicBlock* block) {
582 successors_.Delete(block);
583 }
584
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100585 void ClearAllPredecessors() {
586 predecessors_.Reset();
587 }
588
589 void AddPredecessor(HBasicBlock* block) {
590 predecessors_.Add(block);
591 block->successors_.Add(this);
592 }
593
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100594 void SwapPredecessors() {
Nicolas Geoffrayc83d4412014-09-18 16:46:20 +0100595 DCHECK_EQ(predecessors_.Size(), 2u);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +0100596 HBasicBlock* temp = predecessors_.Get(0);
597 predecessors_.Put(0, predecessors_.Get(1));
598 predecessors_.Put(1, temp);
599 }
600
David Brazdil769c9e52015-04-27 13:54:09 +0100601 void SwapSuccessors() {
602 DCHECK_EQ(successors_.Size(), 2u);
603 HBasicBlock* temp = successors_.Get(0);
604 successors_.Put(0, successors_.Get(1));
605 successors_.Put(1, temp);
606 }
607
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100608 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) {
609 for (size_t i = 0, e = predecessors_.Size(); i < e; ++i) {
610 if (predecessors_.Get(i) == predecessor) {
611 return i;
612 }
613 }
614 return -1;
615 }
616
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100617 size_t GetSuccessorIndexOf(HBasicBlock* successor) {
618 for (size_t i = 0, e = successors_.Size(); i < e; ++i) {
619 if (successors_.Get(i) == successor) {
620 return i;
621 }
622 }
623 return -1;
624 }
625
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000626 // Split the block into two blocks just after `cursor`. Returns the newly
627 // created block. Note that this method just updates raw block information,
628 // like predecessors, successors, dominators, and instruction list. It does not
629 // update the graph, reverse post order, loop information, nor make sure the
630 // blocks are consistent (for example ending with a control flow instruction).
631 HBasicBlock* SplitAfter(HInstruction* cursor);
632
633 // Merge `other` at the end of `this`. Successors and dominated blocks of
634 // `other` are changed to be successors and dominated blocks of `this`. Note
635 // that this method does not update the graph, reverse post order, loop
636 // information, nor make sure the blocks are consistent (for example ending
637 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +0100638 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000639
640 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
641 // of `this` are moved to `other`.
642 // Note that this method does not update the graph, reverse post order, loop
643 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +0000644 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000645 void ReplaceWith(HBasicBlock* other);
646
David Brazdil2d7352b2015-04-20 14:52:42 +0100647 // Merge `other` at the end of `this`. This method updates loops, reverse post
648 // order, links to predecessors, successors, dominators and deletes the block
649 // from the graph. The two blocks must be successive, i.e. `this` the only
650 // predecessor of `other` and vice versa.
651 void MergeWith(HBasicBlock* other);
652
653 // Disconnects `this` from all its predecessors, successors and dominator,
654 // removes it from all loops it is included in and eventually from the graph.
655 // The block must not dominate any other block. Predecessors and successors
656 // are safely updated.
657 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +0000658
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000659 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100660 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +0100661 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +0100662 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +0100663 // Replace instruction `initial` with `replacement` within this block.
664 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
665 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100666 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100667 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +0000668 // RemoveInstruction and RemovePhi delete a given instruction from the respective
669 // instruction list. With 'ensure_safety' set to true, it verifies that the
670 // instruction is not in use and removes it from the use lists of its inputs.
671 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
672 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +0100673 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100674
675 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +0100676 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100677 }
678
Roland Levillain6b879dd2014-09-22 17:13:44 +0100679 bool IsLoopPreHeaderFirstPredecessor() const {
680 DCHECK(IsLoopHeader());
681 DCHECK(!GetPredecessors().IsEmpty());
682 return GetPredecessors().Get(0) == GetLoopInformation()->GetPreHeader();
683 }
684
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100685 HLoopInformation* GetLoopInformation() const {
686 return loop_information_;
687 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000688
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000689 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100690 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000691 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100692 void SetInLoop(HLoopInformation* info) {
693 if (IsLoopHeader()) {
694 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +0100695 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100696 loop_information_ = info;
697 } else if (loop_information_->Contains(*info->GetHeader())) {
698 // Block is currently part of an outer loop. Make it part of this inner loop.
699 // Note that a non loop header having a loop information means this loop information
700 // has already been populated
701 loop_information_ = info;
702 } else {
703 // Block is part of an inner loop. Do not update the loop information.
704 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
705 // at this point, because this method is being called while populating `info`.
706 }
707 }
708
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000709 // Raw update of the loop information.
710 void SetLoopInformation(HLoopInformation* info) {
711 loop_information_ = info;
712 }
713
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +0100714 bool IsInLoop() const { return loop_information_ != nullptr; }
715
David Brazdila4b8c212015-05-07 09:59:30 +0100716 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100717 bool Dominates(HBasicBlock* block) const;
718
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100719 size_t GetLifetimeStart() const { return lifetime_start_; }
720 size_t GetLifetimeEnd() const { return lifetime_end_; }
721
722 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
723 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
724
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100725 uint32_t GetDexPc() const { return dex_pc_; }
726
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000727 bool IsCatchBlock() const { return is_catch_block_; }
728 void SetIsCatchBlock() { is_catch_block_ = true; }
729
David Brazdil8d5b8b22015-03-24 10:51:52 +0000730 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +0000731 bool EndsWithIf() const;
732 bool HasSinglePhi() const;
733
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000734 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000735 HGraph* graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000736 GrowableArray<HBasicBlock*> predecessors_;
737 GrowableArray<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100738 HInstructionList instructions_;
739 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000740 HLoopInformation* loop_information_;
741 HBasicBlock* dominator_;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100742 GrowableArray<HBasicBlock*> dominated_blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000743 int block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100744 // The dex program counter of the first instruction of this block.
745 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100746 size_t lifetime_start_;
747 size_t lifetime_end_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000748 bool is_catch_block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000749
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000750 friend class HGraph;
751 friend class HInstruction;
752
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000753 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
754};
755
David Brazdilb2bd1c52015-03-25 11:17:37 +0000756// Iterates over the LoopInformation of all loops which contain 'block'
757// from the innermost to the outermost.
758class HLoopInformationOutwardIterator : public ValueObject {
759 public:
760 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
761 : current_(block.GetLoopInformation()) {}
762
763 bool Done() const { return current_ == nullptr; }
764
765 void Advance() {
766 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +0100767 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +0000768 }
769
770 HLoopInformation* Current() const {
771 DCHECK(!Done());
772 return current_;
773 }
774
775 private:
776 HLoopInformation* current_;
777
778 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
779};
780
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100781#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
782 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000783 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000784 M(ArrayGet, Instruction) \
785 M(ArrayLength, Instruction) \
786 M(ArraySet, Instruction) \
David Brazdil66d126e2015-04-03 16:02:44 +0100787 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000788 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +0000789 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000790 M(CheckCast, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100791 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000792 M(Compare, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100793 M(Condition, BinaryOperation) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700794 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000795 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +0000796 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000797 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100798 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000799 M(Exit, Instruction) \
800 M(FloatConstant, Constant) \
801 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100802 M(GreaterThan, Condition) \
803 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100804 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000805 M(InstanceFieldGet, Instruction) \
806 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000807 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100808 M(IntConstant, Constant) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +0000809 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000810 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100811 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000812 M(LessThan, Condition) \
813 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000814 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000815 M(LoadException, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100816 M(LoadLocal, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000817 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100818 M(Local, Instruction) \
819 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +0100820 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +0000821 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000822 M(Mul, BinaryOperation) \
823 M(Neg, UnaryOperation) \
824 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100825 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +0100826 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000827 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000828 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000829 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000830 M(Or, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100831 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000832 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100833 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000834 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100835 M(Return, Instruction) \
836 M(ReturnVoid, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000837 M(Shl, BinaryOperation) \
838 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100839 M(StaticFieldGet, Instruction) \
840 M(StaticFieldSet, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100841 M(StoreLocal, Instruction) \
842 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100843 M(SuspendCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +0000844 M(Temporary, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000845 M(Throw, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +0000846 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +0000847 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +0000848 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000849
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100850#define FOR_EACH_INSTRUCTION(M) \
851 FOR_EACH_CONCRETE_INSTRUCTION(M) \
852 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +0100853 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +0100854 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100855 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -0700856
Nicolas Geoffray360231a2014-10-08 21:07:48 +0100857#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000858FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
859#undef FORWARD_DECLARATION
860
Roland Levillainccc07a92014-09-16 14:48:16 +0100861#define DECLARE_INSTRUCTION(type) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000862 InstructionKind GetKind() const OVERRIDE { return k##type; } \
863 const char* DebugName() const OVERRIDE { return #type; } \
864 const H##type* As##type() const OVERRIDE { return this; } \
865 H##type* As##type() OVERRIDE { return this; } \
866 bool InstructionTypeEquals(HInstruction* other) const OVERRIDE { \
Roland Levillainccc07a92014-09-16 14:48:16 +0100867 return other->Is##type(); \
868 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000869 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000870
David Brazdiled596192015-01-23 10:39:45 +0000871template <typename T> class HUseList;
872
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100873template <typename T>
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700874class HUseListNode : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000875 public:
David Brazdiled596192015-01-23 10:39:45 +0000876 HUseListNode* GetPrevious() const { return prev_; }
877 HUseListNode* GetNext() const { return next_; }
878 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100879 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +0100880 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100881
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000882 private:
David Brazdiled596192015-01-23 10:39:45 +0000883 HUseListNode(T user, size_t index)
884 : user_(user), index_(index), prev_(nullptr), next_(nullptr) {}
885
886 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +0100887 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +0000888 HUseListNode<T>* prev_;
889 HUseListNode<T>* next_;
890
891 friend class HUseList<T>;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000892
893 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
894};
895
David Brazdiled596192015-01-23 10:39:45 +0000896template <typename T>
897class HUseList : public ValueObject {
898 public:
899 HUseList() : first_(nullptr) {}
900
901 void Clear() {
902 first_ = nullptr;
903 }
904
905 // Adds a new entry at the beginning of the use list and returns
906 // the newly created node.
907 HUseListNode<T>* AddUse(T user, size_t index, ArenaAllocator* arena) {
David Brazdilea55b932015-01-27 17:12:29 +0000908 HUseListNode<T>* new_node = new (arena) HUseListNode<T>(user, index);
David Brazdiled596192015-01-23 10:39:45 +0000909 if (IsEmpty()) {
910 first_ = new_node;
911 } else {
912 first_->prev_ = new_node;
913 new_node->next_ = first_;
914 first_ = new_node;
915 }
916 return new_node;
917 }
918
919 HUseListNode<T>* GetFirst() const {
920 return first_;
921 }
922
923 void Remove(HUseListNode<T>* node) {
David Brazdil1abb4192015-02-17 18:33:36 +0000924 DCHECK(node != nullptr);
925 DCHECK(Contains(node));
926
David Brazdiled596192015-01-23 10:39:45 +0000927 if (node->prev_ != nullptr) {
928 node->prev_->next_ = node->next_;
929 }
930 if (node->next_ != nullptr) {
931 node->next_->prev_ = node->prev_;
932 }
933 if (node == first_) {
934 first_ = node->next_;
935 }
936 }
937
David Brazdil1abb4192015-02-17 18:33:36 +0000938 bool Contains(const HUseListNode<T>* node) const {
939 if (node == nullptr) {
940 return false;
941 }
942 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
943 if (current == node) {
944 return true;
945 }
946 }
947 return false;
948 }
949
David Brazdiled596192015-01-23 10:39:45 +0000950 bool IsEmpty() const {
951 return first_ == nullptr;
952 }
953
954 bool HasOnlyOneUse() const {
955 return first_ != nullptr && first_->next_ == nullptr;
956 }
957
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100958 size_t SizeSlow() const {
959 size_t count = 0;
960 for (HUseListNode<T>* current = first_; current != nullptr; current = current->GetNext()) {
961 ++count;
962 }
963 return count;
964 }
965
David Brazdiled596192015-01-23 10:39:45 +0000966 private:
967 HUseListNode<T>* first_;
968};
969
970template<typename T>
971class HUseIterator : public ValueObject {
972 public:
973 explicit HUseIterator(const HUseList<T>& uses) : current_(uses.GetFirst()) {}
974
975 bool Done() const { return current_ == nullptr; }
976
977 void Advance() {
978 DCHECK(!Done());
979 current_ = current_->GetNext();
980 }
981
982 HUseListNode<T>* Current() const {
983 DCHECK(!Done());
984 return current_;
985 }
986
987 private:
988 HUseListNode<T>* current_;
989
990 friend class HValue;
991};
992
David Brazdil1abb4192015-02-17 18:33:36 +0000993// This class is used by HEnvironment and HInstruction classes to record the
994// instructions they use and pointers to the corresponding HUseListNodes kept
995// by the used instructions.
996template <typename T>
997class HUserRecord : public ValueObject {
998 public:
999 HUserRecord() : instruction_(nullptr), use_node_(nullptr) {}
1000 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), use_node_(nullptr) {}
1001
1002 HUserRecord(const HUserRecord<T>& old_record, HUseListNode<T>* use_node)
1003 : instruction_(old_record.instruction_), use_node_(use_node) {
1004 DCHECK(instruction_ != nullptr);
1005 DCHECK(use_node_ != nullptr);
1006 DCHECK(old_record.use_node_ == nullptr);
1007 }
1008
1009 HInstruction* GetInstruction() const { return instruction_; }
1010 HUseListNode<T>* GetUseNode() const { return use_node_; }
1011
1012 private:
1013 // Instruction used by the user.
1014 HInstruction* instruction_;
1015
1016 // Corresponding entry in the use list kept by 'instruction_'.
1017 HUseListNode<T>* use_node_;
1018};
1019
Calin Juravle27df7582015-04-17 19:12:31 +01001020// TODO: Add better documentation to this class and maybe refactor with more suggestive names.
1021// - Has(All)SideEffects suggests that all the side effects are present but only ChangesSomething
1022// flag is consider.
1023// - DependsOn suggests that there is a real dependency between side effects but it only
1024// checks DependendsOnSomething flag.
1025//
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001026// Represents the side effects an instruction may have.
1027class SideEffects : public ValueObject {
1028 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001029 SideEffects() : flags_(0) {}
1030
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001031 static SideEffects None() {
1032 return SideEffects(0);
1033 }
1034
1035 static SideEffects All() {
1036 return SideEffects(ChangesSomething().flags_ | DependsOnSomething().flags_);
1037 }
1038
1039 static SideEffects ChangesSomething() {
1040 return SideEffects((1 << kFlagChangesCount) - 1);
1041 }
1042
1043 static SideEffects DependsOnSomething() {
1044 int count = kFlagDependsOnCount - kFlagChangesCount;
1045 return SideEffects(((1 << count) - 1) << kFlagChangesCount);
1046 }
1047
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001048 SideEffects Union(SideEffects other) const {
1049 return SideEffects(flags_ | other.flags_);
1050 }
1051
Roland Levillain72bceff2014-09-15 18:29:00 +01001052 bool HasSideEffects() const {
1053 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
1054 return (flags_ & all_bits_set) != 0;
1055 }
1056
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001057 bool HasAllSideEffects() const {
1058 size_t all_bits_set = (1 << kFlagChangesCount) - 1;
1059 return all_bits_set == (flags_ & all_bits_set);
1060 }
1061
1062 bool DependsOn(SideEffects other) const {
1063 size_t depends_flags = other.ComputeDependsFlags();
1064 return (flags_ & depends_flags) != 0;
1065 }
1066
1067 bool HasDependencies() const {
1068 int count = kFlagDependsOnCount - kFlagChangesCount;
1069 size_t all_bits_set = (1 << count) - 1;
1070 return ((flags_ >> kFlagChangesCount) & all_bits_set) != 0;
1071 }
1072
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001073 private:
1074 static constexpr int kFlagChangesSomething = 0;
1075 static constexpr int kFlagChangesCount = kFlagChangesSomething + 1;
1076
1077 static constexpr int kFlagDependsOnSomething = kFlagChangesCount;
1078 static constexpr int kFlagDependsOnCount = kFlagDependsOnSomething + 1;
1079
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001080 explicit SideEffects(size_t flags) : flags_(flags) {}
1081
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001082 size_t ComputeDependsFlags() const {
1083 return flags_ << kFlagChangesCount;
1084 }
1085
1086 size_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001087};
1088
David Brazdiled596192015-01-23 10:39:45 +00001089// A HEnvironment object contains the values of virtual registers at a given location.
1090class HEnvironment : public ArenaObject<kArenaAllocMisc> {
1091 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001092 HEnvironment(ArenaAllocator* arena,
1093 size_t number_of_vregs,
1094 const DexFile& dex_file,
1095 uint32_t method_idx,
1096 uint32_t dex_pc)
1097 : vregs_(arena, number_of_vregs),
1098 locations_(arena, number_of_vregs),
1099 parent_(nullptr),
1100 dex_file_(dex_file),
1101 method_idx_(method_idx),
1102 dex_pc_(dex_pc) {
David Brazdiled596192015-01-23 10:39:45 +00001103 vregs_.SetSize(number_of_vregs);
1104 for (size_t i = 0; i < number_of_vregs; i++) {
David Brazdil1abb4192015-02-17 18:33:36 +00001105 vregs_.Put(i, HUserRecord<HEnvironment*>());
David Brazdiled596192015-01-23 10:39:45 +00001106 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001107
1108 locations_.SetSize(number_of_vregs);
1109 for (size_t i = 0; i < number_of_vregs; ++i) {
1110 locations_.Put(i, Location());
1111 }
1112 }
1113
1114 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
1115 parent_ = new (allocator) HEnvironment(allocator,
1116 parent->Size(),
1117 parent->GetDexFile(),
1118 parent->GetMethodIdx(),
1119 parent->GetDexPc());
1120 if (parent->GetParent() != nullptr) {
1121 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1122 }
1123 parent_->CopyFrom(parent);
David Brazdiled596192015-01-23 10:39:45 +00001124 }
1125
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001126 void CopyFrom(const GrowableArray<HInstruction*>& locals);
1127 void CopyFrom(HEnvironment* environment);
1128
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001129 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1130 // input to the loop phi instead. This is for inserting instructions that
1131 // require an environment (like HDeoptimization) in the loop pre-header.
1132 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001133
1134 void SetRawEnvAt(size_t index, HInstruction* instruction) {
David Brazdil1abb4192015-02-17 18:33:36 +00001135 vregs_.Put(index, HUserRecord<HEnvironment*>(instruction));
David Brazdiled596192015-01-23 10:39:45 +00001136 }
1137
1138 HInstruction* GetInstructionAt(size_t index) const {
David Brazdil1abb4192015-02-17 18:33:36 +00001139 return vregs_.Get(index).GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001140 }
1141
David Brazdil1abb4192015-02-17 18:33:36 +00001142 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001143
1144 size_t Size() const { return vregs_.Size(); }
1145
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001146 HEnvironment* GetParent() const { return parent_; }
1147
1148 void SetLocationAt(size_t index, Location location) {
1149 locations_.Put(index, location);
1150 }
1151
1152 Location GetLocationAt(size_t index) const {
1153 return locations_.Get(index);
1154 }
1155
1156 uint32_t GetDexPc() const {
1157 return dex_pc_;
1158 }
1159
1160 uint32_t GetMethodIdx() const {
1161 return method_idx_;
1162 }
1163
1164 const DexFile& GetDexFile() const {
1165 return dex_file_;
1166 }
1167
David Brazdiled596192015-01-23 10:39:45 +00001168 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001169 // Record instructions' use entries of this environment for constant-time removal.
1170 // It should only be called by HInstruction when a new environment use is added.
1171 void RecordEnvUse(HUseListNode<HEnvironment*>* env_use) {
1172 DCHECK(env_use->GetUser() == this);
1173 size_t index = env_use->GetIndex();
1174 vregs_.Put(index, HUserRecord<HEnvironment*>(vregs_.Get(index), env_use));
1175 }
David Brazdiled596192015-01-23 10:39:45 +00001176
David Brazdil1abb4192015-02-17 18:33:36 +00001177 GrowableArray<HUserRecord<HEnvironment*> > vregs_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001178 GrowableArray<Location> locations_;
1179 HEnvironment* parent_;
1180 const DexFile& dex_file_;
1181 const uint32_t method_idx_;
1182 const uint32_t dex_pc_;
David Brazdiled596192015-01-23 10:39:45 +00001183
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001184 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001185
1186 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1187};
1188
Calin Juravleacf735c2015-02-12 15:25:22 +00001189class ReferenceTypeInfo : ValueObject {
1190 public:
Calin Juravleb1498f62015-02-16 13:13:29 +00001191 typedef Handle<mirror::Class> TypeHandle;
1192
1193 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact)
1194 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1195 if (type_handle->IsObjectClass()) {
1196 // Override the type handle to be consistent with the case when we get to
1197 // Top but don't have the Object class available. It avoids having to guess
1198 // what value the type_handle has when it's Top.
1199 return ReferenceTypeInfo(TypeHandle(), is_exact, true);
1200 } else {
1201 return ReferenceTypeInfo(type_handle, is_exact, false);
1202 }
1203 }
1204
1205 static ReferenceTypeInfo CreateTop(bool is_exact) {
1206 return ReferenceTypeInfo(TypeHandle(), is_exact, true);
Calin Juravleacf735c2015-02-12 15:25:22 +00001207 }
1208
1209 bool IsExact() const { return is_exact_; }
1210 bool IsTop() const { return is_top_; }
1211
1212 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
1213
Calin Juravleb1498f62015-02-16 13:13:29 +00001214 bool IsSupertypeOf(ReferenceTypeInfo rti) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Calin Juravleacf735c2015-02-12 15:25:22 +00001215 if (IsTop()) {
1216 // Top (equivalent for java.lang.Object) is supertype of anything.
1217 return true;
1218 }
1219 if (rti.IsTop()) {
1220 // If we get here `this` is not Top() so it can't be a supertype.
1221 return false;
1222 }
1223 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
1224 }
1225
1226 // Returns true if the type information provide the same amount of details.
1227 // Note that it does not mean that the instructions have the same actual type
1228 // (e.g. tops are equal but they can be the result of a merge).
1229 bool IsEqual(ReferenceTypeInfo rti) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1230 if (IsExact() != rti.IsExact()) {
1231 return false;
1232 }
1233 if (IsTop() && rti.IsTop()) {
1234 // `Top` means java.lang.Object, so the types are equivalent.
1235 return true;
1236 }
1237 if (IsTop() || rti.IsTop()) {
1238 // If only one is top or object than they are not equivalent.
1239 // NB: We need this extra check because the type_handle of `Top` is invalid
1240 // and we cannot inspect its reference.
1241 return false;
1242 }
1243
1244 // Finally check the types.
1245 return GetTypeHandle().Get() == rti.GetTypeHandle().Get();
1246 }
1247
1248 private:
Calin Juravleb1498f62015-02-16 13:13:29 +00001249 ReferenceTypeInfo() : ReferenceTypeInfo(TypeHandle(), false, true) {}
1250 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact, bool is_top)
1251 : type_handle_(type_handle), is_exact_(is_exact), is_top_(is_top) {}
1252
Calin Juravleacf735c2015-02-12 15:25:22 +00001253 // The class of the object.
Calin Juravleb1498f62015-02-16 13:13:29 +00001254 TypeHandle type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00001255 // Whether or not the type is exact or a superclass of the actual type.
Calin Juravleb1498f62015-02-16 13:13:29 +00001256 // Whether or not we have any information about this type.
Calin Juravleacf735c2015-02-12 15:25:22 +00001257 bool is_exact_;
1258 // A true value here means that the object type should be java.lang.Object.
1259 // We don't have access to the corresponding mirror object every time so this
1260 // flag acts as a substitute. When true, the TypeHandle refers to a null
1261 // pointer and should not be used.
1262 bool is_top_;
1263};
1264
1265std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
1266
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001267class HInstruction : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001268 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001269 explicit HInstruction(SideEffects side_effects)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001270 : previous_(nullptr),
1271 next_(nullptr),
1272 block_(nullptr),
1273 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001274 ssa_index_(-1),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001275 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001276 locations_(nullptr),
1277 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001278 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001279 side_effects_(side_effects),
1280 reference_type_info_(ReferenceTypeInfo::CreateTop(/* is_exact */ false)) {}
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001281
Dave Allison20dfc792014-06-16 20:44:29 -07001282 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001283
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001284#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001285 enum InstructionKind {
1286 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1287 };
1288#undef DECLARE_KIND
1289
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001290 HInstruction* GetNext() const { return next_; }
1291 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001292
Calin Juravle77520bc2015-01-12 18:45:46 +00001293 HInstruction* GetNextDisregardingMoves() const;
1294 HInstruction* GetPreviousDisregardingMoves() const;
1295
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001296 HBasicBlock* GetBlock() const { return block_; }
1297 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001298 bool IsInBlock() const { return block_ != nullptr; }
1299 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray3ac17fc2014-08-06 23:02:54 +01001300 bool IsLoopHeaderPhi() { return IsPhi() && block_->IsLoopHeader(); }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001301
Roland Levillain6b879dd2014-09-22 17:13:44 +01001302 virtual size_t InputCount() const = 0;
David Brazdil1abb4192015-02-17 18:33:36 +00001303 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001304
1305 virtual void Accept(HGraphVisitor* visitor) = 0;
1306 virtual const char* DebugName() const = 0;
1307
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001308 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
David Brazdil1abb4192015-02-17 18:33:36 +00001309 void SetRawInputAt(size_t index, HInstruction* input) {
1310 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1311 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001312
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001313 virtual bool NeedsEnvironment() const { return false; }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001314 virtual uint32_t GetDexPc() const {
1315 LOG(FATAL) << "GetDexPc() cannot be called on an instruction that"
1316 " does not need an environment";
1317 UNREACHABLE();
1318 }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001319 virtual bool IsControlFlow() const { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01001320 virtual bool CanThrow() const { return false; }
Roland Levillain72bceff2014-09-15 18:29:00 +01001321 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001322
Calin Juravle10e244f2015-01-26 18:54:32 +00001323 // Does not apply for all instructions, but having this at top level greatly
1324 // simplifies the null check elimination.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001325 virtual bool CanBeNull() const {
1326 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1327 return true;
1328 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001329
Calin Juravle641547a2015-04-21 22:08:51 +01001330 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj) const {
1331 UNUSED(obj);
1332 return false;
1333 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001334
Calin Juravleacf735c2015-02-12 15:25:22 +00001335 void SetReferenceTypeInfo(ReferenceTypeInfo reference_type_info) {
Calin Juravle61d544b2015-02-23 16:46:57 +00001336 DCHECK_EQ(GetType(), Primitive::kPrimNot);
Calin Juravleacf735c2015-02-12 15:25:22 +00001337 reference_type_info_ = reference_type_info;
1338 }
1339
Calin Juravle61d544b2015-02-23 16:46:57 +00001340 ReferenceTypeInfo GetReferenceTypeInfo() const {
1341 DCHECK_EQ(GetType(), Primitive::kPrimNot);
1342 return reference_type_info_;
1343 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001344
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001345 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001346 DCHECK(user != nullptr);
1347 HUseListNode<HInstruction*>* use =
1348 uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1349 user->SetRawInputRecordAt(index, HUserRecord<HInstruction*>(user->InputRecordAt(index), use));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001350 }
1351
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001352 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001353 DCHECK(user != nullptr);
David Brazdiled596192015-01-23 10:39:45 +00001354 HUseListNode<HEnvironment*>* env_use =
1355 env_uses_.AddUse(user, index, GetBlock()->GetGraph()->GetArena());
1356 user->RecordEnvUse(env_use);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001357 }
1358
David Brazdil1abb4192015-02-17 18:33:36 +00001359 void RemoveAsUserOfInput(size_t input) {
1360 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
1361 input_use.GetInstruction()->uses_.Remove(input_use.GetUseNode());
1362 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001363
David Brazdil1abb4192015-02-17 18:33:36 +00001364 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1365 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001366
David Brazdiled596192015-01-23 10:39:45 +00001367 bool HasUses() const { return !uses_.IsEmpty() || !env_uses_.IsEmpty(); }
1368 bool HasEnvironmentUses() const { return !env_uses_.IsEmpty(); }
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001369 bool HasNonEnvironmentUses() const { return !uses_.IsEmpty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001370 bool HasOnlyOneNonEnvironmentUse() const {
1371 return !HasEnvironmentUses() && GetUses().HasOnlyOneUse();
1372 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001373
Roland Levillain6c82d402014-10-13 16:10:27 +01001374 // Does this instruction strictly dominate `other_instruction`?
1375 // Returns false if this instruction and `other_instruction` are the same.
1376 // Aborts if this instruction and `other_instruction` are both phis.
1377 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01001378
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001379 int GetId() const { return id_; }
1380 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001381
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001382 int GetSsaIndex() const { return ssa_index_; }
1383 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
1384 bool HasSsaIndex() const { return ssa_index_ != -1; }
1385
1386 bool HasEnvironment() const { return environment_ != nullptr; }
1387 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001388 // Set the `environment_` field. Raw because this method does not
1389 // update the uses lists.
1390 void SetRawEnvironment(HEnvironment* environment) { environment_ = environment; }
1391
1392 // Set the environment of this instruction, copying it from `environment`. While
1393 // copying, the uses lists are being updated.
1394 void CopyEnvironmentFrom(HEnvironment* environment) {
1395 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001396 environment_ = new (allocator) HEnvironment(
1397 allocator,
1398 environment->Size(),
1399 environment->GetDexFile(),
1400 environment->GetMethodIdx(),
1401 environment->GetDexPc());
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001402 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001403 if (environment->GetParent() != nullptr) {
1404 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1405 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001406 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001407
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001408 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
1409 HBasicBlock* block) {
1410 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001411 environment_ = new (allocator) HEnvironment(
1412 allocator,
1413 environment->Size(),
1414 environment->GetDexFile(),
1415 environment->GetMethodIdx(),
1416 environment->GetDexPc());
1417 if (environment->GetParent() != nullptr) {
1418 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
1419 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001420 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
1421 }
1422
Nicolas Geoffray39468442014-09-02 15:17:15 +01001423 // Returns the number of entries in the environment. Typically, that is the
1424 // number of dex registers in a method. It could be more in case of inlining.
1425 size_t EnvironmentSize() const;
1426
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001427 LocationSummary* GetLocations() const { return locations_; }
1428 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001429
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001430 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001431 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001432
Alexandre Rames188d4312015-04-09 18:30:21 +01001433 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
1434 // uses of this instruction by `other` are *not* updated.
1435 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
1436 ReplaceWith(other);
1437 other->ReplaceInput(this, use_index);
1438 }
1439
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001440 // Move `this` instruction before `cursor`.
1441 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001442
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001443#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01001444 bool Is##type() const { return (As##type() != nullptr); } \
1445 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001446 virtual H##type* As##type() { return nullptr; }
1447
1448 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
1449#undef INSTRUCTION_TYPE_CHECK
1450
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001451 // Returns whether the instruction can be moved within the graph.
1452 virtual bool CanBeMoved() const { return false; }
1453
1454 // Returns whether the two instructions are of the same kind.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001455 virtual bool InstructionTypeEquals(HInstruction* other) const {
1456 UNUSED(other);
1457 return false;
1458 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001459
1460 // Returns whether any data encoded in the two instructions is equal.
1461 // This method does not look at the inputs. Both instructions must be
1462 // of the same type, otherwise the method has undefined behavior.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001463 virtual bool InstructionDataEquals(HInstruction* other) const {
1464 UNUSED(other);
1465 return false;
1466 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001467
1468 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00001469 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001470 // 2) Their inputs are identical.
1471 bool Equals(HInstruction* other) const;
1472
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001473 virtual InstructionKind GetKind() const = 0;
1474
1475 virtual size_t ComputeHashCode() const {
1476 size_t result = GetKind();
1477 for (size_t i = 0, e = InputCount(); i < e; ++i) {
1478 result = (result * 31) + InputAt(i)->GetId();
1479 }
1480 return result;
1481 }
1482
1483 SideEffects GetSideEffects() const { return side_effects_; }
1484
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001485 size_t GetLifetimePosition() const { return lifetime_position_; }
1486 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
1487 LiveInterval* GetLiveInterval() const { return live_interval_; }
1488 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
1489 bool HasLiveInterval() const { return live_interval_ != nullptr; }
1490
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001491 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
1492
1493 // Returns whether the code generation of the instruction will require to have access
1494 // to the current method. Such instructions are:
1495 // (1): Instructions that require an environment, as calling the runtime requires
1496 // to walk the stack and have the current method stored at a specific stack address.
1497 // (2): Object literals like classes and strings, that are loaded from the dex cache
1498 // fields of the current method.
1499 bool NeedsCurrentMethod() const {
1500 return NeedsEnvironment() || IsLoadClass() || IsLoadString();
1501 }
1502
Nicolas Geoffray9437b782015-03-25 10:08:51 +00001503 virtual bool NeedsDexCache() const { return false; }
1504
David Brazdil1abb4192015-02-17 18:33:36 +00001505 protected:
1506 virtual const HUserRecord<HInstruction*> InputRecordAt(size_t i) const = 0;
1507 virtual void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) = 0;
1508
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001509 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001510 void RemoveEnvironmentUser(HUseListNode<HEnvironment*>* use_node) { env_uses_.Remove(use_node); }
1511
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001512 HInstruction* previous_;
1513 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001514 HBasicBlock* block_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001515
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001516 // An instruction gets an id when it is added to the graph.
1517 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01001518 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001519 int id_;
1520
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001521 // When doing liveness analysis, instructions that have uses get an SSA index.
1522 int ssa_index_;
1523
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001524 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00001525 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001526
1527 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00001528 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001529
Nicolas Geoffray39468442014-09-02 15:17:15 +01001530 // The environment associated with this instruction. Not null if the instruction
1531 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001532 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001533
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001534 // Set by the code generator.
1535 LocationSummary* locations_;
1536
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001537 // Set by the liveness analysis.
1538 LiveInterval* live_interval_;
1539
1540 // Set by the liveness analysis, this is the position in a linear
1541 // order of blocks where this instruction's live interval start.
1542 size_t lifetime_position_;
1543
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001544 const SideEffects side_effects_;
1545
Calin Juravleacf735c2015-02-12 15:25:22 +00001546 // TODO: for primitive types this should be marked as invalid.
1547 ReferenceTypeInfo reference_type_info_;
1548
David Brazdil1abb4192015-02-17 18:33:36 +00001549 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001550 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00001551 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001552 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001553 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001554
1555 DISALLOW_COPY_AND_ASSIGN(HInstruction);
1556};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001557std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001558
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001559class HInputIterator : public ValueObject {
1560 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001561 explicit HInputIterator(HInstruction* instruction) : instruction_(instruction), index_(0) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001562
1563 bool Done() const { return index_ == instruction_->InputCount(); }
1564 HInstruction* Current() const { return instruction_->InputAt(index_); }
1565 void Advance() { index_++; }
1566
1567 private:
1568 HInstruction* instruction_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001569 size_t index_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001570
1571 DISALLOW_COPY_AND_ASSIGN(HInputIterator);
1572};
1573
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001574class HInstructionIterator : public ValueObject {
1575 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001576 explicit HInstructionIterator(const HInstructionList& instructions)
1577 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001578 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001579 }
1580
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001581 bool Done() const { return instruction_ == nullptr; }
1582 HInstruction* Current() const { return instruction_; }
1583 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001584 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001585 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001586 }
1587
1588 private:
1589 HInstruction* instruction_;
1590 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001591
1592 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001593};
1594
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001595class HBackwardInstructionIterator : public ValueObject {
1596 public:
1597 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
1598 : instruction_(instructions.last_instruction_) {
1599 next_ = Done() ? nullptr : instruction_->GetPrevious();
1600 }
1601
1602 bool Done() const { return instruction_ == nullptr; }
1603 HInstruction* Current() const { return instruction_; }
1604 void Advance() {
1605 instruction_ = next_;
1606 next_ = Done() ? nullptr : instruction_->GetPrevious();
1607 }
1608
1609 private:
1610 HInstruction* instruction_;
1611 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001612
1613 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001614};
1615
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001616// An embedded container with N elements of type T. Used (with partial
1617// specialization for N=0) because embedded arrays cannot have size 0.
1618template<typename T, intptr_t N>
1619class EmbeddedArray {
1620 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001621 EmbeddedArray() : elements_() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001622
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001623 intptr_t GetLength() const { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001624
1625 const T& operator[](intptr_t i) const {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001626 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001627 return elements_[i];
1628 }
1629
1630 T& operator[](intptr_t i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001631 DCHECK_LT(i, GetLength());
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001632 return elements_[i];
1633 }
1634
1635 const T& At(intptr_t i) const {
1636 return (*this)[i];
1637 }
1638
1639 void SetAt(intptr_t i, const T& val) {
1640 (*this)[i] = val;
1641 }
1642
1643 private:
1644 T elements_[N];
1645};
1646
1647template<typename T>
1648class EmbeddedArray<T, 0> {
1649 public:
1650 intptr_t length() const { return 0; }
1651 const T& operator[](intptr_t i) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001652 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001653 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001654 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001655 }
1656 T& operator[](intptr_t i) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001657 UNUSED(i);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001658 LOG(FATAL) << "Unreachable";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001659 UNREACHABLE();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001660 }
1661};
1662
1663template<intptr_t N>
1664class HTemplateInstruction: public HInstruction {
1665 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001666 HTemplateInstruction<N>(SideEffects side_effects)
1667 : HInstruction(side_effects), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07001668 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001669
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001670 size_t InputCount() const OVERRIDE { return N; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001671
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001672 protected:
David Brazdil1abb4192015-02-17 18:33:36 +00001673 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_[i]; }
1674
1675 void SetRawInputRecordAt(size_t i, const HUserRecord<HInstruction*>& input) OVERRIDE {
1676 inputs_[i] = input;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001677 }
1678
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001679 private:
David Brazdil1abb4192015-02-17 18:33:36 +00001680 EmbeddedArray<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001681
1682 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001683};
1684
Dave Allison20dfc792014-06-16 20:44:29 -07001685template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001686class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07001687 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001688 HExpression<N>(Primitive::Type type, SideEffects side_effects)
1689 : HTemplateInstruction<N>(side_effects), type_(type) {}
Dave Allison20dfc792014-06-16 20:44:29 -07001690 virtual ~HExpression() {}
1691
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001692 Primitive::Type GetType() const OVERRIDE { return type_; }
Dave Allison20dfc792014-06-16 20:44:29 -07001693
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001694 protected:
1695 Primitive::Type type_;
Dave Allison20dfc792014-06-16 20:44:29 -07001696};
1697
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001698// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
1699// instruction that branches to the exit block.
1700class HReturnVoid : public HTemplateInstruction<0> {
1701 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001702 HReturnVoid() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001703
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001704 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001705
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001706 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001707
1708 private:
1709 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
1710};
1711
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001712// Represents dex's RETURN opcodes. A HReturn is a control flow
1713// instruction that branches to the exit block.
1714class HReturn : public HTemplateInstruction<1> {
1715 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001716 explicit HReturn(HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001717 SetRawInputAt(0, value);
1718 }
1719
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001720 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001721
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001722 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001723
1724 private:
1725 DISALLOW_COPY_AND_ASSIGN(HReturn);
1726};
1727
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001728// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001729// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001730// exit block.
1731class HExit : public HTemplateInstruction<0> {
1732 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001733 HExit() : HTemplateInstruction(SideEffects::None()) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001734
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001735 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001736
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001737 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001738
1739 private:
1740 DISALLOW_COPY_AND_ASSIGN(HExit);
1741};
1742
1743// Jumps from one block to another.
1744class HGoto : public HTemplateInstruction<0> {
1745 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001746 HGoto() : HTemplateInstruction(SideEffects::None()) {}
1747
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001748 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001749
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001750 HBasicBlock* GetSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001751 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001752 }
1753
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001754 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001755
1756 private:
1757 DISALLOW_COPY_AND_ASSIGN(HGoto);
1758};
1759
Dave Allison20dfc792014-06-16 20:44:29 -07001760
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001761// Conditional branch. A block ending with an HIf instruction must have
1762// two successors.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001763class HIf : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001764 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001765 explicit HIf(HInstruction* input) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001766 SetRawInputAt(0, input);
1767 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001768
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001769 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001770
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001771 HBasicBlock* IfTrueSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001772 return GetBlock()->GetSuccessors().Get(0);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001773 }
1774
1775 HBasicBlock* IfFalseSuccessor() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001776 return GetBlock()->GetSuccessors().Get(1);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001777 }
1778
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001779 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001780
1781 private:
1782 DISALLOW_COPY_AND_ASSIGN(HIf);
1783};
1784
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001785// Deoptimize to interpreter, upon checking a condition.
1786class HDeoptimize : public HTemplateInstruction<1> {
1787 public:
1788 HDeoptimize(HInstruction* cond, uint32_t dex_pc)
1789 : HTemplateInstruction(SideEffects::None()),
1790 dex_pc_(dex_pc) {
1791 SetRawInputAt(0, cond);
1792 }
1793
1794 bool NeedsEnvironment() const OVERRIDE { return true; }
1795 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001796 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001797
1798 DECLARE_INSTRUCTION(Deoptimize);
1799
1800 private:
1801 uint32_t dex_pc_;
1802
1803 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
1804};
1805
Roland Levillain88cb1752014-10-20 16:36:47 +01001806class HUnaryOperation : public HExpression<1> {
1807 public:
1808 HUnaryOperation(Primitive::Type result_type, HInstruction* input)
1809 : HExpression(result_type, SideEffects::None()) {
1810 SetRawInputAt(0, input);
1811 }
1812
1813 HInstruction* GetInput() const { return InputAt(0); }
1814 Primitive::Type GetResultType() const { return GetType(); }
1815
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001816 bool CanBeMoved() const OVERRIDE { return true; }
1817 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001818 UNUSED(other);
1819 return true;
1820 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001821
Roland Levillain9240d6a2014-10-20 16:47:04 +01001822 // Try to statically evaluate `operation` and return a HConstant
1823 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001824 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01001825 HConstant* TryStaticEvaluation() const;
1826
1827 // Apply this operation to `x`.
1828 virtual int32_t Evaluate(int32_t x) const = 0;
1829 virtual int64_t Evaluate(int64_t x) const = 0;
1830
Roland Levillain88cb1752014-10-20 16:36:47 +01001831 DECLARE_INSTRUCTION(UnaryOperation);
1832
1833 private:
1834 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
1835};
1836
Dave Allison20dfc792014-06-16 20:44:29 -07001837class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001838 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001839 HBinaryOperation(Primitive::Type result_type,
1840 HInstruction* left,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001841 HInstruction* right) : HExpression(result_type, SideEffects::None()) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001842 SetRawInputAt(0, left);
1843 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001844 }
1845
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001846 HInstruction* GetLeft() const { return InputAt(0); }
1847 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07001848 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001849
Mingyao Yangdc5ac732015-02-25 11:28:05 -08001850 virtual bool IsCommutative() const { return false; }
1851
1852 // Put constant on the right.
1853 // Returns whether order is changed.
1854 bool OrderInputsWithConstantOnTheRight() {
1855 HInstruction* left = InputAt(0);
1856 HInstruction* right = InputAt(1);
1857 if (left->IsConstant() && !right->IsConstant()) {
1858 ReplaceInput(right, 0);
1859 ReplaceInput(left, 1);
1860 return true;
1861 }
1862 return false;
1863 }
1864
1865 // Order inputs by instruction id, but favor constant on the right side.
1866 // This helps GVN for commutative ops.
1867 void OrderInputs() {
1868 DCHECK(IsCommutative());
1869 HInstruction* left = InputAt(0);
1870 HInstruction* right = InputAt(1);
1871 if (left == right || (!left->IsConstant() && right->IsConstant())) {
1872 return;
1873 }
1874 if (OrderInputsWithConstantOnTheRight()) {
1875 return;
1876 }
1877 // Order according to instruction id.
1878 if (left->GetId() > right->GetId()) {
1879 ReplaceInput(right, 0);
1880 ReplaceInput(left, 1);
1881 }
1882 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001883
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001884 bool CanBeMoved() const OVERRIDE { return true; }
1885 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001886 UNUSED(other);
1887 return true;
1888 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001889
Roland Levillain9240d6a2014-10-20 16:47:04 +01001890 // Try to statically evaluate `operation` and return a HConstant
Roland Levillain556c3d12014-09-18 15:25:07 +01001891 // containing the result of this evaluation. If `operation` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001892 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01001893 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01001894
1895 // Apply this operation to `x` and `y`.
1896 virtual int32_t Evaluate(int32_t x, int32_t y) const = 0;
1897 virtual int64_t Evaluate(int64_t x, int64_t y) const = 0;
1898
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001899 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001900 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001901 HConstant* GetConstantRight() const;
1902
1903 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001904 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00001905 HInstruction* GetLeastConstantLeft() const;
1906
Roland Levillainccc07a92014-09-16 14:48:16 +01001907 DECLARE_INSTRUCTION(BinaryOperation);
1908
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001909 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001910 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
1911};
1912
Dave Allison20dfc792014-06-16 20:44:29 -07001913class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001914 public:
Dave Allison20dfc792014-06-16 20:44:29 -07001915 HCondition(HInstruction* first, HInstruction* second)
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001916 : HBinaryOperation(Primitive::kPrimBoolean, first, second),
1917 needs_materialization_(true) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001918
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001919 bool NeedsMaterialization() const { return needs_materialization_; }
1920 void ClearNeedsMaterialization() { needs_materialization_ = false; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00001921
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001922 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001923 // `instruction`, and disregard moves in between.
1924 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001925
Dave Allison20dfc792014-06-16 20:44:29 -07001926 DECLARE_INSTRUCTION(Condition);
1927
1928 virtual IfCondition GetCondition() const = 0;
1929
1930 private:
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001931 // For register allocation purposes, returns whether this instruction needs to be
1932 // materialized (that is, not just be in the processor flags).
1933 bool needs_materialization_;
1934
Dave Allison20dfc792014-06-16 20:44:29 -07001935 DISALLOW_COPY_AND_ASSIGN(HCondition);
1936};
1937
1938// Instruction to check if two inputs are equal to each other.
1939class HEqual : public HCondition {
1940 public:
1941 HEqual(HInstruction* first, HInstruction* second)
1942 : HCondition(first, second) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001943
Mingyao Yangdc5ac732015-02-25 11:28:05 -08001944 bool IsCommutative() const OVERRIDE { return true; }
1945
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001946 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001947 return x == y ? 1 : 0;
1948 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001949 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001950 return x == y ? 1 : 0;
1951 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001952
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001953 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001954
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001955 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001956 return kCondEQ;
1957 }
1958
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001959 private:
1960 DISALLOW_COPY_AND_ASSIGN(HEqual);
1961};
1962
Dave Allison20dfc792014-06-16 20:44:29 -07001963class HNotEqual : public HCondition {
1964 public:
1965 HNotEqual(HInstruction* first, HInstruction* second)
1966 : HCondition(first, second) {}
1967
Mingyao Yangdc5ac732015-02-25 11:28:05 -08001968 bool IsCommutative() const OVERRIDE { return true; }
1969
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001970 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001971 return x != y ? 1 : 0;
1972 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001973 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001974 return x != y ? 1 : 0;
1975 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001976
Dave Allison20dfc792014-06-16 20:44:29 -07001977 DECLARE_INSTRUCTION(NotEqual);
1978
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001979 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07001980 return kCondNE;
1981 }
1982
1983 private:
1984 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
1985};
1986
1987class HLessThan : public HCondition {
1988 public:
1989 HLessThan(HInstruction* first, HInstruction* second)
1990 : HCondition(first, second) {}
1991
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001992 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001993 return x < y ? 1 : 0;
1994 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001995 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01001996 return x < y ? 1 : 0;
1997 }
Roland Levillain556c3d12014-09-18 15:25:07 +01001998
Dave Allison20dfc792014-06-16 20:44:29 -07001999 DECLARE_INSTRUCTION(LessThan);
2000
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002001 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002002 return kCondLT;
2003 }
2004
2005 private:
2006 DISALLOW_COPY_AND_ASSIGN(HLessThan);
2007};
2008
2009class HLessThanOrEqual : public HCondition {
2010 public:
2011 HLessThanOrEqual(HInstruction* first, HInstruction* second)
2012 : HCondition(first, second) {}
2013
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002014 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002015 return x <= y ? 1 : 0;
2016 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002017 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002018 return x <= y ? 1 : 0;
2019 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002020
Dave Allison20dfc792014-06-16 20:44:29 -07002021 DECLARE_INSTRUCTION(LessThanOrEqual);
2022
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002023 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002024 return kCondLE;
2025 }
2026
2027 private:
2028 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
2029};
2030
2031class HGreaterThan : public HCondition {
2032 public:
2033 HGreaterThan(HInstruction* first, HInstruction* second)
2034 : HCondition(first, second) {}
2035
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002036 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002037 return x > y ? 1 : 0;
2038 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002039 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002040 return x > y ? 1 : 0;
2041 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002042
Dave Allison20dfc792014-06-16 20:44:29 -07002043 DECLARE_INSTRUCTION(GreaterThan);
2044
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002045 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002046 return kCondGT;
2047 }
2048
2049 private:
2050 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
2051};
2052
2053class HGreaterThanOrEqual : public HCondition {
2054 public:
2055 HGreaterThanOrEqual(HInstruction* first, HInstruction* second)
2056 : HCondition(first, second) {}
2057
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002058 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002059 return x >= y ? 1 : 0;
2060 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002061 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002062 return x >= y ? 1 : 0;
2063 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002064
Dave Allison20dfc792014-06-16 20:44:29 -07002065 DECLARE_INSTRUCTION(GreaterThanOrEqual);
2066
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002067 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07002068 return kCondGE;
2069 }
2070
2071 private:
2072 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
2073};
2074
2075
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002076// Instruction to check how two inputs compare to each other.
2077// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
2078class HCompare : public HBinaryOperation {
2079 public:
Calin Juravleddb7df22014-11-25 20:56:51 +00002080 // The bias applies for floating point operations and indicates how NaN
2081 // comparisons are treated:
2082 enum Bias {
2083 kNoBias, // bias is not applicable (i.e. for long operation)
2084 kGtBias, // return 1 for NaN comparisons
2085 kLtBias, // return -1 for NaN comparisons
2086 };
2087
2088 HCompare(Primitive::Type type, HInstruction* first, HInstruction* second, Bias bias)
2089 : HBinaryOperation(Primitive::kPrimInt, first, second), bias_(bias) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002090 DCHECK_EQ(type, first->GetType());
2091 DCHECK_EQ(type, second->GetType());
2092 }
2093
Calin Juravleddb7df22014-11-25 20:56:51 +00002094 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01002095 return
2096 x == y ? 0 :
2097 x > y ? 1 :
2098 -1;
2099 }
Calin Juravleddb7df22014-11-25 20:56:51 +00002100
2101 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain556c3d12014-09-18 15:25:07 +01002102 return
2103 x == y ? 0 :
2104 x > y ? 1 :
2105 -1;
2106 }
2107
Calin Juravleddb7df22014-11-25 20:56:51 +00002108 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2109 return bias_ == other->AsCompare()->bias_;
2110 }
2111
2112 bool IsGtBias() { return bias_ == kGtBias; }
2113
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002114 DECLARE_INSTRUCTION(Compare);
2115
2116 private:
Calin Juravleddb7df22014-11-25 20:56:51 +00002117 const Bias bias_;
2118
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01002119 DISALLOW_COPY_AND_ASSIGN(HCompare);
2120};
2121
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002122// A local in the graph. Corresponds to a Dex register.
2123class HLocal : public HTemplateInstruction<0> {
2124 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002125 explicit HLocal(uint16_t reg_number)
2126 : HTemplateInstruction(SideEffects::None()), reg_number_(reg_number) {}
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002127
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002128 DECLARE_INSTRUCTION(Local);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002129
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002130 uint16_t GetRegNumber() const { return reg_number_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002131
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002132 private:
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002133 // The Dex register number.
2134 const uint16_t reg_number_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002135
2136 DISALLOW_COPY_AND_ASSIGN(HLocal);
2137};
2138
2139// Load a given local. The local is an input of this instruction.
Dave Allison20dfc792014-06-16 20:44:29 -07002140class HLoadLocal : public HExpression<1> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002141 public:
Roland Levillain5799fc02014-09-25 12:15:20 +01002142 HLoadLocal(HLocal* local, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002143 : HExpression(type, SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002144 SetRawInputAt(0, local);
2145 }
2146
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002147 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
2148
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002149 DECLARE_INSTRUCTION(LoadLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002150
2151 private:
2152 DISALLOW_COPY_AND_ASSIGN(HLoadLocal);
2153};
2154
2155// Store a value in a given local. This instruction has two inputs: the value
2156// and the local.
2157class HStoreLocal : public HTemplateInstruction<2> {
2158 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002159 HStoreLocal(HLocal* local, HInstruction* value) : HTemplateInstruction(SideEffects::None()) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002160 SetRawInputAt(0, local);
2161 SetRawInputAt(1, value);
2162 }
2163
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002164 HLocal* GetLocal() const { return reinterpret_cast<HLocal*>(InputAt(0)); }
2165
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002166 DECLARE_INSTRUCTION(StoreLocal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002167
2168 private:
2169 DISALLOW_COPY_AND_ASSIGN(HStoreLocal);
2170};
2171
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002172class HConstant : public HExpression<0> {
2173 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002174 explicit HConstant(Primitive::Type type) : HExpression(type, SideEffects::None()) {}
2175
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002176 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002177
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002178 virtual bool IsMinusOne() const { return false; }
2179 virtual bool IsZero() const { return false; }
2180 virtual bool IsOne() const { return false; }
2181
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002182 DECLARE_INSTRUCTION(Constant);
2183
2184 private:
2185 DISALLOW_COPY_AND_ASSIGN(HConstant);
2186};
2187
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002188class HFloatConstant : public HConstant {
2189 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002190 float GetValue() const { return value_; }
2191
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002192 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002193 return bit_cast<uint32_t, float>(other->AsFloatConstant()->value_) ==
2194 bit_cast<uint32_t, float>(value_);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002195 }
2196
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002197 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002198
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002199 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002200 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002201 }
2202 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002203 return value_ == 0.0f;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002204 }
2205 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002206 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2207 }
2208 bool IsNaN() const {
2209 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002210 }
2211
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002212 DECLARE_INSTRUCTION(FloatConstant);
2213
2214 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002215 explicit HFloatConstant(float value) : HConstant(Primitive::kPrimFloat), value_(value) {}
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002216 explicit HFloatConstant(int32_t value)
2217 : HConstant(Primitive::kPrimFloat), value_(bit_cast<float, int32_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00002218
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002219 const float value_;
2220
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002221 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00002222 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002223 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002224 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2225};
2226
2227class HDoubleConstant : public HConstant {
2228 public:
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002229 double GetValue() const { return value_; }
2230
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002231 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Roland Levillainda4d79b2015-03-24 14:36:11 +00002232 return bit_cast<uint64_t, double>(other->AsDoubleConstant()->value_) ==
2233 bit_cast<uint64_t, double>(value_);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002234 }
2235
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002236 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002237
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002238 bool IsMinusOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002239 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002240 }
2241 bool IsZero() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002242 return value_ == 0.0;
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002243 }
2244 bool IsOne() const OVERRIDE {
Roland Levillain3b55ebb2015-05-08 13:13:19 +01002245 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
2246 }
2247 bool IsNaN() const {
2248 return std::isnan(value_);
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002249 }
2250
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002251 DECLARE_INSTRUCTION(DoubleConstant);
2252
2253 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002254 explicit HDoubleConstant(double value) : HConstant(Primitive::kPrimDouble), value_(value) {}
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002255 explicit HDoubleConstant(int64_t value)
2256 : HConstant(Primitive::kPrimDouble), value_(bit_cast<double, int64_t>(value)) {}
David Brazdil8d5b8b22015-03-24 10:51:52 +00002257
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002258 const double value_;
2259
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002260 // Only the SsaBuilder and HGraph can create floating-point constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +00002261 friend class SsaBuilder;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +00002262 friend class HGraph;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002263 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2264};
2265
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002266class HNullConstant : public HConstant {
2267 public:
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002268 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
2269 return true;
2270 }
2271
2272 size_t ComputeHashCode() const OVERRIDE { return 0; }
2273
2274 DECLARE_INSTRUCTION(NullConstant);
2275
2276 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002277 HNullConstant() : HConstant(Primitive::kPrimNot) {}
2278
2279 friend class HGraph;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002280 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2281};
2282
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002283// Constants of the type int. Those can be from Dex instructions, or
2284// synthesized (for example with the if-eqz instruction).
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002285class HIntConstant : public HConstant {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002286 public:
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002287 int32_t GetValue() const { return value_; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002288
Calin Juravle61d544b2015-02-23 16:46:57 +00002289 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002290 return other->AsIntConstant()->value_ == value_;
2291 }
2292
Calin Juravle61d544b2015-02-23 16:46:57 +00002293 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2294
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002295 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2296 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2297 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2298
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002299 DECLARE_INSTRUCTION(IntConstant);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002300
2301 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002302 explicit HIntConstant(int32_t value) : HConstant(Primitive::kPrimInt), value_(value) {}
2303
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002304 const int32_t value_;
2305
David Brazdil8d5b8b22015-03-24 10:51:52 +00002306 friend class HGraph;
2307 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
Zheng Xuad4450e2015-04-17 18:48:56 +08002308 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002309 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2310};
2311
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002312class HLongConstant : public HConstant {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002313 public:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002314 int64_t GetValue() const { return value_; }
2315
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002316 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002317 return other->AsLongConstant()->value_ == value_;
2318 }
2319
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002320 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002321
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00002322 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
2323 bool IsZero() const OVERRIDE { return GetValue() == 0; }
2324 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2325
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002326 DECLARE_INSTRUCTION(LongConstant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002327
2328 private:
David Brazdil8d5b8b22015-03-24 10:51:52 +00002329 explicit HLongConstant(int64_t value) : HConstant(Primitive::kPrimLong), value_(value) {}
2330
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002331 const int64_t value_;
2332
David Brazdil8d5b8b22015-03-24 10:51:52 +00002333 friend class HGraph;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002334 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2335};
2336
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002337enum class Intrinsics {
2338#define OPTIMIZING_INTRINSICS(Name, IsStatic) k ## Name,
2339#include "intrinsics_list.h"
2340 kNone,
2341 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
2342#undef INTRINSICS_LIST
2343#undef OPTIMIZING_INTRINSICS
2344};
2345std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic);
2346
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002347class HInvoke : public HInstruction {
2348 public:
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002349 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002350
2351 // Runtime needs to walk the stack, so Dex -> Dex calls need to
2352 // know their environment.
Calin Juravle77520bc2015-01-12 18:45:46 +00002353 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002354
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01002355 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002356 SetRawInputAt(index, argument);
2357 }
2358
Roland Levillain3e3d7332015-04-28 11:00:54 +01002359 // Return the number of arguments. This number can be lower than
2360 // the number of inputs returned by InputCount(), as some invoke
2361 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
2362 // inputs at the end of their list of inputs.
2363 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
2364
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002365 Primitive::Type GetType() const OVERRIDE { return return_type_; }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002366
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002367 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002368
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002369 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
2370
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01002371 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002372 return intrinsic_;
2373 }
2374
2375 void SetIntrinsic(Intrinsics intrinsic) {
2376 intrinsic_ = intrinsic;
2377 }
2378
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002379 DECLARE_INSTRUCTION(Invoke);
2380
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002381 protected:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002382 HInvoke(ArenaAllocator* arena,
2383 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01002384 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002385 Primitive::Type return_type,
2386 uint32_t dex_pc,
2387 uint32_t dex_method_index)
2388 : HInstruction(SideEffects::All()),
Roland Levillain3e3d7332015-04-28 11:00:54 +01002389 number_of_arguments_(number_of_arguments),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002390 inputs_(arena, number_of_arguments),
2391 return_type_(return_type),
2392 dex_pc_(dex_pc),
2393 dex_method_index_(dex_method_index),
2394 intrinsic_(Intrinsics::kNone) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01002395 uint32_t number_of_inputs = number_of_arguments + number_of_other_inputs;
2396 inputs_.SetSize(number_of_inputs);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002397 }
2398
David Brazdil1abb4192015-02-17 18:33:36 +00002399 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
2400 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
2401 inputs_.Put(index, input);
2402 }
2403
Roland Levillain3e3d7332015-04-28 11:00:54 +01002404 uint32_t number_of_arguments_;
David Brazdil1abb4192015-02-17 18:33:36 +00002405 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002406 const Primitive::Type return_type_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002407 const uint32_t dex_pc_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002408 const uint32_t dex_method_index_;
2409 Intrinsics intrinsic_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002410
2411 private:
2412 DISALLOW_COPY_AND_ASSIGN(HInvoke);
2413};
2414
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002415class HInvokeStaticOrDirect : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002416 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01002417 // Requirements of this method call regarding the class
2418 // initialization (clinit) check of its declaring class.
2419 enum class ClinitCheckRequirement {
2420 kNone, // Class already initialized.
2421 kExplicit, // Static call having explicit clinit check as last input.
2422 kImplicit, // Static call implicitly requiring a clinit check.
2423 };
2424
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002425 HInvokeStaticOrDirect(ArenaAllocator* arena,
2426 uint32_t number_of_arguments,
2427 Primitive::Type return_type,
2428 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002429 uint32_t dex_method_index,
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002430 bool is_recursive,
Jeff Hao848f70a2014-01-15 13:49:50 -08002431 int32_t string_init_offset,
Nicolas Geoffray79041292015-03-26 10:05:54 +00002432 InvokeType original_invoke_type,
Roland Levillain4c0eb422015-04-24 16:43:49 +01002433 InvokeType invoke_type,
2434 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01002435 : HInvoke(arena,
2436 number_of_arguments,
2437 clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u,
2438 return_type,
2439 dex_pc,
2440 dex_method_index),
Nicolas Geoffray79041292015-03-26 10:05:54 +00002441 original_invoke_type_(original_invoke_type),
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002442 invoke_type_(invoke_type),
Roland Levillain4c0eb422015-04-24 16:43:49 +01002443 is_recursive_(is_recursive),
Jeff Hao848f70a2014-01-15 13:49:50 -08002444 clinit_check_requirement_(clinit_check_requirement),
2445 string_init_offset_(string_init_offset) {}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002446
Calin Juravle641547a2015-04-21 22:08:51 +01002447 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
2448 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00002449 // We access the method via the dex cache so we can't do an implicit null check.
2450 // TODO: for intrinsics we can generate implicit null checks.
2451 return false;
2452 }
2453
Nicolas Geoffray79041292015-03-26 10:05:54 +00002454 InvokeType GetOriginalInvokeType() const { return original_invoke_type_; }
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002455 InvokeType GetInvokeType() const { return invoke_type_; }
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002456 bool IsRecursive() const { return is_recursive_; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002457 bool NeedsDexCache() const OVERRIDE { return !IsRecursive(); }
Jeff Hao848f70a2014-01-15 13:49:50 -08002458 bool IsStringInit() const { return string_init_offset_ != 0; }
2459 int32_t GetStringInitOffset() const { return string_init_offset_; }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002460
Roland Levillain4c0eb422015-04-24 16:43:49 +01002461 // Is this instruction a call to a static method?
2462 bool IsStatic() const {
2463 return GetInvokeType() == kStatic;
2464 }
2465
Roland Levillain3e3d7332015-04-28 11:00:54 +01002466 // Remove the art::HLoadClass instruction set as last input by
2467 // art::PrepareForRegisterAllocation::VisitClinitCheck in lieu of
2468 // the initial art::HClinitCheck instruction (only relevant for
2469 // static calls with explicit clinit check).
2470 void RemoveLoadClassAsLastInput() {
Roland Levillain4c0eb422015-04-24 16:43:49 +01002471 DCHECK(IsStaticWithExplicitClinitCheck());
2472 size_t last_input_index = InputCount() - 1;
2473 HInstruction* last_input = InputAt(last_input_index);
2474 DCHECK(last_input != nullptr);
Roland Levillain3e3d7332015-04-28 11:00:54 +01002475 DCHECK(last_input->IsLoadClass()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01002476 RemoveAsUserOfInput(last_input_index);
2477 inputs_.DeleteAt(last_input_index);
2478 clinit_check_requirement_ = ClinitCheckRequirement::kImplicit;
2479 DCHECK(IsStaticWithImplicitClinitCheck());
2480 }
2481
2482 // Is this a call to a static method whose declaring class has an
2483 // explicit intialization check in the graph?
2484 bool IsStaticWithExplicitClinitCheck() const {
2485 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kExplicit);
2486 }
2487
2488 // Is this a call to a static method whose declaring class has an
2489 // implicit intialization check requirement?
2490 bool IsStaticWithImplicitClinitCheck() const {
2491 return IsStatic() && (clinit_check_requirement_ == ClinitCheckRequirement::kImplicit);
2492 }
2493
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002494 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002495
Roland Levillain4c0eb422015-04-24 16:43:49 +01002496 protected:
2497 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE {
2498 const HUserRecord<HInstruction*> input_record = HInvoke::InputRecordAt(i);
2499 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck() && (i == InputCount() - 1)) {
2500 HInstruction* input = input_record.GetInstruction();
2501 // `input` is the last input of a static invoke marked as having
2502 // an explicit clinit check. It must either be:
2503 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
2504 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
2505 DCHECK(input != nullptr);
2506 DCHECK(input->IsClinitCheck() || input->IsLoadClass()) << input->DebugName();
2507 }
2508 return input_record;
2509 }
2510
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002511 private:
Nicolas Geoffray79041292015-03-26 10:05:54 +00002512 const InvokeType original_invoke_type_;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002513 const InvokeType invoke_type_;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002514 const bool is_recursive_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01002515 ClinitCheckRequirement clinit_check_requirement_;
Jeff Hao848f70a2014-01-15 13:49:50 -08002516 // Thread entrypoint offset for string init method if this is a string init invoke.
2517 // Note that there are multiple string init methods, each having its own offset.
2518 int32_t string_init_offset_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002519
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002520 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002521};
2522
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002523class HInvokeVirtual : public HInvoke {
2524 public:
2525 HInvokeVirtual(ArenaAllocator* arena,
2526 uint32_t number_of_arguments,
2527 Primitive::Type return_type,
2528 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002529 uint32_t dex_method_index,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002530 uint32_t vtable_index)
Roland Levillain3e3d7332015-04-28 11:00:54 +01002531 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002532 vtable_index_(vtable_index) {}
2533
Calin Juravle641547a2015-04-21 22:08:51 +01002534 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00002535 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01002536 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00002537 }
2538
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002539 uint32_t GetVTableIndex() const { return vtable_index_; }
2540
2541 DECLARE_INSTRUCTION(InvokeVirtual);
2542
2543 private:
2544 const uint32_t vtable_index_;
2545
2546 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
2547};
2548
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002549class HInvokeInterface : public HInvoke {
2550 public:
2551 HInvokeInterface(ArenaAllocator* arena,
2552 uint32_t number_of_arguments,
2553 Primitive::Type return_type,
2554 uint32_t dex_pc,
2555 uint32_t dex_method_index,
2556 uint32_t imt_index)
Roland Levillain3e3d7332015-04-28 11:00:54 +01002557 : HInvoke(arena, number_of_arguments, 0u, return_type, dex_pc, dex_method_index),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002558 imt_index_(imt_index) {}
2559
Calin Juravle641547a2015-04-21 22:08:51 +01002560 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00002561 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01002562 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00002563 }
2564
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002565 uint32_t GetImtIndex() const { return imt_index_; }
2566 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
2567
2568 DECLARE_INSTRUCTION(InvokeInterface);
2569
2570 private:
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002571 const uint32_t imt_index_;
2572
2573 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
2574};
2575
Dave Allison20dfc792014-06-16 20:44:29 -07002576class HNewInstance : public HExpression<0> {
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002577 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002578 HNewInstance(uint32_t dex_pc, uint16_t type_index, QuickEntrypointEnum entrypoint)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002579 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2580 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002581 type_index_(type_index),
2582 entrypoint_(entrypoint) {}
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002583
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002584 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002585 uint16_t GetTypeIndex() const { return type_index_; }
2586
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002587 // Calls runtime so needs an environment.
Calin Juravle92a6ed22014-12-02 18:58:03 +00002588 bool NeedsEnvironment() const OVERRIDE { return true; }
2589 // It may throw when called on:
2590 // - interfaces
2591 // - abstract/innaccessible/unknown classes
2592 // TODO: optimize when possible.
2593 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002594
Calin Juravle10e244f2015-01-26 18:54:32 +00002595 bool CanBeNull() const OVERRIDE { return false; }
2596
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002597 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
2598
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002599 DECLARE_INSTRUCTION(NewInstance);
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002600
2601 private:
2602 const uint32_t dex_pc_;
2603 const uint16_t type_index_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002604 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01002605
2606 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
2607};
2608
Roland Levillain88cb1752014-10-20 16:36:47 +01002609class HNeg : public HUnaryOperation {
2610 public:
2611 explicit HNeg(Primitive::Type result_type, HInstruction* input)
2612 : HUnaryOperation(result_type, input) {}
2613
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002614 int32_t Evaluate(int32_t x) const OVERRIDE { return -x; }
2615 int64_t Evaluate(int64_t x) const OVERRIDE { return -x; }
Roland Levillain9240d6a2014-10-20 16:47:04 +01002616
Roland Levillain88cb1752014-10-20 16:36:47 +01002617 DECLARE_INSTRUCTION(Neg);
2618
2619 private:
2620 DISALLOW_COPY_AND_ASSIGN(HNeg);
2621};
2622
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002623class HNewArray : public HExpression<1> {
2624 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002625 HNewArray(HInstruction* length,
2626 uint32_t dex_pc,
2627 uint16_t type_index,
2628 QuickEntrypointEnum entrypoint)
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002629 : HExpression(Primitive::kPrimNot, SideEffects::None()),
2630 dex_pc_(dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002631 type_index_(type_index),
2632 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002633 SetRawInputAt(0, length);
2634 }
2635
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002636 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002637 uint16_t GetTypeIndex() const { return type_index_; }
2638
2639 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00002640 bool NeedsEnvironment() const OVERRIDE { return true; }
2641
Mingyao Yang0c365e62015-03-31 15:09:29 -07002642 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
2643 bool CanThrow() const OVERRIDE { return true; }
2644
Calin Juravle10e244f2015-01-26 18:54:32 +00002645 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002646
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002647 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
2648
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002649 DECLARE_INSTRUCTION(NewArray);
2650
2651 private:
2652 const uint32_t dex_pc_;
2653 const uint16_t type_index_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00002654 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01002655
2656 DISALLOW_COPY_AND_ASSIGN(HNewArray);
2657};
2658
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002659class HAdd : public HBinaryOperation {
2660 public:
2661 HAdd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2662 : HBinaryOperation(result_type, left, right) {}
2663
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002664 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002665
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002666 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002667 return x + y;
2668 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002669 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002670 return x + y;
2671 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002672
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002673 DECLARE_INSTRUCTION(Add);
2674
2675 private:
2676 DISALLOW_COPY_AND_ASSIGN(HAdd);
2677};
2678
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002679class HSub : public HBinaryOperation {
2680 public:
2681 HSub(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2682 : HBinaryOperation(result_type, left, right) {}
2683
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002684 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002685 return x - y;
2686 }
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002687 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Roland Levillain93445682014-10-06 19:24:02 +01002688 return x - y;
2689 }
Roland Levillain556c3d12014-09-18 15:25:07 +01002690
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002691 DECLARE_INSTRUCTION(Sub);
2692
2693 private:
2694 DISALLOW_COPY_AND_ASSIGN(HSub);
2695};
2696
Calin Juravle34bacdf2014-10-07 20:23:36 +01002697class HMul : public HBinaryOperation {
2698 public:
2699 HMul(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2700 : HBinaryOperation(result_type, left, right) {}
2701
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002702 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002703
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002704 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x * y; }
2705 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x * y; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002706
2707 DECLARE_INSTRUCTION(Mul);
2708
2709 private:
2710 DISALLOW_COPY_AND_ASSIGN(HMul);
2711};
2712
Calin Juravle7c4954d2014-10-28 16:57:40 +00002713class HDiv : public HBinaryOperation {
2714 public:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002715 HDiv(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
2716 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00002717
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002718 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00002719 // Our graph structure ensures we never have 0 for `y` during constant folding.
2720 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00002721 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00002722 return (y == -1) ? -x : x / y;
2723 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002724
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002725 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002726 DCHECK_NE(y, 0);
2727 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2728 return (y == -1) ? -x : x / y;
2729 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00002730
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002731 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002732
Calin Juravle7c4954d2014-10-28 16:57:40 +00002733 DECLARE_INSTRUCTION(Div);
2734
2735 private:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002736 const uint32_t dex_pc_;
2737
Calin Juravle7c4954d2014-10-28 16:57:40 +00002738 DISALLOW_COPY_AND_ASSIGN(HDiv);
2739};
2740
Calin Juravlebacfec32014-11-14 15:54:36 +00002741class HRem : public HBinaryOperation {
2742 public:
2743 HRem(Primitive::Type result_type, HInstruction* left, HInstruction* right, uint32_t dex_pc)
2744 : HBinaryOperation(result_type, left, right), dex_pc_(dex_pc) {}
2745
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002746 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002747 DCHECK_NE(y, 0);
2748 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2749 return (y == -1) ? 0 : x % y;
2750 }
2751
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002752 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
Calin Juravlebacfec32014-11-14 15:54:36 +00002753 DCHECK_NE(y, 0);
2754 // Special case -1 to avoid getting a SIGFPE on x86(_64).
2755 return (y == -1) ? 0 : x % y;
2756 }
2757
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002758 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Calin Juravlebacfec32014-11-14 15:54:36 +00002759
2760 DECLARE_INSTRUCTION(Rem);
2761
2762 private:
2763 const uint32_t dex_pc_;
2764
2765 DISALLOW_COPY_AND_ASSIGN(HRem);
2766};
2767
Calin Juravled0d48522014-11-04 16:40:20 +00002768class HDivZeroCheck : public HExpression<1> {
2769 public:
2770 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
2771 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
2772 SetRawInputAt(0, value);
2773 }
2774
2775 bool CanBeMoved() const OVERRIDE { return true; }
2776
2777 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2778 UNUSED(other);
2779 return true;
2780 }
2781
2782 bool NeedsEnvironment() const OVERRIDE { return true; }
2783 bool CanThrow() const OVERRIDE { return true; }
2784
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002785 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Calin Juravled0d48522014-11-04 16:40:20 +00002786
2787 DECLARE_INSTRUCTION(DivZeroCheck);
2788
2789 private:
2790 const uint32_t dex_pc_;
2791
2792 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
2793};
2794
Calin Juravle9aec02f2014-11-18 23:06:35 +00002795class HShl : public HBinaryOperation {
2796 public:
2797 HShl(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2798 : HBinaryOperation(result_type, left, right) {}
2799
2800 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x << (y & kMaxIntShiftValue); }
2801 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x << (y & kMaxLongShiftValue); }
2802
2803 DECLARE_INSTRUCTION(Shl);
2804
2805 private:
2806 DISALLOW_COPY_AND_ASSIGN(HShl);
2807};
2808
2809class HShr : public HBinaryOperation {
2810 public:
2811 HShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2812 : HBinaryOperation(result_type, left, right) {}
2813
2814 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x >> (y & kMaxIntShiftValue); }
2815 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x >> (y & kMaxLongShiftValue); }
2816
2817 DECLARE_INSTRUCTION(Shr);
2818
2819 private:
2820 DISALLOW_COPY_AND_ASSIGN(HShr);
2821};
2822
2823class HUShr : public HBinaryOperation {
2824 public:
2825 HUShr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2826 : HBinaryOperation(result_type, left, right) {}
2827
2828 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE {
2829 uint32_t ux = static_cast<uint32_t>(x);
2830 uint32_t uy = static_cast<uint32_t>(y) & kMaxIntShiftValue;
2831 return static_cast<int32_t>(ux >> uy);
2832 }
2833
2834 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE {
2835 uint64_t ux = static_cast<uint64_t>(x);
2836 uint64_t uy = static_cast<uint64_t>(y) & kMaxLongShiftValue;
2837 return static_cast<int64_t>(ux >> uy);
2838 }
2839
2840 DECLARE_INSTRUCTION(UShr);
2841
2842 private:
2843 DISALLOW_COPY_AND_ASSIGN(HUShr);
2844};
2845
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002846class HAnd : public HBinaryOperation {
2847 public:
2848 HAnd(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2849 : HBinaryOperation(result_type, left, right) {}
2850
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002851 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002852
2853 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x & y; }
2854 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x & y; }
2855
2856 DECLARE_INSTRUCTION(And);
2857
2858 private:
2859 DISALLOW_COPY_AND_ASSIGN(HAnd);
2860};
2861
2862class HOr : public HBinaryOperation {
2863 public:
2864 HOr(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2865 : HBinaryOperation(result_type, left, right) {}
2866
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002867 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002868
2869 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x | y; }
2870 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x | y; }
2871
2872 DECLARE_INSTRUCTION(Or);
2873
2874 private:
2875 DISALLOW_COPY_AND_ASSIGN(HOr);
2876};
2877
2878class HXor : public HBinaryOperation {
2879 public:
2880 HXor(Primitive::Type result_type, HInstruction* left, HInstruction* right)
2881 : HBinaryOperation(result_type, left, right) {}
2882
Mingyao Yangdc5ac732015-02-25 11:28:05 -08002883 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002884
2885 int32_t Evaluate(int32_t x, int32_t y) const OVERRIDE { return x ^ y; }
2886 int64_t Evaluate(int64_t x, int64_t y) const OVERRIDE { return x ^ y; }
2887
2888 DECLARE_INSTRUCTION(Xor);
2889
2890 private:
2891 DISALLOW_COPY_AND_ASSIGN(HXor);
2892};
2893
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002894// The value of a parameter in this method. Its location depends on
2895// the calling convention.
Dave Allison20dfc792014-06-16 20:44:29 -07002896class HParameterValue : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002897 public:
Calin Juravle10e244f2015-01-26 18:54:32 +00002898 HParameterValue(uint8_t index, Primitive::Type parameter_type, bool is_this = false)
2899 : HExpression(parameter_type, SideEffects::None()), index_(index), is_this_(is_this) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002900
2901 uint8_t GetIndex() const { return index_; }
2902
Calin Juravle10e244f2015-01-26 18:54:32 +00002903 bool CanBeNull() const OVERRIDE { return !is_this_; }
2904
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002905 DECLARE_INSTRUCTION(ParameterValue);
2906
2907 private:
2908 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00002909 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002910 const uint8_t index_;
2911
Calin Juravle10e244f2015-01-26 18:54:32 +00002912 // Whether or not the parameter value corresponds to 'this' argument.
2913 const bool is_this_;
2914
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002915 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
2916};
2917
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002918class HNot : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002919 public:
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002920 explicit HNot(Primitive::Type result_type, HInstruction* input)
2921 : HUnaryOperation(result_type, input) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002922
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002923 bool CanBeMoved() const OVERRIDE { return true; }
2924 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002925 UNUSED(other);
2926 return true;
2927 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002928
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002929 int32_t Evaluate(int32_t x) const OVERRIDE { return ~x; }
2930 int64_t Evaluate(int64_t x) const OVERRIDE { return ~x; }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01002931
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01002932 DECLARE_INSTRUCTION(Not);
2933
2934 private:
2935 DISALLOW_COPY_AND_ASSIGN(HNot);
2936};
2937
David Brazdil66d126e2015-04-03 16:02:44 +01002938class HBooleanNot : public HUnaryOperation {
2939 public:
2940 explicit HBooleanNot(HInstruction* input)
2941 : HUnaryOperation(Primitive::Type::kPrimBoolean, input) {}
2942
2943 bool CanBeMoved() const OVERRIDE { return true; }
2944 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
2945 UNUSED(other);
2946 return true;
2947 }
2948
2949 int32_t Evaluate(int32_t x) const OVERRIDE {
2950 DCHECK(IsUint<1>(x));
2951 return !x;
2952 }
2953
2954 int64_t Evaluate(int64_t x ATTRIBUTE_UNUSED) const OVERRIDE {
2955 LOG(FATAL) << DebugName() << " cannot be used with 64-bit values";
2956 UNREACHABLE();
2957 }
2958
2959 DECLARE_INSTRUCTION(BooleanNot);
2960
2961 private:
2962 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
2963};
2964
Roland Levillaindff1f282014-11-05 14:15:05 +00002965class HTypeConversion : public HExpression<1> {
2966 public:
2967 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00002968 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
2969 : HExpression(result_type, SideEffects::None()), dex_pc_(dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002970 SetRawInputAt(0, input);
2971 DCHECK_NE(input->GetType(), result_type);
2972 }
2973
2974 HInstruction* GetInput() const { return InputAt(0); }
2975 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
2976 Primitive::Type GetResultType() const { return GetType(); }
2977
Roland Levillain624279f2014-12-04 11:54:28 +00002978 // Required by the x86 and ARM code generators when producing calls
2979 // to the runtime.
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002980 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Roland Levillain624279f2014-12-04 11:54:28 +00002981
Roland Levillaindff1f282014-11-05 14:15:05 +00002982 bool CanBeMoved() const OVERRIDE { return true; }
Roland Levillained9b1952014-11-06 11:10:17 +00002983 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE { return true; }
Roland Levillaindff1f282014-11-05 14:15:05 +00002984
Mark Mendelle82549b2015-05-06 10:55:34 -04002985 // Try to statically evaluate the conversion and return a HConstant
2986 // containing the result. If the input cannot be converted, return nullptr.
2987 HConstant* TryStaticEvaluation() const;
2988
Roland Levillaindff1f282014-11-05 14:15:05 +00002989 DECLARE_INSTRUCTION(TypeConversion);
2990
2991 private:
Roland Levillain624279f2014-12-04 11:54:28 +00002992 const uint32_t dex_pc_;
2993
Roland Levillaindff1f282014-11-05 14:15:05 +00002994 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
2995};
2996
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00002997static constexpr uint32_t kNoRegNumber = -1;
2998
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002999class HPhi : public HInstruction {
3000 public:
3001 HPhi(ArenaAllocator* arena, uint32_t reg_number, size_t number_of_inputs, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003002 : HInstruction(SideEffects::None()),
3003 inputs_(arena, number_of_inputs),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003004 reg_number_(reg_number),
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01003005 type_(type),
Calin Juravle10e244f2015-01-26 18:54:32 +00003006 is_live_(false),
3007 can_be_null_(true) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003008 inputs_.SetSize(number_of_inputs);
3009 }
3010
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +00003011 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
3012 static Primitive::Type ToPhiType(Primitive::Type type) {
3013 switch (type) {
3014 case Primitive::kPrimBoolean:
3015 case Primitive::kPrimByte:
3016 case Primitive::kPrimShort:
3017 case Primitive::kPrimChar:
3018 return Primitive::kPrimInt;
3019 default:
3020 return type;
3021 }
3022 }
3023
Calin Juravle10e244f2015-01-26 18:54:32 +00003024 size_t InputCount() const OVERRIDE { return inputs_.Size(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003025
3026 void AddInput(HInstruction* input);
David Brazdil2d7352b2015-04-20 14:52:42 +01003027 void RemoveInputAt(size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003028
Calin Juravle10e244f2015-01-26 18:54:32 +00003029 Primitive::Type GetType() const OVERRIDE { return type_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003030 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003031
Calin Juravle10e244f2015-01-26 18:54:32 +00003032 bool CanBeNull() const OVERRIDE { return can_be_null_; }
3033 void SetCanBeNull(bool can_be_null) { can_be_null_ = can_be_null; }
3034
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003035 uint32_t GetRegNumber() const { return reg_number_; }
3036
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01003037 void SetDead() { is_live_ = false; }
3038 void SetLive() { is_live_ = true; }
3039 bool IsDead() const { return !is_live_; }
3040 bool IsLive() const { return is_live_; }
3041
Calin Juravlea4f88312015-04-16 12:57:19 +01003042 // Returns the next equivalent phi (starting from the current one) or null if there is none.
3043 // An equivalent phi is a phi having the same dex register and type.
3044 // It assumes that phis with the same dex register are adjacent.
3045 HPhi* GetNextEquivalentPhiWithSameType() {
3046 HInstruction* next = GetNext();
3047 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
3048 if (next->GetType() == GetType()) {
3049 return next->AsPhi();
3050 }
3051 next = next->GetNext();
3052 }
3053 return nullptr;
3054 }
3055
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003056 DECLARE_INSTRUCTION(Phi);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003057
David Brazdil1abb4192015-02-17 18:33:36 +00003058 protected:
3059 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const OVERRIDE { return inputs_.Get(i); }
3060
3061 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) OVERRIDE {
3062 inputs_.Put(index, input);
3063 }
3064
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003065 private:
David Brazdil1abb4192015-02-17 18:33:36 +00003066 GrowableArray<HUserRecord<HInstruction*> > inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003067 const uint32_t reg_number_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003068 Primitive::Type type_;
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01003069 bool is_live_;
Calin Juravle10e244f2015-01-26 18:54:32 +00003070 bool can_be_null_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003071
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003072 DISALLOW_COPY_AND_ASSIGN(HPhi);
3073};
3074
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003075class HNullCheck : public HExpression<1> {
3076 public:
3077 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003078 : HExpression(value->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003079 SetRawInputAt(0, value);
3080 }
3081
Calin Juravle10e244f2015-01-26 18:54:32 +00003082 bool CanBeMoved() const OVERRIDE { return true; }
3083 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003084 UNUSED(other);
3085 return true;
3086 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003087
Calin Juravle10e244f2015-01-26 18:54:32 +00003088 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003089
Calin Juravle10e244f2015-01-26 18:54:32 +00003090 bool CanThrow() const OVERRIDE { return true; }
3091
3092 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01003093
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003094 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003095
3096 DECLARE_INSTRUCTION(NullCheck);
3097
3098 private:
3099 const uint32_t dex_pc_;
3100
3101 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
3102};
3103
3104class FieldInfo : public ValueObject {
3105 public:
Calin Juravle52c48962014-12-16 17:02:57 +00003106 FieldInfo(MemberOffset field_offset, Primitive::Type field_type, bool is_volatile)
3107 : field_offset_(field_offset), field_type_(field_type), is_volatile_(is_volatile) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003108
3109 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003110 Primitive::Type GetFieldType() const { return field_type_; }
Calin Juravle52c48962014-12-16 17:02:57 +00003111 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003112
3113 private:
3114 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01003115 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00003116 const bool is_volatile_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003117};
3118
3119class HInstanceFieldGet : public HExpression<1> {
3120 public:
3121 HInstanceFieldGet(HInstruction* value,
3122 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003123 MemberOffset field_offset,
3124 bool is_volatile)
Nicolas Geoffray2af23072015-04-30 11:15:40 +00003125 : HExpression(field_type, SideEffects::DependsOnSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00003126 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003127 SetRawInputAt(0, value);
3128 }
3129
Calin Juravle10c9cbe2014-12-19 10:50:19 +00003130 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003131
3132 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3133 HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
3134 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003135 }
3136
Calin Juravle641547a2015-04-21 22:08:51 +01003137 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3138 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00003139 }
3140
3141 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01003142 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
3143 }
3144
Calin Juravle52c48962014-12-16 17:02:57 +00003145 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003146 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003147 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003148 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003149
3150 DECLARE_INSTRUCTION(InstanceFieldGet);
3151
3152 private:
3153 const FieldInfo field_info_;
3154
3155 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
3156};
3157
3158class HInstanceFieldSet : public HTemplateInstruction<2> {
3159 public:
3160 HInstanceFieldSet(HInstruction* object,
3161 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01003162 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003163 MemberOffset field_offset,
3164 bool is_volatile)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003165 : HTemplateInstruction(SideEffects::ChangesSomething()),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003166 field_info_(field_offset, field_type, is_volatile),
3167 value_can_be_null_(true) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003168 SetRawInputAt(0, object);
3169 SetRawInputAt(1, value);
3170 }
3171
Calin Juravle641547a2015-04-21 22:08:51 +01003172 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3173 return (obj == InputAt(0)) && GetFieldOffset().Uint32Value() < kPageSize;
Calin Juravle77520bc2015-01-12 18:45:46 +00003174 }
3175
Calin Juravle52c48962014-12-16 17:02:57 +00003176 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003177 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003178 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003179 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003180 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003181 bool GetValueCanBeNull() const { return value_can_be_null_; }
3182 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003183
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003184 DECLARE_INSTRUCTION(InstanceFieldSet);
3185
3186 private:
3187 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003188 bool value_can_be_null_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003189
3190 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
3191};
3192
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003193class HArrayGet : public HExpression<2> {
3194 public:
3195 HArrayGet(HInstruction* array, HInstruction* index, Primitive::Type type)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003196 : HExpression(type, SideEffects::DependsOnSomething()) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003197 SetRawInputAt(0, array);
3198 SetRawInputAt(1, index);
3199 }
3200
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003201 bool CanBeMoved() const OVERRIDE { return true; }
3202 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003203 UNUSED(other);
3204 return true;
3205 }
Calin Juravle641547a2015-04-21 22:08:51 +01003206 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3207 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00003208 // TODO: We can be smarter here.
3209 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
3210 // which generates the implicit null check. There are cases when these can be removed
3211 // to produce better code. If we ever add optimizations to do so we should allow an
3212 // implicit check here (as long as the address falls in the first page).
3213 return false;
3214 }
3215
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003216 void SetType(Primitive::Type type) { type_ = type; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003217
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003218 HInstruction* GetArray() const { return InputAt(0); }
3219 HInstruction* GetIndex() const { return InputAt(1); }
3220
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003221 DECLARE_INSTRUCTION(ArrayGet);
3222
3223 private:
3224 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
3225};
3226
3227class HArraySet : public HTemplateInstruction<3> {
3228 public:
3229 HArraySet(HInstruction* array,
3230 HInstruction* index,
3231 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003232 Primitive::Type expected_component_type,
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003233 uint32_t dex_pc)
3234 : HTemplateInstruction(SideEffects::ChangesSomething()),
3235 dex_pc_(dex_pc),
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003236 expected_component_type_(expected_component_type),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003237 needs_type_check_(value->GetType() == Primitive::kPrimNot),
3238 value_can_be_null_(true) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003239 SetRawInputAt(0, array);
3240 SetRawInputAt(1, index);
3241 SetRawInputAt(2, value);
3242 }
3243
Calin Juravle77520bc2015-01-12 18:45:46 +00003244 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003245 // We currently always call a runtime method to catch array store
3246 // exceptions.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003247 return needs_type_check_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003248 }
3249
Calin Juravle641547a2015-04-21 22:08:51 +01003250 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3251 UNUSED(obj);
Calin Juravle77520bc2015-01-12 18:45:46 +00003252 // TODO: Same as for ArrayGet.
3253 return false;
3254 }
3255
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003256 void ClearNeedsTypeCheck() {
3257 needs_type_check_ = false;
3258 }
3259
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003260 void ClearValueCanBeNull() {
3261 value_can_be_null_ = false;
3262 }
3263
3264 bool GetValueCanBeNull() const { return value_can_be_null_; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003265 bool NeedsTypeCheck() const { return needs_type_check_; }
3266
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003267 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003268
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003269 HInstruction* GetArray() const { return InputAt(0); }
3270 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003271 HInstruction* GetValue() const { return InputAt(2); }
3272
3273 Primitive::Type GetComponentType() const {
3274 // The Dex format does not type floating point index operations. Since the
3275 // `expected_component_type_` is set during building and can therefore not
3276 // be correct, we also check what is the value type. If it is a floating
3277 // point type, we must use that type.
3278 Primitive::Type value_type = GetValue()->GetType();
3279 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
3280 ? value_type
3281 : expected_component_type_;
3282 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01003283
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003284 DECLARE_INSTRUCTION(ArraySet);
3285
3286 private:
3287 const uint32_t dex_pc_;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003288 const Primitive::Type expected_component_type_;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003289 bool needs_type_check_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003290 bool value_can_be_null_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003291
3292 DISALLOW_COPY_AND_ASSIGN(HArraySet);
3293};
3294
3295class HArrayLength : public HExpression<1> {
3296 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003297 explicit HArrayLength(HInstruction* array)
3298 : HExpression(Primitive::kPrimInt, SideEffects::None()) {
3299 // Note that arrays do not change length, so the instruction does not
3300 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003301 SetRawInputAt(0, array);
3302 }
3303
Calin Juravle77520bc2015-01-12 18:45:46 +00003304 bool CanBeMoved() const OVERRIDE { return true; }
3305 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003306 UNUSED(other);
3307 return true;
3308 }
Calin Juravle641547a2015-04-21 22:08:51 +01003309 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
3310 return obj == InputAt(0);
3311 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003312
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003313 DECLARE_INSTRUCTION(ArrayLength);
3314
3315 private:
3316 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
3317};
3318
3319class HBoundsCheck : public HExpression<2> {
3320 public:
3321 HBoundsCheck(HInstruction* index, HInstruction* length, uint32_t dex_pc)
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003322 : HExpression(index->GetType(), SideEffects::None()), dex_pc_(dex_pc) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003323 DCHECK(index->GetType() == Primitive::kPrimInt);
3324 SetRawInputAt(0, index);
3325 SetRawInputAt(1, length);
3326 }
3327
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003328 bool CanBeMoved() const OVERRIDE { return true; }
3329 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003330 UNUSED(other);
3331 return true;
3332 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003333
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003334 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003335
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003336 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01003337
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003338 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003339
3340 DECLARE_INSTRUCTION(BoundsCheck);
3341
3342 private:
3343 const uint32_t dex_pc_;
3344
3345 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
3346};
3347
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003348/**
3349 * Some DEX instructions are folded into multiple HInstructions that need
3350 * to stay live until the last HInstruction. This class
3351 * is used as a marker for the baseline compiler to ensure its preceding
Calin Juravlef97f9fb2014-11-11 15:38:19 +00003352 * HInstruction stays live. `index` represents the stack location index of the
3353 * instruction (the actual offset is computed as index * vreg_size).
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003354 */
3355class HTemporary : public HTemplateInstruction<0> {
3356 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003357 explicit HTemporary(size_t index) : HTemplateInstruction(SideEffects::None()), index_(index) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003358
3359 size_t GetIndex() const { return index_; }
3360
Nicolas Geoffray421e9f92014-11-11 18:21:53 +00003361 Primitive::Type GetType() const OVERRIDE {
3362 // The previous instruction is the one that will be stored in the temporary location.
3363 DCHECK(GetPrevious() != nullptr);
3364 return GetPrevious()->GetType();
3365 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00003366
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003367 DECLARE_INSTRUCTION(Temporary);
3368
3369 private:
3370 const size_t index_;
3371
3372 DISALLOW_COPY_AND_ASSIGN(HTemporary);
3373};
3374
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003375class HSuspendCheck : public HTemplateInstruction<0> {
3376 public:
3377 explicit HSuspendCheck(uint32_t dex_pc)
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01003378 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003379
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003380 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003381 return true;
3382 }
3383
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003384 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01003385 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
3386 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003387
3388 DECLARE_INSTRUCTION(SuspendCheck);
3389
3390 private:
3391 const uint32_t dex_pc_;
3392
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01003393 // Only used for code generation, in order to share the same slow path between back edges
3394 // of a same loop.
3395 SlowPathCode* slow_path_;
3396
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00003397 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
3398};
3399
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003400/**
3401 * Instruction to load a Class object.
3402 */
3403class HLoadClass : public HExpression<0> {
3404 public:
3405 HLoadClass(uint16_t type_index,
3406 bool is_referrers_class,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003407 uint32_t dex_pc)
3408 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3409 type_index_(type_index),
3410 is_referrers_class_(is_referrers_class),
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003411 dex_pc_(dex_pc),
Calin Juravleb1498f62015-02-16 13:13:29 +00003412 generate_clinit_check_(false),
3413 loaded_class_rti_(ReferenceTypeInfo::CreateTop(/* is_exact */ false)) {}
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003414
3415 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003416
3417 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3418 return other->AsLoadClass()->type_index_ == type_index_;
3419 }
3420
3421 size_t ComputeHashCode() const OVERRIDE { return type_index_; }
3422
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003423 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003424 uint16_t GetTypeIndex() const { return type_index_; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003425 bool IsReferrersClass() const { return is_referrers_class_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003426
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003427 bool NeedsEnvironment() const OVERRIDE {
3428 // Will call runtime and load the class if the class is not loaded yet.
3429 // TODO: finer grain decision.
3430 return !is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003431 }
3432
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003433 bool MustGenerateClinitCheck() const {
3434 return generate_clinit_check_;
3435 }
3436
3437 void SetMustGenerateClinitCheck() {
3438 generate_clinit_check_ = true;
3439 }
3440
3441 bool CanCallRuntime() const {
3442 return MustGenerateClinitCheck() || !is_referrers_class_;
3443 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003444
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003445 bool CanThrow() const OVERRIDE {
3446 // May call runtime and and therefore can throw.
3447 // TODO: finer grain decision.
3448 return !is_referrers_class_;
3449 }
3450
Calin Juravleacf735c2015-02-12 15:25:22 +00003451 ReferenceTypeInfo GetLoadedClassRTI() {
3452 return loaded_class_rti_;
3453 }
3454
3455 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
3456 // Make sure we only set exact types (the loaded class should never be merged).
3457 DCHECK(rti.IsExact());
3458 loaded_class_rti_ = rti;
3459 }
3460
3461 bool IsResolved() {
3462 return loaded_class_rti_.IsExact();
3463 }
3464
Nicolas Geoffray9437b782015-03-25 10:08:51 +00003465 bool NeedsDexCache() const OVERRIDE { return !is_referrers_class_; }
3466
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003467 DECLARE_INSTRUCTION(LoadClass);
3468
3469 private:
3470 const uint16_t type_index_;
3471 const bool is_referrers_class_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003472 const uint32_t dex_pc_;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003473 // Whether this instruction must generate the initialization check.
3474 // Used for code generation.
3475 bool generate_clinit_check_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003476
Calin Juravleacf735c2015-02-12 15:25:22 +00003477 ReferenceTypeInfo loaded_class_rti_;
3478
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003479 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
3480};
3481
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003482class HLoadString : public HExpression<0> {
3483 public:
3484 HLoadString(uint32_t string_index, uint32_t dex_pc)
3485 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3486 string_index_(string_index),
3487 dex_pc_(dex_pc) {}
3488
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003489 bool CanBeMoved() const OVERRIDE { return true; }
3490
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003491 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3492 return other->AsLoadString()->string_index_ == string_index_;
3493 }
3494
3495 size_t ComputeHashCode() const OVERRIDE { return string_index_; }
3496
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003497 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003498 uint32_t GetStringIndex() const { return string_index_; }
3499
3500 // TODO: Can we deopt or debug when we resolve a string?
3501 bool NeedsEnvironment() const OVERRIDE { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00003502 bool NeedsDexCache() const OVERRIDE { return true; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00003503
3504 DECLARE_INSTRUCTION(LoadString);
3505
3506 private:
3507 const uint32_t string_index_;
3508 const uint32_t dex_pc_;
3509
3510 DISALLOW_COPY_AND_ASSIGN(HLoadString);
3511};
3512
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003513/**
3514 * Performs an initialization check on its Class object input.
3515 */
3516class HClinitCheck : public HExpression<1> {
3517 public:
3518 explicit HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Nicolas Geoffraya0466e12015-03-27 15:00:40 +00003519 : HExpression(Primitive::kPrimNot, SideEffects::ChangesSomething()),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003520 dex_pc_(dex_pc) {
3521 SetRawInputAt(0, constant);
3522 }
3523
Nicolas Geoffray424f6762014-11-03 14:51:25 +00003524 bool CanBeMoved() const OVERRIDE { return true; }
3525 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
3526 UNUSED(other);
3527 return true;
3528 }
3529
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003530 bool NeedsEnvironment() const OVERRIDE {
3531 // May call runtime to initialize the class.
3532 return true;
3533 }
3534
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003535 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003536
3537 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
3538
3539 DECLARE_INSTRUCTION(ClinitCheck);
3540
3541 private:
3542 const uint32_t dex_pc_;
3543
3544 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
3545};
3546
3547class HStaticFieldGet : public HExpression<1> {
3548 public:
3549 HStaticFieldGet(HInstruction* cls,
3550 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003551 MemberOffset field_offset,
3552 bool is_volatile)
Nicolas Geoffray2af23072015-04-30 11:15:40 +00003553 : HExpression(field_type, SideEffects::DependsOnSomething()),
Calin Juravle52c48962014-12-16 17:02:57 +00003554 field_info_(field_offset, field_type, is_volatile) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003555 SetRawInputAt(0, cls);
3556 }
3557
Calin Juravle52c48962014-12-16 17:02:57 +00003558
Calin Juravle10c9cbe2014-12-19 10:50:19 +00003559 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003560
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003561 bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
Calin Juravle52c48962014-12-16 17:02:57 +00003562 HStaticFieldGet* other_get = other->AsStaticFieldGet();
3563 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003564 }
3565
3566 size_t ComputeHashCode() const OVERRIDE {
3567 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
3568 }
3569
Calin Juravle52c48962014-12-16 17:02:57 +00003570 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003571 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
3572 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003573 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003574
3575 DECLARE_INSTRUCTION(StaticFieldGet);
3576
3577 private:
3578 const FieldInfo field_info_;
3579
3580 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
3581};
3582
3583class HStaticFieldSet : public HTemplateInstruction<2> {
3584 public:
3585 HStaticFieldSet(HInstruction* cls,
3586 HInstruction* value,
3587 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00003588 MemberOffset field_offset,
3589 bool is_volatile)
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003590 : HTemplateInstruction(SideEffects::ChangesSomething()),
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003591 field_info_(field_offset, field_type, is_volatile),
3592 value_can_be_null_(true) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003593 SetRawInputAt(0, cls);
3594 SetRawInputAt(1, value);
3595 }
3596
Calin Juravle52c48962014-12-16 17:02:57 +00003597 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003598 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
3599 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00003600 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003601
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003602 HInstruction* GetValue() const { return InputAt(1); }
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003603 bool GetValueCanBeNull() const { return value_can_be_null_; }
3604 void ClearValueCanBeNull() { value_can_be_null_ = false; }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00003605
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003606 DECLARE_INSTRUCTION(StaticFieldSet);
3607
3608 private:
3609 const FieldInfo field_info_;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003610 bool value_can_be_null_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01003611
3612 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
3613};
3614
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003615// Implement the move-exception DEX instruction.
3616class HLoadException : public HExpression<0> {
3617 public:
3618 HLoadException() : HExpression(Primitive::kPrimNot, SideEffects::None()) {}
3619
3620 DECLARE_INSTRUCTION(LoadException);
3621
3622 private:
3623 DISALLOW_COPY_AND_ASSIGN(HLoadException);
3624};
3625
3626class HThrow : public HTemplateInstruction<1> {
3627 public:
3628 HThrow(HInstruction* exception, uint32_t dex_pc)
3629 : HTemplateInstruction(SideEffects::None()), dex_pc_(dex_pc) {
3630 SetRawInputAt(0, exception);
3631 }
3632
3633 bool IsControlFlow() const OVERRIDE { return true; }
3634
3635 bool NeedsEnvironment() const OVERRIDE { return true; }
3636
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003637 bool CanThrow() const OVERRIDE { return true; }
3638
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003639 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003640
3641 DECLARE_INSTRUCTION(Throw);
3642
3643 private:
Nicolas Geoffray82091da2015-01-26 10:02:45 +00003644 const uint32_t dex_pc_;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003645
3646 DISALLOW_COPY_AND_ASSIGN(HThrow);
3647};
3648
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003649class HInstanceOf : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003650 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003651 HInstanceOf(HInstruction* object,
3652 HLoadClass* constant,
3653 bool class_is_final,
3654 uint32_t dex_pc)
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003655 : HExpression(Primitive::kPrimBoolean, SideEffects::None()),
3656 class_is_final_(class_is_final),
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003657 must_do_null_check_(true),
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003658 dex_pc_(dex_pc) {
3659 SetRawInputAt(0, object);
3660 SetRawInputAt(1, constant);
3661 }
3662
3663 bool CanBeMoved() const OVERRIDE { return true; }
3664
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003665 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003666 return true;
3667 }
3668
3669 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003670 return false;
3671 }
3672
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003673 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003674
3675 bool IsClassFinal() const { return class_is_final_; }
3676
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003677 // Used only in code generation.
3678 bool MustDoNullCheck() const { return must_do_null_check_; }
3679 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
3680
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003681 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003682
3683 private:
3684 const bool class_is_final_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003685 bool must_do_null_check_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003686 const uint32_t dex_pc_;
3687
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003688 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
3689};
3690
Calin Juravleb1498f62015-02-16 13:13:29 +00003691class HBoundType : public HExpression<1> {
3692 public:
3693 HBoundType(HInstruction* input, ReferenceTypeInfo bound_type)
3694 : HExpression(Primitive::kPrimNot, SideEffects::None()),
3695 bound_type_(bound_type) {
Calin Juravle61d544b2015-02-23 16:46:57 +00003696 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00003697 SetRawInputAt(0, input);
3698 }
3699
3700 const ReferenceTypeInfo& GetBoundType() const { return bound_type_; }
3701
3702 bool CanBeNull() const OVERRIDE {
3703 // `null instanceof ClassX` always return false so we can't be null.
3704 return false;
3705 }
3706
3707 DECLARE_INSTRUCTION(BoundType);
3708
3709 private:
3710 // Encodes the most upper class that this instruction can have. In other words
3711 // it is always the case that GetBoundType().IsSupertypeOf(GetReferenceType()).
3712 // It is used to bound the type in cases like `if (x instanceof ClassX) {}`
3713 const ReferenceTypeInfo bound_type_;
3714
3715 DISALLOW_COPY_AND_ASSIGN(HBoundType);
3716};
3717
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003718class HCheckCast : public HTemplateInstruction<2> {
3719 public:
3720 HCheckCast(HInstruction* object,
3721 HLoadClass* constant,
3722 bool class_is_final,
3723 uint32_t dex_pc)
3724 : HTemplateInstruction(SideEffects::None()),
3725 class_is_final_(class_is_final),
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003726 must_do_null_check_(true),
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003727 dex_pc_(dex_pc) {
3728 SetRawInputAt(0, object);
3729 SetRawInputAt(1, constant);
3730 }
3731
3732 bool CanBeMoved() const OVERRIDE { return true; }
3733
3734 bool InstructionDataEquals(HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
3735 return true;
3736 }
3737
3738 bool NeedsEnvironment() const OVERRIDE {
3739 // Instruction may throw a CheckCastError.
3740 return true;
3741 }
3742
3743 bool CanThrow() const OVERRIDE { return true; }
3744
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003745 bool MustDoNullCheck() const { return must_do_null_check_; }
3746 void ClearMustDoNullCheck() { must_do_null_check_ = false; }
3747
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003748 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003749
3750 bool IsClassFinal() const { return class_is_final_; }
3751
3752 DECLARE_INSTRUCTION(CheckCast);
3753
3754 private:
3755 const bool class_is_final_;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01003756 bool must_do_null_check_;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00003757 const uint32_t dex_pc_;
3758
3759 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00003760};
3761
Calin Juravle27df7582015-04-17 19:12:31 +01003762class HMemoryBarrier : public HTemplateInstruction<0> {
3763 public:
3764 explicit HMemoryBarrier(MemBarrierKind barrier_kind)
3765 : HTemplateInstruction(SideEffects::None()),
3766 barrier_kind_(barrier_kind) {}
3767
3768 MemBarrierKind GetBarrierKind() { return barrier_kind_; }
3769
3770 DECLARE_INSTRUCTION(MemoryBarrier);
3771
3772 private:
3773 const MemBarrierKind barrier_kind_;
3774
3775 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
3776};
3777
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003778class HMonitorOperation : public HTemplateInstruction<1> {
3779 public:
3780 enum OperationKind {
3781 kEnter,
3782 kExit,
3783 };
3784
3785 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
3786 : HTemplateInstruction(SideEffects::None()), kind_(kind), dex_pc_(dex_pc) {
3787 SetRawInputAt(0, object);
3788 }
3789
3790 // Instruction may throw a Java exception, so we need an environment.
3791 bool NeedsEnvironment() const OVERRIDE { return true; }
3792 bool CanThrow() const OVERRIDE { return true; }
3793
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01003794 uint32_t GetDexPc() const OVERRIDE { return dex_pc_; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003795
3796 bool IsEnter() const { return kind_ == kEnter; }
3797
3798 DECLARE_INSTRUCTION(MonitorOperation);
3799
Calin Juravle52c48962014-12-16 17:02:57 +00003800 private:
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00003801 const OperationKind kind_;
3802 const uint32_t dex_pc_;
3803
3804 private:
3805 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
3806};
3807
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003808class MoveOperands : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003809 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01003810 MoveOperands(Location source,
3811 Location destination,
3812 Primitive::Type type,
3813 HInstruction* instruction)
3814 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003815
3816 Location GetSource() const { return source_; }
3817 Location GetDestination() const { return destination_; }
3818
3819 void SetSource(Location value) { source_ = value; }
3820 void SetDestination(Location value) { destination_ = value; }
3821
3822 // The parallel move resolver marks moves as "in-progress" by clearing the
3823 // destination (but not the source).
3824 Location MarkPending() {
3825 DCHECK(!IsPending());
3826 Location dest = destination_;
3827 destination_ = Location::NoLocation();
3828 return dest;
3829 }
3830
3831 void ClearPending(Location dest) {
3832 DCHECK(IsPending());
3833 destination_ = dest;
3834 }
3835
3836 bool IsPending() const {
3837 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
3838 return destination_.IsInvalid() && !source_.IsInvalid();
3839 }
3840
3841 // True if this blocks a move from the given location.
3842 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08003843 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003844 }
3845
3846 // A move is redundant if it's been eliminated, if its source and
3847 // destination are the same, or if its destination is unneeded.
3848 bool IsRedundant() const {
3849 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
3850 }
3851
3852 // We clear both operands to indicate move that's been eliminated.
3853 void Eliminate() {
3854 source_ = destination_ = Location::NoLocation();
3855 }
3856
3857 bool IsEliminated() const {
3858 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
3859 return source_.IsInvalid();
3860 }
3861
Nicolas Geoffray90218252015-04-15 11:56:51 +01003862 bool Is64BitMove() const {
3863 return Primitive::Is64BitType(type_);
3864 }
3865
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003866 HInstruction* GetInstruction() const { return instruction_; }
3867
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003868 private:
3869 Location source_;
3870 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01003871 // The type this move is for.
3872 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003873 // The instruction this move is assocatied with. Null when this move is
3874 // for moving an input in the expected locations of user (including a phi user).
3875 // This is only used in debug mode, to ensure we do not connect interval siblings
3876 // in the same parallel move.
3877 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003878};
3879
3880static constexpr size_t kDefaultNumberOfMoves = 4;
3881
3882class HParallelMove : public HTemplateInstruction<0> {
3883 public:
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003884 explicit HParallelMove(ArenaAllocator* arena)
3885 : HTemplateInstruction(SideEffects::None()), moves_(arena, kDefaultNumberOfMoves) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003886
Nicolas Geoffray90218252015-04-15 11:56:51 +01003887 void AddMove(Location source,
3888 Location destination,
3889 Primitive::Type type,
3890 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003891 DCHECK(source.IsValid());
3892 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003893 if (kIsDebugBuild) {
3894 if (instruction != nullptr) {
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00003895 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003896 if (moves_.Get(i).GetInstruction() == instruction) {
3897 // Special case the situation where the move is for the spill slot
3898 // of the instruction.
3899 if ((GetPrevious() == instruction)
3900 || ((GetPrevious() == nullptr)
3901 && instruction->IsPhi()
3902 && instruction->GetBlock() == GetBlock())) {
3903 DCHECK_NE(destination.GetKind(), moves_.Get(i).GetDestination().GetKind())
3904 << "Doing parallel moves for the same instruction.";
3905 } else {
3906 DCHECK(false) << "Doing parallel moves for the same instruction.";
3907 }
3908 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00003909 }
3910 }
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003911 for (size_t i = 0, e = moves_.Size(); i < e; ++i) {
Zheng Xuad4450e2015-04-17 18:48:56 +08003912 DCHECK(!destination.OverlapsWith(moves_.Get(i).GetDestination()))
3913 << "Overlapped destination for two moves in a parallel move.";
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00003914 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01003915 }
Nicolas Geoffray90218252015-04-15 11:56:51 +01003916 moves_.Add(MoveOperands(source, destination, type, instruction));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003917 }
3918
3919 MoveOperands* MoveOperandsAt(size_t index) const {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003920 return moves_.GetRawStorage() + index;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003921 }
3922
3923 size_t NumMoves() const { return moves_.Size(); }
3924
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003925 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003926
3927 private:
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00003928 GrowableArray<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01003929
3930 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
3931};
3932
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003933class HGraphVisitor : public ValueObject {
3934 public:
Dave Allison20dfc792014-06-16 20:44:29 -07003935 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
3936 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003937
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003938 virtual void VisitInstruction(HInstruction* instruction) { UNUSED(instruction); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003939 virtual void VisitBasicBlock(HBasicBlock* block);
3940
Roland Levillain633021e2014-10-01 14:12:25 +01003941 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003942 void VisitInsertionOrder();
3943
Roland Levillain633021e2014-10-01 14:12:25 +01003944 // Visit the graph following dominator tree reverse post-order.
3945 void VisitReversePostOrder();
3946
Nicolas Geoffray787c3072014-03-17 10:20:19 +00003947 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00003948
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003949 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003950#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003951 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
3952
3953 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
3954
3955#undef DECLARE_VISIT_INSTRUCTION
3956
3957 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07003958 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003959
3960 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
3961};
3962
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003963class HGraphDelegateVisitor : public HGraphVisitor {
3964 public:
3965 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
3966 virtual ~HGraphDelegateVisitor() {}
3967
3968 // Visit functions that delegate to to super class.
3969#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003970 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003971
3972 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
3973
3974#undef DECLARE_VISIT_INSTRUCTION
3975
3976 private:
3977 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
3978};
3979
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003980class HInsertionOrderIterator : public ValueObject {
3981 public:
3982 explicit HInsertionOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {}
3983
3984 bool Done() const { return index_ == graph_.GetBlocks().Size(); }
3985 HBasicBlock* Current() const { return graph_.GetBlocks().Get(index_); }
3986 void Advance() { ++index_; }
3987
3988 private:
3989 const HGraph& graph_;
3990 size_t index_;
3991
3992 DISALLOW_COPY_AND_ASSIGN(HInsertionOrderIterator);
3993};
3994
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01003995class HReversePostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01003996 public:
David Brazdil10f56cb2015-03-24 18:49:14 +00003997 explicit HReversePostOrderIterator(const HGraph& graph) : graph_(graph), index_(0) {
3998 // Check that reverse post order of the graph has been built.
3999 DCHECK(!graph.GetReversePostOrder().IsEmpty());
4000 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004001
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004002 bool Done() const { return index_ == graph_.GetReversePostOrder().Size(); }
4003 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004004 void Advance() { ++index_; }
4005
4006 private:
4007 const HGraph& graph_;
4008 size_t index_;
4009
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004010 DISALLOW_COPY_AND_ASSIGN(HReversePostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004011};
4012
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004013class HPostOrderIterator : public ValueObject {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004014 public:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004015 explicit HPostOrderIterator(const HGraph& graph)
David Brazdil10f56cb2015-03-24 18:49:14 +00004016 : graph_(graph), index_(graph_.GetReversePostOrder().Size()) {
4017 // Check that reverse post order of the graph has been built.
4018 DCHECK(!graph.GetReversePostOrder().IsEmpty());
4019 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004020
4021 bool Done() const { return index_ == 0; }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004022 HBasicBlock* Current() const { return graph_.GetReversePostOrder().Get(index_ - 1); }
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004023 void Advance() { --index_; }
4024
4025 private:
4026 const HGraph& graph_;
4027 size_t index_;
4028
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01004029 DISALLOW_COPY_AND_ASSIGN(HPostOrderIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01004030};
4031
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01004032class HLinearPostOrderIterator : public ValueObject {
4033 public:
4034 explicit HLinearPostOrderIterator(const HGraph& graph)
4035 : order_(graph.GetLinearOrder()), index_(graph.GetLinearOrder().Size()) {}
4036
4037 bool Done() const { return index_ == 0; }
4038
4039 HBasicBlock* Current() const { return order_.Get(index_ -1); }
4040
4041 void Advance() {
4042 --index_;
4043 DCHECK_GE(index_, 0U);
4044 }
4045
4046 private:
4047 const GrowableArray<HBasicBlock*>& order_;
4048 size_t index_;
4049
4050 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
4051};
4052
4053class HLinearOrderIterator : public ValueObject {
4054 public:
4055 explicit HLinearOrderIterator(const HGraph& graph)
4056 : order_(graph.GetLinearOrder()), index_(0) {}
4057
4058 bool Done() const { return index_ == order_.Size(); }
4059 HBasicBlock* Current() const { return order_.Get(index_); }
4060 void Advance() { ++index_; }
4061
4062 private:
4063 const GrowableArray<HBasicBlock*>& order_;
4064 size_t index_;
4065
4066 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
4067};
4068
Nicolas Geoffray82091da2015-01-26 10:02:45 +00004069// Iterator over the blocks that art part of the loop. Includes blocks part
4070// of an inner loop. The order in which the blocks are iterated is on their
4071// block id.
4072class HBlocksInLoopIterator : public ValueObject {
4073 public:
4074 explicit HBlocksInLoopIterator(const HLoopInformation& info)
4075 : blocks_in_loop_(info.GetBlocks()),
4076 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
4077 index_(0) {
4078 if (!blocks_in_loop_.IsBitSet(index_)) {
4079 Advance();
4080 }
4081 }
4082
4083 bool Done() const { return index_ == blocks_.Size(); }
4084 HBasicBlock* Current() const { return blocks_.Get(index_); }
4085 void Advance() {
4086 ++index_;
4087 for (size_t e = blocks_.Size(); index_ < e; ++index_) {
4088 if (blocks_in_loop_.IsBitSet(index_)) {
4089 break;
4090 }
4091 }
4092 }
4093
4094 private:
4095 const BitVector& blocks_in_loop_;
4096 const GrowableArray<HBasicBlock*>& blocks_;
4097 size_t index_;
4098
4099 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
4100};
4101
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00004102inline int64_t Int64FromConstant(HConstant* constant) {
4103 DCHECK(constant->IsIntConstant() || constant->IsLongConstant());
4104 return constant->IsIntConstant() ? constant->AsIntConstant()->GetValue()
4105 : constant->AsLongConstant()->GetValue();
4106}
4107
Nicolas Geoffray818f2102014-02-18 16:43:35 +00004108} // namespace art
4109
4110#endif // ART_COMPILER_OPTIMIZING_NODES_H_