blob: 9f085acdb379e32b5286d9b3c9e9e7ac996ddef2 [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
Mythri Alle5097f832021-11-02 14:52:30 +000024#include "art_method.h"
Alex Light86fe9b82020-11-16 16:54:01 +000025#include "base/arena_allocator.h"
Mathieu Chartiere5d80f82015-10-15 17:47:48 -070026#include "base/arena_bit_vector.h"
David Brazdil8d5b8b22015-03-24 10:51:52 +000027#include "base/arena_containers.h"
Mathieu Chartierb666f482015-02-18 14:33:14 -080028#include "base/arena_object.h"
David Brazdild9c90372016-09-14 16:53:55 +010029#include "base/array_ref.h"
Vladimir Marko3fae1292019-06-07 11:26:25 +010030#include "base/intrusive_forward_list.h"
Vladimir Marko2c45bc92016-10-25 16:54:12 +010031#include "base/iteration_range.h"
Vladimír Marko434d9682022-11-04 14:04:17 +000032#include "base/macros.h"
Andreas Gampe7fbc4a52018-11-28 08:26:47 -080033#include "base/mutex.h"
David Sehrc431b9d2018-03-02 12:01:51 -080034#include "base/quasi_atomic.h"
Vladimir Marko60584552015-09-03 13:35:12 +000035#include "base/stl_util.h"
David Brazdild9c90372016-09-14 16:53:55 +010036#include "base/transform_array_ref.h"
Alex Lightdc281e72021-01-06 12:35:31 -080037#include "block_namer.h"
Vladimir Marko02ca05a2020-05-12 13:58:51 +010038#include "class_root.h"
Nicolas Geoffray0d60a2b2020-06-17 14:31:56 +010039#include "compilation_kind.h"
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010040#include "data_type.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070041#include "deoptimization_kind.h"
David Sehr9e734c72018-01-04 17:56:19 -080042#include "dex/dex_file.h"
43#include "dex/dex_file_types.h"
David Sehr8c0961f2018-01-23 16:11:38 -080044#include "dex/invoke_type.h"
David Sehr312f3b22018-03-19 08:39:26 -070045#include "dex/method_reference.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000046#include "entrypoints/quick/quick_entrypoints_enum.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000047#include "handle.h"
48#include "handle_scope.h"
Nicolas Geoffray762869d2016-07-15 15:28:35 +010049#include "intrinsics_enum.h"
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010050#include "locations.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000051#include "mirror/class.h"
Orion Hodson18259d72018-04-12 11:18:23 +010052#include "mirror/method_type.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010053#include "offsets.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000054
Vladimír Marko434d9682022-11-04 14:04:17 +000055namespace art HIDDEN {
Nicolas Geoffray818f2102014-02-18 16:43:35 +000056
Vladimir Markoca6fff82017-10-03 14:49:14 +010057class ArenaStack;
Nicolas Geoffray22df3e02022-01-10 09:31:57 +000058class CodeGenerator;
David Brazdil1abb4192015-02-17 18:33:36 +000059class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000060class HBasicBlock;
Igor Murashkind01745e2017-04-05 16:40:31 -070061class HConstructorFence;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010062class HCurrentMethod;
David Brazdil8d5b8b22015-03-24 10:51:52 +000063class HDoubleConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010064class HEnvironment;
David Brazdil8d5b8b22015-03-24 10:51:52 +000065class HFloatConstant;
David Brazdilfc6a86a2015-06-26 10:33:45 +000066class HGraphBuilder;
David Brazdil8d5b8b22015-03-24 10:51:52 +000067class HGraphVisitor;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000068class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000069class HIntConstant;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000070class HInvoke;
David Brazdil8d5b8b22015-03-24 10:51:52 +000071class HLongConstant;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +000072class HNullConstant;
Igor Murashkind01745e2017-04-05 16:40:31 -070073class HParameterValue;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010074class HPhi;
Nicolas Geoffray3c049742014-09-24 18:10:46 +010075class HSuspendCheck;
David Brazdilffee3d32015-07-06 11:48:53 +010076class HTryBoundary;
Alex Light3a73ffb2021-01-25 14:11:05 +000077class FieldInfo;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010078class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000079class LocationSummary;
Nicolas Geoffray9e598902021-11-19 14:53:07 +000080class ProfilingInfo;
Nicolas Geoffraydb216f42015-05-05 17:02:20 +010081class SlowPathCode;
David Brazdil8d5b8b22015-03-24 10:51:52 +000082class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000083
Mathieu Chartier736b5602015-09-02 14:54:11 -070084namespace mirror {
85class DexCache;
86} // namespace mirror
87
Nicolas Geoffray818f2102014-02-18 16:43:35 +000088static const int kDefaultNumberOfBlocks = 8;
89static const int kDefaultNumberOfSuccessors = 2;
90static const int kDefaultNumberOfPredecessors = 2;
David Brazdilb618ade2015-07-29 10:31:29 +010091static const int kDefaultNumberOfExceptionalPredecessors = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010092static const int kDefaultNumberOfDominatedBlocks = 1;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000093static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000094
Roland Levillain5b5b9312016-03-22 14:57:31 +000095// The maximum (meaningful) distance (31) that can be used in an integer shift/rotate operation.
96static constexpr int32_t kMaxIntShiftDistance = 0x1f;
97// The maximum (meaningful) distance (63) that can be used in a long shift/rotate operation.
98static constexpr int32_t kMaxLongShiftDistance = 0x3f;
Calin Juravle9aec02f2014-11-18 23:06:35 +000099
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +0100100static constexpr uint32_t kUnknownFieldIndex = static_cast<uint32_t>(-1);
Mingyao Yang8df69d42015-10-22 15:40:58 -0700101static constexpr uint16_t kUnknownClassDefIndex = static_cast<uint16_t>(-1);
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +0100102
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100103static constexpr InvokeType kInvalidInvokeType = static_cast<InvokeType>(-1);
104
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600105static constexpr uint32_t kNoDexPc = -1;
106
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100107inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
108 // For the purposes of the compiler, the dex files must actually be the same object
109 // if we want to safely treat them as the same. This is especially important for JIT
110 // as custom class loaders can open the same underlying file (or memory) multiple
111 // times and provide different class resolution but no two class loaders should ever
112 // use the same DexFile object - doing so is an unsupported hack that can lead to
113 // all sorts of weird failures.
114 return &lhs == &rhs;
115}
116
Dave Allison20dfc792014-06-16 20:44:29 -0700117enum IfCondition {
Aart Bike9f37602015-10-09 11:15:55 -0700118 // All types.
119 kCondEQ, // ==
120 kCondNE, // !=
121 // Signed integers and floating-point numbers.
122 kCondLT, // <
123 kCondLE, // <=
124 kCondGT, // >
125 kCondGE, // >=
126 // Unsigned integers.
127 kCondB, // <
128 kCondBE, // <=
129 kCondA, // >
130 kCondAE, // >=
Scott Wakeling2c76e062016-08-31 09:48:54 +0100131 // First and last aliases.
132 kCondFirst = kCondEQ,
133 kCondLast = kCondAE,
Dave Allison20dfc792014-06-16 20:44:29 -0700134};
135
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000136enum GraphAnalysisResult {
David Brazdil86ea7ee2016-02-16 09:26:07 +0000137 kAnalysisSkipped,
David Brazdilbadd8262016-02-02 16:28:56 +0000138 kAnalysisInvalidBytecode,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000139 kAnalysisFailThrowCatchLoop,
140 kAnalysisFailAmbiguousArrayOp,
Nicolas Geoffray0846a8f2018-09-12 15:21:07 +0100141 kAnalysisFailIrreducibleLoopAndStringInit,
Nicolas Geoffray7cfc8f52019-08-07 10:41:09 +0100142 kAnalysisFailPhiEquivalentInOsr,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000143 kAnalysisSuccess,
David Brazdil4833f5a2015-12-16 10:37:39 +0000144};
145
Andreas Gampe9186ced2016-12-12 14:28:21 -0800146template <typename T>
147static inline typename std::make_unsigned<T>::type MakeUnsigned(T x) {
148 return static_cast<typename std::make_unsigned<T>::type>(x);
149}
150
Vladimir Markof9f64412015-09-02 14:05:49 +0100151class HInstructionList : public ValueObject {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100152 public:
153 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
154
155 void AddInstruction(HInstruction* instruction);
156 void RemoveInstruction(HInstruction* instruction);
157
David Brazdilc3d743f2015-04-22 13:40:50 +0100158 // Insert `instruction` before/after an existing instruction `cursor`.
159 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
160 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
161
Roland Levillain6b469232014-09-25 10:10:38 +0100162 // Return true if this list contains `instruction`.
163 bool Contains(HInstruction* instruction) const;
164
Roland Levillainccc07a92014-09-16 14:48:16 +0100165 // Return true if `instruction1` is found before `instruction2` in
166 // this instruction list and false otherwise. Abort if none
167 // of these instructions is found.
168 bool FoundBefore(const HInstruction* instruction1,
169 const HInstruction* instruction2) const;
170
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000171 bool IsEmpty() const { return first_instruction_ == nullptr; }
172 void Clear() { first_instruction_ = last_instruction_ = nullptr; }
173
174 // Update the block of all instructions to be `block`.
175 void SetBlockOfInstructions(HBasicBlock* block) const;
176
177 void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list);
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000178 void AddBefore(HInstruction* cursor, const HInstructionList& instruction_list);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000179 void Add(const HInstructionList& instruction_list);
180
David Brazdil2d7352b2015-04-20 14:52:42 +0100181 // Return the number of instructions in the list. This is an expensive operation.
182 size_t CountSize() const;
183
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100184 private:
185 HInstruction* first_instruction_;
186 HInstruction* last_instruction_;
187
188 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000189 friend class HGraph;
190 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100191 friend class HInstructionIterator;
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +0000192 friend class HInstructionIteratorHandleChanges;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100193 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100194
195 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
196};
197
David Brazdil4833f5a2015-12-16 10:37:39 +0000198class ReferenceTypeInfo : ValueObject {
199 public:
Vladimir Marko4f990712021-07-14 12:45:13 +0100200 using TypeHandle = Handle<mirror::Class>;
David Brazdil4833f5a2015-12-16 10:37:39 +0000201
Vladimir Markoa1de9182016-02-25 11:37:38 +0000202 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact);
203
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700204 static ReferenceTypeInfo Create(TypeHandle type_handle) REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil94ab38f2016-06-21 17:48:19 +0100205 return Create(type_handle, type_handle->CannotBeAssignedFromOtherTypes());
206 }
207
Vladimir Markoa1de9182016-02-25 11:37:38 +0000208 static ReferenceTypeInfo CreateUnchecked(TypeHandle type_handle, bool is_exact) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000209 return ReferenceTypeInfo(type_handle, is_exact);
210 }
211
212 static ReferenceTypeInfo CreateInvalid() { return ReferenceTypeInfo(); }
213
Vladimir Markof39745e2016-01-26 12:16:55 +0000214 static bool IsValidHandle(TypeHandle handle) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000215 return handle.GetReference() != nullptr;
216 }
217
Vladimir Marko456307a2016-04-19 14:12:13 +0000218 bool IsValid() const {
David Brazdil4833f5a2015-12-16 10:37:39 +0000219 return IsValidHandle(type_handle_);
220 }
221
222 bool IsExact() const { return is_exact_; }
223
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700224 bool IsObjectClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000225 DCHECK(IsValid());
226 return GetTypeHandle()->IsObjectClass();
227 }
228
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700229 bool IsStringClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000230 DCHECK(IsValid());
231 return GetTypeHandle()->IsStringClass();
232 }
233
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700234 bool IsObjectArray() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000235 DCHECK(IsValid());
236 return IsArrayClass() && GetTypeHandle()->GetComponentType()->IsObjectClass();
237 }
238
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700239 bool IsInterface() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000240 DCHECK(IsValid());
241 return GetTypeHandle()->IsInterface();
242 }
243
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700244 bool IsArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000245 DCHECK(IsValid());
246 return GetTypeHandle()->IsArrayClass();
247 }
248
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700249 bool IsPrimitiveArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000250 DCHECK(IsValid());
251 return GetTypeHandle()->IsPrimitiveArray();
252 }
253
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700254 bool IsNonPrimitiveArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000255 DCHECK(IsValid());
256 return GetTypeHandle()->IsArrayClass() && !GetTypeHandle()->IsPrimitiveArray();
257 }
258
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700259 bool CanArrayHold(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 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(rti.GetTypeHandle().Get());
264 }
265
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700266 bool CanArrayHoldValuesOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000267 DCHECK(IsValid());
268 if (!IsExact()) return false;
269 if (!IsArrayClass()) return false;
270 if (!rti.IsArrayClass()) return false;
271 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(
272 rti.GetTypeHandle()->GetComponentType());
273 }
274
275 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
276
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700277 bool IsSupertypeOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000278 DCHECK(IsValid());
279 DCHECK(rti.IsValid());
280 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
281 }
282
David Brazdil4833f5a2015-12-16 10:37:39 +0000283 // Returns true if the type information provide the same amount of details.
284 // Note that it does not mean that the instructions have the same actual type
285 // (because the type can be the result of a merge).
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700286 bool IsEqual(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000287 if (!IsValid() && !rti.IsValid()) {
288 // Invalid types are equal.
289 return true;
290 }
291 if (!IsValid() || !rti.IsValid()) {
292 // One is valid, the other not.
293 return false;
294 }
295 return IsExact() == rti.IsExact()
296 && GetTypeHandle().Get() == rti.GetTypeHandle().Get();
297 }
298
299 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +0000300 ReferenceTypeInfo() : type_handle_(TypeHandle()), is_exact_(false) {}
301 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact)
302 : type_handle_(type_handle), is_exact_(is_exact) { }
David Brazdil4833f5a2015-12-16 10:37:39 +0000303
304 // The class of the object.
305 TypeHandle type_handle_;
306 // Whether or not the type is exact or a superclass of the actual type.
307 // Whether or not we have any information about this type.
308 bool is_exact_;
309};
310
311std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
312
Vladimir Marko02ca05a2020-05-12 13:58:51 +0100313class HandleCache {
314 public:
315 explicit HandleCache(VariableSizedHandleScope* handles) : handles_(handles) { }
316
317 VariableSizedHandleScope* GetHandles() { return handles_; }
318
319 template <typename T>
320 MutableHandle<T> NewHandle(T* object) REQUIRES_SHARED(Locks::mutator_lock_) {
321 return handles_->NewHandle(object);
322 }
323
324 template <typename T>
325 MutableHandle<T> NewHandle(ObjPtr<T> object) REQUIRES_SHARED(Locks::mutator_lock_) {
326 return handles_->NewHandle(object);
327 }
328
329 ReferenceTypeInfo::TypeHandle GetObjectClassHandle() {
330 return GetRootHandle(ClassRoot::kJavaLangObject, &object_class_handle_);
331 }
332
333 ReferenceTypeInfo::TypeHandle GetClassClassHandle() {
334 return GetRootHandle(ClassRoot::kJavaLangClass, &class_class_handle_);
335 }
336
337 ReferenceTypeInfo::TypeHandle GetMethodHandleClassHandle() {
338 return GetRootHandle(ClassRoot::kJavaLangInvokeMethodHandleImpl, &method_handle_class_handle_);
339 }
340
341 ReferenceTypeInfo::TypeHandle GetMethodTypeClassHandle() {
342 return GetRootHandle(ClassRoot::kJavaLangInvokeMethodType, &method_type_class_handle_);
343 }
344
345 ReferenceTypeInfo::TypeHandle GetStringClassHandle() {
346 return GetRootHandle(ClassRoot::kJavaLangString, &string_class_handle_);
347 }
348
349 ReferenceTypeInfo::TypeHandle GetThrowableClassHandle() {
350 return GetRootHandle(ClassRoot::kJavaLangThrowable, &throwable_class_handle_);
351 }
352
353
354 private:
355 inline ReferenceTypeInfo::TypeHandle GetRootHandle(ClassRoot class_root,
356 ReferenceTypeInfo::TypeHandle* cache) {
357 if (UNLIKELY(!ReferenceTypeInfo::IsValidHandle(*cache))) {
358 *cache = CreateRootHandle(handles_, class_root);
359 }
360 return *cache;
361 }
362
363 static ReferenceTypeInfo::TypeHandle CreateRootHandle(VariableSizedHandleScope* handles,
364 ClassRoot class_root);
365
366 VariableSizedHandleScope* handles_;
367
368 ReferenceTypeInfo::TypeHandle object_class_handle_;
369 ReferenceTypeInfo::TypeHandle class_class_handle_;
370 ReferenceTypeInfo::TypeHandle method_handle_class_handle_;
371 ReferenceTypeInfo::TypeHandle method_type_class_handle_;
372 ReferenceTypeInfo::TypeHandle string_class_handle_;
373 ReferenceTypeInfo::TypeHandle throwable_class_handle_;
374};
375
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000376// Control-flow graph of a method. Contains a list of basic blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100377class HGraph : public ArenaObject<kArenaAllocGraph> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000378 public:
Vladimir Markoca6fff82017-10-03 14:49:14 +0100379 HGraph(ArenaAllocator* allocator,
380 ArenaStack* arena_stack,
Vladimir Marko02ca05a2020-05-12 13:58:51 +0100381 VariableSizedHandleScope* handles,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100382 const DexFile& dex_file,
383 uint32_t method_idx,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700384 InstructionSet instruction_set,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100385 InvokeType invoke_type = kInvalidInvokeType,
Hans Boehm206348c2018-12-05 11:11:33 -0800386 bool dead_reference_safe = false,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100387 bool debuggable = false,
Nicolas Geoffray0d60a2b2020-06-17 14:31:56 +0100388 CompilationKind compilation_kind = CompilationKind::kOptimized,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100389 int start_instruction_id = 0)
Vladimir Markoca6fff82017-10-03 14:49:14 +0100390 : allocator_(allocator),
391 arena_stack_(arena_stack),
Vladimir Marko02ca05a2020-05-12 13:58:51 +0100392 handle_cache_(handles),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100393 blocks_(allocator->Adapter(kArenaAllocBlockList)),
394 reverse_post_order_(allocator->Adapter(kArenaAllocReversePostOrder)),
395 linear_order_(allocator->Adapter(kArenaAllocLinearOrder)),
Alex Light86fe9b82020-11-16 16:54:01 +0000396 reachability_graph_(allocator, 0, 0, true, kArenaAllocReachabilityGraph),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700397 entry_block_(nullptr),
398 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100399 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100400 number_of_vregs_(0),
401 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000402 temporaries_vreg_slots_(0),
Mark Mendell1152c922015-04-24 17:06:35 -0400403 has_bounds_checks_(false),
David Brazdil77a48ae2015-09-15 12:34:04 +0000404 has_try_catch_(false),
Artem Serov2808be82018-12-20 19:15:11 +0000405 has_monitor_operations_(false),
Artem Serov0b284aa2021-12-14 23:16:21 +0000406 has_traditional_simd_(false),
407 has_predicated_simd_(false),
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800408 has_loops_(false),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000409 has_irreducible_loops_(false),
Vladimir Markod3e9c622020-08-05 12:20:28 +0100410 has_direct_critical_native_call_(false),
Santiago Aboy Solanes78f3d3a2022-07-15 14:30:05 +0100411 has_always_throwing_invokes_(false),
Hans Boehm206348c2018-12-05 11:11:33 -0800412 dead_reference_safe_(dead_reference_safe),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000413 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000414 current_instruction_id_(start_instruction_id),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100415 dex_file_(dex_file),
416 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100417 invoke_type_(invoke_type),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100418 in_ssa_form_(false),
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800419 number_of_cha_guards_(0),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700420 instruction_set_(instruction_set),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000421 cached_null_constant_(nullptr),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100422 cached_int_constants_(std::less<int32_t>(), allocator->Adapter(kArenaAllocConstantsMap)),
423 cached_float_constants_(std::less<int32_t>(), allocator->Adapter(kArenaAllocConstantsMap)),
424 cached_long_constants_(std::less<int64_t>(), allocator->Adapter(kArenaAllocConstantsMap)),
425 cached_double_constants_(std::less<int64_t>(), allocator->Adapter(kArenaAllocConstantsMap)),
David Brazdil4833f5a2015-12-16 10:37:39 +0000426 cached_current_method_(nullptr),
Nicolas Geoffray53fec082017-03-27 12:56:16 +0100427 art_method_(nullptr),
Nicolas Geoffray0d60a2b2020-06-17 14:31:56 +0100428 compilation_kind_(compilation_kind),
Vladimir Marko695348f2020-05-19 14:42:02 +0100429 cha_single_implementation_list_(allocator->Adapter(kArenaAllocCHA)) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100430 blocks_.reserve(kDefaultNumberOfBlocks);
431 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000432
Alex Lightdc281e72021-01-06 12:35:31 -0800433 std::ostream& Dump(std::ostream& os,
Nicolas Geoffray22df3e02022-01-10 09:31:57 +0000434 CodeGenerator* codegen,
Alex Lightdc281e72021-01-06 12:35:31 -0800435 std::optional<std::reference_wrapper<const BlockNamer>> namer = std::nullopt);
436
Vladimir Markoca6fff82017-10-03 14:49:14 +0100437 ArenaAllocator* GetAllocator() const { return allocator_; }
438 ArenaStack* GetArenaStack() const { return arena_stack_; }
Vladimir Marko02ca05a2020-05-12 13:58:51 +0100439
440 HandleCache* GetHandleCache() { return &handle_cache_; }
441
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100442 const ArenaVector<HBasicBlock*>& GetBlocks() const { return blocks_; }
443
Alex Light210a78d2020-11-30 16:58:05 -0800444 // An iterator to only blocks that are still actually in the graph (when
445 // blocks are removed they are replaced with 'nullptr' in GetBlocks to
446 // simplify block-id assignment and avoid memmoves in the block-list).
447 IterationRange<FilterNull<ArenaVector<HBasicBlock*>::const_iterator>> GetActiveBlocks() const {
448 return FilterOutNull(MakeIterationRange(GetBlocks()));
449 }
450
David Brazdil69ba7b72015-06-23 18:27:30 +0100451 bool IsInSsaForm() const { return in_ssa_form_; }
David Brazdilbadd8262016-02-02 16:28:56 +0000452 void SetInSsaForm() { in_ssa_form_ = true; }
David Brazdil69ba7b72015-06-23 18:27:30 +0100453
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000454 HBasicBlock* GetEntryBlock() const { return entry_block_; }
455 HBasicBlock* GetExitBlock() const { return exit_block_; }
David Brazdilc7af85d2015-05-26 12:05:55 +0100456 bool HasExitBlock() const { return exit_block_ != nullptr; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000457
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000458 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
459 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000460
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000461 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100462
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100463 void ComputeDominanceInformation();
464 void ClearDominanceInformation();
Alex Light86fe9b82020-11-16 16:54:01 +0000465 void ComputeReachabilityInformation();
466 void ClearReachabilityInformation();
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000467 void ClearLoopInformation();
468 void FindBackEdges(ArenaBitVector* visited);
469 GraphAnalysisResult BuildDominatorTree();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100470 void SimplifyCFG();
David Brazdilffee3d32015-07-06 11:48:53 +0100471 void SimplifyCatchBlocks();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000472
David Brazdil4833f5a2015-12-16 10:37:39 +0000473 // Analyze all natural loops in this graph. Returns a code specifying that it
474 // was successful or the reason for failure. The method will fail if a loop
David Brazdil4833f5a2015-12-16 10:37:39 +0000475 // is a throw-catch loop, i.e. the header is a catch block.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000476 GraphAnalysisResult AnalyzeLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100477
David Brazdilffee3d32015-07-06 11:48:53 +0100478 // Iterate over blocks to compute try block membership. Needs reverse post
479 // order and loop information.
480 void ComputeTryBlockInformation();
481
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000482 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
Nicolas Geoffray55bd7492016-02-16 15:37:12 +0000483 // Returns the instruction to replace the invoke expression or null if the
484 // invoke is for a void method. Note that the caller is responsible for replacing
485 // and removing the invoke instruction.
Calin Juravle2e768302015-07-28 14:41:11 +0000486 HInstruction* InlineInto(HGraph* outer_graph, HInvoke* invoke);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000487
Nicolas Geoffraya1d8ddf2016-02-29 11:46:58 +0000488 // Update the loop and try membership of `block`, which was spawned from `reference`.
489 // In case `reference` is a back edge, `replace_if_back_edge` notifies whether `block`
490 // should be the new back edge.
Santiago Aboy Solanes8efb1a62022-06-24 11:16:35 +0100491 // `has_more_specific_try_catch_info` will be set to true when inlining a try catch.
Nicolas Geoffraya1d8ddf2016-02-29 11:46:58 +0000492 void UpdateLoopAndTryInformationOfNewBlock(HBasicBlock* block,
493 HBasicBlock* reference,
Santiago Aboy Solanes8efb1a62022-06-24 11:16:35 +0100494 bool replace_if_back_edge,
495 bool has_more_specific_try_catch_info = false);
Nicolas Geoffraya1d8ddf2016-02-29 11:46:58 +0000496
Mingyao Yang3584bce2015-05-19 16:01:59 -0700497 // Need to add a couple of blocks to test if the loop body is entered and
498 // put deoptimization instructions, etc.
499 void TransformLoopHeaderForBCE(HBasicBlock* header);
500
Aart Bikf8f5a162017-02-06 15:35:29 -0800501 // Adds a new loop directly after the loop with the given header and exit.
502 // Returns the new preheader.
503 HBasicBlock* TransformLoopForVectorization(HBasicBlock* header,
504 HBasicBlock* body,
505 HBasicBlock* exit);
506
David Brazdil8a7c0fe2015-11-02 20:24:55 +0000507 // Removes `block` from the graph. Assumes `block` has been disconnected from
508 // other blocks and has no instructions or phis.
509 void DeleteDeadEmptyBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000510
David Brazdilfc6a86a2015-06-26 10:33:45 +0000511 // Splits the edge between `block` and `successor` while preserving the
512 // indices in the predecessor/successor lists. If there are multiple edges
513 // between the blocks, the lowest indices are used.
514 // Returns the new block which is empty and has the same dex pc as `successor`.
515 HBasicBlock* SplitEdge(HBasicBlock* block, HBasicBlock* successor);
516
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100517 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
Santiago Aboy Solanes31242a92022-12-05 14:38:00 +0000518
519 // Splits the edge between `block` and `successor` and then updates the graph's RPO to keep
520 // consistency without recomputing the whole graph.
521 HBasicBlock* SplitEdgeAndUpdateRPO(HBasicBlock* block, HBasicBlock* successor);
522
Artem Serovc73ee372017-07-31 15:08:40 +0100523 void OrderLoopHeaderPredecessors(HBasicBlock* header);
Artem Serov09faaea2017-12-07 14:36:01 +0000524
525 // Transform a loop into a format with a single preheader.
526 //
527 // Each phi in the header should be split: original one in the header should only hold
528 // inputs reachable from the back edges and a single input from the preheader. The newly created
529 // phi in the preheader should collate the inputs from the original multiple incoming blocks.
530 //
531 // Loops in the graph typically have a single preheader, so this method is used to "repair" loops
532 // that no longer have this property.
533 void TransformLoopToSinglePreheaderFormat(HBasicBlock* header);
534
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100535 void SimplifyLoop(HBasicBlock* header);
536
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000537 int32_t GetNextInstructionId() {
Nicolas Geoffrayc9c31042017-06-29 14:04:16 +0100538 CHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000539 return current_instruction_id_++;
540 }
541
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000542 int32_t GetCurrentInstructionId() const {
543 return current_instruction_id_;
544 }
545
546 void SetCurrentInstructionId(int32_t id) {
Nicolas Geoffrayc9c31042017-06-29 14:04:16 +0100547 CHECK_GE(id, current_instruction_id_);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000548 current_instruction_id_ = id;
549 }
550
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100551 uint16_t GetMaximumNumberOfOutVRegs() const {
552 return maximum_number_of_out_vregs_;
553 }
554
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000555 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
556 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100557 }
558
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100559 void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) {
560 maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value);
561 }
562
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000563 void UpdateTemporariesVRegSlots(size_t slots) {
564 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100565 }
566
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000567 size_t GetTemporariesVRegSlots() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100568 DCHECK(!in_ssa_form_);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000569 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100570 }
571
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100572 void SetNumberOfVRegs(uint16_t number_of_vregs) {
573 number_of_vregs_ = number_of_vregs;
574 }
575
576 uint16_t GetNumberOfVRegs() const {
577 return number_of_vregs_;
578 }
579
580 void SetNumberOfInVRegs(uint16_t value) {
581 number_of_in_vregs_ = value;
582 }
583
David Brazdildee58d62016-04-07 09:54:26 +0000584 uint16_t GetNumberOfInVRegs() const {
585 return number_of_in_vregs_;
586 }
587
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100588 uint16_t GetNumberOfLocalVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100589 DCHECK(!in_ssa_form_);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100590 return number_of_vregs_ - number_of_in_vregs_;
591 }
592
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100593 const ArenaVector<HBasicBlock*>& GetReversePostOrder() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100594 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100595 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100596
Eric Holk1868de92020-02-12 09:10:21 -0800597 ArrayRef<HBasicBlock* const> GetReversePostOrderSkipEntryBlock() const {
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100598 DCHECK(GetReversePostOrder()[0] == entry_block_);
599 return ArrayRef<HBasicBlock* const>(GetReversePostOrder()).SubArray(1);
600 }
601
602 IterationRange<ArenaVector<HBasicBlock*>::const_reverse_iterator> GetPostOrder() const {
603 return ReverseRange(GetReversePostOrder());
604 }
605
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100606 const ArenaVector<HBasicBlock*>& GetLinearOrder() const {
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100607 return linear_order_;
608 }
609
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100610 IterationRange<ArenaVector<HBasicBlock*>::const_reverse_iterator> GetLinearPostOrder() const {
611 return ReverseRange(GetLinearOrder());
612 }
613
Mark Mendell1152c922015-04-24 17:06:35 -0400614 bool HasBoundsChecks() const {
615 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800616 }
617
Mark Mendell1152c922015-04-24 17:06:35 -0400618 void SetHasBoundsChecks(bool value) {
619 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800620 }
621
Alex Light86fe9b82020-11-16 16:54:01 +0000622 // Returns true if dest is reachable from source, using either blocks or block-ids.
623 bool PathBetween(const HBasicBlock* source, const HBasicBlock* dest) const;
624 bool PathBetween(uint32_t source_id, uint32_t dest_id) const;
625
Hans Boehm206348c2018-12-05 11:11:33 -0800626 // Is the code known to be robust against eliminating dead references
627 // and the effects of early finalization?
628 bool IsDeadReferenceSafe() const { return dead_reference_safe_; }
629
630 void MarkDeadReferenceUnsafe() { dead_reference_safe_ = false; }
631
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000632 bool IsDebuggable() const { return debuggable_; }
633
David Brazdil8d5b8b22015-03-24 10:51:52 +0000634 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000635 // already, it is created and inserted into the graph. This method is only for
636 // integral types.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100637 HConstant* GetConstant(DataType::Type type, int64_t value, uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000638
639 // TODO: This is problematic for the consistency of reference type propagation
640 // because it can be created anytime after the pass and thus it will be left
641 // with an invalid type.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600642 HNullConstant* GetNullConstant(uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000643
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600644 HIntConstant* GetIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc) {
645 return CreateConstant(value, &cached_int_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000646 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600647 HLongConstant* GetLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc) {
648 return CreateConstant(value, &cached_long_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000649 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600650 HFloatConstant* GetFloatConstant(float value, uint32_t dex_pc = kNoDexPc) {
651 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000652 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600653 HDoubleConstant* GetDoubleConstant(double value, uint32_t dex_pc = kNoDexPc) {
654 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000655 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000656
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100657 HCurrentMethod* GetCurrentMethod();
658
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100659 const DexFile& GetDexFile() const {
660 return dex_file_;
661 }
662
663 uint32_t GetMethodIdx() const {
664 return method_idx_;
665 }
666
Igor Murashkind01745e2017-04-05 16:40:31 -0700667 // Get the method name (without the signature), e.g. "<init>"
668 const char* GetMethodName() const;
669
670 // Get the pretty method name (class + name + optionally signature).
671 std::string PrettyMethod(bool with_signature = true) const;
672
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100673 InvokeType GetInvokeType() const {
674 return invoke_type_;
675 }
676
Mark Mendellc4701932015-04-10 13:18:51 -0400677 InstructionSet GetInstructionSet() const {
678 return instruction_set_;
679 }
680
Nicolas Geoffray0d60a2b2020-06-17 14:31:56 +0100681 bool IsCompilingOsr() const { return compilation_kind_ == CompilationKind::kOsr; }
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000682
Nicolas Geoffray0d60a2b2020-06-17 14:31:56 +0100683 bool IsCompilingBaseline() const { return compilation_kind_ == CompilationKind::kBaseline; }
684
685 CompilationKind GetCompilationKind() const { return compilation_kind_; }
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +0000686
Mingyao Yang063fc772016-08-02 11:02:54 -0700687 ArenaSet<ArtMethod*>& GetCHASingleImplementationList() {
688 return cha_single_implementation_list_;
689 }
690
Stelios Ioannouc54cc7c2021-07-09 17:06:03 +0100691 // In case of OSR we intend to use SuspendChecks as an entry point to the
692 // function; for debuggable graphs we might deoptimize to interpreter from
693 // SuspendChecks. In these cases we should always generate code for them.
694 bool SuspendChecksAreAllowedToNoOp() const {
695 return !IsDebuggable() && !IsCompilingOsr();
696 }
697
Mingyao Yang063fc772016-08-02 11:02:54 -0700698 void AddCHASingleImplementationDependency(ArtMethod* method) {
699 cha_single_implementation_list_.insert(method);
700 }
701
702 bool HasShouldDeoptimizeFlag() const {
Mythri Alle5097f832021-11-02 14:52:30 +0000703 return number_of_cha_guards_ != 0 || debuggable_;
Mingyao Yang063fc772016-08-02 11:02:54 -0700704 }
705
David Brazdil77a48ae2015-09-15 12:34:04 +0000706 bool HasTryCatch() const { return has_try_catch_; }
707 void SetHasTryCatch(bool value) { has_try_catch_ = value; }
David Brazdilbbd733e2015-08-18 17:48:17 +0100708
Artem Serov2808be82018-12-20 19:15:11 +0000709 bool HasMonitorOperations() const { return has_monitor_operations_; }
710 void SetHasMonitorOperations(bool value) { has_monitor_operations_ = value; }
711
Artem Serov0b284aa2021-12-14 23:16:21 +0000712 bool HasTraditionalSIMD() { return has_traditional_simd_; }
713 void SetHasTraditionalSIMD(bool value) { has_traditional_simd_ = value; }
714
715 bool HasPredicatedSIMD() { return has_predicated_simd_; }
716 void SetHasPredicatedSIMD(bool value) { has_predicated_simd_ = value; }
717
718 bool HasSIMD() const { return has_traditional_simd_ || has_predicated_simd_; }
Aart Bikb13c65b2017-03-21 20:14:07 -0700719
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800720 bool HasLoops() const { return has_loops_; }
721 void SetHasLoops(bool value) { has_loops_ = value; }
722
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000723 bool HasIrreducibleLoops() const { return has_irreducible_loops_; }
724 void SetHasIrreducibleLoops(bool value) { has_irreducible_loops_ = value; }
725
Vladimir Markod3e9c622020-08-05 12:20:28 +0100726 bool HasDirectCriticalNativeCall() const { return has_direct_critical_native_call_; }
727 void SetHasDirectCriticalNativeCall(bool value) { has_direct_critical_native_call_ = value; }
728
Santiago Aboy Solanes78f3d3a2022-07-15 14:30:05 +0100729 bool HasAlwaysThrowingInvokes() const { return has_always_throwing_invokes_; }
730 void SetHasAlwaysThrowingInvokes(bool value) { has_always_throwing_invokes_ = value; }
731
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100732 ArtMethod* GetArtMethod() const { return art_method_; }
733 void SetArtMethod(ArtMethod* method) { art_method_ = method; }
734
Nicolas Geoffray9e598902021-11-19 14:53:07 +0000735 void SetProfilingInfo(ProfilingInfo* info) { profiling_info_ = info; }
736 ProfilingInfo* GetProfilingInfo() const { return profiling_info_; }
737
Roland Levillain5e8d5f02016-10-18 18:03:43 +0100738 // Returns an instruction with the opposite Boolean value from 'cond'.
Mark Mendellf6529172015-11-17 11:16:56 -0500739 // The instruction has been inserted into the graph, either as a constant, or
740 // before cursor.
741 HInstruction* InsertOppositeCondition(HInstruction* cond, HInstruction* cursor);
742
Vladimir Marko02ca05a2020-05-12 13:58:51 +0100743 ReferenceTypeInfo GetInexactObjectRti() {
744 return ReferenceTypeInfo::Create(handle_cache_.GetObjectClassHandle(), /* is_exact= */ false);
745 }
Nicolas Geoffray18401b72016-03-11 13:35:51 +0000746
Stelios Ioannouc54cc7c2021-07-09 17:06:03 +0100747 uint32_t GetNumberOfCHAGuards() const { return number_of_cha_guards_; }
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800748 void SetNumberOfCHAGuards(uint32_t num) { number_of_cha_guards_ = num; }
749 void IncrementNumberOfCHAGuards() { number_of_cha_guards_++; }
750
David Brazdil2d7352b2015-04-20 14:52:42 +0100751 private:
Santiago Aboy Solanes65fd6a32022-08-22 13:12:46 +0100752 void RemoveDeadBlocksInstructionsAsUsersAndDisconnect(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100753 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000754
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000755 template <class InstructionType, typename ValueType>
756 InstructionType* CreateConstant(ValueType value,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600757 ArenaSafeMap<ValueType, InstructionType*>* cache,
758 uint32_t dex_pc = kNoDexPc) {
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000759 // Try to find an existing constant of the given value.
760 InstructionType* constant = nullptr;
761 auto cached_constant = cache->find(value);
762 if (cached_constant != cache->end()) {
763 constant = cached_constant->second;
764 }
765
766 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100767 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000768 if (constant == nullptr || constant->GetBlock() == nullptr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100769 constant = new (allocator_) InstructionType(value, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000770 cache->Overwrite(value, constant);
771 InsertConstant(constant);
772 }
773 return constant;
774 }
775
David Brazdil8d5b8b22015-03-24 10:51:52 +0000776 void InsertConstant(HConstant* instruction);
777
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000778 // Cache a float constant into the graph. This method should only be
779 // called by the SsaBuilder when creating "equivalent" instructions.
780 void CacheFloatConstant(HFloatConstant* constant);
781
782 // See CacheFloatConstant comment.
783 void CacheDoubleConstant(HDoubleConstant* constant);
784
Vladimir Markoca6fff82017-10-03 14:49:14 +0100785 ArenaAllocator* const allocator_;
786 ArenaStack* const arena_stack_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000787
Vladimir Marko02ca05a2020-05-12 13:58:51 +0100788 HandleCache handle_cache_;
789
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000790 // List of blocks in insertion order.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100791 ArenaVector<HBasicBlock*> blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000792
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100793 // List of blocks to perform a reverse post order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100794 ArenaVector<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000795
Aart Bik281c6812016-08-26 11:31:48 -0700796 // List of blocks to perform a linear order tree traversal. Unlike the reverse
797 // post order, this order is not incrementally kept up-to-date.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100798 ArenaVector<HBasicBlock*> linear_order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100799
Alex Light86fe9b82020-11-16 16:54:01 +0000800 // Reachability graph for checking connectedness between nodes. Acts as a partitioned vector where
801 // each RoundUp(blocks_.size(), BitVector::kWordBits) is the reachability of each node.
802 ArenaBitVectorArray reachability_graph_;
803
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000804 HBasicBlock* entry_block_;
805 HBasicBlock* exit_block_;
806
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100807 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100808 uint16_t maximum_number_of_out_vregs_;
809
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100810 // The number of virtual registers in this method. Contains the parameters.
811 uint16_t number_of_vregs_;
812
813 // The number of virtual registers used by parameters of this method.
814 uint16_t number_of_in_vregs_;
815
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000816 // Number of vreg size slots that the temporaries use (used in baseline compiler).
817 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100818
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800819 // Flag whether there are bounds checks in the graph. We can skip
Santiago Aboy Solanes74da6682022-12-16 19:28:47 +0000820 // BCE if it's false.
Mark Mendell1152c922015-04-24 17:06:35 -0400821 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800822
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800823 // Flag whether there are try/catch blocks in the graph. We will skip
Santiago Aboy Solanes74da6682022-12-16 19:28:47 +0000824 // try/catch-related passes if it's false.
David Brazdil77a48ae2015-09-15 12:34:04 +0000825 bool has_try_catch_;
826
Artem Serov2808be82018-12-20 19:15:11 +0000827 // Flag whether there are any HMonitorOperation in the graph. If yes this will mandate
828 // DexRegisterMap to be present to allow deadlock analysis for non-debuggable code.
829 bool has_monitor_operations_;
830
Artem Serov0b284aa2021-12-14 23:16:21 +0000831 // Flags whether SIMD (traditional or predicated) instructions appear in the graph.
832 // If either is true, the code generators may have to be more careful spilling the wider
Aart Bikb13c65b2017-03-21 20:14:07 -0700833 // contents of SIMD registers.
Artem Serov0b284aa2021-12-14 23:16:21 +0000834 bool has_traditional_simd_;
835 bool has_predicated_simd_;
Aart Bikb13c65b2017-03-21 20:14:07 -0700836
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800837 // Flag whether there are any loops in the graph. We can skip loop
Santiago Aboy Solanes74da6682022-12-16 19:28:47 +0000838 // optimization if it's false.
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800839 bool has_loops_;
840
Santiago Aboy Solanes74da6682022-12-16 19:28:47 +0000841 // Flag whether there are any irreducible loops in the graph.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000842 bool has_irreducible_loops_;
843
Vladimir Markod3e9c622020-08-05 12:20:28 +0100844 // Flag whether there are any direct calls to native code registered
845 // for @CriticalNative methods.
846 bool has_direct_critical_native_call_;
847
Santiago Aboy Solanes78f3d3a2022-07-15 14:30:05 +0100848 // Flag whether the graph contains invokes that always throw.
849 bool has_always_throwing_invokes_;
850
Hans Boehm206348c2018-12-05 11:11:33 -0800851 // Is the code known to be robust against eliminating dead references
852 // and the effects of early finalization? If false, dead reference variables
853 // are kept if they might be visible to the garbage collector.
854 // Currently this means that the class was declared to be dead-reference-safe,
855 // the method accesses no reachability-sensitive fields or data, and the same
856 // is true for any methods that were inlined into the current one.
857 bool dead_reference_safe_;
858
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000859 // Indicates whether the graph should be compiled in a way that
860 // ensures full debuggability. If false, we can apply more
861 // aggressive optimizations that may limit the level of debugging.
862 const bool debuggable_;
863
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000864 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000865 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000866
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100867 // The dex file from which the method is from.
868 const DexFile& dex_file_;
869
870 // The method index in the dex file.
871 const uint32_t method_idx_;
872
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100873 // If inlined, this encodes how the callee is being invoked.
874 const InvokeType invoke_type_;
875
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100876 // Whether the graph has been transformed to SSA form. Only used
877 // in debug mode to ensure we are not using properties only valid
878 // for non-SSA form (like the number of temporaries).
879 bool in_ssa_form_;
880
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800881 // Number of CHA guards in the graph. Used to short-circuit the
882 // CHA guard optimization pass when there is no CHA guard left.
883 uint32_t number_of_cha_guards_;
884
Mathieu Chartiere401d142015-04-22 13:56:20 -0700885 const InstructionSet instruction_set_;
886
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000887 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000888 HNullConstant* cached_null_constant_;
889 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000890 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000891 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000892 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000893
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100894 HCurrentMethod* cached_current_method_;
895
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100896 // The ArtMethod this graph is for. Note that for AOT, it may be null,
897 // for example for methods whose declaring class could not be resolved
898 // (such as when the superclass could not be found).
899 ArtMethod* art_method_;
900
Nicolas Geoffray9e598902021-11-19 14:53:07 +0000901 // The `ProfilingInfo` associated with the method being compiled.
902 ProfilingInfo* profiling_info_;
903
Nicolas Geoffray0d60a2b2020-06-17 14:31:56 +0100904 // How we are compiling the graph: either optimized, osr, or baseline.
905 // For osr, we will make all loops seen as irreducible and emit special
906 // stack maps to mark compiled code entries which the interpreter can
907 // directly jump to.
908 const CompilationKind compilation_kind_;
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +0000909
Mingyao Yang063fc772016-08-02 11:02:54 -0700910 // List of methods that are assumed to have single implementation.
911 ArenaSet<ArtMethod*> cha_single_implementation_list_;
912
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000913 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100914 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000915 friend class HInliner; // For the reverse post order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000916 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000917 DISALLOW_COPY_AND_ASSIGN(HGraph);
918};
919
Vladimir Markof9f64412015-09-02 14:05:49 +0100920class HLoopInformation : public ArenaObject<kArenaAllocLoopInfo> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000921 public:
922 HLoopInformation(HBasicBlock* header, HGraph* graph)
923 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100924 suspend_check_(nullptr),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000925 irreducible_(false),
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100926 contains_irreducible_loop_(false),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100927 back_edges_(graph->GetAllocator()->Adapter(kArenaAllocLoopInfoBackEdges)),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100928 // Make bit vector growable, as the number of blocks may change.
Vladimir Markoca6fff82017-10-03 14:49:14 +0100929 blocks_(graph->GetAllocator(),
930 graph->GetBlocks().size(),
931 true,
932 kArenaAllocLoopInfoBackEdges) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100933 back_edges_.reserve(kDefaultNumberOfBackEdges);
934 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100935
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000936 bool IsIrreducible() const { return irreducible_; }
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100937 bool ContainsIrreducibleLoop() const { return contains_irreducible_loop_; }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000938
939 void Dump(std::ostream& os);
940
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100941 HBasicBlock* GetHeader() const {
942 return header_;
943 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000944
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000945 void SetHeader(HBasicBlock* block) {
946 header_ = block;
947 }
948
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100949 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
950 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
951 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
952
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000953 void AddBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100954 back_edges_.push_back(back_edge);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000955 }
956
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100957 void RemoveBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100958 RemoveElement(back_edges_, back_edge);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100959 }
960
David Brazdil46e2a392015-03-16 17:31:52 +0000961 bool IsBackEdge(const HBasicBlock& block) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100962 return ContainsElement(back_edges_, &block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100963 }
964
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000965 size_t NumberOfBackEdges() const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100966 return back_edges_.size();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000967 }
968
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100969 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100970
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100971 const ArenaVector<HBasicBlock*>& GetBackEdges() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100972 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100973 }
974
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100975 // Returns the lifetime position of the back edge that has the
976 // greatest lifetime position.
977 size_t GetLifetimeEnd() const;
978
979 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100980 ReplaceElement(back_edges_, existing, new_back_edge);
Nicolas Geoffray57902602015-04-21 14:28:41 +0100981 }
982
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000983 // Finds blocks that are part of this loop.
984 void Populate();
David Brazdila4b8c212015-05-07 09:59:30 +0100985
Artem Serov7f4aff62017-06-21 17:02:18 +0100986 // Updates blocks population of the loop and all of its outer' ones recursively after the
987 // population of the inner loop is updated.
988 void PopulateInnerLoopUpwards(HLoopInformation* inner_loop);
989
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100990 // Returns whether this loop information contains `block`.
991 // Note that this loop information *must* be populated before entering this function.
992 bool Contains(const HBasicBlock& block) const;
993
994 // Returns whether this loop information is an inner loop of `other`.
995 // Note that `other` *must* be populated before entering this function.
996 bool IsIn(const HLoopInformation& other) const;
997
Mingyao Yang4b467ed2015-11-19 17:04:22 -0800998 // Returns true if instruction is not defined within this loop.
999 bool IsDefinedOutOfTheLoop(HInstruction* instruction) const;
Aart Bik73f1f3b2015-10-28 15:28:08 -07001000
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001001 const ArenaBitVector& GetBlocks() const { return blocks_; }
1002
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001003 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +00001004 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001005
Nicolas Geoffray788f2f02016-01-22 12:41:38 +00001006 void ClearAllBlocks() {
1007 blocks_.ClearAllBits();
1008 }
1009
David Brazdil3f4a5222016-05-06 12:46:21 +01001010 bool HasBackEdgeNotDominatedByHeader() const;
1011
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +01001012 bool IsPopulated() const {
1013 return blocks_.GetHighestBitSet() != -1;
1014 }
1015
Anton Shaminf89381f2016-05-16 16:44:13 +06001016 bool DominatesAllBackEdges(HBasicBlock* block);
1017
David Sehrc757dec2016-11-04 15:48:34 -07001018 bool HasExitEdge() const;
1019
Artem Serov7f4aff62017-06-21 17:02:18 +01001020 // Resets back edge and blocks-in-loop data.
1021 void ResetBasicBlockData() {
1022 back_edges_.clear();
1023 ClearAllBlocks();
1024 }
1025
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001026 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001027 // Internal recursive implementation of `Populate`.
1028 void PopulateRecursive(HBasicBlock* block);
David Brazdilc2e8af92016-04-05 17:15:19 +01001029 void PopulateIrreducibleRecursive(HBasicBlock* block, ArenaBitVector* finalized);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001030
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001031 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001032 HSuspendCheck* suspend_check_;
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001033 bool irreducible_;
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +01001034 bool contains_irreducible_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001035 ArenaVector<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001036 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001037
1038 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
1039};
1040
David Brazdilec16f792015-08-19 15:04:01 +01001041// Stores try/catch information for basic blocks.
1042// Note that HGraph is constructed so that catch blocks cannot simultaneously
1043// be try blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +01001044class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> {
David Brazdilec16f792015-08-19 15:04:01 +01001045 public:
1046 // Try block information constructor.
1047 explicit TryCatchInformation(const HTryBoundary& try_entry)
1048 : try_entry_(&try_entry),
1049 catch_dex_file_(nullptr),
Nicolas Geoffrayebc3b252019-01-09 10:47:02 +00001050 catch_type_index_(dex::TypeIndex::Invalid()) {
David Brazdilec16f792015-08-19 15:04:01 +01001051 DCHECK(try_entry_ != nullptr);
1052 }
1053
1054 // Catch block information constructor.
Andreas Gampea5b09a62016-11-17 15:21:22 -08001055 TryCatchInformation(dex::TypeIndex catch_type_index, const DexFile& dex_file)
David Brazdilec16f792015-08-19 15:04:01 +01001056 : try_entry_(nullptr),
1057 catch_dex_file_(&dex_file),
1058 catch_type_index_(catch_type_index) {}
1059
1060 bool IsTryBlock() const { return try_entry_ != nullptr; }
1061
1062 const HTryBoundary& GetTryEntry() const {
1063 DCHECK(IsTryBlock());
1064 return *try_entry_;
1065 }
1066
1067 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
1068
Nicolas Geoffrayebc3b252019-01-09 10:47:02 +00001069 bool IsValidTypeIndex() const {
David Brazdilec16f792015-08-19 15:04:01 +01001070 DCHECK(IsCatchBlock());
Nicolas Geoffrayebc3b252019-01-09 10:47:02 +00001071 return catch_type_index_.IsValid();
David Brazdilec16f792015-08-19 15:04:01 +01001072 }
1073
Andreas Gampea5b09a62016-11-17 15:21:22 -08001074 dex::TypeIndex GetCatchTypeIndex() const {
David Brazdilec16f792015-08-19 15:04:01 +01001075 DCHECK(IsCatchBlock());
1076 return catch_type_index_;
1077 }
1078
1079 const DexFile& GetCatchDexFile() const {
1080 DCHECK(IsCatchBlock());
1081 return *catch_dex_file_;
1082 }
1083
Nicolas Geoffrayebc3b252019-01-09 10:47:02 +00001084 void SetInvalidTypeIndex() {
1085 catch_type_index_ = dex::TypeIndex::Invalid();
1086 }
1087
David Brazdilec16f792015-08-19 15:04:01 +01001088 private:
1089 // One of possibly several TryBoundary instructions entering the block's try.
1090 // Only set for try blocks.
1091 const HTryBoundary* try_entry_;
1092
1093 // Exception type information. Only set for catch blocks.
1094 const DexFile* catch_dex_file_;
Nicolas Geoffrayebc3b252019-01-09 10:47:02 +00001095 dex::TypeIndex catch_type_index_;
David Brazdilec16f792015-08-19 15:04:01 +01001096};
1097
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001098static constexpr size_t kNoLifetime = -1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001099static constexpr uint32_t kInvalidBlockId = static_cast<uint32_t>(-1);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001100
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001101// A block in a method. Contains the list of instructions represented
1102// as a double linked list. Each block knows its predecessors and
1103// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001104
Vladimir Markof9f64412015-09-02 14:05:49 +01001105class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001106 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -07001107 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001108 : graph_(graph),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001109 predecessors_(graph->GetAllocator()->Adapter(kArenaAllocPredecessors)),
1110 successors_(graph->GetAllocator()->Adapter(kArenaAllocSuccessors)),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001111 loop_information_(nullptr),
1112 dominator_(nullptr),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001113 dominated_blocks_(graph->GetAllocator()->Adapter(kArenaAllocDominated)),
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001114 block_id_(kInvalidBlockId),
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001115 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001116 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001117 lifetime_end_(kNoLifetime),
Vladimir Marko60584552015-09-03 13:35:12 +00001118 try_catch_information_(nullptr) {
1119 predecessors_.reserve(kDefaultNumberOfPredecessors);
1120 successors_.reserve(kDefaultNumberOfSuccessors);
1121 dominated_blocks_.reserve(kDefaultNumberOfDominatedBlocks);
1122 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001123
Vladimir Marko60584552015-09-03 13:35:12 +00001124 const ArenaVector<HBasicBlock*>& GetPredecessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001125 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001126 }
1127
Alex Light3a73ffb2021-01-25 14:11:05 +00001128 size_t GetNumberOfPredecessors() const {
1129 return GetPredecessors().size();
1130 }
1131
Vladimir Marko60584552015-09-03 13:35:12 +00001132 const ArenaVector<HBasicBlock*>& GetSuccessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001133 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001134 }
1135
David Brazdild26a4112015-11-10 11:07:31 +00001136 ArrayRef<HBasicBlock* const> GetNormalSuccessors() const;
1137 ArrayRef<HBasicBlock* const> GetExceptionalSuccessors() const;
1138
Vladimir Marko60584552015-09-03 13:35:12 +00001139 bool HasSuccessor(const HBasicBlock* block, size_t start_from = 0u) {
1140 return ContainsElement(successors_, block, start_from);
1141 }
1142
1143 const ArenaVector<HBasicBlock*>& GetDominatedBlocks() const {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001144 return dominated_blocks_;
1145 }
1146
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001147 bool IsEntryBlock() const {
1148 return graph_->GetEntryBlock() == this;
1149 }
1150
1151 bool IsExitBlock() const {
1152 return graph_->GetExitBlock() == this;
1153 }
1154
David Brazdil46e2a392015-03-16 17:31:52 +00001155 bool IsSingleGoto() const;
Mads Ager16e52892017-07-14 13:11:37 +02001156 bool IsSingleReturn() const;
Mingyao Yang46721ef2017-10-05 14:45:17 -07001157 bool IsSingleReturnOrReturnVoidAllowingPhis() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +00001158 bool IsSingleTryBoundary() const;
1159
1160 // Returns true if this block emits nothing but a jump.
1161 bool IsSingleJump() const {
1162 HLoopInformation* loop_info = GetLoopInformation();
1163 return (IsSingleGoto() || IsSingleTryBoundary())
1164 // Back edges generate a suspend check.
1165 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
1166 }
David Brazdil46e2a392015-03-16 17:31:52 +00001167
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001168 void AddBackEdge(HBasicBlock* back_edge) {
1169 if (loop_information_ == nullptr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001170 loop_information_ = new (graph_->GetAllocator()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001171 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001172 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001173 loop_information_->AddBackEdge(back_edge);
1174 }
1175
Artem Serov7f4aff62017-06-21 17:02:18 +01001176 // Registers a back edge; if the block was not a loop header before the call associates a newly
1177 // created loop info with it.
1178 //
1179 // Used in SuperblockCloner to preserve LoopInformation object instead of reseting loop
1180 // info for all blocks during back edges recalculation.
1181 void AddBackEdgeWhileUpdating(HBasicBlock* back_edge) {
1182 if (loop_information_ == nullptr || loop_information_->GetHeader() != this) {
1183 loop_information_ = new (graph_->GetAllocator()) HLoopInformation(this, graph_);
1184 }
1185 loop_information_->AddBackEdge(back_edge);
1186 }
1187
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001188 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001189 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001190
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001191 uint32_t GetBlockId() const { return block_id_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001192 void SetBlockId(int id) { block_id_ = id; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001193 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001194
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001195 HBasicBlock* GetDominator() const { return dominator_; }
1196 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Vladimir Marko60584552015-09-03 13:35:12 +00001197 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.push_back(block); }
1198
1199 void RemoveDominatedBlock(HBasicBlock* block) {
1200 RemoveElement(dominated_blocks_, block);
Vladimir Marko91e11c02015-09-02 17:03:22 +01001201 }
Vladimir Marko60584552015-09-03 13:35:12 +00001202
1203 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
1204 ReplaceElement(dominated_blocks_, existing, new_block);
1205 }
1206
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +01001207 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001208
1209 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +01001210 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001211 }
1212
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001213 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
1214 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +01001215 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001216 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +01001217 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
1218 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001219
Nicolas Geoffray09aa1472016-01-19 10:52:54 +00001220 HInstruction* GetFirstInstructionDisregardMoves() const;
1221
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001222 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001223 successors_.push_back(block);
1224 block->predecessors_.push_back(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001225 }
1226
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001227 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
1228 size_t successor_index = GetSuccessorIndexOf(existing);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001229 existing->RemovePredecessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +00001230 new_block->predecessors_.push_back(this);
1231 successors_[successor_index] = new_block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001232 }
1233
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001234 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
1235 size_t predecessor_index = GetPredecessorIndexOf(existing);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001236 existing->RemoveSuccessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +00001237 new_block->successors_.push_back(this);
1238 predecessors_[predecessor_index] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001239 }
1240
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001241 // Insert `this` between `predecessor` and `successor. This method
Alex Light1e52a072019-06-25 09:12:04 -07001242 // preserves the indices, and will update the first edge found between
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001243 // `predecessor` and `successor`.
1244 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
1245 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001246 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko60584552015-09-03 13:35:12 +00001247 successor->predecessors_[predecessor_index] = this;
1248 predecessor->successors_[successor_index] = this;
1249 successors_.push_back(successor);
1250 predecessors_.push_back(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001251 }
1252
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001253 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001254 predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001255 }
1256
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001257 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001258 successors_.erase(successors_.begin() + GetSuccessorIndexOf(block));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001259 }
1260
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001261 void ClearAllPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001262 predecessors_.clear();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001263 }
1264
1265 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001266 predecessors_.push_back(block);
1267 block->successors_.push_back(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001268 }
1269
Nicolas Geoffray604c6e42014-09-17 12:08:44 +01001270 void SwapPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001271 DCHECK_EQ(predecessors_.size(), 2u);
1272 std::swap(predecessors_[0], predecessors_[1]);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +01001273 }
1274
David Brazdil769c9e52015-04-27 13:54:09 +01001275 void SwapSuccessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001276 DCHECK_EQ(successors_.size(), 2u);
1277 std::swap(successors_[0], successors_[1]);
David Brazdil769c9e52015-04-27 13:54:09 +01001278 }
1279
David Brazdilfc6a86a2015-06-26 10:33:45 +00001280 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko60584552015-09-03 13:35:12 +00001281 return IndexOfElement(predecessors_, predecessor);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001282 }
1283
David Brazdilfc6a86a2015-06-26 10:33:45 +00001284 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko60584552015-09-03 13:35:12 +00001285 return IndexOfElement(successors_, successor);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001286 }
1287
David Brazdilfc6a86a2015-06-26 10:33:45 +00001288 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00001289 DCHECK_EQ(GetPredecessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +01001290 return GetPredecessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +00001291 }
1292
1293 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00001294 DCHECK_EQ(GetSuccessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +01001295 return GetSuccessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +00001296 }
1297
1298 // Returns whether the first occurrence of `predecessor` in the list of
1299 // predecessors is at index `idx`.
1300 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01001301 DCHECK_EQ(GetPredecessors()[idx], predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +00001302 return GetPredecessorIndexOf(predecessor) == idx;
1303 }
1304
David Brazdild7558da2015-09-22 13:04:14 +01001305 // Create a new block between this block and its predecessors. The new block
1306 // is added to the graph, all predecessor edges are relinked to it and an edge
1307 // is created to `this`. Returns the new empty block. Reverse post order or
1308 // loop and try/catch information are not updated.
1309 HBasicBlock* CreateImmediateDominator();
1310
David Brazdilfc6a86a2015-06-26 10:33:45 +00001311 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +01001312 // created, latter block. Note that this method will add the block to the
1313 // graph, create a Goto at the end of the former block and will create an edge
1314 // between the blocks. It will not, however, update the reverse post order or
David Brazdild7558da2015-09-22 13:04:14 +01001315 // loop and try/catch information.
Santiago Aboy Solanes78f3d3a2022-07-15 14:30:05 +01001316 HBasicBlock* SplitBefore(HInstruction* cursor, bool require_graph_not_in_ssa_form = true);
David Brazdilfc6a86a2015-06-26 10:33:45 +00001317
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001318 // Split the block into two blocks just before `cursor`. Returns the newly
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001319 // created block. Note that this method just updates raw block information,
1320 // like predecessors, successors, dominators, and instruction list. It does not
1321 // update the graph, reverse post order, loop information, nor make sure the
1322 // blocks are consistent (for example ending with a control flow instruction).
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001323 HBasicBlock* SplitBeforeForInlining(HInstruction* cursor);
1324
1325 // Similar to `SplitBeforeForInlining` but does it after `cursor`.
1326 HBasicBlock* SplitAfterForInlining(HInstruction* cursor);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001327
1328 // Merge `other` at the end of `this`. Successors and dominated blocks of
1329 // `other` are changed to be successors and dominated blocks of `this`. Note
1330 // that this method does not update the graph, reverse post order, loop
1331 // information, nor make sure the blocks are consistent (for example ending
1332 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +01001333 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001334
1335 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
1336 // of `this` are moved to `other`.
1337 // Note that this method does not update the graph, reverse post order, loop
1338 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +00001339 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001340 void ReplaceWith(HBasicBlock* other);
1341
Aart Bik6b69e0a2017-01-11 10:20:43 -08001342 // Merges the instructions of `other` at the end of `this`.
1343 void MergeInstructionsWith(HBasicBlock* other);
1344
David Brazdil2d7352b2015-04-20 14:52:42 +01001345 // Merge `other` at the end of `this`. This method updates loops, reverse post
1346 // order, links to predecessors, successors, dominators and deletes the block
1347 // from the graph. The two blocks must be successive, i.e. `this` the only
1348 // predecessor of `other` and vice versa.
1349 void MergeWith(HBasicBlock* other);
1350
1351 // Disconnects `this` from all its predecessors, successors and dominator,
1352 // removes it from all loops it is included in and eventually from the graph.
1353 // The block must not dominate any other block. Predecessors and successors
1354 // are safely updated.
1355 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +00001356
Santiago Aboy Solanesd48da352022-07-28 17:58:53 +01001357 // Disconnects `this` from all its successors and updates their phis, if the successors have them.
1358 // If `visited` is provided, it will use the information to know if a successor is reachable and
1359 // skip updating those phis.
1360 void DisconnectFromSuccessors(const ArenaBitVector* visited = nullptr);
1361
Santiago Aboy Solanes7023bf82022-08-19 10:28:11 +01001362 // Removes the catch phi uses of the instructions in `this`, and then remove the instruction
1363 // itself. If `building_dominator_tree` is true, it will not remove the instruction as user, since
1364 // we do it in a previous step. This is a special case for building up the dominator tree: we want
1365 // to eliminate uses before inputs but we don't have domination information, so we remove all
1366 // connections from input/uses first before removing any instruction.
1367 // This method assumes the instructions have been removed from all users with the exception of
1368 // catch phis because of missing exceptional edges in the graph.
1369 void RemoveCatchPhiUsesAndInstruction(bool building_dominator_tree);
Santiago Aboy Solanesd48da352022-07-28 17:58:53 +01001370
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001371 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001372 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001373 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001374 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Artem Serovcced8ba2017-07-19 18:18:09 +01001375 // Replace phi `initial` with `replacement` within this block.
1376 void ReplaceAndRemovePhiWith(HPhi* initial, HPhi* replacement);
Roland Levillainccc07a92014-09-16 14:48:16 +01001377 // Replace instruction `initial` with `replacement` within this block.
1378 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
1379 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001380 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001381 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +00001382 // RemoveInstruction and RemovePhi delete a given instruction from the respective
1383 // instruction list. With 'ensure_safety' set to true, it verifies that the
1384 // instruction is not in use and removes it from the use lists of its inputs.
1385 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
1386 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +01001387 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001388
1389 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +01001390 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001391 }
1392
Roland Levillain6b879dd2014-09-22 17:13:44 +01001393 bool IsLoopPreHeaderFirstPredecessor() const {
1394 DCHECK(IsLoopHeader());
Vladimir Markoec7802a2015-10-01 20:57:57 +01001395 return GetPredecessors()[0] == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +01001396 }
1397
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001398 bool IsFirstPredecessorBackEdge() const {
1399 DCHECK(IsLoopHeader());
1400 return GetLoopInformation()->IsBackEdge(*GetPredecessors()[0]);
1401 }
1402
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001403 HLoopInformation* GetLoopInformation() const {
1404 return loop_information_;
1405 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001406
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001407 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001408 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001409 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001410 void SetInLoop(HLoopInformation* info) {
1411 if (IsLoopHeader()) {
1412 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +01001413 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001414 loop_information_ = info;
1415 } else if (loop_information_->Contains(*info->GetHeader())) {
1416 // Block is currently part of an outer loop. Make it part of this inner loop.
1417 // Note that a non loop header having a loop information means this loop information
1418 // has already been populated
1419 loop_information_ = info;
1420 } else {
1421 // Block is part of an inner loop. Do not update the loop information.
1422 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
1423 // at this point, because this method is being called while populating `info`.
1424 }
1425 }
1426
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001427 // Raw update of the loop information.
1428 void SetLoopInformation(HLoopInformation* info) {
1429 loop_information_ = info;
1430 }
1431
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001432 bool IsInLoop() const { return loop_information_ != nullptr; }
1433
David Brazdilec16f792015-08-19 15:04:01 +01001434 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
1435
1436 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
1437 try_catch_information_ = try_catch_information;
1438 }
1439
1440 bool IsTryBlock() const {
1441 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
1442 }
1443
1444 bool IsCatchBlock() const {
1445 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
1446 }
David Brazdilffee3d32015-07-06 11:48:53 +01001447
1448 // Returns the try entry that this block's successors should have. They will
1449 // be in the same try, unless the block ends in a try boundary. In that case,
1450 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +01001451 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001452
Aart Bik75ff2c92018-04-21 01:28:11 +00001453 bool HasThrowingInstructions() const;
1454
David Brazdila4b8c212015-05-07 09:59:30 +01001455 // Returns whether this block dominates the blocked passed as parameter.
Vladimir Marko8d100ba2022-03-04 10:13:10 +00001456 bool Dominates(const HBasicBlock* block) const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001457
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001458 size_t GetLifetimeStart() const { return lifetime_start_; }
1459 size_t GetLifetimeEnd() const { return lifetime_end_; }
1460
1461 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
1462 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
1463
David Brazdil8d5b8b22015-03-24 10:51:52 +00001464 bool EndsWithControlFlowInstruction() const;
Aart Bik4dc09e72018-05-11 14:40:31 -07001465 bool EndsWithReturn() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001466 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001467 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001468 bool HasSinglePhi() const;
1469
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001470 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001471 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +00001472 ArenaVector<HBasicBlock*> predecessors_;
1473 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001474 HInstructionList instructions_;
1475 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001476 HLoopInformation* loop_information_;
1477 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +00001478 ArenaVector<HBasicBlock*> dominated_blocks_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001479 uint32_t block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001480 // The dex program counter of the first instruction of this block.
1481 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001482 size_t lifetime_start_;
1483 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +01001484 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +01001485
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001486 friend class HGraph;
1487 friend class HInstruction;
Alex Light3a73ffb2021-01-25 14:11:05 +00001488 // Allow manual control of the ordering of predecessors/successors
1489 friend class OptimizingUnitTestHelper;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001490
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001491 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
1492};
1493
David Brazdilb2bd1c52015-03-25 11:17:37 +00001494// Iterates over the LoopInformation of all loops which contain 'block'
1495// from the innermost to the outermost.
1496class HLoopInformationOutwardIterator : public ValueObject {
1497 public:
1498 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
1499 : current_(block.GetLoopInformation()) {}
1500
1501 bool Done() const { return current_ == nullptr; }
1502
1503 void Advance() {
1504 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +01001505 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +00001506 }
1507
1508 HLoopInformation* Current() const {
1509 DCHECK(!Done());
1510 return current_;
1511 }
1512
1513 private:
1514 HLoopInformation* current_;
1515
1516 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
1517};
1518
Artem Serov1a719e42019-07-18 14:24:55 +01001519#define FOR_EACH_CONCRETE_INSTRUCTION_SCALAR_COMMON(M) \
Aart Bike9f37602015-10-09 11:15:55 -07001520 M(Above, Condition) \
1521 M(AboveOrEqual, Condition) \
Aart Bik3dad3412018-02-28 12:01:46 -08001522 M(Abs, UnaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001523 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001524 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001525 M(ArrayGet, Instruction) \
1526 M(ArrayLength, Instruction) \
1527 M(ArraySet, Instruction) \
Aart Bike9f37602015-10-09 11:15:55 -07001528 M(Below, Condition) \
1529 M(BelowOrEqual, Condition) \
David Brazdil66d126e2015-04-03 16:02:44 +01001530 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001531 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001532 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001533 M(CheckCast, Instruction) \
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00001534 M(ClassTableGet, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001535 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001536 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001537 M(Compare, BinaryOperation) \
Igor Murashkind01745e2017-04-05 16:40:31 -07001538 M(ConstructorFence, Instruction) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001539 M(CurrentMethod, Instruction) \
Mingyao Yang063fc772016-08-02 11:02:54 -07001540 M(ShouldDeoptimizeFlag, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001541 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001542 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001543 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001544 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001545 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001546 M(Exit, Instruction) \
1547 M(FloatConstant, Constant) \
1548 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001549 M(GreaterThan, Condition) \
1550 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001551 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001552 M(InstanceFieldGet, Instruction) \
1553 M(InstanceFieldSet, Instruction) \
Alex Light3a73ffb2021-01-25 14:11:05 +00001554 M(PredicatedInstanceFieldGet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001555 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001556 M(IntConstant, Constant) \
xueliang.zhonge0eb4832017-10-30 13:43:14 +00001557 M(IntermediateAddress, Instruction) \
Calin Juravle175dc732015-08-25 15:42:32 +01001558 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001559 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001560 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001561 M(InvokeVirtual, Invoke) \
Orion Hodsonac141392017-01-13 11:53:47 +00001562 M(InvokePolymorphic, Invoke) \
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001563 M(InvokeCustom, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001564 M(LessThan, Condition) \
1565 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001566 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001567 M(LoadException, Instruction) \
Orion Hodsondbaa5c72018-05-10 08:22:46 +01001568 M(LoadMethodHandle, Instruction) \
Orion Hodson18259d72018-04-12 11:18:23 +01001569 M(LoadMethodType, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001570 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001571 M(LongConstant, Constant) \
Aart Bik1f8d51b2018-02-15 10:42:37 -08001572 M(Max, Instruction) \
Calin Juravle27df7582015-04-17 19:12:31 +01001573 M(MemoryBarrier, Instruction) \
Mythri Alle5097f832021-11-02 14:52:30 +00001574 M(MethodEntryHook, Instruction) \
1575 M(MethodExitHook, Instruction) \
Aart Bik1f8d51b2018-02-15 10:42:37 -08001576 M(Min, BinaryOperation) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001577 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001578 M(Mul, BinaryOperation) \
1579 M(Neg, UnaryOperation) \
1580 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001581 M(NewInstance, Instruction) \
Santiago Aboy Solanescaf9b652022-06-24 10:03:30 +01001582 M(Nop, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001583 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001584 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001585 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001586 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001587 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001588 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001589 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001590 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001591 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001592 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001593 M(Return, Instruction) \
1594 M(ReturnVoid, Instruction) \
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001595 M(Ror, BinaryOperation) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001596 M(Shl, BinaryOperation) \
1597 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001598 M(StaticFieldGet, Instruction) \
1599 M(StaticFieldSet, Instruction) \
Vladimir Marko552a1342017-10-31 10:56:47 +00001600 M(StringBuilderAppend, Instruction) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001601 M(UnresolvedInstanceFieldGet, Instruction) \
1602 M(UnresolvedInstanceFieldSet, Instruction) \
1603 M(UnresolvedStaticFieldGet, Instruction) \
1604 M(UnresolvedStaticFieldSet, Instruction) \
David Brazdil74eb1b22015-12-14 11:44:01 +00001605 M(Select, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001606 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001607 M(SuspendCheck, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001608 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001609 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001610 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001611 M(UShr, BinaryOperation) \
Artem Serov1a719e42019-07-18 14:24:55 +01001612 M(Xor, BinaryOperation)
1613
1614#define FOR_EACH_CONCRETE_INSTRUCTION_VECTOR_COMMON(M) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001615 M(VecReplicateScalar, VecUnaryOperation) \
Aart Bik0148de42017-09-05 09:25:01 -07001616 M(VecExtractScalar, VecUnaryOperation) \
1617 M(VecReduce, VecUnaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001618 M(VecCnv, VecUnaryOperation) \
1619 M(VecNeg, VecUnaryOperation) \
Aart Bik6daebeb2017-04-03 14:35:41 -07001620 M(VecAbs, VecUnaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001621 M(VecNot, VecUnaryOperation) \
1622 M(VecAdd, VecBinaryOperation) \
Aart Bikf3e61ee2017-04-12 17:09:20 -07001623 M(VecHalvingAdd, VecBinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001624 M(VecSub, VecBinaryOperation) \
1625 M(VecMul, VecBinaryOperation) \
1626 M(VecDiv, VecBinaryOperation) \
Aart Bikf3e61ee2017-04-12 17:09:20 -07001627 M(VecMin, VecBinaryOperation) \
1628 M(VecMax, VecBinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001629 M(VecAnd, VecBinaryOperation) \
1630 M(VecAndNot, VecBinaryOperation) \
1631 M(VecOr, VecBinaryOperation) \
1632 M(VecXor, VecBinaryOperation) \
Aart Bik29aa0822018-03-08 11:28:00 -08001633 M(VecSaturationAdd, VecBinaryOperation) \
1634 M(VecSaturationSub, VecBinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001635 M(VecShl, VecBinaryOperation) \
1636 M(VecShr, VecBinaryOperation) \
1637 M(VecUShr, VecBinaryOperation) \
Aart Bik8de59162017-04-21 09:42:01 -07001638 M(VecSetScalars, VecOperation) \
Artem Serovf34dd202017-04-10 17:41:46 +01001639 M(VecMultiplyAccumulate, VecOperation) \
Aart Bikdbbac8f2017-09-01 13:06:08 -07001640 M(VecSADAccumulate, VecOperation) \
Artem Serovaaac0e32018-08-07 00:52:22 +01001641 M(VecDotProd, VecOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001642 M(VecLoad, VecMemoryOperation) \
1643 M(VecStore, VecMemoryOperation) \
Artem Serov07718842020-02-24 18:51:42 +00001644 M(VecPredSetAll, VecPredSetOperation) \
1645 M(VecPredWhile, VecPredSetOperation) \
Artem Serov0b284aa2021-12-14 23:16:21 +00001646 M(VecPredToBoolean, VecOperation) \
1647 M(VecCondition, VecPredSetOperation) \
1648 M(VecPredNot, VecPredSetOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001649
Artem Serov1a719e42019-07-18 14:24:55 +01001650#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
1651 FOR_EACH_CONCRETE_INSTRUCTION_SCALAR_COMMON(M) \
1652 FOR_EACH_CONCRETE_INSTRUCTION_VECTOR_COMMON(M)
1653
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001654/*
1655 * Instructions, shared across several (not all) architectures.
1656 */
1657#if !defined(ART_ENABLE_CODEGEN_arm) && !defined(ART_ENABLE_CODEGEN_arm64)
1658#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M)
1659#else
1660#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Artem Serov7fc63502016-02-09 17:15:29 +00001661 M(BitwiseNegatedRight, Instruction) \
Anton Kirilov74234da2017-01-13 14:42:47 +00001662 M(DataProcWithShifterOp, Instruction) \
Artem Serov328429f2016-07-06 16:23:04 +01001663 M(MultiplyAccumulate, Instruction) \
Artem Serove1811ed2017-04-27 16:50:47 +01001664 M(IntermediateAddressIndex, Instruction)
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001665#endif
1666
Alexandre Ramesef20f712015-06-09 10:29:30 +01001667#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
1668
1669#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
1670
Lifang Xia12e1aff2023-06-08 20:36:52 +08001671#define FOR_EACH_CONCRETE_INSTRUCTION_RISCV64(M)
1672
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001673#ifndef ART_ENABLE_CODEGEN_x86
1674#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1675#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001676#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1677 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001678 M(X86LoadFromConstantTable, Instruction) \
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001679 M(X86FPNeg, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001680 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001681#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001682
Shalini Salomi Bodapatidd121f62018-10-26 15:03:53 +05301683#if defined(ART_ENABLE_CODEGEN_x86) || defined(ART_ENABLE_CODEGEN_x86_64)
1684#define FOR_EACH_CONCRETE_INSTRUCTION_X86_COMMON(M) \
Shalini Salomi Bodapati81d15be2019-05-30 11:00:42 +05301685 M(X86AndNot, Instruction) \
Shalini Salomi Bodapatib45a4352019-07-10 16:09:41 +05301686 M(X86MaskOrResetLeastSetBit, Instruction)
Shalini Salomi Bodapatidd121f62018-10-26 15:03:53 +05301687#else
1688#define FOR_EACH_CONCRETE_INSTRUCTION_X86_COMMON(M)
1689#endif
1690
Alexandre Ramesef20f712015-06-09 10:29:30 +01001691#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1692
1693#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1694 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001695 FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001696 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1697 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
1698 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
Shalini Salomi Bodapatidd121f62018-10-26 15:03:53 +05301699 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M) \
1700 FOR_EACH_CONCRETE_INSTRUCTION_X86_COMMON(M)
Alexandre Ramesef20f712015-06-09 10:29:30 +01001701
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001702#define FOR_EACH_ABSTRACT_INSTRUCTION(M) \
1703 M(Condition, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001704 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001705 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001706 M(BinaryOperation, Instruction) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001707 M(Invoke, Instruction) \
1708 M(VecOperation, Instruction) \
1709 M(VecUnaryOperation, VecOperation) \
1710 M(VecBinaryOperation, VecOperation) \
Artem Serov07718842020-02-24 18:51:42 +00001711 M(VecMemoryOperation, VecOperation) \
1712 M(VecPredSetOperation, VecOperation)
Dave Allison20dfc792014-06-16 20:44:29 -07001713
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001714#define FOR_EACH_INSTRUCTION(M) \
1715 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1716 FOR_EACH_ABSTRACT_INSTRUCTION(M)
1717
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001718#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001719FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1720#undef FORWARD_DECLARATION
1721
Vladimir Marko372f10e2016-05-17 16:30:10 +01001722#define DECLARE_INSTRUCTION(type) \
Artem Serovcced8ba2017-07-19 18:18:09 +01001723 private: \
1724 H##type& operator=(const H##type&) = delete; \
1725 public: \
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01001726 const char* DebugName() const override { return #type; } \
1727 HInstruction* Clone(ArenaAllocator* arena) const override { \
Artem Serovcced8ba2017-07-19 18:18:09 +01001728 DCHECK(IsClonable()); \
Vladimir Marko5150dbe2023-04-26 09:13:59 +00001729 return new (arena) H##type(*this); \
Artem Serovcced8ba2017-07-19 18:18:09 +01001730 } \
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01001731 void Accept(HGraphVisitor* visitor) override
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001732
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001733#define DECLARE_ABSTRACT_INSTRUCTION(type) \
Artem Serovcced8ba2017-07-19 18:18:09 +01001734 private: \
1735 H##type& operator=(const H##type&) = delete; \
Vladimir Markoa90dd512018-05-04 15:04:45 +01001736 public:
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001737
Alex Light3a73ffb2021-01-25 14:11:05 +00001738#define DEFAULT_COPY_CONSTRUCTOR(type) H##type(const H##type& other) = default;
Artem Serovcced8ba2017-07-19 18:18:09 +01001739
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001740template <typename T>
Vladimir Marko82b07402017-03-01 19:02:04 +00001741class HUseListNode : public ArenaObject<kArenaAllocUseListNode>,
1742 public IntrusiveForwardListNode<HUseListNode<T>> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001743 public:
Igor Murashkind01745e2017-04-05 16:40:31 -07001744 // Get the instruction which has this use as one of the inputs.
David Brazdiled596192015-01-23 10:39:45 +00001745 T GetUser() const { return user_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07001746 // Get the position of the input record that this use corresponds to.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001747 size_t GetIndex() const { return index_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07001748 // Set the position of the input record that this use corresponds to.
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001749 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001750
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001751 private:
David Brazdiled596192015-01-23 10:39:45 +00001752 HUseListNode(T user, size_t index)
Vladimir Marko46817b82016-03-29 12:21:58 +01001753 : user_(user), index_(index) {}
David Brazdiled596192015-01-23 10:39:45 +00001754
1755 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001756 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001757
Vladimir Marko46817b82016-03-29 12:21:58 +01001758 friend class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001759
1760 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1761};
1762
David Brazdiled596192015-01-23 10:39:45 +00001763template <typename T>
Vladimir Marko46817b82016-03-29 12:21:58 +01001764using HUseList = IntrusiveForwardList<HUseListNode<T>>;
David Brazdiled596192015-01-23 10:39:45 +00001765
David Brazdil1abb4192015-02-17 18:33:36 +00001766// This class is used by HEnvironment and HInstruction classes to record the
1767// instructions they use and pointers to the corresponding HUseListNodes kept
1768// by the used instructions.
1769template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001770class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001771 public:
Vladimir Marko46817b82016-03-29 12:21:58 +01001772 HUserRecord() : instruction_(nullptr), before_use_node_() {}
1773 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), before_use_node_() {}
David Brazdil1abb4192015-02-17 18:33:36 +00001774
Vladimir Marko46817b82016-03-29 12:21:58 +01001775 HUserRecord(const HUserRecord<T>& old_record, typename HUseList<T>::iterator before_use_node)
1776 : HUserRecord(old_record.instruction_, before_use_node) {}
1777 HUserRecord(HInstruction* instruction, typename HUseList<T>::iterator before_use_node)
1778 : instruction_(instruction), before_use_node_(before_use_node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001779 DCHECK(instruction_ != nullptr);
David Brazdil1abb4192015-02-17 18:33:36 +00001780 }
1781
1782 HInstruction* GetInstruction() const { return instruction_; }
Vladimir Marko46817b82016-03-29 12:21:58 +01001783 typename HUseList<T>::iterator GetBeforeUseNode() const { return before_use_node_; }
1784 typename HUseList<T>::iterator GetUseNode() const { return ++GetBeforeUseNode(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001785
1786 private:
1787 // Instruction used by the user.
1788 HInstruction* instruction_;
1789
Vladimir Marko46817b82016-03-29 12:21:58 +01001790 // Iterator before the corresponding entry in the use list kept by 'instruction_'.
1791 typename HUseList<T>::iterator before_use_node_;
David Brazdil1abb4192015-02-17 18:33:36 +00001792};
1793
Vladimir Markoe9004912016-06-16 16:50:52 +01001794// Helper class that extracts the input instruction from HUserRecord<HInstruction*>.
1795// This is used for HInstruction::GetInputs() to return a container wrapper providing
1796// HInstruction* values even though the underlying container has HUserRecord<>s.
1797struct HInputExtractor {
1798 HInstruction* operator()(HUserRecord<HInstruction*>& record) const {
1799 return record.GetInstruction();
1800 }
1801 const HInstruction* operator()(const HUserRecord<HInstruction*>& record) const {
1802 return record.GetInstruction();
1803 }
1804};
1805
1806using HInputsRef = TransformArrayRef<HUserRecord<HInstruction*>, HInputExtractor>;
1807using HConstInputsRef = TransformArrayRef<const HUserRecord<HInstruction*>, HInputExtractor>;
1808
Aart Bik854a02b2015-07-14 16:07:00 -07001809/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001810 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001811 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001812 * For write/read dependences on fields/arrays, the dependence analysis uses
1813 * type disambiguation (e.g. a float field write cannot modify the value of an
1814 * integer field read) and the access type (e.g. a reference array write cannot
1815 * modify the value of a reference field read [although it may modify the
1816 * reference fetch prior to reading the field, which is represented by its own
1817 * write/read dependence]). The analysis makes conservative points-to
1818 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1819 * the same, and any reference read depends on any reference read without
1820 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001821 *
Artem Serovd1aa7d02018-06-22 11:35:46 +01001822 * kDependsOnGCBit is defined in the following way: instructions with kDependsOnGCBit must not be
1823 * alive across the point where garbage collection might happen.
1824 *
1825 * Note: Instructions with kCanTriggerGCBit do not depend on each other.
1826 *
1827 * kCanTriggerGCBit must be used for instructions for which GC might happen on the path across
1828 * those instructions from the compiler perspective (between this instruction and the next one
1829 * in the IR).
1830 *
1831 * Note: Instructions which can cause GC only on a fatal slow path do not need
1832 * kCanTriggerGCBit as the execution never returns to the instruction next to the exceptional
1833 * one. However the execution may return to compiled code if there is a catch block in the
1834 * current method; for this purpose the TryBoundary exit instruction has kCanTriggerGCBit
1835 * set.
1836 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001837 * The internal representation uses 38-bit and is described in the table below.
1838 * The first line indicates the side effect, and for field/array accesses the
1839 * second line indicates the type of the access (in the order of the
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001840 * DataType::Type enum).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001841 * The two numbered lines below indicate the bit position in the bitfield (read
1842 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001843 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001844 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1845 * +-------------+---------+---------+--------------+---------+---------+
1846 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1847 * | 3 |333333322|222222221| 1 |111111110|000000000|
1848 * | 7 |654321098|765432109| 8 |765432109|876543210|
1849 *
1850 * Note that, to ease the implementation, 'changes' bits are least significant
1851 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001852 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001853class SideEffects : public ValueObject {
1854 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001855 SideEffects() : flags_(0) {}
1856
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001857 static SideEffects None() {
1858 return SideEffects(0);
1859 }
1860
1861 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001862 return SideEffects(kAllChangeBits | kAllDependOnBits);
1863 }
1864
1865 static SideEffects AllChanges() {
1866 return SideEffects(kAllChangeBits);
1867 }
1868
1869 static SideEffects AllDependencies() {
1870 return SideEffects(kAllDependOnBits);
1871 }
1872
1873 static SideEffects AllExceptGCDependency() {
1874 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1875 }
1876
1877 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001878 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001879 }
1880
Aart Bik34c3ba92015-07-20 14:08:59 -07001881 static SideEffects AllWrites() {
1882 return SideEffects(kAllWrites);
1883 }
1884
1885 static SideEffects AllReads() {
1886 return SideEffects(kAllReads);
1887 }
1888
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001889 static SideEffects FieldWriteOfType(DataType::Type type, bool is_volatile) {
Aart Bik34c3ba92015-07-20 14:08:59 -07001890 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001891 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001892 : SideEffects(TypeFlag(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001893 }
1894
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001895 static SideEffects ArrayWriteOfType(DataType::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001896 return SideEffects(TypeFlag(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001897 }
1898
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001899 static SideEffects FieldReadOfType(DataType::Type type, bool is_volatile) {
Aart Bik34c3ba92015-07-20 14:08:59 -07001900 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001901 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001902 : SideEffects(TypeFlag(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001903 }
1904
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001905 static SideEffects ArrayReadOfType(DataType::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001906 return SideEffects(TypeFlag(type, kArrayReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001907 }
1908
Artem Serovd1aa7d02018-06-22 11:35:46 +01001909 // Returns whether GC might happen across this instruction from the compiler perspective so
1910 // the next instruction in the IR would see that.
1911 //
1912 // See the SideEffect class comments.
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001913 static SideEffects CanTriggerGC() {
1914 return SideEffects(1ULL << kCanTriggerGCBit);
1915 }
1916
Artem Serovd1aa7d02018-06-22 11:35:46 +01001917 // Returns whether the instruction must not be alive across a GC point.
1918 //
1919 // See the SideEffect class comments.
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001920 static SideEffects DependsOnGC() {
1921 return SideEffects(1ULL << kDependsOnGCBit);
1922 }
1923
Aart Bik854a02b2015-07-14 16:07:00 -07001924 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001925 SideEffects Union(SideEffects other) const {
1926 return SideEffects(flags_ | other.flags_);
1927 }
1928
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001929 SideEffects Exclusion(SideEffects other) const {
1930 return SideEffects(flags_ & ~other.flags_);
1931 }
1932
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001933 void Add(SideEffects other) {
1934 flags_ |= other.flags_;
1935 }
1936
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001937 bool Includes(SideEffects other) const {
1938 return (other.flags_ & flags_) == other.flags_;
1939 }
1940
1941 bool HasSideEffects() const {
1942 return (flags_ & kAllChangeBits);
1943 }
1944
1945 bool HasDependencies() const {
1946 return (flags_ & kAllDependOnBits);
1947 }
1948
1949 // Returns true if there are no side effects or dependencies.
1950 bool DoesNothing() const {
1951 return flags_ == 0;
1952 }
1953
Aart Bik854a02b2015-07-14 16:07:00 -07001954 // Returns true if something is written.
1955 bool DoesAnyWrite() const {
1956 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001957 }
1958
Aart Bik854a02b2015-07-14 16:07:00 -07001959 // Returns true if something is read.
1960 bool DoesAnyRead() const {
1961 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001962 }
1963
Aart Bik854a02b2015-07-14 16:07:00 -07001964 // Returns true if potentially everything is written and read
1965 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001966 bool DoesAllReadWrite() const {
1967 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1968 }
1969
Aart Bik854a02b2015-07-14 16:07:00 -07001970 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001971 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001972 }
1973
Roland Levillain0d5a2812015-11-13 10:07:31 +00001974 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001975 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001976 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1977 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001978 }
1979
1980 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001981 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001982 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001983 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001984 for (int s = kLastBit; s >= 0; s--) {
1985 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1986 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1987 // This is a bit for the GC side effect.
1988 if (current_bit_is_set) {
1989 flags += "GC";
1990 }
Aart Bik854a02b2015-07-14 16:07:00 -07001991 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001992 } else {
1993 // This is a bit for the array/field analysis.
1994 // The underscore character stands for the 'can trigger GC' bit.
1995 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1996 if (current_bit_is_set) {
1997 flags += kDebug[s];
1998 }
1999 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
2000 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
2001 flags += "|";
2002 }
2003 }
Aart Bik854a02b2015-07-14 16:07:00 -07002004 }
2005 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002006 }
2007
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002008 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002009
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002010 private:
2011 static constexpr int kFieldArrayAnalysisBits = 9;
2012
2013 static constexpr int kFieldWriteOffset = 0;
2014 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
2015 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
2016 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
2017
2018 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
2019
2020 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
2021 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
2022 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
2023 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
2024
2025 static constexpr int kLastBit = kDependsOnGCBit;
2026 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
2027
2028 // Aliases.
2029
2030 static_assert(kChangeBits == kDependOnBits,
2031 "the 'change' bits should match the 'depend on' bits.");
2032
2033 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
2034 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
2035 static constexpr uint64_t kAllWrites =
2036 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
2037 static constexpr uint64_t kAllReads =
2038 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002039
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002040 // Translates type to bit flag. The type must correspond to a Java type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002041 static uint64_t TypeFlag(DataType::Type type, int offset) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002042 int shift;
2043 switch (type) {
2044 case DataType::Type::kReference: shift = 0; break;
2045 case DataType::Type::kBool: shift = 1; break;
2046 case DataType::Type::kInt8: shift = 2; break;
2047 case DataType::Type::kUint16: shift = 3; break;
2048 case DataType::Type::kInt16: shift = 4; break;
2049 case DataType::Type::kInt32: shift = 5; break;
2050 case DataType::Type::kInt64: shift = 6; break;
2051 case DataType::Type::kFloat32: shift = 7; break;
2052 case DataType::Type::kFloat64: shift = 8; break;
2053 default:
2054 LOG(FATAL) << "Unexpected data type " << type;
2055 UNREACHABLE();
2056 }
Aart Bik854a02b2015-07-14 16:07:00 -07002057 DCHECK_LE(kFieldWriteOffset, shift);
2058 DCHECK_LT(shift, kArrayWriteOffset);
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002059 return UINT64_C(1) << (shift + offset);
Aart Bik854a02b2015-07-14 16:07:00 -07002060 }
2061
2062 // Private constructor on direct flags value.
2063 explicit SideEffects(uint64_t flags) : flags_(flags) {}
2064
2065 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002066};
2067
David Brazdiled596192015-01-23 10:39:45 +00002068// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01002069class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00002070 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002071 ALWAYS_INLINE HEnvironment(ArenaAllocator* allocator,
Mingyao Yang01b47b02017-02-03 12:09:57 -08002072 size_t number_of_vregs,
2073 ArtMethod* method,
2074 uint32_t dex_pc,
2075 HInstruction* holder)
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002076 : vregs_(number_of_vregs, allocator->Adapter(kArenaAllocEnvironmentVRegs)),
2077 locations_(allocator->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002078 parent_(nullptr),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00002079 method_(method),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002080 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002081 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002082 }
2083
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002084 ALWAYS_INLINE HEnvironment(ArenaAllocator* allocator,
2085 const HEnvironment& to_copy,
2086 HInstruction* holder)
2087 : HEnvironment(allocator,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002088 to_copy.Size(),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00002089 to_copy.GetMethod(),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002090 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002091 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002092
Vladimir Markoec32f642017-06-02 10:51:55 +01002093 void AllocateLocations() {
2094 DCHECK(locations_.empty());
2095 locations_.resize(vregs_.size());
2096 }
2097
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002098 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002099 if (parent_ != nullptr) {
2100 parent_->SetAndCopyParentChain(allocator, parent);
2101 } else {
2102 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
2103 parent_->CopyFrom(parent);
2104 if (parent->GetParent() != nullptr) {
2105 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
2106 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002107 }
David Brazdiled596192015-01-23 10:39:45 +00002108 }
2109
Vladimir Marko69d310e2017-10-09 14:12:23 +01002110 void CopyFrom(ArrayRef<HInstruction* const> locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01002111 void CopyFrom(HEnvironment* environment);
2112
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002113 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
2114 // input to the loop phi instead. This is for inserting instructions that
2115 // require an environment (like HDeoptimization) in the loop pre-header.
2116 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00002117
2118 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01002119 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00002120 }
2121
2122 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01002123 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00002124 }
2125
David Brazdil1abb4192015-02-17 18:33:36 +00002126 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00002127
Artem Serovca210e32017-12-15 13:43:20 +00002128 // Replaces the input at the position 'index' with the replacement; the replacement and old
2129 // input instructions' env_uses_ lists are adjusted. The function works similar to
2130 // HInstruction::ReplaceInput.
2131 void ReplaceInput(HInstruction* replacement, size_t index);
2132
Vladimir Markofa6b93c2015-09-15 10:15:55 +01002133 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00002134
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002135 HEnvironment* GetParent() const { return parent_; }
2136
2137 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01002138 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002139 }
2140
2141 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01002142 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002143 }
2144
2145 uint32_t GetDexPc() const {
2146 return dex_pc_;
2147 }
2148
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00002149 ArtMethod* GetMethod() const {
2150 return method_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002151 }
2152
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002153 HInstruction* GetHolder() const {
2154 return holder_;
2155 }
2156
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00002157
2158 bool IsFromInlinedInvoke() const {
2159 return GetParent() != nullptr;
2160 }
2161
Alex Light3a73ffb2021-01-25 14:11:05 +00002162 class EnvInputSelector {
2163 public:
2164 explicit EnvInputSelector(const HEnvironment* e) : env_(e) {}
2165 HInstruction* operator()(size_t s) const {
2166 return env_->GetInstructionAt(s);
2167 }
2168 private:
2169 const HEnvironment* env_;
2170 };
2171
2172 using HConstEnvInputRef = TransformIterator<CountIter, EnvInputSelector>;
2173 IterationRange<HConstEnvInputRef> GetEnvInputs() const {
2174 IterationRange<CountIter> range(Range(Size()));
2175 return MakeIterationRange(MakeTransformIterator(range.begin(), EnvInputSelector(this)),
2176 MakeTransformIterator(range.end(), EnvInputSelector(this)));
2177 }
2178
David Brazdiled596192015-01-23 10:39:45 +00002179 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01002180 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
2181 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002182 HEnvironment* parent_;
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00002183 ArtMethod* method_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002184 const uint32_t dex_pc_;
David Brazdiled596192015-01-23 10:39:45 +00002185
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01002186 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002187 HInstruction* const holder_;
2188
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01002189 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00002190
2191 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
2192};
2193
Alex Lightdc281e72021-01-06 12:35:31 -08002194std::ostream& operator<<(std::ostream& os, const HInstruction& rhs);
2195
Alex Light3a73ffb2021-01-25 14:11:05 +00002196// Iterates over the Environments
2197class HEnvironmentIterator : public ValueObject,
2198 public std::iterator<std::forward_iterator_tag, HEnvironment*> {
2199 public:
2200 explicit HEnvironmentIterator(HEnvironment* cur) : cur_(cur) {}
2201
2202 HEnvironment* operator*() const {
2203 return cur_;
2204 }
2205
2206 HEnvironmentIterator& operator++() {
2207 DCHECK(cur_ != nullptr);
2208 cur_ = cur_->GetParent();
2209 return *this;
2210 }
2211
2212 HEnvironmentIterator operator++(int) {
2213 HEnvironmentIterator prev(*this);
2214 ++(*this);
2215 return prev;
2216 }
2217
2218 bool operator==(const HEnvironmentIterator& other) const {
2219 return other.cur_ == cur_;
2220 }
2221
2222 bool operator!=(const HEnvironmentIterator& other) const {
2223 return !(*this == other);
2224 }
2225
2226 private:
2227 HEnvironment* cur_;
2228};
2229
Vladimir Markof9f64412015-09-02 14:05:49 +01002230class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002231 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302232#define DECLARE_KIND(type, super) k##type,
Vladimir Marko9974e3c2020-06-10 16:27:06 +01002233 enum InstructionKind { // private marker to avoid generate-operator-out.py from processing.
Vladimir Markoe3946222018-05-04 14:18:47 +01002234 FOR_EACH_CONCRETE_INSTRUCTION(DECLARE_KIND)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302235 kLastInstructionKind
2236 };
2237#undef DECLARE_KIND
2238
2239 HInstruction(InstructionKind kind, SideEffects side_effects, uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01002240 : HInstruction(kind, DataType::Type::kVoid, side_effects, dex_pc) {}
2241
2242 HInstruction(InstructionKind kind, DataType::Type type, SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002243 : previous_(nullptr),
2244 next_(nullptr),
2245 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002246 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002247 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002248 ssa_index_(-1),
Vladimir Markoa1de9182016-02-25 11:37:38 +00002249 packed_fields_(0u),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002250 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002251 locations_(nullptr),
2252 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002253 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00002254 side_effects_(side_effects),
Vladimir Markoa1de9182016-02-25 11:37:38 +00002255 reference_type_handle_(ReferenceTypeInfo::CreateInvalid().GetTypeHandle()) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302256 SetPackedField<InstructionKindField>(kind);
Vladimir Markobd785672018-05-03 17:09:09 +01002257 SetPackedField<TypeField>(type);
Vladimir Markoa1de9182016-02-25 11:37:38 +00002258 SetPackedFlag<kFlagReferenceTypeIsExact>(ReferenceTypeInfo::CreateInvalid().IsExact());
2259 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002260
Dave Allison20dfc792014-06-16 20:44:29 -07002261 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002262
Vladimir Markoc9fcfd02021-01-05 16:57:30 +00002263 std::ostream& Dump(std::ostream& os, bool dump_args = false);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002264
Alex Lightdc281e72021-01-06 12:35:31 -08002265 // Helper for dumping without argument information using operator<<
2266 struct NoArgsDump {
2267 const HInstruction* ins;
2268 };
2269 NoArgsDump DumpWithoutArgs() const {
2270 return NoArgsDump{this};
2271 }
2272 // Helper for dumping with argument information using operator<<
2273 struct ArgsDump {
2274 const HInstruction* ins;
2275 };
2276 ArgsDump DumpWithArgs() const {
2277 return ArgsDump{this};
2278 }
2279
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002280 HInstruction* GetNext() const { return next_; }
2281 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002282
Calin Juravle77520bc2015-01-12 18:45:46 +00002283 HInstruction* GetNextDisregardingMoves() const;
2284 HInstruction* GetPreviousDisregardingMoves() const;
2285
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002286 HBasicBlock* GetBlock() const { return block_; }
Vladimir Markoca6fff82017-10-03 14:49:14 +01002287 ArenaAllocator* GetAllocator() const { return block_->GetGraph()->GetAllocator(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002288 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002289 bool IsInBlock() const { return block_ != nullptr; }
2290 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00002291 bool IsLoopHeaderPhi() const { return IsPhi() && block_->IsLoopHeader(); }
2292 bool IsIrreducibleLoopHeaderPhi() const {
2293 return IsLoopHeaderPhi() && GetBlock()->GetLoopInformation()->IsIrreducible();
2294 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002295
Vladimir Marko372f10e2016-05-17 16:30:10 +01002296 virtual ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() = 0;
2297
2298 ArrayRef<const HUserRecord<HInstruction*>> GetInputRecords() const {
2299 // One virtual method is enough, just const_cast<> and then re-add the const.
2300 return ArrayRef<const HUserRecord<HInstruction*>>(
2301 const_cast<HInstruction*>(this)->GetInputRecords());
2302 }
2303
Vladimir Markoe9004912016-06-16 16:50:52 +01002304 HInputsRef GetInputs() {
2305 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01002306 }
2307
Vladimir Markoe9004912016-06-16 16:50:52 +01002308 HConstInputsRef GetInputs() const {
2309 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01002310 }
2311
2312 size_t InputCount() const { return GetInputRecords().size(); }
David Brazdil1abb4192015-02-17 18:33:36 +00002313 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002314
Aart Bik2767f4b2016-10-28 15:03:53 -07002315 bool HasInput(HInstruction* input) const {
2316 for (const HInstruction* i : GetInputs()) {
2317 if (i == input) {
2318 return true;
2319 }
2320 }
2321 return false;
2322 }
2323
Vladimir Marko372f10e2016-05-17 16:30:10 +01002324 void SetRawInputAt(size_t index, HInstruction* input) {
2325 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
2326 }
2327
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002328 virtual void Accept(HGraphVisitor* visitor) = 0;
2329 virtual const char* DebugName() const = 0;
2330
Vladimir Markobd785672018-05-03 17:09:09 +01002331 DataType::Type GetType() const {
2332 return TypeField::Decode(GetPackedFields());
2333 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002334
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002335 virtual bool NeedsEnvironment() const { return false; }
Santiago Aboy Solanese43aa3f2021-11-01 09:02:09 +00002336 virtual bool NeedsBss() const {
2337 return false;
2338 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002339
2340 uint32_t GetDexPc() const { return dex_pc_; }
2341
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002342 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01002343
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002344 // Can the instruction throw?
2345 // TODO: We should rename to CanVisiblyThrow, as some instructions (like HNewInstance),
2346 // could throw OOME, but it is still OK to remove them if they are unused.
Roland Levillaine161a2a2014-10-03 12:45:18 +01002347 virtual bool CanThrow() const { return false; }
Aart Bika8b8e9b2018-01-09 11:01:02 -08002348
2349 // Does the instruction always throw an exception unconditionally?
2350 virtual bool AlwaysThrows() const { return false; }
Alex Light3a73ffb2021-01-25 14:11:05 +00002351 // Will this instruction only cause async exceptions if it causes any at all?
2352 virtual bool OnlyThrowsAsyncExceptions() const {
2353 return false;
2354 }
Aart Bika8b8e9b2018-01-09 11:01:02 -08002355
David Brazdilec16f792015-08-19 15:04:01 +01002356 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07002357
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002358 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07002359 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002360
Calin Juravle10e244f2015-01-26 18:54:32 +00002361 // Does not apply for all instructions, but having this at top level greatly
2362 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00002363 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002364 virtual bool CanBeNull() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002365 DCHECK_EQ(GetType(), DataType::Type::kReference) << "CanBeNull only applies to reference types";
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002366 return true;
2367 }
Calin Juravle10e244f2015-01-26 18:54:32 +00002368
Stefano Cianciulli78f3c722023-05-16 10:32:54 +00002369 virtual bool CanDoImplicitNullCheckOn([[maybe_unused]] HInstruction* obj) const { return false; }
Calin Juravle77520bc2015-01-12 18:45:46 +00002370
Nicolas Geoffray61ba8d22018-08-07 09:55:57 +01002371 // If this instruction will do an implicit null check, return the `HNullCheck` associated
2372 // with it. Otherwise return null.
2373 HNullCheck* GetImplicitNullCheck() const {
Vladimir Markoee1f1262019-06-26 14:37:17 +01002374 // Go over previous non-move instructions that are emitted at use site.
2375 HInstruction* prev_not_move = GetPreviousDisregardingMoves();
2376 while (prev_not_move != nullptr && prev_not_move->IsEmittedAtUseSite()) {
2377 if (prev_not_move->IsNullCheck()) {
Vladimir Markocde64972023-04-25 16:40:06 +00002378 return prev_not_move->AsNullCheck();
Vladimir Markoee1f1262019-06-26 14:37:17 +01002379 }
2380 prev_not_move = prev_not_move->GetPreviousDisregardingMoves();
Nicolas Geoffray61ba8d22018-08-07 09:55:57 +01002381 }
2382 return nullptr;
2383 }
2384
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00002385 virtual bool IsActualObject() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002386 return GetType() == DataType::Type::kReference;
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00002387 }
2388
Santiago Aboy Solanese05bc3e2023-02-20 14:26:23 +00002389 // Sets the ReferenceTypeInfo. The RTI must be valid.
Calin Juravle2e768302015-07-28 14:41:11 +00002390 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Santiago Aboy Solanese05bc3e2023-02-20 14:26:23 +00002391 // Same as above, but we only set it if it's valid. Otherwise, we don't change the current RTI.
2392 void SetReferenceTypeInfoIfValid(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00002393
Calin Juravle61d544b2015-02-23 16:46:57 +00002394 ReferenceTypeInfo GetReferenceTypeInfo() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002395 DCHECK_EQ(GetType(), DataType::Type::kReference);
Vladimir Markoa1de9182016-02-25 11:37:38 +00002396 return ReferenceTypeInfo::CreateUnchecked(reference_type_handle_,
Vladimir Marko456307a2016-04-19 14:12:13 +00002397 GetPackedFlag<kFlagReferenceTypeIsExact>());
Calin Juravle61d544b2015-02-23 16:46:57 +00002398 }
Calin Juravleacf735c2015-02-12 15:25:22 +00002399
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002400 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00002401 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01002402 // Note: fixup_end remains valid across push_front().
2403 auto fixup_end = uses_.empty() ? uses_.begin() : ++uses_.begin();
Vladimir Marko3224f382020-06-23 14:19:53 +01002404 ArenaAllocator* allocator = user->GetBlock()->GetGraph()->GetAllocator();
Vladimir Marko46817b82016-03-29 12:21:58 +01002405 HUseListNode<HInstruction*>* new_node =
Vladimir Marko3224f382020-06-23 14:19:53 +01002406 new (allocator) HUseListNode<HInstruction*>(user, index);
Vladimir Marko46817b82016-03-29 12:21:58 +01002407 uses_.push_front(*new_node);
2408 FixUpUserRecordsAfterUseInsertion(fixup_end);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002409 }
2410
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002411 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01002412 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01002413 // Note: env_fixup_end remains valid across push_front().
2414 auto env_fixup_end = env_uses_.empty() ? env_uses_.begin() : ++env_uses_.begin();
2415 HUseListNode<HEnvironment*>* new_node =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002416 new (GetBlock()->GetGraph()->GetAllocator()) HUseListNode<HEnvironment*>(user, index);
Vladimir Marko46817b82016-03-29 12:21:58 +01002417 env_uses_.push_front(*new_node);
2418 FixUpUserRecordsAfterEnvUseInsertion(env_fixup_end);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002419 }
2420
David Brazdil1abb4192015-02-17 18:33:36 +00002421 void RemoveAsUserOfInput(size_t input) {
2422 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
Vladimir Marko46817b82016-03-29 12:21:58 +01002423 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
2424 input_use.GetInstruction()->uses_.erase_after(before_use_node);
2425 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
David Brazdil1abb4192015-02-17 18:33:36 +00002426 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002427
Vladimir Marko372f10e2016-05-17 16:30:10 +01002428 void RemoveAsUserOfAllInputs() {
2429 for (const HUserRecord<HInstruction*>& input_use : GetInputRecords()) {
2430 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
2431 input_use.GetInstruction()->uses_.erase_after(before_use_node);
2432 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
2433 }
2434 }
2435
David Brazdil1abb4192015-02-17 18:33:36 +00002436 const HUseList<HInstruction*>& GetUses() const { return uses_; }
2437 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002438
Vladimir Marko46817b82016-03-29 12:21:58 +01002439 bool HasUses() const { return !uses_.empty() || !env_uses_.empty(); }
2440 bool HasEnvironmentUses() const { return !env_uses_.empty(); }
2441 bool HasNonEnvironmentUses() const { return !uses_.empty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01002442 bool HasOnlyOneNonEnvironmentUse() const {
Vladimir Marko46817b82016-03-29 12:21:58 +01002443 return !HasEnvironmentUses() && GetUses().HasExactlyOneElement();
Alexandre Rames188d4312015-04-09 18:30:21 +01002444 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002445
Aart Bikcc42be02016-10-20 16:14:16 -07002446 bool IsRemovable() const {
Aart Bik482095d2016-10-10 15:39:10 -07002447 return
Aart Bikff7d89c2016-11-07 08:49:28 -08002448 !DoesAnyWrite() &&
Aart Bik482095d2016-10-10 15:39:10 -07002449 !CanThrow() &&
2450 !IsSuspendCheck() &&
2451 !IsControlFlow() &&
Santiago Aboy Solanescaf9b652022-06-24 10:03:30 +01002452 !IsNop() &&
Aart Bik482095d2016-10-10 15:39:10 -07002453 !IsParameterValue() &&
Aart Bik482095d2016-10-10 15:39:10 -07002454 // If we added an explicit barrier then we should keep it.
Igor Murashkind01745e2017-04-05 16:40:31 -07002455 !IsMemoryBarrier() &&
2456 !IsConstructorFence();
Aart Bik482095d2016-10-10 15:39:10 -07002457 }
2458
Aart Bikcc42be02016-10-20 16:14:16 -07002459 bool IsDeadAndRemovable() const {
2460 return IsRemovable() && !HasUses();
2461 }
2462
Santiago Aboy Solanes8c3b58a2022-08-15 13:21:59 +00002463 // Does this instruction dominate `other_instruction`?
2464 // Aborts if this instruction and `other_instruction` are different phis.
2465 bool Dominates(HInstruction* other_instruction) const;
2466
2467 // Same but with `strictly dominates` i.e. returns false if this instruction and
2468 // `other_instruction` are the same.
Roland Levillain6c82d402014-10-13 16:10:27 +01002469 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01002470
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002471 int GetId() const { return id_; }
2472 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002473
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002474 int GetSsaIndex() const { return ssa_index_; }
2475 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
2476 bool HasSsaIndex() const { return ssa_index_ != -1; }
2477
2478 bool HasEnvironment() const { return environment_ != nullptr; }
2479 HEnvironment* GetEnvironment() const { return environment_; }
Alex Light3a73ffb2021-01-25 14:11:05 +00002480 IterationRange<HEnvironmentIterator> GetAllEnvironments() const {
2481 return MakeIterationRange(HEnvironmentIterator(GetEnvironment()),
2482 HEnvironmentIterator(nullptr));
2483 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002484 // Set the `environment_` field. Raw because this method does not
2485 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002486 void SetRawEnvironment(HEnvironment* environment) {
2487 DCHECK(environment_ == nullptr);
2488 DCHECK_EQ(environment->GetHolder(), this);
2489 environment_ = environment;
2490 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002491
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002492 void InsertRawEnvironment(HEnvironment* environment) {
2493 DCHECK(environment_ != nullptr);
2494 DCHECK_EQ(environment->GetHolder(), this);
2495 DCHECK(environment->GetParent() == nullptr);
2496 environment->parent_ = environment_;
2497 environment_ = environment;
2498 }
2499
Vladimir Markocac5a7e2016-02-22 10:39:50 +00002500 void RemoveEnvironment();
2501
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002502 // Set the environment of this instruction, copying it from `environment`. While
2503 // copying, the uses lists are being updated.
2504 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002505 DCHECK(environment_ == nullptr);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002506 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetAllocator();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002507 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002508 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002509 if (environment->GetParent() != nullptr) {
2510 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2511 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002512 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002513
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002514 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
2515 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002516 DCHECK(environment_ == nullptr);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002517 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetAllocator();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002518 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002519 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002520 if (environment->GetParent() != nullptr) {
2521 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2522 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002523 }
2524
Nicolas Geoffray39468442014-09-02 15:17:15 +01002525 // Returns the number of entries in the environment. Typically, that is the
2526 // number of dex registers in a method. It could be more in case of inlining.
2527 size_t EnvironmentSize() const;
2528
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002529 LocationSummary* GetLocations() const { return locations_; }
2530 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002531
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002532 void ReplaceWith(HInstruction* instruction);
Santiago Aboy Solanes8c3b58a2022-08-15 13:21:59 +00002533 void ReplaceUsesDominatedBy(HInstruction* dominator,
2534 HInstruction* replacement,
2535 bool strictly_dominated = true);
Nicolas Geoffray8a62a4c2018-07-03 09:39:07 +01002536 void ReplaceEnvUsesDominatedBy(HInstruction* dominator, HInstruction* replacement);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002537 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002538
Alexandre Rames188d4312015-04-09 18:30:21 +01002539 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
2540 // uses of this instruction by `other` are *not* updated.
2541 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
2542 ReplaceWith(other);
2543 other->ReplaceInput(this, use_index);
2544 }
2545
Alexandre Rames22aa54b2016-10-18 09:32:29 +01002546 // Move `this` instruction before `cursor`
2547 void MoveBefore(HInstruction* cursor, bool do_checks = true);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002548
Vladimir Markofb337ea2015-11-25 15:25:10 +00002549 // Move `this` before its first user and out of any loops. If there is no
2550 // out-of-loop user that dominates all other users, move the instruction
2551 // to the end of the out-of-loop common dominator of the user's blocks.
2552 //
2553 // This can be used only on non-throwing instructions with no side effects that
2554 // have at least one use but no environment uses.
2555 void MoveBeforeFirstUserAndOutOfLoops();
2556
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002557#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Markoa90dd512018-05-04 15:04:45 +01002558 bool Is##type() const;
2559
2560 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
2561#undef INSTRUCTION_TYPE_CHECK
2562
2563#define INSTRUCTION_TYPE_CAST(type, super) \
Vladimir Markocde64972023-04-25 16:40:06 +00002564 const H##type* As##type() const; \
2565 H##type* As##type(); \
Vladimir Marko79dc2172023-04-05 10:33:07 +00002566 const H##type* As##type##OrNull() const; \
2567 H##type* As##type##OrNull();
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002568
Vladimir Markoa90dd512018-05-04 15:04:45 +01002569 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CAST)
2570#undef INSTRUCTION_TYPE_CAST
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002571
Artem Serovcced8ba2017-07-19 18:18:09 +01002572 // Return a clone of the instruction if it is clonable (shallow copy by default, custom copy
2573 // if a custom copy-constructor is provided for a particular type). If IsClonable() is false for
2574 // the instruction then the behaviour of this function is undefined.
2575 //
2576 // Note: It is semantically valid to create a clone of the instruction only until
2577 // prepare_for_register_allocator phase as lifetime, intervals and codegen info are not
2578 // copied.
2579 //
2580 // Note: HEnvironment and some other fields are not copied and are set to default values, see
2581 // 'explicit HInstruction(const HInstruction& other)' for details.
Stefano Cianciulli78f3c722023-05-16 10:32:54 +00002582 virtual HInstruction* Clone([[maybe_unused]] ArenaAllocator* arena) const {
Artem Serovcced8ba2017-07-19 18:18:09 +01002583 LOG(FATAL) << "Cloning is not implemented for the instruction " <<
2584 DebugName() << " " << GetId();
2585 UNREACHABLE();
2586 }
2587
Alex Light3a73ffb2021-01-25 14:11:05 +00002588 virtual bool IsFieldAccess() const {
2589 return false;
2590 }
2591
2592 virtual const FieldInfo& GetFieldInfo() const {
2593 CHECK(IsFieldAccess()) << "Only callable on field accessors not " << DebugName() << " "
2594 << *this;
2595 LOG(FATAL) << "Must be overridden by field accessors. Not implemented by " << *this;
2596 UNREACHABLE();
2597 }
2598
Artem Serovcced8ba2017-07-19 18:18:09 +01002599 // Return whether instruction can be cloned (copied).
2600 virtual bool IsClonable() const { return false; }
2601
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002602 // Returns whether the instruction can be moved within the graph.
Aart Bik71bf7b42016-11-16 10:17:46 -08002603 // TODO: this method is used by LICM and GVN with possibly different
2604 // meanings? split and rename?
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002605 virtual bool CanBeMoved() const { return false; }
2606
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002607 // Returns whether any data encoded in the two instructions is equal.
2608 // This method does not look at the inputs. Both instructions must be
2609 // of the same type, otherwise the method has undefined behavior.
Stefano Cianciulli78f3c722023-05-16 10:32:54 +00002610 virtual bool InstructionDataEquals([[maybe_unused]] const HInstruction* other) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002611 return false;
2612 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002613
2614 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00002615 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002616 // 2) Their inputs are identical.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002617 bool Equals(const HInstruction* other) const;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002618
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302619 InstructionKind GetKind() const { return GetPackedField<InstructionKindField>(); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002620
2621 virtual size_t ComputeHashCode() const {
2622 size_t result = GetKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01002623 for (const HInstruction* input : GetInputs()) {
2624 result = (result * 31) + input->GetId();
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002625 }
2626 return result;
2627 }
2628
2629 SideEffects GetSideEffects() const { return side_effects_; }
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00002630 void SetSideEffects(SideEffects other) { side_effects_ = other; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002631 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002632
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002633 size_t GetLifetimePosition() const { return lifetime_position_; }
2634 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2635 LiveInterval* GetLiveInterval() const { return live_interval_; }
2636 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2637 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2638
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002639 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2640
2641 // Returns whether the code generation of the instruction will require to have access
2642 // to the current method. Such instructions are:
2643 // (1): Instructions that require an environment, as calling the runtime requires
2644 // to walk the stack and have the current method stored at a specific stack address.
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002645 // (2): HCurrentMethod, potentially used by HInvokeStaticOrDirect, HLoadString, or HLoadClass
2646 // to access the dex cache.
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002647 bool NeedsCurrentMethod() const {
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002648 return NeedsEnvironment() || IsCurrentMethod();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002649 }
2650
Mark Mendellc4701932015-04-10 13:18:51 -04002651 // Does this instruction have any use in an environment before
2652 // control flow hits 'other'?
2653 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2654
2655 // Remove all references to environment uses of this instruction.
2656 // The caller must ensure that this is safe to do.
2657 void RemoveEnvironmentUsers();
2658
Vladimir Markoa1de9182016-02-25 11:37:38 +00002659 bool IsEmittedAtUseSite() const { return GetPackedFlag<kFlagEmittedAtUseSite>(); }
2660 void MarkEmittedAtUseSite() { SetPackedFlag<kFlagEmittedAtUseSite>(true); }
David Brazdilb3e773e2016-01-26 11:28:37 +00002661
David Brazdil1abb4192015-02-17 18:33:36 +00002662 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002663 // If set, the machine code for this instruction is assumed to be generated by
2664 // its users. Used by liveness analysis to compute use positions accordingly.
2665 static constexpr size_t kFlagEmittedAtUseSite = 0u;
2666 static constexpr size_t kFlagReferenceTypeIsExact = kFlagEmittedAtUseSite + 1;
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302667 static constexpr size_t kFieldInstructionKind = kFlagReferenceTypeIsExact + 1;
2668 static constexpr size_t kFieldInstructionKindSize =
2669 MinimumBitsToStore(static_cast<size_t>(InstructionKind::kLastInstructionKind - 1));
Vladimir Markobd785672018-05-03 17:09:09 +01002670 static constexpr size_t kFieldType =
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302671 kFieldInstructionKind + kFieldInstructionKindSize;
Vladimir Markobd785672018-05-03 17:09:09 +01002672 static constexpr size_t kFieldTypeSize =
2673 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
2674 static constexpr size_t kNumberOfGenericPackedBits = kFieldType + kFieldTypeSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00002675 static constexpr size_t kMaxNumberOfPackedBits = sizeof(uint32_t) * kBitsPerByte;
2676
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302677 static_assert(kNumberOfGenericPackedBits <= kMaxNumberOfPackedBits,
2678 "Too many generic packed fields");
2679
Vladimir Markobd785672018-05-03 17:09:09 +01002680 using TypeField = BitField<DataType::Type, kFieldType, kFieldTypeSize>;
2681
Vladimir Marko372f10e2016-05-17 16:30:10 +01002682 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const {
2683 return GetInputRecords()[i];
2684 }
2685
2686 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) {
2687 ArrayRef<HUserRecord<HInstruction*>> input_records = GetInputRecords();
2688 input_records[index] = input;
2689 }
David Brazdil1abb4192015-02-17 18:33:36 +00002690
Vladimir Markoa1de9182016-02-25 11:37:38 +00002691 uint32_t GetPackedFields() const {
2692 return packed_fields_;
2693 }
2694
2695 template <size_t flag>
2696 bool GetPackedFlag() const {
2697 return (packed_fields_ & (1u << flag)) != 0u;
2698 }
2699
2700 template <size_t flag>
2701 void SetPackedFlag(bool value = true) {
2702 packed_fields_ = (packed_fields_ & ~(1u << flag)) | ((value ? 1u : 0u) << flag);
2703 }
2704
2705 template <typename BitFieldType>
2706 typename BitFieldType::value_type GetPackedField() const {
2707 return BitFieldType::Decode(packed_fields_);
2708 }
2709
2710 template <typename BitFieldType>
2711 void SetPackedField(typename BitFieldType::value_type value) {
2712 DCHECK(IsUint<BitFieldType::size>(static_cast<uintptr_t>(value)));
2713 packed_fields_ = BitFieldType::Update(value, packed_fields_);
2714 }
2715
Artem Serovcced8ba2017-07-19 18:18:09 +01002716 // Copy construction for the instruction (used for Clone function).
2717 //
2718 // Fields (e.g. lifetime, intervals and codegen info) associated with phases starting from
2719 // prepare_for_register_allocator are not copied (set to default values).
2720 //
2721 // Copy constructors must be provided for every HInstruction type; default copy constructor is
2722 // fine for most of them. However for some of the instructions a custom copy constructor must be
2723 // specified (when instruction has non-trivially copyable fields and must have a special behaviour
2724 // for copying them).
2725 explicit HInstruction(const HInstruction& other)
2726 : previous_(nullptr),
2727 next_(nullptr),
2728 block_(nullptr),
2729 dex_pc_(other.dex_pc_),
2730 id_(-1),
2731 ssa_index_(-1),
2732 packed_fields_(other.packed_fields_),
2733 environment_(nullptr),
2734 locations_(nullptr),
2735 live_interval_(nullptr),
2736 lifetime_position_(kNoLifetime),
2737 side_effects_(other.side_effects_),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302738 reference_type_handle_(other.reference_type_handle_) {
2739 }
Artem Serovcced8ba2017-07-19 18:18:09 +01002740
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002741 private:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302742 using InstructionKindField =
2743 BitField<InstructionKind, kFieldInstructionKind, kFieldInstructionKindSize>;
2744
Vladimir Marko46817b82016-03-29 12:21:58 +01002745 void FixUpUserRecordsAfterUseInsertion(HUseList<HInstruction*>::iterator fixup_end) {
2746 auto before_use_node = uses_.before_begin();
2747 for (auto use_node = uses_.begin(); use_node != fixup_end; ++use_node) {
2748 HInstruction* user = use_node->GetUser();
2749 size_t input_index = use_node->GetIndex();
2750 user->SetRawInputRecordAt(input_index, HUserRecord<HInstruction*>(this, before_use_node));
2751 before_use_node = use_node;
2752 }
2753 }
2754
2755 void FixUpUserRecordsAfterUseRemoval(HUseList<HInstruction*>::iterator before_use_node) {
2756 auto next = ++HUseList<HInstruction*>::iterator(before_use_node);
2757 if (next != uses_.end()) {
2758 HInstruction* next_user = next->GetUser();
2759 size_t next_index = next->GetIndex();
2760 DCHECK(next_user->InputRecordAt(next_index).GetInstruction() == this);
2761 next_user->SetRawInputRecordAt(next_index, HUserRecord<HInstruction*>(this, before_use_node));
2762 }
2763 }
2764
2765 void FixUpUserRecordsAfterEnvUseInsertion(HUseList<HEnvironment*>::iterator env_fixup_end) {
2766 auto before_env_use_node = env_uses_.before_begin();
2767 for (auto env_use_node = env_uses_.begin(); env_use_node != env_fixup_end; ++env_use_node) {
2768 HEnvironment* user = env_use_node->GetUser();
2769 size_t input_index = env_use_node->GetIndex();
2770 user->vregs_[input_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2771 before_env_use_node = env_use_node;
2772 }
2773 }
2774
2775 void FixUpUserRecordsAfterEnvUseRemoval(HUseList<HEnvironment*>::iterator before_env_use_node) {
2776 auto next = ++HUseList<HEnvironment*>::iterator(before_env_use_node);
2777 if (next != env_uses_.end()) {
2778 HEnvironment* next_user = next->GetUser();
2779 size_t next_index = next->GetIndex();
2780 DCHECK(next_user->vregs_[next_index].GetInstruction() == this);
2781 next_user->vregs_[next_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2782 }
2783 }
David Brazdil1abb4192015-02-17 18:33:36 +00002784
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002785 HInstruction* previous_;
2786 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002787 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002788 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002789
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002790 // An instruction gets an id when it is added to the graph.
2791 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002792 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002793 int id_;
2794
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002795 // When doing liveness analysis, instructions that have uses get an SSA index.
2796 int ssa_index_;
2797
Vladimir Markoa1de9182016-02-25 11:37:38 +00002798 // Packed fields.
2799 uint32_t packed_fields_;
David Brazdilb3e773e2016-01-26 11:28:37 +00002800
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002801 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002802 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002803
2804 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002805 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002806
Nicolas Geoffray39468442014-09-02 15:17:15 +01002807 // The environment associated with this instruction. Not null if the instruction
2808 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002809 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002810
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002811 // Set by the code generator.
2812 LocationSummary* locations_;
2813
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002814 // Set by the liveness analysis.
2815 LiveInterval* live_interval_;
2816
2817 // Set by the liveness analysis, this is the position in a linear
2818 // order of blocks where this instruction's live interval start.
2819 size_t lifetime_position_;
2820
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002821 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002822
Vladimir Markoa1de9182016-02-25 11:37:38 +00002823 // The reference handle part of the reference type info.
2824 // The IsExact() flag is stored in packed fields.
Calin Juravleacf735c2015-02-12 15:25:22 +00002825 // TODO: for primitive types this should be marked as invalid.
Vladimir Markoa1de9182016-02-25 11:37:38 +00002826 ReferenceTypeInfo::TypeHandle reference_type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00002827
David Brazdil1abb4192015-02-17 18:33:36 +00002828 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002829 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002830 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002831 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002832 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002833};
Alex Light3a73ffb2021-01-25 14:11:05 +00002834
Vladimir Marko9974e3c2020-06-10 16:27:06 +01002835std::ostream& operator<<(std::ostream& os, HInstruction::InstructionKind rhs);
Alex Lightdc281e72021-01-06 12:35:31 -08002836std::ostream& operator<<(std::ostream& os, const HInstruction::NoArgsDump rhs);
2837std::ostream& operator<<(std::ostream& os, const HInstruction::ArgsDump rhs);
2838std::ostream& operator<<(std::ostream& os, const HUseList<HInstruction*>& lst);
2839std::ostream& operator<<(std::ostream& os, const HUseList<HEnvironment*>& lst);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002840
Alex Light3a73ffb2021-01-25 14:11:05 +00002841// Forward declarations for friends
2842template <typename InnerIter> struct HSTLInstructionIterator;
2843
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002844// Iterates over the instructions, while preserving the next instruction
2845// in case the current instruction gets removed from the list by the user
2846// of this iterator.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002847class HInstructionIterator : public ValueObject {
2848 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002849 explicit HInstructionIterator(const HInstructionList& instructions)
2850 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002851 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002852 }
2853
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002854 bool Done() const { return instruction_ == nullptr; }
2855 HInstruction* Current() const { return instruction_; }
2856 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002857 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002858 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002859 }
2860
2861 private:
Alex Light3a73ffb2021-01-25 14:11:05 +00002862 HInstructionIterator() : instruction_(nullptr), next_(nullptr) {}
2863
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002864 HInstruction* instruction_;
2865 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002866
Alex Light3a73ffb2021-01-25 14:11:05 +00002867 friend struct HSTLInstructionIterator<HInstructionIterator>;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002868};
2869
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002870// Iterates over the instructions without saving the next instruction,
2871// therefore handling changes in the graph potentially made by the user
2872// of this iterator.
2873class HInstructionIteratorHandleChanges : public ValueObject {
2874 public:
2875 explicit HInstructionIteratorHandleChanges(const HInstructionList& instructions)
2876 : instruction_(instructions.first_instruction_) {
2877 }
2878
2879 bool Done() const { return instruction_ == nullptr; }
2880 HInstruction* Current() const { return instruction_; }
2881 void Advance() {
2882 instruction_ = instruction_->GetNext();
2883 }
2884
2885 private:
Alex Light3a73ffb2021-01-25 14:11:05 +00002886 HInstructionIteratorHandleChanges() : instruction_(nullptr) {}
2887
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002888 HInstruction* instruction_;
2889
Alex Light3a73ffb2021-01-25 14:11:05 +00002890 friend struct HSTLInstructionIterator<HInstructionIteratorHandleChanges>;
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002891};
2892
2893
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002894class HBackwardInstructionIterator : public ValueObject {
2895 public:
2896 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2897 : instruction_(instructions.last_instruction_) {
2898 next_ = Done() ? nullptr : instruction_->GetPrevious();
2899 }
2900
2901 bool Done() const { return instruction_ == nullptr; }
2902 HInstruction* Current() const { return instruction_; }
2903 void Advance() {
2904 instruction_ = next_;
2905 next_ = Done() ? nullptr : instruction_->GetPrevious();
2906 }
2907
2908 private:
Alex Light3a73ffb2021-01-25 14:11:05 +00002909 HBackwardInstructionIterator() : instruction_(nullptr), next_(nullptr) {}
2910
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002911 HInstruction* instruction_;
2912 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002913
Alex Light3a73ffb2021-01-25 14:11:05 +00002914 friend struct HSTLInstructionIterator<HBackwardInstructionIterator>;
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002915};
2916
Alex Light3a73ffb2021-01-25 14:11:05 +00002917template <typename InnerIter>
2918struct HSTLInstructionIterator : public ValueObject,
2919 public std::iterator<std::forward_iterator_tag, HInstruction*> {
2920 public:
2921 static_assert(std::is_same_v<InnerIter, HBackwardInstructionIterator> ||
2922 std::is_same_v<InnerIter, HInstructionIterator> ||
2923 std::is_same_v<InnerIter, HInstructionIteratorHandleChanges>,
2924 "Unknown wrapped iterator!");
2925
2926 explicit HSTLInstructionIterator(InnerIter inner) : inner_(inner) {}
2927 HInstruction* operator*() const {
2928 DCHECK(inner_.Current() != nullptr);
2929 return inner_.Current();
2930 }
2931
2932 HSTLInstructionIterator<InnerIter>& operator++() {
2933 DCHECK(*this != HSTLInstructionIterator<InnerIter>::EndIter());
2934 inner_.Advance();
2935 return *this;
2936 }
2937
2938 HSTLInstructionIterator<InnerIter> operator++(int) {
2939 HSTLInstructionIterator<InnerIter> prev(*this);
2940 ++(*this);
2941 return prev;
2942 }
2943
2944 bool operator==(const HSTLInstructionIterator<InnerIter>& other) const {
2945 return inner_.Current() == other.inner_.Current();
2946 }
2947
2948 bool operator!=(const HSTLInstructionIterator<InnerIter>& other) const {
2949 return !(*this == other);
2950 }
2951
2952 static HSTLInstructionIterator<InnerIter> EndIter() {
2953 return HSTLInstructionIterator<InnerIter>(InnerIter());
2954 }
2955
2956 private:
2957 InnerIter inner_;
2958};
2959
2960template <typename InnerIter>
2961IterationRange<HSTLInstructionIterator<InnerIter>> MakeSTLInstructionIteratorRange(InnerIter iter) {
2962 return MakeIterationRange(HSTLInstructionIterator<InnerIter>(iter),
2963 HSTLInstructionIterator<InnerIter>::EndIter());
2964}
2965
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002966class HVariableInputSizeInstruction : public HInstruction {
2967 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08002968 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002969 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() override {
Mingyao Yangb0b051a2016-11-17 09:04:53 -08002970 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
2971 }
2972
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002973 void AddInput(HInstruction* input);
2974 void InsertInputAt(size_t index, HInstruction* input);
2975 void RemoveInputAt(size_t index);
2976
Igor Murashkind01745e2017-04-05 16:40:31 -07002977 // Removes all the inputs.
2978 // Also removes this instructions from each input's use list
2979 // (for non-environment uses only).
2980 void RemoveAllInputs();
2981
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002982 protected:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302983 HVariableInputSizeInstruction(InstructionKind inst_kind,
2984 SideEffects side_effects,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002985 uint32_t dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002986 ArenaAllocator* allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002987 size_t number_of_inputs,
2988 ArenaAllocKind kind)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302989 : HInstruction(inst_kind, side_effects, dex_pc),
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002990 inputs_(number_of_inputs, allocator->Adapter(kind)) {}
Vladimir Markobd785672018-05-03 17:09:09 +01002991 HVariableInputSizeInstruction(InstructionKind inst_kind,
2992 DataType::Type type,
2993 SideEffects side_effects,
2994 uint32_t dex_pc,
2995 ArenaAllocator* allocator,
2996 size_t number_of_inputs,
2997 ArenaAllocKind kind)
2998 : HInstruction(inst_kind, type, side_effects, dex_pc),
2999 inputs_(number_of_inputs, allocator->Adapter(kind)) {}
Mingyao Yanga9dbe832016-12-15 12:02:53 -08003000
Artem Serovcced8ba2017-07-19 18:18:09 +01003001 DEFAULT_COPY_CONSTRUCTOR(VariableInputSizeInstruction);
Mingyao Yanga9dbe832016-12-15 12:02:53 -08003002
Artem Serovcced8ba2017-07-19 18:18:09 +01003003 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Mingyao Yanga9dbe832016-12-15 12:02:53 -08003004};
3005
Vladimir Markof9f64412015-09-02 14:05:49 +01003006template<size_t N>
Vladimir Markobd785672018-05-03 17:09:09 +01003007class HExpression : public HInstruction {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003008 public:
Vladimir Markobd785672018-05-03 17:09:09 +01003009 HExpression<N>(InstructionKind kind, SideEffects side_effects, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303010 : HInstruction(kind, side_effects, dex_pc), inputs_() {}
Vladimir Markobd785672018-05-03 17:09:09 +01003011 HExpression<N>(InstructionKind kind,
3012 DataType::Type type,
3013 SideEffects side_effects,
3014 uint32_t dex_pc)
3015 : HInstruction(kind, type, side_effects, dex_pc), inputs_() {}
3016 virtual ~HExpression() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003017
Vladimir Marko372f10e2016-05-17 16:30:10 +01003018 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01003019 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Vladimir Marko372f10e2016-05-17 16:30:10 +01003020 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003021 }
3022
Artem Serovcced8ba2017-07-19 18:18:09 +01003023 protected:
Vladimir Markobd785672018-05-03 17:09:09 +01003024 DEFAULT_COPY_CONSTRUCTOR(Expression<N>);
Artem Serovcced8ba2017-07-19 18:18:09 +01003025
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003026 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01003027 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003028
3029 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003030};
3031
Vladimir Markobd785672018-05-03 17:09:09 +01003032// HExpression specialization for N=0.
Vladimir Markof9f64412015-09-02 14:05:49 +01003033template<>
Vladimir Markobd785672018-05-03 17:09:09 +01003034class HExpression<0> : public HInstruction {
Vladimir Markof9f64412015-09-02 14:05:49 +01003035 public:
Vladimir Markobd785672018-05-03 17:09:09 +01003036 using HInstruction::HInstruction;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003037
Vladimir Markobd785672018-05-03 17:09:09 +01003038 virtual ~HExpression() {}
Vladimir Markof9f64412015-09-02 14:05:49 +01003039
Vladimir Marko372f10e2016-05-17 16:30:10 +01003040 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01003041 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Vladimir Marko372f10e2016-05-17 16:30:10 +01003042 return ArrayRef<HUserRecord<HInstruction*>>();
Vladimir Markof9f64412015-09-02 14:05:49 +01003043 }
3044
Artem Serovcced8ba2017-07-19 18:18:09 +01003045 protected:
Vladimir Markobd785672018-05-03 17:09:09 +01003046 DEFAULT_COPY_CONSTRUCTOR(Expression<0>);
Artem Serovcced8ba2017-07-19 18:18:09 +01003047
Vladimir Markof9f64412015-09-02 14:05:49 +01003048 private:
3049 friend class SsaBuilder;
3050};
3051
Mythri Alle5097f832021-11-02 14:52:30 +00003052class HMethodEntryHook : public HExpression<0> {
3053 public:
3054 explicit HMethodEntryHook(uint32_t dex_pc)
3055 : HExpression(kMethodEntryHook, SideEffects::All(), dex_pc) {}
3056
3057 bool NeedsEnvironment() const override {
3058 return true;
3059 }
3060
Mythri Alle9c970b92021-12-02 15:26:31 +00003061 bool CanThrow() const override { return true; }
3062
Mythri Alle5097f832021-11-02 14:52:30 +00003063 DECLARE_INSTRUCTION(MethodEntryHook);
3064
3065 protected:
3066 DEFAULT_COPY_CONSTRUCTOR(MethodEntryHook);
3067};
3068
3069class HMethodExitHook : public HExpression<1> {
3070 public:
3071 HMethodExitHook(HInstruction* value, uint32_t dex_pc)
3072 : HExpression(kMethodExitHook, SideEffects::All(), dex_pc) {
3073 SetRawInputAt(0, value);
3074 }
3075
3076 bool NeedsEnvironment() const override {
3077 return true;
3078 }
3079
Mythri Alle9c970b92021-12-02 15:26:31 +00003080 bool CanThrow() const override { return true; }
3081
Mythri Alle5097f832021-11-02 14:52:30 +00003082 DECLARE_INSTRUCTION(MethodExitHook);
3083
3084 protected:
3085 DEFAULT_COPY_CONSTRUCTOR(MethodExitHook);
3086};
3087
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003088// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
3089// instruction that branches to the exit block.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003090class HReturnVoid final : public HExpression<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003091 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003092 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01003093 : HExpression(kReturnVoid, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303094 }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01003095
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003096 bool IsControlFlow() const override { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003097
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003098 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003099
Artem Serovcced8ba2017-07-19 18:18:09 +01003100 protected:
3101 DEFAULT_COPY_CONSTRUCTOR(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003102};
3103
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003104// Represents dex's RETURN opcodes. A HReturn is a control flow
3105// instruction that branches to the exit block.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003106class HReturn final : public HExpression<1> {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003107 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003108 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01003109 : HExpression(kReturn, SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003110 SetRawInputAt(0, value);
3111 }
3112
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003113 bool IsControlFlow() const override { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01003114
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003115 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003116
Artem Serovcced8ba2017-07-19 18:18:09 +01003117 protected:
3118 DEFAULT_COPY_CONSTRUCTOR(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003119};
3120
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003121class HPhi final : public HVariableInputSizeInstruction {
David Brazdildee58d62016-04-07 09:54:26 +00003122 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003123 HPhi(ArenaAllocator* allocator,
David Brazdildee58d62016-04-07 09:54:26 +00003124 uint32_t reg_number,
3125 size_t number_of_inputs,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003126 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +00003127 uint32_t dex_pc = kNoDexPc)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08003128 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303129 kPhi,
Vladimir Markobd785672018-05-03 17:09:09 +01003130 ToPhiType(type),
Mingyao Yanga9dbe832016-12-15 12:02:53 -08003131 SideEffects::None(),
3132 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003133 allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08003134 number_of_inputs,
3135 kArenaAllocPhiInputs),
David Brazdildee58d62016-04-07 09:54:26 +00003136 reg_number_(reg_number) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003137 DCHECK_NE(GetType(), DataType::Type::kVoid);
David Brazdildee58d62016-04-07 09:54:26 +00003138 // Phis are constructed live and marked dead if conflicting or unused.
3139 // Individual steps of SsaBuilder should assume that if a phi has been
3140 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
3141 SetPackedFlag<kFlagIsLive>(true);
3142 SetPackedFlag<kFlagCanBeNull>(true);
3143 }
3144
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003145 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01003146
David Brazdildee58d62016-04-07 09:54:26 +00003147 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003148 static DataType::Type ToPhiType(DataType::Type type) {
3149 return DataType::Kind(type);
David Brazdildee58d62016-04-07 09:54:26 +00003150 }
3151
3152 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
3153
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003154 void SetType(DataType::Type new_type) {
David Brazdildee58d62016-04-07 09:54:26 +00003155 // Make sure that only valid type changes occur. The following are allowed:
3156 // (1) int -> float/ref (primitive type propagation),
3157 // (2) long -> double (primitive type propagation).
3158 DCHECK(GetType() == new_type ||
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003159 (GetType() == DataType::Type::kInt32 && new_type == DataType::Type::kFloat32) ||
3160 (GetType() == DataType::Type::kInt32 && new_type == DataType::Type::kReference) ||
3161 (GetType() == DataType::Type::kInt64 && new_type == DataType::Type::kFloat64));
David Brazdildee58d62016-04-07 09:54:26 +00003162 SetPackedField<TypeField>(new_type);
3163 }
3164
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003165 bool CanBeNull() const override { return GetPackedFlag<kFlagCanBeNull>(); }
David Brazdildee58d62016-04-07 09:54:26 +00003166 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
3167
3168 uint32_t GetRegNumber() const { return reg_number_; }
3169
3170 void SetDead() { SetPackedFlag<kFlagIsLive>(false); }
3171 void SetLive() { SetPackedFlag<kFlagIsLive>(true); }
3172 bool IsDead() const { return !IsLive(); }
3173 bool IsLive() const { return GetPackedFlag<kFlagIsLive>(); }
3174
Vladimir Markoe9004912016-06-16 16:50:52 +01003175 bool IsVRegEquivalentOf(const HInstruction* other) const {
David Brazdildee58d62016-04-07 09:54:26 +00003176 return other != nullptr
3177 && other->IsPhi()
Vladimir Marko5150dbe2023-04-26 09:13:59 +00003178 && other->GetBlock() == GetBlock()
Vladimir Markocde64972023-04-25 16:40:06 +00003179 && other->AsPhi()->GetRegNumber() == GetRegNumber();
David Brazdildee58d62016-04-07 09:54:26 +00003180 }
3181
Nicolas Geoffrayf57c1ae2017-06-28 17:40:18 +01003182 bool HasEquivalentPhi() const {
Vladimir Markocde64972023-04-25 16:40:06 +00003183 if (GetPrevious() != nullptr && GetPrevious()->AsPhi()->GetRegNumber() == GetRegNumber()) {
Nicolas Geoffrayf57c1ae2017-06-28 17:40:18 +01003184 return true;
3185 }
Vladimir Markocde64972023-04-25 16:40:06 +00003186 if (GetNext() != nullptr && GetNext()->AsPhi()->GetRegNumber() == GetRegNumber()) {
Nicolas Geoffrayf57c1ae2017-06-28 17:40:18 +01003187 return true;
3188 }
3189 return false;
3190 }
3191
David Brazdildee58d62016-04-07 09:54:26 +00003192 // Returns the next equivalent phi (starting from the current one) or null if there is none.
3193 // An equivalent phi is a phi having the same dex register and type.
3194 // It assumes that phis with the same dex register are adjacent.
3195 HPhi* GetNextEquivalentPhiWithSameType() {
3196 HInstruction* next = GetNext();
Vladimir Markocde64972023-04-25 16:40:06 +00003197 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
David Brazdildee58d62016-04-07 09:54:26 +00003198 if (next->GetType() == GetType()) {
Vladimir Markocde64972023-04-25 16:40:06 +00003199 return next->AsPhi();
David Brazdildee58d62016-04-07 09:54:26 +00003200 }
3201 next = next->GetNext();
3202 }
3203 return nullptr;
3204 }
3205
3206 DECLARE_INSTRUCTION(Phi);
3207
Artem Serovcced8ba2017-07-19 18:18:09 +01003208 protected:
3209 DEFAULT_COPY_CONSTRUCTOR(Phi);
3210
David Brazdildee58d62016-04-07 09:54:26 +00003211 private:
Vladimir Markobd785672018-05-03 17:09:09 +01003212 static constexpr size_t kFlagIsLive = HInstruction::kNumberOfGenericPackedBits;
David Brazdildee58d62016-04-07 09:54:26 +00003213 static constexpr size_t kFlagCanBeNull = kFlagIsLive + 1;
3214 static constexpr size_t kNumberOfPhiPackedBits = kFlagCanBeNull + 1;
3215 static_assert(kNumberOfPhiPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
David Brazdildee58d62016-04-07 09:54:26 +00003216
David Brazdildee58d62016-04-07 09:54:26 +00003217 const uint32_t reg_number_;
David Brazdildee58d62016-04-07 09:54:26 +00003218};
3219
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003220// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003221// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003222// exit block.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003223class HExit final : public HExpression<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003224 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303225 explicit HExit(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01003226 : HExpression(kExit, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303227 }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01003228
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003229 bool IsControlFlow() const override { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003230
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003231 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003232
Artem Serovcced8ba2017-07-19 18:18:09 +01003233 protected:
3234 DEFAULT_COPY_CONSTRUCTOR(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003235};
3236
3237// Jumps from one block to another.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003238class HGoto final : public HExpression<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003239 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303240 explicit HGoto(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01003241 : HExpression(kGoto, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303242 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003243
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003244 bool IsClonable() const override { return true; }
3245 bool IsControlFlow() const override { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003246
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00003247 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00003248 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00003249 }
3250
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003251 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003252
Artem Serovcced8ba2017-07-19 18:18:09 +01003253 protected:
3254 DEFAULT_COPY_CONSTRUCTOR(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003255};
3256
Roland Levillain9867bc72015-08-05 10:21:34 +01003257class HConstant : public HExpression<0> {
3258 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303259 explicit HConstant(InstructionKind kind, DataType::Type type, uint32_t dex_pc = kNoDexPc)
3260 : HExpression(kind, type, SideEffects::None(), dex_pc) {
3261 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003262
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003263 bool CanBeMoved() const override { return true; }
Roland Levillain9867bc72015-08-05 10:21:34 +01003264
Roland Levillain1a653882016-03-18 18:05:57 +00003265 // Is this constant -1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01003266 virtual bool IsMinusOne() const { return false; }
Roland Levillain1a653882016-03-18 18:05:57 +00003267 // Is this constant 0 in the arithmetic sense?
3268 virtual bool IsArithmeticZero() const { return false; }
3269 // Is this constant a 0-bit pattern?
3270 virtual bool IsZeroBitPattern() const { return false; }
3271 // Is this constant 1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01003272 virtual bool IsOne() const { return false; }
3273
David Brazdil77a48ae2015-09-15 12:34:04 +00003274 virtual uint64_t GetValueAsUint64() const = 0;
3275
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003276 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01003277
Artem Serovcced8ba2017-07-19 18:18:09 +01003278 protected:
3279 DEFAULT_COPY_CONSTRUCTOR(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01003280};
3281
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003282class HNullConstant final : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01003283 public:
Stefano Cianciulli78f3c722023-05-16 10:32:54 +00003284 bool InstructionDataEquals([[maybe_unused]] const HInstruction* other) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01003285 return true;
3286 }
3287
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003288 uint64_t GetValueAsUint64() const override { return 0; }
David Brazdil77a48ae2015-09-15 12:34:04 +00003289
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003290 size_t ComputeHashCode() const override { return 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01003291
Roland Levillain1a653882016-03-18 18:05:57 +00003292 // The null constant representation is a 0-bit pattern.
Yi Kong39402542019-03-24 02:47:16 -07003293 bool IsZeroBitPattern() const override { return true; }
Roland Levillain1a653882016-03-18 18:05:57 +00003294
Roland Levillain9867bc72015-08-05 10:21:34 +01003295 DECLARE_INSTRUCTION(NullConstant);
3296
Artem Serovcced8ba2017-07-19 18:18:09 +01003297 protected:
3298 DEFAULT_COPY_CONSTRUCTOR(NullConstant);
3299
Roland Levillain9867bc72015-08-05 10:21:34 +01003300 private:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003301 explicit HNullConstant(uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303302 : HConstant(kNullConstant, DataType::Type::kReference, dex_pc) {
3303 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003304
3305 friend class HGraph;
Roland Levillain9867bc72015-08-05 10:21:34 +01003306};
3307
3308// Constants of the type int. Those can be from Dex instructions, or
3309// synthesized (for example with the if-eqz instruction).
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003310class HIntConstant final : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01003311 public:
3312 int32_t GetValue() const { return value_; }
3313
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003314 uint64_t GetValueAsUint64() const override {
David Brazdil9f389d42015-10-01 14:32:56 +01003315 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
3316 }
David Brazdil77a48ae2015-09-15 12:34:04 +00003317
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003318 bool InstructionDataEquals(const HInstruction* other) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003319 DCHECK(other->IsIntConstant()) << other->DebugName();
Vladimir Markocde64972023-04-25 16:40:06 +00003320 return other->AsIntConstant()->value_ == value_;
Roland Levillain9867bc72015-08-05 10:21:34 +01003321 }
3322
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003323 size_t ComputeHashCode() const override { return GetValue(); }
Roland Levillain9867bc72015-08-05 10:21:34 +01003324
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003325 bool IsMinusOne() const override { return GetValue() == -1; }
3326 bool IsArithmeticZero() const override { return GetValue() == 0; }
3327 bool IsZeroBitPattern() const override { return GetValue() == 0; }
3328 bool IsOne() const override { return GetValue() == 1; }
Roland Levillain9867bc72015-08-05 10:21:34 +01003329
Roland Levillain1a653882016-03-18 18:05:57 +00003330 // Integer constants are used to encode Boolean values as well,
3331 // where 1 means true and 0 means false.
3332 bool IsTrue() const { return GetValue() == 1; }
3333 bool IsFalse() const { return GetValue() == 0; }
3334
Roland Levillain9867bc72015-08-05 10:21:34 +01003335 DECLARE_INSTRUCTION(IntConstant);
3336
Artem Serovcced8ba2017-07-19 18:18:09 +01003337 protected:
3338 DEFAULT_COPY_CONSTRUCTOR(IntConstant);
3339
Roland Levillain9867bc72015-08-05 10:21:34 +01003340 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003341 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303342 : HConstant(kIntConstant, DataType::Type::kInt32, dex_pc), value_(value) {
3343 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003344 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303345 : HConstant(kIntConstant, DataType::Type::kInt32, dex_pc),
3346 value_(value ? 1 : 0) {
3347 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003348
3349 const int32_t value_;
3350
3351 friend class HGraph;
3352 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
3353 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
Roland Levillain9867bc72015-08-05 10:21:34 +01003354};
3355
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003356class HLongConstant final : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01003357 public:
3358 int64_t GetValue() const { return value_; }
3359
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003360 uint64_t GetValueAsUint64() const override { return value_; }
David Brazdil77a48ae2015-09-15 12:34:04 +00003361
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003362 bool InstructionDataEquals(const HInstruction* other) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003363 DCHECK(other->IsLongConstant()) << other->DebugName();
Vladimir Markocde64972023-04-25 16:40:06 +00003364 return other->AsLongConstant()->value_ == value_;
Roland Levillain9867bc72015-08-05 10:21:34 +01003365 }
3366
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003367 size_t ComputeHashCode() const override { return static_cast<size_t>(GetValue()); }
Roland Levillain9867bc72015-08-05 10:21:34 +01003368
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003369 bool IsMinusOne() const override { return GetValue() == -1; }
3370 bool IsArithmeticZero() const override { return GetValue() == 0; }
3371 bool IsZeroBitPattern() const override { return GetValue() == 0; }
3372 bool IsOne() const override { return GetValue() == 1; }
Roland Levillain9867bc72015-08-05 10:21:34 +01003373
3374 DECLARE_INSTRUCTION(LongConstant);
3375
Artem Serovcced8ba2017-07-19 18:18:09 +01003376 protected:
3377 DEFAULT_COPY_CONSTRUCTOR(LongConstant);
3378
Roland Levillain9867bc72015-08-05 10:21:34 +01003379 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003380 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303381 : HConstant(kLongConstant, DataType::Type::kInt64, dex_pc),
3382 value_(value) {
3383 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003384
3385 const int64_t value_;
3386
3387 friend class HGraph;
Roland Levillain9867bc72015-08-05 10:21:34 +01003388};
Dave Allison20dfc792014-06-16 20:44:29 -07003389
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003390class HFloatConstant final : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003391 public:
3392 float GetValue() const { return value_; }
3393
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003394 uint64_t GetValueAsUint64() const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003395 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
3396 }
3397
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003398 bool InstructionDataEquals(const HInstruction* other) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003399 DCHECK(other->IsFloatConstant()) << other->DebugName();
Vladimir Markocde64972023-04-25 16:40:06 +00003400 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003401 }
3402
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003403 size_t ComputeHashCode() const override { return static_cast<size_t>(GetValue()); }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003404
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003405 bool IsMinusOne() const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003406 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
3407 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003408 bool IsArithmeticZero() const override {
Roland Levillain1a653882016-03-18 18:05:57 +00003409 return std::fpclassify(value_) == FP_ZERO;
3410 }
3411 bool IsArithmeticPositiveZero() const {
3412 return IsArithmeticZero() && !std::signbit(value_);
3413 }
3414 bool IsArithmeticNegativeZero() const {
3415 return IsArithmeticZero() && std::signbit(value_);
3416 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003417 bool IsZeroBitPattern() const override {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00003418 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(0.0f);
Roland Levillain31dd3d62016-02-16 12:21:02 +00003419 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003420 bool IsOne() const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003421 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
3422 }
3423 bool IsNaN() const {
3424 return std::isnan(value_);
3425 }
3426
3427 DECLARE_INSTRUCTION(FloatConstant);
3428
Artem Serovcced8ba2017-07-19 18:18:09 +01003429 protected:
3430 DEFAULT_COPY_CONSTRUCTOR(FloatConstant);
3431
Roland Levillain31dd3d62016-02-16 12:21:02 +00003432 private:
3433 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303434 : HConstant(kFloatConstant, DataType::Type::kFloat32, dex_pc),
3435 value_(value) {
3436 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003437 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303438 : HConstant(kFloatConstant, DataType::Type::kFloat32, dex_pc),
3439 value_(bit_cast<float, int32_t>(value)) {
3440 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003441
3442 const float value_;
3443
3444 // Only the SsaBuilder and HGraph can create floating-point constants.
3445 friend class SsaBuilder;
3446 friend class HGraph;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003447};
3448
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003449class HDoubleConstant final : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003450 public:
3451 double GetValue() const { return value_; }
3452
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003453 uint64_t GetValueAsUint64() const override { return bit_cast<uint64_t, double>(value_); }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003454
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003455 bool InstructionDataEquals(const HInstruction* other) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003456 DCHECK(other->IsDoubleConstant()) << other->DebugName();
Vladimir Markocde64972023-04-25 16:40:06 +00003457 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003458 }
3459
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003460 size_t ComputeHashCode() const override { return static_cast<size_t>(GetValue()); }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003461
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003462 bool IsMinusOne() const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003463 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
3464 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003465 bool IsArithmeticZero() const override {
Roland Levillain1a653882016-03-18 18:05:57 +00003466 return std::fpclassify(value_) == FP_ZERO;
3467 }
3468 bool IsArithmeticPositiveZero() const {
3469 return IsArithmeticZero() && !std::signbit(value_);
3470 }
3471 bool IsArithmeticNegativeZero() const {
3472 return IsArithmeticZero() && std::signbit(value_);
3473 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003474 bool IsZeroBitPattern() const override {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00003475 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((0.0));
Roland Levillain31dd3d62016-02-16 12:21:02 +00003476 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003477 bool IsOne() const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003478 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
3479 }
3480 bool IsNaN() const {
3481 return std::isnan(value_);
3482 }
3483
3484 DECLARE_INSTRUCTION(DoubleConstant);
3485
Artem Serovcced8ba2017-07-19 18:18:09 +01003486 protected:
3487 DEFAULT_COPY_CONSTRUCTOR(DoubleConstant);
3488
Roland Levillain31dd3d62016-02-16 12:21:02 +00003489 private:
3490 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303491 : HConstant(kDoubleConstant, DataType::Type::kFloat64, dex_pc),
3492 value_(value) {
3493 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003494 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303495 : HConstant(kDoubleConstant, DataType::Type::kFloat64, dex_pc),
3496 value_(bit_cast<double, int64_t>(value)) {
3497 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003498
3499 const double value_;
3500
3501 // Only the SsaBuilder and HGraph can create floating-point constants.
3502 friend class SsaBuilder;
3503 friend class HGraph;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003504};
3505
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003506// Conditional branch. A block ending with an HIf instruction must have
3507// two successors.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003508class HIf final : public HExpression<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003509 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003510 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01003511 : HExpression(kIf, SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003512 SetRawInputAt(0, input);
3513 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003514
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003515 bool IsClonable() const override { return true; }
3516 bool IsControlFlow() const override { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003517
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003518 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01003519 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003520 }
3521
3522 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01003523 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003524 }
3525
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003526 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003527
Artem Serovcced8ba2017-07-19 18:18:09 +01003528 protected:
3529 DEFAULT_COPY_CONSTRUCTOR(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003530};
3531
David Brazdilfc6a86a2015-06-26 10:33:45 +00003532
3533// Abstract instruction which marks the beginning and/or end of a try block and
3534// links it to the respective exception handlers. Behaves the same as a Goto in
3535// non-exceptional control flow.
3536// Normal-flow successor is stored at index zero, exception handlers under
3537// higher indices in no particular order.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003538class HTryBoundary final : public HExpression<0> {
David Brazdilfc6a86a2015-06-26 10:33:45 +00003539 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003540 enum class BoundaryKind {
David Brazdil56e1acc2015-06-30 15:41:36 +01003541 kEntry,
3542 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00003543 kLast = kExit
David Brazdil56e1acc2015-06-30 15:41:36 +01003544 };
3545
Artem Serovd1aa7d02018-06-22 11:35:46 +01003546 // SideEffects::CanTriggerGC prevents instructions with SideEffects::DependOnGC to be alive
3547 // across the catch block entering edges as GC might happen during throwing an exception.
3548 // TryBoundary with BoundaryKind::kExit is conservatively used for that as there is no
3549 // HInstruction which a catch block must start from.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003550 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
Artem Serovd1aa7d02018-06-22 11:35:46 +01003551 : HExpression(kTryBoundary,
3552 (kind == BoundaryKind::kExit) ? SideEffects::CanTriggerGC()
3553 : SideEffects::None(),
3554 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003555 SetPackedField<BoundaryKindField>(kind);
3556 }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003557
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003558 bool IsControlFlow() const override { return true; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003559
3560 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01003561 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003562
David Brazdild26a4112015-11-10 11:07:31 +00003563 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
3564 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
3565 }
3566
David Brazdilfc6a86a2015-06-26 10:33:45 +00003567 // Returns whether `handler` is among its exception handlers (non-zero index
3568 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01003569 bool HasExceptionHandler(const HBasicBlock& handler) const {
3570 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00003571 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00003572 }
3573
3574 // If not present already, adds `handler` to its block's list of exception
3575 // handlers.
3576 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01003577 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00003578 GetBlock()->AddSuccessor(handler);
3579 }
3580 }
3581
Vladimir Markoa1de9182016-02-25 11:37:38 +00003582 BoundaryKind GetBoundaryKind() const { return GetPackedField<BoundaryKindField>(); }
3583 bool IsEntry() const { return GetBoundaryKind() == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003584
David Brazdilffee3d32015-07-06 11:48:53 +01003585 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
3586
David Brazdilfc6a86a2015-06-26 10:33:45 +00003587 DECLARE_INSTRUCTION(TryBoundary);
3588
Artem Serovcced8ba2017-07-19 18:18:09 +01003589 protected:
3590 DEFAULT_COPY_CONSTRUCTOR(TryBoundary);
3591
David Brazdilfc6a86a2015-06-26 10:33:45 +00003592 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003593 static constexpr size_t kFieldBoundaryKind = kNumberOfGenericPackedBits;
3594 static constexpr size_t kFieldBoundaryKindSize =
3595 MinimumBitsToStore(static_cast<size_t>(BoundaryKind::kLast));
3596 static constexpr size_t kNumberOfTryBoundaryPackedBits =
3597 kFieldBoundaryKind + kFieldBoundaryKindSize;
3598 static_assert(kNumberOfTryBoundaryPackedBits <= kMaxNumberOfPackedBits,
3599 "Too many packed fields.");
3600 using BoundaryKindField = BitField<BoundaryKind, kFieldBoundaryKind, kFieldBoundaryKindSize>;
David Brazdilfc6a86a2015-06-26 10:33:45 +00003601};
3602
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003603// Deoptimize to interpreter, upon checking a condition.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003604class HDeoptimize final : public HVariableInputSizeInstruction {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003605 public:
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003606 // Use this constructor when the `HDeoptimize` acts as a barrier, where no code can move
3607 // across.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003608 HDeoptimize(ArenaAllocator* allocator,
3609 HInstruction* cond,
3610 DeoptimizationKind kind,
3611 uint32_t dex_pc)
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003612 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303613 kDeoptimize,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003614 SideEffects::All(),
3615 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003616 allocator,
Andreas Gampe3db70682018-12-26 15:12:03 -08003617 /* number_of_inputs= */ 1,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003618 kArenaAllocMisc) {
3619 SetPackedFlag<kFieldCanBeMoved>(false);
3620 SetPackedField<DeoptimizeKindField>(kind);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003621 SetRawInputAt(0, cond);
3622 }
3623
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003624 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01003625
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003626 // Use this constructor when the `HDeoptimize` guards an instruction, and any user
3627 // that relies on the deoptimization to pass should have its input be the `HDeoptimize`
3628 // instead of `guard`.
3629 // We set CanTriggerGC to prevent any intermediate address to be live
3630 // at the point of the `HDeoptimize`.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003631 HDeoptimize(ArenaAllocator* allocator,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003632 HInstruction* cond,
3633 HInstruction* guard,
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003634 DeoptimizationKind kind,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003635 uint32_t dex_pc)
3636 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303637 kDeoptimize,
Vladimir Markobd785672018-05-03 17:09:09 +01003638 guard->GetType(),
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003639 SideEffects::CanTriggerGC(),
3640 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003641 allocator,
Andreas Gampe3db70682018-12-26 15:12:03 -08003642 /* number_of_inputs= */ 2,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003643 kArenaAllocMisc) {
3644 SetPackedFlag<kFieldCanBeMoved>(true);
3645 SetPackedField<DeoptimizeKindField>(kind);
3646 SetRawInputAt(0, cond);
3647 SetRawInputAt(1, guard);
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01003648 }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003649
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003650 bool CanBeMoved() const override { return GetPackedFlag<kFieldCanBeMoved>(); }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003651
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003652 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Marko9dd046d2023-04-25 15:45:29 +00003653 return (other->CanBeMoved() == CanBeMoved()) &&
Vladimir Markocde64972023-04-25 16:40:06 +00003654 (other->AsDeoptimize()->GetDeoptimizationKind() == GetDeoptimizationKind());
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003655 }
3656
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003657 bool NeedsEnvironment() const override { return true; }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003658
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003659 bool CanThrow() const override { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003660
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003661 DeoptimizationKind GetDeoptimizationKind() const { return GetPackedField<DeoptimizeKindField>(); }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003662
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003663 bool GuardsAnInput() const {
3664 return InputCount() == 2;
3665 }
3666
3667 HInstruction* GuardedInput() const {
3668 DCHECK(GuardsAnInput());
3669 return InputAt(1);
3670 }
3671
3672 void RemoveGuard() {
3673 RemoveInputAt(1);
3674 }
3675
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003676 DECLARE_INSTRUCTION(Deoptimize);
3677
Artem Serovcced8ba2017-07-19 18:18:09 +01003678 protected:
3679 DEFAULT_COPY_CONSTRUCTOR(Deoptimize);
3680
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003681 private:
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003682 static constexpr size_t kFieldCanBeMoved = kNumberOfGenericPackedBits;
3683 static constexpr size_t kFieldDeoptimizeKind = kNumberOfGenericPackedBits + 1;
3684 static constexpr size_t kFieldDeoptimizeKindSize =
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003685 MinimumBitsToStore(static_cast<size_t>(DeoptimizationKind::kLast));
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003686 static constexpr size_t kNumberOfDeoptimizePackedBits =
3687 kFieldDeoptimizeKind + kFieldDeoptimizeKindSize;
3688 static_assert(kNumberOfDeoptimizePackedBits <= kMaxNumberOfPackedBits,
3689 "Too many packed fields.");
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003690 using DeoptimizeKindField =
3691 BitField<DeoptimizationKind, kFieldDeoptimizeKind, kFieldDeoptimizeKindSize>;
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003692};
3693
Mingyao Yang063fc772016-08-02 11:02:54 -07003694// Represents a should_deoptimize flag. Currently used for CHA-based devirtualization.
3695// The compiled code checks this flag value in a guard before devirtualized call and
3696// if it's true, starts to do deoptimization.
3697// It has a 4-byte slot on stack.
3698// TODO: allocate a register for this flag.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003699class HShouldDeoptimizeFlag final : public HVariableInputSizeInstruction {
Mingyao Yang063fc772016-08-02 11:02:54 -07003700 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003701 // CHA guards are only optimized in a separate pass and it has no side effects
3702 // with regard to other passes.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003703 HShouldDeoptimizeFlag(ArenaAllocator* allocator, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303704 : HVariableInputSizeInstruction(kShouldDeoptimizeFlag,
Vladimir Markobd785672018-05-03 17:09:09 +01003705 DataType::Type::kInt32,
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303706 SideEffects::None(),
3707 dex_pc,
3708 allocator,
3709 0,
3710 kArenaAllocCHA) {
Mingyao Yang063fc772016-08-02 11:02:54 -07003711 }
3712
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003713 // We do all CHA guard elimination/motion in a single pass, after which there is no
3714 // further guard elimination/motion since a guard might have been used for justification
3715 // of the elimination of another guard. Therefore, we pretend this guard cannot be moved
3716 // to avoid other optimizations trying to move it.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003717 bool CanBeMoved() const override { return false; }
Mingyao Yang063fc772016-08-02 11:02:54 -07003718
3719 DECLARE_INSTRUCTION(ShouldDeoptimizeFlag);
3720
Artem Serovcced8ba2017-07-19 18:18:09 +01003721 protected:
3722 DEFAULT_COPY_CONSTRUCTOR(ShouldDeoptimizeFlag);
Mingyao Yang063fc772016-08-02 11:02:54 -07003723};
3724
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003725// Represents the ArtMethod that was passed as a first argument to
3726// the method. It is used by instructions that depend on it, like
3727// instructions that work with the dex cache.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003728class HCurrentMethod final : public HExpression<0> {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003729 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003730 explicit HCurrentMethod(DataType::Type type, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303731 : HExpression(kCurrentMethod, type, SideEffects::None(), dex_pc) {
3732 }
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003733
3734 DECLARE_INSTRUCTION(CurrentMethod);
3735
Artem Serovcced8ba2017-07-19 18:18:09 +01003736 protected:
3737 DEFAULT_COPY_CONSTRUCTOR(CurrentMethod);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003738};
3739
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003740// Fetches an ArtMethod from the virtual table or the interface method table
3741// of a class.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003742class HClassTableGet final : public HExpression<1> {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003743 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003744 enum class TableKind {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003745 kVTable,
3746 kIMTable,
Vladimir Markoa1de9182016-02-25 11:37:38 +00003747 kLast = kIMTable
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003748 };
3749 HClassTableGet(HInstruction* cls,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003750 DataType::Type type,
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003751 TableKind kind,
3752 size_t index,
3753 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303754 : HExpression(kClassTableGet, type, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003755 index_(index) {
3756 SetPackedField<TableKindField>(kind);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003757 SetRawInputAt(0, cls);
3758 }
3759
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003760 bool IsClonable() const override { return true; }
3761 bool CanBeMoved() const override { return true; }
3762 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Markocde64972023-04-25 16:40:06 +00003763 return other->AsClassTableGet()->GetIndex() == index_ &&
3764 other->AsClassTableGet()->GetPackedFields() == GetPackedFields();
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003765 }
3766
Vladimir Markoa1de9182016-02-25 11:37:38 +00003767 TableKind GetTableKind() const { return GetPackedField<TableKindField>(); }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003768 size_t GetIndex() const { return index_; }
3769
3770 DECLARE_INSTRUCTION(ClassTableGet);
3771
Artem Serovcced8ba2017-07-19 18:18:09 +01003772 protected:
3773 DEFAULT_COPY_CONSTRUCTOR(ClassTableGet);
3774
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003775 private:
Vladimir Markobd785672018-05-03 17:09:09 +01003776 static constexpr size_t kFieldTableKind = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003777 static constexpr size_t kFieldTableKindSize =
3778 MinimumBitsToStore(static_cast<size_t>(TableKind::kLast));
3779 static constexpr size_t kNumberOfClassTableGetPackedBits = kFieldTableKind + kFieldTableKindSize;
3780 static_assert(kNumberOfClassTableGetPackedBits <= kMaxNumberOfPackedBits,
3781 "Too many packed fields.");
Vladimir Marko1adb0de2023-03-03 09:16:24 +00003782 using TableKindField = BitField<TableKind, kFieldTableKind, kFieldTableKindSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003783
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003784 // The index of the ArtMethod in the table.
3785 const size_t index_;
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003786};
3787
Mark Mendellfe57faa2015-09-18 09:26:15 -04003788// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
3789// have one successor for each entry in the switch table, and the final successor
3790// will be the block containing the next Dex opcode.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003791class HPackedSwitch final : public HExpression<1> {
Mark Mendellfe57faa2015-09-18 09:26:15 -04003792 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003793 HPackedSwitch(int32_t start_value,
3794 uint32_t num_entries,
3795 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04003796 uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01003797 : HExpression(kPackedSwitch, SideEffects::None(), dex_pc),
Mark Mendellfe57faa2015-09-18 09:26:15 -04003798 start_value_(start_value),
3799 num_entries_(num_entries) {
3800 SetRawInputAt(0, input);
3801 }
3802
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003803 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01003804
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003805 bool IsControlFlow() const override { return true; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04003806
3807 int32_t GetStartValue() const { return start_value_; }
3808
Vladimir Marko211c2112015-09-24 16:52:33 +01003809 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04003810
3811 HBasicBlock* GetDefaultBlock() const {
3812 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01003813 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04003814 }
3815 DECLARE_INSTRUCTION(PackedSwitch);
3816
Artem Serovcced8ba2017-07-19 18:18:09 +01003817 protected:
3818 DEFAULT_COPY_CONSTRUCTOR(PackedSwitch);
3819
Mark Mendellfe57faa2015-09-18 09:26:15 -04003820 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003821 const int32_t start_value_;
3822 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04003823};
3824
Roland Levillain88cb1752014-10-20 16:36:47 +01003825class HUnaryOperation : public HExpression<1> {
3826 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303827 HUnaryOperation(InstructionKind kind,
3828 DataType::Type result_type,
3829 HInstruction* input,
3830 uint32_t dex_pc = kNoDexPc)
3831 : HExpression(kind, result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01003832 SetRawInputAt(0, input);
3833 }
3834
Artem Serovcced8ba2017-07-19 18:18:09 +01003835 // All of the UnaryOperation instructions are clonable.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003836 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01003837
Roland Levillain88cb1752014-10-20 16:36:47 +01003838 HInstruction* GetInput() const { return InputAt(0); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003839 DataType::Type GetResultType() const { return GetType(); }
Roland Levillain88cb1752014-10-20 16:36:47 +01003840
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003841 bool CanBeMoved() const override { return true; }
Stefano Cianciulli78f3c722023-05-16 10:32:54 +00003842 bool InstructionDataEquals([[maybe_unused]] const HInstruction* other) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003843 return true;
3844 }
Roland Levillain88cb1752014-10-20 16:36:47 +01003845
Roland Levillain31dd3d62016-02-16 12:21:02 +00003846 // Try to statically evaluate `this` and return a HConstant
3847 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003848 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003849 HConstant* TryStaticEvaluation() const;
3850
3851 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003852 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
3853 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003854 virtual HConstant* Evaluate(HFloatConstant* x) const = 0;
3855 virtual HConstant* Evaluate(HDoubleConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01003856
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003857 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01003858
Artem Serovcced8ba2017-07-19 18:18:09 +01003859 protected:
3860 DEFAULT_COPY_CONSTRUCTOR(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01003861};
3862
Dave Allison20dfc792014-06-16 20:44:29 -07003863class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003864 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303865 HBinaryOperation(InstructionKind kind,
3866 DataType::Type result_type,
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003867 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003868 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003869 SideEffects side_effects = SideEffects::None(),
3870 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303871 : HExpression(kind, result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003872 SetRawInputAt(0, left);
3873 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003874 }
3875
Artem Serovcced8ba2017-07-19 18:18:09 +01003876 // All of the BinaryOperation instructions are clonable.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003877 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01003878
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003879 HInstruction* GetLeft() const { return InputAt(0); }
3880 HInstruction* GetRight() const { return InputAt(1); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003881 DataType::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003882
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003883 virtual bool IsCommutative() const { return false; }
3884
3885 // Put constant on the right.
3886 // Returns whether order is changed.
3887 bool OrderInputsWithConstantOnTheRight() {
3888 HInstruction* left = InputAt(0);
3889 HInstruction* right = InputAt(1);
3890 if (left->IsConstant() && !right->IsConstant()) {
3891 ReplaceInput(right, 0);
3892 ReplaceInput(left, 1);
3893 return true;
3894 }
3895 return false;
3896 }
3897
3898 // Order inputs by instruction id, but favor constant on the right side.
3899 // This helps GVN for commutative ops.
3900 void OrderInputs() {
3901 DCHECK(IsCommutative());
3902 HInstruction* left = InputAt(0);
3903 HInstruction* right = InputAt(1);
3904 if (left == right || (!left->IsConstant() && right->IsConstant())) {
3905 return;
3906 }
3907 if (OrderInputsWithConstantOnTheRight()) {
3908 return;
3909 }
3910 // Order according to instruction id.
3911 if (left->GetId() > right->GetId()) {
3912 ReplaceInput(right, 0);
3913 ReplaceInput(left, 1);
3914 }
3915 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003916
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003917 bool CanBeMoved() const override { return true; }
Stefano Cianciulli78f3c722023-05-16 10:32:54 +00003918 bool InstructionDataEquals([[maybe_unused]] const HInstruction* other) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003919 return true;
3920 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003921
Roland Levillain31dd3d62016-02-16 12:21:02 +00003922 // Try to statically evaluate `this` and return a HConstant
3923 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003924 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003925 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01003926
3927 // Apply this operation to `x` and `y`.
Stefano Cianciulli78f3c722023-05-16 10:32:54 +00003928 virtual HConstant* Evaluate([[maybe_unused]] HNullConstant* x,
3929 [[maybe_unused]] HNullConstant* y) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003930 LOG(FATAL) << DebugName() << " is not defined for the (null, null) case.";
3931 UNREACHABLE();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003932 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003933 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
3934 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
Stefano Cianciulli78f3c722023-05-16 10:32:54 +00003935 virtual HConstant* Evaluate([[maybe_unused]] HLongConstant* x,
3936 [[maybe_unused]] HIntConstant* y) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003937 LOG(FATAL) << DebugName() << " is not defined for the (long, int) case.";
3938 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01003939 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003940 virtual HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const = 0;
3941 virtual HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const = 0;
Roland Levillain556c3d12014-09-18 15:25:07 +01003942
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003943 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003944 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003945 HConstant* GetConstantRight() const;
3946
3947 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003948 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003949 HInstruction* GetLeastConstantLeft() const;
3950
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003951 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01003952
Artem Serovcced8ba2017-07-19 18:18:09 +01003953 protected:
3954 DEFAULT_COPY_CONSTRUCTOR(BinaryOperation);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003955};
3956
Mark Mendellc4701932015-04-10 13:18:51 -04003957// The comparison bias applies for floating point operations and indicates how NaN
3958// comparisons are treated:
Vladimir Marko9974e3c2020-06-10 16:27:06 +01003959enum class ComparisonBias { // private marker to avoid generate-operator-out.py from processing.
Mark Mendellc4701932015-04-10 13:18:51 -04003960 kNoBias, // bias is not applicable (i.e. for long operation)
3961 kGtBias, // return 1 for NaN comparisons
3962 kLtBias, // return -1 for NaN comparisons
Vladimir Markoa1de9182016-02-25 11:37:38 +00003963 kLast = kLtBias
Mark Mendellc4701932015-04-10 13:18:51 -04003964};
3965
Vladimir Marko9974e3c2020-06-10 16:27:06 +01003966std::ostream& operator<<(std::ostream& os, ComparisonBias rhs);
Roland Levillain31dd3d62016-02-16 12:21:02 +00003967
Dave Allison20dfc792014-06-16 20:44:29 -07003968class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003969 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303970 HCondition(InstructionKind kind,
3971 HInstruction* first,
3972 HInstruction* second,
3973 uint32_t dex_pc = kNoDexPc)
3974 : HBinaryOperation(kind,
3975 DataType::Type::kBool,
3976 first,
3977 second,
3978 SideEffects::None(),
3979 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003980 SetPackedField<ComparisonBiasField>(ComparisonBias::kNoBias);
3981 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003982
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003983 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003984 // `instruction`, and disregard moves in between.
3985 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003986
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003987 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003988
3989 virtual IfCondition GetCondition() const = 0;
3990
Mark Mendellc4701932015-04-10 13:18:51 -04003991 virtual IfCondition GetOppositeCondition() const = 0;
3992
Vladimir Markoa1de9182016-02-25 11:37:38 +00003993 bool IsGtBias() const { return GetBias() == ComparisonBias::kGtBias; }
Anton Shaminbdd79352016-02-15 12:48:36 +06003994 bool IsLtBias() const { return GetBias() == ComparisonBias::kLtBias; }
3995
Vladimir Markoa1de9182016-02-25 11:37:38 +00003996 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
3997 void SetBias(ComparisonBias bias) { SetPackedField<ComparisonBiasField>(bias); }
Mark Mendellc4701932015-04-10 13:18:51 -04003998
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003999 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Markocde64972023-04-25 16:40:06 +00004000 return GetPackedFields() == other->AsCondition()->GetPackedFields();
Mark Mendellc4701932015-04-10 13:18:51 -04004001 }
4002
Roland Levillain4fa13f62015-07-06 18:11:54 +01004003 bool IsFPConditionTrueIfNaN() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004004 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01004005 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06004006 if (if_cond == kCondNE) {
4007 return true;
4008 } else if (if_cond == kCondEQ) {
4009 return false;
4010 }
4011 return ((if_cond == kCondGT) || (if_cond == kCondGE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01004012 }
4013
4014 bool IsFPConditionFalseIfNaN() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004015 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01004016 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06004017 if (if_cond == kCondEQ) {
4018 return true;
4019 } else if (if_cond == kCondNE) {
4020 return false;
4021 }
4022 return ((if_cond == kCondLT) || (if_cond == kCondLE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01004023 }
4024
Roland Levillain31dd3d62016-02-16 12:21:02 +00004025 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00004026 // Needed if we merge a HCompare into a HCondition.
Vladimir Markobd785672018-05-03 17:09:09 +01004027 static constexpr size_t kFieldComparisonBias = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004028 static constexpr size_t kFieldComparisonBiasSize =
4029 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
4030 static constexpr size_t kNumberOfConditionPackedBits =
4031 kFieldComparisonBias + kFieldComparisonBiasSize;
4032 static_assert(kNumberOfConditionPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
4033 using ComparisonBiasField =
4034 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
4035
Roland Levillain31dd3d62016-02-16 12:21:02 +00004036 template <typename T>
4037 int32_t Compare(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
4038
4039 template <typename T>
4040 int32_t CompareFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004041 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00004042 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
4043 // Handle the bias.
4044 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compare(x, y);
4045 }
4046
4047 // Return an integer constant containing the result of a condition evaluated at compile time.
4048 HIntConstant* MakeConstantCondition(bool value, uint32_t dex_pc) const {
4049 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
4050 }
4051
Artem Serovcced8ba2017-07-19 18:18:09 +01004052 DEFAULT_COPY_CONSTRUCTOR(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07004053};
4054
4055// Instruction to check if two inputs are equal to each other.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004056class HEqual final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07004057 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004058 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304059 : HCondition(kEqual, first, second, dex_pc) {
4060 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01004061
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004062 bool IsCommutative() const override { return true; }
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004063
Stefano Cianciulli78f3c722023-05-16 10:32:54 +00004064 HConstant* Evaluate([[maybe_unused]] HNullConstant* x,
4065 [[maybe_unused]] HNullConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004066 return MakeConstantCondition(true, GetDexPc());
4067 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004068 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004069 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4070 }
4071 // In the following Evaluate methods, a HCompare instruction has
4072 // been merged into this HEqual instruction; evaluate it as
4073 // `Compare(x, y) == 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004074 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004075 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0),
4076 GetDexPc());
4077 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004078 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004079 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
4080 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004081 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004082 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00004083 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004084
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01004085 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00004086
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004087 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07004088 return kCondEQ;
4089 }
4090
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004091 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04004092 return kCondNE;
4093 }
4094
Artem Serovcced8ba2017-07-19 18:18:09 +01004095 protected:
4096 DEFAULT_COPY_CONSTRUCTOR(Equal);
4097
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00004098 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004099 template <typename T> static bool Compute(T x, T y) { return x == y; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00004100};
4101
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004102class HNotEqual final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07004103 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304104 HNotEqual(HInstruction* first, HInstruction* second,
4105 uint32_t dex_pc = kNoDexPc)
4106 : HCondition(kNotEqual, first, second, dex_pc) {
4107 }
Dave Allison20dfc792014-06-16 20:44:29 -07004108
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004109 bool IsCommutative() const override { return true; }
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004110
Stefano Cianciulli78f3c722023-05-16 10:32:54 +00004111 HConstant* Evaluate([[maybe_unused]] HNullConstant* x,
4112 [[maybe_unused]] HNullConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004113 return MakeConstantCondition(false, GetDexPc());
4114 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004115 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004116 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4117 }
4118 // In the following Evaluate methods, a HCompare instruction has
4119 // been merged into this HNotEqual instruction; evaluate it as
4120 // `Compare(x, y) != 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004121 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004122 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
4123 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004124 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004125 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
4126 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004127 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004128 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00004129 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004130
Dave Allison20dfc792014-06-16 20:44:29 -07004131 DECLARE_INSTRUCTION(NotEqual);
4132
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004133 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07004134 return kCondNE;
4135 }
4136
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004137 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04004138 return kCondEQ;
4139 }
4140
Artem Serovcced8ba2017-07-19 18:18:09 +01004141 protected:
4142 DEFAULT_COPY_CONSTRUCTOR(NotEqual);
4143
Dave Allison20dfc792014-06-16 20:44:29 -07004144 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004145 template <typename T> static bool Compute(T x, T y) { return x != y; }
Dave Allison20dfc792014-06-16 20:44:29 -07004146};
4147
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004148class HLessThan final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07004149 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304150 HLessThan(HInstruction* first, HInstruction* second,
4151 uint32_t dex_pc = kNoDexPc)
4152 : HCondition(kLessThan, first, second, dex_pc) {
4153 }
Dave Allison20dfc792014-06-16 20:44:29 -07004154
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004155 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004156 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004157 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004158 // In the following Evaluate methods, a HCompare instruction has
4159 // been merged into this HLessThan instruction; evaluate it as
4160 // `Compare(x, y) < 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004161 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004162 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
4163 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004164 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004165 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
4166 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004167 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004168 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004169 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004170
Dave Allison20dfc792014-06-16 20:44:29 -07004171 DECLARE_INSTRUCTION(LessThan);
4172
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004173 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07004174 return kCondLT;
4175 }
4176
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004177 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04004178 return kCondGE;
4179 }
4180
Artem Serovcced8ba2017-07-19 18:18:09 +01004181 protected:
4182 DEFAULT_COPY_CONSTRUCTOR(LessThan);
4183
Dave Allison20dfc792014-06-16 20:44:29 -07004184 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004185 template <typename T> static bool Compute(T x, T y) { return x < y; }
Dave Allison20dfc792014-06-16 20:44:29 -07004186};
4187
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004188class HLessThanOrEqual final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07004189 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304190 HLessThanOrEqual(HInstruction* first, HInstruction* second,
4191 uint32_t dex_pc = kNoDexPc)
4192 : HCondition(kLessThanOrEqual, first, second, dex_pc) {
4193 }
Dave Allison20dfc792014-06-16 20:44:29 -07004194
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004195 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004196 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004197 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004198 // In the following Evaluate methods, a HCompare instruction has
4199 // been merged into this HLessThanOrEqual instruction; evaluate it as
4200 // `Compare(x, y) <= 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004201 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004202 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
4203 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004204 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004205 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
4206 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004207 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004208 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004209 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004210
Dave Allison20dfc792014-06-16 20:44:29 -07004211 DECLARE_INSTRUCTION(LessThanOrEqual);
4212
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004213 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07004214 return kCondLE;
4215 }
4216
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004217 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04004218 return kCondGT;
4219 }
4220
Artem Serovcced8ba2017-07-19 18:18:09 +01004221 protected:
4222 DEFAULT_COPY_CONSTRUCTOR(LessThanOrEqual);
4223
Dave Allison20dfc792014-06-16 20:44:29 -07004224 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004225 template <typename T> static bool Compute(T x, T y) { return x <= y; }
Dave Allison20dfc792014-06-16 20:44:29 -07004226};
4227
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004228class HGreaterThan final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07004229 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004230 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304231 : HCondition(kGreaterThan, first, second, dex_pc) {
4232 }
Dave Allison20dfc792014-06-16 20:44:29 -07004233
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004234 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004235 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004236 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004237 // In the following Evaluate methods, a HCompare instruction has
4238 // been merged into this HGreaterThan instruction; evaluate it as
4239 // `Compare(x, y) > 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004240 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004241 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
4242 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004243 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004244 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
4245 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004246 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004247 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004248 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004249
Dave Allison20dfc792014-06-16 20:44:29 -07004250 DECLARE_INSTRUCTION(GreaterThan);
4251
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004252 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07004253 return kCondGT;
4254 }
4255
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004256 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04004257 return kCondLE;
4258 }
4259
Artem Serovcced8ba2017-07-19 18:18:09 +01004260 protected:
4261 DEFAULT_COPY_CONSTRUCTOR(GreaterThan);
4262
Dave Allison20dfc792014-06-16 20:44:29 -07004263 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004264 template <typename T> static bool Compute(T x, T y) { return x > y; }
Dave Allison20dfc792014-06-16 20:44:29 -07004265};
4266
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004267class HGreaterThanOrEqual final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07004268 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004269 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304270 : HCondition(kGreaterThanOrEqual, first, second, dex_pc) {
4271 }
Dave Allison20dfc792014-06-16 20:44:29 -07004272
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004273 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004274 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004275 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004276 // In the following Evaluate methods, a HCompare instruction has
4277 // been merged into this HGreaterThanOrEqual instruction; evaluate it as
4278 // `Compare(x, y) >= 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004279 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004280 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
4281 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004282 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004283 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
4284 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004285 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004286 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004287 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004288
Dave Allison20dfc792014-06-16 20:44:29 -07004289 DECLARE_INSTRUCTION(GreaterThanOrEqual);
4290
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004291 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07004292 return kCondGE;
4293 }
4294
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004295 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04004296 return kCondLT;
4297 }
4298
Artem Serovcced8ba2017-07-19 18:18:09 +01004299 protected:
4300 DEFAULT_COPY_CONSTRUCTOR(GreaterThanOrEqual);
4301
Dave Allison20dfc792014-06-16 20:44:29 -07004302 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004303 template <typename T> static bool Compute(T x, T y) { return x >= y; }
Dave Allison20dfc792014-06-16 20:44:29 -07004304};
4305
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004306class HBelow final : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07004307 public:
4308 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304309 : HCondition(kBelow, first, second, dex_pc) {
4310 }
Aart Bike9f37602015-10-09 11:15:55 -07004311
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004312 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004313 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07004314 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004315 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004316 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4317 }
Stefano Cianciulli78f3c722023-05-16 10:32:54 +00004318 HConstant* Evaluate([[maybe_unused]] HFloatConstant* x,
4319 [[maybe_unused]] HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004320 LOG(FATAL) << DebugName() << " is not defined for float values";
4321 UNREACHABLE();
4322 }
Stefano Cianciulli78f3c722023-05-16 10:32:54 +00004323 HConstant* Evaluate([[maybe_unused]] HDoubleConstant* x,
4324 [[maybe_unused]] HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004325 LOG(FATAL) << DebugName() << " is not defined for double values";
4326 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07004327 }
4328
4329 DECLARE_INSTRUCTION(Below);
4330
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004331 IfCondition GetCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004332 return kCondB;
4333 }
4334
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004335 IfCondition GetOppositeCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004336 return kCondAE;
4337 }
4338
Artem Serovcced8ba2017-07-19 18:18:09 +01004339 protected:
4340 DEFAULT_COPY_CONSTRUCTOR(Below);
4341
Aart Bike9f37602015-10-09 11:15:55 -07004342 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004343 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004344 return MakeUnsigned(x) < MakeUnsigned(y);
4345 }
Aart Bike9f37602015-10-09 11:15:55 -07004346};
4347
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004348class HBelowOrEqual final : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07004349 public:
4350 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304351 : HCondition(kBelowOrEqual, first, second, dex_pc) {
4352 }
Aart Bike9f37602015-10-09 11:15:55 -07004353
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004354 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004355 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07004356 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004357 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004358 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4359 }
Stefano Cianciulli78f3c722023-05-16 10:32:54 +00004360 HConstant* Evaluate([[maybe_unused]] HFloatConstant* x,
4361 [[maybe_unused]] HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004362 LOG(FATAL) << DebugName() << " is not defined for float values";
4363 UNREACHABLE();
4364 }
Stefano Cianciulli78f3c722023-05-16 10:32:54 +00004365 HConstant* Evaluate([[maybe_unused]] HDoubleConstant* x,
4366 [[maybe_unused]] HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004367 LOG(FATAL) << DebugName() << " is not defined for double values";
4368 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07004369 }
4370
4371 DECLARE_INSTRUCTION(BelowOrEqual);
4372
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004373 IfCondition GetCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004374 return kCondBE;
4375 }
4376
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004377 IfCondition GetOppositeCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004378 return kCondA;
4379 }
4380
Artem Serovcced8ba2017-07-19 18:18:09 +01004381 protected:
4382 DEFAULT_COPY_CONSTRUCTOR(BelowOrEqual);
4383
Aart Bike9f37602015-10-09 11:15:55 -07004384 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004385 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004386 return MakeUnsigned(x) <= MakeUnsigned(y);
4387 }
Aart Bike9f37602015-10-09 11:15:55 -07004388};
4389
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004390class HAbove final : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07004391 public:
4392 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304393 : HCondition(kAbove, first, second, dex_pc) {
4394 }
Aart Bike9f37602015-10-09 11:15:55 -07004395
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004396 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004397 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07004398 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004399 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004400 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4401 }
Stefano Cianciulli78f3c722023-05-16 10:32:54 +00004402 HConstant* Evaluate([[maybe_unused]] HFloatConstant* x,
4403 [[maybe_unused]] HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004404 LOG(FATAL) << DebugName() << " is not defined for float values";
4405 UNREACHABLE();
4406 }
Stefano Cianciulli78f3c722023-05-16 10:32:54 +00004407 HConstant* Evaluate([[maybe_unused]] HDoubleConstant* x,
4408 [[maybe_unused]] HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004409 LOG(FATAL) << DebugName() << " is not defined for double values";
4410 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07004411 }
4412
4413 DECLARE_INSTRUCTION(Above);
4414
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004415 IfCondition GetCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004416 return kCondA;
4417 }
4418
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004419 IfCondition GetOppositeCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004420 return kCondBE;
4421 }
4422
Artem Serovcced8ba2017-07-19 18:18:09 +01004423 protected:
4424 DEFAULT_COPY_CONSTRUCTOR(Above);
4425
Aart Bike9f37602015-10-09 11:15:55 -07004426 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004427 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004428 return MakeUnsigned(x) > MakeUnsigned(y);
4429 }
Aart Bike9f37602015-10-09 11:15:55 -07004430};
4431
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004432class HAboveOrEqual final : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07004433 public:
4434 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304435 : HCondition(kAboveOrEqual, first, second, dex_pc) {
4436 }
Aart Bike9f37602015-10-09 11:15:55 -07004437
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004438 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004439 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07004440 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004441 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004442 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4443 }
Stefano Cianciulli78f3c722023-05-16 10:32:54 +00004444 HConstant* Evaluate([[maybe_unused]] HFloatConstant* x,
4445 [[maybe_unused]] HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004446 LOG(FATAL) << DebugName() << " is not defined for float values";
4447 UNREACHABLE();
4448 }
Stefano Cianciulli78f3c722023-05-16 10:32:54 +00004449 HConstant* Evaluate([[maybe_unused]] HDoubleConstant* x,
4450 [[maybe_unused]] HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004451 LOG(FATAL) << DebugName() << " is not defined for double values";
4452 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07004453 }
4454
4455 DECLARE_INSTRUCTION(AboveOrEqual);
4456
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004457 IfCondition GetCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004458 return kCondAE;
4459 }
4460
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004461 IfCondition GetOppositeCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004462 return kCondB;
4463 }
4464
Artem Serovcced8ba2017-07-19 18:18:09 +01004465 protected:
4466 DEFAULT_COPY_CONSTRUCTOR(AboveOrEqual);
4467
Aart Bike9f37602015-10-09 11:15:55 -07004468 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004469 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004470 return MakeUnsigned(x) >= MakeUnsigned(y);
4471 }
Aart Bike9f37602015-10-09 11:15:55 -07004472};
Dave Allison20dfc792014-06-16 20:44:29 -07004473
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004474// Instruction to check how two inputs compare to each other.
4475// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004476class HCompare final : public HBinaryOperation {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004477 public:
Roland Levillaina5c4a402016-03-15 15:02:50 +00004478 // Note that `comparison_type` is the type of comparison performed
4479 // between the comparison's inputs, not the type of the instantiated
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004480 // HCompare instruction (which is always DataType::Type::kInt).
4481 HCompare(DataType::Type comparison_type,
Alexey Frunze4dda3372015-06-01 18:31:49 -07004482 HInstruction* first,
4483 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04004484 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07004485 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304486 : HBinaryOperation(kCompare,
4487 DataType::Type::kInt32,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004488 first,
4489 second,
Roland Levillaina5c4a402016-03-15 15:02:50 +00004490 SideEffectsForArchRuntimeCalls(comparison_type),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004491 dex_pc) {
4492 SetPackedField<ComparisonBiasField>(bias);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004493 }
4494
Roland Levillain9867bc72015-08-05 10:21:34 +01004495 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004496 int32_t Compute(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
4497
4498 template <typename T>
4499 int32_t ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004500 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00004501 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
4502 // Handle the bias.
4503 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compute(x, y);
4504 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004505
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004506 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004507 // Note that there is no "cmp-int" Dex instruction so we shouldn't
4508 // reach this code path when processing a freshly built HIR
4509 // graph. However HCompare integer instructions can be synthesized
4510 // by the instruction simplifier to implement IntegerCompare and
4511 // IntegerSignum intrinsics, so we have to handle this case.
4512 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004513 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004514 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004515 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4516 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004517 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004518 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4519 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004520 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004521 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01004522 }
4523
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004524 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Markocde64972023-04-25 16:40:06 +00004525 return GetPackedFields() == other->AsCompare()->GetPackedFields();
Calin Juravleddb7df22014-11-25 20:56:51 +00004526 }
4527
Vladimir Markoa1de9182016-02-25 11:37:38 +00004528 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
Mark Mendellc4701932015-04-10 13:18:51 -04004529
Roland Levillain31dd3d62016-02-16 12:21:02 +00004530 // Does this compare instruction have a "gt bias" (vs an "lt bias")?
Vladimir Markoa1de9182016-02-25 11:37:38 +00004531 // Only meaningful for floating-point comparisons.
Roland Levillain31dd3d62016-02-16 12:21:02 +00004532 bool IsGtBias() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004533 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004534 return GetBias() == ComparisonBias::kGtBias;
Roland Levillain31dd3d62016-02-16 12:21:02 +00004535 }
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004536
Stefano Cianciulli78f3c722023-05-16 10:32:54 +00004537 static SideEffects SideEffectsForArchRuntimeCalls([[maybe_unused]] DataType::Type type) {
Roland Levillain1693a1f2016-03-15 14:57:31 +00004538 // Comparisons do not require a runtime call in any back end.
4539 return SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004540 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004541
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004542 DECLARE_INSTRUCTION(Compare);
4543
Roland Levillain31dd3d62016-02-16 12:21:02 +00004544 protected:
Vladimir Markobd785672018-05-03 17:09:09 +01004545 static constexpr size_t kFieldComparisonBias = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004546 static constexpr size_t kFieldComparisonBiasSize =
4547 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
4548 static constexpr size_t kNumberOfComparePackedBits =
4549 kFieldComparisonBias + kFieldComparisonBiasSize;
4550 static_assert(kNumberOfComparePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
4551 using ComparisonBiasField =
4552 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
4553
Roland Levillain31dd3d62016-02-16 12:21:02 +00004554 // Return an integer constant containing the result of a comparison evaluated at compile time.
4555 HIntConstant* MakeConstantComparison(int32_t value, uint32_t dex_pc) const {
4556 DCHECK(value == -1 || value == 0 || value == 1) << value;
4557 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
4558 }
4559
Artem Serovcced8ba2017-07-19 18:18:09 +01004560 DEFAULT_COPY_CONSTRUCTOR(Compare);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004561};
4562
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004563class HNewInstance final : public HExpression<1> {
David Brazdil6de19382016-01-08 17:37:10 +00004564 public:
4565 HNewInstance(HInstruction* cls,
David Brazdil6de19382016-01-08 17:37:10 +00004566 uint32_t dex_pc,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004567 dex::TypeIndex type_index,
David Brazdil6de19382016-01-08 17:37:10 +00004568 const DexFile& dex_file,
David Brazdil6de19382016-01-08 17:37:10 +00004569 bool finalizable,
4570 QuickEntrypointEnum entrypoint)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304571 : HExpression(kNewInstance,
4572 DataType::Type::kReference,
4573 SideEffects::CanTriggerGC(),
4574 dex_pc),
David Brazdil6de19382016-01-08 17:37:10 +00004575 type_index_(type_index),
4576 dex_file_(dex_file),
David Brazdil6de19382016-01-08 17:37:10 +00004577 entrypoint_(entrypoint) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004578 SetPackedFlag<kFlagFinalizable>(finalizable);
Alex Light3a73ffb2021-01-25 14:11:05 +00004579 SetPackedFlag<kFlagPartialMaterialization>(false);
David Brazdil6de19382016-01-08 17:37:10 +00004580 SetRawInputAt(0, cls);
David Brazdil6de19382016-01-08 17:37:10 +00004581 }
4582
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004583 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004584
Alex Light3a73ffb2021-01-25 14:11:05 +00004585 void SetPartialMaterialization() {
4586 SetPackedFlag<kFlagPartialMaterialization>(true);
4587 }
4588
Andreas Gampea5b09a62016-11-17 15:21:22 -08004589 dex::TypeIndex GetTypeIndex() const { return type_index_; }
David Brazdil6de19382016-01-08 17:37:10 +00004590 const DexFile& GetDexFile() const { return dex_file_; }
4591
4592 // Calls runtime so needs an environment.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004593 bool NeedsEnvironment() const override { return true; }
David Brazdil6de19382016-01-08 17:37:10 +00004594
Mingyao Yang062157f2016-03-02 10:15:36 -08004595 // Can throw errors when out-of-memory or if it's not instantiable/accessible.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004596 bool CanThrow() const override { return true; }
Alex Light3a73ffb2021-01-25 14:11:05 +00004597 bool OnlyThrowsAsyncExceptions() const override {
4598 return !IsFinalizable() && !NeedsChecks();
4599 }
Mingyao Yang062157f2016-03-02 10:15:36 -08004600
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004601 bool NeedsChecks() const {
4602 return entrypoint_ == kQuickAllocObjectWithChecks;
4603 }
David Brazdil6de19382016-01-08 17:37:10 +00004604
Vladimir Markoa1de9182016-02-25 11:37:38 +00004605 bool IsFinalizable() const { return GetPackedFlag<kFlagFinalizable>(); }
David Brazdil6de19382016-01-08 17:37:10 +00004606
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004607 bool CanBeNull() const override { return false; }
David Brazdil6de19382016-01-08 17:37:10 +00004608
Alex Light3a73ffb2021-01-25 14:11:05 +00004609 bool IsPartialMaterialization() const {
4610 return GetPackedFlag<kFlagPartialMaterialization>();
4611 }
4612
David Brazdil6de19382016-01-08 17:37:10 +00004613 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
4614
4615 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
4616 entrypoint_ = entrypoint;
4617 }
4618
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004619 HLoadClass* GetLoadClass() const {
4620 HInstruction* input = InputAt(0);
4621 if (input->IsClinitCheck()) {
4622 input = input->InputAt(0);
4623 }
4624 DCHECK(input->IsLoadClass());
Vladimir Markocde64972023-04-25 16:40:06 +00004625 return input->AsLoadClass();
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004626 }
4627
David Brazdil6de19382016-01-08 17:37:10 +00004628 bool IsStringAlloc() const;
4629
4630 DECLARE_INSTRUCTION(NewInstance);
4631
Artem Serovcced8ba2017-07-19 18:18:09 +01004632 protected:
4633 DEFAULT_COPY_CONSTRUCTOR(NewInstance);
4634
David Brazdil6de19382016-01-08 17:37:10 +00004635 private:
Vladimir Markobd785672018-05-03 17:09:09 +01004636 static constexpr size_t kFlagFinalizable = kNumberOfGenericPackedBits;
Alex Light3a73ffb2021-01-25 14:11:05 +00004637 static constexpr size_t kFlagPartialMaterialization = kFlagFinalizable + 1;
4638 static constexpr size_t kNumberOfNewInstancePackedBits = kFlagPartialMaterialization + 1;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004639 static_assert(kNumberOfNewInstancePackedBits <= kMaxNumberOfPackedBits,
4640 "Too many packed fields.");
4641
Andreas Gampea5b09a62016-11-17 15:21:22 -08004642 const dex::TypeIndex type_index_;
David Brazdil6de19382016-01-08 17:37:10 +00004643 const DexFile& dex_file_;
David Brazdil6de19382016-01-08 17:37:10 +00004644 QuickEntrypointEnum entrypoint_;
David Brazdil6de19382016-01-08 17:37:10 +00004645};
4646
Nicolas Geoffray8f2eb252020-11-06 13:39:54 +00004647enum IntrinsicNeedsEnvironment {
4648 kNoEnvironment, // Intrinsic does not require an environment.
4649 kNeedsEnvironment // Intrinsic requires an environment.
agicsaki57b81ec2015-08-11 17:39:37 -07004650};
4651
Aart Bik5d75afe2015-12-14 11:57:01 -08004652enum IntrinsicSideEffects {
4653 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
4654 kReadSideEffects, // Intrinsic may read heap memory.
4655 kWriteSideEffects, // Intrinsic may write heap memory.
4656 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
4657};
4658
4659enum IntrinsicExceptions {
4660 kNoThrow, // Intrinsic does not throw any exceptions.
4661 kCanThrow // Intrinsic may throw exceptions.
4662};
4663
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01004664// Determines how to load an ArtMethod*.
4665enum class MethodLoadKind {
4666 // Use a String init ArtMethod* loaded from Thread entrypoints.
4667 kStringInit,
4668
4669 // Use the method's own ArtMethod* loaded by the register allocator.
4670 kRecursive,
4671
4672 // Use PC-relative boot image ArtMethod* address that will be known at link time.
4673 // Used for boot image methods referenced by boot image code.
4674 kBootImageLinkTimePcRelative,
4675
4676 // Load from an entry in the .data.bimg.rel.ro using a PC-relative load.
4677 // Used for app->boot calls with relocatable image.
4678 kBootImageRelRo,
4679
4680 // Load from an entry in the .bss section using a PC-relative load.
4681 // Used for methods outside boot image referenced by AOT-compiled app and boot image code.
4682 kBssEntry,
4683
4684 // Use ArtMethod* at a known address, embed the direct address in the code.
4685 // Used for for JIT-compiled calls.
4686 kJitDirectAddress,
4687
4688 // Make a runtime call to resolve and call the method. This is the last-resort-kind
4689 // used when other kinds are unimplemented on a particular architecture.
4690 kRuntimeCall,
4691};
4692
4693// Determines the location of the code pointer of an invoke.
4694enum class CodePtrLocation {
4695 // Recursive call, use local PC-relative call instruction.
4696 kCallSelf,
4697
4698 // Use native pointer from the Artmethod*.
4699 // Used for @CriticalNative to avoid going through the compiled stub. This call goes through
4700 // a special resolution stub if the class is not initialized or no native code is registered.
4701 kCallCriticalNative,
4702
4703 // Use code pointer from the ArtMethod*.
4704 // Used when we don't know the target code. This is also the last-resort-kind used when
4705 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
4706 kCallArtMethod,
4707};
4708
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01004709static inline bool IsPcRelativeMethodLoadKind(MethodLoadKind load_kind) {
4710 return load_kind == MethodLoadKind::kBootImageLinkTimePcRelative ||
4711 load_kind == MethodLoadKind::kBootImageRelRo ||
4712 load_kind == MethodLoadKind::kBssEntry;
4713}
4714
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004715class HInvoke : public HVariableInputSizeInstruction {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004716 public:
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004717 bool NeedsEnvironment() const override;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004718
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01004719 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004720 SetRawInputAt(index, argument);
4721 }
4722
Roland Levillain3e3d7332015-04-28 11:00:54 +01004723 // Return the number of arguments. This number can be lower than
4724 // the number of inputs returned by InputCount(), as some invoke
4725 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
4726 // inputs at the end of their list of inputs.
4727 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
4728
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004729 InvokeType GetInvokeType() const {
4730 return GetPackedField<InvokeTypeField>();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004731 }
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004732
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01004733 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004734 return intrinsic_;
4735 }
4736
Aart Bik5d75afe2015-12-14 11:57:01 -08004737 void SetIntrinsic(Intrinsics intrinsic,
Nicolas Geoffray8f2eb252020-11-06 13:39:54 +00004738 IntrinsicNeedsEnvironment needs_env,
Aart Bik5d75afe2015-12-14 11:57:01 -08004739 IntrinsicSideEffects side_effects,
4740 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004741
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01004742 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00004743 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01004744 }
4745
Aart Bikff7d89c2016-11-07 08:49:28 -08004746 void SetCanThrow(bool can_throw) { SetPackedFlag<kFlagCanThrow>(can_throw); }
4747
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004748 bool CanThrow() const override { return GetPackedFlag<kFlagCanThrow>(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08004749
Aart Bika8b8e9b2018-01-09 11:01:02 -08004750 void SetAlwaysThrows(bool always_throws) { SetPackedFlag<kFlagAlwaysThrows>(always_throws); }
4751
Santiago Aboy Solanesa3bd09c2022-07-29 14:09:28 +01004752 bool AlwaysThrows() const override final { return GetPackedFlag<kFlagAlwaysThrows>(); }
Aart Bika8b8e9b2018-01-09 11:01:02 -08004753
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004754 bool CanBeMoved() const override { return IsIntrinsic() && !DoesAnyWrite(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08004755
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004756 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Markocde64972023-04-25 16:40:06 +00004757 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
Aart Bik5d75afe2015-12-14 11:57:01 -08004758 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01004759
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004760 uint32_t* GetIntrinsicOptimizations() {
4761 return &intrinsic_optimizations_;
4762 }
4763
4764 const uint32_t* GetIntrinsicOptimizations() const {
4765 return &intrinsic_optimizations_;
4766 }
4767
4768 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
4769
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004770 ArtMethod* GetResolvedMethod() const { return resolved_method_; }
Mythri Alled60aff52023-04-26 12:56:36 +00004771 void SetResolvedMethod(ArtMethod* method, bool enable_intrinsic_opt);
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01004772
4773 MethodReference GetMethodReference() const { return method_reference_; }
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004774
Nicolas Geoffrayb0f405e2020-09-24 15:10:35 +01004775 const MethodReference GetResolvedMethodReference() const {
4776 return resolved_method_reference_;
4777 }
4778
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004779 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004780
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004781 protected:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004782 static constexpr size_t kFieldInvokeType = kNumberOfGenericPackedBits;
4783 static constexpr size_t kFieldInvokeTypeSize =
Vladimir Markoa1de9182016-02-25 11:37:38 +00004784 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
Vladimir Markobd785672018-05-03 17:09:09 +01004785 static constexpr size_t kFlagCanThrow = kFieldInvokeType + kFieldInvokeTypeSize;
Aart Bika8b8e9b2018-01-09 11:01:02 -08004786 static constexpr size_t kFlagAlwaysThrows = kFlagCanThrow + 1;
4787 static constexpr size_t kNumberOfInvokePackedBits = kFlagAlwaysThrows + 1;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004788 static_assert(kNumberOfInvokePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004789 using InvokeTypeField = BitField<InvokeType, kFieldInvokeType, kFieldInvokeTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004790
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304791 HInvoke(InstructionKind kind,
4792 ArenaAllocator* allocator,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004793 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01004794 uint32_t number_of_other_inputs,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004795 DataType::Type return_type,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004796 uint32_t dex_pc,
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01004797 MethodReference method_reference,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004798 ArtMethod* resolved_method,
Nicolas Geoffrayb0f405e2020-09-24 15:10:35 +01004799 MethodReference resolved_method_reference,
Mythri Alled60aff52023-04-26 12:56:36 +00004800 InvokeType invoke_type,
4801 bool enable_intrinsic_opt)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004802 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304803 kind,
Vladimir Markobd785672018-05-03 17:09:09 +01004804 return_type,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004805 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
4806 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004807 allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004808 number_of_arguments + number_of_other_inputs,
4809 kArenaAllocInvokeInputs),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004810 number_of_arguments_(number_of_arguments),
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01004811 method_reference_(method_reference),
Nicolas Geoffrayb0f405e2020-09-24 15:10:35 +01004812 resolved_method_reference_(resolved_method_reference),
agicsaki57b81ec2015-08-11 17:39:37 -07004813 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004814 intrinsic_optimizations_(0) {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004815 SetPackedField<InvokeTypeField>(invoke_type);
Vladimir Markoa1de9182016-02-25 11:37:38 +00004816 SetPackedFlag<kFlagCanThrow>(true);
Mythri Alled60aff52023-04-26 12:56:36 +00004817 SetResolvedMethod(resolved_method, enable_intrinsic_opt);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004818 }
4819
Artem Serovcced8ba2017-07-19 18:18:09 +01004820 DEFAULT_COPY_CONSTRUCTOR(Invoke);
4821
Roland Levillain3e3d7332015-04-28 11:00:54 +01004822 uint32_t number_of_arguments_;
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00004823 ArtMethod* resolved_method_;
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01004824 const MethodReference method_reference_;
Nicolas Geoffrayb0f405e2020-09-24 15:10:35 +01004825 // Cached values of the resolved method, to avoid needing the mutator lock.
4826 const MethodReference resolved_method_reference_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004827 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004828
4829 // A magic word holding optimizations for intrinsics. See intrinsics.h.
4830 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004831};
4832
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004833class HInvokeUnresolved final : public HInvoke {
Calin Juravle175dc732015-08-25 15:42:32 +01004834 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004835 HInvokeUnresolved(ArenaAllocator* allocator,
Calin Juravle175dc732015-08-25 15:42:32 +01004836 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004837 DataType::Type return_type,
Calin Juravle175dc732015-08-25 15:42:32 +01004838 uint32_t dex_pc,
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01004839 MethodReference method_reference,
Calin Juravle175dc732015-08-25 15:42:32 +01004840 InvokeType invoke_type)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304841 : HInvoke(kInvokeUnresolved,
4842 allocator,
Calin Juravle175dc732015-08-25 15:42:32 +01004843 number_of_arguments,
Andreas Gampe3db70682018-12-26 15:12:03 -08004844 /* number_of_other_inputs= */ 0u,
Calin Juravle175dc732015-08-25 15:42:32 +01004845 return_type,
4846 dex_pc,
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01004847 method_reference,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004848 nullptr,
Nicolas Geoffrayb0f405e2020-09-24 15:10:35 +01004849 MethodReference(nullptr, 0u),
Mythri Alled60aff52023-04-26 12:56:36 +00004850 invoke_type,
4851 /* enable_intrinsic_opt= */ false) {
Calin Juravle175dc732015-08-25 15:42:32 +01004852 }
4853
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004854 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004855
Calin Juravle175dc732015-08-25 15:42:32 +01004856 DECLARE_INSTRUCTION(InvokeUnresolved);
4857
Artem Serovcced8ba2017-07-19 18:18:09 +01004858 protected:
4859 DEFAULT_COPY_CONSTRUCTOR(InvokeUnresolved);
Calin Juravle175dc732015-08-25 15:42:32 +01004860};
4861
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004862class HInvokePolymorphic final : public HInvoke {
Orion Hodsonac141392017-01-13 11:53:47 +00004863 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004864 HInvokePolymorphic(ArenaAllocator* allocator,
Orion Hodsonac141392017-01-13 11:53:47 +00004865 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004866 DataType::Type return_type,
Orion Hodsonac141392017-01-13 11:53:47 +00004867 uint32_t dex_pc,
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01004868 MethodReference method_reference,
Andra Danciub931b8c2020-07-27 13:16:37 +00004869 // resolved_method is the ArtMethod object corresponding to the polymorphic
4870 // method (e.g. VarHandle.get), resolved using the class linker. It is needed
4871 // to pass intrinsic information to the HInvokePolymorphic node.
Andra Danciu73c31802020-09-01 13:17:05 +00004872 ArtMethod* resolved_method,
Nicolas Geoffrayb0f405e2020-09-24 15:10:35 +01004873 MethodReference resolved_method_reference,
Mythri Alled60aff52023-04-26 12:56:36 +00004874 dex::ProtoIndex proto_idx,
4875 bool enable_intrinsic_opt)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304876 : HInvoke(kInvokePolymorphic,
4877 allocator,
Orion Hodsonac141392017-01-13 11:53:47 +00004878 number_of_arguments,
Andra Danciu49a19f32020-08-27 12:44:25 +00004879 /* number_of_other_inputs= */ 0u,
Orion Hodsonac141392017-01-13 11:53:47 +00004880 return_type,
4881 dex_pc,
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01004882 method_reference,
Andra Danciua0130e82020-07-23 12:34:56 +00004883 resolved_method,
Nicolas Geoffrayb0f405e2020-09-24 15:10:35 +01004884 resolved_method_reference,
Mythri Alled60aff52023-04-26 12:56:36 +00004885 kPolymorphic,
4886 enable_intrinsic_opt),
Andra Danciu73c31802020-09-01 13:17:05 +00004887 proto_idx_(proto_idx) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304888 }
Orion Hodsonac141392017-01-13 11:53:47 +00004889
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004890 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004891
Andra Danciu73c31802020-09-01 13:17:05 +00004892 dex::ProtoIndex GetProtoIndex() { return proto_idx_; }
4893
Orion Hodsonac141392017-01-13 11:53:47 +00004894 DECLARE_INSTRUCTION(InvokePolymorphic);
4895
Artem Serovcced8ba2017-07-19 18:18:09 +01004896 protected:
Andra Danciu73c31802020-09-01 13:17:05 +00004897 dex::ProtoIndex proto_idx_;
Artem Serovcced8ba2017-07-19 18:18:09 +01004898 DEFAULT_COPY_CONSTRUCTOR(InvokePolymorphic);
Orion Hodsonac141392017-01-13 11:53:47 +00004899};
4900
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004901class HInvokeCustom final : public HInvoke {
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004902 public:
4903 HInvokeCustom(ArenaAllocator* allocator,
4904 uint32_t number_of_arguments,
4905 uint32_t call_site_index,
4906 DataType::Type return_type,
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01004907 uint32_t dex_pc,
Mythri Alled60aff52023-04-26 12:56:36 +00004908 MethodReference method_reference,
4909 bool enable_intrinsic_opt)
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004910 : HInvoke(kInvokeCustom,
4911 allocator,
4912 number_of_arguments,
Andreas Gampe3db70682018-12-26 15:12:03 -08004913 /* number_of_other_inputs= */ 0u,
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004914 return_type,
4915 dex_pc,
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01004916 method_reference,
Andreas Gampe3db70682018-12-26 15:12:03 -08004917 /* resolved_method= */ nullptr,
Nicolas Geoffrayb0f405e2020-09-24 15:10:35 +01004918 MethodReference(nullptr, 0u),
Mythri Alled60aff52023-04-26 12:56:36 +00004919 kStatic,
4920 enable_intrinsic_opt),
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004921 call_site_index_(call_site_index) {
4922 }
4923
4924 uint32_t GetCallSiteIndex() const { return call_site_index_; }
4925
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004926 bool IsClonable() const override { return true; }
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004927
4928 DECLARE_INSTRUCTION(InvokeCustom);
4929
4930 protected:
4931 DEFAULT_COPY_CONSTRUCTOR(InvokeCustom);
4932
4933 private:
4934 uint32_t call_site_index_;
4935};
4936
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004937class HInvokeStaticOrDirect final : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004938 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01004939 // Requirements of this method call regarding the class
4940 // initialization (clinit) check of its declaring class.
Vladimir Marko9974e3c2020-06-10 16:27:06 +01004941 enum class ClinitCheckRequirement { // private marker to avoid generate-operator-out.py from processing.
Roland Levillain4c0eb422015-04-24 16:43:49 +01004942 kNone, // Class already initialized.
4943 kExplicit, // Static call having explicit clinit check as last input.
4944 kImplicit, // Static call implicitly requiring a clinit check.
Vladimir Markoa1de9182016-02-25 11:37:38 +00004945 kLast = kImplicit
Roland Levillain4c0eb422015-04-24 16:43:49 +01004946 };
4947
Vladimir Marko58155012015-08-19 12:49:41 +00004948 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01004949 MethodLoadKind method_load_kind;
4950 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00004951 // The method load data holds
4952 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
4953 // Note that there are multiple string init methods, each having its own offset.
4954 // - the method address for kDirectAddress
Vladimir Markodc151b22015-10-15 18:02:30 +01004955 uint64_t method_load_data;
Vladimir Marko58155012015-08-19 12:49:41 +00004956 };
4957
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004958 HInvokeStaticOrDirect(ArenaAllocator* allocator,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004959 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004960 DataType::Type return_type,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004961 uint32_t dex_pc,
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01004962 MethodReference method_reference,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004963 ArtMethod* resolved_method,
Vladimir Marko58155012015-08-19 12:49:41 +00004964 DispatchInfo dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004965 InvokeType invoke_type,
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01004966 MethodReference resolved_method_reference,
Mythri Alled60aff52023-04-26 12:56:36 +00004967 ClinitCheckRequirement clinit_check_requirement,
4968 bool enable_intrinsic_opt)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304969 : HInvoke(kInvokeStaticOrDirect,
4970 allocator,
Roland Levillain3e3d7332015-04-28 11:00:54 +01004971 number_of_arguments,
Vladimir Marko86c87522020-05-11 16:55:55 +01004972 // There is potentially one extra argument for the HCurrentMethod input,
4973 // and one other if the clinit check is explicit. These can be removed later.
4974 (NeedsCurrentMethodInput(dispatch_info) ? 1u : 0u) +
David Brazdildee58d62016-04-07 09:54:26 +00004975 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004976 return_type,
4977 dex_pc,
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01004978 method_reference,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004979 resolved_method,
Nicolas Geoffrayb0f405e2020-09-24 15:10:35 +01004980 resolved_method_reference,
Mythri Alled60aff52023-04-26 12:56:36 +00004981 invoke_type,
4982 enable_intrinsic_opt),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004983 dispatch_info_(dispatch_info) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004984 SetPackedField<ClinitCheckRequirementField>(clinit_check_requirement);
4985 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004986
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004987 bool IsClonable() const override { return true; }
Santiago Aboy Solanese43aa3f2021-11-01 09:02:09 +00004988 bool NeedsBss() const override {
4989 return GetMethodLoadKind() == MethodLoadKind::kBssEntry;
4990 }
Artem Serovcced8ba2017-07-19 18:18:09 +01004991
Vladimir Marko86c87522020-05-11 16:55:55 +01004992 void SetDispatchInfo(DispatchInfo dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00004993 bool had_current_method_input = HasCurrentMethodInput();
Vladimir Marko86c87522020-05-11 16:55:55 +01004994 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info);
Vladimir Markob554b5a2015-11-06 12:57:55 +00004995
4996 // Using the current method is the default and once we find a better
4997 // method load kind, we should not go back to using the current method.
4998 DCHECK(had_current_method_input || !needs_current_method_input);
4999
5000 if (had_current_method_input && !needs_current_method_input) {
Vladimir Marko86c87522020-05-11 16:55:55 +01005001 DCHECK_EQ(InputAt(GetCurrentMethodIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
5002 RemoveInputAt(GetCurrentMethodIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00005003 }
Vladimir Markodc151b22015-10-15 18:02:30 +01005004 dispatch_info_ = dispatch_info;
5005 }
5006
Aart Bik6daebeb2017-04-03 14:35:41 -07005007 DispatchInfo GetDispatchInfo() const {
5008 return dispatch_info_;
5009 }
5010
Vladimir Marko372f10e2016-05-17 16:30:10 +01005011 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005012 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01005013 ArrayRef<HUserRecord<HInstruction*>> input_records = HInvoke::GetInputRecords();
5014 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck()) {
5015 DCHECK(!input_records.empty());
5016 DCHECK_GT(input_records.size(), GetNumberOfArguments());
5017 HInstruction* last_input = input_records.back().GetInstruction();
5018 // Note: `last_input` may be null during arguments setup.
5019 if (last_input != nullptr) {
5020 // `last_input` is the last input of a static invoke marked as having
5021 // an explicit clinit check. It must either be:
5022 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
5023 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
5024 DCHECK(last_input->IsClinitCheck() || last_input->IsLoadClass()) << last_input->DebugName();
5025 }
5026 }
5027 return input_records;
5028 }
5029
Stefano Cianciulli78f3c722023-05-16 10:32:54 +00005030 bool CanDoImplicitNullCheckOn([[maybe_unused]] HInstruction* obj) const override {
Vladimir Marko86c87522020-05-11 16:55:55 +01005031 // We do not access the method via object reference, so we cannot do an implicit null check.
Calin Juravle77520bc2015-01-12 18:45:46 +00005032 // TODO: for intrinsics we can generate implicit null checks.
5033 return false;
5034 }
5035
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005036 bool CanBeNull() const override {
Vladimir Markobd785672018-05-03 17:09:09 +01005037 return GetType() == DataType::Type::kReference && !IsStringInit();
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07005038 }
5039
Vladimir Marko58155012015-08-19 12:49:41 +00005040 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
Nicolas Geoffray282795c2021-09-24 18:16:41 +01005041 CodePtrLocation GetCodePtrLocation() const {
5042 // We do CHA analysis after sharpening. When a method has CHA inlining, it
5043 // cannot call itself, as if the CHA optmization is invalid we want to make
5044 // sure the method is never executed again. So, while sharpening can return
5045 // kCallSelf, we bypass it here if there is a CHA optimization.
5046 if (dispatch_info_.code_ptr_location == CodePtrLocation::kCallSelf &&
5047 GetBlock()->GetGraph()->HasShouldDeoptimizeFlag()) {
5048 return CodePtrLocation::kCallArtMethod;
5049 } else {
5050 return dispatch_info_.code_ptr_location;
5051 }
5052 }
Vladimir Marko58155012015-08-19 12:49:41 +00005053 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Marko58155012015-08-19 12:49:41 +00005054 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01005055 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kJitDirectAddress; }
Vladimir Marko65979462017-05-19 17:25:12 +01005056 bool HasPcRelativeMethodLoadKind() const {
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01005057 return IsPcRelativeMethodLoadKind(GetMethodLoadKind());
Vladimir Marko65979462017-05-19 17:25:12 +01005058 }
Vladimir Marko58155012015-08-19 12:49:41 +00005059
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005060 QuickEntrypointEnum GetStringInitEntryPoint() const {
Vladimir Marko58155012015-08-19 12:49:41 +00005061 DCHECK(IsStringInit());
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005062 return static_cast<QuickEntrypointEnum>(dispatch_info_.method_load_data);
Vladimir Marko58155012015-08-19 12:49:41 +00005063 }
5064
5065 uint64_t GetMethodAddress() const {
5066 DCHECK(HasMethodAddress());
5067 return dispatch_info_.method_load_data;
5068 }
5069
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00005070 const DexFile& GetDexFileForPcRelativeDexCache() const;
5071
Vladimir Markoa1de9182016-02-25 11:37:38 +00005072 ClinitCheckRequirement GetClinitCheckRequirement() const {
5073 return GetPackedField<ClinitCheckRequirementField>();
5074 }
Calin Juravle68ad6492015-08-18 17:08:12 +01005075
Roland Levillain4c0eb422015-04-24 16:43:49 +01005076 // Is this instruction a call to a static method?
5077 bool IsStatic() const {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01005078 return GetInvokeType() == kStatic;
5079 }
5080
Vladimir Marko86c87522020-05-11 16:55:55 +01005081 // Does this method load kind need the current method as an input?
5082 static bool NeedsCurrentMethodInput(DispatchInfo dispatch_info) {
5083 return dispatch_info.method_load_kind == MethodLoadKind::kRecursive ||
5084 dispatch_info.method_load_kind == MethodLoadKind::kRuntimeCall ||
5085 dispatch_info.code_ptr_location == CodePtrLocation::kCallCriticalNative;
5086 }
5087
5088 // Get the index of the current method input.
5089 size_t GetCurrentMethodIndex() const {
5090 DCHECK(HasCurrentMethodInput());
5091 return GetCurrentMethodIndexUnchecked();
5092 }
5093 size_t GetCurrentMethodIndexUnchecked() const {
5094 return GetNumberOfArguments();
5095 }
5096
5097 // Check if the method has a current method input.
5098 bool HasCurrentMethodInput() const {
5099 if (NeedsCurrentMethodInput(GetDispatchInfo())) {
5100 DCHECK(InputAt(GetCurrentMethodIndexUnchecked()) == nullptr || // During argument setup.
5101 InputAt(GetCurrentMethodIndexUnchecked())->IsCurrentMethod());
5102 return true;
5103 } else {
5104 DCHECK(InputCount() == GetCurrentMethodIndexUnchecked() ||
5105 InputAt(GetCurrentMethodIndexUnchecked()) == nullptr || // During argument setup.
5106 !InputAt(GetCurrentMethodIndexUnchecked())->IsCurrentMethod());
5107 return false;
5108 }
5109 }
5110
5111 // Get the index of the special input.
5112 size_t GetSpecialInputIndex() const {
5113 DCHECK(HasSpecialInput());
5114 return GetSpecialInputIndexUnchecked();
5115 }
5116 size_t GetSpecialInputIndexUnchecked() const {
5117 return GetNumberOfArguments() + (HasCurrentMethodInput() ? 1u : 0u);
5118 }
5119
5120 // Check if the method has a special input.
5121 bool HasSpecialInput() const {
5122 size_t other_inputs =
5123 GetSpecialInputIndexUnchecked() + (IsStaticWithExplicitClinitCheck() ? 1u : 0u);
5124 size_t input_count = InputCount();
5125 DCHECK_LE(input_count - other_inputs, 1u) << other_inputs << " " << input_count;
5126 return other_inputs != input_count;
5127 }
5128
5129 void AddSpecialInput(HInstruction* input) {
5130 // We allow only one special input.
5131 DCHECK(!HasSpecialInput());
5132 InsertInputAt(GetSpecialInputIndexUnchecked(), input);
5133 }
5134
Vladimir Markofbb184a2015-11-13 14:47:00 +00005135 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
5136 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
5137 // instruction; only relevant for static calls with explicit clinit check.
5138 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01005139 DCHECK(IsStaticWithExplicitClinitCheck());
Vladimir Marko372f10e2016-05-17 16:30:10 +01005140 size_t last_input_index = inputs_.size() - 1u;
5141 HInstruction* last_input = inputs_.back().GetInstruction();
Roland Levillain4c0eb422015-04-24 16:43:49 +01005142 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00005143 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01005144 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005145 inputs_.pop_back();
Vladimir Markoa1de9182016-02-25 11:37:38 +00005146 SetPackedField<ClinitCheckRequirementField>(new_requirement);
Vladimir Markofbb184a2015-11-13 14:47:00 +00005147 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01005148 }
5149
5150 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00005151 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01005152 bool IsStaticWithExplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005153 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kExplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01005154 }
5155
5156 // Is this a call to a static method whose declaring class has an
5157 // implicit intialization check requirement?
5158 bool IsStaticWithImplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005159 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kImplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01005160 }
5161
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00005162 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00005163
Artem Serovcced8ba2017-07-19 18:18:09 +01005164 protected:
5165 DEFAULT_COPY_CONSTRUCTOR(InvokeStaticOrDirect);
5166
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00005167 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01005168 static constexpr size_t kFieldClinitCheckRequirement = kNumberOfInvokePackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005169 static constexpr size_t kFieldClinitCheckRequirementSize =
5170 MinimumBitsToStore(static_cast<size_t>(ClinitCheckRequirement::kLast));
5171 static constexpr size_t kNumberOfInvokeStaticOrDirectPackedBits =
5172 kFieldClinitCheckRequirement + kFieldClinitCheckRequirementSize;
5173 static_assert(kNumberOfInvokeStaticOrDirectPackedBits <= kMaxNumberOfPackedBits,
5174 "Too many packed fields.");
Vladimir Markoa1de9182016-02-25 11:37:38 +00005175 using ClinitCheckRequirementField = BitField<ClinitCheckRequirement,
5176 kFieldClinitCheckRequirement,
5177 kFieldClinitCheckRequirementSize>;
5178
Vladimir Marko58155012015-08-19 12:49:41 +00005179 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00005180};
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01005181std::ostream& operator<<(std::ostream& os, MethodLoadKind rhs);
5182std::ostream& operator<<(std::ostream& os, CodePtrLocation rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00005183std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00005184
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005185class HInvokeVirtual final : public HInvoke {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01005186 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01005187 HInvokeVirtual(ArenaAllocator* allocator,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01005188 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005189 DataType::Type return_type,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01005190 uint32_t dex_pc,
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01005191 MethodReference method_reference,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01005192 ArtMethod* resolved_method,
Nicolas Geoffrayb0f405e2020-09-24 15:10:35 +01005193 MethodReference resolved_method_reference,
Mythri Alled60aff52023-04-26 12:56:36 +00005194 uint32_t vtable_index,
5195 bool enable_intrinsic_opt)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305196 : HInvoke(kInvokeVirtual,
5197 allocator,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01005198 number_of_arguments,
5199 0u,
5200 return_type,
5201 dex_pc,
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01005202 method_reference,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01005203 resolved_method,
Nicolas Geoffrayb0f405e2020-09-24 15:10:35 +01005204 resolved_method_reference,
Mythri Alled60aff52023-04-26 12:56:36 +00005205 kVirtual,
5206 enable_intrinsic_opt),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305207 vtable_index_(vtable_index) {
5208 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01005209
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005210 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01005211
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005212 bool CanBeNull() const override {
Aart Bik71bf7b42016-11-16 10:17:46 -08005213 switch (GetIntrinsic()) {
5214 case Intrinsics::kThreadCurrentThread:
5215 case Intrinsics::kStringBufferAppend:
5216 case Intrinsics::kStringBufferToString:
Vladimir Markod4561172017-10-30 17:48:25 +00005217 case Intrinsics::kStringBuilderAppendObject:
5218 case Intrinsics::kStringBuilderAppendString:
5219 case Intrinsics::kStringBuilderAppendCharSequence:
5220 case Intrinsics::kStringBuilderAppendCharArray:
5221 case Intrinsics::kStringBuilderAppendBoolean:
5222 case Intrinsics::kStringBuilderAppendChar:
5223 case Intrinsics::kStringBuilderAppendInt:
5224 case Intrinsics::kStringBuilderAppendLong:
5225 case Intrinsics::kStringBuilderAppendFloat:
5226 case Intrinsics::kStringBuilderAppendDouble:
Aart Bik71bf7b42016-11-16 10:17:46 -08005227 case Intrinsics::kStringBuilderToString:
5228 return false;
5229 default:
5230 return HInvoke::CanBeNull();
5231 }
5232 }
5233
Vladimir Markoac27ac02021-02-01 09:31:02 +00005234 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override;
Calin Juravle77520bc2015-01-12 18:45:46 +00005235
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01005236 uint32_t GetVTableIndex() const { return vtable_index_; }
5237
5238 DECLARE_INSTRUCTION(InvokeVirtual);
5239
Artem Serovcced8ba2017-07-19 18:18:09 +01005240 protected:
5241 DEFAULT_COPY_CONSTRUCTOR(InvokeVirtual);
5242
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01005243 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01005244 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01005245 const uint32_t vtable_index_;
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01005246};
5247
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005248class HInvokeInterface final : public HInvoke {
Nicolas Geoffray52839d12014-11-07 17:47:25 +00005249 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01005250 HInvokeInterface(ArenaAllocator* allocator,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00005251 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005252 DataType::Type return_type,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00005253 uint32_t dex_pc,
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01005254 MethodReference method_reference,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01005255 ArtMethod* resolved_method,
Nicolas Geoffrayb0f405e2020-09-24 15:10:35 +01005256 MethodReference resolved_method_reference,
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01005257 uint32_t imt_index,
Mythri Alled60aff52023-04-26 12:56:36 +00005258 MethodLoadKind load_kind,
5259 bool enable_intrinsic_opt)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305260 : HInvoke(kInvokeInterface,
5261 allocator,
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01005262 number_of_arguments + (NeedsCurrentMethod(load_kind) ? 1 : 0),
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01005263 0u,
5264 return_type,
5265 dex_pc,
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01005266 method_reference,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01005267 resolved_method,
Nicolas Geoffrayb0f405e2020-09-24 15:10:35 +01005268 resolved_method_reference,
Mythri Alled60aff52023-04-26 12:56:36 +00005269 kInterface,
5270 enable_intrinsic_opt),
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01005271 imt_index_(imt_index),
5272 hidden_argument_load_kind_(load_kind) {
5273 }
5274
5275 static bool NeedsCurrentMethod(MethodLoadKind load_kind) {
5276 return load_kind == MethodLoadKind::kRecursive;
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305277 }
Nicolas Geoffray52839d12014-11-07 17:47:25 +00005278
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005279 bool IsClonable() const override { return true; }
Santiago Aboy Solanese43aa3f2021-11-01 09:02:09 +00005280 bool NeedsBss() const override {
5281 return GetHiddenArgumentLoadKind() == MethodLoadKind::kBssEntry;
5282 }
Artem Serovcced8ba2017-07-19 18:18:09 +01005283
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005284 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Calin Juravle77520bc2015-01-12 18:45:46 +00005285 // TODO: Add implicit null checks in intrinsics.
Nicolas Geoffray61ba8d22018-08-07 09:55:57 +01005286 return (obj == InputAt(0)) && !IsIntrinsic();
Calin Juravle77520bc2015-01-12 18:45:46 +00005287 }
5288
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01005289 size_t GetSpecialInputIndex() const {
5290 return GetNumberOfArguments();
5291 }
5292
5293 void AddSpecialInput(HInstruction* input) {
5294 InsertInputAt(GetSpecialInputIndex(), input);
5295 }
5296
Nicolas Geoffray52839d12014-11-07 17:47:25 +00005297 uint32_t GetImtIndex() const { return imt_index_; }
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01005298 MethodLoadKind GetHiddenArgumentLoadKind() const { return hidden_argument_load_kind_; }
Nicolas Geoffray52839d12014-11-07 17:47:25 +00005299
5300 DECLARE_INSTRUCTION(InvokeInterface);
5301
Artem Serovcced8ba2017-07-19 18:18:09 +01005302 protected:
5303 DEFAULT_COPY_CONSTRUCTOR(InvokeInterface);
5304
Nicolas Geoffray52839d12014-11-07 17:47:25 +00005305 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01005306 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00005307 const uint32_t imt_index_;
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01005308
5309 // How the hidden argument (the interface method) is being loaded.
5310 const MethodLoadKind hidden_argument_load_kind_;
Nicolas Geoffray52839d12014-11-07 17:47:25 +00005311};
5312
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005313class HNeg final : public HUnaryOperation {
Roland Levillain88cb1752014-10-20 16:36:47 +01005314 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005315 HNeg(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305316 : HUnaryOperation(kNeg, result_type, input, dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005317 DCHECK_EQ(result_type, DataType::Kind(input->GetType()));
Roland Levillain937e6cd2016-03-22 11:54:37 +00005318 }
Roland Levillain88cb1752014-10-20 16:36:47 +01005319
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005320 template <typename T> static T Compute(T x) { return -x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005321
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005322 HConstant* Evaluate(HIntConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005323 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005324 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005325 HConstant* Evaluate(HLongConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005326 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005327 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005328 HConstant* Evaluate(HFloatConstant* x) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005329 return GetBlock()->GetGraph()->GetFloatConstant(Compute(x->GetValue()), GetDexPc());
5330 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005331 HConstant* Evaluate(HDoubleConstant* x) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005332 return GetBlock()->GetGraph()->GetDoubleConstant(Compute(x->GetValue()), GetDexPc());
5333 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01005334
Roland Levillain88cb1752014-10-20 16:36:47 +01005335 DECLARE_INSTRUCTION(Neg);
5336
Artem Serovcced8ba2017-07-19 18:18:09 +01005337 protected:
5338 DEFAULT_COPY_CONSTRUCTOR(Neg);
Roland Levillain88cb1752014-10-20 16:36:47 +01005339};
5340
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005341class HNewArray final : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01005342 public:
Vladimir Markob5461632018-10-15 14:24:21 +01005343 HNewArray(HInstruction* cls, HInstruction* length, uint32_t dex_pc, size_t component_size_shift)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305344 : HExpression(kNewArray, DataType::Type::kReference, SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005345 SetRawInputAt(0, cls);
5346 SetRawInputAt(1, length);
Vladimir Markob5461632018-10-15 14:24:21 +01005347 SetPackedField<ComponentSizeShiftField>(component_size_shift);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01005348 }
5349
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005350 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01005351
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01005352 // Calls runtime so needs an environment.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005353 bool NeedsEnvironment() const override { return true; }
Calin Juravle10e244f2015-01-26 18:54:32 +00005354
Mingyao Yang0c365e62015-03-31 15:09:29 -07005355 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005356 bool CanThrow() const override { return true; }
Mingyao Yang0c365e62015-03-31 15:09:29 -07005357
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005358 bool CanBeNull() const override { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01005359
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005360 HLoadClass* GetLoadClass() const {
5361 DCHECK(InputAt(0)->IsLoadClass());
Vladimir Markocde64972023-04-25 16:40:06 +00005362 return InputAt(0)->AsLoadClass();
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005363 }
5364
5365 HInstruction* GetLength() const {
5366 return InputAt(1);
5367 }
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00005368
Vladimir Markob5461632018-10-15 14:24:21 +01005369 size_t GetComponentSizeShift() {
5370 return GetPackedField<ComponentSizeShiftField>();
5371 }
5372
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01005373 DECLARE_INSTRUCTION(NewArray);
5374
Artem Serovcced8ba2017-07-19 18:18:09 +01005375 protected:
5376 DEFAULT_COPY_CONSTRUCTOR(NewArray);
Vladimir Markob5461632018-10-15 14:24:21 +01005377
5378 private:
5379 static constexpr size_t kFieldComponentSizeShift = kNumberOfGenericPackedBits;
5380 static constexpr size_t kFieldComponentSizeShiftSize = MinimumBitsToStore(3u);
5381 static constexpr size_t kNumberOfNewArrayPackedBits =
5382 kFieldComponentSizeShift + kFieldComponentSizeShiftSize;
5383 static_assert(kNumberOfNewArrayPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5384 using ComponentSizeShiftField =
Vladimir Marko1adb0de2023-03-03 09:16:24 +00005385 BitField<size_t, kFieldComponentSizeShift, kFieldComponentSizeShiftSize>;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01005386};
5387
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005388class HAdd final : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00005389 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005390 HAdd(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005391 HInstruction* left,
5392 HInstruction* right,
5393 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305394 : HBinaryOperation(kAdd, result_type, left, right, SideEffects::None(), dex_pc) {
5395 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00005396
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005397 bool IsCommutative() const override { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00005398
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005399 template <typename T> static T Compute(T x, T y) { return x + y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005400
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005401 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005402 return GetBlock()->GetGraph()->GetIntConstant(
5403 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01005404 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005405 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005406 return GetBlock()->GetGraph()->GetLongConstant(
5407 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01005408 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005409 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005410 return GetBlock()->GetGraph()->GetFloatConstant(
5411 Compute(x->GetValue(), y->GetValue()), GetDexPc());
5412 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005413 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005414 return GetBlock()->GetGraph()->GetDoubleConstant(
5415 Compute(x->GetValue(), y->GetValue()), GetDexPc());
5416 }
Roland Levillain556c3d12014-09-18 15:25:07 +01005417
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00005418 DECLARE_INSTRUCTION(Add);
5419
Artem Serovcced8ba2017-07-19 18:18:09 +01005420 protected:
5421 DEFAULT_COPY_CONSTRUCTOR(Add);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00005422};
5423
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005424class HSub final : public HBinaryOperation {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005425 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005426 HSub(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005427 HInstruction* left,
5428 HInstruction* right,
5429 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305430 : HBinaryOperation(kSub, result_type, left, right, SideEffects::None(), dex_pc) {
5431 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005432
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005433 template <typename T> static T Compute(T x, T y) { return x - y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005434
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005435 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005436 return GetBlock()->GetGraph()->GetIntConstant(
5437 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01005438 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005439 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005440 return GetBlock()->GetGraph()->GetLongConstant(
5441 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01005442 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005443 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005444 return GetBlock()->GetGraph()->GetFloatConstant(
5445 Compute(x->GetValue(), y->GetValue()), GetDexPc());
5446 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005447 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005448 return GetBlock()->GetGraph()->GetDoubleConstant(
5449 Compute(x->GetValue(), y->GetValue()), GetDexPc());
5450 }
Roland Levillain556c3d12014-09-18 15:25:07 +01005451
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005452 DECLARE_INSTRUCTION(Sub);
5453
Artem Serovcced8ba2017-07-19 18:18:09 +01005454 protected:
5455 DEFAULT_COPY_CONSTRUCTOR(Sub);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005456};
5457
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005458class HMul final : public HBinaryOperation {
Calin Juravle34bacdf2014-10-07 20:23:36 +01005459 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005460 HMul(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005461 HInstruction* left,
5462 HInstruction* right,
5463 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305464 : HBinaryOperation(kMul, result_type, left, right, SideEffects::None(), dex_pc) {
5465 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01005466
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005467 bool IsCommutative() const override { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01005468
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005469 template <typename T> static T Compute(T x, T y) { return x * y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005470
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005471 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005472 return GetBlock()->GetGraph()->GetIntConstant(
5473 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005474 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005475 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005476 return GetBlock()->GetGraph()->GetLongConstant(
5477 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005478 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005479 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005480 return GetBlock()->GetGraph()->GetFloatConstant(
5481 Compute(x->GetValue(), y->GetValue()), GetDexPc());
5482 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005483 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005484 return GetBlock()->GetGraph()->GetDoubleConstant(
5485 Compute(x->GetValue(), y->GetValue()), GetDexPc());
5486 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01005487
5488 DECLARE_INSTRUCTION(Mul);
5489
Artem Serovcced8ba2017-07-19 18:18:09 +01005490 protected:
5491 DEFAULT_COPY_CONSTRUCTOR(Mul);
Calin Juravle34bacdf2014-10-07 20:23:36 +01005492};
5493
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005494class HDiv final : public HBinaryOperation {
Calin Juravle7c4954d2014-10-28 16:57:40 +00005495 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005496 HDiv(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005497 HInstruction* left,
5498 HInstruction* right,
5499 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305500 : HBinaryOperation(kDiv, result_type, left, right, SideEffects::None(), dex_pc) {
5501 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00005502
Roland Levillain9867bc72015-08-05 10:21:34 +01005503 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00005504 T ComputeIntegral(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005505 DCHECK(!DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01005506 // Our graph structure ensures we never have 0 for `y` during
5507 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00005508 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00005509 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00005510 return (y == -1) ? -x : x / y;
5511 }
Calin Juravlebacfec32014-11-14 15:54:36 +00005512
Roland Levillain31dd3d62016-02-16 12:21:02 +00005513 template <typename T>
5514 T ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005515 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00005516 return x / y;
5517 }
5518
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005519 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005520 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00005521 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005522 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005523 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005524 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00005525 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5526 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005527 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005528 return GetBlock()->GetGraph()->GetFloatConstant(
5529 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
5530 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005531 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005532 return GetBlock()->GetGraph()->GetDoubleConstant(
5533 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00005534 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00005535
5536 DECLARE_INSTRUCTION(Div);
5537
Artem Serovcced8ba2017-07-19 18:18:09 +01005538 protected:
5539 DEFAULT_COPY_CONSTRUCTOR(Div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00005540};
5541
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005542class HRem final : public HBinaryOperation {
Calin Juravlebacfec32014-11-14 15:54:36 +00005543 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005544 HRem(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005545 HInstruction* left,
5546 HInstruction* right,
5547 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305548 : HBinaryOperation(kRem, result_type, left, right, SideEffects::None(), dex_pc) {
5549 }
Calin Juravlebacfec32014-11-14 15:54:36 +00005550
Roland Levillain9867bc72015-08-05 10:21:34 +01005551 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00005552 T ComputeIntegral(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005553 DCHECK(!DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01005554 // Our graph structure ensures we never have 0 for `y` during
5555 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00005556 DCHECK_NE(y, 0);
5557 // Special case -1 to avoid getting a SIGFPE on x86(_64).
5558 return (y == -1) ? 0 : x % y;
5559 }
5560
Roland Levillain31dd3d62016-02-16 12:21:02 +00005561 template <typename T>
5562 T ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005563 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00005564 return std::fmod(x, y);
5565 }
5566
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005567 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005568 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00005569 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005570 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005571 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005572 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00005573 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00005574 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005575 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005576 return GetBlock()->GetGraph()->GetFloatConstant(
5577 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
5578 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005579 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005580 return GetBlock()->GetGraph()->GetDoubleConstant(
5581 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
5582 }
Calin Juravlebacfec32014-11-14 15:54:36 +00005583
5584 DECLARE_INSTRUCTION(Rem);
5585
Artem Serovcced8ba2017-07-19 18:18:09 +01005586 protected:
5587 DEFAULT_COPY_CONSTRUCTOR(Rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00005588};
5589
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005590class HMin final : public HBinaryOperation {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005591 public:
5592 HMin(DataType::Type result_type,
5593 HInstruction* left,
5594 HInstruction* right,
5595 uint32_t dex_pc)
5596 : HBinaryOperation(kMin, result_type, left, right, SideEffects::None(), dex_pc) {}
5597
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005598 bool IsCommutative() const override { return true; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005599
5600 // Evaluation for integral values.
5601 template <typename T> static T ComputeIntegral(T x, T y) {
5602 return (x <= y) ? x : y;
5603 }
5604
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005605 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005606 return GetBlock()->GetGraph()->GetIntConstant(
5607 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5608 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005609 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005610 return GetBlock()->GetGraph()->GetLongConstant(
5611 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5612 }
5613 // TODO: Evaluation for floating-point values.
Stefano Cianciulli78f3c722023-05-16 10:32:54 +00005614 HConstant* Evaluate([[maybe_unused]] HFloatConstant* x,
5615 [[maybe_unused]] HFloatConstant* y) const override {
5616 return nullptr;
5617 }
5618 HConstant* Evaluate([[maybe_unused]] HDoubleConstant* x,
5619 [[maybe_unused]] HDoubleConstant* y) const override {
5620 return nullptr;
5621 }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005622
5623 DECLARE_INSTRUCTION(Min);
5624
5625 protected:
5626 DEFAULT_COPY_CONSTRUCTOR(Min);
5627};
5628
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005629class HMax final : public HBinaryOperation {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005630 public:
5631 HMax(DataType::Type result_type,
5632 HInstruction* left,
5633 HInstruction* right,
5634 uint32_t dex_pc)
5635 : HBinaryOperation(kMax, result_type, left, right, SideEffects::None(), dex_pc) {}
5636
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005637 bool IsCommutative() const override { return true; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005638
5639 // Evaluation for integral values.
5640 template <typename T> static T ComputeIntegral(T x, T y) {
5641 return (x >= y) ? x : y;
5642 }
5643
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005644 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005645 return GetBlock()->GetGraph()->GetIntConstant(
5646 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5647 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005648 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005649 return GetBlock()->GetGraph()->GetLongConstant(
5650 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5651 }
5652 // TODO: Evaluation for floating-point values.
Stefano Cianciulli78f3c722023-05-16 10:32:54 +00005653 HConstant* Evaluate([[maybe_unused]] HFloatConstant* x,
5654 [[maybe_unused]] HFloatConstant* y) const override {
5655 return nullptr;
5656 }
5657 HConstant* Evaluate([[maybe_unused]] HDoubleConstant* x,
5658 [[maybe_unused]] HDoubleConstant* y) const override {
5659 return nullptr;
5660 }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005661
5662 DECLARE_INSTRUCTION(Max);
5663
5664 protected:
5665 DEFAULT_COPY_CONSTRUCTOR(Max);
5666};
5667
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005668class HAbs final : public HUnaryOperation {
Aart Bik3dad3412018-02-28 12:01:46 -08005669 public:
5670 HAbs(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
5671 : HUnaryOperation(kAbs, result_type, input, dex_pc) {}
5672
5673 // Evaluation for integral values.
5674 template <typename T> static T ComputeIntegral(T x) {
5675 return x < 0 ? -x : x;
5676 }
5677
5678 // Evaluation for floating-point values.
5679 // Note, as a "quality of implementation", rather than pure "spec compliance",
5680 // we require that Math.abs() clears the sign bit (but changes nothing else)
5681 // for all floating-point numbers, including NaN (signaling NaN may become quiet though).
5682 // http://b/30758343
5683 template <typename T, typename S> static T ComputeFP(T x) {
5684 S bits = bit_cast<S, T>(x);
5685 return bit_cast<T, S>(bits & std::numeric_limits<S>::max());
5686 }
5687
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005688 HConstant* Evaluate(HIntConstant* x) const override {
Aart Bik3dad3412018-02-28 12:01:46 -08005689 return GetBlock()->GetGraph()->GetIntConstant(ComputeIntegral(x->GetValue()), GetDexPc());
5690 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005691 HConstant* Evaluate(HLongConstant* x) const override {
Aart Bik3dad3412018-02-28 12:01:46 -08005692 return GetBlock()->GetGraph()->GetLongConstant(ComputeIntegral(x->GetValue()), GetDexPc());
5693 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005694 HConstant* Evaluate(HFloatConstant* x) const override {
Aart Bik3dad3412018-02-28 12:01:46 -08005695 return GetBlock()->GetGraph()->GetFloatConstant(
5696 ComputeFP<float, int32_t>(x->GetValue()), GetDexPc());
5697 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005698 HConstant* Evaluate(HDoubleConstant* x) const override {
Aart Bik3dad3412018-02-28 12:01:46 -08005699 return GetBlock()->GetGraph()->GetDoubleConstant(
5700 ComputeFP<double, int64_t>(x->GetValue()), GetDexPc());
5701 }
5702
5703 DECLARE_INSTRUCTION(Abs);
5704
5705 protected:
5706 DEFAULT_COPY_CONSTRUCTOR(Abs);
5707};
5708
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005709class HDivZeroCheck final : public HExpression<1> {
Calin Juravled0d48522014-11-04 16:40:20 +00005710 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00005711 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
Artem Serovd1aa7d02018-06-22 11:35:46 +01005712 // constructor. However it can only do it on a fatal slow path so execution never returns to the
5713 // instruction following the current one; thus 'SideEffects::None()' is used.
Calin Juravled0d48522014-11-04 16:40:20 +00005714 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Artem Serovd1aa7d02018-06-22 11:35:46 +01005715 : HExpression(kDivZeroCheck, value->GetType(), SideEffects::None(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00005716 SetRawInputAt(0, value);
5717 }
5718
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005719 bool IsClonable() const override { return true; }
5720 bool CanBeMoved() const override { return true; }
Calin Juravled0d48522014-11-04 16:40:20 +00005721
Stefano Cianciulli78f3c722023-05-16 10:32:54 +00005722 bool InstructionDataEquals([[maybe_unused]] const HInstruction* other) const override {
Calin Juravled0d48522014-11-04 16:40:20 +00005723 return true;
5724 }
5725
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005726 bool NeedsEnvironment() const override { return true; }
5727 bool CanThrow() const override { return true; }
Calin Juravled0d48522014-11-04 16:40:20 +00005728
Calin Juravled0d48522014-11-04 16:40:20 +00005729 DECLARE_INSTRUCTION(DivZeroCheck);
5730
Artem Serovcced8ba2017-07-19 18:18:09 +01005731 protected:
5732 DEFAULT_COPY_CONSTRUCTOR(DivZeroCheck);
Calin Juravled0d48522014-11-04 16:40:20 +00005733};
5734
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005735class HShl final : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005736 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005737 HShl(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005738 HInstruction* value,
5739 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005740 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305741 : HBinaryOperation(kShl, result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005742 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5743 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01005744 }
5745
Roland Levillain5b5b9312016-03-22 14:57:31 +00005746 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005747 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005748 return value << (distance & max_shift_distance);
5749 }
5750
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005751 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005752 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005753 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005754 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005755 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005756 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005757 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005758 }
Stefano Cianciulli78f3c722023-05-16 10:32:54 +00005759 HConstant* Evaluate([[maybe_unused]] HLongConstant* value,
5760 [[maybe_unused]] HLongConstant* distance) const override {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005761 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5762 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01005763 }
Stefano Cianciulli78f3c722023-05-16 10:32:54 +00005764 HConstant* Evaluate([[maybe_unused]] HFloatConstant* value,
5765 [[maybe_unused]] HFloatConstant* distance) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005766 LOG(FATAL) << DebugName() << " is not defined for float values";
5767 UNREACHABLE();
5768 }
Stefano Cianciulli78f3c722023-05-16 10:32:54 +00005769 HConstant* Evaluate([[maybe_unused]] HDoubleConstant* value,
5770 [[maybe_unused]] HDoubleConstant* distance) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005771 LOG(FATAL) << DebugName() << " is not defined for double values";
5772 UNREACHABLE();
5773 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005774
5775 DECLARE_INSTRUCTION(Shl);
5776
Artem Serovcced8ba2017-07-19 18:18:09 +01005777 protected:
5778 DEFAULT_COPY_CONSTRUCTOR(Shl);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005779};
5780
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005781class HShr final : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005782 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005783 HShr(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005784 HInstruction* value,
5785 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005786 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305787 : HBinaryOperation(kShr, result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005788 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5789 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01005790 }
5791
Roland Levillain5b5b9312016-03-22 14:57:31 +00005792 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005793 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005794 return value >> (distance & max_shift_distance);
5795 }
5796
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005797 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005798 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005799 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005800 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005801 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005802 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005803 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005804 }
Stefano Cianciulli78f3c722023-05-16 10:32:54 +00005805 HConstant* Evaluate([[maybe_unused]] HLongConstant* value,
5806 [[maybe_unused]] HLongConstant* distance) const override {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005807 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5808 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01005809 }
Stefano Cianciulli78f3c722023-05-16 10:32:54 +00005810 HConstant* Evaluate([[maybe_unused]] HFloatConstant* value,
5811 [[maybe_unused]] HFloatConstant* distance) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005812 LOG(FATAL) << DebugName() << " is not defined for float values";
5813 UNREACHABLE();
5814 }
Stefano Cianciulli78f3c722023-05-16 10:32:54 +00005815 HConstant* Evaluate([[maybe_unused]] HDoubleConstant* value,
5816 [[maybe_unused]] HDoubleConstant* distance) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005817 LOG(FATAL) << DebugName() << " is not defined for double values";
5818 UNREACHABLE();
5819 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005820
5821 DECLARE_INSTRUCTION(Shr);
5822
Artem Serovcced8ba2017-07-19 18:18:09 +01005823 protected:
5824 DEFAULT_COPY_CONSTRUCTOR(Shr);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005825};
5826
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005827class HUShr final : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005828 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005829 HUShr(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005830 HInstruction* value,
5831 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005832 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305833 : HBinaryOperation(kUShr, result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005834 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5835 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Calin Juravle9aec02f2014-11-18 23:06:35 +00005836 }
5837
Roland Levillain5b5b9312016-03-22 14:57:31 +00005838 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005839 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Vladimir Marko4f990712021-07-14 12:45:13 +01005840 using V = std::make_unsigned_t<T>;
Roland Levillain5b5b9312016-03-22 14:57:31 +00005841 V ux = static_cast<V>(value);
5842 return static_cast<T>(ux >> (distance & max_shift_distance));
5843 }
5844
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005845 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005846 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005847 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005848 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005849 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005850 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005851 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005852 }
Stefano Cianciulli78f3c722023-05-16 10:32:54 +00005853 HConstant* Evaluate([[maybe_unused]] HLongConstant* value,
5854 [[maybe_unused]] HLongConstant* distance) const override {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005855 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5856 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00005857 }
Stefano Cianciulli78f3c722023-05-16 10:32:54 +00005858 HConstant* Evaluate([[maybe_unused]] HFloatConstant* value,
5859 [[maybe_unused]] HFloatConstant* distance) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005860 LOG(FATAL) << DebugName() << " is not defined for float values";
5861 UNREACHABLE();
5862 }
Stefano Cianciulli78f3c722023-05-16 10:32:54 +00005863 HConstant* Evaluate([[maybe_unused]] HDoubleConstant* value,
5864 [[maybe_unused]] HDoubleConstant* distance) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005865 LOG(FATAL) << DebugName() << " is not defined for double values";
5866 UNREACHABLE();
5867 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005868
5869 DECLARE_INSTRUCTION(UShr);
5870
Artem Serovcced8ba2017-07-19 18:18:09 +01005871 protected:
5872 DEFAULT_COPY_CONSTRUCTOR(UShr);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005873};
5874
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005875class HAnd final : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005876 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005877 HAnd(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005878 HInstruction* left,
5879 HInstruction* right,
5880 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305881 : HBinaryOperation(kAnd, result_type, left, right, SideEffects::None(), dex_pc) {
5882 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005883
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005884 bool IsCommutative() const override { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005885
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005886 template <typename T> static T Compute(T x, T y) { return x & y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005887
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005888 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005889 return GetBlock()->GetGraph()->GetIntConstant(
5890 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005891 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005892 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005893 return GetBlock()->GetGraph()->GetLongConstant(
5894 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005895 }
Stefano Cianciulli78f3c722023-05-16 10:32:54 +00005896 HConstant* Evaluate([[maybe_unused]] HFloatConstant* x,
5897 [[maybe_unused]] HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005898 LOG(FATAL) << DebugName() << " is not defined for float values";
5899 UNREACHABLE();
5900 }
Stefano Cianciulli78f3c722023-05-16 10:32:54 +00005901 HConstant* Evaluate([[maybe_unused]] HDoubleConstant* x,
5902 [[maybe_unused]] HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005903 LOG(FATAL) << DebugName() << " is not defined for double values";
5904 UNREACHABLE();
5905 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005906
5907 DECLARE_INSTRUCTION(And);
5908
Artem Serovcced8ba2017-07-19 18:18:09 +01005909 protected:
5910 DEFAULT_COPY_CONSTRUCTOR(And);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005911};
5912
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005913class HOr final : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005914 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005915 HOr(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005916 HInstruction* left,
5917 HInstruction* right,
5918 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305919 : HBinaryOperation(kOr, result_type, left, right, SideEffects::None(), dex_pc) {
5920 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005921
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005922 bool IsCommutative() const override { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005923
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005924 template <typename T> static T Compute(T x, T y) { return x | y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005925
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005926 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005927 return GetBlock()->GetGraph()->GetIntConstant(
5928 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005929 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005930 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005931 return GetBlock()->GetGraph()->GetLongConstant(
5932 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005933 }
Stefano Cianciulli78f3c722023-05-16 10:32:54 +00005934 HConstant* Evaluate([[maybe_unused]] HFloatConstant* x,
5935 [[maybe_unused]] HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005936 LOG(FATAL) << DebugName() << " is not defined for float values";
5937 UNREACHABLE();
5938 }
Stefano Cianciulli78f3c722023-05-16 10:32:54 +00005939 HConstant* Evaluate([[maybe_unused]] HDoubleConstant* x,
5940 [[maybe_unused]] HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005941 LOG(FATAL) << DebugName() << " is not defined for double values";
5942 UNREACHABLE();
5943 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005944
5945 DECLARE_INSTRUCTION(Or);
5946
Artem Serovcced8ba2017-07-19 18:18:09 +01005947 protected:
5948 DEFAULT_COPY_CONSTRUCTOR(Or);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005949};
5950
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005951class HXor final : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005952 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005953 HXor(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005954 HInstruction* left,
5955 HInstruction* right,
5956 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305957 : HBinaryOperation(kXor, result_type, left, right, SideEffects::None(), dex_pc) {
5958 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005959
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005960 bool IsCommutative() const override { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005961
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005962 template <typename T> static T Compute(T x, T y) { return x ^ y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005963
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005964 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005965 return GetBlock()->GetGraph()->GetIntConstant(
5966 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005967 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005968 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005969 return GetBlock()->GetGraph()->GetLongConstant(
5970 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005971 }
Stefano Cianciulli78f3c722023-05-16 10:32:54 +00005972 HConstant* Evaluate([[maybe_unused]] HFloatConstant* x,
5973 [[maybe_unused]] HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005974 LOG(FATAL) << DebugName() << " is not defined for float values";
5975 UNREACHABLE();
5976 }
Stefano Cianciulli78f3c722023-05-16 10:32:54 +00005977 HConstant* Evaluate([[maybe_unused]] HDoubleConstant* x,
5978 [[maybe_unused]] HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005979 LOG(FATAL) << DebugName() << " is not defined for double values";
5980 UNREACHABLE();
5981 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005982
5983 DECLARE_INSTRUCTION(Xor);
5984
Artem Serovcced8ba2017-07-19 18:18:09 +01005985 protected:
5986 DEFAULT_COPY_CONSTRUCTOR(Xor);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005987};
5988
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005989class HRor final : public HBinaryOperation {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005990 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005991 HRor(DataType::Type result_type, HInstruction* value, HInstruction* distance)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305992 : HBinaryOperation(kRor, result_type, value, distance) {
Roland Levillain22c49222016-03-18 14:04:28 +00005993 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005994
Roland Levillain5b5b9312016-03-22 14:57:31 +00005995 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005996 static T Compute(T value, int32_t distance, int32_t max_shift_value) {
Vladimir Marko4f990712021-07-14 12:45:13 +01005997 using V = std::make_unsigned_t<T>;
Roland Levillain5b5b9312016-03-22 14:57:31 +00005998 V ux = static_cast<V>(value);
5999 if ((distance & max_shift_value) == 0) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00006000 return static_cast<T>(ux);
6001 } else {
6002 const V reg_bits = sizeof(T) * 8;
Roland Levillain5b5b9312016-03-22 14:57:31 +00006003 return static_cast<T>(ux >> (distance & max_shift_value)) |
6004 (value << (reg_bits - (distance & max_shift_value)));
Scott Wakeling40a04bf2015-12-11 09:50:36 +00006005 }
6006 }
6007
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006008 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const override {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00006009 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00006010 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00006011 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006012 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const override {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00006013 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00006014 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00006015 }
Stefano Cianciulli78f3c722023-05-16 10:32:54 +00006016 HConstant* Evaluate([[maybe_unused]] HLongConstant* value,
6017 [[maybe_unused]] HLongConstant* distance) const override {
Roland Levillain5b5b9312016-03-22 14:57:31 +00006018 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
6019 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00006020 }
Stefano Cianciulli78f3c722023-05-16 10:32:54 +00006021 HConstant* Evaluate([[maybe_unused]] HFloatConstant* value,
6022 [[maybe_unused]] HFloatConstant* distance) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00006023 LOG(FATAL) << DebugName() << " is not defined for float values";
6024 UNREACHABLE();
6025 }
Stefano Cianciulli78f3c722023-05-16 10:32:54 +00006026 HConstant* Evaluate([[maybe_unused]] HDoubleConstant* value,
6027 [[maybe_unused]] HDoubleConstant* distance) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00006028 LOG(FATAL) << DebugName() << " is not defined for double values";
6029 UNREACHABLE();
6030 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00006031
6032 DECLARE_INSTRUCTION(Ror);
6033
Artem Serovcced8ba2017-07-19 18:18:09 +01006034 protected:
6035 DEFAULT_COPY_CONSTRUCTOR(Ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00006036};
6037
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01006038// The value of a parameter in this method. Its location depends on
6039// the calling convention.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006040class HParameterValue final : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01006041 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00006042 HParameterValue(const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08006043 dex::TypeIndex type_index,
Calin Juravlee6e3bea2015-10-14 13:53:10 +00006044 uint8_t index,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006045 DataType::Type parameter_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006046 bool is_this = false)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306047 : HExpression(kParameterValue, parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00006048 dex_file_(dex_file),
6049 type_index_(type_index),
Vladimir Markoa1de9182016-02-25 11:37:38 +00006050 index_(index) {
6051 SetPackedFlag<kFlagIsThis>(is_this);
6052 SetPackedFlag<kFlagCanBeNull>(!is_this);
6053 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01006054
Calin Juravlee6e3bea2015-10-14 13:53:10 +00006055 const DexFile& GetDexFile() const { return dex_file_; }
Andreas Gampea5b09a62016-11-17 15:21:22 -08006056 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01006057 uint8_t GetIndex() const { return index_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07006058 bool IsThis() const { return GetPackedFlag<kFlagIsThis>(); }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01006059
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006060 bool CanBeNull() const override { return GetPackedFlag<kFlagCanBeNull>(); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006061 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00006062
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01006063 DECLARE_INSTRUCTION(ParameterValue);
6064
Artem Serovcced8ba2017-07-19 18:18:09 +01006065 protected:
6066 DEFAULT_COPY_CONSTRUCTOR(ParameterValue);
6067
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01006068 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006069 // Whether or not the parameter value corresponds to 'this' argument.
Vladimir Markobd785672018-05-03 17:09:09 +01006070 static constexpr size_t kFlagIsThis = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006071 static constexpr size_t kFlagCanBeNull = kFlagIsThis + 1;
6072 static constexpr size_t kNumberOfParameterValuePackedBits = kFlagCanBeNull + 1;
6073 static_assert(kNumberOfParameterValuePackedBits <= kMaxNumberOfPackedBits,
6074 "Too many packed fields.");
6075
Calin Juravlee6e3bea2015-10-14 13:53:10 +00006076 const DexFile& dex_file_;
Andreas Gampea5b09a62016-11-17 15:21:22 -08006077 const dex::TypeIndex type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01006078 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00006079 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01006080 const uint8_t index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01006081};
6082
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006083class HNot final : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01006084 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006085 HNot(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306086 : HUnaryOperation(kNot, result_type, input, dex_pc) {
6087 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01006088
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006089 bool CanBeMoved() const override { return true; }
Stefano Cianciulli78f3c722023-05-16 10:32:54 +00006090 bool InstructionDataEquals([[maybe_unused]] const HInstruction* other) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07006091 return true;
6092 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01006093
Anton Kirilov7fe30f92016-06-29 17:03:40 +01006094 template <typename T> static T Compute(T x) { return ~x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01006095
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006096 HConstant* Evaluate(HIntConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006097 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01006098 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006099 HConstant* Evaluate(HLongConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006100 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01006101 }
Stefano Cianciulli78f3c722023-05-16 10:32:54 +00006102 HConstant* Evaluate([[maybe_unused]] HFloatConstant* x) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00006103 LOG(FATAL) << DebugName() << " is not defined for float values";
6104 UNREACHABLE();
6105 }
Stefano Cianciulli78f3c722023-05-16 10:32:54 +00006106 HConstant* Evaluate([[maybe_unused]] HDoubleConstant* x) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00006107 LOG(FATAL) << DebugName() << " is not defined for double values";
6108 UNREACHABLE();
6109 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01006110
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01006111 DECLARE_INSTRUCTION(Not);
6112
Artem Serovcced8ba2017-07-19 18:18:09 +01006113 protected:
6114 DEFAULT_COPY_CONSTRUCTOR(Not);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01006115};
6116
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006117class HBooleanNot final : public HUnaryOperation {
David Brazdil66d126e2015-04-03 16:02:44 +01006118 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006119 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306120 : HUnaryOperation(kBooleanNot, DataType::Type::kBool, input, dex_pc) {
6121 }
David Brazdil66d126e2015-04-03 16:02:44 +01006122
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006123 bool CanBeMoved() const override { return true; }
Stefano Cianciulli78f3c722023-05-16 10:32:54 +00006124 bool InstructionDataEquals([[maybe_unused]] const HInstruction* other) const override {
David Brazdil66d126e2015-04-03 16:02:44 +01006125 return true;
6126 }
6127
Anton Kirilov7fe30f92016-06-29 17:03:40 +01006128 template <typename T> static bool Compute(T x) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00006129 DCHECK(IsUint<1>(x)) << x;
David Brazdil66d126e2015-04-03 16:02:44 +01006130 return !x;
6131 }
6132
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006133 HConstant* Evaluate(HIntConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006134 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01006135 }
Stefano Cianciulli78f3c722023-05-16 10:32:54 +00006136 HConstant* Evaluate([[maybe_unused]] HLongConstant* x) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01006137 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01006138 UNREACHABLE();
6139 }
Stefano Cianciulli78f3c722023-05-16 10:32:54 +00006140 HConstant* Evaluate([[maybe_unused]] HFloatConstant* x) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00006141 LOG(FATAL) << DebugName() << " is not defined for float values";
6142 UNREACHABLE();
6143 }
Stefano Cianciulli78f3c722023-05-16 10:32:54 +00006144 HConstant* Evaluate([[maybe_unused]] HDoubleConstant* x) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00006145 LOG(FATAL) << DebugName() << " is not defined for double values";
6146 UNREACHABLE();
6147 }
David Brazdil66d126e2015-04-03 16:02:44 +01006148
6149 DECLARE_INSTRUCTION(BooleanNot);
6150
Artem Serovcced8ba2017-07-19 18:18:09 +01006151 protected:
6152 DEFAULT_COPY_CONSTRUCTOR(BooleanNot);
David Brazdil66d126e2015-04-03 16:02:44 +01006153};
6154
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006155class HTypeConversion final : public HExpression<1> {
Roland Levillaindff1f282014-11-05 14:15:05 +00006156 public:
6157 // Instantiate a type conversion of `input` to `result_type`.
Vladimir Markoc8fb2112017-10-03 11:37:52 +01006158 HTypeConversion(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306159 : HExpression(kTypeConversion, result_type, SideEffects::None(), dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00006160 SetRawInputAt(0, input);
Roland Levillainf355c3f2016-03-30 19:09:03 +01006161 // Invariant: We should never generate a conversion to a Boolean value.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006162 DCHECK_NE(DataType::Type::kBool, result_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00006163 }
6164
6165 HInstruction* GetInput() const { return InputAt(0); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006166 DataType::Type GetInputType() const { return GetInput()->GetType(); }
6167 DataType::Type GetResultType() const { return GetType(); }
Roland Levillaindff1f282014-11-05 14:15:05 +00006168
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006169 bool IsClonable() const override { return true; }
6170 bool CanBeMoved() const override { return true; }
Stefano Cianciulli78f3c722023-05-16 10:32:54 +00006171 bool InstructionDataEquals([[maybe_unused]] const HInstruction* other) const override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01006172 return true;
6173 }
Nicolas Geoffrayacc56ac2018-10-09 08:45:24 +01006174 // Return whether the conversion is implicit. This includes conversion to the same type.
6175 bool IsImplicitConversion() const {
6176 return DataType::IsTypeConversionImplicit(GetInputType(), GetResultType());
6177 }
Roland Levillaindff1f282014-11-05 14:15:05 +00006178
Mark Mendelle82549b2015-05-06 10:55:34 -04006179 // Try to statically evaluate the conversion and return a HConstant
6180 // containing the result. If the input cannot be converted, return nullptr.
6181 HConstant* TryStaticEvaluation() const;
6182
Roland Levillaindff1f282014-11-05 14:15:05 +00006183 DECLARE_INSTRUCTION(TypeConversion);
6184
Artem Serovcced8ba2017-07-19 18:18:09 +01006185 protected:
6186 DEFAULT_COPY_CONSTRUCTOR(TypeConversion);
Roland Levillaindff1f282014-11-05 14:15:05 +00006187};
6188
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00006189static constexpr uint32_t kNoRegNumber = -1;
6190
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006191class HNullCheck final : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006192 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00006193 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
Artem Serovd1aa7d02018-06-22 11:35:46 +01006194 // constructor. However it can only do it on a fatal slow path so execution never returns to the
6195 // instruction following the current one; thus 'SideEffects::None()' is used.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006196 HNullCheck(HInstruction* value, uint32_t dex_pc)
Artem Serovd1aa7d02018-06-22 11:35:46 +01006197 : HExpression(kNullCheck, value->GetType(), SideEffects::None(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006198 SetRawInputAt(0, value);
6199 }
6200
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006201 bool IsClonable() const override { return true; }
6202 bool CanBeMoved() const override { return true; }
Stefano Cianciulli78f3c722023-05-16 10:32:54 +00006203 bool InstructionDataEquals([[maybe_unused]] const HInstruction* other) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07006204 return true;
6205 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01006206
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006207 bool NeedsEnvironment() const override { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006208
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006209 bool CanThrow() const override { return true; }
Calin Juravle10e244f2015-01-26 18:54:32 +00006210
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006211 bool CanBeNull() const override { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01006212
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006213 DECLARE_INSTRUCTION(NullCheck);
6214
Artem Serovcced8ba2017-07-19 18:18:09 +01006215 protected:
6216 DEFAULT_COPY_CONSTRUCTOR(NullCheck);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006217};
6218
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006219// Embeds an ArtField and all the information required by the compiler. We cache
6220// that information to avoid requiring the mutator lock every time we need it.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006221class FieldInfo : public ValueObject {
6222 public:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006223 FieldInfo(ArtField* field,
6224 MemberOffset field_offset,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006225 DataType::Type field_type,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006226 bool is_volatile,
6227 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006228 uint16_t declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006229 const DexFile& dex_file)
6230 : field_(field),
6231 field_offset_(field_offset),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006232 field_type_(field_type),
6233 is_volatile_(is_volatile),
6234 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07006235 declaring_class_def_index_(declaring_class_def_index),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006236 dex_file_(dex_file) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006237
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006238 ArtField* GetField() const { return field_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006239 MemberOffset GetFieldOffset() const { return field_offset_; }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006240 DataType::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006241 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07006242 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006243 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00006244 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006245
Alex Light3a73ffb2021-01-25 14:11:05 +00006246 bool Equals(const FieldInfo& other) const {
6247 return field_ == other.field_ &&
6248 field_offset_ == other.field_offset_ &&
6249 field_type_ == other.field_type_ &&
6250 is_volatile_ == other.is_volatile_ &&
6251 index_ == other.index_ &&
6252 declaring_class_def_index_ == other.declaring_class_def_index_ &&
6253 &dex_file_ == &other.dex_file_;
6254 }
6255
6256 std::ostream& Dump(std::ostream& os) const {
6257 os << field_ << ", off: " << field_offset_ << ", type: " << field_type_
6258 << ", volatile: " << std::boolalpha << is_volatile_ << ", index_: " << std::dec << index_
6259 << ", declaring_class: " << declaring_class_def_index_ << ", dex: " << dex_file_;
6260 return os;
6261 }
6262
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006263 private:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006264 ArtField* const field_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006265 const MemberOffset field_offset_;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006266 const DataType::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00006267 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07006268 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07006269 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006270 const DexFile& dex_file_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006271};
6272
Alex Light3a73ffb2021-01-25 14:11:05 +00006273inline bool operator==(const FieldInfo& a, const FieldInfo& b) {
6274 return a.Equals(b);
6275}
6276
6277inline std::ostream& operator<<(std::ostream& os, const FieldInfo& a) {
6278 return a.Dump(os);
6279}
6280
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006281class HInstanceFieldGet final : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006282 public:
6283 HInstanceFieldGet(HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006284 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006285 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006286 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006287 bool is_volatile,
6288 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006289 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006290 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01006291 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306292 : HExpression(kInstanceFieldGet,
6293 field_type,
6294 SideEffects::FieldReadOfType(field_type, is_volatile),
6295 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006296 field_info_(field,
6297 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006298 field_type,
6299 is_volatile,
6300 field_idx,
6301 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006302 dex_file) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006303 SetRawInputAt(0, value);
6304 }
6305
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006306 bool IsClonable() const override { return true; }
6307 bool CanBeMoved() const override { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006308
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006309 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Markocde64972023-04-25 16:40:06 +00006310 const HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00006311 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01006312 }
6313
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006314 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01006315 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00006316 }
6317
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006318 size_t ComputeHashCode() const override {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01006319 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
6320 }
6321
Alex Light3a73ffb2021-01-25 14:11:05 +00006322 bool IsFieldAccess() const override { return true; }
6323 const FieldInfo& GetFieldInfo() const override { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006324 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006325 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006326 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006327
Vladimir Marko61b92282017-10-11 13:23:17 +01006328 void SetType(DataType::Type new_type) {
6329 DCHECK(DataType::IsIntegralType(GetType()));
6330 DCHECK(DataType::IsIntegralType(new_type));
6331 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
6332 SetPackedField<TypeField>(new_type);
6333 }
6334
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006335 DECLARE_INSTRUCTION(InstanceFieldGet);
6336
Artem Serovcced8ba2017-07-19 18:18:09 +01006337 protected:
6338 DEFAULT_COPY_CONSTRUCTOR(InstanceFieldGet);
6339
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006340 private:
6341 const FieldInfo field_info_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006342};
6343
Alex Light3a73ffb2021-01-25 14:11:05 +00006344class HPredicatedInstanceFieldGet final : public HExpression<2> {
6345 public:
6346 HPredicatedInstanceFieldGet(HInstanceFieldGet* orig,
6347 HInstruction* target,
6348 HInstruction* default_val)
6349 : HExpression(kPredicatedInstanceFieldGet,
6350 orig->GetFieldType(),
6351 orig->GetSideEffects(),
6352 orig->GetDexPc()),
6353 field_info_(orig->GetFieldInfo()) {
6354 // NB Default-val is at 0 so we can avoid doing a move.
6355 SetRawInputAt(1, target);
6356 SetRawInputAt(0, default_val);
6357 }
6358
6359 HPredicatedInstanceFieldGet(HInstruction* value,
6360 ArtField* field,
6361 HInstruction* default_value,
6362 DataType::Type field_type,
6363 MemberOffset field_offset,
6364 bool is_volatile,
6365 uint32_t field_idx,
6366 uint16_t declaring_class_def_index,
6367 const DexFile& dex_file,
6368 uint32_t dex_pc)
6369 : HExpression(kPredicatedInstanceFieldGet,
6370 field_type,
6371 SideEffects::FieldReadOfType(field_type, is_volatile),
6372 dex_pc),
6373 field_info_(field,
6374 field_offset,
6375 field_type,
6376 is_volatile,
6377 field_idx,
6378 declaring_class_def_index,
6379 dex_file) {
Alex Light74328052021-03-29 18:11:23 -07006380 SetRawInputAt(1, value);
6381 SetRawInputAt(0, default_value);
Alex Light3a73ffb2021-01-25 14:11:05 +00006382 }
6383
6384 bool IsClonable() const override {
6385 return true;
6386 }
6387 bool CanBeMoved() const override {
6388 return !IsVolatile();
6389 }
6390
6391 HInstruction* GetDefaultValue() const {
6392 return InputAt(0);
6393 }
6394 HInstruction* GetTarget() const {
6395 return InputAt(1);
6396 }
6397
6398 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Markocde64972023-04-25 16:40:06 +00006399 const HPredicatedInstanceFieldGet* other_get = other->AsPredicatedInstanceFieldGet();
Alex Light3a73ffb2021-01-25 14:11:05 +00006400 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue() &&
6401 GetDefaultValue() == other_get->GetDefaultValue();
6402 }
6403
6404 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
6405 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
6406 }
6407
6408 size_t ComputeHashCode() const override {
6409 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
6410 }
6411
6412 bool IsFieldAccess() const override { return true; }
6413 const FieldInfo& GetFieldInfo() const override { return field_info_; }
6414 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
6415 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
6416 bool IsVolatile() const { return field_info_.IsVolatile(); }
6417
6418 void SetType(DataType::Type new_type) {
6419 DCHECK(DataType::IsIntegralType(GetType()));
6420 DCHECK(DataType::IsIntegralType(new_type));
6421 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
6422 SetPackedField<TypeField>(new_type);
6423 }
6424
6425 DECLARE_INSTRUCTION(PredicatedInstanceFieldGet);
6426
6427 protected:
6428 DEFAULT_COPY_CONSTRUCTOR(PredicatedInstanceFieldGet);
6429
6430 private:
6431 const FieldInfo field_info_;
6432};
6433
Santiago Aboy Solanesd4229602023-01-03 16:20:50 +00006434enum class WriteBarrierKind {
6435 // Emit the write barrier, with a runtime optimization which checks if the value that it is being
6436 // set is null.
6437 kEmitWithNullCheck,
6438 // Emit the write barrier, without the runtime null check optimization. This could be set because:
6439 // A) It is a write barrier for an ArraySet (which does the optimization with the type check, so
6440 // it never does the optimization at the write barrier stage)
6441 // B) We know that the input can't be null
6442 // C) This write barrier is actually several write barriers coalesced into one. Potentially we
6443 // could ask if every value is null for a runtime optimization at the cost of compile time / code
6444 // size. At the time of writing it was deemed not worth the effort.
6445 kEmitNoNullCheck,
6446 // Skip emitting the write barrier. This could be set because:
6447 // A) The write barrier is not needed (e.g. it is not a reference, or the value is the null
6448 // constant)
6449 // B) This write barrier was coalesced into another one so there's no need to emit it.
6450 kDontEmit,
6451 kLast = kDontEmit
6452};
6453std::ostream& operator<<(std::ostream& os, WriteBarrierKind rhs);
6454
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006455class HInstanceFieldSet final : public HExpression<2> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006456 public:
6457 HInstanceFieldSet(HInstruction* object,
6458 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006459 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006460 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006461 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006462 bool is_volatile,
6463 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006464 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006465 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01006466 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006467 : HExpression(kInstanceFieldSet,
6468 SideEffects::FieldWriteOfType(field_type, is_volatile),
6469 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006470 field_info_(field,
6471 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006472 field_type,
6473 is_volatile,
6474 field_idx,
6475 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006476 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006477 SetPackedFlag<kFlagValueCanBeNull>(true);
Alex Light3a73ffb2021-01-25 14:11:05 +00006478 SetPackedFlag<kFlagIsPredicatedSet>(false);
Santiago Aboy Solanesd4229602023-01-03 16:20:50 +00006479 SetPackedField<WriteBarrierKindField>(WriteBarrierKind::kEmitWithNullCheck);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006480 SetRawInputAt(0, object);
6481 SetRawInputAt(1, value);
6482 }
6483
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006484 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01006485
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006486 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01006487 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00006488 }
6489
Alex Light3a73ffb2021-01-25 14:11:05 +00006490 bool IsFieldAccess() const override { return true; }
6491 const FieldInfo& GetFieldInfo() const override { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006492 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006493 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006494 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006495 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006496 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
6497 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Alex Light3a73ffb2021-01-25 14:11:05 +00006498 bool GetIsPredicatedSet() const { return GetPackedFlag<kFlagIsPredicatedSet>(); }
6499 void SetIsPredicatedSet(bool value = true) { SetPackedFlag<kFlagIsPredicatedSet>(value); }
Santiago Aboy Solanesd4229602023-01-03 16:20:50 +00006500 WriteBarrierKind GetWriteBarrierKind() { return GetPackedField<WriteBarrierKindField>(); }
6501 void SetWriteBarrierKind(WriteBarrierKind kind) {
6502 DCHECK(kind != WriteBarrierKind::kEmitWithNullCheck)
6503 << "We shouldn't go back to the original value.";
6504 SetPackedField<WriteBarrierKindField>(kind);
6505 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006506
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006507 DECLARE_INSTRUCTION(InstanceFieldSet);
6508
Artem Serovcced8ba2017-07-19 18:18:09 +01006509 protected:
6510 DEFAULT_COPY_CONSTRUCTOR(InstanceFieldSet);
6511
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006512 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006513 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
Alex Light3a73ffb2021-01-25 14:11:05 +00006514 static constexpr size_t kFlagIsPredicatedSet = kFlagValueCanBeNull + 1;
Santiago Aboy Solanesd4229602023-01-03 16:20:50 +00006515 static constexpr size_t kWriteBarrierKind = kFlagIsPredicatedSet + 1;
6516 static constexpr size_t kWriteBarrierKindSize =
6517 MinimumBitsToStore(static_cast<size_t>(WriteBarrierKind::kLast));
6518 static constexpr size_t kNumberOfInstanceFieldSetPackedBits =
6519 kWriteBarrierKind + kWriteBarrierKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006520 static_assert(kNumberOfInstanceFieldSetPackedBits <= kMaxNumberOfPackedBits,
6521 "Too many packed fields.");
6522
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006523 const FieldInfo field_info_;
Santiago Aboy Solanesd4229602023-01-03 16:20:50 +00006524 using WriteBarrierKindField =
6525 BitField<WriteBarrierKind, kWriteBarrierKind, kWriteBarrierKindSize>;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006526};
6527
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006528class HArrayGet final : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006529 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006530 HArrayGet(HInstruction* array,
6531 HInstruction* index,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006532 DataType::Type type,
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006533 uint32_t dex_pc)
6534 : HArrayGet(array,
6535 index,
6536 type,
6537 SideEffects::ArrayReadOfType(type),
6538 dex_pc,
Andreas Gampe3db70682018-12-26 15:12:03 -08006539 /* is_string_char_at= */ false) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306540 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006541
6542 HArrayGet(HInstruction* array,
6543 HInstruction* index,
6544 DataType::Type type,
6545 SideEffects side_effects,
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006546 uint32_t dex_pc,
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006547 bool is_string_char_at)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306548 : HExpression(kArrayGet, type, side_effects, dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006549 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006550 SetRawInputAt(0, array);
6551 SetRawInputAt(1, index);
6552 }
6553
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006554 bool IsClonable() const override { return true; }
6555 bool CanBeMoved() const override { return true; }
Stefano Cianciulli78f3c722023-05-16 10:32:54 +00006556 bool InstructionDataEquals([[maybe_unused]] const HInstruction* other) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07006557 return true;
6558 }
Stefano Cianciulli78f3c722023-05-16 10:32:54 +00006559 bool CanDoImplicitNullCheckOn([[maybe_unused]] HInstruction* obj) const override {
Calin Juravle77520bc2015-01-12 18:45:46 +00006560 // TODO: We can be smarter here.
Vladimir Marko66d691d2017-04-07 17:53:39 +01006561 // Currently, unless the array is the result of NewArray, the array access is always
6562 // preceded by some form of null NullCheck necessary for the bounds check, usually
6563 // implicit null check on the ArrayLength input to BoundsCheck or Deoptimize for
6564 // dynamic BCE. There are cases when these could be removed to produce better code.
6565 // If we ever add optimizations to do so we should allow an implicit check here
6566 // (as long as the address falls in the first page).
6567 //
6568 // As an example of such fancy optimization, we could eliminate BoundsCheck for
6569 // a = cond ? new int[1] : null;
6570 // a[0]; // The Phi does not need bounds check for either input.
Calin Juravle77520bc2015-01-12 18:45:46 +00006571 return false;
6572 }
6573
David Brazdil4833f5a2015-12-16 10:37:39 +00006574 bool IsEquivalentOf(HArrayGet* other) const {
6575 bool result = (GetDexPc() == other->GetDexPc());
6576 if (kIsDebugBuild && result) {
6577 DCHECK_EQ(GetBlock(), other->GetBlock());
6578 DCHECK_EQ(GetArray(), other->GetArray());
6579 DCHECK_EQ(GetIndex(), other->GetIndex());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006580 if (DataType::IsIntOrLongType(GetType())) {
6581 DCHECK(DataType::IsFloatingPointType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00006582 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006583 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
6584 DCHECK(DataType::IsIntOrLongType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00006585 }
6586 }
6587 return result;
6588 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01006589
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006590 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
6591
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006592 HInstruction* GetArray() const { return InputAt(0); }
6593 HInstruction* GetIndex() const { return InputAt(1); }
6594
Vladimir Marko61b92282017-10-11 13:23:17 +01006595 void SetType(DataType::Type new_type) {
6596 DCHECK(DataType::IsIntegralType(GetType()));
6597 DCHECK(DataType::IsIntegralType(new_type));
6598 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
6599 SetPackedField<TypeField>(new_type);
6600 }
6601
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006602 DECLARE_INSTRUCTION(ArrayGet);
6603
Artem Serovcced8ba2017-07-19 18:18:09 +01006604 protected:
6605 DEFAULT_COPY_CONSTRUCTOR(ArrayGet);
6606
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006607 private:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006608 // We treat a String as an array, creating the HArrayGet from String.charAt()
6609 // intrinsic in the instruction simplifier. We can always determine whether
6610 // a particular HArrayGet is actually a String.charAt() by looking at the type
6611 // of the input but that requires holding the mutator lock, so we prefer to use
6612 // a flag, so that code generators don't need to do the locking.
Vladimir Markobd785672018-05-03 17:09:09 +01006613 static constexpr size_t kFlagIsStringCharAt = kNumberOfGenericPackedBits;
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006614 static constexpr size_t kNumberOfArrayGetPackedBits = kFlagIsStringCharAt + 1;
6615 static_assert(kNumberOfArrayGetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6616 "Too many packed fields.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006617};
6618
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006619class HArraySet final : public HExpression<3> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006620 public:
6621 HArraySet(HInstruction* array,
6622 HInstruction* index,
6623 HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006624 DataType::Type expected_component_type,
Aart Bik18b36ab2016-04-13 16:41:35 -07006625 uint32_t dex_pc)
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006626 : HArraySet(array,
6627 index,
6628 value,
6629 expected_component_type,
6630 // Make a best guess for side effects now, may be refined during SSA building.
6631 ComputeSideEffects(GetComponentType(value->GetType(), expected_component_type)),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306632 dex_pc) {
6633 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006634
6635 HArraySet(HInstruction* array,
6636 HInstruction* index,
6637 HInstruction* value,
6638 DataType::Type expected_component_type,
6639 SideEffects side_effects,
6640 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006641 : HExpression(kArraySet, side_effects, dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006642 SetPackedField<ExpectedComponentTypeField>(expected_component_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006643 SetPackedFlag<kFlagNeedsTypeCheck>(value->GetType() == DataType::Type::kReference);
Vladimir Markoa1de9182016-02-25 11:37:38 +00006644 SetPackedFlag<kFlagValueCanBeNull>(true);
6645 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(false);
Santiago Aboy Solanesd4229602023-01-03 16:20:50 +00006646 // ArraySets never do the null check optimization at the write barrier stage.
6647 SetPackedField<WriteBarrierKindField>(WriteBarrierKind::kEmitNoNullCheck);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006648 SetRawInputAt(0, array);
6649 SetRawInputAt(1, index);
6650 SetRawInputAt(2, value);
6651 }
6652
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006653 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01006654
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006655 bool NeedsEnvironment() const override {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006656 // We call a runtime method to throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006657 return NeedsTypeCheck();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006658 }
6659
Mingyao Yang81014cb2015-06-02 03:16:27 -07006660 // Can throw ArrayStoreException.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006661 bool CanThrow() const override { return NeedsTypeCheck(); }
Mingyao Yang81014cb2015-06-02 03:16:27 -07006662
Stefano Cianciulli78f3c722023-05-16 10:32:54 +00006663 bool CanDoImplicitNullCheckOn([[maybe_unused]] HInstruction* obj) const override {
Calin Juravle77520bc2015-01-12 18:45:46 +00006664 // TODO: Same as for ArrayGet.
6665 return false;
6666 }
6667
Santiago Aboy Solanes13c3ce12022-12-05 12:05:43 +00006668 void ClearTypeCheck() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006669 SetPackedFlag<kFlagNeedsTypeCheck>(false);
Santiago Aboy Solanes13c3ce12022-12-05 12:05:43 +00006670 // Clear the `CanTriggerGC` flag too as we can only trigger a GC when doing a type check.
6671 SetSideEffects(GetSideEffects().Exclusion(SideEffects::CanTriggerGC()));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006672 }
6673
Nicolas Geoffray07276db2015-05-18 14:22:09 +01006674 void ClearValueCanBeNull() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006675 SetPackedFlag<kFlagValueCanBeNull>(false);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01006676 }
6677
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006678 void SetStaticTypeOfArrayIsObjectArray() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006679 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(true);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006680 }
6681
Vladimir Markoa1de9182016-02-25 11:37:38 +00006682 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
6683 bool NeedsTypeCheck() const { return GetPackedFlag<kFlagNeedsTypeCheck>(); }
6684 bool StaticTypeOfArrayIsObjectArray() const {
6685 return GetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>();
6686 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006687
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006688 HInstruction* GetArray() const { return InputAt(0); }
6689 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006690 HInstruction* GetValue() const { return InputAt(2); }
6691
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006692 DataType::Type GetComponentType() const {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006693 return GetComponentType(GetValue()->GetType(), GetRawExpectedComponentType());
6694 }
6695
6696 static DataType::Type GetComponentType(DataType::Type value_type,
6697 DataType::Type expected_component_type) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006698 // The Dex format does not type floating point index operations. Since the
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006699 // `expected_component_type` comes from SSA building and can therefore not
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006700 // be correct, we also check what is the value type. If it is a floating
6701 // point type, we must use that type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006702 return ((value_type == DataType::Type::kFloat32) || (value_type == DataType::Type::kFloat64))
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006703 ? value_type
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006704 : expected_component_type;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006705 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01006706
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006707 DataType::Type GetRawExpectedComponentType() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006708 return GetPackedField<ExpectedComponentTypeField>();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01006709 }
6710
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006711 static SideEffects ComputeSideEffects(DataType::Type type) {
6712 return SideEffects::ArrayWriteOfType(type).Union(SideEffectsForArchRuntimeCalls(type));
Aart Bik18b36ab2016-04-13 16:41:35 -07006713 }
6714
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006715 static SideEffects SideEffectsForArchRuntimeCalls(DataType::Type value_type) {
6716 return (value_type == DataType::Type::kReference) ? SideEffects::CanTriggerGC()
6717 : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006718 }
6719
Santiago Aboy Solanesd4229602023-01-03 16:20:50 +00006720 WriteBarrierKind GetWriteBarrierKind() { return GetPackedField<WriteBarrierKindField>(); }
6721
6722 void SetWriteBarrierKind(WriteBarrierKind kind) {
6723 DCHECK(kind != WriteBarrierKind::kEmitNoNullCheck)
6724 << "We shouldn't go back to the original value.";
6725 DCHECK(kind != WriteBarrierKind::kEmitWithNullCheck)
6726 << "We never do the null check optimization for ArraySets.";
6727 SetPackedField<WriteBarrierKindField>(kind);
6728 }
6729
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006730 DECLARE_INSTRUCTION(ArraySet);
6731
Artem Serovcced8ba2017-07-19 18:18:09 +01006732 protected:
6733 DEFAULT_COPY_CONSTRUCTOR(ArraySet);
6734
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006735 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006736 static constexpr size_t kFieldExpectedComponentType = kNumberOfGenericPackedBits;
6737 static constexpr size_t kFieldExpectedComponentTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006738 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00006739 static constexpr size_t kFlagNeedsTypeCheck =
6740 kFieldExpectedComponentType + kFieldExpectedComponentTypeSize;
6741 static constexpr size_t kFlagValueCanBeNull = kFlagNeedsTypeCheck + 1;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006742 // Cached information for the reference_type_info_ so that codegen
6743 // does not need to inspect the static type.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006744 static constexpr size_t kFlagStaticTypeOfArrayIsObjectArray = kFlagValueCanBeNull + 1;
Santiago Aboy Solanesd4229602023-01-03 16:20:50 +00006745 static constexpr size_t kWriteBarrierKind = kFlagStaticTypeOfArrayIsObjectArray + 1;
6746 static constexpr size_t kWriteBarrierKindSize =
6747 MinimumBitsToStore(static_cast<size_t>(WriteBarrierKind::kLast));
6748 static constexpr size_t kNumberOfArraySetPackedBits = kWriteBarrierKind + kWriteBarrierKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006749 static_assert(kNumberOfArraySetPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6750 using ExpectedComponentTypeField =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006751 BitField<DataType::Type, kFieldExpectedComponentType, kFieldExpectedComponentTypeSize>;
Santiago Aboy Solanesd4229602023-01-03 16:20:50 +00006752
6753 using WriteBarrierKindField =
6754 BitField<WriteBarrierKind, kWriteBarrierKind, kWriteBarrierKindSize>;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006755};
6756
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006757class HArrayLength final : public HExpression<1> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006758 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006759 HArrayLength(HInstruction* array, uint32_t dex_pc, bool is_string_length = false)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306760 : HExpression(kArrayLength, DataType::Type::kInt32, SideEffects::None(), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006761 SetPackedFlag<kFlagIsStringLength>(is_string_length);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01006762 // Note that arrays do not change length, so the instruction does not
6763 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006764 SetRawInputAt(0, array);
6765 }
6766
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006767 bool IsClonable() const override { return true; }
6768 bool CanBeMoved() const override { return true; }
Stefano Cianciulli78f3c722023-05-16 10:32:54 +00006769 bool InstructionDataEquals([[maybe_unused]] const HInstruction* other) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07006770 return true;
6771 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006772 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Calin Juravle641547a2015-04-21 22:08:51 +01006773 return obj == InputAt(0);
6774 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01006775
Vladimir Markodce016e2016-04-28 13:10:02 +01006776 bool IsStringLength() const { return GetPackedFlag<kFlagIsStringLength>(); }
6777
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006778 DECLARE_INSTRUCTION(ArrayLength);
6779
Artem Serovcced8ba2017-07-19 18:18:09 +01006780 protected:
6781 DEFAULT_COPY_CONSTRUCTOR(ArrayLength);
6782
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006783 private:
Vladimir Markodce016e2016-04-28 13:10:02 +01006784 // We treat a String as an array, creating the HArrayLength from String.length()
6785 // or String.isEmpty() intrinsic in the instruction simplifier. We can always
6786 // determine whether a particular HArrayLength is actually a String.length() by
6787 // looking at the type of the input but that requires holding the mutator lock, so
6788 // we prefer to use a flag, so that code generators don't need to do the locking.
Vladimir Markobd785672018-05-03 17:09:09 +01006789 static constexpr size_t kFlagIsStringLength = kNumberOfGenericPackedBits;
Vladimir Markodce016e2016-04-28 13:10:02 +01006790 static constexpr size_t kNumberOfArrayLengthPackedBits = kFlagIsStringLength + 1;
6791 static_assert(kNumberOfArrayLengthPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6792 "Too many packed fields.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006793};
6794
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006795class HBoundsCheck final : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006796 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00006797 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
Artem Serovd1aa7d02018-06-22 11:35:46 +01006798 // constructor. However it can only do it on a fatal slow path so execution never returns to the
6799 // instruction following the current one; thus 'SideEffects::None()' is used.
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006800 HBoundsCheck(HInstruction* index,
6801 HInstruction* length,
6802 uint32_t dex_pc,
Vladimir Marko0259c242017-12-04 11:27:47 +00006803 bool is_string_char_at = false)
Artem Serovd1aa7d02018-06-22 11:35:46 +01006804 : HExpression(kBoundsCheck, index->GetType(), SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006805 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(index->GetType()));
Vladimir Marko0259c242017-12-04 11:27:47 +00006806 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006807 SetRawInputAt(0, index);
6808 SetRawInputAt(1, length);
6809 }
6810
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006811 bool IsClonable() const override { return true; }
6812 bool CanBeMoved() const override { return true; }
Stefano Cianciulli78f3c722023-05-16 10:32:54 +00006813 bool InstructionDataEquals([[maybe_unused]] const HInstruction* other) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07006814 return true;
6815 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01006816
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006817 bool NeedsEnvironment() const override { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006818
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006819 bool CanThrow() const override { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01006820
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00006821 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006822
Alexandre Ramese6dbf482015-10-19 10:10:41 +01006823 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006824
6825 DECLARE_INSTRUCTION(BoundsCheck);
6826
Artem Serovcced8ba2017-07-19 18:18:09 +01006827 protected:
6828 DEFAULT_COPY_CONSTRUCTOR(BoundsCheck);
6829
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006830 private:
Vladimir Markobd785672018-05-03 17:09:09 +01006831 static constexpr size_t kFlagIsStringCharAt = kNumberOfGenericPackedBits;
Vladimir Markof02046e2018-10-02 15:31:32 +01006832 static constexpr size_t kNumberOfBoundsCheckPackedBits = kFlagIsStringCharAt + 1;
6833 static_assert(kNumberOfBoundsCheckPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6834 "Too many packed fields.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006835};
6836
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006837class HSuspendCheck final : public HExpression<0> {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006838 public:
Stelios Ioannouc54cc7c2021-07-09 17:06:03 +01006839 explicit HSuspendCheck(uint32_t dex_pc = kNoDexPc, bool is_no_op = false)
Vladimir Markobd785672018-05-03 17:09:09 +01006840 : HExpression(kSuspendCheck, SideEffects::CanTriggerGC(), dex_pc),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306841 slow_path_(nullptr) {
Stelios Ioannouc54cc7c2021-07-09 17:06:03 +01006842 SetPackedFlag<kFlagIsNoOp>(is_no_op);
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306843 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006844
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006845 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01006846
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006847 bool NeedsEnvironment() const override {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006848 return true;
6849 }
6850
Stelios Ioannouc54cc7c2021-07-09 17:06:03 +01006851 void SetIsNoOp(bool is_no_op) { SetPackedFlag<kFlagIsNoOp>(is_no_op); }
6852 bool IsNoOp() const { return GetPackedFlag<kFlagIsNoOp>(); }
6853
6854
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006855 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
6856 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006857
6858 DECLARE_INSTRUCTION(SuspendCheck);
6859
Artem Serovcced8ba2017-07-19 18:18:09 +01006860 protected:
6861 DEFAULT_COPY_CONSTRUCTOR(SuspendCheck);
6862
Stelios Ioannouc54cc7c2021-07-09 17:06:03 +01006863 // True if the HSuspendCheck should not emit any code during codegen. It is
6864 // not possible to simply remove this instruction to disable codegen, as
6865 // other optimizations (e.g: CHAGuardVisitor::HoistGuard) depend on
6866 // HSuspendCheck being present in every loop.
6867 static constexpr size_t kFlagIsNoOp = kNumberOfGenericPackedBits;
6868 static constexpr size_t kNumberOfSuspendCheckPackedBits = kFlagIsNoOp + 1;
6869 static_assert(kNumberOfSuspendCheckPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6870 "Too many packed fields.");
6871
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006872 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006873 // Only used for code generation, in order to share the same slow path between back edges
6874 // of a same loop.
6875 SlowPathCode* slow_path_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006876};
6877
Santiago Aboy Solanescaf9b652022-06-24 10:03:30 +01006878// Pseudo-instruction which doesn't generate any code.
6879// If `emit_environment` is true, it can be used to generate an environment. It is used, for
6880// example, to provide the native debugger with mapping information. It ensures that we can generate
6881// line number and local variables at this point.
6882class HNop : public HExpression<0> {
David Srbecky0cf44932015-12-09 14:09:59 +00006883 public:
Santiago Aboy Solanescaf9b652022-06-24 10:03:30 +01006884 explicit HNop(uint32_t dex_pc, bool needs_environment)
6885 : HExpression<0>(kNop, SideEffects::None(), dex_pc), needs_environment_(needs_environment) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306886 }
David Srbecky0cf44932015-12-09 14:09:59 +00006887
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006888 bool NeedsEnvironment() const override {
Santiago Aboy Solanescaf9b652022-06-24 10:03:30 +01006889 return needs_environment_;
David Srbecky0cf44932015-12-09 14:09:59 +00006890 }
6891
Santiago Aboy Solanescaf9b652022-06-24 10:03:30 +01006892 DECLARE_INSTRUCTION(Nop);
David Srbecky0cf44932015-12-09 14:09:59 +00006893
Artem Serovcced8ba2017-07-19 18:18:09 +01006894 protected:
Santiago Aboy Solanescaf9b652022-06-24 10:03:30 +01006895 DEFAULT_COPY_CONSTRUCTOR(Nop);
6896
6897 private:
6898 bool needs_environment_;
David Srbecky0cf44932015-12-09 14:09:59 +00006899};
6900
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006901/**
6902 * Instruction to load a Class object.
6903 */
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006904class HLoadClass final : public HInstruction {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006905 public:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006906 // Determines how to load the Class.
6907 enum class LoadKind {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006908 // We cannot load this class. See HSharpening::SharpenLoadClass.
6909 kInvalid = -1,
6910
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006911 // Use the Class* from the method's own ArtMethod*.
6912 kReferrersClass,
6913
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006914 // Use PC-relative boot image Class* address that will be known at link time.
Vladimir Marko764d4542017-05-16 10:31:41 +01006915 // Used for boot image classes referenced by boot image code.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006916 kBootImageLinkTimePcRelative,
6917
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006918 // Load from an entry in the .data.bimg.rel.ro using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006919 // Used for boot image classes referenced by apps in AOT-compiled code.
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006920 kBootImageRelRo,
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006921
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006922 // Load from an entry in the .bss section using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006923 // Used for classes outside boot image referenced by AOT-compiled app and boot image code.
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006924 kBssEntry,
6925
Vladimir Marko8f63f102020-09-28 12:10:28 +01006926 // Load from an entry for public class in the .bss section using a PC-relative load.
6927 // Used for classes that were unresolved during AOT-compilation outside the literal
6928 // package of the compiling class. Such classes are accessible only if they are public
6929 // and the .bss entry shall therefore be filled only if the resolved class is public.
6930 kBssEntryPublic,
6931
6932 // Load from an entry for package class in the .bss section using a PC-relative load.
6933 // Used for classes that were unresolved during AOT-compilation but within the literal
6934 // package of the compiling class. Such classes are accessible if they are public or
6935 // in the same package which, given the literal package match, requires only matching
6936 // defining class loader and the .bss entry shall therefore be filled only if at least
6937 // one of those conditions holds. Note that all code in an oat file belongs to classes
6938 // with the same defining class loader.
6939 kBssEntryPackage,
6940
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006941 // Use a known boot image Class* address, embedded in the code by the codegen.
6942 // Used for boot image classes referenced by apps in JIT-compiled code.
6943 kJitBootImageAddress,
6944
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006945 // Load from the root table associated with the JIT compiled method.
6946 kJitTableAddress,
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006947
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006948 // Load using a simple runtime call. This is the fall-back load kind when
6949 // the codegen is unable to use another appropriate kind.
6950 kRuntimeCall,
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006951
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006952 kLast = kRuntimeCall
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006953 };
6954
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01006955 HLoadClass(HCurrentMethod* current_method,
Andreas Gampea5b09a62016-11-17 15:21:22 -08006956 dex::TypeIndex type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01006957 const DexFile& dex_file,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006958 Handle<mirror::Class> klass,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006959 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01006960 uint32_t dex_pc,
Nicolas Geoffray56876342016-12-16 16:09:08 +00006961 bool needs_access_check)
Vladimir Markobd785672018-05-03 17:09:09 +01006962 : HInstruction(kLoadClass,
6963 DataType::Type::kReference,
6964 SideEffectsForArchRuntimeCalls(),
6965 dex_pc),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006966 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006967 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01006968 dex_file_(dex_file),
Vladimir Marko175e7862018-03-27 09:03:13 +00006969 klass_(klass) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01006970 // Referrers class should not need access check. We never inline unverified
6971 // methods so we can't possibly end up in this situation.
Santiago Aboy Solanes872ec722022-02-18 14:10:25 +00006972 DCHECK_IMPLIES(is_referrers_class, !needs_access_check);
Vladimir Markoa1de9182016-02-25 11:37:38 +00006973
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006974 SetPackedField<LoadKindField>(
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006975 is_referrers_class ? LoadKind::kReferrersClass : LoadKind::kRuntimeCall);
Vladimir Markoa1de9182016-02-25 11:37:38 +00006976 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
Nicolas Geoffray56876342016-12-16 16:09:08 +00006977 SetPackedFlag<kFlagIsInBootImage>(false);
Vladimir Markoa1de9182016-02-25 11:37:38 +00006978 SetPackedFlag<kFlagGenerateClInitCheck>(false);
Vladimir Marko175e7862018-03-27 09:03:13 +00006979 SetPackedFlag<kFlagValidLoadedClassRTI>(false);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006980 }
6981
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006982 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01006983
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006984 void SetLoadKind(LoadKind load_kind);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006985
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006986 LoadKind GetLoadKind() const {
6987 return GetPackedField<LoadKindField>();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01006988 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006989
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006990 bool HasPcRelativeLoadKind() const {
6991 return GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
6992 GetLoadKind() == LoadKind::kBootImageRelRo ||
Vladimir Marko8f63f102020-09-28 12:10:28 +01006993 GetLoadKind() == LoadKind::kBssEntry ||
6994 GetLoadKind() == LoadKind::kBssEntryPublic ||
6995 GetLoadKind() == LoadKind::kBssEntryPackage;
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006996 }
6997
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006998 bool CanBeMoved() const override { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006999
Yi Kong39402542019-03-24 02:47:16 -07007000 bool InstructionDataEquals(const HInstruction* other) const override;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007001
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007002 size_t ComputeHashCode() const override { return type_index_.index_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007003
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007004 bool CanBeNull() const override { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007005
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007006 bool NeedsEnvironment() const override {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00007007 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007008 }
Santiago Aboy Solanese43aa3f2021-11-01 09:02:09 +00007009 bool NeedsBss() const override {
7010 LoadKind load_kind = GetLoadKind();
7011 return load_kind == LoadKind::kBssEntry ||
7012 load_kind == LoadKind::kBssEntryPublic ||
7013 load_kind == LoadKind::kBssEntryPackage;
7014 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007015
Calin Juravle0ba218d2015-05-19 18:46:01 +01007016 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007017 SetPackedFlag<kFlagGenerateClInitCheck>(generate_clinit_check);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00007018 }
7019
7020 bool CanCallRuntime() const {
Vladimir Marko41559982017-01-06 14:04:23 +00007021 return NeedsAccessCheck() ||
7022 MustGenerateClinitCheck() ||
Nicolas Geoffray358bb392023-07-25 12:42:58 +01007023 NeedsBss() ||
7024 GetLoadKind() == LoadKind::kRuntimeCall;
Calin Juravle98893e12015-10-02 21:05:03 +01007025 }
7026
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007027 bool CanThrow() const override {
Vladimir Marko41559982017-01-06 14:04:23 +00007028 return NeedsAccessCheck() ||
7029 MustGenerateClinitCheck() ||
7030 // If the class is in the boot image, the lookup in the runtime call cannot throw.
Nicolas Geoffray358bb392023-07-25 12:42:58 +01007031 ((GetLoadKind() == LoadKind::kRuntimeCall || NeedsBss()) && !IsInBootImage());
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007032 }
7033
Calin Juravleacf735c2015-02-12 15:25:22 +00007034 ReferenceTypeInfo GetLoadedClassRTI() {
Vladimir Marko175e7862018-03-27 09:03:13 +00007035 if (GetPackedFlag<kFlagValidLoadedClassRTI>()) {
7036 // Note: The is_exact flag from the return value should not be used.
Andreas Gampe3db70682018-12-26 15:12:03 -08007037 return ReferenceTypeInfo::CreateUnchecked(klass_, /* is_exact= */ true);
Vladimir Marko175e7862018-03-27 09:03:13 +00007038 } else {
7039 return ReferenceTypeInfo::CreateInvalid();
7040 }
Calin Juravleacf735c2015-02-12 15:25:22 +00007041 }
7042
Vladimir Marko175e7862018-03-27 09:03:13 +00007043 // Loaded class RTI is marked as valid by RTP if the klass_ is admissible.
Vladimir Markoffafe8b2021-04-23 10:29:27 +00007044 void SetValidLoadedClassRTI() {
Vladimir Marko175e7862018-03-27 09:03:13 +00007045 DCHECK(klass_ != nullptr);
7046 SetPackedFlag<kFlagValidLoadedClassRTI>(true);
Calin Juravleacf735c2015-02-12 15:25:22 +00007047 }
7048
Andreas Gampea5b09a62016-11-17 15:21:22 -08007049 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007050 const DexFile& GetDexFile() const { return dex_file_; }
7051
Alexandre Rames78e3ef62015-08-12 13:43:29 +01007052 static SideEffects SideEffectsForArchRuntimeCalls() {
7053 return SideEffects::CanTriggerGC();
7054 }
7055
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007056 bool IsReferrersClass() const { return GetLoadKind() == LoadKind::kReferrersClass; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00007057 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
Mathieu Chartier31b12e32016-09-02 17:11:57 -07007058 bool IsInBootImage() const { return GetPackedFlag<kFlagIsInBootImage>(); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00007059 bool MustGenerateClinitCheck() const { return GetPackedFlag<kFlagGenerateClInitCheck>(); }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00007060
Vladimir Markoa9f303c2018-07-20 16:43:56 +01007061 bool MustResolveTypeOnSlowPath() const {
7062 // Check that this instruction has a slow path.
Vladimir Marko8f63f102020-09-28 12:10:28 +01007063 LoadKind load_kind = GetLoadKind();
7064 DCHECK(load_kind != LoadKind::kRuntimeCall); // kRuntimeCall calls on main path.
7065 bool must_resolve_type_on_slow_path =
7066 load_kind == LoadKind::kBssEntry ||
7067 load_kind == LoadKind::kBssEntryPublic ||
7068 load_kind == LoadKind::kBssEntryPackage;
7069 DCHECK(must_resolve_type_on_slow_path || MustGenerateClinitCheck());
7070 return must_resolve_type_on_slow_path;
Vladimir Markoa9f303c2018-07-20 16:43:56 +01007071 }
7072
Mathieu Chartier31b12e32016-09-02 17:11:57 -07007073 void MarkInBootImage() {
7074 SetPackedFlag<kFlagIsInBootImage>(true);
7075 }
7076
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007077 void AddSpecialInput(HInstruction* special_input);
7078
7079 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01007080 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007081 return ArrayRef<HUserRecord<HInstruction*>>(
7082 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
7083 }
7084
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007085 Handle<mirror::Class> GetClass() const {
7086 return klass_;
7087 }
7088
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007089 DECLARE_INSTRUCTION(LoadClass);
7090
Artem Serovcced8ba2017-07-19 18:18:09 +01007091 protected:
7092 DEFAULT_COPY_CONSTRUCTOR(LoadClass);
7093
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007094 private:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007095 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfGenericPackedBits;
Vladimir Marko41559982017-01-06 14:04:23 +00007096 static constexpr size_t kFlagIsInBootImage = kFlagNeedsAccessCheck + 1;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00007097 // Whether this instruction must generate the initialization check.
7098 // Used for code generation.
Mathieu Chartier31b12e32016-09-02 17:11:57 -07007099 static constexpr size_t kFlagGenerateClInitCheck = kFlagIsInBootImage + 1;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007100 static constexpr size_t kFieldLoadKind = kFlagGenerateClInitCheck + 1;
7101 static constexpr size_t kFieldLoadKindSize =
7102 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
Vladimir Marko175e7862018-03-27 09:03:13 +00007103 static constexpr size_t kFlagValidLoadedClassRTI = kFieldLoadKind + kFieldLoadKindSize;
7104 static constexpr size_t kNumberOfLoadClassPackedBits = kFlagValidLoadedClassRTI + 1;
Vladimir Markoa1de9182016-02-25 11:37:38 +00007105 static_assert(kNumberOfLoadClassPackedBits < kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007106 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
7107
7108 static bool HasTypeReference(LoadKind load_kind) {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007109 return load_kind == LoadKind::kReferrersClass ||
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007110 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007111 load_kind == LoadKind::kBssEntry ||
Vladimir Marko8f63f102020-09-28 12:10:28 +01007112 load_kind == LoadKind::kBssEntryPublic ||
7113 load_kind == LoadKind::kBssEntryPackage ||
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007114 load_kind == LoadKind::kRuntimeCall;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007115 }
7116
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007117 void SetLoadKindInternal(LoadKind load_kind);
7118
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007119 // The special input is the HCurrentMethod for kRuntimeCall or kReferrersClass.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007120 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko8f63f102020-09-28 12:10:28 +01007121 // for PC-relative loads, i.e. kBssEntry* or kBootImageLinkTimePcRelative.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007122 HUserRecord<HInstruction*> special_input_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00007123
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00007124 // A type index and dex file where the class can be accessed. The dex file can be:
7125 // - The compiling method's dex file if the class is defined there too.
7126 // - The compiling method's dex file if the class is referenced there.
7127 // - The dex file where the class is defined. When the load kind can only be
Vladimir Marko8f63f102020-09-28 12:10:28 +01007128 // kBssEntry* or kRuntimeCall, we cannot emit code for this `HLoadClass`.
Andreas Gampea5b09a62016-11-17 15:21:22 -08007129 const dex::TypeIndex type_index_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00007130 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007131
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007132 Handle<mirror::Class> klass_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007133};
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007134std::ostream& operator<<(std::ostream& os, HLoadClass::LoadKind rhs);
7135
7136// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
Vladimir Marko28e012a2017-12-07 11:22:59 +00007137inline void HLoadClass::SetLoadKind(LoadKind load_kind) {
7138 // The load kind should be determined before inserting the instruction to the graph.
7139 DCHECK(GetBlock() == nullptr);
7140 DCHECK(GetEnvironment() == nullptr);
7141 SetPackedField<LoadKindField>(load_kind);
7142 if (load_kind != LoadKind::kRuntimeCall && load_kind != LoadKind::kReferrersClass) {
7143 special_input_ = HUserRecord<HInstruction*>(nullptr);
7144 }
7145 if (!NeedsEnvironment()) {
7146 SetSideEffects(SideEffects::None());
7147 }
7148}
7149
7150// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007151inline void HLoadClass::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007152 // The special input is used for PC-relative loads on some architectures,
7153 // including literal pool loads, which are PC-relative too.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007154 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007155 GetLoadKind() == LoadKind::kBootImageRelRo ||
Vladimir Marko8e524ad2018-07-13 10:27:43 +01007156 GetLoadKind() == LoadKind::kBssEntry ||
Vladimir Marko8f63f102020-09-28 12:10:28 +01007157 GetLoadKind() == LoadKind::kBssEntryPublic ||
7158 GetLoadKind() == LoadKind::kBssEntryPackage ||
Vladimir Marko8e524ad2018-07-13 10:27:43 +01007159 GetLoadKind() == LoadKind::kJitBootImageAddress) << GetLoadKind();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007160 DCHECK(special_input_.GetInstruction() == nullptr);
7161 special_input_ = HUserRecord<HInstruction*>(special_input);
7162 special_input->AddUseAt(this, 0);
7163}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007164
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007165class HLoadString final : public HInstruction {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00007166 public:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007167 // Determines how to load the String.
7168 enum class LoadKind {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007169 // Use PC-relative boot image String* address that will be known at link time.
Vladimir Marko764d4542017-05-16 10:31:41 +01007170 // Used for boot image strings referenced by boot image code.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007171 kBootImageLinkTimePcRelative,
7172
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007173 // Load from an entry in the .data.bimg.rel.ro using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01007174 // Used for boot image strings referenced by apps in AOT-compiled code.
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007175 kBootImageRelRo,
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007176
Vladimir Markoaad75c62016-10-03 08:46:48 +00007177 // Load from an entry in the .bss section using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01007178 // Used for strings outside boot image referenced by AOT-compiled app and boot image code.
Vladimir Markoaad75c62016-10-03 08:46:48 +00007179 kBssEntry,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007180
Vladimir Marko8e524ad2018-07-13 10:27:43 +01007181 // Use a known boot image String* address, embedded in the code by the codegen.
7182 // Used for boot image strings referenced by apps in JIT-compiled code.
7183 kJitBootImageAddress,
7184
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007185 // Load from the root table associated with the JIT compiled method.
7186 kJitTableAddress,
7187
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007188 // Load using a simple runtime call. This is the fall-back load kind when
7189 // the codegen is unable to use another appropriate kind.
7190 kRuntimeCall,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007191
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007192 kLast = kRuntimeCall,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007193 };
7194
Nicolas Geoffray917d0162015-11-24 18:25:35 +00007195 HLoadString(HCurrentMethod* current_method,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08007196 dex::StringIndex string_index,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007197 const DexFile& dex_file,
7198 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01007199 : HInstruction(kLoadString,
7200 DataType::Type::kReference,
7201 SideEffectsForArchRuntimeCalls(),
7202 dex_pc),
Vladimir Marko372f10e2016-05-17 16:30:10 +01007203 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007204 string_index_(string_index),
7205 dex_file_(dex_file) {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007206 SetPackedField<LoadKindField>(LoadKind::kRuntimeCall);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01007207 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00007208
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007209 bool IsClonable() const override { return true; }
Santiago Aboy Solanese43aa3f2021-11-01 09:02:09 +00007210 bool NeedsBss() const override {
7211 return GetLoadKind() == LoadKind::kBssEntry;
7212 }
Artem Serovcced8ba2017-07-19 18:18:09 +01007213
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007214 void SetLoadKind(LoadKind load_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007215
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007216 LoadKind GetLoadKind() const {
7217 return GetPackedField<LoadKindField>();
7218 }
7219
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007220 bool HasPcRelativeLoadKind() const {
7221 return GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
7222 GetLoadKind() == LoadKind::kBootImageRelRo ||
7223 GetLoadKind() == LoadKind::kBssEntry;
7224 }
7225
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007226 const DexFile& GetDexFile() const {
7227 return dex_file_;
7228 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007229
Andreas Gampe8a0128a2016-11-28 07:38:35 -08007230 dex::StringIndex GetStringIndex() const {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007231 return string_index_;
7232 }
7233
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007234 Handle<mirror::String> GetString() const {
7235 return string_;
7236 }
7237
7238 void SetString(Handle<mirror::String> str) {
7239 string_ = str;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007240 }
7241
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007242 bool CanBeMoved() const override { return true; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00007243
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007244 bool InstructionDataEquals(const HInstruction* other) const override;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00007245
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007246 size_t ComputeHashCode() const override { return string_index_.index_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00007247
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007248 // Will call the runtime if we need to load the string through
7249 // the dex cache and the string is not guaranteed to be there yet.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007250 bool NeedsEnvironment() const override {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007251 LoadKind load_kind = GetLoadKind();
Vladimir Marko764d4542017-05-16 10:31:41 +01007252 if (load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007253 load_kind == LoadKind::kBootImageRelRo ||
Vladimir Marko8e524ad2018-07-13 10:27:43 +01007254 load_kind == LoadKind::kJitBootImageAddress ||
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007255 load_kind == LoadKind::kJitTableAddress) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007256 return false;
7257 }
Vladimir Marko4d1be4922017-01-06 14:43:11 +00007258 return true;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007259 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00007260
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007261 bool CanBeNull() const override { return false; }
7262 bool CanThrow() const override { return NeedsEnvironment(); }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00007263
Alexandre Rames78e3ef62015-08-12 13:43:29 +01007264 static SideEffects SideEffectsForArchRuntimeCalls() {
7265 return SideEffects::CanTriggerGC();
7266 }
7267
Vladimir Marko372f10e2016-05-17 16:30:10 +01007268 void AddSpecialInput(HInstruction* special_input);
7269
7270 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01007271 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Vladimir Marko372f10e2016-05-17 16:30:10 +01007272 return ArrayRef<HUserRecord<HInstruction*>>(
7273 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007274 }
7275
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00007276 DECLARE_INSTRUCTION(LoadString);
7277
Artem Serovcced8ba2017-07-19 18:18:09 +01007278 protected:
7279 DEFAULT_COPY_CONSTRUCTOR(LoadString);
7280
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00007281 private:
Vladimir Marko4d1be4922017-01-06 14:43:11 +00007282 static constexpr size_t kFieldLoadKind = kNumberOfGenericPackedBits;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007283 static constexpr size_t kFieldLoadKindSize =
7284 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
7285 static constexpr size_t kNumberOfLoadStringPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00007286 static_assert(kNumberOfLoadStringPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007287 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00007288
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007289 void SetLoadKindInternal(LoadKind load_kind);
7290
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007291 // The special input is the HCurrentMethod for kRuntimeCall.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007292 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko48886c22017-01-06 11:45:47 +00007293 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Marko372f10e2016-05-17 16:30:10 +01007294 HUserRecord<HInstruction*> special_input_;
7295
Andreas Gampe8a0128a2016-11-28 07:38:35 -08007296 dex::StringIndex string_index_;
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007297 const DexFile& dex_file_;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007298
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007299 Handle<mirror::String> string_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00007300};
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007301std::ostream& operator<<(std::ostream& os, HLoadString::LoadKind rhs);
7302
7303// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
Vladimir Marko28e012a2017-12-07 11:22:59 +00007304inline void HLoadString::SetLoadKind(LoadKind load_kind) {
7305 // The load kind should be determined before inserting the instruction to the graph.
7306 DCHECK(GetBlock() == nullptr);
7307 DCHECK(GetEnvironment() == nullptr);
7308 DCHECK_EQ(GetLoadKind(), LoadKind::kRuntimeCall);
7309 SetPackedField<LoadKindField>(load_kind);
7310 if (load_kind != LoadKind::kRuntimeCall) {
7311 special_input_ = HUserRecord<HInstruction*>(nullptr);
7312 }
7313 if (!NeedsEnvironment()) {
7314 SetSideEffects(SideEffects::None());
7315 }
7316}
7317
7318// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007319inline void HLoadString::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007320 // The special input is used for PC-relative loads on some architectures,
7321 // including literal pool loads, which are PC-relative too.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007322 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007323 GetLoadKind() == LoadKind::kBootImageRelRo ||
Vladimir Marko8e524ad2018-07-13 10:27:43 +01007324 GetLoadKind() == LoadKind::kBssEntry ||
7325 GetLoadKind() == LoadKind::kJitBootImageAddress) << GetLoadKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01007326 // HLoadString::GetInputRecords() returns an empty array at this point,
7327 // so use the GetInputRecords() from the base class to set the input record.
7328 DCHECK(special_input_.GetInstruction() == nullptr);
7329 special_input_ = HUserRecord<HInstruction*>(special_input);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007330 special_input->AddUseAt(this, 0);
7331}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00007332
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007333class HLoadMethodHandle final : public HInstruction {
Orion Hodsondbaa5c72018-05-10 08:22:46 +01007334 public:
7335 HLoadMethodHandle(HCurrentMethod* current_method,
Orion Hodson4c8e12e2018-05-18 08:33:20 +01007336 uint16_t method_handle_idx,
7337 const DexFile& dex_file,
7338 uint32_t dex_pc)
Orion Hodsondbaa5c72018-05-10 08:22:46 +01007339 : HInstruction(kLoadMethodHandle,
7340 DataType::Type::kReference,
7341 SideEffectsForArchRuntimeCalls(),
7342 dex_pc),
7343 special_input_(HUserRecord<HInstruction*>(current_method)),
7344 method_handle_idx_(method_handle_idx),
7345 dex_file_(dex_file) {
7346 }
7347
7348 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01007349 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Orion Hodsondbaa5c72018-05-10 08:22:46 +01007350 return ArrayRef<HUserRecord<HInstruction*>>(
7351 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
7352 }
7353
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007354 bool IsClonable() const override { return true; }
Orion Hodsondbaa5c72018-05-10 08:22:46 +01007355
7356 uint16_t GetMethodHandleIndex() const { return method_handle_idx_; }
7357
7358 const DexFile& GetDexFile() const { return dex_file_; }
7359
7360 static SideEffects SideEffectsForArchRuntimeCalls() {
7361 return SideEffects::CanTriggerGC();
7362 }
7363
Nicolas Geoffrayaafb81b2022-06-29 09:36:23 +01007364 bool CanThrow() const override { return true; }
7365
7366 bool NeedsEnvironment() const override { return true; }
7367
Orion Hodsondbaa5c72018-05-10 08:22:46 +01007368 DECLARE_INSTRUCTION(LoadMethodHandle);
7369
7370 protected:
7371 DEFAULT_COPY_CONSTRUCTOR(LoadMethodHandle);
7372
7373 private:
7374 // The special input is the HCurrentMethod for kRuntimeCall.
7375 HUserRecord<HInstruction*> special_input_;
7376
7377 const uint16_t method_handle_idx_;
7378 const DexFile& dex_file_;
7379};
7380
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007381class HLoadMethodType final : public HInstruction {
Orion Hodson18259d72018-04-12 11:18:23 +01007382 public:
7383 HLoadMethodType(HCurrentMethod* current_method,
Orion Hodson06d10a72018-05-14 08:53:38 +01007384 dex::ProtoIndex proto_index,
Orion Hodson18259d72018-04-12 11:18:23 +01007385 const DexFile& dex_file,
7386 uint32_t dex_pc)
7387 : HInstruction(kLoadMethodType,
7388 DataType::Type::kReference,
7389 SideEffectsForArchRuntimeCalls(),
7390 dex_pc),
7391 special_input_(HUserRecord<HInstruction*>(current_method)),
Orion Hodson06d10a72018-05-14 08:53:38 +01007392 proto_index_(proto_index),
Orion Hodson18259d72018-04-12 11:18:23 +01007393 dex_file_(dex_file) {
7394 }
7395
7396 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01007397 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Orion Hodson18259d72018-04-12 11:18:23 +01007398 return ArrayRef<HUserRecord<HInstruction*>>(
7399 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
7400 }
7401
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007402 bool IsClonable() const override { return true; }
Orion Hodson18259d72018-04-12 11:18:23 +01007403
Orion Hodson06d10a72018-05-14 08:53:38 +01007404 dex::ProtoIndex GetProtoIndex() const { return proto_index_; }
Orion Hodson18259d72018-04-12 11:18:23 +01007405
7406 const DexFile& GetDexFile() const { return dex_file_; }
7407
7408 static SideEffects SideEffectsForArchRuntimeCalls() {
7409 return SideEffects::CanTriggerGC();
7410 }
7411
Nicolas Geoffrayaafb81b2022-06-29 09:36:23 +01007412 bool CanThrow() const override { return true; }
7413
7414 bool NeedsEnvironment() const override { return true; }
7415
Orion Hodson18259d72018-04-12 11:18:23 +01007416 DECLARE_INSTRUCTION(LoadMethodType);
7417
7418 protected:
7419 DEFAULT_COPY_CONSTRUCTOR(LoadMethodType);
7420
7421 private:
7422 // The special input is the HCurrentMethod for kRuntimeCall.
7423 HUserRecord<HInstruction*> special_input_;
7424
Orion Hodson06d10a72018-05-14 08:53:38 +01007425 const dex::ProtoIndex proto_index_;
Orion Hodson18259d72018-04-12 11:18:23 +01007426 const DexFile& dex_file_;
7427};
7428
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007429/**
7430 * Performs an initialization check on its Class object input.
7431 */
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007432class HClinitCheck final : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007433 public:
Roland Levillain3887c462015-08-12 18:15:42 +01007434 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07007435 : HExpression(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307436 kClinitCheck,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007437 DataType::Type::kReference,
Mingyao Yang217eb062017-12-11 15:20:07 -08007438 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06007439 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007440 SetRawInputAt(0, constant);
7441 }
Artem Serova6e26142018-06-19 14:55:17 +01007442 // TODO: Make ClinitCheck clonable.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007443 bool CanBeMoved() const override { return true; }
Stefano Cianciulli78f3c722023-05-16 10:32:54 +00007444 bool InstructionDataEquals([[maybe_unused]] const HInstruction* other) const override {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00007445 return true;
7446 }
7447
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007448 bool NeedsEnvironment() const override {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007449 // May call runtime to initialize the class.
7450 return true;
7451 }
7452
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007453 bool CanThrow() const override { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007454
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00007455 HLoadClass* GetLoadClass() const {
7456 DCHECK(InputAt(0)->IsLoadClass());
Vladimir Markocde64972023-04-25 16:40:06 +00007457 return InputAt(0)->AsLoadClass();
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00007458 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007459
7460 DECLARE_INSTRUCTION(ClinitCheck);
7461
Artem Serovcced8ba2017-07-19 18:18:09 +01007462
7463 protected:
7464 DEFAULT_COPY_CONSTRUCTOR(ClinitCheck);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007465};
7466
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007467class HStaticFieldGet final : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007468 public:
7469 HStaticFieldGet(HInstruction* cls,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00007470 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007471 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00007472 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01007473 bool is_volatile,
7474 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07007475 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07007476 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01007477 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307478 : HExpression(kStaticFieldGet,
7479 field_type,
7480 SideEffects::FieldReadOfType(field_type, is_volatile),
7481 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00007482 field_info_(field,
7483 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07007484 field_type,
7485 is_volatile,
7486 field_idx,
7487 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00007488 dex_file) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007489 SetRawInputAt(0, cls);
7490 }
7491
Calin Juravle52c48962014-12-16 17:02:57 +00007492
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007493 bool IsClonable() const override { return true; }
7494 bool CanBeMoved() const override { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00007495
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007496 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Markocde64972023-04-25 16:40:06 +00007497 const HStaticFieldGet* other_get = other->AsStaticFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00007498 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007499 }
7500
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007501 size_t ComputeHashCode() const override {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007502 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
7503 }
7504
Alex Light3a73ffb2021-01-25 14:11:05 +00007505 bool IsFieldAccess() const override { return true; }
7506 const FieldInfo& GetFieldInfo() const override { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007507 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007508 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00007509 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007510
Vladimir Marko61b92282017-10-11 13:23:17 +01007511 void SetType(DataType::Type new_type) {
7512 DCHECK(DataType::IsIntegralType(GetType()));
7513 DCHECK(DataType::IsIntegralType(new_type));
7514 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
7515 SetPackedField<TypeField>(new_type);
7516 }
7517
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007518 DECLARE_INSTRUCTION(StaticFieldGet);
7519
Artem Serovcced8ba2017-07-19 18:18:09 +01007520 protected:
7521 DEFAULT_COPY_CONSTRUCTOR(StaticFieldGet);
7522
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007523 private:
7524 const FieldInfo field_info_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007525};
7526
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007527class HStaticFieldSet final : public HExpression<2> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007528 public:
7529 HStaticFieldSet(HInstruction* cls,
7530 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00007531 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007532 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00007533 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01007534 bool is_volatile,
7535 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07007536 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07007537 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01007538 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01007539 : HExpression(kStaticFieldSet,
7540 SideEffects::FieldWriteOfType(field_type, is_volatile),
7541 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00007542 field_info_(field,
7543 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07007544 field_type,
7545 is_volatile,
7546 field_idx,
7547 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00007548 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007549 SetPackedFlag<kFlagValueCanBeNull>(true);
Santiago Aboy Solanesd4229602023-01-03 16:20:50 +00007550 SetPackedField<WriteBarrierKindField>(WriteBarrierKind::kEmitWithNullCheck);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007551 SetRawInputAt(0, cls);
7552 SetRawInputAt(1, value);
7553 }
7554
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007555 bool IsClonable() const override { return true; }
Alex Light3a73ffb2021-01-25 14:11:05 +00007556 bool IsFieldAccess() const override { return true; }
7557 const FieldInfo& GetFieldInfo() const override { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007558 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007559 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00007560 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007561
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00007562 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00007563 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
7564 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00007565
Santiago Aboy Solanesd4229602023-01-03 16:20:50 +00007566 WriteBarrierKind GetWriteBarrierKind() { return GetPackedField<WriteBarrierKindField>(); }
7567 void SetWriteBarrierKind(WriteBarrierKind kind) {
7568 DCHECK(kind != WriteBarrierKind::kEmitWithNullCheck)
7569 << "We shouldn't go back to the original value.";
7570 SetPackedField<WriteBarrierKindField>(kind);
7571 }
7572
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007573 DECLARE_INSTRUCTION(StaticFieldSet);
7574
Artem Serovcced8ba2017-07-19 18:18:09 +01007575 protected:
7576 DEFAULT_COPY_CONSTRUCTOR(StaticFieldSet);
7577
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007578 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007579 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
Santiago Aboy Solanesd4229602023-01-03 16:20:50 +00007580 static constexpr size_t kWriteBarrierKind = kFlagValueCanBeNull + 1;
7581 static constexpr size_t kWriteBarrierKindSize =
7582 MinimumBitsToStore(static_cast<size_t>(WriteBarrierKind::kLast));
7583 static constexpr size_t kNumberOfStaticFieldSetPackedBits =
7584 kWriteBarrierKind + kWriteBarrierKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00007585 static_assert(kNumberOfStaticFieldSetPackedBits <= kMaxNumberOfPackedBits,
7586 "Too many packed fields.");
7587
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007588 const FieldInfo field_info_;
Santiago Aboy Solanesd4229602023-01-03 16:20:50 +00007589 using WriteBarrierKindField =
7590 BitField<WriteBarrierKind, kWriteBarrierKind, kWriteBarrierKindSize>;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007591};
7592
Vladimir Marko552a1342017-10-31 10:56:47 +00007593class HStringBuilderAppend final : public HVariableInputSizeInstruction {
7594 public:
7595 HStringBuilderAppend(HIntConstant* format,
7596 uint32_t number_of_arguments,
Vladimir Marko41de4502019-05-21 10:00:15 +01007597 bool has_fp_args,
Vladimir Marko552a1342017-10-31 10:56:47 +00007598 ArenaAllocator* allocator,
7599 uint32_t dex_pc)
7600 : HVariableInputSizeInstruction(
7601 kStringBuilderAppend,
7602 DataType::Type::kReference,
Vladimir Marko41de4502019-05-21 10:00:15 +01007603 SideEffects::CanTriggerGC().Union(
7604 // The runtime call may read memory from inputs. It never writes outside
7605 // of the newly allocated result object or newly allocated helper objects,
7606 // except for float/double arguments where we reuse thread-local helper objects.
7607 has_fp_args ? SideEffects::AllWritesAndReads() : SideEffects::AllReads()),
Vladimir Marko552a1342017-10-31 10:56:47 +00007608 dex_pc,
7609 allocator,
7610 number_of_arguments + /* format */ 1u,
7611 kArenaAllocInvokeInputs) {
7612 DCHECK_GE(number_of_arguments, 1u); // There must be something to append.
7613 SetRawInputAt(FormatIndex(), format);
7614 }
7615
7616 void SetArgumentAt(size_t index, HInstruction* argument) {
7617 DCHECK_LE(index, GetNumberOfArguments());
7618 SetRawInputAt(index, argument);
7619 }
7620
7621 // Return the number of arguments, excluding the format.
7622 size_t GetNumberOfArguments() const {
7623 DCHECK_GE(InputCount(), 1u);
7624 return InputCount() - 1u;
7625 }
7626
7627 size_t FormatIndex() const {
7628 return GetNumberOfArguments();
7629 }
7630
7631 HIntConstant* GetFormat() {
Vladimir Markocde64972023-04-25 16:40:06 +00007632 return InputAt(FormatIndex())->AsIntConstant();
Vladimir Marko552a1342017-10-31 10:56:47 +00007633 }
7634
7635 bool NeedsEnvironment() const override { return true; }
7636
7637 bool CanThrow() const override { return true; }
7638
Vladimir Markob1fe5e12020-03-10 14:30:49 +00007639 bool CanBeNull() const override { return false; }
7640
Vladimir Marko552a1342017-10-31 10:56:47 +00007641 DECLARE_INSTRUCTION(StringBuilderAppend);
7642
7643 protected:
7644 DEFAULT_COPY_CONSTRUCTOR(StringBuilderAppend);
7645};
7646
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007647class HUnresolvedInstanceFieldGet final : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01007648 public:
7649 HUnresolvedInstanceFieldGet(HInstruction* obj,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007650 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01007651 uint32_t field_index,
7652 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307653 : HExpression(kUnresolvedInstanceFieldGet,
7654 field_type,
7655 SideEffects::AllExceptGCDependency(),
7656 dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01007657 field_index_(field_index) {
7658 SetRawInputAt(0, obj);
7659 }
7660
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007661 bool IsClonable() const override { return true; }
7662 bool NeedsEnvironment() const override { return true; }
7663 bool CanThrow() const override { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01007664
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007665 DataType::Type GetFieldType() const { return GetType(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01007666 uint32_t GetFieldIndex() const { return field_index_; }
7667
7668 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
7669
Artem Serovcced8ba2017-07-19 18:18:09 +01007670 protected:
7671 DEFAULT_COPY_CONSTRUCTOR(UnresolvedInstanceFieldGet);
7672
Calin Juravlee460d1d2015-09-29 04:52:17 +01007673 private:
7674 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01007675};
7676
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007677class HUnresolvedInstanceFieldSet final : public HExpression<2> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01007678 public:
7679 HUnresolvedInstanceFieldSet(HInstruction* obj,
7680 HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007681 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01007682 uint32_t field_index,
7683 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01007684 : HExpression(kUnresolvedInstanceFieldSet, SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01007685 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007686 SetPackedField<FieldTypeField>(field_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007687 DCHECK_EQ(DataType::Kind(field_type), DataType::Kind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01007688 SetRawInputAt(0, obj);
7689 SetRawInputAt(1, value);
7690 }
7691
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007692 bool IsClonable() const override { return true; }
7693 bool NeedsEnvironment() const override { return true; }
7694 bool CanThrow() const override { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01007695
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007696 DataType::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01007697 uint32_t GetFieldIndex() const { return field_index_; }
7698
7699 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
7700
Artem Serovcced8ba2017-07-19 18:18:09 +01007701 protected:
7702 DEFAULT_COPY_CONSTRUCTOR(UnresolvedInstanceFieldSet);
7703
Calin Juravlee460d1d2015-09-29 04:52:17 +01007704 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007705 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
7706 static constexpr size_t kFieldFieldTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007707 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00007708 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
7709 kFieldFieldType + kFieldFieldTypeSize;
7710 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
7711 "Too many packed fields.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007712 using FieldTypeField = BitField<DataType::Type, kFieldFieldType, kFieldFieldTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00007713
Calin Juravlee460d1d2015-09-29 04:52:17 +01007714 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01007715};
7716
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007717class HUnresolvedStaticFieldGet final : public HExpression<0> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01007718 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007719 HUnresolvedStaticFieldGet(DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01007720 uint32_t field_index,
7721 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307722 : HExpression(kUnresolvedStaticFieldGet,
7723 field_type,
7724 SideEffects::AllExceptGCDependency(),
7725 dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01007726 field_index_(field_index) {
7727 }
7728
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007729 bool IsClonable() const override { return true; }
7730 bool NeedsEnvironment() const override { return true; }
7731 bool CanThrow() const override { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01007732
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007733 DataType::Type GetFieldType() const { return GetType(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01007734 uint32_t GetFieldIndex() const { return field_index_; }
7735
7736 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
7737
Artem Serovcced8ba2017-07-19 18:18:09 +01007738 protected:
7739 DEFAULT_COPY_CONSTRUCTOR(UnresolvedStaticFieldGet);
7740
Calin Juravlee460d1d2015-09-29 04:52:17 +01007741 private:
7742 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01007743};
7744
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007745class HUnresolvedStaticFieldSet final : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01007746 public:
7747 HUnresolvedStaticFieldSet(HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007748 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01007749 uint32_t field_index,
7750 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01007751 : HExpression(kUnresolvedStaticFieldSet, SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01007752 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007753 SetPackedField<FieldTypeField>(field_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007754 DCHECK_EQ(DataType::Kind(field_type), DataType::Kind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01007755 SetRawInputAt(0, value);
7756 }
7757
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007758 bool IsClonable() const override { return true; }
7759 bool NeedsEnvironment() const override { return true; }
7760 bool CanThrow() const override { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01007761
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007762 DataType::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01007763 uint32_t GetFieldIndex() const { return field_index_; }
7764
7765 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
7766
Artem Serovcced8ba2017-07-19 18:18:09 +01007767 protected:
7768 DEFAULT_COPY_CONSTRUCTOR(UnresolvedStaticFieldSet);
7769
Calin Juravlee460d1d2015-09-29 04:52:17 +01007770 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007771 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
7772 static constexpr size_t kFieldFieldTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007773 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00007774 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
7775 kFieldFieldType + kFieldFieldTypeSize;
7776 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
7777 "Too many packed fields.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007778 using FieldTypeField = BitField<DataType::Type, kFieldFieldType, kFieldFieldTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00007779
Calin Juravlee460d1d2015-09-29 04:52:17 +01007780 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01007781};
7782
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007783// Implement the move-exception DEX instruction.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007784class HLoadException final : public HExpression<0> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007785 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06007786 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307787 : HExpression(kLoadException, DataType::Type::kReference, SideEffects::None(), dex_pc) {
7788 }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007789
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007790 bool CanBeNull() const override { return false; }
David Brazdilbbd733e2015-08-18 17:48:17 +01007791
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007792 DECLARE_INSTRUCTION(LoadException);
7793
Artem Serovcced8ba2017-07-19 18:18:09 +01007794 protected:
7795 DEFAULT_COPY_CONSTRUCTOR(LoadException);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007796};
7797
David Brazdilcb1c0552015-08-04 16:22:25 +01007798// Implicit part of move-exception which clears thread-local exception storage.
7799// Must not be removed because the runtime expects the TLS to get cleared.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007800class HClearException final : public HExpression<0> {
David Brazdilcb1c0552015-08-04 16:22:25 +01007801 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06007802 explicit HClearException(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01007803 : HExpression(kClearException, SideEffects::AllWrites(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307804 }
David Brazdilcb1c0552015-08-04 16:22:25 +01007805
7806 DECLARE_INSTRUCTION(ClearException);
7807
Artem Serovcced8ba2017-07-19 18:18:09 +01007808 protected:
7809 DEFAULT_COPY_CONSTRUCTOR(ClearException);
David Brazdilcb1c0552015-08-04 16:22:25 +01007810};
7811
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007812class HThrow final : public HExpression<1> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007813 public:
7814 HThrow(HInstruction* exception, uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01007815 : HExpression(kThrow, SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007816 SetRawInputAt(0, exception);
7817 }
7818
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007819 bool IsControlFlow() const override { return true; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007820
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007821 bool NeedsEnvironment() const override { return true; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007822
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007823 bool CanThrow() const override { return true; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007824
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007825 bool AlwaysThrows() const override { return true; }
Aart Bika8b8e9b2018-01-09 11:01:02 -08007826
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007827 DECLARE_INSTRUCTION(Throw);
7828
Artem Serovcced8ba2017-07-19 18:18:09 +01007829 protected:
7830 DEFAULT_COPY_CONSTRUCTOR(Throw);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007831};
7832
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007833/**
7834 * Implementation strategies for the code generator of a HInstanceOf
7835 * or `HCheckCast`.
7836 */
Vladimir Marko9974e3c2020-06-10 16:27:06 +01007837enum class TypeCheckKind { // private marker to avoid generate-operator-out.py from processing.
Calin Juravle98893e12015-10-02 21:05:03 +01007838 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007839 kExactCheck, // Can do a single class compare.
7840 kClassHierarchyCheck, // Can just walk the super class chain.
7841 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
7842 kInterfaceCheck, // No optimization yet when checking against an interface.
7843 kArrayObjectCheck, // Can just check if the array is not primitive.
Vladimir Markoa1de9182016-02-25 11:37:38 +00007844 kArrayCheck, // No optimization yet when checking against a generic array.
Vladimir Marko175e7862018-03-27 09:03:13 +00007845 kBitstringCheck, // Compare the type check bitstring.
Vladimir Markoa1de9182016-02-25 11:37:38 +00007846 kLast = kArrayCheck
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007847};
7848
Roland Levillain86503782016-02-11 19:07:30 +00007849std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs);
7850
Vladimir Marko175e7862018-03-27 09:03:13 +00007851// Note: HTypeCheckInstruction is just a helper class, not an abstract instruction with an
7852// `IsTypeCheckInstruction()`. (New virtual methods in the HInstruction class have a high cost.)
7853class HTypeCheckInstruction : public HVariableInputSizeInstruction {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007854 public:
Vladimir Marko175e7862018-03-27 09:03:13 +00007855 HTypeCheckInstruction(InstructionKind kind,
Vladimir Markobd785672018-05-03 17:09:09 +01007856 DataType::Type type,
Vladimir Marko175e7862018-03-27 09:03:13 +00007857 HInstruction* object,
7858 HInstruction* target_class_or_null,
7859 TypeCheckKind check_kind,
7860 Handle<mirror::Class> klass,
7861 uint32_t dex_pc,
7862 ArenaAllocator* allocator,
7863 HIntConstant* bitstring_path_to_root,
7864 HIntConstant* bitstring_mask,
7865 SideEffects side_effects)
7866 : HVariableInputSizeInstruction(
7867 kind,
Vladimir Markobd785672018-05-03 17:09:09 +01007868 type,
Vladimir Marko175e7862018-03-27 09:03:13 +00007869 side_effects,
7870 dex_pc,
7871 allocator,
Andreas Gampe3db70682018-12-26 15:12:03 -08007872 /* number_of_inputs= */ check_kind == TypeCheckKind::kBitstringCheck ? 4u : 2u,
Vladimir Marko175e7862018-03-27 09:03:13 +00007873 kArenaAllocTypeCheckInputs),
7874 klass_(klass) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007875 SetPackedField<TypeCheckKindField>(check_kind);
7876 SetPackedFlag<kFlagMustDoNullCheck>(true);
Vladimir Marko175e7862018-03-27 09:03:13 +00007877 SetPackedFlag<kFlagValidTargetClassRTI>(false);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007878 SetRawInputAt(0, object);
Vladimir Marko175e7862018-03-27 09:03:13 +00007879 SetRawInputAt(1, target_class_or_null);
7880 DCHECK_EQ(check_kind == TypeCheckKind::kBitstringCheck, bitstring_path_to_root != nullptr);
7881 DCHECK_EQ(check_kind == TypeCheckKind::kBitstringCheck, bitstring_mask != nullptr);
7882 if (check_kind == TypeCheckKind::kBitstringCheck) {
7883 DCHECK(target_class_or_null->IsNullConstant());
7884 SetRawInputAt(2, bitstring_path_to_root);
7885 SetRawInputAt(3, bitstring_mask);
7886 } else {
7887 DCHECK(target_class_or_null->IsLoadClass());
7888 }
Vladimir Marko87584542017-12-12 17:47:52 +00007889 }
7890
7891 HLoadClass* GetTargetClass() const {
Vladimir Marko175e7862018-03-27 09:03:13 +00007892 DCHECK_NE(GetTypeCheckKind(), TypeCheckKind::kBitstringCheck);
Vladimir Marko87584542017-12-12 17:47:52 +00007893 HInstruction* load_class = InputAt(1);
7894 DCHECK(load_class->IsLoadClass());
Vladimir Markocde64972023-04-25 16:40:06 +00007895 return load_class->AsLoadClass();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007896 }
7897
Vladimir Marko175e7862018-03-27 09:03:13 +00007898 uint32_t GetBitstringPathToRoot() const {
7899 DCHECK_EQ(GetTypeCheckKind(), TypeCheckKind::kBitstringCheck);
7900 HInstruction* path_to_root = InputAt(2);
7901 DCHECK(path_to_root->IsIntConstant());
Vladimir Markocde64972023-04-25 16:40:06 +00007902 return static_cast<uint32_t>(path_to_root->AsIntConstant()->GetValue());
Vladimir Marko175e7862018-03-27 09:03:13 +00007903 }
7904
7905 uint32_t GetBitstringMask() const {
7906 DCHECK_EQ(GetTypeCheckKind(), TypeCheckKind::kBitstringCheck);
7907 HInstruction* mask = InputAt(3);
7908 DCHECK(mask->IsIntConstant());
Vladimir Markocde64972023-04-25 16:40:06 +00007909 return static_cast<uint32_t>(mask->AsIntConstant()->GetValue());
Vladimir Marko175e7862018-03-27 09:03:13 +00007910 }
7911
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007912 bool IsClonable() const override { return true; }
7913 bool CanBeMoved() const override { return true; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007914
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007915 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Marko175e7862018-03-27 09:03:13 +00007916 DCHECK(other->IsInstanceOf() || other->IsCheckCast()) << other->DebugName();
7917 return GetPackedFields() == down_cast<const HTypeCheckInstruction*>(other)->GetPackedFields();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007918 }
7919
Andreas Gampe3fbd3ad2018-03-26 21:14:46 +00007920 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
7921 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
7922 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
7923 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
7924
Vladimir Marko175e7862018-03-27 09:03:13 +00007925 ReferenceTypeInfo GetTargetClassRTI() {
7926 if (GetPackedFlag<kFlagValidTargetClassRTI>()) {
7927 // Note: The is_exact flag from the return value should not be used.
Andreas Gampe3db70682018-12-26 15:12:03 -08007928 return ReferenceTypeInfo::CreateUnchecked(klass_, /* is_exact= */ true);
Vladimir Marko175e7862018-03-27 09:03:13 +00007929 } else {
7930 return ReferenceTypeInfo::CreateInvalid();
7931 }
7932 }
7933
7934 // Target class RTI is marked as valid by RTP if the klass_ is admissible.
Vladimir Markoffafe8b2021-04-23 10:29:27 +00007935 void SetValidTargetClassRTI() {
Vladimir Marko175e7862018-03-27 09:03:13 +00007936 DCHECK(klass_ != nullptr);
7937 SetPackedFlag<kFlagValidTargetClassRTI>(true);
7938 }
7939
7940 Handle<mirror::Class> GetClass() const {
7941 return klass_;
7942 }
7943
7944 protected:
7945 DEFAULT_COPY_CONSTRUCTOR(TypeCheckInstruction);
7946
7947 private:
7948 static constexpr size_t kFieldTypeCheckKind = kNumberOfGenericPackedBits;
7949 static constexpr size_t kFieldTypeCheckKindSize =
7950 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
7951 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
7952 static constexpr size_t kFlagValidTargetClassRTI = kFlagMustDoNullCheck + 1;
7953 static constexpr size_t kNumberOfInstanceOfPackedBits = kFlagValidTargetClassRTI + 1;
7954 static_assert(kNumberOfInstanceOfPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
7955 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
7956
7957 Handle<mirror::Class> klass_;
7958};
7959
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007960class HInstanceOf final : public HTypeCheckInstruction {
Vladimir Marko175e7862018-03-27 09:03:13 +00007961 public:
7962 HInstanceOf(HInstruction* object,
7963 HInstruction* target_class_or_null,
7964 TypeCheckKind check_kind,
7965 Handle<mirror::Class> klass,
7966 uint32_t dex_pc,
7967 ArenaAllocator* allocator,
7968 HIntConstant* bitstring_path_to_root,
7969 HIntConstant* bitstring_mask)
7970 : HTypeCheckInstruction(kInstanceOf,
Vladimir Markobd785672018-05-03 17:09:09 +01007971 DataType::Type::kBool,
Vladimir Marko175e7862018-03-27 09:03:13 +00007972 object,
7973 target_class_or_null,
7974 check_kind,
7975 klass,
7976 dex_pc,
7977 allocator,
7978 bitstring_path_to_root,
7979 bitstring_mask,
7980 SideEffectsForArchRuntimeCalls(check_kind)) {}
7981
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007982 bool IsClonable() const override { return true; }
Artem Serova6e26142018-06-19 14:55:17 +01007983
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007984 bool NeedsEnvironment() const override {
Vladimir Marko175e7862018-03-27 09:03:13 +00007985 return CanCallRuntime(GetTypeCheckKind());
7986 }
7987
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00007988 static bool CanCallRuntime(TypeCheckKind check_kind) {
Vladimir Marko54f4fbd2020-02-12 10:52:22 +00007989 // TODO: Re-evaluate now that mips codegen has been removed.
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00007990 return check_kind != TypeCheckKind::kExactCheck;
7991 }
7992
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007993 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00007994 return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01007995 }
7996
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007997 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007998
Artem Serovcced8ba2017-07-19 18:18:09 +01007999 protected:
8000 DEFAULT_COPY_CONSTRUCTOR(InstanceOf);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00008001};
8002
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01008003class HBoundType final : public HExpression<1> {
Calin Juravleb1498f62015-02-16 13:13:29 +00008004 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -07008005 explicit HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05308006 : HExpression(kBoundType, DataType::Type::kReference, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00008007 upper_bound_(ReferenceTypeInfo::CreateInvalid()) {
8008 SetPackedFlag<kFlagUpperCanBeNull>(true);
8009 SetPackedFlag<kFlagCanBeNull>(true);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008010 DCHECK_EQ(input->GetType(), DataType::Type::kReference);
Calin Juravleb1498f62015-02-16 13:13:29 +00008011 SetRawInputAt(0, input);
8012 }
8013
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01008014 bool InstructionDataEquals(const HInstruction* other) const override;
8015 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01008016
David Brazdilf5552582015-12-27 13:36:12 +00008017 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00008018 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00008019 bool GetUpperCanBeNull() const { return GetPackedFlag<kFlagUpperCanBeNull>(); }
David Brazdilf5552582015-12-27 13:36:12 +00008020 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00008021
Calin Juravlea5ae3c32015-07-28 14:40:50 +00008022 void SetCanBeNull(bool can_be_null) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00008023 DCHECK(GetUpperCanBeNull() || !can_be_null);
8024 SetPackedFlag<kFlagCanBeNull>(can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00008025 }
8026
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01008027 bool CanBeNull() const override { return GetPackedFlag<kFlagCanBeNull>(); }
Calin Juravlea5ae3c32015-07-28 14:40:50 +00008028
Calin Juravleb1498f62015-02-16 13:13:29 +00008029 DECLARE_INSTRUCTION(BoundType);
8030
Artem Serovcced8ba2017-07-19 18:18:09 +01008031 protected:
8032 DEFAULT_COPY_CONSTRUCTOR(BoundType);
8033
Calin Juravleb1498f62015-02-16 13:13:29 +00008034 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00008035 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
8036 // is false then CanBeNull() cannot be true).
Vladimir Markobd785672018-05-03 17:09:09 +01008037 static constexpr size_t kFlagUpperCanBeNull = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00008038 static constexpr size_t kFlagCanBeNull = kFlagUpperCanBeNull + 1;
8039 static constexpr size_t kNumberOfBoundTypePackedBits = kFlagCanBeNull + 1;
8040 static_assert(kNumberOfBoundTypePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
8041
Calin Juravleb1498f62015-02-16 13:13:29 +00008042 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00008043 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
8044 // It is used to bound the type in cases like:
8045 // if (x instanceof ClassX) {
8046 // // uper_bound_ will be ClassX
8047 // }
David Brazdilf5552582015-12-27 13:36:12 +00008048 ReferenceTypeInfo upper_bound_;
Calin Juravleb1498f62015-02-16 13:13:29 +00008049};
8050
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01008051class HCheckCast final : public HTypeCheckInstruction {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00008052 public:
8053 HCheckCast(HInstruction* object,
Vladimir Marko175e7862018-03-27 09:03:13 +00008054 HInstruction* target_class_or_null,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00008055 TypeCheckKind check_kind,
Vladimir Marko175e7862018-03-27 09:03:13 +00008056 Handle<mirror::Class> klass,
8057 uint32_t dex_pc,
8058 ArenaAllocator* allocator,
8059 HIntConstant* bitstring_path_to_root,
8060 HIntConstant* bitstring_mask)
8061 : HTypeCheckInstruction(kCheckCast,
Vladimir Markobd785672018-05-03 17:09:09 +01008062 DataType::Type::kVoid,
Vladimir Marko175e7862018-03-27 09:03:13 +00008063 object,
8064 target_class_or_null,
8065 check_kind,
8066 klass,
8067 dex_pc,
8068 allocator,
8069 bitstring_path_to_root,
8070 bitstring_mask,
8071 SideEffects::CanTriggerGC()) {}
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00008072
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01008073 bool IsClonable() const override { return true; }
8074 bool NeedsEnvironment() const override {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00008075 // Instruction may throw a CheckCastError.
8076 return true;
8077 }
8078
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01008079 bool CanThrow() const override { return true; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00008080
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00008081 DECLARE_INSTRUCTION(CheckCast);
8082
Artem Serovcced8ba2017-07-19 18:18:09 +01008083 protected:
8084 DEFAULT_COPY_CONSTRUCTOR(CheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00008085};
8086
Andreas Gampe26de38b2016-07-27 17:53:11 -07008087/**
8088 * @brief Memory barrier types (see "The JSR-133 Cookbook for Compiler Writers").
8089 * @details We define the combined barrier types that are actually required
8090 * by the Java Memory Model, rather than using exactly the terminology from
8091 * the JSR-133 cookbook. These should, in many cases, be replaced by acquire/release
8092 * primitives. Note that the JSR-133 cookbook generally does not deal with
8093 * store atomicity issues, and the recipes there are not always entirely sufficient.
8094 * The current recipe is as follows:
8095 * -# Use AnyStore ~= (LoadStore | StoreStore) ~= release barrier before volatile store.
8096 * -# Use AnyAny barrier after volatile store. (StoreLoad is as expensive.)
8097 * -# Use LoadAny barrier ~= (LoadLoad | LoadStore) ~= acquire barrier after each volatile load.
8098 * -# Use StoreStore barrier after all stores but before return from any constructor whose
8099 * class has final fields.
8100 * -# Use NTStoreStore to order non-temporal stores with respect to all later
8101 * store-to-memory instructions. Only generated together with non-temporal stores.
8102 */
8103enum MemBarrierKind {
8104 kAnyStore,
8105 kLoadAny,
8106 kStoreStore,
8107 kAnyAny,
8108 kNTStoreStore,
8109 kLastBarrierKind = kNTStoreStore
8110};
Vladimir Marko9974e3c2020-06-10 16:27:06 +01008111std::ostream& operator<<(std::ostream& os, MemBarrierKind kind);
Andreas Gampe26de38b2016-07-27 17:53:11 -07008112
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01008113class HMemoryBarrier final : public HExpression<0> {
Calin Juravle27df7582015-04-17 19:12:31 +01008114 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06008115 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01008116 : HExpression(kMemoryBarrier,
8117 SideEffects::AllWritesAndReads(), // Assume write/read on all fields/arrays.
8118 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00008119 SetPackedField<BarrierKindField>(barrier_kind);
8120 }
Calin Juravle27df7582015-04-17 19:12:31 +01008121
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01008122 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01008123
Vladimir Markoa1de9182016-02-25 11:37:38 +00008124 MemBarrierKind GetBarrierKind() { return GetPackedField<BarrierKindField>(); }
Calin Juravle27df7582015-04-17 19:12:31 +01008125
8126 DECLARE_INSTRUCTION(MemoryBarrier);
8127
Artem Serovcced8ba2017-07-19 18:18:09 +01008128 protected:
8129 DEFAULT_COPY_CONSTRUCTOR(MemoryBarrier);
8130
Calin Juravle27df7582015-04-17 19:12:31 +01008131 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00008132 static constexpr size_t kFieldBarrierKind = HInstruction::kNumberOfGenericPackedBits;
8133 static constexpr size_t kFieldBarrierKindSize =
8134 MinimumBitsToStore(static_cast<size_t>(kLastBarrierKind));
8135 static constexpr size_t kNumberOfMemoryBarrierPackedBits =
8136 kFieldBarrierKind + kFieldBarrierKindSize;
8137 static_assert(kNumberOfMemoryBarrierPackedBits <= kMaxNumberOfPackedBits,
8138 "Too many packed fields.");
8139 using BarrierKindField = BitField<MemBarrierKind, kFieldBarrierKind, kFieldBarrierKindSize>;
Calin Juravle27df7582015-04-17 19:12:31 +01008140};
8141
Igor Murashkind01745e2017-04-05 16:40:31 -07008142// A constructor fence orders all prior stores to fields that could be accessed via a final field of
8143// the specified object(s), with respect to any subsequent store that might "publish"
8144// (i.e. make visible) the specified object to another thread.
8145//
8146// JLS 17.5.1 "Semantics of final fields" states that a freeze action happens
8147// for all final fields (that were set) at the end of the invoked constructor.
8148//
8149// The constructor fence models the freeze actions for the final fields of an object
8150// being constructed (semantically at the end of the constructor). Constructor fences
8151// have a per-object affinity; two separate objects being constructed get two separate
8152// constructor fences.
8153//
8154// (Note: that if calling a super-constructor or forwarding to another constructor,
8155// the freezes would happen at the end of *that* constructor being invoked).
8156//
8157// The memory model guarantees that when the object being constructed is "published" after
8158// constructor completion (i.e. escapes the current thread via a store), then any final field
8159// writes must be observable on other threads (once they observe that publication).
8160//
8161// Further, anything written before the freeze, and read by dereferencing through the final field,
8162// must also be visible (so final object field could itself have an object with non-final fields;
8163// yet the freeze must also extend to them).
8164//
8165// Constructor example:
8166//
8167// class HasFinal {
8168// final int field; Optimizing IR for <init>()V:
8169// HasFinal() {
8170// field = 123; HInstanceFieldSet(this, HasFinal.field, 123)
8171// // freeze(this.field); HConstructorFence(this)
8172// } HReturn
8173// }
8174//
8175// HConstructorFence can serve double duty as a fence for new-instance/new-array allocations of
8176// already-initialized classes; in that case the allocation must act as a "default-initializer"
8177// of the object which effectively writes the class pointer "final field".
8178//
8179// For example, we can model default-initialiation as roughly the equivalent of the following:
8180//
8181// class Object {
8182// private final Class header;
8183// }
8184//
8185// Java code: Optimizing IR:
8186//
8187// T new_instance<T>() {
8188// Object obj = allocate_memory(T.class.size); obj = HInvoke(art_quick_alloc_object, T)
8189// obj.header = T.class; // header write is done by above call.
8190// // freeze(obj.header) HConstructorFence(obj)
8191// return (T)obj;
8192// }
8193//
8194// See also:
Vladimir Markoc1c34522018-10-31 13:56:49 +00008195// * DexCompilationUnit::RequiresConstructorBarrier
Igor Murashkind01745e2017-04-05 16:40:31 -07008196// * QuasiAtomic::ThreadFenceForConstructor
8197//
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01008198class HConstructorFence final : public HVariableInputSizeInstruction {
Igor Murashkind01745e2017-04-05 16:40:31 -07008199 // A fence has variable inputs because the inputs can be removed
8200 // after prepare_for_register_allocation phase.
8201 // (TODO: In the future a fence could freeze multiple objects
8202 // after merging two fences together.)
8203 public:
8204 // `fence_object` is the reference that needs to be protected for correct publication.
8205 //
8206 // It makes sense in the following situations:
8207 // * <init> constructors, it's the "this" parameter (i.e. HParameterValue, s.t. IsThis() == true).
8208 // * new-instance-like instructions, it's the return value (i.e. HNewInstance).
8209 //
8210 // After construction the `fence_object` becomes the 0th input.
8211 // This is not an input in a real sense, but just a convenient place to stash the information
8212 // about the associated object.
8213 HConstructorFence(HInstruction* fence_object,
8214 uint32_t dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01008215 ArenaAllocator* allocator)
Igor Murashkind01745e2017-04-05 16:40:31 -07008216 // We strongly suspect there is not a more accurate way to describe the fine-grained reordering
8217 // constraints described in the class header. We claim that these SideEffects constraints
8218 // enforce a superset of the real constraints.
8219 //
8220 // The ordering described above is conservatively modeled with SideEffects as follows:
8221 //
8222 // * To prevent reordering of the publication stores:
8223 // ----> "Reads of objects" is the initial SideEffect.
8224 // * For every primitive final field store in the constructor:
8225 // ----> Union that field's type as a read (e.g. "Read of T") into the SideEffect.
8226 // * If there are any stores to reference final fields in the constructor:
8227 // ----> Use a more conservative "AllReads" SideEffect because any stores to any references
8228 // that are reachable from `fence_object` also need to be prevented for reordering
8229 // (and we do not want to do alias analysis to figure out what those stores are).
8230 //
8231 // In the implementation, this initially starts out as an "all reads" side effect; this is an
8232 // even more conservative approach than the one described above, and prevents all of the
8233 // above reordering without analyzing any of the instructions in the constructor.
8234 //
8235 // If in a later phase we discover that there are no writes to reference final fields,
8236 // we can refine the side effect to a smaller set of type reads (see above constraints).
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05308237 : HVariableInputSizeInstruction(kConstructorFence,
8238 SideEffects::AllReads(),
Igor Murashkind01745e2017-04-05 16:40:31 -07008239 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01008240 allocator,
Andreas Gampe3db70682018-12-26 15:12:03 -08008241 /* number_of_inputs= */ 1,
Igor Murashkind01745e2017-04-05 16:40:31 -07008242 kArenaAllocConstructorFenceInputs) {
8243 DCHECK(fence_object != nullptr);
8244 SetRawInputAt(0, fence_object);
8245 }
8246
8247 // The object associated with this constructor fence.
8248 //
8249 // (Note: This will be null after the prepare_for_register_allocation phase,
8250 // as all constructor fence inputs are removed there).
8251 HInstruction* GetFenceObject() const {
8252 return InputAt(0);
8253 }
8254
8255 // Find all the HConstructorFence uses (`fence_use`) for `this` and:
8256 // - Delete `fence_use` from `this`'s use list.
8257 // - Delete `this` from `fence_use`'s inputs list.
8258 // - If the `fence_use` is dead, remove it from the graph.
8259 //
8260 // A fence is considered dead once it no longer has any uses
8261 // and all of the inputs are dead.
8262 //
8263 // This must *not* be called during/after prepare_for_register_allocation,
8264 // because that removes all the inputs to the fences but the fence is actually
8265 // still considered live.
Igor Murashkin6ef45672017-08-08 13:59:55 -07008266 //
8267 // Returns how many HConstructorFence instructions were removed from graph.
8268 static size_t RemoveConstructorFences(HInstruction* instruction);
Igor Murashkind01745e2017-04-05 16:40:31 -07008269
Igor Murashkindd018df2017-08-09 10:38:31 -07008270 // Combine all inputs of `this` and `other` instruction and remove
8271 // `other` from the graph.
8272 //
8273 // Inputs are unique after the merge.
8274 //
8275 // Requirement: `this` must not be the same as `other.
8276 void Merge(HConstructorFence* other);
8277
Igor Murashkin79d8fa72017-04-18 09:37:23 -07008278 // Check if this constructor fence is protecting
8279 // an HNewInstance or HNewArray that is also the immediate
8280 // predecessor of `this`.
8281 //
Igor Murashkindd018df2017-08-09 10:38:31 -07008282 // If `ignore_inputs` is true, then the immediate predecessor doesn't need
8283 // to be one of the inputs of `this`.
8284 //
Igor Murashkin79d8fa72017-04-18 09:37:23 -07008285 // Returns the associated HNewArray or HNewInstance,
8286 // or null otherwise.
Igor Murashkindd018df2017-08-09 10:38:31 -07008287 HInstruction* GetAssociatedAllocation(bool ignore_inputs = false);
Igor Murashkin79d8fa72017-04-18 09:37:23 -07008288
Igor Murashkind01745e2017-04-05 16:40:31 -07008289 DECLARE_INSTRUCTION(ConstructorFence);
8290
Artem Serovcced8ba2017-07-19 18:18:09 +01008291 protected:
8292 DEFAULT_COPY_CONSTRUCTOR(ConstructorFence);
Igor Murashkind01745e2017-04-05 16:40:31 -07008293};
8294
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01008295class HMonitorOperation final : public HExpression<1> {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00008296 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00008297 enum class OperationKind {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00008298 kEnter,
8299 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00008300 kLast = kExit
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00008301 };
8302
8303 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01008304 : HExpression(kMonitorOperation,
8305 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
8306 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00008307 SetPackedField<OperationKindField>(kind);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00008308 SetRawInputAt(0, object);
8309 }
8310
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00008311 // Instruction may go into runtime, so we need an environment.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01008312 bool NeedsEnvironment() const override { return true; }
David Brazdilbff75032015-07-08 17:26:51 +00008313
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01008314 bool CanThrow() const override {
David Brazdilbff75032015-07-08 17:26:51 +00008315 // Verifier guarantees that monitor-exit cannot throw.
8316 // This is important because it allows the HGraphBuilder to remove
8317 // a dead throw-catch loop generated for `synchronized` blocks/methods.
8318 return IsEnter();
8319 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00008320
Vladimir Markoa1de9182016-02-25 11:37:38 +00008321 OperationKind GetOperationKind() const { return GetPackedField<OperationKindField>(); }
8322 bool IsEnter() const { return GetOperationKind() == OperationKind::kEnter; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00008323
8324 DECLARE_INSTRUCTION(MonitorOperation);
8325
Artem Serovcced8ba2017-07-19 18:18:09 +01008326 protected:
8327 DEFAULT_COPY_CONSTRUCTOR(MonitorOperation);
8328
Calin Juravle52c48962014-12-16 17:02:57 +00008329 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00008330 static constexpr size_t kFieldOperationKind = HInstruction::kNumberOfGenericPackedBits;
8331 static constexpr size_t kFieldOperationKindSize =
8332 MinimumBitsToStore(static_cast<size_t>(OperationKind::kLast));
8333 static constexpr size_t kNumberOfMonitorOperationPackedBits =
8334 kFieldOperationKind + kFieldOperationKindSize;
8335 static_assert(kNumberOfMonitorOperationPackedBits <= HInstruction::kMaxNumberOfPackedBits,
8336 "Too many packed fields.");
8337 using OperationKindField = BitField<OperationKind, kFieldOperationKind, kFieldOperationKindSize>;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00008338};
8339
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01008340class HSelect final : public HExpression<3> {
David Brazdil74eb1b22015-12-14 11:44:01 +00008341 public:
8342 HSelect(HInstruction* condition,
8343 HInstruction* true_value,
8344 HInstruction* false_value,
8345 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05308346 : HExpression(kSelect, HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
David Brazdil74eb1b22015-12-14 11:44:01 +00008347 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
8348
8349 // First input must be `true_value` or `false_value` to allow codegens to
8350 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
8351 // that architectures which implement HSelect as a conditional move also
8352 // will not need to invert the condition.
8353 SetRawInputAt(0, false_value);
8354 SetRawInputAt(1, true_value);
8355 SetRawInputAt(2, condition);
8356 }
8357
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01008358 bool IsClonable() const override { return true; }
David Brazdil74eb1b22015-12-14 11:44:01 +00008359 HInstruction* GetFalseValue() const { return InputAt(0); }
8360 HInstruction* GetTrueValue() const { return InputAt(1); }
8361 HInstruction* GetCondition() const { return InputAt(2); }
8362
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01008363 bool CanBeMoved() const override { return true; }
Stefano Cianciulli78f3c722023-05-16 10:32:54 +00008364 bool InstructionDataEquals([[maybe_unused]] const HInstruction* other) const override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01008365 return true;
8366 }
David Brazdil74eb1b22015-12-14 11:44:01 +00008367
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01008368 bool CanBeNull() const override {
David Brazdil74eb1b22015-12-14 11:44:01 +00008369 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
8370 }
8371
8372 DECLARE_INSTRUCTION(Select);
8373
Artem Serovcced8ba2017-07-19 18:18:09 +01008374 protected:
8375 DEFAULT_COPY_CONSTRUCTOR(Select);
David Brazdil74eb1b22015-12-14 11:44:01 +00008376};
8377
Vladimir Markof9f64412015-09-02 14:05:49 +01008378class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01008379 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01008380 MoveOperands(Location source,
8381 Location destination,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008382 DataType::Type type,
Nicolas Geoffray90218252015-04-15 11:56:51 +01008383 HInstruction* instruction)
8384 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01008385
8386 Location GetSource() const { return source_; }
8387 Location GetDestination() const { return destination_; }
8388
8389 void SetSource(Location value) { source_ = value; }
8390 void SetDestination(Location value) { destination_ = value; }
8391
8392 // The parallel move resolver marks moves as "in-progress" by clearing the
8393 // destination (but not the source).
8394 Location MarkPending() {
8395 DCHECK(!IsPending());
8396 Location dest = destination_;
8397 destination_ = Location::NoLocation();
8398 return dest;
8399 }
8400
8401 void ClearPending(Location dest) {
8402 DCHECK(IsPending());
8403 destination_ = dest;
8404 }
8405
8406 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00008407 DCHECK(source_.IsValid() || destination_.IsInvalid());
8408 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01008409 }
8410
8411 // True if this blocks a move from the given location.
8412 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08008413 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01008414 }
8415
8416 // A move is redundant if it's been eliminated, if its source and
8417 // destination are the same, or if its destination is unneeded.
8418 bool IsRedundant() const {
8419 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
8420 }
8421
8422 // We clear both operands to indicate move that's been eliminated.
8423 void Eliminate() {
8424 source_ = destination_ = Location::NoLocation();
8425 }
8426
8427 bool IsEliminated() const {
Santiago Aboy Solanes872ec722022-02-18 14:10:25 +00008428 DCHECK_IMPLIES(source_.IsInvalid(), destination_.IsInvalid());
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01008429 return source_.IsInvalid();
8430 }
8431
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008432 DataType::Type GetType() const { return type_; }
Alexey Frunze4dda3372015-06-01 18:31:49 -07008433
Nicolas Geoffray90218252015-04-15 11:56:51 +01008434 bool Is64BitMove() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008435 return DataType::Is64BitType(type_);
Nicolas Geoffray90218252015-04-15 11:56:51 +01008436 }
8437
Nicolas Geoffray740475d2014-09-29 10:33:25 +01008438 HInstruction* GetInstruction() const { return instruction_; }
8439
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01008440 private:
8441 Location source_;
8442 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01008443 // The type this move is for.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008444 DataType::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01008445 // The instruction this move is assocatied with. Null when this move is
8446 // for moving an input in the expected locations of user (including a phi user).
8447 // This is only used in debug mode, to ensure we do not connect interval siblings
8448 // in the same parallel move.
8449 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01008450};
8451
Roland Levillainc9285912015-12-18 10:38:42 +00008452std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
8453
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01008454static constexpr size_t kDefaultNumberOfMoves = 4;
8455
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01008456class HParallelMove final : public HExpression<0> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01008457 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01008458 explicit HParallelMove(ArenaAllocator* allocator, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01008459 : HExpression(kParallelMove, SideEffects::None(), dex_pc),
Vladimir Markoe764d2e2017-10-05 14:35:55 +01008460 moves_(allocator->Adapter(kArenaAllocMoveOperands)) {
Vladimir Marko225b6462015-09-28 12:17:40 +01008461 moves_.reserve(kDefaultNumberOfMoves);
8462 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01008463
Nicolas Geoffray90218252015-04-15 11:56:51 +01008464 void AddMove(Location source,
8465 Location destination,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008466 DataType::Type type,
Nicolas Geoffray90218252015-04-15 11:56:51 +01008467 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00008468 DCHECK(source.IsValid());
8469 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00008470 if (kIsDebugBuild) {
8471 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01008472 for (const MoveOperands& move : moves_) {
8473 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00008474 // Special case the situation where the move is for the spill slot
8475 // of the instruction.
8476 if ((GetPrevious() == instruction)
8477 || ((GetPrevious() == nullptr)
8478 && instruction->IsPhi()
8479 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01008480 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00008481 << "Doing parallel moves for the same instruction.";
8482 } else {
8483 DCHECK(false) << "Doing parallel moves for the same instruction.";
8484 }
8485 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00008486 }
8487 }
Vladimir Marko225b6462015-09-28 12:17:40 +01008488 for (const MoveOperands& move : moves_) {
8489 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01008490 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01008491 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Alex Lighta7c66e72021-01-27 11:41:58 -08008492 << source << " ==> " << destination << " for " << SafePrint(instruction);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00008493 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01008494 }
Vladimir Marko225b6462015-09-28 12:17:40 +01008495 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01008496 }
8497
Vladimir Marko225b6462015-09-28 12:17:40 +01008498 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01008499 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01008500 }
8501
Vladimir Marko225b6462015-09-28 12:17:40 +01008502 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01008503
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01008504 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01008505
Artem Serovcced8ba2017-07-19 18:18:09 +01008506 protected:
8507 DEFAULT_COPY_CONSTRUCTOR(ParallelMove);
8508
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01008509 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01008510 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01008511};
8512
xueliang.zhonge0eb4832017-10-30 13:43:14 +00008513// This instruction computes an intermediate address pointing in the 'middle' of an object. The
8514// result pointer cannot be handled by GC, so extra care is taken to make sure that this value is
8515// never used across anything that can trigger GC.
8516// The result of this instruction is not a pointer in the sense of `DataType::Type::kreference`.
8517// So we represent it by the type `DataType::Type::kInt`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01008518class HIntermediateAddress final : public HExpression<2> {
xueliang.zhonge0eb4832017-10-30 13:43:14 +00008519 public:
8520 HIntermediateAddress(HInstruction* base_address, HInstruction* offset, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05308521 : HExpression(kIntermediateAddress,
8522 DataType::Type::kInt32,
8523 SideEffects::DependsOnGC(),
8524 dex_pc) {
xueliang.zhonge0eb4832017-10-30 13:43:14 +00008525 DCHECK_EQ(DataType::Size(DataType::Type::kInt32),
8526 DataType::Size(DataType::Type::kReference))
8527 << "kPrimInt and kPrimNot have different sizes.";
8528 SetRawInputAt(0, base_address);
8529 SetRawInputAt(1, offset);
8530 }
8531
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01008532 bool IsClonable() const override { return true; }
8533 bool CanBeMoved() const override { return true; }
Stefano Cianciulli78f3c722023-05-16 10:32:54 +00008534 bool InstructionDataEquals([[maybe_unused]] const HInstruction* other) const override {
xueliang.zhonge0eb4832017-10-30 13:43:14 +00008535 return true;
8536 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01008537 bool IsActualObject() const override { return false; }
xueliang.zhonge0eb4832017-10-30 13:43:14 +00008538
8539 HInstruction* GetBaseAddress() const { return InputAt(0); }
8540 HInstruction* GetOffset() const { return InputAt(1); }
8541
8542 DECLARE_INSTRUCTION(IntermediateAddress);
8543
Artem Serovcced8ba2017-07-19 18:18:09 +01008544 protected:
8545 DEFAULT_COPY_CONSTRUCTOR(IntermediateAddress);
xueliang.zhonge0eb4832017-10-30 13:43:14 +00008546};
8547
8548
Mark Mendell0616ae02015-04-17 12:49:27 -04008549} // namespace art
8550
Aart Bikf8f5a162017-02-06 15:35:29 -08008551#include "nodes_vector.h"
8552
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03008553#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
8554#include "nodes_shared.h"
8555#endif
Shalini Salomi Bodapatidd121f62018-10-26 15:03:53 +05308556#if defined(ART_ENABLE_CODEGEN_x86) || defined(ART_ENABLE_CODEGEN_x86_64)
Mark Mendell0616ae02015-04-17 12:49:27 -04008557#include "nodes_x86.h"
8558#endif
8559
Vladimír Marko434d9682022-11-04 14:04:17 +00008560namespace art HIDDEN {
Mark Mendell0616ae02015-04-17 12:49:27 -04008561
Igor Murashkin6ef45672017-08-08 13:59:55 -07008562class OptimizingCompilerStats;
8563
Nicolas Geoffray818f2102014-02-18 16:43:35 +00008564class HGraphVisitor : public ValueObject {
8565 public:
Igor Murashkin6ef45672017-08-08 13:59:55 -07008566 explicit HGraphVisitor(HGraph* graph, OptimizingCompilerStats* stats = nullptr)
8567 : stats_(stats),
8568 graph_(graph) {}
Dave Allison20dfc792014-06-16 20:44:29 -07008569 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00008570
Stefano Cianciulli78f3c722023-05-16 10:32:54 +00008571 virtual void VisitInstruction([[maybe_unused]] HInstruction* instruction) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00008572 virtual void VisitBasicBlock(HBasicBlock* block);
8573
Roland Levillain633021e2014-10-01 14:12:25 +01008574 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00008575 void VisitInsertionOrder();
8576
Roland Levillain633021e2014-10-01 14:12:25 +01008577 // Visit the graph following dominator tree reverse post-order.
8578 void VisitReversePostOrder();
8579
Nicolas Geoffray787c3072014-03-17 10:20:19 +00008580 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00008581
Nicolas Geoffray818f2102014-02-18 16:43:35 +00008582 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01008583#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00008584 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
8585
8586 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
8587
8588#undef DECLARE_VISIT_INSTRUCTION
8589
Igor Murashkin6ef45672017-08-08 13:59:55 -07008590 protected:
8591 OptimizingCompilerStats* stats_;
8592
Nicolas Geoffray818f2102014-02-18 16:43:35 +00008593 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07008594 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00008595
8596 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
8597};
8598
Nicolas Geoffray360231a2014-10-08 21:07:48 +01008599class HGraphDelegateVisitor : public HGraphVisitor {
8600 public:
Igor Murashkin6ef45672017-08-08 13:59:55 -07008601 explicit HGraphDelegateVisitor(HGraph* graph, OptimizingCompilerStats* stats = nullptr)
8602 : HGraphVisitor(graph, stats) {}
Nicolas Geoffray360231a2014-10-08 21:07:48 +01008603 virtual ~HGraphDelegateVisitor() {}
8604
8605 // Visit functions that delegate to to super class.
8606#define DECLARE_VISIT_INSTRUCTION(name, super) \
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01008607 void Visit##name(H##name* instr) override { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01008608
8609 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
8610
8611#undef DECLARE_VISIT_INSTRUCTION
8612
8613 private:
8614 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
8615};
8616
Artem Serovcced8ba2017-07-19 18:18:09 +01008617// Create a clone of the instruction, insert it into the graph; replace the old one with a new
8618// and remove the old instruction.
8619HInstruction* ReplaceInstrOrPhiByClone(HInstruction* instr);
8620
8621// Create a clone for each clonable instructions/phis and replace the original with the clone.
8622//
8623// Used for testing individual instruction cloner.
Vladimir Marko30759fa2023-04-05 09:43:21 +02008624class CloneAndReplaceInstructionVisitor final : public HGraphDelegateVisitor {
Artem Serovcced8ba2017-07-19 18:18:09 +01008625 public:
8626 explicit CloneAndReplaceInstructionVisitor(HGraph* graph)
Artem Serov7f4aff62017-06-21 17:02:18 +01008627 : HGraphDelegateVisitor(graph), instr_replaced_by_clones_count_(0) {}
Artem Serovcced8ba2017-07-19 18:18:09 +01008628
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01008629 void VisitInstruction(HInstruction* instruction) override {
Artem Serovcced8ba2017-07-19 18:18:09 +01008630 if (instruction->IsClonable()) {
8631 ReplaceInstrOrPhiByClone(instruction);
Artem Serov7f4aff62017-06-21 17:02:18 +01008632 instr_replaced_by_clones_count_++;
Artem Serovcced8ba2017-07-19 18:18:09 +01008633 }
8634 }
8635
Artem Serov7f4aff62017-06-21 17:02:18 +01008636 size_t GetInstrReplacedByClonesCount() const { return instr_replaced_by_clones_count_; }
Artem Serovcced8ba2017-07-19 18:18:09 +01008637
8638 private:
Artem Serov7f4aff62017-06-21 17:02:18 +01008639 size_t instr_replaced_by_clones_count_;
Artem Serovcced8ba2017-07-19 18:18:09 +01008640
8641 DISALLOW_COPY_AND_ASSIGN(CloneAndReplaceInstructionVisitor);
8642};
8643
Artem Serov0b284aa2021-12-14 23:16:21 +00008644// Iterator over the blocks that are part of the loop; includes blocks which are part
Nicolas Geoffray82091da2015-01-26 10:02:45 +00008645// of an inner loop. The order in which the blocks are iterated is on their
8646// block id.
8647class HBlocksInLoopIterator : public ValueObject {
8648 public:
8649 explicit HBlocksInLoopIterator(const HLoopInformation& info)
8650 : blocks_in_loop_(info.GetBlocks()),
8651 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
8652 index_(0) {
8653 if (!blocks_in_loop_.IsBitSet(index_)) {
8654 Advance();
8655 }
8656 }
8657
Vladimir Markofa6b93c2015-09-15 10:15:55 +01008658 bool Done() const { return index_ == blocks_.size(); }
8659 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00008660 void Advance() {
8661 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01008662 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00008663 if (blocks_in_loop_.IsBitSet(index_)) {
8664 break;
8665 }
8666 }
8667 }
8668
8669 private:
8670 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01008671 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00008672 size_t index_;
8673
8674 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
8675};
8676
Artem Serov0b284aa2021-12-14 23:16:21 +00008677// Iterator over the blocks that are part of the loop; includes blocks which are part
Mingyao Yang3584bce2015-05-19 16:01:59 -07008678// of an inner loop. The order in which the blocks are iterated is reverse
8679// post order.
8680class HBlocksInLoopReversePostOrderIterator : public ValueObject {
8681 public:
8682 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
8683 : blocks_in_loop_(info.GetBlocks()),
8684 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
8685 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01008686 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07008687 Advance();
8688 }
8689 }
8690
Vladimir Markofa6b93c2015-09-15 10:15:55 +01008691 bool Done() const { return index_ == blocks_.size(); }
8692 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07008693 void Advance() {
8694 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01008695 for (size_t e = blocks_.size(); index_ < e; ++index_) {
8696 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07008697 break;
8698 }
8699 }
8700 }
8701
8702 private:
8703 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01008704 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07008705 size_t index_;
8706
8707 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
8708};
8709
Artem Serov0b284aa2021-12-14 23:16:21 +00008710// Iterator over the blocks that are part of the loop; includes blocks which are part
8711// of an inner loop. The order in which the blocks are iterated is post order.
8712class HBlocksInLoopPostOrderIterator : public ValueObject {
8713 public:
8714 explicit HBlocksInLoopPostOrderIterator(const HLoopInformation& info)
8715 : blocks_in_loop_(info.GetBlocks()),
8716 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
8717 index_(blocks_.size() - 1) {
8718 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
8719 Advance();
8720 }
8721 }
8722
8723 bool Done() const { return index_ < 0; }
8724 HBasicBlock* Current() const { return blocks_[index_]; }
8725 void Advance() {
8726 --index_;
8727 for (; index_ >= 0; --index_) {
8728 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
8729 break;
8730 }
8731 }
8732 }
8733
8734 private:
8735 const BitVector& blocks_in_loop_;
8736 const ArenaVector<HBasicBlock*>& blocks_;
8737
8738 int32_t index_;
8739
8740 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopPostOrderIterator);
8741};
8742
Aart Bikf3e61ee2017-04-12 17:09:20 -07008743// Returns int64_t value of a properly typed constant.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00008744inline int64_t Int64FromConstant(HConstant* constant) {
David Brazdilc0b601b2016-02-08 14:20:45 +00008745 if (constant->IsIntConstant()) {
Vladimir Markocde64972023-04-25 16:40:06 +00008746 return constant->AsIntConstant()->GetValue();
David Brazdilc0b601b2016-02-08 14:20:45 +00008747 } else if (constant->IsLongConstant()) {
Vladimir Markocde64972023-04-25 16:40:06 +00008748 return constant->AsLongConstant()->GetValue();
David Brazdilc0b601b2016-02-08 14:20:45 +00008749 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00008750 DCHECK(constant->IsNullConstant()) << constant->DebugName();
David Brazdilc0b601b2016-02-08 14:20:45 +00008751 return 0;
8752 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00008753}
8754
Aart Bikf3e61ee2017-04-12 17:09:20 -07008755// Returns true iff instruction is an integral constant (and sets value on success).
8756inline bool IsInt64AndGet(HInstruction* instruction, /*out*/ int64_t* value) {
8757 if (instruction->IsIntConstant()) {
Vladimir Markocde64972023-04-25 16:40:06 +00008758 *value = instruction->AsIntConstant()->GetValue();
Aart Bikf3e61ee2017-04-12 17:09:20 -07008759 return true;
8760 } else if (instruction->IsLongConstant()) {
Vladimir Markocde64972023-04-25 16:40:06 +00008761 *value = instruction->AsLongConstant()->GetValue();
Aart Bikf3e61ee2017-04-12 17:09:20 -07008762 return true;
8763 } else if (instruction->IsNullConstant()) {
8764 *value = 0;
8765 return true;
8766 }
8767 return false;
8768}
8769
Aart Bik0148de42017-09-05 09:25:01 -07008770// Returns true iff instruction is the given integral constant.
8771inline bool IsInt64Value(HInstruction* instruction, int64_t value) {
8772 int64_t val = 0;
8773 return IsInt64AndGet(instruction, &val) && val == value;
8774}
8775
8776// Returns true iff instruction is a zero bit pattern.
8777inline bool IsZeroBitPattern(HInstruction* instruction) {
Vladimir Markocde64972023-04-25 16:40:06 +00008778 return instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern();
Aart Bik0148de42017-09-05 09:25:01 -07008779}
8780
Vladimir Marko6d5b7e32018-05-09 16:52:48 +01008781// Implement HInstruction::Is##type() for concrete instructions.
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008782#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Markoa90dd512018-05-04 15:04:45 +01008783 inline bool HInstruction::Is##type() const { return GetKind() == k##type; }
8784 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
8785#undef INSTRUCTION_TYPE_CHECK
8786
Vladimir Marko6d5b7e32018-05-09 16:52:48 +01008787// Implement HInstruction::Is##type() for abstract instructions.
Vladimir Markoa90dd512018-05-04 15:04:45 +01008788#define INSTRUCTION_TYPE_CHECK_RESULT(type, super) \
8789 std::is_base_of<BaseType, H##type>::value,
8790#define INSTRUCTION_TYPE_CHECK(type, super) \
8791 inline bool HInstruction::Is##type() const { \
8792 DCHECK_LT(GetKind(), kLastInstructionKind); \
8793 using BaseType = H##type; \
8794 static constexpr bool results[] = { \
8795 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK_RESULT) \
8796 }; \
8797 return results[static_cast<size_t>(GetKind())]; \
8798 }
8799
8800 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
8801#undef INSTRUCTION_TYPE_CHECK
Vladimir Marko6d5b7e32018-05-09 16:52:48 +01008802#undef INSTRUCTION_TYPE_CHECK_RESULT
Vladimir Markoa90dd512018-05-04 15:04:45 +01008803
8804#define INSTRUCTION_TYPE_CAST(type, super) \
Vladimir Markocde64972023-04-25 16:40:06 +00008805 inline const H##type* HInstruction::As##type() const { \
8806 DCHECK(Is##type()); \
8807 return down_cast<const H##type*>(this); \
8808 } \
8809 inline H##type* HInstruction::As##type() { \
8810 DCHECK(Is##type()); \
8811 return down_cast<H##type*>(this); \
8812 } \
Vladimir Marko79dc2172023-04-05 10:33:07 +00008813 inline const H##type* HInstruction::As##type##OrNull() const { \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008814 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
8815 } \
Vladimir Marko79dc2172023-04-05 10:33:07 +00008816 inline H##type* HInstruction::As##type##OrNull() { \
Vladimir Markof2eef5f2023-04-06 10:29:19 +00008817 return Is##type() ? down_cast<H##type*>(this) : nullptr; \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008818 }
8819
Vladimir Markoa90dd512018-05-04 15:04:45 +01008820 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CAST)
8821#undef INSTRUCTION_TYPE_CAST
8822
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008823
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00008824// Create space in `blocks` for adding `number_of_new_blocks` entries
8825// starting at location `at`. Blocks after `at` are moved accordingly.
8826inline void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks,
8827 size_t number_of_new_blocks,
8828 size_t after) {
8829 DCHECK_LT(after, blocks->size());
8830 size_t old_size = blocks->size();
8831 size_t new_size = old_size + number_of_new_blocks;
8832 blocks->resize(new_size);
8833 std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end());
8834}
8835
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00008836/*
8837 * Hunt "under the hood" of array lengths (leading to array references),
8838 * null checks (also leading to array references), and new arrays
8839 * (leading to the actual length). This makes it more likely related
8840 * instructions become actually comparable.
8841 */
8842inline HInstruction* HuntForDeclaration(HInstruction* instruction) {
8843 while (instruction->IsArrayLength() ||
8844 instruction->IsNullCheck() ||
8845 instruction->IsNewArray()) {
8846 instruction = instruction->IsNewArray()
Vladimir Markocde64972023-04-25 16:40:06 +00008847 ? instruction->AsNewArray()->GetLength()
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00008848 : instruction->InputAt(0);
8849 }
8850 return instruction;
8851}
8852
Artem Serovb47b9782019-12-04 21:02:09 +00008853inline bool IsAddOrSub(const HInstruction* instruction) {
8854 return instruction->IsAdd() || instruction->IsSub();
8855}
8856
Artem Serov21c7e6f2017-07-27 16:04:42 +01008857void RemoveEnvironmentUses(HInstruction* instruction);
8858bool HasEnvironmentUsedByOthers(HInstruction* instruction);
8859void ResetEnvironmentInputRecords(HInstruction* instruction);
8860
Evgeny Astigeevichaf92a0f2020-06-26 13:28:33 +01008861// Detects an instruction that is >= 0. As long as the value is carried by
8862// a single instruction, arithmetic wrap-around cannot occur.
8863bool IsGEZero(HInstruction* instruction);
8864
Nicolas Geoffray818f2102014-02-18 16:43:35 +00008865} // namespace art
8866
8867#endif // ART_COMPILER_OPTIMIZING_NODES_H_