blob: 8c64d25aee3190f8e6bd7d483b67f8ff73d02daa [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
Vladimir Marko60584552015-09-03 13:35:12 +000020#include <algorithm>
Vladimir Markof9f64412015-09-02 14:05:49 +010021#include <array>
Roland Levillain9867bc72015-08-05 10:21:34 +010022#include <type_traits>
23
Mathieu Chartiere5d80f82015-10-15 17:47:48 -070024#include "base/arena_bit_vector.h"
David Brazdil8d5b8b22015-03-24 10:51:52 +000025#include "base/arena_containers.h"
Mathieu Chartierb666f482015-02-18 14:33:14 -080026#include "base/arena_object.h"
David Brazdild9c90372016-09-14 16:53:55 +010027#include "base/array_ref.h"
Vladimir Marko2c45bc92016-10-25 16:54:12 +010028#include "base/iteration_range.h"
Vladimir Marko60584552015-09-03 13:35:12 +000029#include "base/stl_util.h"
David Brazdild9c90372016-09-14 16:53:55 +010030#include "base/transform_array_ref.h"
Vladimir Marko87f3fcb2016-04-28 15:52:11 +010031#include "dex_file.h"
Andreas Gampea5b09a62016-11-17 15:21:22 -080032#include "dex_file_types.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000033#include "entrypoints/quick/quick_entrypoints_enum.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000034#include "handle.h"
35#include "handle_scope.h"
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000036#include "invoke_type.h"
Nicolas Geoffray762869d2016-07-15 15:28:35 +010037#include "intrinsics_enum.h"
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010038#include "locations.h"
Vladimir Marko58155012015-08-19 12:49:41 +000039#include "method_reference.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000040#include "mirror/class.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010041#include "offsets.h"
Ian Rogerse63db272014-07-15 15:36:11 -070042#include "primitive.h"
Vladimir Marko46817b82016-03-29 12:21:58 +010043#include "utils/intrusive_forward_list.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000044
45namespace art {
46
David Brazdil1abb4192015-02-17 18:33:36 +000047class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000048class HBasicBlock;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010049class HCurrentMethod;
David Brazdil8d5b8b22015-03-24 10:51:52 +000050class HDoubleConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010051class HEnvironment;
David Brazdil8d5b8b22015-03-24 10:51:52 +000052class HFloatConstant;
David Brazdilfc6a86a2015-06-26 10:33:45 +000053class HGraphBuilder;
David Brazdil8d5b8b22015-03-24 10:51:52 +000054class HGraphVisitor;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000055class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000056class HIntConstant;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000057class HInvoke;
David Brazdil8d5b8b22015-03-24 10:51:52 +000058class HLongConstant;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +000059class HNullConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010060class HPhi;
Nicolas Geoffray3c049742014-09-24 18:10:46 +010061class HSuspendCheck;
David Brazdilffee3d32015-07-06 11:48:53 +010062class HTryBoundary;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010063class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000064class LocationSummary;
Nicolas Geoffraydb216f42015-05-05 17:02:20 +010065class SlowPathCode;
David Brazdil8d5b8b22015-03-24 10:51:52 +000066class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000067
Mathieu Chartier736b5602015-09-02 14:54:11 -070068namespace mirror {
69class DexCache;
70} // namespace mirror
71
Nicolas Geoffray818f2102014-02-18 16:43:35 +000072static const int kDefaultNumberOfBlocks = 8;
73static const int kDefaultNumberOfSuccessors = 2;
74static const int kDefaultNumberOfPredecessors = 2;
David Brazdilb618ade2015-07-29 10:31:29 +010075static const int kDefaultNumberOfExceptionalPredecessors = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010076static const int kDefaultNumberOfDominatedBlocks = 1;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000077static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000078
Roland Levillain5b5b9312016-03-22 14:57:31 +000079// The maximum (meaningful) distance (31) that can be used in an integer shift/rotate operation.
80static constexpr int32_t kMaxIntShiftDistance = 0x1f;
81// The maximum (meaningful) distance (63) that can be used in a long shift/rotate operation.
82static constexpr int32_t kMaxLongShiftDistance = 0x3f;
Calin Juravle9aec02f2014-11-18 23:06:35 +000083
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010084static constexpr uint32_t kUnknownFieldIndex = static_cast<uint32_t>(-1);
Mingyao Yang8df69d42015-10-22 15:40:58 -070085static constexpr uint16_t kUnknownClassDefIndex = static_cast<uint16_t>(-1);
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010086
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +010087static constexpr InvokeType kInvalidInvokeType = static_cast<InvokeType>(-1);
88
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +060089static constexpr uint32_t kNoDexPc = -1;
90
Vladimir Markodbb7f5b2016-03-30 13:23:58 +010091inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
92 // For the purposes of the compiler, the dex files must actually be the same object
93 // if we want to safely treat them as the same. This is especially important for JIT
94 // as custom class loaders can open the same underlying file (or memory) multiple
95 // times and provide different class resolution but no two class loaders should ever
96 // use the same DexFile object - doing so is an unsupported hack that can lead to
97 // all sorts of weird failures.
98 return &lhs == &rhs;
99}
100
Dave Allison20dfc792014-06-16 20:44:29 -0700101enum IfCondition {
Aart Bike9f37602015-10-09 11:15:55 -0700102 // All types.
103 kCondEQ, // ==
104 kCondNE, // !=
105 // Signed integers and floating-point numbers.
106 kCondLT, // <
107 kCondLE, // <=
108 kCondGT, // >
109 kCondGE, // >=
110 // Unsigned integers.
111 kCondB, // <
112 kCondBE, // <=
113 kCondA, // >
114 kCondAE, // >=
Scott Wakeling2c76e062016-08-31 09:48:54 +0100115 // First and last aliases.
116 kCondFirst = kCondEQ,
117 kCondLast = kCondAE,
Dave Allison20dfc792014-06-16 20:44:29 -0700118};
119
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000120enum GraphAnalysisResult {
David Brazdil86ea7ee2016-02-16 09:26:07 +0000121 kAnalysisSkipped,
David Brazdilbadd8262016-02-02 16:28:56 +0000122 kAnalysisInvalidBytecode,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000123 kAnalysisFailThrowCatchLoop,
124 kAnalysisFailAmbiguousArrayOp,
125 kAnalysisSuccess,
David Brazdil4833f5a2015-12-16 10:37:39 +0000126};
127
Andreas Gampe9186ced2016-12-12 14:28:21 -0800128template <typename T>
129static inline typename std::make_unsigned<T>::type MakeUnsigned(T x) {
130 return static_cast<typename std::make_unsigned<T>::type>(x);
131}
132
Vladimir Markof9f64412015-09-02 14:05:49 +0100133class HInstructionList : public ValueObject {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100134 public:
135 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
136
137 void AddInstruction(HInstruction* instruction);
138 void RemoveInstruction(HInstruction* instruction);
139
David Brazdilc3d743f2015-04-22 13:40:50 +0100140 // Insert `instruction` before/after an existing instruction `cursor`.
141 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
142 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
143
Roland Levillain6b469232014-09-25 10:10:38 +0100144 // Return true if this list contains `instruction`.
145 bool Contains(HInstruction* instruction) const;
146
Roland Levillainccc07a92014-09-16 14:48:16 +0100147 // Return true if `instruction1` is found before `instruction2` in
148 // this instruction list and false otherwise. Abort if none
149 // of these instructions is found.
150 bool FoundBefore(const HInstruction* instruction1,
151 const HInstruction* instruction2) const;
152
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000153 bool IsEmpty() const { return first_instruction_ == nullptr; }
154 void Clear() { first_instruction_ = last_instruction_ = nullptr; }
155
156 // Update the block of all instructions to be `block`.
157 void SetBlockOfInstructions(HBasicBlock* block) const;
158
159 void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list);
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000160 void AddBefore(HInstruction* cursor, const HInstructionList& instruction_list);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000161 void Add(const HInstructionList& instruction_list);
162
David Brazdil2d7352b2015-04-20 14:52:42 +0100163 // Return the number of instructions in the list. This is an expensive operation.
164 size_t CountSize() const;
165
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100166 private:
167 HInstruction* first_instruction_;
168 HInstruction* last_instruction_;
169
170 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000171 friend class HGraph;
172 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100173 friend class HInstructionIterator;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100174 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100175
176 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
177};
178
David Brazdil4833f5a2015-12-16 10:37:39 +0000179class ReferenceTypeInfo : ValueObject {
180 public:
181 typedef Handle<mirror::Class> TypeHandle;
182
Vladimir Markoa1de9182016-02-25 11:37:38 +0000183 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact);
184
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700185 static ReferenceTypeInfo Create(TypeHandle type_handle) REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil94ab38f2016-06-21 17:48:19 +0100186 return Create(type_handle, type_handle->CannotBeAssignedFromOtherTypes());
187 }
188
Vladimir Markoa1de9182016-02-25 11:37:38 +0000189 static ReferenceTypeInfo CreateUnchecked(TypeHandle type_handle, bool is_exact) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000190 return ReferenceTypeInfo(type_handle, is_exact);
191 }
192
193 static ReferenceTypeInfo CreateInvalid() { return ReferenceTypeInfo(); }
194
Vladimir Markof39745e2016-01-26 12:16:55 +0000195 static bool IsValidHandle(TypeHandle handle) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000196 return handle.GetReference() != nullptr;
197 }
198
Vladimir Marko456307a2016-04-19 14:12:13 +0000199 bool IsValid() const {
David Brazdil4833f5a2015-12-16 10:37:39 +0000200 return IsValidHandle(type_handle_);
201 }
202
203 bool IsExact() const { return is_exact_; }
204
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700205 bool IsObjectClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000206 DCHECK(IsValid());
207 return GetTypeHandle()->IsObjectClass();
208 }
209
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700210 bool IsStringClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000211 DCHECK(IsValid());
212 return GetTypeHandle()->IsStringClass();
213 }
214
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700215 bool IsObjectArray() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000216 DCHECK(IsValid());
217 return IsArrayClass() && GetTypeHandle()->GetComponentType()->IsObjectClass();
218 }
219
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700220 bool IsInterface() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000221 DCHECK(IsValid());
222 return GetTypeHandle()->IsInterface();
223 }
224
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700225 bool IsArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000226 DCHECK(IsValid());
227 return GetTypeHandle()->IsArrayClass();
228 }
229
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700230 bool IsPrimitiveArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000231 DCHECK(IsValid());
232 return GetTypeHandle()->IsPrimitiveArray();
233 }
234
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700235 bool IsNonPrimitiveArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000236 DCHECK(IsValid());
237 return GetTypeHandle()->IsArrayClass() && !GetTypeHandle()->IsPrimitiveArray();
238 }
239
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700240 bool CanArrayHold(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000241 DCHECK(IsValid());
242 if (!IsExact()) return false;
243 if (!IsArrayClass()) return false;
244 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(rti.GetTypeHandle().Get());
245 }
246
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700247 bool CanArrayHoldValuesOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000248 DCHECK(IsValid());
249 if (!IsExact()) return false;
250 if (!IsArrayClass()) return false;
251 if (!rti.IsArrayClass()) return false;
252 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(
253 rti.GetTypeHandle()->GetComponentType());
254 }
255
256 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
257
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700258 bool IsSupertypeOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000259 DCHECK(IsValid());
260 DCHECK(rti.IsValid());
261 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
262 }
263
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700264 bool IsStrictSupertypeOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000265 DCHECK(IsValid());
266 DCHECK(rti.IsValid());
267 return GetTypeHandle().Get() != rti.GetTypeHandle().Get() &&
268 GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
269 }
270
271 // Returns true if the type information provide the same amount of details.
272 // Note that it does not mean that the instructions have the same actual type
273 // (because the type can be the result of a merge).
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700274 bool IsEqual(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000275 if (!IsValid() && !rti.IsValid()) {
276 // Invalid types are equal.
277 return true;
278 }
279 if (!IsValid() || !rti.IsValid()) {
280 // One is valid, the other not.
281 return false;
282 }
283 return IsExact() == rti.IsExact()
284 && GetTypeHandle().Get() == rti.GetTypeHandle().Get();
285 }
286
287 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +0000288 ReferenceTypeInfo() : type_handle_(TypeHandle()), is_exact_(false) {}
289 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact)
290 : type_handle_(type_handle), is_exact_(is_exact) { }
David Brazdil4833f5a2015-12-16 10:37:39 +0000291
292 // The class of the object.
293 TypeHandle type_handle_;
294 // Whether or not the type is exact or a superclass of the actual type.
295 // Whether or not we have any information about this type.
296 bool is_exact_;
297};
298
299std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
300
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000301// Control-flow graph of a method. Contains a list of basic blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100302class HGraph : public ArenaObject<kArenaAllocGraph> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000303 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100304 HGraph(ArenaAllocator* arena,
305 const DexFile& dex_file,
306 uint32_t method_idx,
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100307 bool should_generate_constructor_barrier,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700308 InstructionSet instruction_set,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100309 InvokeType invoke_type = kInvalidInvokeType,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100310 bool debuggable = false,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000311 bool osr = false,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100312 int start_instruction_id = 0)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000313 : arena_(arena),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100314 blocks_(arena->Adapter(kArenaAllocBlockList)),
315 reverse_post_order_(arena->Adapter(kArenaAllocReversePostOrder)),
316 linear_order_(arena->Adapter(kArenaAllocLinearOrder)),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700317 entry_block_(nullptr),
318 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100319 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100320 number_of_vregs_(0),
321 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000322 temporaries_vreg_slots_(0),
Mark Mendell1152c922015-04-24 17:06:35 -0400323 has_bounds_checks_(false),
David Brazdil77a48ae2015-09-15 12:34:04 +0000324 has_try_catch_(false),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000325 has_irreducible_loops_(false),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000326 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000327 current_instruction_id_(start_instruction_id),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100328 dex_file_(dex_file),
329 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100330 invoke_type_(invoke_type),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100331 in_ssa_form_(false),
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100332 should_generate_constructor_barrier_(should_generate_constructor_barrier),
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800333 number_of_cha_guards_(0),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700334 instruction_set_(instruction_set),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000335 cached_null_constant_(nullptr),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100336 cached_int_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
337 cached_float_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
338 cached_long_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
339 cached_double_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
David Brazdil4833f5a2015-12-16 10:37:39 +0000340 cached_current_method_(nullptr),
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000341 inexact_object_rti_(ReferenceTypeInfo::CreateInvalid()),
Mingyao Yang063fc772016-08-02 11:02:54 -0700342 osr_(osr),
343 cha_single_implementation_list_(arena->Adapter(kArenaAllocCHA)) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100344 blocks_.reserve(kDefaultNumberOfBlocks);
345 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000346
David Brazdilbadd8262016-02-02 16:28:56 +0000347 // Acquires and stores RTI of inexact Object to be used when creating HNullConstant.
Mathieu Chartiere8a3c572016-10-11 16:52:17 -0700348 void InitializeInexactObjectRTI(VariableSizedHandleScope* handles);
David Brazdilbadd8262016-02-02 16:28:56 +0000349
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000350 ArenaAllocator* GetArena() const { return arena_; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100351 const ArenaVector<HBasicBlock*>& GetBlocks() const { return blocks_; }
352
David Brazdil69ba7b72015-06-23 18:27:30 +0100353 bool IsInSsaForm() const { return in_ssa_form_; }
David Brazdilbadd8262016-02-02 16:28:56 +0000354 void SetInSsaForm() { in_ssa_form_ = true; }
David Brazdil69ba7b72015-06-23 18:27:30 +0100355
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000356 HBasicBlock* GetEntryBlock() const { return entry_block_; }
357 HBasicBlock* GetExitBlock() const { return exit_block_; }
David Brazdilc7af85d2015-05-26 12:05:55 +0100358 bool HasExitBlock() const { return exit_block_ != nullptr; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000359
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000360 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
361 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000362
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000363 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100364
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100365 void ComputeDominanceInformation();
366 void ClearDominanceInformation();
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000367 void ClearLoopInformation();
368 void FindBackEdges(ArenaBitVector* visited);
369 GraphAnalysisResult BuildDominatorTree();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100370 void SimplifyCFG();
David Brazdilffee3d32015-07-06 11:48:53 +0100371 void SimplifyCatchBlocks();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000372
David Brazdil4833f5a2015-12-16 10:37:39 +0000373 // Analyze all natural loops in this graph. Returns a code specifying that it
374 // was successful or the reason for failure. The method will fail if a loop
David Brazdil4833f5a2015-12-16 10:37:39 +0000375 // is a throw-catch loop, i.e. the header is a catch block.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000376 GraphAnalysisResult AnalyzeLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100377
David Brazdilffee3d32015-07-06 11:48:53 +0100378 // Iterate over blocks to compute try block membership. Needs reverse post
379 // order and loop information.
380 void ComputeTryBlockInformation();
381
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000382 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
Nicolas Geoffray55bd7492016-02-16 15:37:12 +0000383 // Returns the instruction to replace the invoke expression or null if the
384 // invoke is for a void method. Note that the caller is responsible for replacing
385 // and removing the invoke instruction.
Calin Juravle2e768302015-07-28 14:41:11 +0000386 HInstruction* InlineInto(HGraph* outer_graph, HInvoke* invoke);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000387
Nicolas Geoffraya1d8ddf2016-02-29 11:46:58 +0000388 // Update the loop and try membership of `block`, which was spawned from `reference`.
389 // In case `reference` is a back edge, `replace_if_back_edge` notifies whether `block`
390 // should be the new back edge.
391 void UpdateLoopAndTryInformationOfNewBlock(HBasicBlock* block,
392 HBasicBlock* reference,
393 bool replace_if_back_edge);
394
Mingyao Yang3584bce2015-05-19 16:01:59 -0700395 // Need to add a couple of blocks to test if the loop body is entered and
396 // put deoptimization instructions, etc.
397 void TransformLoopHeaderForBCE(HBasicBlock* header);
398
David Brazdil8a7c0fe2015-11-02 20:24:55 +0000399 // Removes `block` from the graph. Assumes `block` has been disconnected from
400 // other blocks and has no instructions or phis.
401 void DeleteDeadEmptyBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000402
David Brazdilfc6a86a2015-06-26 10:33:45 +0000403 // Splits the edge between `block` and `successor` while preserving the
404 // indices in the predecessor/successor lists. If there are multiple edges
405 // between the blocks, the lowest indices are used.
406 // Returns the new block which is empty and has the same dex pc as `successor`.
407 HBasicBlock* SplitEdge(HBasicBlock* block, HBasicBlock* successor);
408
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100409 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
410 void SimplifyLoop(HBasicBlock* header);
411
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000412 int32_t GetNextInstructionId() {
413 DCHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000414 return current_instruction_id_++;
415 }
416
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000417 int32_t GetCurrentInstructionId() const {
418 return current_instruction_id_;
419 }
420
421 void SetCurrentInstructionId(int32_t id) {
David Brazdil3f523062016-02-29 16:53:33 +0000422 DCHECK_GE(id, current_instruction_id_);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000423 current_instruction_id_ = id;
424 }
425
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100426 uint16_t GetMaximumNumberOfOutVRegs() const {
427 return maximum_number_of_out_vregs_;
428 }
429
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000430 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
431 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100432 }
433
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100434 void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) {
435 maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value);
436 }
437
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000438 void UpdateTemporariesVRegSlots(size_t slots) {
439 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100440 }
441
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000442 size_t GetTemporariesVRegSlots() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100443 DCHECK(!in_ssa_form_);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000444 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100445 }
446
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100447 void SetNumberOfVRegs(uint16_t number_of_vregs) {
448 number_of_vregs_ = number_of_vregs;
449 }
450
451 uint16_t GetNumberOfVRegs() const {
452 return number_of_vregs_;
453 }
454
455 void SetNumberOfInVRegs(uint16_t value) {
456 number_of_in_vregs_ = value;
457 }
458
David Brazdildee58d62016-04-07 09:54:26 +0000459 uint16_t GetNumberOfInVRegs() const {
460 return number_of_in_vregs_;
461 }
462
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100463 uint16_t GetNumberOfLocalVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100464 DCHECK(!in_ssa_form_);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100465 return number_of_vregs_ - number_of_in_vregs_;
466 }
467
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100468 const ArenaVector<HBasicBlock*>& GetReversePostOrder() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100469 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100470 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100471
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100472 ArrayRef<HBasicBlock* const> GetReversePostOrderSkipEntryBlock() {
473 DCHECK(GetReversePostOrder()[0] == entry_block_);
474 return ArrayRef<HBasicBlock* const>(GetReversePostOrder()).SubArray(1);
475 }
476
477 IterationRange<ArenaVector<HBasicBlock*>::const_reverse_iterator> GetPostOrder() const {
478 return ReverseRange(GetReversePostOrder());
479 }
480
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100481 const ArenaVector<HBasicBlock*>& GetLinearOrder() const {
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100482 return linear_order_;
483 }
484
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100485 IterationRange<ArenaVector<HBasicBlock*>::const_reverse_iterator> GetLinearPostOrder() const {
486 return ReverseRange(GetLinearOrder());
487 }
488
Mark Mendell1152c922015-04-24 17:06:35 -0400489 bool HasBoundsChecks() const {
490 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800491 }
492
Mark Mendell1152c922015-04-24 17:06:35 -0400493 void SetHasBoundsChecks(bool value) {
494 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800495 }
496
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100497 bool ShouldGenerateConstructorBarrier() const {
498 return should_generate_constructor_barrier_;
499 }
500
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000501 bool IsDebuggable() const { return debuggable_; }
502
David Brazdil8d5b8b22015-03-24 10:51:52 +0000503 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000504 // already, it is created and inserted into the graph. This method is only for
505 // integral types.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600506 HConstant* GetConstant(Primitive::Type type, int64_t value, uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000507
508 // TODO: This is problematic for the consistency of reference type propagation
509 // because it can be created anytime after the pass and thus it will be left
510 // with an invalid type.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600511 HNullConstant* GetNullConstant(uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000512
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600513 HIntConstant* GetIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc) {
514 return CreateConstant(value, &cached_int_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000515 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600516 HLongConstant* GetLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc) {
517 return CreateConstant(value, &cached_long_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000518 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600519 HFloatConstant* GetFloatConstant(float value, uint32_t dex_pc = kNoDexPc) {
520 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000521 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600522 HDoubleConstant* GetDoubleConstant(double value, uint32_t dex_pc = kNoDexPc) {
523 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000524 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000525
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100526 HCurrentMethod* GetCurrentMethod();
527
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100528 const DexFile& GetDexFile() const {
529 return dex_file_;
530 }
531
532 uint32_t GetMethodIdx() const {
533 return method_idx_;
534 }
535
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100536 InvokeType GetInvokeType() const {
537 return invoke_type_;
538 }
539
Mark Mendellc4701932015-04-10 13:18:51 -0400540 InstructionSet GetInstructionSet() const {
541 return instruction_set_;
542 }
543
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000544 bool IsCompilingOsr() const { return osr_; }
545
Mingyao Yang063fc772016-08-02 11:02:54 -0700546 ArenaSet<ArtMethod*>& GetCHASingleImplementationList() {
547 return cha_single_implementation_list_;
548 }
549
550 void AddCHASingleImplementationDependency(ArtMethod* method) {
551 cha_single_implementation_list_.insert(method);
552 }
553
554 bool HasShouldDeoptimizeFlag() const {
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800555 return number_of_cha_guards_ != 0;
Mingyao Yang063fc772016-08-02 11:02:54 -0700556 }
557
David Brazdil77a48ae2015-09-15 12:34:04 +0000558 bool HasTryCatch() const { return has_try_catch_; }
559 void SetHasTryCatch(bool value) { has_try_catch_ = value; }
David Brazdilbbd733e2015-08-18 17:48:17 +0100560
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000561 bool HasIrreducibleLoops() const { return has_irreducible_loops_; }
562 void SetHasIrreducibleLoops(bool value) { has_irreducible_loops_ = value; }
563
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100564 ArtMethod* GetArtMethod() const { return art_method_; }
565 void SetArtMethod(ArtMethod* method) { art_method_ = method; }
566
Mark Mendellf6529172015-11-17 11:16:56 -0500567 // Returns an instruction with the opposite boolean value from 'cond'.
568 // The instruction has been inserted into the graph, either as a constant, or
569 // before cursor.
570 HInstruction* InsertOppositeCondition(HInstruction* cond, HInstruction* cursor);
571
Nicolas Geoffray18401b72016-03-11 13:35:51 +0000572 ReferenceTypeInfo GetInexactObjectRti() const { return inexact_object_rti_; }
573
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800574 uint32_t GetNumberOfCHAGuards() { return number_of_cha_guards_; }
575 void SetNumberOfCHAGuards(uint32_t num) { number_of_cha_guards_ = num; }
576 void IncrementNumberOfCHAGuards() { number_of_cha_guards_++; }
577
David Brazdil2d7352b2015-04-20 14:52:42 +0100578 private:
Roland Levillainfc600dc2014-12-02 17:16:31 +0000579 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100580 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000581
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000582 template <class InstructionType, typename ValueType>
583 InstructionType* CreateConstant(ValueType value,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600584 ArenaSafeMap<ValueType, InstructionType*>* cache,
585 uint32_t dex_pc = kNoDexPc) {
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000586 // Try to find an existing constant of the given value.
587 InstructionType* constant = nullptr;
588 auto cached_constant = cache->find(value);
589 if (cached_constant != cache->end()) {
590 constant = cached_constant->second;
591 }
592
593 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100594 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000595 if (constant == nullptr || constant->GetBlock() == nullptr) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600596 constant = new (arena_) InstructionType(value, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000597 cache->Overwrite(value, constant);
598 InsertConstant(constant);
599 }
600 return constant;
601 }
602
David Brazdil8d5b8b22015-03-24 10:51:52 +0000603 void InsertConstant(HConstant* instruction);
604
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000605 // Cache a float constant into the graph. This method should only be
606 // called by the SsaBuilder when creating "equivalent" instructions.
607 void CacheFloatConstant(HFloatConstant* constant);
608
609 // See CacheFloatConstant comment.
610 void CacheDoubleConstant(HDoubleConstant* constant);
611
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000612 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000613
614 // List of blocks in insertion order.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100615 ArenaVector<HBasicBlock*> blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000616
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100617 // List of blocks to perform a reverse post order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100618 ArenaVector<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000619
Aart Bik281c6812016-08-26 11:31:48 -0700620 // List of blocks to perform a linear order tree traversal. Unlike the reverse
621 // post order, this order is not incrementally kept up-to-date.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100622 ArenaVector<HBasicBlock*> linear_order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100623
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000624 HBasicBlock* entry_block_;
625 HBasicBlock* exit_block_;
626
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100627 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100628 uint16_t maximum_number_of_out_vregs_;
629
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100630 // The number of virtual registers in this method. Contains the parameters.
631 uint16_t number_of_vregs_;
632
633 // The number of virtual registers used by parameters of this method.
634 uint16_t number_of_in_vregs_;
635
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000636 // Number of vreg size slots that the temporaries use (used in baseline compiler).
637 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100638
Mark Mendell1152c922015-04-24 17:06:35 -0400639 // Has bounds checks. We can totally skip BCE if it's false.
640 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800641
David Brazdil77a48ae2015-09-15 12:34:04 +0000642 // Flag whether there are any try/catch blocks in the graph. We will skip
643 // try/catch-related passes if false.
644 bool has_try_catch_;
645
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000646 // Flag whether there are any irreducible loops in the graph.
647 bool has_irreducible_loops_;
648
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000649 // Indicates whether the graph should be compiled in a way that
650 // ensures full debuggability. If false, we can apply more
651 // aggressive optimizations that may limit the level of debugging.
652 const bool debuggable_;
653
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000654 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000655 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000656
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100657 // The dex file from which the method is from.
658 const DexFile& dex_file_;
659
660 // The method index in the dex file.
661 const uint32_t method_idx_;
662
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100663 // If inlined, this encodes how the callee is being invoked.
664 const InvokeType invoke_type_;
665
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100666 // Whether the graph has been transformed to SSA form. Only used
667 // in debug mode to ensure we are not using properties only valid
668 // for non-SSA form (like the number of temporaries).
669 bool in_ssa_form_;
670
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100671 const bool should_generate_constructor_barrier_;
672
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800673 // Number of CHA guards in the graph. Used to short-circuit the
674 // CHA guard optimization pass when there is no CHA guard left.
675 uint32_t number_of_cha_guards_;
676
Mathieu Chartiere401d142015-04-22 13:56:20 -0700677 const InstructionSet instruction_set_;
678
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000679 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000680 HNullConstant* cached_null_constant_;
681 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000682 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000683 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000684 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000685
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100686 HCurrentMethod* cached_current_method_;
687
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100688 // The ArtMethod this graph is for. Note that for AOT, it may be null,
689 // for example for methods whose declaring class could not be resolved
690 // (such as when the superclass could not be found).
691 ArtMethod* art_method_;
692
David Brazdil4833f5a2015-12-16 10:37:39 +0000693 // Keep the RTI of inexact Object to avoid having to pass stack handle
694 // collection pointer to passes which may create NullConstant.
695 ReferenceTypeInfo inexact_object_rti_;
696
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000697 // Whether we are compiling this graph for on stack replacement: this will
698 // make all loops seen as irreducible and emit special stack maps to mark
699 // compiled code entries which the interpreter can directly jump to.
700 const bool osr_;
701
Mingyao Yang063fc772016-08-02 11:02:54 -0700702 // List of methods that are assumed to have single implementation.
703 ArenaSet<ArtMethod*> cha_single_implementation_list_;
704
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000705 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100706 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000707 friend class HInliner; // For the reverse post order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000708 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000709 DISALLOW_COPY_AND_ASSIGN(HGraph);
710};
711
Vladimir Markof9f64412015-09-02 14:05:49 +0100712class HLoopInformation : public ArenaObject<kArenaAllocLoopInfo> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000713 public:
714 HLoopInformation(HBasicBlock* header, HGraph* graph)
715 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100716 suspend_check_(nullptr),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000717 irreducible_(false),
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100718 contains_irreducible_loop_(false),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100719 back_edges_(graph->GetArena()->Adapter(kArenaAllocLoopInfoBackEdges)),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100720 // Make bit vector growable, as the number of blocks may change.
Vladimir Markof6a35de2016-03-21 12:01:50 +0000721 blocks_(graph->GetArena(), graph->GetBlocks().size(), true, kArenaAllocLoopInfoBackEdges) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100722 back_edges_.reserve(kDefaultNumberOfBackEdges);
723 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100724
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000725 bool IsIrreducible() const { return irreducible_; }
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100726 bool ContainsIrreducibleLoop() const { return contains_irreducible_loop_; }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000727
728 void Dump(std::ostream& os);
729
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100730 HBasicBlock* GetHeader() const {
731 return header_;
732 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000733
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000734 void SetHeader(HBasicBlock* block) {
735 header_ = block;
736 }
737
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100738 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
739 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
740 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
741
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000742 void AddBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100743 back_edges_.push_back(back_edge);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000744 }
745
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100746 void RemoveBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100747 RemoveElement(back_edges_, back_edge);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100748 }
749
David Brazdil46e2a392015-03-16 17:31:52 +0000750 bool IsBackEdge(const HBasicBlock& block) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100751 return ContainsElement(back_edges_, &block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100752 }
753
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000754 size_t NumberOfBackEdges() const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100755 return back_edges_.size();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000756 }
757
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100758 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100759
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100760 const ArenaVector<HBasicBlock*>& GetBackEdges() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100761 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100762 }
763
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100764 // Returns the lifetime position of the back edge that has the
765 // greatest lifetime position.
766 size_t GetLifetimeEnd() const;
767
768 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100769 ReplaceElement(back_edges_, existing, new_back_edge);
Nicolas Geoffray57902602015-04-21 14:28:41 +0100770 }
771
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000772 // Finds blocks that are part of this loop.
773 void Populate();
David Brazdila4b8c212015-05-07 09:59:30 +0100774
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100775 // Returns whether this loop information contains `block`.
776 // Note that this loop information *must* be populated before entering this function.
777 bool Contains(const HBasicBlock& block) const;
778
779 // Returns whether this loop information is an inner loop of `other`.
780 // Note that `other` *must* be populated before entering this function.
781 bool IsIn(const HLoopInformation& other) const;
782
Mingyao Yang4b467ed2015-11-19 17:04:22 -0800783 // Returns true if instruction is not defined within this loop.
784 bool IsDefinedOutOfTheLoop(HInstruction* instruction) const;
Aart Bik73f1f3b2015-10-28 15:28:08 -0700785
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100786 const ArenaBitVector& GetBlocks() const { return blocks_; }
787
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000788 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000789 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000790
Nicolas Geoffray788f2f02016-01-22 12:41:38 +0000791 void ClearAllBlocks() {
792 blocks_.ClearAllBits();
793 }
794
David Brazdil3f4a5222016-05-06 12:46:21 +0100795 bool HasBackEdgeNotDominatedByHeader() const;
796
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100797 bool IsPopulated() const {
798 return blocks_.GetHighestBitSet() != -1;
799 }
800
Anton Shaminf89381f2016-05-16 16:44:13 +0600801 bool DominatesAllBackEdges(HBasicBlock* block);
802
David Sehrc757dec2016-11-04 15:48:34 -0700803 bool HasExitEdge() const;
804
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000805 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100806 // Internal recursive implementation of `Populate`.
807 void PopulateRecursive(HBasicBlock* block);
David Brazdilc2e8af92016-04-05 17:15:19 +0100808 void PopulateIrreducibleRecursive(HBasicBlock* block, ArenaBitVector* finalized);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100809
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000810 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100811 HSuspendCheck* suspend_check_;
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000812 bool irreducible_;
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100813 bool contains_irreducible_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100814 ArenaVector<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100815 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000816
817 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
818};
819
David Brazdilec16f792015-08-19 15:04:01 +0100820// Stores try/catch information for basic blocks.
821// Note that HGraph is constructed so that catch blocks cannot simultaneously
822// be try blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100823class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> {
David Brazdilec16f792015-08-19 15:04:01 +0100824 public:
825 // Try block information constructor.
826 explicit TryCatchInformation(const HTryBoundary& try_entry)
827 : try_entry_(&try_entry),
828 catch_dex_file_(nullptr),
829 catch_type_index_(DexFile::kDexNoIndex16) {
830 DCHECK(try_entry_ != nullptr);
831 }
832
833 // Catch block information constructor.
Andreas Gampea5b09a62016-11-17 15:21:22 -0800834 TryCatchInformation(dex::TypeIndex catch_type_index, const DexFile& dex_file)
David Brazdilec16f792015-08-19 15:04:01 +0100835 : try_entry_(nullptr),
836 catch_dex_file_(&dex_file),
837 catch_type_index_(catch_type_index) {}
838
839 bool IsTryBlock() const { return try_entry_ != nullptr; }
840
841 const HTryBoundary& GetTryEntry() const {
842 DCHECK(IsTryBlock());
843 return *try_entry_;
844 }
845
846 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
847
848 bool IsCatchAllTypeIndex() const {
849 DCHECK(IsCatchBlock());
Andreas Gampea5b09a62016-11-17 15:21:22 -0800850 return !catch_type_index_.IsValid();
David Brazdilec16f792015-08-19 15:04:01 +0100851 }
852
Andreas Gampea5b09a62016-11-17 15:21:22 -0800853 dex::TypeIndex GetCatchTypeIndex() const {
David Brazdilec16f792015-08-19 15:04:01 +0100854 DCHECK(IsCatchBlock());
855 return catch_type_index_;
856 }
857
858 const DexFile& GetCatchDexFile() const {
859 DCHECK(IsCatchBlock());
860 return *catch_dex_file_;
861 }
862
863 private:
864 // One of possibly several TryBoundary instructions entering the block's try.
865 // Only set for try blocks.
866 const HTryBoundary* try_entry_;
867
868 // Exception type information. Only set for catch blocks.
869 const DexFile* catch_dex_file_;
Andreas Gampea5b09a62016-11-17 15:21:22 -0800870 const dex::TypeIndex catch_type_index_;
David Brazdilec16f792015-08-19 15:04:01 +0100871};
872
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100873static constexpr size_t kNoLifetime = -1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100874static constexpr uint32_t kInvalidBlockId = static_cast<uint32_t>(-1);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100875
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000876// A block in a method. Contains the list of instructions represented
877// as a double linked list. Each block knows its predecessors and
878// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100879
Vladimir Markof9f64412015-09-02 14:05:49 +0100880class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000881 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -0700882 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000883 : graph_(graph),
Vladimir Marko60584552015-09-03 13:35:12 +0000884 predecessors_(graph->GetArena()->Adapter(kArenaAllocPredecessors)),
885 successors_(graph->GetArena()->Adapter(kArenaAllocSuccessors)),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000886 loop_information_(nullptr),
887 dominator_(nullptr),
Vladimir Marko60584552015-09-03 13:35:12 +0000888 dominated_blocks_(graph->GetArena()->Adapter(kArenaAllocDominated)),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100889 block_id_(kInvalidBlockId),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100890 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100891 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000892 lifetime_end_(kNoLifetime),
Vladimir Marko60584552015-09-03 13:35:12 +0000893 try_catch_information_(nullptr) {
894 predecessors_.reserve(kDefaultNumberOfPredecessors);
895 successors_.reserve(kDefaultNumberOfSuccessors);
896 dominated_blocks_.reserve(kDefaultNumberOfDominatedBlocks);
897 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000898
Vladimir Marko60584552015-09-03 13:35:12 +0000899 const ArenaVector<HBasicBlock*>& GetPredecessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100900 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000901 }
902
Vladimir Marko60584552015-09-03 13:35:12 +0000903 const ArenaVector<HBasicBlock*>& GetSuccessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100904 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000905 }
906
David Brazdild26a4112015-11-10 11:07:31 +0000907 ArrayRef<HBasicBlock* const> GetNormalSuccessors() const;
908 ArrayRef<HBasicBlock* const> GetExceptionalSuccessors() const;
909
Vladimir Marko60584552015-09-03 13:35:12 +0000910 bool HasSuccessor(const HBasicBlock* block, size_t start_from = 0u) {
911 return ContainsElement(successors_, block, start_from);
912 }
913
914 const ArenaVector<HBasicBlock*>& GetDominatedBlocks() const {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100915 return dominated_blocks_;
916 }
917
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100918 bool IsEntryBlock() const {
919 return graph_->GetEntryBlock() == this;
920 }
921
922 bool IsExitBlock() const {
923 return graph_->GetExitBlock() == this;
924 }
925
David Brazdil46e2a392015-03-16 17:31:52 +0000926 bool IsSingleGoto() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000927 bool IsSingleTryBoundary() const;
928
929 // Returns true if this block emits nothing but a jump.
930 bool IsSingleJump() const {
931 HLoopInformation* loop_info = GetLoopInformation();
932 return (IsSingleGoto() || IsSingleTryBoundary())
933 // Back edges generate a suspend check.
934 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
935 }
David Brazdil46e2a392015-03-16 17:31:52 +0000936
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000937 void AddBackEdge(HBasicBlock* back_edge) {
938 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000939 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000940 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100941 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000942 loop_information_->AddBackEdge(back_edge);
943 }
944
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000945 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000946 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000947
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100948 uint32_t GetBlockId() const { return block_id_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000949 void SetBlockId(int id) { block_id_ = id; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600950 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000951
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000952 HBasicBlock* GetDominator() const { return dominator_; }
953 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Vladimir Marko60584552015-09-03 13:35:12 +0000954 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.push_back(block); }
955
956 void RemoveDominatedBlock(HBasicBlock* block) {
957 RemoveElement(dominated_blocks_, block);
Vladimir Marko91e11c02015-09-02 17:03:22 +0100958 }
Vladimir Marko60584552015-09-03 13:35:12 +0000959
960 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
961 ReplaceElement(dominated_blocks_, existing, new_block);
962 }
963
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100964 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000965
966 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100967 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000968 }
969
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100970 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
971 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100972 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100973 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +0100974 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
975 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000976
Nicolas Geoffray09aa1472016-01-19 10:52:54 +0000977 HInstruction* GetFirstInstructionDisregardMoves() const;
978
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000979 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000980 successors_.push_back(block);
981 block->predecessors_.push_back(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000982 }
983
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100984 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
985 size_t successor_index = GetSuccessorIndexOf(existing);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100986 existing->RemovePredecessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000987 new_block->predecessors_.push_back(this);
988 successors_[successor_index] = new_block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000989 }
990
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000991 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
992 size_t predecessor_index = GetPredecessorIndexOf(existing);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000993 existing->RemoveSuccessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000994 new_block->successors_.push_back(this);
995 predecessors_[predecessor_index] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000996 }
997
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100998 // Insert `this` between `predecessor` and `successor. This method
999 // preserves the indicies, and will update the first edge found between
1000 // `predecessor` and `successor`.
1001 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
1002 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001003 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko60584552015-09-03 13:35:12 +00001004 successor->predecessors_[predecessor_index] = this;
1005 predecessor->successors_[successor_index] = this;
1006 successors_.push_back(successor);
1007 predecessors_.push_back(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001008 }
1009
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001010 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001011 predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001012 }
1013
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001014 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001015 successors_.erase(successors_.begin() + GetSuccessorIndexOf(block));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001016 }
1017
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001018 void ClearAllPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001019 predecessors_.clear();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001020 }
1021
1022 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001023 predecessors_.push_back(block);
1024 block->successors_.push_back(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001025 }
1026
Nicolas Geoffray604c6e42014-09-17 12:08:44 +01001027 void SwapPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001028 DCHECK_EQ(predecessors_.size(), 2u);
1029 std::swap(predecessors_[0], predecessors_[1]);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +01001030 }
1031
David Brazdil769c9e52015-04-27 13:54:09 +01001032 void SwapSuccessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001033 DCHECK_EQ(successors_.size(), 2u);
1034 std::swap(successors_[0], successors_[1]);
David Brazdil769c9e52015-04-27 13:54:09 +01001035 }
1036
David Brazdilfc6a86a2015-06-26 10:33:45 +00001037 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko60584552015-09-03 13:35:12 +00001038 return IndexOfElement(predecessors_, predecessor);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001039 }
1040
David Brazdilfc6a86a2015-06-26 10:33:45 +00001041 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko60584552015-09-03 13:35:12 +00001042 return IndexOfElement(successors_, successor);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001043 }
1044
David Brazdilfc6a86a2015-06-26 10:33:45 +00001045 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00001046 DCHECK_EQ(GetPredecessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +01001047 return GetPredecessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +00001048 }
1049
1050 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00001051 DCHECK_EQ(GetSuccessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +01001052 return GetSuccessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +00001053 }
1054
1055 // Returns whether the first occurrence of `predecessor` in the list of
1056 // predecessors is at index `idx`.
1057 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01001058 DCHECK_EQ(GetPredecessors()[idx], predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +00001059 return GetPredecessorIndexOf(predecessor) == idx;
1060 }
1061
David Brazdild7558da2015-09-22 13:04:14 +01001062 // Create a new block between this block and its predecessors. The new block
1063 // is added to the graph, all predecessor edges are relinked to it and an edge
1064 // is created to `this`. Returns the new empty block. Reverse post order or
1065 // loop and try/catch information are not updated.
1066 HBasicBlock* CreateImmediateDominator();
1067
David Brazdilfc6a86a2015-06-26 10:33:45 +00001068 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +01001069 // created, latter block. Note that this method will add the block to the
1070 // graph, create a Goto at the end of the former block and will create an edge
1071 // between the blocks. It will not, however, update the reverse post order or
David Brazdild7558da2015-09-22 13:04:14 +01001072 // loop and try/catch information.
David Brazdilfc6a86a2015-06-26 10:33:45 +00001073 HBasicBlock* SplitBefore(HInstruction* cursor);
1074
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001075 // Split the block into two blocks just before `cursor`. Returns the newly
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001076 // created block. Note that this method just updates raw block information,
1077 // like predecessors, successors, dominators, and instruction list. It does not
1078 // update the graph, reverse post order, loop information, nor make sure the
1079 // blocks are consistent (for example ending with a control flow instruction).
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001080 HBasicBlock* SplitBeforeForInlining(HInstruction* cursor);
1081
1082 // Similar to `SplitBeforeForInlining` but does it after `cursor`.
1083 HBasicBlock* SplitAfterForInlining(HInstruction* cursor);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001084
1085 // Merge `other` at the end of `this`. Successors and dominated blocks of
1086 // `other` are changed to be successors and dominated blocks of `this`. Note
1087 // that this method does not update the graph, reverse post order, loop
1088 // information, nor make sure the blocks are consistent (for example ending
1089 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +01001090 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001091
1092 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
1093 // of `this` are moved to `other`.
1094 // Note that this method does not update the graph, reverse post order, loop
1095 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +00001096 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001097 void ReplaceWith(HBasicBlock* other);
1098
David Brazdil2d7352b2015-04-20 14:52:42 +01001099 // Merge `other` at the end of `this`. This method updates loops, reverse post
1100 // order, links to predecessors, successors, dominators and deletes the block
1101 // from the graph. The two blocks must be successive, i.e. `this` the only
1102 // predecessor of `other` and vice versa.
1103 void MergeWith(HBasicBlock* other);
1104
1105 // Disconnects `this` from all its predecessors, successors and dominator,
1106 // removes it from all loops it is included in and eventually from the graph.
1107 // The block must not dominate any other block. Predecessors and successors
1108 // are safely updated.
1109 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +00001110
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001111 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001112 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001113 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001114 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +01001115 // Replace instruction `initial` with `replacement` within this block.
1116 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
1117 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001118 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001119 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +00001120 // RemoveInstruction and RemovePhi delete a given instruction from the respective
1121 // instruction list. With 'ensure_safety' set to true, it verifies that the
1122 // instruction is not in use and removes it from the use lists of its inputs.
1123 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
1124 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +01001125 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001126
1127 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +01001128 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001129 }
1130
Roland Levillain6b879dd2014-09-22 17:13:44 +01001131 bool IsLoopPreHeaderFirstPredecessor() const {
1132 DCHECK(IsLoopHeader());
Vladimir Markoec7802a2015-10-01 20:57:57 +01001133 return GetPredecessors()[0] == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +01001134 }
1135
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001136 bool IsFirstPredecessorBackEdge() const {
1137 DCHECK(IsLoopHeader());
1138 return GetLoopInformation()->IsBackEdge(*GetPredecessors()[0]);
1139 }
1140
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001141 HLoopInformation* GetLoopInformation() const {
1142 return loop_information_;
1143 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001144
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001145 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001146 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001147 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001148 void SetInLoop(HLoopInformation* info) {
1149 if (IsLoopHeader()) {
1150 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +01001151 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001152 loop_information_ = info;
1153 } else if (loop_information_->Contains(*info->GetHeader())) {
1154 // Block is currently part of an outer loop. Make it part of this inner loop.
1155 // Note that a non loop header having a loop information means this loop information
1156 // has already been populated
1157 loop_information_ = info;
1158 } else {
1159 // Block is part of an inner loop. Do not update the loop information.
1160 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
1161 // at this point, because this method is being called while populating `info`.
1162 }
1163 }
1164
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001165 // Raw update of the loop information.
1166 void SetLoopInformation(HLoopInformation* info) {
1167 loop_information_ = info;
1168 }
1169
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001170 bool IsInLoop() const { return loop_information_ != nullptr; }
1171
David Brazdilec16f792015-08-19 15:04:01 +01001172 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
1173
1174 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
1175 try_catch_information_ = try_catch_information;
1176 }
1177
1178 bool IsTryBlock() const {
1179 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
1180 }
1181
1182 bool IsCatchBlock() const {
1183 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
1184 }
David Brazdilffee3d32015-07-06 11:48:53 +01001185
1186 // Returns the try entry that this block's successors should have. They will
1187 // be in the same try, unless the block ends in a try boundary. In that case,
1188 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +01001189 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001190
David Brazdild7558da2015-09-22 13:04:14 +01001191 bool HasThrowingInstructions() const;
1192
David Brazdila4b8c212015-05-07 09:59:30 +01001193 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001194 bool Dominates(HBasicBlock* block) const;
1195
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001196 size_t GetLifetimeStart() const { return lifetime_start_; }
1197 size_t GetLifetimeEnd() const { return lifetime_end_; }
1198
1199 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
1200 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
1201
David Brazdil8d5b8b22015-03-24 10:51:52 +00001202 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001203 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001204 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001205 bool HasSinglePhi() const;
1206
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001207 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001208 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +00001209 ArenaVector<HBasicBlock*> predecessors_;
1210 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001211 HInstructionList instructions_;
1212 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001213 HLoopInformation* loop_information_;
1214 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +00001215 ArenaVector<HBasicBlock*> dominated_blocks_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001216 uint32_t block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001217 // The dex program counter of the first instruction of this block.
1218 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001219 size_t lifetime_start_;
1220 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +01001221 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +01001222
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001223 friend class HGraph;
1224 friend class HInstruction;
1225
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001226 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
1227};
1228
David Brazdilb2bd1c52015-03-25 11:17:37 +00001229// Iterates over the LoopInformation of all loops which contain 'block'
1230// from the innermost to the outermost.
1231class HLoopInformationOutwardIterator : public ValueObject {
1232 public:
1233 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
1234 : current_(block.GetLoopInformation()) {}
1235
1236 bool Done() const { return current_ == nullptr; }
1237
1238 void Advance() {
1239 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +01001240 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +00001241 }
1242
1243 HLoopInformation* Current() const {
1244 DCHECK(!Done());
1245 return current_;
1246 }
1247
1248 private:
1249 HLoopInformation* current_;
1250
1251 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
1252};
1253
Alexandre Ramesef20f712015-06-09 10:29:30 +01001254#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Aart Bike9f37602015-10-09 11:15:55 -07001255 M(Above, Condition) \
1256 M(AboveOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001257 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001258 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001259 M(ArrayGet, Instruction) \
1260 M(ArrayLength, Instruction) \
1261 M(ArraySet, Instruction) \
Aart Bike9f37602015-10-09 11:15:55 -07001262 M(Below, Condition) \
1263 M(BelowOrEqual, Condition) \
David Brazdil66d126e2015-04-03 16:02:44 +01001264 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001265 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001266 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001267 M(CheckCast, Instruction) \
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00001268 M(ClassTableGet, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001269 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001270 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001271 M(Compare, BinaryOperation) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001272 M(CurrentMethod, Instruction) \
Mingyao Yang063fc772016-08-02 11:02:54 -07001273 M(ShouldDeoptimizeFlag, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001274 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001275 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001276 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001277 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001278 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001279 M(Exit, Instruction) \
1280 M(FloatConstant, Constant) \
1281 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001282 M(GreaterThan, Condition) \
1283 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001284 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001285 M(InstanceFieldGet, Instruction) \
1286 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001287 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001288 M(IntConstant, Constant) \
Calin Juravle175dc732015-08-25 15:42:32 +01001289 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001290 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001291 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001292 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001293 M(LessThan, Condition) \
1294 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001295 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001296 M(LoadException, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001297 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001298 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +01001299 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001300 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001301 M(Mul, BinaryOperation) \
David Srbecky0cf44932015-12-09 14:09:59 +00001302 M(NativeDebugInfo, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001303 M(Neg, UnaryOperation) \
1304 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001305 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001306 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001307 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001308 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001309 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001310 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001311 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001312 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001313 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001314 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001315 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001316 M(Return, Instruction) \
1317 M(ReturnVoid, Instruction) \
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001318 M(Ror, BinaryOperation) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001319 M(Shl, BinaryOperation) \
1320 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001321 M(StaticFieldGet, Instruction) \
1322 M(StaticFieldSet, Instruction) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001323 M(UnresolvedInstanceFieldGet, Instruction) \
1324 M(UnresolvedInstanceFieldSet, Instruction) \
1325 M(UnresolvedStaticFieldGet, Instruction) \
1326 M(UnresolvedStaticFieldSet, Instruction) \
David Brazdil74eb1b22015-12-14 11:44:01 +00001327 M(Select, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001328 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001329 M(SuspendCheck, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001330 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001331 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001332 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001333 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001334 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001335
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001336/*
1337 * Instructions, shared across several (not all) architectures.
1338 */
1339#if !defined(ART_ENABLE_CODEGEN_arm) && !defined(ART_ENABLE_CODEGEN_arm64)
1340#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M)
1341#else
1342#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Artem Serov7fc63502016-02-09 17:15:29 +00001343 M(BitwiseNegatedRight, Instruction) \
Artem Serov328429f2016-07-06 16:23:04 +01001344 M(MultiplyAccumulate, Instruction) \
1345 M(IntermediateAddress, Instruction)
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001346#endif
1347
Vladimir Markob4536b72015-11-24 13:45:23 +00001348#ifndef ART_ENABLE_CODEGEN_arm
Alexandre Ramesef20f712015-06-09 10:29:30 +01001349#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
Vladimir Markob4536b72015-11-24 13:45:23 +00001350#else
1351#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1352 M(ArmDexCacheArraysBase, Instruction)
1353#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001354
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001355#ifndef ART_ENABLE_CODEGEN_arm64
Alexandre Ramesef20f712015-06-09 10:29:30 +01001356#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001357#else
1358#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Artem Serov328429f2016-07-06 16:23:04 +01001359 M(Arm64DataProcWithShifterOp, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001360#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001361
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001362#ifndef ART_ENABLE_CODEGEN_mips
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001363#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001364#else
1365#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
1366 M(MipsComputeBaseMethodAddress, Instruction) \
Alexey Frunze96b66822016-09-10 02:32:44 -07001367 M(MipsDexCacheArraysBase, Instruction) \
1368 M(MipsPackedSwitch, Instruction)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001369#endif
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001370
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001371#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1372
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001373#ifndef ART_ENABLE_CODEGEN_x86
1374#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1375#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001376#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1377 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001378 M(X86LoadFromConstantTable, Instruction) \
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001379 M(X86FPNeg, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001380 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001381#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001382
1383#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1384
1385#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1386 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001387 FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001388 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1389 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001390 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001391 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001392 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1393 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1394
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001395#define FOR_EACH_ABSTRACT_INSTRUCTION(M) \
1396 M(Condition, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001397 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001398 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001399 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001400 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -07001401
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001402#define FOR_EACH_INSTRUCTION(M) \
1403 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1404 FOR_EACH_ABSTRACT_INSTRUCTION(M)
1405
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001406#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001407FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1408#undef FORWARD_DECLARATION
1409
Vladimir Marko372f10e2016-05-17 16:30:10 +01001410#define DECLARE_INSTRUCTION(type) \
1411 InstructionKind GetKindInternal() const OVERRIDE { return k##type; } \
1412 const char* DebugName() const OVERRIDE { return #type; } \
1413 bool InstructionTypeEquals(const HInstruction* other) const OVERRIDE { \
1414 return other->Is##type(); \
1415 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001416 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001417
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001418#define DECLARE_ABSTRACT_INSTRUCTION(type) \
1419 bool Is##type() const { return As##type() != nullptr; } \
1420 const H##type* As##type() const { return this; } \
1421 H##type* As##type() { return this; }
1422
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001423template <typename T>
Vladimir Markof9f64412015-09-02 14:05:49 +01001424class HUseListNode : public ArenaObject<kArenaAllocUseListNode> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001425 public:
David Brazdiled596192015-01-23 10:39:45 +00001426 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001427 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001428 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001429
Vladimir Marko46817b82016-03-29 12:21:58 +01001430 // Hook for the IntrusiveForwardList<>.
1431 // TODO: Hide this better.
1432 IntrusiveForwardListHook hook;
1433
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001434 private:
David Brazdiled596192015-01-23 10:39:45 +00001435 HUseListNode(T user, size_t index)
Vladimir Marko46817b82016-03-29 12:21:58 +01001436 : user_(user), index_(index) {}
David Brazdiled596192015-01-23 10:39:45 +00001437
1438 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001439 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001440
Vladimir Marko46817b82016-03-29 12:21:58 +01001441 friend class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001442
1443 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1444};
1445
David Brazdiled596192015-01-23 10:39:45 +00001446template <typename T>
Vladimir Marko46817b82016-03-29 12:21:58 +01001447using HUseList = IntrusiveForwardList<HUseListNode<T>>;
David Brazdiled596192015-01-23 10:39:45 +00001448
David Brazdil1abb4192015-02-17 18:33:36 +00001449// This class is used by HEnvironment and HInstruction classes to record the
1450// instructions they use and pointers to the corresponding HUseListNodes kept
1451// by the used instructions.
1452template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001453class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001454 public:
Vladimir Marko46817b82016-03-29 12:21:58 +01001455 HUserRecord() : instruction_(nullptr), before_use_node_() {}
1456 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), before_use_node_() {}
David Brazdil1abb4192015-02-17 18:33:36 +00001457
Vladimir Marko46817b82016-03-29 12:21:58 +01001458 HUserRecord(const HUserRecord<T>& old_record, typename HUseList<T>::iterator before_use_node)
1459 : HUserRecord(old_record.instruction_, before_use_node) {}
1460 HUserRecord(HInstruction* instruction, typename HUseList<T>::iterator before_use_node)
1461 : instruction_(instruction), before_use_node_(before_use_node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001462 DCHECK(instruction_ != nullptr);
David Brazdil1abb4192015-02-17 18:33:36 +00001463 }
1464
1465 HInstruction* GetInstruction() const { return instruction_; }
Vladimir Marko46817b82016-03-29 12:21:58 +01001466 typename HUseList<T>::iterator GetBeforeUseNode() const { return before_use_node_; }
1467 typename HUseList<T>::iterator GetUseNode() const { return ++GetBeforeUseNode(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001468
1469 private:
1470 // Instruction used by the user.
1471 HInstruction* instruction_;
1472
Vladimir Marko46817b82016-03-29 12:21:58 +01001473 // Iterator before the corresponding entry in the use list kept by 'instruction_'.
1474 typename HUseList<T>::iterator before_use_node_;
David Brazdil1abb4192015-02-17 18:33:36 +00001475};
1476
Vladimir Markoe9004912016-06-16 16:50:52 +01001477// Helper class that extracts the input instruction from HUserRecord<HInstruction*>.
1478// This is used for HInstruction::GetInputs() to return a container wrapper providing
1479// HInstruction* values even though the underlying container has HUserRecord<>s.
1480struct HInputExtractor {
1481 HInstruction* operator()(HUserRecord<HInstruction*>& record) const {
1482 return record.GetInstruction();
1483 }
1484 const HInstruction* operator()(const HUserRecord<HInstruction*>& record) const {
1485 return record.GetInstruction();
1486 }
1487};
1488
1489using HInputsRef = TransformArrayRef<HUserRecord<HInstruction*>, HInputExtractor>;
1490using HConstInputsRef = TransformArrayRef<const HUserRecord<HInstruction*>, HInputExtractor>;
1491
Aart Bik854a02b2015-07-14 16:07:00 -07001492/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001493 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001494 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001495 * For write/read dependences on fields/arrays, the dependence analysis uses
1496 * type disambiguation (e.g. a float field write cannot modify the value of an
1497 * integer field read) and the access type (e.g. a reference array write cannot
1498 * modify the value of a reference field read [although it may modify the
1499 * reference fetch prior to reading the field, which is represented by its own
1500 * write/read dependence]). The analysis makes conservative points-to
1501 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1502 * the same, and any reference read depends on any reference read without
1503 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001504 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001505 * The internal representation uses 38-bit and is described in the table below.
1506 * The first line indicates the side effect, and for field/array accesses the
1507 * second line indicates the type of the access (in the order of the
1508 * Primitive::Type enum).
1509 * The two numbered lines below indicate the bit position in the bitfield (read
1510 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001511 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001512 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1513 * +-------------+---------+---------+--------------+---------+---------+
1514 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1515 * | 3 |333333322|222222221| 1 |111111110|000000000|
1516 * | 7 |654321098|765432109| 8 |765432109|876543210|
1517 *
1518 * Note that, to ease the implementation, 'changes' bits are least significant
1519 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001520 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001521class SideEffects : public ValueObject {
1522 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001523 SideEffects() : flags_(0) {}
1524
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001525 static SideEffects None() {
1526 return SideEffects(0);
1527 }
1528
1529 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001530 return SideEffects(kAllChangeBits | kAllDependOnBits);
1531 }
1532
1533 static SideEffects AllChanges() {
1534 return SideEffects(kAllChangeBits);
1535 }
1536
1537 static SideEffects AllDependencies() {
1538 return SideEffects(kAllDependOnBits);
1539 }
1540
1541 static SideEffects AllExceptGCDependency() {
1542 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1543 }
1544
1545 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001546 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001547 }
1548
Aart Bik34c3ba92015-07-20 14:08:59 -07001549 static SideEffects AllWrites() {
1550 return SideEffects(kAllWrites);
1551 }
1552
1553 static SideEffects AllReads() {
1554 return SideEffects(kAllReads);
1555 }
1556
1557 static SideEffects FieldWriteOfType(Primitive::Type type, bool is_volatile) {
1558 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001559 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001560 : SideEffects(TypeFlag(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001561 }
1562
Aart Bik854a02b2015-07-14 16:07:00 -07001563 static SideEffects ArrayWriteOfType(Primitive::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001564 return SideEffects(TypeFlag(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001565 }
1566
Aart Bik34c3ba92015-07-20 14:08:59 -07001567 static SideEffects FieldReadOfType(Primitive::Type type, bool is_volatile) {
1568 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001569 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001570 : SideEffects(TypeFlag(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001571 }
1572
1573 static SideEffects ArrayReadOfType(Primitive::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001574 return SideEffects(TypeFlag(type, kArrayReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001575 }
1576
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001577 static SideEffects CanTriggerGC() {
1578 return SideEffects(1ULL << kCanTriggerGCBit);
1579 }
1580
1581 static SideEffects DependsOnGC() {
1582 return SideEffects(1ULL << kDependsOnGCBit);
1583 }
1584
Aart Bik854a02b2015-07-14 16:07:00 -07001585 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001586 SideEffects Union(SideEffects other) const {
1587 return SideEffects(flags_ | other.flags_);
1588 }
1589
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001590 SideEffects Exclusion(SideEffects other) const {
1591 return SideEffects(flags_ & ~other.flags_);
1592 }
1593
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001594 void Add(SideEffects other) {
1595 flags_ |= other.flags_;
1596 }
1597
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001598 bool Includes(SideEffects other) const {
1599 return (other.flags_ & flags_) == other.flags_;
1600 }
1601
1602 bool HasSideEffects() const {
1603 return (flags_ & kAllChangeBits);
1604 }
1605
1606 bool HasDependencies() const {
1607 return (flags_ & kAllDependOnBits);
1608 }
1609
1610 // Returns true if there are no side effects or dependencies.
1611 bool DoesNothing() const {
1612 return flags_ == 0;
1613 }
1614
Aart Bik854a02b2015-07-14 16:07:00 -07001615 // Returns true if something is written.
1616 bool DoesAnyWrite() const {
1617 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001618 }
1619
Aart Bik854a02b2015-07-14 16:07:00 -07001620 // Returns true if something is read.
1621 bool DoesAnyRead() const {
1622 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001623 }
1624
Aart Bik854a02b2015-07-14 16:07:00 -07001625 // Returns true if potentially everything is written and read
1626 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001627 bool DoesAllReadWrite() const {
1628 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1629 }
1630
Aart Bik854a02b2015-07-14 16:07:00 -07001631 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001632 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001633 }
1634
Roland Levillain0d5a2812015-11-13 10:07:31 +00001635 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001636 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001637 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1638 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001639 }
1640
1641 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001642 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001643 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001644 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001645 for (int s = kLastBit; s >= 0; s--) {
1646 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1647 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1648 // This is a bit for the GC side effect.
1649 if (current_bit_is_set) {
1650 flags += "GC";
1651 }
Aart Bik854a02b2015-07-14 16:07:00 -07001652 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001653 } else {
1654 // This is a bit for the array/field analysis.
1655 // The underscore character stands for the 'can trigger GC' bit.
1656 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1657 if (current_bit_is_set) {
1658 flags += kDebug[s];
1659 }
1660 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1661 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1662 flags += "|";
1663 }
1664 }
Aart Bik854a02b2015-07-14 16:07:00 -07001665 }
1666 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001667 }
1668
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001669 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001670
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001671 private:
1672 static constexpr int kFieldArrayAnalysisBits = 9;
1673
1674 static constexpr int kFieldWriteOffset = 0;
1675 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1676 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1677 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1678
1679 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1680
1681 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1682 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1683 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1684 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1685
1686 static constexpr int kLastBit = kDependsOnGCBit;
1687 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1688
1689 // Aliases.
1690
1691 static_assert(kChangeBits == kDependOnBits,
1692 "the 'change' bits should match the 'depend on' bits.");
1693
1694 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1695 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1696 static constexpr uint64_t kAllWrites =
1697 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1698 static constexpr uint64_t kAllReads =
1699 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001700
Aart Bik854a02b2015-07-14 16:07:00 -07001701 // Translates type to bit flag.
1702 static uint64_t TypeFlag(Primitive::Type type, int offset) {
1703 CHECK_NE(type, Primitive::kPrimVoid);
1704 const uint64_t one = 1;
1705 const int shift = type; // 0-based consecutive enum
1706 DCHECK_LE(kFieldWriteOffset, shift);
1707 DCHECK_LT(shift, kArrayWriteOffset);
1708 return one << (type + offset);
1709 }
1710
1711 // Private constructor on direct flags value.
1712 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1713
1714 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001715};
1716
David Brazdiled596192015-01-23 10:39:45 +00001717// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001718class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001719 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001720 HEnvironment(ArenaAllocator* arena,
1721 size_t number_of_vregs,
1722 const DexFile& dex_file,
1723 uint32_t method_idx,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001724 uint32_t dex_pc,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001725 InvokeType invoke_type,
1726 HInstruction* holder)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001727 : vregs_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentVRegs)),
1728 locations_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001729 parent_(nullptr),
1730 dex_file_(dex_file),
1731 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001732 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001733 invoke_type_(invoke_type),
1734 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001735 }
1736
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001737 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001738 : HEnvironment(arena,
1739 to_copy.Size(),
1740 to_copy.GetDexFile(),
1741 to_copy.GetMethodIdx(),
1742 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001743 to_copy.GetInvokeType(),
1744 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001745
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001746 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001747 if (parent_ != nullptr) {
1748 parent_->SetAndCopyParentChain(allocator, parent);
1749 } else {
1750 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1751 parent_->CopyFrom(parent);
1752 if (parent->GetParent() != nullptr) {
1753 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1754 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001755 }
David Brazdiled596192015-01-23 10:39:45 +00001756 }
1757
Vladimir Marko71bf8092015-09-15 15:33:14 +01001758 void CopyFrom(const ArenaVector<HInstruction*>& locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001759 void CopyFrom(HEnvironment* environment);
1760
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001761 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1762 // input to the loop phi instead. This is for inserting instructions that
1763 // require an environment (like HDeoptimization) in the loop pre-header.
1764 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001765
1766 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001767 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001768 }
1769
1770 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001771 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001772 }
1773
David Brazdil1abb4192015-02-17 18:33:36 +00001774 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001775
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001776 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001777
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001778 HEnvironment* GetParent() const { return parent_; }
1779
1780 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001781 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001782 }
1783
1784 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001785 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001786 }
1787
1788 uint32_t GetDexPc() const {
1789 return dex_pc_;
1790 }
1791
1792 uint32_t GetMethodIdx() const {
1793 return method_idx_;
1794 }
1795
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001796 InvokeType GetInvokeType() const {
1797 return invoke_type_;
1798 }
1799
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001800 const DexFile& GetDexFile() const {
1801 return dex_file_;
1802 }
1803
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001804 HInstruction* GetHolder() const {
1805 return holder_;
1806 }
1807
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00001808
1809 bool IsFromInlinedInvoke() const {
1810 return GetParent() != nullptr;
1811 }
1812
David Brazdiled596192015-01-23 10:39:45 +00001813 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001814 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1815 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001816 HEnvironment* parent_;
1817 const DexFile& dex_file_;
1818 const uint32_t method_idx_;
1819 const uint32_t dex_pc_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001820 const InvokeType invoke_type_;
David Brazdiled596192015-01-23 10:39:45 +00001821
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001822 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001823 HInstruction* const holder_;
1824
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001825 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001826
1827 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1828};
1829
Vladimir Markof9f64412015-09-02 14:05:49 +01001830class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001831 public:
Calin Juravle154746b2015-10-06 15:46:54 +01001832 HInstruction(SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001833 : previous_(nullptr),
1834 next_(nullptr),
1835 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001836 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001837 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001838 ssa_index_(-1),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001839 packed_fields_(0u),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001840 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001841 locations_(nullptr),
1842 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001843 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001844 side_effects_(side_effects),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001845 reference_type_handle_(ReferenceTypeInfo::CreateInvalid().GetTypeHandle()) {
1846 SetPackedFlag<kFlagReferenceTypeIsExact>(ReferenceTypeInfo::CreateInvalid().IsExact());
1847 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001848
Dave Allison20dfc792014-06-16 20:44:29 -07001849 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001850
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001851#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001852 enum InstructionKind {
1853 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1854 };
1855#undef DECLARE_KIND
1856
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001857 HInstruction* GetNext() const { return next_; }
1858 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001859
Calin Juravle77520bc2015-01-12 18:45:46 +00001860 HInstruction* GetNextDisregardingMoves() const;
1861 HInstruction* GetPreviousDisregardingMoves() const;
1862
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001863 HBasicBlock* GetBlock() const { return block_; }
Roland Levillain9867bc72015-08-05 10:21:34 +01001864 ArenaAllocator* GetArena() const { return block_->GetGraph()->GetArena(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001865 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001866 bool IsInBlock() const { return block_ != nullptr; }
1867 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001868 bool IsLoopHeaderPhi() const { return IsPhi() && block_->IsLoopHeader(); }
1869 bool IsIrreducibleLoopHeaderPhi() const {
1870 return IsLoopHeaderPhi() && GetBlock()->GetLoopInformation()->IsIrreducible();
1871 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001872
Vladimir Marko372f10e2016-05-17 16:30:10 +01001873 virtual ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() = 0;
1874
1875 ArrayRef<const HUserRecord<HInstruction*>> GetInputRecords() const {
1876 // One virtual method is enough, just const_cast<> and then re-add the const.
1877 return ArrayRef<const HUserRecord<HInstruction*>>(
1878 const_cast<HInstruction*>(this)->GetInputRecords());
1879 }
1880
Vladimir Markoe9004912016-06-16 16:50:52 +01001881 HInputsRef GetInputs() {
1882 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01001883 }
1884
Vladimir Markoe9004912016-06-16 16:50:52 +01001885 HConstInputsRef GetInputs() const {
1886 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01001887 }
1888
1889 size_t InputCount() const { return GetInputRecords().size(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001890 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001891
Aart Bik2767f4b2016-10-28 15:03:53 -07001892 bool HasInput(HInstruction* input) const {
1893 for (const HInstruction* i : GetInputs()) {
1894 if (i == input) {
1895 return true;
1896 }
1897 }
1898 return false;
1899 }
1900
Vladimir Marko372f10e2016-05-17 16:30:10 +01001901 void SetRawInputAt(size_t index, HInstruction* input) {
1902 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1903 }
1904
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001905 virtual void Accept(HGraphVisitor* visitor) = 0;
1906 virtual const char* DebugName() const = 0;
1907
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001908 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
1909
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001910 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001911
1912 uint32_t GetDexPc() const { return dex_pc_; }
1913
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001914 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001915
Roland Levillaine161a2a2014-10-03 12:45:18 +01001916 virtual bool CanThrow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001917 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001918
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001919 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001920 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001921
Calin Juravle10e244f2015-01-26 18:54:32 +00001922 // Does not apply for all instructions, but having this at top level greatly
1923 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001924 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001925 virtual bool CanBeNull() const {
1926 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1927 return true;
1928 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001929
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001930 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01001931 return false;
1932 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001933
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00001934 virtual bool IsActualObject() const {
1935 return GetType() == Primitive::kPrimNot;
1936 }
1937
Calin Juravle2e768302015-07-28 14:41:11 +00001938 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001939
Calin Juravle61d544b2015-02-23 16:46:57 +00001940 ReferenceTypeInfo GetReferenceTypeInfo() const {
1941 DCHECK_EQ(GetType(), Primitive::kPrimNot);
Vladimir Markoa1de9182016-02-25 11:37:38 +00001942 return ReferenceTypeInfo::CreateUnchecked(reference_type_handle_,
Vladimir Marko456307a2016-04-19 14:12:13 +00001943 GetPackedFlag<kFlagReferenceTypeIsExact>());
Calin Juravle61d544b2015-02-23 16:46:57 +00001944 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001945
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001946 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001947 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01001948 // Note: fixup_end remains valid across push_front().
1949 auto fixup_end = uses_.empty() ? uses_.begin() : ++uses_.begin();
1950 HUseListNode<HInstruction*>* new_node =
1951 new (GetBlock()->GetGraph()->GetArena()) HUseListNode<HInstruction*>(user, index);
1952 uses_.push_front(*new_node);
1953 FixUpUserRecordsAfterUseInsertion(fixup_end);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001954 }
1955
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001956 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001957 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01001958 // Note: env_fixup_end remains valid across push_front().
1959 auto env_fixup_end = env_uses_.empty() ? env_uses_.begin() : ++env_uses_.begin();
1960 HUseListNode<HEnvironment*>* new_node =
1961 new (GetBlock()->GetGraph()->GetArena()) HUseListNode<HEnvironment*>(user, index);
1962 env_uses_.push_front(*new_node);
1963 FixUpUserRecordsAfterEnvUseInsertion(env_fixup_end);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001964 }
1965
David Brazdil1abb4192015-02-17 18:33:36 +00001966 void RemoveAsUserOfInput(size_t input) {
1967 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
Vladimir Marko46817b82016-03-29 12:21:58 +01001968 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
1969 input_use.GetInstruction()->uses_.erase_after(before_use_node);
1970 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
David Brazdil1abb4192015-02-17 18:33:36 +00001971 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001972
Vladimir Marko372f10e2016-05-17 16:30:10 +01001973 void RemoveAsUserOfAllInputs() {
1974 for (const HUserRecord<HInstruction*>& input_use : GetInputRecords()) {
1975 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
1976 input_use.GetInstruction()->uses_.erase_after(before_use_node);
1977 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
1978 }
1979 }
1980
David Brazdil1abb4192015-02-17 18:33:36 +00001981 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1982 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001983
Vladimir Marko46817b82016-03-29 12:21:58 +01001984 bool HasUses() const { return !uses_.empty() || !env_uses_.empty(); }
1985 bool HasEnvironmentUses() const { return !env_uses_.empty(); }
1986 bool HasNonEnvironmentUses() const { return !uses_.empty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001987 bool HasOnlyOneNonEnvironmentUse() const {
Vladimir Marko46817b82016-03-29 12:21:58 +01001988 return !HasEnvironmentUses() && GetUses().HasExactlyOneElement();
Alexandre Rames188d4312015-04-09 18:30:21 +01001989 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001990
Aart Bikcc42be02016-10-20 16:14:16 -07001991 bool IsRemovable() const {
Aart Bik482095d2016-10-10 15:39:10 -07001992 return
Aart Bikff7d89c2016-11-07 08:49:28 -08001993 !DoesAnyWrite() &&
Aart Bik482095d2016-10-10 15:39:10 -07001994 !CanThrow() &&
1995 !IsSuspendCheck() &&
1996 !IsControlFlow() &&
1997 !IsNativeDebugInfo() &&
1998 !IsParameterValue() &&
Aart Bik482095d2016-10-10 15:39:10 -07001999 // If we added an explicit barrier then we should keep it.
2000 !IsMemoryBarrier();
2001 }
2002
Aart Bikcc42be02016-10-20 16:14:16 -07002003 bool IsDeadAndRemovable() const {
2004 return IsRemovable() && !HasUses();
2005 }
2006
Roland Levillain6c82d402014-10-13 16:10:27 +01002007 // Does this instruction strictly dominate `other_instruction`?
2008 // Returns false if this instruction and `other_instruction` are the same.
2009 // Aborts if this instruction and `other_instruction` are both phis.
2010 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01002011
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002012 int GetId() const { return id_; }
2013 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002014
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002015 int GetSsaIndex() const { return ssa_index_; }
2016 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
2017 bool HasSsaIndex() const { return ssa_index_ != -1; }
2018
2019 bool HasEnvironment() const { return environment_ != nullptr; }
2020 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002021 // Set the `environment_` field. Raw because this method does not
2022 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002023 void SetRawEnvironment(HEnvironment* environment) {
2024 DCHECK(environment_ == nullptr);
2025 DCHECK_EQ(environment->GetHolder(), this);
2026 environment_ = environment;
2027 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002028
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002029 void InsertRawEnvironment(HEnvironment* environment) {
2030 DCHECK(environment_ != nullptr);
2031 DCHECK_EQ(environment->GetHolder(), this);
2032 DCHECK(environment->GetParent() == nullptr);
2033 environment->parent_ = environment_;
2034 environment_ = environment;
2035 }
2036
Vladimir Markocac5a7e2016-02-22 10:39:50 +00002037 void RemoveEnvironment();
2038
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002039 // Set the environment of this instruction, copying it from `environment`. While
2040 // copying, the uses lists are being updated.
2041 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002042 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002043 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002044 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002045 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002046 if (environment->GetParent() != nullptr) {
2047 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2048 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002049 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002050
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002051 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
2052 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002053 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002054 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002055 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002056 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002057 if (environment->GetParent() != nullptr) {
2058 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2059 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002060 }
2061
Nicolas Geoffray39468442014-09-02 15:17:15 +01002062 // Returns the number of entries in the environment. Typically, that is the
2063 // number of dex registers in a method. It could be more in case of inlining.
2064 size_t EnvironmentSize() const;
2065
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002066 LocationSummary* GetLocations() const { return locations_; }
2067 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002068
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002069 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002070 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002071
Alexandre Rames188d4312015-04-09 18:30:21 +01002072 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
2073 // uses of this instruction by `other` are *not* updated.
2074 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
2075 ReplaceWith(other);
2076 other->ReplaceInput(this, use_index);
2077 }
2078
Nicolas Geoffray82091da2015-01-26 10:02:45 +00002079 // Move `this` instruction before `cursor`.
2080 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002081
Vladimir Markofb337ea2015-11-25 15:25:10 +00002082 // Move `this` before its first user and out of any loops. If there is no
2083 // out-of-loop user that dominates all other users, move the instruction
2084 // to the end of the out-of-loop common dominator of the user's blocks.
2085 //
2086 // This can be used only on non-throwing instructions with no side effects that
2087 // have at least one use but no environment uses.
2088 void MoveBeforeFirstUserAndOutOfLoops();
2089
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002090#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002091 bool Is##type() const; \
2092 const H##type* As##type() const; \
2093 H##type* As##type();
2094
2095 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
2096#undef INSTRUCTION_TYPE_CHECK
2097
2098#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01002099 bool Is##type() const { return (As##type() != nullptr); } \
2100 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002101 virtual H##type* As##type() { return nullptr; }
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002102 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002103#undef INSTRUCTION_TYPE_CHECK
2104
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002105 // Returns whether the instruction can be moved within the graph.
Aart Bik71bf7b42016-11-16 10:17:46 -08002106 // TODO: this method is used by LICM and GVN with possibly different
2107 // meanings? split and rename?
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002108 virtual bool CanBeMoved() const { return false; }
2109
2110 // Returns whether the two instructions are of the same kind.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002111 virtual bool InstructionTypeEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002112 return false;
2113 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002114
2115 // Returns whether any data encoded in the two instructions is equal.
2116 // This method does not look at the inputs. Both instructions must be
2117 // of the same type, otherwise the method has undefined behavior.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002118 virtual bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002119 return false;
2120 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002121
2122 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00002123 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002124 // 2) Their inputs are identical.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002125 bool Equals(const HInstruction* other) const;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002126
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002127 // TODO: Remove this indirection when the [[pure]] attribute proposal (n3744)
2128 // is adopted and implemented by our C++ compiler(s). Fow now, we need to hide
2129 // the virtual function because the __attribute__((__pure__)) doesn't really
2130 // apply the strong requirement for virtual functions, preventing optimizations.
2131 InstructionKind GetKind() const PURE;
2132 virtual InstructionKind GetKindInternal() const = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002133
2134 virtual size_t ComputeHashCode() const {
2135 size_t result = GetKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01002136 for (const HInstruction* input : GetInputs()) {
2137 result = (result * 31) + input->GetId();
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002138 }
2139 return result;
2140 }
2141
2142 SideEffects GetSideEffects() const { return side_effects_; }
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00002143 void SetSideEffects(SideEffects other) { side_effects_ = other; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002144 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002145
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002146 size_t GetLifetimePosition() const { return lifetime_position_; }
2147 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2148 LiveInterval* GetLiveInterval() const { return live_interval_; }
2149 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2150 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2151
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002152 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2153
2154 // Returns whether the code generation of the instruction will require to have access
2155 // to the current method. Such instructions are:
2156 // (1): Instructions that require an environment, as calling the runtime requires
2157 // to walk the stack and have the current method stored at a specific stack address.
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002158 // (2): HCurrentMethod, potentially used by HInvokeStaticOrDirect, HLoadString, or HLoadClass
2159 // to access the dex cache.
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002160 bool NeedsCurrentMethod() const {
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002161 return NeedsEnvironment() || IsCurrentMethod();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002162 }
2163
Vladimir Markodc151b22015-10-15 18:02:30 +01002164 // Returns whether the code generation of the instruction will require to have access
2165 // to the dex cache of the current method's declaring class via the current method.
2166 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002167
Mark Mendellc4701932015-04-10 13:18:51 -04002168 // Does this instruction have any use in an environment before
2169 // control flow hits 'other'?
2170 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2171
2172 // Remove all references to environment uses of this instruction.
2173 // The caller must ensure that this is safe to do.
2174 void RemoveEnvironmentUsers();
2175
Vladimir Markoa1de9182016-02-25 11:37:38 +00002176 bool IsEmittedAtUseSite() const { return GetPackedFlag<kFlagEmittedAtUseSite>(); }
2177 void MarkEmittedAtUseSite() { SetPackedFlag<kFlagEmittedAtUseSite>(true); }
David Brazdilb3e773e2016-01-26 11:28:37 +00002178
David Brazdil1abb4192015-02-17 18:33:36 +00002179 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002180 // If set, the machine code for this instruction is assumed to be generated by
2181 // its users. Used by liveness analysis to compute use positions accordingly.
2182 static constexpr size_t kFlagEmittedAtUseSite = 0u;
2183 static constexpr size_t kFlagReferenceTypeIsExact = kFlagEmittedAtUseSite + 1;
2184 static constexpr size_t kNumberOfGenericPackedBits = kFlagReferenceTypeIsExact + 1;
2185 static constexpr size_t kMaxNumberOfPackedBits = sizeof(uint32_t) * kBitsPerByte;
2186
Vladimir Marko372f10e2016-05-17 16:30:10 +01002187 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const {
2188 return GetInputRecords()[i];
2189 }
2190
2191 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) {
2192 ArrayRef<HUserRecord<HInstruction*>> input_records = GetInputRecords();
2193 input_records[index] = input;
2194 }
David Brazdil1abb4192015-02-17 18:33:36 +00002195
Vladimir Markoa1de9182016-02-25 11:37:38 +00002196 uint32_t GetPackedFields() const {
2197 return packed_fields_;
2198 }
2199
2200 template <size_t flag>
2201 bool GetPackedFlag() const {
2202 return (packed_fields_ & (1u << flag)) != 0u;
2203 }
2204
2205 template <size_t flag>
2206 void SetPackedFlag(bool value = true) {
2207 packed_fields_ = (packed_fields_ & ~(1u << flag)) | ((value ? 1u : 0u) << flag);
2208 }
2209
2210 template <typename BitFieldType>
2211 typename BitFieldType::value_type GetPackedField() const {
2212 return BitFieldType::Decode(packed_fields_);
2213 }
2214
2215 template <typename BitFieldType>
2216 void SetPackedField(typename BitFieldType::value_type value) {
2217 DCHECK(IsUint<BitFieldType::size>(static_cast<uintptr_t>(value)));
2218 packed_fields_ = BitFieldType::Update(value, packed_fields_);
2219 }
2220
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002221 private:
Vladimir Marko46817b82016-03-29 12:21:58 +01002222 void FixUpUserRecordsAfterUseInsertion(HUseList<HInstruction*>::iterator fixup_end) {
2223 auto before_use_node = uses_.before_begin();
2224 for (auto use_node = uses_.begin(); use_node != fixup_end; ++use_node) {
2225 HInstruction* user = use_node->GetUser();
2226 size_t input_index = use_node->GetIndex();
2227 user->SetRawInputRecordAt(input_index, HUserRecord<HInstruction*>(this, before_use_node));
2228 before_use_node = use_node;
2229 }
2230 }
2231
2232 void FixUpUserRecordsAfterUseRemoval(HUseList<HInstruction*>::iterator before_use_node) {
2233 auto next = ++HUseList<HInstruction*>::iterator(before_use_node);
2234 if (next != uses_.end()) {
2235 HInstruction* next_user = next->GetUser();
2236 size_t next_index = next->GetIndex();
2237 DCHECK(next_user->InputRecordAt(next_index).GetInstruction() == this);
2238 next_user->SetRawInputRecordAt(next_index, HUserRecord<HInstruction*>(this, before_use_node));
2239 }
2240 }
2241
2242 void FixUpUserRecordsAfterEnvUseInsertion(HUseList<HEnvironment*>::iterator env_fixup_end) {
2243 auto before_env_use_node = env_uses_.before_begin();
2244 for (auto env_use_node = env_uses_.begin(); env_use_node != env_fixup_end; ++env_use_node) {
2245 HEnvironment* user = env_use_node->GetUser();
2246 size_t input_index = env_use_node->GetIndex();
2247 user->vregs_[input_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2248 before_env_use_node = env_use_node;
2249 }
2250 }
2251
2252 void FixUpUserRecordsAfterEnvUseRemoval(HUseList<HEnvironment*>::iterator before_env_use_node) {
2253 auto next = ++HUseList<HEnvironment*>::iterator(before_env_use_node);
2254 if (next != env_uses_.end()) {
2255 HEnvironment* next_user = next->GetUser();
2256 size_t next_index = next->GetIndex();
2257 DCHECK(next_user->vregs_[next_index].GetInstruction() == this);
2258 next_user->vregs_[next_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2259 }
2260 }
David Brazdil1abb4192015-02-17 18:33:36 +00002261
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002262 HInstruction* previous_;
2263 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002264 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002265 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002266
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002267 // An instruction gets an id when it is added to the graph.
2268 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002269 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002270 int id_;
2271
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002272 // When doing liveness analysis, instructions that have uses get an SSA index.
2273 int ssa_index_;
2274
Vladimir Markoa1de9182016-02-25 11:37:38 +00002275 // Packed fields.
2276 uint32_t packed_fields_;
David Brazdilb3e773e2016-01-26 11:28:37 +00002277
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002278 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002279 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002280
2281 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002282 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002283
Nicolas Geoffray39468442014-09-02 15:17:15 +01002284 // The environment associated with this instruction. Not null if the instruction
2285 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002286 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002287
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002288 // Set by the code generator.
2289 LocationSummary* locations_;
2290
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002291 // Set by the liveness analysis.
2292 LiveInterval* live_interval_;
2293
2294 // Set by the liveness analysis, this is the position in a linear
2295 // order of blocks where this instruction's live interval start.
2296 size_t lifetime_position_;
2297
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002298 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002299
Vladimir Markoa1de9182016-02-25 11:37:38 +00002300 // The reference handle part of the reference type info.
2301 // The IsExact() flag is stored in packed fields.
Calin Juravleacf735c2015-02-12 15:25:22 +00002302 // TODO: for primitive types this should be marked as invalid.
Vladimir Markoa1de9182016-02-25 11:37:38 +00002303 ReferenceTypeInfo::TypeHandle reference_type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00002304
David Brazdil1abb4192015-02-17 18:33:36 +00002305 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002306 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002307 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002308 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002309 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002310
2311 DISALLOW_COPY_AND_ASSIGN(HInstruction);
2312};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002313std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002314
2315class HInstructionIterator : public ValueObject {
2316 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002317 explicit HInstructionIterator(const HInstructionList& instructions)
2318 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002319 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002320 }
2321
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002322 bool Done() const { return instruction_ == nullptr; }
2323 HInstruction* Current() const { return instruction_; }
2324 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002325 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002326 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002327 }
2328
2329 private:
2330 HInstruction* instruction_;
2331 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002332
2333 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002334};
2335
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002336class HBackwardInstructionIterator : public ValueObject {
2337 public:
2338 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2339 : instruction_(instructions.last_instruction_) {
2340 next_ = Done() ? nullptr : instruction_->GetPrevious();
2341 }
2342
2343 bool Done() const { return instruction_ == nullptr; }
2344 HInstruction* Current() const { return instruction_; }
2345 void Advance() {
2346 instruction_ = next_;
2347 next_ = Done() ? nullptr : instruction_->GetPrevious();
2348 }
2349
2350 private:
2351 HInstruction* instruction_;
2352 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002353
2354 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002355};
2356
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002357class HVariableInputSizeInstruction : public HInstruction {
2358 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08002359 using HInstruction::GetInputRecords; // Keep the const version visible.
2360 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
2361 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
2362 }
2363
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002364 void AddInput(HInstruction* input);
2365 void InsertInputAt(size_t index, HInstruction* input);
2366 void RemoveInputAt(size_t index);
2367
2368 protected:
2369 HVariableInputSizeInstruction(SideEffects side_effects,
2370 uint32_t dex_pc,
2371 ArenaAllocator* arena,
2372 size_t number_of_inputs,
2373 ArenaAllocKind kind)
2374 : HInstruction(side_effects, dex_pc),
2375 inputs_(number_of_inputs, arena->Adapter(kind)) {}
2376
2377 ArenaVector<HUserRecord<HInstruction*>> inputs_;
2378
2379 private:
2380 DISALLOW_COPY_AND_ASSIGN(HVariableInputSizeInstruction);
2381};
2382
Vladimir Markof9f64412015-09-02 14:05:49 +01002383template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002384class HTemplateInstruction: public HInstruction {
2385 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002386 HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002387 : HInstruction(side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002388 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002389
Vladimir Marko372f10e2016-05-17 16:30:10 +01002390 using HInstruction::GetInputRecords; // Keep the const version visible.
2391 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2392 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002393 }
2394
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002395 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002396 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002397
2398 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002399};
2400
Vladimir Markof9f64412015-09-02 14:05:49 +01002401// HTemplateInstruction specialization for N=0.
2402template<>
2403class HTemplateInstruction<0>: public HInstruction {
2404 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002405 explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002406 : HInstruction(side_effects, dex_pc) {}
2407
Vladimir Markof9f64412015-09-02 14:05:49 +01002408 virtual ~HTemplateInstruction() {}
2409
Vladimir Marko372f10e2016-05-17 16:30:10 +01002410 using HInstruction::GetInputRecords; // Keep the const version visible.
2411 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2412 return ArrayRef<HUserRecord<HInstruction*>>();
Vladimir Markof9f64412015-09-02 14:05:49 +01002413 }
2414
2415 private:
2416 friend class SsaBuilder;
2417};
2418
Dave Allison20dfc792014-06-16 20:44:29 -07002419template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002420class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002421 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002422 HExpression<N>(Primitive::Type type, SideEffects side_effects, uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002423 : HTemplateInstruction<N>(side_effects, dex_pc) {
2424 this->template SetPackedField<TypeField>(type);
2425 }
Dave Allison20dfc792014-06-16 20:44:29 -07002426 virtual ~HExpression() {}
2427
Vladimir Markoa1de9182016-02-25 11:37:38 +00002428 Primitive::Type GetType() const OVERRIDE {
2429 return TypeField::Decode(this->GetPackedFields());
2430 }
Dave Allison20dfc792014-06-16 20:44:29 -07002431
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002432 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002433 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2434 static constexpr size_t kFieldTypeSize =
2435 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
2436 static constexpr size_t kNumberOfExpressionPackedBits = kFieldType + kFieldTypeSize;
2437 static_assert(kNumberOfExpressionPackedBits <= HInstruction::kMaxNumberOfPackedBits,
2438 "Too many packed fields.");
2439 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
Dave Allison20dfc792014-06-16 20:44:29 -07002440};
2441
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002442// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2443// instruction that branches to the exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002444class HReturnVoid FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002445 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002446 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
2447 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002448
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002449 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002450
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002451 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002452
2453 private:
2454 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2455};
2456
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002457// Represents dex's RETURN opcodes. A HReturn is a control flow
2458// instruction that branches to the exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002459class HReturn FINAL : public HTemplateInstruction<1> {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002460 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002461 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
2462 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002463 SetRawInputAt(0, value);
2464 }
2465
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002466 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002467
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002468 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002469
2470 private:
2471 DISALLOW_COPY_AND_ASSIGN(HReturn);
2472};
2473
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002474class HPhi FINAL : public HVariableInputSizeInstruction {
David Brazdildee58d62016-04-07 09:54:26 +00002475 public:
2476 HPhi(ArenaAllocator* arena,
2477 uint32_t reg_number,
2478 size_t number_of_inputs,
2479 Primitive::Type type,
2480 uint32_t dex_pc = kNoDexPc)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002481 : HVariableInputSizeInstruction(
2482 SideEffects::None(),
2483 dex_pc,
2484 arena,
2485 number_of_inputs,
2486 kArenaAllocPhiInputs),
David Brazdildee58d62016-04-07 09:54:26 +00002487 reg_number_(reg_number) {
2488 SetPackedField<TypeField>(ToPhiType(type));
2489 DCHECK_NE(GetType(), Primitive::kPrimVoid);
2490 // Phis are constructed live and marked dead if conflicting or unused.
2491 // Individual steps of SsaBuilder should assume that if a phi has been
2492 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
2493 SetPackedFlag<kFlagIsLive>(true);
2494 SetPackedFlag<kFlagCanBeNull>(true);
2495 }
2496
2497 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
2498 static Primitive::Type ToPhiType(Primitive::Type type) {
2499 return Primitive::PrimitiveKind(type);
2500 }
2501
2502 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
2503
David Brazdildee58d62016-04-07 09:54:26 +00002504 Primitive::Type GetType() const OVERRIDE { return GetPackedField<TypeField>(); }
2505 void SetType(Primitive::Type new_type) {
2506 // Make sure that only valid type changes occur. The following are allowed:
2507 // (1) int -> float/ref (primitive type propagation),
2508 // (2) long -> double (primitive type propagation).
2509 DCHECK(GetType() == new_type ||
2510 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimFloat) ||
2511 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimNot) ||
2512 (GetType() == Primitive::kPrimLong && new_type == Primitive::kPrimDouble));
2513 SetPackedField<TypeField>(new_type);
2514 }
2515
2516 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
2517 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
2518
2519 uint32_t GetRegNumber() const { return reg_number_; }
2520
2521 void SetDead() { SetPackedFlag<kFlagIsLive>(false); }
2522 void SetLive() { SetPackedFlag<kFlagIsLive>(true); }
2523 bool IsDead() const { return !IsLive(); }
2524 bool IsLive() const { return GetPackedFlag<kFlagIsLive>(); }
2525
Vladimir Markoe9004912016-06-16 16:50:52 +01002526 bool IsVRegEquivalentOf(const HInstruction* other) const {
David Brazdildee58d62016-04-07 09:54:26 +00002527 return other != nullptr
2528 && other->IsPhi()
2529 && other->AsPhi()->GetBlock() == GetBlock()
2530 && other->AsPhi()->GetRegNumber() == GetRegNumber();
2531 }
2532
2533 // Returns the next equivalent phi (starting from the current one) or null if there is none.
2534 // An equivalent phi is a phi having the same dex register and type.
2535 // It assumes that phis with the same dex register are adjacent.
2536 HPhi* GetNextEquivalentPhiWithSameType() {
2537 HInstruction* next = GetNext();
2538 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
2539 if (next->GetType() == GetType()) {
2540 return next->AsPhi();
2541 }
2542 next = next->GetNext();
2543 }
2544 return nullptr;
2545 }
2546
2547 DECLARE_INSTRUCTION(Phi);
2548
David Brazdildee58d62016-04-07 09:54:26 +00002549 private:
2550 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2551 static constexpr size_t kFieldTypeSize =
2552 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
2553 static constexpr size_t kFlagIsLive = kFieldType + kFieldTypeSize;
2554 static constexpr size_t kFlagCanBeNull = kFlagIsLive + 1;
2555 static constexpr size_t kNumberOfPhiPackedBits = kFlagCanBeNull + 1;
2556 static_assert(kNumberOfPhiPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
2557 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
2558
David Brazdildee58d62016-04-07 09:54:26 +00002559 const uint32_t reg_number_;
2560
2561 DISALLOW_COPY_AND_ASSIGN(HPhi);
2562};
2563
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002564// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002565// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002566// exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002567class HExit FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002568 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002569 explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002570
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002571 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002572
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002573 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002574
2575 private:
2576 DISALLOW_COPY_AND_ASSIGN(HExit);
2577};
2578
2579// Jumps from one block to another.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002580class HGoto FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002581 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002582 explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002583
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002584 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002585
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002586 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002587 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002588 }
2589
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002590 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002591
2592 private:
2593 DISALLOW_COPY_AND_ASSIGN(HGoto);
2594};
2595
Roland Levillain9867bc72015-08-05 10:21:34 +01002596class HConstant : public HExpression<0> {
2597 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002598 explicit HConstant(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2599 : HExpression(type, SideEffects::None(), dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002600
2601 bool CanBeMoved() const OVERRIDE { return true; }
2602
Roland Levillain1a653882016-03-18 18:05:57 +00002603 // Is this constant -1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002604 virtual bool IsMinusOne() const { return false; }
Roland Levillain1a653882016-03-18 18:05:57 +00002605 // Is this constant 0 in the arithmetic sense?
2606 virtual bool IsArithmeticZero() const { return false; }
2607 // Is this constant a 0-bit pattern?
2608 virtual bool IsZeroBitPattern() const { return false; }
2609 // Is this constant 1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002610 virtual bool IsOne() const { return false; }
2611
David Brazdil77a48ae2015-09-15 12:34:04 +00002612 virtual uint64_t GetValueAsUint64() const = 0;
2613
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002614 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002615
2616 private:
2617 DISALLOW_COPY_AND_ASSIGN(HConstant);
2618};
2619
Vladimir Markofcb503c2016-05-18 12:48:17 +01002620class HNullConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002621 public:
Vladimir Marko372f10e2016-05-17 16:30:10 +01002622 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01002623 return true;
2624 }
2625
David Brazdil77a48ae2015-09-15 12:34:04 +00002626 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2627
Roland Levillain9867bc72015-08-05 10:21:34 +01002628 size_t ComputeHashCode() const OVERRIDE { return 0; }
2629
Roland Levillain1a653882016-03-18 18:05:57 +00002630 // The null constant representation is a 0-bit pattern.
2631 virtual bool IsZeroBitPattern() const { return true; }
2632
Roland Levillain9867bc72015-08-05 10:21:34 +01002633 DECLARE_INSTRUCTION(NullConstant);
2634
2635 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002636 explicit HNullConstant(uint32_t dex_pc = kNoDexPc) : HConstant(Primitive::kPrimNot, dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002637
2638 friend class HGraph;
2639 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2640};
2641
2642// Constants of the type int. Those can be from Dex instructions, or
2643// synthesized (for example with the if-eqz instruction).
Vladimir Markofcb503c2016-05-18 12:48:17 +01002644class HIntConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002645 public:
2646 int32_t GetValue() const { return value_; }
2647
David Brazdil9f389d42015-10-01 14:32:56 +01002648 uint64_t GetValueAsUint64() const OVERRIDE {
2649 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2650 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002651
Vladimir Marko372f10e2016-05-17 16:30:10 +01002652 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002653 DCHECK(other->IsIntConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002654 return other->AsIntConstant()->value_ == value_;
2655 }
2656
2657 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2658
2659 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002660 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2661 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002662 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2663
Roland Levillain1a653882016-03-18 18:05:57 +00002664 // Integer constants are used to encode Boolean values as well,
2665 // where 1 means true and 0 means false.
2666 bool IsTrue() const { return GetValue() == 1; }
2667 bool IsFalse() const { return GetValue() == 0; }
2668
Roland Levillain9867bc72015-08-05 10:21:34 +01002669 DECLARE_INSTRUCTION(IntConstant);
2670
2671 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002672 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2673 : HConstant(Primitive::kPrimInt, dex_pc), value_(value) {}
2674 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
2675 : HConstant(Primitive::kPrimInt, dex_pc), value_(value ? 1 : 0) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002676
2677 const int32_t value_;
2678
2679 friend class HGraph;
2680 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2681 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2682 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2683};
2684
Vladimir Markofcb503c2016-05-18 12:48:17 +01002685class HLongConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002686 public:
2687 int64_t GetValue() const { return value_; }
2688
David Brazdil77a48ae2015-09-15 12:34:04 +00002689 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2690
Vladimir Marko372f10e2016-05-17 16:30:10 +01002691 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002692 DCHECK(other->IsLongConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002693 return other->AsLongConstant()->value_ == value_;
2694 }
2695
2696 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2697
2698 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002699 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2700 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002701 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2702
2703 DECLARE_INSTRUCTION(LongConstant);
2704
2705 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002706 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2707 : HConstant(Primitive::kPrimLong, dex_pc), value_(value) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002708
2709 const int64_t value_;
2710
2711 friend class HGraph;
2712 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2713};
Dave Allison20dfc792014-06-16 20:44:29 -07002714
Vladimir Markofcb503c2016-05-18 12:48:17 +01002715class HFloatConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002716 public:
2717 float GetValue() const { return value_; }
2718
2719 uint64_t GetValueAsUint64() const OVERRIDE {
2720 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
2721 }
2722
Vladimir Marko372f10e2016-05-17 16:30:10 +01002723 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002724 DCHECK(other->IsFloatConstant()) << other->DebugName();
2725 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
2726 }
2727
2728 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2729
2730 bool IsMinusOne() const OVERRIDE {
2731 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
2732 }
Roland Levillain1a653882016-03-18 18:05:57 +00002733 bool IsArithmeticZero() const OVERRIDE {
2734 return std::fpclassify(value_) == FP_ZERO;
2735 }
2736 bool IsArithmeticPositiveZero() const {
2737 return IsArithmeticZero() && !std::signbit(value_);
2738 }
2739 bool IsArithmeticNegativeZero() const {
2740 return IsArithmeticZero() && std::signbit(value_);
2741 }
2742 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002743 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(0.0f);
Roland Levillain31dd3d62016-02-16 12:21:02 +00002744 }
2745 bool IsOne() const OVERRIDE {
2746 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2747 }
2748 bool IsNaN() const {
2749 return std::isnan(value_);
2750 }
2751
2752 DECLARE_INSTRUCTION(FloatConstant);
2753
2754 private:
2755 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
2756 : HConstant(Primitive::kPrimFloat, dex_pc), value_(value) {}
2757 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2758 : HConstant(Primitive::kPrimFloat, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
2759
2760 const float value_;
2761
2762 // Only the SsaBuilder and HGraph can create floating-point constants.
2763 friend class SsaBuilder;
2764 friend class HGraph;
2765 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2766};
2767
Vladimir Markofcb503c2016-05-18 12:48:17 +01002768class HDoubleConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002769 public:
2770 double GetValue() const { return value_; }
2771
2772 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
2773
Vladimir Marko372f10e2016-05-17 16:30:10 +01002774 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002775 DCHECK(other->IsDoubleConstant()) << other->DebugName();
2776 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
2777 }
2778
2779 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2780
2781 bool IsMinusOne() const OVERRIDE {
2782 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
2783 }
Roland Levillain1a653882016-03-18 18:05:57 +00002784 bool IsArithmeticZero() const OVERRIDE {
2785 return std::fpclassify(value_) == FP_ZERO;
2786 }
2787 bool IsArithmeticPositiveZero() const {
2788 return IsArithmeticZero() && !std::signbit(value_);
2789 }
2790 bool IsArithmeticNegativeZero() const {
2791 return IsArithmeticZero() && std::signbit(value_);
2792 }
2793 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002794 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((0.0));
Roland Levillain31dd3d62016-02-16 12:21:02 +00002795 }
2796 bool IsOne() const OVERRIDE {
2797 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
2798 }
2799 bool IsNaN() const {
2800 return std::isnan(value_);
2801 }
2802
2803 DECLARE_INSTRUCTION(DoubleConstant);
2804
2805 private:
2806 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
2807 : HConstant(Primitive::kPrimDouble, dex_pc), value_(value) {}
2808 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2809 : HConstant(Primitive::kPrimDouble, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
2810
2811 const double value_;
2812
2813 // Only the SsaBuilder and HGraph can create floating-point constants.
2814 friend class SsaBuilder;
2815 friend class HGraph;
2816 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2817};
2818
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002819// Conditional branch. A block ending with an HIf instruction must have
2820// two successors.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002821class HIf FINAL : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002822 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002823 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
2824 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002825 SetRawInputAt(0, input);
2826 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002827
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002828 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002829
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002830 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002831 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002832 }
2833
2834 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002835 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002836 }
2837
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002838 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002839
2840 private:
2841 DISALLOW_COPY_AND_ASSIGN(HIf);
2842};
2843
David Brazdilfc6a86a2015-06-26 10:33:45 +00002844
2845// Abstract instruction which marks the beginning and/or end of a try block and
2846// links it to the respective exception handlers. Behaves the same as a Goto in
2847// non-exceptional control flow.
2848// Normal-flow successor is stored at index zero, exception handlers under
2849// higher indices in no particular order.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002850class HTryBoundary FINAL : public HTemplateInstruction<0> {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002851 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002852 enum class BoundaryKind {
David Brazdil56e1acc2015-06-30 15:41:36 +01002853 kEntry,
2854 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00002855 kLast = kExit
David Brazdil56e1acc2015-06-30 15:41:36 +01002856 };
2857
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002858 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002859 : HTemplateInstruction(SideEffects::None(), dex_pc) {
2860 SetPackedField<BoundaryKindField>(kind);
2861 }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002862
2863 bool IsControlFlow() const OVERRIDE { return true; }
2864
2865 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01002866 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002867
David Brazdild26a4112015-11-10 11:07:31 +00002868 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
2869 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
2870 }
2871
David Brazdilfc6a86a2015-06-26 10:33:45 +00002872 // Returns whether `handler` is among its exception handlers (non-zero index
2873 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002874 bool HasExceptionHandler(const HBasicBlock& handler) const {
2875 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00002876 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002877 }
2878
2879 // If not present already, adds `handler` to its block's list of exception
2880 // handlers.
2881 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002882 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002883 GetBlock()->AddSuccessor(handler);
2884 }
2885 }
2886
Vladimir Markoa1de9182016-02-25 11:37:38 +00002887 BoundaryKind GetBoundaryKind() const { return GetPackedField<BoundaryKindField>(); }
2888 bool IsEntry() const { return GetBoundaryKind() == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002889
David Brazdilffee3d32015-07-06 11:48:53 +01002890 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2891
David Brazdilfc6a86a2015-06-26 10:33:45 +00002892 DECLARE_INSTRUCTION(TryBoundary);
2893
2894 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002895 static constexpr size_t kFieldBoundaryKind = kNumberOfGenericPackedBits;
2896 static constexpr size_t kFieldBoundaryKindSize =
2897 MinimumBitsToStore(static_cast<size_t>(BoundaryKind::kLast));
2898 static constexpr size_t kNumberOfTryBoundaryPackedBits =
2899 kFieldBoundaryKind + kFieldBoundaryKindSize;
2900 static_assert(kNumberOfTryBoundaryPackedBits <= kMaxNumberOfPackedBits,
2901 "Too many packed fields.");
2902 using BoundaryKindField = BitField<BoundaryKind, kFieldBoundaryKind, kFieldBoundaryKindSize>;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002903
2904 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2905};
2906
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002907// Deoptimize to interpreter, upon checking a condition.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002908class HDeoptimize FINAL : public HTemplateInstruction<1> {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002909 public:
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002910 // We set CanTriggerGC to prevent any intermediate address to be live
2911 // at the point of the `HDeoptimize`.
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002912 HDeoptimize(HInstruction* cond, uint32_t dex_pc)
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002913 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002914 SetRawInputAt(0, cond);
2915 }
2916
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002917 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01002918 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002919 return true;
2920 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002921 bool NeedsEnvironment() const OVERRIDE { return true; }
2922 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002923
2924 DECLARE_INSTRUCTION(Deoptimize);
2925
2926 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002927 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2928};
2929
Mingyao Yang063fc772016-08-02 11:02:54 -07002930// Represents a should_deoptimize flag. Currently used for CHA-based devirtualization.
2931// The compiled code checks this flag value in a guard before devirtualized call and
2932// if it's true, starts to do deoptimization.
2933// It has a 4-byte slot on stack.
2934// TODO: allocate a register for this flag.
Mingyao Yangb0b051a2016-11-17 09:04:53 -08002935class HShouldDeoptimizeFlag FINAL : public HVariableInputSizeInstruction {
Mingyao Yang063fc772016-08-02 11:02:54 -07002936 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08002937 // CHA guards are only optimized in a separate pass and it has no side effects
2938 // with regard to other passes.
2939 HShouldDeoptimizeFlag(ArenaAllocator* arena, uint32_t dex_pc)
2940 : HVariableInputSizeInstruction(SideEffects::None(), dex_pc, arena, 0, kArenaAllocCHA) {
Mingyao Yang063fc772016-08-02 11:02:54 -07002941 }
2942
Mingyao Yangb0b051a2016-11-17 09:04:53 -08002943 Primitive::Type GetType() const OVERRIDE { return Primitive::kPrimInt; }
2944
2945 // We do all CHA guard elimination/motion in a single pass, after which there is no
2946 // further guard elimination/motion since a guard might have been used for justification
2947 // of the elimination of another guard. Therefore, we pretend this guard cannot be moved
2948 // to avoid other optimizations trying to move it.
Mingyao Yang063fc772016-08-02 11:02:54 -07002949 bool CanBeMoved() const OVERRIDE { return false; }
2950
2951 DECLARE_INSTRUCTION(ShouldDeoptimizeFlag);
2952
2953 private:
2954 DISALLOW_COPY_AND_ASSIGN(HShouldDeoptimizeFlag);
2955};
2956
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002957// Represents the ArtMethod that was passed as a first argument to
2958// the method. It is used by instructions that depend on it, like
2959// instructions that work with the dex cache.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002960class HCurrentMethod FINAL : public HExpression<0> {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002961 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002962 explicit HCurrentMethod(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2963 : HExpression(type, SideEffects::None(), dex_pc) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002964
2965 DECLARE_INSTRUCTION(CurrentMethod);
2966
2967 private:
2968 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
2969};
2970
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002971// Fetches an ArtMethod from the virtual table or the interface method table
2972// of a class.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002973class HClassTableGet FINAL : public HExpression<1> {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002974 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002975 enum class TableKind {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002976 kVTable,
2977 kIMTable,
Vladimir Markoa1de9182016-02-25 11:37:38 +00002978 kLast = kIMTable
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002979 };
2980 HClassTableGet(HInstruction* cls,
2981 Primitive::Type type,
2982 TableKind kind,
2983 size_t index,
2984 uint32_t dex_pc)
2985 : HExpression(type, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00002986 index_(index) {
2987 SetPackedField<TableKindField>(kind);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002988 SetRawInputAt(0, cls);
2989 }
2990
2991 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01002992 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002993 return other->AsClassTableGet()->GetIndex() == index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00002994 other->AsClassTableGet()->GetPackedFields() == GetPackedFields();
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002995 }
2996
Vladimir Markoa1de9182016-02-25 11:37:38 +00002997 TableKind GetTableKind() const { return GetPackedField<TableKindField>(); }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002998 size_t GetIndex() const { return index_; }
2999
3000 DECLARE_INSTRUCTION(ClassTableGet);
3001
3002 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003003 static constexpr size_t kFieldTableKind = kNumberOfExpressionPackedBits;
3004 static constexpr size_t kFieldTableKindSize =
3005 MinimumBitsToStore(static_cast<size_t>(TableKind::kLast));
3006 static constexpr size_t kNumberOfClassTableGetPackedBits = kFieldTableKind + kFieldTableKindSize;
3007 static_assert(kNumberOfClassTableGetPackedBits <= kMaxNumberOfPackedBits,
3008 "Too many packed fields.");
3009 using TableKindField = BitField<TableKind, kFieldTableKind, kFieldTableKind>;
3010
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003011 // The index of the ArtMethod in the table.
3012 const size_t index_;
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003013
3014 DISALLOW_COPY_AND_ASSIGN(HClassTableGet);
3015};
3016
Mark Mendellfe57faa2015-09-18 09:26:15 -04003017// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
3018// have one successor for each entry in the switch table, and the final successor
3019// will be the block containing the next Dex opcode.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003020class HPackedSwitch FINAL : public HTemplateInstruction<1> {
Mark Mendellfe57faa2015-09-18 09:26:15 -04003021 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003022 HPackedSwitch(int32_t start_value,
3023 uint32_t num_entries,
3024 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04003025 uint32_t dex_pc = kNoDexPc)
3026 : HTemplateInstruction(SideEffects::None(), dex_pc),
3027 start_value_(start_value),
3028 num_entries_(num_entries) {
3029 SetRawInputAt(0, input);
3030 }
3031
3032 bool IsControlFlow() const OVERRIDE { return true; }
3033
3034 int32_t GetStartValue() const { return start_value_; }
3035
Vladimir Marko211c2112015-09-24 16:52:33 +01003036 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04003037
3038 HBasicBlock* GetDefaultBlock() const {
3039 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01003040 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04003041 }
3042 DECLARE_INSTRUCTION(PackedSwitch);
3043
3044 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003045 const int32_t start_value_;
3046 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04003047
3048 DISALLOW_COPY_AND_ASSIGN(HPackedSwitch);
3049};
3050
Roland Levillain88cb1752014-10-20 16:36:47 +01003051class HUnaryOperation : public HExpression<1> {
3052 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003053 HUnaryOperation(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
3054 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01003055 SetRawInputAt(0, input);
3056 }
3057
3058 HInstruction* GetInput() const { return InputAt(0); }
3059 Primitive::Type GetResultType() const { return GetType(); }
3060
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003061 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003062 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003063 return true;
3064 }
Roland Levillain88cb1752014-10-20 16:36:47 +01003065
Roland Levillain31dd3d62016-02-16 12:21:02 +00003066 // Try to statically evaluate `this` and return a HConstant
3067 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003068 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003069 HConstant* TryStaticEvaluation() const;
3070
3071 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003072 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
3073 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003074 virtual HConstant* Evaluate(HFloatConstant* x) const = 0;
3075 virtual HConstant* Evaluate(HDoubleConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01003076
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003077 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01003078
3079 private:
3080 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
3081};
3082
Dave Allison20dfc792014-06-16 20:44:29 -07003083class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003084 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003085 HBinaryOperation(Primitive::Type result_type,
3086 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003087 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003088 SideEffects side_effects = SideEffects::None(),
3089 uint32_t dex_pc = kNoDexPc)
3090 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003091 SetRawInputAt(0, left);
3092 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003093 }
3094
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003095 HInstruction* GetLeft() const { return InputAt(0); }
3096 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07003097 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003098
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003099 virtual bool IsCommutative() const { return false; }
3100
3101 // Put constant on the right.
3102 // Returns whether order is changed.
3103 bool OrderInputsWithConstantOnTheRight() {
3104 HInstruction* left = InputAt(0);
3105 HInstruction* right = InputAt(1);
3106 if (left->IsConstant() && !right->IsConstant()) {
3107 ReplaceInput(right, 0);
3108 ReplaceInput(left, 1);
3109 return true;
3110 }
3111 return false;
3112 }
3113
3114 // Order inputs by instruction id, but favor constant on the right side.
3115 // This helps GVN for commutative ops.
3116 void OrderInputs() {
3117 DCHECK(IsCommutative());
3118 HInstruction* left = InputAt(0);
3119 HInstruction* right = InputAt(1);
3120 if (left == right || (!left->IsConstant() && right->IsConstant())) {
3121 return;
3122 }
3123 if (OrderInputsWithConstantOnTheRight()) {
3124 return;
3125 }
3126 // Order according to instruction id.
3127 if (left->GetId() > right->GetId()) {
3128 ReplaceInput(right, 0);
3129 ReplaceInput(left, 1);
3130 }
3131 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003132
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003133 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003134 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003135 return true;
3136 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003137
Roland Levillain31dd3d62016-02-16 12:21:02 +00003138 // Try to statically evaluate `this` and return a HConstant
3139 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003140 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003141 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01003142
3143 // Apply this operation to `x` and `y`.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003144 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3145 HNullConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003146 LOG(FATAL) << DebugName() << " is not defined for the (null, null) case.";
3147 UNREACHABLE();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003148 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003149 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
3150 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
Roland Levillain9867bc72015-08-05 10:21:34 +01003151 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
3152 HIntConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003153 LOG(FATAL) << DebugName() << " is not defined for the (long, int) case.";
3154 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01003155 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003156 virtual HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const = 0;
3157 virtual HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const = 0;
Roland Levillain556c3d12014-09-18 15:25:07 +01003158
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003159 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003160 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003161 HConstant* GetConstantRight() const;
3162
3163 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003164 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003165 HInstruction* GetLeastConstantLeft() const;
3166
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003167 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01003168
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003169 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003170 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
3171};
3172
Mark Mendellc4701932015-04-10 13:18:51 -04003173// The comparison bias applies for floating point operations and indicates how NaN
3174// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01003175enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04003176 kNoBias, // bias is not applicable (i.e. for long operation)
3177 kGtBias, // return 1 for NaN comparisons
3178 kLtBias, // return -1 for NaN comparisons
Vladimir Markoa1de9182016-02-25 11:37:38 +00003179 kLast = kLtBias
Mark Mendellc4701932015-04-10 13:18:51 -04003180};
3181
Roland Levillain31dd3d62016-02-16 12:21:02 +00003182std::ostream& operator<<(std::ostream& os, const ComparisonBias& rhs);
3183
Dave Allison20dfc792014-06-16 20:44:29 -07003184class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003185 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003186 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00003187 : HBinaryOperation(Primitive::kPrimBoolean, first, second, SideEffects::None(), dex_pc) {
3188 SetPackedField<ComparisonBiasField>(ComparisonBias::kNoBias);
3189 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003190
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003191 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003192 // `instruction`, and disregard moves in between.
3193 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003194
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003195 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003196
3197 virtual IfCondition GetCondition() const = 0;
3198
Mark Mendellc4701932015-04-10 13:18:51 -04003199 virtual IfCondition GetOppositeCondition() const = 0;
3200
Vladimir Markoa1de9182016-02-25 11:37:38 +00003201 bool IsGtBias() const { return GetBias() == ComparisonBias::kGtBias; }
Anton Shaminbdd79352016-02-15 12:48:36 +06003202 bool IsLtBias() const { return GetBias() == ComparisonBias::kLtBias; }
3203
Vladimir Markoa1de9182016-02-25 11:37:38 +00003204 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
3205 void SetBias(ComparisonBias bias) { SetPackedField<ComparisonBiasField>(bias); }
Mark Mendellc4701932015-04-10 13:18:51 -04003206
Vladimir Marko372f10e2016-05-17 16:30:10 +01003207 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003208 return GetPackedFields() == other->AsCondition()->GetPackedFields();
Mark Mendellc4701932015-04-10 13:18:51 -04003209 }
3210
Roland Levillain4fa13f62015-07-06 18:11:54 +01003211 bool IsFPConditionTrueIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003212 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003213 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003214 if (if_cond == kCondNE) {
3215 return true;
3216 } else if (if_cond == kCondEQ) {
3217 return false;
3218 }
3219 return ((if_cond == kCondGT) || (if_cond == kCondGE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003220 }
3221
3222 bool IsFPConditionFalseIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003223 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003224 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003225 if (if_cond == kCondEQ) {
3226 return true;
3227 } else if (if_cond == kCondNE) {
3228 return false;
3229 }
3230 return ((if_cond == kCondLT) || (if_cond == kCondLE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003231 }
3232
Roland Levillain31dd3d62016-02-16 12:21:02 +00003233 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003234 // Needed if we merge a HCompare into a HCondition.
3235 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3236 static constexpr size_t kFieldComparisonBiasSize =
3237 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3238 static constexpr size_t kNumberOfConditionPackedBits =
3239 kFieldComparisonBias + kFieldComparisonBiasSize;
3240 static_assert(kNumberOfConditionPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3241 using ComparisonBiasField =
3242 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3243
Roland Levillain31dd3d62016-02-16 12:21:02 +00003244 template <typename T>
3245 int32_t Compare(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3246
3247 template <typename T>
3248 int32_t CompareFP(T x, T y) const {
3249 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3250 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3251 // Handle the bias.
3252 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compare(x, y);
3253 }
3254
3255 // Return an integer constant containing the result of a condition evaluated at compile time.
3256 HIntConstant* MakeConstantCondition(bool value, uint32_t dex_pc) const {
3257 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3258 }
3259
Dave Allison20dfc792014-06-16 20:44:29 -07003260 private:
3261 DISALLOW_COPY_AND_ASSIGN(HCondition);
3262};
3263
3264// Instruction to check if two inputs are equal to each other.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003265class HEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003266 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003267 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3268 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003269
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003270 bool IsCommutative() const OVERRIDE { return true; }
3271
Vladimir Marko9e23df52015-11-10 17:14:35 +00003272 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3273 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003274 return MakeConstantCondition(true, GetDexPc());
3275 }
3276 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3277 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3278 }
3279 // In the following Evaluate methods, a HCompare instruction has
3280 // been merged into this HEqual instruction; evaluate it as
3281 // `Compare(x, y) == 0`.
3282 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3283 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0),
3284 GetDexPc());
3285 }
3286 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3287 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3288 }
3289 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3290 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003291 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003292
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003293 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003294
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003295 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003296 return kCondEQ;
3297 }
3298
Mark Mendellc4701932015-04-10 13:18:51 -04003299 IfCondition GetOppositeCondition() const OVERRIDE {
3300 return kCondNE;
3301 }
3302
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003303 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003304 template <typename T> static bool Compute(T x, T y) { return x == y; }
Aart Bike9f37602015-10-09 11:15:55 -07003305
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003306 DISALLOW_COPY_AND_ASSIGN(HEqual);
3307};
3308
Vladimir Markofcb503c2016-05-18 12:48:17 +01003309class HNotEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003310 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003311 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3312 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003313
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003314 bool IsCommutative() const OVERRIDE { return true; }
3315
Vladimir Marko9e23df52015-11-10 17:14:35 +00003316 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3317 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003318 return MakeConstantCondition(false, GetDexPc());
3319 }
3320 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3321 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3322 }
3323 // In the following Evaluate methods, a HCompare instruction has
3324 // been merged into this HNotEqual instruction; evaluate it as
3325 // `Compare(x, y) != 0`.
3326 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3327 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3328 }
3329 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3330 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3331 }
3332 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3333 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003334 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003335
Dave Allison20dfc792014-06-16 20:44:29 -07003336 DECLARE_INSTRUCTION(NotEqual);
3337
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003338 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003339 return kCondNE;
3340 }
3341
Mark Mendellc4701932015-04-10 13:18:51 -04003342 IfCondition GetOppositeCondition() const OVERRIDE {
3343 return kCondEQ;
3344 }
3345
Dave Allison20dfc792014-06-16 20:44:29 -07003346 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003347 template <typename T> static bool Compute(T x, T y) { return x != y; }
Aart Bike9f37602015-10-09 11:15:55 -07003348
Dave Allison20dfc792014-06-16 20:44:29 -07003349 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
3350};
3351
Vladimir Markofcb503c2016-05-18 12:48:17 +01003352class HLessThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003353 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003354 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3355 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003356
Roland Levillain9867bc72015-08-05 10:21:34 +01003357 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003358 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003359 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003360 // In the following Evaluate methods, a HCompare instruction has
3361 // been merged into this HLessThan instruction; evaluate it as
3362 // `Compare(x, y) < 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003363 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003364 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3365 }
3366 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3367 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3368 }
3369 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3370 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003371 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003372
Dave Allison20dfc792014-06-16 20:44:29 -07003373 DECLARE_INSTRUCTION(LessThan);
3374
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003375 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003376 return kCondLT;
3377 }
3378
Mark Mendellc4701932015-04-10 13:18:51 -04003379 IfCondition GetOppositeCondition() const OVERRIDE {
3380 return kCondGE;
3381 }
3382
Dave Allison20dfc792014-06-16 20:44:29 -07003383 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003384 template <typename T> static bool Compute(T x, T y) { return x < y; }
Aart Bike9f37602015-10-09 11:15:55 -07003385
Dave Allison20dfc792014-06-16 20:44:29 -07003386 DISALLOW_COPY_AND_ASSIGN(HLessThan);
3387};
3388
Vladimir Markofcb503c2016-05-18 12:48:17 +01003389class HLessThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003390 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003391 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3392 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003393
Roland Levillain9867bc72015-08-05 10:21:34 +01003394 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003395 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003396 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003397 // In the following Evaluate methods, a HCompare instruction has
3398 // been merged into this HLessThanOrEqual instruction; evaluate it as
3399 // `Compare(x, y) <= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003400 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003401 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3402 }
3403 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3404 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3405 }
3406 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3407 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003408 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003409
Dave Allison20dfc792014-06-16 20:44:29 -07003410 DECLARE_INSTRUCTION(LessThanOrEqual);
3411
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003412 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003413 return kCondLE;
3414 }
3415
Mark Mendellc4701932015-04-10 13:18:51 -04003416 IfCondition GetOppositeCondition() const OVERRIDE {
3417 return kCondGT;
3418 }
3419
Dave Allison20dfc792014-06-16 20:44:29 -07003420 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003421 template <typename T> static bool Compute(T x, T y) { return x <= y; }
Aart Bike9f37602015-10-09 11:15:55 -07003422
Dave Allison20dfc792014-06-16 20:44:29 -07003423 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
3424};
3425
Vladimir Markofcb503c2016-05-18 12:48:17 +01003426class HGreaterThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003427 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003428 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3429 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003430
Roland Levillain9867bc72015-08-05 10:21:34 +01003431 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003432 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003433 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003434 // In the following Evaluate methods, a HCompare instruction has
3435 // been merged into this HGreaterThan instruction; evaluate it as
3436 // `Compare(x, y) > 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003437 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003438 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3439 }
3440 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3441 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3442 }
3443 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3444 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003445 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003446
Dave Allison20dfc792014-06-16 20:44:29 -07003447 DECLARE_INSTRUCTION(GreaterThan);
3448
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003449 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003450 return kCondGT;
3451 }
3452
Mark Mendellc4701932015-04-10 13:18:51 -04003453 IfCondition GetOppositeCondition() const OVERRIDE {
3454 return kCondLE;
3455 }
3456
Dave Allison20dfc792014-06-16 20:44:29 -07003457 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003458 template <typename T> static bool Compute(T x, T y) { return x > y; }
Aart Bike9f37602015-10-09 11:15:55 -07003459
Dave Allison20dfc792014-06-16 20:44:29 -07003460 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
3461};
3462
Vladimir Markofcb503c2016-05-18 12:48:17 +01003463class HGreaterThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003464 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003465 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3466 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003467
Roland Levillain9867bc72015-08-05 10:21:34 +01003468 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003469 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003470 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003471 // In the following Evaluate methods, a HCompare instruction has
3472 // been merged into this HGreaterThanOrEqual instruction; evaluate it as
3473 // `Compare(x, y) >= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003474 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003475 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3476 }
3477 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3478 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3479 }
3480 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3481 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003482 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003483
Dave Allison20dfc792014-06-16 20:44:29 -07003484 DECLARE_INSTRUCTION(GreaterThanOrEqual);
3485
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003486 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003487 return kCondGE;
3488 }
3489
Mark Mendellc4701932015-04-10 13:18:51 -04003490 IfCondition GetOppositeCondition() const OVERRIDE {
3491 return kCondLT;
3492 }
3493
Dave Allison20dfc792014-06-16 20:44:29 -07003494 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003495 template <typename T> static bool Compute(T x, T y) { return x >= y; }
Aart Bike9f37602015-10-09 11:15:55 -07003496
Dave Allison20dfc792014-06-16 20:44:29 -07003497 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
3498};
3499
Vladimir Markofcb503c2016-05-18 12:48:17 +01003500class HBelow FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003501 public:
3502 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3503 : HCondition(first, second, dex_pc) {}
3504
3505 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003506 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003507 }
3508 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003509 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3510 }
3511 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3512 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3513 LOG(FATAL) << DebugName() << " is not defined for float values";
3514 UNREACHABLE();
3515 }
3516 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3517 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3518 LOG(FATAL) << DebugName() << " is not defined for double values";
3519 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003520 }
3521
3522 DECLARE_INSTRUCTION(Below);
3523
3524 IfCondition GetCondition() const OVERRIDE {
3525 return kCondB;
3526 }
3527
3528 IfCondition GetOppositeCondition() const OVERRIDE {
3529 return kCondAE;
3530 }
3531
3532 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003533 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003534 return MakeUnsigned(x) < MakeUnsigned(y);
3535 }
Aart Bike9f37602015-10-09 11:15:55 -07003536
3537 DISALLOW_COPY_AND_ASSIGN(HBelow);
3538};
3539
Vladimir Markofcb503c2016-05-18 12:48:17 +01003540class HBelowOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003541 public:
3542 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3543 : HCondition(first, second, dex_pc) {}
3544
3545 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003546 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003547 }
3548 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003549 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3550 }
3551 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3552 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3553 LOG(FATAL) << DebugName() << " is not defined for float values";
3554 UNREACHABLE();
3555 }
3556 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3557 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3558 LOG(FATAL) << DebugName() << " is not defined for double values";
3559 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003560 }
3561
3562 DECLARE_INSTRUCTION(BelowOrEqual);
3563
3564 IfCondition GetCondition() const OVERRIDE {
3565 return kCondBE;
3566 }
3567
3568 IfCondition GetOppositeCondition() const OVERRIDE {
3569 return kCondA;
3570 }
3571
3572 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003573 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003574 return MakeUnsigned(x) <= MakeUnsigned(y);
3575 }
Aart Bike9f37602015-10-09 11:15:55 -07003576
3577 DISALLOW_COPY_AND_ASSIGN(HBelowOrEqual);
3578};
3579
Vladimir Markofcb503c2016-05-18 12:48:17 +01003580class HAbove FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003581 public:
3582 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3583 : HCondition(first, second, dex_pc) {}
3584
3585 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003586 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003587 }
3588 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003589 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3590 }
3591 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3592 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3593 LOG(FATAL) << DebugName() << " is not defined for float values";
3594 UNREACHABLE();
3595 }
3596 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3597 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3598 LOG(FATAL) << DebugName() << " is not defined for double values";
3599 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003600 }
3601
3602 DECLARE_INSTRUCTION(Above);
3603
3604 IfCondition GetCondition() const OVERRIDE {
3605 return kCondA;
3606 }
3607
3608 IfCondition GetOppositeCondition() const OVERRIDE {
3609 return kCondBE;
3610 }
3611
3612 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003613 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003614 return MakeUnsigned(x) > MakeUnsigned(y);
3615 }
Aart Bike9f37602015-10-09 11:15:55 -07003616
3617 DISALLOW_COPY_AND_ASSIGN(HAbove);
3618};
3619
Vladimir Markofcb503c2016-05-18 12:48:17 +01003620class HAboveOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003621 public:
3622 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3623 : HCondition(first, second, dex_pc) {}
3624
3625 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003626 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003627 }
3628 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003629 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3630 }
3631 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3632 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3633 LOG(FATAL) << DebugName() << " is not defined for float values";
3634 UNREACHABLE();
3635 }
3636 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3637 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3638 LOG(FATAL) << DebugName() << " is not defined for double values";
3639 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003640 }
3641
3642 DECLARE_INSTRUCTION(AboveOrEqual);
3643
3644 IfCondition GetCondition() const OVERRIDE {
3645 return kCondAE;
3646 }
3647
3648 IfCondition GetOppositeCondition() const OVERRIDE {
3649 return kCondB;
3650 }
3651
3652 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003653 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003654 return MakeUnsigned(x) >= MakeUnsigned(y);
3655 }
Aart Bike9f37602015-10-09 11:15:55 -07003656
3657 DISALLOW_COPY_AND_ASSIGN(HAboveOrEqual);
3658};
Dave Allison20dfc792014-06-16 20:44:29 -07003659
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003660// Instruction to check how two inputs compare to each other.
3661// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003662class HCompare FINAL : public HBinaryOperation {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003663 public:
Roland Levillaina5c4a402016-03-15 15:02:50 +00003664 // Note that `comparison_type` is the type of comparison performed
3665 // between the comparison's inputs, not the type of the instantiated
3666 // HCompare instruction (which is always Primitive::kPrimInt).
3667 HCompare(Primitive::Type comparison_type,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003668 HInstruction* first,
3669 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04003670 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003671 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003672 : HBinaryOperation(Primitive::kPrimInt,
3673 first,
3674 second,
Roland Levillaina5c4a402016-03-15 15:02:50 +00003675 SideEffectsForArchRuntimeCalls(comparison_type),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003676 dex_pc) {
3677 SetPackedField<ComparisonBiasField>(bias);
Roland Levillain5b5b9312016-03-22 14:57:31 +00003678 DCHECK_EQ(comparison_type, Primitive::PrimitiveKind(first->GetType()));
3679 DCHECK_EQ(comparison_type, Primitive::PrimitiveKind(second->GetType()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003680 }
3681
Roland Levillain9867bc72015-08-05 10:21:34 +01003682 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00003683 int32_t Compute(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3684
3685 template <typename T>
3686 int32_t ComputeFP(T x, T y) const {
3687 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3688 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3689 // Handle the bias.
3690 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compute(x, y);
3691 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003692
Roland Levillain9867bc72015-08-05 10:21:34 +01003693 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003694 // Note that there is no "cmp-int" Dex instruction so we shouldn't
3695 // reach this code path when processing a freshly built HIR
3696 // graph. However HCompare integer instructions can be synthesized
3697 // by the instruction simplifier to implement IntegerCompare and
3698 // IntegerSignum intrinsics, so we have to handle this case.
3699 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003700 }
3701 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003702 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3703 }
3704 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3705 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
3706 }
3707 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3708 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01003709 }
3710
Vladimir Marko372f10e2016-05-17 16:30:10 +01003711 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003712 return GetPackedFields() == other->AsCompare()->GetPackedFields();
Calin Juravleddb7df22014-11-25 20:56:51 +00003713 }
3714
Vladimir Markoa1de9182016-02-25 11:37:38 +00003715 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
Mark Mendellc4701932015-04-10 13:18:51 -04003716
Roland Levillain31dd3d62016-02-16 12:21:02 +00003717 // Does this compare instruction have a "gt bias" (vs an "lt bias")?
Vladimir Markoa1de9182016-02-25 11:37:38 +00003718 // Only meaningful for floating-point comparisons.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003719 bool IsGtBias() const {
3720 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Vladimir Markoa1de9182016-02-25 11:37:38 +00003721 return GetBias() == ComparisonBias::kGtBias;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003722 }
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003723
Roland Levillain1693a1f2016-03-15 14:57:31 +00003724 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type ATTRIBUTE_UNUSED) {
3725 // Comparisons do not require a runtime call in any back end.
3726 return SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003727 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003728
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003729 DECLARE_INSTRUCTION(Compare);
3730
Roland Levillain31dd3d62016-02-16 12:21:02 +00003731 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003732 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3733 static constexpr size_t kFieldComparisonBiasSize =
3734 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3735 static constexpr size_t kNumberOfComparePackedBits =
3736 kFieldComparisonBias + kFieldComparisonBiasSize;
3737 static_assert(kNumberOfComparePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3738 using ComparisonBiasField =
3739 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3740
Roland Levillain31dd3d62016-02-16 12:21:02 +00003741 // Return an integer constant containing the result of a comparison evaluated at compile time.
3742 HIntConstant* MakeConstantComparison(int32_t value, uint32_t dex_pc) const {
3743 DCHECK(value == -1 || value == 0 || value == 1) << value;
3744 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3745 }
3746
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003747 private:
3748 DISALLOW_COPY_AND_ASSIGN(HCompare);
3749};
3750
Vladimir Markofcb503c2016-05-18 12:48:17 +01003751class HNewInstance FINAL : public HExpression<2> {
David Brazdil6de19382016-01-08 17:37:10 +00003752 public:
3753 HNewInstance(HInstruction* cls,
3754 HCurrentMethod* current_method,
3755 uint32_t dex_pc,
Andreas Gampea5b09a62016-11-17 15:21:22 -08003756 dex::TypeIndex type_index,
David Brazdil6de19382016-01-08 17:37:10 +00003757 const DexFile& dex_file,
Mingyao Yang062157f2016-03-02 10:15:36 -08003758 bool needs_access_check,
David Brazdil6de19382016-01-08 17:37:10 +00003759 bool finalizable,
3760 QuickEntrypointEnum entrypoint)
3761 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
3762 type_index_(type_index),
3763 dex_file_(dex_file),
David Brazdil6de19382016-01-08 17:37:10 +00003764 entrypoint_(entrypoint) {
Mingyao Yang062157f2016-03-02 10:15:36 -08003765 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
Vladimir Markoa1de9182016-02-25 11:37:38 +00003766 SetPackedFlag<kFlagFinalizable>(finalizable);
David Brazdil6de19382016-01-08 17:37:10 +00003767 SetRawInputAt(0, cls);
3768 SetRawInputAt(1, current_method);
3769 }
3770
Andreas Gampea5b09a62016-11-17 15:21:22 -08003771 dex::TypeIndex GetTypeIndex() const { return type_index_; }
David Brazdil6de19382016-01-08 17:37:10 +00003772 const DexFile& GetDexFile() const { return dex_file_; }
3773
3774 // Calls runtime so needs an environment.
3775 bool NeedsEnvironment() const OVERRIDE { return true; }
3776
Mingyao Yang062157f2016-03-02 10:15:36 -08003777 // Can throw errors when out-of-memory or if it's not instantiable/accessible.
3778 bool CanThrow() const OVERRIDE { return true; }
3779
3780 // Needs to call into runtime to make sure it's instantiable/accessible.
3781 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
David Brazdil6de19382016-01-08 17:37:10 +00003782
Vladimir Markoa1de9182016-02-25 11:37:38 +00003783 bool IsFinalizable() const { return GetPackedFlag<kFlagFinalizable>(); }
David Brazdil6de19382016-01-08 17:37:10 +00003784
3785 bool CanBeNull() const OVERRIDE { return false; }
3786
3787 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3788
3789 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
3790 entrypoint_ = entrypoint;
3791 }
3792
3793 bool IsStringAlloc() const;
3794
3795 DECLARE_INSTRUCTION(NewInstance);
3796
3797 private:
Mingyao Yang062157f2016-03-02 10:15:36 -08003798 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfExpressionPackedBits;
3799 static constexpr size_t kFlagFinalizable = kFlagNeedsAccessCheck + 1;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003800 static constexpr size_t kNumberOfNewInstancePackedBits = kFlagFinalizable + 1;
3801 static_assert(kNumberOfNewInstancePackedBits <= kMaxNumberOfPackedBits,
3802 "Too many packed fields.");
3803
Andreas Gampea5b09a62016-11-17 15:21:22 -08003804 const dex::TypeIndex type_index_;
David Brazdil6de19382016-01-08 17:37:10 +00003805 const DexFile& dex_file_;
David Brazdil6de19382016-01-08 17:37:10 +00003806 QuickEntrypointEnum entrypoint_;
3807
3808 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3809};
3810
Agi Csaki05f20562015-08-19 14:58:14 -07003811enum IntrinsicNeedsEnvironmentOrCache {
3812 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
3813 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07003814};
3815
Aart Bik5d75afe2015-12-14 11:57:01 -08003816enum IntrinsicSideEffects {
3817 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
3818 kReadSideEffects, // Intrinsic may read heap memory.
3819 kWriteSideEffects, // Intrinsic may write heap memory.
3820 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
3821};
3822
3823enum IntrinsicExceptions {
3824 kNoThrow, // Intrinsic does not throw any exceptions.
3825 kCanThrow // Intrinsic may throw exceptions.
3826};
3827
Mingyao Yanga9dbe832016-12-15 12:02:53 -08003828class HInvoke : public HVariableInputSizeInstruction {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003829 public:
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003830 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003831
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01003832 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003833 SetRawInputAt(index, argument);
3834 }
3835
Roland Levillain3e3d7332015-04-28 11:00:54 +01003836 // Return the number of arguments. This number can be lower than
3837 // the number of inputs returned by InputCount(), as some invoke
3838 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
3839 // inputs at the end of their list of inputs.
3840 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
3841
Vladimir Markoa1de9182016-02-25 11:37:38 +00003842 Primitive::Type GetType() const OVERRIDE { return GetPackedField<ReturnTypeField>(); }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003843
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003844 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003845 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003846
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003847 InvokeType GetInvokeType() const {
3848 return GetPackedField<InvokeTypeField>();
Vladimir Markoa1de9182016-02-25 11:37:38 +00003849 }
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003850
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01003851 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003852 return intrinsic_;
3853 }
3854
Aart Bik5d75afe2015-12-14 11:57:01 -08003855 void SetIntrinsic(Intrinsics intrinsic,
3856 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
3857 IntrinsicSideEffects side_effects,
3858 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003859
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003860 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00003861 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003862 }
3863
Aart Bikff7d89c2016-11-07 08:49:28 -08003864 void SetCanThrow(bool can_throw) { SetPackedFlag<kFlagCanThrow>(can_throw); }
3865
Vladimir Markoa1de9182016-02-25 11:37:38 +00003866 bool CanThrow() const OVERRIDE { return GetPackedFlag<kFlagCanThrow>(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08003867
Aart Bik71bf7b42016-11-16 10:17:46 -08003868 bool CanBeMoved() const OVERRIDE { return IsIntrinsic() && !DoesAnyWrite(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08003869
Vladimir Marko372f10e2016-05-17 16:30:10 +01003870 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Aart Bik5d75afe2015-12-14 11:57:01 -08003871 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
3872 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003873
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003874 uint32_t* GetIntrinsicOptimizations() {
3875 return &intrinsic_optimizations_;
3876 }
3877
3878 const uint32_t* GetIntrinsicOptimizations() const {
3879 return &intrinsic_optimizations_;
3880 }
3881
3882 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
3883
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003884 ArtMethod* GetResolvedMethod() const { return resolved_method_; }
3885
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003886 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003887
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003888 protected:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003889 static constexpr size_t kFieldInvokeType = kNumberOfGenericPackedBits;
3890 static constexpr size_t kFieldInvokeTypeSize =
Vladimir Markoa1de9182016-02-25 11:37:38 +00003891 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
3892 static constexpr size_t kFieldReturnType =
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003893 kFieldInvokeType + kFieldInvokeTypeSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003894 static constexpr size_t kFieldReturnTypeSize =
3895 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
3896 static constexpr size_t kFlagCanThrow = kFieldReturnType + kFieldReturnTypeSize;
3897 static constexpr size_t kNumberOfInvokePackedBits = kFlagCanThrow + 1;
3898 static_assert(kNumberOfInvokePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003899 using InvokeTypeField = BitField<InvokeType, kFieldInvokeType, kFieldInvokeTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003900 using ReturnTypeField = BitField<Primitive::Type, kFieldReturnType, kFieldReturnTypeSize>;
3901
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003902 HInvoke(ArenaAllocator* arena,
3903 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01003904 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003905 Primitive::Type return_type,
3906 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003907 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003908 ArtMethod* resolved_method,
3909 InvokeType invoke_type)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08003910 : HVariableInputSizeInstruction(
3911 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
3912 dex_pc,
3913 arena,
3914 number_of_arguments + number_of_other_inputs,
3915 kArenaAllocInvokeInputs),
Roland Levillain3e3d7332015-04-28 11:00:54 +01003916 number_of_arguments_(number_of_arguments),
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003917 resolved_method_(resolved_method),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003918 dex_method_index_(dex_method_index),
agicsaki57b81ec2015-08-11 17:39:37 -07003919 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003920 intrinsic_optimizations_(0) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003921 SetPackedField<ReturnTypeField>(return_type);
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003922 SetPackedField<InvokeTypeField>(invoke_type);
Vladimir Markoa1de9182016-02-25 11:37:38 +00003923 SetPackedFlag<kFlagCanThrow>(true);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003924 }
3925
Roland Levillain3e3d7332015-04-28 11:00:54 +01003926 uint32_t number_of_arguments_;
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003927 ArtMethod* const resolved_method_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003928 const uint32_t dex_method_index_;
3929 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003930
3931 // A magic word holding optimizations for intrinsics. See intrinsics.h.
3932 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003933
3934 private:
3935 DISALLOW_COPY_AND_ASSIGN(HInvoke);
3936};
3937
Vladimir Markofcb503c2016-05-18 12:48:17 +01003938class HInvokeUnresolved FINAL : public HInvoke {
Calin Juravle175dc732015-08-25 15:42:32 +01003939 public:
3940 HInvokeUnresolved(ArenaAllocator* arena,
3941 uint32_t number_of_arguments,
3942 Primitive::Type return_type,
3943 uint32_t dex_pc,
3944 uint32_t dex_method_index,
3945 InvokeType invoke_type)
3946 : HInvoke(arena,
3947 number_of_arguments,
3948 0u /* number_of_other_inputs */,
3949 return_type,
3950 dex_pc,
3951 dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003952 nullptr,
Calin Juravle175dc732015-08-25 15:42:32 +01003953 invoke_type) {
3954 }
3955
3956 DECLARE_INSTRUCTION(InvokeUnresolved);
3957
3958 private:
3959 DISALLOW_COPY_AND_ASSIGN(HInvokeUnresolved);
3960};
3961
Vladimir Markofcb503c2016-05-18 12:48:17 +01003962class HInvokeStaticOrDirect FINAL : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003963 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01003964 // Requirements of this method call regarding the class
3965 // initialization (clinit) check of its declaring class.
3966 enum class ClinitCheckRequirement {
3967 kNone, // Class already initialized.
3968 kExplicit, // Static call having explicit clinit check as last input.
3969 kImplicit, // Static call implicitly requiring a clinit check.
Vladimir Markoa1de9182016-02-25 11:37:38 +00003970 kLast = kImplicit
Roland Levillain4c0eb422015-04-24 16:43:49 +01003971 };
3972
Vladimir Marko58155012015-08-19 12:49:41 +00003973 // Determines how to load the target ArtMethod*.
3974 enum class MethodLoadKind {
3975 // Use a String init ArtMethod* loaded from Thread entrypoints.
3976 kStringInit,
3977
3978 // Use the method's own ArtMethod* loaded by the register allocator.
3979 kRecursive,
3980
3981 // Use ArtMethod* at a known address, embed the direct address in the code.
3982 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
3983 kDirectAddress,
3984
Vladimir Markoa1de9182016-02-25 11:37:38 +00003985 // Load from resolved methods array in the dex cache using a PC-relative load.
Vladimir Marko58155012015-08-19 12:49:41 +00003986 // Used when we need to use the dex cache, for example for invoke-static that
3987 // may cause class initialization (the entry may point to a resolution method),
3988 // and we know that we can access the dex cache arrays using a PC-relative load.
3989 kDexCachePcRelative,
3990
3991 // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*.
3992 // Used for JIT when we need to use the dex cache. This is also the last-resort-kind
3993 // used when other kinds are unavailable (say, dex cache arrays are not PC-relative)
3994 // or unimplemented or impractical (i.e. slow) on a particular architecture.
3995 kDexCacheViaMethod,
3996 };
3997
3998 // Determines the location of the code pointer.
3999 enum class CodePtrLocation {
4000 // Recursive call, use local PC-relative call instruction.
4001 kCallSelf,
4002
Vladimir Marko58155012015-08-19 12:49:41 +00004003 // Use code pointer from the ArtMethod*.
4004 // Used when we don't know the target code. This is also the last-resort-kind used when
4005 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
4006 kCallArtMethod,
4007 };
4008
4009 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01004010 MethodLoadKind method_load_kind;
4011 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00004012 // The method load data holds
4013 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
4014 // Note that there are multiple string init methods, each having its own offset.
4015 // - the method address for kDirectAddress
4016 // - the dex cache arrays offset for kDexCachePcRel.
Vladimir Markodc151b22015-10-15 18:02:30 +01004017 uint64_t method_load_data;
Vladimir Marko58155012015-08-19 12:49:41 +00004018 };
4019
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004020 HInvokeStaticOrDirect(ArenaAllocator* arena,
4021 uint32_t number_of_arguments,
4022 Primitive::Type return_type,
4023 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004024 uint32_t method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004025 ArtMethod* resolved_method,
Vladimir Marko58155012015-08-19 12:49:41 +00004026 DispatchInfo dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004027 InvokeType invoke_type,
4028 MethodReference target_method,
Roland Levillain4c0eb422015-04-24 16:43:49 +01004029 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01004030 : HInvoke(arena,
4031 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00004032 // There is potentially one extra argument for the HCurrentMethod node, and
4033 // potentially one other if the clinit check is explicit, and potentially
4034 // one other if the method is a string factory.
4035 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
David Brazdildee58d62016-04-07 09:54:26 +00004036 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004037 return_type,
4038 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004039 method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004040 resolved_method,
4041 invoke_type),
Vladimir Marko58155012015-08-19 12:49:41 +00004042 target_method_(target_method),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004043 dispatch_info_(dispatch_info) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004044 SetPackedField<ClinitCheckRequirementField>(clinit_check_requirement);
4045 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004046
Vladimir Markodc151b22015-10-15 18:02:30 +01004047 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00004048 bool had_current_method_input = HasCurrentMethodInput();
4049 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
4050
4051 // Using the current method is the default and once we find a better
4052 // method load kind, we should not go back to using the current method.
4053 DCHECK(had_current_method_input || !needs_current_method_input);
4054
4055 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004056 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
4057 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004058 }
Vladimir Markodc151b22015-10-15 18:02:30 +01004059 dispatch_info_ = dispatch_info;
4060 }
4061
Vladimir Markoc53c0792015-11-19 15:48:33 +00004062 void AddSpecialInput(HInstruction* input) {
4063 // We allow only one special input.
4064 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
4065 DCHECK(InputCount() == GetSpecialInputIndex() ||
4066 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
4067 InsertInputAt(GetSpecialInputIndex(), input);
4068 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004069
Vladimir Marko372f10e2016-05-17 16:30:10 +01004070 using HInstruction::GetInputRecords; // Keep the const version visible.
4071 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
4072 ArrayRef<HUserRecord<HInstruction*>> input_records = HInvoke::GetInputRecords();
4073 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck()) {
4074 DCHECK(!input_records.empty());
4075 DCHECK_GT(input_records.size(), GetNumberOfArguments());
4076 HInstruction* last_input = input_records.back().GetInstruction();
4077 // Note: `last_input` may be null during arguments setup.
4078 if (last_input != nullptr) {
4079 // `last_input` is the last input of a static invoke marked as having
4080 // an explicit clinit check. It must either be:
4081 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
4082 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
4083 DCHECK(last_input->IsClinitCheck() || last_input->IsLoadClass()) << last_input->DebugName();
4084 }
4085 }
4086 return input_records;
4087 }
4088
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004089 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004090 // We access the method via the dex cache so we can't do an implicit null check.
4091 // TODO: for intrinsics we can generate implicit null checks.
4092 return false;
4093 }
4094
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004095 bool CanBeNull() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004096 return GetPackedField<ReturnTypeField>() == Primitive::kPrimNot && !IsStringInit();
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004097 }
4098
Vladimir Markoc53c0792015-11-19 15:48:33 +00004099 // Get the index of the special input, if any.
4100 //
David Brazdil6de19382016-01-08 17:37:10 +00004101 // If the invoke HasCurrentMethodInput(), the "special input" is the current
4102 // method pointer; otherwise there may be one platform-specific special input,
4103 // such as PC-relative addressing base.
Vladimir Markoc53c0792015-11-19 15:48:33 +00004104 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray97793072016-02-16 15:33:54 +00004105 bool HasSpecialInput() const { return GetNumberOfArguments() != InputCount(); }
Vladimir Markoc53c0792015-11-19 15:48:33 +00004106
Vladimir Marko58155012015-08-19 12:49:41 +00004107 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
4108 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
4109 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01004110 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00004111 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko58155012015-08-19 12:49:41 +00004112 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004113 bool HasPcRelativeDexCache() const {
Vladimir Markodc151b22015-10-15 18:02:30 +01004114 return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative;
4115 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004116 bool HasCurrentMethodInput() const {
4117 // This function can be called only after the invoke has been fully initialized by the builder.
4118 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004119 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004120 return true;
4121 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004122 DCHECK(InputCount() == GetSpecialInputIndex() ||
4123 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004124 return false;
4125 }
4126 }
Vladimir Marko58155012015-08-19 12:49:41 +00004127
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004128 QuickEntrypointEnum GetStringInitEntryPoint() const {
Vladimir Marko58155012015-08-19 12:49:41 +00004129 DCHECK(IsStringInit());
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004130 return static_cast<QuickEntrypointEnum>(dispatch_info_.method_load_data);
Vladimir Marko58155012015-08-19 12:49:41 +00004131 }
4132
4133 uint64_t GetMethodAddress() const {
4134 DCHECK(HasMethodAddress());
4135 return dispatch_info_.method_load_data;
4136 }
4137
4138 uint32_t GetDexCacheArrayOffset() const {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004139 DCHECK(HasPcRelativeDexCache());
Vladimir Marko58155012015-08-19 12:49:41 +00004140 return dispatch_info_.method_load_data;
4141 }
4142
Vladimir Markoa1de9182016-02-25 11:37:38 +00004143 ClinitCheckRequirement GetClinitCheckRequirement() const {
4144 return GetPackedField<ClinitCheckRequirementField>();
4145 }
Calin Juravle68ad6492015-08-18 17:08:12 +01004146
Roland Levillain4c0eb422015-04-24 16:43:49 +01004147 // Is this instruction a call to a static method?
4148 bool IsStatic() const {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004149 return GetInvokeType() == kStatic;
4150 }
4151
4152 MethodReference GetTargetMethod() const {
4153 return target_method_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01004154 }
4155
Vladimir Markofbb184a2015-11-13 14:47:00 +00004156 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
4157 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
4158 // instruction; only relevant for static calls with explicit clinit check.
4159 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01004160 DCHECK(IsStaticWithExplicitClinitCheck());
Vladimir Marko372f10e2016-05-17 16:30:10 +01004161 size_t last_input_index = inputs_.size() - 1u;
4162 HInstruction* last_input = inputs_.back().GetInstruction();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004163 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004164 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004165 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004166 inputs_.pop_back();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004167 SetPackedField<ClinitCheckRequirementField>(new_requirement);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004168 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004169 }
4170
4171 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00004172 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01004173 bool IsStaticWithExplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004174 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kExplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004175 }
4176
4177 // Is this a call to a static method whose declaring class has an
4178 // implicit intialization check requirement?
4179 bool IsStaticWithImplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004180 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kImplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004181 }
4182
Vladimir Markob554b5a2015-11-06 12:57:55 +00004183 // Does this method load kind need the current method as an input?
4184 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
4185 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kDexCacheViaMethod;
4186 }
4187
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004188 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004189
4190 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004191 static constexpr size_t kFieldClinitCheckRequirement = kNumberOfInvokePackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004192 static constexpr size_t kFieldClinitCheckRequirementSize =
4193 MinimumBitsToStore(static_cast<size_t>(ClinitCheckRequirement::kLast));
4194 static constexpr size_t kNumberOfInvokeStaticOrDirectPackedBits =
4195 kFieldClinitCheckRequirement + kFieldClinitCheckRequirementSize;
4196 static_assert(kNumberOfInvokeStaticOrDirectPackedBits <= kMaxNumberOfPackedBits,
4197 "Too many packed fields.");
Vladimir Markoa1de9182016-02-25 11:37:38 +00004198 using ClinitCheckRequirementField = BitField<ClinitCheckRequirement,
4199 kFieldClinitCheckRequirement,
4200 kFieldClinitCheckRequirementSize>;
4201
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004202 // Cached values of the resolved method, to avoid needing the mutator lock.
Vladimir Marko58155012015-08-19 12:49:41 +00004203 MethodReference target_method_;
4204 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004205
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004206 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004207};
Vladimir Markof64242a2015-12-01 14:58:23 +00004208std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004209std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004210
Vladimir Markofcb503c2016-05-18 12:48:17 +01004211class HInvokeVirtual FINAL : public HInvoke {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004212 public:
4213 HInvokeVirtual(ArenaAllocator* arena,
4214 uint32_t number_of_arguments,
4215 Primitive::Type return_type,
4216 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004217 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004218 ArtMethod* resolved_method,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004219 uint32_t vtable_index)
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004220 : HInvoke(arena,
4221 number_of_arguments,
4222 0u,
4223 return_type,
4224 dex_pc,
4225 dex_method_index,
4226 resolved_method,
4227 kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004228 vtable_index_(vtable_index) {}
4229
Aart Bik71bf7b42016-11-16 10:17:46 -08004230 bool CanBeNull() const OVERRIDE {
4231 switch (GetIntrinsic()) {
4232 case Intrinsics::kThreadCurrentThread:
4233 case Intrinsics::kStringBufferAppend:
4234 case Intrinsics::kStringBufferToString:
4235 case Intrinsics::kStringBuilderAppend:
4236 case Intrinsics::kStringBuilderToString:
4237 return false;
4238 default:
4239 return HInvoke::CanBeNull();
4240 }
4241 }
4242
Calin Juravle641547a2015-04-21 22:08:51 +01004243 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004244 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004245 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004246 }
4247
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004248 uint32_t GetVTableIndex() const { return vtable_index_; }
4249
4250 DECLARE_INSTRUCTION(InvokeVirtual);
4251
4252 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004253 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004254 const uint32_t vtable_index_;
4255
4256 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
4257};
4258
Vladimir Markofcb503c2016-05-18 12:48:17 +01004259class HInvokeInterface FINAL : public HInvoke {
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004260 public:
4261 HInvokeInterface(ArenaAllocator* arena,
4262 uint32_t number_of_arguments,
4263 Primitive::Type return_type,
4264 uint32_t dex_pc,
4265 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004266 ArtMethod* resolved_method,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004267 uint32_t imt_index)
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004268 : HInvoke(arena,
4269 number_of_arguments,
4270 0u,
4271 return_type,
4272 dex_pc,
4273 dex_method_index,
4274 resolved_method,
4275 kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004276 imt_index_(imt_index) {}
4277
Calin Juravle641547a2015-04-21 22:08:51 +01004278 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004279 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004280 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004281 }
4282
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004283 uint32_t GetImtIndex() const { return imt_index_; }
4284 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
4285
4286 DECLARE_INSTRUCTION(InvokeInterface);
4287
4288 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004289 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004290 const uint32_t imt_index_;
4291
4292 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
4293};
4294
Vladimir Markofcb503c2016-05-18 12:48:17 +01004295class HNeg FINAL : public HUnaryOperation {
Roland Levillain88cb1752014-10-20 16:36:47 +01004296 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004297 HNeg(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Roland Levillain937e6cd2016-03-22 11:54:37 +00004298 : HUnaryOperation(result_type, input, dex_pc) {
4299 DCHECK_EQ(result_type, Primitive::PrimitiveKind(input->GetType()));
4300 }
Roland Levillain88cb1752014-10-20 16:36:47 +01004301
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004302 template <typename T> static T Compute(T x) { return -x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004303
4304 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004305 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004306 }
4307 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004308 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004309 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004310 HConstant* Evaluate(HFloatConstant* x) const OVERRIDE {
4311 return GetBlock()->GetGraph()->GetFloatConstant(Compute(x->GetValue()), GetDexPc());
4312 }
4313 HConstant* Evaluate(HDoubleConstant* x) const OVERRIDE {
4314 return GetBlock()->GetGraph()->GetDoubleConstant(Compute(x->GetValue()), GetDexPc());
4315 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01004316
Roland Levillain88cb1752014-10-20 16:36:47 +01004317 DECLARE_INSTRUCTION(Neg);
4318
4319 private:
4320 DISALLOW_COPY_AND_ASSIGN(HNeg);
4321};
4322
Vladimir Markofcb503c2016-05-18 12:48:17 +01004323class HNewArray FINAL : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004324 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004325 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004326 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004327 uint32_t dex_pc,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004328 dex::TypeIndex type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004329 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004330 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004331 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004332 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004333 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004334 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004335 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004336 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004337 }
4338
Andreas Gampea5b09a62016-11-17 15:21:22 -08004339 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004340 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004341
4342 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00004343 bool NeedsEnvironment() const OVERRIDE { return true; }
4344
Mingyao Yang0c365e62015-03-31 15:09:29 -07004345 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
4346 bool CanThrow() const OVERRIDE { return true; }
4347
Calin Juravle10e244f2015-01-26 18:54:32 +00004348 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004349
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004350 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
4351
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004352 DECLARE_INSTRUCTION(NewArray);
4353
4354 private:
Andreas Gampea5b09a62016-11-17 15:21:22 -08004355 const dex::TypeIndex type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004356 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004357 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004358
4359 DISALLOW_COPY_AND_ASSIGN(HNewArray);
4360};
4361
Vladimir Markofcb503c2016-05-18 12:48:17 +01004362class HAdd FINAL : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004363 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004364 HAdd(Primitive::Type result_type,
4365 HInstruction* left,
4366 HInstruction* right,
4367 uint32_t dex_pc = kNoDexPc)
4368 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004369
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004370 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004371
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004372 template <typename T> static T Compute(T x, T y) { return x + y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004373
4374 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004375 return GetBlock()->GetGraph()->GetIntConstant(
4376 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004377 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004378 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004379 return GetBlock()->GetGraph()->GetLongConstant(
4380 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004381 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004382 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4383 return GetBlock()->GetGraph()->GetFloatConstant(
4384 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4385 }
4386 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4387 return GetBlock()->GetGraph()->GetDoubleConstant(
4388 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4389 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004390
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004391 DECLARE_INSTRUCTION(Add);
4392
4393 private:
4394 DISALLOW_COPY_AND_ASSIGN(HAdd);
4395};
4396
Vladimir Markofcb503c2016-05-18 12:48:17 +01004397class HSub FINAL : public HBinaryOperation {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004398 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004399 HSub(Primitive::Type result_type,
4400 HInstruction* left,
4401 HInstruction* right,
4402 uint32_t dex_pc = kNoDexPc)
4403 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004404
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004405 template <typename T> static T Compute(T x, T y) { return x - y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004406
4407 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004408 return GetBlock()->GetGraph()->GetIntConstant(
4409 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004410 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004411 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004412 return GetBlock()->GetGraph()->GetLongConstant(
4413 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004414 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004415 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4416 return GetBlock()->GetGraph()->GetFloatConstant(
4417 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4418 }
4419 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4420 return GetBlock()->GetGraph()->GetDoubleConstant(
4421 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4422 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004423
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004424 DECLARE_INSTRUCTION(Sub);
4425
4426 private:
4427 DISALLOW_COPY_AND_ASSIGN(HSub);
4428};
4429
Vladimir Markofcb503c2016-05-18 12:48:17 +01004430class HMul FINAL : public HBinaryOperation {
Calin Juravle34bacdf2014-10-07 20:23:36 +01004431 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004432 HMul(Primitive::Type result_type,
4433 HInstruction* left,
4434 HInstruction* right,
4435 uint32_t dex_pc = kNoDexPc)
4436 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01004437
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004438 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004439
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004440 template <typename T> static T Compute(T x, T y) { return x * y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004441
4442 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004443 return GetBlock()->GetGraph()->GetIntConstant(
4444 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004445 }
4446 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004447 return GetBlock()->GetGraph()->GetLongConstant(
4448 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004449 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004450 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4451 return GetBlock()->GetGraph()->GetFloatConstant(
4452 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4453 }
4454 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4455 return GetBlock()->GetGraph()->GetDoubleConstant(
4456 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4457 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004458
4459 DECLARE_INSTRUCTION(Mul);
4460
4461 private:
4462 DISALLOW_COPY_AND_ASSIGN(HMul);
4463};
4464
Vladimir Markofcb503c2016-05-18 12:48:17 +01004465class HDiv FINAL : public HBinaryOperation {
Calin Juravle7c4954d2014-10-28 16:57:40 +00004466 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004467 HDiv(Primitive::Type result_type,
4468 HInstruction* left,
4469 HInstruction* right,
4470 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01004471 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00004472
Roland Levillain9867bc72015-08-05 10:21:34 +01004473 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004474 T ComputeIntegral(T x, T y) const {
4475 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004476 // Our graph structure ensures we never have 0 for `y` during
4477 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004478 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00004479 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004480 return (y == -1) ? -x : x / y;
4481 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004482
Roland Levillain31dd3d62016-02-16 12:21:02 +00004483 template <typename T>
4484 T ComputeFP(T x, T y) const {
4485 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4486 return x / y;
4487 }
4488
Roland Levillain9867bc72015-08-05 10:21:34 +01004489 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004490 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004491 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004492 }
4493 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004494 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004495 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
4496 }
4497 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4498 return GetBlock()->GetGraph()->GetFloatConstant(
4499 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4500 }
4501 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4502 return GetBlock()->GetGraph()->GetDoubleConstant(
4503 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004504 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004505
4506 DECLARE_INSTRUCTION(Div);
4507
4508 private:
4509 DISALLOW_COPY_AND_ASSIGN(HDiv);
4510};
4511
Vladimir Markofcb503c2016-05-18 12:48:17 +01004512class HRem FINAL : public HBinaryOperation {
Calin Juravlebacfec32014-11-14 15:54:36 +00004513 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004514 HRem(Primitive::Type result_type,
4515 HInstruction* left,
4516 HInstruction* right,
4517 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01004518 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00004519
Roland Levillain9867bc72015-08-05 10:21:34 +01004520 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004521 T ComputeIntegral(T x, T y) const {
4522 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004523 // Our graph structure ensures we never have 0 for `y` during
4524 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00004525 DCHECK_NE(y, 0);
4526 // Special case -1 to avoid getting a SIGFPE on x86(_64).
4527 return (y == -1) ? 0 : x % y;
4528 }
4529
Roland Levillain31dd3d62016-02-16 12:21:02 +00004530 template <typename T>
4531 T ComputeFP(T x, T y) const {
4532 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4533 return std::fmod(x, y);
4534 }
4535
Roland Levillain9867bc72015-08-05 10:21:34 +01004536 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004537 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004538 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004539 }
4540 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004541 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004542 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004543 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004544 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4545 return GetBlock()->GetGraph()->GetFloatConstant(
4546 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4547 }
4548 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4549 return GetBlock()->GetGraph()->GetDoubleConstant(
4550 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4551 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004552
4553 DECLARE_INSTRUCTION(Rem);
4554
4555 private:
Calin Juravlebacfec32014-11-14 15:54:36 +00004556 DISALLOW_COPY_AND_ASSIGN(HRem);
4557};
4558
Vladimir Markofcb503c2016-05-18 12:48:17 +01004559class HDivZeroCheck FINAL : public HExpression<1> {
Calin Juravled0d48522014-11-04 16:40:20 +00004560 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00004561 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
4562 // constructor.
Calin Juravled0d48522014-11-04 16:40:20 +00004563 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Alexandre Rames780aece2016-01-13 14:34:39 +00004564 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00004565 SetRawInputAt(0, value);
4566 }
4567
Serguei Katkov8c0676c2015-08-03 13:55:33 +06004568 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
4569
Calin Juravled0d48522014-11-04 16:40:20 +00004570 bool CanBeMoved() const OVERRIDE { return true; }
4571
Vladimir Marko372f10e2016-05-17 16:30:10 +01004572 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00004573 return true;
4574 }
4575
4576 bool NeedsEnvironment() const OVERRIDE { return true; }
4577 bool CanThrow() const OVERRIDE { return true; }
4578
Calin Juravled0d48522014-11-04 16:40:20 +00004579 DECLARE_INSTRUCTION(DivZeroCheck);
4580
4581 private:
Calin Juravled0d48522014-11-04 16:40:20 +00004582 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
4583};
4584
Vladimir Markofcb503c2016-05-18 12:48:17 +01004585class HShl FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004586 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004587 HShl(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004588 HInstruction* value,
4589 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004590 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004591 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4592 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4593 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004594 }
4595
Roland Levillain5b5b9312016-03-22 14:57:31 +00004596 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004597 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004598 return value << (distance & max_shift_distance);
4599 }
4600
4601 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004602 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004603 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004604 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004605 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004606 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004607 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004608 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004609 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4610 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4611 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4612 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004613 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004614 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4615 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004616 LOG(FATAL) << DebugName() << " is not defined for float values";
4617 UNREACHABLE();
4618 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004619 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4620 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004621 LOG(FATAL) << DebugName() << " is not defined for double values";
4622 UNREACHABLE();
4623 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004624
4625 DECLARE_INSTRUCTION(Shl);
4626
4627 private:
4628 DISALLOW_COPY_AND_ASSIGN(HShl);
4629};
4630
Vladimir Markofcb503c2016-05-18 12:48:17 +01004631class HShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004632 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004633 HShr(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004634 HInstruction* value,
4635 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004636 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004637 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4638 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4639 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004640 }
4641
Roland Levillain5b5b9312016-03-22 14:57:31 +00004642 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004643 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004644 return value >> (distance & max_shift_distance);
4645 }
4646
4647 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004648 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004649 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004650 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004651 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004652 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004653 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004654 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004655 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4656 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4657 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4658 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004659 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004660 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4661 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004662 LOG(FATAL) << DebugName() << " is not defined for float values";
4663 UNREACHABLE();
4664 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004665 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4666 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004667 LOG(FATAL) << DebugName() << " is not defined for double values";
4668 UNREACHABLE();
4669 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004670
4671 DECLARE_INSTRUCTION(Shr);
4672
4673 private:
4674 DISALLOW_COPY_AND_ASSIGN(HShr);
4675};
4676
Vladimir Markofcb503c2016-05-18 12:48:17 +01004677class HUShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004678 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004679 HUShr(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004680 HInstruction* value,
4681 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004682 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004683 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4684 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4685 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004686 }
4687
Roland Levillain5b5b9312016-03-22 14:57:31 +00004688 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004689 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004690 typedef typename std::make_unsigned<T>::type V;
4691 V ux = static_cast<V>(value);
4692 return static_cast<T>(ux >> (distance & max_shift_distance));
4693 }
4694
4695 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004696 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004697 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004698 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004699 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004700 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004701 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004702 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004703 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4704 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4705 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4706 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004707 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004708 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4709 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004710 LOG(FATAL) << DebugName() << " is not defined for float values";
4711 UNREACHABLE();
4712 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004713 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4714 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004715 LOG(FATAL) << DebugName() << " is not defined for double values";
4716 UNREACHABLE();
4717 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004718
4719 DECLARE_INSTRUCTION(UShr);
4720
4721 private:
4722 DISALLOW_COPY_AND_ASSIGN(HUShr);
4723};
4724
Vladimir Markofcb503c2016-05-18 12:48:17 +01004725class HAnd FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004726 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004727 HAnd(Primitive::Type result_type,
4728 HInstruction* left,
4729 HInstruction* right,
4730 uint32_t dex_pc = kNoDexPc)
4731 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004732
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004733 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004734
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004735 template <typename T> static T Compute(T x, T y) { return x & y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004736
4737 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004738 return GetBlock()->GetGraph()->GetIntConstant(
4739 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004740 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004741 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004742 return GetBlock()->GetGraph()->GetLongConstant(
4743 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004744 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004745 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4746 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4747 LOG(FATAL) << DebugName() << " is not defined for float values";
4748 UNREACHABLE();
4749 }
4750 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4751 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4752 LOG(FATAL) << DebugName() << " is not defined for double values";
4753 UNREACHABLE();
4754 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004755
4756 DECLARE_INSTRUCTION(And);
4757
4758 private:
4759 DISALLOW_COPY_AND_ASSIGN(HAnd);
4760};
4761
Vladimir Markofcb503c2016-05-18 12:48:17 +01004762class HOr FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004763 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004764 HOr(Primitive::Type result_type,
4765 HInstruction* left,
4766 HInstruction* right,
4767 uint32_t dex_pc = kNoDexPc)
4768 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004769
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004770 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004771
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004772 template <typename T> static T Compute(T x, T y) { return x | y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004773
4774 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004775 return GetBlock()->GetGraph()->GetIntConstant(
4776 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004777 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004778 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004779 return GetBlock()->GetGraph()->GetLongConstant(
4780 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004781 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004782 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4783 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4784 LOG(FATAL) << DebugName() << " is not defined for float values";
4785 UNREACHABLE();
4786 }
4787 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4788 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4789 LOG(FATAL) << DebugName() << " is not defined for double values";
4790 UNREACHABLE();
4791 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004792
4793 DECLARE_INSTRUCTION(Or);
4794
4795 private:
4796 DISALLOW_COPY_AND_ASSIGN(HOr);
4797};
4798
Vladimir Markofcb503c2016-05-18 12:48:17 +01004799class HXor FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004800 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004801 HXor(Primitive::Type result_type,
4802 HInstruction* left,
4803 HInstruction* right,
4804 uint32_t dex_pc = kNoDexPc)
4805 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004806
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004807 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004808
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004809 template <typename T> static T Compute(T x, T y) { return x ^ y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004810
4811 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004812 return GetBlock()->GetGraph()->GetIntConstant(
4813 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004814 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004815 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004816 return GetBlock()->GetGraph()->GetLongConstant(
4817 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004818 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004819 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4820 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4821 LOG(FATAL) << DebugName() << " is not defined for float values";
4822 UNREACHABLE();
4823 }
4824 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4825 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4826 LOG(FATAL) << DebugName() << " is not defined for double values";
4827 UNREACHABLE();
4828 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004829
4830 DECLARE_INSTRUCTION(Xor);
4831
4832 private:
4833 DISALLOW_COPY_AND_ASSIGN(HXor);
4834};
4835
Vladimir Markofcb503c2016-05-18 12:48:17 +01004836class HRor FINAL : public HBinaryOperation {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004837 public:
4838 HRor(Primitive::Type result_type, HInstruction* value, HInstruction* distance)
Roland Levillain22c49222016-03-18 14:04:28 +00004839 : HBinaryOperation(result_type, value, distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004840 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4841 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain22c49222016-03-18 14:04:28 +00004842 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004843
Roland Levillain5b5b9312016-03-22 14:57:31 +00004844 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004845 static T Compute(T value, int32_t distance, int32_t max_shift_value) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004846 typedef typename std::make_unsigned<T>::type V;
4847 V ux = static_cast<V>(value);
4848 if ((distance & max_shift_value) == 0) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004849 return static_cast<T>(ux);
4850 } else {
4851 const V reg_bits = sizeof(T) * 8;
Roland Levillain5b5b9312016-03-22 14:57:31 +00004852 return static_cast<T>(ux >> (distance & max_shift_value)) |
4853 (value << (reg_bits - (distance & max_shift_value)));
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004854 }
4855 }
4856
Roland Levillain5b5b9312016-03-22 14:57:31 +00004857 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004858 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004859 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004860 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004861 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004862 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004863 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004864 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004865 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4866 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4867 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4868 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004869 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004870 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4871 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004872 LOG(FATAL) << DebugName() << " is not defined for float values";
4873 UNREACHABLE();
4874 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004875 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4876 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004877 LOG(FATAL) << DebugName() << " is not defined for double values";
4878 UNREACHABLE();
4879 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004880
4881 DECLARE_INSTRUCTION(Ror);
4882
4883 private:
4884 DISALLOW_COPY_AND_ASSIGN(HRor);
4885};
4886
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004887// The value of a parameter in this method. Its location depends on
4888// the calling convention.
Vladimir Markofcb503c2016-05-18 12:48:17 +01004889class HParameterValue FINAL : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004890 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004891 HParameterValue(const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004892 dex::TypeIndex type_index,
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004893 uint8_t index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004894 Primitive::Type parameter_type,
4895 bool is_this = false)
4896 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004897 dex_file_(dex_file),
4898 type_index_(type_index),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004899 index_(index) {
4900 SetPackedFlag<kFlagIsThis>(is_this);
4901 SetPackedFlag<kFlagCanBeNull>(!is_this);
4902 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004903
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004904 const DexFile& GetDexFile() const { return dex_file_; }
Andreas Gampea5b09a62016-11-17 15:21:22 -08004905 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004906 uint8_t GetIndex() const { return index_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00004907 bool IsThis() const { return GetPackedFlag<kFlagIsThis>(); }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004908
Vladimir Markoa1de9182016-02-25 11:37:38 +00004909 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
4910 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00004911
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004912 DECLARE_INSTRUCTION(ParameterValue);
4913
4914 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00004915 // Whether or not the parameter value corresponds to 'this' argument.
4916 static constexpr size_t kFlagIsThis = kNumberOfExpressionPackedBits;
4917 static constexpr size_t kFlagCanBeNull = kFlagIsThis + 1;
4918 static constexpr size_t kNumberOfParameterValuePackedBits = kFlagCanBeNull + 1;
4919 static_assert(kNumberOfParameterValuePackedBits <= kMaxNumberOfPackedBits,
4920 "Too many packed fields.");
4921
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004922 const DexFile& dex_file_;
Andreas Gampea5b09a62016-11-17 15:21:22 -08004923 const dex::TypeIndex type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004924 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00004925 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004926 const uint8_t index_;
4927
4928 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
4929};
4930
Vladimir Markofcb503c2016-05-18 12:48:17 +01004931class HNot FINAL : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004932 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004933 HNot(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
4934 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004935
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004936 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01004937 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004938 return true;
4939 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004940
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004941 template <typename T> static T Compute(T x) { return ~x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004942
4943 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004944 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004945 }
4946 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004947 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004948 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004949 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4950 LOG(FATAL) << DebugName() << " is not defined for float values";
4951 UNREACHABLE();
4952 }
4953 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4954 LOG(FATAL) << DebugName() << " is not defined for double values";
4955 UNREACHABLE();
4956 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004957
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004958 DECLARE_INSTRUCTION(Not);
4959
4960 private:
4961 DISALLOW_COPY_AND_ASSIGN(HNot);
4962};
4963
Vladimir Markofcb503c2016-05-18 12:48:17 +01004964class HBooleanNot FINAL : public HUnaryOperation {
David Brazdil66d126e2015-04-03 16:02:44 +01004965 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004966 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
4967 : HUnaryOperation(Primitive::Type::kPrimBoolean, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01004968
4969 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01004970 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01004971 return true;
4972 }
4973
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004974 template <typename T> static bool Compute(T x) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004975 DCHECK(IsUint<1>(x)) << x;
David Brazdil66d126e2015-04-03 16:02:44 +01004976 return !x;
4977 }
4978
Roland Levillain9867bc72015-08-05 10:21:34 +01004979 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004980 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004981 }
4982 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4983 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01004984 UNREACHABLE();
4985 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004986 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4987 LOG(FATAL) << DebugName() << " is not defined for float values";
4988 UNREACHABLE();
4989 }
4990 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4991 LOG(FATAL) << DebugName() << " is not defined for double values";
4992 UNREACHABLE();
4993 }
David Brazdil66d126e2015-04-03 16:02:44 +01004994
4995 DECLARE_INSTRUCTION(BooleanNot);
4996
4997 private:
4998 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
4999};
5000
Vladimir Markofcb503c2016-05-18 12:48:17 +01005001class HTypeConversion FINAL : public HExpression<1> {
Roland Levillaindff1f282014-11-05 14:15:05 +00005002 public:
5003 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00005004 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005005 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00005006 SetRawInputAt(0, input);
Roland Levillainf355c3f2016-03-30 19:09:03 +01005007 // Invariant: We should never generate a conversion to a Boolean value.
5008 DCHECK_NE(Primitive::kPrimBoolean, result_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00005009 }
5010
5011 HInstruction* GetInput() const { return InputAt(0); }
5012 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
5013 Primitive::Type GetResultType() const { return GetType(); }
5014
5015 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005016 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
5017 return true;
5018 }
Roland Levillaindff1f282014-11-05 14:15:05 +00005019
Mark Mendelle82549b2015-05-06 10:55:34 -04005020 // Try to statically evaluate the conversion and return a HConstant
5021 // containing the result. If the input cannot be converted, return nullptr.
5022 HConstant* TryStaticEvaluation() const;
5023
Roland Levillaindff1f282014-11-05 14:15:05 +00005024 DECLARE_INSTRUCTION(TypeConversion);
5025
5026 private:
5027 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
5028};
5029
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00005030static constexpr uint32_t kNoRegNumber = -1;
5031
Vladimir Markofcb503c2016-05-18 12:48:17 +01005032class HNullCheck FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005033 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005034 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
5035 // constructor.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005036 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005037 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005038 SetRawInputAt(0, value);
5039 }
5040
Calin Juravle10e244f2015-01-26 18:54:32 +00005041 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005042 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005043 return true;
5044 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005045
Calin Juravle10e244f2015-01-26 18:54:32 +00005046 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005047
Calin Juravle10e244f2015-01-26 18:54:32 +00005048 bool CanThrow() const OVERRIDE { return true; }
5049
5050 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005051
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005052
5053 DECLARE_INSTRUCTION(NullCheck);
5054
5055 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005056 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
5057};
5058
5059class FieldInfo : public ValueObject {
5060 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005061 FieldInfo(MemberOffset field_offset,
5062 Primitive::Type field_type,
5063 bool is_volatile,
5064 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005065 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005066 const DexFile& dex_file,
5067 Handle<mirror::DexCache> dex_cache)
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005068 : field_offset_(field_offset),
5069 field_type_(field_type),
5070 is_volatile_(is_volatile),
5071 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005072 declaring_class_def_index_(declaring_class_def_index),
Mathieu Chartier736b5602015-09-02 14:54:11 -07005073 dex_file_(dex_file),
5074 dex_cache_(dex_cache) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005075
5076 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005077 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005078 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07005079 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005080 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00005081 bool IsVolatile() const { return is_volatile_; }
Mathieu Chartier736b5602015-09-02 14:54:11 -07005082 Handle<mirror::DexCache> GetDexCache() const { return dex_cache_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005083
5084 private:
5085 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01005086 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00005087 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005088 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07005089 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005090 const DexFile& dex_file_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005091 const Handle<mirror::DexCache> dex_cache_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005092};
5093
Vladimir Markofcb503c2016-05-18 12:48:17 +01005094class HInstanceFieldGet FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005095 public:
5096 HInstanceFieldGet(HInstruction* value,
5097 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005098 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005099 bool is_volatile,
5100 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005101 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005102 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005103 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005104 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005105 : HExpression(field_type, SideEffects::FieldReadOfType(field_type, is_volatile), dex_pc),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005106 field_info_(field_offset,
5107 field_type,
5108 is_volatile,
5109 field_idx,
5110 declaring_class_def_index,
5111 dex_file,
5112 dex_cache) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005113 SetRawInputAt(0, value);
5114 }
5115
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005116 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005117
Vladimir Marko372f10e2016-05-17 16:30:10 +01005118 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
5119 const HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005120 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005121 }
5122
Calin Juravle641547a2015-04-21 22:08:51 +01005123 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005124 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005125 }
5126
5127 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01005128 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5129 }
5130
Calin Juravle52c48962014-12-16 17:02:57 +00005131 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005132 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005133 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005134 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005135
5136 DECLARE_INSTRUCTION(InstanceFieldGet);
5137
5138 private:
5139 const FieldInfo field_info_;
5140
5141 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
5142};
5143
Vladimir Markofcb503c2016-05-18 12:48:17 +01005144class HInstanceFieldSet FINAL : public HTemplateInstruction<2> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005145 public:
5146 HInstanceFieldSet(HInstruction* object,
5147 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01005148 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005149 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005150 bool is_volatile,
5151 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005152 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005153 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005154 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005155 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005156 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile), dex_pc),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005157 field_info_(field_offset,
5158 field_type,
5159 is_volatile,
5160 field_idx,
5161 declaring_class_def_index,
5162 dex_file,
Vladimir Markoa1de9182016-02-25 11:37:38 +00005163 dex_cache) {
5164 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005165 SetRawInputAt(0, object);
5166 SetRawInputAt(1, value);
5167 }
5168
Calin Juravle641547a2015-04-21 22:08:51 +01005169 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005170 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005171 }
5172
Calin Juravle52c48962014-12-16 17:02:57 +00005173 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005174 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005175 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005176 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005177 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005178 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5179 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005180
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005181 DECLARE_INSTRUCTION(InstanceFieldSet);
5182
5183 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005184 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5185 static constexpr size_t kNumberOfInstanceFieldSetPackedBits = kFlagValueCanBeNull + 1;
5186 static_assert(kNumberOfInstanceFieldSetPackedBits <= kMaxNumberOfPackedBits,
5187 "Too many packed fields.");
5188
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005189 const FieldInfo field_info_;
5190
5191 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
5192};
5193
Vladimir Markofcb503c2016-05-18 12:48:17 +01005194class HArrayGet FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005195 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005196 HArrayGet(HInstruction* array,
5197 HInstruction* index,
5198 Primitive::Type type,
5199 uint32_t dex_pc,
5200 bool is_string_char_at = false)
Aart Bik18b36ab2016-04-13 16:41:35 -07005201 : HExpression(type, SideEffects::ArrayReadOfType(type), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005202 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005203 SetRawInputAt(0, array);
5204 SetRawInputAt(1, index);
5205 }
5206
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005207 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005208 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005209 return true;
5210 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005211 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005212 // TODO: We can be smarter here.
5213 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
5214 // which generates the implicit null check. There are cases when these can be removed
5215 // to produce better code. If we ever add optimizations to do so we should allow an
5216 // implicit check here (as long as the address falls in the first page).
5217 return false;
5218 }
5219
David Brazdil4833f5a2015-12-16 10:37:39 +00005220 bool IsEquivalentOf(HArrayGet* other) const {
5221 bool result = (GetDexPc() == other->GetDexPc());
5222 if (kIsDebugBuild && result) {
5223 DCHECK_EQ(GetBlock(), other->GetBlock());
5224 DCHECK_EQ(GetArray(), other->GetArray());
5225 DCHECK_EQ(GetIndex(), other->GetIndex());
5226 if (Primitive::IsIntOrLongType(GetType())) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005227 DCHECK(Primitive::IsFloatingPointType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005228 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005229 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
5230 DCHECK(Primitive::IsIntOrLongType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005231 }
5232 }
5233 return result;
5234 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005235
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005236 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
5237
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005238 HInstruction* GetArray() const { return InputAt(0); }
5239 HInstruction* GetIndex() const { return InputAt(1); }
5240
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005241 DECLARE_INSTRUCTION(ArrayGet);
5242
5243 private:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005244 // We treat a String as an array, creating the HArrayGet from String.charAt()
5245 // intrinsic in the instruction simplifier. We can always determine whether
5246 // a particular HArrayGet is actually a String.charAt() by looking at the type
5247 // of the input but that requires holding the mutator lock, so we prefer to use
5248 // a flag, so that code generators don't need to do the locking.
5249 static constexpr size_t kFlagIsStringCharAt = kNumberOfExpressionPackedBits;
5250 static constexpr size_t kNumberOfArrayGetPackedBits = kFlagIsStringCharAt + 1;
5251 static_assert(kNumberOfArrayGetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5252 "Too many packed fields.");
5253
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005254 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
5255};
5256
Vladimir Markofcb503c2016-05-18 12:48:17 +01005257class HArraySet FINAL : public HTemplateInstruction<3> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005258 public:
5259 HArraySet(HInstruction* array,
5260 HInstruction* index,
5261 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005262 Primitive::Type expected_component_type,
Aart Bik18b36ab2016-04-13 16:41:35 -07005263 uint32_t dex_pc)
5264 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005265 SetPackedField<ExpectedComponentTypeField>(expected_component_type);
5266 SetPackedFlag<kFlagNeedsTypeCheck>(value->GetType() == Primitive::kPrimNot);
5267 SetPackedFlag<kFlagValueCanBeNull>(true);
5268 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(false);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005269 SetRawInputAt(0, array);
5270 SetRawInputAt(1, index);
5271 SetRawInputAt(2, value);
Aart Bik18b36ab2016-04-13 16:41:35 -07005272 // Make a best guess now, may be refined during SSA building.
5273 ComputeSideEffects();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005274 }
5275
Calin Juravle77520bc2015-01-12 18:45:46 +00005276 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005277 // We call a runtime method to throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005278 return NeedsTypeCheck();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005279 }
5280
Mingyao Yang81014cb2015-06-02 03:16:27 -07005281 // Can throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005282 bool CanThrow() const OVERRIDE { return NeedsTypeCheck(); }
Mingyao Yang81014cb2015-06-02 03:16:27 -07005283
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005284 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005285 // TODO: Same as for ArrayGet.
5286 return false;
5287 }
5288
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005289 void ClearNeedsTypeCheck() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005290 SetPackedFlag<kFlagNeedsTypeCheck>(false);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005291 }
5292
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005293 void ClearValueCanBeNull() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005294 SetPackedFlag<kFlagValueCanBeNull>(false);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005295 }
5296
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005297 void SetStaticTypeOfArrayIsObjectArray() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005298 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(true);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005299 }
5300
Vladimir Markoa1de9182016-02-25 11:37:38 +00005301 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5302 bool NeedsTypeCheck() const { return GetPackedFlag<kFlagNeedsTypeCheck>(); }
5303 bool StaticTypeOfArrayIsObjectArray() const {
5304 return GetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>();
5305 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005306
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005307 HInstruction* GetArray() const { return InputAt(0); }
5308 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005309 HInstruction* GetValue() const { return InputAt(2); }
5310
5311 Primitive::Type GetComponentType() const {
5312 // The Dex format does not type floating point index operations. Since the
5313 // `expected_component_type_` is set during building and can therefore not
5314 // be correct, we also check what is the value type. If it is a floating
5315 // point type, we must use that type.
5316 Primitive::Type value_type = GetValue()->GetType();
5317 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
5318 ? value_type
Vladimir Markoa1de9182016-02-25 11:37:38 +00005319 : GetRawExpectedComponentType();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005320 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005321
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005322 Primitive::Type GetRawExpectedComponentType() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005323 return GetPackedField<ExpectedComponentTypeField>();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005324 }
5325
Aart Bik18b36ab2016-04-13 16:41:35 -07005326 void ComputeSideEffects() {
5327 Primitive::Type type = GetComponentType();
5328 SetSideEffects(SideEffects::ArrayWriteOfType(type).Union(
5329 SideEffectsForArchRuntimeCalls(type)));
5330 }
5331
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005332 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
5333 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
5334 }
5335
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005336 DECLARE_INSTRUCTION(ArraySet);
5337
5338 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005339 static constexpr size_t kFieldExpectedComponentType = kNumberOfGenericPackedBits;
5340 static constexpr size_t kFieldExpectedComponentTypeSize =
5341 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
5342 static constexpr size_t kFlagNeedsTypeCheck =
5343 kFieldExpectedComponentType + kFieldExpectedComponentTypeSize;
5344 static constexpr size_t kFlagValueCanBeNull = kFlagNeedsTypeCheck + 1;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005345 // Cached information for the reference_type_info_ so that codegen
5346 // does not need to inspect the static type.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005347 static constexpr size_t kFlagStaticTypeOfArrayIsObjectArray = kFlagValueCanBeNull + 1;
5348 static constexpr size_t kNumberOfArraySetPackedBits =
5349 kFlagStaticTypeOfArrayIsObjectArray + 1;
5350 static_assert(kNumberOfArraySetPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5351 using ExpectedComponentTypeField =
5352 BitField<Primitive::Type, kFieldExpectedComponentType, kFieldExpectedComponentTypeSize>;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005353
5354 DISALLOW_COPY_AND_ASSIGN(HArraySet);
5355};
5356
Vladimir Markofcb503c2016-05-18 12:48:17 +01005357class HArrayLength FINAL : public HExpression<1> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005358 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005359 HArrayLength(HInstruction* array, uint32_t dex_pc, bool is_string_length = false)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005360 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005361 SetPackedFlag<kFlagIsStringLength>(is_string_length);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005362 // Note that arrays do not change length, so the instruction does not
5363 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005364 SetRawInputAt(0, array);
5365 }
5366
Calin Juravle77520bc2015-01-12 18:45:46 +00005367 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005368 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005369 return true;
5370 }
Calin Juravle641547a2015-04-21 22:08:51 +01005371 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5372 return obj == InputAt(0);
5373 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005374
Vladimir Markodce016e2016-04-28 13:10:02 +01005375 bool IsStringLength() const { return GetPackedFlag<kFlagIsStringLength>(); }
5376
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005377 DECLARE_INSTRUCTION(ArrayLength);
5378
5379 private:
Vladimir Markodce016e2016-04-28 13:10:02 +01005380 // We treat a String as an array, creating the HArrayLength from String.length()
5381 // or String.isEmpty() intrinsic in the instruction simplifier. We can always
5382 // determine whether a particular HArrayLength is actually a String.length() by
5383 // looking at the type of the input but that requires holding the mutator lock, so
5384 // we prefer to use a flag, so that code generators don't need to do the locking.
5385 static constexpr size_t kFlagIsStringLength = kNumberOfExpressionPackedBits;
5386 static constexpr size_t kNumberOfArrayLengthPackedBits = kFlagIsStringLength + 1;
5387 static_assert(kNumberOfArrayLengthPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5388 "Too many packed fields.");
5389
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005390 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
5391};
5392
Vladimir Markofcb503c2016-05-18 12:48:17 +01005393class HBoundsCheck FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005394 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005395 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
5396 // constructor.
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005397 HBoundsCheck(HInstruction* index,
5398 HInstruction* length,
5399 uint32_t dex_pc,
5400 uint32_t string_char_at_method_index = DexFile::kDexNoIndex)
5401 : HExpression(index->GetType(), SideEffects::CanTriggerGC(), dex_pc),
5402 string_char_at_method_index_(string_char_at_method_index) {
David Brazdildee58d62016-04-07 09:54:26 +00005403 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(index->GetType()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005404 SetRawInputAt(0, index);
5405 SetRawInputAt(1, length);
5406 }
5407
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005408 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005409 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005410 return true;
5411 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005412
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005413 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005414
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005415 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005416
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005417 bool IsStringCharAt() const { return GetStringCharAtMethodIndex() != DexFile::kDexNoIndex; }
5418 uint32_t GetStringCharAtMethodIndex() const { return string_char_at_method_index_; }
5419
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005420 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005421
5422 DECLARE_INSTRUCTION(BoundsCheck);
5423
5424 private:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005425 // We treat a String as an array, creating the HBoundsCheck from String.charAt()
5426 // intrinsic in the instruction simplifier. We want to include the String.charAt()
5427 // in the stack trace if we actually throw the StringIndexOutOfBoundsException,
5428 // so we need to create an HEnvironment which will be translated to an InlineInfo
5429 // indicating the extra stack frame. Since we add this HEnvironment quite late,
5430 // in the PrepareForRegisterAllocation pass, we need to remember the method index
5431 // from the invoke as we don't want to look again at the dex bytecode.
5432 uint32_t string_char_at_method_index_; // DexFile::kDexNoIndex if regular array.
5433
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005434 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
5435};
5436
Vladimir Markofcb503c2016-05-18 12:48:17 +01005437class HSuspendCheck FINAL : public HTemplateInstruction<0> {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005438 public:
David Brazdil86ea7ee2016-02-16 09:26:07 +00005439 explicit HSuspendCheck(uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005440 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005441
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005442 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005443 return true;
5444 }
5445
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005446 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
5447 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005448
5449 DECLARE_INSTRUCTION(SuspendCheck);
5450
5451 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005452 // Only used for code generation, in order to share the same slow path between back edges
5453 // of a same loop.
5454 SlowPathCode* slow_path_;
5455
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005456 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
5457};
5458
David Srbecky0cf44932015-12-09 14:09:59 +00005459// Pseudo-instruction which provides the native debugger with mapping information.
5460// It ensures that we can generate line number and local variables at this point.
5461class HNativeDebugInfo : public HTemplateInstruction<0> {
5462 public:
5463 explicit HNativeDebugInfo(uint32_t dex_pc)
5464 : HTemplateInstruction<0>(SideEffects::None(), dex_pc) {}
5465
5466 bool NeedsEnvironment() const OVERRIDE {
5467 return true;
5468 }
5469
5470 DECLARE_INSTRUCTION(NativeDebugInfo);
5471
5472 private:
5473 DISALLOW_COPY_AND_ASSIGN(HNativeDebugInfo);
5474};
5475
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005476/**
5477 * Instruction to load a Class object.
5478 */
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005479class HLoadClass FINAL : public HInstruction {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005480 public:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005481 // Determines how to load the Class.
5482 enum class LoadKind {
5483 // Use the Class* from the method's own ArtMethod*.
5484 kReferrersClass,
5485
5486 // Use boot image Class* address that will be known at link time.
5487 // Used for boot image classes referenced by boot image code in non-PIC mode.
5488 kBootImageLinkTimeAddress,
5489
5490 // Use PC-relative boot image Class* address that will be known at link time.
5491 // Used for boot image classes referenced by boot image code in PIC mode.
5492 kBootImageLinkTimePcRelative,
5493
5494 // Use a known boot image Class* address, embedded in the code by the codegen.
5495 // Used for boot image classes referenced by apps in AOT- and JIT-compiled code.
5496 // Note: codegen needs to emit a linker patch if indicated by compiler options'
5497 // GetIncludePatchInformation().
5498 kBootImageAddress,
5499
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005500 // Load from the root table associated with the JIT compiled method.
5501 kJitTableAddress,
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005502
5503 // Load from resolved types array in the dex cache using a PC-relative load.
5504 // Used for classes outside boot image when we know that we can access
5505 // the dex cache arrays using a PC-relative load.
5506 kDexCachePcRelative,
5507
5508 // Load from resolved types array accessed through the class loaded from
5509 // the compiled method's own ArtMethod*. This is the default access type when
5510 // all other types are unavailable.
5511 kDexCacheViaMethod,
5512
5513 kLast = kDexCacheViaMethod
5514 };
5515
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005516 HLoadClass(HCurrentMethod* current_method,
Andreas Gampea5b09a62016-11-17 15:21:22 -08005517 dex::TypeIndex type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005518 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005519 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01005520 uint32_t dex_pc,
Nicolas Geoffray56876342016-12-16 16:09:08 +00005521 bool needs_access_check)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005522 : HInstruction(SideEffectsForArchRuntimeCalls(), dex_pc),
5523 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005524 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005525 dex_file_(dex_file),
Calin Juravle2e768302015-07-28 14:41:11 +00005526 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01005527 // Referrers class should not need access check. We never inline unverified
5528 // methods so we can't possibly end up in this situation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005529 DCHECK(!is_referrers_class || !needs_access_check);
5530
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005531 SetPackedField<LoadKindField>(
5532 is_referrers_class ? LoadKind::kReferrersClass : LoadKind::kDexCacheViaMethod);
Vladimir Markoa1de9182016-02-25 11:37:38 +00005533 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
Nicolas Geoffray56876342016-12-16 16:09:08 +00005534 SetPackedFlag<kFlagIsInDexCache>(false);
5535 SetPackedFlag<kFlagIsInBootImage>(false);
Vladimir Markoa1de9182016-02-25 11:37:38 +00005536 SetPackedFlag<kFlagGenerateClInitCheck>(false);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005537 }
5538
5539 void SetLoadKindWithAddress(LoadKind load_kind, uint64_t address) {
5540 DCHECK(HasAddress(load_kind));
5541 load_data_.address = address;
5542 SetLoadKindInternal(load_kind);
5543 }
5544
5545 void SetLoadKindWithTypeReference(LoadKind load_kind,
5546 const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08005547 dex::TypeIndex type_index) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005548 DCHECK(HasTypeReference(load_kind));
5549 DCHECK(IsSameDexFile(dex_file_, dex_file));
5550 DCHECK_EQ(type_index_, type_index);
5551 SetLoadKindInternal(load_kind);
5552 }
5553
5554 void SetLoadKindWithDexCacheReference(LoadKind load_kind,
5555 const DexFile& dex_file,
5556 uint32_t element_index) {
5557 DCHECK(HasDexCacheReference(load_kind));
5558 DCHECK(IsSameDexFile(dex_file_, dex_file));
5559 load_data_.dex_cache_element_index = element_index;
5560 SetLoadKindInternal(load_kind);
5561 }
5562
5563 LoadKind GetLoadKind() const {
5564 return GetPackedField<LoadKindField>();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005565 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005566
5567 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005568
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005569 bool InstructionDataEquals(const HInstruction* other) const;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005570
Andreas Gampea5b09a62016-11-17 15:21:22 -08005571 size_t ComputeHashCode() const OVERRIDE { return type_index_.index_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005572
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01005573 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005574
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005575 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005576 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005577 }
5578
Calin Juravle0ba218d2015-05-19 18:46:01 +01005579 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00005580 // The entrypoint the code generator is going to call does not do
5581 // clinit of the class.
5582 DCHECK(!NeedsAccessCheck());
Vladimir Markoa1de9182016-02-25 11:37:38 +00005583 SetPackedFlag<kFlagGenerateClInitCheck>(generate_clinit_check);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005584 }
5585
5586 bool CanCallRuntime() const {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005587 return MustGenerateClinitCheck() ||
Vladimir Markoa1de9182016-02-25 11:37:38 +00005588 (!IsReferrersClass() && !IsInDexCache()) ||
5589 NeedsAccessCheck();
Calin Juravle98893e12015-10-02 21:05:03 +01005590 }
5591
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005592 bool CanThrow() const OVERRIDE {
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01005593 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005594 }
5595
Calin Juravleacf735c2015-02-12 15:25:22 +00005596 ReferenceTypeInfo GetLoadedClassRTI() {
5597 return loaded_class_rti_;
5598 }
5599
5600 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
5601 // Make sure we only set exact types (the loaded class should never be merged).
5602 DCHECK(rti.IsExact());
5603 loaded_class_rti_ = rti;
5604 }
5605
Andreas Gampea5b09a62016-11-17 15:21:22 -08005606 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005607 const DexFile& GetDexFile() const { return dex_file_; }
5608
5609 uint32_t GetDexCacheElementOffset() const;
5610
5611 uint64_t GetAddress() const {
5612 DCHECK(HasAddress(GetLoadKind()));
5613 return load_data_.address;
5614 }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005615
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005616 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
5617 return !IsReferrersClass();
5618 }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00005619
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005620 static SideEffects SideEffectsForArchRuntimeCalls() {
5621 return SideEffects::CanTriggerGC();
5622 }
5623
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005624 bool IsReferrersClass() const { return GetLoadKind() == LoadKind::kReferrersClass; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005625 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
5626 bool IsInDexCache() const { return GetPackedFlag<kFlagIsInDexCache>(); }
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005627 bool IsInBootImage() const { return GetPackedFlag<kFlagIsInBootImage>(); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005628 bool MustGenerateClinitCheck() const { return GetPackedFlag<kFlagGenerateClInitCheck>(); }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005629
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005630 void MarkInDexCache() {
5631 SetPackedFlag<kFlagIsInDexCache>(true);
5632 DCHECK(!NeedsEnvironment());
5633 RemoveEnvironment();
5634 SetSideEffects(SideEffects::None());
5635 }
5636
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005637 void MarkInBootImage() {
5638 SetPackedFlag<kFlagIsInBootImage>(true);
5639 }
5640
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005641 void AddSpecialInput(HInstruction* special_input);
5642
5643 using HInstruction::GetInputRecords; // Keep the const version visible.
5644 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
5645 return ArrayRef<HUserRecord<HInstruction*>>(
5646 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
5647 }
5648
5649 Primitive::Type GetType() const OVERRIDE {
5650 return Primitive::kPrimNot;
5651 }
5652
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005653 DECLARE_INSTRUCTION(LoadClass);
5654
5655 private:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005656 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005657 static constexpr size_t kFlagIsInDexCache = kFlagNeedsAccessCheck + 1;
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005658 static constexpr size_t kFlagIsInBootImage = kFlagIsInDexCache + 1;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005659 // Whether this instruction must generate the initialization check.
5660 // Used for code generation.
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005661 static constexpr size_t kFlagGenerateClInitCheck = kFlagIsInBootImage + 1;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005662 static constexpr size_t kFieldLoadKind = kFlagGenerateClInitCheck + 1;
5663 static constexpr size_t kFieldLoadKindSize =
5664 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
5665 static constexpr size_t kNumberOfLoadClassPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005666 static_assert(kNumberOfLoadClassPackedBits < kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005667 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
5668
5669 static bool HasTypeReference(LoadKind load_kind) {
5670 return load_kind == LoadKind::kBootImageLinkTimeAddress ||
5671 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
5672 load_kind == LoadKind::kDexCacheViaMethod ||
5673 load_kind == LoadKind::kReferrersClass;
5674 }
5675
5676 static bool HasAddress(LoadKind load_kind) {
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005677 return load_kind == LoadKind::kBootImageAddress ||
5678 load_kind == LoadKind::kJitTableAddress;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005679 }
5680
5681 static bool HasDexCacheReference(LoadKind load_kind) {
5682 return load_kind == LoadKind::kDexCachePcRelative;
5683 }
5684
5685 void SetLoadKindInternal(LoadKind load_kind);
5686
5687 // The special input is the HCurrentMethod for kDexCacheViaMethod or kReferrersClass.
5688 // For other load kinds it's empty or possibly some architecture-specific instruction
5689 // for PC-relative loads, i.e. kDexCachePcRelative or kBootImageLinkTimePcRelative.
5690 HUserRecord<HInstruction*> special_input_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005691
Andreas Gampea5b09a62016-11-17 15:21:22 -08005692 const dex::TypeIndex type_index_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005693 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005694
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005695 union {
5696 uint32_t dex_cache_element_index; // Only for dex cache reference.
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005697 uint64_t address; // Up to 64-bit, needed for kJitTableAddress on 64-bit targets.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005698 } load_data_;
5699
Calin Juravleacf735c2015-02-12 15:25:22 +00005700 ReferenceTypeInfo loaded_class_rti_;
5701
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005702 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
5703};
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005704std::ostream& operator<<(std::ostream& os, HLoadClass::LoadKind rhs);
5705
5706// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
5707inline uint32_t HLoadClass::GetDexCacheElementOffset() const {
5708 DCHECK(HasDexCacheReference(GetLoadKind())) << GetLoadKind();
5709 return load_data_.dex_cache_element_index;
5710}
5711
5712// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
5713inline void HLoadClass::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005714 // The special input is used for PC-relative loads on some architectures,
5715 // including literal pool loads, which are PC-relative too.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005716 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Alexey Frunze06a46c42016-07-19 15:00:40 -07005717 GetLoadKind() == LoadKind::kDexCachePcRelative ||
5718 GetLoadKind() == LoadKind::kBootImageLinkTimeAddress ||
5719 GetLoadKind() == LoadKind::kBootImageAddress) << GetLoadKind();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005720 DCHECK(special_input_.GetInstruction() == nullptr);
5721 special_input_ = HUserRecord<HInstruction*>(special_input);
5722 special_input->AddUseAt(this, 0);
5723}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005724
Vladimir Marko372f10e2016-05-17 16:30:10 +01005725class HLoadString FINAL : public HInstruction {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005726 public:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005727 // Determines how to load the String.
5728 enum class LoadKind {
5729 // Use boot image String* address that will be known at link time.
5730 // Used for boot image strings referenced by boot image code in non-PIC mode.
5731 kBootImageLinkTimeAddress,
5732
5733 // Use PC-relative boot image String* address that will be known at link time.
5734 // Used for boot image strings referenced by boot image code in PIC mode.
5735 kBootImageLinkTimePcRelative,
5736
5737 // Use a known boot image String* address, embedded in the code by the codegen.
5738 // Used for boot image strings referenced by apps in AOT- and JIT-compiled code.
5739 // Note: codegen needs to emit a linker patch if indicated by compiler options'
5740 // GetIncludePatchInformation().
5741 kBootImageAddress,
5742
Vladimir Markoaad75c62016-10-03 08:46:48 +00005743 // Load from an entry in the .bss section using a PC-relative load.
5744 // Used for strings outside boot image when .bss is accessible with a PC-relative load.
5745 kBssEntry,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005746
5747 // Load from resolved strings array accessed through the class loaded from
5748 // the compiled method's own ArtMethod*. This is the default access type when
5749 // all other types are unavailable.
5750 kDexCacheViaMethod,
5751
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005752 // Load from the root table associated with the JIT compiled method.
5753 kJitTableAddress,
5754
5755 kLast = kJitTableAddress,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005756 };
5757
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005758 HLoadString(HCurrentMethod* current_method,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005759 dex::StringIndex string_index,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005760 const DexFile& dex_file,
5761 uint32_t dex_pc)
Vladimir Marko372f10e2016-05-17 16:30:10 +01005762 : HInstruction(SideEffectsForArchRuntimeCalls(), dex_pc),
5763 special_input_(HUserRecord<HInstruction*>(current_method)),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005764 string_index_(string_index) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005765 SetPackedField<LoadKindField>(LoadKind::kDexCacheViaMethod);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005766 load_data_.dex_file_ = &dex_file;
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005767 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005768
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005769 void SetLoadKindWithAddress(LoadKind load_kind, uint64_t address) {
5770 DCHECK(HasAddress(load_kind));
5771 load_data_.address = address;
5772 SetLoadKindInternal(load_kind);
5773 }
5774
5775 void SetLoadKindWithStringReference(LoadKind load_kind,
5776 const DexFile& dex_file,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005777 dex::StringIndex string_index) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005778 DCHECK(HasStringReference(load_kind));
Vladimir Markoaad75c62016-10-03 08:46:48 +00005779 load_data_.dex_file_ = &dex_file;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005780 string_index_ = string_index;
5781 SetLoadKindInternal(load_kind);
5782 }
5783
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005784 LoadKind GetLoadKind() const {
5785 return GetPackedField<LoadKindField>();
5786 }
5787
5788 const DexFile& GetDexFile() const;
5789
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005790 dex::StringIndex GetStringIndex() const {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005791 return string_index_;
5792 }
5793
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005794 uint64_t GetAddress() const {
5795 DCHECK(HasAddress(GetLoadKind()));
5796 return load_data_.address;
5797 }
5798
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005799 bool CanBeMoved() const OVERRIDE { return true; }
5800
Vladimir Marko372f10e2016-05-17 16:30:10 +01005801 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005802
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005803 size_t ComputeHashCode() const OVERRIDE { return string_index_.index_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005804
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005805 // Will call the runtime if we need to load the string through
5806 // the dex cache and the string is not guaranteed to be there yet.
5807 bool NeedsEnvironment() const OVERRIDE {
5808 LoadKind load_kind = GetLoadKind();
5809 if (load_kind == LoadKind::kBootImageLinkTimeAddress ||
5810 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005811 load_kind == LoadKind::kBootImageAddress ||
5812 load_kind == LoadKind::kJitTableAddress) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005813 return false;
5814 }
Vladimir Marko4d1be492017-01-06 14:43:11 +00005815 return true;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005816 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005817
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005818 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
5819 return GetLoadKind() == LoadKind::kDexCacheViaMethod;
5820 }
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005821
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07005822 bool CanBeNull() const OVERRIDE { return false; }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005823 bool CanThrow() const OVERRIDE { return NeedsEnvironment(); }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005824
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005825 static SideEffects SideEffectsForArchRuntimeCalls() {
5826 return SideEffects::CanTriggerGC();
5827 }
5828
Vladimir Marko372f10e2016-05-17 16:30:10 +01005829 void AddSpecialInput(HInstruction* special_input);
5830
5831 using HInstruction::GetInputRecords; // Keep the const version visible.
5832 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
5833 return ArrayRef<HUserRecord<HInstruction*>>(
5834 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005835 }
5836
Vladimir Marko372f10e2016-05-17 16:30:10 +01005837 Primitive::Type GetType() const OVERRIDE {
5838 return Primitive::kPrimNot;
5839 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005840
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005841 DECLARE_INSTRUCTION(LoadString);
5842
5843 private:
Vladimir Marko4d1be492017-01-06 14:43:11 +00005844 static constexpr size_t kFieldLoadKind = kNumberOfGenericPackedBits;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005845 static constexpr size_t kFieldLoadKindSize =
5846 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
5847 static constexpr size_t kNumberOfLoadStringPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005848 static_assert(kNumberOfLoadStringPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005849 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005850
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005851 static bool HasStringReference(LoadKind load_kind) {
5852 return load_kind == LoadKind::kBootImageLinkTimeAddress ||
5853 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoaad75c62016-10-03 08:46:48 +00005854 load_kind == LoadKind::kBssEntry ||
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005855 load_kind == LoadKind::kDexCacheViaMethod ||
5856 load_kind == LoadKind::kJitTableAddress;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005857 }
5858
5859 static bool HasAddress(LoadKind load_kind) {
Vladimir Marko54d6a202016-11-09 12:46:38 +00005860 return load_kind == LoadKind::kBootImageAddress;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005861 }
5862
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005863 void SetLoadKindInternal(LoadKind load_kind);
5864
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005865 // The special input is the HCurrentMethod for kDexCacheViaMethod.
5866 // For other load kinds it's empty or possibly some architecture-specific instruction
5867 // for PC-relative loads, i.e. kDexCachePcRelative or kBootImageLinkTimePcRelative.
Vladimir Marko372f10e2016-05-17 16:30:10 +01005868 HUserRecord<HInstruction*> special_input_;
5869
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005870 // String index serves also as the hash code and it's also needed for slow-paths,
5871 // so it must not be overwritten with other load data.
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005872 dex::StringIndex string_index_;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005873
5874 union {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005875 const DexFile* dex_file_; // For string reference.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005876 uint64_t address; // Up to 64-bit, needed for kDexCacheAddress on 64-bit targets.
5877 } load_data_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005878
5879 DISALLOW_COPY_AND_ASSIGN(HLoadString);
5880};
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005881std::ostream& operator<<(std::ostream& os, HLoadString::LoadKind rhs);
5882
5883// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
5884inline const DexFile& HLoadString::GetDexFile() const {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005885 DCHECK(HasStringReference(GetLoadKind())) << GetLoadKind();
5886 return *load_data_.dex_file_;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005887}
5888
5889// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
5890inline void HLoadString::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005891 // The special input is used for PC-relative loads on some architectures,
5892 // including literal pool loads, which are PC-relative too.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005893 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoaad75c62016-10-03 08:46:48 +00005894 GetLoadKind() == LoadKind::kBssEntry ||
Alexey Frunze06a46c42016-07-19 15:00:40 -07005895 GetLoadKind() == LoadKind::kBootImageLinkTimeAddress ||
5896 GetLoadKind() == LoadKind::kBootImageAddress) << GetLoadKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01005897 // HLoadString::GetInputRecords() returns an empty array at this point,
5898 // so use the GetInputRecords() from the base class to set the input record.
5899 DCHECK(special_input_.GetInstruction() == nullptr);
5900 special_input_ = HUserRecord<HInstruction*>(special_input);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005901 special_input->AddUseAt(this, 0);
5902}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005903
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005904/**
5905 * Performs an initialization check on its Class object input.
5906 */
Vladimir Markofcb503c2016-05-18 12:48:17 +01005907class HClinitCheck FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005908 public:
Roland Levillain3887c462015-08-12 18:15:42 +01005909 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07005910 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005911 Primitive::kPrimNot,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005912 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
5913 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005914 SetRawInputAt(0, constant);
5915 }
5916
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005917 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005918 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005919 return true;
5920 }
5921
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005922 bool NeedsEnvironment() const OVERRIDE {
5923 // May call runtime to initialize the class.
5924 return true;
5925 }
5926
Nicolas Geoffray729645a2015-11-19 13:29:02 +00005927 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005928
5929 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
5930
5931 DECLARE_INSTRUCTION(ClinitCheck);
5932
5933 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005934 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
5935};
5936
Vladimir Markofcb503c2016-05-18 12:48:17 +01005937class HStaticFieldGet FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005938 public:
5939 HStaticFieldGet(HInstruction* cls,
5940 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005941 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005942 bool is_volatile,
5943 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005944 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005945 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005946 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005947 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005948 : HExpression(field_type, SideEffects::FieldReadOfType(field_type, is_volatile), dex_pc),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005949 field_info_(field_offset,
5950 field_type,
5951 is_volatile,
5952 field_idx,
5953 declaring_class_def_index,
5954 dex_file,
5955 dex_cache) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005956 SetRawInputAt(0, cls);
5957 }
5958
Calin Juravle52c48962014-12-16 17:02:57 +00005959
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005960 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005961
Vladimir Marko372f10e2016-05-17 16:30:10 +01005962 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
5963 const HStaticFieldGet* other_get = other->AsStaticFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005964 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005965 }
5966
5967 size_t ComputeHashCode() const OVERRIDE {
5968 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5969 }
5970
Calin Juravle52c48962014-12-16 17:02:57 +00005971 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005972 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5973 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005974 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005975
5976 DECLARE_INSTRUCTION(StaticFieldGet);
5977
5978 private:
5979 const FieldInfo field_info_;
5980
5981 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
5982};
5983
Vladimir Markofcb503c2016-05-18 12:48:17 +01005984class HStaticFieldSet FINAL : public HTemplateInstruction<2> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005985 public:
5986 HStaticFieldSet(HInstruction* cls,
5987 HInstruction* value,
5988 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005989 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005990 bool is_volatile,
5991 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005992 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005993 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005994 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005995 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005996 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile), dex_pc),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005997 field_info_(field_offset,
5998 field_type,
5999 is_volatile,
6000 field_idx,
6001 declaring_class_def_index,
6002 dex_file,
Vladimir Markoa1de9182016-02-25 11:37:38 +00006003 dex_cache) {
6004 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006005 SetRawInputAt(0, cls);
6006 SetRawInputAt(1, value);
6007 }
6008
Calin Juravle52c48962014-12-16 17:02:57 +00006009 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006010 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
6011 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006012 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006013
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006014 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006015 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
6016 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006017
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006018 DECLARE_INSTRUCTION(StaticFieldSet);
6019
6020 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006021 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
6022 static constexpr size_t kNumberOfStaticFieldSetPackedBits = kFlagValueCanBeNull + 1;
6023 static_assert(kNumberOfStaticFieldSetPackedBits <= kMaxNumberOfPackedBits,
6024 "Too many packed fields.");
6025
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006026 const FieldInfo field_info_;
6027
6028 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
6029};
6030
Vladimir Markofcb503c2016-05-18 12:48:17 +01006031class HUnresolvedInstanceFieldGet FINAL : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006032 public:
6033 HUnresolvedInstanceFieldGet(HInstruction* obj,
6034 Primitive::Type field_type,
6035 uint32_t field_index,
6036 uint32_t dex_pc)
6037 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
6038 field_index_(field_index) {
6039 SetRawInputAt(0, obj);
6040 }
6041
6042 bool NeedsEnvironment() const OVERRIDE { return true; }
6043 bool CanThrow() const OVERRIDE { return true; }
6044
6045 Primitive::Type GetFieldType() const { return GetType(); }
6046 uint32_t GetFieldIndex() const { return field_index_; }
6047
6048 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
6049
6050 private:
6051 const uint32_t field_index_;
6052
6053 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldGet);
6054};
6055
Vladimir Markofcb503c2016-05-18 12:48:17 +01006056class HUnresolvedInstanceFieldSet FINAL : public HTemplateInstruction<2> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006057 public:
6058 HUnresolvedInstanceFieldSet(HInstruction* obj,
6059 HInstruction* value,
6060 Primitive::Type field_type,
6061 uint32_t field_index,
6062 uint32_t dex_pc)
6063 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006064 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006065 SetPackedField<FieldTypeField>(field_type);
David Brazdildee58d62016-04-07 09:54:26 +00006066 DCHECK_EQ(Primitive::PrimitiveKind(field_type), Primitive::PrimitiveKind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006067 SetRawInputAt(0, obj);
6068 SetRawInputAt(1, value);
6069 }
6070
6071 bool NeedsEnvironment() const OVERRIDE { return true; }
6072 bool CanThrow() const OVERRIDE { return true; }
6073
Vladimir Markoa1de9182016-02-25 11:37:38 +00006074 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006075 uint32_t GetFieldIndex() const { return field_index_; }
6076
6077 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
6078
6079 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006080 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6081 static constexpr size_t kFieldFieldTypeSize =
6082 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
6083 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6084 kFieldFieldType + kFieldFieldTypeSize;
6085 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6086 "Too many packed fields.");
6087 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
6088
Calin Juravlee460d1d2015-09-29 04:52:17 +01006089 const uint32_t field_index_;
6090
6091 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldSet);
6092};
6093
Vladimir Markofcb503c2016-05-18 12:48:17 +01006094class HUnresolvedStaticFieldGet FINAL : public HExpression<0> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006095 public:
6096 HUnresolvedStaticFieldGet(Primitive::Type field_type,
6097 uint32_t field_index,
6098 uint32_t dex_pc)
6099 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
6100 field_index_(field_index) {
6101 }
6102
6103 bool NeedsEnvironment() const OVERRIDE { return true; }
6104 bool CanThrow() const OVERRIDE { return true; }
6105
6106 Primitive::Type GetFieldType() const { return GetType(); }
6107 uint32_t GetFieldIndex() const { return field_index_; }
6108
6109 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
6110
6111 private:
6112 const uint32_t field_index_;
6113
6114 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldGet);
6115};
6116
Vladimir Markofcb503c2016-05-18 12:48:17 +01006117class HUnresolvedStaticFieldSet FINAL : public HTemplateInstruction<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006118 public:
6119 HUnresolvedStaticFieldSet(HInstruction* value,
6120 Primitive::Type field_type,
6121 uint32_t field_index,
6122 uint32_t dex_pc)
6123 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006124 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006125 SetPackedField<FieldTypeField>(field_type);
David Brazdildee58d62016-04-07 09:54:26 +00006126 DCHECK_EQ(Primitive::PrimitiveKind(field_type), Primitive::PrimitiveKind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006127 SetRawInputAt(0, value);
6128 }
6129
6130 bool NeedsEnvironment() const OVERRIDE { return true; }
6131 bool CanThrow() const OVERRIDE { return true; }
6132
Vladimir Markoa1de9182016-02-25 11:37:38 +00006133 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006134 uint32_t GetFieldIndex() const { return field_index_; }
6135
6136 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
6137
6138 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006139 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6140 static constexpr size_t kFieldFieldTypeSize =
6141 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
6142 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6143 kFieldFieldType + kFieldFieldTypeSize;
6144 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6145 "Too many packed fields.");
6146 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
6147
Calin Juravlee460d1d2015-09-29 04:52:17 +01006148 const uint32_t field_index_;
6149
6150 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldSet);
6151};
6152
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006153// Implement the move-exception DEX instruction.
Vladimir Markofcb503c2016-05-18 12:48:17 +01006154class HLoadException FINAL : public HExpression<0> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006155 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006156 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
6157 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006158
David Brazdilbbd733e2015-08-18 17:48:17 +01006159 bool CanBeNull() const OVERRIDE { return false; }
6160
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006161 DECLARE_INSTRUCTION(LoadException);
6162
6163 private:
6164 DISALLOW_COPY_AND_ASSIGN(HLoadException);
6165};
6166
David Brazdilcb1c0552015-08-04 16:22:25 +01006167// Implicit part of move-exception which clears thread-local exception storage.
6168// Must not be removed because the runtime expects the TLS to get cleared.
Vladimir Markofcb503c2016-05-18 12:48:17 +01006169class HClearException FINAL : public HTemplateInstruction<0> {
David Brazdilcb1c0552015-08-04 16:22:25 +01006170 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006171 explicit HClearException(uint32_t dex_pc = kNoDexPc)
6172 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01006173
6174 DECLARE_INSTRUCTION(ClearException);
6175
6176 private:
6177 DISALLOW_COPY_AND_ASSIGN(HClearException);
6178};
6179
Vladimir Markofcb503c2016-05-18 12:48:17 +01006180class HThrow FINAL : public HTemplateInstruction<1> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006181 public:
6182 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006183 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006184 SetRawInputAt(0, exception);
6185 }
6186
6187 bool IsControlFlow() const OVERRIDE { return true; }
6188
6189 bool NeedsEnvironment() const OVERRIDE { return true; }
6190
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006191 bool CanThrow() const OVERRIDE { return true; }
6192
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006193
6194 DECLARE_INSTRUCTION(Throw);
6195
6196 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006197 DISALLOW_COPY_AND_ASSIGN(HThrow);
6198};
6199
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006200/**
6201 * Implementation strategies for the code generator of a HInstanceOf
6202 * or `HCheckCast`.
6203 */
6204enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01006205 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006206 kExactCheck, // Can do a single class compare.
6207 kClassHierarchyCheck, // Can just walk the super class chain.
6208 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
6209 kInterfaceCheck, // No optimization yet when checking against an interface.
6210 kArrayObjectCheck, // Can just check if the array is not primitive.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006211 kArrayCheck, // No optimization yet when checking against a generic array.
6212 kLast = kArrayCheck
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006213};
6214
Roland Levillain86503782016-02-11 19:07:30 +00006215std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs);
6216
Vladimir Markofcb503c2016-05-18 12:48:17 +01006217class HInstanceOf FINAL : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006218 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006219 HInstanceOf(HInstruction* object,
6220 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006221 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006222 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006223 : HExpression(Primitive::kPrimBoolean,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006224 SideEffectsForArchRuntimeCalls(check_kind),
Vladimir Markoa1de9182016-02-25 11:37:38 +00006225 dex_pc) {
6226 SetPackedField<TypeCheckKindField>(check_kind);
6227 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006228 SetRawInputAt(0, object);
6229 SetRawInputAt(1, constant);
6230 }
6231
6232 bool CanBeMoved() const OVERRIDE { return true; }
6233
Vladimir Marko372f10e2016-05-17 16:30:10 +01006234 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006235 return true;
6236 }
6237
6238 bool NeedsEnvironment() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006239 return CanCallRuntime(GetTypeCheckKind());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006240 }
6241
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006242 // Used only in code generation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006243 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6244 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6245 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6246 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006247
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006248 static bool CanCallRuntime(TypeCheckKind check_kind) {
6249 // Mips currently does runtime calls for any other checks.
6250 return check_kind != TypeCheckKind::kExactCheck;
6251 }
6252
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006253 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006254 return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006255 }
6256
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006257 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006258
6259 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006260 static constexpr size_t kFieldTypeCheckKind = kNumberOfExpressionPackedBits;
6261 static constexpr size_t kFieldTypeCheckKindSize =
6262 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6263 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6264 static constexpr size_t kNumberOfInstanceOfPackedBits = kFlagMustDoNullCheck + 1;
6265 static_assert(kNumberOfInstanceOfPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6266 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006267
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006268 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
6269};
6270
Vladimir Markofcb503c2016-05-18 12:48:17 +01006271class HBoundType FINAL : public HExpression<1> {
Calin Juravleb1498f62015-02-16 13:13:29 +00006272 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -07006273 explicit HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006274 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00006275 upper_bound_(ReferenceTypeInfo::CreateInvalid()) {
6276 SetPackedFlag<kFlagUpperCanBeNull>(true);
6277 SetPackedFlag<kFlagCanBeNull>(true);
Calin Juravle61d544b2015-02-23 16:46:57 +00006278 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00006279 SetRawInputAt(0, input);
6280 }
6281
David Brazdilf5552582015-12-27 13:36:12 +00006282 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006283 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006284 bool GetUpperCanBeNull() const { return GetPackedFlag<kFlagUpperCanBeNull>(); }
David Brazdilf5552582015-12-27 13:36:12 +00006285 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006286
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006287 void SetCanBeNull(bool can_be_null) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006288 DCHECK(GetUpperCanBeNull() || !can_be_null);
6289 SetPackedFlag<kFlagCanBeNull>(can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006290 }
6291
Vladimir Markoa1de9182016-02-25 11:37:38 +00006292 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006293
Calin Juravleb1498f62015-02-16 13:13:29 +00006294 DECLARE_INSTRUCTION(BoundType);
6295
6296 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006297 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
6298 // is false then CanBeNull() cannot be true).
6299 static constexpr size_t kFlagUpperCanBeNull = kNumberOfExpressionPackedBits;
6300 static constexpr size_t kFlagCanBeNull = kFlagUpperCanBeNull + 1;
6301 static constexpr size_t kNumberOfBoundTypePackedBits = kFlagCanBeNull + 1;
6302 static_assert(kNumberOfBoundTypePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6303
Calin Juravleb1498f62015-02-16 13:13:29 +00006304 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006305 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
6306 // It is used to bound the type in cases like:
6307 // if (x instanceof ClassX) {
6308 // // uper_bound_ will be ClassX
6309 // }
David Brazdilf5552582015-12-27 13:36:12 +00006310 ReferenceTypeInfo upper_bound_;
Calin Juravleb1498f62015-02-16 13:13:29 +00006311
6312 DISALLOW_COPY_AND_ASSIGN(HBoundType);
6313};
6314
Vladimir Markofcb503c2016-05-18 12:48:17 +01006315class HCheckCast FINAL : public HTemplateInstruction<2> {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006316 public:
6317 HCheckCast(HInstruction* object,
6318 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006319 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006320 uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00006321 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
6322 SetPackedField<TypeCheckKindField>(check_kind);
6323 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006324 SetRawInputAt(0, object);
6325 SetRawInputAt(1, constant);
6326 }
6327
6328 bool CanBeMoved() const OVERRIDE { return true; }
6329
Vladimir Marko372f10e2016-05-17 16:30:10 +01006330 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006331 return true;
6332 }
6333
6334 bool NeedsEnvironment() const OVERRIDE {
6335 // Instruction may throw a CheckCastError.
6336 return true;
6337 }
6338
6339 bool CanThrow() const OVERRIDE { return true; }
6340
Vladimir Markoa1de9182016-02-25 11:37:38 +00006341 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6342 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6343 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6344 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006345
6346 DECLARE_INSTRUCTION(CheckCast);
6347
6348 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006349 static constexpr size_t kFieldTypeCheckKind = kNumberOfGenericPackedBits;
6350 static constexpr size_t kFieldTypeCheckKindSize =
6351 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6352 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6353 static constexpr size_t kNumberOfCheckCastPackedBits = kFlagMustDoNullCheck + 1;
6354 static_assert(kNumberOfCheckCastPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6355 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006356
6357 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006358};
6359
Andreas Gampe26de38b2016-07-27 17:53:11 -07006360/**
6361 * @brief Memory barrier types (see "The JSR-133 Cookbook for Compiler Writers").
6362 * @details We define the combined barrier types that are actually required
6363 * by the Java Memory Model, rather than using exactly the terminology from
6364 * the JSR-133 cookbook. These should, in many cases, be replaced by acquire/release
6365 * primitives. Note that the JSR-133 cookbook generally does not deal with
6366 * store atomicity issues, and the recipes there are not always entirely sufficient.
6367 * The current recipe is as follows:
6368 * -# Use AnyStore ~= (LoadStore | StoreStore) ~= release barrier before volatile store.
6369 * -# Use AnyAny barrier after volatile store. (StoreLoad is as expensive.)
6370 * -# Use LoadAny barrier ~= (LoadLoad | LoadStore) ~= acquire barrier after each volatile load.
6371 * -# Use StoreStore barrier after all stores but before return from any constructor whose
6372 * class has final fields.
6373 * -# Use NTStoreStore to order non-temporal stores with respect to all later
6374 * store-to-memory instructions. Only generated together with non-temporal stores.
6375 */
6376enum MemBarrierKind {
6377 kAnyStore,
6378 kLoadAny,
6379 kStoreStore,
6380 kAnyAny,
6381 kNTStoreStore,
6382 kLastBarrierKind = kNTStoreStore
6383};
6384std::ostream& operator<<(std::ostream& os, const MemBarrierKind& kind);
6385
Vladimir Markofcb503c2016-05-18 12:48:17 +01006386class HMemoryBarrier FINAL : public HTemplateInstruction<0> {
Calin Juravle27df7582015-04-17 19:12:31 +01006387 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006388 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07006389 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006390 SideEffects::AllWritesAndReads(), dex_pc) { // Assume write/read on all fields/arrays.
6391 SetPackedField<BarrierKindField>(barrier_kind);
6392 }
Calin Juravle27df7582015-04-17 19:12:31 +01006393
Vladimir Markoa1de9182016-02-25 11:37:38 +00006394 MemBarrierKind GetBarrierKind() { return GetPackedField<BarrierKindField>(); }
Calin Juravle27df7582015-04-17 19:12:31 +01006395
6396 DECLARE_INSTRUCTION(MemoryBarrier);
6397
6398 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006399 static constexpr size_t kFieldBarrierKind = HInstruction::kNumberOfGenericPackedBits;
6400 static constexpr size_t kFieldBarrierKindSize =
6401 MinimumBitsToStore(static_cast<size_t>(kLastBarrierKind));
6402 static constexpr size_t kNumberOfMemoryBarrierPackedBits =
6403 kFieldBarrierKind + kFieldBarrierKindSize;
6404 static_assert(kNumberOfMemoryBarrierPackedBits <= kMaxNumberOfPackedBits,
6405 "Too many packed fields.");
6406 using BarrierKindField = BitField<MemBarrierKind, kFieldBarrierKind, kFieldBarrierKindSize>;
Calin Juravle27df7582015-04-17 19:12:31 +01006407
6408 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
6409};
6410
Vladimir Markofcb503c2016-05-18 12:48:17 +01006411class HMonitorOperation FINAL : public HTemplateInstruction<1> {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006412 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006413 enum class OperationKind {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006414 kEnter,
6415 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00006416 kLast = kExit
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006417 };
6418
6419 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006420 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006421 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
6422 dex_pc) {
6423 SetPackedField<OperationKindField>(kind);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006424 SetRawInputAt(0, object);
6425 }
6426
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006427 // Instruction may go into runtime, so we need an environment.
6428 bool NeedsEnvironment() const OVERRIDE { return true; }
David Brazdilbff75032015-07-08 17:26:51 +00006429
6430 bool CanThrow() const OVERRIDE {
6431 // Verifier guarantees that monitor-exit cannot throw.
6432 // This is important because it allows the HGraphBuilder to remove
6433 // a dead throw-catch loop generated for `synchronized` blocks/methods.
6434 return IsEnter();
6435 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006436
Vladimir Markoa1de9182016-02-25 11:37:38 +00006437 OperationKind GetOperationKind() const { return GetPackedField<OperationKindField>(); }
6438 bool IsEnter() const { return GetOperationKind() == OperationKind::kEnter; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006439
6440 DECLARE_INSTRUCTION(MonitorOperation);
6441
Calin Juravle52c48962014-12-16 17:02:57 +00006442 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006443 static constexpr size_t kFieldOperationKind = HInstruction::kNumberOfGenericPackedBits;
6444 static constexpr size_t kFieldOperationKindSize =
6445 MinimumBitsToStore(static_cast<size_t>(OperationKind::kLast));
6446 static constexpr size_t kNumberOfMonitorOperationPackedBits =
6447 kFieldOperationKind + kFieldOperationKindSize;
6448 static_assert(kNumberOfMonitorOperationPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6449 "Too many packed fields.");
6450 using OperationKindField = BitField<OperationKind, kFieldOperationKind, kFieldOperationKindSize>;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006451
6452 private:
6453 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
6454};
6455
Vladimir Markofcb503c2016-05-18 12:48:17 +01006456class HSelect FINAL : public HExpression<3> {
David Brazdil74eb1b22015-12-14 11:44:01 +00006457 public:
6458 HSelect(HInstruction* condition,
6459 HInstruction* true_value,
6460 HInstruction* false_value,
6461 uint32_t dex_pc)
6462 : HExpression(HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
6463 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
6464
6465 // First input must be `true_value` or `false_value` to allow codegens to
6466 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
6467 // that architectures which implement HSelect as a conditional move also
6468 // will not need to invert the condition.
6469 SetRawInputAt(0, false_value);
6470 SetRawInputAt(1, true_value);
6471 SetRawInputAt(2, condition);
6472 }
6473
6474 HInstruction* GetFalseValue() const { return InputAt(0); }
6475 HInstruction* GetTrueValue() const { return InputAt(1); }
6476 HInstruction* GetCondition() const { return InputAt(2); }
6477
6478 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01006479 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
6480 return true;
6481 }
David Brazdil74eb1b22015-12-14 11:44:01 +00006482
6483 bool CanBeNull() const OVERRIDE {
6484 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
6485 }
6486
6487 DECLARE_INSTRUCTION(Select);
6488
6489 private:
6490 DISALLOW_COPY_AND_ASSIGN(HSelect);
6491};
6492
Vladimir Markof9f64412015-09-02 14:05:49 +01006493class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006494 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01006495 MoveOperands(Location source,
6496 Location destination,
6497 Primitive::Type type,
6498 HInstruction* instruction)
6499 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006500
6501 Location GetSource() const { return source_; }
6502 Location GetDestination() const { return destination_; }
6503
6504 void SetSource(Location value) { source_ = value; }
6505 void SetDestination(Location value) { destination_ = value; }
6506
6507 // The parallel move resolver marks moves as "in-progress" by clearing the
6508 // destination (but not the source).
6509 Location MarkPending() {
6510 DCHECK(!IsPending());
6511 Location dest = destination_;
6512 destination_ = Location::NoLocation();
6513 return dest;
6514 }
6515
6516 void ClearPending(Location dest) {
6517 DCHECK(IsPending());
6518 destination_ = dest;
6519 }
6520
6521 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00006522 DCHECK(source_.IsValid() || destination_.IsInvalid());
6523 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006524 }
6525
6526 // True if this blocks a move from the given location.
6527 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08006528 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006529 }
6530
6531 // A move is redundant if it's been eliminated, if its source and
6532 // destination are the same, or if its destination is unneeded.
6533 bool IsRedundant() const {
6534 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
6535 }
6536
6537 // We clear both operands to indicate move that's been eliminated.
6538 void Eliminate() {
6539 source_ = destination_ = Location::NoLocation();
6540 }
6541
6542 bool IsEliminated() const {
6543 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
6544 return source_.IsInvalid();
6545 }
6546
Alexey Frunze4dda3372015-06-01 18:31:49 -07006547 Primitive::Type GetType() const { return type_; }
6548
Nicolas Geoffray90218252015-04-15 11:56:51 +01006549 bool Is64BitMove() const {
6550 return Primitive::Is64BitType(type_);
6551 }
6552
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006553 HInstruction* GetInstruction() const { return instruction_; }
6554
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006555 private:
6556 Location source_;
6557 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01006558 // The type this move is for.
6559 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006560 // The instruction this move is assocatied with. Null when this move is
6561 // for moving an input in the expected locations of user (including a phi user).
6562 // This is only used in debug mode, to ensure we do not connect interval siblings
6563 // in the same parallel move.
6564 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006565};
6566
Roland Levillainc9285912015-12-18 10:38:42 +00006567std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
6568
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006569static constexpr size_t kDefaultNumberOfMoves = 4;
6570
Vladimir Markofcb503c2016-05-18 12:48:17 +01006571class HParallelMove FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006572 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006573 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
Vladimir Marko225b6462015-09-28 12:17:40 +01006574 : HTemplateInstruction(SideEffects::None(), dex_pc),
6575 moves_(arena->Adapter(kArenaAllocMoveOperands)) {
6576 moves_.reserve(kDefaultNumberOfMoves);
6577 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006578
Nicolas Geoffray90218252015-04-15 11:56:51 +01006579 void AddMove(Location source,
6580 Location destination,
6581 Primitive::Type type,
6582 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00006583 DCHECK(source.IsValid());
6584 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006585 if (kIsDebugBuild) {
6586 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006587 for (const MoveOperands& move : moves_) {
6588 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006589 // Special case the situation where the move is for the spill slot
6590 // of the instruction.
6591 if ((GetPrevious() == instruction)
6592 || ((GetPrevious() == nullptr)
6593 && instruction->IsPhi()
6594 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006595 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006596 << "Doing parallel moves for the same instruction.";
6597 } else {
6598 DCHECK(false) << "Doing parallel moves for the same instruction.";
6599 }
6600 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00006601 }
6602 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006603 for (const MoveOperands& move : moves_) {
6604 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006605 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01006606 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006607 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006608 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006609 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006610 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006611 }
6612
Vladimir Marko225b6462015-09-28 12:17:40 +01006613 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006614 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006615 }
6616
Vladimir Marko225b6462015-09-28 12:17:40 +01006617 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006618
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01006619 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006620
6621 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01006622 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006623
6624 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
6625};
6626
Mark Mendell0616ae02015-04-17 12:49:27 -04006627} // namespace art
6628
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03006629#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
6630#include "nodes_shared.h"
6631#endif
Vladimir Markob4536b72015-11-24 13:45:23 +00006632#ifdef ART_ENABLE_CODEGEN_arm
6633#include "nodes_arm.h"
6634#endif
Alexandre Ramese6dbf482015-10-19 10:10:41 +01006635#ifdef ART_ENABLE_CODEGEN_arm64
6636#include "nodes_arm64.h"
6637#endif
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006638#ifdef ART_ENABLE_CODEGEN_mips
6639#include "nodes_mips.h"
6640#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04006641#ifdef ART_ENABLE_CODEGEN_x86
6642#include "nodes_x86.h"
6643#endif
6644
6645namespace art {
6646
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006647class HGraphVisitor : public ValueObject {
6648 public:
Dave Allison20dfc792014-06-16 20:44:29 -07006649 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
6650 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006651
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006652 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006653 virtual void VisitBasicBlock(HBasicBlock* block);
6654
Roland Levillain633021e2014-10-01 14:12:25 +01006655 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006656 void VisitInsertionOrder();
6657
Roland Levillain633021e2014-10-01 14:12:25 +01006658 // Visit the graph following dominator tree reverse post-order.
6659 void VisitReversePostOrder();
6660
Nicolas Geoffray787c3072014-03-17 10:20:19 +00006661 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00006662
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006663 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006664#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006665 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
6666
6667 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6668
6669#undef DECLARE_VISIT_INSTRUCTION
6670
6671 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07006672 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006673
6674 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
6675};
6676
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006677class HGraphDelegateVisitor : public HGraphVisitor {
6678 public:
6679 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
6680 virtual ~HGraphDelegateVisitor() {}
6681
6682 // Visit functions that delegate to to super class.
6683#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00006684 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006685
6686 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6687
6688#undef DECLARE_VISIT_INSTRUCTION
6689
6690 private:
6691 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
6692};
6693
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006694// Iterator over the blocks that art part of the loop. Includes blocks part
6695// of an inner loop. The order in which the blocks are iterated is on their
6696// block id.
6697class HBlocksInLoopIterator : public ValueObject {
6698 public:
6699 explicit HBlocksInLoopIterator(const HLoopInformation& info)
6700 : blocks_in_loop_(info.GetBlocks()),
6701 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
6702 index_(0) {
6703 if (!blocks_in_loop_.IsBitSet(index_)) {
6704 Advance();
6705 }
6706 }
6707
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006708 bool Done() const { return index_ == blocks_.size(); }
6709 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006710 void Advance() {
6711 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006712 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006713 if (blocks_in_loop_.IsBitSet(index_)) {
6714 break;
6715 }
6716 }
6717 }
6718
6719 private:
6720 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006721 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006722 size_t index_;
6723
6724 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
6725};
6726
Mingyao Yang3584bce2015-05-19 16:01:59 -07006727// Iterator over the blocks that art part of the loop. Includes blocks part
6728// of an inner loop. The order in which the blocks are iterated is reverse
6729// post order.
6730class HBlocksInLoopReversePostOrderIterator : public ValueObject {
6731 public:
6732 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
6733 : blocks_in_loop_(info.GetBlocks()),
6734 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
6735 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006736 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006737 Advance();
6738 }
6739 }
6740
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006741 bool Done() const { return index_ == blocks_.size(); }
6742 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07006743 void Advance() {
6744 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006745 for (size_t e = blocks_.size(); index_ < e; ++index_) {
6746 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006747 break;
6748 }
6749 }
6750 }
6751
6752 private:
6753 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006754 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07006755 size_t index_;
6756
6757 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
6758};
6759
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006760inline int64_t Int64FromConstant(HConstant* constant) {
David Brazdilc0b601b2016-02-08 14:20:45 +00006761 if (constant->IsIntConstant()) {
6762 return constant->AsIntConstant()->GetValue();
6763 } else if (constant->IsLongConstant()) {
6764 return constant->AsLongConstant()->GetValue();
6765 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00006766 DCHECK(constant->IsNullConstant()) << constant->DebugName();
David Brazdilc0b601b2016-02-08 14:20:45 +00006767 return 0;
6768 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006769}
6770
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006771#define INSTRUCTION_TYPE_CHECK(type, super) \
6772 inline bool HInstruction::Is##type() const { return GetKind() == k##type; } \
6773 inline const H##type* HInstruction::As##type() const { \
6774 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
6775 } \
6776 inline H##type* HInstruction::As##type() { \
6777 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
6778 }
6779
6780 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
6781#undef INSTRUCTION_TYPE_CHECK
6782
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00006783// Create space in `blocks` for adding `number_of_new_blocks` entries
6784// starting at location `at`. Blocks after `at` are moved accordingly.
6785inline void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks,
6786 size_t number_of_new_blocks,
6787 size_t after) {
6788 DCHECK_LT(after, blocks->size());
6789 size_t old_size = blocks->size();
6790 size_t new_size = old_size + number_of_new_blocks;
6791 blocks->resize(new_size);
6792 std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end());
6793}
6794
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006795} // namespace art
6796
6797#endif // ART_COMPILER_OPTIMIZING_NODES_H_