blob: 67e02f236db649b11523af95454b82ff638b09ff [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 Marko3fae1292019-06-07 11:26:25 +010028#include "base/intrusive_forward_list.h"
Vladimir Marko2c45bc92016-10-25 16:54:12 +010029#include "base/iteration_range.h"
Andreas Gampe7fbc4a52018-11-28 08:26:47 -080030#include "base/mutex.h"
David Sehrc431b9d2018-03-02 12:01:51 -080031#include "base/quasi_atomic.h"
Vladimir Marko60584552015-09-03 13:35:12 +000032#include "base/stl_util.h"
David Brazdild9c90372016-09-14 16:53:55 +010033#include "base/transform_array_ref.h"
Nicolas Geoffray76d4bb0f32018-09-21 12:58:45 +010034#include "art_method.h"
Vladimir Marko02ca05a2020-05-12 13:58:51 +010035#include "class_root.h"
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010036#include "data_type.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070037#include "deoptimization_kind.h"
David Sehr9e734c72018-01-04 17:56:19 -080038#include "dex/dex_file.h"
39#include "dex/dex_file_types.h"
David Sehr8c0961f2018-01-23 16:11:38 -080040#include "dex/invoke_type.h"
David Sehr312f3b22018-03-19 08:39:26 -070041#include "dex/method_reference.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000042#include "entrypoints/quick/quick_entrypoints_enum.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000043#include "handle.h"
44#include "handle_scope.h"
Nicolas Geoffray762869d2016-07-15 15:28:35 +010045#include "intrinsics_enum.h"
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010046#include "locations.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000047#include "mirror/class.h"
Orion Hodson18259d72018-04-12 11:18:23 +010048#include "mirror/method_type.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010049#include "offsets.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000050
Vladimir Marko0a516052019-10-14 13:00:44 +000051namespace art {
Nicolas Geoffray818f2102014-02-18 16:43:35 +000052
Vladimir Markoca6fff82017-10-03 14:49:14 +010053class ArenaStack;
David Brazdil1abb4192015-02-17 18:33:36 +000054class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000055class HBasicBlock;
Igor Murashkind01745e2017-04-05 16:40:31 -070056class HConstructorFence;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010057class HCurrentMethod;
David Brazdil8d5b8b22015-03-24 10:51:52 +000058class HDoubleConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010059class HEnvironment;
David Brazdil8d5b8b22015-03-24 10:51:52 +000060class HFloatConstant;
David Brazdilfc6a86a2015-06-26 10:33:45 +000061class HGraphBuilder;
David Brazdil8d5b8b22015-03-24 10:51:52 +000062class HGraphVisitor;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000063class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000064class HIntConstant;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000065class HInvoke;
David Brazdil8d5b8b22015-03-24 10:51:52 +000066class HLongConstant;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +000067class HNullConstant;
Igor Murashkind01745e2017-04-05 16:40:31 -070068class HParameterValue;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010069class HPhi;
Nicolas Geoffray3c049742014-09-24 18:10:46 +010070class HSuspendCheck;
David Brazdilffee3d32015-07-06 11:48:53 +010071class HTryBoundary;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010072class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000073class LocationSummary;
Nicolas Geoffraydb216f42015-05-05 17:02:20 +010074class SlowPathCode;
David Brazdil8d5b8b22015-03-24 10:51:52 +000075class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000076
Mathieu Chartier736b5602015-09-02 14:54:11 -070077namespace mirror {
78class DexCache;
79} // namespace mirror
80
Nicolas Geoffray818f2102014-02-18 16:43:35 +000081static const int kDefaultNumberOfBlocks = 8;
82static const int kDefaultNumberOfSuccessors = 2;
83static const int kDefaultNumberOfPredecessors = 2;
David Brazdilb618ade2015-07-29 10:31:29 +010084static const int kDefaultNumberOfExceptionalPredecessors = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010085static const int kDefaultNumberOfDominatedBlocks = 1;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000086static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000087
Roland Levillain5b5b9312016-03-22 14:57:31 +000088// The maximum (meaningful) distance (31) that can be used in an integer shift/rotate operation.
89static constexpr int32_t kMaxIntShiftDistance = 0x1f;
90// The maximum (meaningful) distance (63) that can be used in a long shift/rotate operation.
91static constexpr int32_t kMaxLongShiftDistance = 0x3f;
Calin Juravle9aec02f2014-11-18 23:06:35 +000092
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010093static constexpr uint32_t kUnknownFieldIndex = static_cast<uint32_t>(-1);
Mingyao Yang8df69d42015-10-22 15:40:58 -070094static constexpr uint16_t kUnknownClassDefIndex = static_cast<uint16_t>(-1);
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010095
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +010096static constexpr InvokeType kInvalidInvokeType = static_cast<InvokeType>(-1);
97
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +060098static constexpr uint32_t kNoDexPc = -1;
99
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100100inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
101 // For the purposes of the compiler, the dex files must actually be the same object
102 // if we want to safely treat them as the same. This is especially important for JIT
103 // as custom class loaders can open the same underlying file (or memory) multiple
104 // times and provide different class resolution but no two class loaders should ever
105 // use the same DexFile object - doing so is an unsupported hack that can lead to
106 // all sorts of weird failures.
107 return &lhs == &rhs;
108}
109
Dave Allison20dfc792014-06-16 20:44:29 -0700110enum IfCondition {
Aart Bike9f37602015-10-09 11:15:55 -0700111 // All types.
112 kCondEQ, // ==
113 kCondNE, // !=
114 // Signed integers and floating-point numbers.
115 kCondLT, // <
116 kCondLE, // <=
117 kCondGT, // >
118 kCondGE, // >=
119 // Unsigned integers.
120 kCondB, // <
121 kCondBE, // <=
122 kCondA, // >
123 kCondAE, // >=
Scott Wakeling2c76e062016-08-31 09:48:54 +0100124 // First and last aliases.
125 kCondFirst = kCondEQ,
126 kCondLast = kCondAE,
Dave Allison20dfc792014-06-16 20:44:29 -0700127};
128
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000129enum GraphAnalysisResult {
David Brazdil86ea7ee2016-02-16 09:26:07 +0000130 kAnalysisSkipped,
David Brazdilbadd8262016-02-02 16:28:56 +0000131 kAnalysisInvalidBytecode,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000132 kAnalysisFailThrowCatchLoop,
133 kAnalysisFailAmbiguousArrayOp,
Nicolas Geoffray0846a8f2018-09-12 15:21:07 +0100134 kAnalysisFailIrreducibleLoopAndStringInit,
Nicolas Geoffray7cfc8f52019-08-07 10:41:09 +0100135 kAnalysisFailPhiEquivalentInOsr,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000136 kAnalysisSuccess,
David Brazdil4833f5a2015-12-16 10:37:39 +0000137};
138
Andreas Gampe9186ced2016-12-12 14:28:21 -0800139template <typename T>
140static inline typename std::make_unsigned<T>::type MakeUnsigned(T x) {
141 return static_cast<typename std::make_unsigned<T>::type>(x);
142}
143
Vladimir Markof9f64412015-09-02 14:05:49 +0100144class HInstructionList : public ValueObject {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100145 public:
146 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
147
148 void AddInstruction(HInstruction* instruction);
149 void RemoveInstruction(HInstruction* instruction);
150
David Brazdilc3d743f2015-04-22 13:40:50 +0100151 // Insert `instruction` before/after an existing instruction `cursor`.
152 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
153 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
154
Roland Levillain6b469232014-09-25 10:10:38 +0100155 // Return true if this list contains `instruction`.
156 bool Contains(HInstruction* instruction) const;
157
Roland Levillainccc07a92014-09-16 14:48:16 +0100158 // Return true if `instruction1` is found before `instruction2` in
159 // this instruction list and false otherwise. Abort if none
160 // of these instructions is found.
161 bool FoundBefore(const HInstruction* instruction1,
162 const HInstruction* instruction2) const;
163
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000164 bool IsEmpty() const { return first_instruction_ == nullptr; }
165 void Clear() { first_instruction_ = last_instruction_ = nullptr; }
166
167 // Update the block of all instructions to be `block`.
168 void SetBlockOfInstructions(HBasicBlock* block) const;
169
170 void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list);
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000171 void AddBefore(HInstruction* cursor, const HInstructionList& instruction_list);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000172 void Add(const HInstructionList& instruction_list);
173
David Brazdil2d7352b2015-04-20 14:52:42 +0100174 // Return the number of instructions in the list. This is an expensive operation.
175 size_t CountSize() const;
176
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100177 private:
178 HInstruction* first_instruction_;
179 HInstruction* last_instruction_;
180
181 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000182 friend class HGraph;
183 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100184 friend class HInstructionIterator;
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +0000185 friend class HInstructionIteratorHandleChanges;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100186 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100187
188 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
189};
190
David Brazdil4833f5a2015-12-16 10:37:39 +0000191class ReferenceTypeInfo : ValueObject {
192 public:
193 typedef Handle<mirror::Class> TypeHandle;
194
Vladimir Markoa1de9182016-02-25 11:37:38 +0000195 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact);
196
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700197 static ReferenceTypeInfo Create(TypeHandle type_handle) REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil94ab38f2016-06-21 17:48:19 +0100198 return Create(type_handle, type_handle->CannotBeAssignedFromOtherTypes());
199 }
200
Vladimir Markoa1de9182016-02-25 11:37:38 +0000201 static ReferenceTypeInfo CreateUnchecked(TypeHandle type_handle, bool is_exact) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000202 return ReferenceTypeInfo(type_handle, is_exact);
203 }
204
205 static ReferenceTypeInfo CreateInvalid() { return ReferenceTypeInfo(); }
206
Vladimir Markof39745e2016-01-26 12:16:55 +0000207 static bool IsValidHandle(TypeHandle handle) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000208 return handle.GetReference() != nullptr;
209 }
210
Vladimir Marko456307a2016-04-19 14:12:13 +0000211 bool IsValid() const {
David Brazdil4833f5a2015-12-16 10:37:39 +0000212 return IsValidHandle(type_handle_);
213 }
214
215 bool IsExact() const { return is_exact_; }
216
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700217 bool IsObjectClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000218 DCHECK(IsValid());
219 return GetTypeHandle()->IsObjectClass();
220 }
221
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700222 bool IsStringClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000223 DCHECK(IsValid());
224 return GetTypeHandle()->IsStringClass();
225 }
226
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700227 bool IsObjectArray() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000228 DCHECK(IsValid());
229 return IsArrayClass() && GetTypeHandle()->GetComponentType()->IsObjectClass();
230 }
231
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700232 bool IsInterface() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000233 DCHECK(IsValid());
234 return GetTypeHandle()->IsInterface();
235 }
236
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700237 bool IsArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000238 DCHECK(IsValid());
239 return GetTypeHandle()->IsArrayClass();
240 }
241
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700242 bool IsPrimitiveArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000243 DCHECK(IsValid());
244 return GetTypeHandle()->IsPrimitiveArray();
245 }
246
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700247 bool IsNonPrimitiveArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000248 DCHECK(IsValid());
249 return GetTypeHandle()->IsArrayClass() && !GetTypeHandle()->IsPrimitiveArray();
250 }
251
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700252 bool CanArrayHold(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000253 DCHECK(IsValid());
254 if (!IsExact()) return false;
255 if (!IsArrayClass()) return false;
256 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(rti.GetTypeHandle().Get());
257 }
258
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700259 bool CanArrayHoldValuesOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000260 DCHECK(IsValid());
261 if (!IsExact()) return false;
262 if (!IsArrayClass()) return false;
263 if (!rti.IsArrayClass()) return false;
264 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(
265 rti.GetTypeHandle()->GetComponentType());
266 }
267
268 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
269
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700270 bool IsSupertypeOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000271 DCHECK(IsValid());
272 DCHECK(rti.IsValid());
273 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
274 }
275
David Brazdil4833f5a2015-12-16 10:37:39 +0000276 // Returns true if the type information provide the same amount of details.
277 // Note that it does not mean that the instructions have the same actual type
278 // (because the type can be the result of a merge).
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700279 bool IsEqual(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000280 if (!IsValid() && !rti.IsValid()) {
281 // Invalid types are equal.
282 return true;
283 }
284 if (!IsValid() || !rti.IsValid()) {
285 // One is valid, the other not.
286 return false;
287 }
288 return IsExact() == rti.IsExact()
289 && GetTypeHandle().Get() == rti.GetTypeHandle().Get();
290 }
291
292 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +0000293 ReferenceTypeInfo() : type_handle_(TypeHandle()), is_exact_(false) {}
294 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact)
295 : type_handle_(type_handle), is_exact_(is_exact) { }
David Brazdil4833f5a2015-12-16 10:37:39 +0000296
297 // The class of the object.
298 TypeHandle type_handle_;
299 // Whether or not the type is exact or a superclass of the actual type.
300 // Whether or not we have any information about this type.
301 bool is_exact_;
302};
303
304std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
305
Vladimir Marko02ca05a2020-05-12 13:58:51 +0100306class HandleCache {
307 public:
308 explicit HandleCache(VariableSizedHandleScope* handles) : handles_(handles) { }
309
310 VariableSizedHandleScope* GetHandles() { return handles_; }
311
312 template <typename T>
313 MutableHandle<T> NewHandle(T* object) REQUIRES_SHARED(Locks::mutator_lock_) {
314 return handles_->NewHandle(object);
315 }
316
317 template <typename T>
318 MutableHandle<T> NewHandle(ObjPtr<T> object) REQUIRES_SHARED(Locks::mutator_lock_) {
319 return handles_->NewHandle(object);
320 }
321
322 ReferenceTypeInfo::TypeHandle GetObjectClassHandle() {
323 return GetRootHandle(ClassRoot::kJavaLangObject, &object_class_handle_);
324 }
325
326 ReferenceTypeInfo::TypeHandle GetClassClassHandle() {
327 return GetRootHandle(ClassRoot::kJavaLangClass, &class_class_handle_);
328 }
329
330 ReferenceTypeInfo::TypeHandle GetMethodHandleClassHandle() {
331 return GetRootHandle(ClassRoot::kJavaLangInvokeMethodHandleImpl, &method_handle_class_handle_);
332 }
333
334 ReferenceTypeInfo::TypeHandle GetMethodTypeClassHandle() {
335 return GetRootHandle(ClassRoot::kJavaLangInvokeMethodType, &method_type_class_handle_);
336 }
337
338 ReferenceTypeInfo::TypeHandle GetStringClassHandle() {
339 return GetRootHandle(ClassRoot::kJavaLangString, &string_class_handle_);
340 }
341
342 ReferenceTypeInfo::TypeHandle GetThrowableClassHandle() {
343 return GetRootHandle(ClassRoot::kJavaLangThrowable, &throwable_class_handle_);
344 }
345
346
347 private:
348 inline ReferenceTypeInfo::TypeHandle GetRootHandle(ClassRoot class_root,
349 ReferenceTypeInfo::TypeHandle* cache) {
350 if (UNLIKELY(!ReferenceTypeInfo::IsValidHandle(*cache))) {
351 *cache = CreateRootHandle(handles_, class_root);
352 }
353 return *cache;
354 }
355
356 static ReferenceTypeInfo::TypeHandle CreateRootHandle(VariableSizedHandleScope* handles,
357 ClassRoot class_root);
358
359 VariableSizedHandleScope* handles_;
360
361 ReferenceTypeInfo::TypeHandle object_class_handle_;
362 ReferenceTypeInfo::TypeHandle class_class_handle_;
363 ReferenceTypeInfo::TypeHandle method_handle_class_handle_;
364 ReferenceTypeInfo::TypeHandle method_type_class_handle_;
365 ReferenceTypeInfo::TypeHandle string_class_handle_;
366 ReferenceTypeInfo::TypeHandle throwable_class_handle_;
367};
368
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000369// Control-flow graph of a method. Contains a list of basic blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100370class HGraph : public ArenaObject<kArenaAllocGraph> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000371 public:
Vladimir Markoca6fff82017-10-03 14:49:14 +0100372 HGraph(ArenaAllocator* allocator,
373 ArenaStack* arena_stack,
Vladimir Marko02ca05a2020-05-12 13:58:51 +0100374 VariableSizedHandleScope* handles,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100375 const DexFile& dex_file,
376 uint32_t method_idx,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700377 InstructionSet instruction_set,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100378 InvokeType invoke_type = kInvalidInvokeType,
Hans Boehm206348c2018-12-05 11:11:33 -0800379 bool dead_reference_safe = false,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100380 bool debuggable = false,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000381 bool osr = false,
Nicolas Geoffraya48c3df2019-06-27 13:11:12 +0000382 bool is_shared_jit_code = false,
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +0000383 bool baseline = false,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100384 int start_instruction_id = 0)
Vladimir Markoca6fff82017-10-03 14:49:14 +0100385 : allocator_(allocator),
386 arena_stack_(arena_stack),
Vladimir Marko02ca05a2020-05-12 13:58:51 +0100387 handle_cache_(handles),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100388 blocks_(allocator->Adapter(kArenaAllocBlockList)),
389 reverse_post_order_(allocator->Adapter(kArenaAllocReversePostOrder)),
390 linear_order_(allocator->Adapter(kArenaAllocLinearOrder)),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700391 entry_block_(nullptr),
392 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100393 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100394 number_of_vregs_(0),
395 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000396 temporaries_vreg_slots_(0),
Mark Mendell1152c922015-04-24 17:06:35 -0400397 has_bounds_checks_(false),
David Brazdil77a48ae2015-09-15 12:34:04 +0000398 has_try_catch_(false),
Artem Serov2808be82018-12-20 19:15:11 +0000399 has_monitor_operations_(false),
Aart Bikb13c65b2017-03-21 20:14:07 -0700400 has_simd_(false),
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800401 has_loops_(false),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000402 has_irreducible_loops_(false),
Hans Boehm206348c2018-12-05 11:11:33 -0800403 dead_reference_safe_(dead_reference_safe),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000404 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000405 current_instruction_id_(start_instruction_id),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100406 dex_file_(dex_file),
407 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100408 invoke_type_(invoke_type),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100409 in_ssa_form_(false),
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800410 number_of_cha_guards_(0),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700411 instruction_set_(instruction_set),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000412 cached_null_constant_(nullptr),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100413 cached_int_constants_(std::less<int32_t>(), allocator->Adapter(kArenaAllocConstantsMap)),
414 cached_float_constants_(std::less<int32_t>(), allocator->Adapter(kArenaAllocConstantsMap)),
415 cached_long_constants_(std::less<int64_t>(), allocator->Adapter(kArenaAllocConstantsMap)),
416 cached_double_constants_(std::less<int64_t>(), allocator->Adapter(kArenaAllocConstantsMap)),
David Brazdil4833f5a2015-12-16 10:37:39 +0000417 cached_current_method_(nullptr),
Nicolas Geoffray53fec082017-03-27 12:56:16 +0100418 art_method_(nullptr),
Mingyao Yang063fc772016-08-02 11:02:54 -0700419 osr_(osr),
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +0000420 baseline_(baseline),
Nicolas Geoffraya48c3df2019-06-27 13:11:12 +0000421 cha_single_implementation_list_(allocator->Adapter(kArenaAllocCHA)),
422 is_shared_jit_code_(is_shared_jit_code) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100423 blocks_.reserve(kDefaultNumberOfBlocks);
424 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000425
Vladimir Markoca6fff82017-10-03 14:49:14 +0100426 ArenaAllocator* GetAllocator() const { return allocator_; }
427 ArenaStack* GetArenaStack() const { return arena_stack_; }
Vladimir Marko02ca05a2020-05-12 13:58:51 +0100428
429 HandleCache* GetHandleCache() { return &handle_cache_; }
430
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100431 const ArenaVector<HBasicBlock*>& GetBlocks() const { return blocks_; }
432
David Brazdil69ba7b72015-06-23 18:27:30 +0100433 bool IsInSsaForm() const { return in_ssa_form_; }
David Brazdilbadd8262016-02-02 16:28:56 +0000434 void SetInSsaForm() { in_ssa_form_ = true; }
David Brazdil69ba7b72015-06-23 18:27:30 +0100435
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000436 HBasicBlock* GetEntryBlock() const { return entry_block_; }
437 HBasicBlock* GetExitBlock() const { return exit_block_; }
David Brazdilc7af85d2015-05-26 12:05:55 +0100438 bool HasExitBlock() const { return exit_block_ != nullptr; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000439
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000440 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
441 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000442
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000443 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100444
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100445 void ComputeDominanceInformation();
446 void ClearDominanceInformation();
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000447 void ClearLoopInformation();
448 void FindBackEdges(ArenaBitVector* visited);
449 GraphAnalysisResult BuildDominatorTree();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100450 void SimplifyCFG();
David Brazdilffee3d32015-07-06 11:48:53 +0100451 void SimplifyCatchBlocks();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000452
David Brazdil4833f5a2015-12-16 10:37:39 +0000453 // Analyze all natural loops in this graph. Returns a code specifying that it
454 // was successful or the reason for failure. The method will fail if a loop
David Brazdil4833f5a2015-12-16 10:37:39 +0000455 // is a throw-catch loop, i.e. the header is a catch block.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000456 GraphAnalysisResult AnalyzeLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100457
David Brazdilffee3d32015-07-06 11:48:53 +0100458 // Iterate over blocks to compute try block membership. Needs reverse post
459 // order and loop information.
460 void ComputeTryBlockInformation();
461
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000462 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
Nicolas Geoffray55bd7492016-02-16 15:37:12 +0000463 // Returns the instruction to replace the invoke expression or null if the
464 // invoke is for a void method. Note that the caller is responsible for replacing
465 // and removing the invoke instruction.
Calin Juravle2e768302015-07-28 14:41:11 +0000466 HInstruction* InlineInto(HGraph* outer_graph, HInvoke* invoke);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000467
Nicolas Geoffraya1d8ddf2016-02-29 11:46:58 +0000468 // Update the loop and try membership of `block`, which was spawned from `reference`.
469 // In case `reference` is a back edge, `replace_if_back_edge` notifies whether `block`
470 // should be the new back edge.
471 void UpdateLoopAndTryInformationOfNewBlock(HBasicBlock* block,
472 HBasicBlock* reference,
473 bool replace_if_back_edge);
474
Mingyao Yang3584bce2015-05-19 16:01:59 -0700475 // Need to add a couple of blocks to test if the loop body is entered and
476 // put deoptimization instructions, etc.
477 void TransformLoopHeaderForBCE(HBasicBlock* header);
478
Aart Bikf8f5a162017-02-06 15:35:29 -0800479 // Adds a new loop directly after the loop with the given header and exit.
480 // Returns the new preheader.
481 HBasicBlock* TransformLoopForVectorization(HBasicBlock* header,
482 HBasicBlock* body,
483 HBasicBlock* exit);
484
David Brazdil8a7c0fe2015-11-02 20:24:55 +0000485 // Removes `block` from the graph. Assumes `block` has been disconnected from
486 // other blocks and has no instructions or phis.
487 void DeleteDeadEmptyBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000488
David Brazdilfc6a86a2015-06-26 10:33:45 +0000489 // Splits the edge between `block` and `successor` while preserving the
490 // indices in the predecessor/successor lists. If there are multiple edges
491 // between the blocks, the lowest indices are used.
492 // Returns the new block which is empty and has the same dex pc as `successor`.
493 HBasicBlock* SplitEdge(HBasicBlock* block, HBasicBlock* successor);
494
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100495 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
Artem Serovc73ee372017-07-31 15:08:40 +0100496 void OrderLoopHeaderPredecessors(HBasicBlock* header);
Artem Serov09faaea2017-12-07 14:36:01 +0000497
498 // Transform a loop into a format with a single preheader.
499 //
500 // Each phi in the header should be split: original one in the header should only hold
501 // inputs reachable from the back edges and a single input from the preheader. The newly created
502 // phi in the preheader should collate the inputs from the original multiple incoming blocks.
503 //
504 // Loops in the graph typically have a single preheader, so this method is used to "repair" loops
505 // that no longer have this property.
506 void TransformLoopToSinglePreheaderFormat(HBasicBlock* header);
507
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100508 void SimplifyLoop(HBasicBlock* header);
509
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000510 int32_t GetNextInstructionId() {
Nicolas Geoffrayc9c31042017-06-29 14:04:16 +0100511 CHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000512 return current_instruction_id_++;
513 }
514
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000515 int32_t GetCurrentInstructionId() const {
516 return current_instruction_id_;
517 }
518
519 void SetCurrentInstructionId(int32_t id) {
Nicolas Geoffrayc9c31042017-06-29 14:04:16 +0100520 CHECK_GE(id, current_instruction_id_);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000521 current_instruction_id_ = id;
522 }
523
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100524 uint16_t GetMaximumNumberOfOutVRegs() const {
525 return maximum_number_of_out_vregs_;
526 }
527
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000528 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
529 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100530 }
531
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100532 void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) {
533 maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value);
534 }
535
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000536 void UpdateTemporariesVRegSlots(size_t slots) {
537 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100538 }
539
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000540 size_t GetTemporariesVRegSlots() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100541 DCHECK(!in_ssa_form_);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000542 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100543 }
544
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100545 void SetNumberOfVRegs(uint16_t number_of_vregs) {
546 number_of_vregs_ = number_of_vregs;
547 }
548
549 uint16_t GetNumberOfVRegs() const {
550 return number_of_vregs_;
551 }
552
553 void SetNumberOfInVRegs(uint16_t value) {
554 number_of_in_vregs_ = value;
555 }
556
David Brazdildee58d62016-04-07 09:54:26 +0000557 uint16_t GetNumberOfInVRegs() const {
558 return number_of_in_vregs_;
559 }
560
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100561 uint16_t GetNumberOfLocalVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100562 DCHECK(!in_ssa_form_);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100563 return number_of_vregs_ - number_of_in_vregs_;
564 }
565
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100566 const ArenaVector<HBasicBlock*>& GetReversePostOrder() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100567 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100568 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100569
Eric Holk1868de92020-02-12 09:10:21 -0800570 ArrayRef<HBasicBlock* const> GetReversePostOrderSkipEntryBlock() const {
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100571 DCHECK(GetReversePostOrder()[0] == entry_block_);
572 return ArrayRef<HBasicBlock* const>(GetReversePostOrder()).SubArray(1);
573 }
574
575 IterationRange<ArenaVector<HBasicBlock*>::const_reverse_iterator> GetPostOrder() const {
576 return ReverseRange(GetReversePostOrder());
577 }
578
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100579 const ArenaVector<HBasicBlock*>& GetLinearOrder() const {
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100580 return linear_order_;
581 }
582
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100583 IterationRange<ArenaVector<HBasicBlock*>::const_reverse_iterator> GetLinearPostOrder() const {
584 return ReverseRange(GetLinearOrder());
585 }
586
Mark Mendell1152c922015-04-24 17:06:35 -0400587 bool HasBoundsChecks() const {
588 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800589 }
590
Mark Mendell1152c922015-04-24 17:06:35 -0400591 void SetHasBoundsChecks(bool value) {
592 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800593 }
594
Hans Boehm206348c2018-12-05 11:11:33 -0800595 // Is the code known to be robust against eliminating dead references
596 // and the effects of early finalization?
597 bool IsDeadReferenceSafe() const { return dead_reference_safe_; }
598
599 void MarkDeadReferenceUnsafe() { dead_reference_safe_ = false; }
600
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000601 bool IsDebuggable() const { return debuggable_; }
602
David Brazdil8d5b8b22015-03-24 10:51:52 +0000603 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000604 // already, it is created and inserted into the graph. This method is only for
605 // integral types.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100606 HConstant* GetConstant(DataType::Type type, int64_t value, uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000607
608 // TODO: This is problematic for the consistency of reference type propagation
609 // because it can be created anytime after the pass and thus it will be left
610 // with an invalid type.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600611 HNullConstant* GetNullConstant(uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000612
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600613 HIntConstant* GetIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc) {
614 return CreateConstant(value, &cached_int_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000615 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600616 HLongConstant* GetLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc) {
617 return CreateConstant(value, &cached_long_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000618 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600619 HFloatConstant* GetFloatConstant(float value, uint32_t dex_pc = kNoDexPc) {
620 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000621 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600622 HDoubleConstant* GetDoubleConstant(double value, uint32_t dex_pc = kNoDexPc) {
623 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000624 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000625
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100626 HCurrentMethod* GetCurrentMethod();
627
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100628 const DexFile& GetDexFile() const {
629 return dex_file_;
630 }
631
632 uint32_t GetMethodIdx() const {
633 return method_idx_;
634 }
635
Igor Murashkind01745e2017-04-05 16:40:31 -0700636 // Get the method name (without the signature), e.g. "<init>"
637 const char* GetMethodName() const;
638
639 // Get the pretty method name (class + name + optionally signature).
640 std::string PrettyMethod(bool with_signature = true) const;
641
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100642 InvokeType GetInvokeType() const {
643 return invoke_type_;
644 }
645
Mark Mendellc4701932015-04-10 13:18:51 -0400646 InstructionSet GetInstructionSet() const {
647 return instruction_set_;
648 }
649
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000650 bool IsCompilingOsr() const { return osr_; }
651
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +0000652 bool IsCompilingBaseline() const { return baseline_; }
653
Nicolas Geoffraya48c3df2019-06-27 13:11:12 +0000654 bool IsCompilingForSharedJitCode() const {
655 return is_shared_jit_code_;
656 }
657
Mingyao Yang063fc772016-08-02 11:02:54 -0700658 ArenaSet<ArtMethod*>& GetCHASingleImplementationList() {
659 return cha_single_implementation_list_;
660 }
661
662 void AddCHASingleImplementationDependency(ArtMethod* method) {
663 cha_single_implementation_list_.insert(method);
664 }
665
666 bool HasShouldDeoptimizeFlag() const {
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800667 return number_of_cha_guards_ != 0;
Mingyao Yang063fc772016-08-02 11:02:54 -0700668 }
669
David Brazdil77a48ae2015-09-15 12:34:04 +0000670 bool HasTryCatch() const { return has_try_catch_; }
671 void SetHasTryCatch(bool value) { has_try_catch_ = value; }
David Brazdilbbd733e2015-08-18 17:48:17 +0100672
Artem Serov2808be82018-12-20 19:15:11 +0000673 bool HasMonitorOperations() const { return has_monitor_operations_; }
674 void SetHasMonitorOperations(bool value) { has_monitor_operations_ = value; }
675
Aart Bikb13c65b2017-03-21 20:14:07 -0700676 bool HasSIMD() const { return has_simd_; }
677 void SetHasSIMD(bool value) { has_simd_ = value; }
678
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800679 bool HasLoops() const { return has_loops_; }
680 void SetHasLoops(bool value) { has_loops_ = value; }
681
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000682 bool HasIrreducibleLoops() const { return has_irreducible_loops_; }
683 void SetHasIrreducibleLoops(bool value) { has_irreducible_loops_ = value; }
684
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100685 ArtMethod* GetArtMethod() const { return art_method_; }
686 void SetArtMethod(ArtMethod* method) { art_method_ = method; }
687
Roland Levillain5e8d5f02016-10-18 18:03:43 +0100688 // Returns an instruction with the opposite Boolean value from 'cond'.
Mark Mendellf6529172015-11-17 11:16:56 -0500689 // The instruction has been inserted into the graph, either as a constant, or
690 // before cursor.
691 HInstruction* InsertOppositeCondition(HInstruction* cond, HInstruction* cursor);
692
Vladimir Marko02ca05a2020-05-12 13:58:51 +0100693 ReferenceTypeInfo GetInexactObjectRti() {
694 return ReferenceTypeInfo::Create(handle_cache_.GetObjectClassHandle(), /* is_exact= */ false);
695 }
Nicolas Geoffray18401b72016-03-11 13:35:51 +0000696
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800697 uint32_t GetNumberOfCHAGuards() { return number_of_cha_guards_; }
698 void SetNumberOfCHAGuards(uint32_t num) { number_of_cha_guards_ = num; }
699 void IncrementNumberOfCHAGuards() { number_of_cha_guards_++; }
700
David Brazdil2d7352b2015-04-20 14:52:42 +0100701 private:
Roland Levillainfc600dc2014-12-02 17:16:31 +0000702 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100703 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000704
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000705 template <class InstructionType, typename ValueType>
706 InstructionType* CreateConstant(ValueType value,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600707 ArenaSafeMap<ValueType, InstructionType*>* cache,
708 uint32_t dex_pc = kNoDexPc) {
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000709 // Try to find an existing constant of the given value.
710 InstructionType* constant = nullptr;
711 auto cached_constant = cache->find(value);
712 if (cached_constant != cache->end()) {
713 constant = cached_constant->second;
714 }
715
716 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100717 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000718 if (constant == nullptr || constant->GetBlock() == nullptr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100719 constant = new (allocator_) InstructionType(value, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000720 cache->Overwrite(value, constant);
721 InsertConstant(constant);
722 }
723 return constant;
724 }
725
David Brazdil8d5b8b22015-03-24 10:51:52 +0000726 void InsertConstant(HConstant* instruction);
727
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000728 // Cache a float constant into the graph. This method should only be
729 // called by the SsaBuilder when creating "equivalent" instructions.
730 void CacheFloatConstant(HFloatConstant* constant);
731
732 // See CacheFloatConstant comment.
733 void CacheDoubleConstant(HDoubleConstant* constant);
734
Vladimir Markoca6fff82017-10-03 14:49:14 +0100735 ArenaAllocator* const allocator_;
736 ArenaStack* const arena_stack_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000737
Vladimir Marko02ca05a2020-05-12 13:58:51 +0100738 HandleCache handle_cache_;
739
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000740 // List of blocks in insertion order.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100741 ArenaVector<HBasicBlock*> blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000742
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100743 // List of blocks to perform a reverse post order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100744 ArenaVector<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000745
Aart Bik281c6812016-08-26 11:31:48 -0700746 // List of blocks to perform a linear order tree traversal. Unlike the reverse
747 // post order, this order is not incrementally kept up-to-date.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100748 ArenaVector<HBasicBlock*> linear_order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100749
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000750 HBasicBlock* entry_block_;
751 HBasicBlock* exit_block_;
752
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100753 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100754 uint16_t maximum_number_of_out_vregs_;
755
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100756 // The number of virtual registers in this method. Contains the parameters.
757 uint16_t number_of_vregs_;
758
759 // The number of virtual registers used by parameters of this method.
760 uint16_t number_of_in_vregs_;
761
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000762 // Number of vreg size slots that the temporaries use (used in baseline compiler).
763 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100764
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800765 // Flag whether there are bounds checks in the graph. We can skip
766 // BCE if it's false. It's only best effort to keep it up to date in
767 // the presence of code elimination so there might be false positives.
Mark Mendell1152c922015-04-24 17:06:35 -0400768 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800769
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800770 // Flag whether there are try/catch blocks in the graph. We will skip
771 // try/catch-related passes if it's false. It's only best effort to keep
772 // it up to date in the presence of code elimination so there might be
773 // false positives.
David Brazdil77a48ae2015-09-15 12:34:04 +0000774 bool has_try_catch_;
775
Artem Serov2808be82018-12-20 19:15:11 +0000776 // Flag whether there are any HMonitorOperation in the graph. If yes this will mandate
777 // DexRegisterMap to be present to allow deadlock analysis for non-debuggable code.
778 bool has_monitor_operations_;
779
Aart Bikb13c65b2017-03-21 20:14:07 -0700780 // Flag whether SIMD instructions appear in the graph. If true, the
781 // code generators may have to be more careful spilling the wider
782 // contents of SIMD registers.
783 bool has_simd_;
784
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800785 // Flag whether there are any loops in the graph. We can skip loop
786 // optimization if it's false. It's only best effort to keep it up
787 // to date in the presence of code elimination so there might be false
788 // positives.
789 bool has_loops_;
790
791 // Flag whether there are any irreducible loops in the graph. It's only
792 // best effort to keep it up to date in the presence of code elimination
793 // so there might be false positives.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000794 bool has_irreducible_loops_;
795
Hans Boehm206348c2018-12-05 11:11:33 -0800796 // Is the code known to be robust against eliminating dead references
797 // and the effects of early finalization? If false, dead reference variables
798 // are kept if they might be visible to the garbage collector.
799 // Currently this means that the class was declared to be dead-reference-safe,
800 // the method accesses no reachability-sensitive fields or data, and the same
801 // is true for any methods that were inlined into the current one.
802 bool dead_reference_safe_;
803
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000804 // Indicates whether the graph should be compiled in a way that
805 // ensures full debuggability. If false, we can apply more
806 // aggressive optimizations that may limit the level of debugging.
807 const bool debuggable_;
808
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000809 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000810 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000811
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100812 // The dex file from which the method is from.
813 const DexFile& dex_file_;
814
815 // The method index in the dex file.
816 const uint32_t method_idx_;
817
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100818 // If inlined, this encodes how the callee is being invoked.
819 const InvokeType invoke_type_;
820
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100821 // Whether the graph has been transformed to SSA form. Only used
822 // in debug mode to ensure we are not using properties only valid
823 // for non-SSA form (like the number of temporaries).
824 bool in_ssa_form_;
825
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800826 // Number of CHA guards in the graph. Used to short-circuit the
827 // CHA guard optimization pass when there is no CHA guard left.
828 uint32_t number_of_cha_guards_;
829
Mathieu Chartiere401d142015-04-22 13:56:20 -0700830 const InstructionSet instruction_set_;
831
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000832 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000833 HNullConstant* cached_null_constant_;
834 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000835 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000836 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000837 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000838
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100839 HCurrentMethod* cached_current_method_;
840
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100841 // The ArtMethod this graph is for. Note that for AOT, it may be null,
842 // for example for methods whose declaring class could not be resolved
843 // (such as when the superclass could not be found).
844 ArtMethod* art_method_;
845
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000846 // Whether we are compiling this graph for on stack replacement: this will
847 // make all loops seen as irreducible and emit special stack maps to mark
848 // compiled code entries which the interpreter can directly jump to.
849 const bool osr_;
850
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +0000851 // Whether we are compiling baseline (not running optimizations). This affects
852 // the code being generated.
853 const bool baseline_;
854
Mingyao Yang063fc772016-08-02 11:02:54 -0700855 // List of methods that are assumed to have single implementation.
856 ArenaSet<ArtMethod*> cha_single_implementation_list_;
857
Nicolas Geoffraya48c3df2019-06-27 13:11:12 +0000858 // Whether we are JIT compiling in the shared region area, putting
859 // restrictions on, for example, how literals are being generated.
860 bool is_shared_jit_code_;
861
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000862 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100863 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000864 friend class HInliner; // For the reverse post order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000865 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000866 DISALLOW_COPY_AND_ASSIGN(HGraph);
867};
868
Vladimir Markof9f64412015-09-02 14:05:49 +0100869class HLoopInformation : public ArenaObject<kArenaAllocLoopInfo> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000870 public:
871 HLoopInformation(HBasicBlock* header, HGraph* graph)
872 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100873 suspend_check_(nullptr),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000874 irreducible_(false),
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100875 contains_irreducible_loop_(false),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100876 back_edges_(graph->GetAllocator()->Adapter(kArenaAllocLoopInfoBackEdges)),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100877 // Make bit vector growable, as the number of blocks may change.
Vladimir Markoca6fff82017-10-03 14:49:14 +0100878 blocks_(graph->GetAllocator(),
879 graph->GetBlocks().size(),
880 true,
881 kArenaAllocLoopInfoBackEdges) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100882 back_edges_.reserve(kDefaultNumberOfBackEdges);
883 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100884
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000885 bool IsIrreducible() const { return irreducible_; }
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100886 bool ContainsIrreducibleLoop() const { return contains_irreducible_loop_; }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000887
888 void Dump(std::ostream& os);
889
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100890 HBasicBlock* GetHeader() const {
891 return header_;
892 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000893
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000894 void SetHeader(HBasicBlock* block) {
895 header_ = block;
896 }
897
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100898 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
899 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
900 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
901
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000902 void AddBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100903 back_edges_.push_back(back_edge);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000904 }
905
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100906 void RemoveBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100907 RemoveElement(back_edges_, back_edge);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100908 }
909
David Brazdil46e2a392015-03-16 17:31:52 +0000910 bool IsBackEdge(const HBasicBlock& block) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100911 return ContainsElement(back_edges_, &block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100912 }
913
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000914 size_t NumberOfBackEdges() const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100915 return back_edges_.size();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000916 }
917
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100918 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100919
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100920 const ArenaVector<HBasicBlock*>& GetBackEdges() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100921 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100922 }
923
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100924 // Returns the lifetime position of the back edge that has the
925 // greatest lifetime position.
926 size_t GetLifetimeEnd() const;
927
928 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100929 ReplaceElement(back_edges_, existing, new_back_edge);
Nicolas Geoffray57902602015-04-21 14:28:41 +0100930 }
931
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000932 // Finds blocks that are part of this loop.
933 void Populate();
David Brazdila4b8c212015-05-07 09:59:30 +0100934
Artem Serov7f4aff62017-06-21 17:02:18 +0100935 // Updates blocks population of the loop and all of its outer' ones recursively after the
936 // population of the inner loop is updated.
937 void PopulateInnerLoopUpwards(HLoopInformation* inner_loop);
938
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100939 // Returns whether this loop information contains `block`.
940 // Note that this loop information *must* be populated before entering this function.
941 bool Contains(const HBasicBlock& block) const;
942
943 // Returns whether this loop information is an inner loop of `other`.
944 // Note that `other` *must* be populated before entering this function.
945 bool IsIn(const HLoopInformation& other) const;
946
Mingyao Yang4b467ed2015-11-19 17:04:22 -0800947 // Returns true if instruction is not defined within this loop.
948 bool IsDefinedOutOfTheLoop(HInstruction* instruction) const;
Aart Bik73f1f3b2015-10-28 15:28:08 -0700949
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100950 const ArenaBitVector& GetBlocks() const { return blocks_; }
951
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000952 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000953 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000954
Nicolas Geoffray788f2f02016-01-22 12:41:38 +0000955 void ClearAllBlocks() {
956 blocks_.ClearAllBits();
957 }
958
David Brazdil3f4a5222016-05-06 12:46:21 +0100959 bool HasBackEdgeNotDominatedByHeader() const;
960
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100961 bool IsPopulated() const {
962 return blocks_.GetHighestBitSet() != -1;
963 }
964
Anton Shaminf89381f2016-05-16 16:44:13 +0600965 bool DominatesAllBackEdges(HBasicBlock* block);
966
David Sehrc757dec2016-11-04 15:48:34 -0700967 bool HasExitEdge() const;
968
Artem Serov7f4aff62017-06-21 17:02:18 +0100969 // Resets back edge and blocks-in-loop data.
970 void ResetBasicBlockData() {
971 back_edges_.clear();
972 ClearAllBlocks();
973 }
974
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000975 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100976 // Internal recursive implementation of `Populate`.
977 void PopulateRecursive(HBasicBlock* block);
David Brazdilc2e8af92016-04-05 17:15:19 +0100978 void PopulateIrreducibleRecursive(HBasicBlock* block, ArenaBitVector* finalized);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100979
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000980 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100981 HSuspendCheck* suspend_check_;
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000982 bool irreducible_;
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100983 bool contains_irreducible_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100984 ArenaVector<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100985 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000986
987 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
988};
989
David Brazdilec16f792015-08-19 15:04:01 +0100990// Stores try/catch information for basic blocks.
991// Note that HGraph is constructed so that catch blocks cannot simultaneously
992// be try blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100993class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> {
David Brazdilec16f792015-08-19 15:04:01 +0100994 public:
995 // Try block information constructor.
996 explicit TryCatchInformation(const HTryBoundary& try_entry)
997 : try_entry_(&try_entry),
998 catch_dex_file_(nullptr),
Nicolas Geoffrayebc3b252019-01-09 10:47:02 +0000999 catch_type_index_(dex::TypeIndex::Invalid()) {
David Brazdilec16f792015-08-19 15:04:01 +01001000 DCHECK(try_entry_ != nullptr);
1001 }
1002
1003 // Catch block information constructor.
Andreas Gampea5b09a62016-11-17 15:21:22 -08001004 TryCatchInformation(dex::TypeIndex catch_type_index, const DexFile& dex_file)
David Brazdilec16f792015-08-19 15:04:01 +01001005 : try_entry_(nullptr),
1006 catch_dex_file_(&dex_file),
1007 catch_type_index_(catch_type_index) {}
1008
1009 bool IsTryBlock() const { return try_entry_ != nullptr; }
1010
1011 const HTryBoundary& GetTryEntry() const {
1012 DCHECK(IsTryBlock());
1013 return *try_entry_;
1014 }
1015
1016 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
1017
Nicolas Geoffrayebc3b252019-01-09 10:47:02 +00001018 bool IsValidTypeIndex() const {
David Brazdilec16f792015-08-19 15:04:01 +01001019 DCHECK(IsCatchBlock());
Nicolas Geoffrayebc3b252019-01-09 10:47:02 +00001020 return catch_type_index_.IsValid();
David Brazdilec16f792015-08-19 15:04:01 +01001021 }
1022
Andreas Gampea5b09a62016-11-17 15:21:22 -08001023 dex::TypeIndex GetCatchTypeIndex() const {
David Brazdilec16f792015-08-19 15:04:01 +01001024 DCHECK(IsCatchBlock());
1025 return catch_type_index_;
1026 }
1027
1028 const DexFile& GetCatchDexFile() const {
1029 DCHECK(IsCatchBlock());
1030 return *catch_dex_file_;
1031 }
1032
Nicolas Geoffrayebc3b252019-01-09 10:47:02 +00001033 void SetInvalidTypeIndex() {
1034 catch_type_index_ = dex::TypeIndex::Invalid();
1035 }
1036
David Brazdilec16f792015-08-19 15:04:01 +01001037 private:
1038 // One of possibly several TryBoundary instructions entering the block's try.
1039 // Only set for try blocks.
1040 const HTryBoundary* try_entry_;
1041
1042 // Exception type information. Only set for catch blocks.
1043 const DexFile* catch_dex_file_;
Nicolas Geoffrayebc3b252019-01-09 10:47:02 +00001044 dex::TypeIndex catch_type_index_;
David Brazdilec16f792015-08-19 15:04:01 +01001045};
1046
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001047static constexpr size_t kNoLifetime = -1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001048static constexpr uint32_t kInvalidBlockId = static_cast<uint32_t>(-1);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001049
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001050// A block in a method. Contains the list of instructions represented
1051// as a double linked list. Each block knows its predecessors and
1052// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001053
Vladimir Markof9f64412015-09-02 14:05:49 +01001054class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001055 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -07001056 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001057 : graph_(graph),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001058 predecessors_(graph->GetAllocator()->Adapter(kArenaAllocPredecessors)),
1059 successors_(graph->GetAllocator()->Adapter(kArenaAllocSuccessors)),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001060 loop_information_(nullptr),
1061 dominator_(nullptr),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001062 dominated_blocks_(graph->GetAllocator()->Adapter(kArenaAllocDominated)),
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001063 block_id_(kInvalidBlockId),
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001064 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001065 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001066 lifetime_end_(kNoLifetime),
Vladimir Marko60584552015-09-03 13:35:12 +00001067 try_catch_information_(nullptr) {
1068 predecessors_.reserve(kDefaultNumberOfPredecessors);
1069 successors_.reserve(kDefaultNumberOfSuccessors);
1070 dominated_blocks_.reserve(kDefaultNumberOfDominatedBlocks);
1071 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001072
Vladimir Marko60584552015-09-03 13:35:12 +00001073 const ArenaVector<HBasicBlock*>& GetPredecessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001074 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001075 }
1076
Vladimir Marko60584552015-09-03 13:35:12 +00001077 const ArenaVector<HBasicBlock*>& GetSuccessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001078 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001079 }
1080
David Brazdild26a4112015-11-10 11:07:31 +00001081 ArrayRef<HBasicBlock* const> GetNormalSuccessors() const;
1082 ArrayRef<HBasicBlock* const> GetExceptionalSuccessors() const;
1083
Vladimir Marko60584552015-09-03 13:35:12 +00001084 bool HasSuccessor(const HBasicBlock* block, size_t start_from = 0u) {
1085 return ContainsElement(successors_, block, start_from);
1086 }
1087
1088 const ArenaVector<HBasicBlock*>& GetDominatedBlocks() const {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001089 return dominated_blocks_;
1090 }
1091
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001092 bool IsEntryBlock() const {
1093 return graph_->GetEntryBlock() == this;
1094 }
1095
1096 bool IsExitBlock() const {
1097 return graph_->GetExitBlock() == this;
1098 }
1099
David Brazdil46e2a392015-03-16 17:31:52 +00001100 bool IsSingleGoto() const;
Mads Ager16e52892017-07-14 13:11:37 +02001101 bool IsSingleReturn() const;
Mingyao Yang46721ef2017-10-05 14:45:17 -07001102 bool IsSingleReturnOrReturnVoidAllowingPhis() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +00001103 bool IsSingleTryBoundary() const;
1104
1105 // Returns true if this block emits nothing but a jump.
1106 bool IsSingleJump() const {
1107 HLoopInformation* loop_info = GetLoopInformation();
1108 return (IsSingleGoto() || IsSingleTryBoundary())
1109 // Back edges generate a suspend check.
1110 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
1111 }
David Brazdil46e2a392015-03-16 17:31:52 +00001112
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001113 void AddBackEdge(HBasicBlock* back_edge) {
1114 if (loop_information_ == nullptr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001115 loop_information_ = new (graph_->GetAllocator()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001116 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001117 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001118 loop_information_->AddBackEdge(back_edge);
1119 }
1120
Artem Serov7f4aff62017-06-21 17:02:18 +01001121 // Registers a back edge; if the block was not a loop header before the call associates a newly
1122 // created loop info with it.
1123 //
1124 // Used in SuperblockCloner to preserve LoopInformation object instead of reseting loop
1125 // info for all blocks during back edges recalculation.
1126 void AddBackEdgeWhileUpdating(HBasicBlock* back_edge) {
1127 if (loop_information_ == nullptr || loop_information_->GetHeader() != this) {
1128 loop_information_ = new (graph_->GetAllocator()) HLoopInformation(this, graph_);
1129 }
1130 loop_information_->AddBackEdge(back_edge);
1131 }
1132
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001133 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001134 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001135
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001136 uint32_t GetBlockId() const { return block_id_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001137 void SetBlockId(int id) { block_id_ = id; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001138 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001139
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001140 HBasicBlock* GetDominator() const { return dominator_; }
1141 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Vladimir Marko60584552015-09-03 13:35:12 +00001142 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.push_back(block); }
1143
1144 void RemoveDominatedBlock(HBasicBlock* block) {
1145 RemoveElement(dominated_blocks_, block);
Vladimir Marko91e11c02015-09-02 17:03:22 +01001146 }
Vladimir Marko60584552015-09-03 13:35:12 +00001147
1148 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
1149 ReplaceElement(dominated_blocks_, existing, new_block);
1150 }
1151
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +01001152 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001153
1154 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +01001155 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001156 }
1157
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001158 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
1159 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +01001160 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001161 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +01001162 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
1163 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001164
Nicolas Geoffray09aa1472016-01-19 10:52:54 +00001165 HInstruction* GetFirstInstructionDisregardMoves() const;
1166
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001167 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001168 successors_.push_back(block);
1169 block->predecessors_.push_back(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001170 }
1171
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001172 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
1173 size_t successor_index = GetSuccessorIndexOf(existing);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001174 existing->RemovePredecessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +00001175 new_block->predecessors_.push_back(this);
1176 successors_[successor_index] = new_block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001177 }
1178
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001179 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
1180 size_t predecessor_index = GetPredecessorIndexOf(existing);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001181 existing->RemoveSuccessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +00001182 new_block->successors_.push_back(this);
1183 predecessors_[predecessor_index] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001184 }
1185
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001186 // Insert `this` between `predecessor` and `successor. This method
Alex Light1e52a072019-06-25 09:12:04 -07001187 // preserves the indices, and will update the first edge found between
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001188 // `predecessor` and `successor`.
1189 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
1190 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001191 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko60584552015-09-03 13:35:12 +00001192 successor->predecessors_[predecessor_index] = this;
1193 predecessor->successors_[successor_index] = this;
1194 successors_.push_back(successor);
1195 predecessors_.push_back(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001196 }
1197
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001198 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001199 predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001200 }
1201
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001202 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001203 successors_.erase(successors_.begin() + GetSuccessorIndexOf(block));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001204 }
1205
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001206 void ClearAllPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001207 predecessors_.clear();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001208 }
1209
1210 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001211 predecessors_.push_back(block);
1212 block->successors_.push_back(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001213 }
1214
Nicolas Geoffray604c6e42014-09-17 12:08:44 +01001215 void SwapPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001216 DCHECK_EQ(predecessors_.size(), 2u);
1217 std::swap(predecessors_[0], predecessors_[1]);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +01001218 }
1219
David Brazdil769c9e52015-04-27 13:54:09 +01001220 void SwapSuccessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001221 DCHECK_EQ(successors_.size(), 2u);
1222 std::swap(successors_[0], successors_[1]);
David Brazdil769c9e52015-04-27 13:54:09 +01001223 }
1224
David Brazdilfc6a86a2015-06-26 10:33:45 +00001225 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko60584552015-09-03 13:35:12 +00001226 return IndexOfElement(predecessors_, predecessor);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001227 }
1228
David Brazdilfc6a86a2015-06-26 10:33:45 +00001229 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko60584552015-09-03 13:35:12 +00001230 return IndexOfElement(successors_, successor);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001231 }
1232
David Brazdilfc6a86a2015-06-26 10:33:45 +00001233 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00001234 DCHECK_EQ(GetPredecessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +01001235 return GetPredecessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +00001236 }
1237
1238 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00001239 DCHECK_EQ(GetSuccessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +01001240 return GetSuccessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +00001241 }
1242
1243 // Returns whether the first occurrence of `predecessor` in the list of
1244 // predecessors is at index `idx`.
1245 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01001246 DCHECK_EQ(GetPredecessors()[idx], predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +00001247 return GetPredecessorIndexOf(predecessor) == idx;
1248 }
1249
David Brazdild7558da2015-09-22 13:04:14 +01001250 // Create a new block between this block and its predecessors. The new block
1251 // is added to the graph, all predecessor edges are relinked to it and an edge
1252 // is created to `this`. Returns the new empty block. Reverse post order or
1253 // loop and try/catch information are not updated.
1254 HBasicBlock* CreateImmediateDominator();
1255
David Brazdilfc6a86a2015-06-26 10:33:45 +00001256 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +01001257 // created, latter block. Note that this method will add the block to the
1258 // graph, create a Goto at the end of the former block and will create an edge
1259 // between the blocks. It will not, however, update the reverse post order or
David Brazdild7558da2015-09-22 13:04:14 +01001260 // loop and try/catch information.
David Brazdilfc6a86a2015-06-26 10:33:45 +00001261 HBasicBlock* SplitBefore(HInstruction* cursor);
1262
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001263 // Split the block into two blocks just before `cursor`. Returns the newly
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001264 // created block. Note that this method just updates raw block information,
1265 // like predecessors, successors, dominators, and instruction list. It does not
1266 // update the graph, reverse post order, loop information, nor make sure the
1267 // blocks are consistent (for example ending with a control flow instruction).
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001268 HBasicBlock* SplitBeforeForInlining(HInstruction* cursor);
1269
1270 // Similar to `SplitBeforeForInlining` but does it after `cursor`.
1271 HBasicBlock* SplitAfterForInlining(HInstruction* cursor);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001272
1273 // Merge `other` at the end of `this`. Successors and dominated blocks of
1274 // `other` are changed to be successors and dominated blocks of `this`. Note
1275 // that this method does not update the graph, reverse post order, loop
1276 // information, nor make sure the blocks are consistent (for example ending
1277 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +01001278 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001279
1280 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
1281 // of `this` are moved to `other`.
1282 // Note that this method does not update the graph, reverse post order, loop
1283 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +00001284 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001285 void ReplaceWith(HBasicBlock* other);
1286
Aart Bik6b69e0a2017-01-11 10:20:43 -08001287 // Merges the instructions of `other` at the end of `this`.
1288 void MergeInstructionsWith(HBasicBlock* other);
1289
David Brazdil2d7352b2015-04-20 14:52:42 +01001290 // Merge `other` at the end of `this`. This method updates loops, reverse post
1291 // order, links to predecessors, successors, dominators and deletes the block
1292 // from the graph. The two blocks must be successive, i.e. `this` the only
1293 // predecessor of `other` and vice versa.
1294 void MergeWith(HBasicBlock* other);
1295
1296 // Disconnects `this` from all its predecessors, successors and dominator,
1297 // removes it from all loops it is included in and eventually from the graph.
1298 // The block must not dominate any other block. Predecessors and successors
1299 // are safely updated.
1300 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +00001301
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001302 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001303 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001304 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001305 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Artem Serovcced8ba2017-07-19 18:18:09 +01001306 // Replace phi `initial` with `replacement` within this block.
1307 void ReplaceAndRemovePhiWith(HPhi* initial, HPhi* replacement);
Roland Levillainccc07a92014-09-16 14:48:16 +01001308 // Replace instruction `initial` with `replacement` within this block.
1309 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
1310 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001311 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001312 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +00001313 // RemoveInstruction and RemovePhi delete a given instruction from the respective
1314 // instruction list. With 'ensure_safety' set to true, it verifies that the
1315 // instruction is not in use and removes it from the use lists of its inputs.
1316 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
1317 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +01001318 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001319
1320 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +01001321 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001322 }
1323
Roland Levillain6b879dd2014-09-22 17:13:44 +01001324 bool IsLoopPreHeaderFirstPredecessor() const {
1325 DCHECK(IsLoopHeader());
Vladimir Markoec7802a2015-10-01 20:57:57 +01001326 return GetPredecessors()[0] == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +01001327 }
1328
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001329 bool IsFirstPredecessorBackEdge() const {
1330 DCHECK(IsLoopHeader());
1331 return GetLoopInformation()->IsBackEdge(*GetPredecessors()[0]);
1332 }
1333
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001334 HLoopInformation* GetLoopInformation() const {
1335 return loop_information_;
1336 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001337
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001338 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001339 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001340 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001341 void SetInLoop(HLoopInformation* info) {
1342 if (IsLoopHeader()) {
1343 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +01001344 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001345 loop_information_ = info;
1346 } else if (loop_information_->Contains(*info->GetHeader())) {
1347 // Block is currently part of an outer loop. Make it part of this inner loop.
1348 // Note that a non loop header having a loop information means this loop information
1349 // has already been populated
1350 loop_information_ = info;
1351 } else {
1352 // Block is part of an inner loop. Do not update the loop information.
1353 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
1354 // at this point, because this method is being called while populating `info`.
1355 }
1356 }
1357
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001358 // Raw update of the loop information.
1359 void SetLoopInformation(HLoopInformation* info) {
1360 loop_information_ = info;
1361 }
1362
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001363 bool IsInLoop() const { return loop_information_ != nullptr; }
1364
David Brazdilec16f792015-08-19 15:04:01 +01001365 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
1366
1367 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
1368 try_catch_information_ = try_catch_information;
1369 }
1370
1371 bool IsTryBlock() const {
1372 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
1373 }
1374
1375 bool IsCatchBlock() const {
1376 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
1377 }
David Brazdilffee3d32015-07-06 11:48:53 +01001378
1379 // Returns the try entry that this block's successors should have. They will
1380 // be in the same try, unless the block ends in a try boundary. In that case,
1381 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +01001382 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001383
Aart Bik75ff2c92018-04-21 01:28:11 +00001384 bool HasThrowingInstructions() const;
1385
David Brazdila4b8c212015-05-07 09:59:30 +01001386 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001387 bool Dominates(HBasicBlock* block) const;
1388
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001389 size_t GetLifetimeStart() const { return lifetime_start_; }
1390 size_t GetLifetimeEnd() const { return lifetime_end_; }
1391
1392 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
1393 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
1394
David Brazdil8d5b8b22015-03-24 10:51:52 +00001395 bool EndsWithControlFlowInstruction() const;
Aart Bik4dc09e72018-05-11 14:40:31 -07001396 bool EndsWithReturn() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001397 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001398 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001399 bool HasSinglePhi() const;
1400
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001401 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001402 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +00001403 ArenaVector<HBasicBlock*> predecessors_;
1404 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001405 HInstructionList instructions_;
1406 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001407 HLoopInformation* loop_information_;
1408 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +00001409 ArenaVector<HBasicBlock*> dominated_blocks_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001410 uint32_t block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001411 // The dex program counter of the first instruction of this block.
1412 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001413 size_t lifetime_start_;
1414 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +01001415 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +01001416
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001417 friend class HGraph;
1418 friend class HInstruction;
1419
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001420 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
1421};
1422
David Brazdilb2bd1c52015-03-25 11:17:37 +00001423// Iterates over the LoopInformation of all loops which contain 'block'
1424// from the innermost to the outermost.
1425class HLoopInformationOutwardIterator : public ValueObject {
1426 public:
1427 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
1428 : current_(block.GetLoopInformation()) {}
1429
1430 bool Done() const { return current_ == nullptr; }
1431
1432 void Advance() {
1433 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +01001434 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +00001435 }
1436
1437 HLoopInformation* Current() const {
1438 DCHECK(!Done());
1439 return current_;
1440 }
1441
1442 private:
1443 HLoopInformation* current_;
1444
1445 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
1446};
1447
Artem Serov1a719e42019-07-18 14:24:55 +01001448#define FOR_EACH_CONCRETE_INSTRUCTION_SCALAR_COMMON(M) \
Aart Bike9f37602015-10-09 11:15:55 -07001449 M(Above, Condition) \
1450 M(AboveOrEqual, Condition) \
Aart Bik3dad3412018-02-28 12:01:46 -08001451 M(Abs, UnaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001452 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001453 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001454 M(ArrayGet, Instruction) \
1455 M(ArrayLength, Instruction) \
1456 M(ArraySet, Instruction) \
Aart Bike9f37602015-10-09 11:15:55 -07001457 M(Below, Condition) \
1458 M(BelowOrEqual, Condition) \
David Brazdil66d126e2015-04-03 16:02:44 +01001459 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001460 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001461 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001462 M(CheckCast, Instruction) \
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00001463 M(ClassTableGet, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001464 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001465 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001466 M(Compare, BinaryOperation) \
Igor Murashkind01745e2017-04-05 16:40:31 -07001467 M(ConstructorFence, Instruction) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001468 M(CurrentMethod, Instruction) \
Mingyao Yang063fc772016-08-02 11:02:54 -07001469 M(ShouldDeoptimizeFlag, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001470 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001471 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001472 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001473 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001474 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001475 M(Exit, Instruction) \
1476 M(FloatConstant, Constant) \
1477 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001478 M(GreaterThan, Condition) \
1479 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001480 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001481 M(InstanceFieldGet, Instruction) \
1482 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001483 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001484 M(IntConstant, Constant) \
xueliang.zhonge0eb4832017-10-30 13:43:14 +00001485 M(IntermediateAddress, Instruction) \
Calin Juravle175dc732015-08-25 15:42:32 +01001486 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001487 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001488 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001489 M(InvokeVirtual, Invoke) \
Orion Hodsonac141392017-01-13 11:53:47 +00001490 M(InvokePolymorphic, Invoke) \
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001491 M(InvokeCustom, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001492 M(LessThan, Condition) \
1493 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001494 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001495 M(LoadException, Instruction) \
Orion Hodsondbaa5c72018-05-10 08:22:46 +01001496 M(LoadMethodHandle, Instruction) \
Orion Hodson18259d72018-04-12 11:18:23 +01001497 M(LoadMethodType, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001498 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001499 M(LongConstant, Constant) \
Aart Bik1f8d51b2018-02-15 10:42:37 -08001500 M(Max, Instruction) \
Calin Juravle27df7582015-04-17 19:12:31 +01001501 M(MemoryBarrier, Instruction) \
Aart Bik1f8d51b2018-02-15 10:42:37 -08001502 M(Min, BinaryOperation) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001503 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001504 M(Mul, BinaryOperation) \
David Srbecky0cf44932015-12-09 14:09:59 +00001505 M(NativeDebugInfo, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001506 M(Neg, UnaryOperation) \
1507 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001508 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001509 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001510 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001511 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001512 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001513 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001514 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001515 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001516 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001517 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001518 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001519 M(Return, Instruction) \
1520 M(ReturnVoid, Instruction) \
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001521 M(Ror, BinaryOperation) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001522 M(Shl, BinaryOperation) \
1523 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001524 M(StaticFieldGet, Instruction) \
1525 M(StaticFieldSet, Instruction) \
Vladimir Marko552a1342017-10-31 10:56:47 +00001526 M(StringBuilderAppend, Instruction) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001527 M(UnresolvedInstanceFieldGet, Instruction) \
1528 M(UnresolvedInstanceFieldSet, Instruction) \
1529 M(UnresolvedStaticFieldGet, Instruction) \
1530 M(UnresolvedStaticFieldSet, Instruction) \
David Brazdil74eb1b22015-12-14 11:44:01 +00001531 M(Select, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001532 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001533 M(SuspendCheck, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001534 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001535 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001536 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001537 M(UShr, BinaryOperation) \
Artem Serov1a719e42019-07-18 14:24:55 +01001538 M(Xor, BinaryOperation)
1539
1540#define FOR_EACH_CONCRETE_INSTRUCTION_VECTOR_COMMON(M) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001541 M(VecReplicateScalar, VecUnaryOperation) \
Aart Bik0148de42017-09-05 09:25:01 -07001542 M(VecExtractScalar, VecUnaryOperation) \
1543 M(VecReduce, VecUnaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001544 M(VecCnv, VecUnaryOperation) \
1545 M(VecNeg, VecUnaryOperation) \
Aart Bik6daebeb2017-04-03 14:35:41 -07001546 M(VecAbs, VecUnaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001547 M(VecNot, VecUnaryOperation) \
1548 M(VecAdd, VecBinaryOperation) \
Aart Bikf3e61ee2017-04-12 17:09:20 -07001549 M(VecHalvingAdd, VecBinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001550 M(VecSub, VecBinaryOperation) \
1551 M(VecMul, VecBinaryOperation) \
1552 M(VecDiv, VecBinaryOperation) \
Aart Bikf3e61ee2017-04-12 17:09:20 -07001553 M(VecMin, VecBinaryOperation) \
1554 M(VecMax, VecBinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001555 M(VecAnd, VecBinaryOperation) \
1556 M(VecAndNot, VecBinaryOperation) \
1557 M(VecOr, VecBinaryOperation) \
1558 M(VecXor, VecBinaryOperation) \
Aart Bik29aa0822018-03-08 11:28:00 -08001559 M(VecSaturationAdd, VecBinaryOperation) \
1560 M(VecSaturationSub, VecBinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001561 M(VecShl, VecBinaryOperation) \
1562 M(VecShr, VecBinaryOperation) \
1563 M(VecUShr, VecBinaryOperation) \
Aart Bik8de59162017-04-21 09:42:01 -07001564 M(VecSetScalars, VecOperation) \
Artem Serovf34dd202017-04-10 17:41:46 +01001565 M(VecMultiplyAccumulate, VecOperation) \
Aart Bikdbbac8f2017-09-01 13:06:08 -07001566 M(VecSADAccumulate, VecOperation) \
Artem Serovaaac0e32018-08-07 00:52:22 +01001567 M(VecDotProd, VecOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001568 M(VecLoad, VecMemoryOperation) \
1569 M(VecStore, VecMemoryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001570
Artem Serov1a719e42019-07-18 14:24:55 +01001571#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
1572 FOR_EACH_CONCRETE_INSTRUCTION_SCALAR_COMMON(M) \
1573 FOR_EACH_CONCRETE_INSTRUCTION_VECTOR_COMMON(M)
1574
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001575/*
1576 * Instructions, shared across several (not all) architectures.
1577 */
1578#if !defined(ART_ENABLE_CODEGEN_arm) && !defined(ART_ENABLE_CODEGEN_arm64)
1579#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M)
1580#else
1581#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Artem Serov7fc63502016-02-09 17:15:29 +00001582 M(BitwiseNegatedRight, Instruction) \
Anton Kirilov74234da2017-01-13 14:42:47 +00001583 M(DataProcWithShifterOp, Instruction) \
Artem Serov328429f2016-07-06 16:23:04 +01001584 M(MultiplyAccumulate, Instruction) \
Artem Serove1811ed2017-04-27 16:50:47 +01001585 M(IntermediateAddressIndex, Instruction)
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001586#endif
1587
Alexandre Ramesef20f712015-06-09 10:29:30 +01001588#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
1589
1590#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
1591
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001592#ifndef ART_ENABLE_CODEGEN_x86
1593#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1594#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001595#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1596 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001597 M(X86LoadFromConstantTable, Instruction) \
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001598 M(X86FPNeg, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001599 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001600#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001601
Shalini Salomi Bodapatidd121f62018-10-26 15:03:53 +05301602#if defined(ART_ENABLE_CODEGEN_x86) || defined(ART_ENABLE_CODEGEN_x86_64)
1603#define FOR_EACH_CONCRETE_INSTRUCTION_X86_COMMON(M) \
Shalini Salomi Bodapati81d15be2019-05-30 11:00:42 +05301604 M(X86AndNot, Instruction) \
Shalini Salomi Bodapatib45a4352019-07-10 16:09:41 +05301605 M(X86MaskOrResetLeastSetBit, Instruction)
Shalini Salomi Bodapatidd121f62018-10-26 15:03:53 +05301606#else
1607#define FOR_EACH_CONCRETE_INSTRUCTION_X86_COMMON(M)
1608#endif
1609
Alexandre Ramesef20f712015-06-09 10:29:30 +01001610#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1611
1612#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1613 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001614 FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001615 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1616 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
1617 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
Shalini Salomi Bodapatidd121f62018-10-26 15:03:53 +05301618 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M) \
1619 FOR_EACH_CONCRETE_INSTRUCTION_X86_COMMON(M)
Alexandre Ramesef20f712015-06-09 10:29:30 +01001620
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001621#define FOR_EACH_ABSTRACT_INSTRUCTION(M) \
1622 M(Condition, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001623 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001624 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001625 M(BinaryOperation, Instruction) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001626 M(Invoke, Instruction) \
1627 M(VecOperation, Instruction) \
1628 M(VecUnaryOperation, VecOperation) \
1629 M(VecBinaryOperation, VecOperation) \
1630 M(VecMemoryOperation, VecOperation)
Dave Allison20dfc792014-06-16 20:44:29 -07001631
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001632#define FOR_EACH_INSTRUCTION(M) \
1633 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1634 FOR_EACH_ABSTRACT_INSTRUCTION(M)
1635
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001636#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001637FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1638#undef FORWARD_DECLARATION
1639
Vladimir Marko372f10e2016-05-17 16:30:10 +01001640#define DECLARE_INSTRUCTION(type) \
Artem Serovcced8ba2017-07-19 18:18:09 +01001641 private: \
1642 H##type& operator=(const H##type&) = delete; \
1643 public: \
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01001644 const char* DebugName() const override { return #type; } \
1645 HInstruction* Clone(ArenaAllocator* arena) const override { \
Artem Serovcced8ba2017-07-19 18:18:09 +01001646 DCHECK(IsClonable()); \
1647 return new (arena) H##type(*this->As##type()); \
1648 } \
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01001649 void Accept(HGraphVisitor* visitor) override
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001650
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001651#define DECLARE_ABSTRACT_INSTRUCTION(type) \
Artem Serovcced8ba2017-07-19 18:18:09 +01001652 private: \
1653 H##type& operator=(const H##type&) = delete; \
Vladimir Markoa90dd512018-05-04 15:04:45 +01001654 public:
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001655
Artem Serovcced8ba2017-07-19 18:18:09 +01001656#define DEFAULT_COPY_CONSTRUCTOR(type) \
1657 explicit H##type(const H##type& other) = default;
1658
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001659template <typename T>
Vladimir Marko82b07402017-03-01 19:02:04 +00001660class HUseListNode : public ArenaObject<kArenaAllocUseListNode>,
1661 public IntrusiveForwardListNode<HUseListNode<T>> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001662 public:
Igor Murashkind01745e2017-04-05 16:40:31 -07001663 // Get the instruction which has this use as one of the inputs.
David Brazdiled596192015-01-23 10:39:45 +00001664 T GetUser() const { return user_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07001665 // Get the position of the input record that this use corresponds to.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001666 size_t GetIndex() const { return index_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07001667 // Set the position of the input record that this use corresponds to.
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001668 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001669
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001670 private:
David Brazdiled596192015-01-23 10:39:45 +00001671 HUseListNode(T user, size_t index)
Vladimir Marko46817b82016-03-29 12:21:58 +01001672 : user_(user), index_(index) {}
David Brazdiled596192015-01-23 10:39:45 +00001673
1674 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001675 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001676
Vladimir Marko46817b82016-03-29 12:21:58 +01001677 friend class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001678
1679 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1680};
1681
David Brazdiled596192015-01-23 10:39:45 +00001682template <typename T>
Vladimir Marko46817b82016-03-29 12:21:58 +01001683using HUseList = IntrusiveForwardList<HUseListNode<T>>;
David Brazdiled596192015-01-23 10:39:45 +00001684
David Brazdil1abb4192015-02-17 18:33:36 +00001685// This class is used by HEnvironment and HInstruction classes to record the
1686// instructions they use and pointers to the corresponding HUseListNodes kept
1687// by the used instructions.
1688template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001689class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001690 public:
Vladimir Marko46817b82016-03-29 12:21:58 +01001691 HUserRecord() : instruction_(nullptr), before_use_node_() {}
1692 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), before_use_node_() {}
David Brazdil1abb4192015-02-17 18:33:36 +00001693
Vladimir Marko46817b82016-03-29 12:21:58 +01001694 HUserRecord(const HUserRecord<T>& old_record, typename HUseList<T>::iterator before_use_node)
1695 : HUserRecord(old_record.instruction_, before_use_node) {}
1696 HUserRecord(HInstruction* instruction, typename HUseList<T>::iterator before_use_node)
1697 : instruction_(instruction), before_use_node_(before_use_node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001698 DCHECK(instruction_ != nullptr);
David Brazdil1abb4192015-02-17 18:33:36 +00001699 }
1700
1701 HInstruction* GetInstruction() const { return instruction_; }
Vladimir Marko46817b82016-03-29 12:21:58 +01001702 typename HUseList<T>::iterator GetBeforeUseNode() const { return before_use_node_; }
1703 typename HUseList<T>::iterator GetUseNode() const { return ++GetBeforeUseNode(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001704
1705 private:
1706 // Instruction used by the user.
1707 HInstruction* instruction_;
1708
Vladimir Marko46817b82016-03-29 12:21:58 +01001709 // Iterator before the corresponding entry in the use list kept by 'instruction_'.
1710 typename HUseList<T>::iterator before_use_node_;
David Brazdil1abb4192015-02-17 18:33:36 +00001711};
1712
Vladimir Markoe9004912016-06-16 16:50:52 +01001713// Helper class that extracts the input instruction from HUserRecord<HInstruction*>.
1714// This is used for HInstruction::GetInputs() to return a container wrapper providing
1715// HInstruction* values even though the underlying container has HUserRecord<>s.
1716struct HInputExtractor {
1717 HInstruction* operator()(HUserRecord<HInstruction*>& record) const {
1718 return record.GetInstruction();
1719 }
1720 const HInstruction* operator()(const HUserRecord<HInstruction*>& record) const {
1721 return record.GetInstruction();
1722 }
1723};
1724
1725using HInputsRef = TransformArrayRef<HUserRecord<HInstruction*>, HInputExtractor>;
1726using HConstInputsRef = TransformArrayRef<const HUserRecord<HInstruction*>, HInputExtractor>;
1727
Aart Bik854a02b2015-07-14 16:07:00 -07001728/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001729 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001730 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001731 * For write/read dependences on fields/arrays, the dependence analysis uses
1732 * type disambiguation (e.g. a float field write cannot modify the value of an
1733 * integer field read) and the access type (e.g. a reference array write cannot
1734 * modify the value of a reference field read [although it may modify the
1735 * reference fetch prior to reading the field, which is represented by its own
1736 * write/read dependence]). The analysis makes conservative points-to
1737 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1738 * the same, and any reference read depends on any reference read without
1739 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001740 *
Artem Serovd1aa7d02018-06-22 11:35:46 +01001741 * kDependsOnGCBit is defined in the following way: instructions with kDependsOnGCBit must not be
1742 * alive across the point where garbage collection might happen.
1743 *
1744 * Note: Instructions with kCanTriggerGCBit do not depend on each other.
1745 *
1746 * kCanTriggerGCBit must be used for instructions for which GC might happen on the path across
1747 * those instructions from the compiler perspective (between this instruction and the next one
1748 * in the IR).
1749 *
1750 * Note: Instructions which can cause GC only on a fatal slow path do not need
1751 * kCanTriggerGCBit as the execution never returns to the instruction next to the exceptional
1752 * one. However the execution may return to compiled code if there is a catch block in the
1753 * current method; for this purpose the TryBoundary exit instruction has kCanTriggerGCBit
1754 * set.
1755 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001756 * The internal representation uses 38-bit and is described in the table below.
1757 * The first line indicates the side effect, and for field/array accesses the
1758 * second line indicates the type of the access (in the order of the
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001759 * DataType::Type enum).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001760 * The two numbered lines below indicate the bit position in the bitfield (read
1761 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001762 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001763 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1764 * +-------------+---------+---------+--------------+---------+---------+
1765 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1766 * | 3 |333333322|222222221| 1 |111111110|000000000|
1767 * | 7 |654321098|765432109| 8 |765432109|876543210|
1768 *
1769 * Note that, to ease the implementation, 'changes' bits are least significant
1770 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001771 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001772class SideEffects : public ValueObject {
1773 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001774 SideEffects() : flags_(0) {}
1775
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001776 static SideEffects None() {
1777 return SideEffects(0);
1778 }
1779
1780 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001781 return SideEffects(kAllChangeBits | kAllDependOnBits);
1782 }
1783
1784 static SideEffects AllChanges() {
1785 return SideEffects(kAllChangeBits);
1786 }
1787
1788 static SideEffects AllDependencies() {
1789 return SideEffects(kAllDependOnBits);
1790 }
1791
1792 static SideEffects AllExceptGCDependency() {
1793 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1794 }
1795
1796 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001797 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001798 }
1799
Aart Bik34c3ba92015-07-20 14:08:59 -07001800 static SideEffects AllWrites() {
1801 return SideEffects(kAllWrites);
1802 }
1803
1804 static SideEffects AllReads() {
1805 return SideEffects(kAllReads);
1806 }
1807
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001808 static SideEffects FieldWriteOfType(DataType::Type type, bool is_volatile) {
Aart Bik34c3ba92015-07-20 14:08:59 -07001809 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001810 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001811 : SideEffects(TypeFlag(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001812 }
1813
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001814 static SideEffects ArrayWriteOfType(DataType::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001815 return SideEffects(TypeFlag(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001816 }
1817
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001818 static SideEffects FieldReadOfType(DataType::Type type, bool is_volatile) {
Aart Bik34c3ba92015-07-20 14:08:59 -07001819 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001820 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001821 : SideEffects(TypeFlag(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001822 }
1823
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001824 static SideEffects ArrayReadOfType(DataType::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001825 return SideEffects(TypeFlag(type, kArrayReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001826 }
1827
Artem Serovd1aa7d02018-06-22 11:35:46 +01001828 // Returns whether GC might happen across this instruction from the compiler perspective so
1829 // the next instruction in the IR would see that.
1830 //
1831 // See the SideEffect class comments.
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001832 static SideEffects CanTriggerGC() {
1833 return SideEffects(1ULL << kCanTriggerGCBit);
1834 }
1835
Artem Serovd1aa7d02018-06-22 11:35:46 +01001836 // Returns whether the instruction must not be alive across a GC point.
1837 //
1838 // See the SideEffect class comments.
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001839 static SideEffects DependsOnGC() {
1840 return SideEffects(1ULL << kDependsOnGCBit);
1841 }
1842
Aart Bik854a02b2015-07-14 16:07:00 -07001843 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001844 SideEffects Union(SideEffects other) const {
1845 return SideEffects(flags_ | other.flags_);
1846 }
1847
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001848 SideEffects Exclusion(SideEffects other) const {
1849 return SideEffects(flags_ & ~other.flags_);
1850 }
1851
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001852 void Add(SideEffects other) {
1853 flags_ |= other.flags_;
1854 }
1855
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001856 bool Includes(SideEffects other) const {
1857 return (other.flags_ & flags_) == other.flags_;
1858 }
1859
1860 bool HasSideEffects() const {
1861 return (flags_ & kAllChangeBits);
1862 }
1863
1864 bool HasDependencies() const {
1865 return (flags_ & kAllDependOnBits);
1866 }
1867
1868 // Returns true if there are no side effects or dependencies.
1869 bool DoesNothing() const {
1870 return flags_ == 0;
1871 }
1872
Aart Bik854a02b2015-07-14 16:07:00 -07001873 // Returns true if something is written.
1874 bool DoesAnyWrite() const {
1875 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001876 }
1877
Aart Bik854a02b2015-07-14 16:07:00 -07001878 // Returns true if something is read.
1879 bool DoesAnyRead() const {
1880 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001881 }
1882
Aart Bik854a02b2015-07-14 16:07:00 -07001883 // Returns true if potentially everything is written and read
1884 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001885 bool DoesAllReadWrite() const {
1886 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1887 }
1888
Aart Bik854a02b2015-07-14 16:07:00 -07001889 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001890 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001891 }
1892
Roland Levillain0d5a2812015-11-13 10:07:31 +00001893 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001894 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001895 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1896 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001897 }
1898
1899 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001900 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001901 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001902 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001903 for (int s = kLastBit; s >= 0; s--) {
1904 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1905 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1906 // This is a bit for the GC side effect.
1907 if (current_bit_is_set) {
1908 flags += "GC";
1909 }
Aart Bik854a02b2015-07-14 16:07:00 -07001910 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001911 } else {
1912 // This is a bit for the array/field analysis.
1913 // The underscore character stands for the 'can trigger GC' bit.
1914 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1915 if (current_bit_is_set) {
1916 flags += kDebug[s];
1917 }
1918 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1919 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1920 flags += "|";
1921 }
1922 }
Aart Bik854a02b2015-07-14 16:07:00 -07001923 }
1924 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001925 }
1926
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001927 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001928
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001929 private:
1930 static constexpr int kFieldArrayAnalysisBits = 9;
1931
1932 static constexpr int kFieldWriteOffset = 0;
1933 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1934 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1935 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1936
1937 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1938
1939 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1940 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1941 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1942 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1943
1944 static constexpr int kLastBit = kDependsOnGCBit;
1945 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1946
1947 // Aliases.
1948
1949 static_assert(kChangeBits == kDependOnBits,
1950 "the 'change' bits should match the 'depend on' bits.");
1951
1952 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1953 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1954 static constexpr uint64_t kAllWrites =
1955 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1956 static constexpr uint64_t kAllReads =
1957 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001958
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001959 // Translates type to bit flag. The type must correspond to a Java type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001960 static uint64_t TypeFlag(DataType::Type type, int offset) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001961 int shift;
1962 switch (type) {
1963 case DataType::Type::kReference: shift = 0; break;
1964 case DataType::Type::kBool: shift = 1; break;
1965 case DataType::Type::kInt8: shift = 2; break;
1966 case DataType::Type::kUint16: shift = 3; break;
1967 case DataType::Type::kInt16: shift = 4; break;
1968 case DataType::Type::kInt32: shift = 5; break;
1969 case DataType::Type::kInt64: shift = 6; break;
1970 case DataType::Type::kFloat32: shift = 7; break;
1971 case DataType::Type::kFloat64: shift = 8; break;
1972 default:
1973 LOG(FATAL) << "Unexpected data type " << type;
1974 UNREACHABLE();
1975 }
Aart Bik854a02b2015-07-14 16:07:00 -07001976 DCHECK_LE(kFieldWriteOffset, shift);
1977 DCHECK_LT(shift, kArrayWriteOffset);
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001978 return UINT64_C(1) << (shift + offset);
Aart Bik854a02b2015-07-14 16:07:00 -07001979 }
1980
1981 // Private constructor on direct flags value.
1982 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1983
1984 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001985};
1986
David Brazdiled596192015-01-23 10:39:45 +00001987// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001988class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001989 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001990 ALWAYS_INLINE HEnvironment(ArenaAllocator* allocator,
Mingyao Yang01b47b02017-02-03 12:09:57 -08001991 size_t number_of_vregs,
1992 ArtMethod* method,
1993 uint32_t dex_pc,
1994 HInstruction* holder)
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001995 : vregs_(number_of_vregs, allocator->Adapter(kArenaAllocEnvironmentVRegs)),
1996 locations_(allocator->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001997 parent_(nullptr),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001998 method_(method),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001999 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002000 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002001 }
2002
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002003 ALWAYS_INLINE HEnvironment(ArenaAllocator* allocator,
2004 const HEnvironment& to_copy,
2005 HInstruction* holder)
2006 : HEnvironment(allocator,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002007 to_copy.Size(),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00002008 to_copy.GetMethod(),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002009 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002010 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002011
Vladimir Markoec32f642017-06-02 10:51:55 +01002012 void AllocateLocations() {
2013 DCHECK(locations_.empty());
2014 locations_.resize(vregs_.size());
2015 }
2016
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002017 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002018 if (parent_ != nullptr) {
2019 parent_->SetAndCopyParentChain(allocator, parent);
2020 } else {
2021 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
2022 parent_->CopyFrom(parent);
2023 if (parent->GetParent() != nullptr) {
2024 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
2025 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002026 }
David Brazdiled596192015-01-23 10:39:45 +00002027 }
2028
Vladimir Marko69d310e2017-10-09 14:12:23 +01002029 void CopyFrom(ArrayRef<HInstruction* const> locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01002030 void CopyFrom(HEnvironment* environment);
2031
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002032 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
2033 // input to the loop phi instead. This is for inserting instructions that
2034 // require an environment (like HDeoptimization) in the loop pre-header.
2035 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00002036
2037 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01002038 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00002039 }
2040
2041 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01002042 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00002043 }
2044
David Brazdil1abb4192015-02-17 18:33:36 +00002045 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00002046
Artem Serovca210e32017-12-15 13:43:20 +00002047 // Replaces the input at the position 'index' with the replacement; the replacement and old
2048 // input instructions' env_uses_ lists are adjusted. The function works similar to
2049 // HInstruction::ReplaceInput.
2050 void ReplaceInput(HInstruction* replacement, size_t index);
2051
Vladimir Markofa6b93c2015-09-15 10:15:55 +01002052 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00002053
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002054 HEnvironment* GetParent() const { return parent_; }
2055
2056 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01002057 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002058 }
2059
2060 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01002061 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002062 }
2063
2064 uint32_t GetDexPc() const {
2065 return dex_pc_;
2066 }
2067
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00002068 ArtMethod* GetMethod() const {
2069 return method_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002070 }
2071
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002072 HInstruction* GetHolder() const {
2073 return holder_;
2074 }
2075
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00002076
2077 bool IsFromInlinedInvoke() const {
2078 return GetParent() != nullptr;
2079 }
2080
David Brazdiled596192015-01-23 10:39:45 +00002081 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01002082 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
2083 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002084 HEnvironment* parent_;
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00002085 ArtMethod* method_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002086 const uint32_t dex_pc_;
David Brazdiled596192015-01-23 10:39:45 +00002087
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01002088 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002089 HInstruction* const holder_;
2090
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01002091 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00002092
2093 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
2094};
2095
Vladimir Markof9f64412015-09-02 14:05:49 +01002096class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002097 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302098#define DECLARE_KIND(type, super) k##type,
2099 enum InstructionKind {
Vladimir Markoe3946222018-05-04 14:18:47 +01002100 FOR_EACH_CONCRETE_INSTRUCTION(DECLARE_KIND)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302101 kLastInstructionKind
2102 };
2103#undef DECLARE_KIND
2104
2105 HInstruction(InstructionKind kind, SideEffects side_effects, uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01002106 : HInstruction(kind, DataType::Type::kVoid, side_effects, dex_pc) {}
2107
2108 HInstruction(InstructionKind kind, DataType::Type type, SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002109 : previous_(nullptr),
2110 next_(nullptr),
2111 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002112 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002113 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002114 ssa_index_(-1),
Vladimir Markoa1de9182016-02-25 11:37:38 +00002115 packed_fields_(0u),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002116 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002117 locations_(nullptr),
2118 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002119 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00002120 side_effects_(side_effects),
Vladimir Markoa1de9182016-02-25 11:37:38 +00002121 reference_type_handle_(ReferenceTypeInfo::CreateInvalid().GetTypeHandle()) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302122 SetPackedField<InstructionKindField>(kind);
Vladimir Markobd785672018-05-03 17:09:09 +01002123 SetPackedField<TypeField>(type);
Vladimir Markoa1de9182016-02-25 11:37:38 +00002124 SetPackedFlag<kFlagReferenceTypeIsExact>(ReferenceTypeInfo::CreateInvalid().IsExact());
2125 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002126
Dave Allison20dfc792014-06-16 20:44:29 -07002127 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002128
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002129
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002130 HInstruction* GetNext() const { return next_; }
2131 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002132
Calin Juravle77520bc2015-01-12 18:45:46 +00002133 HInstruction* GetNextDisregardingMoves() const;
2134 HInstruction* GetPreviousDisregardingMoves() const;
2135
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002136 HBasicBlock* GetBlock() const { return block_; }
Vladimir Markoca6fff82017-10-03 14:49:14 +01002137 ArenaAllocator* GetAllocator() const { return block_->GetGraph()->GetAllocator(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002138 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002139 bool IsInBlock() const { return block_ != nullptr; }
2140 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00002141 bool IsLoopHeaderPhi() const { return IsPhi() && block_->IsLoopHeader(); }
2142 bool IsIrreducibleLoopHeaderPhi() const {
2143 return IsLoopHeaderPhi() && GetBlock()->GetLoopInformation()->IsIrreducible();
2144 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002145
Vladimir Marko372f10e2016-05-17 16:30:10 +01002146 virtual ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() = 0;
2147
2148 ArrayRef<const HUserRecord<HInstruction*>> GetInputRecords() const {
2149 // One virtual method is enough, just const_cast<> and then re-add the const.
2150 return ArrayRef<const HUserRecord<HInstruction*>>(
2151 const_cast<HInstruction*>(this)->GetInputRecords());
2152 }
2153
Vladimir Markoe9004912016-06-16 16:50:52 +01002154 HInputsRef GetInputs() {
2155 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01002156 }
2157
Vladimir Markoe9004912016-06-16 16:50:52 +01002158 HConstInputsRef GetInputs() const {
2159 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01002160 }
2161
2162 size_t InputCount() const { return GetInputRecords().size(); }
David Brazdil1abb4192015-02-17 18:33:36 +00002163 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002164
Aart Bik2767f4b2016-10-28 15:03:53 -07002165 bool HasInput(HInstruction* input) const {
2166 for (const HInstruction* i : GetInputs()) {
2167 if (i == input) {
2168 return true;
2169 }
2170 }
2171 return false;
2172 }
2173
Vladimir Marko372f10e2016-05-17 16:30:10 +01002174 void SetRawInputAt(size_t index, HInstruction* input) {
2175 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
2176 }
2177
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002178 virtual void Accept(HGraphVisitor* visitor) = 0;
2179 virtual const char* DebugName() const = 0;
2180
Vladimir Markobd785672018-05-03 17:09:09 +01002181 DataType::Type GetType() const {
2182 return TypeField::Decode(GetPackedFields());
2183 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002184
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002185 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002186
2187 uint32_t GetDexPc() const { return dex_pc_; }
2188
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002189 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01002190
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002191 // Can the instruction throw?
2192 // TODO: We should rename to CanVisiblyThrow, as some instructions (like HNewInstance),
2193 // could throw OOME, but it is still OK to remove them if they are unused.
Roland Levillaine161a2a2014-10-03 12:45:18 +01002194 virtual bool CanThrow() const { return false; }
Aart Bika8b8e9b2018-01-09 11:01:02 -08002195
2196 // Does the instruction always throw an exception unconditionally?
2197 virtual bool AlwaysThrows() const { return false; }
2198
David Brazdilec16f792015-08-19 15:04:01 +01002199 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07002200
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002201 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07002202 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002203
Calin Juravle10e244f2015-01-26 18:54:32 +00002204 // Does not apply for all instructions, but having this at top level greatly
2205 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00002206 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002207 virtual bool CanBeNull() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002208 DCHECK_EQ(GetType(), DataType::Type::kReference) << "CanBeNull only applies to reference types";
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002209 return true;
2210 }
Calin Juravle10e244f2015-01-26 18:54:32 +00002211
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002212 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01002213 return false;
2214 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002215
Nicolas Geoffray61ba8d22018-08-07 09:55:57 +01002216 // If this instruction will do an implicit null check, return the `HNullCheck` associated
2217 // with it. Otherwise return null.
2218 HNullCheck* GetImplicitNullCheck() const {
Vladimir Markoee1f1262019-06-26 14:37:17 +01002219 // Go over previous non-move instructions that are emitted at use site.
2220 HInstruction* prev_not_move = GetPreviousDisregardingMoves();
2221 while (prev_not_move != nullptr && prev_not_move->IsEmittedAtUseSite()) {
2222 if (prev_not_move->IsNullCheck()) {
2223 return prev_not_move->AsNullCheck();
2224 }
2225 prev_not_move = prev_not_move->GetPreviousDisregardingMoves();
Nicolas Geoffray61ba8d22018-08-07 09:55:57 +01002226 }
2227 return nullptr;
2228 }
2229
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00002230 virtual bool IsActualObject() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002231 return GetType() == DataType::Type::kReference;
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00002232 }
2233
Calin Juravle2e768302015-07-28 14:41:11 +00002234 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00002235
Calin Juravle61d544b2015-02-23 16:46:57 +00002236 ReferenceTypeInfo GetReferenceTypeInfo() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002237 DCHECK_EQ(GetType(), DataType::Type::kReference);
Vladimir Markoa1de9182016-02-25 11:37:38 +00002238 return ReferenceTypeInfo::CreateUnchecked(reference_type_handle_,
Vladimir Marko456307a2016-04-19 14:12:13 +00002239 GetPackedFlag<kFlagReferenceTypeIsExact>());
Calin Juravle61d544b2015-02-23 16:46:57 +00002240 }
Calin Juravleacf735c2015-02-12 15:25:22 +00002241
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002242 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00002243 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01002244 // Note: fixup_end remains valid across push_front().
2245 auto fixup_end = uses_.empty() ? uses_.begin() : ++uses_.begin();
2246 HUseListNode<HInstruction*>* new_node =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002247 new (GetBlock()->GetGraph()->GetAllocator()) HUseListNode<HInstruction*>(user, index);
Vladimir Marko46817b82016-03-29 12:21:58 +01002248 uses_.push_front(*new_node);
2249 FixUpUserRecordsAfterUseInsertion(fixup_end);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002250 }
2251
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002252 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01002253 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01002254 // Note: env_fixup_end remains valid across push_front().
2255 auto env_fixup_end = env_uses_.empty() ? env_uses_.begin() : ++env_uses_.begin();
2256 HUseListNode<HEnvironment*>* new_node =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002257 new (GetBlock()->GetGraph()->GetAllocator()) HUseListNode<HEnvironment*>(user, index);
Vladimir Marko46817b82016-03-29 12:21:58 +01002258 env_uses_.push_front(*new_node);
2259 FixUpUserRecordsAfterEnvUseInsertion(env_fixup_end);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002260 }
2261
David Brazdil1abb4192015-02-17 18:33:36 +00002262 void RemoveAsUserOfInput(size_t input) {
2263 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
Vladimir Marko46817b82016-03-29 12:21:58 +01002264 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
2265 input_use.GetInstruction()->uses_.erase_after(before_use_node);
2266 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
David Brazdil1abb4192015-02-17 18:33:36 +00002267 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002268
Vladimir Marko372f10e2016-05-17 16:30:10 +01002269 void RemoveAsUserOfAllInputs() {
2270 for (const HUserRecord<HInstruction*>& input_use : GetInputRecords()) {
2271 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
2272 input_use.GetInstruction()->uses_.erase_after(before_use_node);
2273 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
2274 }
2275 }
2276
David Brazdil1abb4192015-02-17 18:33:36 +00002277 const HUseList<HInstruction*>& GetUses() const { return uses_; }
2278 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002279
Vladimir Marko46817b82016-03-29 12:21:58 +01002280 bool HasUses() const { return !uses_.empty() || !env_uses_.empty(); }
2281 bool HasEnvironmentUses() const { return !env_uses_.empty(); }
2282 bool HasNonEnvironmentUses() const { return !uses_.empty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01002283 bool HasOnlyOneNonEnvironmentUse() const {
Vladimir Marko46817b82016-03-29 12:21:58 +01002284 return !HasEnvironmentUses() && GetUses().HasExactlyOneElement();
Alexandre Rames188d4312015-04-09 18:30:21 +01002285 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002286
Aart Bikcc42be02016-10-20 16:14:16 -07002287 bool IsRemovable() const {
Aart Bik482095d2016-10-10 15:39:10 -07002288 return
Aart Bikff7d89c2016-11-07 08:49:28 -08002289 !DoesAnyWrite() &&
Aart Bik482095d2016-10-10 15:39:10 -07002290 !CanThrow() &&
2291 !IsSuspendCheck() &&
2292 !IsControlFlow() &&
2293 !IsNativeDebugInfo() &&
2294 !IsParameterValue() &&
Aart Bik482095d2016-10-10 15:39:10 -07002295 // If we added an explicit barrier then we should keep it.
Igor Murashkind01745e2017-04-05 16:40:31 -07002296 !IsMemoryBarrier() &&
2297 !IsConstructorFence();
Aart Bik482095d2016-10-10 15:39:10 -07002298 }
2299
Aart Bikcc42be02016-10-20 16:14:16 -07002300 bool IsDeadAndRemovable() const {
2301 return IsRemovable() && !HasUses();
2302 }
2303
Roland Levillain6c82d402014-10-13 16:10:27 +01002304 // Does this instruction strictly dominate `other_instruction`?
2305 // Returns false if this instruction and `other_instruction` are the same.
2306 // Aborts if this instruction and `other_instruction` are both phis.
2307 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01002308
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002309 int GetId() const { return id_; }
2310 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002311
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002312 int GetSsaIndex() const { return ssa_index_; }
2313 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
2314 bool HasSsaIndex() const { return ssa_index_ != -1; }
2315
2316 bool HasEnvironment() const { return environment_ != nullptr; }
2317 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002318 // Set the `environment_` field. Raw because this method does not
2319 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002320 void SetRawEnvironment(HEnvironment* environment) {
2321 DCHECK(environment_ == nullptr);
2322 DCHECK_EQ(environment->GetHolder(), this);
2323 environment_ = environment;
2324 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002325
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002326 void InsertRawEnvironment(HEnvironment* environment) {
2327 DCHECK(environment_ != nullptr);
2328 DCHECK_EQ(environment->GetHolder(), this);
2329 DCHECK(environment->GetParent() == nullptr);
2330 environment->parent_ = environment_;
2331 environment_ = environment;
2332 }
2333
Vladimir Markocac5a7e2016-02-22 10:39:50 +00002334 void RemoveEnvironment();
2335
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002336 // Set the environment of this instruction, copying it from `environment`. While
2337 // copying, the uses lists are being updated.
2338 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002339 DCHECK(environment_ == nullptr);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002340 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetAllocator();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002341 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002342 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002343 if (environment->GetParent() != nullptr) {
2344 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2345 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002346 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002347
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002348 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
2349 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002350 DCHECK(environment_ == nullptr);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002351 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetAllocator();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002352 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002353 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002354 if (environment->GetParent() != nullptr) {
2355 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2356 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002357 }
2358
Nicolas Geoffray39468442014-09-02 15:17:15 +01002359 // Returns the number of entries in the environment. Typically, that is the
2360 // number of dex registers in a method. It could be more in case of inlining.
2361 size_t EnvironmentSize() const;
2362
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002363 LocationSummary* GetLocations() const { return locations_; }
2364 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002365
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002366 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00002367 void ReplaceUsesDominatedBy(HInstruction* dominator, HInstruction* replacement);
Nicolas Geoffray8a62a4c2018-07-03 09:39:07 +01002368 void ReplaceEnvUsesDominatedBy(HInstruction* dominator, HInstruction* replacement);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002369 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002370
Alexandre Rames188d4312015-04-09 18:30:21 +01002371 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
2372 // uses of this instruction by `other` are *not* updated.
2373 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
2374 ReplaceWith(other);
2375 other->ReplaceInput(this, use_index);
2376 }
2377
Alexandre Rames22aa54b2016-10-18 09:32:29 +01002378 // Move `this` instruction before `cursor`
2379 void MoveBefore(HInstruction* cursor, bool do_checks = true);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002380
Vladimir Markofb337ea2015-11-25 15:25:10 +00002381 // Move `this` before its first user and out of any loops. If there is no
2382 // out-of-loop user that dominates all other users, move the instruction
2383 // to the end of the out-of-loop common dominator of the user's blocks.
2384 //
2385 // This can be used only on non-throwing instructions with no side effects that
2386 // have at least one use but no environment uses.
2387 void MoveBeforeFirstUserAndOutOfLoops();
2388
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002389#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Markoa90dd512018-05-04 15:04:45 +01002390 bool Is##type() const;
2391
2392 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
2393#undef INSTRUCTION_TYPE_CHECK
2394
2395#define INSTRUCTION_TYPE_CAST(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002396 const H##type* As##type() const; \
2397 H##type* As##type();
2398
Vladimir Markoa90dd512018-05-04 15:04:45 +01002399 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CAST)
2400#undef INSTRUCTION_TYPE_CAST
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002401
Artem Serovcced8ba2017-07-19 18:18:09 +01002402 // Return a clone of the instruction if it is clonable (shallow copy by default, custom copy
2403 // if a custom copy-constructor is provided for a particular type). If IsClonable() is false for
2404 // the instruction then the behaviour of this function is undefined.
2405 //
2406 // Note: It is semantically valid to create a clone of the instruction only until
2407 // prepare_for_register_allocator phase as lifetime, intervals and codegen info are not
2408 // copied.
2409 //
2410 // Note: HEnvironment and some other fields are not copied and are set to default values, see
2411 // 'explicit HInstruction(const HInstruction& other)' for details.
2412 virtual HInstruction* Clone(ArenaAllocator* arena ATTRIBUTE_UNUSED) const {
2413 LOG(FATAL) << "Cloning is not implemented for the instruction " <<
2414 DebugName() << " " << GetId();
2415 UNREACHABLE();
2416 }
2417
2418 // Return whether instruction can be cloned (copied).
2419 virtual bool IsClonable() const { return false; }
2420
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002421 // Returns whether the instruction can be moved within the graph.
Aart Bik71bf7b42016-11-16 10:17:46 -08002422 // TODO: this method is used by LICM and GVN with possibly different
2423 // meanings? split and rename?
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002424 virtual bool CanBeMoved() const { return false; }
2425
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002426 // Returns whether any data encoded in the two instructions is equal.
2427 // This method does not look at the inputs. Both instructions must be
2428 // of the same type, otherwise the method has undefined behavior.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002429 virtual bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002430 return false;
2431 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002432
2433 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00002434 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002435 // 2) Their inputs are identical.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002436 bool Equals(const HInstruction* other) const;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002437
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302438 InstructionKind GetKind() const { return GetPackedField<InstructionKindField>(); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002439
2440 virtual size_t ComputeHashCode() const {
2441 size_t result = GetKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01002442 for (const HInstruction* input : GetInputs()) {
2443 result = (result * 31) + input->GetId();
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002444 }
2445 return result;
2446 }
2447
2448 SideEffects GetSideEffects() const { return side_effects_; }
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00002449 void SetSideEffects(SideEffects other) { side_effects_ = other; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002450 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002451
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002452 size_t GetLifetimePosition() const { return lifetime_position_; }
2453 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2454 LiveInterval* GetLiveInterval() const { return live_interval_; }
2455 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2456 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2457
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002458 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2459
2460 // Returns whether the code generation of the instruction will require to have access
2461 // to the current method. Such instructions are:
2462 // (1): Instructions that require an environment, as calling the runtime requires
2463 // to walk the stack and have the current method stored at a specific stack address.
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002464 // (2): HCurrentMethod, potentially used by HInvokeStaticOrDirect, HLoadString, or HLoadClass
2465 // to access the dex cache.
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002466 bool NeedsCurrentMethod() const {
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002467 return NeedsEnvironment() || IsCurrentMethod();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002468 }
2469
Vladimir Markodc151b22015-10-15 18:02:30 +01002470 // Returns whether the code generation of the instruction will require to have access
2471 // to the dex cache of the current method's declaring class via the current method.
2472 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002473
Mark Mendellc4701932015-04-10 13:18:51 -04002474 // Does this instruction have any use in an environment before
2475 // control flow hits 'other'?
2476 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2477
2478 // Remove all references to environment uses of this instruction.
2479 // The caller must ensure that this is safe to do.
2480 void RemoveEnvironmentUsers();
2481
Vladimir Markoa1de9182016-02-25 11:37:38 +00002482 bool IsEmittedAtUseSite() const { return GetPackedFlag<kFlagEmittedAtUseSite>(); }
2483 void MarkEmittedAtUseSite() { SetPackedFlag<kFlagEmittedAtUseSite>(true); }
David Brazdilb3e773e2016-01-26 11:28:37 +00002484
David Brazdil1abb4192015-02-17 18:33:36 +00002485 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002486 // If set, the machine code for this instruction is assumed to be generated by
2487 // its users. Used by liveness analysis to compute use positions accordingly.
2488 static constexpr size_t kFlagEmittedAtUseSite = 0u;
2489 static constexpr size_t kFlagReferenceTypeIsExact = kFlagEmittedAtUseSite + 1;
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302490 static constexpr size_t kFieldInstructionKind = kFlagReferenceTypeIsExact + 1;
2491 static constexpr size_t kFieldInstructionKindSize =
2492 MinimumBitsToStore(static_cast<size_t>(InstructionKind::kLastInstructionKind - 1));
Vladimir Markobd785672018-05-03 17:09:09 +01002493 static constexpr size_t kFieldType =
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302494 kFieldInstructionKind + kFieldInstructionKindSize;
Vladimir Markobd785672018-05-03 17:09:09 +01002495 static constexpr size_t kFieldTypeSize =
2496 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
2497 static constexpr size_t kNumberOfGenericPackedBits = kFieldType + kFieldTypeSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00002498 static constexpr size_t kMaxNumberOfPackedBits = sizeof(uint32_t) * kBitsPerByte;
2499
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302500 static_assert(kNumberOfGenericPackedBits <= kMaxNumberOfPackedBits,
2501 "Too many generic packed fields");
2502
Vladimir Markobd785672018-05-03 17:09:09 +01002503 using TypeField = BitField<DataType::Type, kFieldType, kFieldTypeSize>;
2504
Vladimir Marko372f10e2016-05-17 16:30:10 +01002505 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const {
2506 return GetInputRecords()[i];
2507 }
2508
2509 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) {
2510 ArrayRef<HUserRecord<HInstruction*>> input_records = GetInputRecords();
2511 input_records[index] = input;
2512 }
David Brazdil1abb4192015-02-17 18:33:36 +00002513
Vladimir Markoa1de9182016-02-25 11:37:38 +00002514 uint32_t GetPackedFields() const {
2515 return packed_fields_;
2516 }
2517
2518 template <size_t flag>
2519 bool GetPackedFlag() const {
2520 return (packed_fields_ & (1u << flag)) != 0u;
2521 }
2522
2523 template <size_t flag>
2524 void SetPackedFlag(bool value = true) {
2525 packed_fields_ = (packed_fields_ & ~(1u << flag)) | ((value ? 1u : 0u) << flag);
2526 }
2527
2528 template <typename BitFieldType>
2529 typename BitFieldType::value_type GetPackedField() const {
2530 return BitFieldType::Decode(packed_fields_);
2531 }
2532
2533 template <typename BitFieldType>
2534 void SetPackedField(typename BitFieldType::value_type value) {
2535 DCHECK(IsUint<BitFieldType::size>(static_cast<uintptr_t>(value)));
2536 packed_fields_ = BitFieldType::Update(value, packed_fields_);
2537 }
2538
Artem Serovcced8ba2017-07-19 18:18:09 +01002539 // Copy construction for the instruction (used for Clone function).
2540 //
2541 // Fields (e.g. lifetime, intervals and codegen info) associated with phases starting from
2542 // prepare_for_register_allocator are not copied (set to default values).
2543 //
2544 // Copy constructors must be provided for every HInstruction type; default copy constructor is
2545 // fine for most of them. However for some of the instructions a custom copy constructor must be
2546 // specified (when instruction has non-trivially copyable fields and must have a special behaviour
2547 // for copying them).
2548 explicit HInstruction(const HInstruction& other)
2549 : previous_(nullptr),
2550 next_(nullptr),
2551 block_(nullptr),
2552 dex_pc_(other.dex_pc_),
2553 id_(-1),
2554 ssa_index_(-1),
2555 packed_fields_(other.packed_fields_),
2556 environment_(nullptr),
2557 locations_(nullptr),
2558 live_interval_(nullptr),
2559 lifetime_position_(kNoLifetime),
2560 side_effects_(other.side_effects_),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302561 reference_type_handle_(other.reference_type_handle_) {
2562 }
Artem Serovcced8ba2017-07-19 18:18:09 +01002563
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002564 private:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302565 using InstructionKindField =
2566 BitField<InstructionKind, kFieldInstructionKind, kFieldInstructionKindSize>;
2567
Vladimir Marko46817b82016-03-29 12:21:58 +01002568 void FixUpUserRecordsAfterUseInsertion(HUseList<HInstruction*>::iterator fixup_end) {
2569 auto before_use_node = uses_.before_begin();
2570 for (auto use_node = uses_.begin(); use_node != fixup_end; ++use_node) {
2571 HInstruction* user = use_node->GetUser();
2572 size_t input_index = use_node->GetIndex();
2573 user->SetRawInputRecordAt(input_index, HUserRecord<HInstruction*>(this, before_use_node));
2574 before_use_node = use_node;
2575 }
2576 }
2577
2578 void FixUpUserRecordsAfterUseRemoval(HUseList<HInstruction*>::iterator before_use_node) {
2579 auto next = ++HUseList<HInstruction*>::iterator(before_use_node);
2580 if (next != uses_.end()) {
2581 HInstruction* next_user = next->GetUser();
2582 size_t next_index = next->GetIndex();
2583 DCHECK(next_user->InputRecordAt(next_index).GetInstruction() == this);
2584 next_user->SetRawInputRecordAt(next_index, HUserRecord<HInstruction*>(this, before_use_node));
2585 }
2586 }
2587
2588 void FixUpUserRecordsAfterEnvUseInsertion(HUseList<HEnvironment*>::iterator env_fixup_end) {
2589 auto before_env_use_node = env_uses_.before_begin();
2590 for (auto env_use_node = env_uses_.begin(); env_use_node != env_fixup_end; ++env_use_node) {
2591 HEnvironment* user = env_use_node->GetUser();
2592 size_t input_index = env_use_node->GetIndex();
2593 user->vregs_[input_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2594 before_env_use_node = env_use_node;
2595 }
2596 }
2597
2598 void FixUpUserRecordsAfterEnvUseRemoval(HUseList<HEnvironment*>::iterator before_env_use_node) {
2599 auto next = ++HUseList<HEnvironment*>::iterator(before_env_use_node);
2600 if (next != env_uses_.end()) {
2601 HEnvironment* next_user = next->GetUser();
2602 size_t next_index = next->GetIndex();
2603 DCHECK(next_user->vregs_[next_index].GetInstruction() == this);
2604 next_user->vregs_[next_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2605 }
2606 }
David Brazdil1abb4192015-02-17 18:33:36 +00002607
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002608 HInstruction* previous_;
2609 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002610 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002611 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002612
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002613 // An instruction gets an id when it is added to the graph.
2614 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002615 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002616 int id_;
2617
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002618 // When doing liveness analysis, instructions that have uses get an SSA index.
2619 int ssa_index_;
2620
Vladimir Markoa1de9182016-02-25 11:37:38 +00002621 // Packed fields.
2622 uint32_t packed_fields_;
David Brazdilb3e773e2016-01-26 11:28:37 +00002623
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002624 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002625 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002626
2627 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002628 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002629
Nicolas Geoffray39468442014-09-02 15:17:15 +01002630 // The environment associated with this instruction. Not null if the instruction
2631 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002632 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002633
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002634 // Set by the code generator.
2635 LocationSummary* locations_;
2636
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002637 // Set by the liveness analysis.
2638 LiveInterval* live_interval_;
2639
2640 // Set by the liveness analysis, this is the position in a linear
2641 // order of blocks where this instruction's live interval start.
2642 size_t lifetime_position_;
2643
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002644 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002645
Vladimir Markoa1de9182016-02-25 11:37:38 +00002646 // The reference handle part of the reference type info.
2647 // The IsExact() flag is stored in packed fields.
Calin Juravleacf735c2015-02-12 15:25:22 +00002648 // TODO: for primitive types this should be marked as invalid.
Vladimir Markoa1de9182016-02-25 11:37:38 +00002649 ReferenceTypeInfo::TypeHandle reference_type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00002650
David Brazdil1abb4192015-02-17 18:33:36 +00002651 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002652 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002653 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002654 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002655 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002656};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002657std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002658
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002659// Iterates over the instructions, while preserving the next instruction
2660// in case the current instruction gets removed from the list by the user
2661// of this iterator.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002662class HInstructionIterator : public ValueObject {
2663 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002664 explicit HInstructionIterator(const HInstructionList& instructions)
2665 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002666 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002667 }
2668
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002669 bool Done() const { return instruction_ == nullptr; }
2670 HInstruction* Current() const { return instruction_; }
2671 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002672 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002673 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002674 }
2675
2676 private:
2677 HInstruction* instruction_;
2678 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002679
2680 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002681};
2682
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002683// Iterates over the instructions without saving the next instruction,
2684// therefore handling changes in the graph potentially made by the user
2685// of this iterator.
2686class HInstructionIteratorHandleChanges : public ValueObject {
2687 public:
2688 explicit HInstructionIteratorHandleChanges(const HInstructionList& instructions)
2689 : instruction_(instructions.first_instruction_) {
2690 }
2691
2692 bool Done() const { return instruction_ == nullptr; }
2693 HInstruction* Current() const { return instruction_; }
2694 void Advance() {
2695 instruction_ = instruction_->GetNext();
2696 }
2697
2698 private:
2699 HInstruction* instruction_;
2700
2701 DISALLOW_COPY_AND_ASSIGN(HInstructionIteratorHandleChanges);
2702};
2703
2704
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002705class HBackwardInstructionIterator : public ValueObject {
2706 public:
2707 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2708 : instruction_(instructions.last_instruction_) {
2709 next_ = Done() ? nullptr : instruction_->GetPrevious();
2710 }
2711
2712 bool Done() const { return instruction_ == nullptr; }
2713 HInstruction* Current() const { return instruction_; }
2714 void Advance() {
2715 instruction_ = next_;
2716 next_ = Done() ? nullptr : instruction_->GetPrevious();
2717 }
2718
2719 private:
2720 HInstruction* instruction_;
2721 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002722
2723 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002724};
2725
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002726class HVariableInputSizeInstruction : public HInstruction {
2727 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08002728 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002729 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() override {
Mingyao Yangb0b051a2016-11-17 09:04:53 -08002730 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
2731 }
2732
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002733 void AddInput(HInstruction* input);
2734 void InsertInputAt(size_t index, HInstruction* input);
2735 void RemoveInputAt(size_t index);
2736
Igor Murashkind01745e2017-04-05 16:40:31 -07002737 // Removes all the inputs.
2738 // Also removes this instructions from each input's use list
2739 // (for non-environment uses only).
2740 void RemoveAllInputs();
2741
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002742 protected:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302743 HVariableInputSizeInstruction(InstructionKind inst_kind,
2744 SideEffects side_effects,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002745 uint32_t dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002746 ArenaAllocator* allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002747 size_t number_of_inputs,
2748 ArenaAllocKind kind)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302749 : HInstruction(inst_kind, side_effects, dex_pc),
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002750 inputs_(number_of_inputs, allocator->Adapter(kind)) {}
Vladimir Markobd785672018-05-03 17:09:09 +01002751 HVariableInputSizeInstruction(InstructionKind inst_kind,
2752 DataType::Type type,
2753 SideEffects side_effects,
2754 uint32_t dex_pc,
2755 ArenaAllocator* allocator,
2756 size_t number_of_inputs,
2757 ArenaAllocKind kind)
2758 : HInstruction(inst_kind, type, side_effects, dex_pc),
2759 inputs_(number_of_inputs, allocator->Adapter(kind)) {}
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002760
Artem Serovcced8ba2017-07-19 18:18:09 +01002761 DEFAULT_COPY_CONSTRUCTOR(VariableInputSizeInstruction);
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002762
Artem Serovcced8ba2017-07-19 18:18:09 +01002763 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002764};
2765
Vladimir Markof9f64412015-09-02 14:05:49 +01002766template<size_t N>
Vladimir Markobd785672018-05-03 17:09:09 +01002767class HExpression : public HInstruction {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002768 public:
Vladimir Markobd785672018-05-03 17:09:09 +01002769 HExpression<N>(InstructionKind kind, SideEffects side_effects, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302770 : HInstruction(kind, side_effects, dex_pc), inputs_() {}
Vladimir Markobd785672018-05-03 17:09:09 +01002771 HExpression<N>(InstructionKind kind,
2772 DataType::Type type,
2773 SideEffects side_effects,
2774 uint32_t dex_pc)
2775 : HInstruction(kind, type, side_effects, dex_pc), inputs_() {}
2776 virtual ~HExpression() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002777
Vladimir Marko372f10e2016-05-17 16:30:10 +01002778 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01002779 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Vladimir Marko372f10e2016-05-17 16:30:10 +01002780 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002781 }
2782
Artem Serovcced8ba2017-07-19 18:18:09 +01002783 protected:
Vladimir Markobd785672018-05-03 17:09:09 +01002784 DEFAULT_COPY_CONSTRUCTOR(Expression<N>);
Artem Serovcced8ba2017-07-19 18:18:09 +01002785
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002786 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002787 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002788
2789 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002790};
2791
Vladimir Markobd785672018-05-03 17:09:09 +01002792// HExpression specialization for N=0.
Vladimir Markof9f64412015-09-02 14:05:49 +01002793template<>
Vladimir Markobd785672018-05-03 17:09:09 +01002794class HExpression<0> : public HInstruction {
Vladimir Markof9f64412015-09-02 14:05:49 +01002795 public:
Vladimir Markobd785672018-05-03 17:09:09 +01002796 using HInstruction::HInstruction;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002797
Vladimir Markobd785672018-05-03 17:09:09 +01002798 virtual ~HExpression() {}
Vladimir Markof9f64412015-09-02 14:05:49 +01002799
Vladimir Marko372f10e2016-05-17 16:30:10 +01002800 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01002801 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Vladimir Marko372f10e2016-05-17 16:30:10 +01002802 return ArrayRef<HUserRecord<HInstruction*>>();
Vladimir Markof9f64412015-09-02 14:05:49 +01002803 }
2804
Artem Serovcced8ba2017-07-19 18:18:09 +01002805 protected:
Vladimir Markobd785672018-05-03 17:09:09 +01002806 DEFAULT_COPY_CONSTRUCTOR(Expression<0>);
Artem Serovcced8ba2017-07-19 18:18:09 +01002807
Vladimir Markof9f64412015-09-02 14:05:49 +01002808 private:
2809 friend class SsaBuilder;
2810};
2811
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002812// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2813// instruction that branches to the exit block.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002814class HReturnVoid final : public HExpression<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002815 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002816 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01002817 : HExpression(kReturnVoid, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302818 }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002819
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002820 bool IsControlFlow() const override { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002821
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002822 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002823
Artem Serovcced8ba2017-07-19 18:18:09 +01002824 protected:
2825 DEFAULT_COPY_CONSTRUCTOR(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002826};
2827
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002828// Represents dex's RETURN opcodes. A HReturn is a control flow
2829// instruction that branches to the exit block.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002830class HReturn final : public HExpression<1> {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002831 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002832 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01002833 : HExpression(kReturn, SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002834 SetRawInputAt(0, value);
2835 }
2836
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002837 bool IsControlFlow() const override { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002838
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002839 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002840
Artem Serovcced8ba2017-07-19 18:18:09 +01002841 protected:
2842 DEFAULT_COPY_CONSTRUCTOR(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002843};
2844
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002845class HPhi final : public HVariableInputSizeInstruction {
David Brazdildee58d62016-04-07 09:54:26 +00002846 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002847 HPhi(ArenaAllocator* allocator,
David Brazdildee58d62016-04-07 09:54:26 +00002848 uint32_t reg_number,
2849 size_t number_of_inputs,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002850 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +00002851 uint32_t dex_pc = kNoDexPc)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002852 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302853 kPhi,
Vladimir Markobd785672018-05-03 17:09:09 +01002854 ToPhiType(type),
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002855 SideEffects::None(),
2856 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002857 allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002858 number_of_inputs,
2859 kArenaAllocPhiInputs),
David Brazdildee58d62016-04-07 09:54:26 +00002860 reg_number_(reg_number) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002861 DCHECK_NE(GetType(), DataType::Type::kVoid);
David Brazdildee58d62016-04-07 09:54:26 +00002862 // Phis are constructed live and marked dead if conflicting or unused.
2863 // Individual steps of SsaBuilder should assume that if a phi has been
2864 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
2865 SetPackedFlag<kFlagIsLive>(true);
2866 SetPackedFlag<kFlagCanBeNull>(true);
2867 }
2868
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002869 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01002870
David Brazdildee58d62016-04-07 09:54:26 +00002871 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002872 static DataType::Type ToPhiType(DataType::Type type) {
2873 return DataType::Kind(type);
David Brazdildee58d62016-04-07 09:54:26 +00002874 }
2875
2876 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
2877
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002878 void SetType(DataType::Type new_type) {
David Brazdildee58d62016-04-07 09:54:26 +00002879 // Make sure that only valid type changes occur. The following are allowed:
2880 // (1) int -> float/ref (primitive type propagation),
2881 // (2) long -> double (primitive type propagation).
2882 DCHECK(GetType() == new_type ||
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002883 (GetType() == DataType::Type::kInt32 && new_type == DataType::Type::kFloat32) ||
2884 (GetType() == DataType::Type::kInt32 && new_type == DataType::Type::kReference) ||
2885 (GetType() == DataType::Type::kInt64 && new_type == DataType::Type::kFloat64));
David Brazdildee58d62016-04-07 09:54:26 +00002886 SetPackedField<TypeField>(new_type);
2887 }
2888
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002889 bool CanBeNull() const override { return GetPackedFlag<kFlagCanBeNull>(); }
David Brazdildee58d62016-04-07 09:54:26 +00002890 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
2891
2892 uint32_t GetRegNumber() const { return reg_number_; }
2893
2894 void SetDead() { SetPackedFlag<kFlagIsLive>(false); }
2895 void SetLive() { SetPackedFlag<kFlagIsLive>(true); }
2896 bool IsDead() const { return !IsLive(); }
2897 bool IsLive() const { return GetPackedFlag<kFlagIsLive>(); }
2898
Vladimir Markoe9004912016-06-16 16:50:52 +01002899 bool IsVRegEquivalentOf(const HInstruction* other) const {
David Brazdildee58d62016-04-07 09:54:26 +00002900 return other != nullptr
2901 && other->IsPhi()
2902 && other->AsPhi()->GetBlock() == GetBlock()
2903 && other->AsPhi()->GetRegNumber() == GetRegNumber();
2904 }
2905
Nicolas Geoffrayf57c1ae2017-06-28 17:40:18 +01002906 bool HasEquivalentPhi() const {
2907 if (GetPrevious() != nullptr && GetPrevious()->AsPhi()->GetRegNumber() == GetRegNumber()) {
2908 return true;
2909 }
2910 if (GetNext() != nullptr && GetNext()->AsPhi()->GetRegNumber() == GetRegNumber()) {
2911 return true;
2912 }
2913 return false;
2914 }
2915
David Brazdildee58d62016-04-07 09:54:26 +00002916 // Returns the next equivalent phi (starting from the current one) or null if there is none.
2917 // An equivalent phi is a phi having the same dex register and type.
2918 // It assumes that phis with the same dex register are adjacent.
2919 HPhi* GetNextEquivalentPhiWithSameType() {
2920 HInstruction* next = GetNext();
2921 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
2922 if (next->GetType() == GetType()) {
2923 return next->AsPhi();
2924 }
2925 next = next->GetNext();
2926 }
2927 return nullptr;
2928 }
2929
2930 DECLARE_INSTRUCTION(Phi);
2931
Artem Serovcced8ba2017-07-19 18:18:09 +01002932 protected:
2933 DEFAULT_COPY_CONSTRUCTOR(Phi);
2934
David Brazdildee58d62016-04-07 09:54:26 +00002935 private:
Vladimir Markobd785672018-05-03 17:09:09 +01002936 static constexpr size_t kFlagIsLive = HInstruction::kNumberOfGenericPackedBits;
David Brazdildee58d62016-04-07 09:54:26 +00002937 static constexpr size_t kFlagCanBeNull = kFlagIsLive + 1;
2938 static constexpr size_t kNumberOfPhiPackedBits = kFlagCanBeNull + 1;
2939 static_assert(kNumberOfPhiPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
David Brazdildee58d62016-04-07 09:54:26 +00002940
David Brazdildee58d62016-04-07 09:54:26 +00002941 const uint32_t reg_number_;
David Brazdildee58d62016-04-07 09:54:26 +00002942};
2943
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002944// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002945// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002946// exit block.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002947class HExit final : public HExpression<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002948 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302949 explicit HExit(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01002950 : HExpression(kExit, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302951 }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002952
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002953 bool IsControlFlow() const override { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002954
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002955 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002956
Artem Serovcced8ba2017-07-19 18:18:09 +01002957 protected:
2958 DEFAULT_COPY_CONSTRUCTOR(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002959};
2960
2961// Jumps from one block to another.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002962class HGoto final : public HExpression<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002963 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302964 explicit HGoto(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01002965 : HExpression(kGoto, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302966 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002967
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002968 bool IsClonable() const override { return true; }
2969 bool IsControlFlow() const override { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002970
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002971 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002972 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002973 }
2974
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002975 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002976
Artem Serovcced8ba2017-07-19 18:18:09 +01002977 protected:
2978 DEFAULT_COPY_CONSTRUCTOR(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002979};
2980
Roland Levillain9867bc72015-08-05 10:21:34 +01002981class HConstant : public HExpression<0> {
2982 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302983 explicit HConstant(InstructionKind kind, DataType::Type type, uint32_t dex_pc = kNoDexPc)
2984 : HExpression(kind, type, SideEffects::None(), dex_pc) {
2985 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002986
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002987 bool CanBeMoved() const override { return true; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002988
Roland Levillain1a653882016-03-18 18:05:57 +00002989 // Is this constant -1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002990 virtual bool IsMinusOne() const { return false; }
Roland Levillain1a653882016-03-18 18:05:57 +00002991 // Is this constant 0 in the arithmetic sense?
2992 virtual bool IsArithmeticZero() const { return false; }
2993 // Is this constant a 0-bit pattern?
2994 virtual bool IsZeroBitPattern() const { return false; }
2995 // Is this constant 1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002996 virtual bool IsOne() const { return false; }
2997
David Brazdil77a48ae2015-09-15 12:34:04 +00002998 virtual uint64_t GetValueAsUint64() const = 0;
2999
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003000 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01003001
Artem Serovcced8ba2017-07-19 18:18:09 +01003002 protected:
3003 DEFAULT_COPY_CONSTRUCTOR(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01003004};
3005
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003006class HNullConstant final : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01003007 public:
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003008 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01003009 return true;
3010 }
3011
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003012 uint64_t GetValueAsUint64() const override { return 0; }
David Brazdil77a48ae2015-09-15 12:34:04 +00003013
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003014 size_t ComputeHashCode() const override { return 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01003015
Roland Levillain1a653882016-03-18 18:05:57 +00003016 // The null constant representation is a 0-bit pattern.
Yi Kong39402542019-03-24 02:47:16 -07003017 bool IsZeroBitPattern() const override { return true; }
Roland Levillain1a653882016-03-18 18:05:57 +00003018
Roland Levillain9867bc72015-08-05 10:21:34 +01003019 DECLARE_INSTRUCTION(NullConstant);
3020
Artem Serovcced8ba2017-07-19 18:18:09 +01003021 protected:
3022 DEFAULT_COPY_CONSTRUCTOR(NullConstant);
3023
Roland Levillain9867bc72015-08-05 10:21:34 +01003024 private:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003025 explicit HNullConstant(uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303026 : HConstant(kNullConstant, DataType::Type::kReference, dex_pc) {
3027 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003028
3029 friend class HGraph;
Roland Levillain9867bc72015-08-05 10:21:34 +01003030};
3031
3032// Constants of the type int. Those can be from Dex instructions, or
3033// synthesized (for example with the if-eqz instruction).
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003034class HIntConstant final : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01003035 public:
3036 int32_t GetValue() const { return value_; }
3037
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003038 uint64_t GetValueAsUint64() const override {
David Brazdil9f389d42015-10-01 14:32:56 +01003039 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
3040 }
David Brazdil77a48ae2015-09-15 12:34:04 +00003041
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003042 bool InstructionDataEquals(const HInstruction* other) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003043 DCHECK(other->IsIntConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01003044 return other->AsIntConstant()->value_ == value_;
3045 }
3046
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003047 size_t ComputeHashCode() const override { return GetValue(); }
Roland Levillain9867bc72015-08-05 10:21:34 +01003048
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003049 bool IsMinusOne() const override { return GetValue() == -1; }
3050 bool IsArithmeticZero() const override { return GetValue() == 0; }
3051 bool IsZeroBitPattern() const override { return GetValue() == 0; }
3052 bool IsOne() const override { return GetValue() == 1; }
Roland Levillain9867bc72015-08-05 10:21:34 +01003053
Roland Levillain1a653882016-03-18 18:05:57 +00003054 // Integer constants are used to encode Boolean values as well,
3055 // where 1 means true and 0 means false.
3056 bool IsTrue() const { return GetValue() == 1; }
3057 bool IsFalse() const { return GetValue() == 0; }
3058
Roland Levillain9867bc72015-08-05 10:21:34 +01003059 DECLARE_INSTRUCTION(IntConstant);
3060
Artem Serovcced8ba2017-07-19 18:18:09 +01003061 protected:
3062 DEFAULT_COPY_CONSTRUCTOR(IntConstant);
3063
Roland Levillain9867bc72015-08-05 10:21:34 +01003064 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003065 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303066 : HConstant(kIntConstant, DataType::Type::kInt32, dex_pc), value_(value) {
3067 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003068 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303069 : HConstant(kIntConstant, DataType::Type::kInt32, dex_pc),
3070 value_(value ? 1 : 0) {
3071 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003072
3073 const int32_t value_;
3074
3075 friend class HGraph;
3076 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
3077 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
Roland Levillain9867bc72015-08-05 10:21:34 +01003078};
3079
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003080class HLongConstant final : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01003081 public:
3082 int64_t GetValue() const { return value_; }
3083
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003084 uint64_t GetValueAsUint64() const override { return value_; }
David Brazdil77a48ae2015-09-15 12:34:04 +00003085
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003086 bool InstructionDataEquals(const HInstruction* other) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003087 DCHECK(other->IsLongConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01003088 return other->AsLongConstant()->value_ == value_;
3089 }
3090
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003091 size_t ComputeHashCode() const override { return static_cast<size_t>(GetValue()); }
Roland Levillain9867bc72015-08-05 10:21:34 +01003092
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003093 bool IsMinusOne() const override { return GetValue() == -1; }
3094 bool IsArithmeticZero() const override { return GetValue() == 0; }
3095 bool IsZeroBitPattern() const override { return GetValue() == 0; }
3096 bool IsOne() const override { return GetValue() == 1; }
Roland Levillain9867bc72015-08-05 10:21:34 +01003097
3098 DECLARE_INSTRUCTION(LongConstant);
3099
Artem Serovcced8ba2017-07-19 18:18:09 +01003100 protected:
3101 DEFAULT_COPY_CONSTRUCTOR(LongConstant);
3102
Roland Levillain9867bc72015-08-05 10:21:34 +01003103 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003104 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303105 : HConstant(kLongConstant, DataType::Type::kInt64, dex_pc),
3106 value_(value) {
3107 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003108
3109 const int64_t value_;
3110
3111 friend class HGraph;
Roland Levillain9867bc72015-08-05 10:21:34 +01003112};
Dave Allison20dfc792014-06-16 20:44:29 -07003113
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003114class HFloatConstant final : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003115 public:
3116 float GetValue() const { return value_; }
3117
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003118 uint64_t GetValueAsUint64() const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003119 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
3120 }
3121
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003122 bool InstructionDataEquals(const HInstruction* other) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003123 DCHECK(other->IsFloatConstant()) << other->DebugName();
3124 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
3125 }
3126
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003127 size_t ComputeHashCode() const override { return static_cast<size_t>(GetValue()); }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003128
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003129 bool IsMinusOne() const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003130 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
3131 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003132 bool IsArithmeticZero() const override {
Roland Levillain1a653882016-03-18 18:05:57 +00003133 return std::fpclassify(value_) == FP_ZERO;
3134 }
3135 bool IsArithmeticPositiveZero() const {
3136 return IsArithmeticZero() && !std::signbit(value_);
3137 }
3138 bool IsArithmeticNegativeZero() const {
3139 return IsArithmeticZero() && std::signbit(value_);
3140 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003141 bool IsZeroBitPattern() const override {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00003142 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(0.0f);
Roland Levillain31dd3d62016-02-16 12:21:02 +00003143 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003144 bool IsOne() const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003145 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
3146 }
3147 bool IsNaN() const {
3148 return std::isnan(value_);
3149 }
3150
3151 DECLARE_INSTRUCTION(FloatConstant);
3152
Artem Serovcced8ba2017-07-19 18:18:09 +01003153 protected:
3154 DEFAULT_COPY_CONSTRUCTOR(FloatConstant);
3155
Roland Levillain31dd3d62016-02-16 12:21:02 +00003156 private:
3157 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303158 : HConstant(kFloatConstant, DataType::Type::kFloat32, dex_pc),
3159 value_(value) {
3160 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003161 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303162 : HConstant(kFloatConstant, DataType::Type::kFloat32, dex_pc),
3163 value_(bit_cast<float, int32_t>(value)) {
3164 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003165
3166 const float value_;
3167
3168 // Only the SsaBuilder and HGraph can create floating-point constants.
3169 friend class SsaBuilder;
3170 friend class HGraph;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003171};
3172
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003173class HDoubleConstant final : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003174 public:
3175 double GetValue() const { return value_; }
3176
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003177 uint64_t GetValueAsUint64() const override { return bit_cast<uint64_t, double>(value_); }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003178
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003179 bool InstructionDataEquals(const HInstruction* other) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003180 DCHECK(other->IsDoubleConstant()) << other->DebugName();
3181 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
3182 }
3183
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003184 size_t ComputeHashCode() const override { return static_cast<size_t>(GetValue()); }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003185
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003186 bool IsMinusOne() const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003187 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
3188 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003189 bool IsArithmeticZero() const override {
Roland Levillain1a653882016-03-18 18:05:57 +00003190 return std::fpclassify(value_) == FP_ZERO;
3191 }
3192 bool IsArithmeticPositiveZero() const {
3193 return IsArithmeticZero() && !std::signbit(value_);
3194 }
3195 bool IsArithmeticNegativeZero() const {
3196 return IsArithmeticZero() && std::signbit(value_);
3197 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003198 bool IsZeroBitPattern() const override {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00003199 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((0.0));
Roland Levillain31dd3d62016-02-16 12:21:02 +00003200 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003201 bool IsOne() const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003202 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
3203 }
3204 bool IsNaN() const {
3205 return std::isnan(value_);
3206 }
3207
3208 DECLARE_INSTRUCTION(DoubleConstant);
3209
Artem Serovcced8ba2017-07-19 18:18:09 +01003210 protected:
3211 DEFAULT_COPY_CONSTRUCTOR(DoubleConstant);
3212
Roland Levillain31dd3d62016-02-16 12:21:02 +00003213 private:
3214 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303215 : HConstant(kDoubleConstant, DataType::Type::kFloat64, dex_pc),
3216 value_(value) {
3217 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003218 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303219 : HConstant(kDoubleConstant, DataType::Type::kFloat64, dex_pc),
3220 value_(bit_cast<double, int64_t>(value)) {
3221 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003222
3223 const double value_;
3224
3225 // Only the SsaBuilder and HGraph can create floating-point constants.
3226 friend class SsaBuilder;
3227 friend class HGraph;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003228};
3229
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003230// Conditional branch. A block ending with an HIf instruction must have
3231// two successors.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003232class HIf final : public HExpression<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003233 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003234 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01003235 : HExpression(kIf, SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003236 SetRawInputAt(0, input);
3237 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003238
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003239 bool IsClonable() const override { return true; }
3240 bool IsControlFlow() const override { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003241
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003242 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01003243 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003244 }
3245
3246 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01003247 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003248 }
3249
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003250 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003251
Artem Serovcced8ba2017-07-19 18:18:09 +01003252 protected:
3253 DEFAULT_COPY_CONSTRUCTOR(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003254};
3255
David Brazdilfc6a86a2015-06-26 10:33:45 +00003256
3257// Abstract instruction which marks the beginning and/or end of a try block and
3258// links it to the respective exception handlers. Behaves the same as a Goto in
3259// non-exceptional control flow.
3260// Normal-flow successor is stored at index zero, exception handlers under
3261// higher indices in no particular order.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003262class HTryBoundary final : public HExpression<0> {
David Brazdilfc6a86a2015-06-26 10:33:45 +00003263 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003264 enum class BoundaryKind {
David Brazdil56e1acc2015-06-30 15:41:36 +01003265 kEntry,
3266 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00003267 kLast = kExit
David Brazdil56e1acc2015-06-30 15:41:36 +01003268 };
3269
Artem Serovd1aa7d02018-06-22 11:35:46 +01003270 // SideEffects::CanTriggerGC prevents instructions with SideEffects::DependOnGC to be alive
3271 // across the catch block entering edges as GC might happen during throwing an exception.
3272 // TryBoundary with BoundaryKind::kExit is conservatively used for that as there is no
3273 // HInstruction which a catch block must start from.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003274 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
Artem Serovd1aa7d02018-06-22 11:35:46 +01003275 : HExpression(kTryBoundary,
3276 (kind == BoundaryKind::kExit) ? SideEffects::CanTriggerGC()
3277 : SideEffects::None(),
3278 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003279 SetPackedField<BoundaryKindField>(kind);
3280 }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003281
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003282 bool IsControlFlow() const override { return true; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003283
3284 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01003285 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003286
David Brazdild26a4112015-11-10 11:07:31 +00003287 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
3288 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
3289 }
3290
David Brazdilfc6a86a2015-06-26 10:33:45 +00003291 // Returns whether `handler` is among its exception handlers (non-zero index
3292 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01003293 bool HasExceptionHandler(const HBasicBlock& handler) const {
3294 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00003295 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00003296 }
3297
3298 // If not present already, adds `handler` to its block's list of exception
3299 // handlers.
3300 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01003301 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00003302 GetBlock()->AddSuccessor(handler);
3303 }
3304 }
3305
Vladimir Markoa1de9182016-02-25 11:37:38 +00003306 BoundaryKind GetBoundaryKind() const { return GetPackedField<BoundaryKindField>(); }
3307 bool IsEntry() const { return GetBoundaryKind() == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003308
David Brazdilffee3d32015-07-06 11:48:53 +01003309 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
3310
David Brazdilfc6a86a2015-06-26 10:33:45 +00003311 DECLARE_INSTRUCTION(TryBoundary);
3312
Artem Serovcced8ba2017-07-19 18:18:09 +01003313 protected:
3314 DEFAULT_COPY_CONSTRUCTOR(TryBoundary);
3315
David Brazdilfc6a86a2015-06-26 10:33:45 +00003316 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003317 static constexpr size_t kFieldBoundaryKind = kNumberOfGenericPackedBits;
3318 static constexpr size_t kFieldBoundaryKindSize =
3319 MinimumBitsToStore(static_cast<size_t>(BoundaryKind::kLast));
3320 static constexpr size_t kNumberOfTryBoundaryPackedBits =
3321 kFieldBoundaryKind + kFieldBoundaryKindSize;
3322 static_assert(kNumberOfTryBoundaryPackedBits <= kMaxNumberOfPackedBits,
3323 "Too many packed fields.");
3324 using BoundaryKindField = BitField<BoundaryKind, kFieldBoundaryKind, kFieldBoundaryKindSize>;
David Brazdilfc6a86a2015-06-26 10:33:45 +00003325};
3326
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003327// Deoptimize to interpreter, upon checking a condition.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003328class HDeoptimize final : public HVariableInputSizeInstruction {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003329 public:
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003330 // Use this constructor when the `HDeoptimize` acts as a barrier, where no code can move
3331 // across.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003332 HDeoptimize(ArenaAllocator* allocator,
3333 HInstruction* cond,
3334 DeoptimizationKind kind,
3335 uint32_t dex_pc)
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003336 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303337 kDeoptimize,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003338 SideEffects::All(),
3339 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003340 allocator,
Andreas Gampe3db70682018-12-26 15:12:03 -08003341 /* number_of_inputs= */ 1,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003342 kArenaAllocMisc) {
3343 SetPackedFlag<kFieldCanBeMoved>(false);
3344 SetPackedField<DeoptimizeKindField>(kind);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003345 SetRawInputAt(0, cond);
3346 }
3347
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003348 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01003349
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003350 // Use this constructor when the `HDeoptimize` guards an instruction, and any user
3351 // that relies on the deoptimization to pass should have its input be the `HDeoptimize`
3352 // instead of `guard`.
3353 // We set CanTriggerGC to prevent any intermediate address to be live
3354 // at the point of the `HDeoptimize`.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003355 HDeoptimize(ArenaAllocator* allocator,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003356 HInstruction* cond,
3357 HInstruction* guard,
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003358 DeoptimizationKind kind,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003359 uint32_t dex_pc)
3360 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303361 kDeoptimize,
Vladimir Markobd785672018-05-03 17:09:09 +01003362 guard->GetType(),
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003363 SideEffects::CanTriggerGC(),
3364 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003365 allocator,
Andreas Gampe3db70682018-12-26 15:12:03 -08003366 /* number_of_inputs= */ 2,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003367 kArenaAllocMisc) {
3368 SetPackedFlag<kFieldCanBeMoved>(true);
3369 SetPackedField<DeoptimizeKindField>(kind);
3370 SetRawInputAt(0, cond);
3371 SetRawInputAt(1, guard);
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01003372 }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003373
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003374 bool CanBeMoved() const override { return GetPackedFlag<kFieldCanBeMoved>(); }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003375
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003376 bool InstructionDataEquals(const HInstruction* other) const override {
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003377 return (other->CanBeMoved() == CanBeMoved()) && (other->AsDeoptimize()->GetKind() == GetKind());
3378 }
3379
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003380 bool NeedsEnvironment() const override { return true; }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003381
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003382 bool CanThrow() const override { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003383
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003384 DeoptimizationKind GetDeoptimizationKind() const { return GetPackedField<DeoptimizeKindField>(); }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003385
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003386 bool GuardsAnInput() const {
3387 return InputCount() == 2;
3388 }
3389
3390 HInstruction* GuardedInput() const {
3391 DCHECK(GuardsAnInput());
3392 return InputAt(1);
3393 }
3394
3395 void RemoveGuard() {
3396 RemoveInputAt(1);
3397 }
3398
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003399 DECLARE_INSTRUCTION(Deoptimize);
3400
Artem Serovcced8ba2017-07-19 18:18:09 +01003401 protected:
3402 DEFAULT_COPY_CONSTRUCTOR(Deoptimize);
3403
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003404 private:
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003405 static constexpr size_t kFieldCanBeMoved = kNumberOfGenericPackedBits;
3406 static constexpr size_t kFieldDeoptimizeKind = kNumberOfGenericPackedBits + 1;
3407 static constexpr size_t kFieldDeoptimizeKindSize =
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003408 MinimumBitsToStore(static_cast<size_t>(DeoptimizationKind::kLast));
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003409 static constexpr size_t kNumberOfDeoptimizePackedBits =
3410 kFieldDeoptimizeKind + kFieldDeoptimizeKindSize;
3411 static_assert(kNumberOfDeoptimizePackedBits <= kMaxNumberOfPackedBits,
3412 "Too many packed fields.");
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003413 using DeoptimizeKindField =
3414 BitField<DeoptimizationKind, kFieldDeoptimizeKind, kFieldDeoptimizeKindSize>;
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003415};
3416
Mingyao Yang063fc772016-08-02 11:02:54 -07003417// Represents a should_deoptimize flag. Currently used for CHA-based devirtualization.
3418// The compiled code checks this flag value in a guard before devirtualized call and
3419// if it's true, starts to do deoptimization.
3420// It has a 4-byte slot on stack.
3421// TODO: allocate a register for this flag.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003422class HShouldDeoptimizeFlag final : public HVariableInputSizeInstruction {
Mingyao Yang063fc772016-08-02 11:02:54 -07003423 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003424 // CHA guards are only optimized in a separate pass and it has no side effects
3425 // with regard to other passes.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003426 HShouldDeoptimizeFlag(ArenaAllocator* allocator, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303427 : HVariableInputSizeInstruction(kShouldDeoptimizeFlag,
Vladimir Markobd785672018-05-03 17:09:09 +01003428 DataType::Type::kInt32,
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303429 SideEffects::None(),
3430 dex_pc,
3431 allocator,
3432 0,
3433 kArenaAllocCHA) {
Mingyao Yang063fc772016-08-02 11:02:54 -07003434 }
3435
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003436 // We do all CHA guard elimination/motion in a single pass, after which there is no
3437 // further guard elimination/motion since a guard might have been used for justification
3438 // of the elimination of another guard. Therefore, we pretend this guard cannot be moved
3439 // to avoid other optimizations trying to move it.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003440 bool CanBeMoved() const override { return false; }
Mingyao Yang063fc772016-08-02 11:02:54 -07003441
3442 DECLARE_INSTRUCTION(ShouldDeoptimizeFlag);
3443
Artem Serovcced8ba2017-07-19 18:18:09 +01003444 protected:
3445 DEFAULT_COPY_CONSTRUCTOR(ShouldDeoptimizeFlag);
Mingyao Yang063fc772016-08-02 11:02:54 -07003446};
3447
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003448// Represents the ArtMethod that was passed as a first argument to
3449// the method. It is used by instructions that depend on it, like
3450// instructions that work with the dex cache.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003451class HCurrentMethod final : public HExpression<0> {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003452 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003453 explicit HCurrentMethod(DataType::Type type, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303454 : HExpression(kCurrentMethod, type, SideEffects::None(), dex_pc) {
3455 }
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003456
3457 DECLARE_INSTRUCTION(CurrentMethod);
3458
Artem Serovcced8ba2017-07-19 18:18:09 +01003459 protected:
3460 DEFAULT_COPY_CONSTRUCTOR(CurrentMethod);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003461};
3462
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003463// Fetches an ArtMethod from the virtual table or the interface method table
3464// of a class.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003465class HClassTableGet final : public HExpression<1> {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003466 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003467 enum class TableKind {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003468 kVTable,
3469 kIMTable,
Vladimir Markoa1de9182016-02-25 11:37:38 +00003470 kLast = kIMTable
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003471 };
3472 HClassTableGet(HInstruction* cls,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003473 DataType::Type type,
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003474 TableKind kind,
3475 size_t index,
3476 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303477 : HExpression(kClassTableGet, type, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003478 index_(index) {
3479 SetPackedField<TableKindField>(kind);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003480 SetRawInputAt(0, cls);
3481 }
3482
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003483 bool IsClonable() const override { return true; }
3484 bool CanBeMoved() const override { return true; }
3485 bool InstructionDataEquals(const HInstruction* other) const override {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003486 return other->AsClassTableGet()->GetIndex() == index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00003487 other->AsClassTableGet()->GetPackedFields() == GetPackedFields();
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003488 }
3489
Vladimir Markoa1de9182016-02-25 11:37:38 +00003490 TableKind GetTableKind() const { return GetPackedField<TableKindField>(); }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003491 size_t GetIndex() const { return index_; }
3492
3493 DECLARE_INSTRUCTION(ClassTableGet);
3494
Artem Serovcced8ba2017-07-19 18:18:09 +01003495 protected:
3496 DEFAULT_COPY_CONSTRUCTOR(ClassTableGet);
3497
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003498 private:
Vladimir Markobd785672018-05-03 17:09:09 +01003499 static constexpr size_t kFieldTableKind = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003500 static constexpr size_t kFieldTableKindSize =
3501 MinimumBitsToStore(static_cast<size_t>(TableKind::kLast));
3502 static constexpr size_t kNumberOfClassTableGetPackedBits = kFieldTableKind + kFieldTableKindSize;
3503 static_assert(kNumberOfClassTableGetPackedBits <= kMaxNumberOfPackedBits,
3504 "Too many packed fields.");
3505 using TableKindField = BitField<TableKind, kFieldTableKind, kFieldTableKind>;
3506
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003507 // The index of the ArtMethod in the table.
3508 const size_t index_;
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003509};
3510
Mark Mendellfe57faa2015-09-18 09:26:15 -04003511// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
3512// have one successor for each entry in the switch table, and the final successor
3513// will be the block containing the next Dex opcode.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003514class HPackedSwitch final : public HExpression<1> {
Mark Mendellfe57faa2015-09-18 09:26:15 -04003515 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003516 HPackedSwitch(int32_t start_value,
3517 uint32_t num_entries,
3518 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04003519 uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01003520 : HExpression(kPackedSwitch, SideEffects::None(), dex_pc),
Mark Mendellfe57faa2015-09-18 09:26:15 -04003521 start_value_(start_value),
3522 num_entries_(num_entries) {
3523 SetRawInputAt(0, input);
3524 }
3525
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003526 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01003527
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003528 bool IsControlFlow() const override { return true; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04003529
3530 int32_t GetStartValue() const { return start_value_; }
3531
Vladimir Marko211c2112015-09-24 16:52:33 +01003532 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04003533
3534 HBasicBlock* GetDefaultBlock() const {
3535 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01003536 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04003537 }
3538 DECLARE_INSTRUCTION(PackedSwitch);
3539
Artem Serovcced8ba2017-07-19 18:18:09 +01003540 protected:
3541 DEFAULT_COPY_CONSTRUCTOR(PackedSwitch);
3542
Mark Mendellfe57faa2015-09-18 09:26:15 -04003543 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003544 const int32_t start_value_;
3545 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04003546};
3547
Roland Levillain88cb1752014-10-20 16:36:47 +01003548class HUnaryOperation : public HExpression<1> {
3549 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303550 HUnaryOperation(InstructionKind kind,
3551 DataType::Type result_type,
3552 HInstruction* input,
3553 uint32_t dex_pc = kNoDexPc)
3554 : HExpression(kind, result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01003555 SetRawInputAt(0, input);
3556 }
3557
Artem Serovcced8ba2017-07-19 18:18:09 +01003558 // All of the UnaryOperation instructions are clonable.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003559 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01003560
Roland Levillain88cb1752014-10-20 16:36:47 +01003561 HInstruction* GetInput() const { return InputAt(0); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003562 DataType::Type GetResultType() const { return GetType(); }
Roland Levillain88cb1752014-10-20 16:36:47 +01003563
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003564 bool CanBeMoved() const override { return true; }
3565 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003566 return true;
3567 }
Roland Levillain88cb1752014-10-20 16:36:47 +01003568
Roland Levillain31dd3d62016-02-16 12:21:02 +00003569 // Try to statically evaluate `this` and return a HConstant
3570 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003571 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003572 HConstant* TryStaticEvaluation() const;
3573
3574 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003575 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
3576 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003577 virtual HConstant* Evaluate(HFloatConstant* x) const = 0;
3578 virtual HConstant* Evaluate(HDoubleConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01003579
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003580 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01003581
Artem Serovcced8ba2017-07-19 18:18:09 +01003582 protected:
3583 DEFAULT_COPY_CONSTRUCTOR(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01003584};
3585
Dave Allison20dfc792014-06-16 20:44:29 -07003586class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003587 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303588 HBinaryOperation(InstructionKind kind,
3589 DataType::Type result_type,
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003590 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003591 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003592 SideEffects side_effects = SideEffects::None(),
3593 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303594 : HExpression(kind, result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003595 SetRawInputAt(0, left);
3596 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003597 }
3598
Artem Serovcced8ba2017-07-19 18:18:09 +01003599 // All of the BinaryOperation instructions are clonable.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003600 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01003601
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003602 HInstruction* GetLeft() const { return InputAt(0); }
3603 HInstruction* GetRight() const { return InputAt(1); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003604 DataType::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003605
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003606 virtual bool IsCommutative() const { return false; }
3607
3608 // Put constant on the right.
3609 // Returns whether order is changed.
3610 bool OrderInputsWithConstantOnTheRight() {
3611 HInstruction* left = InputAt(0);
3612 HInstruction* right = InputAt(1);
3613 if (left->IsConstant() && !right->IsConstant()) {
3614 ReplaceInput(right, 0);
3615 ReplaceInput(left, 1);
3616 return true;
3617 }
3618 return false;
3619 }
3620
3621 // Order inputs by instruction id, but favor constant on the right side.
3622 // This helps GVN for commutative ops.
3623 void OrderInputs() {
3624 DCHECK(IsCommutative());
3625 HInstruction* left = InputAt(0);
3626 HInstruction* right = InputAt(1);
3627 if (left == right || (!left->IsConstant() && right->IsConstant())) {
3628 return;
3629 }
3630 if (OrderInputsWithConstantOnTheRight()) {
3631 return;
3632 }
3633 // Order according to instruction id.
3634 if (left->GetId() > right->GetId()) {
3635 ReplaceInput(right, 0);
3636 ReplaceInput(left, 1);
3637 }
3638 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003639
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003640 bool CanBeMoved() const override { return true; }
3641 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003642 return true;
3643 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003644
Roland Levillain31dd3d62016-02-16 12:21:02 +00003645 // Try to statically evaluate `this` and return a HConstant
3646 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003647 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003648 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01003649
3650 // Apply this operation to `x` and `y`.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003651 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3652 HNullConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003653 LOG(FATAL) << DebugName() << " is not defined for the (null, null) case.";
3654 UNREACHABLE();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003655 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003656 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
3657 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
Roland Levillain9867bc72015-08-05 10:21:34 +01003658 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
3659 HIntConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003660 LOG(FATAL) << DebugName() << " is not defined for the (long, int) case.";
3661 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01003662 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003663 virtual HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const = 0;
3664 virtual HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const = 0;
Roland Levillain556c3d12014-09-18 15:25:07 +01003665
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003666 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003667 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003668 HConstant* GetConstantRight() const;
3669
3670 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003671 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003672 HInstruction* GetLeastConstantLeft() const;
3673
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003674 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01003675
Artem Serovcced8ba2017-07-19 18:18:09 +01003676 protected:
3677 DEFAULT_COPY_CONSTRUCTOR(BinaryOperation);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003678};
3679
Mark Mendellc4701932015-04-10 13:18:51 -04003680// The comparison bias applies for floating point operations and indicates how NaN
3681// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01003682enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04003683 kNoBias, // bias is not applicable (i.e. for long operation)
3684 kGtBias, // return 1 for NaN comparisons
3685 kLtBias, // return -1 for NaN comparisons
Vladimir Markoa1de9182016-02-25 11:37:38 +00003686 kLast = kLtBias
Mark Mendellc4701932015-04-10 13:18:51 -04003687};
3688
Roland Levillain31dd3d62016-02-16 12:21:02 +00003689std::ostream& operator<<(std::ostream& os, const ComparisonBias& rhs);
3690
Dave Allison20dfc792014-06-16 20:44:29 -07003691class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003692 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303693 HCondition(InstructionKind kind,
3694 HInstruction* first,
3695 HInstruction* second,
3696 uint32_t dex_pc = kNoDexPc)
3697 : HBinaryOperation(kind,
3698 DataType::Type::kBool,
3699 first,
3700 second,
3701 SideEffects::None(),
3702 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003703 SetPackedField<ComparisonBiasField>(ComparisonBias::kNoBias);
3704 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003705
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003706 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003707 // `instruction`, and disregard moves in between.
3708 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003709
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003710 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003711
3712 virtual IfCondition GetCondition() const = 0;
3713
Mark Mendellc4701932015-04-10 13:18:51 -04003714 virtual IfCondition GetOppositeCondition() const = 0;
3715
Vladimir Markoa1de9182016-02-25 11:37:38 +00003716 bool IsGtBias() const { return GetBias() == ComparisonBias::kGtBias; }
Anton Shaminbdd79352016-02-15 12:48:36 +06003717 bool IsLtBias() const { return GetBias() == ComparisonBias::kLtBias; }
3718
Vladimir Markoa1de9182016-02-25 11:37:38 +00003719 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
3720 void SetBias(ComparisonBias bias) { SetPackedField<ComparisonBiasField>(bias); }
Mark Mendellc4701932015-04-10 13:18:51 -04003721
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003722 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003723 return GetPackedFields() == other->AsCondition()->GetPackedFields();
Mark Mendellc4701932015-04-10 13:18:51 -04003724 }
3725
Roland Levillain4fa13f62015-07-06 18:11:54 +01003726 bool IsFPConditionTrueIfNaN() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003727 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003728 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003729 if (if_cond == kCondNE) {
3730 return true;
3731 } else if (if_cond == kCondEQ) {
3732 return false;
3733 }
3734 return ((if_cond == kCondGT) || (if_cond == kCondGE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003735 }
3736
3737 bool IsFPConditionFalseIfNaN() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003738 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003739 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003740 if (if_cond == kCondEQ) {
3741 return true;
3742 } else if (if_cond == kCondNE) {
3743 return false;
3744 }
3745 return ((if_cond == kCondLT) || (if_cond == kCondLE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003746 }
3747
Roland Levillain31dd3d62016-02-16 12:21:02 +00003748 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003749 // Needed if we merge a HCompare into a HCondition.
Vladimir Markobd785672018-05-03 17:09:09 +01003750 static constexpr size_t kFieldComparisonBias = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003751 static constexpr size_t kFieldComparisonBiasSize =
3752 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3753 static constexpr size_t kNumberOfConditionPackedBits =
3754 kFieldComparisonBias + kFieldComparisonBiasSize;
3755 static_assert(kNumberOfConditionPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3756 using ComparisonBiasField =
3757 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3758
Roland Levillain31dd3d62016-02-16 12:21:02 +00003759 template <typename T>
3760 int32_t Compare(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3761
3762 template <typename T>
3763 int32_t CompareFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003764 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003765 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3766 // Handle the bias.
3767 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compare(x, y);
3768 }
3769
3770 // Return an integer constant containing the result of a condition evaluated at compile time.
3771 HIntConstant* MakeConstantCondition(bool value, uint32_t dex_pc) const {
3772 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3773 }
3774
Artem Serovcced8ba2017-07-19 18:18:09 +01003775 DEFAULT_COPY_CONSTRUCTOR(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003776};
3777
3778// Instruction to check if two inputs are equal to each other.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003779class HEqual final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003780 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003781 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303782 : HCondition(kEqual, first, second, dex_pc) {
3783 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003784
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003785 bool IsCommutative() const override { return true; }
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003786
Vladimir Marko9e23df52015-11-10 17:14:35 +00003787 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003788 HNullConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003789 return MakeConstantCondition(true, GetDexPc());
3790 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003791 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003792 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3793 }
3794 // In the following Evaluate methods, a HCompare instruction has
3795 // been merged into this HEqual instruction; evaluate it as
3796 // `Compare(x, y) == 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003797 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003798 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0),
3799 GetDexPc());
3800 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003801 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003802 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3803 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003804 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003805 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003806 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003807
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003808 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003809
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003810 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07003811 return kCondEQ;
3812 }
3813
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003814 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04003815 return kCondNE;
3816 }
3817
Artem Serovcced8ba2017-07-19 18:18:09 +01003818 protected:
3819 DEFAULT_COPY_CONSTRUCTOR(Equal);
3820
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003821 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003822 template <typename T> static bool Compute(T x, T y) { return x == y; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003823};
3824
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003825class HNotEqual final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003826 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303827 HNotEqual(HInstruction* first, HInstruction* second,
3828 uint32_t dex_pc = kNoDexPc)
3829 : HCondition(kNotEqual, first, second, dex_pc) {
3830 }
Dave Allison20dfc792014-06-16 20:44:29 -07003831
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003832 bool IsCommutative() const override { return true; }
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003833
Vladimir Marko9e23df52015-11-10 17:14:35 +00003834 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003835 HNullConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003836 return MakeConstantCondition(false, GetDexPc());
3837 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003838 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003839 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3840 }
3841 // In the following Evaluate methods, a HCompare instruction has
3842 // been merged into this HNotEqual instruction; evaluate it as
3843 // `Compare(x, y) != 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003844 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003845 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3846 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003847 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003848 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3849 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003850 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003851 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003852 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003853
Dave Allison20dfc792014-06-16 20:44:29 -07003854 DECLARE_INSTRUCTION(NotEqual);
3855
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003856 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07003857 return kCondNE;
3858 }
3859
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003860 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04003861 return kCondEQ;
3862 }
3863
Artem Serovcced8ba2017-07-19 18:18:09 +01003864 protected:
3865 DEFAULT_COPY_CONSTRUCTOR(NotEqual);
3866
Dave Allison20dfc792014-06-16 20:44:29 -07003867 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003868 template <typename T> static bool Compute(T x, T y) { return x != y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003869};
3870
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003871class HLessThan final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003872 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303873 HLessThan(HInstruction* first, HInstruction* second,
3874 uint32_t dex_pc = kNoDexPc)
3875 : HCondition(kLessThan, first, second, dex_pc) {
3876 }
Dave Allison20dfc792014-06-16 20:44:29 -07003877
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003878 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003879 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003880 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003881 // In the following Evaluate methods, a HCompare instruction has
3882 // been merged into this HLessThan instruction; evaluate it as
3883 // `Compare(x, y) < 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003884 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003885 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3886 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003887 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003888 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3889 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003890 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003891 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003892 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003893
Dave Allison20dfc792014-06-16 20:44:29 -07003894 DECLARE_INSTRUCTION(LessThan);
3895
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003896 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07003897 return kCondLT;
3898 }
3899
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003900 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04003901 return kCondGE;
3902 }
3903
Artem Serovcced8ba2017-07-19 18:18:09 +01003904 protected:
3905 DEFAULT_COPY_CONSTRUCTOR(LessThan);
3906
Dave Allison20dfc792014-06-16 20:44:29 -07003907 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003908 template <typename T> static bool Compute(T x, T y) { return x < y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003909};
3910
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003911class HLessThanOrEqual final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003912 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303913 HLessThanOrEqual(HInstruction* first, HInstruction* second,
3914 uint32_t dex_pc = kNoDexPc)
3915 : HCondition(kLessThanOrEqual, first, second, dex_pc) {
3916 }
Dave Allison20dfc792014-06-16 20:44:29 -07003917
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003918 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003919 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003920 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003921 // In the following Evaluate methods, a HCompare instruction has
3922 // been merged into this HLessThanOrEqual instruction; evaluate it as
3923 // `Compare(x, y) <= 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003924 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003925 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3926 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003927 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003928 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3929 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003930 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003931 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003932 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003933
Dave Allison20dfc792014-06-16 20:44:29 -07003934 DECLARE_INSTRUCTION(LessThanOrEqual);
3935
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003936 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07003937 return kCondLE;
3938 }
3939
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003940 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04003941 return kCondGT;
3942 }
3943
Artem Serovcced8ba2017-07-19 18:18:09 +01003944 protected:
3945 DEFAULT_COPY_CONSTRUCTOR(LessThanOrEqual);
3946
Dave Allison20dfc792014-06-16 20:44:29 -07003947 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003948 template <typename T> static bool Compute(T x, T y) { return x <= y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003949};
3950
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003951class HGreaterThan final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003952 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003953 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303954 : HCondition(kGreaterThan, first, second, dex_pc) {
3955 }
Dave Allison20dfc792014-06-16 20:44:29 -07003956
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003957 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003958 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003959 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003960 // In the following Evaluate methods, a HCompare instruction has
3961 // been merged into this HGreaterThan instruction; evaluate it as
3962 // `Compare(x, y) > 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003963 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003964 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3965 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003966 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003967 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3968 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003969 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003970 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003971 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003972
Dave Allison20dfc792014-06-16 20:44:29 -07003973 DECLARE_INSTRUCTION(GreaterThan);
3974
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003975 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07003976 return kCondGT;
3977 }
3978
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003979 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04003980 return kCondLE;
3981 }
3982
Artem Serovcced8ba2017-07-19 18:18:09 +01003983 protected:
3984 DEFAULT_COPY_CONSTRUCTOR(GreaterThan);
3985
Dave Allison20dfc792014-06-16 20:44:29 -07003986 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003987 template <typename T> static bool Compute(T x, T y) { return x > y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003988};
3989
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003990class HGreaterThanOrEqual final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003991 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003992 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303993 : HCondition(kGreaterThanOrEqual, first, second, dex_pc) {
3994 }
Dave Allison20dfc792014-06-16 20:44:29 -07003995
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003996 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003997 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003998 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003999 // In the following Evaluate methods, a HCompare instruction has
4000 // been merged into this HGreaterThanOrEqual instruction; evaluate it as
4001 // `Compare(x, y) >= 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004002 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004003 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
4004 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004005 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004006 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
4007 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004008 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004009 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004010 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004011
Dave Allison20dfc792014-06-16 20:44:29 -07004012 DECLARE_INSTRUCTION(GreaterThanOrEqual);
4013
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004014 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07004015 return kCondGE;
4016 }
4017
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004018 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04004019 return kCondLT;
4020 }
4021
Artem Serovcced8ba2017-07-19 18:18:09 +01004022 protected:
4023 DEFAULT_COPY_CONSTRUCTOR(GreaterThanOrEqual);
4024
Dave Allison20dfc792014-06-16 20:44:29 -07004025 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004026 template <typename T> static bool Compute(T x, T y) { return x >= y; }
Dave Allison20dfc792014-06-16 20:44:29 -07004027};
4028
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004029class HBelow final : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07004030 public:
4031 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304032 : HCondition(kBelow, first, second, dex_pc) {
4033 }
Aart Bike9f37602015-10-09 11:15:55 -07004034
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004035 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004036 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07004037 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004038 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004039 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4040 }
4041 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004042 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004043 LOG(FATAL) << DebugName() << " is not defined for float values";
4044 UNREACHABLE();
4045 }
4046 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004047 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004048 LOG(FATAL) << DebugName() << " is not defined for double values";
4049 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07004050 }
4051
4052 DECLARE_INSTRUCTION(Below);
4053
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004054 IfCondition GetCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004055 return kCondB;
4056 }
4057
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004058 IfCondition GetOppositeCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004059 return kCondAE;
4060 }
4061
Artem Serovcced8ba2017-07-19 18:18:09 +01004062 protected:
4063 DEFAULT_COPY_CONSTRUCTOR(Below);
4064
Aart Bike9f37602015-10-09 11:15:55 -07004065 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004066 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004067 return MakeUnsigned(x) < MakeUnsigned(y);
4068 }
Aart Bike9f37602015-10-09 11:15:55 -07004069};
4070
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004071class HBelowOrEqual final : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07004072 public:
4073 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304074 : HCondition(kBelowOrEqual, first, second, dex_pc) {
4075 }
Aart Bike9f37602015-10-09 11:15:55 -07004076
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004077 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004078 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07004079 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004080 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004081 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4082 }
4083 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004084 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004085 LOG(FATAL) << DebugName() << " is not defined for float values";
4086 UNREACHABLE();
4087 }
4088 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004089 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004090 LOG(FATAL) << DebugName() << " is not defined for double values";
4091 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07004092 }
4093
4094 DECLARE_INSTRUCTION(BelowOrEqual);
4095
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004096 IfCondition GetCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004097 return kCondBE;
4098 }
4099
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004100 IfCondition GetOppositeCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004101 return kCondA;
4102 }
4103
Artem Serovcced8ba2017-07-19 18:18:09 +01004104 protected:
4105 DEFAULT_COPY_CONSTRUCTOR(BelowOrEqual);
4106
Aart Bike9f37602015-10-09 11:15:55 -07004107 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004108 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004109 return MakeUnsigned(x) <= MakeUnsigned(y);
4110 }
Aart Bike9f37602015-10-09 11:15:55 -07004111};
4112
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004113class HAbove final : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07004114 public:
4115 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304116 : HCondition(kAbove, first, second, dex_pc) {
4117 }
Aart Bike9f37602015-10-09 11:15:55 -07004118
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004119 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004120 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07004121 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004122 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004123 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4124 }
4125 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004126 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004127 LOG(FATAL) << DebugName() << " is not defined for float values";
4128 UNREACHABLE();
4129 }
4130 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004131 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004132 LOG(FATAL) << DebugName() << " is not defined for double values";
4133 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07004134 }
4135
4136 DECLARE_INSTRUCTION(Above);
4137
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004138 IfCondition GetCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004139 return kCondA;
4140 }
4141
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004142 IfCondition GetOppositeCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004143 return kCondBE;
4144 }
4145
Artem Serovcced8ba2017-07-19 18:18:09 +01004146 protected:
4147 DEFAULT_COPY_CONSTRUCTOR(Above);
4148
Aart Bike9f37602015-10-09 11:15:55 -07004149 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004150 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004151 return MakeUnsigned(x) > MakeUnsigned(y);
4152 }
Aart Bike9f37602015-10-09 11:15:55 -07004153};
4154
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004155class HAboveOrEqual final : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07004156 public:
4157 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304158 : HCondition(kAboveOrEqual, first, second, dex_pc) {
4159 }
Aart Bike9f37602015-10-09 11:15:55 -07004160
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004161 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004162 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07004163 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004164 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004165 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4166 }
4167 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004168 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004169 LOG(FATAL) << DebugName() << " is not defined for float values";
4170 UNREACHABLE();
4171 }
4172 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004173 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004174 LOG(FATAL) << DebugName() << " is not defined for double values";
4175 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07004176 }
4177
4178 DECLARE_INSTRUCTION(AboveOrEqual);
4179
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004180 IfCondition GetCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004181 return kCondAE;
4182 }
4183
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004184 IfCondition GetOppositeCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004185 return kCondB;
4186 }
4187
Artem Serovcced8ba2017-07-19 18:18:09 +01004188 protected:
4189 DEFAULT_COPY_CONSTRUCTOR(AboveOrEqual);
4190
Aart Bike9f37602015-10-09 11:15:55 -07004191 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004192 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004193 return MakeUnsigned(x) >= MakeUnsigned(y);
4194 }
Aart Bike9f37602015-10-09 11:15:55 -07004195};
Dave Allison20dfc792014-06-16 20:44:29 -07004196
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004197// Instruction to check how two inputs compare to each other.
4198// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004199class HCompare final : public HBinaryOperation {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004200 public:
Roland Levillaina5c4a402016-03-15 15:02:50 +00004201 // Note that `comparison_type` is the type of comparison performed
4202 // between the comparison's inputs, not the type of the instantiated
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004203 // HCompare instruction (which is always DataType::Type::kInt).
4204 HCompare(DataType::Type comparison_type,
Alexey Frunze4dda3372015-06-01 18:31:49 -07004205 HInstruction* first,
4206 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04004207 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07004208 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304209 : HBinaryOperation(kCompare,
4210 DataType::Type::kInt32,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004211 first,
4212 second,
Roland Levillaina5c4a402016-03-15 15:02:50 +00004213 SideEffectsForArchRuntimeCalls(comparison_type),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004214 dex_pc) {
4215 SetPackedField<ComparisonBiasField>(bias);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004216 }
4217
Roland Levillain9867bc72015-08-05 10:21:34 +01004218 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004219 int32_t Compute(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
4220
4221 template <typename T>
4222 int32_t ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004223 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00004224 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
4225 // Handle the bias.
4226 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compute(x, y);
4227 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004228
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004229 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004230 // Note that there is no "cmp-int" Dex instruction so we shouldn't
4231 // reach this code path when processing a freshly built HIR
4232 // graph. However HCompare integer instructions can be synthesized
4233 // by the instruction simplifier to implement IntegerCompare and
4234 // IntegerSignum intrinsics, so we have to handle this case.
4235 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004236 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004237 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004238 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4239 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004240 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004241 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4242 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004243 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004244 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01004245 }
4246
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004247 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004248 return GetPackedFields() == other->AsCompare()->GetPackedFields();
Calin Juravleddb7df22014-11-25 20:56:51 +00004249 }
4250
Vladimir Markoa1de9182016-02-25 11:37:38 +00004251 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
Mark Mendellc4701932015-04-10 13:18:51 -04004252
Roland Levillain31dd3d62016-02-16 12:21:02 +00004253 // Does this compare instruction have a "gt bias" (vs an "lt bias")?
Vladimir Markoa1de9182016-02-25 11:37:38 +00004254 // Only meaningful for floating-point comparisons.
Roland Levillain31dd3d62016-02-16 12:21:02 +00004255 bool IsGtBias() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004256 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004257 return GetBias() == ComparisonBias::kGtBias;
Roland Levillain31dd3d62016-02-16 12:21:02 +00004258 }
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004259
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004260 static SideEffects SideEffectsForArchRuntimeCalls(DataType::Type type ATTRIBUTE_UNUSED) {
Roland Levillain1693a1f2016-03-15 14:57:31 +00004261 // Comparisons do not require a runtime call in any back end.
4262 return SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004263 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004264
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004265 DECLARE_INSTRUCTION(Compare);
4266
Roland Levillain31dd3d62016-02-16 12:21:02 +00004267 protected:
Vladimir Markobd785672018-05-03 17:09:09 +01004268 static constexpr size_t kFieldComparisonBias = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004269 static constexpr size_t kFieldComparisonBiasSize =
4270 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
4271 static constexpr size_t kNumberOfComparePackedBits =
4272 kFieldComparisonBias + kFieldComparisonBiasSize;
4273 static_assert(kNumberOfComparePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
4274 using ComparisonBiasField =
4275 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
4276
Roland Levillain31dd3d62016-02-16 12:21:02 +00004277 // Return an integer constant containing the result of a comparison evaluated at compile time.
4278 HIntConstant* MakeConstantComparison(int32_t value, uint32_t dex_pc) const {
4279 DCHECK(value == -1 || value == 0 || value == 1) << value;
4280 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
4281 }
4282
Artem Serovcced8ba2017-07-19 18:18:09 +01004283 DEFAULT_COPY_CONSTRUCTOR(Compare);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004284};
4285
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004286class HNewInstance final : public HExpression<1> {
David Brazdil6de19382016-01-08 17:37:10 +00004287 public:
4288 HNewInstance(HInstruction* cls,
David Brazdil6de19382016-01-08 17:37:10 +00004289 uint32_t dex_pc,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004290 dex::TypeIndex type_index,
David Brazdil6de19382016-01-08 17:37:10 +00004291 const DexFile& dex_file,
David Brazdil6de19382016-01-08 17:37:10 +00004292 bool finalizable,
4293 QuickEntrypointEnum entrypoint)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304294 : HExpression(kNewInstance,
4295 DataType::Type::kReference,
4296 SideEffects::CanTriggerGC(),
4297 dex_pc),
David Brazdil6de19382016-01-08 17:37:10 +00004298 type_index_(type_index),
4299 dex_file_(dex_file),
David Brazdil6de19382016-01-08 17:37:10 +00004300 entrypoint_(entrypoint) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004301 SetPackedFlag<kFlagFinalizable>(finalizable);
David Brazdil6de19382016-01-08 17:37:10 +00004302 SetRawInputAt(0, cls);
David Brazdil6de19382016-01-08 17:37:10 +00004303 }
4304
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004305 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004306
Andreas Gampea5b09a62016-11-17 15:21:22 -08004307 dex::TypeIndex GetTypeIndex() const { return type_index_; }
David Brazdil6de19382016-01-08 17:37:10 +00004308 const DexFile& GetDexFile() const { return dex_file_; }
4309
4310 // Calls runtime so needs an environment.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004311 bool NeedsEnvironment() const override { return true; }
David Brazdil6de19382016-01-08 17:37:10 +00004312
Mingyao Yang062157f2016-03-02 10:15:36 -08004313 // Can throw errors when out-of-memory or if it's not instantiable/accessible.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004314 bool CanThrow() const override { return true; }
Mingyao Yang062157f2016-03-02 10:15:36 -08004315
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004316 bool NeedsChecks() const {
4317 return entrypoint_ == kQuickAllocObjectWithChecks;
4318 }
David Brazdil6de19382016-01-08 17:37:10 +00004319
Vladimir Markoa1de9182016-02-25 11:37:38 +00004320 bool IsFinalizable() const { return GetPackedFlag<kFlagFinalizable>(); }
David Brazdil6de19382016-01-08 17:37:10 +00004321
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004322 bool CanBeNull() const override { return false; }
David Brazdil6de19382016-01-08 17:37:10 +00004323
4324 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
4325
4326 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
4327 entrypoint_ = entrypoint;
4328 }
4329
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004330 HLoadClass* GetLoadClass() const {
4331 HInstruction* input = InputAt(0);
4332 if (input->IsClinitCheck()) {
4333 input = input->InputAt(0);
4334 }
4335 DCHECK(input->IsLoadClass());
4336 return input->AsLoadClass();
4337 }
4338
David Brazdil6de19382016-01-08 17:37:10 +00004339 bool IsStringAlloc() const;
4340
4341 DECLARE_INSTRUCTION(NewInstance);
4342
Artem Serovcced8ba2017-07-19 18:18:09 +01004343 protected:
4344 DEFAULT_COPY_CONSTRUCTOR(NewInstance);
4345
David Brazdil6de19382016-01-08 17:37:10 +00004346 private:
Vladimir Markobd785672018-05-03 17:09:09 +01004347 static constexpr size_t kFlagFinalizable = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004348 static constexpr size_t kNumberOfNewInstancePackedBits = kFlagFinalizable + 1;
4349 static_assert(kNumberOfNewInstancePackedBits <= kMaxNumberOfPackedBits,
4350 "Too many packed fields.");
4351
Andreas Gampea5b09a62016-11-17 15:21:22 -08004352 const dex::TypeIndex type_index_;
David Brazdil6de19382016-01-08 17:37:10 +00004353 const DexFile& dex_file_;
David Brazdil6de19382016-01-08 17:37:10 +00004354 QuickEntrypointEnum entrypoint_;
David Brazdil6de19382016-01-08 17:37:10 +00004355};
4356
Agi Csaki05f20562015-08-19 14:58:14 -07004357enum IntrinsicNeedsEnvironmentOrCache {
4358 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
4359 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07004360};
4361
Aart Bik5d75afe2015-12-14 11:57:01 -08004362enum IntrinsicSideEffects {
4363 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
4364 kReadSideEffects, // Intrinsic may read heap memory.
4365 kWriteSideEffects, // Intrinsic may write heap memory.
4366 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
4367};
4368
4369enum IntrinsicExceptions {
4370 kNoThrow, // Intrinsic does not throw any exceptions.
4371 kCanThrow // Intrinsic may throw exceptions.
4372};
4373
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004374class HInvoke : public HVariableInputSizeInstruction {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004375 public:
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004376 bool NeedsEnvironment() const override;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004377
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01004378 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004379 SetRawInputAt(index, argument);
4380 }
4381
Roland Levillain3e3d7332015-04-28 11:00:54 +01004382 // Return the number of arguments. This number can be lower than
4383 // the number of inputs returned by InputCount(), as some invoke
4384 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
4385 // inputs at the end of their list of inputs.
4386 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
4387
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004388 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
4389
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004390 InvokeType GetInvokeType() const {
4391 return GetPackedField<InvokeTypeField>();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004392 }
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004393
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01004394 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004395 return intrinsic_;
4396 }
4397
Aart Bik5d75afe2015-12-14 11:57:01 -08004398 void SetIntrinsic(Intrinsics intrinsic,
4399 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
4400 IntrinsicSideEffects side_effects,
4401 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004402
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01004403 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00004404 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01004405 }
4406
Aart Bikff7d89c2016-11-07 08:49:28 -08004407 void SetCanThrow(bool can_throw) { SetPackedFlag<kFlagCanThrow>(can_throw); }
4408
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004409 bool CanThrow() const override { return GetPackedFlag<kFlagCanThrow>(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08004410
Aart Bika8b8e9b2018-01-09 11:01:02 -08004411 void SetAlwaysThrows(bool always_throws) { SetPackedFlag<kFlagAlwaysThrows>(always_throws); }
4412
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004413 bool AlwaysThrows() const override { return GetPackedFlag<kFlagAlwaysThrows>(); }
Aart Bika8b8e9b2018-01-09 11:01:02 -08004414
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004415 bool CanBeMoved() const override { return IsIntrinsic() && !DoesAnyWrite(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08004416
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004417 bool InstructionDataEquals(const HInstruction* other) const override {
Aart Bik5d75afe2015-12-14 11:57:01 -08004418 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
4419 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01004420
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004421 uint32_t* GetIntrinsicOptimizations() {
4422 return &intrinsic_optimizations_;
4423 }
4424
4425 const uint32_t* GetIntrinsicOptimizations() const {
4426 return &intrinsic_optimizations_;
4427 }
4428
4429 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
4430
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004431 ArtMethod* GetResolvedMethod() const { return resolved_method_; }
Nicolas Geoffray76d4bb0f32018-09-21 12:58:45 +01004432 void SetResolvedMethod(ArtMethod* method) REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004433
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004434 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004435
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004436 protected:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004437 static constexpr size_t kFieldInvokeType = kNumberOfGenericPackedBits;
4438 static constexpr size_t kFieldInvokeTypeSize =
Vladimir Markoa1de9182016-02-25 11:37:38 +00004439 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
Vladimir Markobd785672018-05-03 17:09:09 +01004440 static constexpr size_t kFlagCanThrow = kFieldInvokeType + kFieldInvokeTypeSize;
Aart Bika8b8e9b2018-01-09 11:01:02 -08004441 static constexpr size_t kFlagAlwaysThrows = kFlagCanThrow + 1;
4442 static constexpr size_t kNumberOfInvokePackedBits = kFlagAlwaysThrows + 1;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004443 static_assert(kNumberOfInvokePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004444 using InvokeTypeField = BitField<InvokeType, kFieldInvokeType, kFieldInvokeTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004445
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304446 HInvoke(InstructionKind kind,
4447 ArenaAllocator* allocator,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004448 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01004449 uint32_t number_of_other_inputs,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004450 DataType::Type return_type,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004451 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004452 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004453 ArtMethod* resolved_method,
4454 InvokeType invoke_type)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004455 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304456 kind,
Vladimir Markobd785672018-05-03 17:09:09 +01004457 return_type,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004458 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
4459 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004460 allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004461 number_of_arguments + number_of_other_inputs,
4462 kArenaAllocInvokeInputs),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004463 number_of_arguments_(number_of_arguments),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004464 dex_method_index_(dex_method_index),
agicsaki57b81ec2015-08-11 17:39:37 -07004465 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004466 intrinsic_optimizations_(0) {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004467 SetPackedField<InvokeTypeField>(invoke_type);
Vladimir Markoa1de9182016-02-25 11:37:38 +00004468 SetPackedFlag<kFlagCanThrow>(true);
Nicolas Geoffray76d4bb0f32018-09-21 12:58:45 +01004469 // Check mutator lock, constructors lack annotalysis support.
4470 Locks::mutator_lock_->AssertNotExclusiveHeld(Thread::Current());
4471 SetResolvedMethod(resolved_method);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004472 }
4473
Artem Serovcced8ba2017-07-19 18:18:09 +01004474 DEFAULT_COPY_CONSTRUCTOR(Invoke);
4475
Roland Levillain3e3d7332015-04-28 11:00:54 +01004476 uint32_t number_of_arguments_;
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00004477 ArtMethod* resolved_method_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004478 const uint32_t dex_method_index_;
4479 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004480
4481 // A magic word holding optimizations for intrinsics. See intrinsics.h.
4482 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004483};
4484
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004485class HInvokeUnresolved final : public HInvoke {
Calin Juravle175dc732015-08-25 15:42:32 +01004486 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004487 HInvokeUnresolved(ArenaAllocator* allocator,
Calin Juravle175dc732015-08-25 15:42:32 +01004488 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004489 DataType::Type return_type,
Calin Juravle175dc732015-08-25 15:42:32 +01004490 uint32_t dex_pc,
4491 uint32_t dex_method_index,
4492 InvokeType invoke_type)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304493 : HInvoke(kInvokeUnresolved,
4494 allocator,
Calin Juravle175dc732015-08-25 15:42:32 +01004495 number_of_arguments,
Andreas Gampe3db70682018-12-26 15:12:03 -08004496 /* number_of_other_inputs= */ 0u,
Calin Juravle175dc732015-08-25 15:42:32 +01004497 return_type,
4498 dex_pc,
4499 dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004500 nullptr,
Calin Juravle175dc732015-08-25 15:42:32 +01004501 invoke_type) {
4502 }
4503
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004504 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004505
Calin Juravle175dc732015-08-25 15:42:32 +01004506 DECLARE_INSTRUCTION(InvokeUnresolved);
4507
Artem Serovcced8ba2017-07-19 18:18:09 +01004508 protected:
4509 DEFAULT_COPY_CONSTRUCTOR(InvokeUnresolved);
Calin Juravle175dc732015-08-25 15:42:32 +01004510};
4511
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004512class HInvokePolymorphic final : public HInvoke {
Orion Hodsonac141392017-01-13 11:53:47 +00004513 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004514 HInvokePolymorphic(ArenaAllocator* allocator,
Orion Hodsonac141392017-01-13 11:53:47 +00004515 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004516 DataType::Type return_type,
Orion Hodsonac141392017-01-13 11:53:47 +00004517 uint32_t dex_pc,
4518 uint32_t dex_method_index)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304519 : HInvoke(kInvokePolymorphic,
4520 allocator,
Orion Hodsonac141392017-01-13 11:53:47 +00004521 number_of_arguments,
Andreas Gampe3db70682018-12-26 15:12:03 -08004522 /* number_of_other_inputs= */ 0u,
Orion Hodsonac141392017-01-13 11:53:47 +00004523 return_type,
4524 dex_pc,
4525 dex_method_index,
4526 nullptr,
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304527 kVirtual) {
4528 }
Orion Hodsonac141392017-01-13 11:53:47 +00004529
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004530 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004531
Orion Hodsonac141392017-01-13 11:53:47 +00004532 DECLARE_INSTRUCTION(InvokePolymorphic);
4533
Artem Serovcced8ba2017-07-19 18:18:09 +01004534 protected:
4535 DEFAULT_COPY_CONSTRUCTOR(InvokePolymorphic);
Orion Hodsonac141392017-01-13 11:53:47 +00004536};
4537
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004538class HInvokeCustom final : public HInvoke {
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004539 public:
4540 HInvokeCustom(ArenaAllocator* allocator,
4541 uint32_t number_of_arguments,
4542 uint32_t call_site_index,
4543 DataType::Type return_type,
4544 uint32_t dex_pc)
4545 : HInvoke(kInvokeCustom,
4546 allocator,
4547 number_of_arguments,
Andreas Gampe3db70682018-12-26 15:12:03 -08004548 /* number_of_other_inputs= */ 0u,
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004549 return_type,
4550 dex_pc,
Andreas Gampe3db70682018-12-26 15:12:03 -08004551 /* dex_method_index= */ dex::kDexNoIndex,
4552 /* resolved_method= */ nullptr,
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004553 kStatic),
4554 call_site_index_(call_site_index) {
4555 }
4556
4557 uint32_t GetCallSiteIndex() const { return call_site_index_; }
4558
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004559 bool IsClonable() const override { return true; }
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004560
4561 DECLARE_INSTRUCTION(InvokeCustom);
4562
4563 protected:
4564 DEFAULT_COPY_CONSTRUCTOR(InvokeCustom);
4565
4566 private:
4567 uint32_t call_site_index_;
4568};
4569
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004570class HInvokeStaticOrDirect final : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004571 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01004572 // Requirements of this method call regarding the class
4573 // initialization (clinit) check of its declaring class.
4574 enum class ClinitCheckRequirement {
4575 kNone, // Class already initialized.
4576 kExplicit, // Static call having explicit clinit check as last input.
4577 kImplicit, // Static call implicitly requiring a clinit check.
Vladimir Markoa1de9182016-02-25 11:37:38 +00004578 kLast = kImplicit
Roland Levillain4c0eb422015-04-24 16:43:49 +01004579 };
4580
Vladimir Marko58155012015-08-19 12:49:41 +00004581 // Determines how to load the target ArtMethod*.
4582 enum class MethodLoadKind {
4583 // Use a String init ArtMethod* loaded from Thread entrypoints.
4584 kStringInit,
4585
4586 // Use the method's own ArtMethod* loaded by the register allocator.
4587 kRecursive,
4588
Vladimir Marko65979462017-05-19 17:25:12 +01004589 // Use PC-relative boot image ArtMethod* address that will be known at link time.
4590 // Used for boot image methods referenced by boot image code.
4591 kBootImageLinkTimePcRelative,
4592
Vladimir Markob066d432018-01-03 13:14:37 +00004593 // Load from an entry in the .data.bimg.rel.ro using a PC-relative load.
4594 // Used for app->boot calls with relocatable image.
4595 kBootImageRelRo,
4596
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004597 // Load from an entry in the .bss section using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004598 // Used for methods outside boot image referenced by AOT-compiled app and boot image code.
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004599 kBssEntry,
Vladimir Marko58155012015-08-19 12:49:41 +00004600
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004601 // Use ArtMethod* at a known address, embed the direct address in the code.
4602 // Used for for JIT-compiled calls.
4603 kJitDirectAddress,
4604
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004605 // Make a runtime call to resolve and call the method. This is the last-resort-kind
4606 // used when other kinds are unimplemented on a particular architecture.
4607 kRuntimeCall,
Vladimir Marko58155012015-08-19 12:49:41 +00004608 };
4609
4610 // Determines the location of the code pointer.
4611 enum class CodePtrLocation {
4612 // Recursive call, use local PC-relative call instruction.
4613 kCallSelf,
4614
Vladimir Marko58155012015-08-19 12:49:41 +00004615 // Use code pointer from the ArtMethod*.
4616 // Used when we don't know the target code. This is also the last-resort-kind used when
4617 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
4618 kCallArtMethod,
4619 };
4620
4621 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01004622 MethodLoadKind method_load_kind;
4623 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00004624 // The method load data holds
4625 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
4626 // Note that there are multiple string init methods, each having its own offset.
4627 // - the method address for kDirectAddress
Vladimir Markodc151b22015-10-15 18:02:30 +01004628 uint64_t method_load_data;
Vladimir Marko58155012015-08-19 12:49:41 +00004629 };
4630
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004631 HInvokeStaticOrDirect(ArenaAllocator* allocator,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004632 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004633 DataType::Type return_type,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004634 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004635 uint32_t method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004636 ArtMethod* resolved_method,
Vladimir Marko58155012015-08-19 12:49:41 +00004637 DispatchInfo dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004638 InvokeType invoke_type,
4639 MethodReference target_method,
Roland Levillain4c0eb422015-04-24 16:43:49 +01004640 ClinitCheckRequirement clinit_check_requirement)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304641 : HInvoke(kInvokeStaticOrDirect,
4642 allocator,
Roland Levillain3e3d7332015-04-28 11:00:54 +01004643 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00004644 // There is potentially one extra argument for the HCurrentMethod node, and
Nicolas Geoffray0846a8f2018-09-12 15:21:07 +01004645 // potentially one other if the clinit check is explicit.
Vladimir Markob554b5a2015-11-06 12:57:55 +00004646 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
David Brazdildee58d62016-04-07 09:54:26 +00004647 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004648 return_type,
4649 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004650 method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004651 resolved_method,
4652 invoke_type),
Vladimir Marko58155012015-08-19 12:49:41 +00004653 target_method_(target_method),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004654 dispatch_info_(dispatch_info) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004655 SetPackedField<ClinitCheckRequirementField>(clinit_check_requirement);
4656 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004657
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004658 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004659
Vladimir Markodc151b22015-10-15 18:02:30 +01004660 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00004661 bool had_current_method_input = HasCurrentMethodInput();
4662 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
4663
4664 // Using the current method is the default and once we find a better
4665 // method load kind, we should not go back to using the current method.
4666 DCHECK(had_current_method_input || !needs_current_method_input);
4667
4668 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004669 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
4670 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004671 }
Vladimir Markodc151b22015-10-15 18:02:30 +01004672 dispatch_info_ = dispatch_info;
4673 }
4674
Aart Bik6daebeb2017-04-03 14:35:41 -07004675 DispatchInfo GetDispatchInfo() const {
4676 return dispatch_info_;
4677 }
4678
Vladimir Markoc53c0792015-11-19 15:48:33 +00004679 void AddSpecialInput(HInstruction* input) {
4680 // We allow only one special input.
4681 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
4682 DCHECK(InputCount() == GetSpecialInputIndex() ||
4683 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
4684 InsertInputAt(GetSpecialInputIndex(), input);
4685 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004686
Vladimir Marko372f10e2016-05-17 16:30:10 +01004687 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004688 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01004689 ArrayRef<HUserRecord<HInstruction*>> input_records = HInvoke::GetInputRecords();
4690 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck()) {
4691 DCHECK(!input_records.empty());
4692 DCHECK_GT(input_records.size(), GetNumberOfArguments());
4693 HInstruction* last_input = input_records.back().GetInstruction();
4694 // Note: `last_input` may be null during arguments setup.
4695 if (last_input != nullptr) {
4696 // `last_input` is the last input of a static invoke marked as having
4697 // an explicit clinit check. It must either be:
4698 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
4699 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
4700 DCHECK(last_input->IsClinitCheck() || last_input->IsLoadClass()) << last_input->DebugName();
4701 }
4702 }
4703 return input_records;
4704 }
4705
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004706 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const override {
Calin Juravle77520bc2015-01-12 18:45:46 +00004707 // We access the method via the dex cache so we can't do an implicit null check.
4708 // TODO: for intrinsics we can generate implicit null checks.
4709 return false;
4710 }
4711
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004712 bool CanBeNull() const override {
Vladimir Markobd785672018-05-03 17:09:09 +01004713 return GetType() == DataType::Type::kReference && !IsStringInit();
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004714 }
4715
Vladimir Markoc53c0792015-11-19 15:48:33 +00004716 // Get the index of the special input, if any.
4717 //
David Brazdil6de19382016-01-08 17:37:10 +00004718 // If the invoke HasCurrentMethodInput(), the "special input" is the current
4719 // method pointer; otherwise there may be one platform-specific special input,
4720 // such as PC-relative addressing base.
Vladimir Markoc53c0792015-11-19 15:48:33 +00004721 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray97793072016-02-16 15:33:54 +00004722 bool HasSpecialInput() const { return GetNumberOfArguments() != InputCount(); }
Vladimir Markoc53c0792015-11-19 15:48:33 +00004723
Vladimir Marko58155012015-08-19 12:49:41 +00004724 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
4725 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
4726 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004727 bool NeedsDexCacheOfDeclaringClass() const override;
Vladimir Marko58155012015-08-19 12:49:41 +00004728 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004729 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kJitDirectAddress; }
Vladimir Marko65979462017-05-19 17:25:12 +01004730 bool HasPcRelativeMethodLoadKind() const {
4731 return GetMethodLoadKind() == MethodLoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markob066d432018-01-03 13:14:37 +00004732 GetMethodLoadKind() == MethodLoadKind::kBootImageRelRo ||
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004733 GetMethodLoadKind() == MethodLoadKind::kBssEntry;
Vladimir Marko65979462017-05-19 17:25:12 +01004734 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004735 bool HasCurrentMethodInput() const {
4736 // This function can be called only after the invoke has been fully initialized by the builder.
4737 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004738 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004739 return true;
4740 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004741 DCHECK(InputCount() == GetSpecialInputIndex() ||
4742 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004743 return false;
4744 }
4745 }
Vladimir Marko58155012015-08-19 12:49:41 +00004746
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004747 QuickEntrypointEnum GetStringInitEntryPoint() const {
Vladimir Marko58155012015-08-19 12:49:41 +00004748 DCHECK(IsStringInit());
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004749 return static_cast<QuickEntrypointEnum>(dispatch_info_.method_load_data);
Vladimir Marko58155012015-08-19 12:49:41 +00004750 }
4751
4752 uint64_t GetMethodAddress() const {
4753 DCHECK(HasMethodAddress());
4754 return dispatch_info_.method_load_data;
4755 }
4756
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00004757 const DexFile& GetDexFileForPcRelativeDexCache() const;
4758
Vladimir Markoa1de9182016-02-25 11:37:38 +00004759 ClinitCheckRequirement GetClinitCheckRequirement() const {
4760 return GetPackedField<ClinitCheckRequirementField>();
4761 }
Calin Juravle68ad6492015-08-18 17:08:12 +01004762
Roland Levillain4c0eb422015-04-24 16:43:49 +01004763 // Is this instruction a call to a static method?
4764 bool IsStatic() const {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004765 return GetInvokeType() == kStatic;
4766 }
4767
4768 MethodReference GetTargetMethod() const {
4769 return target_method_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01004770 }
4771
Vladimir Markofbb184a2015-11-13 14:47:00 +00004772 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
4773 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
4774 // instruction; only relevant for static calls with explicit clinit check.
4775 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01004776 DCHECK(IsStaticWithExplicitClinitCheck());
Vladimir Marko372f10e2016-05-17 16:30:10 +01004777 size_t last_input_index = inputs_.size() - 1u;
4778 HInstruction* last_input = inputs_.back().GetInstruction();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004779 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004780 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004781 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004782 inputs_.pop_back();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004783 SetPackedField<ClinitCheckRequirementField>(new_requirement);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004784 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004785 }
4786
4787 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00004788 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01004789 bool IsStaticWithExplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004790 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kExplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004791 }
4792
4793 // Is this a call to a static method whose declaring class has an
4794 // implicit intialization check requirement?
4795 bool IsStaticWithImplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004796 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kImplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004797 }
4798
Vladimir Markob554b5a2015-11-06 12:57:55 +00004799 // Does this method load kind need the current method as an input?
4800 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004801 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kRuntimeCall;
Vladimir Markob554b5a2015-11-06 12:57:55 +00004802 }
4803
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004804 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004805
Artem Serovcced8ba2017-07-19 18:18:09 +01004806 protected:
4807 DEFAULT_COPY_CONSTRUCTOR(InvokeStaticOrDirect);
4808
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004809 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004810 static constexpr size_t kFieldClinitCheckRequirement = kNumberOfInvokePackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004811 static constexpr size_t kFieldClinitCheckRequirementSize =
4812 MinimumBitsToStore(static_cast<size_t>(ClinitCheckRequirement::kLast));
4813 static constexpr size_t kNumberOfInvokeStaticOrDirectPackedBits =
4814 kFieldClinitCheckRequirement + kFieldClinitCheckRequirementSize;
4815 static_assert(kNumberOfInvokeStaticOrDirectPackedBits <= kMaxNumberOfPackedBits,
4816 "Too many packed fields.");
Vladimir Markoa1de9182016-02-25 11:37:38 +00004817 using ClinitCheckRequirementField = BitField<ClinitCheckRequirement,
4818 kFieldClinitCheckRequirement,
4819 kFieldClinitCheckRequirementSize>;
4820
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004821 // Cached values of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffrayea179f42018-02-08 22:30:18 +00004822 const MethodReference target_method_;
Vladimir Marko58155012015-08-19 12:49:41 +00004823 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004824};
Vladimir Markof64242a2015-12-01 14:58:23 +00004825std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004826std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004827
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004828class HInvokeVirtual final : public HInvoke {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004829 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004830 HInvokeVirtual(ArenaAllocator* allocator,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004831 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004832 DataType::Type return_type,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004833 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004834 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004835 ArtMethod* resolved_method,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004836 uint32_t vtable_index)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304837 : HInvoke(kInvokeVirtual,
4838 allocator,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004839 number_of_arguments,
4840 0u,
4841 return_type,
4842 dex_pc,
4843 dex_method_index,
4844 resolved_method,
4845 kVirtual),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304846 vtable_index_(vtable_index) {
4847 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004848
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004849 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004850
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004851 bool CanBeNull() const override {
Aart Bik71bf7b42016-11-16 10:17:46 -08004852 switch (GetIntrinsic()) {
4853 case Intrinsics::kThreadCurrentThread:
4854 case Intrinsics::kStringBufferAppend:
4855 case Intrinsics::kStringBufferToString:
Vladimir Markod4561172017-10-30 17:48:25 +00004856 case Intrinsics::kStringBuilderAppendObject:
4857 case Intrinsics::kStringBuilderAppendString:
4858 case Intrinsics::kStringBuilderAppendCharSequence:
4859 case Intrinsics::kStringBuilderAppendCharArray:
4860 case Intrinsics::kStringBuilderAppendBoolean:
4861 case Intrinsics::kStringBuilderAppendChar:
4862 case Intrinsics::kStringBuilderAppendInt:
4863 case Intrinsics::kStringBuilderAppendLong:
4864 case Intrinsics::kStringBuilderAppendFloat:
4865 case Intrinsics::kStringBuilderAppendDouble:
Aart Bik71bf7b42016-11-16 10:17:46 -08004866 case Intrinsics::kStringBuilderToString:
4867 return false;
4868 default:
4869 return HInvoke::CanBeNull();
4870 }
4871 }
4872
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004873 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Calin Juravle77520bc2015-01-12 18:45:46 +00004874 // TODO: Add implicit null checks in intrinsics.
Nicolas Geoffray61ba8d22018-08-07 09:55:57 +01004875 return (obj == InputAt(0)) && !IsIntrinsic();
Calin Juravle77520bc2015-01-12 18:45:46 +00004876 }
4877
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004878 uint32_t GetVTableIndex() const { return vtable_index_; }
4879
4880 DECLARE_INSTRUCTION(InvokeVirtual);
4881
Artem Serovcced8ba2017-07-19 18:18:09 +01004882 protected:
4883 DEFAULT_COPY_CONSTRUCTOR(InvokeVirtual);
4884
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004885 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004886 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004887 const uint32_t vtable_index_;
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004888};
4889
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004890class HInvokeInterface final : public HInvoke {
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004891 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004892 HInvokeInterface(ArenaAllocator* allocator,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004893 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004894 DataType::Type return_type,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004895 uint32_t dex_pc,
4896 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004897 ArtMethod* resolved_method,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004898 uint32_t imt_index)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304899 : HInvoke(kInvokeInterface,
4900 allocator,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004901 number_of_arguments,
4902 0u,
4903 return_type,
4904 dex_pc,
4905 dex_method_index,
4906 resolved_method,
4907 kInterface),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304908 imt_index_(imt_index) {
4909 }
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004910
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004911 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004912
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004913 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Calin Juravle77520bc2015-01-12 18:45:46 +00004914 // TODO: Add implicit null checks in intrinsics.
Nicolas Geoffray61ba8d22018-08-07 09:55:57 +01004915 return (obj == InputAt(0)) && !IsIntrinsic();
Calin Juravle77520bc2015-01-12 18:45:46 +00004916 }
4917
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004918 bool NeedsDexCacheOfDeclaringClass() const override {
Nicolas Geoffrayfbdfa6d2017-02-03 10:43:13 +00004919 // The assembly stub currently needs it.
4920 return true;
4921 }
4922
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004923 uint32_t GetImtIndex() const { return imt_index_; }
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004924
4925 DECLARE_INSTRUCTION(InvokeInterface);
4926
Artem Serovcced8ba2017-07-19 18:18:09 +01004927 protected:
4928 DEFAULT_COPY_CONSTRUCTOR(InvokeInterface);
4929
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004930 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004931 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004932 const uint32_t imt_index_;
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004933};
4934
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004935class HNeg final : public HUnaryOperation {
Roland Levillain88cb1752014-10-20 16:36:47 +01004936 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004937 HNeg(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304938 : HUnaryOperation(kNeg, result_type, input, dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004939 DCHECK_EQ(result_type, DataType::Kind(input->GetType()));
Roland Levillain937e6cd2016-03-22 11:54:37 +00004940 }
Roland Levillain88cb1752014-10-20 16:36:47 +01004941
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004942 template <typename T> static T Compute(T x) { return -x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004943
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004944 HConstant* Evaluate(HIntConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004945 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004946 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004947 HConstant* Evaluate(HLongConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004948 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004949 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004950 HConstant* Evaluate(HFloatConstant* x) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004951 return GetBlock()->GetGraph()->GetFloatConstant(Compute(x->GetValue()), GetDexPc());
4952 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004953 HConstant* Evaluate(HDoubleConstant* x) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004954 return GetBlock()->GetGraph()->GetDoubleConstant(Compute(x->GetValue()), GetDexPc());
4955 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01004956
Roland Levillain88cb1752014-10-20 16:36:47 +01004957 DECLARE_INSTRUCTION(Neg);
4958
Artem Serovcced8ba2017-07-19 18:18:09 +01004959 protected:
4960 DEFAULT_COPY_CONSTRUCTOR(Neg);
Roland Levillain88cb1752014-10-20 16:36:47 +01004961};
4962
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004963class HNewArray final : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004964 public:
Vladimir Markob5461632018-10-15 14:24:21 +01004965 HNewArray(HInstruction* cls, HInstruction* length, uint32_t dex_pc, size_t component_size_shift)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304966 : HExpression(kNewArray, DataType::Type::kReference, SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004967 SetRawInputAt(0, cls);
4968 SetRawInputAt(1, length);
Vladimir Markob5461632018-10-15 14:24:21 +01004969 SetPackedField<ComponentSizeShiftField>(component_size_shift);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004970 }
4971
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004972 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004973
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004974 // Calls runtime so needs an environment.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004975 bool NeedsEnvironment() const override { return true; }
Calin Juravle10e244f2015-01-26 18:54:32 +00004976
Mingyao Yang0c365e62015-03-31 15:09:29 -07004977 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004978 bool CanThrow() const override { return true; }
Mingyao Yang0c365e62015-03-31 15:09:29 -07004979
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004980 bool CanBeNull() const override { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004981
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004982 HLoadClass* GetLoadClass() const {
4983 DCHECK(InputAt(0)->IsLoadClass());
4984 return InputAt(0)->AsLoadClass();
4985 }
4986
4987 HInstruction* GetLength() const {
4988 return InputAt(1);
4989 }
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004990
Vladimir Markob5461632018-10-15 14:24:21 +01004991 size_t GetComponentSizeShift() {
4992 return GetPackedField<ComponentSizeShiftField>();
4993 }
4994
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004995 DECLARE_INSTRUCTION(NewArray);
4996
Artem Serovcced8ba2017-07-19 18:18:09 +01004997 protected:
4998 DEFAULT_COPY_CONSTRUCTOR(NewArray);
Vladimir Markob5461632018-10-15 14:24:21 +01004999
5000 private:
5001 static constexpr size_t kFieldComponentSizeShift = kNumberOfGenericPackedBits;
5002 static constexpr size_t kFieldComponentSizeShiftSize = MinimumBitsToStore(3u);
5003 static constexpr size_t kNumberOfNewArrayPackedBits =
5004 kFieldComponentSizeShift + kFieldComponentSizeShiftSize;
5005 static_assert(kNumberOfNewArrayPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5006 using ComponentSizeShiftField =
5007 BitField<size_t, kFieldComponentSizeShift, kFieldComponentSizeShift>;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01005008};
5009
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005010class HAdd final : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00005011 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005012 HAdd(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005013 HInstruction* left,
5014 HInstruction* right,
5015 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305016 : HBinaryOperation(kAdd, result_type, left, right, SideEffects::None(), dex_pc) {
5017 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00005018
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005019 bool IsCommutative() const override { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00005020
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005021 template <typename T> static T Compute(T x, T y) { return x + y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005022
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005023 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005024 return GetBlock()->GetGraph()->GetIntConstant(
5025 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01005026 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005027 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005028 return GetBlock()->GetGraph()->GetLongConstant(
5029 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01005030 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005031 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005032 return GetBlock()->GetGraph()->GetFloatConstant(
5033 Compute(x->GetValue(), y->GetValue()), GetDexPc());
5034 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005035 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005036 return GetBlock()->GetGraph()->GetDoubleConstant(
5037 Compute(x->GetValue(), y->GetValue()), GetDexPc());
5038 }
Roland Levillain556c3d12014-09-18 15:25:07 +01005039
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00005040 DECLARE_INSTRUCTION(Add);
5041
Artem Serovcced8ba2017-07-19 18:18:09 +01005042 protected:
5043 DEFAULT_COPY_CONSTRUCTOR(Add);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00005044};
5045
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005046class HSub final : public HBinaryOperation {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005047 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005048 HSub(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005049 HInstruction* left,
5050 HInstruction* right,
5051 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305052 : HBinaryOperation(kSub, result_type, left, right, SideEffects::None(), dex_pc) {
5053 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005054
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005055 template <typename T> static T Compute(T x, T y) { return x - y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005056
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005057 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005058 return GetBlock()->GetGraph()->GetIntConstant(
5059 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01005060 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005061 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005062 return GetBlock()->GetGraph()->GetLongConstant(
5063 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01005064 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005065 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005066 return GetBlock()->GetGraph()->GetFloatConstant(
5067 Compute(x->GetValue(), y->GetValue()), GetDexPc());
5068 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005069 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005070 return GetBlock()->GetGraph()->GetDoubleConstant(
5071 Compute(x->GetValue(), y->GetValue()), GetDexPc());
5072 }
Roland Levillain556c3d12014-09-18 15:25:07 +01005073
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005074 DECLARE_INSTRUCTION(Sub);
5075
Artem Serovcced8ba2017-07-19 18:18:09 +01005076 protected:
5077 DEFAULT_COPY_CONSTRUCTOR(Sub);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005078};
5079
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005080class HMul final : public HBinaryOperation {
Calin Juravle34bacdf2014-10-07 20:23:36 +01005081 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005082 HMul(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005083 HInstruction* left,
5084 HInstruction* right,
5085 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305086 : HBinaryOperation(kMul, result_type, left, right, SideEffects::None(), dex_pc) {
5087 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01005088
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005089 bool IsCommutative() const override { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01005090
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005091 template <typename T> static T Compute(T x, T y) { return x * y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005092
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005093 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005094 return GetBlock()->GetGraph()->GetIntConstant(
5095 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005096 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005097 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005098 return GetBlock()->GetGraph()->GetLongConstant(
5099 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005100 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005101 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005102 return GetBlock()->GetGraph()->GetFloatConstant(
5103 Compute(x->GetValue(), y->GetValue()), GetDexPc());
5104 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005105 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005106 return GetBlock()->GetGraph()->GetDoubleConstant(
5107 Compute(x->GetValue(), y->GetValue()), GetDexPc());
5108 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01005109
5110 DECLARE_INSTRUCTION(Mul);
5111
Artem Serovcced8ba2017-07-19 18:18:09 +01005112 protected:
5113 DEFAULT_COPY_CONSTRUCTOR(Mul);
Calin Juravle34bacdf2014-10-07 20:23:36 +01005114};
5115
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005116class HDiv final : public HBinaryOperation {
Calin Juravle7c4954d2014-10-28 16:57:40 +00005117 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005118 HDiv(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005119 HInstruction* left,
5120 HInstruction* right,
5121 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305122 : HBinaryOperation(kDiv, result_type, left, right, SideEffects::None(), dex_pc) {
5123 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00005124
Roland Levillain9867bc72015-08-05 10:21:34 +01005125 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00005126 T ComputeIntegral(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005127 DCHECK(!DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01005128 // Our graph structure ensures we never have 0 for `y` during
5129 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00005130 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00005131 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00005132 return (y == -1) ? -x : x / y;
5133 }
Calin Juravlebacfec32014-11-14 15:54:36 +00005134
Roland Levillain31dd3d62016-02-16 12:21:02 +00005135 template <typename T>
5136 T ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005137 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00005138 return x / y;
5139 }
5140
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005141 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005142 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00005143 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005144 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005145 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005146 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00005147 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5148 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005149 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005150 return GetBlock()->GetGraph()->GetFloatConstant(
5151 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
5152 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005153 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005154 return GetBlock()->GetGraph()->GetDoubleConstant(
5155 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00005156 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00005157
5158 DECLARE_INSTRUCTION(Div);
5159
Artem Serovcced8ba2017-07-19 18:18:09 +01005160 protected:
5161 DEFAULT_COPY_CONSTRUCTOR(Div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00005162};
5163
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005164class HRem final : public HBinaryOperation {
Calin Juravlebacfec32014-11-14 15:54:36 +00005165 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005166 HRem(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005167 HInstruction* left,
5168 HInstruction* right,
5169 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305170 : HBinaryOperation(kRem, result_type, left, right, SideEffects::None(), dex_pc) {
5171 }
Calin Juravlebacfec32014-11-14 15:54:36 +00005172
Roland Levillain9867bc72015-08-05 10:21:34 +01005173 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00005174 T ComputeIntegral(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005175 DCHECK(!DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01005176 // Our graph structure ensures we never have 0 for `y` during
5177 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00005178 DCHECK_NE(y, 0);
5179 // Special case -1 to avoid getting a SIGFPE on x86(_64).
5180 return (y == -1) ? 0 : x % y;
5181 }
5182
Roland Levillain31dd3d62016-02-16 12:21:02 +00005183 template <typename T>
5184 T ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005185 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00005186 return std::fmod(x, y);
5187 }
5188
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005189 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005190 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00005191 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005192 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005193 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005194 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00005195 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00005196 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005197 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005198 return GetBlock()->GetGraph()->GetFloatConstant(
5199 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
5200 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005201 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005202 return GetBlock()->GetGraph()->GetDoubleConstant(
5203 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
5204 }
Calin Juravlebacfec32014-11-14 15:54:36 +00005205
5206 DECLARE_INSTRUCTION(Rem);
5207
Artem Serovcced8ba2017-07-19 18:18:09 +01005208 protected:
5209 DEFAULT_COPY_CONSTRUCTOR(Rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00005210};
5211
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005212class HMin final : public HBinaryOperation {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005213 public:
5214 HMin(DataType::Type result_type,
5215 HInstruction* left,
5216 HInstruction* right,
5217 uint32_t dex_pc)
5218 : HBinaryOperation(kMin, result_type, left, right, SideEffects::None(), dex_pc) {}
5219
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005220 bool IsCommutative() const override { return true; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005221
5222 // Evaluation for integral values.
5223 template <typename T> static T ComputeIntegral(T x, T y) {
5224 return (x <= y) ? x : y;
5225 }
5226
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005227 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005228 return GetBlock()->GetGraph()->GetIntConstant(
5229 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5230 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005231 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005232 return GetBlock()->GetGraph()->GetLongConstant(
5233 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5234 }
5235 // TODO: Evaluation for floating-point values.
5236 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005237 HFloatConstant* y ATTRIBUTE_UNUSED) const override { return nullptr; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005238 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005239 HDoubleConstant* y ATTRIBUTE_UNUSED) const override { return nullptr; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005240
5241 DECLARE_INSTRUCTION(Min);
5242
5243 protected:
5244 DEFAULT_COPY_CONSTRUCTOR(Min);
5245};
5246
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005247class HMax final : public HBinaryOperation {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005248 public:
5249 HMax(DataType::Type result_type,
5250 HInstruction* left,
5251 HInstruction* right,
5252 uint32_t dex_pc)
5253 : HBinaryOperation(kMax, result_type, left, right, SideEffects::None(), dex_pc) {}
5254
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005255 bool IsCommutative() const override { return true; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005256
5257 // Evaluation for integral values.
5258 template <typename T> static T ComputeIntegral(T x, T y) {
5259 return (x >= y) ? x : y;
5260 }
5261
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005262 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005263 return GetBlock()->GetGraph()->GetIntConstant(
5264 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5265 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005266 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005267 return GetBlock()->GetGraph()->GetLongConstant(
5268 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5269 }
5270 // TODO: Evaluation for floating-point values.
5271 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005272 HFloatConstant* y ATTRIBUTE_UNUSED) const override { return nullptr; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005273 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005274 HDoubleConstant* y ATTRIBUTE_UNUSED) const override { return nullptr; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005275
5276 DECLARE_INSTRUCTION(Max);
5277
5278 protected:
5279 DEFAULT_COPY_CONSTRUCTOR(Max);
5280};
5281
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005282class HAbs final : public HUnaryOperation {
Aart Bik3dad3412018-02-28 12:01:46 -08005283 public:
5284 HAbs(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
5285 : HUnaryOperation(kAbs, result_type, input, dex_pc) {}
5286
5287 // Evaluation for integral values.
5288 template <typename T> static T ComputeIntegral(T x) {
5289 return x < 0 ? -x : x;
5290 }
5291
5292 // Evaluation for floating-point values.
5293 // Note, as a "quality of implementation", rather than pure "spec compliance",
5294 // we require that Math.abs() clears the sign bit (but changes nothing else)
5295 // for all floating-point numbers, including NaN (signaling NaN may become quiet though).
5296 // http://b/30758343
5297 template <typename T, typename S> static T ComputeFP(T x) {
5298 S bits = bit_cast<S, T>(x);
5299 return bit_cast<T, S>(bits & std::numeric_limits<S>::max());
5300 }
5301
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005302 HConstant* Evaluate(HIntConstant* x) const override {
Aart Bik3dad3412018-02-28 12:01:46 -08005303 return GetBlock()->GetGraph()->GetIntConstant(ComputeIntegral(x->GetValue()), GetDexPc());
5304 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005305 HConstant* Evaluate(HLongConstant* x) const override {
Aart Bik3dad3412018-02-28 12:01:46 -08005306 return GetBlock()->GetGraph()->GetLongConstant(ComputeIntegral(x->GetValue()), GetDexPc());
5307 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005308 HConstant* Evaluate(HFloatConstant* x) const override {
Aart Bik3dad3412018-02-28 12:01:46 -08005309 return GetBlock()->GetGraph()->GetFloatConstant(
5310 ComputeFP<float, int32_t>(x->GetValue()), GetDexPc());
5311 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005312 HConstant* Evaluate(HDoubleConstant* x) const override {
Aart Bik3dad3412018-02-28 12:01:46 -08005313 return GetBlock()->GetGraph()->GetDoubleConstant(
5314 ComputeFP<double, int64_t>(x->GetValue()), GetDexPc());
5315 }
5316
5317 DECLARE_INSTRUCTION(Abs);
5318
5319 protected:
5320 DEFAULT_COPY_CONSTRUCTOR(Abs);
5321};
5322
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005323class HDivZeroCheck final : public HExpression<1> {
Calin Juravled0d48522014-11-04 16:40:20 +00005324 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00005325 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
Artem Serovd1aa7d02018-06-22 11:35:46 +01005326 // constructor. However it can only do it on a fatal slow path so execution never returns to the
5327 // instruction following the current one; thus 'SideEffects::None()' is used.
Calin Juravled0d48522014-11-04 16:40:20 +00005328 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Artem Serovd1aa7d02018-06-22 11:35:46 +01005329 : HExpression(kDivZeroCheck, value->GetType(), SideEffects::None(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00005330 SetRawInputAt(0, value);
5331 }
5332
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005333 bool IsClonable() const override { return true; }
5334 bool CanBeMoved() const override { return true; }
Calin Juravled0d48522014-11-04 16:40:20 +00005335
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005336 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Calin Juravled0d48522014-11-04 16:40:20 +00005337 return true;
5338 }
5339
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005340 bool NeedsEnvironment() const override { return true; }
5341 bool CanThrow() const override { return true; }
Calin Juravled0d48522014-11-04 16:40:20 +00005342
Calin Juravled0d48522014-11-04 16:40:20 +00005343 DECLARE_INSTRUCTION(DivZeroCheck);
5344
Artem Serovcced8ba2017-07-19 18:18:09 +01005345 protected:
5346 DEFAULT_COPY_CONSTRUCTOR(DivZeroCheck);
Calin Juravled0d48522014-11-04 16:40:20 +00005347};
5348
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005349class HShl final : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005350 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005351 HShl(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005352 HInstruction* value,
5353 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005354 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305355 : HBinaryOperation(kShl, result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005356 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5357 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01005358 }
5359
Roland Levillain5b5b9312016-03-22 14:57:31 +00005360 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005361 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005362 return value << (distance & max_shift_distance);
5363 }
5364
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005365 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005366 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005367 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005368 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005369 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005370 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005371 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005372 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005373 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005374 HLongConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005375 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5376 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01005377 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005378 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005379 HFloatConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005380 LOG(FATAL) << DebugName() << " is not defined for float values";
5381 UNREACHABLE();
5382 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005383 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005384 HDoubleConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005385 LOG(FATAL) << DebugName() << " is not defined for double values";
5386 UNREACHABLE();
5387 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005388
5389 DECLARE_INSTRUCTION(Shl);
5390
Artem Serovcced8ba2017-07-19 18:18:09 +01005391 protected:
5392 DEFAULT_COPY_CONSTRUCTOR(Shl);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005393};
5394
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005395class HShr final : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005396 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005397 HShr(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005398 HInstruction* value,
5399 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005400 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305401 : HBinaryOperation(kShr, result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005402 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5403 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01005404 }
5405
Roland Levillain5b5b9312016-03-22 14:57:31 +00005406 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005407 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005408 return value >> (distance & max_shift_distance);
5409 }
5410
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005411 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005412 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005413 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005414 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005415 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005416 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005417 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005418 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005419 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005420 HLongConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005421 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5422 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01005423 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005424 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005425 HFloatConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005426 LOG(FATAL) << DebugName() << " is not defined for float values";
5427 UNREACHABLE();
5428 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005429 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005430 HDoubleConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005431 LOG(FATAL) << DebugName() << " is not defined for double values";
5432 UNREACHABLE();
5433 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005434
5435 DECLARE_INSTRUCTION(Shr);
5436
Artem Serovcced8ba2017-07-19 18:18:09 +01005437 protected:
5438 DEFAULT_COPY_CONSTRUCTOR(Shr);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005439};
5440
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005441class HUShr final : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005442 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005443 HUShr(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005444 HInstruction* value,
5445 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005446 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305447 : HBinaryOperation(kUShr, result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005448 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5449 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Calin Juravle9aec02f2014-11-18 23:06:35 +00005450 }
5451
Roland Levillain5b5b9312016-03-22 14:57:31 +00005452 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005453 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005454 typedef typename std::make_unsigned<T>::type V;
5455 V ux = static_cast<V>(value);
5456 return static_cast<T>(ux >> (distance & max_shift_distance));
5457 }
5458
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005459 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005460 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005461 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005462 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005463 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005464 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005465 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005466 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005467 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005468 HLongConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005469 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5470 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00005471 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005472 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005473 HFloatConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005474 LOG(FATAL) << DebugName() << " is not defined for float values";
5475 UNREACHABLE();
5476 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005477 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005478 HDoubleConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005479 LOG(FATAL) << DebugName() << " is not defined for double values";
5480 UNREACHABLE();
5481 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005482
5483 DECLARE_INSTRUCTION(UShr);
5484
Artem Serovcced8ba2017-07-19 18:18:09 +01005485 protected:
5486 DEFAULT_COPY_CONSTRUCTOR(UShr);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005487};
5488
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005489class HAnd final : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005490 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005491 HAnd(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005492 HInstruction* left,
5493 HInstruction* right,
5494 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305495 : HBinaryOperation(kAnd, result_type, left, right, SideEffects::None(), dex_pc) {
5496 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005497
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005498 bool IsCommutative() const override { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005499
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005500 template <typename T> static T Compute(T x, T y) { return x & y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005501
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005502 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005503 return GetBlock()->GetGraph()->GetIntConstant(
5504 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005505 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005506 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005507 return GetBlock()->GetGraph()->GetLongConstant(
5508 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005509 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005510 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005511 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005512 LOG(FATAL) << DebugName() << " is not defined for float values";
5513 UNREACHABLE();
5514 }
5515 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005516 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005517 LOG(FATAL) << DebugName() << " is not defined for double values";
5518 UNREACHABLE();
5519 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005520
5521 DECLARE_INSTRUCTION(And);
5522
Artem Serovcced8ba2017-07-19 18:18:09 +01005523 protected:
5524 DEFAULT_COPY_CONSTRUCTOR(And);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005525};
5526
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005527class HOr final : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005528 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005529 HOr(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005530 HInstruction* left,
5531 HInstruction* right,
5532 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305533 : HBinaryOperation(kOr, result_type, left, right, SideEffects::None(), dex_pc) {
5534 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005535
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005536 bool IsCommutative() const override { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005537
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005538 template <typename T> static T Compute(T x, T y) { return x | y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005539
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005540 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005541 return GetBlock()->GetGraph()->GetIntConstant(
5542 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005543 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005544 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005545 return GetBlock()->GetGraph()->GetLongConstant(
5546 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005547 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005548 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005549 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005550 LOG(FATAL) << DebugName() << " is not defined for float values";
5551 UNREACHABLE();
5552 }
5553 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005554 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005555 LOG(FATAL) << DebugName() << " is not defined for double values";
5556 UNREACHABLE();
5557 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005558
5559 DECLARE_INSTRUCTION(Or);
5560
Artem Serovcced8ba2017-07-19 18:18:09 +01005561 protected:
5562 DEFAULT_COPY_CONSTRUCTOR(Or);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005563};
5564
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005565class HXor final : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005566 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005567 HXor(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005568 HInstruction* left,
5569 HInstruction* right,
5570 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305571 : HBinaryOperation(kXor, result_type, left, right, SideEffects::None(), dex_pc) {
5572 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005573
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005574 bool IsCommutative() const override { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005575
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005576 template <typename T> static T Compute(T x, T y) { return x ^ y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005577
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005578 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005579 return GetBlock()->GetGraph()->GetIntConstant(
5580 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005581 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005582 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005583 return GetBlock()->GetGraph()->GetLongConstant(
5584 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005585 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005586 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005587 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005588 LOG(FATAL) << DebugName() << " is not defined for float values";
5589 UNREACHABLE();
5590 }
5591 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005592 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005593 LOG(FATAL) << DebugName() << " is not defined for double values";
5594 UNREACHABLE();
5595 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005596
5597 DECLARE_INSTRUCTION(Xor);
5598
Artem Serovcced8ba2017-07-19 18:18:09 +01005599 protected:
5600 DEFAULT_COPY_CONSTRUCTOR(Xor);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005601};
5602
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005603class HRor final : public HBinaryOperation {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005604 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005605 HRor(DataType::Type result_type, HInstruction* value, HInstruction* distance)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305606 : HBinaryOperation(kRor, result_type, value, distance) {
Roland Levillain22c49222016-03-18 14:04:28 +00005607 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005608
Roland Levillain5b5b9312016-03-22 14:57:31 +00005609 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005610 static T Compute(T value, int32_t distance, int32_t max_shift_value) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005611 typedef typename std::make_unsigned<T>::type V;
5612 V ux = static_cast<V>(value);
5613 if ((distance & max_shift_value) == 0) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005614 return static_cast<T>(ux);
5615 } else {
5616 const V reg_bits = sizeof(T) * 8;
Roland Levillain5b5b9312016-03-22 14:57:31 +00005617 return static_cast<T>(ux >> (distance & max_shift_value)) |
5618 (value << (reg_bits - (distance & max_shift_value)));
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005619 }
5620 }
5621
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005622 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const override {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005623 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005624 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005625 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005626 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const override {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005627 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005628 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005629 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005630 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005631 HLongConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005632 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5633 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005634 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005635 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005636 HFloatConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005637 LOG(FATAL) << DebugName() << " is not defined for float values";
5638 UNREACHABLE();
5639 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005640 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005641 HDoubleConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005642 LOG(FATAL) << DebugName() << " is not defined for double values";
5643 UNREACHABLE();
5644 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005645
5646 DECLARE_INSTRUCTION(Ror);
5647
Artem Serovcced8ba2017-07-19 18:18:09 +01005648 protected:
5649 DEFAULT_COPY_CONSTRUCTOR(Ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005650};
5651
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005652// The value of a parameter in this method. Its location depends on
5653// the calling convention.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005654class HParameterValue final : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005655 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005656 HParameterValue(const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08005657 dex::TypeIndex type_index,
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005658 uint8_t index,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005659 DataType::Type parameter_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005660 bool is_this = false)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305661 : HExpression(kParameterValue, parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005662 dex_file_(dex_file),
5663 type_index_(type_index),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005664 index_(index) {
5665 SetPackedFlag<kFlagIsThis>(is_this);
5666 SetPackedFlag<kFlagCanBeNull>(!is_this);
5667 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005668
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005669 const DexFile& GetDexFile() const { return dex_file_; }
Andreas Gampea5b09a62016-11-17 15:21:22 -08005670 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005671 uint8_t GetIndex() const { return index_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07005672 bool IsThis() const { return GetPackedFlag<kFlagIsThis>(); }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005673
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005674 bool CanBeNull() const override { return GetPackedFlag<kFlagCanBeNull>(); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005675 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00005676
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005677 DECLARE_INSTRUCTION(ParameterValue);
5678
Artem Serovcced8ba2017-07-19 18:18:09 +01005679 protected:
5680 DEFAULT_COPY_CONSTRUCTOR(ParameterValue);
5681
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005682 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005683 // Whether or not the parameter value corresponds to 'this' argument.
Vladimir Markobd785672018-05-03 17:09:09 +01005684 static constexpr size_t kFlagIsThis = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005685 static constexpr size_t kFlagCanBeNull = kFlagIsThis + 1;
5686 static constexpr size_t kNumberOfParameterValuePackedBits = kFlagCanBeNull + 1;
5687 static_assert(kNumberOfParameterValuePackedBits <= kMaxNumberOfPackedBits,
5688 "Too many packed fields.");
5689
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005690 const DexFile& dex_file_;
Andreas Gampea5b09a62016-11-17 15:21:22 -08005691 const dex::TypeIndex type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005692 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00005693 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005694 const uint8_t index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005695};
5696
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005697class HNot final : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005698 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005699 HNot(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305700 : HUnaryOperation(kNot, result_type, input, dex_pc) {
5701 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005702
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005703 bool CanBeMoved() const override { return true; }
5704 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005705 return true;
5706 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005707
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005708 template <typename T> static T Compute(T x) { return ~x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005709
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005710 HConstant* Evaluate(HIntConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005711 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005712 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005713 HConstant* Evaluate(HLongConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005714 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005715 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005716 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005717 LOG(FATAL) << DebugName() << " is not defined for float values";
5718 UNREACHABLE();
5719 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005720 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005721 LOG(FATAL) << DebugName() << " is not defined for double values";
5722 UNREACHABLE();
5723 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01005724
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005725 DECLARE_INSTRUCTION(Not);
5726
Artem Serovcced8ba2017-07-19 18:18:09 +01005727 protected:
5728 DEFAULT_COPY_CONSTRUCTOR(Not);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005729};
5730
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005731class HBooleanNot final : public HUnaryOperation {
David Brazdil66d126e2015-04-03 16:02:44 +01005732 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005733 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305734 : HUnaryOperation(kBooleanNot, DataType::Type::kBool, input, dex_pc) {
5735 }
David Brazdil66d126e2015-04-03 16:02:44 +01005736
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005737 bool CanBeMoved() const override { return true; }
5738 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
David Brazdil66d126e2015-04-03 16:02:44 +01005739 return true;
5740 }
5741
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005742 template <typename T> static bool Compute(T x) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005743 DCHECK(IsUint<1>(x)) << x;
David Brazdil66d126e2015-04-03 16:02:44 +01005744 return !x;
5745 }
5746
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005747 HConstant* Evaluate(HIntConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005748 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005749 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005750 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005751 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01005752 UNREACHABLE();
5753 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005754 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005755 LOG(FATAL) << DebugName() << " is not defined for float values";
5756 UNREACHABLE();
5757 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005758 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005759 LOG(FATAL) << DebugName() << " is not defined for double values";
5760 UNREACHABLE();
5761 }
David Brazdil66d126e2015-04-03 16:02:44 +01005762
5763 DECLARE_INSTRUCTION(BooleanNot);
5764
Artem Serovcced8ba2017-07-19 18:18:09 +01005765 protected:
5766 DEFAULT_COPY_CONSTRUCTOR(BooleanNot);
David Brazdil66d126e2015-04-03 16:02:44 +01005767};
5768
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005769class HTypeConversion final : public HExpression<1> {
Roland Levillaindff1f282014-11-05 14:15:05 +00005770 public:
5771 // Instantiate a type conversion of `input` to `result_type`.
Vladimir Markoc8fb2112017-10-03 11:37:52 +01005772 HTypeConversion(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305773 : HExpression(kTypeConversion, result_type, SideEffects::None(), dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00005774 SetRawInputAt(0, input);
Roland Levillainf355c3f2016-03-30 19:09:03 +01005775 // Invariant: We should never generate a conversion to a Boolean value.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005776 DCHECK_NE(DataType::Type::kBool, result_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00005777 }
5778
5779 HInstruction* GetInput() const { return InputAt(0); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005780 DataType::Type GetInputType() const { return GetInput()->GetType(); }
5781 DataType::Type GetResultType() const { return GetType(); }
Roland Levillaindff1f282014-11-05 14:15:05 +00005782
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005783 bool IsClonable() const override { return true; }
5784 bool CanBeMoved() const override { return true; }
5785 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01005786 return true;
5787 }
Nicolas Geoffrayacc56ac2018-10-09 08:45:24 +01005788 // Return whether the conversion is implicit. This includes conversion to the same type.
5789 bool IsImplicitConversion() const {
5790 return DataType::IsTypeConversionImplicit(GetInputType(), GetResultType());
5791 }
Roland Levillaindff1f282014-11-05 14:15:05 +00005792
Mark Mendelle82549b2015-05-06 10:55:34 -04005793 // Try to statically evaluate the conversion and return a HConstant
5794 // containing the result. If the input cannot be converted, return nullptr.
5795 HConstant* TryStaticEvaluation() const;
5796
Roland Levillaindff1f282014-11-05 14:15:05 +00005797 DECLARE_INSTRUCTION(TypeConversion);
5798
Artem Serovcced8ba2017-07-19 18:18:09 +01005799 protected:
5800 DEFAULT_COPY_CONSTRUCTOR(TypeConversion);
Roland Levillaindff1f282014-11-05 14:15:05 +00005801};
5802
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00005803static constexpr uint32_t kNoRegNumber = -1;
5804
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005805class HNullCheck final : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005806 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005807 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
Artem Serovd1aa7d02018-06-22 11:35:46 +01005808 // constructor. However it can only do it on a fatal slow path so execution never returns to the
5809 // instruction following the current one; thus 'SideEffects::None()' is used.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005810 HNullCheck(HInstruction* value, uint32_t dex_pc)
Artem Serovd1aa7d02018-06-22 11:35:46 +01005811 : HExpression(kNullCheck, value->GetType(), SideEffects::None(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005812 SetRawInputAt(0, value);
5813 }
5814
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005815 bool IsClonable() const override { return true; }
5816 bool CanBeMoved() const override { return true; }
5817 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005818 return true;
5819 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005820
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005821 bool NeedsEnvironment() const override { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005822
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005823 bool CanThrow() const override { return true; }
Calin Juravle10e244f2015-01-26 18:54:32 +00005824
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005825 bool CanBeNull() const override { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005826
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005827 DECLARE_INSTRUCTION(NullCheck);
5828
Artem Serovcced8ba2017-07-19 18:18:09 +01005829 protected:
5830 DEFAULT_COPY_CONSTRUCTOR(NullCheck);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005831};
5832
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005833// Embeds an ArtField and all the information required by the compiler. We cache
5834// that information to avoid requiring the mutator lock every time we need it.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005835class FieldInfo : public ValueObject {
5836 public:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005837 FieldInfo(ArtField* field,
5838 MemberOffset field_offset,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005839 DataType::Type field_type,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005840 bool is_volatile,
5841 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005842 uint16_t declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005843 const DexFile& dex_file)
5844 : field_(field),
5845 field_offset_(field_offset),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005846 field_type_(field_type),
5847 is_volatile_(is_volatile),
5848 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005849 declaring_class_def_index_(declaring_class_def_index),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005850 dex_file_(dex_file) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005851
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005852 ArtField* GetField() const { return field_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005853 MemberOffset GetFieldOffset() const { return field_offset_; }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005854 DataType::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005855 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07005856 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005857 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00005858 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005859
5860 private:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005861 ArtField* const field_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005862 const MemberOffset field_offset_;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005863 const DataType::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00005864 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005865 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07005866 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005867 const DexFile& dex_file_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005868};
5869
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005870class HInstanceFieldGet final : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005871 public:
5872 HInstanceFieldGet(HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005873 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005874 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005875 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005876 bool is_volatile,
5877 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005878 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005879 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005880 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305881 : HExpression(kInstanceFieldGet,
5882 field_type,
5883 SideEffects::FieldReadOfType(field_type, is_volatile),
5884 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005885 field_info_(field,
5886 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005887 field_type,
5888 is_volatile,
5889 field_idx,
5890 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005891 dex_file) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005892 SetRawInputAt(0, value);
5893 }
5894
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005895 bool IsClonable() const override { return true; }
5896 bool CanBeMoved() const override { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005897
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005898 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01005899 const HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005900 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005901 }
5902
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005903 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005904 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005905 }
5906
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005907 size_t ComputeHashCode() const override {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01005908 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5909 }
5910
Calin Juravle52c48962014-12-16 17:02:57 +00005911 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005912 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005913 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005914 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005915
Vladimir Marko61b92282017-10-11 13:23:17 +01005916 void SetType(DataType::Type new_type) {
5917 DCHECK(DataType::IsIntegralType(GetType()));
5918 DCHECK(DataType::IsIntegralType(new_type));
5919 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
5920 SetPackedField<TypeField>(new_type);
5921 }
5922
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005923 DECLARE_INSTRUCTION(InstanceFieldGet);
5924
Artem Serovcced8ba2017-07-19 18:18:09 +01005925 protected:
5926 DEFAULT_COPY_CONSTRUCTOR(InstanceFieldGet);
5927
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005928 private:
5929 const FieldInfo field_info_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005930};
5931
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005932class HInstanceFieldSet final : public HExpression<2> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005933 public:
5934 HInstanceFieldSet(HInstruction* object,
5935 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005936 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005937 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005938 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005939 bool is_volatile,
5940 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005941 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005942 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005943 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01005944 : HExpression(kInstanceFieldSet,
5945 SideEffects::FieldWriteOfType(field_type, is_volatile),
5946 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005947 field_info_(field,
5948 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005949 field_type,
5950 is_volatile,
5951 field_idx,
5952 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005953 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005954 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005955 SetRawInputAt(0, object);
5956 SetRawInputAt(1, value);
5957 }
5958
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005959 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01005960
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005961 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005962 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005963 }
5964
Calin Juravle52c48962014-12-16 17:02:57 +00005965 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005966 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005967 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005968 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005969 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005970 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5971 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005972
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005973 DECLARE_INSTRUCTION(InstanceFieldSet);
5974
Artem Serovcced8ba2017-07-19 18:18:09 +01005975 protected:
5976 DEFAULT_COPY_CONSTRUCTOR(InstanceFieldSet);
5977
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005978 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005979 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5980 static constexpr size_t kNumberOfInstanceFieldSetPackedBits = kFlagValueCanBeNull + 1;
5981 static_assert(kNumberOfInstanceFieldSetPackedBits <= kMaxNumberOfPackedBits,
5982 "Too many packed fields.");
5983
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005984 const FieldInfo field_info_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005985};
5986
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005987class HArrayGet final : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005988 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005989 HArrayGet(HInstruction* array,
5990 HInstruction* index,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005991 DataType::Type type,
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005992 uint32_t dex_pc)
5993 : HArrayGet(array,
5994 index,
5995 type,
5996 SideEffects::ArrayReadOfType(type),
5997 dex_pc,
Andreas Gampe3db70682018-12-26 15:12:03 -08005998 /* is_string_char_at= */ false) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305999 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006000
6001 HArrayGet(HInstruction* array,
6002 HInstruction* index,
6003 DataType::Type type,
6004 SideEffects side_effects,
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006005 uint32_t dex_pc,
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006006 bool is_string_char_at)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306007 : HExpression(kArrayGet, type, side_effects, dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006008 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006009 SetRawInputAt(0, array);
6010 SetRawInputAt(1, index);
6011 }
6012
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006013 bool IsClonable() const override { return true; }
6014 bool CanBeMoved() const override { return true; }
6015 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07006016 return true;
6017 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006018 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const override {
Calin Juravle77520bc2015-01-12 18:45:46 +00006019 // TODO: We can be smarter here.
Vladimir Marko66d691d2017-04-07 17:53:39 +01006020 // Currently, unless the array is the result of NewArray, the array access is always
6021 // preceded by some form of null NullCheck necessary for the bounds check, usually
6022 // implicit null check on the ArrayLength input to BoundsCheck or Deoptimize for
6023 // dynamic BCE. There are cases when these could be removed to produce better code.
6024 // If we ever add optimizations to do so we should allow an implicit check here
6025 // (as long as the address falls in the first page).
6026 //
6027 // As an example of such fancy optimization, we could eliminate BoundsCheck for
6028 // a = cond ? new int[1] : null;
6029 // a[0]; // The Phi does not need bounds check for either input.
Calin Juravle77520bc2015-01-12 18:45:46 +00006030 return false;
6031 }
6032
David Brazdil4833f5a2015-12-16 10:37:39 +00006033 bool IsEquivalentOf(HArrayGet* other) const {
6034 bool result = (GetDexPc() == other->GetDexPc());
6035 if (kIsDebugBuild && result) {
6036 DCHECK_EQ(GetBlock(), other->GetBlock());
6037 DCHECK_EQ(GetArray(), other->GetArray());
6038 DCHECK_EQ(GetIndex(), other->GetIndex());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006039 if (DataType::IsIntOrLongType(GetType())) {
6040 DCHECK(DataType::IsFloatingPointType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00006041 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006042 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
6043 DCHECK(DataType::IsIntOrLongType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00006044 }
6045 }
6046 return result;
6047 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01006048
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006049 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
6050
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006051 HInstruction* GetArray() const { return InputAt(0); }
6052 HInstruction* GetIndex() const { return InputAt(1); }
6053
Vladimir Marko61b92282017-10-11 13:23:17 +01006054 void SetType(DataType::Type new_type) {
6055 DCHECK(DataType::IsIntegralType(GetType()));
6056 DCHECK(DataType::IsIntegralType(new_type));
6057 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
6058 SetPackedField<TypeField>(new_type);
6059 }
6060
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006061 DECLARE_INSTRUCTION(ArrayGet);
6062
Artem Serovcced8ba2017-07-19 18:18:09 +01006063 protected:
6064 DEFAULT_COPY_CONSTRUCTOR(ArrayGet);
6065
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006066 private:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006067 // We treat a String as an array, creating the HArrayGet from String.charAt()
6068 // intrinsic in the instruction simplifier. We can always determine whether
6069 // a particular HArrayGet is actually a String.charAt() by looking at the type
6070 // of the input but that requires holding the mutator lock, so we prefer to use
6071 // a flag, so that code generators don't need to do the locking.
Vladimir Markobd785672018-05-03 17:09:09 +01006072 static constexpr size_t kFlagIsStringCharAt = kNumberOfGenericPackedBits;
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006073 static constexpr size_t kNumberOfArrayGetPackedBits = kFlagIsStringCharAt + 1;
6074 static_assert(kNumberOfArrayGetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6075 "Too many packed fields.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006076};
6077
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006078class HArraySet final : public HExpression<3> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006079 public:
6080 HArraySet(HInstruction* array,
6081 HInstruction* index,
6082 HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006083 DataType::Type expected_component_type,
Aart Bik18b36ab2016-04-13 16:41:35 -07006084 uint32_t dex_pc)
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006085 : HArraySet(array,
6086 index,
6087 value,
6088 expected_component_type,
6089 // Make a best guess for side effects now, may be refined during SSA building.
6090 ComputeSideEffects(GetComponentType(value->GetType(), expected_component_type)),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306091 dex_pc) {
6092 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006093
6094 HArraySet(HInstruction* array,
6095 HInstruction* index,
6096 HInstruction* value,
6097 DataType::Type expected_component_type,
6098 SideEffects side_effects,
6099 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006100 : HExpression(kArraySet, side_effects, dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006101 SetPackedField<ExpectedComponentTypeField>(expected_component_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006102 SetPackedFlag<kFlagNeedsTypeCheck>(value->GetType() == DataType::Type::kReference);
Vladimir Markoa1de9182016-02-25 11:37:38 +00006103 SetPackedFlag<kFlagValueCanBeNull>(true);
6104 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(false);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006105 SetRawInputAt(0, array);
6106 SetRawInputAt(1, index);
6107 SetRawInputAt(2, value);
6108 }
6109
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006110 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01006111
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006112 bool NeedsEnvironment() const override {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006113 // We call a runtime method to throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006114 return NeedsTypeCheck();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006115 }
6116
Mingyao Yang81014cb2015-06-02 03:16:27 -07006117 // Can throw ArrayStoreException.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006118 bool CanThrow() const override { return NeedsTypeCheck(); }
Mingyao Yang81014cb2015-06-02 03:16:27 -07006119
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006120 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const override {
Calin Juravle77520bc2015-01-12 18:45:46 +00006121 // TODO: Same as for ArrayGet.
6122 return false;
6123 }
6124
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006125 void ClearNeedsTypeCheck() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006126 SetPackedFlag<kFlagNeedsTypeCheck>(false);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006127 }
6128
Nicolas Geoffray07276db2015-05-18 14:22:09 +01006129 void ClearValueCanBeNull() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006130 SetPackedFlag<kFlagValueCanBeNull>(false);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01006131 }
6132
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006133 void SetStaticTypeOfArrayIsObjectArray() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006134 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(true);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006135 }
6136
Vladimir Markoa1de9182016-02-25 11:37:38 +00006137 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
6138 bool NeedsTypeCheck() const { return GetPackedFlag<kFlagNeedsTypeCheck>(); }
6139 bool StaticTypeOfArrayIsObjectArray() const {
6140 return GetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>();
6141 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006142
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006143 HInstruction* GetArray() const { return InputAt(0); }
6144 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006145 HInstruction* GetValue() const { return InputAt(2); }
6146
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006147 DataType::Type GetComponentType() const {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006148 return GetComponentType(GetValue()->GetType(), GetRawExpectedComponentType());
6149 }
6150
6151 static DataType::Type GetComponentType(DataType::Type value_type,
6152 DataType::Type expected_component_type) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006153 // The Dex format does not type floating point index operations. Since the
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006154 // `expected_component_type` comes from SSA building and can therefore not
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006155 // be correct, we also check what is the value type. If it is a floating
6156 // point type, we must use that type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006157 return ((value_type == DataType::Type::kFloat32) || (value_type == DataType::Type::kFloat64))
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006158 ? value_type
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006159 : expected_component_type;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006160 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01006161
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006162 DataType::Type GetRawExpectedComponentType() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006163 return GetPackedField<ExpectedComponentTypeField>();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01006164 }
6165
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006166 static SideEffects ComputeSideEffects(DataType::Type type) {
6167 return SideEffects::ArrayWriteOfType(type).Union(SideEffectsForArchRuntimeCalls(type));
Aart Bik18b36ab2016-04-13 16:41:35 -07006168 }
6169
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006170 static SideEffects SideEffectsForArchRuntimeCalls(DataType::Type value_type) {
6171 return (value_type == DataType::Type::kReference) ? SideEffects::CanTriggerGC()
6172 : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006173 }
6174
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006175 DECLARE_INSTRUCTION(ArraySet);
6176
Artem Serovcced8ba2017-07-19 18:18:09 +01006177 protected:
6178 DEFAULT_COPY_CONSTRUCTOR(ArraySet);
6179
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006180 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006181 static constexpr size_t kFieldExpectedComponentType = kNumberOfGenericPackedBits;
6182 static constexpr size_t kFieldExpectedComponentTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006183 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00006184 static constexpr size_t kFlagNeedsTypeCheck =
6185 kFieldExpectedComponentType + kFieldExpectedComponentTypeSize;
6186 static constexpr size_t kFlagValueCanBeNull = kFlagNeedsTypeCheck + 1;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006187 // Cached information for the reference_type_info_ so that codegen
6188 // does not need to inspect the static type.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006189 static constexpr size_t kFlagStaticTypeOfArrayIsObjectArray = kFlagValueCanBeNull + 1;
6190 static constexpr size_t kNumberOfArraySetPackedBits =
6191 kFlagStaticTypeOfArrayIsObjectArray + 1;
6192 static_assert(kNumberOfArraySetPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6193 using ExpectedComponentTypeField =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006194 BitField<DataType::Type, kFieldExpectedComponentType, kFieldExpectedComponentTypeSize>;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006195};
6196
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006197class HArrayLength final : public HExpression<1> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006198 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006199 HArrayLength(HInstruction* array, uint32_t dex_pc, bool is_string_length = false)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306200 : HExpression(kArrayLength, DataType::Type::kInt32, SideEffects::None(), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006201 SetPackedFlag<kFlagIsStringLength>(is_string_length);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01006202 // Note that arrays do not change length, so the instruction does not
6203 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006204 SetRawInputAt(0, array);
6205 }
6206
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006207 bool IsClonable() const override { return true; }
6208 bool CanBeMoved() const override { return true; }
6209 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07006210 return true;
6211 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006212 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Calin Juravle641547a2015-04-21 22:08:51 +01006213 return obj == InputAt(0);
6214 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01006215
Vladimir Markodce016e2016-04-28 13:10:02 +01006216 bool IsStringLength() const { return GetPackedFlag<kFlagIsStringLength>(); }
6217
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006218 DECLARE_INSTRUCTION(ArrayLength);
6219
Artem Serovcced8ba2017-07-19 18:18:09 +01006220 protected:
6221 DEFAULT_COPY_CONSTRUCTOR(ArrayLength);
6222
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006223 private:
Vladimir Markodce016e2016-04-28 13:10:02 +01006224 // We treat a String as an array, creating the HArrayLength from String.length()
6225 // or String.isEmpty() intrinsic in the instruction simplifier. We can always
6226 // determine whether a particular HArrayLength is actually a String.length() by
6227 // looking at the type of the input but that requires holding the mutator lock, so
6228 // we prefer to use a flag, so that code generators don't need to do the locking.
Vladimir Markobd785672018-05-03 17:09:09 +01006229 static constexpr size_t kFlagIsStringLength = kNumberOfGenericPackedBits;
Vladimir Markodce016e2016-04-28 13:10:02 +01006230 static constexpr size_t kNumberOfArrayLengthPackedBits = kFlagIsStringLength + 1;
6231 static_assert(kNumberOfArrayLengthPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6232 "Too many packed fields.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006233};
6234
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006235class HBoundsCheck final : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006236 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00006237 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
Artem Serovd1aa7d02018-06-22 11:35:46 +01006238 // constructor. However it can only do it on a fatal slow path so execution never returns to the
6239 // instruction following the current one; thus 'SideEffects::None()' is used.
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006240 HBoundsCheck(HInstruction* index,
6241 HInstruction* length,
6242 uint32_t dex_pc,
Vladimir Marko0259c242017-12-04 11:27:47 +00006243 bool is_string_char_at = false)
Artem Serovd1aa7d02018-06-22 11:35:46 +01006244 : HExpression(kBoundsCheck, index->GetType(), SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006245 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(index->GetType()));
Vladimir Marko0259c242017-12-04 11:27:47 +00006246 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006247 SetRawInputAt(0, index);
6248 SetRawInputAt(1, length);
6249 }
6250
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006251 bool IsClonable() const override { return true; }
6252 bool CanBeMoved() const override { return true; }
6253 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07006254 return true;
6255 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01006256
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006257 bool NeedsEnvironment() const override { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006258
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006259 bool CanThrow() const override { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01006260
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00006261 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006262
Alexandre Ramese6dbf482015-10-19 10:10:41 +01006263 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006264
6265 DECLARE_INSTRUCTION(BoundsCheck);
6266
Artem Serovcced8ba2017-07-19 18:18:09 +01006267 protected:
6268 DEFAULT_COPY_CONSTRUCTOR(BoundsCheck);
6269
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006270 private:
Vladimir Markobd785672018-05-03 17:09:09 +01006271 static constexpr size_t kFlagIsStringCharAt = kNumberOfGenericPackedBits;
Vladimir Markof02046e2018-10-02 15:31:32 +01006272 static constexpr size_t kNumberOfBoundsCheckPackedBits = kFlagIsStringCharAt + 1;
6273 static_assert(kNumberOfBoundsCheckPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6274 "Too many packed fields.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006275};
6276
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006277class HSuspendCheck final : public HExpression<0> {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006278 public:
David Brazdil86ea7ee2016-02-16 09:26:07 +00006279 explicit HSuspendCheck(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01006280 : HExpression(kSuspendCheck, SideEffects::CanTriggerGC(), dex_pc),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306281 slow_path_(nullptr) {
6282 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006283
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006284 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01006285
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006286 bool NeedsEnvironment() const override {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006287 return true;
6288 }
6289
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006290 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
6291 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006292
6293 DECLARE_INSTRUCTION(SuspendCheck);
6294
Artem Serovcced8ba2017-07-19 18:18:09 +01006295 protected:
6296 DEFAULT_COPY_CONSTRUCTOR(SuspendCheck);
6297
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006298 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006299 // Only used for code generation, in order to share the same slow path between back edges
6300 // of a same loop.
6301 SlowPathCode* slow_path_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006302};
6303
David Srbecky0cf44932015-12-09 14:09:59 +00006304// Pseudo-instruction which provides the native debugger with mapping information.
6305// It ensures that we can generate line number and local variables at this point.
Vladimir Markobd785672018-05-03 17:09:09 +01006306class HNativeDebugInfo : public HExpression<0> {
David Srbecky0cf44932015-12-09 14:09:59 +00006307 public:
6308 explicit HNativeDebugInfo(uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006309 : HExpression<0>(kNativeDebugInfo, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306310 }
David Srbecky0cf44932015-12-09 14:09:59 +00006311
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006312 bool NeedsEnvironment() const override {
David Srbecky0cf44932015-12-09 14:09:59 +00006313 return true;
6314 }
6315
6316 DECLARE_INSTRUCTION(NativeDebugInfo);
6317
Artem Serovcced8ba2017-07-19 18:18:09 +01006318 protected:
6319 DEFAULT_COPY_CONSTRUCTOR(NativeDebugInfo);
David Srbecky0cf44932015-12-09 14:09:59 +00006320};
6321
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006322/**
6323 * Instruction to load a Class object.
6324 */
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006325class HLoadClass final : public HInstruction {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006326 public:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006327 // Determines how to load the Class.
6328 enum class LoadKind {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006329 // We cannot load this class. See HSharpening::SharpenLoadClass.
6330 kInvalid = -1,
6331
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006332 // Use the Class* from the method's own ArtMethod*.
6333 kReferrersClass,
6334
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006335 // Use PC-relative boot image Class* address that will be known at link time.
Vladimir Marko764d4542017-05-16 10:31:41 +01006336 // Used for boot image classes referenced by boot image code.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006337 kBootImageLinkTimePcRelative,
6338
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006339 // Load from an entry in the .data.bimg.rel.ro using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006340 // Used for boot image classes referenced by apps in AOT-compiled code.
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006341 kBootImageRelRo,
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006342
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006343 // Load from an entry in the .bss section using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006344 // Used for classes outside boot image referenced by AOT-compiled app and boot image code.
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006345 kBssEntry,
6346
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006347 // Use a known boot image Class* address, embedded in the code by the codegen.
6348 // Used for boot image classes referenced by apps in JIT-compiled code.
6349 kJitBootImageAddress,
6350
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006351 // Load from the root table associated with the JIT compiled method.
6352 kJitTableAddress,
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006353
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006354 // Load using a simple runtime call. This is the fall-back load kind when
6355 // the codegen is unable to use another appropriate kind.
6356 kRuntimeCall,
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006357
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006358 kLast = kRuntimeCall
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006359 };
6360
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01006361 HLoadClass(HCurrentMethod* current_method,
Andreas Gampea5b09a62016-11-17 15:21:22 -08006362 dex::TypeIndex type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01006363 const DexFile& dex_file,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006364 Handle<mirror::Class> klass,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006365 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01006366 uint32_t dex_pc,
Nicolas Geoffray56876342016-12-16 16:09:08 +00006367 bool needs_access_check)
Vladimir Markobd785672018-05-03 17:09:09 +01006368 : HInstruction(kLoadClass,
6369 DataType::Type::kReference,
6370 SideEffectsForArchRuntimeCalls(),
6371 dex_pc),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006372 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006373 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01006374 dex_file_(dex_file),
Vladimir Marko175e7862018-03-27 09:03:13 +00006375 klass_(klass) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01006376 // Referrers class should not need access check. We never inline unverified
6377 // methods so we can't possibly end up in this situation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006378 DCHECK(!is_referrers_class || !needs_access_check);
6379
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006380 SetPackedField<LoadKindField>(
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006381 is_referrers_class ? LoadKind::kReferrersClass : LoadKind::kRuntimeCall);
Vladimir Markoa1de9182016-02-25 11:37:38 +00006382 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
Nicolas Geoffray56876342016-12-16 16:09:08 +00006383 SetPackedFlag<kFlagIsInBootImage>(false);
Vladimir Markoa1de9182016-02-25 11:37:38 +00006384 SetPackedFlag<kFlagGenerateClInitCheck>(false);
Vladimir Marko175e7862018-03-27 09:03:13 +00006385 SetPackedFlag<kFlagValidLoadedClassRTI>(false);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006386 }
6387
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006388 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01006389
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006390 void SetLoadKind(LoadKind load_kind);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006391
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006392 LoadKind GetLoadKind() const {
6393 return GetPackedField<LoadKindField>();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01006394 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006395
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006396 bool HasPcRelativeLoadKind() const {
6397 return GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
6398 GetLoadKind() == LoadKind::kBootImageRelRo ||
6399 GetLoadKind() == LoadKind::kBssEntry;
6400 }
6401
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006402 bool CanBeMoved() const override { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006403
Yi Kong39402542019-03-24 02:47:16 -07006404 bool InstructionDataEquals(const HInstruction* other) const override;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006405
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006406 size_t ComputeHashCode() const override { return type_index_.index_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006407
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006408 bool CanBeNull() const override { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006409
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006410 bool NeedsEnvironment() const override {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006411 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006412 }
6413
Calin Juravle0ba218d2015-05-19 18:46:01 +01006414 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00006415 // The entrypoint the code generator is going to call does not do
6416 // clinit of the class.
6417 DCHECK(!NeedsAccessCheck());
Vladimir Markoa1de9182016-02-25 11:37:38 +00006418 SetPackedFlag<kFlagGenerateClInitCheck>(generate_clinit_check);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006419 }
6420
6421 bool CanCallRuntime() const {
Vladimir Marko41559982017-01-06 14:04:23 +00006422 return NeedsAccessCheck() ||
6423 MustGenerateClinitCheck() ||
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006424 GetLoadKind() == LoadKind::kRuntimeCall ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006425 GetLoadKind() == LoadKind::kBssEntry;
Calin Juravle98893e12015-10-02 21:05:03 +01006426 }
6427
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006428 bool CanThrow() const override {
Vladimir Marko41559982017-01-06 14:04:23 +00006429 return NeedsAccessCheck() ||
6430 MustGenerateClinitCheck() ||
6431 // If the class is in the boot image, the lookup in the runtime call cannot throw.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006432 ((GetLoadKind() == LoadKind::kRuntimeCall ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006433 GetLoadKind() == LoadKind::kBssEntry) &&
6434 !IsInBootImage());
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006435 }
6436
Calin Juravleacf735c2015-02-12 15:25:22 +00006437 ReferenceTypeInfo GetLoadedClassRTI() {
Vladimir Marko175e7862018-03-27 09:03:13 +00006438 if (GetPackedFlag<kFlagValidLoadedClassRTI>()) {
6439 // Note: The is_exact flag from the return value should not be used.
Andreas Gampe3db70682018-12-26 15:12:03 -08006440 return ReferenceTypeInfo::CreateUnchecked(klass_, /* is_exact= */ true);
Vladimir Marko175e7862018-03-27 09:03:13 +00006441 } else {
6442 return ReferenceTypeInfo::CreateInvalid();
6443 }
Calin Juravleacf735c2015-02-12 15:25:22 +00006444 }
6445
Vladimir Marko175e7862018-03-27 09:03:13 +00006446 // Loaded class RTI is marked as valid by RTP if the klass_ is admissible.
6447 void SetValidLoadedClassRTI() REQUIRES_SHARED(Locks::mutator_lock_) {
6448 DCHECK(klass_ != nullptr);
6449 SetPackedFlag<kFlagValidLoadedClassRTI>(true);
Calin Juravleacf735c2015-02-12 15:25:22 +00006450 }
6451
Andreas Gampea5b09a62016-11-17 15:21:22 -08006452 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006453 const DexFile& GetDexFile() const { return dex_file_; }
6454
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006455 bool NeedsDexCacheOfDeclaringClass() const override {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006456 return GetLoadKind() == LoadKind::kRuntimeCall;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006457 }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00006458
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006459 static SideEffects SideEffectsForArchRuntimeCalls() {
6460 return SideEffects::CanTriggerGC();
6461 }
6462
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006463 bool IsReferrersClass() const { return GetLoadKind() == LoadKind::kReferrersClass; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006464 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006465 bool IsInBootImage() const { return GetPackedFlag<kFlagIsInBootImage>(); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006466 bool MustGenerateClinitCheck() const { return GetPackedFlag<kFlagGenerateClInitCheck>(); }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006467
Vladimir Markoa9f303c2018-07-20 16:43:56 +01006468 bool MustResolveTypeOnSlowPath() const {
6469 // Check that this instruction has a slow path.
6470 DCHECK(GetLoadKind() != LoadKind::kRuntimeCall); // kRuntimeCall calls on main path.
6471 DCHECK(GetLoadKind() == LoadKind::kBssEntry || MustGenerateClinitCheck());
6472 return GetLoadKind() == LoadKind::kBssEntry;
6473 }
6474
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006475 void MarkInBootImage() {
6476 SetPackedFlag<kFlagIsInBootImage>(true);
6477 }
6478
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006479 void AddSpecialInput(HInstruction* special_input);
6480
6481 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01006482 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006483 return ArrayRef<HUserRecord<HInstruction*>>(
6484 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
6485 }
6486
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006487 Handle<mirror::Class> GetClass() const {
6488 return klass_;
6489 }
6490
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006491 DECLARE_INSTRUCTION(LoadClass);
6492
Artem Serovcced8ba2017-07-19 18:18:09 +01006493 protected:
6494 DEFAULT_COPY_CONSTRUCTOR(LoadClass);
6495
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006496 private:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006497 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfGenericPackedBits;
Vladimir Marko41559982017-01-06 14:04:23 +00006498 static constexpr size_t kFlagIsInBootImage = kFlagNeedsAccessCheck + 1;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006499 // Whether this instruction must generate the initialization check.
6500 // Used for code generation.
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006501 static constexpr size_t kFlagGenerateClInitCheck = kFlagIsInBootImage + 1;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006502 static constexpr size_t kFieldLoadKind = kFlagGenerateClInitCheck + 1;
6503 static constexpr size_t kFieldLoadKindSize =
6504 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
Vladimir Marko175e7862018-03-27 09:03:13 +00006505 static constexpr size_t kFlagValidLoadedClassRTI = kFieldLoadKind + kFieldLoadKindSize;
6506 static constexpr size_t kNumberOfLoadClassPackedBits = kFlagValidLoadedClassRTI + 1;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006507 static_assert(kNumberOfLoadClassPackedBits < kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006508 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
6509
6510 static bool HasTypeReference(LoadKind load_kind) {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006511 return load_kind == LoadKind::kReferrersClass ||
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006512 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006513 load_kind == LoadKind::kBssEntry ||
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006514 load_kind == LoadKind::kRuntimeCall;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006515 }
6516
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006517 void SetLoadKindInternal(LoadKind load_kind);
6518
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006519 // The special input is the HCurrentMethod for kRuntimeCall or kReferrersClass.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006520 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006521 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006522 HUserRecord<HInstruction*> special_input_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006523
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006524 // A type index and dex file where the class can be accessed. The dex file can be:
6525 // - The compiling method's dex file if the class is defined there too.
6526 // - The compiling method's dex file if the class is referenced there.
6527 // - The dex file where the class is defined. When the load kind can only be
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006528 // kBssEntry or kRuntimeCall, we cannot emit code for this `HLoadClass`.
Andreas Gampea5b09a62016-11-17 15:21:22 -08006529 const dex::TypeIndex type_index_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006530 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006531
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006532 Handle<mirror::Class> klass_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006533};
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006534std::ostream& operator<<(std::ostream& os, HLoadClass::LoadKind rhs);
6535
6536// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
Vladimir Marko28e012a2017-12-07 11:22:59 +00006537inline void HLoadClass::SetLoadKind(LoadKind load_kind) {
6538 // The load kind should be determined before inserting the instruction to the graph.
6539 DCHECK(GetBlock() == nullptr);
6540 DCHECK(GetEnvironment() == nullptr);
6541 SetPackedField<LoadKindField>(load_kind);
6542 if (load_kind != LoadKind::kRuntimeCall && load_kind != LoadKind::kReferrersClass) {
6543 special_input_ = HUserRecord<HInstruction*>(nullptr);
6544 }
6545 if (!NeedsEnvironment()) {
6546 SetSideEffects(SideEffects::None());
6547 }
6548}
6549
6550// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006551inline void HLoadClass::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07006552 // The special input is used for PC-relative loads on some architectures,
6553 // including literal pool loads, which are PC-relative too.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006554 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006555 GetLoadKind() == LoadKind::kBootImageRelRo ||
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006556 GetLoadKind() == LoadKind::kBssEntry ||
6557 GetLoadKind() == LoadKind::kJitBootImageAddress) << GetLoadKind();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006558 DCHECK(special_input_.GetInstruction() == nullptr);
6559 special_input_ = HUserRecord<HInstruction*>(special_input);
6560 special_input->AddUseAt(this, 0);
6561}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006562
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006563class HLoadString final : public HInstruction {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006564 public:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006565 // Determines how to load the String.
6566 enum class LoadKind {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006567 // Use PC-relative boot image String* address that will be known at link time.
Vladimir Marko764d4542017-05-16 10:31:41 +01006568 // Used for boot image strings referenced by boot image code.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006569 kBootImageLinkTimePcRelative,
6570
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006571 // Load from an entry in the .data.bimg.rel.ro using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006572 // Used for boot image strings referenced by apps in AOT-compiled code.
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006573 kBootImageRelRo,
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006574
Vladimir Markoaad75c62016-10-03 08:46:48 +00006575 // Load from an entry in the .bss section using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006576 // Used for strings outside boot image referenced by AOT-compiled app and boot image code.
Vladimir Markoaad75c62016-10-03 08:46:48 +00006577 kBssEntry,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006578
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006579 // Use a known boot image String* address, embedded in the code by the codegen.
6580 // Used for boot image strings referenced by apps in JIT-compiled code.
6581 kJitBootImageAddress,
6582
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006583 // Load from the root table associated with the JIT compiled method.
6584 kJitTableAddress,
6585
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006586 // Load using a simple runtime call. This is the fall-back load kind when
6587 // the codegen is unable to use another appropriate kind.
6588 kRuntimeCall,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006589
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006590 kLast = kRuntimeCall,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006591 };
6592
Nicolas Geoffray917d0162015-11-24 18:25:35 +00006593 HLoadString(HCurrentMethod* current_method,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006594 dex::StringIndex string_index,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006595 const DexFile& dex_file,
6596 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006597 : HInstruction(kLoadString,
6598 DataType::Type::kReference,
6599 SideEffectsForArchRuntimeCalls(),
6600 dex_pc),
Vladimir Marko372f10e2016-05-17 16:30:10 +01006601 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006602 string_index_(string_index),
6603 dex_file_(dex_file) {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006604 SetPackedField<LoadKindField>(LoadKind::kRuntimeCall);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01006605 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006606
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006607 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01006608
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006609 void SetLoadKind(LoadKind load_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006610
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006611 LoadKind GetLoadKind() const {
6612 return GetPackedField<LoadKindField>();
6613 }
6614
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006615 bool HasPcRelativeLoadKind() const {
6616 return GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
6617 GetLoadKind() == LoadKind::kBootImageRelRo ||
6618 GetLoadKind() == LoadKind::kBssEntry;
6619 }
6620
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006621 const DexFile& GetDexFile() const {
6622 return dex_file_;
6623 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006624
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006625 dex::StringIndex GetStringIndex() const {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006626 return string_index_;
6627 }
6628
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006629 Handle<mirror::String> GetString() const {
6630 return string_;
6631 }
6632
6633 void SetString(Handle<mirror::String> str) {
6634 string_ = str;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006635 }
6636
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006637 bool CanBeMoved() const override { return true; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006638
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006639 bool InstructionDataEquals(const HInstruction* other) const override;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006640
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006641 size_t ComputeHashCode() const override { return string_index_.index_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006642
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006643 // Will call the runtime if we need to load the string through
6644 // the dex cache and the string is not guaranteed to be there yet.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006645 bool NeedsEnvironment() const override {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006646 LoadKind load_kind = GetLoadKind();
Vladimir Marko764d4542017-05-16 10:31:41 +01006647 if (load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006648 load_kind == LoadKind::kBootImageRelRo ||
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006649 load_kind == LoadKind::kJitBootImageAddress ||
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006650 load_kind == LoadKind::kJitTableAddress) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006651 return false;
6652 }
Vladimir Marko4d1be4922017-01-06 14:43:11 +00006653 return true;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006654 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006655
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006656 bool NeedsDexCacheOfDeclaringClass() const override {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006657 return GetLoadKind() == LoadKind::kRuntimeCall;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006658 }
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006659
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006660 bool CanBeNull() const override { return false; }
6661 bool CanThrow() const override { return NeedsEnvironment(); }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006662
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006663 static SideEffects SideEffectsForArchRuntimeCalls() {
6664 return SideEffects::CanTriggerGC();
6665 }
6666
Vladimir Marko372f10e2016-05-17 16:30:10 +01006667 void AddSpecialInput(HInstruction* special_input);
6668
6669 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01006670 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Vladimir Marko372f10e2016-05-17 16:30:10 +01006671 return ArrayRef<HUserRecord<HInstruction*>>(
6672 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006673 }
6674
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006675 DECLARE_INSTRUCTION(LoadString);
6676
Artem Serovcced8ba2017-07-19 18:18:09 +01006677 protected:
6678 DEFAULT_COPY_CONSTRUCTOR(LoadString);
6679
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006680 private:
Vladimir Marko4d1be4922017-01-06 14:43:11 +00006681 static constexpr size_t kFieldLoadKind = kNumberOfGenericPackedBits;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006682 static constexpr size_t kFieldLoadKindSize =
6683 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
6684 static constexpr size_t kNumberOfLoadStringPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006685 static_assert(kNumberOfLoadStringPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006686 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006687
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006688 void SetLoadKindInternal(LoadKind load_kind);
6689
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006690 // The special input is the HCurrentMethod for kRuntimeCall.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006691 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko48886c22017-01-06 11:45:47 +00006692 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Marko372f10e2016-05-17 16:30:10 +01006693 HUserRecord<HInstruction*> special_input_;
6694
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006695 dex::StringIndex string_index_;
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006696 const DexFile& dex_file_;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006697
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006698 Handle<mirror::String> string_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006699};
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006700std::ostream& operator<<(std::ostream& os, HLoadString::LoadKind rhs);
6701
6702// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
Vladimir Marko28e012a2017-12-07 11:22:59 +00006703inline void HLoadString::SetLoadKind(LoadKind load_kind) {
6704 // The load kind should be determined before inserting the instruction to the graph.
6705 DCHECK(GetBlock() == nullptr);
6706 DCHECK(GetEnvironment() == nullptr);
6707 DCHECK_EQ(GetLoadKind(), LoadKind::kRuntimeCall);
6708 SetPackedField<LoadKindField>(load_kind);
6709 if (load_kind != LoadKind::kRuntimeCall) {
6710 special_input_ = HUserRecord<HInstruction*>(nullptr);
6711 }
6712 if (!NeedsEnvironment()) {
6713 SetSideEffects(SideEffects::None());
6714 }
6715}
6716
6717// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006718inline void HLoadString::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07006719 // The special input is used for PC-relative loads on some architectures,
6720 // including literal pool loads, which are PC-relative too.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006721 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006722 GetLoadKind() == LoadKind::kBootImageRelRo ||
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006723 GetLoadKind() == LoadKind::kBssEntry ||
6724 GetLoadKind() == LoadKind::kJitBootImageAddress) << GetLoadKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01006725 // HLoadString::GetInputRecords() returns an empty array at this point,
6726 // so use the GetInputRecords() from the base class to set the input record.
6727 DCHECK(special_input_.GetInstruction() == nullptr);
6728 special_input_ = HUserRecord<HInstruction*>(special_input);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006729 special_input->AddUseAt(this, 0);
6730}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006731
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006732class HLoadMethodHandle final : public HInstruction {
Orion Hodsondbaa5c72018-05-10 08:22:46 +01006733 public:
6734 HLoadMethodHandle(HCurrentMethod* current_method,
Orion Hodson4c8e12e2018-05-18 08:33:20 +01006735 uint16_t method_handle_idx,
6736 const DexFile& dex_file,
6737 uint32_t dex_pc)
Orion Hodsondbaa5c72018-05-10 08:22:46 +01006738 : HInstruction(kLoadMethodHandle,
6739 DataType::Type::kReference,
6740 SideEffectsForArchRuntimeCalls(),
6741 dex_pc),
6742 special_input_(HUserRecord<HInstruction*>(current_method)),
6743 method_handle_idx_(method_handle_idx),
6744 dex_file_(dex_file) {
6745 }
6746
6747 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01006748 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Orion Hodsondbaa5c72018-05-10 08:22:46 +01006749 return ArrayRef<HUserRecord<HInstruction*>>(
6750 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
6751 }
6752
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006753 bool IsClonable() const override { return true; }
Orion Hodsondbaa5c72018-05-10 08:22:46 +01006754
6755 uint16_t GetMethodHandleIndex() const { return method_handle_idx_; }
6756
6757 const DexFile& GetDexFile() const { return dex_file_; }
6758
6759 static SideEffects SideEffectsForArchRuntimeCalls() {
6760 return SideEffects::CanTriggerGC();
6761 }
6762
6763 DECLARE_INSTRUCTION(LoadMethodHandle);
6764
6765 protected:
6766 DEFAULT_COPY_CONSTRUCTOR(LoadMethodHandle);
6767
6768 private:
6769 // The special input is the HCurrentMethod for kRuntimeCall.
6770 HUserRecord<HInstruction*> special_input_;
6771
6772 const uint16_t method_handle_idx_;
6773 const DexFile& dex_file_;
6774};
6775
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006776class HLoadMethodType final : public HInstruction {
Orion Hodson18259d72018-04-12 11:18:23 +01006777 public:
6778 HLoadMethodType(HCurrentMethod* current_method,
Orion Hodson06d10a72018-05-14 08:53:38 +01006779 dex::ProtoIndex proto_index,
Orion Hodson18259d72018-04-12 11:18:23 +01006780 const DexFile& dex_file,
6781 uint32_t dex_pc)
6782 : HInstruction(kLoadMethodType,
6783 DataType::Type::kReference,
6784 SideEffectsForArchRuntimeCalls(),
6785 dex_pc),
6786 special_input_(HUserRecord<HInstruction*>(current_method)),
Orion Hodson06d10a72018-05-14 08:53:38 +01006787 proto_index_(proto_index),
Orion Hodson18259d72018-04-12 11:18:23 +01006788 dex_file_(dex_file) {
6789 }
6790
6791 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01006792 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Orion Hodson18259d72018-04-12 11:18:23 +01006793 return ArrayRef<HUserRecord<HInstruction*>>(
6794 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
6795 }
6796
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006797 bool IsClonable() const override { return true; }
Orion Hodson18259d72018-04-12 11:18:23 +01006798
Orion Hodson06d10a72018-05-14 08:53:38 +01006799 dex::ProtoIndex GetProtoIndex() const { return proto_index_; }
Orion Hodson18259d72018-04-12 11:18:23 +01006800
6801 const DexFile& GetDexFile() const { return dex_file_; }
6802
6803 static SideEffects SideEffectsForArchRuntimeCalls() {
6804 return SideEffects::CanTriggerGC();
6805 }
6806
6807 DECLARE_INSTRUCTION(LoadMethodType);
6808
6809 protected:
6810 DEFAULT_COPY_CONSTRUCTOR(LoadMethodType);
6811
6812 private:
6813 // The special input is the HCurrentMethod for kRuntimeCall.
6814 HUserRecord<HInstruction*> special_input_;
6815
Orion Hodson06d10a72018-05-14 08:53:38 +01006816 const dex::ProtoIndex proto_index_;
Orion Hodson18259d72018-04-12 11:18:23 +01006817 const DexFile& dex_file_;
6818};
6819
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006820/**
6821 * Performs an initialization check on its Class object input.
6822 */
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006823class HClinitCheck final : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006824 public:
Roland Levillain3887c462015-08-12 18:15:42 +01006825 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07006826 : HExpression(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306827 kClinitCheck,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006828 DataType::Type::kReference,
Mingyao Yang217eb062017-12-11 15:20:07 -08006829 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006830 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006831 SetRawInputAt(0, constant);
6832 }
Artem Serova6e26142018-06-19 14:55:17 +01006833 // TODO: Make ClinitCheck clonable.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006834 bool CanBeMoved() const override { return true; }
6835 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006836 return true;
6837 }
6838
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006839 bool NeedsEnvironment() const override {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006840 // May call runtime to initialize the class.
6841 return true;
6842 }
6843
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006844 bool CanThrow() const override { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006845
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00006846 HLoadClass* GetLoadClass() const {
6847 DCHECK(InputAt(0)->IsLoadClass());
6848 return InputAt(0)->AsLoadClass();
6849 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006850
6851 DECLARE_INSTRUCTION(ClinitCheck);
6852
Artem Serovcced8ba2017-07-19 18:18:09 +01006853
6854 protected:
6855 DEFAULT_COPY_CONSTRUCTOR(ClinitCheck);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006856};
6857
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006858class HStaticFieldGet final : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006859 public:
6860 HStaticFieldGet(HInstruction* cls,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006861 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006862 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006863 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006864 bool is_volatile,
6865 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006866 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006867 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01006868 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306869 : HExpression(kStaticFieldGet,
6870 field_type,
6871 SideEffects::FieldReadOfType(field_type, is_volatile),
6872 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006873 field_info_(field,
6874 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006875 field_type,
6876 is_volatile,
6877 field_idx,
6878 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006879 dex_file) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006880 SetRawInputAt(0, cls);
6881 }
6882
Calin Juravle52c48962014-12-16 17:02:57 +00006883
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006884 bool IsClonable() const override { return true; }
6885 bool CanBeMoved() const override { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006886
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006887 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01006888 const HStaticFieldGet* other_get = other->AsStaticFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00006889 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006890 }
6891
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006892 size_t ComputeHashCode() const override {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006893 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
6894 }
6895
Calin Juravle52c48962014-12-16 17:02:57 +00006896 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006897 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006898 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006899 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006900
Vladimir Marko61b92282017-10-11 13:23:17 +01006901 void SetType(DataType::Type new_type) {
6902 DCHECK(DataType::IsIntegralType(GetType()));
6903 DCHECK(DataType::IsIntegralType(new_type));
6904 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
6905 SetPackedField<TypeField>(new_type);
6906 }
6907
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006908 DECLARE_INSTRUCTION(StaticFieldGet);
6909
Artem Serovcced8ba2017-07-19 18:18:09 +01006910 protected:
6911 DEFAULT_COPY_CONSTRUCTOR(StaticFieldGet);
6912
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006913 private:
6914 const FieldInfo field_info_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006915};
6916
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006917class HStaticFieldSet final : public HExpression<2> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006918 public:
6919 HStaticFieldSet(HInstruction* cls,
6920 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006921 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006922 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006923 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006924 bool is_volatile,
6925 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006926 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006927 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01006928 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006929 : HExpression(kStaticFieldSet,
6930 SideEffects::FieldWriteOfType(field_type, is_volatile),
6931 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006932 field_info_(field,
6933 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006934 field_type,
6935 is_volatile,
6936 field_idx,
6937 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006938 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006939 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006940 SetRawInputAt(0, cls);
6941 SetRawInputAt(1, value);
6942 }
6943
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006944 bool IsClonable() const override { return true; }
Calin Juravle52c48962014-12-16 17:02:57 +00006945 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006946 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006947 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006948 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006949
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006950 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006951 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
6952 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006953
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006954 DECLARE_INSTRUCTION(StaticFieldSet);
6955
Artem Serovcced8ba2017-07-19 18:18:09 +01006956 protected:
6957 DEFAULT_COPY_CONSTRUCTOR(StaticFieldSet);
6958
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006959 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006960 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
6961 static constexpr size_t kNumberOfStaticFieldSetPackedBits = kFlagValueCanBeNull + 1;
6962 static_assert(kNumberOfStaticFieldSetPackedBits <= kMaxNumberOfPackedBits,
6963 "Too many packed fields.");
6964
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006965 const FieldInfo field_info_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006966};
6967
Vladimir Marko552a1342017-10-31 10:56:47 +00006968class HStringBuilderAppend final : public HVariableInputSizeInstruction {
6969 public:
6970 HStringBuilderAppend(HIntConstant* format,
6971 uint32_t number_of_arguments,
6972 ArenaAllocator* allocator,
6973 uint32_t dex_pc)
6974 : HVariableInputSizeInstruction(
6975 kStringBuilderAppend,
6976 DataType::Type::kReference,
6977 // The runtime call may read memory from inputs. It never writes outside
6978 // of the newly allocated result object (or newly allocated helper objects).
6979 SideEffects::AllReads().Union(SideEffects::CanTriggerGC()),
6980 dex_pc,
6981 allocator,
6982 number_of_arguments + /* format */ 1u,
6983 kArenaAllocInvokeInputs) {
6984 DCHECK_GE(number_of_arguments, 1u); // There must be something to append.
6985 SetRawInputAt(FormatIndex(), format);
6986 }
6987
6988 void SetArgumentAt(size_t index, HInstruction* argument) {
6989 DCHECK_LE(index, GetNumberOfArguments());
6990 SetRawInputAt(index, argument);
6991 }
6992
6993 // Return the number of arguments, excluding the format.
6994 size_t GetNumberOfArguments() const {
6995 DCHECK_GE(InputCount(), 1u);
6996 return InputCount() - 1u;
6997 }
6998
6999 size_t FormatIndex() const {
7000 return GetNumberOfArguments();
7001 }
7002
7003 HIntConstant* GetFormat() {
7004 return InputAt(FormatIndex())->AsIntConstant();
7005 }
7006
7007 bool NeedsEnvironment() const override { return true; }
7008
7009 bool CanThrow() const override { return true; }
7010
Vladimir Markob1fe5e12020-03-10 14:30:49 +00007011 bool CanBeNull() const override { return false; }
7012
Vladimir Marko552a1342017-10-31 10:56:47 +00007013 DECLARE_INSTRUCTION(StringBuilderAppend);
7014
7015 protected:
7016 DEFAULT_COPY_CONSTRUCTOR(StringBuilderAppend);
7017};
7018
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007019class HUnresolvedInstanceFieldGet final : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01007020 public:
7021 HUnresolvedInstanceFieldGet(HInstruction* obj,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007022 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01007023 uint32_t field_index,
7024 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307025 : HExpression(kUnresolvedInstanceFieldGet,
7026 field_type,
7027 SideEffects::AllExceptGCDependency(),
7028 dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01007029 field_index_(field_index) {
7030 SetRawInputAt(0, obj);
7031 }
7032
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007033 bool IsClonable() const override { return true; }
7034 bool NeedsEnvironment() const override { return true; }
7035 bool CanThrow() const override { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01007036
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007037 DataType::Type GetFieldType() const { return GetType(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01007038 uint32_t GetFieldIndex() const { return field_index_; }
7039
7040 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
7041
Artem Serovcced8ba2017-07-19 18:18:09 +01007042 protected:
7043 DEFAULT_COPY_CONSTRUCTOR(UnresolvedInstanceFieldGet);
7044
Calin Juravlee460d1d2015-09-29 04:52:17 +01007045 private:
7046 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01007047};
7048
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007049class HUnresolvedInstanceFieldSet final : public HExpression<2> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01007050 public:
7051 HUnresolvedInstanceFieldSet(HInstruction* obj,
7052 HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007053 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01007054 uint32_t field_index,
7055 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01007056 : HExpression(kUnresolvedInstanceFieldSet, SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01007057 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007058 SetPackedField<FieldTypeField>(field_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007059 DCHECK_EQ(DataType::Kind(field_type), DataType::Kind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01007060 SetRawInputAt(0, obj);
7061 SetRawInputAt(1, value);
7062 }
7063
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007064 bool IsClonable() const override { return true; }
7065 bool NeedsEnvironment() const override { return true; }
7066 bool CanThrow() const override { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01007067
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007068 DataType::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01007069 uint32_t GetFieldIndex() const { return field_index_; }
7070
7071 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
7072
Artem Serovcced8ba2017-07-19 18:18:09 +01007073 protected:
7074 DEFAULT_COPY_CONSTRUCTOR(UnresolvedInstanceFieldSet);
7075
Calin Juravlee460d1d2015-09-29 04:52:17 +01007076 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007077 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
7078 static constexpr size_t kFieldFieldTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007079 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00007080 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
7081 kFieldFieldType + kFieldFieldTypeSize;
7082 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
7083 "Too many packed fields.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007084 using FieldTypeField = BitField<DataType::Type, kFieldFieldType, kFieldFieldTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00007085
Calin Juravlee460d1d2015-09-29 04:52:17 +01007086 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01007087};
7088
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007089class HUnresolvedStaticFieldGet final : public HExpression<0> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01007090 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007091 HUnresolvedStaticFieldGet(DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01007092 uint32_t field_index,
7093 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307094 : HExpression(kUnresolvedStaticFieldGet,
7095 field_type,
7096 SideEffects::AllExceptGCDependency(),
7097 dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01007098 field_index_(field_index) {
7099 }
7100
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007101 bool IsClonable() const override { return true; }
7102 bool NeedsEnvironment() const override { return true; }
7103 bool CanThrow() const override { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01007104
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007105 DataType::Type GetFieldType() const { return GetType(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01007106 uint32_t GetFieldIndex() const { return field_index_; }
7107
7108 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
7109
Artem Serovcced8ba2017-07-19 18:18:09 +01007110 protected:
7111 DEFAULT_COPY_CONSTRUCTOR(UnresolvedStaticFieldGet);
7112
Calin Juravlee460d1d2015-09-29 04:52:17 +01007113 private:
7114 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01007115};
7116
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007117class HUnresolvedStaticFieldSet final : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01007118 public:
7119 HUnresolvedStaticFieldSet(HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007120 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01007121 uint32_t field_index,
7122 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01007123 : HExpression(kUnresolvedStaticFieldSet, SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01007124 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007125 SetPackedField<FieldTypeField>(field_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007126 DCHECK_EQ(DataType::Kind(field_type), DataType::Kind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01007127 SetRawInputAt(0, value);
7128 }
7129
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007130 bool IsClonable() const override { return true; }
7131 bool NeedsEnvironment() const override { return true; }
7132 bool CanThrow() const override { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01007133
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007134 DataType::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01007135 uint32_t GetFieldIndex() const { return field_index_; }
7136
7137 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
7138
Artem Serovcced8ba2017-07-19 18:18:09 +01007139 protected:
7140 DEFAULT_COPY_CONSTRUCTOR(UnresolvedStaticFieldSet);
7141
Calin Juravlee460d1d2015-09-29 04:52:17 +01007142 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007143 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
7144 static constexpr size_t kFieldFieldTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007145 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00007146 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
7147 kFieldFieldType + kFieldFieldTypeSize;
7148 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
7149 "Too many packed fields.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007150 using FieldTypeField = BitField<DataType::Type, kFieldFieldType, kFieldFieldTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00007151
Calin Juravlee460d1d2015-09-29 04:52:17 +01007152 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01007153};
7154
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007155// Implement the move-exception DEX instruction.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007156class HLoadException final : public HExpression<0> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007157 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06007158 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307159 : HExpression(kLoadException, DataType::Type::kReference, SideEffects::None(), dex_pc) {
7160 }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007161
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007162 bool CanBeNull() const override { return false; }
David Brazdilbbd733e2015-08-18 17:48:17 +01007163
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007164 DECLARE_INSTRUCTION(LoadException);
7165
Artem Serovcced8ba2017-07-19 18:18:09 +01007166 protected:
7167 DEFAULT_COPY_CONSTRUCTOR(LoadException);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007168};
7169
David Brazdilcb1c0552015-08-04 16:22:25 +01007170// Implicit part of move-exception which clears thread-local exception storage.
7171// Must not be removed because the runtime expects the TLS to get cleared.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007172class HClearException final : public HExpression<0> {
David Brazdilcb1c0552015-08-04 16:22:25 +01007173 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06007174 explicit HClearException(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01007175 : HExpression(kClearException, SideEffects::AllWrites(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307176 }
David Brazdilcb1c0552015-08-04 16:22:25 +01007177
7178 DECLARE_INSTRUCTION(ClearException);
7179
Artem Serovcced8ba2017-07-19 18:18:09 +01007180 protected:
7181 DEFAULT_COPY_CONSTRUCTOR(ClearException);
David Brazdilcb1c0552015-08-04 16:22:25 +01007182};
7183
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007184class HThrow final : public HExpression<1> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007185 public:
7186 HThrow(HInstruction* exception, uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01007187 : HExpression(kThrow, SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007188 SetRawInputAt(0, exception);
7189 }
7190
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007191 bool IsControlFlow() const override { return true; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007192
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007193 bool NeedsEnvironment() const override { return true; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007194
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007195 bool CanThrow() const override { return true; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007196
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007197 bool AlwaysThrows() const override { return true; }
Aart Bika8b8e9b2018-01-09 11:01:02 -08007198
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007199 DECLARE_INSTRUCTION(Throw);
7200
Artem Serovcced8ba2017-07-19 18:18:09 +01007201 protected:
7202 DEFAULT_COPY_CONSTRUCTOR(Throw);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007203};
7204
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007205/**
7206 * Implementation strategies for the code generator of a HInstanceOf
7207 * or `HCheckCast`.
7208 */
7209enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01007210 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007211 kExactCheck, // Can do a single class compare.
7212 kClassHierarchyCheck, // Can just walk the super class chain.
7213 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
7214 kInterfaceCheck, // No optimization yet when checking against an interface.
7215 kArrayObjectCheck, // Can just check if the array is not primitive.
Vladimir Markoa1de9182016-02-25 11:37:38 +00007216 kArrayCheck, // No optimization yet when checking against a generic array.
Vladimir Marko175e7862018-03-27 09:03:13 +00007217 kBitstringCheck, // Compare the type check bitstring.
Vladimir Markoa1de9182016-02-25 11:37:38 +00007218 kLast = kArrayCheck
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007219};
7220
Roland Levillain86503782016-02-11 19:07:30 +00007221std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs);
7222
Vladimir Marko175e7862018-03-27 09:03:13 +00007223// Note: HTypeCheckInstruction is just a helper class, not an abstract instruction with an
7224// `IsTypeCheckInstruction()`. (New virtual methods in the HInstruction class have a high cost.)
7225class HTypeCheckInstruction : public HVariableInputSizeInstruction {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007226 public:
Vladimir Marko175e7862018-03-27 09:03:13 +00007227 HTypeCheckInstruction(InstructionKind kind,
Vladimir Markobd785672018-05-03 17:09:09 +01007228 DataType::Type type,
Vladimir Marko175e7862018-03-27 09:03:13 +00007229 HInstruction* object,
7230 HInstruction* target_class_or_null,
7231 TypeCheckKind check_kind,
7232 Handle<mirror::Class> klass,
7233 uint32_t dex_pc,
7234 ArenaAllocator* allocator,
7235 HIntConstant* bitstring_path_to_root,
7236 HIntConstant* bitstring_mask,
7237 SideEffects side_effects)
7238 : HVariableInputSizeInstruction(
7239 kind,
Vladimir Markobd785672018-05-03 17:09:09 +01007240 type,
Vladimir Marko175e7862018-03-27 09:03:13 +00007241 side_effects,
7242 dex_pc,
7243 allocator,
Andreas Gampe3db70682018-12-26 15:12:03 -08007244 /* number_of_inputs= */ check_kind == TypeCheckKind::kBitstringCheck ? 4u : 2u,
Vladimir Marko175e7862018-03-27 09:03:13 +00007245 kArenaAllocTypeCheckInputs),
7246 klass_(klass) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007247 SetPackedField<TypeCheckKindField>(check_kind);
7248 SetPackedFlag<kFlagMustDoNullCheck>(true);
Vladimir Marko175e7862018-03-27 09:03:13 +00007249 SetPackedFlag<kFlagValidTargetClassRTI>(false);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007250 SetRawInputAt(0, object);
Vladimir Marko175e7862018-03-27 09:03:13 +00007251 SetRawInputAt(1, target_class_or_null);
7252 DCHECK_EQ(check_kind == TypeCheckKind::kBitstringCheck, bitstring_path_to_root != nullptr);
7253 DCHECK_EQ(check_kind == TypeCheckKind::kBitstringCheck, bitstring_mask != nullptr);
7254 if (check_kind == TypeCheckKind::kBitstringCheck) {
7255 DCHECK(target_class_or_null->IsNullConstant());
7256 SetRawInputAt(2, bitstring_path_to_root);
7257 SetRawInputAt(3, bitstring_mask);
7258 } else {
7259 DCHECK(target_class_or_null->IsLoadClass());
7260 }
Vladimir Marko87584542017-12-12 17:47:52 +00007261 }
7262
7263 HLoadClass* GetTargetClass() const {
Vladimir Marko175e7862018-03-27 09:03:13 +00007264 DCHECK_NE(GetTypeCheckKind(), TypeCheckKind::kBitstringCheck);
Vladimir Marko87584542017-12-12 17:47:52 +00007265 HInstruction* load_class = InputAt(1);
7266 DCHECK(load_class->IsLoadClass());
7267 return load_class->AsLoadClass();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007268 }
7269
Vladimir Marko175e7862018-03-27 09:03:13 +00007270 uint32_t GetBitstringPathToRoot() const {
7271 DCHECK_EQ(GetTypeCheckKind(), TypeCheckKind::kBitstringCheck);
7272 HInstruction* path_to_root = InputAt(2);
7273 DCHECK(path_to_root->IsIntConstant());
7274 return static_cast<uint32_t>(path_to_root->AsIntConstant()->GetValue());
7275 }
7276
7277 uint32_t GetBitstringMask() const {
7278 DCHECK_EQ(GetTypeCheckKind(), TypeCheckKind::kBitstringCheck);
7279 HInstruction* mask = InputAt(3);
7280 DCHECK(mask->IsIntConstant());
7281 return static_cast<uint32_t>(mask->AsIntConstant()->GetValue());
7282 }
7283
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007284 bool IsClonable() const override { return true; }
7285 bool CanBeMoved() const override { return true; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007286
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007287 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Marko175e7862018-03-27 09:03:13 +00007288 DCHECK(other->IsInstanceOf() || other->IsCheckCast()) << other->DebugName();
7289 return GetPackedFields() == down_cast<const HTypeCheckInstruction*>(other)->GetPackedFields();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007290 }
7291
Andreas Gampe3fbd3ad2018-03-26 21:14:46 +00007292 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
7293 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
7294 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
7295 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
7296
Vladimir Marko175e7862018-03-27 09:03:13 +00007297 ReferenceTypeInfo GetTargetClassRTI() {
7298 if (GetPackedFlag<kFlagValidTargetClassRTI>()) {
7299 // Note: The is_exact flag from the return value should not be used.
Andreas Gampe3db70682018-12-26 15:12:03 -08007300 return ReferenceTypeInfo::CreateUnchecked(klass_, /* is_exact= */ true);
Vladimir Marko175e7862018-03-27 09:03:13 +00007301 } else {
7302 return ReferenceTypeInfo::CreateInvalid();
7303 }
7304 }
7305
7306 // Target class RTI is marked as valid by RTP if the klass_ is admissible.
7307 void SetValidTargetClassRTI() REQUIRES_SHARED(Locks::mutator_lock_) {
7308 DCHECK(klass_ != nullptr);
7309 SetPackedFlag<kFlagValidTargetClassRTI>(true);
7310 }
7311
7312 Handle<mirror::Class> GetClass() const {
7313 return klass_;
7314 }
7315
7316 protected:
7317 DEFAULT_COPY_CONSTRUCTOR(TypeCheckInstruction);
7318
7319 private:
7320 static constexpr size_t kFieldTypeCheckKind = kNumberOfGenericPackedBits;
7321 static constexpr size_t kFieldTypeCheckKindSize =
7322 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
7323 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
7324 static constexpr size_t kFlagValidTargetClassRTI = kFlagMustDoNullCheck + 1;
7325 static constexpr size_t kNumberOfInstanceOfPackedBits = kFlagValidTargetClassRTI + 1;
7326 static_assert(kNumberOfInstanceOfPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
7327 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
7328
7329 Handle<mirror::Class> klass_;
7330};
7331
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007332class HInstanceOf final : public HTypeCheckInstruction {
Vladimir Marko175e7862018-03-27 09:03:13 +00007333 public:
7334 HInstanceOf(HInstruction* object,
7335 HInstruction* target_class_or_null,
7336 TypeCheckKind check_kind,
7337 Handle<mirror::Class> klass,
7338 uint32_t dex_pc,
7339 ArenaAllocator* allocator,
7340 HIntConstant* bitstring_path_to_root,
7341 HIntConstant* bitstring_mask)
7342 : HTypeCheckInstruction(kInstanceOf,
Vladimir Markobd785672018-05-03 17:09:09 +01007343 DataType::Type::kBool,
Vladimir Marko175e7862018-03-27 09:03:13 +00007344 object,
7345 target_class_or_null,
7346 check_kind,
7347 klass,
7348 dex_pc,
7349 allocator,
7350 bitstring_path_to_root,
7351 bitstring_mask,
7352 SideEffectsForArchRuntimeCalls(check_kind)) {}
7353
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007354 bool IsClonable() const override { return true; }
Artem Serova6e26142018-06-19 14:55:17 +01007355
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007356 bool NeedsEnvironment() const override {
Vladimir Marko175e7862018-03-27 09:03:13 +00007357 return CanCallRuntime(GetTypeCheckKind());
7358 }
7359
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00007360 static bool CanCallRuntime(TypeCheckKind check_kind) {
Vladimir Marko54f4fbd2020-02-12 10:52:22 +00007361 // TODO: Re-evaluate now that mips codegen has been removed.
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00007362 return check_kind != TypeCheckKind::kExactCheck;
7363 }
7364
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007365 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00007366 return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01007367 }
7368
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007369 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007370
Artem Serovcced8ba2017-07-19 18:18:09 +01007371 protected:
7372 DEFAULT_COPY_CONSTRUCTOR(InstanceOf);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007373};
7374
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007375class HBoundType final : public HExpression<1> {
Calin Juravleb1498f62015-02-16 13:13:29 +00007376 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -07007377 explicit HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307378 : HExpression(kBoundType, DataType::Type::kReference, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00007379 upper_bound_(ReferenceTypeInfo::CreateInvalid()) {
7380 SetPackedFlag<kFlagUpperCanBeNull>(true);
7381 SetPackedFlag<kFlagCanBeNull>(true);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007382 DCHECK_EQ(input->GetType(), DataType::Type::kReference);
Calin Juravleb1498f62015-02-16 13:13:29 +00007383 SetRawInputAt(0, input);
7384 }
7385
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007386 bool InstructionDataEquals(const HInstruction* other) const override;
7387 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01007388
David Brazdilf5552582015-12-27 13:36:12 +00007389 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007390 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00007391 bool GetUpperCanBeNull() const { return GetPackedFlag<kFlagUpperCanBeNull>(); }
David Brazdilf5552582015-12-27 13:36:12 +00007392 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00007393
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007394 void SetCanBeNull(bool can_be_null) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007395 DCHECK(GetUpperCanBeNull() || !can_be_null);
7396 SetPackedFlag<kFlagCanBeNull>(can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00007397 }
7398
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007399 bool CanBeNull() const override { return GetPackedFlag<kFlagCanBeNull>(); }
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007400
Calin Juravleb1498f62015-02-16 13:13:29 +00007401 DECLARE_INSTRUCTION(BoundType);
7402
Artem Serovcced8ba2017-07-19 18:18:09 +01007403 protected:
7404 DEFAULT_COPY_CONSTRUCTOR(BoundType);
7405
Calin Juravleb1498f62015-02-16 13:13:29 +00007406 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007407 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
7408 // is false then CanBeNull() cannot be true).
Vladimir Markobd785672018-05-03 17:09:09 +01007409 static constexpr size_t kFlagUpperCanBeNull = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00007410 static constexpr size_t kFlagCanBeNull = kFlagUpperCanBeNull + 1;
7411 static constexpr size_t kNumberOfBoundTypePackedBits = kFlagCanBeNull + 1;
7412 static_assert(kNumberOfBoundTypePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
7413
Calin Juravleb1498f62015-02-16 13:13:29 +00007414 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007415 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
7416 // It is used to bound the type in cases like:
7417 // if (x instanceof ClassX) {
7418 // // uper_bound_ will be ClassX
7419 // }
David Brazdilf5552582015-12-27 13:36:12 +00007420 ReferenceTypeInfo upper_bound_;
Calin Juravleb1498f62015-02-16 13:13:29 +00007421};
7422
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007423class HCheckCast final : public HTypeCheckInstruction {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007424 public:
7425 HCheckCast(HInstruction* object,
Vladimir Marko175e7862018-03-27 09:03:13 +00007426 HInstruction* target_class_or_null,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007427 TypeCheckKind check_kind,
Vladimir Marko175e7862018-03-27 09:03:13 +00007428 Handle<mirror::Class> klass,
7429 uint32_t dex_pc,
7430 ArenaAllocator* allocator,
7431 HIntConstant* bitstring_path_to_root,
7432 HIntConstant* bitstring_mask)
7433 : HTypeCheckInstruction(kCheckCast,
Vladimir Markobd785672018-05-03 17:09:09 +01007434 DataType::Type::kVoid,
Vladimir Marko175e7862018-03-27 09:03:13 +00007435 object,
7436 target_class_or_null,
7437 check_kind,
7438 klass,
7439 dex_pc,
7440 allocator,
7441 bitstring_path_to_root,
7442 bitstring_mask,
7443 SideEffects::CanTriggerGC()) {}
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007444
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007445 bool IsClonable() const override { return true; }
7446 bool NeedsEnvironment() const override {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007447 // Instruction may throw a CheckCastError.
7448 return true;
7449 }
7450
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007451 bool CanThrow() const override { return true; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007452
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007453 DECLARE_INSTRUCTION(CheckCast);
7454
Artem Serovcced8ba2017-07-19 18:18:09 +01007455 protected:
7456 DEFAULT_COPY_CONSTRUCTOR(CheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007457};
7458
Andreas Gampe26de38b2016-07-27 17:53:11 -07007459/**
7460 * @brief Memory barrier types (see "The JSR-133 Cookbook for Compiler Writers").
7461 * @details We define the combined barrier types that are actually required
7462 * by the Java Memory Model, rather than using exactly the terminology from
7463 * the JSR-133 cookbook. These should, in many cases, be replaced by acquire/release
7464 * primitives. Note that the JSR-133 cookbook generally does not deal with
7465 * store atomicity issues, and the recipes there are not always entirely sufficient.
7466 * The current recipe is as follows:
7467 * -# Use AnyStore ~= (LoadStore | StoreStore) ~= release barrier before volatile store.
7468 * -# Use AnyAny barrier after volatile store. (StoreLoad is as expensive.)
7469 * -# Use LoadAny barrier ~= (LoadLoad | LoadStore) ~= acquire barrier after each volatile load.
7470 * -# Use StoreStore barrier after all stores but before return from any constructor whose
7471 * class has final fields.
7472 * -# Use NTStoreStore to order non-temporal stores with respect to all later
7473 * store-to-memory instructions. Only generated together with non-temporal stores.
7474 */
7475enum MemBarrierKind {
7476 kAnyStore,
7477 kLoadAny,
7478 kStoreStore,
7479 kAnyAny,
7480 kNTStoreStore,
7481 kLastBarrierKind = kNTStoreStore
7482};
7483std::ostream& operator<<(std::ostream& os, const MemBarrierKind& kind);
7484
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007485class HMemoryBarrier final : public HExpression<0> {
Calin Juravle27df7582015-04-17 19:12:31 +01007486 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06007487 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01007488 : HExpression(kMemoryBarrier,
7489 SideEffects::AllWritesAndReads(), // Assume write/read on all fields/arrays.
7490 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007491 SetPackedField<BarrierKindField>(barrier_kind);
7492 }
Calin Juravle27df7582015-04-17 19:12:31 +01007493
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007494 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01007495
Vladimir Markoa1de9182016-02-25 11:37:38 +00007496 MemBarrierKind GetBarrierKind() { return GetPackedField<BarrierKindField>(); }
Calin Juravle27df7582015-04-17 19:12:31 +01007497
7498 DECLARE_INSTRUCTION(MemoryBarrier);
7499
Artem Serovcced8ba2017-07-19 18:18:09 +01007500 protected:
7501 DEFAULT_COPY_CONSTRUCTOR(MemoryBarrier);
7502
Calin Juravle27df7582015-04-17 19:12:31 +01007503 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007504 static constexpr size_t kFieldBarrierKind = HInstruction::kNumberOfGenericPackedBits;
7505 static constexpr size_t kFieldBarrierKindSize =
7506 MinimumBitsToStore(static_cast<size_t>(kLastBarrierKind));
7507 static constexpr size_t kNumberOfMemoryBarrierPackedBits =
7508 kFieldBarrierKind + kFieldBarrierKindSize;
7509 static_assert(kNumberOfMemoryBarrierPackedBits <= kMaxNumberOfPackedBits,
7510 "Too many packed fields.");
7511 using BarrierKindField = BitField<MemBarrierKind, kFieldBarrierKind, kFieldBarrierKindSize>;
Calin Juravle27df7582015-04-17 19:12:31 +01007512};
7513
Igor Murashkind01745e2017-04-05 16:40:31 -07007514// A constructor fence orders all prior stores to fields that could be accessed via a final field of
7515// the specified object(s), with respect to any subsequent store that might "publish"
7516// (i.e. make visible) the specified object to another thread.
7517//
7518// JLS 17.5.1 "Semantics of final fields" states that a freeze action happens
7519// for all final fields (that were set) at the end of the invoked constructor.
7520//
7521// The constructor fence models the freeze actions for the final fields of an object
7522// being constructed (semantically at the end of the constructor). Constructor fences
7523// have a per-object affinity; two separate objects being constructed get two separate
7524// constructor fences.
7525//
7526// (Note: that if calling a super-constructor or forwarding to another constructor,
7527// the freezes would happen at the end of *that* constructor being invoked).
7528//
7529// The memory model guarantees that when the object being constructed is "published" after
7530// constructor completion (i.e. escapes the current thread via a store), then any final field
7531// writes must be observable on other threads (once they observe that publication).
7532//
7533// Further, anything written before the freeze, and read by dereferencing through the final field,
7534// must also be visible (so final object field could itself have an object with non-final fields;
7535// yet the freeze must also extend to them).
7536//
7537// Constructor example:
7538//
7539// class HasFinal {
7540// final int field; Optimizing IR for <init>()V:
7541// HasFinal() {
7542// field = 123; HInstanceFieldSet(this, HasFinal.field, 123)
7543// // freeze(this.field); HConstructorFence(this)
7544// } HReturn
7545// }
7546//
7547// HConstructorFence can serve double duty as a fence for new-instance/new-array allocations of
7548// already-initialized classes; in that case the allocation must act as a "default-initializer"
7549// of the object which effectively writes the class pointer "final field".
7550//
7551// For example, we can model default-initialiation as roughly the equivalent of the following:
7552//
7553// class Object {
7554// private final Class header;
7555// }
7556//
7557// Java code: Optimizing IR:
7558//
7559// T new_instance<T>() {
7560// Object obj = allocate_memory(T.class.size); obj = HInvoke(art_quick_alloc_object, T)
7561// obj.header = T.class; // header write is done by above call.
7562// // freeze(obj.header) HConstructorFence(obj)
7563// return (T)obj;
7564// }
7565//
7566// See also:
Vladimir Markoc1c34522018-10-31 13:56:49 +00007567// * DexCompilationUnit::RequiresConstructorBarrier
Igor Murashkind01745e2017-04-05 16:40:31 -07007568// * QuasiAtomic::ThreadFenceForConstructor
7569//
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007570class HConstructorFence final : public HVariableInputSizeInstruction {
Igor Murashkind01745e2017-04-05 16:40:31 -07007571 // A fence has variable inputs because the inputs can be removed
7572 // after prepare_for_register_allocation phase.
7573 // (TODO: In the future a fence could freeze multiple objects
7574 // after merging two fences together.)
7575 public:
7576 // `fence_object` is the reference that needs to be protected for correct publication.
7577 //
7578 // It makes sense in the following situations:
7579 // * <init> constructors, it's the "this" parameter (i.e. HParameterValue, s.t. IsThis() == true).
7580 // * new-instance-like instructions, it's the return value (i.e. HNewInstance).
7581 //
7582 // After construction the `fence_object` becomes the 0th input.
7583 // This is not an input in a real sense, but just a convenient place to stash the information
7584 // about the associated object.
7585 HConstructorFence(HInstruction* fence_object,
7586 uint32_t dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007587 ArenaAllocator* allocator)
Igor Murashkind01745e2017-04-05 16:40:31 -07007588 // We strongly suspect there is not a more accurate way to describe the fine-grained reordering
7589 // constraints described in the class header. We claim that these SideEffects constraints
7590 // enforce a superset of the real constraints.
7591 //
7592 // The ordering described above is conservatively modeled with SideEffects as follows:
7593 //
7594 // * To prevent reordering of the publication stores:
7595 // ----> "Reads of objects" is the initial SideEffect.
7596 // * For every primitive final field store in the constructor:
7597 // ----> Union that field's type as a read (e.g. "Read of T") into the SideEffect.
7598 // * If there are any stores to reference final fields in the constructor:
7599 // ----> Use a more conservative "AllReads" SideEffect because any stores to any references
7600 // that are reachable from `fence_object` also need to be prevented for reordering
7601 // (and we do not want to do alias analysis to figure out what those stores are).
7602 //
7603 // In the implementation, this initially starts out as an "all reads" side effect; this is an
7604 // even more conservative approach than the one described above, and prevents all of the
7605 // above reordering without analyzing any of the instructions in the constructor.
7606 //
7607 // If in a later phase we discover that there are no writes to reference final fields,
7608 // we can refine the side effect to a smaller set of type reads (see above constraints).
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307609 : HVariableInputSizeInstruction(kConstructorFence,
7610 SideEffects::AllReads(),
Igor Murashkind01745e2017-04-05 16:40:31 -07007611 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007612 allocator,
Andreas Gampe3db70682018-12-26 15:12:03 -08007613 /* number_of_inputs= */ 1,
Igor Murashkind01745e2017-04-05 16:40:31 -07007614 kArenaAllocConstructorFenceInputs) {
7615 DCHECK(fence_object != nullptr);
7616 SetRawInputAt(0, fence_object);
7617 }
7618
7619 // The object associated with this constructor fence.
7620 //
7621 // (Note: This will be null after the prepare_for_register_allocation phase,
7622 // as all constructor fence inputs are removed there).
7623 HInstruction* GetFenceObject() const {
7624 return InputAt(0);
7625 }
7626
7627 // Find all the HConstructorFence uses (`fence_use`) for `this` and:
7628 // - Delete `fence_use` from `this`'s use list.
7629 // - Delete `this` from `fence_use`'s inputs list.
7630 // - If the `fence_use` is dead, remove it from the graph.
7631 //
7632 // A fence is considered dead once it no longer has any uses
7633 // and all of the inputs are dead.
7634 //
7635 // This must *not* be called during/after prepare_for_register_allocation,
7636 // because that removes all the inputs to the fences but the fence is actually
7637 // still considered live.
Igor Murashkin6ef45672017-08-08 13:59:55 -07007638 //
7639 // Returns how many HConstructorFence instructions were removed from graph.
7640 static size_t RemoveConstructorFences(HInstruction* instruction);
Igor Murashkind01745e2017-04-05 16:40:31 -07007641
Igor Murashkindd018df2017-08-09 10:38:31 -07007642 // Combine all inputs of `this` and `other` instruction and remove
7643 // `other` from the graph.
7644 //
7645 // Inputs are unique after the merge.
7646 //
7647 // Requirement: `this` must not be the same as `other.
7648 void Merge(HConstructorFence* other);
7649
Igor Murashkin79d8fa72017-04-18 09:37:23 -07007650 // Check if this constructor fence is protecting
7651 // an HNewInstance or HNewArray that is also the immediate
7652 // predecessor of `this`.
7653 //
Igor Murashkindd018df2017-08-09 10:38:31 -07007654 // If `ignore_inputs` is true, then the immediate predecessor doesn't need
7655 // to be one of the inputs of `this`.
7656 //
Igor Murashkin79d8fa72017-04-18 09:37:23 -07007657 // Returns the associated HNewArray or HNewInstance,
7658 // or null otherwise.
Igor Murashkindd018df2017-08-09 10:38:31 -07007659 HInstruction* GetAssociatedAllocation(bool ignore_inputs = false);
Igor Murashkin79d8fa72017-04-18 09:37:23 -07007660
Igor Murashkind01745e2017-04-05 16:40:31 -07007661 DECLARE_INSTRUCTION(ConstructorFence);
7662
Artem Serovcced8ba2017-07-19 18:18:09 +01007663 protected:
7664 DEFAULT_COPY_CONSTRUCTOR(ConstructorFence);
Igor Murashkind01745e2017-04-05 16:40:31 -07007665};
7666
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007667class HMonitorOperation final : public HExpression<1> {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007668 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007669 enum class OperationKind {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007670 kEnter,
7671 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00007672 kLast = kExit
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007673 };
7674
7675 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01007676 : HExpression(kMonitorOperation,
7677 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
7678 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007679 SetPackedField<OperationKindField>(kind);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007680 SetRawInputAt(0, object);
7681 }
7682
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00007683 // Instruction may go into runtime, so we need an environment.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007684 bool NeedsEnvironment() const override { return true; }
David Brazdilbff75032015-07-08 17:26:51 +00007685
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007686 bool CanThrow() const override {
David Brazdilbff75032015-07-08 17:26:51 +00007687 // Verifier guarantees that monitor-exit cannot throw.
7688 // This is important because it allows the HGraphBuilder to remove
7689 // a dead throw-catch loop generated for `synchronized` blocks/methods.
7690 return IsEnter();
7691 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007692
Vladimir Markoa1de9182016-02-25 11:37:38 +00007693 OperationKind GetOperationKind() const { return GetPackedField<OperationKindField>(); }
7694 bool IsEnter() const { return GetOperationKind() == OperationKind::kEnter; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007695
7696 DECLARE_INSTRUCTION(MonitorOperation);
7697
Artem Serovcced8ba2017-07-19 18:18:09 +01007698 protected:
7699 DEFAULT_COPY_CONSTRUCTOR(MonitorOperation);
7700
Calin Juravle52c48962014-12-16 17:02:57 +00007701 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007702 static constexpr size_t kFieldOperationKind = HInstruction::kNumberOfGenericPackedBits;
7703 static constexpr size_t kFieldOperationKindSize =
7704 MinimumBitsToStore(static_cast<size_t>(OperationKind::kLast));
7705 static constexpr size_t kNumberOfMonitorOperationPackedBits =
7706 kFieldOperationKind + kFieldOperationKindSize;
7707 static_assert(kNumberOfMonitorOperationPackedBits <= HInstruction::kMaxNumberOfPackedBits,
7708 "Too many packed fields.");
7709 using OperationKindField = BitField<OperationKind, kFieldOperationKind, kFieldOperationKindSize>;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007710};
7711
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007712class HSelect final : public HExpression<3> {
David Brazdil74eb1b22015-12-14 11:44:01 +00007713 public:
7714 HSelect(HInstruction* condition,
7715 HInstruction* true_value,
7716 HInstruction* false_value,
7717 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307718 : HExpression(kSelect, HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
David Brazdil74eb1b22015-12-14 11:44:01 +00007719 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
7720
7721 // First input must be `true_value` or `false_value` to allow codegens to
7722 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
7723 // that architectures which implement HSelect as a conditional move also
7724 // will not need to invert the condition.
7725 SetRawInputAt(0, false_value);
7726 SetRawInputAt(1, true_value);
7727 SetRawInputAt(2, condition);
7728 }
7729
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007730 bool IsClonable() const override { return true; }
David Brazdil74eb1b22015-12-14 11:44:01 +00007731 HInstruction* GetFalseValue() const { return InputAt(0); }
7732 HInstruction* GetTrueValue() const { return InputAt(1); }
7733 HInstruction* GetCondition() const { return InputAt(2); }
7734
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007735 bool CanBeMoved() const override { return true; }
7736 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01007737 return true;
7738 }
David Brazdil74eb1b22015-12-14 11:44:01 +00007739
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007740 bool CanBeNull() const override {
David Brazdil74eb1b22015-12-14 11:44:01 +00007741 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
7742 }
7743
7744 DECLARE_INSTRUCTION(Select);
7745
Artem Serovcced8ba2017-07-19 18:18:09 +01007746 protected:
7747 DEFAULT_COPY_CONSTRUCTOR(Select);
David Brazdil74eb1b22015-12-14 11:44:01 +00007748};
7749
Vladimir Markof9f64412015-09-02 14:05:49 +01007750class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007751 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01007752 MoveOperands(Location source,
7753 Location destination,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007754 DataType::Type type,
Nicolas Geoffray90218252015-04-15 11:56:51 +01007755 HInstruction* instruction)
7756 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007757
7758 Location GetSource() const { return source_; }
7759 Location GetDestination() const { return destination_; }
7760
7761 void SetSource(Location value) { source_ = value; }
7762 void SetDestination(Location value) { destination_ = value; }
7763
7764 // The parallel move resolver marks moves as "in-progress" by clearing the
7765 // destination (but not the source).
7766 Location MarkPending() {
7767 DCHECK(!IsPending());
7768 Location dest = destination_;
7769 destination_ = Location::NoLocation();
7770 return dest;
7771 }
7772
7773 void ClearPending(Location dest) {
7774 DCHECK(IsPending());
7775 destination_ = dest;
7776 }
7777
7778 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00007779 DCHECK(source_.IsValid() || destination_.IsInvalid());
7780 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007781 }
7782
7783 // True if this blocks a move from the given location.
7784 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08007785 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007786 }
7787
7788 // A move is redundant if it's been eliminated, if its source and
7789 // destination are the same, or if its destination is unneeded.
7790 bool IsRedundant() const {
7791 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
7792 }
7793
7794 // We clear both operands to indicate move that's been eliminated.
7795 void Eliminate() {
7796 source_ = destination_ = Location::NoLocation();
7797 }
7798
7799 bool IsEliminated() const {
7800 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
7801 return source_.IsInvalid();
7802 }
7803
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007804 DataType::Type GetType() const { return type_; }
Alexey Frunze4dda3372015-06-01 18:31:49 -07007805
Nicolas Geoffray90218252015-04-15 11:56:51 +01007806 bool Is64BitMove() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007807 return DataType::Is64BitType(type_);
Nicolas Geoffray90218252015-04-15 11:56:51 +01007808 }
7809
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007810 HInstruction* GetInstruction() const { return instruction_; }
7811
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007812 private:
7813 Location source_;
7814 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01007815 // The type this move is for.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007816 DataType::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007817 // The instruction this move is assocatied with. Null when this move is
7818 // for moving an input in the expected locations of user (including a phi user).
7819 // This is only used in debug mode, to ensure we do not connect interval siblings
7820 // in the same parallel move.
7821 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007822};
7823
Roland Levillainc9285912015-12-18 10:38:42 +00007824std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
7825
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007826static constexpr size_t kDefaultNumberOfMoves = 4;
7827
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007828class HParallelMove final : public HExpression<0> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007829 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007830 explicit HParallelMove(ArenaAllocator* allocator, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01007831 : HExpression(kParallelMove, SideEffects::None(), dex_pc),
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007832 moves_(allocator->Adapter(kArenaAllocMoveOperands)) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007833 moves_.reserve(kDefaultNumberOfMoves);
7834 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007835
Nicolas Geoffray90218252015-04-15 11:56:51 +01007836 void AddMove(Location source,
7837 Location destination,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007838 DataType::Type type,
Nicolas Geoffray90218252015-04-15 11:56:51 +01007839 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00007840 DCHECK(source.IsValid());
7841 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00007842 if (kIsDebugBuild) {
7843 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007844 for (const MoveOperands& move : moves_) {
7845 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007846 // Special case the situation where the move is for the spill slot
7847 // of the instruction.
7848 if ((GetPrevious() == instruction)
7849 || ((GetPrevious() == nullptr)
7850 && instruction->IsPhi()
7851 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007852 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007853 << "Doing parallel moves for the same instruction.";
7854 } else {
7855 DCHECK(false) << "Doing parallel moves for the same instruction.";
7856 }
7857 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00007858 }
7859 }
Vladimir Marko225b6462015-09-28 12:17:40 +01007860 for (const MoveOperands& move : moves_) {
7861 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01007862 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01007863 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01007864 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00007865 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007866 }
Vladimir Marko225b6462015-09-28 12:17:40 +01007867 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007868 }
7869
Vladimir Marko225b6462015-09-28 12:17:40 +01007870 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007871 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007872 }
7873
Vladimir Marko225b6462015-09-28 12:17:40 +01007874 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007875
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01007876 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007877
Artem Serovcced8ba2017-07-19 18:18:09 +01007878 protected:
7879 DEFAULT_COPY_CONSTRUCTOR(ParallelMove);
7880
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007881 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01007882 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007883};
7884
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007885// This instruction computes an intermediate address pointing in the 'middle' of an object. The
7886// result pointer cannot be handled by GC, so extra care is taken to make sure that this value is
7887// never used across anything that can trigger GC.
7888// The result of this instruction is not a pointer in the sense of `DataType::Type::kreference`.
7889// So we represent it by the type `DataType::Type::kInt`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007890class HIntermediateAddress final : public HExpression<2> {
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007891 public:
7892 HIntermediateAddress(HInstruction* base_address, HInstruction* offset, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307893 : HExpression(kIntermediateAddress,
7894 DataType::Type::kInt32,
7895 SideEffects::DependsOnGC(),
7896 dex_pc) {
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007897 DCHECK_EQ(DataType::Size(DataType::Type::kInt32),
7898 DataType::Size(DataType::Type::kReference))
7899 << "kPrimInt and kPrimNot have different sizes.";
7900 SetRawInputAt(0, base_address);
7901 SetRawInputAt(1, offset);
7902 }
7903
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007904 bool IsClonable() const override { return true; }
7905 bool CanBeMoved() const override { return true; }
7906 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007907 return true;
7908 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007909 bool IsActualObject() const override { return false; }
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007910
7911 HInstruction* GetBaseAddress() const { return InputAt(0); }
7912 HInstruction* GetOffset() const { return InputAt(1); }
7913
7914 DECLARE_INSTRUCTION(IntermediateAddress);
7915
Artem Serovcced8ba2017-07-19 18:18:09 +01007916 protected:
7917 DEFAULT_COPY_CONSTRUCTOR(IntermediateAddress);
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007918};
7919
7920
Mark Mendell0616ae02015-04-17 12:49:27 -04007921} // namespace art
7922
Aart Bikf8f5a162017-02-06 15:35:29 -08007923#include "nodes_vector.h"
7924
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03007925#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
7926#include "nodes_shared.h"
7927#endif
Shalini Salomi Bodapatidd121f62018-10-26 15:03:53 +05307928#if defined(ART_ENABLE_CODEGEN_x86) || defined(ART_ENABLE_CODEGEN_x86_64)
Mark Mendell0616ae02015-04-17 12:49:27 -04007929#include "nodes_x86.h"
7930#endif
7931
Vladimir Marko0a516052019-10-14 13:00:44 +00007932namespace art {
Mark Mendell0616ae02015-04-17 12:49:27 -04007933
Igor Murashkin6ef45672017-08-08 13:59:55 -07007934class OptimizingCompilerStats;
7935
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007936class HGraphVisitor : public ValueObject {
7937 public:
Igor Murashkin6ef45672017-08-08 13:59:55 -07007938 explicit HGraphVisitor(HGraph* graph, OptimizingCompilerStats* stats = nullptr)
7939 : stats_(stats),
7940 graph_(graph) {}
Dave Allison20dfc792014-06-16 20:44:29 -07007941 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007942
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007943 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007944 virtual void VisitBasicBlock(HBasicBlock* block);
7945
Roland Levillain633021e2014-10-01 14:12:25 +01007946 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007947 void VisitInsertionOrder();
7948
Roland Levillain633021e2014-10-01 14:12:25 +01007949 // Visit the graph following dominator tree reverse post-order.
7950 void VisitReversePostOrder();
7951
Nicolas Geoffray787c3072014-03-17 10:20:19 +00007952 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007953
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007954 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007955#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007956 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
7957
7958 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
7959
7960#undef DECLARE_VISIT_INSTRUCTION
7961
Igor Murashkin6ef45672017-08-08 13:59:55 -07007962 protected:
7963 OptimizingCompilerStats* stats_;
7964
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007965 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07007966 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007967
7968 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
7969};
7970
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007971class HGraphDelegateVisitor : public HGraphVisitor {
7972 public:
Igor Murashkin6ef45672017-08-08 13:59:55 -07007973 explicit HGraphDelegateVisitor(HGraph* graph, OptimizingCompilerStats* stats = nullptr)
7974 : HGraphVisitor(graph, stats) {}
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007975 virtual ~HGraphDelegateVisitor() {}
7976
7977 // Visit functions that delegate to to super class.
7978#define DECLARE_VISIT_INSTRUCTION(name, super) \
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007979 void Visit##name(H##name* instr) override { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007980
7981 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
7982
7983#undef DECLARE_VISIT_INSTRUCTION
7984
7985 private:
7986 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
7987};
7988
Artem Serovcced8ba2017-07-19 18:18:09 +01007989// Create a clone of the instruction, insert it into the graph; replace the old one with a new
7990// and remove the old instruction.
7991HInstruction* ReplaceInstrOrPhiByClone(HInstruction* instr);
7992
7993// Create a clone for each clonable instructions/phis and replace the original with the clone.
7994//
7995// Used for testing individual instruction cloner.
7996class CloneAndReplaceInstructionVisitor : public HGraphDelegateVisitor {
7997 public:
7998 explicit CloneAndReplaceInstructionVisitor(HGraph* graph)
Artem Serov7f4aff62017-06-21 17:02:18 +01007999 : HGraphDelegateVisitor(graph), instr_replaced_by_clones_count_(0) {}
Artem Serovcced8ba2017-07-19 18:18:09 +01008000
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01008001 void VisitInstruction(HInstruction* instruction) override {
Artem Serovcced8ba2017-07-19 18:18:09 +01008002 if (instruction->IsClonable()) {
8003 ReplaceInstrOrPhiByClone(instruction);
Artem Serov7f4aff62017-06-21 17:02:18 +01008004 instr_replaced_by_clones_count_++;
Artem Serovcced8ba2017-07-19 18:18:09 +01008005 }
8006 }
8007
Artem Serov7f4aff62017-06-21 17:02:18 +01008008 size_t GetInstrReplacedByClonesCount() const { return instr_replaced_by_clones_count_; }
Artem Serovcced8ba2017-07-19 18:18:09 +01008009
8010 private:
Artem Serov7f4aff62017-06-21 17:02:18 +01008011 size_t instr_replaced_by_clones_count_;
Artem Serovcced8ba2017-07-19 18:18:09 +01008012
8013 DISALLOW_COPY_AND_ASSIGN(CloneAndReplaceInstructionVisitor);
8014};
8015
Nicolas Geoffray82091da2015-01-26 10:02:45 +00008016// Iterator over the blocks that art part of the loop. Includes blocks part
8017// of an inner loop. The order in which the blocks are iterated is on their
8018// block id.
8019class HBlocksInLoopIterator : public ValueObject {
8020 public:
8021 explicit HBlocksInLoopIterator(const HLoopInformation& info)
8022 : blocks_in_loop_(info.GetBlocks()),
8023 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
8024 index_(0) {
8025 if (!blocks_in_loop_.IsBitSet(index_)) {
8026 Advance();
8027 }
8028 }
8029
Vladimir Markofa6b93c2015-09-15 10:15:55 +01008030 bool Done() const { return index_ == blocks_.size(); }
8031 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00008032 void Advance() {
8033 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01008034 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00008035 if (blocks_in_loop_.IsBitSet(index_)) {
8036 break;
8037 }
8038 }
8039 }
8040
8041 private:
8042 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01008043 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00008044 size_t index_;
8045
8046 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
8047};
8048
Mingyao Yang3584bce2015-05-19 16:01:59 -07008049// Iterator over the blocks that art part of the loop. Includes blocks part
8050// of an inner loop. The order in which the blocks are iterated is reverse
8051// post order.
8052class HBlocksInLoopReversePostOrderIterator : public ValueObject {
8053 public:
8054 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
8055 : blocks_in_loop_(info.GetBlocks()),
8056 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
8057 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01008058 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07008059 Advance();
8060 }
8061 }
8062
Vladimir Markofa6b93c2015-09-15 10:15:55 +01008063 bool Done() const { return index_ == blocks_.size(); }
8064 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07008065 void Advance() {
8066 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01008067 for (size_t e = blocks_.size(); index_ < e; ++index_) {
8068 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07008069 break;
8070 }
8071 }
8072 }
8073
8074 private:
8075 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01008076 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07008077 size_t index_;
8078
8079 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
8080};
8081
Aart Bikf3e61ee2017-04-12 17:09:20 -07008082// Returns int64_t value of a properly typed constant.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00008083inline int64_t Int64FromConstant(HConstant* constant) {
David Brazdilc0b601b2016-02-08 14:20:45 +00008084 if (constant->IsIntConstant()) {
8085 return constant->AsIntConstant()->GetValue();
8086 } else if (constant->IsLongConstant()) {
8087 return constant->AsLongConstant()->GetValue();
8088 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00008089 DCHECK(constant->IsNullConstant()) << constant->DebugName();
David Brazdilc0b601b2016-02-08 14:20:45 +00008090 return 0;
8091 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00008092}
8093
Aart Bikf3e61ee2017-04-12 17:09:20 -07008094// Returns true iff instruction is an integral constant (and sets value on success).
8095inline bool IsInt64AndGet(HInstruction* instruction, /*out*/ int64_t* value) {
8096 if (instruction->IsIntConstant()) {
8097 *value = instruction->AsIntConstant()->GetValue();
8098 return true;
8099 } else if (instruction->IsLongConstant()) {
8100 *value = instruction->AsLongConstant()->GetValue();
8101 return true;
8102 } else if (instruction->IsNullConstant()) {
8103 *value = 0;
8104 return true;
8105 }
8106 return false;
8107}
8108
Aart Bik0148de42017-09-05 09:25:01 -07008109// Returns true iff instruction is the given integral constant.
8110inline bool IsInt64Value(HInstruction* instruction, int64_t value) {
8111 int64_t val = 0;
8112 return IsInt64AndGet(instruction, &val) && val == value;
8113}
8114
8115// Returns true iff instruction is a zero bit pattern.
8116inline bool IsZeroBitPattern(HInstruction* instruction) {
8117 return instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern();
8118}
8119
Vladimir Marko6d5b7e32018-05-09 16:52:48 +01008120// Implement HInstruction::Is##type() for concrete instructions.
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008121#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Markoa90dd512018-05-04 15:04:45 +01008122 inline bool HInstruction::Is##type() const { return GetKind() == k##type; }
8123 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
8124#undef INSTRUCTION_TYPE_CHECK
8125
Vladimir Marko6d5b7e32018-05-09 16:52:48 +01008126// Implement HInstruction::Is##type() for abstract instructions.
Vladimir Markoa90dd512018-05-04 15:04:45 +01008127#define INSTRUCTION_TYPE_CHECK_RESULT(type, super) \
8128 std::is_base_of<BaseType, H##type>::value,
8129#define INSTRUCTION_TYPE_CHECK(type, super) \
8130 inline bool HInstruction::Is##type() const { \
8131 DCHECK_LT(GetKind(), kLastInstructionKind); \
8132 using BaseType = H##type; \
8133 static constexpr bool results[] = { \
8134 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK_RESULT) \
8135 }; \
8136 return results[static_cast<size_t>(GetKind())]; \
8137 }
8138
8139 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
8140#undef INSTRUCTION_TYPE_CHECK
Vladimir Marko6d5b7e32018-05-09 16:52:48 +01008141#undef INSTRUCTION_TYPE_CHECK_RESULT
Vladimir Markoa90dd512018-05-04 15:04:45 +01008142
8143#define INSTRUCTION_TYPE_CAST(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008144 inline const H##type* HInstruction::As##type() const { \
8145 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
8146 } \
8147 inline H##type* HInstruction::As##type() { \
8148 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
8149 }
8150
Vladimir Markoa90dd512018-05-04 15:04:45 +01008151 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CAST)
8152#undef INSTRUCTION_TYPE_CAST
8153
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008154
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00008155// Create space in `blocks` for adding `number_of_new_blocks` entries
8156// starting at location `at`. Blocks after `at` are moved accordingly.
8157inline void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks,
8158 size_t number_of_new_blocks,
8159 size_t after) {
8160 DCHECK_LT(after, blocks->size());
8161 size_t old_size = blocks->size();
8162 size_t new_size = old_size + number_of_new_blocks;
8163 blocks->resize(new_size);
8164 std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end());
8165}
8166
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00008167/*
8168 * Hunt "under the hood" of array lengths (leading to array references),
8169 * null checks (also leading to array references), and new arrays
8170 * (leading to the actual length). This makes it more likely related
8171 * instructions become actually comparable.
8172 */
8173inline HInstruction* HuntForDeclaration(HInstruction* instruction) {
8174 while (instruction->IsArrayLength() ||
8175 instruction->IsNullCheck() ||
8176 instruction->IsNewArray()) {
8177 instruction = instruction->IsNewArray()
8178 ? instruction->AsNewArray()->GetLength()
8179 : instruction->InputAt(0);
8180 }
8181 return instruction;
8182}
8183
Artem Serovb47b9782019-12-04 21:02:09 +00008184inline bool IsAddOrSub(const HInstruction* instruction) {
8185 return instruction->IsAdd() || instruction->IsSub();
8186}
8187
Artem Serov21c7e6f2017-07-27 16:04:42 +01008188void RemoveEnvironmentUses(HInstruction* instruction);
8189bool HasEnvironmentUsedByOthers(HInstruction* instruction);
8190void ResetEnvironmentInputRecords(HInstruction* instruction);
8191
Nicolas Geoffray818f2102014-02-18 16:43:35 +00008192} // namespace art
8193
8194#endif // ART_COMPILER_OPTIMIZING_NODES_H_