blob: 06fb88e8377db3e11983668c871de5a5c3f7c49b [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
Alex Light86fe9b82020-11-16 16:54:01 +000024#include "base/arena_allocator.h"
Mathieu Chartiere5d80f82015-10-15 17:47:48 -070025#include "base/arena_bit_vector.h"
David Brazdil8d5b8b22015-03-24 10:51:52 +000026#include "base/arena_containers.h"
Mathieu Chartierb666f482015-02-18 14:33:14 -080027#include "base/arena_object.h"
David Brazdild9c90372016-09-14 16:53:55 +010028#include "base/array_ref.h"
Vladimir Marko3fae1292019-06-07 11:26:25 +010029#include "base/intrusive_forward_list.h"
Vladimir Marko2c45bc92016-10-25 16:54:12 +010030#include "base/iteration_range.h"
Andreas Gampe7fbc4a52018-11-28 08:26:47 -080031#include "base/mutex.h"
David Sehrc431b9d2018-03-02 12:01:51 -080032#include "base/quasi_atomic.h"
Vladimir Marko60584552015-09-03 13:35:12 +000033#include "base/stl_util.h"
David Brazdild9c90372016-09-14 16:53:55 +010034#include "base/transform_array_ref.h"
Nicolas Geoffray76d4bb0f32018-09-21 12:58:45 +010035#include "art_method.h"
Alex Lightdc281e72021-01-06 12:35:31 -080036#include "block_namer.h"
Vladimir Marko02ca05a2020-05-12 13:58:51 +010037#include "class_root.h"
Nicolas Geoffray0d60a2b2020-06-17 14:31:56 +010038#include "compilation_kind.h"
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010039#include "data_type.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070040#include "deoptimization_kind.h"
David Sehr9e734c72018-01-04 17:56:19 -080041#include "dex/dex_file.h"
42#include "dex/dex_file_types.h"
David Sehr8c0961f2018-01-23 16:11:38 -080043#include "dex/invoke_type.h"
David Sehr312f3b22018-03-19 08:39:26 -070044#include "dex/method_reference.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000045#include "entrypoints/quick/quick_entrypoints_enum.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000046#include "handle.h"
47#include "handle_scope.h"
Nicolas Geoffray762869d2016-07-15 15:28:35 +010048#include "intrinsics_enum.h"
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010049#include "locations.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000050#include "mirror/class.h"
Orion Hodson18259d72018-04-12 11:18:23 +010051#include "mirror/method_type.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010052#include "offsets.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000053
Vladimir Marko0a516052019-10-14 13:00:44 +000054namespace art {
Nicolas Geoffray818f2102014-02-18 16:43:35 +000055
Vladimir Markoca6fff82017-10-03 14:49:14 +010056class ArenaStack;
David Brazdil1abb4192015-02-17 18:33:36 +000057class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000058class HBasicBlock;
Igor Murashkind01745e2017-04-05 16:40:31 -070059class HConstructorFence;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010060class HCurrentMethod;
David Brazdil8d5b8b22015-03-24 10:51:52 +000061class HDoubleConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010062class HEnvironment;
David Brazdil8d5b8b22015-03-24 10:51:52 +000063class HFloatConstant;
David Brazdilfc6a86a2015-06-26 10:33:45 +000064class HGraphBuilder;
David Brazdil8d5b8b22015-03-24 10:51:52 +000065class HGraphVisitor;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000066class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000067class HIntConstant;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000068class HInvoke;
David Brazdil8d5b8b22015-03-24 10:51:52 +000069class HLongConstant;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +000070class HNullConstant;
Igor Murashkind01745e2017-04-05 16:40:31 -070071class HParameterValue;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010072class HPhi;
Nicolas Geoffray3c049742014-09-24 18:10:46 +010073class HSuspendCheck;
David Brazdilffee3d32015-07-06 11:48:53 +010074class HTryBoundary;
Alex Light3a73ffb2021-01-25 14:11:05 +000075class FieldInfo;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010076class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000077class LocationSummary;
Nicolas Geoffraydb216f42015-05-05 17:02:20 +010078class SlowPathCode;
David Brazdil8d5b8b22015-03-24 10:51:52 +000079class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000080
Mathieu Chartier736b5602015-09-02 14:54:11 -070081namespace mirror {
82class DexCache;
83} // namespace mirror
84
Nicolas Geoffray818f2102014-02-18 16:43:35 +000085static const int kDefaultNumberOfBlocks = 8;
86static const int kDefaultNumberOfSuccessors = 2;
87static const int kDefaultNumberOfPredecessors = 2;
David Brazdilb618ade2015-07-29 10:31:29 +010088static const int kDefaultNumberOfExceptionalPredecessors = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010089static const int kDefaultNumberOfDominatedBlocks = 1;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000090static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000091
Roland Levillain5b5b9312016-03-22 14:57:31 +000092// The maximum (meaningful) distance (31) that can be used in an integer shift/rotate operation.
93static constexpr int32_t kMaxIntShiftDistance = 0x1f;
94// The maximum (meaningful) distance (63) that can be used in a long shift/rotate operation.
95static constexpr int32_t kMaxLongShiftDistance = 0x3f;
Calin Juravle9aec02f2014-11-18 23:06:35 +000096
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010097static constexpr uint32_t kUnknownFieldIndex = static_cast<uint32_t>(-1);
Mingyao Yang8df69d42015-10-22 15:40:58 -070098static constexpr uint16_t kUnknownClassDefIndex = static_cast<uint16_t>(-1);
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010099
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100100static constexpr InvokeType kInvalidInvokeType = static_cast<InvokeType>(-1);
101
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600102static constexpr uint32_t kNoDexPc = -1;
103
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100104inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
105 // For the purposes of the compiler, the dex files must actually be the same object
106 // if we want to safely treat them as the same. This is especially important for JIT
107 // as custom class loaders can open the same underlying file (or memory) multiple
108 // times and provide different class resolution but no two class loaders should ever
109 // use the same DexFile object - doing so is an unsupported hack that can lead to
110 // all sorts of weird failures.
111 return &lhs == &rhs;
112}
113
Dave Allison20dfc792014-06-16 20:44:29 -0700114enum IfCondition {
Aart Bike9f37602015-10-09 11:15:55 -0700115 // All types.
116 kCondEQ, // ==
117 kCondNE, // !=
118 // Signed integers and floating-point numbers.
119 kCondLT, // <
120 kCondLE, // <=
121 kCondGT, // >
122 kCondGE, // >=
123 // Unsigned integers.
124 kCondB, // <
125 kCondBE, // <=
126 kCondA, // >
127 kCondAE, // >=
Scott Wakeling2c76e062016-08-31 09:48:54 +0100128 // First and last aliases.
129 kCondFirst = kCondEQ,
130 kCondLast = kCondAE,
Dave Allison20dfc792014-06-16 20:44:29 -0700131};
132
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000133enum GraphAnalysisResult {
David Brazdil86ea7ee2016-02-16 09:26:07 +0000134 kAnalysisSkipped,
David Brazdilbadd8262016-02-02 16:28:56 +0000135 kAnalysisInvalidBytecode,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000136 kAnalysisFailThrowCatchLoop,
137 kAnalysisFailAmbiguousArrayOp,
Nicolas Geoffray0846a8f2018-09-12 15:21:07 +0100138 kAnalysisFailIrreducibleLoopAndStringInit,
Nicolas Geoffray7cfc8f52019-08-07 10:41:09 +0100139 kAnalysisFailPhiEquivalentInOsr,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000140 kAnalysisSuccess,
David Brazdil4833f5a2015-12-16 10:37:39 +0000141};
142
Andreas Gampe9186ced2016-12-12 14:28:21 -0800143template <typename T>
144static inline typename std::make_unsigned<T>::type MakeUnsigned(T x) {
145 return static_cast<typename std::make_unsigned<T>::type>(x);
146}
147
Vladimir Markof9f64412015-09-02 14:05:49 +0100148class HInstructionList : public ValueObject {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100149 public:
150 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
151
152 void AddInstruction(HInstruction* instruction);
153 void RemoveInstruction(HInstruction* instruction);
154
David Brazdilc3d743f2015-04-22 13:40:50 +0100155 // Insert `instruction` before/after an existing instruction `cursor`.
156 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
157 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
158
Roland Levillain6b469232014-09-25 10:10:38 +0100159 // Return true if this list contains `instruction`.
160 bool Contains(HInstruction* instruction) const;
161
Roland Levillainccc07a92014-09-16 14:48:16 +0100162 // Return true if `instruction1` is found before `instruction2` in
163 // this instruction list and false otherwise. Abort if none
164 // of these instructions is found.
165 bool FoundBefore(const HInstruction* instruction1,
166 const HInstruction* instruction2) const;
167
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000168 bool IsEmpty() const { return first_instruction_ == nullptr; }
169 void Clear() { first_instruction_ = last_instruction_ = nullptr; }
170
171 // Update the block of all instructions to be `block`.
172 void SetBlockOfInstructions(HBasicBlock* block) const;
173
174 void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list);
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000175 void AddBefore(HInstruction* cursor, const HInstructionList& instruction_list);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000176 void Add(const HInstructionList& instruction_list);
177
David Brazdil2d7352b2015-04-20 14:52:42 +0100178 // Return the number of instructions in the list. This is an expensive operation.
179 size_t CountSize() const;
180
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100181 private:
182 HInstruction* first_instruction_;
183 HInstruction* last_instruction_;
184
185 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000186 friend class HGraph;
187 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100188 friend class HInstructionIterator;
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +0000189 friend class HInstructionIteratorHandleChanges;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100190 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100191
192 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
193};
194
David Brazdil4833f5a2015-12-16 10:37:39 +0000195class ReferenceTypeInfo : ValueObject {
196 public:
Vladimir Marko4f990712021-07-14 12:45:13 +0100197 using TypeHandle = Handle<mirror::Class>;
David Brazdil4833f5a2015-12-16 10:37:39 +0000198
Vladimir Markoa1de9182016-02-25 11:37:38 +0000199 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact);
200
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700201 static ReferenceTypeInfo Create(TypeHandle type_handle) REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil94ab38f2016-06-21 17:48:19 +0100202 return Create(type_handle, type_handle->CannotBeAssignedFromOtherTypes());
203 }
204
Vladimir Markoa1de9182016-02-25 11:37:38 +0000205 static ReferenceTypeInfo CreateUnchecked(TypeHandle type_handle, bool is_exact) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000206 return ReferenceTypeInfo(type_handle, is_exact);
207 }
208
209 static ReferenceTypeInfo CreateInvalid() { return ReferenceTypeInfo(); }
210
Vladimir Markof39745e2016-01-26 12:16:55 +0000211 static bool IsValidHandle(TypeHandle handle) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000212 return handle.GetReference() != nullptr;
213 }
214
Vladimir Marko456307a2016-04-19 14:12:13 +0000215 bool IsValid() const {
David Brazdil4833f5a2015-12-16 10:37:39 +0000216 return IsValidHandle(type_handle_);
217 }
218
219 bool IsExact() const { return is_exact_; }
220
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700221 bool IsObjectClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000222 DCHECK(IsValid());
223 return GetTypeHandle()->IsObjectClass();
224 }
225
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700226 bool IsStringClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000227 DCHECK(IsValid());
228 return GetTypeHandle()->IsStringClass();
229 }
230
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700231 bool IsObjectArray() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000232 DCHECK(IsValid());
233 return IsArrayClass() && GetTypeHandle()->GetComponentType()->IsObjectClass();
234 }
235
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700236 bool IsInterface() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000237 DCHECK(IsValid());
238 return GetTypeHandle()->IsInterface();
239 }
240
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700241 bool IsArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000242 DCHECK(IsValid());
243 return GetTypeHandle()->IsArrayClass();
244 }
245
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700246 bool IsPrimitiveArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000247 DCHECK(IsValid());
248 return GetTypeHandle()->IsPrimitiveArray();
249 }
250
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700251 bool IsNonPrimitiveArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000252 DCHECK(IsValid());
253 return GetTypeHandle()->IsArrayClass() && !GetTypeHandle()->IsPrimitiveArray();
254 }
255
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700256 bool CanArrayHold(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000257 DCHECK(IsValid());
258 if (!IsExact()) return false;
259 if (!IsArrayClass()) return false;
260 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(rti.GetTypeHandle().Get());
261 }
262
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700263 bool CanArrayHoldValuesOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000264 DCHECK(IsValid());
265 if (!IsExact()) return false;
266 if (!IsArrayClass()) return false;
267 if (!rti.IsArrayClass()) return false;
268 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(
269 rti.GetTypeHandle()->GetComponentType());
270 }
271
272 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
273
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700274 bool IsSupertypeOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000275 DCHECK(IsValid());
276 DCHECK(rti.IsValid());
277 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
278 }
279
David Brazdil4833f5a2015-12-16 10:37:39 +0000280 // Returns true if the type information provide the same amount of details.
281 // Note that it does not mean that the instructions have the same actual type
282 // (because the type can be the result of a merge).
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700283 bool IsEqual(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000284 if (!IsValid() && !rti.IsValid()) {
285 // Invalid types are equal.
286 return true;
287 }
288 if (!IsValid() || !rti.IsValid()) {
289 // One is valid, the other not.
290 return false;
291 }
292 return IsExact() == rti.IsExact()
293 && GetTypeHandle().Get() == rti.GetTypeHandle().Get();
294 }
295
296 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +0000297 ReferenceTypeInfo() : type_handle_(TypeHandle()), is_exact_(false) {}
298 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact)
299 : type_handle_(type_handle), is_exact_(is_exact) { }
David Brazdil4833f5a2015-12-16 10:37:39 +0000300
301 // The class of the object.
302 TypeHandle type_handle_;
303 // Whether or not the type is exact or a superclass of the actual type.
304 // Whether or not we have any information about this type.
305 bool is_exact_;
306};
307
308std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
309
Vladimir Marko02ca05a2020-05-12 13:58:51 +0100310class HandleCache {
311 public:
312 explicit HandleCache(VariableSizedHandleScope* handles) : handles_(handles) { }
313
314 VariableSizedHandleScope* GetHandles() { return handles_; }
315
316 template <typename T>
317 MutableHandle<T> NewHandle(T* object) REQUIRES_SHARED(Locks::mutator_lock_) {
318 return handles_->NewHandle(object);
319 }
320
321 template <typename T>
322 MutableHandle<T> NewHandle(ObjPtr<T> object) REQUIRES_SHARED(Locks::mutator_lock_) {
323 return handles_->NewHandle(object);
324 }
325
326 ReferenceTypeInfo::TypeHandle GetObjectClassHandle() {
327 return GetRootHandle(ClassRoot::kJavaLangObject, &object_class_handle_);
328 }
329
330 ReferenceTypeInfo::TypeHandle GetClassClassHandle() {
331 return GetRootHandle(ClassRoot::kJavaLangClass, &class_class_handle_);
332 }
333
334 ReferenceTypeInfo::TypeHandle GetMethodHandleClassHandle() {
335 return GetRootHandle(ClassRoot::kJavaLangInvokeMethodHandleImpl, &method_handle_class_handle_);
336 }
337
338 ReferenceTypeInfo::TypeHandle GetMethodTypeClassHandle() {
339 return GetRootHandle(ClassRoot::kJavaLangInvokeMethodType, &method_type_class_handle_);
340 }
341
342 ReferenceTypeInfo::TypeHandle GetStringClassHandle() {
343 return GetRootHandle(ClassRoot::kJavaLangString, &string_class_handle_);
344 }
345
346 ReferenceTypeInfo::TypeHandle GetThrowableClassHandle() {
347 return GetRootHandle(ClassRoot::kJavaLangThrowable, &throwable_class_handle_);
348 }
349
350
351 private:
352 inline ReferenceTypeInfo::TypeHandle GetRootHandle(ClassRoot class_root,
353 ReferenceTypeInfo::TypeHandle* cache) {
354 if (UNLIKELY(!ReferenceTypeInfo::IsValidHandle(*cache))) {
355 *cache = CreateRootHandle(handles_, class_root);
356 }
357 return *cache;
358 }
359
360 static ReferenceTypeInfo::TypeHandle CreateRootHandle(VariableSizedHandleScope* handles,
361 ClassRoot class_root);
362
363 VariableSizedHandleScope* handles_;
364
365 ReferenceTypeInfo::TypeHandle object_class_handle_;
366 ReferenceTypeInfo::TypeHandle class_class_handle_;
367 ReferenceTypeInfo::TypeHandle method_handle_class_handle_;
368 ReferenceTypeInfo::TypeHandle method_type_class_handle_;
369 ReferenceTypeInfo::TypeHandle string_class_handle_;
370 ReferenceTypeInfo::TypeHandle throwable_class_handle_;
371};
372
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000373// Control-flow graph of a method. Contains a list of basic blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100374class HGraph : public ArenaObject<kArenaAllocGraph> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000375 public:
Vladimir Markoca6fff82017-10-03 14:49:14 +0100376 HGraph(ArenaAllocator* allocator,
377 ArenaStack* arena_stack,
Vladimir Marko02ca05a2020-05-12 13:58:51 +0100378 VariableSizedHandleScope* handles,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100379 const DexFile& dex_file,
380 uint32_t method_idx,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700381 InstructionSet instruction_set,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100382 InvokeType invoke_type = kInvalidInvokeType,
Hans Boehm206348c2018-12-05 11:11:33 -0800383 bool dead_reference_safe = false,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100384 bool debuggable = false,
Nicolas Geoffray0d60a2b2020-06-17 14:31:56 +0100385 CompilationKind compilation_kind = CompilationKind::kOptimized,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100386 int start_instruction_id = 0)
Vladimir Markoca6fff82017-10-03 14:49:14 +0100387 : allocator_(allocator),
388 arena_stack_(arena_stack),
Vladimir Marko02ca05a2020-05-12 13:58:51 +0100389 handle_cache_(handles),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100390 blocks_(allocator->Adapter(kArenaAllocBlockList)),
391 reverse_post_order_(allocator->Adapter(kArenaAllocReversePostOrder)),
392 linear_order_(allocator->Adapter(kArenaAllocLinearOrder)),
Alex Light86fe9b82020-11-16 16:54:01 +0000393 reachability_graph_(allocator, 0, 0, true, kArenaAllocReachabilityGraph),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700394 entry_block_(nullptr),
395 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100396 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100397 number_of_vregs_(0),
398 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000399 temporaries_vreg_slots_(0),
Mark Mendell1152c922015-04-24 17:06:35 -0400400 has_bounds_checks_(false),
David Brazdil77a48ae2015-09-15 12:34:04 +0000401 has_try_catch_(false),
Artem Serov2808be82018-12-20 19:15:11 +0000402 has_monitor_operations_(false),
Aart Bikb13c65b2017-03-21 20:14:07 -0700403 has_simd_(false),
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800404 has_loops_(false),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000405 has_irreducible_loops_(false),
Vladimir Markod3e9c622020-08-05 12:20:28 +0100406 has_direct_critical_native_call_(false),
Hans Boehm206348c2018-12-05 11:11:33 -0800407 dead_reference_safe_(dead_reference_safe),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000408 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000409 current_instruction_id_(start_instruction_id),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100410 dex_file_(dex_file),
411 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100412 invoke_type_(invoke_type),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100413 in_ssa_form_(false),
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800414 number_of_cha_guards_(0),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700415 instruction_set_(instruction_set),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000416 cached_null_constant_(nullptr),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100417 cached_int_constants_(std::less<int32_t>(), allocator->Adapter(kArenaAllocConstantsMap)),
418 cached_float_constants_(std::less<int32_t>(), allocator->Adapter(kArenaAllocConstantsMap)),
419 cached_long_constants_(std::less<int64_t>(), allocator->Adapter(kArenaAllocConstantsMap)),
420 cached_double_constants_(std::less<int64_t>(), allocator->Adapter(kArenaAllocConstantsMap)),
David Brazdil4833f5a2015-12-16 10:37:39 +0000421 cached_current_method_(nullptr),
Nicolas Geoffray53fec082017-03-27 12:56:16 +0100422 art_method_(nullptr),
Nicolas Geoffray0d60a2b2020-06-17 14:31:56 +0100423 compilation_kind_(compilation_kind),
Vladimir Marko695348f2020-05-19 14:42:02 +0100424 cha_single_implementation_list_(allocator->Adapter(kArenaAllocCHA)) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100425 blocks_.reserve(kDefaultNumberOfBlocks);
426 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000427
Alex Lightdc281e72021-01-06 12:35:31 -0800428 std::ostream& Dump(std::ostream& os,
429 std::optional<std::reference_wrapper<const BlockNamer>> namer = std::nullopt);
430
Vladimir Markoca6fff82017-10-03 14:49:14 +0100431 ArenaAllocator* GetAllocator() const { return allocator_; }
432 ArenaStack* GetArenaStack() const { return arena_stack_; }
Vladimir Marko02ca05a2020-05-12 13:58:51 +0100433
434 HandleCache* GetHandleCache() { return &handle_cache_; }
435
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100436 const ArenaVector<HBasicBlock*>& GetBlocks() const { return blocks_; }
437
Alex Light210a78d2020-11-30 16:58:05 -0800438 // An iterator to only blocks that are still actually in the graph (when
439 // blocks are removed they are replaced with 'nullptr' in GetBlocks to
440 // simplify block-id assignment and avoid memmoves in the block-list).
441 IterationRange<FilterNull<ArenaVector<HBasicBlock*>::const_iterator>> GetActiveBlocks() const {
442 return FilterOutNull(MakeIterationRange(GetBlocks()));
443 }
444
David Brazdil69ba7b72015-06-23 18:27:30 +0100445 bool IsInSsaForm() const { return in_ssa_form_; }
David Brazdilbadd8262016-02-02 16:28:56 +0000446 void SetInSsaForm() { in_ssa_form_ = true; }
David Brazdil69ba7b72015-06-23 18:27:30 +0100447
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000448 HBasicBlock* GetEntryBlock() const { return entry_block_; }
449 HBasicBlock* GetExitBlock() const { return exit_block_; }
David Brazdilc7af85d2015-05-26 12:05:55 +0100450 bool HasExitBlock() const { return exit_block_ != nullptr; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000451
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000452 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
453 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000454
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000455 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100456
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100457 void ComputeDominanceInformation();
458 void ClearDominanceInformation();
Alex Light86fe9b82020-11-16 16:54:01 +0000459 void ComputeReachabilityInformation();
460 void ClearReachabilityInformation();
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000461 void ClearLoopInformation();
462 void FindBackEdges(ArenaBitVector* visited);
463 GraphAnalysisResult BuildDominatorTree();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100464 void SimplifyCFG();
David Brazdilffee3d32015-07-06 11:48:53 +0100465 void SimplifyCatchBlocks();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000466
David Brazdil4833f5a2015-12-16 10:37:39 +0000467 // Analyze all natural loops in this graph. Returns a code specifying that it
468 // was successful or the reason for failure. The method will fail if a loop
David Brazdil4833f5a2015-12-16 10:37:39 +0000469 // is a throw-catch loop, i.e. the header is a catch block.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000470 GraphAnalysisResult AnalyzeLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100471
David Brazdilffee3d32015-07-06 11:48:53 +0100472 // Iterate over blocks to compute try block membership. Needs reverse post
473 // order and loop information.
474 void ComputeTryBlockInformation();
475
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000476 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
Nicolas Geoffray55bd7492016-02-16 15:37:12 +0000477 // Returns the instruction to replace the invoke expression or null if the
478 // invoke is for a void method. Note that the caller is responsible for replacing
479 // and removing the invoke instruction.
Calin Juravle2e768302015-07-28 14:41:11 +0000480 HInstruction* InlineInto(HGraph* outer_graph, HInvoke* invoke);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000481
Nicolas Geoffraya1d8ddf2016-02-29 11:46:58 +0000482 // Update the loop and try membership of `block`, which was spawned from `reference`.
483 // In case `reference` is a back edge, `replace_if_back_edge` notifies whether `block`
484 // should be the new back edge.
485 void UpdateLoopAndTryInformationOfNewBlock(HBasicBlock* block,
486 HBasicBlock* reference,
487 bool replace_if_back_edge);
488
Mingyao Yang3584bce2015-05-19 16:01:59 -0700489 // Need to add a couple of blocks to test if the loop body is entered and
490 // put deoptimization instructions, etc.
491 void TransformLoopHeaderForBCE(HBasicBlock* header);
492
Aart Bikf8f5a162017-02-06 15:35:29 -0800493 // Adds a new loop directly after the loop with the given header and exit.
494 // Returns the new preheader.
495 HBasicBlock* TransformLoopForVectorization(HBasicBlock* header,
496 HBasicBlock* body,
497 HBasicBlock* exit);
498
David Brazdil8a7c0fe2015-11-02 20:24:55 +0000499 // Removes `block` from the graph. Assumes `block` has been disconnected from
500 // other blocks and has no instructions or phis.
501 void DeleteDeadEmptyBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000502
David Brazdilfc6a86a2015-06-26 10:33:45 +0000503 // Splits the edge between `block` and `successor` while preserving the
504 // indices in the predecessor/successor lists. If there are multiple edges
505 // between the blocks, the lowest indices are used.
506 // Returns the new block which is empty and has the same dex pc as `successor`.
507 HBasicBlock* SplitEdge(HBasicBlock* block, HBasicBlock* successor);
508
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100509 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
Artem Serovc73ee372017-07-31 15:08:40 +0100510 void OrderLoopHeaderPredecessors(HBasicBlock* header);
Artem Serov09faaea2017-12-07 14:36:01 +0000511
512 // Transform a loop into a format with a single preheader.
513 //
514 // Each phi in the header should be split: original one in the header should only hold
515 // inputs reachable from the back edges and a single input from the preheader. The newly created
516 // phi in the preheader should collate the inputs from the original multiple incoming blocks.
517 //
518 // Loops in the graph typically have a single preheader, so this method is used to "repair" loops
519 // that no longer have this property.
520 void TransformLoopToSinglePreheaderFormat(HBasicBlock* header);
521
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100522 void SimplifyLoop(HBasicBlock* header);
523
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000524 int32_t GetNextInstructionId() {
Nicolas Geoffrayc9c31042017-06-29 14:04:16 +0100525 CHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000526 return current_instruction_id_++;
527 }
528
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000529 int32_t GetCurrentInstructionId() const {
530 return current_instruction_id_;
531 }
532
533 void SetCurrentInstructionId(int32_t id) {
Nicolas Geoffrayc9c31042017-06-29 14:04:16 +0100534 CHECK_GE(id, current_instruction_id_);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000535 current_instruction_id_ = id;
536 }
537
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100538 uint16_t GetMaximumNumberOfOutVRegs() const {
539 return maximum_number_of_out_vregs_;
540 }
541
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000542 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
543 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100544 }
545
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100546 void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) {
547 maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value);
548 }
549
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000550 void UpdateTemporariesVRegSlots(size_t slots) {
551 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100552 }
553
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000554 size_t GetTemporariesVRegSlots() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100555 DCHECK(!in_ssa_form_);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000556 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100557 }
558
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100559 void SetNumberOfVRegs(uint16_t number_of_vregs) {
560 number_of_vregs_ = number_of_vregs;
561 }
562
563 uint16_t GetNumberOfVRegs() const {
564 return number_of_vregs_;
565 }
566
567 void SetNumberOfInVRegs(uint16_t value) {
568 number_of_in_vregs_ = value;
569 }
570
David Brazdildee58d62016-04-07 09:54:26 +0000571 uint16_t GetNumberOfInVRegs() const {
572 return number_of_in_vregs_;
573 }
574
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100575 uint16_t GetNumberOfLocalVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100576 DCHECK(!in_ssa_form_);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100577 return number_of_vregs_ - number_of_in_vregs_;
578 }
579
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100580 const ArenaVector<HBasicBlock*>& GetReversePostOrder() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100581 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100582 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100583
Eric Holk1868de92020-02-12 09:10:21 -0800584 ArrayRef<HBasicBlock* const> GetReversePostOrderSkipEntryBlock() const {
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100585 DCHECK(GetReversePostOrder()[0] == entry_block_);
586 return ArrayRef<HBasicBlock* const>(GetReversePostOrder()).SubArray(1);
587 }
588
589 IterationRange<ArenaVector<HBasicBlock*>::const_reverse_iterator> GetPostOrder() const {
590 return ReverseRange(GetReversePostOrder());
591 }
592
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100593 const ArenaVector<HBasicBlock*>& GetLinearOrder() const {
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100594 return linear_order_;
595 }
596
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100597 IterationRange<ArenaVector<HBasicBlock*>::const_reverse_iterator> GetLinearPostOrder() const {
598 return ReverseRange(GetLinearOrder());
599 }
600
Mark Mendell1152c922015-04-24 17:06:35 -0400601 bool HasBoundsChecks() const {
602 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800603 }
604
Mark Mendell1152c922015-04-24 17:06:35 -0400605 void SetHasBoundsChecks(bool value) {
606 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800607 }
608
Alex Light86fe9b82020-11-16 16:54:01 +0000609 // Returns true if dest is reachable from source, using either blocks or block-ids.
610 bool PathBetween(const HBasicBlock* source, const HBasicBlock* dest) const;
611 bool PathBetween(uint32_t source_id, uint32_t dest_id) const;
612
Hans Boehm206348c2018-12-05 11:11:33 -0800613 // Is the code known to be robust against eliminating dead references
614 // and the effects of early finalization?
615 bool IsDeadReferenceSafe() const { return dead_reference_safe_; }
616
617 void MarkDeadReferenceUnsafe() { dead_reference_safe_ = false; }
618
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000619 bool IsDebuggable() const { return debuggable_; }
620
David Brazdil8d5b8b22015-03-24 10:51:52 +0000621 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000622 // already, it is created and inserted into the graph. This method is only for
623 // integral types.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100624 HConstant* GetConstant(DataType::Type type, int64_t value, uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000625
626 // TODO: This is problematic for the consistency of reference type propagation
627 // because it can be created anytime after the pass and thus it will be left
628 // with an invalid type.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600629 HNullConstant* GetNullConstant(uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000630
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600631 HIntConstant* GetIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc) {
632 return CreateConstant(value, &cached_int_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000633 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600634 HLongConstant* GetLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc) {
635 return CreateConstant(value, &cached_long_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000636 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600637 HFloatConstant* GetFloatConstant(float value, uint32_t dex_pc = kNoDexPc) {
638 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000639 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600640 HDoubleConstant* GetDoubleConstant(double value, uint32_t dex_pc = kNoDexPc) {
641 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000642 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000643
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100644 HCurrentMethod* GetCurrentMethod();
645
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100646 const DexFile& GetDexFile() const {
647 return dex_file_;
648 }
649
650 uint32_t GetMethodIdx() const {
651 return method_idx_;
652 }
653
Igor Murashkind01745e2017-04-05 16:40:31 -0700654 // Get the method name (without the signature), e.g. "<init>"
655 const char* GetMethodName() const;
656
657 // Get the pretty method name (class + name + optionally signature).
658 std::string PrettyMethod(bool with_signature = true) const;
659
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100660 InvokeType GetInvokeType() const {
661 return invoke_type_;
662 }
663
Mark Mendellc4701932015-04-10 13:18:51 -0400664 InstructionSet GetInstructionSet() const {
665 return instruction_set_;
666 }
667
Nicolas Geoffray0d60a2b2020-06-17 14:31:56 +0100668 bool IsCompilingOsr() const { return compilation_kind_ == CompilationKind::kOsr; }
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000669
Nicolas Geoffray0d60a2b2020-06-17 14:31:56 +0100670 bool IsCompilingBaseline() const { return compilation_kind_ == CompilationKind::kBaseline; }
671
672 CompilationKind GetCompilationKind() const { return compilation_kind_; }
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +0000673
Mingyao Yang063fc772016-08-02 11:02:54 -0700674 ArenaSet<ArtMethod*>& GetCHASingleImplementationList() {
675 return cha_single_implementation_list_;
676 }
677
678 void AddCHASingleImplementationDependency(ArtMethod* method) {
679 cha_single_implementation_list_.insert(method);
680 }
681
682 bool HasShouldDeoptimizeFlag() const {
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800683 return number_of_cha_guards_ != 0;
Mingyao Yang063fc772016-08-02 11:02:54 -0700684 }
685
David Brazdil77a48ae2015-09-15 12:34:04 +0000686 bool HasTryCatch() const { return has_try_catch_; }
687 void SetHasTryCatch(bool value) { has_try_catch_ = value; }
David Brazdilbbd733e2015-08-18 17:48:17 +0100688
Artem Serov2808be82018-12-20 19:15:11 +0000689 bool HasMonitorOperations() const { return has_monitor_operations_; }
690 void SetHasMonitorOperations(bool value) { has_monitor_operations_ = value; }
691
Aart Bikb13c65b2017-03-21 20:14:07 -0700692 bool HasSIMD() const { return has_simd_; }
693 void SetHasSIMD(bool value) { has_simd_ = value; }
694
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800695 bool HasLoops() const { return has_loops_; }
696 void SetHasLoops(bool value) { has_loops_ = value; }
697
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000698 bool HasIrreducibleLoops() const { return has_irreducible_loops_; }
699 void SetHasIrreducibleLoops(bool value) { has_irreducible_loops_ = value; }
700
Vladimir Markod3e9c622020-08-05 12:20:28 +0100701 bool HasDirectCriticalNativeCall() const { return has_direct_critical_native_call_; }
702 void SetHasDirectCriticalNativeCall(bool value) { has_direct_critical_native_call_ = value; }
703
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100704 ArtMethod* GetArtMethod() const { return art_method_; }
705 void SetArtMethod(ArtMethod* method) { art_method_ = method; }
706
Roland Levillain5e8d5f02016-10-18 18:03:43 +0100707 // Returns an instruction with the opposite Boolean value from 'cond'.
Mark Mendellf6529172015-11-17 11:16:56 -0500708 // The instruction has been inserted into the graph, either as a constant, or
709 // before cursor.
710 HInstruction* InsertOppositeCondition(HInstruction* cond, HInstruction* cursor);
711
Vladimir Marko02ca05a2020-05-12 13:58:51 +0100712 ReferenceTypeInfo GetInexactObjectRti() {
713 return ReferenceTypeInfo::Create(handle_cache_.GetObjectClassHandle(), /* is_exact= */ false);
714 }
Nicolas Geoffray18401b72016-03-11 13:35:51 +0000715
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800716 uint32_t GetNumberOfCHAGuards() { return number_of_cha_guards_; }
717 void SetNumberOfCHAGuards(uint32_t num) { number_of_cha_guards_ = num; }
718 void IncrementNumberOfCHAGuards() { number_of_cha_guards_++; }
719
David Brazdil2d7352b2015-04-20 14:52:42 +0100720 private:
Roland Levillainfc600dc2014-12-02 17:16:31 +0000721 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100722 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000723
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000724 template <class InstructionType, typename ValueType>
725 InstructionType* CreateConstant(ValueType value,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600726 ArenaSafeMap<ValueType, InstructionType*>* cache,
727 uint32_t dex_pc = kNoDexPc) {
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000728 // Try to find an existing constant of the given value.
729 InstructionType* constant = nullptr;
730 auto cached_constant = cache->find(value);
731 if (cached_constant != cache->end()) {
732 constant = cached_constant->second;
733 }
734
735 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100736 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000737 if (constant == nullptr || constant->GetBlock() == nullptr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100738 constant = new (allocator_) InstructionType(value, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000739 cache->Overwrite(value, constant);
740 InsertConstant(constant);
741 }
742 return constant;
743 }
744
David Brazdil8d5b8b22015-03-24 10:51:52 +0000745 void InsertConstant(HConstant* instruction);
746
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000747 // Cache a float constant into the graph. This method should only be
748 // called by the SsaBuilder when creating "equivalent" instructions.
749 void CacheFloatConstant(HFloatConstant* constant);
750
751 // See CacheFloatConstant comment.
752 void CacheDoubleConstant(HDoubleConstant* constant);
753
Vladimir Markoca6fff82017-10-03 14:49:14 +0100754 ArenaAllocator* const allocator_;
755 ArenaStack* const arena_stack_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000756
Vladimir Marko02ca05a2020-05-12 13:58:51 +0100757 HandleCache handle_cache_;
758
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000759 // List of blocks in insertion order.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100760 ArenaVector<HBasicBlock*> blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000761
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100762 // List of blocks to perform a reverse post order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100763 ArenaVector<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000764
Aart Bik281c6812016-08-26 11:31:48 -0700765 // List of blocks to perform a linear order tree traversal. Unlike the reverse
766 // post order, this order is not incrementally kept up-to-date.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100767 ArenaVector<HBasicBlock*> linear_order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100768
Alex Light86fe9b82020-11-16 16:54:01 +0000769 // Reachability graph for checking connectedness between nodes. Acts as a partitioned vector where
770 // each RoundUp(blocks_.size(), BitVector::kWordBits) is the reachability of each node.
771 ArenaBitVectorArray reachability_graph_;
772
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000773 HBasicBlock* entry_block_;
774 HBasicBlock* exit_block_;
775
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100776 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100777 uint16_t maximum_number_of_out_vregs_;
778
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100779 // The number of virtual registers in this method. Contains the parameters.
780 uint16_t number_of_vregs_;
781
782 // The number of virtual registers used by parameters of this method.
783 uint16_t number_of_in_vregs_;
784
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000785 // Number of vreg size slots that the temporaries use (used in baseline compiler).
786 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100787
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800788 // Flag whether there are bounds checks in the graph. We can skip
789 // BCE if it's false. It's only best effort to keep it up to date in
790 // the presence of code elimination so there might be false positives.
Mark Mendell1152c922015-04-24 17:06:35 -0400791 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800792
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800793 // Flag whether there are try/catch blocks in the graph. We will skip
794 // try/catch-related passes if it's false. It's only best effort to keep
795 // it up to date in the presence of code elimination so there might be
796 // false positives.
David Brazdil77a48ae2015-09-15 12:34:04 +0000797 bool has_try_catch_;
798
Artem Serov2808be82018-12-20 19:15:11 +0000799 // Flag whether there are any HMonitorOperation in the graph. If yes this will mandate
800 // DexRegisterMap to be present to allow deadlock analysis for non-debuggable code.
801 bool has_monitor_operations_;
802
Aart Bikb13c65b2017-03-21 20:14:07 -0700803 // Flag whether SIMD instructions appear in the graph. If true, the
804 // code generators may have to be more careful spilling the wider
805 // contents of SIMD registers.
806 bool has_simd_;
807
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800808 // Flag whether there are any loops in the graph. We can skip loop
809 // optimization if it's false. It's only best effort to keep it up
810 // to date in the presence of code elimination so there might be false
811 // positives.
812 bool has_loops_;
813
814 // Flag whether there are any irreducible loops in the graph. It's only
815 // best effort to keep it up to date in the presence of code elimination
816 // so there might be false positives.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000817 bool has_irreducible_loops_;
818
Vladimir Markod3e9c622020-08-05 12:20:28 +0100819 // Flag whether there are any direct calls to native code registered
820 // for @CriticalNative methods.
821 bool has_direct_critical_native_call_;
822
Hans Boehm206348c2018-12-05 11:11:33 -0800823 // Is the code known to be robust against eliminating dead references
824 // and the effects of early finalization? If false, dead reference variables
825 // are kept if they might be visible to the garbage collector.
826 // Currently this means that the class was declared to be dead-reference-safe,
827 // the method accesses no reachability-sensitive fields or data, and the same
828 // is true for any methods that were inlined into the current one.
829 bool dead_reference_safe_;
830
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000831 // Indicates whether the graph should be compiled in a way that
832 // ensures full debuggability. If false, we can apply more
833 // aggressive optimizations that may limit the level of debugging.
834 const bool debuggable_;
835
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000836 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000837 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000838
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100839 // The dex file from which the method is from.
840 const DexFile& dex_file_;
841
842 // The method index in the dex file.
843 const uint32_t method_idx_;
844
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100845 // If inlined, this encodes how the callee is being invoked.
846 const InvokeType invoke_type_;
847
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100848 // Whether the graph has been transformed to SSA form. Only used
849 // in debug mode to ensure we are not using properties only valid
850 // for non-SSA form (like the number of temporaries).
851 bool in_ssa_form_;
852
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800853 // Number of CHA guards in the graph. Used to short-circuit the
854 // CHA guard optimization pass when there is no CHA guard left.
855 uint32_t number_of_cha_guards_;
856
Mathieu Chartiere401d142015-04-22 13:56:20 -0700857 const InstructionSet instruction_set_;
858
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000859 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000860 HNullConstant* cached_null_constant_;
861 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000862 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000863 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000864 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000865
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100866 HCurrentMethod* cached_current_method_;
867
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100868 // The ArtMethod this graph is for. Note that for AOT, it may be null,
869 // for example for methods whose declaring class could not be resolved
870 // (such as when the superclass could not be found).
871 ArtMethod* art_method_;
872
Nicolas Geoffray0d60a2b2020-06-17 14:31:56 +0100873 // How we are compiling the graph: either optimized, osr, or baseline.
874 // For osr, we will make all loops seen as irreducible and emit special
875 // stack maps to mark compiled code entries which the interpreter can
876 // directly jump to.
877 const CompilationKind compilation_kind_;
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +0000878
Mingyao Yang063fc772016-08-02 11:02:54 -0700879 // List of methods that are assumed to have single implementation.
880 ArenaSet<ArtMethod*> cha_single_implementation_list_;
881
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000882 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100883 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000884 friend class HInliner; // For the reverse post order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000885 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000886 DISALLOW_COPY_AND_ASSIGN(HGraph);
887};
888
Alex Lightdc281e72021-01-06 12:35:31 -0800889inline std::ostream& operator<<(std::ostream& os, HGraph& graph) {
890 return graph.Dump(os);
891}
892
Vladimir Markof9f64412015-09-02 14:05:49 +0100893class HLoopInformation : public ArenaObject<kArenaAllocLoopInfo> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000894 public:
895 HLoopInformation(HBasicBlock* header, HGraph* graph)
896 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100897 suspend_check_(nullptr),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000898 irreducible_(false),
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100899 contains_irreducible_loop_(false),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100900 back_edges_(graph->GetAllocator()->Adapter(kArenaAllocLoopInfoBackEdges)),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100901 // Make bit vector growable, as the number of blocks may change.
Vladimir Markoca6fff82017-10-03 14:49:14 +0100902 blocks_(graph->GetAllocator(),
903 graph->GetBlocks().size(),
904 true,
905 kArenaAllocLoopInfoBackEdges) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100906 back_edges_.reserve(kDefaultNumberOfBackEdges);
907 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100908
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000909 bool IsIrreducible() const { return irreducible_; }
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100910 bool ContainsIrreducibleLoop() const { return contains_irreducible_loop_; }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000911
912 void Dump(std::ostream& os);
913
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100914 HBasicBlock* GetHeader() const {
915 return header_;
916 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000917
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000918 void SetHeader(HBasicBlock* block) {
919 header_ = block;
920 }
921
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100922 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
923 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
924 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
925
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000926 void AddBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100927 back_edges_.push_back(back_edge);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000928 }
929
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100930 void RemoveBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100931 RemoveElement(back_edges_, back_edge);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100932 }
933
David Brazdil46e2a392015-03-16 17:31:52 +0000934 bool IsBackEdge(const HBasicBlock& block) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100935 return ContainsElement(back_edges_, &block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100936 }
937
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000938 size_t NumberOfBackEdges() const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100939 return back_edges_.size();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000940 }
941
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100942 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100943
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100944 const ArenaVector<HBasicBlock*>& GetBackEdges() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100945 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100946 }
947
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100948 // Returns the lifetime position of the back edge that has the
949 // greatest lifetime position.
950 size_t GetLifetimeEnd() const;
951
952 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100953 ReplaceElement(back_edges_, existing, new_back_edge);
Nicolas Geoffray57902602015-04-21 14:28:41 +0100954 }
955
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000956 // Finds blocks that are part of this loop.
957 void Populate();
David Brazdila4b8c212015-05-07 09:59:30 +0100958
Artem Serov7f4aff62017-06-21 17:02:18 +0100959 // Updates blocks population of the loop and all of its outer' ones recursively after the
960 // population of the inner loop is updated.
961 void PopulateInnerLoopUpwards(HLoopInformation* inner_loop);
962
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100963 // Returns whether this loop information contains `block`.
964 // Note that this loop information *must* be populated before entering this function.
965 bool Contains(const HBasicBlock& block) const;
966
967 // Returns whether this loop information is an inner loop of `other`.
968 // Note that `other` *must* be populated before entering this function.
969 bool IsIn(const HLoopInformation& other) const;
970
Mingyao Yang4b467ed2015-11-19 17:04:22 -0800971 // Returns true if instruction is not defined within this loop.
972 bool IsDefinedOutOfTheLoop(HInstruction* instruction) const;
Aart Bik73f1f3b2015-10-28 15:28:08 -0700973
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100974 const ArenaBitVector& GetBlocks() const { return blocks_; }
975
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000976 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000977 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000978
Nicolas Geoffray788f2f02016-01-22 12:41:38 +0000979 void ClearAllBlocks() {
980 blocks_.ClearAllBits();
981 }
982
David Brazdil3f4a5222016-05-06 12:46:21 +0100983 bool HasBackEdgeNotDominatedByHeader() const;
984
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100985 bool IsPopulated() const {
986 return blocks_.GetHighestBitSet() != -1;
987 }
988
Anton Shaminf89381f2016-05-16 16:44:13 +0600989 bool DominatesAllBackEdges(HBasicBlock* block);
990
David Sehrc757dec2016-11-04 15:48:34 -0700991 bool HasExitEdge() const;
992
Artem Serov7f4aff62017-06-21 17:02:18 +0100993 // Resets back edge and blocks-in-loop data.
994 void ResetBasicBlockData() {
995 back_edges_.clear();
996 ClearAllBlocks();
997 }
998
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000999 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001000 // Internal recursive implementation of `Populate`.
1001 void PopulateRecursive(HBasicBlock* block);
David Brazdilc2e8af92016-04-05 17:15:19 +01001002 void PopulateIrreducibleRecursive(HBasicBlock* block, ArenaBitVector* finalized);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001003
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001004 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001005 HSuspendCheck* suspend_check_;
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001006 bool irreducible_;
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +01001007 bool contains_irreducible_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001008 ArenaVector<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001009 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001010
1011 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
1012};
1013
David Brazdilec16f792015-08-19 15:04:01 +01001014// Stores try/catch information for basic blocks.
1015// Note that HGraph is constructed so that catch blocks cannot simultaneously
1016// be try blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +01001017class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> {
David Brazdilec16f792015-08-19 15:04:01 +01001018 public:
1019 // Try block information constructor.
1020 explicit TryCatchInformation(const HTryBoundary& try_entry)
1021 : try_entry_(&try_entry),
1022 catch_dex_file_(nullptr),
Nicolas Geoffrayebc3b252019-01-09 10:47:02 +00001023 catch_type_index_(dex::TypeIndex::Invalid()) {
David Brazdilec16f792015-08-19 15:04:01 +01001024 DCHECK(try_entry_ != nullptr);
1025 }
1026
1027 // Catch block information constructor.
Andreas Gampea5b09a62016-11-17 15:21:22 -08001028 TryCatchInformation(dex::TypeIndex catch_type_index, const DexFile& dex_file)
David Brazdilec16f792015-08-19 15:04:01 +01001029 : try_entry_(nullptr),
1030 catch_dex_file_(&dex_file),
1031 catch_type_index_(catch_type_index) {}
1032
1033 bool IsTryBlock() const { return try_entry_ != nullptr; }
1034
1035 const HTryBoundary& GetTryEntry() const {
1036 DCHECK(IsTryBlock());
1037 return *try_entry_;
1038 }
1039
1040 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
1041
Nicolas Geoffrayebc3b252019-01-09 10:47:02 +00001042 bool IsValidTypeIndex() const {
David Brazdilec16f792015-08-19 15:04:01 +01001043 DCHECK(IsCatchBlock());
Nicolas Geoffrayebc3b252019-01-09 10:47:02 +00001044 return catch_type_index_.IsValid();
David Brazdilec16f792015-08-19 15:04:01 +01001045 }
1046
Andreas Gampea5b09a62016-11-17 15:21:22 -08001047 dex::TypeIndex GetCatchTypeIndex() const {
David Brazdilec16f792015-08-19 15:04:01 +01001048 DCHECK(IsCatchBlock());
1049 return catch_type_index_;
1050 }
1051
1052 const DexFile& GetCatchDexFile() const {
1053 DCHECK(IsCatchBlock());
1054 return *catch_dex_file_;
1055 }
1056
Nicolas Geoffrayebc3b252019-01-09 10:47:02 +00001057 void SetInvalidTypeIndex() {
1058 catch_type_index_ = dex::TypeIndex::Invalid();
1059 }
1060
David Brazdilec16f792015-08-19 15:04:01 +01001061 private:
1062 // One of possibly several TryBoundary instructions entering the block's try.
1063 // Only set for try blocks.
1064 const HTryBoundary* try_entry_;
1065
1066 // Exception type information. Only set for catch blocks.
1067 const DexFile* catch_dex_file_;
Nicolas Geoffrayebc3b252019-01-09 10:47:02 +00001068 dex::TypeIndex catch_type_index_;
David Brazdilec16f792015-08-19 15:04:01 +01001069};
1070
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001071static constexpr size_t kNoLifetime = -1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001072static constexpr uint32_t kInvalidBlockId = static_cast<uint32_t>(-1);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001073
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001074// A block in a method. Contains the list of instructions represented
1075// as a double linked list. Each block knows its predecessors and
1076// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001077
Vladimir Markof9f64412015-09-02 14:05:49 +01001078class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001079 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -07001080 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001081 : graph_(graph),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001082 predecessors_(graph->GetAllocator()->Adapter(kArenaAllocPredecessors)),
1083 successors_(graph->GetAllocator()->Adapter(kArenaAllocSuccessors)),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001084 loop_information_(nullptr),
1085 dominator_(nullptr),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001086 dominated_blocks_(graph->GetAllocator()->Adapter(kArenaAllocDominated)),
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001087 block_id_(kInvalidBlockId),
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001088 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001089 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001090 lifetime_end_(kNoLifetime),
Vladimir Marko60584552015-09-03 13:35:12 +00001091 try_catch_information_(nullptr) {
1092 predecessors_.reserve(kDefaultNumberOfPredecessors);
1093 successors_.reserve(kDefaultNumberOfSuccessors);
1094 dominated_blocks_.reserve(kDefaultNumberOfDominatedBlocks);
1095 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001096
Vladimir Marko60584552015-09-03 13:35:12 +00001097 const ArenaVector<HBasicBlock*>& GetPredecessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001098 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001099 }
1100
Alex Light3a73ffb2021-01-25 14:11:05 +00001101 size_t GetNumberOfPredecessors() const {
1102 return GetPredecessors().size();
1103 }
1104
Vladimir Marko60584552015-09-03 13:35:12 +00001105 const ArenaVector<HBasicBlock*>& GetSuccessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001106 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001107 }
1108
David Brazdild26a4112015-11-10 11:07:31 +00001109 ArrayRef<HBasicBlock* const> GetNormalSuccessors() const;
1110 ArrayRef<HBasicBlock* const> GetExceptionalSuccessors() const;
1111
Vladimir Marko60584552015-09-03 13:35:12 +00001112 bool HasSuccessor(const HBasicBlock* block, size_t start_from = 0u) {
1113 return ContainsElement(successors_, block, start_from);
1114 }
1115
1116 const ArenaVector<HBasicBlock*>& GetDominatedBlocks() const {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001117 return dominated_blocks_;
1118 }
1119
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001120 bool IsEntryBlock() const {
1121 return graph_->GetEntryBlock() == this;
1122 }
1123
1124 bool IsExitBlock() const {
1125 return graph_->GetExitBlock() == this;
1126 }
1127
David Brazdil46e2a392015-03-16 17:31:52 +00001128 bool IsSingleGoto() const;
Mads Ager16e52892017-07-14 13:11:37 +02001129 bool IsSingleReturn() const;
Mingyao Yang46721ef2017-10-05 14:45:17 -07001130 bool IsSingleReturnOrReturnVoidAllowingPhis() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +00001131 bool IsSingleTryBoundary() const;
1132
1133 // Returns true if this block emits nothing but a jump.
1134 bool IsSingleJump() const {
1135 HLoopInformation* loop_info = GetLoopInformation();
1136 return (IsSingleGoto() || IsSingleTryBoundary())
1137 // Back edges generate a suspend check.
1138 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
1139 }
David Brazdil46e2a392015-03-16 17:31:52 +00001140
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001141 void AddBackEdge(HBasicBlock* back_edge) {
1142 if (loop_information_ == nullptr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001143 loop_information_ = new (graph_->GetAllocator()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001144 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001145 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001146 loop_information_->AddBackEdge(back_edge);
1147 }
1148
Artem Serov7f4aff62017-06-21 17:02:18 +01001149 // Registers a back edge; if the block was not a loop header before the call associates a newly
1150 // created loop info with it.
1151 //
1152 // Used in SuperblockCloner to preserve LoopInformation object instead of reseting loop
1153 // info for all blocks during back edges recalculation.
1154 void AddBackEdgeWhileUpdating(HBasicBlock* back_edge) {
1155 if (loop_information_ == nullptr || loop_information_->GetHeader() != this) {
1156 loop_information_ = new (graph_->GetAllocator()) HLoopInformation(this, graph_);
1157 }
1158 loop_information_->AddBackEdge(back_edge);
1159 }
1160
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001161 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001162 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001163
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001164 uint32_t GetBlockId() const { return block_id_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001165 void SetBlockId(int id) { block_id_ = id; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001166 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001167
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001168 HBasicBlock* GetDominator() const { return dominator_; }
1169 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Vladimir Marko60584552015-09-03 13:35:12 +00001170 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.push_back(block); }
1171
1172 void RemoveDominatedBlock(HBasicBlock* block) {
1173 RemoveElement(dominated_blocks_, block);
Vladimir Marko91e11c02015-09-02 17:03:22 +01001174 }
Vladimir Marko60584552015-09-03 13:35:12 +00001175
1176 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
1177 ReplaceElement(dominated_blocks_, existing, new_block);
1178 }
1179
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +01001180 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001181
1182 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +01001183 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001184 }
1185
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001186 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
1187 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +01001188 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001189 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +01001190 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
1191 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001192
Nicolas Geoffray09aa1472016-01-19 10:52:54 +00001193 HInstruction* GetFirstInstructionDisregardMoves() const;
1194
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001195 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001196 successors_.push_back(block);
1197 block->predecessors_.push_back(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001198 }
1199
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001200 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
1201 size_t successor_index = GetSuccessorIndexOf(existing);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001202 existing->RemovePredecessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +00001203 new_block->predecessors_.push_back(this);
1204 successors_[successor_index] = new_block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001205 }
1206
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001207 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
1208 size_t predecessor_index = GetPredecessorIndexOf(existing);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001209 existing->RemoveSuccessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +00001210 new_block->successors_.push_back(this);
1211 predecessors_[predecessor_index] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001212 }
1213
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001214 // Insert `this` between `predecessor` and `successor. This method
Alex Light1e52a072019-06-25 09:12:04 -07001215 // preserves the indices, and will update the first edge found between
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001216 // `predecessor` and `successor`.
1217 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
1218 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001219 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko60584552015-09-03 13:35:12 +00001220 successor->predecessors_[predecessor_index] = this;
1221 predecessor->successors_[successor_index] = this;
1222 successors_.push_back(successor);
1223 predecessors_.push_back(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001224 }
1225
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001226 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001227 predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001228 }
1229
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001230 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001231 successors_.erase(successors_.begin() + GetSuccessorIndexOf(block));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001232 }
1233
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001234 void ClearAllPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001235 predecessors_.clear();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001236 }
1237
1238 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001239 predecessors_.push_back(block);
1240 block->successors_.push_back(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001241 }
1242
Nicolas Geoffray604c6e42014-09-17 12:08:44 +01001243 void SwapPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001244 DCHECK_EQ(predecessors_.size(), 2u);
1245 std::swap(predecessors_[0], predecessors_[1]);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +01001246 }
1247
David Brazdil769c9e52015-04-27 13:54:09 +01001248 void SwapSuccessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001249 DCHECK_EQ(successors_.size(), 2u);
1250 std::swap(successors_[0], successors_[1]);
David Brazdil769c9e52015-04-27 13:54:09 +01001251 }
1252
David Brazdilfc6a86a2015-06-26 10:33:45 +00001253 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko60584552015-09-03 13:35:12 +00001254 return IndexOfElement(predecessors_, predecessor);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001255 }
1256
David Brazdilfc6a86a2015-06-26 10:33:45 +00001257 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko60584552015-09-03 13:35:12 +00001258 return IndexOfElement(successors_, successor);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001259 }
1260
David Brazdilfc6a86a2015-06-26 10:33:45 +00001261 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00001262 DCHECK_EQ(GetPredecessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +01001263 return GetPredecessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +00001264 }
1265
1266 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00001267 DCHECK_EQ(GetSuccessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +01001268 return GetSuccessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +00001269 }
1270
1271 // Returns whether the first occurrence of `predecessor` in the list of
1272 // predecessors is at index `idx`.
1273 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01001274 DCHECK_EQ(GetPredecessors()[idx], predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +00001275 return GetPredecessorIndexOf(predecessor) == idx;
1276 }
1277
David Brazdild7558da2015-09-22 13:04:14 +01001278 // Create a new block between this block and its predecessors. The new block
1279 // is added to the graph, all predecessor edges are relinked to it and an edge
1280 // is created to `this`. Returns the new empty block. Reverse post order or
1281 // loop and try/catch information are not updated.
1282 HBasicBlock* CreateImmediateDominator();
1283
David Brazdilfc6a86a2015-06-26 10:33:45 +00001284 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +01001285 // created, latter block. Note that this method will add the block to the
1286 // graph, create a Goto at the end of the former block and will create an edge
1287 // between the blocks. It will not, however, update the reverse post order or
David Brazdild7558da2015-09-22 13:04:14 +01001288 // loop and try/catch information.
David Brazdilfc6a86a2015-06-26 10:33:45 +00001289 HBasicBlock* SplitBefore(HInstruction* cursor);
1290
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001291 // Split the block into two blocks just before `cursor`. Returns the newly
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001292 // created block. Note that this method just updates raw block information,
1293 // like predecessors, successors, dominators, and instruction list. It does not
1294 // update the graph, reverse post order, loop information, nor make sure the
1295 // blocks are consistent (for example ending with a control flow instruction).
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001296 HBasicBlock* SplitBeforeForInlining(HInstruction* cursor);
1297
1298 // Similar to `SplitBeforeForInlining` but does it after `cursor`.
1299 HBasicBlock* SplitAfterForInlining(HInstruction* cursor);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001300
1301 // Merge `other` at the end of `this`. Successors and dominated blocks of
1302 // `other` are changed to be successors and dominated blocks of `this`. Note
1303 // that this method does not update the graph, reverse post order, loop
1304 // information, nor make sure the blocks are consistent (for example ending
1305 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +01001306 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001307
1308 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
1309 // of `this` are moved to `other`.
1310 // Note that this method does not update the graph, reverse post order, loop
1311 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +00001312 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001313 void ReplaceWith(HBasicBlock* other);
1314
Aart Bik6b69e0a2017-01-11 10:20:43 -08001315 // Merges the instructions of `other` at the end of `this`.
1316 void MergeInstructionsWith(HBasicBlock* other);
1317
David Brazdil2d7352b2015-04-20 14:52:42 +01001318 // Merge `other` at the end of `this`. This method updates loops, reverse post
1319 // order, links to predecessors, successors, dominators and deletes the block
1320 // from the graph. The two blocks must be successive, i.e. `this` the only
1321 // predecessor of `other` and vice versa.
1322 void MergeWith(HBasicBlock* other);
1323
1324 // Disconnects `this` from all its predecessors, successors and dominator,
1325 // removes it from all loops it is included in and eventually from the graph.
1326 // The block must not dominate any other block. Predecessors and successors
1327 // are safely updated.
1328 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +00001329
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001330 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001331 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001332 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001333 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Artem Serovcced8ba2017-07-19 18:18:09 +01001334 // Replace phi `initial` with `replacement` within this block.
1335 void ReplaceAndRemovePhiWith(HPhi* initial, HPhi* replacement);
Roland Levillainccc07a92014-09-16 14:48:16 +01001336 // Replace instruction `initial` with `replacement` within this block.
1337 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
1338 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001339 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001340 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +00001341 // RemoveInstruction and RemovePhi delete a given instruction from the respective
1342 // instruction list. With 'ensure_safety' set to true, it verifies that the
1343 // instruction is not in use and removes it from the use lists of its inputs.
1344 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
1345 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +01001346 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001347
1348 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +01001349 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001350 }
1351
Roland Levillain6b879dd2014-09-22 17:13:44 +01001352 bool IsLoopPreHeaderFirstPredecessor() const {
1353 DCHECK(IsLoopHeader());
Vladimir Markoec7802a2015-10-01 20:57:57 +01001354 return GetPredecessors()[0] == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +01001355 }
1356
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001357 bool IsFirstPredecessorBackEdge() const {
1358 DCHECK(IsLoopHeader());
1359 return GetLoopInformation()->IsBackEdge(*GetPredecessors()[0]);
1360 }
1361
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001362 HLoopInformation* GetLoopInformation() const {
1363 return loop_information_;
1364 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001365
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001366 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001367 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001368 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001369 void SetInLoop(HLoopInformation* info) {
1370 if (IsLoopHeader()) {
1371 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +01001372 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001373 loop_information_ = info;
1374 } else if (loop_information_->Contains(*info->GetHeader())) {
1375 // Block is currently part of an outer loop. Make it part of this inner loop.
1376 // Note that a non loop header having a loop information means this loop information
1377 // has already been populated
1378 loop_information_ = info;
1379 } else {
1380 // Block is part of an inner loop. Do not update the loop information.
1381 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
1382 // at this point, because this method is being called while populating `info`.
1383 }
1384 }
1385
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001386 // Raw update of the loop information.
1387 void SetLoopInformation(HLoopInformation* info) {
1388 loop_information_ = info;
1389 }
1390
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001391 bool IsInLoop() const { return loop_information_ != nullptr; }
1392
David Brazdilec16f792015-08-19 15:04:01 +01001393 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
1394
1395 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
1396 try_catch_information_ = try_catch_information;
1397 }
1398
1399 bool IsTryBlock() const {
1400 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
1401 }
1402
1403 bool IsCatchBlock() const {
1404 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
1405 }
David Brazdilffee3d32015-07-06 11:48:53 +01001406
1407 // Returns the try entry that this block's successors should have. They will
1408 // be in the same try, unless the block ends in a try boundary. In that case,
1409 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +01001410 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001411
Aart Bik75ff2c92018-04-21 01:28:11 +00001412 bool HasThrowingInstructions() const;
1413
David Brazdila4b8c212015-05-07 09:59:30 +01001414 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001415 bool Dominates(HBasicBlock* block) const;
1416
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001417 size_t GetLifetimeStart() const { return lifetime_start_; }
1418 size_t GetLifetimeEnd() const { return lifetime_end_; }
1419
1420 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
1421 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
1422
David Brazdil8d5b8b22015-03-24 10:51:52 +00001423 bool EndsWithControlFlowInstruction() const;
Aart Bik4dc09e72018-05-11 14:40:31 -07001424 bool EndsWithReturn() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001425 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001426 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001427 bool HasSinglePhi() const;
1428
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001429 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001430 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +00001431 ArenaVector<HBasicBlock*> predecessors_;
1432 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001433 HInstructionList instructions_;
1434 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001435 HLoopInformation* loop_information_;
1436 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +00001437 ArenaVector<HBasicBlock*> dominated_blocks_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001438 uint32_t block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001439 // The dex program counter of the first instruction of this block.
1440 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001441 size_t lifetime_start_;
1442 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +01001443 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +01001444
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001445 friend class HGraph;
1446 friend class HInstruction;
Alex Light3a73ffb2021-01-25 14:11:05 +00001447 // Allow manual control of the ordering of predecessors/successors
1448 friend class OptimizingUnitTestHelper;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001449
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001450 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
1451};
1452
David Brazdilb2bd1c52015-03-25 11:17:37 +00001453// Iterates over the LoopInformation of all loops which contain 'block'
1454// from the innermost to the outermost.
1455class HLoopInformationOutwardIterator : public ValueObject {
1456 public:
1457 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
1458 : current_(block.GetLoopInformation()) {}
1459
1460 bool Done() const { return current_ == nullptr; }
1461
1462 void Advance() {
1463 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +01001464 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +00001465 }
1466
1467 HLoopInformation* Current() const {
1468 DCHECK(!Done());
1469 return current_;
1470 }
1471
1472 private:
1473 HLoopInformation* current_;
1474
1475 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
1476};
1477
Artem Serov1a719e42019-07-18 14:24:55 +01001478#define FOR_EACH_CONCRETE_INSTRUCTION_SCALAR_COMMON(M) \
Aart Bike9f37602015-10-09 11:15:55 -07001479 M(Above, Condition) \
1480 M(AboveOrEqual, Condition) \
Aart Bik3dad3412018-02-28 12:01:46 -08001481 M(Abs, UnaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001482 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001483 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001484 M(ArrayGet, Instruction) \
1485 M(ArrayLength, Instruction) \
1486 M(ArraySet, Instruction) \
Aart Bike9f37602015-10-09 11:15:55 -07001487 M(Below, Condition) \
1488 M(BelowOrEqual, Condition) \
David Brazdil66d126e2015-04-03 16:02:44 +01001489 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001490 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001491 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001492 M(CheckCast, Instruction) \
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00001493 M(ClassTableGet, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001494 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001495 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001496 M(Compare, BinaryOperation) \
Igor Murashkind01745e2017-04-05 16:40:31 -07001497 M(ConstructorFence, Instruction) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001498 M(CurrentMethod, Instruction) \
Mingyao Yang063fc772016-08-02 11:02:54 -07001499 M(ShouldDeoptimizeFlag, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001500 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001501 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001502 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001503 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001504 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001505 M(Exit, Instruction) \
1506 M(FloatConstant, Constant) \
1507 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001508 M(GreaterThan, Condition) \
1509 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001510 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001511 M(InstanceFieldGet, Instruction) \
1512 M(InstanceFieldSet, Instruction) \
Alex Light3a73ffb2021-01-25 14:11:05 +00001513 M(PredicatedInstanceFieldGet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001514 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001515 M(IntConstant, Constant) \
xueliang.zhonge0eb4832017-10-30 13:43:14 +00001516 M(IntermediateAddress, Instruction) \
Calin Juravle175dc732015-08-25 15:42:32 +01001517 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001518 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001519 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001520 M(InvokeVirtual, Invoke) \
Orion Hodsonac141392017-01-13 11:53:47 +00001521 M(InvokePolymorphic, Invoke) \
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001522 M(InvokeCustom, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001523 M(LessThan, Condition) \
1524 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001525 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001526 M(LoadException, Instruction) \
Orion Hodsondbaa5c72018-05-10 08:22:46 +01001527 M(LoadMethodHandle, Instruction) \
Orion Hodson18259d72018-04-12 11:18:23 +01001528 M(LoadMethodType, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001529 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001530 M(LongConstant, Constant) \
Aart Bik1f8d51b2018-02-15 10:42:37 -08001531 M(Max, Instruction) \
Calin Juravle27df7582015-04-17 19:12:31 +01001532 M(MemoryBarrier, Instruction) \
Aart Bik1f8d51b2018-02-15 10:42:37 -08001533 M(Min, BinaryOperation) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001534 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001535 M(Mul, BinaryOperation) \
David Srbecky0cf44932015-12-09 14:09:59 +00001536 M(NativeDebugInfo, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001537 M(Neg, UnaryOperation) \
1538 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001539 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001540 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001541 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001542 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001543 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001544 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001545 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001546 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001547 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001548 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001549 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001550 M(Return, Instruction) \
1551 M(ReturnVoid, Instruction) \
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001552 M(Ror, BinaryOperation) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001553 M(Shl, BinaryOperation) \
1554 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001555 M(StaticFieldGet, Instruction) \
1556 M(StaticFieldSet, Instruction) \
Vladimir Marko552a1342017-10-31 10:56:47 +00001557 M(StringBuilderAppend, Instruction) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001558 M(UnresolvedInstanceFieldGet, Instruction) \
1559 M(UnresolvedInstanceFieldSet, Instruction) \
1560 M(UnresolvedStaticFieldGet, Instruction) \
1561 M(UnresolvedStaticFieldSet, Instruction) \
David Brazdil74eb1b22015-12-14 11:44:01 +00001562 M(Select, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001563 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001564 M(SuspendCheck, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001565 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001566 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001567 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001568 M(UShr, BinaryOperation) \
Artem Serov1a719e42019-07-18 14:24:55 +01001569 M(Xor, BinaryOperation)
1570
1571#define FOR_EACH_CONCRETE_INSTRUCTION_VECTOR_COMMON(M) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001572 M(VecReplicateScalar, VecUnaryOperation) \
Aart Bik0148de42017-09-05 09:25:01 -07001573 M(VecExtractScalar, VecUnaryOperation) \
1574 M(VecReduce, VecUnaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001575 M(VecCnv, VecUnaryOperation) \
1576 M(VecNeg, VecUnaryOperation) \
Aart Bik6daebeb2017-04-03 14:35:41 -07001577 M(VecAbs, VecUnaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001578 M(VecNot, VecUnaryOperation) \
1579 M(VecAdd, VecBinaryOperation) \
Aart Bikf3e61ee2017-04-12 17:09:20 -07001580 M(VecHalvingAdd, VecBinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001581 M(VecSub, VecBinaryOperation) \
1582 M(VecMul, VecBinaryOperation) \
1583 M(VecDiv, VecBinaryOperation) \
Aart Bikf3e61ee2017-04-12 17:09:20 -07001584 M(VecMin, VecBinaryOperation) \
1585 M(VecMax, VecBinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001586 M(VecAnd, VecBinaryOperation) \
1587 M(VecAndNot, VecBinaryOperation) \
1588 M(VecOr, VecBinaryOperation) \
1589 M(VecXor, VecBinaryOperation) \
Aart Bik29aa0822018-03-08 11:28:00 -08001590 M(VecSaturationAdd, VecBinaryOperation) \
1591 M(VecSaturationSub, VecBinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001592 M(VecShl, VecBinaryOperation) \
1593 M(VecShr, VecBinaryOperation) \
1594 M(VecUShr, VecBinaryOperation) \
Aart Bik8de59162017-04-21 09:42:01 -07001595 M(VecSetScalars, VecOperation) \
Artem Serovf34dd202017-04-10 17:41:46 +01001596 M(VecMultiplyAccumulate, VecOperation) \
Aart Bikdbbac8f2017-09-01 13:06:08 -07001597 M(VecSADAccumulate, VecOperation) \
Artem Serovaaac0e32018-08-07 00:52:22 +01001598 M(VecDotProd, VecOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001599 M(VecLoad, VecMemoryOperation) \
1600 M(VecStore, VecMemoryOperation) \
Artem Serov07718842020-02-24 18:51:42 +00001601 M(VecPredSetAll, VecPredSetOperation) \
1602 M(VecPredWhile, VecPredSetOperation) \
1603 M(VecPredCondition, VecOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001604
Artem Serov1a719e42019-07-18 14:24:55 +01001605#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
1606 FOR_EACH_CONCRETE_INSTRUCTION_SCALAR_COMMON(M) \
1607 FOR_EACH_CONCRETE_INSTRUCTION_VECTOR_COMMON(M)
1608
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001609/*
1610 * Instructions, shared across several (not all) architectures.
1611 */
1612#if !defined(ART_ENABLE_CODEGEN_arm) && !defined(ART_ENABLE_CODEGEN_arm64)
1613#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M)
1614#else
1615#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Artem Serov7fc63502016-02-09 17:15:29 +00001616 M(BitwiseNegatedRight, Instruction) \
Anton Kirilov74234da2017-01-13 14:42:47 +00001617 M(DataProcWithShifterOp, Instruction) \
Artem Serov328429f2016-07-06 16:23:04 +01001618 M(MultiplyAccumulate, Instruction) \
Artem Serove1811ed2017-04-27 16:50:47 +01001619 M(IntermediateAddressIndex, Instruction)
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001620#endif
1621
Alexandre Ramesef20f712015-06-09 10:29:30 +01001622#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
1623
1624#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
1625
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001626#ifndef ART_ENABLE_CODEGEN_x86
1627#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1628#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001629#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1630 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001631 M(X86LoadFromConstantTable, Instruction) \
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001632 M(X86FPNeg, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001633 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001634#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001635
Shalini Salomi Bodapatidd121f62018-10-26 15:03:53 +05301636#if defined(ART_ENABLE_CODEGEN_x86) || defined(ART_ENABLE_CODEGEN_x86_64)
1637#define FOR_EACH_CONCRETE_INSTRUCTION_X86_COMMON(M) \
Shalini Salomi Bodapati81d15be2019-05-30 11:00:42 +05301638 M(X86AndNot, Instruction) \
Shalini Salomi Bodapatib45a4352019-07-10 16:09:41 +05301639 M(X86MaskOrResetLeastSetBit, Instruction)
Shalini Salomi Bodapatidd121f62018-10-26 15:03:53 +05301640#else
1641#define FOR_EACH_CONCRETE_INSTRUCTION_X86_COMMON(M)
1642#endif
1643
Alexandre Ramesef20f712015-06-09 10:29:30 +01001644#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1645
1646#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1647 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001648 FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001649 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1650 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
1651 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
Shalini Salomi Bodapatidd121f62018-10-26 15:03:53 +05301652 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M) \
1653 FOR_EACH_CONCRETE_INSTRUCTION_X86_COMMON(M)
Alexandre Ramesef20f712015-06-09 10:29:30 +01001654
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001655#define FOR_EACH_ABSTRACT_INSTRUCTION(M) \
1656 M(Condition, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001657 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001658 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001659 M(BinaryOperation, Instruction) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001660 M(Invoke, Instruction) \
1661 M(VecOperation, Instruction) \
1662 M(VecUnaryOperation, VecOperation) \
1663 M(VecBinaryOperation, VecOperation) \
Artem Serov07718842020-02-24 18:51:42 +00001664 M(VecMemoryOperation, VecOperation) \
1665 M(VecPredSetOperation, VecOperation)
Dave Allison20dfc792014-06-16 20:44:29 -07001666
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001667#define FOR_EACH_INSTRUCTION(M) \
1668 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1669 FOR_EACH_ABSTRACT_INSTRUCTION(M)
1670
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001671#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001672FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1673#undef FORWARD_DECLARATION
1674
Vladimir Marko372f10e2016-05-17 16:30:10 +01001675#define DECLARE_INSTRUCTION(type) \
Artem Serovcced8ba2017-07-19 18:18:09 +01001676 private: \
1677 H##type& operator=(const H##type&) = delete; \
1678 public: \
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01001679 const char* DebugName() const override { return #type; } \
1680 HInstruction* Clone(ArenaAllocator* arena) const override { \
Artem Serovcced8ba2017-07-19 18:18:09 +01001681 DCHECK(IsClonable()); \
1682 return new (arena) H##type(*this->As##type()); \
1683 } \
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01001684 void Accept(HGraphVisitor* visitor) override
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001685
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001686#define DECLARE_ABSTRACT_INSTRUCTION(type) \
Artem Serovcced8ba2017-07-19 18:18:09 +01001687 private: \
1688 H##type& operator=(const H##type&) = delete; \
Vladimir Markoa90dd512018-05-04 15:04:45 +01001689 public:
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001690
Alex Light3a73ffb2021-01-25 14:11:05 +00001691#define DEFAULT_COPY_CONSTRUCTOR(type) H##type(const H##type& other) = default;
Artem Serovcced8ba2017-07-19 18:18:09 +01001692
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001693template <typename T>
Vladimir Marko82b07402017-03-01 19:02:04 +00001694class HUseListNode : public ArenaObject<kArenaAllocUseListNode>,
1695 public IntrusiveForwardListNode<HUseListNode<T>> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001696 public:
Igor Murashkind01745e2017-04-05 16:40:31 -07001697 // Get the instruction which has this use as one of the inputs.
David Brazdiled596192015-01-23 10:39:45 +00001698 T GetUser() const { return user_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07001699 // Get the position of the input record that this use corresponds to.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001700 size_t GetIndex() const { return index_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07001701 // Set the position of the input record that this use corresponds to.
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001702 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001703
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001704 private:
David Brazdiled596192015-01-23 10:39:45 +00001705 HUseListNode(T user, size_t index)
Vladimir Marko46817b82016-03-29 12:21:58 +01001706 : user_(user), index_(index) {}
David Brazdiled596192015-01-23 10:39:45 +00001707
1708 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001709 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001710
Vladimir Marko46817b82016-03-29 12:21:58 +01001711 friend class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001712
1713 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1714};
1715
David Brazdiled596192015-01-23 10:39:45 +00001716template <typename T>
Vladimir Marko46817b82016-03-29 12:21:58 +01001717using HUseList = IntrusiveForwardList<HUseListNode<T>>;
David Brazdiled596192015-01-23 10:39:45 +00001718
David Brazdil1abb4192015-02-17 18:33:36 +00001719// This class is used by HEnvironment and HInstruction classes to record the
1720// instructions they use and pointers to the corresponding HUseListNodes kept
1721// by the used instructions.
1722template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001723class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001724 public:
Vladimir Marko46817b82016-03-29 12:21:58 +01001725 HUserRecord() : instruction_(nullptr), before_use_node_() {}
1726 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), before_use_node_() {}
David Brazdil1abb4192015-02-17 18:33:36 +00001727
Vladimir Marko46817b82016-03-29 12:21:58 +01001728 HUserRecord(const HUserRecord<T>& old_record, typename HUseList<T>::iterator before_use_node)
1729 : HUserRecord(old_record.instruction_, before_use_node) {}
1730 HUserRecord(HInstruction* instruction, typename HUseList<T>::iterator before_use_node)
1731 : instruction_(instruction), before_use_node_(before_use_node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001732 DCHECK(instruction_ != nullptr);
David Brazdil1abb4192015-02-17 18:33:36 +00001733 }
1734
1735 HInstruction* GetInstruction() const { return instruction_; }
Vladimir Marko46817b82016-03-29 12:21:58 +01001736 typename HUseList<T>::iterator GetBeforeUseNode() const { return before_use_node_; }
1737 typename HUseList<T>::iterator GetUseNode() const { return ++GetBeforeUseNode(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001738
1739 private:
1740 // Instruction used by the user.
1741 HInstruction* instruction_;
1742
Vladimir Marko46817b82016-03-29 12:21:58 +01001743 // Iterator before the corresponding entry in the use list kept by 'instruction_'.
1744 typename HUseList<T>::iterator before_use_node_;
David Brazdil1abb4192015-02-17 18:33:36 +00001745};
1746
Vladimir Markoe9004912016-06-16 16:50:52 +01001747// Helper class that extracts the input instruction from HUserRecord<HInstruction*>.
1748// This is used for HInstruction::GetInputs() to return a container wrapper providing
1749// HInstruction* values even though the underlying container has HUserRecord<>s.
1750struct HInputExtractor {
1751 HInstruction* operator()(HUserRecord<HInstruction*>& record) const {
1752 return record.GetInstruction();
1753 }
1754 const HInstruction* operator()(const HUserRecord<HInstruction*>& record) const {
1755 return record.GetInstruction();
1756 }
1757};
1758
1759using HInputsRef = TransformArrayRef<HUserRecord<HInstruction*>, HInputExtractor>;
1760using HConstInputsRef = TransformArrayRef<const HUserRecord<HInstruction*>, HInputExtractor>;
1761
Aart Bik854a02b2015-07-14 16:07:00 -07001762/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001763 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001764 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001765 * For write/read dependences on fields/arrays, the dependence analysis uses
1766 * type disambiguation (e.g. a float field write cannot modify the value of an
1767 * integer field read) and the access type (e.g. a reference array write cannot
1768 * modify the value of a reference field read [although it may modify the
1769 * reference fetch prior to reading the field, which is represented by its own
1770 * write/read dependence]). The analysis makes conservative points-to
1771 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1772 * the same, and any reference read depends on any reference read without
1773 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001774 *
Artem Serovd1aa7d02018-06-22 11:35:46 +01001775 * kDependsOnGCBit is defined in the following way: instructions with kDependsOnGCBit must not be
1776 * alive across the point where garbage collection might happen.
1777 *
1778 * Note: Instructions with kCanTriggerGCBit do not depend on each other.
1779 *
1780 * kCanTriggerGCBit must be used for instructions for which GC might happen on the path across
1781 * those instructions from the compiler perspective (between this instruction and the next one
1782 * in the IR).
1783 *
1784 * Note: Instructions which can cause GC only on a fatal slow path do not need
1785 * kCanTriggerGCBit as the execution never returns to the instruction next to the exceptional
1786 * one. However the execution may return to compiled code if there is a catch block in the
1787 * current method; for this purpose the TryBoundary exit instruction has kCanTriggerGCBit
1788 * set.
1789 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001790 * The internal representation uses 38-bit and is described in the table below.
1791 * The first line indicates the side effect, and for field/array accesses the
1792 * second line indicates the type of the access (in the order of the
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001793 * DataType::Type enum).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001794 * The two numbered lines below indicate the bit position in the bitfield (read
1795 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001796 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001797 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1798 * +-------------+---------+---------+--------------+---------+---------+
1799 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1800 * | 3 |333333322|222222221| 1 |111111110|000000000|
1801 * | 7 |654321098|765432109| 8 |765432109|876543210|
1802 *
1803 * Note that, to ease the implementation, 'changes' bits are least significant
1804 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001805 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001806class SideEffects : public ValueObject {
1807 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001808 SideEffects() : flags_(0) {}
1809
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001810 static SideEffects None() {
1811 return SideEffects(0);
1812 }
1813
1814 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001815 return SideEffects(kAllChangeBits | kAllDependOnBits);
1816 }
1817
1818 static SideEffects AllChanges() {
1819 return SideEffects(kAllChangeBits);
1820 }
1821
1822 static SideEffects AllDependencies() {
1823 return SideEffects(kAllDependOnBits);
1824 }
1825
1826 static SideEffects AllExceptGCDependency() {
1827 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1828 }
1829
1830 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001831 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001832 }
1833
Aart Bik34c3ba92015-07-20 14:08:59 -07001834 static SideEffects AllWrites() {
1835 return SideEffects(kAllWrites);
1836 }
1837
1838 static SideEffects AllReads() {
1839 return SideEffects(kAllReads);
1840 }
1841
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001842 static SideEffects FieldWriteOfType(DataType::Type type, bool is_volatile) {
Aart Bik34c3ba92015-07-20 14:08:59 -07001843 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001844 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001845 : SideEffects(TypeFlag(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001846 }
1847
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001848 static SideEffects ArrayWriteOfType(DataType::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001849 return SideEffects(TypeFlag(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001850 }
1851
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001852 static SideEffects FieldReadOfType(DataType::Type type, bool is_volatile) {
Aart Bik34c3ba92015-07-20 14:08:59 -07001853 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001854 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001855 : SideEffects(TypeFlag(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001856 }
1857
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001858 static SideEffects ArrayReadOfType(DataType::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001859 return SideEffects(TypeFlag(type, kArrayReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001860 }
1861
Artem Serovd1aa7d02018-06-22 11:35:46 +01001862 // Returns whether GC might happen across this instruction from the compiler perspective so
1863 // the next instruction in the IR would see that.
1864 //
1865 // See the SideEffect class comments.
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001866 static SideEffects CanTriggerGC() {
1867 return SideEffects(1ULL << kCanTriggerGCBit);
1868 }
1869
Artem Serovd1aa7d02018-06-22 11:35:46 +01001870 // Returns whether the instruction must not be alive across a GC point.
1871 //
1872 // See the SideEffect class comments.
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001873 static SideEffects DependsOnGC() {
1874 return SideEffects(1ULL << kDependsOnGCBit);
1875 }
1876
Aart Bik854a02b2015-07-14 16:07:00 -07001877 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001878 SideEffects Union(SideEffects other) const {
1879 return SideEffects(flags_ | other.flags_);
1880 }
1881
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001882 SideEffects Exclusion(SideEffects other) const {
1883 return SideEffects(flags_ & ~other.flags_);
1884 }
1885
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001886 void Add(SideEffects other) {
1887 flags_ |= other.flags_;
1888 }
1889
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001890 bool Includes(SideEffects other) const {
1891 return (other.flags_ & flags_) == other.flags_;
1892 }
1893
1894 bool HasSideEffects() const {
1895 return (flags_ & kAllChangeBits);
1896 }
1897
1898 bool HasDependencies() const {
1899 return (flags_ & kAllDependOnBits);
1900 }
1901
1902 // Returns true if there are no side effects or dependencies.
1903 bool DoesNothing() const {
1904 return flags_ == 0;
1905 }
1906
Aart Bik854a02b2015-07-14 16:07:00 -07001907 // Returns true if something is written.
1908 bool DoesAnyWrite() const {
1909 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001910 }
1911
Aart Bik854a02b2015-07-14 16:07:00 -07001912 // Returns true if something is read.
1913 bool DoesAnyRead() const {
1914 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001915 }
1916
Aart Bik854a02b2015-07-14 16:07:00 -07001917 // Returns true if potentially everything is written and read
1918 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001919 bool DoesAllReadWrite() const {
1920 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1921 }
1922
Aart Bik854a02b2015-07-14 16:07:00 -07001923 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001924 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001925 }
1926
Roland Levillain0d5a2812015-11-13 10:07:31 +00001927 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001928 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001929 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1930 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001931 }
1932
1933 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001934 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001935 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001936 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001937 for (int s = kLastBit; s >= 0; s--) {
1938 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1939 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1940 // This is a bit for the GC side effect.
1941 if (current_bit_is_set) {
1942 flags += "GC";
1943 }
Aart Bik854a02b2015-07-14 16:07:00 -07001944 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001945 } else {
1946 // This is a bit for the array/field analysis.
1947 // The underscore character stands for the 'can trigger GC' bit.
1948 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1949 if (current_bit_is_set) {
1950 flags += kDebug[s];
1951 }
1952 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1953 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1954 flags += "|";
1955 }
1956 }
Aart Bik854a02b2015-07-14 16:07:00 -07001957 }
1958 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001959 }
1960
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001961 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001962
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001963 private:
1964 static constexpr int kFieldArrayAnalysisBits = 9;
1965
1966 static constexpr int kFieldWriteOffset = 0;
1967 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1968 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1969 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1970
1971 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1972
1973 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1974 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1975 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1976 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1977
1978 static constexpr int kLastBit = kDependsOnGCBit;
1979 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1980
1981 // Aliases.
1982
1983 static_assert(kChangeBits == kDependOnBits,
1984 "the 'change' bits should match the 'depend on' bits.");
1985
1986 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1987 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1988 static constexpr uint64_t kAllWrites =
1989 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1990 static constexpr uint64_t kAllReads =
1991 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001992
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001993 // Translates type to bit flag. The type must correspond to a Java type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001994 static uint64_t TypeFlag(DataType::Type type, int offset) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001995 int shift;
1996 switch (type) {
1997 case DataType::Type::kReference: shift = 0; break;
1998 case DataType::Type::kBool: shift = 1; break;
1999 case DataType::Type::kInt8: shift = 2; break;
2000 case DataType::Type::kUint16: shift = 3; break;
2001 case DataType::Type::kInt16: shift = 4; break;
2002 case DataType::Type::kInt32: shift = 5; break;
2003 case DataType::Type::kInt64: shift = 6; break;
2004 case DataType::Type::kFloat32: shift = 7; break;
2005 case DataType::Type::kFloat64: shift = 8; break;
2006 default:
2007 LOG(FATAL) << "Unexpected data type " << type;
2008 UNREACHABLE();
2009 }
Aart Bik854a02b2015-07-14 16:07:00 -07002010 DCHECK_LE(kFieldWriteOffset, shift);
2011 DCHECK_LT(shift, kArrayWriteOffset);
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002012 return UINT64_C(1) << (shift + offset);
Aart Bik854a02b2015-07-14 16:07:00 -07002013 }
2014
2015 // Private constructor on direct flags value.
2016 explicit SideEffects(uint64_t flags) : flags_(flags) {}
2017
2018 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002019};
2020
David Brazdiled596192015-01-23 10:39:45 +00002021// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01002022class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00002023 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002024 ALWAYS_INLINE HEnvironment(ArenaAllocator* allocator,
Mingyao Yang01b47b02017-02-03 12:09:57 -08002025 size_t number_of_vregs,
2026 ArtMethod* method,
2027 uint32_t dex_pc,
2028 HInstruction* holder)
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002029 : vregs_(number_of_vregs, allocator->Adapter(kArenaAllocEnvironmentVRegs)),
2030 locations_(allocator->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002031 parent_(nullptr),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00002032 method_(method),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002033 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002034 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002035 }
2036
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002037 ALWAYS_INLINE HEnvironment(ArenaAllocator* allocator,
2038 const HEnvironment& to_copy,
2039 HInstruction* holder)
2040 : HEnvironment(allocator,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002041 to_copy.Size(),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00002042 to_copy.GetMethod(),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002043 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002044 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002045
Vladimir Markoec32f642017-06-02 10:51:55 +01002046 void AllocateLocations() {
2047 DCHECK(locations_.empty());
2048 locations_.resize(vregs_.size());
2049 }
2050
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002051 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002052 if (parent_ != nullptr) {
2053 parent_->SetAndCopyParentChain(allocator, parent);
2054 } else {
2055 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
2056 parent_->CopyFrom(parent);
2057 if (parent->GetParent() != nullptr) {
2058 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
2059 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002060 }
David Brazdiled596192015-01-23 10:39:45 +00002061 }
2062
Vladimir Marko69d310e2017-10-09 14:12:23 +01002063 void CopyFrom(ArrayRef<HInstruction* const> locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01002064 void CopyFrom(HEnvironment* environment);
2065
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002066 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
2067 // input to the loop phi instead. This is for inserting instructions that
2068 // require an environment (like HDeoptimization) in the loop pre-header.
2069 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00002070
2071 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01002072 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00002073 }
2074
2075 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01002076 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00002077 }
2078
David Brazdil1abb4192015-02-17 18:33:36 +00002079 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00002080
Artem Serovca210e32017-12-15 13:43:20 +00002081 // Replaces the input at the position 'index' with the replacement; the replacement and old
2082 // input instructions' env_uses_ lists are adjusted. The function works similar to
2083 // HInstruction::ReplaceInput.
2084 void ReplaceInput(HInstruction* replacement, size_t index);
2085
Vladimir Markofa6b93c2015-09-15 10:15:55 +01002086 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00002087
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002088 HEnvironment* GetParent() const { return parent_; }
2089
2090 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01002091 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002092 }
2093
2094 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01002095 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002096 }
2097
2098 uint32_t GetDexPc() const {
2099 return dex_pc_;
2100 }
2101
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00002102 ArtMethod* GetMethod() const {
2103 return method_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002104 }
2105
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002106 HInstruction* GetHolder() const {
2107 return holder_;
2108 }
2109
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00002110
2111 bool IsFromInlinedInvoke() const {
2112 return GetParent() != nullptr;
2113 }
2114
Alex Light3a73ffb2021-01-25 14:11:05 +00002115 class EnvInputSelector {
2116 public:
2117 explicit EnvInputSelector(const HEnvironment* e) : env_(e) {}
2118 HInstruction* operator()(size_t s) const {
2119 return env_->GetInstructionAt(s);
2120 }
2121 private:
2122 const HEnvironment* env_;
2123 };
2124
2125 using HConstEnvInputRef = TransformIterator<CountIter, EnvInputSelector>;
2126 IterationRange<HConstEnvInputRef> GetEnvInputs() const {
2127 IterationRange<CountIter> range(Range(Size()));
2128 return MakeIterationRange(MakeTransformIterator(range.begin(), EnvInputSelector(this)),
2129 MakeTransformIterator(range.end(), EnvInputSelector(this)));
2130 }
2131
David Brazdiled596192015-01-23 10:39:45 +00002132 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01002133 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
2134 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002135 HEnvironment* parent_;
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00002136 ArtMethod* method_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002137 const uint32_t dex_pc_;
David Brazdiled596192015-01-23 10:39:45 +00002138
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01002139 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002140 HInstruction* const holder_;
2141
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01002142 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00002143
2144 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
2145};
2146
Alex Lightdc281e72021-01-06 12:35:31 -08002147std::ostream& operator<<(std::ostream& os, const HInstruction& rhs);
2148
Alex Light3a73ffb2021-01-25 14:11:05 +00002149// Iterates over the Environments
2150class HEnvironmentIterator : public ValueObject,
2151 public std::iterator<std::forward_iterator_tag, HEnvironment*> {
2152 public:
2153 explicit HEnvironmentIterator(HEnvironment* cur) : cur_(cur) {}
2154
2155 HEnvironment* operator*() const {
2156 return cur_;
2157 }
2158
2159 HEnvironmentIterator& operator++() {
2160 DCHECK(cur_ != nullptr);
2161 cur_ = cur_->GetParent();
2162 return *this;
2163 }
2164
2165 HEnvironmentIterator operator++(int) {
2166 HEnvironmentIterator prev(*this);
2167 ++(*this);
2168 return prev;
2169 }
2170
2171 bool operator==(const HEnvironmentIterator& other) const {
2172 return other.cur_ == cur_;
2173 }
2174
2175 bool operator!=(const HEnvironmentIterator& other) const {
2176 return !(*this == other);
2177 }
2178
2179 private:
2180 HEnvironment* cur_;
2181};
2182
Vladimir Markof9f64412015-09-02 14:05:49 +01002183class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002184 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302185#define DECLARE_KIND(type, super) k##type,
Vladimir Marko9974e3c2020-06-10 16:27:06 +01002186 enum InstructionKind { // private marker to avoid generate-operator-out.py from processing.
Vladimir Markoe3946222018-05-04 14:18:47 +01002187 FOR_EACH_CONCRETE_INSTRUCTION(DECLARE_KIND)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302188 kLastInstructionKind
2189 };
2190#undef DECLARE_KIND
2191
2192 HInstruction(InstructionKind kind, SideEffects side_effects, uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01002193 : HInstruction(kind, DataType::Type::kVoid, side_effects, dex_pc) {}
2194
2195 HInstruction(InstructionKind kind, DataType::Type type, SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002196 : previous_(nullptr),
2197 next_(nullptr),
2198 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002199 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002200 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002201 ssa_index_(-1),
Vladimir Markoa1de9182016-02-25 11:37:38 +00002202 packed_fields_(0u),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002203 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002204 locations_(nullptr),
2205 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002206 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00002207 side_effects_(side_effects),
Vladimir Markoa1de9182016-02-25 11:37:38 +00002208 reference_type_handle_(ReferenceTypeInfo::CreateInvalid().GetTypeHandle()) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302209 SetPackedField<InstructionKindField>(kind);
Vladimir Markobd785672018-05-03 17:09:09 +01002210 SetPackedField<TypeField>(type);
Vladimir Markoa1de9182016-02-25 11:37:38 +00002211 SetPackedFlag<kFlagReferenceTypeIsExact>(ReferenceTypeInfo::CreateInvalid().IsExact());
2212 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002213
Dave Allison20dfc792014-06-16 20:44:29 -07002214 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002215
Vladimir Markoc9fcfd02021-01-05 16:57:30 +00002216 std::ostream& Dump(std::ostream& os, bool dump_args = false);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002217
Alex Lightdc281e72021-01-06 12:35:31 -08002218 // Helper for dumping without argument information using operator<<
2219 struct NoArgsDump {
2220 const HInstruction* ins;
2221 };
2222 NoArgsDump DumpWithoutArgs() const {
2223 return NoArgsDump{this};
2224 }
2225 // Helper for dumping with argument information using operator<<
2226 struct ArgsDump {
2227 const HInstruction* ins;
2228 };
2229 ArgsDump DumpWithArgs() const {
2230 return ArgsDump{this};
2231 }
2232
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002233 HInstruction* GetNext() const { return next_; }
2234 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002235
Calin Juravle77520bc2015-01-12 18:45:46 +00002236 HInstruction* GetNextDisregardingMoves() const;
2237 HInstruction* GetPreviousDisregardingMoves() const;
2238
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002239 HBasicBlock* GetBlock() const { return block_; }
Vladimir Markoca6fff82017-10-03 14:49:14 +01002240 ArenaAllocator* GetAllocator() const { return block_->GetGraph()->GetAllocator(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002241 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002242 bool IsInBlock() const { return block_ != nullptr; }
2243 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00002244 bool IsLoopHeaderPhi() const { return IsPhi() && block_->IsLoopHeader(); }
2245 bool IsIrreducibleLoopHeaderPhi() const {
2246 return IsLoopHeaderPhi() && GetBlock()->GetLoopInformation()->IsIrreducible();
2247 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002248
Vladimir Marko372f10e2016-05-17 16:30:10 +01002249 virtual ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() = 0;
2250
2251 ArrayRef<const HUserRecord<HInstruction*>> GetInputRecords() const {
2252 // One virtual method is enough, just const_cast<> and then re-add the const.
2253 return ArrayRef<const HUserRecord<HInstruction*>>(
2254 const_cast<HInstruction*>(this)->GetInputRecords());
2255 }
2256
Vladimir Markoe9004912016-06-16 16:50:52 +01002257 HInputsRef GetInputs() {
2258 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01002259 }
2260
Vladimir Markoe9004912016-06-16 16:50:52 +01002261 HConstInputsRef GetInputs() const {
2262 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01002263 }
2264
2265 size_t InputCount() const { return GetInputRecords().size(); }
David Brazdil1abb4192015-02-17 18:33:36 +00002266 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002267
Aart Bik2767f4b2016-10-28 15:03:53 -07002268 bool HasInput(HInstruction* input) const {
2269 for (const HInstruction* i : GetInputs()) {
2270 if (i == input) {
2271 return true;
2272 }
2273 }
2274 return false;
2275 }
2276
Vladimir Marko372f10e2016-05-17 16:30:10 +01002277 void SetRawInputAt(size_t index, HInstruction* input) {
2278 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
2279 }
2280
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002281 virtual void Accept(HGraphVisitor* visitor) = 0;
2282 virtual const char* DebugName() const = 0;
2283
Vladimir Markobd785672018-05-03 17:09:09 +01002284 DataType::Type GetType() const {
2285 return TypeField::Decode(GetPackedFields());
2286 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002287
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002288 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002289
2290 uint32_t GetDexPc() const { return dex_pc_; }
2291
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002292 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01002293
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002294 // Can the instruction throw?
2295 // TODO: We should rename to CanVisiblyThrow, as some instructions (like HNewInstance),
2296 // could throw OOME, but it is still OK to remove them if they are unused.
Roland Levillaine161a2a2014-10-03 12:45:18 +01002297 virtual bool CanThrow() const { return false; }
Aart Bika8b8e9b2018-01-09 11:01:02 -08002298
2299 // Does the instruction always throw an exception unconditionally?
2300 virtual bool AlwaysThrows() const { return false; }
Alex Light3a73ffb2021-01-25 14:11:05 +00002301 // Will this instruction only cause async exceptions if it causes any at all?
2302 virtual bool OnlyThrowsAsyncExceptions() const {
2303 return false;
2304 }
Aart Bika8b8e9b2018-01-09 11:01:02 -08002305
David Brazdilec16f792015-08-19 15:04:01 +01002306 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07002307
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002308 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07002309 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002310
Calin Juravle10e244f2015-01-26 18:54:32 +00002311 // Does not apply for all instructions, but having this at top level greatly
2312 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00002313 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002314 virtual bool CanBeNull() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002315 DCHECK_EQ(GetType(), DataType::Type::kReference) << "CanBeNull only applies to reference types";
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002316 return true;
2317 }
Calin Juravle10e244f2015-01-26 18:54:32 +00002318
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002319 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01002320 return false;
2321 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002322
Nicolas Geoffray61ba8d22018-08-07 09:55:57 +01002323 // If this instruction will do an implicit null check, return the `HNullCheck` associated
2324 // with it. Otherwise return null.
2325 HNullCheck* GetImplicitNullCheck() const {
Vladimir Markoee1f1262019-06-26 14:37:17 +01002326 // Go over previous non-move instructions that are emitted at use site.
2327 HInstruction* prev_not_move = GetPreviousDisregardingMoves();
2328 while (prev_not_move != nullptr && prev_not_move->IsEmittedAtUseSite()) {
2329 if (prev_not_move->IsNullCheck()) {
2330 return prev_not_move->AsNullCheck();
2331 }
2332 prev_not_move = prev_not_move->GetPreviousDisregardingMoves();
Nicolas Geoffray61ba8d22018-08-07 09:55:57 +01002333 }
2334 return nullptr;
2335 }
2336
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00002337 virtual bool IsActualObject() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002338 return GetType() == DataType::Type::kReference;
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00002339 }
2340
Calin Juravle2e768302015-07-28 14:41:11 +00002341 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00002342
Calin Juravle61d544b2015-02-23 16:46:57 +00002343 ReferenceTypeInfo GetReferenceTypeInfo() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002344 DCHECK_EQ(GetType(), DataType::Type::kReference);
Vladimir Markoa1de9182016-02-25 11:37:38 +00002345 return ReferenceTypeInfo::CreateUnchecked(reference_type_handle_,
Vladimir Marko456307a2016-04-19 14:12:13 +00002346 GetPackedFlag<kFlagReferenceTypeIsExact>());
Calin Juravle61d544b2015-02-23 16:46:57 +00002347 }
Calin Juravleacf735c2015-02-12 15:25:22 +00002348
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002349 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00002350 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01002351 // Note: fixup_end remains valid across push_front().
2352 auto fixup_end = uses_.empty() ? uses_.begin() : ++uses_.begin();
Vladimir Marko3224f382020-06-23 14:19:53 +01002353 ArenaAllocator* allocator = user->GetBlock()->GetGraph()->GetAllocator();
Vladimir Marko46817b82016-03-29 12:21:58 +01002354 HUseListNode<HInstruction*>* new_node =
Vladimir Marko3224f382020-06-23 14:19:53 +01002355 new (allocator) HUseListNode<HInstruction*>(user, index);
Vladimir Marko46817b82016-03-29 12:21:58 +01002356 uses_.push_front(*new_node);
2357 FixUpUserRecordsAfterUseInsertion(fixup_end);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002358 }
2359
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002360 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01002361 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01002362 // Note: env_fixup_end remains valid across push_front().
2363 auto env_fixup_end = env_uses_.empty() ? env_uses_.begin() : ++env_uses_.begin();
2364 HUseListNode<HEnvironment*>* new_node =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002365 new (GetBlock()->GetGraph()->GetAllocator()) HUseListNode<HEnvironment*>(user, index);
Vladimir Marko46817b82016-03-29 12:21:58 +01002366 env_uses_.push_front(*new_node);
2367 FixUpUserRecordsAfterEnvUseInsertion(env_fixup_end);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002368 }
2369
David Brazdil1abb4192015-02-17 18:33:36 +00002370 void RemoveAsUserOfInput(size_t input) {
2371 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
Vladimir Marko46817b82016-03-29 12:21:58 +01002372 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
2373 input_use.GetInstruction()->uses_.erase_after(before_use_node);
2374 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
David Brazdil1abb4192015-02-17 18:33:36 +00002375 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002376
Vladimir Marko372f10e2016-05-17 16:30:10 +01002377 void RemoveAsUserOfAllInputs() {
2378 for (const HUserRecord<HInstruction*>& input_use : GetInputRecords()) {
2379 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
2380 input_use.GetInstruction()->uses_.erase_after(before_use_node);
2381 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
2382 }
2383 }
2384
David Brazdil1abb4192015-02-17 18:33:36 +00002385 const HUseList<HInstruction*>& GetUses() const { return uses_; }
2386 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002387
Vladimir Marko46817b82016-03-29 12:21:58 +01002388 bool HasUses() const { return !uses_.empty() || !env_uses_.empty(); }
2389 bool HasEnvironmentUses() const { return !env_uses_.empty(); }
2390 bool HasNonEnvironmentUses() const { return !uses_.empty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01002391 bool HasOnlyOneNonEnvironmentUse() const {
Vladimir Marko46817b82016-03-29 12:21:58 +01002392 return !HasEnvironmentUses() && GetUses().HasExactlyOneElement();
Alexandre Rames188d4312015-04-09 18:30:21 +01002393 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002394
Aart Bikcc42be02016-10-20 16:14:16 -07002395 bool IsRemovable() const {
Aart Bik482095d2016-10-10 15:39:10 -07002396 return
Aart Bikff7d89c2016-11-07 08:49:28 -08002397 !DoesAnyWrite() &&
Aart Bik482095d2016-10-10 15:39:10 -07002398 !CanThrow() &&
2399 !IsSuspendCheck() &&
2400 !IsControlFlow() &&
2401 !IsNativeDebugInfo() &&
2402 !IsParameterValue() &&
Aart Bik482095d2016-10-10 15:39:10 -07002403 // If we added an explicit barrier then we should keep it.
Igor Murashkind01745e2017-04-05 16:40:31 -07002404 !IsMemoryBarrier() &&
2405 !IsConstructorFence();
Aart Bik482095d2016-10-10 15:39:10 -07002406 }
2407
Aart Bikcc42be02016-10-20 16:14:16 -07002408 bool IsDeadAndRemovable() const {
2409 return IsRemovable() && !HasUses();
2410 }
2411
Roland Levillain6c82d402014-10-13 16:10:27 +01002412 // Does this instruction strictly dominate `other_instruction`?
2413 // Returns false if this instruction and `other_instruction` are the same.
2414 // Aborts if this instruction and `other_instruction` are both phis.
2415 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01002416
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002417 int GetId() const { return id_; }
2418 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002419
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002420 int GetSsaIndex() const { return ssa_index_; }
2421 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
2422 bool HasSsaIndex() const { return ssa_index_ != -1; }
2423
2424 bool HasEnvironment() const { return environment_ != nullptr; }
2425 HEnvironment* GetEnvironment() const { return environment_; }
Alex Light3a73ffb2021-01-25 14:11:05 +00002426 IterationRange<HEnvironmentIterator> GetAllEnvironments() const {
2427 return MakeIterationRange(HEnvironmentIterator(GetEnvironment()),
2428 HEnvironmentIterator(nullptr));
2429 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002430 // Set the `environment_` field. Raw because this method does not
2431 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002432 void SetRawEnvironment(HEnvironment* environment) {
2433 DCHECK(environment_ == nullptr);
2434 DCHECK_EQ(environment->GetHolder(), this);
2435 environment_ = environment;
2436 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002437
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002438 void InsertRawEnvironment(HEnvironment* environment) {
2439 DCHECK(environment_ != nullptr);
2440 DCHECK_EQ(environment->GetHolder(), this);
2441 DCHECK(environment->GetParent() == nullptr);
2442 environment->parent_ = environment_;
2443 environment_ = environment;
2444 }
2445
Vladimir Markocac5a7e2016-02-22 10:39:50 +00002446 void RemoveEnvironment();
2447
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002448 // Set the environment of this instruction, copying it from `environment`. While
2449 // copying, the uses lists are being updated.
2450 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002451 DCHECK(environment_ == nullptr);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002452 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetAllocator();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002453 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002454 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002455 if (environment->GetParent() != nullptr) {
2456 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2457 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002458 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002459
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002460 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
2461 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002462 DCHECK(environment_ == nullptr);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002463 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetAllocator();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002464 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002465 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002466 if (environment->GetParent() != nullptr) {
2467 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2468 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002469 }
2470
Nicolas Geoffray39468442014-09-02 15:17:15 +01002471 // Returns the number of entries in the environment. Typically, that is the
2472 // number of dex registers in a method. It could be more in case of inlining.
2473 size_t EnvironmentSize() const;
2474
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002475 LocationSummary* GetLocations() const { return locations_; }
2476 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002477
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002478 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00002479 void ReplaceUsesDominatedBy(HInstruction* dominator, HInstruction* replacement);
Nicolas Geoffray8a62a4c2018-07-03 09:39:07 +01002480 void ReplaceEnvUsesDominatedBy(HInstruction* dominator, HInstruction* replacement);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002481 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002482
Alexandre Rames188d4312015-04-09 18:30:21 +01002483 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
2484 // uses of this instruction by `other` are *not* updated.
2485 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
2486 ReplaceWith(other);
2487 other->ReplaceInput(this, use_index);
2488 }
2489
Alexandre Rames22aa54b2016-10-18 09:32:29 +01002490 // Move `this` instruction before `cursor`
2491 void MoveBefore(HInstruction* cursor, bool do_checks = true);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002492
Vladimir Markofb337ea2015-11-25 15:25:10 +00002493 // Move `this` before its first user and out of any loops. If there is no
2494 // out-of-loop user that dominates all other users, move the instruction
2495 // to the end of the out-of-loop common dominator of the user's blocks.
2496 //
2497 // This can be used only on non-throwing instructions with no side effects that
2498 // have at least one use but no environment uses.
2499 void MoveBeforeFirstUserAndOutOfLoops();
2500
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002501#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Markoa90dd512018-05-04 15:04:45 +01002502 bool Is##type() const;
2503
2504 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
2505#undef INSTRUCTION_TYPE_CHECK
2506
2507#define INSTRUCTION_TYPE_CAST(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002508 const H##type* As##type() const; \
2509 H##type* As##type();
2510
Vladimir Markoa90dd512018-05-04 15:04:45 +01002511 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CAST)
2512#undef INSTRUCTION_TYPE_CAST
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002513
Artem Serovcced8ba2017-07-19 18:18:09 +01002514 // Return a clone of the instruction if it is clonable (shallow copy by default, custom copy
2515 // if a custom copy-constructor is provided for a particular type). If IsClonable() is false for
2516 // the instruction then the behaviour of this function is undefined.
2517 //
2518 // Note: It is semantically valid to create a clone of the instruction only until
2519 // prepare_for_register_allocator phase as lifetime, intervals and codegen info are not
2520 // copied.
2521 //
2522 // Note: HEnvironment and some other fields are not copied and are set to default values, see
2523 // 'explicit HInstruction(const HInstruction& other)' for details.
2524 virtual HInstruction* Clone(ArenaAllocator* arena ATTRIBUTE_UNUSED) const {
2525 LOG(FATAL) << "Cloning is not implemented for the instruction " <<
2526 DebugName() << " " << GetId();
2527 UNREACHABLE();
2528 }
2529
Alex Light3a73ffb2021-01-25 14:11:05 +00002530 virtual bool IsFieldAccess() const {
2531 return false;
2532 }
2533
2534 virtual const FieldInfo& GetFieldInfo() const {
2535 CHECK(IsFieldAccess()) << "Only callable on field accessors not " << DebugName() << " "
2536 << *this;
2537 LOG(FATAL) << "Must be overridden by field accessors. Not implemented by " << *this;
2538 UNREACHABLE();
2539 }
2540
Artem Serovcced8ba2017-07-19 18:18:09 +01002541 // Return whether instruction can be cloned (copied).
2542 virtual bool IsClonable() const { return false; }
2543
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002544 // Returns whether the instruction can be moved within the graph.
Aart Bik71bf7b42016-11-16 10:17:46 -08002545 // TODO: this method is used by LICM and GVN with possibly different
2546 // meanings? split and rename?
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002547 virtual bool CanBeMoved() const { return false; }
2548
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002549 // Returns whether any data encoded in the two instructions is equal.
2550 // This method does not look at the inputs. Both instructions must be
2551 // of the same type, otherwise the method has undefined behavior.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002552 virtual bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002553 return false;
2554 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002555
2556 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00002557 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002558 // 2) Their inputs are identical.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002559 bool Equals(const HInstruction* other) const;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002560
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302561 InstructionKind GetKind() const { return GetPackedField<InstructionKindField>(); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002562
2563 virtual size_t ComputeHashCode() const {
2564 size_t result = GetKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01002565 for (const HInstruction* input : GetInputs()) {
2566 result = (result * 31) + input->GetId();
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002567 }
2568 return result;
2569 }
2570
2571 SideEffects GetSideEffects() const { return side_effects_; }
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00002572 void SetSideEffects(SideEffects other) { side_effects_ = other; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002573 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002574
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002575 size_t GetLifetimePosition() const { return lifetime_position_; }
2576 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2577 LiveInterval* GetLiveInterval() const { return live_interval_; }
2578 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2579 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2580
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002581 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2582
2583 // Returns whether the code generation of the instruction will require to have access
2584 // to the current method. Such instructions are:
2585 // (1): Instructions that require an environment, as calling the runtime requires
2586 // to walk the stack and have the current method stored at a specific stack address.
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002587 // (2): HCurrentMethod, potentially used by HInvokeStaticOrDirect, HLoadString, or HLoadClass
2588 // to access the dex cache.
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002589 bool NeedsCurrentMethod() const {
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002590 return NeedsEnvironment() || IsCurrentMethod();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002591 }
2592
Mark Mendellc4701932015-04-10 13:18:51 -04002593 // Does this instruction have any use in an environment before
2594 // control flow hits 'other'?
2595 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2596
2597 // Remove all references to environment uses of this instruction.
2598 // The caller must ensure that this is safe to do.
2599 void RemoveEnvironmentUsers();
2600
Vladimir Markoa1de9182016-02-25 11:37:38 +00002601 bool IsEmittedAtUseSite() const { return GetPackedFlag<kFlagEmittedAtUseSite>(); }
2602 void MarkEmittedAtUseSite() { SetPackedFlag<kFlagEmittedAtUseSite>(true); }
David Brazdilb3e773e2016-01-26 11:28:37 +00002603
David Brazdil1abb4192015-02-17 18:33:36 +00002604 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002605 // If set, the machine code for this instruction is assumed to be generated by
2606 // its users. Used by liveness analysis to compute use positions accordingly.
2607 static constexpr size_t kFlagEmittedAtUseSite = 0u;
2608 static constexpr size_t kFlagReferenceTypeIsExact = kFlagEmittedAtUseSite + 1;
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302609 static constexpr size_t kFieldInstructionKind = kFlagReferenceTypeIsExact + 1;
2610 static constexpr size_t kFieldInstructionKindSize =
2611 MinimumBitsToStore(static_cast<size_t>(InstructionKind::kLastInstructionKind - 1));
Vladimir Markobd785672018-05-03 17:09:09 +01002612 static constexpr size_t kFieldType =
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302613 kFieldInstructionKind + kFieldInstructionKindSize;
Vladimir Markobd785672018-05-03 17:09:09 +01002614 static constexpr size_t kFieldTypeSize =
2615 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
2616 static constexpr size_t kNumberOfGenericPackedBits = kFieldType + kFieldTypeSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00002617 static constexpr size_t kMaxNumberOfPackedBits = sizeof(uint32_t) * kBitsPerByte;
2618
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302619 static_assert(kNumberOfGenericPackedBits <= kMaxNumberOfPackedBits,
2620 "Too many generic packed fields");
2621
Vladimir Markobd785672018-05-03 17:09:09 +01002622 using TypeField = BitField<DataType::Type, kFieldType, kFieldTypeSize>;
2623
Vladimir Marko372f10e2016-05-17 16:30:10 +01002624 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const {
2625 return GetInputRecords()[i];
2626 }
2627
2628 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) {
2629 ArrayRef<HUserRecord<HInstruction*>> input_records = GetInputRecords();
2630 input_records[index] = input;
2631 }
David Brazdil1abb4192015-02-17 18:33:36 +00002632
Vladimir Markoa1de9182016-02-25 11:37:38 +00002633 uint32_t GetPackedFields() const {
2634 return packed_fields_;
2635 }
2636
2637 template <size_t flag>
2638 bool GetPackedFlag() const {
2639 return (packed_fields_ & (1u << flag)) != 0u;
2640 }
2641
2642 template <size_t flag>
2643 void SetPackedFlag(bool value = true) {
2644 packed_fields_ = (packed_fields_ & ~(1u << flag)) | ((value ? 1u : 0u) << flag);
2645 }
2646
2647 template <typename BitFieldType>
2648 typename BitFieldType::value_type GetPackedField() const {
2649 return BitFieldType::Decode(packed_fields_);
2650 }
2651
2652 template <typename BitFieldType>
2653 void SetPackedField(typename BitFieldType::value_type value) {
2654 DCHECK(IsUint<BitFieldType::size>(static_cast<uintptr_t>(value)));
2655 packed_fields_ = BitFieldType::Update(value, packed_fields_);
2656 }
2657
Artem Serovcced8ba2017-07-19 18:18:09 +01002658 // Copy construction for the instruction (used for Clone function).
2659 //
2660 // Fields (e.g. lifetime, intervals and codegen info) associated with phases starting from
2661 // prepare_for_register_allocator are not copied (set to default values).
2662 //
2663 // Copy constructors must be provided for every HInstruction type; default copy constructor is
2664 // fine for most of them. However for some of the instructions a custom copy constructor must be
2665 // specified (when instruction has non-trivially copyable fields and must have a special behaviour
2666 // for copying them).
2667 explicit HInstruction(const HInstruction& other)
2668 : previous_(nullptr),
2669 next_(nullptr),
2670 block_(nullptr),
2671 dex_pc_(other.dex_pc_),
2672 id_(-1),
2673 ssa_index_(-1),
2674 packed_fields_(other.packed_fields_),
2675 environment_(nullptr),
2676 locations_(nullptr),
2677 live_interval_(nullptr),
2678 lifetime_position_(kNoLifetime),
2679 side_effects_(other.side_effects_),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302680 reference_type_handle_(other.reference_type_handle_) {
2681 }
Artem Serovcced8ba2017-07-19 18:18:09 +01002682
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002683 private:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302684 using InstructionKindField =
2685 BitField<InstructionKind, kFieldInstructionKind, kFieldInstructionKindSize>;
2686
Vladimir Marko46817b82016-03-29 12:21:58 +01002687 void FixUpUserRecordsAfterUseInsertion(HUseList<HInstruction*>::iterator fixup_end) {
2688 auto before_use_node = uses_.before_begin();
2689 for (auto use_node = uses_.begin(); use_node != fixup_end; ++use_node) {
2690 HInstruction* user = use_node->GetUser();
2691 size_t input_index = use_node->GetIndex();
2692 user->SetRawInputRecordAt(input_index, HUserRecord<HInstruction*>(this, before_use_node));
2693 before_use_node = use_node;
2694 }
2695 }
2696
2697 void FixUpUserRecordsAfterUseRemoval(HUseList<HInstruction*>::iterator before_use_node) {
2698 auto next = ++HUseList<HInstruction*>::iterator(before_use_node);
2699 if (next != uses_.end()) {
2700 HInstruction* next_user = next->GetUser();
2701 size_t next_index = next->GetIndex();
2702 DCHECK(next_user->InputRecordAt(next_index).GetInstruction() == this);
2703 next_user->SetRawInputRecordAt(next_index, HUserRecord<HInstruction*>(this, before_use_node));
2704 }
2705 }
2706
2707 void FixUpUserRecordsAfterEnvUseInsertion(HUseList<HEnvironment*>::iterator env_fixup_end) {
2708 auto before_env_use_node = env_uses_.before_begin();
2709 for (auto env_use_node = env_uses_.begin(); env_use_node != env_fixup_end; ++env_use_node) {
2710 HEnvironment* user = env_use_node->GetUser();
2711 size_t input_index = env_use_node->GetIndex();
2712 user->vregs_[input_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2713 before_env_use_node = env_use_node;
2714 }
2715 }
2716
2717 void FixUpUserRecordsAfterEnvUseRemoval(HUseList<HEnvironment*>::iterator before_env_use_node) {
2718 auto next = ++HUseList<HEnvironment*>::iterator(before_env_use_node);
2719 if (next != env_uses_.end()) {
2720 HEnvironment* next_user = next->GetUser();
2721 size_t next_index = next->GetIndex();
2722 DCHECK(next_user->vregs_[next_index].GetInstruction() == this);
2723 next_user->vregs_[next_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2724 }
2725 }
David Brazdil1abb4192015-02-17 18:33:36 +00002726
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002727 HInstruction* previous_;
2728 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002729 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002730 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002731
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002732 // An instruction gets an id when it is added to the graph.
2733 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002734 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002735 int id_;
2736
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002737 // When doing liveness analysis, instructions that have uses get an SSA index.
2738 int ssa_index_;
2739
Vladimir Markoa1de9182016-02-25 11:37:38 +00002740 // Packed fields.
2741 uint32_t packed_fields_;
David Brazdilb3e773e2016-01-26 11:28:37 +00002742
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002743 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002744 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002745
2746 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002747 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002748
Nicolas Geoffray39468442014-09-02 15:17:15 +01002749 // The environment associated with this instruction. Not null if the instruction
2750 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002751 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002752
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002753 // Set by the code generator.
2754 LocationSummary* locations_;
2755
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002756 // Set by the liveness analysis.
2757 LiveInterval* live_interval_;
2758
2759 // Set by the liveness analysis, this is the position in a linear
2760 // order of blocks where this instruction's live interval start.
2761 size_t lifetime_position_;
2762
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002763 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002764
Vladimir Markoa1de9182016-02-25 11:37:38 +00002765 // The reference handle part of the reference type info.
2766 // The IsExact() flag is stored in packed fields.
Calin Juravleacf735c2015-02-12 15:25:22 +00002767 // TODO: for primitive types this should be marked as invalid.
Vladimir Markoa1de9182016-02-25 11:37:38 +00002768 ReferenceTypeInfo::TypeHandle reference_type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00002769
David Brazdil1abb4192015-02-17 18:33:36 +00002770 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002771 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002772 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002773 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002774 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002775};
Alex Light3a73ffb2021-01-25 14:11:05 +00002776
Vladimir Marko9974e3c2020-06-10 16:27:06 +01002777std::ostream& operator<<(std::ostream& os, HInstruction::InstructionKind rhs);
Alex Lightdc281e72021-01-06 12:35:31 -08002778std::ostream& operator<<(std::ostream& os, const HInstruction::NoArgsDump rhs);
2779std::ostream& operator<<(std::ostream& os, const HInstruction::ArgsDump rhs);
2780std::ostream& operator<<(std::ostream& os, const HUseList<HInstruction*>& lst);
2781std::ostream& operator<<(std::ostream& os, const HUseList<HEnvironment*>& lst);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002782
Alex Light3a73ffb2021-01-25 14:11:05 +00002783// Forward declarations for friends
2784template <typename InnerIter> struct HSTLInstructionIterator;
2785
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002786// Iterates over the instructions, while preserving the next instruction
2787// in case the current instruction gets removed from the list by the user
2788// of this iterator.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002789class HInstructionIterator : public ValueObject {
2790 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002791 explicit HInstructionIterator(const HInstructionList& instructions)
2792 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002793 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002794 }
2795
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002796 bool Done() const { return instruction_ == nullptr; }
2797 HInstruction* Current() const { return instruction_; }
2798 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002799 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002800 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002801 }
2802
2803 private:
Alex Light3a73ffb2021-01-25 14:11:05 +00002804 HInstructionIterator() : instruction_(nullptr), next_(nullptr) {}
2805
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002806 HInstruction* instruction_;
2807 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002808
Alex Light3a73ffb2021-01-25 14:11:05 +00002809 friend struct HSTLInstructionIterator<HInstructionIterator>;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002810};
2811
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002812// Iterates over the instructions without saving the next instruction,
2813// therefore handling changes in the graph potentially made by the user
2814// of this iterator.
2815class HInstructionIteratorHandleChanges : public ValueObject {
2816 public:
2817 explicit HInstructionIteratorHandleChanges(const HInstructionList& instructions)
2818 : instruction_(instructions.first_instruction_) {
2819 }
2820
2821 bool Done() const { return instruction_ == nullptr; }
2822 HInstruction* Current() const { return instruction_; }
2823 void Advance() {
2824 instruction_ = instruction_->GetNext();
2825 }
2826
2827 private:
Alex Light3a73ffb2021-01-25 14:11:05 +00002828 HInstructionIteratorHandleChanges() : instruction_(nullptr) {}
2829
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002830 HInstruction* instruction_;
2831
Alex Light3a73ffb2021-01-25 14:11:05 +00002832 friend struct HSTLInstructionIterator<HInstructionIteratorHandleChanges>;
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002833};
2834
2835
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002836class HBackwardInstructionIterator : public ValueObject {
2837 public:
2838 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2839 : instruction_(instructions.last_instruction_) {
2840 next_ = Done() ? nullptr : instruction_->GetPrevious();
2841 }
2842
2843 bool Done() const { return instruction_ == nullptr; }
2844 HInstruction* Current() const { return instruction_; }
2845 void Advance() {
2846 instruction_ = next_;
2847 next_ = Done() ? nullptr : instruction_->GetPrevious();
2848 }
2849
2850 private:
Alex Light3a73ffb2021-01-25 14:11:05 +00002851 HBackwardInstructionIterator() : instruction_(nullptr), next_(nullptr) {}
2852
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002853 HInstruction* instruction_;
2854 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002855
Alex Light3a73ffb2021-01-25 14:11:05 +00002856 friend struct HSTLInstructionIterator<HBackwardInstructionIterator>;
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002857};
2858
Alex Light3a73ffb2021-01-25 14:11:05 +00002859template <typename InnerIter>
2860struct HSTLInstructionIterator : public ValueObject,
2861 public std::iterator<std::forward_iterator_tag, HInstruction*> {
2862 public:
2863 static_assert(std::is_same_v<InnerIter, HBackwardInstructionIterator> ||
2864 std::is_same_v<InnerIter, HInstructionIterator> ||
2865 std::is_same_v<InnerIter, HInstructionIteratorHandleChanges>,
2866 "Unknown wrapped iterator!");
2867
2868 explicit HSTLInstructionIterator(InnerIter inner) : inner_(inner) {}
2869 HInstruction* operator*() const {
2870 DCHECK(inner_.Current() != nullptr);
2871 return inner_.Current();
2872 }
2873
2874 HSTLInstructionIterator<InnerIter>& operator++() {
2875 DCHECK(*this != HSTLInstructionIterator<InnerIter>::EndIter());
2876 inner_.Advance();
2877 return *this;
2878 }
2879
2880 HSTLInstructionIterator<InnerIter> operator++(int) {
2881 HSTLInstructionIterator<InnerIter> prev(*this);
2882 ++(*this);
2883 return prev;
2884 }
2885
2886 bool operator==(const HSTLInstructionIterator<InnerIter>& other) const {
2887 return inner_.Current() == other.inner_.Current();
2888 }
2889
2890 bool operator!=(const HSTLInstructionIterator<InnerIter>& other) const {
2891 return !(*this == other);
2892 }
2893
2894 static HSTLInstructionIterator<InnerIter> EndIter() {
2895 return HSTLInstructionIterator<InnerIter>(InnerIter());
2896 }
2897
2898 private:
2899 InnerIter inner_;
2900};
2901
2902template <typename InnerIter>
2903IterationRange<HSTLInstructionIterator<InnerIter>> MakeSTLInstructionIteratorRange(InnerIter iter) {
2904 return MakeIterationRange(HSTLInstructionIterator<InnerIter>(iter),
2905 HSTLInstructionIterator<InnerIter>::EndIter());
2906}
2907
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002908class HVariableInputSizeInstruction : public HInstruction {
2909 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08002910 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002911 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() override {
Mingyao Yangb0b051a2016-11-17 09:04:53 -08002912 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
2913 }
2914
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002915 void AddInput(HInstruction* input);
2916 void InsertInputAt(size_t index, HInstruction* input);
2917 void RemoveInputAt(size_t index);
2918
Igor Murashkind01745e2017-04-05 16:40:31 -07002919 // Removes all the inputs.
2920 // Also removes this instructions from each input's use list
2921 // (for non-environment uses only).
2922 void RemoveAllInputs();
2923
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002924 protected:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302925 HVariableInputSizeInstruction(InstructionKind inst_kind,
2926 SideEffects side_effects,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002927 uint32_t dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002928 ArenaAllocator* allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002929 size_t number_of_inputs,
2930 ArenaAllocKind kind)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302931 : HInstruction(inst_kind, side_effects, dex_pc),
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002932 inputs_(number_of_inputs, allocator->Adapter(kind)) {}
Vladimir Markobd785672018-05-03 17:09:09 +01002933 HVariableInputSizeInstruction(InstructionKind inst_kind,
2934 DataType::Type type,
2935 SideEffects side_effects,
2936 uint32_t dex_pc,
2937 ArenaAllocator* allocator,
2938 size_t number_of_inputs,
2939 ArenaAllocKind kind)
2940 : HInstruction(inst_kind, type, side_effects, dex_pc),
2941 inputs_(number_of_inputs, allocator->Adapter(kind)) {}
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002942
Artem Serovcced8ba2017-07-19 18:18:09 +01002943 DEFAULT_COPY_CONSTRUCTOR(VariableInputSizeInstruction);
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002944
Artem Serovcced8ba2017-07-19 18:18:09 +01002945 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002946};
2947
Vladimir Markof9f64412015-09-02 14:05:49 +01002948template<size_t N>
Vladimir Markobd785672018-05-03 17:09:09 +01002949class HExpression : public HInstruction {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002950 public:
Vladimir Markobd785672018-05-03 17:09:09 +01002951 HExpression<N>(InstructionKind kind, SideEffects side_effects, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302952 : HInstruction(kind, side_effects, dex_pc), inputs_() {}
Vladimir Markobd785672018-05-03 17:09:09 +01002953 HExpression<N>(InstructionKind kind,
2954 DataType::Type type,
2955 SideEffects side_effects,
2956 uint32_t dex_pc)
2957 : HInstruction(kind, type, side_effects, dex_pc), inputs_() {}
2958 virtual ~HExpression() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002959
Vladimir Marko372f10e2016-05-17 16:30:10 +01002960 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01002961 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Vladimir Marko372f10e2016-05-17 16:30:10 +01002962 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002963 }
2964
Artem Serovcced8ba2017-07-19 18:18:09 +01002965 protected:
Vladimir Markobd785672018-05-03 17:09:09 +01002966 DEFAULT_COPY_CONSTRUCTOR(Expression<N>);
Artem Serovcced8ba2017-07-19 18:18:09 +01002967
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002968 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002969 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002970
2971 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002972};
2973
Vladimir Markobd785672018-05-03 17:09:09 +01002974// HExpression specialization for N=0.
Vladimir Markof9f64412015-09-02 14:05:49 +01002975template<>
Vladimir Markobd785672018-05-03 17:09:09 +01002976class HExpression<0> : public HInstruction {
Vladimir Markof9f64412015-09-02 14:05:49 +01002977 public:
Vladimir Markobd785672018-05-03 17:09:09 +01002978 using HInstruction::HInstruction;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002979
Vladimir Markobd785672018-05-03 17:09:09 +01002980 virtual ~HExpression() {}
Vladimir Markof9f64412015-09-02 14:05:49 +01002981
Vladimir Marko372f10e2016-05-17 16:30:10 +01002982 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01002983 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Vladimir Marko372f10e2016-05-17 16:30:10 +01002984 return ArrayRef<HUserRecord<HInstruction*>>();
Vladimir Markof9f64412015-09-02 14:05:49 +01002985 }
2986
Artem Serovcced8ba2017-07-19 18:18:09 +01002987 protected:
Vladimir Markobd785672018-05-03 17:09:09 +01002988 DEFAULT_COPY_CONSTRUCTOR(Expression<0>);
Artem Serovcced8ba2017-07-19 18:18:09 +01002989
Vladimir Markof9f64412015-09-02 14:05:49 +01002990 private:
2991 friend class SsaBuilder;
2992};
2993
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002994// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2995// instruction that branches to the exit block.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002996class HReturnVoid final : public HExpression<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002997 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002998 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01002999 : HExpression(kReturnVoid, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303000 }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01003001
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003002 bool IsControlFlow() const override { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003003
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003004 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003005
Artem Serovcced8ba2017-07-19 18:18:09 +01003006 protected:
3007 DEFAULT_COPY_CONSTRUCTOR(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003008};
3009
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003010// Represents dex's RETURN opcodes. A HReturn is a control flow
3011// instruction that branches to the exit block.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003012class HReturn final : public HExpression<1> {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003013 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003014 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01003015 : HExpression(kReturn, SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003016 SetRawInputAt(0, value);
3017 }
3018
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003019 bool IsControlFlow() const override { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01003020
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003021 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003022
Artem Serovcced8ba2017-07-19 18:18:09 +01003023 protected:
3024 DEFAULT_COPY_CONSTRUCTOR(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003025};
3026
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003027class HPhi final : public HVariableInputSizeInstruction {
David Brazdildee58d62016-04-07 09:54:26 +00003028 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003029 HPhi(ArenaAllocator* allocator,
David Brazdildee58d62016-04-07 09:54:26 +00003030 uint32_t reg_number,
3031 size_t number_of_inputs,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003032 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +00003033 uint32_t dex_pc = kNoDexPc)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08003034 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303035 kPhi,
Vladimir Markobd785672018-05-03 17:09:09 +01003036 ToPhiType(type),
Mingyao Yanga9dbe832016-12-15 12:02:53 -08003037 SideEffects::None(),
3038 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003039 allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08003040 number_of_inputs,
3041 kArenaAllocPhiInputs),
David Brazdildee58d62016-04-07 09:54:26 +00003042 reg_number_(reg_number) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003043 DCHECK_NE(GetType(), DataType::Type::kVoid);
David Brazdildee58d62016-04-07 09:54:26 +00003044 // Phis are constructed live and marked dead if conflicting or unused.
3045 // Individual steps of SsaBuilder should assume that if a phi has been
3046 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
3047 SetPackedFlag<kFlagIsLive>(true);
3048 SetPackedFlag<kFlagCanBeNull>(true);
3049 }
3050
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003051 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01003052
David Brazdildee58d62016-04-07 09:54:26 +00003053 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003054 static DataType::Type ToPhiType(DataType::Type type) {
3055 return DataType::Kind(type);
David Brazdildee58d62016-04-07 09:54:26 +00003056 }
3057
3058 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
3059
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003060 void SetType(DataType::Type new_type) {
David Brazdildee58d62016-04-07 09:54:26 +00003061 // Make sure that only valid type changes occur. The following are allowed:
3062 // (1) int -> float/ref (primitive type propagation),
3063 // (2) long -> double (primitive type propagation).
3064 DCHECK(GetType() == new_type ||
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003065 (GetType() == DataType::Type::kInt32 && new_type == DataType::Type::kFloat32) ||
3066 (GetType() == DataType::Type::kInt32 && new_type == DataType::Type::kReference) ||
3067 (GetType() == DataType::Type::kInt64 && new_type == DataType::Type::kFloat64));
David Brazdildee58d62016-04-07 09:54:26 +00003068 SetPackedField<TypeField>(new_type);
3069 }
3070
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003071 bool CanBeNull() const override { return GetPackedFlag<kFlagCanBeNull>(); }
David Brazdildee58d62016-04-07 09:54:26 +00003072 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
3073
3074 uint32_t GetRegNumber() const { return reg_number_; }
3075
3076 void SetDead() { SetPackedFlag<kFlagIsLive>(false); }
3077 void SetLive() { SetPackedFlag<kFlagIsLive>(true); }
3078 bool IsDead() const { return !IsLive(); }
3079 bool IsLive() const { return GetPackedFlag<kFlagIsLive>(); }
3080
Vladimir Markoe9004912016-06-16 16:50:52 +01003081 bool IsVRegEquivalentOf(const HInstruction* other) const {
David Brazdildee58d62016-04-07 09:54:26 +00003082 return other != nullptr
3083 && other->IsPhi()
3084 && other->AsPhi()->GetBlock() == GetBlock()
3085 && other->AsPhi()->GetRegNumber() == GetRegNumber();
3086 }
3087
Nicolas Geoffrayf57c1ae2017-06-28 17:40:18 +01003088 bool HasEquivalentPhi() const {
3089 if (GetPrevious() != nullptr && GetPrevious()->AsPhi()->GetRegNumber() == GetRegNumber()) {
3090 return true;
3091 }
3092 if (GetNext() != nullptr && GetNext()->AsPhi()->GetRegNumber() == GetRegNumber()) {
3093 return true;
3094 }
3095 return false;
3096 }
3097
David Brazdildee58d62016-04-07 09:54:26 +00003098 // Returns the next equivalent phi (starting from the current one) or null if there is none.
3099 // An equivalent phi is a phi having the same dex register and type.
3100 // It assumes that phis with the same dex register are adjacent.
3101 HPhi* GetNextEquivalentPhiWithSameType() {
3102 HInstruction* next = GetNext();
3103 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
3104 if (next->GetType() == GetType()) {
3105 return next->AsPhi();
3106 }
3107 next = next->GetNext();
3108 }
3109 return nullptr;
3110 }
3111
3112 DECLARE_INSTRUCTION(Phi);
3113
Artem Serovcced8ba2017-07-19 18:18:09 +01003114 protected:
3115 DEFAULT_COPY_CONSTRUCTOR(Phi);
3116
David Brazdildee58d62016-04-07 09:54:26 +00003117 private:
Vladimir Markobd785672018-05-03 17:09:09 +01003118 static constexpr size_t kFlagIsLive = HInstruction::kNumberOfGenericPackedBits;
David Brazdildee58d62016-04-07 09:54:26 +00003119 static constexpr size_t kFlagCanBeNull = kFlagIsLive + 1;
3120 static constexpr size_t kNumberOfPhiPackedBits = kFlagCanBeNull + 1;
3121 static_assert(kNumberOfPhiPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
David Brazdildee58d62016-04-07 09:54:26 +00003122
David Brazdildee58d62016-04-07 09:54:26 +00003123 const uint32_t reg_number_;
David Brazdildee58d62016-04-07 09:54:26 +00003124};
3125
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003126// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00003127// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003128// exit block.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003129class HExit final : public HExpression<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003130 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303131 explicit HExit(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01003132 : HExpression(kExit, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303133 }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01003134
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003135 bool IsControlFlow() const override { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003136
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003137 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003138
Artem Serovcced8ba2017-07-19 18:18:09 +01003139 protected:
3140 DEFAULT_COPY_CONSTRUCTOR(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003141};
3142
3143// Jumps from one block to another.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003144class HGoto final : public HExpression<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003145 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303146 explicit HGoto(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01003147 : HExpression(kGoto, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303148 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003149
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003150 bool IsClonable() const override { return true; }
3151 bool IsControlFlow() const override { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003152
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00003153 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00003154 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00003155 }
3156
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003157 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003158
Artem Serovcced8ba2017-07-19 18:18:09 +01003159 protected:
3160 DEFAULT_COPY_CONSTRUCTOR(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00003161};
3162
Roland Levillain9867bc72015-08-05 10:21:34 +01003163class HConstant : public HExpression<0> {
3164 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303165 explicit HConstant(InstructionKind kind, DataType::Type type, uint32_t dex_pc = kNoDexPc)
3166 : HExpression(kind, type, SideEffects::None(), dex_pc) {
3167 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003168
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003169 bool CanBeMoved() const override { return true; }
Roland Levillain9867bc72015-08-05 10:21:34 +01003170
Roland Levillain1a653882016-03-18 18:05:57 +00003171 // Is this constant -1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01003172 virtual bool IsMinusOne() const { return false; }
Roland Levillain1a653882016-03-18 18:05:57 +00003173 // Is this constant 0 in the arithmetic sense?
3174 virtual bool IsArithmeticZero() const { return false; }
3175 // Is this constant a 0-bit pattern?
3176 virtual bool IsZeroBitPattern() const { return false; }
3177 // Is this constant 1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01003178 virtual bool IsOne() const { return false; }
3179
David Brazdil77a48ae2015-09-15 12:34:04 +00003180 virtual uint64_t GetValueAsUint64() const = 0;
3181
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003182 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01003183
Artem Serovcced8ba2017-07-19 18:18:09 +01003184 protected:
3185 DEFAULT_COPY_CONSTRUCTOR(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01003186};
3187
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003188class HNullConstant final : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01003189 public:
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003190 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01003191 return true;
3192 }
3193
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003194 uint64_t GetValueAsUint64() const override { return 0; }
David Brazdil77a48ae2015-09-15 12:34:04 +00003195
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003196 size_t ComputeHashCode() const override { return 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01003197
Roland Levillain1a653882016-03-18 18:05:57 +00003198 // The null constant representation is a 0-bit pattern.
Yi Kong39402542019-03-24 02:47:16 -07003199 bool IsZeroBitPattern() const override { return true; }
Roland Levillain1a653882016-03-18 18:05:57 +00003200
Roland Levillain9867bc72015-08-05 10:21:34 +01003201 DECLARE_INSTRUCTION(NullConstant);
3202
Artem Serovcced8ba2017-07-19 18:18:09 +01003203 protected:
3204 DEFAULT_COPY_CONSTRUCTOR(NullConstant);
3205
Roland Levillain9867bc72015-08-05 10:21:34 +01003206 private:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003207 explicit HNullConstant(uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303208 : HConstant(kNullConstant, DataType::Type::kReference, dex_pc) {
3209 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003210
3211 friend class HGraph;
Roland Levillain9867bc72015-08-05 10:21:34 +01003212};
3213
3214// Constants of the type int. Those can be from Dex instructions, or
3215// synthesized (for example with the if-eqz instruction).
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003216class HIntConstant final : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01003217 public:
3218 int32_t GetValue() const { return value_; }
3219
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003220 uint64_t GetValueAsUint64() const override {
David Brazdil9f389d42015-10-01 14:32:56 +01003221 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
3222 }
David Brazdil77a48ae2015-09-15 12:34:04 +00003223
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003224 bool InstructionDataEquals(const HInstruction* other) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003225 DCHECK(other->IsIntConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01003226 return other->AsIntConstant()->value_ == value_;
3227 }
3228
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003229 size_t ComputeHashCode() const override { return GetValue(); }
Roland Levillain9867bc72015-08-05 10:21:34 +01003230
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003231 bool IsMinusOne() const override { return GetValue() == -1; }
3232 bool IsArithmeticZero() const override { return GetValue() == 0; }
3233 bool IsZeroBitPattern() const override { return GetValue() == 0; }
3234 bool IsOne() const override { return GetValue() == 1; }
Roland Levillain9867bc72015-08-05 10:21:34 +01003235
Roland Levillain1a653882016-03-18 18:05:57 +00003236 // Integer constants are used to encode Boolean values as well,
3237 // where 1 means true and 0 means false.
3238 bool IsTrue() const { return GetValue() == 1; }
3239 bool IsFalse() const { return GetValue() == 0; }
3240
Roland Levillain9867bc72015-08-05 10:21:34 +01003241 DECLARE_INSTRUCTION(IntConstant);
3242
Artem Serovcced8ba2017-07-19 18:18:09 +01003243 protected:
3244 DEFAULT_COPY_CONSTRUCTOR(IntConstant);
3245
Roland Levillain9867bc72015-08-05 10:21:34 +01003246 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003247 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303248 : HConstant(kIntConstant, DataType::Type::kInt32, dex_pc), value_(value) {
3249 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003250 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303251 : HConstant(kIntConstant, DataType::Type::kInt32, dex_pc),
3252 value_(value ? 1 : 0) {
3253 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003254
3255 const int32_t value_;
3256
3257 friend class HGraph;
3258 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
3259 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
Roland Levillain9867bc72015-08-05 10:21:34 +01003260};
3261
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003262class HLongConstant final : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01003263 public:
3264 int64_t GetValue() const { return value_; }
3265
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003266 uint64_t GetValueAsUint64() const override { return value_; }
David Brazdil77a48ae2015-09-15 12:34:04 +00003267
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003268 bool InstructionDataEquals(const HInstruction* other) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003269 DCHECK(other->IsLongConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01003270 return other->AsLongConstant()->value_ == value_;
3271 }
3272
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003273 size_t ComputeHashCode() const override { return static_cast<size_t>(GetValue()); }
Roland Levillain9867bc72015-08-05 10:21:34 +01003274
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003275 bool IsMinusOne() const override { return GetValue() == -1; }
3276 bool IsArithmeticZero() const override { return GetValue() == 0; }
3277 bool IsZeroBitPattern() const override { return GetValue() == 0; }
3278 bool IsOne() const override { return GetValue() == 1; }
Roland Levillain9867bc72015-08-05 10:21:34 +01003279
3280 DECLARE_INSTRUCTION(LongConstant);
3281
Artem Serovcced8ba2017-07-19 18:18:09 +01003282 protected:
3283 DEFAULT_COPY_CONSTRUCTOR(LongConstant);
3284
Roland Levillain9867bc72015-08-05 10:21:34 +01003285 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003286 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303287 : HConstant(kLongConstant, DataType::Type::kInt64, dex_pc),
3288 value_(value) {
3289 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003290
3291 const int64_t value_;
3292
3293 friend class HGraph;
Roland Levillain9867bc72015-08-05 10:21:34 +01003294};
Dave Allison20dfc792014-06-16 20:44:29 -07003295
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003296class HFloatConstant final : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003297 public:
3298 float GetValue() const { return value_; }
3299
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003300 uint64_t GetValueAsUint64() const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003301 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
3302 }
3303
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003304 bool InstructionDataEquals(const HInstruction* other) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003305 DCHECK(other->IsFloatConstant()) << other->DebugName();
3306 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
3307 }
3308
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003309 size_t ComputeHashCode() const override { return static_cast<size_t>(GetValue()); }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003310
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003311 bool IsMinusOne() const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003312 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
3313 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003314 bool IsArithmeticZero() const override {
Roland Levillain1a653882016-03-18 18:05:57 +00003315 return std::fpclassify(value_) == FP_ZERO;
3316 }
3317 bool IsArithmeticPositiveZero() const {
3318 return IsArithmeticZero() && !std::signbit(value_);
3319 }
3320 bool IsArithmeticNegativeZero() const {
3321 return IsArithmeticZero() && std::signbit(value_);
3322 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003323 bool IsZeroBitPattern() const override {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00003324 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(0.0f);
Roland Levillain31dd3d62016-02-16 12:21:02 +00003325 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003326 bool IsOne() const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003327 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
3328 }
3329 bool IsNaN() const {
3330 return std::isnan(value_);
3331 }
3332
3333 DECLARE_INSTRUCTION(FloatConstant);
3334
Artem Serovcced8ba2017-07-19 18:18:09 +01003335 protected:
3336 DEFAULT_COPY_CONSTRUCTOR(FloatConstant);
3337
Roland Levillain31dd3d62016-02-16 12:21:02 +00003338 private:
3339 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303340 : HConstant(kFloatConstant, DataType::Type::kFloat32, dex_pc),
3341 value_(value) {
3342 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003343 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303344 : HConstant(kFloatConstant, DataType::Type::kFloat32, dex_pc),
3345 value_(bit_cast<float, int32_t>(value)) {
3346 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003347
3348 const float value_;
3349
3350 // Only the SsaBuilder and HGraph can create floating-point constants.
3351 friend class SsaBuilder;
3352 friend class HGraph;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003353};
3354
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003355class HDoubleConstant final : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003356 public:
3357 double GetValue() const { return value_; }
3358
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003359 uint64_t GetValueAsUint64() const override { return bit_cast<uint64_t, double>(value_); }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003360
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003361 bool InstructionDataEquals(const HInstruction* other) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003362 DCHECK(other->IsDoubleConstant()) << other->DebugName();
3363 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
3364 }
3365
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003366 size_t ComputeHashCode() const override { return static_cast<size_t>(GetValue()); }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003367
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003368 bool IsMinusOne() const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003369 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
3370 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003371 bool IsArithmeticZero() const override {
Roland Levillain1a653882016-03-18 18:05:57 +00003372 return std::fpclassify(value_) == FP_ZERO;
3373 }
3374 bool IsArithmeticPositiveZero() const {
3375 return IsArithmeticZero() && !std::signbit(value_);
3376 }
3377 bool IsArithmeticNegativeZero() const {
3378 return IsArithmeticZero() && std::signbit(value_);
3379 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003380 bool IsZeroBitPattern() const override {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00003381 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((0.0));
Roland Levillain31dd3d62016-02-16 12:21:02 +00003382 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003383 bool IsOne() const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003384 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
3385 }
3386 bool IsNaN() const {
3387 return std::isnan(value_);
3388 }
3389
3390 DECLARE_INSTRUCTION(DoubleConstant);
3391
Artem Serovcced8ba2017-07-19 18:18:09 +01003392 protected:
3393 DEFAULT_COPY_CONSTRUCTOR(DoubleConstant);
3394
Roland Levillain31dd3d62016-02-16 12:21:02 +00003395 private:
3396 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303397 : HConstant(kDoubleConstant, DataType::Type::kFloat64, dex_pc),
3398 value_(value) {
3399 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003400 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303401 : HConstant(kDoubleConstant, DataType::Type::kFloat64, dex_pc),
3402 value_(bit_cast<double, int64_t>(value)) {
3403 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003404
3405 const double value_;
3406
3407 // Only the SsaBuilder and HGraph can create floating-point constants.
3408 friend class SsaBuilder;
3409 friend class HGraph;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003410};
3411
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003412// Conditional branch. A block ending with an HIf instruction must have
3413// two successors.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003414class HIf final : public HExpression<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003415 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003416 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01003417 : HExpression(kIf, SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003418 SetRawInputAt(0, input);
3419 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003420
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003421 bool IsClonable() const override { return true; }
3422 bool IsControlFlow() const override { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003423
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003424 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01003425 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003426 }
3427
3428 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01003429 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003430 }
3431
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003432 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003433
Artem Serovcced8ba2017-07-19 18:18:09 +01003434 protected:
3435 DEFAULT_COPY_CONSTRUCTOR(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003436};
3437
David Brazdilfc6a86a2015-06-26 10:33:45 +00003438
3439// Abstract instruction which marks the beginning and/or end of a try block and
3440// links it to the respective exception handlers. Behaves the same as a Goto in
3441// non-exceptional control flow.
3442// Normal-flow successor is stored at index zero, exception handlers under
3443// higher indices in no particular order.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003444class HTryBoundary final : public HExpression<0> {
David Brazdilfc6a86a2015-06-26 10:33:45 +00003445 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003446 enum class BoundaryKind {
David Brazdil56e1acc2015-06-30 15:41:36 +01003447 kEntry,
3448 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00003449 kLast = kExit
David Brazdil56e1acc2015-06-30 15:41:36 +01003450 };
3451
Artem Serovd1aa7d02018-06-22 11:35:46 +01003452 // SideEffects::CanTriggerGC prevents instructions with SideEffects::DependOnGC to be alive
3453 // across the catch block entering edges as GC might happen during throwing an exception.
3454 // TryBoundary with BoundaryKind::kExit is conservatively used for that as there is no
3455 // HInstruction which a catch block must start from.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003456 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
Artem Serovd1aa7d02018-06-22 11:35:46 +01003457 : HExpression(kTryBoundary,
3458 (kind == BoundaryKind::kExit) ? SideEffects::CanTriggerGC()
3459 : SideEffects::None(),
3460 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003461 SetPackedField<BoundaryKindField>(kind);
3462 }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003463
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003464 bool IsControlFlow() const override { return true; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003465
3466 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01003467 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003468
David Brazdild26a4112015-11-10 11:07:31 +00003469 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
3470 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
3471 }
3472
David Brazdilfc6a86a2015-06-26 10:33:45 +00003473 // Returns whether `handler` is among its exception handlers (non-zero index
3474 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01003475 bool HasExceptionHandler(const HBasicBlock& handler) const {
3476 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00003477 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00003478 }
3479
3480 // If not present already, adds `handler` to its block's list of exception
3481 // handlers.
3482 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01003483 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00003484 GetBlock()->AddSuccessor(handler);
3485 }
3486 }
3487
Vladimir Markoa1de9182016-02-25 11:37:38 +00003488 BoundaryKind GetBoundaryKind() const { return GetPackedField<BoundaryKindField>(); }
3489 bool IsEntry() const { return GetBoundaryKind() == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003490
David Brazdilffee3d32015-07-06 11:48:53 +01003491 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
3492
David Brazdilfc6a86a2015-06-26 10:33:45 +00003493 DECLARE_INSTRUCTION(TryBoundary);
3494
Artem Serovcced8ba2017-07-19 18:18:09 +01003495 protected:
3496 DEFAULT_COPY_CONSTRUCTOR(TryBoundary);
3497
David Brazdilfc6a86a2015-06-26 10:33:45 +00003498 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003499 static constexpr size_t kFieldBoundaryKind = kNumberOfGenericPackedBits;
3500 static constexpr size_t kFieldBoundaryKindSize =
3501 MinimumBitsToStore(static_cast<size_t>(BoundaryKind::kLast));
3502 static constexpr size_t kNumberOfTryBoundaryPackedBits =
3503 kFieldBoundaryKind + kFieldBoundaryKindSize;
3504 static_assert(kNumberOfTryBoundaryPackedBits <= kMaxNumberOfPackedBits,
3505 "Too many packed fields.");
3506 using BoundaryKindField = BitField<BoundaryKind, kFieldBoundaryKind, kFieldBoundaryKindSize>;
David Brazdilfc6a86a2015-06-26 10:33:45 +00003507};
3508
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003509// Deoptimize to interpreter, upon checking a condition.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003510class HDeoptimize final : public HVariableInputSizeInstruction {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003511 public:
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003512 // Use this constructor when the `HDeoptimize` acts as a barrier, where no code can move
3513 // across.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003514 HDeoptimize(ArenaAllocator* allocator,
3515 HInstruction* cond,
3516 DeoptimizationKind kind,
3517 uint32_t dex_pc)
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003518 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303519 kDeoptimize,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003520 SideEffects::All(),
3521 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003522 allocator,
Andreas Gampe3db70682018-12-26 15:12:03 -08003523 /* number_of_inputs= */ 1,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003524 kArenaAllocMisc) {
3525 SetPackedFlag<kFieldCanBeMoved>(false);
3526 SetPackedField<DeoptimizeKindField>(kind);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003527 SetRawInputAt(0, cond);
3528 }
3529
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003530 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01003531
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003532 // Use this constructor when the `HDeoptimize` guards an instruction, and any user
3533 // that relies on the deoptimization to pass should have its input be the `HDeoptimize`
3534 // instead of `guard`.
3535 // We set CanTriggerGC to prevent any intermediate address to be live
3536 // at the point of the `HDeoptimize`.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003537 HDeoptimize(ArenaAllocator* allocator,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003538 HInstruction* cond,
3539 HInstruction* guard,
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003540 DeoptimizationKind kind,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003541 uint32_t dex_pc)
3542 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303543 kDeoptimize,
Vladimir Markobd785672018-05-03 17:09:09 +01003544 guard->GetType(),
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003545 SideEffects::CanTriggerGC(),
3546 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003547 allocator,
Andreas Gampe3db70682018-12-26 15:12:03 -08003548 /* number_of_inputs= */ 2,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003549 kArenaAllocMisc) {
3550 SetPackedFlag<kFieldCanBeMoved>(true);
3551 SetPackedField<DeoptimizeKindField>(kind);
3552 SetRawInputAt(0, cond);
3553 SetRawInputAt(1, guard);
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01003554 }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003555
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003556 bool CanBeMoved() const override { return GetPackedFlag<kFieldCanBeMoved>(); }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003557
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003558 bool InstructionDataEquals(const HInstruction* other) const override {
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003559 return (other->CanBeMoved() == CanBeMoved()) && (other->AsDeoptimize()->GetKind() == GetKind());
3560 }
3561
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003562 bool NeedsEnvironment() const override { return true; }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003563
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003564 bool CanThrow() const override { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003565
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003566 DeoptimizationKind GetDeoptimizationKind() const { return GetPackedField<DeoptimizeKindField>(); }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003567
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003568 bool GuardsAnInput() const {
3569 return InputCount() == 2;
3570 }
3571
3572 HInstruction* GuardedInput() const {
3573 DCHECK(GuardsAnInput());
3574 return InputAt(1);
3575 }
3576
3577 void RemoveGuard() {
3578 RemoveInputAt(1);
3579 }
3580
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003581 DECLARE_INSTRUCTION(Deoptimize);
3582
Artem Serovcced8ba2017-07-19 18:18:09 +01003583 protected:
3584 DEFAULT_COPY_CONSTRUCTOR(Deoptimize);
3585
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003586 private:
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003587 static constexpr size_t kFieldCanBeMoved = kNumberOfGenericPackedBits;
3588 static constexpr size_t kFieldDeoptimizeKind = kNumberOfGenericPackedBits + 1;
3589 static constexpr size_t kFieldDeoptimizeKindSize =
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003590 MinimumBitsToStore(static_cast<size_t>(DeoptimizationKind::kLast));
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003591 static constexpr size_t kNumberOfDeoptimizePackedBits =
3592 kFieldDeoptimizeKind + kFieldDeoptimizeKindSize;
3593 static_assert(kNumberOfDeoptimizePackedBits <= kMaxNumberOfPackedBits,
3594 "Too many packed fields.");
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003595 using DeoptimizeKindField =
3596 BitField<DeoptimizationKind, kFieldDeoptimizeKind, kFieldDeoptimizeKindSize>;
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003597};
3598
Mingyao Yang063fc772016-08-02 11:02:54 -07003599// Represents a should_deoptimize flag. Currently used for CHA-based devirtualization.
3600// The compiled code checks this flag value in a guard before devirtualized call and
3601// if it's true, starts to do deoptimization.
3602// It has a 4-byte slot on stack.
3603// TODO: allocate a register for this flag.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003604class HShouldDeoptimizeFlag final : public HVariableInputSizeInstruction {
Mingyao Yang063fc772016-08-02 11:02:54 -07003605 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003606 // CHA guards are only optimized in a separate pass and it has no side effects
3607 // with regard to other passes.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003608 HShouldDeoptimizeFlag(ArenaAllocator* allocator, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303609 : HVariableInputSizeInstruction(kShouldDeoptimizeFlag,
Vladimir Markobd785672018-05-03 17:09:09 +01003610 DataType::Type::kInt32,
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303611 SideEffects::None(),
3612 dex_pc,
3613 allocator,
3614 0,
3615 kArenaAllocCHA) {
Mingyao Yang063fc772016-08-02 11:02:54 -07003616 }
3617
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003618 // We do all CHA guard elimination/motion in a single pass, after which there is no
3619 // further guard elimination/motion since a guard might have been used for justification
3620 // of the elimination of another guard. Therefore, we pretend this guard cannot be moved
3621 // to avoid other optimizations trying to move it.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003622 bool CanBeMoved() const override { return false; }
Mingyao Yang063fc772016-08-02 11:02:54 -07003623
3624 DECLARE_INSTRUCTION(ShouldDeoptimizeFlag);
3625
Artem Serovcced8ba2017-07-19 18:18:09 +01003626 protected:
3627 DEFAULT_COPY_CONSTRUCTOR(ShouldDeoptimizeFlag);
Mingyao Yang063fc772016-08-02 11:02:54 -07003628};
3629
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003630// Represents the ArtMethod that was passed as a first argument to
3631// the method. It is used by instructions that depend on it, like
3632// instructions that work with the dex cache.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003633class HCurrentMethod final : public HExpression<0> {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003634 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003635 explicit HCurrentMethod(DataType::Type type, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303636 : HExpression(kCurrentMethod, type, SideEffects::None(), dex_pc) {
3637 }
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003638
3639 DECLARE_INSTRUCTION(CurrentMethod);
3640
Artem Serovcced8ba2017-07-19 18:18:09 +01003641 protected:
3642 DEFAULT_COPY_CONSTRUCTOR(CurrentMethod);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003643};
3644
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003645// Fetches an ArtMethod from the virtual table or the interface method table
3646// of a class.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003647class HClassTableGet final : public HExpression<1> {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003648 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003649 enum class TableKind {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003650 kVTable,
3651 kIMTable,
Vladimir Markoa1de9182016-02-25 11:37:38 +00003652 kLast = kIMTable
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003653 };
3654 HClassTableGet(HInstruction* cls,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003655 DataType::Type type,
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003656 TableKind kind,
3657 size_t index,
3658 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303659 : HExpression(kClassTableGet, type, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003660 index_(index) {
3661 SetPackedField<TableKindField>(kind);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003662 SetRawInputAt(0, cls);
3663 }
3664
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003665 bool IsClonable() const override { return true; }
3666 bool CanBeMoved() const override { return true; }
3667 bool InstructionDataEquals(const HInstruction* other) const override {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003668 return other->AsClassTableGet()->GetIndex() == index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00003669 other->AsClassTableGet()->GetPackedFields() == GetPackedFields();
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003670 }
3671
Vladimir Markoa1de9182016-02-25 11:37:38 +00003672 TableKind GetTableKind() const { return GetPackedField<TableKindField>(); }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003673 size_t GetIndex() const { return index_; }
3674
3675 DECLARE_INSTRUCTION(ClassTableGet);
3676
Artem Serovcced8ba2017-07-19 18:18:09 +01003677 protected:
3678 DEFAULT_COPY_CONSTRUCTOR(ClassTableGet);
3679
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003680 private:
Vladimir Markobd785672018-05-03 17:09:09 +01003681 static constexpr size_t kFieldTableKind = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003682 static constexpr size_t kFieldTableKindSize =
3683 MinimumBitsToStore(static_cast<size_t>(TableKind::kLast));
3684 static constexpr size_t kNumberOfClassTableGetPackedBits = kFieldTableKind + kFieldTableKindSize;
3685 static_assert(kNumberOfClassTableGetPackedBits <= kMaxNumberOfPackedBits,
3686 "Too many packed fields.");
3687 using TableKindField = BitField<TableKind, kFieldTableKind, kFieldTableKind>;
3688
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003689 // The index of the ArtMethod in the table.
3690 const size_t index_;
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003691};
3692
Mark Mendellfe57faa2015-09-18 09:26:15 -04003693// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
3694// have one successor for each entry in the switch table, and the final successor
3695// will be the block containing the next Dex opcode.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003696class HPackedSwitch final : public HExpression<1> {
Mark Mendellfe57faa2015-09-18 09:26:15 -04003697 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003698 HPackedSwitch(int32_t start_value,
3699 uint32_t num_entries,
3700 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04003701 uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01003702 : HExpression(kPackedSwitch, SideEffects::None(), dex_pc),
Mark Mendellfe57faa2015-09-18 09:26:15 -04003703 start_value_(start_value),
3704 num_entries_(num_entries) {
3705 SetRawInputAt(0, input);
3706 }
3707
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003708 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01003709
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003710 bool IsControlFlow() const override { return true; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04003711
3712 int32_t GetStartValue() const { return start_value_; }
3713
Vladimir Marko211c2112015-09-24 16:52:33 +01003714 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04003715
3716 HBasicBlock* GetDefaultBlock() const {
3717 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01003718 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04003719 }
3720 DECLARE_INSTRUCTION(PackedSwitch);
3721
Artem Serovcced8ba2017-07-19 18:18:09 +01003722 protected:
3723 DEFAULT_COPY_CONSTRUCTOR(PackedSwitch);
3724
Mark Mendellfe57faa2015-09-18 09:26:15 -04003725 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003726 const int32_t start_value_;
3727 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04003728};
3729
Roland Levillain88cb1752014-10-20 16:36:47 +01003730class HUnaryOperation : public HExpression<1> {
3731 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303732 HUnaryOperation(InstructionKind kind,
3733 DataType::Type result_type,
3734 HInstruction* input,
3735 uint32_t dex_pc = kNoDexPc)
3736 : HExpression(kind, result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01003737 SetRawInputAt(0, input);
3738 }
3739
Artem Serovcced8ba2017-07-19 18:18:09 +01003740 // All of the UnaryOperation instructions are clonable.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003741 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01003742
Roland Levillain88cb1752014-10-20 16:36:47 +01003743 HInstruction* GetInput() const { return InputAt(0); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003744 DataType::Type GetResultType() const { return GetType(); }
Roland Levillain88cb1752014-10-20 16:36:47 +01003745
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003746 bool CanBeMoved() const override { return true; }
3747 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003748 return true;
3749 }
Roland Levillain88cb1752014-10-20 16:36:47 +01003750
Roland Levillain31dd3d62016-02-16 12:21:02 +00003751 // Try to statically evaluate `this` and return a HConstant
3752 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003753 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003754 HConstant* TryStaticEvaluation() const;
3755
3756 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003757 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
3758 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003759 virtual HConstant* Evaluate(HFloatConstant* x) const = 0;
3760 virtual HConstant* Evaluate(HDoubleConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01003761
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003762 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01003763
Artem Serovcced8ba2017-07-19 18:18:09 +01003764 protected:
3765 DEFAULT_COPY_CONSTRUCTOR(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01003766};
3767
Dave Allison20dfc792014-06-16 20:44:29 -07003768class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003769 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303770 HBinaryOperation(InstructionKind kind,
3771 DataType::Type result_type,
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003772 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003773 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003774 SideEffects side_effects = SideEffects::None(),
3775 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303776 : HExpression(kind, result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003777 SetRawInputAt(0, left);
3778 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003779 }
3780
Artem Serovcced8ba2017-07-19 18:18:09 +01003781 // All of the BinaryOperation instructions are clonable.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003782 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01003783
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003784 HInstruction* GetLeft() const { return InputAt(0); }
3785 HInstruction* GetRight() const { return InputAt(1); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003786 DataType::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003787
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003788 virtual bool IsCommutative() const { return false; }
3789
3790 // Put constant on the right.
3791 // Returns whether order is changed.
3792 bool OrderInputsWithConstantOnTheRight() {
3793 HInstruction* left = InputAt(0);
3794 HInstruction* right = InputAt(1);
3795 if (left->IsConstant() && !right->IsConstant()) {
3796 ReplaceInput(right, 0);
3797 ReplaceInput(left, 1);
3798 return true;
3799 }
3800 return false;
3801 }
3802
3803 // Order inputs by instruction id, but favor constant on the right side.
3804 // This helps GVN for commutative ops.
3805 void OrderInputs() {
3806 DCHECK(IsCommutative());
3807 HInstruction* left = InputAt(0);
3808 HInstruction* right = InputAt(1);
3809 if (left == right || (!left->IsConstant() && right->IsConstant())) {
3810 return;
3811 }
3812 if (OrderInputsWithConstantOnTheRight()) {
3813 return;
3814 }
3815 // Order according to instruction id.
3816 if (left->GetId() > right->GetId()) {
3817 ReplaceInput(right, 0);
3818 ReplaceInput(left, 1);
3819 }
3820 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003821
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003822 bool CanBeMoved() const override { return true; }
3823 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003824 return true;
3825 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003826
Roland Levillain31dd3d62016-02-16 12:21:02 +00003827 // Try to statically evaluate `this` and return a HConstant
3828 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003829 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003830 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01003831
3832 // Apply this operation to `x` and `y`.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003833 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3834 HNullConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003835 LOG(FATAL) << DebugName() << " is not defined for the (null, null) case.";
3836 UNREACHABLE();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003837 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003838 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
3839 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
Roland Levillain9867bc72015-08-05 10:21:34 +01003840 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
3841 HIntConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003842 LOG(FATAL) << DebugName() << " is not defined for the (long, int) case.";
3843 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01003844 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003845 virtual HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const = 0;
3846 virtual HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const = 0;
Roland Levillain556c3d12014-09-18 15:25:07 +01003847
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003848 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003849 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003850 HConstant* GetConstantRight() const;
3851
3852 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003853 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003854 HInstruction* GetLeastConstantLeft() const;
3855
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003856 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01003857
Artem Serovcced8ba2017-07-19 18:18:09 +01003858 protected:
3859 DEFAULT_COPY_CONSTRUCTOR(BinaryOperation);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003860};
3861
Mark Mendellc4701932015-04-10 13:18:51 -04003862// The comparison bias applies for floating point operations and indicates how NaN
3863// comparisons are treated:
Vladimir Marko9974e3c2020-06-10 16:27:06 +01003864enum class ComparisonBias { // private marker to avoid generate-operator-out.py from processing.
Mark Mendellc4701932015-04-10 13:18:51 -04003865 kNoBias, // bias is not applicable (i.e. for long operation)
3866 kGtBias, // return 1 for NaN comparisons
3867 kLtBias, // return -1 for NaN comparisons
Vladimir Markoa1de9182016-02-25 11:37:38 +00003868 kLast = kLtBias
Mark Mendellc4701932015-04-10 13:18:51 -04003869};
3870
Vladimir Marko9974e3c2020-06-10 16:27:06 +01003871std::ostream& operator<<(std::ostream& os, ComparisonBias rhs);
Roland Levillain31dd3d62016-02-16 12:21:02 +00003872
Dave Allison20dfc792014-06-16 20:44:29 -07003873class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003874 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303875 HCondition(InstructionKind kind,
3876 HInstruction* first,
3877 HInstruction* second,
3878 uint32_t dex_pc = kNoDexPc)
3879 : HBinaryOperation(kind,
3880 DataType::Type::kBool,
3881 first,
3882 second,
3883 SideEffects::None(),
3884 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003885 SetPackedField<ComparisonBiasField>(ComparisonBias::kNoBias);
3886 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003887
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003888 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003889 // `instruction`, and disregard moves in between.
3890 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003891
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003892 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003893
3894 virtual IfCondition GetCondition() const = 0;
3895
Mark Mendellc4701932015-04-10 13:18:51 -04003896 virtual IfCondition GetOppositeCondition() const = 0;
3897
Vladimir Markoa1de9182016-02-25 11:37:38 +00003898 bool IsGtBias() const { return GetBias() == ComparisonBias::kGtBias; }
Anton Shaminbdd79352016-02-15 12:48:36 +06003899 bool IsLtBias() const { return GetBias() == ComparisonBias::kLtBias; }
3900
Vladimir Markoa1de9182016-02-25 11:37:38 +00003901 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
3902 void SetBias(ComparisonBias bias) { SetPackedField<ComparisonBiasField>(bias); }
Mark Mendellc4701932015-04-10 13:18:51 -04003903
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003904 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003905 return GetPackedFields() == other->AsCondition()->GetPackedFields();
Mark Mendellc4701932015-04-10 13:18:51 -04003906 }
3907
Roland Levillain4fa13f62015-07-06 18:11:54 +01003908 bool IsFPConditionTrueIfNaN() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003909 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003910 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003911 if (if_cond == kCondNE) {
3912 return true;
3913 } else if (if_cond == kCondEQ) {
3914 return false;
3915 }
3916 return ((if_cond == kCondGT) || (if_cond == kCondGE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003917 }
3918
3919 bool IsFPConditionFalseIfNaN() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003920 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003921 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003922 if (if_cond == kCondEQ) {
3923 return true;
3924 } else if (if_cond == kCondNE) {
3925 return false;
3926 }
3927 return ((if_cond == kCondLT) || (if_cond == kCondLE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003928 }
3929
Roland Levillain31dd3d62016-02-16 12:21:02 +00003930 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003931 // Needed if we merge a HCompare into a HCondition.
Vladimir Markobd785672018-05-03 17:09:09 +01003932 static constexpr size_t kFieldComparisonBias = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003933 static constexpr size_t kFieldComparisonBiasSize =
3934 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3935 static constexpr size_t kNumberOfConditionPackedBits =
3936 kFieldComparisonBias + kFieldComparisonBiasSize;
3937 static_assert(kNumberOfConditionPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3938 using ComparisonBiasField =
3939 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3940
Roland Levillain31dd3d62016-02-16 12:21:02 +00003941 template <typename T>
3942 int32_t Compare(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3943
3944 template <typename T>
3945 int32_t CompareFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003946 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003947 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3948 // Handle the bias.
3949 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compare(x, y);
3950 }
3951
3952 // Return an integer constant containing the result of a condition evaluated at compile time.
3953 HIntConstant* MakeConstantCondition(bool value, uint32_t dex_pc) const {
3954 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3955 }
3956
Artem Serovcced8ba2017-07-19 18:18:09 +01003957 DEFAULT_COPY_CONSTRUCTOR(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003958};
3959
3960// Instruction to check if two inputs are equal to each other.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003961class HEqual final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003962 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003963 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303964 : HCondition(kEqual, first, second, dex_pc) {
3965 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003966
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003967 bool IsCommutative() const override { return true; }
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003968
Vladimir Marko9e23df52015-11-10 17:14:35 +00003969 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003970 HNullConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003971 return MakeConstantCondition(true, GetDexPc());
3972 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003973 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003974 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3975 }
3976 // In the following Evaluate methods, a HCompare instruction has
3977 // been merged into this HEqual instruction; evaluate it as
3978 // `Compare(x, y) == 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003979 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003980 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0),
3981 GetDexPc());
3982 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003983 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003984 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3985 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003986 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003987 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003988 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003989
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003990 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003991
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003992 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07003993 return kCondEQ;
3994 }
3995
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003996 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04003997 return kCondNE;
3998 }
3999
Artem Serovcced8ba2017-07-19 18:18:09 +01004000 protected:
4001 DEFAULT_COPY_CONSTRUCTOR(Equal);
4002
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00004003 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004004 template <typename T> static bool Compute(T x, T y) { return x == y; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00004005};
4006
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004007class HNotEqual final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07004008 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304009 HNotEqual(HInstruction* first, HInstruction* second,
4010 uint32_t dex_pc = kNoDexPc)
4011 : HCondition(kNotEqual, first, second, dex_pc) {
4012 }
Dave Allison20dfc792014-06-16 20:44:29 -07004013
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004014 bool IsCommutative() const override { return true; }
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004015
Vladimir Marko9e23df52015-11-10 17:14:35 +00004016 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004017 HNullConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004018 return MakeConstantCondition(false, GetDexPc());
4019 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004020 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004021 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4022 }
4023 // In the following Evaluate methods, a HCompare instruction has
4024 // been merged into this HNotEqual instruction; evaluate it as
4025 // `Compare(x, y) != 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004026 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004027 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
4028 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004029 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004030 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
4031 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004032 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004033 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00004034 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004035
Dave Allison20dfc792014-06-16 20:44:29 -07004036 DECLARE_INSTRUCTION(NotEqual);
4037
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004038 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07004039 return kCondNE;
4040 }
4041
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004042 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04004043 return kCondEQ;
4044 }
4045
Artem Serovcced8ba2017-07-19 18:18:09 +01004046 protected:
4047 DEFAULT_COPY_CONSTRUCTOR(NotEqual);
4048
Dave Allison20dfc792014-06-16 20:44:29 -07004049 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004050 template <typename T> static bool Compute(T x, T y) { return x != y; }
Dave Allison20dfc792014-06-16 20:44:29 -07004051};
4052
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004053class HLessThan final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07004054 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304055 HLessThan(HInstruction* first, HInstruction* second,
4056 uint32_t dex_pc = kNoDexPc)
4057 : HCondition(kLessThan, first, second, dex_pc) {
4058 }
Dave Allison20dfc792014-06-16 20:44:29 -07004059
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004060 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004061 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004062 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004063 // In the following Evaluate methods, a HCompare instruction has
4064 // been merged into this HLessThan instruction; evaluate it as
4065 // `Compare(x, y) < 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004066 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004067 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
4068 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004069 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004070 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
4071 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004072 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004073 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004074 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004075
Dave Allison20dfc792014-06-16 20:44:29 -07004076 DECLARE_INSTRUCTION(LessThan);
4077
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004078 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07004079 return kCondLT;
4080 }
4081
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004082 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04004083 return kCondGE;
4084 }
4085
Artem Serovcced8ba2017-07-19 18:18:09 +01004086 protected:
4087 DEFAULT_COPY_CONSTRUCTOR(LessThan);
4088
Dave Allison20dfc792014-06-16 20:44:29 -07004089 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004090 template <typename T> static bool Compute(T x, T y) { return x < y; }
Dave Allison20dfc792014-06-16 20:44:29 -07004091};
4092
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004093class HLessThanOrEqual final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07004094 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304095 HLessThanOrEqual(HInstruction* first, HInstruction* second,
4096 uint32_t dex_pc = kNoDexPc)
4097 : HCondition(kLessThanOrEqual, first, second, dex_pc) {
4098 }
Dave Allison20dfc792014-06-16 20:44:29 -07004099
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004100 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004101 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004102 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004103 // In the following Evaluate methods, a HCompare instruction has
4104 // been merged into this HLessThanOrEqual instruction; evaluate it as
4105 // `Compare(x, y) <= 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004106 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004107 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
4108 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004109 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004110 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
4111 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004112 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004113 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004114 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004115
Dave Allison20dfc792014-06-16 20:44:29 -07004116 DECLARE_INSTRUCTION(LessThanOrEqual);
4117
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004118 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07004119 return kCondLE;
4120 }
4121
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004122 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04004123 return kCondGT;
4124 }
4125
Artem Serovcced8ba2017-07-19 18:18:09 +01004126 protected:
4127 DEFAULT_COPY_CONSTRUCTOR(LessThanOrEqual);
4128
Dave Allison20dfc792014-06-16 20:44:29 -07004129 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004130 template <typename T> static bool Compute(T x, T y) { return x <= y; }
Dave Allison20dfc792014-06-16 20:44:29 -07004131};
4132
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004133class HGreaterThan final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07004134 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004135 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304136 : HCondition(kGreaterThan, first, second, dex_pc) {
4137 }
Dave Allison20dfc792014-06-16 20:44:29 -07004138
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004139 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004140 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004141 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004142 // In the following Evaluate methods, a HCompare instruction has
4143 // been merged into this HGreaterThan instruction; evaluate it as
4144 // `Compare(x, y) > 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004145 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004146 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
4147 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004148 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004149 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
4150 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004151 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004152 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004153 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004154
Dave Allison20dfc792014-06-16 20:44:29 -07004155 DECLARE_INSTRUCTION(GreaterThan);
4156
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004157 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07004158 return kCondGT;
4159 }
4160
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004161 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04004162 return kCondLE;
4163 }
4164
Artem Serovcced8ba2017-07-19 18:18:09 +01004165 protected:
4166 DEFAULT_COPY_CONSTRUCTOR(GreaterThan);
4167
Dave Allison20dfc792014-06-16 20:44:29 -07004168 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004169 template <typename T> static bool Compute(T x, T y) { return x > y; }
Dave Allison20dfc792014-06-16 20:44:29 -07004170};
4171
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004172class HGreaterThanOrEqual final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07004173 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004174 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304175 : HCondition(kGreaterThanOrEqual, first, second, dex_pc) {
4176 }
Dave Allison20dfc792014-06-16 20:44:29 -07004177
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004178 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004179 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004180 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004181 // In the following Evaluate methods, a HCompare instruction has
4182 // been merged into this HGreaterThanOrEqual instruction; evaluate it as
4183 // `Compare(x, y) >= 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004184 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004185 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
4186 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004187 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004188 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
4189 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004190 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004191 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004192 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004193
Dave Allison20dfc792014-06-16 20:44:29 -07004194 DECLARE_INSTRUCTION(GreaterThanOrEqual);
4195
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004196 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07004197 return kCondGE;
4198 }
4199
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004200 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04004201 return kCondLT;
4202 }
4203
Artem Serovcced8ba2017-07-19 18:18:09 +01004204 protected:
4205 DEFAULT_COPY_CONSTRUCTOR(GreaterThanOrEqual);
4206
Dave Allison20dfc792014-06-16 20:44:29 -07004207 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004208 template <typename T> static bool Compute(T x, T y) { return x >= y; }
Dave Allison20dfc792014-06-16 20:44:29 -07004209};
4210
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004211class HBelow final : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07004212 public:
4213 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304214 : HCondition(kBelow, first, second, dex_pc) {
4215 }
Aart Bike9f37602015-10-09 11:15:55 -07004216
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004217 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004218 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07004219 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004220 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004221 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4222 }
4223 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004224 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004225 LOG(FATAL) << DebugName() << " is not defined for float values";
4226 UNREACHABLE();
4227 }
4228 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004229 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004230 LOG(FATAL) << DebugName() << " is not defined for double values";
4231 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07004232 }
4233
4234 DECLARE_INSTRUCTION(Below);
4235
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004236 IfCondition GetCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004237 return kCondB;
4238 }
4239
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004240 IfCondition GetOppositeCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004241 return kCondAE;
4242 }
4243
Artem Serovcced8ba2017-07-19 18:18:09 +01004244 protected:
4245 DEFAULT_COPY_CONSTRUCTOR(Below);
4246
Aart Bike9f37602015-10-09 11:15:55 -07004247 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004248 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004249 return MakeUnsigned(x) < MakeUnsigned(y);
4250 }
Aart Bike9f37602015-10-09 11:15:55 -07004251};
4252
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004253class HBelowOrEqual final : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07004254 public:
4255 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304256 : HCondition(kBelowOrEqual, first, second, dex_pc) {
4257 }
Aart Bike9f37602015-10-09 11:15:55 -07004258
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004259 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004260 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07004261 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004262 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004263 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4264 }
4265 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004266 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004267 LOG(FATAL) << DebugName() << " is not defined for float values";
4268 UNREACHABLE();
4269 }
4270 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004271 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004272 LOG(FATAL) << DebugName() << " is not defined for double values";
4273 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07004274 }
4275
4276 DECLARE_INSTRUCTION(BelowOrEqual);
4277
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004278 IfCondition GetCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004279 return kCondBE;
4280 }
4281
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004282 IfCondition GetOppositeCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004283 return kCondA;
4284 }
4285
Artem Serovcced8ba2017-07-19 18:18:09 +01004286 protected:
4287 DEFAULT_COPY_CONSTRUCTOR(BelowOrEqual);
4288
Aart Bike9f37602015-10-09 11:15:55 -07004289 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004290 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004291 return MakeUnsigned(x) <= MakeUnsigned(y);
4292 }
Aart Bike9f37602015-10-09 11:15:55 -07004293};
4294
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004295class HAbove final : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07004296 public:
4297 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304298 : HCondition(kAbove, first, second, dex_pc) {
4299 }
Aart Bike9f37602015-10-09 11:15:55 -07004300
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004301 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004302 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07004303 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004304 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004305 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4306 }
4307 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004308 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004309 LOG(FATAL) << DebugName() << " is not defined for float values";
4310 UNREACHABLE();
4311 }
4312 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004313 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004314 LOG(FATAL) << DebugName() << " is not defined for double values";
4315 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07004316 }
4317
4318 DECLARE_INSTRUCTION(Above);
4319
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004320 IfCondition GetCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004321 return kCondA;
4322 }
4323
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004324 IfCondition GetOppositeCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004325 return kCondBE;
4326 }
4327
Artem Serovcced8ba2017-07-19 18:18:09 +01004328 protected:
4329 DEFAULT_COPY_CONSTRUCTOR(Above);
4330
Aart Bike9f37602015-10-09 11:15:55 -07004331 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004332 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004333 return MakeUnsigned(x) > MakeUnsigned(y);
4334 }
Aart Bike9f37602015-10-09 11:15:55 -07004335};
4336
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004337class HAboveOrEqual final : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07004338 public:
4339 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304340 : HCondition(kAboveOrEqual, first, second, dex_pc) {
4341 }
Aart Bike9f37602015-10-09 11:15:55 -07004342
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004343 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004344 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07004345 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004346 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004347 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4348 }
4349 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004350 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004351 LOG(FATAL) << DebugName() << " is not defined for float values";
4352 UNREACHABLE();
4353 }
4354 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004355 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004356 LOG(FATAL) << DebugName() << " is not defined for double values";
4357 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07004358 }
4359
4360 DECLARE_INSTRUCTION(AboveOrEqual);
4361
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004362 IfCondition GetCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004363 return kCondAE;
4364 }
4365
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004366 IfCondition GetOppositeCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004367 return kCondB;
4368 }
4369
Artem Serovcced8ba2017-07-19 18:18:09 +01004370 protected:
4371 DEFAULT_COPY_CONSTRUCTOR(AboveOrEqual);
4372
Aart Bike9f37602015-10-09 11:15:55 -07004373 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004374 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004375 return MakeUnsigned(x) >= MakeUnsigned(y);
4376 }
Aart Bike9f37602015-10-09 11:15:55 -07004377};
Dave Allison20dfc792014-06-16 20:44:29 -07004378
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004379// Instruction to check how two inputs compare to each other.
4380// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004381class HCompare final : public HBinaryOperation {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004382 public:
Roland Levillaina5c4a402016-03-15 15:02:50 +00004383 // Note that `comparison_type` is the type of comparison performed
4384 // between the comparison's inputs, not the type of the instantiated
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004385 // HCompare instruction (which is always DataType::Type::kInt).
4386 HCompare(DataType::Type comparison_type,
Alexey Frunze4dda3372015-06-01 18:31:49 -07004387 HInstruction* first,
4388 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04004389 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07004390 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304391 : HBinaryOperation(kCompare,
4392 DataType::Type::kInt32,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004393 first,
4394 second,
Roland Levillaina5c4a402016-03-15 15:02:50 +00004395 SideEffectsForArchRuntimeCalls(comparison_type),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004396 dex_pc) {
4397 SetPackedField<ComparisonBiasField>(bias);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004398 }
4399
Roland Levillain9867bc72015-08-05 10:21:34 +01004400 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004401 int32_t Compute(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
4402
4403 template <typename T>
4404 int32_t ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004405 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00004406 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
4407 // Handle the bias.
4408 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compute(x, y);
4409 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004410
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004411 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004412 // Note that there is no "cmp-int" Dex instruction so we shouldn't
4413 // reach this code path when processing a freshly built HIR
4414 // graph. However HCompare integer instructions can be synthesized
4415 // by the instruction simplifier to implement IntegerCompare and
4416 // IntegerSignum intrinsics, so we have to handle this case.
4417 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004418 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004419 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004420 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4421 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004422 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004423 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4424 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004425 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004426 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01004427 }
4428
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004429 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004430 return GetPackedFields() == other->AsCompare()->GetPackedFields();
Calin Juravleddb7df22014-11-25 20:56:51 +00004431 }
4432
Vladimir Markoa1de9182016-02-25 11:37:38 +00004433 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
Mark Mendellc4701932015-04-10 13:18:51 -04004434
Roland Levillain31dd3d62016-02-16 12:21:02 +00004435 // Does this compare instruction have a "gt bias" (vs an "lt bias")?
Vladimir Markoa1de9182016-02-25 11:37:38 +00004436 // Only meaningful for floating-point comparisons.
Roland Levillain31dd3d62016-02-16 12:21:02 +00004437 bool IsGtBias() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004438 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004439 return GetBias() == ComparisonBias::kGtBias;
Roland Levillain31dd3d62016-02-16 12:21:02 +00004440 }
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004441
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004442 static SideEffects SideEffectsForArchRuntimeCalls(DataType::Type type ATTRIBUTE_UNUSED) {
Roland Levillain1693a1f2016-03-15 14:57:31 +00004443 // Comparisons do not require a runtime call in any back end.
4444 return SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004445 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004446
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004447 DECLARE_INSTRUCTION(Compare);
4448
Roland Levillain31dd3d62016-02-16 12:21:02 +00004449 protected:
Vladimir Markobd785672018-05-03 17:09:09 +01004450 static constexpr size_t kFieldComparisonBias = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004451 static constexpr size_t kFieldComparisonBiasSize =
4452 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
4453 static constexpr size_t kNumberOfComparePackedBits =
4454 kFieldComparisonBias + kFieldComparisonBiasSize;
4455 static_assert(kNumberOfComparePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
4456 using ComparisonBiasField =
4457 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
4458
Roland Levillain31dd3d62016-02-16 12:21:02 +00004459 // Return an integer constant containing the result of a comparison evaluated at compile time.
4460 HIntConstant* MakeConstantComparison(int32_t value, uint32_t dex_pc) const {
4461 DCHECK(value == -1 || value == 0 || value == 1) << value;
4462 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
4463 }
4464
Artem Serovcced8ba2017-07-19 18:18:09 +01004465 DEFAULT_COPY_CONSTRUCTOR(Compare);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004466};
4467
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004468class HNewInstance final : public HExpression<1> {
David Brazdil6de19382016-01-08 17:37:10 +00004469 public:
4470 HNewInstance(HInstruction* cls,
David Brazdil6de19382016-01-08 17:37:10 +00004471 uint32_t dex_pc,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004472 dex::TypeIndex type_index,
David Brazdil6de19382016-01-08 17:37:10 +00004473 const DexFile& dex_file,
David Brazdil6de19382016-01-08 17:37:10 +00004474 bool finalizable,
4475 QuickEntrypointEnum entrypoint)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304476 : HExpression(kNewInstance,
4477 DataType::Type::kReference,
4478 SideEffects::CanTriggerGC(),
4479 dex_pc),
David Brazdil6de19382016-01-08 17:37:10 +00004480 type_index_(type_index),
4481 dex_file_(dex_file),
David Brazdil6de19382016-01-08 17:37:10 +00004482 entrypoint_(entrypoint) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004483 SetPackedFlag<kFlagFinalizable>(finalizable);
Alex Light3a73ffb2021-01-25 14:11:05 +00004484 SetPackedFlag<kFlagPartialMaterialization>(false);
David Brazdil6de19382016-01-08 17:37:10 +00004485 SetRawInputAt(0, cls);
David Brazdil6de19382016-01-08 17:37:10 +00004486 }
4487
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004488 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004489
Alex Light3a73ffb2021-01-25 14:11:05 +00004490 void SetPartialMaterialization() {
4491 SetPackedFlag<kFlagPartialMaterialization>(true);
4492 }
4493
Andreas Gampea5b09a62016-11-17 15:21:22 -08004494 dex::TypeIndex GetTypeIndex() const { return type_index_; }
David Brazdil6de19382016-01-08 17:37:10 +00004495 const DexFile& GetDexFile() const { return dex_file_; }
4496
4497 // Calls runtime so needs an environment.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004498 bool NeedsEnvironment() const override { return true; }
David Brazdil6de19382016-01-08 17:37:10 +00004499
Mingyao Yang062157f2016-03-02 10:15:36 -08004500 // Can throw errors when out-of-memory or if it's not instantiable/accessible.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004501 bool CanThrow() const override { return true; }
Alex Light3a73ffb2021-01-25 14:11:05 +00004502 bool OnlyThrowsAsyncExceptions() const override {
4503 return !IsFinalizable() && !NeedsChecks();
4504 }
Mingyao Yang062157f2016-03-02 10:15:36 -08004505
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004506 bool NeedsChecks() const {
4507 return entrypoint_ == kQuickAllocObjectWithChecks;
4508 }
David Brazdil6de19382016-01-08 17:37:10 +00004509
Vladimir Markoa1de9182016-02-25 11:37:38 +00004510 bool IsFinalizable() const { return GetPackedFlag<kFlagFinalizable>(); }
David Brazdil6de19382016-01-08 17:37:10 +00004511
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004512 bool CanBeNull() const override { return false; }
David Brazdil6de19382016-01-08 17:37:10 +00004513
Alex Light3a73ffb2021-01-25 14:11:05 +00004514 bool IsPartialMaterialization() const {
4515 return GetPackedFlag<kFlagPartialMaterialization>();
4516 }
4517
David Brazdil6de19382016-01-08 17:37:10 +00004518 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
4519
4520 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
4521 entrypoint_ = entrypoint;
4522 }
4523
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004524 HLoadClass* GetLoadClass() const {
4525 HInstruction* input = InputAt(0);
4526 if (input->IsClinitCheck()) {
4527 input = input->InputAt(0);
4528 }
4529 DCHECK(input->IsLoadClass());
4530 return input->AsLoadClass();
4531 }
4532
David Brazdil6de19382016-01-08 17:37:10 +00004533 bool IsStringAlloc() const;
4534
4535 DECLARE_INSTRUCTION(NewInstance);
4536
Artem Serovcced8ba2017-07-19 18:18:09 +01004537 protected:
4538 DEFAULT_COPY_CONSTRUCTOR(NewInstance);
4539
David Brazdil6de19382016-01-08 17:37:10 +00004540 private:
Vladimir Markobd785672018-05-03 17:09:09 +01004541 static constexpr size_t kFlagFinalizable = kNumberOfGenericPackedBits;
Alex Light3a73ffb2021-01-25 14:11:05 +00004542 static constexpr size_t kFlagPartialMaterialization = kFlagFinalizable + 1;
4543 static constexpr size_t kNumberOfNewInstancePackedBits = kFlagPartialMaterialization + 1;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004544 static_assert(kNumberOfNewInstancePackedBits <= kMaxNumberOfPackedBits,
4545 "Too many packed fields.");
4546
Andreas Gampea5b09a62016-11-17 15:21:22 -08004547 const dex::TypeIndex type_index_;
David Brazdil6de19382016-01-08 17:37:10 +00004548 const DexFile& dex_file_;
David Brazdil6de19382016-01-08 17:37:10 +00004549 QuickEntrypointEnum entrypoint_;
David Brazdil6de19382016-01-08 17:37:10 +00004550};
4551
Nicolas Geoffray8f2eb252020-11-06 13:39:54 +00004552enum IntrinsicNeedsEnvironment {
4553 kNoEnvironment, // Intrinsic does not require an environment.
4554 kNeedsEnvironment // Intrinsic requires an environment.
agicsaki57b81ec2015-08-11 17:39:37 -07004555};
4556
Aart Bik5d75afe2015-12-14 11:57:01 -08004557enum IntrinsicSideEffects {
4558 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
4559 kReadSideEffects, // Intrinsic may read heap memory.
4560 kWriteSideEffects, // Intrinsic may write heap memory.
4561 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
4562};
4563
4564enum IntrinsicExceptions {
4565 kNoThrow, // Intrinsic does not throw any exceptions.
4566 kCanThrow // Intrinsic may throw exceptions.
4567};
4568
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01004569// Determines how to load an ArtMethod*.
4570enum class MethodLoadKind {
4571 // Use a String init ArtMethod* loaded from Thread entrypoints.
4572 kStringInit,
4573
4574 // Use the method's own ArtMethod* loaded by the register allocator.
4575 kRecursive,
4576
4577 // Use PC-relative boot image ArtMethod* address that will be known at link time.
4578 // Used for boot image methods referenced by boot image code.
4579 kBootImageLinkTimePcRelative,
4580
4581 // Load from an entry in the .data.bimg.rel.ro using a PC-relative load.
4582 // Used for app->boot calls with relocatable image.
4583 kBootImageRelRo,
4584
4585 // Load from an entry in the .bss section using a PC-relative load.
4586 // Used for methods outside boot image referenced by AOT-compiled app and boot image code.
4587 kBssEntry,
4588
4589 // Use ArtMethod* at a known address, embed the direct address in the code.
4590 // Used for for JIT-compiled calls.
4591 kJitDirectAddress,
4592
4593 // Make a runtime call to resolve and call the method. This is the last-resort-kind
4594 // used when other kinds are unimplemented on a particular architecture.
4595 kRuntimeCall,
4596};
4597
4598// Determines the location of the code pointer of an invoke.
4599enum class CodePtrLocation {
4600 // Recursive call, use local PC-relative call instruction.
4601 kCallSelf,
4602
4603 // Use native pointer from the Artmethod*.
4604 // Used for @CriticalNative to avoid going through the compiled stub. This call goes through
4605 // a special resolution stub if the class is not initialized or no native code is registered.
4606 kCallCriticalNative,
4607
4608 // Use code pointer from the ArtMethod*.
4609 // Used when we don't know the target code. This is also the last-resort-kind used when
4610 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
4611 kCallArtMethod,
4612};
4613
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01004614static inline bool IsPcRelativeMethodLoadKind(MethodLoadKind load_kind) {
4615 return load_kind == MethodLoadKind::kBootImageLinkTimePcRelative ||
4616 load_kind == MethodLoadKind::kBootImageRelRo ||
4617 load_kind == MethodLoadKind::kBssEntry;
4618}
4619
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004620class HInvoke : public HVariableInputSizeInstruction {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004621 public:
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004622 bool NeedsEnvironment() const override;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004623
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01004624 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004625 SetRawInputAt(index, argument);
4626 }
4627
Roland Levillain3e3d7332015-04-28 11:00:54 +01004628 // Return the number of arguments. This number can be lower than
4629 // the number of inputs returned by InputCount(), as some invoke
4630 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
4631 // inputs at the end of their list of inputs.
4632 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
4633
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004634 InvokeType GetInvokeType() const {
4635 return GetPackedField<InvokeTypeField>();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004636 }
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004637
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01004638 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004639 return intrinsic_;
4640 }
4641
Aart Bik5d75afe2015-12-14 11:57:01 -08004642 void SetIntrinsic(Intrinsics intrinsic,
Nicolas Geoffray8f2eb252020-11-06 13:39:54 +00004643 IntrinsicNeedsEnvironment needs_env,
Aart Bik5d75afe2015-12-14 11:57:01 -08004644 IntrinsicSideEffects side_effects,
4645 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004646
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01004647 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00004648 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01004649 }
4650
Aart Bikff7d89c2016-11-07 08:49:28 -08004651 void SetCanThrow(bool can_throw) { SetPackedFlag<kFlagCanThrow>(can_throw); }
4652
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004653 bool CanThrow() const override { return GetPackedFlag<kFlagCanThrow>(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08004654
Aart Bika8b8e9b2018-01-09 11:01:02 -08004655 void SetAlwaysThrows(bool always_throws) { SetPackedFlag<kFlagAlwaysThrows>(always_throws); }
4656
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004657 bool AlwaysThrows() const override { return GetPackedFlag<kFlagAlwaysThrows>(); }
Aart Bika8b8e9b2018-01-09 11:01:02 -08004658
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004659 bool CanBeMoved() const override { return IsIntrinsic() && !DoesAnyWrite(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08004660
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004661 bool InstructionDataEquals(const HInstruction* other) const override {
Aart Bik5d75afe2015-12-14 11:57:01 -08004662 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
4663 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01004664
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004665 uint32_t* GetIntrinsicOptimizations() {
4666 return &intrinsic_optimizations_;
4667 }
4668
4669 const uint32_t* GetIntrinsicOptimizations() const {
4670 return &intrinsic_optimizations_;
4671 }
4672
4673 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
4674
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004675 ArtMethod* GetResolvedMethod() const { return resolved_method_; }
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01004676 void SetResolvedMethod(ArtMethod* method);
4677
4678 MethodReference GetMethodReference() const { return method_reference_; }
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004679
Nicolas Geoffrayb0f405e2020-09-24 15:10:35 +01004680 const MethodReference GetResolvedMethodReference() const {
4681 return resolved_method_reference_;
4682 }
4683
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004684 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004685
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004686 protected:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004687 static constexpr size_t kFieldInvokeType = kNumberOfGenericPackedBits;
4688 static constexpr size_t kFieldInvokeTypeSize =
Vladimir Markoa1de9182016-02-25 11:37:38 +00004689 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
Vladimir Markobd785672018-05-03 17:09:09 +01004690 static constexpr size_t kFlagCanThrow = kFieldInvokeType + kFieldInvokeTypeSize;
Aart Bika8b8e9b2018-01-09 11:01:02 -08004691 static constexpr size_t kFlagAlwaysThrows = kFlagCanThrow + 1;
4692 static constexpr size_t kNumberOfInvokePackedBits = kFlagAlwaysThrows + 1;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004693 static_assert(kNumberOfInvokePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004694 using InvokeTypeField = BitField<InvokeType, kFieldInvokeType, kFieldInvokeTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004695
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304696 HInvoke(InstructionKind kind,
4697 ArenaAllocator* allocator,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004698 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01004699 uint32_t number_of_other_inputs,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004700 DataType::Type return_type,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004701 uint32_t dex_pc,
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01004702 MethodReference method_reference,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004703 ArtMethod* resolved_method,
Nicolas Geoffrayb0f405e2020-09-24 15:10:35 +01004704 MethodReference resolved_method_reference,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004705 InvokeType invoke_type)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004706 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304707 kind,
Vladimir Markobd785672018-05-03 17:09:09 +01004708 return_type,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004709 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
4710 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004711 allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004712 number_of_arguments + number_of_other_inputs,
4713 kArenaAllocInvokeInputs),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004714 number_of_arguments_(number_of_arguments),
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01004715 method_reference_(method_reference),
Nicolas Geoffrayb0f405e2020-09-24 15:10:35 +01004716 resolved_method_reference_(resolved_method_reference),
agicsaki57b81ec2015-08-11 17:39:37 -07004717 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004718 intrinsic_optimizations_(0) {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004719 SetPackedField<InvokeTypeField>(invoke_type);
Vladimir Markoa1de9182016-02-25 11:37:38 +00004720 SetPackedFlag<kFlagCanThrow>(true);
Nicolas Geoffray76d4bb0f32018-09-21 12:58:45 +01004721 SetResolvedMethod(resolved_method);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004722 }
4723
Artem Serovcced8ba2017-07-19 18:18:09 +01004724 DEFAULT_COPY_CONSTRUCTOR(Invoke);
4725
Roland Levillain3e3d7332015-04-28 11:00:54 +01004726 uint32_t number_of_arguments_;
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00004727 ArtMethod* resolved_method_;
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01004728 const MethodReference method_reference_;
Nicolas Geoffrayb0f405e2020-09-24 15:10:35 +01004729 // Cached values of the resolved method, to avoid needing the mutator lock.
4730 const MethodReference resolved_method_reference_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004731 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004732
4733 // A magic word holding optimizations for intrinsics. See intrinsics.h.
4734 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004735};
4736
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004737class HInvokeUnresolved final : public HInvoke {
Calin Juravle175dc732015-08-25 15:42:32 +01004738 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004739 HInvokeUnresolved(ArenaAllocator* allocator,
Calin Juravle175dc732015-08-25 15:42:32 +01004740 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004741 DataType::Type return_type,
Calin Juravle175dc732015-08-25 15:42:32 +01004742 uint32_t dex_pc,
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01004743 MethodReference method_reference,
Calin Juravle175dc732015-08-25 15:42:32 +01004744 InvokeType invoke_type)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304745 : HInvoke(kInvokeUnresolved,
4746 allocator,
Calin Juravle175dc732015-08-25 15:42:32 +01004747 number_of_arguments,
Andreas Gampe3db70682018-12-26 15:12:03 -08004748 /* number_of_other_inputs= */ 0u,
Calin Juravle175dc732015-08-25 15:42:32 +01004749 return_type,
4750 dex_pc,
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01004751 method_reference,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004752 nullptr,
Nicolas Geoffrayb0f405e2020-09-24 15:10:35 +01004753 MethodReference(nullptr, 0u),
Calin Juravle175dc732015-08-25 15:42:32 +01004754 invoke_type) {
4755 }
4756
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004757 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004758
Calin Juravle175dc732015-08-25 15:42:32 +01004759 DECLARE_INSTRUCTION(InvokeUnresolved);
4760
Artem Serovcced8ba2017-07-19 18:18:09 +01004761 protected:
4762 DEFAULT_COPY_CONSTRUCTOR(InvokeUnresolved);
Calin Juravle175dc732015-08-25 15:42:32 +01004763};
4764
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004765class HInvokePolymorphic final : public HInvoke {
Orion Hodsonac141392017-01-13 11:53:47 +00004766 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004767 HInvokePolymorphic(ArenaAllocator* allocator,
Orion Hodsonac141392017-01-13 11:53:47 +00004768 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004769 DataType::Type return_type,
Orion Hodsonac141392017-01-13 11:53:47 +00004770 uint32_t dex_pc,
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01004771 MethodReference method_reference,
Andra Danciub931b8c2020-07-27 13:16:37 +00004772 // resolved_method is the ArtMethod object corresponding to the polymorphic
4773 // method (e.g. VarHandle.get), resolved using the class linker. It is needed
4774 // to pass intrinsic information to the HInvokePolymorphic node.
Andra Danciu73c31802020-09-01 13:17:05 +00004775 ArtMethod* resolved_method,
Nicolas Geoffrayb0f405e2020-09-24 15:10:35 +01004776 MethodReference resolved_method_reference,
Andra Danciu73c31802020-09-01 13:17:05 +00004777 dex::ProtoIndex proto_idx)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304778 : HInvoke(kInvokePolymorphic,
4779 allocator,
Orion Hodsonac141392017-01-13 11:53:47 +00004780 number_of_arguments,
Andra Danciu49a19f32020-08-27 12:44:25 +00004781 /* number_of_other_inputs= */ 0u,
Orion Hodsonac141392017-01-13 11:53:47 +00004782 return_type,
4783 dex_pc,
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01004784 method_reference,
Andra Danciua0130e82020-07-23 12:34:56 +00004785 resolved_method,
Nicolas Geoffrayb0f405e2020-09-24 15:10:35 +01004786 resolved_method_reference,
Andra Danciu73c31802020-09-01 13:17:05 +00004787 kPolymorphic),
4788 proto_idx_(proto_idx) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304789 }
Orion Hodsonac141392017-01-13 11:53:47 +00004790
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004791 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004792
Andra Danciu73c31802020-09-01 13:17:05 +00004793 dex::ProtoIndex GetProtoIndex() { return proto_idx_; }
4794
Orion Hodsonac141392017-01-13 11:53:47 +00004795 DECLARE_INSTRUCTION(InvokePolymorphic);
4796
Artem Serovcced8ba2017-07-19 18:18:09 +01004797 protected:
Andra Danciu73c31802020-09-01 13:17:05 +00004798 dex::ProtoIndex proto_idx_;
Artem Serovcced8ba2017-07-19 18:18:09 +01004799 DEFAULT_COPY_CONSTRUCTOR(InvokePolymorphic);
Orion Hodsonac141392017-01-13 11:53:47 +00004800};
4801
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004802class HInvokeCustom final : public HInvoke {
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004803 public:
4804 HInvokeCustom(ArenaAllocator* allocator,
4805 uint32_t number_of_arguments,
4806 uint32_t call_site_index,
4807 DataType::Type return_type,
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01004808 uint32_t dex_pc,
4809 MethodReference method_reference)
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004810 : HInvoke(kInvokeCustom,
4811 allocator,
4812 number_of_arguments,
Andreas Gampe3db70682018-12-26 15:12:03 -08004813 /* number_of_other_inputs= */ 0u,
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004814 return_type,
4815 dex_pc,
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01004816 method_reference,
Andreas Gampe3db70682018-12-26 15:12:03 -08004817 /* resolved_method= */ nullptr,
Nicolas Geoffrayb0f405e2020-09-24 15:10:35 +01004818 MethodReference(nullptr, 0u),
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004819 kStatic),
4820 call_site_index_(call_site_index) {
4821 }
4822
4823 uint32_t GetCallSiteIndex() const { return call_site_index_; }
4824
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004825 bool IsClonable() const override { return true; }
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004826
4827 DECLARE_INSTRUCTION(InvokeCustom);
4828
4829 protected:
4830 DEFAULT_COPY_CONSTRUCTOR(InvokeCustom);
4831
4832 private:
4833 uint32_t call_site_index_;
4834};
4835
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004836class HInvokeStaticOrDirect final : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004837 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01004838 // Requirements of this method call regarding the class
4839 // initialization (clinit) check of its declaring class.
Vladimir Marko9974e3c2020-06-10 16:27:06 +01004840 enum class ClinitCheckRequirement { // private marker to avoid generate-operator-out.py from processing.
Roland Levillain4c0eb422015-04-24 16:43:49 +01004841 kNone, // Class already initialized.
4842 kExplicit, // Static call having explicit clinit check as last input.
4843 kImplicit, // Static call implicitly requiring a clinit check.
Vladimir Markoa1de9182016-02-25 11:37:38 +00004844 kLast = kImplicit
Roland Levillain4c0eb422015-04-24 16:43:49 +01004845 };
4846
Vladimir Marko58155012015-08-19 12:49:41 +00004847 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01004848 MethodLoadKind method_load_kind;
4849 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00004850 // The method load data holds
4851 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
4852 // Note that there are multiple string init methods, each having its own offset.
4853 // - the method address for kDirectAddress
Vladimir Markodc151b22015-10-15 18:02:30 +01004854 uint64_t method_load_data;
Vladimir Marko58155012015-08-19 12:49:41 +00004855 };
4856
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004857 HInvokeStaticOrDirect(ArenaAllocator* allocator,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004858 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004859 DataType::Type return_type,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004860 uint32_t dex_pc,
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01004861 MethodReference method_reference,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004862 ArtMethod* resolved_method,
Vladimir Marko58155012015-08-19 12:49:41 +00004863 DispatchInfo dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004864 InvokeType invoke_type,
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01004865 MethodReference resolved_method_reference,
Roland Levillain4c0eb422015-04-24 16:43:49 +01004866 ClinitCheckRequirement clinit_check_requirement)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304867 : HInvoke(kInvokeStaticOrDirect,
4868 allocator,
Roland Levillain3e3d7332015-04-28 11:00:54 +01004869 number_of_arguments,
Vladimir Marko86c87522020-05-11 16:55:55 +01004870 // There is potentially one extra argument for the HCurrentMethod input,
4871 // and one other if the clinit check is explicit. These can be removed later.
4872 (NeedsCurrentMethodInput(dispatch_info) ? 1u : 0u) +
David Brazdildee58d62016-04-07 09:54:26 +00004873 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004874 return_type,
4875 dex_pc,
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01004876 method_reference,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004877 resolved_method,
Nicolas Geoffrayb0f405e2020-09-24 15:10:35 +01004878 resolved_method_reference,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004879 invoke_type),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004880 dispatch_info_(dispatch_info) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004881 SetPackedField<ClinitCheckRequirementField>(clinit_check_requirement);
4882 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004883
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004884 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004885
Vladimir Marko86c87522020-05-11 16:55:55 +01004886 void SetDispatchInfo(DispatchInfo dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00004887 bool had_current_method_input = HasCurrentMethodInput();
Vladimir Marko86c87522020-05-11 16:55:55 +01004888 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info);
Vladimir Markob554b5a2015-11-06 12:57:55 +00004889
4890 // Using the current method is the default and once we find a better
4891 // method load kind, we should not go back to using the current method.
4892 DCHECK(had_current_method_input || !needs_current_method_input);
4893
4894 if (had_current_method_input && !needs_current_method_input) {
Vladimir Marko86c87522020-05-11 16:55:55 +01004895 DCHECK_EQ(InputAt(GetCurrentMethodIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
4896 RemoveInputAt(GetCurrentMethodIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004897 }
Vladimir Markodc151b22015-10-15 18:02:30 +01004898 dispatch_info_ = dispatch_info;
4899 }
4900
Aart Bik6daebeb2017-04-03 14:35:41 -07004901 DispatchInfo GetDispatchInfo() const {
4902 return dispatch_info_;
4903 }
4904
Vladimir Marko372f10e2016-05-17 16:30:10 +01004905 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004906 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01004907 ArrayRef<HUserRecord<HInstruction*>> input_records = HInvoke::GetInputRecords();
4908 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck()) {
4909 DCHECK(!input_records.empty());
4910 DCHECK_GT(input_records.size(), GetNumberOfArguments());
4911 HInstruction* last_input = input_records.back().GetInstruction();
4912 // Note: `last_input` may be null during arguments setup.
4913 if (last_input != nullptr) {
4914 // `last_input` is the last input of a static invoke marked as having
4915 // an explicit clinit check. It must either be:
4916 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
4917 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
4918 DCHECK(last_input->IsClinitCheck() || last_input->IsLoadClass()) << last_input->DebugName();
4919 }
4920 }
4921 return input_records;
4922 }
4923
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004924 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const override {
Vladimir Marko86c87522020-05-11 16:55:55 +01004925 // We do not access the method via object reference, so we cannot do an implicit null check.
Calin Juravle77520bc2015-01-12 18:45:46 +00004926 // TODO: for intrinsics we can generate implicit null checks.
4927 return false;
4928 }
4929
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004930 bool CanBeNull() const override {
Vladimir Markobd785672018-05-03 17:09:09 +01004931 return GetType() == DataType::Type::kReference && !IsStringInit();
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004932 }
4933
Vladimir Marko58155012015-08-19 12:49:41 +00004934 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
Nicolas Geoffray282795c2021-09-24 18:16:41 +01004935 CodePtrLocation GetCodePtrLocation() const {
4936 // We do CHA analysis after sharpening. When a method has CHA inlining, it
4937 // cannot call itself, as if the CHA optmization is invalid we want to make
4938 // sure the method is never executed again. So, while sharpening can return
4939 // kCallSelf, we bypass it here if there is a CHA optimization.
4940 if (dispatch_info_.code_ptr_location == CodePtrLocation::kCallSelf &&
4941 GetBlock()->GetGraph()->HasShouldDeoptimizeFlag()) {
4942 return CodePtrLocation::kCallArtMethod;
4943 } else {
4944 return dispatch_info_.code_ptr_location;
4945 }
4946 }
Vladimir Marko58155012015-08-19 12:49:41 +00004947 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Marko58155012015-08-19 12:49:41 +00004948 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004949 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kJitDirectAddress; }
Vladimir Marko65979462017-05-19 17:25:12 +01004950 bool HasPcRelativeMethodLoadKind() const {
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01004951 return IsPcRelativeMethodLoadKind(GetMethodLoadKind());
Vladimir Marko65979462017-05-19 17:25:12 +01004952 }
Vladimir Marko58155012015-08-19 12:49:41 +00004953
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004954 QuickEntrypointEnum GetStringInitEntryPoint() const {
Vladimir Marko58155012015-08-19 12:49:41 +00004955 DCHECK(IsStringInit());
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004956 return static_cast<QuickEntrypointEnum>(dispatch_info_.method_load_data);
Vladimir Marko58155012015-08-19 12:49:41 +00004957 }
4958
4959 uint64_t GetMethodAddress() const {
4960 DCHECK(HasMethodAddress());
4961 return dispatch_info_.method_load_data;
4962 }
4963
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00004964 const DexFile& GetDexFileForPcRelativeDexCache() const;
4965
Vladimir Markoa1de9182016-02-25 11:37:38 +00004966 ClinitCheckRequirement GetClinitCheckRequirement() const {
4967 return GetPackedField<ClinitCheckRequirementField>();
4968 }
Calin Juravle68ad6492015-08-18 17:08:12 +01004969
Roland Levillain4c0eb422015-04-24 16:43:49 +01004970 // Is this instruction a call to a static method?
4971 bool IsStatic() const {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004972 return GetInvokeType() == kStatic;
4973 }
4974
Vladimir Marko86c87522020-05-11 16:55:55 +01004975 // Does this method load kind need the current method as an input?
4976 static bool NeedsCurrentMethodInput(DispatchInfo dispatch_info) {
4977 return dispatch_info.method_load_kind == MethodLoadKind::kRecursive ||
4978 dispatch_info.method_load_kind == MethodLoadKind::kRuntimeCall ||
4979 dispatch_info.code_ptr_location == CodePtrLocation::kCallCriticalNative;
4980 }
4981
4982 // Get the index of the current method input.
4983 size_t GetCurrentMethodIndex() const {
4984 DCHECK(HasCurrentMethodInput());
4985 return GetCurrentMethodIndexUnchecked();
4986 }
4987 size_t GetCurrentMethodIndexUnchecked() const {
4988 return GetNumberOfArguments();
4989 }
4990
4991 // Check if the method has a current method input.
4992 bool HasCurrentMethodInput() const {
4993 if (NeedsCurrentMethodInput(GetDispatchInfo())) {
4994 DCHECK(InputAt(GetCurrentMethodIndexUnchecked()) == nullptr || // During argument setup.
4995 InputAt(GetCurrentMethodIndexUnchecked())->IsCurrentMethod());
4996 return true;
4997 } else {
4998 DCHECK(InputCount() == GetCurrentMethodIndexUnchecked() ||
4999 InputAt(GetCurrentMethodIndexUnchecked()) == nullptr || // During argument setup.
5000 !InputAt(GetCurrentMethodIndexUnchecked())->IsCurrentMethod());
5001 return false;
5002 }
5003 }
5004
5005 // Get the index of the special input.
5006 size_t GetSpecialInputIndex() const {
5007 DCHECK(HasSpecialInput());
5008 return GetSpecialInputIndexUnchecked();
5009 }
5010 size_t GetSpecialInputIndexUnchecked() const {
5011 return GetNumberOfArguments() + (HasCurrentMethodInput() ? 1u : 0u);
5012 }
5013
5014 // Check if the method has a special input.
5015 bool HasSpecialInput() const {
5016 size_t other_inputs =
5017 GetSpecialInputIndexUnchecked() + (IsStaticWithExplicitClinitCheck() ? 1u : 0u);
5018 size_t input_count = InputCount();
5019 DCHECK_LE(input_count - other_inputs, 1u) << other_inputs << " " << input_count;
5020 return other_inputs != input_count;
5021 }
5022
5023 void AddSpecialInput(HInstruction* input) {
5024 // We allow only one special input.
5025 DCHECK(!HasSpecialInput());
5026 InsertInputAt(GetSpecialInputIndexUnchecked(), input);
5027 }
5028
Vladimir Markofbb184a2015-11-13 14:47:00 +00005029 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
5030 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
5031 // instruction; only relevant for static calls with explicit clinit check.
5032 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01005033 DCHECK(IsStaticWithExplicitClinitCheck());
Vladimir Marko372f10e2016-05-17 16:30:10 +01005034 size_t last_input_index = inputs_.size() - 1u;
5035 HInstruction* last_input = inputs_.back().GetInstruction();
Roland Levillain4c0eb422015-04-24 16:43:49 +01005036 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00005037 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01005038 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005039 inputs_.pop_back();
Vladimir Markoa1de9182016-02-25 11:37:38 +00005040 SetPackedField<ClinitCheckRequirementField>(new_requirement);
Vladimir Markofbb184a2015-11-13 14:47:00 +00005041 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01005042 }
5043
5044 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00005045 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01005046 bool IsStaticWithExplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005047 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kExplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01005048 }
5049
5050 // Is this a call to a static method whose declaring class has an
5051 // implicit intialization check requirement?
5052 bool IsStaticWithImplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005053 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kImplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01005054 }
5055
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00005056 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00005057
Artem Serovcced8ba2017-07-19 18:18:09 +01005058 protected:
5059 DEFAULT_COPY_CONSTRUCTOR(InvokeStaticOrDirect);
5060
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00005061 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01005062 static constexpr size_t kFieldClinitCheckRequirement = kNumberOfInvokePackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005063 static constexpr size_t kFieldClinitCheckRequirementSize =
5064 MinimumBitsToStore(static_cast<size_t>(ClinitCheckRequirement::kLast));
5065 static constexpr size_t kNumberOfInvokeStaticOrDirectPackedBits =
5066 kFieldClinitCheckRequirement + kFieldClinitCheckRequirementSize;
5067 static_assert(kNumberOfInvokeStaticOrDirectPackedBits <= kMaxNumberOfPackedBits,
5068 "Too many packed fields.");
Vladimir Markoa1de9182016-02-25 11:37:38 +00005069 using ClinitCheckRequirementField = BitField<ClinitCheckRequirement,
5070 kFieldClinitCheckRequirement,
5071 kFieldClinitCheckRequirementSize>;
5072
Vladimir Marko58155012015-08-19 12:49:41 +00005073 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00005074};
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01005075std::ostream& operator<<(std::ostream& os, MethodLoadKind rhs);
5076std::ostream& operator<<(std::ostream& os, CodePtrLocation rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00005077std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00005078
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005079class HInvokeVirtual final : public HInvoke {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01005080 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01005081 HInvokeVirtual(ArenaAllocator* allocator,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01005082 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005083 DataType::Type return_type,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01005084 uint32_t dex_pc,
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01005085 MethodReference method_reference,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01005086 ArtMethod* resolved_method,
Nicolas Geoffrayb0f405e2020-09-24 15:10:35 +01005087 MethodReference resolved_method_reference,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01005088 uint32_t vtable_index)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305089 : HInvoke(kInvokeVirtual,
5090 allocator,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01005091 number_of_arguments,
5092 0u,
5093 return_type,
5094 dex_pc,
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01005095 method_reference,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01005096 resolved_method,
Nicolas Geoffrayb0f405e2020-09-24 15:10:35 +01005097 resolved_method_reference,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01005098 kVirtual),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305099 vtable_index_(vtable_index) {
5100 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01005101
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005102 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01005103
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005104 bool CanBeNull() const override {
Aart Bik71bf7b42016-11-16 10:17:46 -08005105 switch (GetIntrinsic()) {
5106 case Intrinsics::kThreadCurrentThread:
5107 case Intrinsics::kStringBufferAppend:
5108 case Intrinsics::kStringBufferToString:
Vladimir Markod4561172017-10-30 17:48:25 +00005109 case Intrinsics::kStringBuilderAppendObject:
5110 case Intrinsics::kStringBuilderAppendString:
5111 case Intrinsics::kStringBuilderAppendCharSequence:
5112 case Intrinsics::kStringBuilderAppendCharArray:
5113 case Intrinsics::kStringBuilderAppendBoolean:
5114 case Intrinsics::kStringBuilderAppendChar:
5115 case Intrinsics::kStringBuilderAppendInt:
5116 case Intrinsics::kStringBuilderAppendLong:
5117 case Intrinsics::kStringBuilderAppendFloat:
5118 case Intrinsics::kStringBuilderAppendDouble:
Aart Bik71bf7b42016-11-16 10:17:46 -08005119 case Intrinsics::kStringBuilderToString:
5120 return false;
5121 default:
5122 return HInvoke::CanBeNull();
5123 }
5124 }
5125
Vladimir Markoac27ac02021-02-01 09:31:02 +00005126 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override;
Calin Juravle77520bc2015-01-12 18:45:46 +00005127
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01005128 uint32_t GetVTableIndex() const { return vtable_index_; }
5129
5130 DECLARE_INSTRUCTION(InvokeVirtual);
5131
Artem Serovcced8ba2017-07-19 18:18:09 +01005132 protected:
5133 DEFAULT_COPY_CONSTRUCTOR(InvokeVirtual);
5134
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01005135 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01005136 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01005137 const uint32_t vtable_index_;
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01005138};
5139
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005140class HInvokeInterface final : public HInvoke {
Nicolas Geoffray52839d12014-11-07 17:47:25 +00005141 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01005142 HInvokeInterface(ArenaAllocator* allocator,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00005143 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005144 DataType::Type return_type,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00005145 uint32_t dex_pc,
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01005146 MethodReference method_reference,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01005147 ArtMethod* resolved_method,
Nicolas Geoffrayb0f405e2020-09-24 15:10:35 +01005148 MethodReference resolved_method_reference,
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01005149 uint32_t imt_index,
5150 MethodLoadKind load_kind)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305151 : HInvoke(kInvokeInterface,
5152 allocator,
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01005153 number_of_arguments + (NeedsCurrentMethod(load_kind) ? 1 : 0),
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01005154 0u,
5155 return_type,
5156 dex_pc,
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01005157 method_reference,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01005158 resolved_method,
Nicolas Geoffrayb0f405e2020-09-24 15:10:35 +01005159 resolved_method_reference,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01005160 kInterface),
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01005161 imt_index_(imt_index),
5162 hidden_argument_load_kind_(load_kind) {
5163 }
5164
5165 static bool NeedsCurrentMethod(MethodLoadKind load_kind) {
5166 return load_kind == MethodLoadKind::kRecursive;
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305167 }
Nicolas Geoffray52839d12014-11-07 17:47:25 +00005168
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005169 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01005170
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005171 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Calin Juravle77520bc2015-01-12 18:45:46 +00005172 // TODO: Add implicit null checks in intrinsics.
Nicolas Geoffray61ba8d22018-08-07 09:55:57 +01005173 return (obj == InputAt(0)) && !IsIntrinsic();
Calin Juravle77520bc2015-01-12 18:45:46 +00005174 }
5175
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01005176 size_t GetSpecialInputIndex() const {
5177 return GetNumberOfArguments();
5178 }
5179
5180 void AddSpecialInput(HInstruction* input) {
5181 InsertInputAt(GetSpecialInputIndex(), input);
5182 }
5183
Nicolas Geoffray52839d12014-11-07 17:47:25 +00005184 uint32_t GetImtIndex() const { return imt_index_; }
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01005185 MethodLoadKind GetHiddenArgumentLoadKind() const { return hidden_argument_load_kind_; }
Nicolas Geoffray52839d12014-11-07 17:47:25 +00005186
5187 DECLARE_INSTRUCTION(InvokeInterface);
5188
Artem Serovcced8ba2017-07-19 18:18:09 +01005189 protected:
5190 DEFAULT_COPY_CONSTRUCTOR(InvokeInterface);
5191
Nicolas Geoffray52839d12014-11-07 17:47:25 +00005192 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01005193 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00005194 const uint32_t imt_index_;
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01005195
5196 // How the hidden argument (the interface method) is being loaded.
5197 const MethodLoadKind hidden_argument_load_kind_;
Nicolas Geoffray52839d12014-11-07 17:47:25 +00005198};
5199
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005200class HNeg final : public HUnaryOperation {
Roland Levillain88cb1752014-10-20 16:36:47 +01005201 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005202 HNeg(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305203 : HUnaryOperation(kNeg, result_type, input, dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005204 DCHECK_EQ(result_type, DataType::Kind(input->GetType()));
Roland Levillain937e6cd2016-03-22 11:54:37 +00005205 }
Roland Levillain88cb1752014-10-20 16:36:47 +01005206
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005207 template <typename T> static T Compute(T x) { return -x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005208
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005209 HConstant* Evaluate(HIntConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005210 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005211 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005212 HConstant* Evaluate(HLongConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005213 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005214 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005215 HConstant* Evaluate(HFloatConstant* x) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005216 return GetBlock()->GetGraph()->GetFloatConstant(Compute(x->GetValue()), GetDexPc());
5217 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005218 HConstant* Evaluate(HDoubleConstant* x) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005219 return GetBlock()->GetGraph()->GetDoubleConstant(Compute(x->GetValue()), GetDexPc());
5220 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01005221
Roland Levillain88cb1752014-10-20 16:36:47 +01005222 DECLARE_INSTRUCTION(Neg);
5223
Artem Serovcced8ba2017-07-19 18:18:09 +01005224 protected:
5225 DEFAULT_COPY_CONSTRUCTOR(Neg);
Roland Levillain88cb1752014-10-20 16:36:47 +01005226};
5227
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005228class HNewArray final : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01005229 public:
Vladimir Markob5461632018-10-15 14:24:21 +01005230 HNewArray(HInstruction* cls, HInstruction* length, uint32_t dex_pc, size_t component_size_shift)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305231 : HExpression(kNewArray, DataType::Type::kReference, SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005232 SetRawInputAt(0, cls);
5233 SetRawInputAt(1, length);
Vladimir Markob5461632018-10-15 14:24:21 +01005234 SetPackedField<ComponentSizeShiftField>(component_size_shift);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01005235 }
5236
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005237 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01005238
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01005239 // Calls runtime so needs an environment.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005240 bool NeedsEnvironment() const override { return true; }
Calin Juravle10e244f2015-01-26 18:54:32 +00005241
Mingyao Yang0c365e62015-03-31 15:09:29 -07005242 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005243 bool CanThrow() const override { return true; }
Mingyao Yang0c365e62015-03-31 15:09:29 -07005244
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005245 bool CanBeNull() const override { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01005246
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005247 HLoadClass* GetLoadClass() const {
5248 DCHECK(InputAt(0)->IsLoadClass());
5249 return InputAt(0)->AsLoadClass();
5250 }
5251
5252 HInstruction* GetLength() const {
5253 return InputAt(1);
5254 }
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00005255
Vladimir Markob5461632018-10-15 14:24:21 +01005256 size_t GetComponentSizeShift() {
5257 return GetPackedField<ComponentSizeShiftField>();
5258 }
5259
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01005260 DECLARE_INSTRUCTION(NewArray);
5261
Artem Serovcced8ba2017-07-19 18:18:09 +01005262 protected:
5263 DEFAULT_COPY_CONSTRUCTOR(NewArray);
Vladimir Markob5461632018-10-15 14:24:21 +01005264
5265 private:
5266 static constexpr size_t kFieldComponentSizeShift = kNumberOfGenericPackedBits;
5267 static constexpr size_t kFieldComponentSizeShiftSize = MinimumBitsToStore(3u);
5268 static constexpr size_t kNumberOfNewArrayPackedBits =
5269 kFieldComponentSizeShift + kFieldComponentSizeShiftSize;
5270 static_assert(kNumberOfNewArrayPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5271 using ComponentSizeShiftField =
5272 BitField<size_t, kFieldComponentSizeShift, kFieldComponentSizeShift>;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01005273};
5274
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005275class HAdd final : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00005276 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005277 HAdd(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005278 HInstruction* left,
5279 HInstruction* right,
5280 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305281 : HBinaryOperation(kAdd, result_type, left, right, SideEffects::None(), dex_pc) {
5282 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00005283
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005284 bool IsCommutative() const override { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00005285
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005286 template <typename T> static T Compute(T x, T y) { return x + y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005287
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005288 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005289 return GetBlock()->GetGraph()->GetIntConstant(
5290 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01005291 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005292 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005293 return GetBlock()->GetGraph()->GetLongConstant(
5294 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01005295 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005296 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005297 return GetBlock()->GetGraph()->GetFloatConstant(
5298 Compute(x->GetValue(), y->GetValue()), GetDexPc());
5299 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005300 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005301 return GetBlock()->GetGraph()->GetDoubleConstant(
5302 Compute(x->GetValue(), y->GetValue()), GetDexPc());
5303 }
Roland Levillain556c3d12014-09-18 15:25:07 +01005304
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00005305 DECLARE_INSTRUCTION(Add);
5306
Artem Serovcced8ba2017-07-19 18:18:09 +01005307 protected:
5308 DEFAULT_COPY_CONSTRUCTOR(Add);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00005309};
5310
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005311class HSub final : public HBinaryOperation {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005312 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005313 HSub(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005314 HInstruction* left,
5315 HInstruction* right,
5316 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305317 : HBinaryOperation(kSub, result_type, left, right, SideEffects::None(), dex_pc) {
5318 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005319
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005320 template <typename T> static T Compute(T x, T y) { return x - y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005321
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005322 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005323 return GetBlock()->GetGraph()->GetIntConstant(
5324 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01005325 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005326 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005327 return GetBlock()->GetGraph()->GetLongConstant(
5328 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01005329 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005330 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005331 return GetBlock()->GetGraph()->GetFloatConstant(
5332 Compute(x->GetValue(), y->GetValue()), GetDexPc());
5333 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005334 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005335 return GetBlock()->GetGraph()->GetDoubleConstant(
5336 Compute(x->GetValue(), y->GetValue()), GetDexPc());
5337 }
Roland Levillain556c3d12014-09-18 15:25:07 +01005338
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005339 DECLARE_INSTRUCTION(Sub);
5340
Artem Serovcced8ba2017-07-19 18:18:09 +01005341 protected:
5342 DEFAULT_COPY_CONSTRUCTOR(Sub);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005343};
5344
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005345class HMul final : public HBinaryOperation {
Calin Juravle34bacdf2014-10-07 20:23:36 +01005346 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005347 HMul(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005348 HInstruction* left,
5349 HInstruction* right,
5350 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305351 : HBinaryOperation(kMul, result_type, left, right, SideEffects::None(), dex_pc) {
5352 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01005353
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005354 bool IsCommutative() const override { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01005355
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005356 template <typename T> static T Compute(T x, T y) { return x * y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005357
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005358 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005359 return GetBlock()->GetGraph()->GetIntConstant(
5360 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005361 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005362 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005363 return GetBlock()->GetGraph()->GetLongConstant(
5364 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005365 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005366 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005367 return GetBlock()->GetGraph()->GetFloatConstant(
5368 Compute(x->GetValue(), y->GetValue()), GetDexPc());
5369 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005370 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005371 return GetBlock()->GetGraph()->GetDoubleConstant(
5372 Compute(x->GetValue(), y->GetValue()), GetDexPc());
5373 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01005374
5375 DECLARE_INSTRUCTION(Mul);
5376
Artem Serovcced8ba2017-07-19 18:18:09 +01005377 protected:
5378 DEFAULT_COPY_CONSTRUCTOR(Mul);
Calin Juravle34bacdf2014-10-07 20:23:36 +01005379};
5380
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005381class HDiv final : public HBinaryOperation {
Calin Juravle7c4954d2014-10-28 16:57:40 +00005382 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005383 HDiv(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005384 HInstruction* left,
5385 HInstruction* right,
5386 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305387 : HBinaryOperation(kDiv, result_type, left, right, SideEffects::None(), dex_pc) {
5388 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00005389
Roland Levillain9867bc72015-08-05 10:21:34 +01005390 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00005391 T ComputeIntegral(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005392 DCHECK(!DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01005393 // Our graph structure ensures we never have 0 for `y` during
5394 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00005395 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00005396 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00005397 return (y == -1) ? -x : x / y;
5398 }
Calin Juravlebacfec32014-11-14 15:54:36 +00005399
Roland Levillain31dd3d62016-02-16 12:21:02 +00005400 template <typename T>
5401 T ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005402 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00005403 return x / y;
5404 }
5405
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005406 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005407 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00005408 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005409 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005410 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005411 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00005412 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5413 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005414 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005415 return GetBlock()->GetGraph()->GetFloatConstant(
5416 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
5417 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005418 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005419 return GetBlock()->GetGraph()->GetDoubleConstant(
5420 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00005421 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00005422
5423 DECLARE_INSTRUCTION(Div);
5424
Artem Serovcced8ba2017-07-19 18:18:09 +01005425 protected:
5426 DEFAULT_COPY_CONSTRUCTOR(Div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00005427};
5428
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005429class HRem final : public HBinaryOperation {
Calin Juravlebacfec32014-11-14 15:54:36 +00005430 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005431 HRem(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005432 HInstruction* left,
5433 HInstruction* right,
5434 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305435 : HBinaryOperation(kRem, result_type, left, right, SideEffects::None(), dex_pc) {
5436 }
Calin Juravlebacfec32014-11-14 15:54:36 +00005437
Roland Levillain9867bc72015-08-05 10:21:34 +01005438 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00005439 T ComputeIntegral(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005440 DCHECK(!DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01005441 // Our graph structure ensures we never have 0 for `y` during
5442 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00005443 DCHECK_NE(y, 0);
5444 // Special case -1 to avoid getting a SIGFPE on x86(_64).
5445 return (y == -1) ? 0 : x % y;
5446 }
5447
Roland Levillain31dd3d62016-02-16 12:21:02 +00005448 template <typename T>
5449 T ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005450 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00005451 return std::fmod(x, y);
5452 }
5453
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005454 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005455 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00005456 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005457 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005458 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005459 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00005460 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00005461 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005462 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005463 return GetBlock()->GetGraph()->GetFloatConstant(
5464 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
5465 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005466 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005467 return GetBlock()->GetGraph()->GetDoubleConstant(
5468 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
5469 }
Calin Juravlebacfec32014-11-14 15:54:36 +00005470
5471 DECLARE_INSTRUCTION(Rem);
5472
Artem Serovcced8ba2017-07-19 18:18:09 +01005473 protected:
5474 DEFAULT_COPY_CONSTRUCTOR(Rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00005475};
5476
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005477class HMin final : public HBinaryOperation {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005478 public:
5479 HMin(DataType::Type result_type,
5480 HInstruction* left,
5481 HInstruction* right,
5482 uint32_t dex_pc)
5483 : HBinaryOperation(kMin, result_type, left, right, SideEffects::None(), dex_pc) {}
5484
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005485 bool IsCommutative() const override { return true; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005486
5487 // Evaluation for integral values.
5488 template <typename T> static T ComputeIntegral(T x, T y) {
5489 return (x <= y) ? x : y;
5490 }
5491
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005492 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005493 return GetBlock()->GetGraph()->GetIntConstant(
5494 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5495 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005496 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005497 return GetBlock()->GetGraph()->GetLongConstant(
5498 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5499 }
5500 // TODO: Evaluation for floating-point values.
5501 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005502 HFloatConstant* y ATTRIBUTE_UNUSED) const override { return nullptr; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005503 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005504 HDoubleConstant* y ATTRIBUTE_UNUSED) const override { return nullptr; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005505
5506 DECLARE_INSTRUCTION(Min);
5507
5508 protected:
5509 DEFAULT_COPY_CONSTRUCTOR(Min);
5510};
5511
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005512class HMax final : public HBinaryOperation {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005513 public:
5514 HMax(DataType::Type result_type,
5515 HInstruction* left,
5516 HInstruction* right,
5517 uint32_t dex_pc)
5518 : HBinaryOperation(kMax, result_type, left, right, SideEffects::None(), dex_pc) {}
5519
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005520 bool IsCommutative() const override { return true; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005521
5522 // Evaluation for integral values.
5523 template <typename T> static T ComputeIntegral(T x, T y) {
5524 return (x >= y) ? x : y;
5525 }
5526
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005527 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005528 return GetBlock()->GetGraph()->GetIntConstant(
5529 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5530 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005531 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005532 return GetBlock()->GetGraph()->GetLongConstant(
5533 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5534 }
5535 // TODO: Evaluation for floating-point values.
5536 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005537 HFloatConstant* y ATTRIBUTE_UNUSED) const override { return nullptr; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005538 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005539 HDoubleConstant* y ATTRIBUTE_UNUSED) const override { return nullptr; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005540
5541 DECLARE_INSTRUCTION(Max);
5542
5543 protected:
5544 DEFAULT_COPY_CONSTRUCTOR(Max);
5545};
5546
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005547class HAbs final : public HUnaryOperation {
Aart Bik3dad3412018-02-28 12:01:46 -08005548 public:
5549 HAbs(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
5550 : HUnaryOperation(kAbs, result_type, input, dex_pc) {}
5551
5552 // Evaluation for integral values.
5553 template <typename T> static T ComputeIntegral(T x) {
5554 return x < 0 ? -x : x;
5555 }
5556
5557 // Evaluation for floating-point values.
5558 // Note, as a "quality of implementation", rather than pure "spec compliance",
5559 // we require that Math.abs() clears the sign bit (but changes nothing else)
5560 // for all floating-point numbers, including NaN (signaling NaN may become quiet though).
5561 // http://b/30758343
5562 template <typename T, typename S> static T ComputeFP(T x) {
5563 S bits = bit_cast<S, T>(x);
5564 return bit_cast<T, S>(bits & std::numeric_limits<S>::max());
5565 }
5566
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005567 HConstant* Evaluate(HIntConstant* x) const override {
Aart Bik3dad3412018-02-28 12:01:46 -08005568 return GetBlock()->GetGraph()->GetIntConstant(ComputeIntegral(x->GetValue()), GetDexPc());
5569 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005570 HConstant* Evaluate(HLongConstant* x) const override {
Aart Bik3dad3412018-02-28 12:01:46 -08005571 return GetBlock()->GetGraph()->GetLongConstant(ComputeIntegral(x->GetValue()), GetDexPc());
5572 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005573 HConstant* Evaluate(HFloatConstant* x) const override {
Aart Bik3dad3412018-02-28 12:01:46 -08005574 return GetBlock()->GetGraph()->GetFloatConstant(
5575 ComputeFP<float, int32_t>(x->GetValue()), GetDexPc());
5576 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005577 HConstant* Evaluate(HDoubleConstant* x) const override {
Aart Bik3dad3412018-02-28 12:01:46 -08005578 return GetBlock()->GetGraph()->GetDoubleConstant(
5579 ComputeFP<double, int64_t>(x->GetValue()), GetDexPc());
5580 }
5581
5582 DECLARE_INSTRUCTION(Abs);
5583
5584 protected:
5585 DEFAULT_COPY_CONSTRUCTOR(Abs);
5586};
5587
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005588class HDivZeroCheck final : public HExpression<1> {
Calin Juravled0d48522014-11-04 16:40:20 +00005589 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00005590 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
Artem Serovd1aa7d02018-06-22 11:35:46 +01005591 // constructor. However it can only do it on a fatal slow path so execution never returns to the
5592 // instruction following the current one; thus 'SideEffects::None()' is used.
Calin Juravled0d48522014-11-04 16:40:20 +00005593 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Artem Serovd1aa7d02018-06-22 11:35:46 +01005594 : HExpression(kDivZeroCheck, value->GetType(), SideEffects::None(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00005595 SetRawInputAt(0, value);
5596 }
5597
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005598 bool IsClonable() const override { return true; }
5599 bool CanBeMoved() const override { return true; }
Calin Juravled0d48522014-11-04 16:40:20 +00005600
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005601 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Calin Juravled0d48522014-11-04 16:40:20 +00005602 return true;
5603 }
5604
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005605 bool NeedsEnvironment() const override { return true; }
5606 bool CanThrow() const override { return true; }
Calin Juravled0d48522014-11-04 16:40:20 +00005607
Calin Juravled0d48522014-11-04 16:40:20 +00005608 DECLARE_INSTRUCTION(DivZeroCheck);
5609
Artem Serovcced8ba2017-07-19 18:18:09 +01005610 protected:
5611 DEFAULT_COPY_CONSTRUCTOR(DivZeroCheck);
Calin Juravled0d48522014-11-04 16:40:20 +00005612};
5613
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005614class HShl final : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005615 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005616 HShl(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005617 HInstruction* value,
5618 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005619 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305620 : HBinaryOperation(kShl, result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005621 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5622 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01005623 }
5624
Roland Levillain5b5b9312016-03-22 14:57:31 +00005625 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005626 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005627 return value << (distance & max_shift_distance);
5628 }
5629
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005630 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005631 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005632 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005633 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005634 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005635 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005636 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005637 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005638 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005639 HLongConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005640 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5641 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01005642 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005643 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005644 HFloatConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005645 LOG(FATAL) << DebugName() << " is not defined for float values";
5646 UNREACHABLE();
5647 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005648 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005649 HDoubleConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005650 LOG(FATAL) << DebugName() << " is not defined for double values";
5651 UNREACHABLE();
5652 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005653
5654 DECLARE_INSTRUCTION(Shl);
5655
Artem Serovcced8ba2017-07-19 18:18:09 +01005656 protected:
5657 DEFAULT_COPY_CONSTRUCTOR(Shl);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005658};
5659
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005660class HShr final : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005661 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005662 HShr(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005663 HInstruction* value,
5664 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005665 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305666 : HBinaryOperation(kShr, result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005667 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5668 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01005669 }
5670
Roland Levillain5b5b9312016-03-22 14:57:31 +00005671 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005672 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005673 return value >> (distance & max_shift_distance);
5674 }
5675
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005676 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005677 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005678 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005679 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005680 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005681 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005682 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005683 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005684 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005685 HLongConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005686 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5687 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01005688 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005689 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005690 HFloatConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005691 LOG(FATAL) << DebugName() << " is not defined for float values";
5692 UNREACHABLE();
5693 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005694 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005695 HDoubleConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005696 LOG(FATAL) << DebugName() << " is not defined for double values";
5697 UNREACHABLE();
5698 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005699
5700 DECLARE_INSTRUCTION(Shr);
5701
Artem Serovcced8ba2017-07-19 18:18:09 +01005702 protected:
5703 DEFAULT_COPY_CONSTRUCTOR(Shr);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005704};
5705
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005706class HUShr final : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005707 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005708 HUShr(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005709 HInstruction* value,
5710 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005711 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305712 : HBinaryOperation(kUShr, result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005713 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5714 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Calin Juravle9aec02f2014-11-18 23:06:35 +00005715 }
5716
Roland Levillain5b5b9312016-03-22 14:57:31 +00005717 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005718 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Vladimir Marko4f990712021-07-14 12:45:13 +01005719 using V = std::make_unsigned_t<T>;
Roland Levillain5b5b9312016-03-22 14:57:31 +00005720 V ux = static_cast<V>(value);
5721 return static_cast<T>(ux >> (distance & max_shift_distance));
5722 }
5723
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005724 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005725 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005726 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005727 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005728 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005729 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005730 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005731 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005732 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005733 HLongConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005734 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5735 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00005736 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005737 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005738 HFloatConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005739 LOG(FATAL) << DebugName() << " is not defined for float values";
5740 UNREACHABLE();
5741 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005742 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005743 HDoubleConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005744 LOG(FATAL) << DebugName() << " is not defined for double values";
5745 UNREACHABLE();
5746 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005747
5748 DECLARE_INSTRUCTION(UShr);
5749
Artem Serovcced8ba2017-07-19 18:18:09 +01005750 protected:
5751 DEFAULT_COPY_CONSTRUCTOR(UShr);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005752};
5753
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005754class HAnd final : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005755 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005756 HAnd(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005757 HInstruction* left,
5758 HInstruction* right,
5759 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305760 : HBinaryOperation(kAnd, result_type, left, right, SideEffects::None(), dex_pc) {
5761 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005762
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005763 bool IsCommutative() const override { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005764
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005765 template <typename T> static T Compute(T x, T y) { return x & y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005766
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005767 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005768 return GetBlock()->GetGraph()->GetIntConstant(
5769 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005770 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005771 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005772 return GetBlock()->GetGraph()->GetLongConstant(
5773 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005774 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005775 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005776 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005777 LOG(FATAL) << DebugName() << " is not defined for float values";
5778 UNREACHABLE();
5779 }
5780 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005781 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005782 LOG(FATAL) << DebugName() << " is not defined for double values";
5783 UNREACHABLE();
5784 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005785
5786 DECLARE_INSTRUCTION(And);
5787
Artem Serovcced8ba2017-07-19 18:18:09 +01005788 protected:
5789 DEFAULT_COPY_CONSTRUCTOR(And);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005790};
5791
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005792class HOr final : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005793 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005794 HOr(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005795 HInstruction* left,
5796 HInstruction* right,
5797 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305798 : HBinaryOperation(kOr, result_type, left, right, SideEffects::None(), dex_pc) {
5799 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005800
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005801 bool IsCommutative() const override { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005802
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005803 template <typename T> static T Compute(T x, T y) { return x | y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005804
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005805 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005806 return GetBlock()->GetGraph()->GetIntConstant(
5807 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005808 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005809 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005810 return GetBlock()->GetGraph()->GetLongConstant(
5811 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005812 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005813 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005814 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005815 LOG(FATAL) << DebugName() << " is not defined for float values";
5816 UNREACHABLE();
5817 }
5818 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005819 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005820 LOG(FATAL) << DebugName() << " is not defined for double values";
5821 UNREACHABLE();
5822 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005823
5824 DECLARE_INSTRUCTION(Or);
5825
Artem Serovcced8ba2017-07-19 18:18:09 +01005826 protected:
5827 DEFAULT_COPY_CONSTRUCTOR(Or);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005828};
5829
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005830class HXor final : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005831 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005832 HXor(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005833 HInstruction* left,
5834 HInstruction* right,
5835 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305836 : HBinaryOperation(kXor, result_type, left, right, SideEffects::None(), dex_pc) {
5837 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005838
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005839 bool IsCommutative() const override { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005840
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005841 template <typename T> static T Compute(T x, T y) { return x ^ y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005842
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005843 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005844 return GetBlock()->GetGraph()->GetIntConstant(
5845 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005846 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005847 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005848 return GetBlock()->GetGraph()->GetLongConstant(
5849 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005850 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005851 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005852 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005853 LOG(FATAL) << DebugName() << " is not defined for float values";
5854 UNREACHABLE();
5855 }
5856 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005857 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005858 LOG(FATAL) << DebugName() << " is not defined for double values";
5859 UNREACHABLE();
5860 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005861
5862 DECLARE_INSTRUCTION(Xor);
5863
Artem Serovcced8ba2017-07-19 18:18:09 +01005864 protected:
5865 DEFAULT_COPY_CONSTRUCTOR(Xor);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005866};
5867
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005868class HRor final : public HBinaryOperation {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005869 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005870 HRor(DataType::Type result_type, HInstruction* value, HInstruction* distance)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305871 : HBinaryOperation(kRor, result_type, value, distance) {
Roland Levillain22c49222016-03-18 14:04:28 +00005872 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005873
Roland Levillain5b5b9312016-03-22 14:57:31 +00005874 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005875 static T Compute(T value, int32_t distance, int32_t max_shift_value) {
Vladimir Marko4f990712021-07-14 12:45:13 +01005876 using V = std::make_unsigned_t<T>;
Roland Levillain5b5b9312016-03-22 14:57:31 +00005877 V ux = static_cast<V>(value);
5878 if ((distance & max_shift_value) == 0) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005879 return static_cast<T>(ux);
5880 } else {
5881 const V reg_bits = sizeof(T) * 8;
Roland Levillain5b5b9312016-03-22 14:57:31 +00005882 return static_cast<T>(ux >> (distance & max_shift_value)) |
5883 (value << (reg_bits - (distance & max_shift_value)));
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005884 }
5885 }
5886
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005887 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const override {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005888 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005889 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005890 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005891 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const override {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005892 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005893 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005894 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005895 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005896 HLongConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005897 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5898 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005899 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005900 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005901 HFloatConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005902 LOG(FATAL) << DebugName() << " is not defined for float values";
5903 UNREACHABLE();
5904 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005905 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005906 HDoubleConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005907 LOG(FATAL) << DebugName() << " is not defined for double values";
5908 UNREACHABLE();
5909 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005910
5911 DECLARE_INSTRUCTION(Ror);
5912
Artem Serovcced8ba2017-07-19 18:18:09 +01005913 protected:
5914 DEFAULT_COPY_CONSTRUCTOR(Ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005915};
5916
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005917// The value of a parameter in this method. Its location depends on
5918// the calling convention.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005919class HParameterValue final : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005920 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005921 HParameterValue(const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08005922 dex::TypeIndex type_index,
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005923 uint8_t index,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005924 DataType::Type parameter_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005925 bool is_this = false)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305926 : HExpression(kParameterValue, parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005927 dex_file_(dex_file),
5928 type_index_(type_index),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005929 index_(index) {
5930 SetPackedFlag<kFlagIsThis>(is_this);
5931 SetPackedFlag<kFlagCanBeNull>(!is_this);
5932 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005933
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005934 const DexFile& GetDexFile() const { return dex_file_; }
Andreas Gampea5b09a62016-11-17 15:21:22 -08005935 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005936 uint8_t GetIndex() const { return index_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07005937 bool IsThis() const { return GetPackedFlag<kFlagIsThis>(); }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005938
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005939 bool CanBeNull() const override { return GetPackedFlag<kFlagCanBeNull>(); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005940 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00005941
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005942 DECLARE_INSTRUCTION(ParameterValue);
5943
Artem Serovcced8ba2017-07-19 18:18:09 +01005944 protected:
5945 DEFAULT_COPY_CONSTRUCTOR(ParameterValue);
5946
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005947 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005948 // Whether or not the parameter value corresponds to 'this' argument.
Vladimir Markobd785672018-05-03 17:09:09 +01005949 static constexpr size_t kFlagIsThis = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005950 static constexpr size_t kFlagCanBeNull = kFlagIsThis + 1;
5951 static constexpr size_t kNumberOfParameterValuePackedBits = kFlagCanBeNull + 1;
5952 static_assert(kNumberOfParameterValuePackedBits <= kMaxNumberOfPackedBits,
5953 "Too many packed fields.");
5954
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005955 const DexFile& dex_file_;
Andreas Gampea5b09a62016-11-17 15:21:22 -08005956 const dex::TypeIndex type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005957 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00005958 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005959 const uint8_t index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005960};
5961
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005962class HNot final : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005963 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005964 HNot(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305965 : HUnaryOperation(kNot, result_type, input, dex_pc) {
5966 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005967
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005968 bool CanBeMoved() const override { return true; }
5969 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005970 return true;
5971 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005972
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005973 template <typename T> static T Compute(T x) { return ~x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005974
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005975 HConstant* Evaluate(HIntConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005976 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005977 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005978 HConstant* Evaluate(HLongConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005979 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005980 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005981 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005982 LOG(FATAL) << DebugName() << " is not defined for float values";
5983 UNREACHABLE();
5984 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005985 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005986 LOG(FATAL) << DebugName() << " is not defined for double values";
5987 UNREACHABLE();
5988 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01005989
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005990 DECLARE_INSTRUCTION(Not);
5991
Artem Serovcced8ba2017-07-19 18:18:09 +01005992 protected:
5993 DEFAULT_COPY_CONSTRUCTOR(Not);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005994};
5995
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005996class HBooleanNot final : public HUnaryOperation {
David Brazdil66d126e2015-04-03 16:02:44 +01005997 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005998 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305999 : HUnaryOperation(kBooleanNot, DataType::Type::kBool, input, dex_pc) {
6000 }
David Brazdil66d126e2015-04-03 16:02:44 +01006001
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006002 bool CanBeMoved() const override { return true; }
6003 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
David Brazdil66d126e2015-04-03 16:02:44 +01006004 return true;
6005 }
6006
Anton Kirilov7fe30f92016-06-29 17:03:40 +01006007 template <typename T> static bool Compute(T x) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00006008 DCHECK(IsUint<1>(x)) << x;
David Brazdil66d126e2015-04-03 16:02:44 +01006009 return !x;
6010 }
6011
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006012 HConstant* Evaluate(HIntConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006013 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01006014 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006015 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01006016 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01006017 UNREACHABLE();
6018 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006019 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00006020 LOG(FATAL) << DebugName() << " is not defined for float values";
6021 UNREACHABLE();
6022 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006023 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00006024 LOG(FATAL) << DebugName() << " is not defined for double values";
6025 UNREACHABLE();
6026 }
David Brazdil66d126e2015-04-03 16:02:44 +01006027
6028 DECLARE_INSTRUCTION(BooleanNot);
6029
Artem Serovcced8ba2017-07-19 18:18:09 +01006030 protected:
6031 DEFAULT_COPY_CONSTRUCTOR(BooleanNot);
David Brazdil66d126e2015-04-03 16:02:44 +01006032};
6033
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006034class HTypeConversion final : public HExpression<1> {
Roland Levillaindff1f282014-11-05 14:15:05 +00006035 public:
6036 // Instantiate a type conversion of `input` to `result_type`.
Vladimir Markoc8fb2112017-10-03 11:37:52 +01006037 HTypeConversion(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306038 : HExpression(kTypeConversion, result_type, SideEffects::None(), dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00006039 SetRawInputAt(0, input);
Roland Levillainf355c3f2016-03-30 19:09:03 +01006040 // Invariant: We should never generate a conversion to a Boolean value.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006041 DCHECK_NE(DataType::Type::kBool, result_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00006042 }
6043
6044 HInstruction* GetInput() const { return InputAt(0); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006045 DataType::Type GetInputType() const { return GetInput()->GetType(); }
6046 DataType::Type GetResultType() const { return GetType(); }
Roland Levillaindff1f282014-11-05 14:15:05 +00006047
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006048 bool IsClonable() const override { return true; }
6049 bool CanBeMoved() const override { return true; }
6050 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01006051 return true;
6052 }
Nicolas Geoffrayacc56ac2018-10-09 08:45:24 +01006053 // Return whether the conversion is implicit. This includes conversion to the same type.
6054 bool IsImplicitConversion() const {
6055 return DataType::IsTypeConversionImplicit(GetInputType(), GetResultType());
6056 }
Roland Levillaindff1f282014-11-05 14:15:05 +00006057
Mark Mendelle82549b2015-05-06 10:55:34 -04006058 // Try to statically evaluate the conversion and return a HConstant
6059 // containing the result. If the input cannot be converted, return nullptr.
6060 HConstant* TryStaticEvaluation() const;
6061
Roland Levillaindff1f282014-11-05 14:15:05 +00006062 DECLARE_INSTRUCTION(TypeConversion);
6063
Artem Serovcced8ba2017-07-19 18:18:09 +01006064 protected:
6065 DEFAULT_COPY_CONSTRUCTOR(TypeConversion);
Roland Levillaindff1f282014-11-05 14:15:05 +00006066};
6067
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00006068static constexpr uint32_t kNoRegNumber = -1;
6069
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006070class HNullCheck final : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006071 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00006072 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
Artem Serovd1aa7d02018-06-22 11:35:46 +01006073 // constructor. However it can only do it on a fatal slow path so execution never returns to the
6074 // instruction following the current one; thus 'SideEffects::None()' is used.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006075 HNullCheck(HInstruction* value, uint32_t dex_pc)
Artem Serovd1aa7d02018-06-22 11:35:46 +01006076 : HExpression(kNullCheck, value->GetType(), SideEffects::None(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006077 SetRawInputAt(0, value);
6078 }
6079
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006080 bool IsClonable() const override { return true; }
6081 bool CanBeMoved() const override { return true; }
6082 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07006083 return true;
6084 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01006085
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006086 bool NeedsEnvironment() const override { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006087
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006088 bool CanThrow() const override { return true; }
Calin Juravle10e244f2015-01-26 18:54:32 +00006089
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006090 bool CanBeNull() const override { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01006091
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006092 DECLARE_INSTRUCTION(NullCheck);
6093
Artem Serovcced8ba2017-07-19 18:18:09 +01006094 protected:
6095 DEFAULT_COPY_CONSTRUCTOR(NullCheck);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006096};
6097
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006098// Embeds an ArtField and all the information required by the compiler. We cache
6099// that information to avoid requiring the mutator lock every time we need it.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006100class FieldInfo : public ValueObject {
6101 public:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006102 FieldInfo(ArtField* field,
6103 MemberOffset field_offset,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006104 DataType::Type field_type,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006105 bool is_volatile,
6106 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006107 uint16_t declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006108 const DexFile& dex_file)
6109 : field_(field),
6110 field_offset_(field_offset),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006111 field_type_(field_type),
6112 is_volatile_(is_volatile),
6113 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07006114 declaring_class_def_index_(declaring_class_def_index),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006115 dex_file_(dex_file) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006116
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006117 ArtField* GetField() const { return field_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006118 MemberOffset GetFieldOffset() const { return field_offset_; }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006119 DataType::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006120 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07006121 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006122 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00006123 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006124
Alex Light3a73ffb2021-01-25 14:11:05 +00006125 bool Equals(const FieldInfo& other) const {
6126 return field_ == other.field_ &&
6127 field_offset_ == other.field_offset_ &&
6128 field_type_ == other.field_type_ &&
6129 is_volatile_ == other.is_volatile_ &&
6130 index_ == other.index_ &&
6131 declaring_class_def_index_ == other.declaring_class_def_index_ &&
6132 &dex_file_ == &other.dex_file_;
6133 }
6134
6135 std::ostream& Dump(std::ostream& os) const {
6136 os << field_ << ", off: " << field_offset_ << ", type: " << field_type_
6137 << ", volatile: " << std::boolalpha << is_volatile_ << ", index_: " << std::dec << index_
6138 << ", declaring_class: " << declaring_class_def_index_ << ", dex: " << dex_file_;
6139 return os;
6140 }
6141
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006142 private:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006143 ArtField* const field_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006144 const MemberOffset field_offset_;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006145 const DataType::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00006146 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07006147 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07006148 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006149 const DexFile& dex_file_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006150};
6151
Alex Light3a73ffb2021-01-25 14:11:05 +00006152inline bool operator==(const FieldInfo& a, const FieldInfo& b) {
6153 return a.Equals(b);
6154}
6155
6156inline std::ostream& operator<<(std::ostream& os, const FieldInfo& a) {
6157 return a.Dump(os);
6158}
6159
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006160class HInstanceFieldGet final : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006161 public:
6162 HInstanceFieldGet(HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006163 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006164 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006165 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006166 bool is_volatile,
6167 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006168 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006169 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01006170 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306171 : HExpression(kInstanceFieldGet,
6172 field_type,
6173 SideEffects::FieldReadOfType(field_type, is_volatile),
6174 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006175 field_info_(field,
6176 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006177 field_type,
6178 is_volatile,
6179 field_idx,
6180 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006181 dex_file) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006182 SetRawInputAt(0, value);
6183 }
6184
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006185 bool IsClonable() const override { return true; }
6186 bool CanBeMoved() const override { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006187
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006188 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01006189 const HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00006190 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01006191 }
6192
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006193 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01006194 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00006195 }
6196
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006197 size_t ComputeHashCode() const override {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01006198 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
6199 }
6200
Alex Light3a73ffb2021-01-25 14:11:05 +00006201 bool IsFieldAccess() const override { return true; }
6202 const FieldInfo& GetFieldInfo() const override { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006203 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006204 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006205 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006206
Vladimir Marko61b92282017-10-11 13:23:17 +01006207 void SetType(DataType::Type new_type) {
6208 DCHECK(DataType::IsIntegralType(GetType()));
6209 DCHECK(DataType::IsIntegralType(new_type));
6210 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
6211 SetPackedField<TypeField>(new_type);
6212 }
6213
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006214 DECLARE_INSTRUCTION(InstanceFieldGet);
6215
Artem Serovcced8ba2017-07-19 18:18:09 +01006216 protected:
6217 DEFAULT_COPY_CONSTRUCTOR(InstanceFieldGet);
6218
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006219 private:
6220 const FieldInfo field_info_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006221};
6222
Alex Light3a73ffb2021-01-25 14:11:05 +00006223class HPredicatedInstanceFieldGet final : public HExpression<2> {
6224 public:
6225 HPredicatedInstanceFieldGet(HInstanceFieldGet* orig,
6226 HInstruction* target,
6227 HInstruction* default_val)
6228 : HExpression(kPredicatedInstanceFieldGet,
6229 orig->GetFieldType(),
6230 orig->GetSideEffects(),
6231 orig->GetDexPc()),
6232 field_info_(orig->GetFieldInfo()) {
6233 // NB Default-val is at 0 so we can avoid doing a move.
6234 SetRawInputAt(1, target);
6235 SetRawInputAt(0, default_val);
6236 }
6237
6238 HPredicatedInstanceFieldGet(HInstruction* value,
6239 ArtField* field,
6240 HInstruction* default_value,
6241 DataType::Type field_type,
6242 MemberOffset field_offset,
6243 bool is_volatile,
6244 uint32_t field_idx,
6245 uint16_t declaring_class_def_index,
6246 const DexFile& dex_file,
6247 uint32_t dex_pc)
6248 : HExpression(kPredicatedInstanceFieldGet,
6249 field_type,
6250 SideEffects::FieldReadOfType(field_type, is_volatile),
6251 dex_pc),
6252 field_info_(field,
6253 field_offset,
6254 field_type,
6255 is_volatile,
6256 field_idx,
6257 declaring_class_def_index,
6258 dex_file) {
Alex Light74328052021-03-29 18:11:23 -07006259 SetRawInputAt(1, value);
6260 SetRawInputAt(0, default_value);
Alex Light3a73ffb2021-01-25 14:11:05 +00006261 }
6262
6263 bool IsClonable() const override {
6264 return true;
6265 }
6266 bool CanBeMoved() const override {
6267 return !IsVolatile();
6268 }
6269
6270 HInstruction* GetDefaultValue() const {
6271 return InputAt(0);
6272 }
6273 HInstruction* GetTarget() const {
6274 return InputAt(1);
6275 }
6276
6277 bool InstructionDataEquals(const HInstruction* other) const override {
6278 const HPredicatedInstanceFieldGet* other_get = other->AsPredicatedInstanceFieldGet();
6279 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue() &&
6280 GetDefaultValue() == other_get->GetDefaultValue();
6281 }
6282
6283 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
6284 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
6285 }
6286
6287 size_t ComputeHashCode() const override {
6288 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
6289 }
6290
6291 bool IsFieldAccess() const override { return true; }
6292 const FieldInfo& GetFieldInfo() const override { return field_info_; }
6293 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
6294 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
6295 bool IsVolatile() const { return field_info_.IsVolatile(); }
6296
6297 void SetType(DataType::Type new_type) {
6298 DCHECK(DataType::IsIntegralType(GetType()));
6299 DCHECK(DataType::IsIntegralType(new_type));
6300 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
6301 SetPackedField<TypeField>(new_type);
6302 }
6303
6304 DECLARE_INSTRUCTION(PredicatedInstanceFieldGet);
6305
6306 protected:
6307 DEFAULT_COPY_CONSTRUCTOR(PredicatedInstanceFieldGet);
6308
6309 private:
6310 const FieldInfo field_info_;
6311};
6312
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006313class HInstanceFieldSet final : public HExpression<2> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006314 public:
6315 HInstanceFieldSet(HInstruction* object,
6316 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006317 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006318 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006319 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006320 bool is_volatile,
6321 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006322 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006323 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01006324 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006325 : HExpression(kInstanceFieldSet,
6326 SideEffects::FieldWriteOfType(field_type, is_volatile),
6327 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006328 field_info_(field,
6329 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006330 field_type,
6331 is_volatile,
6332 field_idx,
6333 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006334 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006335 SetPackedFlag<kFlagValueCanBeNull>(true);
Alex Light3a73ffb2021-01-25 14:11:05 +00006336 SetPackedFlag<kFlagIsPredicatedSet>(false);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006337 SetRawInputAt(0, object);
6338 SetRawInputAt(1, value);
6339 }
6340
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006341 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01006342
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006343 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01006344 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00006345 }
6346
Alex Light3a73ffb2021-01-25 14:11:05 +00006347 bool IsFieldAccess() const override { return true; }
6348 const FieldInfo& GetFieldInfo() const override { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006349 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006350 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006351 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006352 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006353 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
6354 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Alex Light3a73ffb2021-01-25 14:11:05 +00006355 bool GetIsPredicatedSet() const { return GetPackedFlag<kFlagIsPredicatedSet>(); }
6356 void SetIsPredicatedSet(bool value = true) { SetPackedFlag<kFlagIsPredicatedSet>(value); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006357
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006358 DECLARE_INSTRUCTION(InstanceFieldSet);
6359
Artem Serovcced8ba2017-07-19 18:18:09 +01006360 protected:
6361 DEFAULT_COPY_CONSTRUCTOR(InstanceFieldSet);
6362
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006363 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006364 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
Alex Light3a73ffb2021-01-25 14:11:05 +00006365 static constexpr size_t kFlagIsPredicatedSet = kFlagValueCanBeNull + 1;
6366 static constexpr size_t kNumberOfInstanceFieldSetPackedBits = kFlagIsPredicatedSet + 1;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006367 static_assert(kNumberOfInstanceFieldSetPackedBits <= kMaxNumberOfPackedBits,
6368 "Too many packed fields.");
6369
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006370 const FieldInfo field_info_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006371};
6372
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006373class HArrayGet final : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006374 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006375 HArrayGet(HInstruction* array,
6376 HInstruction* index,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006377 DataType::Type type,
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006378 uint32_t dex_pc)
6379 : HArrayGet(array,
6380 index,
6381 type,
6382 SideEffects::ArrayReadOfType(type),
6383 dex_pc,
Andreas Gampe3db70682018-12-26 15:12:03 -08006384 /* is_string_char_at= */ false) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306385 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006386
6387 HArrayGet(HInstruction* array,
6388 HInstruction* index,
6389 DataType::Type type,
6390 SideEffects side_effects,
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006391 uint32_t dex_pc,
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006392 bool is_string_char_at)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306393 : HExpression(kArrayGet, type, side_effects, dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006394 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006395 SetRawInputAt(0, array);
6396 SetRawInputAt(1, index);
6397 }
6398
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006399 bool IsClonable() const override { return true; }
6400 bool CanBeMoved() const override { return true; }
6401 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07006402 return true;
6403 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006404 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const override {
Calin Juravle77520bc2015-01-12 18:45:46 +00006405 // TODO: We can be smarter here.
Vladimir Marko66d691d2017-04-07 17:53:39 +01006406 // Currently, unless the array is the result of NewArray, the array access is always
6407 // preceded by some form of null NullCheck necessary for the bounds check, usually
6408 // implicit null check on the ArrayLength input to BoundsCheck or Deoptimize for
6409 // dynamic BCE. There are cases when these could be removed to produce better code.
6410 // If we ever add optimizations to do so we should allow an implicit check here
6411 // (as long as the address falls in the first page).
6412 //
6413 // As an example of such fancy optimization, we could eliminate BoundsCheck for
6414 // a = cond ? new int[1] : null;
6415 // a[0]; // The Phi does not need bounds check for either input.
Calin Juravle77520bc2015-01-12 18:45:46 +00006416 return false;
6417 }
6418
David Brazdil4833f5a2015-12-16 10:37:39 +00006419 bool IsEquivalentOf(HArrayGet* other) const {
6420 bool result = (GetDexPc() == other->GetDexPc());
6421 if (kIsDebugBuild && result) {
6422 DCHECK_EQ(GetBlock(), other->GetBlock());
6423 DCHECK_EQ(GetArray(), other->GetArray());
6424 DCHECK_EQ(GetIndex(), other->GetIndex());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006425 if (DataType::IsIntOrLongType(GetType())) {
6426 DCHECK(DataType::IsFloatingPointType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00006427 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006428 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
6429 DCHECK(DataType::IsIntOrLongType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00006430 }
6431 }
6432 return result;
6433 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01006434
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006435 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
6436
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006437 HInstruction* GetArray() const { return InputAt(0); }
6438 HInstruction* GetIndex() const { return InputAt(1); }
6439
Vladimir Marko61b92282017-10-11 13:23:17 +01006440 void SetType(DataType::Type new_type) {
6441 DCHECK(DataType::IsIntegralType(GetType()));
6442 DCHECK(DataType::IsIntegralType(new_type));
6443 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
6444 SetPackedField<TypeField>(new_type);
6445 }
6446
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006447 DECLARE_INSTRUCTION(ArrayGet);
6448
Artem Serovcced8ba2017-07-19 18:18:09 +01006449 protected:
6450 DEFAULT_COPY_CONSTRUCTOR(ArrayGet);
6451
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006452 private:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006453 // We treat a String as an array, creating the HArrayGet from String.charAt()
6454 // intrinsic in the instruction simplifier. We can always determine whether
6455 // a particular HArrayGet is actually a String.charAt() by looking at the type
6456 // of the input but that requires holding the mutator lock, so we prefer to use
6457 // a flag, so that code generators don't need to do the locking.
Vladimir Markobd785672018-05-03 17:09:09 +01006458 static constexpr size_t kFlagIsStringCharAt = kNumberOfGenericPackedBits;
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006459 static constexpr size_t kNumberOfArrayGetPackedBits = kFlagIsStringCharAt + 1;
6460 static_assert(kNumberOfArrayGetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6461 "Too many packed fields.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006462};
6463
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006464class HArraySet final : public HExpression<3> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006465 public:
6466 HArraySet(HInstruction* array,
6467 HInstruction* index,
6468 HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006469 DataType::Type expected_component_type,
Aart Bik18b36ab2016-04-13 16:41:35 -07006470 uint32_t dex_pc)
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006471 : HArraySet(array,
6472 index,
6473 value,
6474 expected_component_type,
6475 // Make a best guess for side effects now, may be refined during SSA building.
6476 ComputeSideEffects(GetComponentType(value->GetType(), expected_component_type)),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306477 dex_pc) {
6478 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006479
6480 HArraySet(HInstruction* array,
6481 HInstruction* index,
6482 HInstruction* value,
6483 DataType::Type expected_component_type,
6484 SideEffects side_effects,
6485 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006486 : HExpression(kArraySet, side_effects, dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006487 SetPackedField<ExpectedComponentTypeField>(expected_component_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006488 SetPackedFlag<kFlagNeedsTypeCheck>(value->GetType() == DataType::Type::kReference);
Vladimir Markoa1de9182016-02-25 11:37:38 +00006489 SetPackedFlag<kFlagValueCanBeNull>(true);
6490 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(false);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006491 SetRawInputAt(0, array);
6492 SetRawInputAt(1, index);
6493 SetRawInputAt(2, value);
6494 }
6495
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006496 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01006497
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006498 bool NeedsEnvironment() const override {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006499 // We call a runtime method to throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006500 return NeedsTypeCheck();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006501 }
6502
Mingyao Yang81014cb2015-06-02 03:16:27 -07006503 // Can throw ArrayStoreException.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006504 bool CanThrow() const override { return NeedsTypeCheck(); }
Mingyao Yang81014cb2015-06-02 03:16:27 -07006505
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006506 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const override {
Calin Juravle77520bc2015-01-12 18:45:46 +00006507 // TODO: Same as for ArrayGet.
6508 return false;
6509 }
6510
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006511 void ClearNeedsTypeCheck() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006512 SetPackedFlag<kFlagNeedsTypeCheck>(false);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006513 }
6514
Nicolas Geoffray07276db2015-05-18 14:22:09 +01006515 void ClearValueCanBeNull() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006516 SetPackedFlag<kFlagValueCanBeNull>(false);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01006517 }
6518
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006519 void SetStaticTypeOfArrayIsObjectArray() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006520 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(true);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006521 }
6522
Vladimir Markoa1de9182016-02-25 11:37:38 +00006523 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
6524 bool NeedsTypeCheck() const { return GetPackedFlag<kFlagNeedsTypeCheck>(); }
6525 bool StaticTypeOfArrayIsObjectArray() const {
6526 return GetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>();
6527 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006528
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006529 HInstruction* GetArray() const { return InputAt(0); }
6530 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006531 HInstruction* GetValue() const { return InputAt(2); }
6532
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006533 DataType::Type GetComponentType() const {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006534 return GetComponentType(GetValue()->GetType(), GetRawExpectedComponentType());
6535 }
6536
6537 static DataType::Type GetComponentType(DataType::Type value_type,
6538 DataType::Type expected_component_type) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006539 // The Dex format does not type floating point index operations. Since the
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006540 // `expected_component_type` comes from SSA building and can therefore not
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006541 // be correct, we also check what is the value type. If it is a floating
6542 // point type, we must use that type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006543 return ((value_type == DataType::Type::kFloat32) || (value_type == DataType::Type::kFloat64))
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006544 ? value_type
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006545 : expected_component_type;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006546 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01006547
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006548 DataType::Type GetRawExpectedComponentType() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006549 return GetPackedField<ExpectedComponentTypeField>();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01006550 }
6551
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006552 static SideEffects ComputeSideEffects(DataType::Type type) {
6553 return SideEffects::ArrayWriteOfType(type).Union(SideEffectsForArchRuntimeCalls(type));
Aart Bik18b36ab2016-04-13 16:41:35 -07006554 }
6555
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006556 static SideEffects SideEffectsForArchRuntimeCalls(DataType::Type value_type) {
6557 return (value_type == DataType::Type::kReference) ? SideEffects::CanTriggerGC()
6558 : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006559 }
6560
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006561 DECLARE_INSTRUCTION(ArraySet);
6562
Artem Serovcced8ba2017-07-19 18:18:09 +01006563 protected:
6564 DEFAULT_COPY_CONSTRUCTOR(ArraySet);
6565
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006566 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006567 static constexpr size_t kFieldExpectedComponentType = kNumberOfGenericPackedBits;
6568 static constexpr size_t kFieldExpectedComponentTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006569 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00006570 static constexpr size_t kFlagNeedsTypeCheck =
6571 kFieldExpectedComponentType + kFieldExpectedComponentTypeSize;
6572 static constexpr size_t kFlagValueCanBeNull = kFlagNeedsTypeCheck + 1;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006573 // Cached information for the reference_type_info_ so that codegen
6574 // does not need to inspect the static type.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006575 static constexpr size_t kFlagStaticTypeOfArrayIsObjectArray = kFlagValueCanBeNull + 1;
6576 static constexpr size_t kNumberOfArraySetPackedBits =
6577 kFlagStaticTypeOfArrayIsObjectArray + 1;
6578 static_assert(kNumberOfArraySetPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6579 using ExpectedComponentTypeField =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006580 BitField<DataType::Type, kFieldExpectedComponentType, kFieldExpectedComponentTypeSize>;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006581};
6582
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006583class HArrayLength final : public HExpression<1> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006584 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006585 HArrayLength(HInstruction* array, uint32_t dex_pc, bool is_string_length = false)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306586 : HExpression(kArrayLength, DataType::Type::kInt32, SideEffects::None(), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006587 SetPackedFlag<kFlagIsStringLength>(is_string_length);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01006588 // Note that arrays do not change length, so the instruction does not
6589 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006590 SetRawInputAt(0, array);
6591 }
6592
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006593 bool IsClonable() const override { return true; }
6594 bool CanBeMoved() const override { return true; }
6595 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07006596 return true;
6597 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006598 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Calin Juravle641547a2015-04-21 22:08:51 +01006599 return obj == InputAt(0);
6600 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01006601
Vladimir Markodce016e2016-04-28 13:10:02 +01006602 bool IsStringLength() const { return GetPackedFlag<kFlagIsStringLength>(); }
6603
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006604 DECLARE_INSTRUCTION(ArrayLength);
6605
Artem Serovcced8ba2017-07-19 18:18:09 +01006606 protected:
6607 DEFAULT_COPY_CONSTRUCTOR(ArrayLength);
6608
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006609 private:
Vladimir Markodce016e2016-04-28 13:10:02 +01006610 // We treat a String as an array, creating the HArrayLength from String.length()
6611 // or String.isEmpty() intrinsic in the instruction simplifier. We can always
6612 // determine whether a particular HArrayLength is actually a String.length() by
6613 // looking at the type of the input but that requires holding the mutator lock, so
6614 // we prefer to use a flag, so that code generators don't need to do the locking.
Vladimir Markobd785672018-05-03 17:09:09 +01006615 static constexpr size_t kFlagIsStringLength = kNumberOfGenericPackedBits;
Vladimir Markodce016e2016-04-28 13:10:02 +01006616 static constexpr size_t kNumberOfArrayLengthPackedBits = kFlagIsStringLength + 1;
6617 static_assert(kNumberOfArrayLengthPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6618 "Too many packed fields.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006619};
6620
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006621class HBoundsCheck final : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006622 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00006623 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
Artem Serovd1aa7d02018-06-22 11:35:46 +01006624 // constructor. However it can only do it on a fatal slow path so execution never returns to the
6625 // instruction following the current one; thus 'SideEffects::None()' is used.
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006626 HBoundsCheck(HInstruction* index,
6627 HInstruction* length,
6628 uint32_t dex_pc,
Vladimir Marko0259c242017-12-04 11:27:47 +00006629 bool is_string_char_at = false)
Artem Serovd1aa7d02018-06-22 11:35:46 +01006630 : HExpression(kBoundsCheck, index->GetType(), SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006631 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(index->GetType()));
Vladimir Marko0259c242017-12-04 11:27:47 +00006632 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006633 SetRawInputAt(0, index);
6634 SetRawInputAt(1, length);
6635 }
6636
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006637 bool IsClonable() const override { return true; }
6638 bool CanBeMoved() const override { return true; }
6639 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07006640 return true;
6641 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01006642
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006643 bool NeedsEnvironment() const override { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006644
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006645 bool CanThrow() const override { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01006646
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00006647 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006648
Alexandre Ramese6dbf482015-10-19 10:10:41 +01006649 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006650
6651 DECLARE_INSTRUCTION(BoundsCheck);
6652
Artem Serovcced8ba2017-07-19 18:18:09 +01006653 protected:
6654 DEFAULT_COPY_CONSTRUCTOR(BoundsCheck);
6655
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006656 private:
Vladimir Markobd785672018-05-03 17:09:09 +01006657 static constexpr size_t kFlagIsStringCharAt = kNumberOfGenericPackedBits;
Vladimir Markof02046e2018-10-02 15:31:32 +01006658 static constexpr size_t kNumberOfBoundsCheckPackedBits = kFlagIsStringCharAt + 1;
6659 static_assert(kNumberOfBoundsCheckPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6660 "Too many packed fields.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006661};
6662
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006663class HSuspendCheck final : public HExpression<0> {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006664 public:
David Brazdil86ea7ee2016-02-16 09:26:07 +00006665 explicit HSuspendCheck(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01006666 : HExpression(kSuspendCheck, SideEffects::CanTriggerGC(), dex_pc),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306667 slow_path_(nullptr) {
6668 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006669
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006670 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01006671
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006672 bool NeedsEnvironment() const override {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006673 return true;
6674 }
6675
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006676 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
6677 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006678
6679 DECLARE_INSTRUCTION(SuspendCheck);
6680
Artem Serovcced8ba2017-07-19 18:18:09 +01006681 protected:
6682 DEFAULT_COPY_CONSTRUCTOR(SuspendCheck);
6683
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006684 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006685 // Only used for code generation, in order to share the same slow path between back edges
6686 // of a same loop.
6687 SlowPathCode* slow_path_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006688};
6689
David Srbecky0cf44932015-12-09 14:09:59 +00006690// Pseudo-instruction which provides the native debugger with mapping information.
6691// It ensures that we can generate line number and local variables at this point.
Vladimir Markobd785672018-05-03 17:09:09 +01006692class HNativeDebugInfo : public HExpression<0> {
David Srbecky0cf44932015-12-09 14:09:59 +00006693 public:
6694 explicit HNativeDebugInfo(uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006695 : HExpression<0>(kNativeDebugInfo, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306696 }
David Srbecky0cf44932015-12-09 14:09:59 +00006697
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006698 bool NeedsEnvironment() const override {
David Srbecky0cf44932015-12-09 14:09:59 +00006699 return true;
6700 }
6701
6702 DECLARE_INSTRUCTION(NativeDebugInfo);
6703
Artem Serovcced8ba2017-07-19 18:18:09 +01006704 protected:
6705 DEFAULT_COPY_CONSTRUCTOR(NativeDebugInfo);
David Srbecky0cf44932015-12-09 14:09:59 +00006706};
6707
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006708/**
6709 * Instruction to load a Class object.
6710 */
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006711class HLoadClass final : public HInstruction {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006712 public:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006713 // Determines how to load the Class.
6714 enum class LoadKind {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006715 // We cannot load this class. See HSharpening::SharpenLoadClass.
6716 kInvalid = -1,
6717
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006718 // Use the Class* from the method's own ArtMethod*.
6719 kReferrersClass,
6720
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006721 // Use PC-relative boot image Class* address that will be known at link time.
Vladimir Marko764d4542017-05-16 10:31:41 +01006722 // Used for boot image classes referenced by boot image code.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006723 kBootImageLinkTimePcRelative,
6724
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006725 // Load from an entry in the .data.bimg.rel.ro using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006726 // Used for boot image classes referenced by apps in AOT-compiled code.
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006727 kBootImageRelRo,
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006728
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006729 // Load from an entry in the .bss section using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006730 // Used for classes outside boot image referenced by AOT-compiled app and boot image code.
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006731 kBssEntry,
6732
Vladimir Marko8f63f102020-09-28 12:10:28 +01006733 // Load from an entry for public class in the .bss section using a PC-relative load.
6734 // Used for classes that were unresolved during AOT-compilation outside the literal
6735 // package of the compiling class. Such classes are accessible only if they are public
6736 // and the .bss entry shall therefore be filled only if the resolved class is public.
6737 kBssEntryPublic,
6738
6739 // Load from an entry for package class in the .bss section using a PC-relative load.
6740 // Used for classes that were unresolved during AOT-compilation but within the literal
6741 // package of the compiling class. Such classes are accessible if they are public or
6742 // in the same package which, given the literal package match, requires only matching
6743 // defining class loader and the .bss entry shall therefore be filled only if at least
6744 // one of those conditions holds. Note that all code in an oat file belongs to classes
6745 // with the same defining class loader.
6746 kBssEntryPackage,
6747
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006748 // Use a known boot image Class* address, embedded in the code by the codegen.
6749 // Used for boot image classes referenced by apps in JIT-compiled code.
6750 kJitBootImageAddress,
6751
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006752 // Load from the root table associated with the JIT compiled method.
6753 kJitTableAddress,
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006754
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006755 // Load using a simple runtime call. This is the fall-back load kind when
6756 // the codegen is unable to use another appropriate kind.
6757 kRuntimeCall,
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006758
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006759 kLast = kRuntimeCall
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006760 };
6761
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01006762 HLoadClass(HCurrentMethod* current_method,
Andreas Gampea5b09a62016-11-17 15:21:22 -08006763 dex::TypeIndex type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01006764 const DexFile& dex_file,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006765 Handle<mirror::Class> klass,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006766 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01006767 uint32_t dex_pc,
Nicolas Geoffray56876342016-12-16 16:09:08 +00006768 bool needs_access_check)
Vladimir Markobd785672018-05-03 17:09:09 +01006769 : HInstruction(kLoadClass,
6770 DataType::Type::kReference,
6771 SideEffectsForArchRuntimeCalls(),
6772 dex_pc),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006773 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006774 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01006775 dex_file_(dex_file),
Vladimir Marko175e7862018-03-27 09:03:13 +00006776 klass_(klass) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01006777 // Referrers class should not need access check. We never inline unverified
6778 // methods so we can't possibly end up in this situation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006779 DCHECK(!is_referrers_class || !needs_access_check);
6780
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006781 SetPackedField<LoadKindField>(
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006782 is_referrers_class ? LoadKind::kReferrersClass : LoadKind::kRuntimeCall);
Vladimir Markoa1de9182016-02-25 11:37:38 +00006783 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
Nicolas Geoffray56876342016-12-16 16:09:08 +00006784 SetPackedFlag<kFlagIsInBootImage>(false);
Vladimir Markoa1de9182016-02-25 11:37:38 +00006785 SetPackedFlag<kFlagGenerateClInitCheck>(false);
Vladimir Marko175e7862018-03-27 09:03:13 +00006786 SetPackedFlag<kFlagValidLoadedClassRTI>(false);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006787 }
6788
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006789 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01006790
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006791 void SetLoadKind(LoadKind load_kind);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006792
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006793 LoadKind GetLoadKind() const {
6794 return GetPackedField<LoadKindField>();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01006795 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006796
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006797 bool HasPcRelativeLoadKind() const {
6798 return GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
6799 GetLoadKind() == LoadKind::kBootImageRelRo ||
Vladimir Marko8f63f102020-09-28 12:10:28 +01006800 GetLoadKind() == LoadKind::kBssEntry ||
6801 GetLoadKind() == LoadKind::kBssEntryPublic ||
6802 GetLoadKind() == LoadKind::kBssEntryPackage;
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006803 }
6804
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006805 bool CanBeMoved() const override { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006806
Yi Kong39402542019-03-24 02:47:16 -07006807 bool InstructionDataEquals(const HInstruction* other) const override;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006808
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006809 size_t ComputeHashCode() const override { return type_index_.index_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006810
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006811 bool CanBeNull() const override { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006812
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006813 bool NeedsEnvironment() const override {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006814 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006815 }
6816
Calin Juravle0ba218d2015-05-19 18:46:01 +01006817 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006818 SetPackedFlag<kFlagGenerateClInitCheck>(generate_clinit_check);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006819 }
6820
6821 bool CanCallRuntime() const {
Vladimir Marko41559982017-01-06 14:04:23 +00006822 return NeedsAccessCheck() ||
6823 MustGenerateClinitCheck() ||
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006824 GetLoadKind() == LoadKind::kRuntimeCall ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006825 GetLoadKind() == LoadKind::kBssEntry;
Calin Juravle98893e12015-10-02 21:05:03 +01006826 }
6827
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006828 bool CanThrow() const override {
Vladimir Marko41559982017-01-06 14:04:23 +00006829 return NeedsAccessCheck() ||
6830 MustGenerateClinitCheck() ||
6831 // If the class is in the boot image, the lookup in the runtime call cannot throw.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006832 ((GetLoadKind() == LoadKind::kRuntimeCall ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006833 GetLoadKind() == LoadKind::kBssEntry) &&
6834 !IsInBootImage());
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006835 }
6836
Calin Juravleacf735c2015-02-12 15:25:22 +00006837 ReferenceTypeInfo GetLoadedClassRTI() {
Vladimir Marko175e7862018-03-27 09:03:13 +00006838 if (GetPackedFlag<kFlagValidLoadedClassRTI>()) {
6839 // Note: The is_exact flag from the return value should not be used.
Andreas Gampe3db70682018-12-26 15:12:03 -08006840 return ReferenceTypeInfo::CreateUnchecked(klass_, /* is_exact= */ true);
Vladimir Marko175e7862018-03-27 09:03:13 +00006841 } else {
6842 return ReferenceTypeInfo::CreateInvalid();
6843 }
Calin Juravleacf735c2015-02-12 15:25:22 +00006844 }
6845
Vladimir Marko175e7862018-03-27 09:03:13 +00006846 // Loaded class RTI is marked as valid by RTP if the klass_ is admissible.
Vladimir Markoffafe8b2021-04-23 10:29:27 +00006847 void SetValidLoadedClassRTI() {
Vladimir Marko175e7862018-03-27 09:03:13 +00006848 DCHECK(klass_ != nullptr);
6849 SetPackedFlag<kFlagValidLoadedClassRTI>(true);
Calin Juravleacf735c2015-02-12 15:25:22 +00006850 }
6851
Andreas Gampea5b09a62016-11-17 15:21:22 -08006852 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006853 const DexFile& GetDexFile() const { return dex_file_; }
6854
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006855 static SideEffects SideEffectsForArchRuntimeCalls() {
6856 return SideEffects::CanTriggerGC();
6857 }
6858
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006859 bool IsReferrersClass() const { return GetLoadKind() == LoadKind::kReferrersClass; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006860 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006861 bool IsInBootImage() const { return GetPackedFlag<kFlagIsInBootImage>(); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006862 bool MustGenerateClinitCheck() const { return GetPackedFlag<kFlagGenerateClInitCheck>(); }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006863
Vladimir Markoa9f303c2018-07-20 16:43:56 +01006864 bool MustResolveTypeOnSlowPath() const {
6865 // Check that this instruction has a slow path.
Vladimir Marko8f63f102020-09-28 12:10:28 +01006866 LoadKind load_kind = GetLoadKind();
6867 DCHECK(load_kind != LoadKind::kRuntimeCall); // kRuntimeCall calls on main path.
6868 bool must_resolve_type_on_slow_path =
6869 load_kind == LoadKind::kBssEntry ||
6870 load_kind == LoadKind::kBssEntryPublic ||
6871 load_kind == LoadKind::kBssEntryPackage;
6872 DCHECK(must_resolve_type_on_slow_path || MustGenerateClinitCheck());
6873 return must_resolve_type_on_slow_path;
Vladimir Markoa9f303c2018-07-20 16:43:56 +01006874 }
6875
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006876 void MarkInBootImage() {
6877 SetPackedFlag<kFlagIsInBootImage>(true);
6878 }
6879
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006880 void AddSpecialInput(HInstruction* special_input);
6881
6882 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01006883 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006884 return ArrayRef<HUserRecord<HInstruction*>>(
6885 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
6886 }
6887
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006888 Handle<mirror::Class> GetClass() const {
6889 return klass_;
6890 }
6891
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006892 DECLARE_INSTRUCTION(LoadClass);
6893
Artem Serovcced8ba2017-07-19 18:18:09 +01006894 protected:
6895 DEFAULT_COPY_CONSTRUCTOR(LoadClass);
6896
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006897 private:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006898 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfGenericPackedBits;
Vladimir Marko41559982017-01-06 14:04:23 +00006899 static constexpr size_t kFlagIsInBootImage = kFlagNeedsAccessCheck + 1;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006900 // Whether this instruction must generate the initialization check.
6901 // Used for code generation.
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006902 static constexpr size_t kFlagGenerateClInitCheck = kFlagIsInBootImage + 1;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006903 static constexpr size_t kFieldLoadKind = kFlagGenerateClInitCheck + 1;
6904 static constexpr size_t kFieldLoadKindSize =
6905 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
Vladimir Marko175e7862018-03-27 09:03:13 +00006906 static constexpr size_t kFlagValidLoadedClassRTI = kFieldLoadKind + kFieldLoadKindSize;
6907 static constexpr size_t kNumberOfLoadClassPackedBits = kFlagValidLoadedClassRTI + 1;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006908 static_assert(kNumberOfLoadClassPackedBits < kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006909 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
6910
6911 static bool HasTypeReference(LoadKind load_kind) {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006912 return load_kind == LoadKind::kReferrersClass ||
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006913 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006914 load_kind == LoadKind::kBssEntry ||
Vladimir Marko8f63f102020-09-28 12:10:28 +01006915 load_kind == LoadKind::kBssEntryPublic ||
6916 load_kind == LoadKind::kBssEntryPackage ||
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006917 load_kind == LoadKind::kRuntimeCall;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006918 }
6919
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006920 void SetLoadKindInternal(LoadKind load_kind);
6921
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006922 // The special input is the HCurrentMethod for kRuntimeCall or kReferrersClass.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006923 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko8f63f102020-09-28 12:10:28 +01006924 // for PC-relative loads, i.e. kBssEntry* or kBootImageLinkTimePcRelative.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006925 HUserRecord<HInstruction*> special_input_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006926
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006927 // A type index and dex file where the class can be accessed. The dex file can be:
6928 // - The compiling method's dex file if the class is defined there too.
6929 // - The compiling method's dex file if the class is referenced there.
6930 // - The dex file where the class is defined. When the load kind can only be
Vladimir Marko8f63f102020-09-28 12:10:28 +01006931 // kBssEntry* or kRuntimeCall, we cannot emit code for this `HLoadClass`.
Andreas Gampea5b09a62016-11-17 15:21:22 -08006932 const dex::TypeIndex type_index_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006933 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006934
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006935 Handle<mirror::Class> klass_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006936};
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006937std::ostream& operator<<(std::ostream& os, HLoadClass::LoadKind rhs);
6938
6939// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
Vladimir Marko28e012a2017-12-07 11:22:59 +00006940inline void HLoadClass::SetLoadKind(LoadKind load_kind) {
6941 // The load kind should be determined before inserting the instruction to the graph.
6942 DCHECK(GetBlock() == nullptr);
6943 DCHECK(GetEnvironment() == nullptr);
6944 SetPackedField<LoadKindField>(load_kind);
6945 if (load_kind != LoadKind::kRuntimeCall && load_kind != LoadKind::kReferrersClass) {
6946 special_input_ = HUserRecord<HInstruction*>(nullptr);
6947 }
6948 if (!NeedsEnvironment()) {
6949 SetSideEffects(SideEffects::None());
6950 }
6951}
6952
6953// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006954inline void HLoadClass::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07006955 // The special input is used for PC-relative loads on some architectures,
6956 // including literal pool loads, which are PC-relative too.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006957 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006958 GetLoadKind() == LoadKind::kBootImageRelRo ||
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006959 GetLoadKind() == LoadKind::kBssEntry ||
Vladimir Marko8f63f102020-09-28 12:10:28 +01006960 GetLoadKind() == LoadKind::kBssEntryPublic ||
6961 GetLoadKind() == LoadKind::kBssEntryPackage ||
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006962 GetLoadKind() == LoadKind::kJitBootImageAddress) << GetLoadKind();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006963 DCHECK(special_input_.GetInstruction() == nullptr);
6964 special_input_ = HUserRecord<HInstruction*>(special_input);
6965 special_input->AddUseAt(this, 0);
6966}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006967
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006968class HLoadString final : public HInstruction {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006969 public:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006970 // Determines how to load the String.
6971 enum class LoadKind {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006972 // Use PC-relative boot image String* address that will be known at link time.
Vladimir Marko764d4542017-05-16 10:31:41 +01006973 // Used for boot image strings referenced by boot image code.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006974 kBootImageLinkTimePcRelative,
6975
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006976 // Load from an entry in the .data.bimg.rel.ro using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006977 // Used for boot image strings referenced by apps in AOT-compiled code.
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006978 kBootImageRelRo,
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006979
Vladimir Markoaad75c62016-10-03 08:46:48 +00006980 // Load from an entry in the .bss section using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006981 // Used for strings outside boot image referenced by AOT-compiled app and boot image code.
Vladimir Markoaad75c62016-10-03 08:46:48 +00006982 kBssEntry,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006983
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006984 // Use a known boot image String* address, embedded in the code by the codegen.
6985 // Used for boot image strings referenced by apps in JIT-compiled code.
6986 kJitBootImageAddress,
6987
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006988 // Load from the root table associated with the JIT compiled method.
6989 kJitTableAddress,
6990
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006991 // Load using a simple runtime call. This is the fall-back load kind when
6992 // the codegen is unable to use another appropriate kind.
6993 kRuntimeCall,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006994
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006995 kLast = kRuntimeCall,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006996 };
6997
Nicolas Geoffray917d0162015-11-24 18:25:35 +00006998 HLoadString(HCurrentMethod* current_method,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006999 dex::StringIndex string_index,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007000 const DexFile& dex_file,
7001 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01007002 : HInstruction(kLoadString,
7003 DataType::Type::kReference,
7004 SideEffectsForArchRuntimeCalls(),
7005 dex_pc),
Vladimir Marko372f10e2016-05-17 16:30:10 +01007006 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007007 string_index_(string_index),
7008 dex_file_(dex_file) {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007009 SetPackedField<LoadKindField>(LoadKind::kRuntimeCall);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01007010 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00007011
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007012 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01007013
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007014 void SetLoadKind(LoadKind load_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007015
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007016 LoadKind GetLoadKind() const {
7017 return GetPackedField<LoadKindField>();
7018 }
7019
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007020 bool HasPcRelativeLoadKind() const {
7021 return GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
7022 GetLoadKind() == LoadKind::kBootImageRelRo ||
7023 GetLoadKind() == LoadKind::kBssEntry;
7024 }
7025
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007026 const DexFile& GetDexFile() const {
7027 return dex_file_;
7028 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007029
Andreas Gampe8a0128a2016-11-28 07:38:35 -08007030 dex::StringIndex GetStringIndex() const {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007031 return string_index_;
7032 }
7033
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007034 Handle<mirror::String> GetString() const {
7035 return string_;
7036 }
7037
7038 void SetString(Handle<mirror::String> str) {
7039 string_ = str;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007040 }
7041
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007042 bool CanBeMoved() const override { return true; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00007043
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007044 bool InstructionDataEquals(const HInstruction* other) const override;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00007045
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007046 size_t ComputeHashCode() const override { return string_index_.index_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00007047
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007048 // Will call the runtime if we need to load the string through
7049 // the dex cache and the string is not guaranteed to be there yet.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007050 bool NeedsEnvironment() const override {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007051 LoadKind load_kind = GetLoadKind();
Vladimir Marko764d4542017-05-16 10:31:41 +01007052 if (load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007053 load_kind == LoadKind::kBootImageRelRo ||
Vladimir Marko8e524ad2018-07-13 10:27:43 +01007054 load_kind == LoadKind::kJitBootImageAddress ||
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007055 load_kind == LoadKind::kJitTableAddress) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007056 return false;
7057 }
Vladimir Marko4d1be4922017-01-06 14:43:11 +00007058 return true;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007059 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00007060
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007061 bool CanBeNull() const override { return false; }
7062 bool CanThrow() const override { return NeedsEnvironment(); }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00007063
Alexandre Rames78e3ef62015-08-12 13:43:29 +01007064 static SideEffects SideEffectsForArchRuntimeCalls() {
7065 return SideEffects::CanTriggerGC();
7066 }
7067
Vladimir Marko372f10e2016-05-17 16:30:10 +01007068 void AddSpecialInput(HInstruction* special_input);
7069
7070 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01007071 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Vladimir Marko372f10e2016-05-17 16:30:10 +01007072 return ArrayRef<HUserRecord<HInstruction*>>(
7073 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007074 }
7075
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00007076 DECLARE_INSTRUCTION(LoadString);
7077
Artem Serovcced8ba2017-07-19 18:18:09 +01007078 protected:
7079 DEFAULT_COPY_CONSTRUCTOR(LoadString);
7080
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00007081 private:
Vladimir Marko4d1be4922017-01-06 14:43:11 +00007082 static constexpr size_t kFieldLoadKind = kNumberOfGenericPackedBits;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007083 static constexpr size_t kFieldLoadKindSize =
7084 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
7085 static constexpr size_t kNumberOfLoadStringPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00007086 static_assert(kNumberOfLoadStringPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007087 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00007088
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007089 void SetLoadKindInternal(LoadKind load_kind);
7090
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007091 // The special input is the HCurrentMethod for kRuntimeCall.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007092 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko48886c22017-01-06 11:45:47 +00007093 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Marko372f10e2016-05-17 16:30:10 +01007094 HUserRecord<HInstruction*> special_input_;
7095
Andreas Gampe8a0128a2016-11-28 07:38:35 -08007096 dex::StringIndex string_index_;
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007097 const DexFile& dex_file_;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007098
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007099 Handle<mirror::String> string_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00007100};
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007101std::ostream& operator<<(std::ostream& os, HLoadString::LoadKind rhs);
7102
7103// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
Vladimir Marko28e012a2017-12-07 11:22:59 +00007104inline void HLoadString::SetLoadKind(LoadKind load_kind) {
7105 // The load kind should be determined before inserting the instruction to the graph.
7106 DCHECK(GetBlock() == nullptr);
7107 DCHECK(GetEnvironment() == nullptr);
7108 DCHECK_EQ(GetLoadKind(), LoadKind::kRuntimeCall);
7109 SetPackedField<LoadKindField>(load_kind);
7110 if (load_kind != LoadKind::kRuntimeCall) {
7111 special_input_ = HUserRecord<HInstruction*>(nullptr);
7112 }
7113 if (!NeedsEnvironment()) {
7114 SetSideEffects(SideEffects::None());
7115 }
7116}
7117
7118// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007119inline void HLoadString::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007120 // The special input is used for PC-relative loads on some architectures,
7121 // including literal pool loads, which are PC-relative too.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007122 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007123 GetLoadKind() == LoadKind::kBootImageRelRo ||
Vladimir Marko8e524ad2018-07-13 10:27:43 +01007124 GetLoadKind() == LoadKind::kBssEntry ||
7125 GetLoadKind() == LoadKind::kJitBootImageAddress) << GetLoadKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01007126 // HLoadString::GetInputRecords() returns an empty array at this point,
7127 // so use the GetInputRecords() from the base class to set the input record.
7128 DCHECK(special_input_.GetInstruction() == nullptr);
7129 special_input_ = HUserRecord<HInstruction*>(special_input);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007130 special_input->AddUseAt(this, 0);
7131}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00007132
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007133class HLoadMethodHandle final : public HInstruction {
Orion Hodsondbaa5c72018-05-10 08:22:46 +01007134 public:
7135 HLoadMethodHandle(HCurrentMethod* current_method,
Orion Hodson4c8e12e2018-05-18 08:33:20 +01007136 uint16_t method_handle_idx,
7137 const DexFile& dex_file,
7138 uint32_t dex_pc)
Orion Hodsondbaa5c72018-05-10 08:22:46 +01007139 : HInstruction(kLoadMethodHandle,
7140 DataType::Type::kReference,
7141 SideEffectsForArchRuntimeCalls(),
7142 dex_pc),
7143 special_input_(HUserRecord<HInstruction*>(current_method)),
7144 method_handle_idx_(method_handle_idx),
7145 dex_file_(dex_file) {
7146 }
7147
7148 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01007149 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Orion Hodsondbaa5c72018-05-10 08:22:46 +01007150 return ArrayRef<HUserRecord<HInstruction*>>(
7151 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
7152 }
7153
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007154 bool IsClonable() const override { return true; }
Orion Hodsondbaa5c72018-05-10 08:22:46 +01007155
7156 uint16_t GetMethodHandleIndex() const { return method_handle_idx_; }
7157
7158 const DexFile& GetDexFile() const { return dex_file_; }
7159
7160 static SideEffects SideEffectsForArchRuntimeCalls() {
7161 return SideEffects::CanTriggerGC();
7162 }
7163
7164 DECLARE_INSTRUCTION(LoadMethodHandle);
7165
7166 protected:
7167 DEFAULT_COPY_CONSTRUCTOR(LoadMethodHandle);
7168
7169 private:
7170 // The special input is the HCurrentMethod for kRuntimeCall.
7171 HUserRecord<HInstruction*> special_input_;
7172
7173 const uint16_t method_handle_idx_;
7174 const DexFile& dex_file_;
7175};
7176
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007177class HLoadMethodType final : public HInstruction {
Orion Hodson18259d72018-04-12 11:18:23 +01007178 public:
7179 HLoadMethodType(HCurrentMethod* current_method,
Orion Hodson06d10a72018-05-14 08:53:38 +01007180 dex::ProtoIndex proto_index,
Orion Hodson18259d72018-04-12 11:18:23 +01007181 const DexFile& dex_file,
7182 uint32_t dex_pc)
7183 : HInstruction(kLoadMethodType,
7184 DataType::Type::kReference,
7185 SideEffectsForArchRuntimeCalls(),
7186 dex_pc),
7187 special_input_(HUserRecord<HInstruction*>(current_method)),
Orion Hodson06d10a72018-05-14 08:53:38 +01007188 proto_index_(proto_index),
Orion Hodson18259d72018-04-12 11:18:23 +01007189 dex_file_(dex_file) {
7190 }
7191
7192 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01007193 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Orion Hodson18259d72018-04-12 11:18:23 +01007194 return ArrayRef<HUserRecord<HInstruction*>>(
7195 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
7196 }
7197
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007198 bool IsClonable() const override { return true; }
Orion Hodson18259d72018-04-12 11:18:23 +01007199
Orion Hodson06d10a72018-05-14 08:53:38 +01007200 dex::ProtoIndex GetProtoIndex() const { return proto_index_; }
Orion Hodson18259d72018-04-12 11:18:23 +01007201
7202 const DexFile& GetDexFile() const { return dex_file_; }
7203
7204 static SideEffects SideEffectsForArchRuntimeCalls() {
7205 return SideEffects::CanTriggerGC();
7206 }
7207
7208 DECLARE_INSTRUCTION(LoadMethodType);
7209
7210 protected:
7211 DEFAULT_COPY_CONSTRUCTOR(LoadMethodType);
7212
7213 private:
7214 // The special input is the HCurrentMethod for kRuntimeCall.
7215 HUserRecord<HInstruction*> special_input_;
7216
Orion Hodson06d10a72018-05-14 08:53:38 +01007217 const dex::ProtoIndex proto_index_;
Orion Hodson18259d72018-04-12 11:18:23 +01007218 const DexFile& dex_file_;
7219};
7220
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007221/**
7222 * Performs an initialization check on its Class object input.
7223 */
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007224class HClinitCheck final : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007225 public:
Roland Levillain3887c462015-08-12 18:15:42 +01007226 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07007227 : HExpression(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307228 kClinitCheck,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007229 DataType::Type::kReference,
Mingyao Yang217eb062017-12-11 15:20:07 -08007230 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06007231 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007232 SetRawInputAt(0, constant);
7233 }
Artem Serova6e26142018-06-19 14:55:17 +01007234 // TODO: Make ClinitCheck clonable.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007235 bool CanBeMoved() const override { return true; }
7236 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00007237 return true;
7238 }
7239
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007240 bool NeedsEnvironment() const override {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007241 // May call runtime to initialize the class.
7242 return true;
7243 }
7244
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007245 bool CanThrow() const override { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007246
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00007247 HLoadClass* GetLoadClass() const {
7248 DCHECK(InputAt(0)->IsLoadClass());
7249 return InputAt(0)->AsLoadClass();
7250 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007251
7252 DECLARE_INSTRUCTION(ClinitCheck);
7253
Artem Serovcced8ba2017-07-19 18:18:09 +01007254
7255 protected:
7256 DEFAULT_COPY_CONSTRUCTOR(ClinitCheck);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007257};
7258
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007259class HStaticFieldGet final : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007260 public:
7261 HStaticFieldGet(HInstruction* cls,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00007262 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007263 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00007264 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01007265 bool is_volatile,
7266 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07007267 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07007268 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01007269 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307270 : HExpression(kStaticFieldGet,
7271 field_type,
7272 SideEffects::FieldReadOfType(field_type, is_volatile),
7273 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00007274 field_info_(field,
7275 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07007276 field_type,
7277 is_volatile,
7278 field_idx,
7279 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00007280 dex_file) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007281 SetRawInputAt(0, cls);
7282 }
7283
Calin Juravle52c48962014-12-16 17:02:57 +00007284
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007285 bool IsClonable() const override { return true; }
7286 bool CanBeMoved() const override { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00007287
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007288 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01007289 const HStaticFieldGet* other_get = other->AsStaticFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00007290 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007291 }
7292
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007293 size_t ComputeHashCode() const override {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007294 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
7295 }
7296
Alex Light3a73ffb2021-01-25 14:11:05 +00007297 bool IsFieldAccess() const override { return true; }
7298 const FieldInfo& GetFieldInfo() const override { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007299 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007300 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00007301 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007302
Vladimir Marko61b92282017-10-11 13:23:17 +01007303 void SetType(DataType::Type new_type) {
7304 DCHECK(DataType::IsIntegralType(GetType()));
7305 DCHECK(DataType::IsIntegralType(new_type));
7306 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
7307 SetPackedField<TypeField>(new_type);
7308 }
7309
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007310 DECLARE_INSTRUCTION(StaticFieldGet);
7311
Artem Serovcced8ba2017-07-19 18:18:09 +01007312 protected:
7313 DEFAULT_COPY_CONSTRUCTOR(StaticFieldGet);
7314
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007315 private:
7316 const FieldInfo field_info_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007317};
7318
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007319class HStaticFieldSet final : public HExpression<2> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007320 public:
7321 HStaticFieldSet(HInstruction* cls,
7322 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00007323 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007324 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00007325 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01007326 bool is_volatile,
7327 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07007328 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07007329 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01007330 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01007331 : HExpression(kStaticFieldSet,
7332 SideEffects::FieldWriteOfType(field_type, is_volatile),
7333 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00007334 field_info_(field,
7335 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07007336 field_type,
7337 is_volatile,
7338 field_idx,
7339 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00007340 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007341 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007342 SetRawInputAt(0, cls);
7343 SetRawInputAt(1, value);
7344 }
7345
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007346 bool IsClonable() const override { return true; }
Alex Light3a73ffb2021-01-25 14:11:05 +00007347 bool IsFieldAccess() const override { return true; }
7348 const FieldInfo& GetFieldInfo() const override { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007349 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007350 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00007351 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007352
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00007353 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00007354 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
7355 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00007356
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007357 DECLARE_INSTRUCTION(StaticFieldSet);
7358
Artem Serovcced8ba2017-07-19 18:18:09 +01007359 protected:
7360 DEFAULT_COPY_CONSTRUCTOR(StaticFieldSet);
7361
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007362 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007363 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
7364 static constexpr size_t kNumberOfStaticFieldSetPackedBits = kFlagValueCanBeNull + 1;
7365 static_assert(kNumberOfStaticFieldSetPackedBits <= kMaxNumberOfPackedBits,
7366 "Too many packed fields.");
7367
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007368 const FieldInfo field_info_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007369};
7370
Vladimir Marko552a1342017-10-31 10:56:47 +00007371class HStringBuilderAppend final : public HVariableInputSizeInstruction {
7372 public:
7373 HStringBuilderAppend(HIntConstant* format,
7374 uint32_t number_of_arguments,
7375 ArenaAllocator* allocator,
7376 uint32_t dex_pc)
7377 : HVariableInputSizeInstruction(
7378 kStringBuilderAppend,
7379 DataType::Type::kReference,
7380 // The runtime call may read memory from inputs. It never writes outside
7381 // of the newly allocated result object (or newly allocated helper objects).
7382 SideEffects::AllReads().Union(SideEffects::CanTriggerGC()),
7383 dex_pc,
7384 allocator,
7385 number_of_arguments + /* format */ 1u,
7386 kArenaAllocInvokeInputs) {
7387 DCHECK_GE(number_of_arguments, 1u); // There must be something to append.
7388 SetRawInputAt(FormatIndex(), format);
7389 }
7390
7391 void SetArgumentAt(size_t index, HInstruction* argument) {
7392 DCHECK_LE(index, GetNumberOfArguments());
7393 SetRawInputAt(index, argument);
7394 }
7395
7396 // Return the number of arguments, excluding the format.
7397 size_t GetNumberOfArguments() const {
7398 DCHECK_GE(InputCount(), 1u);
7399 return InputCount() - 1u;
7400 }
7401
7402 size_t FormatIndex() const {
7403 return GetNumberOfArguments();
7404 }
7405
7406 HIntConstant* GetFormat() {
7407 return InputAt(FormatIndex())->AsIntConstant();
7408 }
7409
7410 bool NeedsEnvironment() const override { return true; }
7411
7412 bool CanThrow() const override { return true; }
7413
Vladimir Markob1fe5e12020-03-10 14:30:49 +00007414 bool CanBeNull() const override { return false; }
7415
Vladimir Marko552a1342017-10-31 10:56:47 +00007416 DECLARE_INSTRUCTION(StringBuilderAppend);
7417
7418 protected:
7419 DEFAULT_COPY_CONSTRUCTOR(StringBuilderAppend);
7420};
7421
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007422class HUnresolvedInstanceFieldGet final : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01007423 public:
7424 HUnresolvedInstanceFieldGet(HInstruction* obj,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007425 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01007426 uint32_t field_index,
7427 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307428 : HExpression(kUnresolvedInstanceFieldGet,
7429 field_type,
7430 SideEffects::AllExceptGCDependency(),
7431 dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01007432 field_index_(field_index) {
7433 SetRawInputAt(0, obj);
7434 }
7435
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007436 bool IsClonable() const override { return true; }
7437 bool NeedsEnvironment() const override { return true; }
7438 bool CanThrow() const override { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01007439
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007440 DataType::Type GetFieldType() const { return GetType(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01007441 uint32_t GetFieldIndex() const { return field_index_; }
7442
7443 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
7444
Artem Serovcced8ba2017-07-19 18:18:09 +01007445 protected:
7446 DEFAULT_COPY_CONSTRUCTOR(UnresolvedInstanceFieldGet);
7447
Calin Juravlee460d1d2015-09-29 04:52:17 +01007448 private:
7449 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01007450};
7451
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007452class HUnresolvedInstanceFieldSet final : public HExpression<2> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01007453 public:
7454 HUnresolvedInstanceFieldSet(HInstruction* obj,
7455 HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007456 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01007457 uint32_t field_index,
7458 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01007459 : HExpression(kUnresolvedInstanceFieldSet, SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01007460 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007461 SetPackedField<FieldTypeField>(field_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007462 DCHECK_EQ(DataType::Kind(field_type), DataType::Kind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01007463 SetRawInputAt(0, obj);
7464 SetRawInputAt(1, value);
7465 }
7466
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007467 bool IsClonable() const override { return true; }
7468 bool NeedsEnvironment() const override { return true; }
7469 bool CanThrow() const override { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01007470
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007471 DataType::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01007472 uint32_t GetFieldIndex() const { return field_index_; }
7473
7474 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
7475
Artem Serovcced8ba2017-07-19 18:18:09 +01007476 protected:
7477 DEFAULT_COPY_CONSTRUCTOR(UnresolvedInstanceFieldSet);
7478
Calin Juravlee460d1d2015-09-29 04:52:17 +01007479 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007480 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
7481 static constexpr size_t kFieldFieldTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007482 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00007483 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
7484 kFieldFieldType + kFieldFieldTypeSize;
7485 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
7486 "Too many packed fields.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007487 using FieldTypeField = BitField<DataType::Type, kFieldFieldType, kFieldFieldTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00007488
Calin Juravlee460d1d2015-09-29 04:52:17 +01007489 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01007490};
7491
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007492class HUnresolvedStaticFieldGet final : public HExpression<0> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01007493 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007494 HUnresolvedStaticFieldGet(DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01007495 uint32_t field_index,
7496 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307497 : HExpression(kUnresolvedStaticFieldGet,
7498 field_type,
7499 SideEffects::AllExceptGCDependency(),
7500 dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01007501 field_index_(field_index) {
7502 }
7503
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007504 bool IsClonable() const override { return true; }
7505 bool NeedsEnvironment() const override { return true; }
7506 bool CanThrow() const override { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01007507
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007508 DataType::Type GetFieldType() const { return GetType(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01007509 uint32_t GetFieldIndex() const { return field_index_; }
7510
7511 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
7512
Artem Serovcced8ba2017-07-19 18:18:09 +01007513 protected:
7514 DEFAULT_COPY_CONSTRUCTOR(UnresolvedStaticFieldGet);
7515
Calin Juravlee460d1d2015-09-29 04:52:17 +01007516 private:
7517 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01007518};
7519
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007520class HUnresolvedStaticFieldSet final : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01007521 public:
7522 HUnresolvedStaticFieldSet(HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007523 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01007524 uint32_t field_index,
7525 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01007526 : HExpression(kUnresolvedStaticFieldSet, SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01007527 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007528 SetPackedField<FieldTypeField>(field_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007529 DCHECK_EQ(DataType::Kind(field_type), DataType::Kind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01007530 SetRawInputAt(0, value);
7531 }
7532
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007533 bool IsClonable() const override { return true; }
7534 bool NeedsEnvironment() const override { return true; }
7535 bool CanThrow() const override { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01007536
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007537 DataType::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01007538 uint32_t GetFieldIndex() const { return field_index_; }
7539
7540 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
7541
Artem Serovcced8ba2017-07-19 18:18:09 +01007542 protected:
7543 DEFAULT_COPY_CONSTRUCTOR(UnresolvedStaticFieldSet);
7544
Calin Juravlee460d1d2015-09-29 04:52:17 +01007545 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007546 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
7547 static constexpr size_t kFieldFieldTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007548 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00007549 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
7550 kFieldFieldType + kFieldFieldTypeSize;
7551 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
7552 "Too many packed fields.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007553 using FieldTypeField = BitField<DataType::Type, kFieldFieldType, kFieldFieldTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00007554
Calin Juravlee460d1d2015-09-29 04:52:17 +01007555 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01007556};
7557
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007558// Implement the move-exception DEX instruction.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007559class HLoadException final : public HExpression<0> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007560 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06007561 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307562 : HExpression(kLoadException, DataType::Type::kReference, SideEffects::None(), dex_pc) {
7563 }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007564
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007565 bool CanBeNull() const override { return false; }
David Brazdilbbd733e2015-08-18 17:48:17 +01007566
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007567 DECLARE_INSTRUCTION(LoadException);
7568
Artem Serovcced8ba2017-07-19 18:18:09 +01007569 protected:
7570 DEFAULT_COPY_CONSTRUCTOR(LoadException);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007571};
7572
David Brazdilcb1c0552015-08-04 16:22:25 +01007573// Implicit part of move-exception which clears thread-local exception storage.
7574// Must not be removed because the runtime expects the TLS to get cleared.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007575class HClearException final : public HExpression<0> {
David Brazdilcb1c0552015-08-04 16:22:25 +01007576 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06007577 explicit HClearException(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01007578 : HExpression(kClearException, SideEffects::AllWrites(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307579 }
David Brazdilcb1c0552015-08-04 16:22:25 +01007580
7581 DECLARE_INSTRUCTION(ClearException);
7582
Artem Serovcced8ba2017-07-19 18:18:09 +01007583 protected:
7584 DEFAULT_COPY_CONSTRUCTOR(ClearException);
David Brazdilcb1c0552015-08-04 16:22:25 +01007585};
7586
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007587class HThrow final : public HExpression<1> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007588 public:
7589 HThrow(HInstruction* exception, uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01007590 : HExpression(kThrow, SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007591 SetRawInputAt(0, exception);
7592 }
7593
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007594 bool IsControlFlow() const override { return true; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007595
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007596 bool NeedsEnvironment() const override { return true; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007597
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007598 bool CanThrow() const override { return true; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007599
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007600 bool AlwaysThrows() const override { return true; }
Aart Bika8b8e9b2018-01-09 11:01:02 -08007601
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007602 DECLARE_INSTRUCTION(Throw);
7603
Artem Serovcced8ba2017-07-19 18:18:09 +01007604 protected:
7605 DEFAULT_COPY_CONSTRUCTOR(Throw);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007606};
7607
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007608/**
7609 * Implementation strategies for the code generator of a HInstanceOf
7610 * or `HCheckCast`.
7611 */
Vladimir Marko9974e3c2020-06-10 16:27:06 +01007612enum class TypeCheckKind { // private marker to avoid generate-operator-out.py from processing.
Calin Juravle98893e12015-10-02 21:05:03 +01007613 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007614 kExactCheck, // Can do a single class compare.
7615 kClassHierarchyCheck, // Can just walk the super class chain.
7616 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
7617 kInterfaceCheck, // No optimization yet when checking against an interface.
7618 kArrayObjectCheck, // Can just check if the array is not primitive.
Vladimir Markoa1de9182016-02-25 11:37:38 +00007619 kArrayCheck, // No optimization yet when checking against a generic array.
Vladimir Marko175e7862018-03-27 09:03:13 +00007620 kBitstringCheck, // Compare the type check bitstring.
Vladimir Markoa1de9182016-02-25 11:37:38 +00007621 kLast = kArrayCheck
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007622};
7623
Roland Levillain86503782016-02-11 19:07:30 +00007624std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs);
7625
Vladimir Marko175e7862018-03-27 09:03:13 +00007626// Note: HTypeCheckInstruction is just a helper class, not an abstract instruction with an
7627// `IsTypeCheckInstruction()`. (New virtual methods in the HInstruction class have a high cost.)
7628class HTypeCheckInstruction : public HVariableInputSizeInstruction {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007629 public:
Vladimir Marko175e7862018-03-27 09:03:13 +00007630 HTypeCheckInstruction(InstructionKind kind,
Vladimir Markobd785672018-05-03 17:09:09 +01007631 DataType::Type type,
Vladimir Marko175e7862018-03-27 09:03:13 +00007632 HInstruction* object,
7633 HInstruction* target_class_or_null,
7634 TypeCheckKind check_kind,
7635 Handle<mirror::Class> klass,
7636 uint32_t dex_pc,
7637 ArenaAllocator* allocator,
7638 HIntConstant* bitstring_path_to_root,
7639 HIntConstant* bitstring_mask,
7640 SideEffects side_effects)
7641 : HVariableInputSizeInstruction(
7642 kind,
Vladimir Markobd785672018-05-03 17:09:09 +01007643 type,
Vladimir Marko175e7862018-03-27 09:03:13 +00007644 side_effects,
7645 dex_pc,
7646 allocator,
Andreas Gampe3db70682018-12-26 15:12:03 -08007647 /* number_of_inputs= */ check_kind == TypeCheckKind::kBitstringCheck ? 4u : 2u,
Vladimir Marko175e7862018-03-27 09:03:13 +00007648 kArenaAllocTypeCheckInputs),
7649 klass_(klass) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007650 SetPackedField<TypeCheckKindField>(check_kind);
7651 SetPackedFlag<kFlagMustDoNullCheck>(true);
Vladimir Marko175e7862018-03-27 09:03:13 +00007652 SetPackedFlag<kFlagValidTargetClassRTI>(false);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007653 SetRawInputAt(0, object);
Vladimir Marko175e7862018-03-27 09:03:13 +00007654 SetRawInputAt(1, target_class_or_null);
7655 DCHECK_EQ(check_kind == TypeCheckKind::kBitstringCheck, bitstring_path_to_root != nullptr);
7656 DCHECK_EQ(check_kind == TypeCheckKind::kBitstringCheck, bitstring_mask != nullptr);
7657 if (check_kind == TypeCheckKind::kBitstringCheck) {
7658 DCHECK(target_class_or_null->IsNullConstant());
7659 SetRawInputAt(2, bitstring_path_to_root);
7660 SetRawInputAt(3, bitstring_mask);
7661 } else {
7662 DCHECK(target_class_or_null->IsLoadClass());
7663 }
Vladimir Marko87584542017-12-12 17:47:52 +00007664 }
7665
7666 HLoadClass* GetTargetClass() const {
Vladimir Marko175e7862018-03-27 09:03:13 +00007667 DCHECK_NE(GetTypeCheckKind(), TypeCheckKind::kBitstringCheck);
Vladimir Marko87584542017-12-12 17:47:52 +00007668 HInstruction* load_class = InputAt(1);
7669 DCHECK(load_class->IsLoadClass());
7670 return load_class->AsLoadClass();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007671 }
7672
Vladimir Marko175e7862018-03-27 09:03:13 +00007673 uint32_t GetBitstringPathToRoot() const {
7674 DCHECK_EQ(GetTypeCheckKind(), TypeCheckKind::kBitstringCheck);
7675 HInstruction* path_to_root = InputAt(2);
7676 DCHECK(path_to_root->IsIntConstant());
7677 return static_cast<uint32_t>(path_to_root->AsIntConstant()->GetValue());
7678 }
7679
7680 uint32_t GetBitstringMask() const {
7681 DCHECK_EQ(GetTypeCheckKind(), TypeCheckKind::kBitstringCheck);
7682 HInstruction* mask = InputAt(3);
7683 DCHECK(mask->IsIntConstant());
7684 return static_cast<uint32_t>(mask->AsIntConstant()->GetValue());
7685 }
7686
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007687 bool IsClonable() const override { return true; }
7688 bool CanBeMoved() const override { return true; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007689
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007690 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Marko175e7862018-03-27 09:03:13 +00007691 DCHECK(other->IsInstanceOf() || other->IsCheckCast()) << other->DebugName();
7692 return GetPackedFields() == down_cast<const HTypeCheckInstruction*>(other)->GetPackedFields();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007693 }
7694
Andreas Gampe3fbd3ad2018-03-26 21:14:46 +00007695 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
7696 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
7697 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
7698 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
7699
Vladimir Marko175e7862018-03-27 09:03:13 +00007700 ReferenceTypeInfo GetTargetClassRTI() {
7701 if (GetPackedFlag<kFlagValidTargetClassRTI>()) {
7702 // Note: The is_exact flag from the return value should not be used.
Andreas Gampe3db70682018-12-26 15:12:03 -08007703 return ReferenceTypeInfo::CreateUnchecked(klass_, /* is_exact= */ true);
Vladimir Marko175e7862018-03-27 09:03:13 +00007704 } else {
7705 return ReferenceTypeInfo::CreateInvalid();
7706 }
7707 }
7708
7709 // Target class RTI is marked as valid by RTP if the klass_ is admissible.
Vladimir Markoffafe8b2021-04-23 10:29:27 +00007710 void SetValidTargetClassRTI() {
Vladimir Marko175e7862018-03-27 09:03:13 +00007711 DCHECK(klass_ != nullptr);
7712 SetPackedFlag<kFlagValidTargetClassRTI>(true);
7713 }
7714
7715 Handle<mirror::Class> GetClass() const {
7716 return klass_;
7717 }
7718
7719 protected:
7720 DEFAULT_COPY_CONSTRUCTOR(TypeCheckInstruction);
7721
7722 private:
7723 static constexpr size_t kFieldTypeCheckKind = kNumberOfGenericPackedBits;
7724 static constexpr size_t kFieldTypeCheckKindSize =
7725 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
7726 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
7727 static constexpr size_t kFlagValidTargetClassRTI = kFlagMustDoNullCheck + 1;
7728 static constexpr size_t kNumberOfInstanceOfPackedBits = kFlagValidTargetClassRTI + 1;
7729 static_assert(kNumberOfInstanceOfPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
7730 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
7731
7732 Handle<mirror::Class> klass_;
7733};
7734
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007735class HInstanceOf final : public HTypeCheckInstruction {
Vladimir Marko175e7862018-03-27 09:03:13 +00007736 public:
7737 HInstanceOf(HInstruction* object,
7738 HInstruction* target_class_or_null,
7739 TypeCheckKind check_kind,
7740 Handle<mirror::Class> klass,
7741 uint32_t dex_pc,
7742 ArenaAllocator* allocator,
7743 HIntConstant* bitstring_path_to_root,
7744 HIntConstant* bitstring_mask)
7745 : HTypeCheckInstruction(kInstanceOf,
Vladimir Markobd785672018-05-03 17:09:09 +01007746 DataType::Type::kBool,
Vladimir Marko175e7862018-03-27 09:03:13 +00007747 object,
7748 target_class_or_null,
7749 check_kind,
7750 klass,
7751 dex_pc,
7752 allocator,
7753 bitstring_path_to_root,
7754 bitstring_mask,
7755 SideEffectsForArchRuntimeCalls(check_kind)) {}
7756
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007757 bool IsClonable() const override { return true; }
Artem Serova6e26142018-06-19 14:55:17 +01007758
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007759 bool NeedsEnvironment() const override {
Vladimir Marko175e7862018-03-27 09:03:13 +00007760 return CanCallRuntime(GetTypeCheckKind());
7761 }
7762
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00007763 static bool CanCallRuntime(TypeCheckKind check_kind) {
Vladimir Marko54f4fbd2020-02-12 10:52:22 +00007764 // TODO: Re-evaluate now that mips codegen has been removed.
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00007765 return check_kind != TypeCheckKind::kExactCheck;
7766 }
7767
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007768 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00007769 return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01007770 }
7771
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007772 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007773
Artem Serovcced8ba2017-07-19 18:18:09 +01007774 protected:
7775 DEFAULT_COPY_CONSTRUCTOR(InstanceOf);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007776};
7777
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007778class HBoundType final : public HExpression<1> {
Calin Juravleb1498f62015-02-16 13:13:29 +00007779 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -07007780 explicit HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307781 : HExpression(kBoundType, DataType::Type::kReference, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00007782 upper_bound_(ReferenceTypeInfo::CreateInvalid()) {
7783 SetPackedFlag<kFlagUpperCanBeNull>(true);
7784 SetPackedFlag<kFlagCanBeNull>(true);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007785 DCHECK_EQ(input->GetType(), DataType::Type::kReference);
Calin Juravleb1498f62015-02-16 13:13:29 +00007786 SetRawInputAt(0, input);
7787 }
7788
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007789 bool InstructionDataEquals(const HInstruction* other) const override;
7790 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01007791
David Brazdilf5552582015-12-27 13:36:12 +00007792 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007793 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00007794 bool GetUpperCanBeNull() const { return GetPackedFlag<kFlagUpperCanBeNull>(); }
David Brazdilf5552582015-12-27 13:36:12 +00007795 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00007796
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007797 void SetCanBeNull(bool can_be_null) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007798 DCHECK(GetUpperCanBeNull() || !can_be_null);
7799 SetPackedFlag<kFlagCanBeNull>(can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00007800 }
7801
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007802 bool CanBeNull() const override { return GetPackedFlag<kFlagCanBeNull>(); }
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007803
Calin Juravleb1498f62015-02-16 13:13:29 +00007804 DECLARE_INSTRUCTION(BoundType);
7805
Artem Serovcced8ba2017-07-19 18:18:09 +01007806 protected:
7807 DEFAULT_COPY_CONSTRUCTOR(BoundType);
7808
Calin Juravleb1498f62015-02-16 13:13:29 +00007809 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007810 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
7811 // is false then CanBeNull() cannot be true).
Vladimir Markobd785672018-05-03 17:09:09 +01007812 static constexpr size_t kFlagUpperCanBeNull = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00007813 static constexpr size_t kFlagCanBeNull = kFlagUpperCanBeNull + 1;
7814 static constexpr size_t kNumberOfBoundTypePackedBits = kFlagCanBeNull + 1;
7815 static_assert(kNumberOfBoundTypePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
7816
Calin Juravleb1498f62015-02-16 13:13:29 +00007817 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007818 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
7819 // It is used to bound the type in cases like:
7820 // if (x instanceof ClassX) {
7821 // // uper_bound_ will be ClassX
7822 // }
David Brazdilf5552582015-12-27 13:36:12 +00007823 ReferenceTypeInfo upper_bound_;
Calin Juravleb1498f62015-02-16 13:13:29 +00007824};
7825
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007826class HCheckCast final : public HTypeCheckInstruction {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007827 public:
7828 HCheckCast(HInstruction* object,
Vladimir Marko175e7862018-03-27 09:03:13 +00007829 HInstruction* target_class_or_null,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007830 TypeCheckKind check_kind,
Vladimir Marko175e7862018-03-27 09:03:13 +00007831 Handle<mirror::Class> klass,
7832 uint32_t dex_pc,
7833 ArenaAllocator* allocator,
7834 HIntConstant* bitstring_path_to_root,
7835 HIntConstant* bitstring_mask)
7836 : HTypeCheckInstruction(kCheckCast,
Vladimir Markobd785672018-05-03 17:09:09 +01007837 DataType::Type::kVoid,
Vladimir Marko175e7862018-03-27 09:03:13 +00007838 object,
7839 target_class_or_null,
7840 check_kind,
7841 klass,
7842 dex_pc,
7843 allocator,
7844 bitstring_path_to_root,
7845 bitstring_mask,
7846 SideEffects::CanTriggerGC()) {}
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007847
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007848 bool IsClonable() const override { return true; }
7849 bool NeedsEnvironment() const override {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007850 // Instruction may throw a CheckCastError.
7851 return true;
7852 }
7853
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007854 bool CanThrow() const override { return true; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007855
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007856 DECLARE_INSTRUCTION(CheckCast);
7857
Artem Serovcced8ba2017-07-19 18:18:09 +01007858 protected:
7859 DEFAULT_COPY_CONSTRUCTOR(CheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007860};
7861
Andreas Gampe26de38b2016-07-27 17:53:11 -07007862/**
7863 * @brief Memory barrier types (see "The JSR-133 Cookbook for Compiler Writers").
7864 * @details We define the combined barrier types that are actually required
7865 * by the Java Memory Model, rather than using exactly the terminology from
7866 * the JSR-133 cookbook. These should, in many cases, be replaced by acquire/release
7867 * primitives. Note that the JSR-133 cookbook generally does not deal with
7868 * store atomicity issues, and the recipes there are not always entirely sufficient.
7869 * The current recipe is as follows:
7870 * -# Use AnyStore ~= (LoadStore | StoreStore) ~= release barrier before volatile store.
7871 * -# Use AnyAny barrier after volatile store. (StoreLoad is as expensive.)
7872 * -# Use LoadAny barrier ~= (LoadLoad | LoadStore) ~= acquire barrier after each volatile load.
7873 * -# Use StoreStore barrier after all stores but before return from any constructor whose
7874 * class has final fields.
7875 * -# Use NTStoreStore to order non-temporal stores with respect to all later
7876 * store-to-memory instructions. Only generated together with non-temporal stores.
7877 */
7878enum MemBarrierKind {
7879 kAnyStore,
7880 kLoadAny,
7881 kStoreStore,
7882 kAnyAny,
7883 kNTStoreStore,
7884 kLastBarrierKind = kNTStoreStore
7885};
Vladimir Marko9974e3c2020-06-10 16:27:06 +01007886std::ostream& operator<<(std::ostream& os, MemBarrierKind kind);
Andreas Gampe26de38b2016-07-27 17:53:11 -07007887
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007888class HMemoryBarrier final : public HExpression<0> {
Calin Juravle27df7582015-04-17 19:12:31 +01007889 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06007890 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01007891 : HExpression(kMemoryBarrier,
7892 SideEffects::AllWritesAndReads(), // Assume write/read on all fields/arrays.
7893 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007894 SetPackedField<BarrierKindField>(barrier_kind);
7895 }
Calin Juravle27df7582015-04-17 19:12:31 +01007896
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007897 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01007898
Vladimir Markoa1de9182016-02-25 11:37:38 +00007899 MemBarrierKind GetBarrierKind() { return GetPackedField<BarrierKindField>(); }
Calin Juravle27df7582015-04-17 19:12:31 +01007900
7901 DECLARE_INSTRUCTION(MemoryBarrier);
7902
Artem Serovcced8ba2017-07-19 18:18:09 +01007903 protected:
7904 DEFAULT_COPY_CONSTRUCTOR(MemoryBarrier);
7905
Calin Juravle27df7582015-04-17 19:12:31 +01007906 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007907 static constexpr size_t kFieldBarrierKind = HInstruction::kNumberOfGenericPackedBits;
7908 static constexpr size_t kFieldBarrierKindSize =
7909 MinimumBitsToStore(static_cast<size_t>(kLastBarrierKind));
7910 static constexpr size_t kNumberOfMemoryBarrierPackedBits =
7911 kFieldBarrierKind + kFieldBarrierKindSize;
7912 static_assert(kNumberOfMemoryBarrierPackedBits <= kMaxNumberOfPackedBits,
7913 "Too many packed fields.");
7914 using BarrierKindField = BitField<MemBarrierKind, kFieldBarrierKind, kFieldBarrierKindSize>;
Calin Juravle27df7582015-04-17 19:12:31 +01007915};
7916
Igor Murashkind01745e2017-04-05 16:40:31 -07007917// A constructor fence orders all prior stores to fields that could be accessed via a final field of
7918// the specified object(s), with respect to any subsequent store that might "publish"
7919// (i.e. make visible) the specified object to another thread.
7920//
7921// JLS 17.5.1 "Semantics of final fields" states that a freeze action happens
7922// for all final fields (that were set) at the end of the invoked constructor.
7923//
7924// The constructor fence models the freeze actions for the final fields of an object
7925// being constructed (semantically at the end of the constructor). Constructor fences
7926// have a per-object affinity; two separate objects being constructed get two separate
7927// constructor fences.
7928//
7929// (Note: that if calling a super-constructor or forwarding to another constructor,
7930// the freezes would happen at the end of *that* constructor being invoked).
7931//
7932// The memory model guarantees that when the object being constructed is "published" after
7933// constructor completion (i.e. escapes the current thread via a store), then any final field
7934// writes must be observable on other threads (once they observe that publication).
7935//
7936// Further, anything written before the freeze, and read by dereferencing through the final field,
7937// must also be visible (so final object field could itself have an object with non-final fields;
7938// yet the freeze must also extend to them).
7939//
7940// Constructor example:
7941//
7942// class HasFinal {
7943// final int field; Optimizing IR for <init>()V:
7944// HasFinal() {
7945// field = 123; HInstanceFieldSet(this, HasFinal.field, 123)
7946// // freeze(this.field); HConstructorFence(this)
7947// } HReturn
7948// }
7949//
7950// HConstructorFence can serve double duty as a fence for new-instance/new-array allocations of
7951// already-initialized classes; in that case the allocation must act as a "default-initializer"
7952// of the object which effectively writes the class pointer "final field".
7953//
7954// For example, we can model default-initialiation as roughly the equivalent of the following:
7955//
7956// class Object {
7957// private final Class header;
7958// }
7959//
7960// Java code: Optimizing IR:
7961//
7962// T new_instance<T>() {
7963// Object obj = allocate_memory(T.class.size); obj = HInvoke(art_quick_alloc_object, T)
7964// obj.header = T.class; // header write is done by above call.
7965// // freeze(obj.header) HConstructorFence(obj)
7966// return (T)obj;
7967// }
7968//
7969// See also:
Vladimir Markoc1c34522018-10-31 13:56:49 +00007970// * DexCompilationUnit::RequiresConstructorBarrier
Igor Murashkind01745e2017-04-05 16:40:31 -07007971// * QuasiAtomic::ThreadFenceForConstructor
7972//
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007973class HConstructorFence final : public HVariableInputSizeInstruction {
Igor Murashkind01745e2017-04-05 16:40:31 -07007974 // A fence has variable inputs because the inputs can be removed
7975 // after prepare_for_register_allocation phase.
7976 // (TODO: In the future a fence could freeze multiple objects
7977 // after merging two fences together.)
7978 public:
7979 // `fence_object` is the reference that needs to be protected for correct publication.
7980 //
7981 // It makes sense in the following situations:
7982 // * <init> constructors, it's the "this" parameter (i.e. HParameterValue, s.t. IsThis() == true).
7983 // * new-instance-like instructions, it's the return value (i.e. HNewInstance).
7984 //
7985 // After construction the `fence_object` becomes the 0th input.
7986 // This is not an input in a real sense, but just a convenient place to stash the information
7987 // about the associated object.
7988 HConstructorFence(HInstruction* fence_object,
7989 uint32_t dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007990 ArenaAllocator* allocator)
Igor Murashkind01745e2017-04-05 16:40:31 -07007991 // We strongly suspect there is not a more accurate way to describe the fine-grained reordering
7992 // constraints described in the class header. We claim that these SideEffects constraints
7993 // enforce a superset of the real constraints.
7994 //
7995 // The ordering described above is conservatively modeled with SideEffects as follows:
7996 //
7997 // * To prevent reordering of the publication stores:
7998 // ----> "Reads of objects" is the initial SideEffect.
7999 // * For every primitive final field store in the constructor:
8000 // ----> Union that field's type as a read (e.g. "Read of T") into the SideEffect.
8001 // * If there are any stores to reference final fields in the constructor:
8002 // ----> Use a more conservative "AllReads" SideEffect because any stores to any references
8003 // that are reachable from `fence_object` also need to be prevented for reordering
8004 // (and we do not want to do alias analysis to figure out what those stores are).
8005 //
8006 // In the implementation, this initially starts out as an "all reads" side effect; this is an
8007 // even more conservative approach than the one described above, and prevents all of the
8008 // above reordering without analyzing any of the instructions in the constructor.
8009 //
8010 // If in a later phase we discover that there are no writes to reference final fields,
8011 // we can refine the side effect to a smaller set of type reads (see above constraints).
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05308012 : HVariableInputSizeInstruction(kConstructorFence,
8013 SideEffects::AllReads(),
Igor Murashkind01745e2017-04-05 16:40:31 -07008014 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01008015 allocator,
Andreas Gampe3db70682018-12-26 15:12:03 -08008016 /* number_of_inputs= */ 1,
Igor Murashkind01745e2017-04-05 16:40:31 -07008017 kArenaAllocConstructorFenceInputs) {
8018 DCHECK(fence_object != nullptr);
8019 SetRawInputAt(0, fence_object);
8020 }
8021
8022 // The object associated with this constructor fence.
8023 //
8024 // (Note: This will be null after the prepare_for_register_allocation phase,
8025 // as all constructor fence inputs are removed there).
8026 HInstruction* GetFenceObject() const {
8027 return InputAt(0);
8028 }
8029
8030 // Find all the HConstructorFence uses (`fence_use`) for `this` and:
8031 // - Delete `fence_use` from `this`'s use list.
8032 // - Delete `this` from `fence_use`'s inputs list.
8033 // - If the `fence_use` is dead, remove it from the graph.
8034 //
8035 // A fence is considered dead once it no longer has any uses
8036 // and all of the inputs are dead.
8037 //
8038 // This must *not* be called during/after prepare_for_register_allocation,
8039 // because that removes all the inputs to the fences but the fence is actually
8040 // still considered live.
Igor Murashkin6ef45672017-08-08 13:59:55 -07008041 //
8042 // Returns how many HConstructorFence instructions were removed from graph.
8043 static size_t RemoveConstructorFences(HInstruction* instruction);
Igor Murashkind01745e2017-04-05 16:40:31 -07008044
Igor Murashkindd018df2017-08-09 10:38:31 -07008045 // Combine all inputs of `this` and `other` instruction and remove
8046 // `other` from the graph.
8047 //
8048 // Inputs are unique after the merge.
8049 //
8050 // Requirement: `this` must not be the same as `other.
8051 void Merge(HConstructorFence* other);
8052
Igor Murashkin79d8fa72017-04-18 09:37:23 -07008053 // Check if this constructor fence is protecting
8054 // an HNewInstance or HNewArray that is also the immediate
8055 // predecessor of `this`.
8056 //
Igor Murashkindd018df2017-08-09 10:38:31 -07008057 // If `ignore_inputs` is true, then the immediate predecessor doesn't need
8058 // to be one of the inputs of `this`.
8059 //
Igor Murashkin79d8fa72017-04-18 09:37:23 -07008060 // Returns the associated HNewArray or HNewInstance,
8061 // or null otherwise.
Igor Murashkindd018df2017-08-09 10:38:31 -07008062 HInstruction* GetAssociatedAllocation(bool ignore_inputs = false);
Igor Murashkin79d8fa72017-04-18 09:37:23 -07008063
Igor Murashkind01745e2017-04-05 16:40:31 -07008064 DECLARE_INSTRUCTION(ConstructorFence);
8065
Artem Serovcced8ba2017-07-19 18:18:09 +01008066 protected:
8067 DEFAULT_COPY_CONSTRUCTOR(ConstructorFence);
Igor Murashkind01745e2017-04-05 16:40:31 -07008068};
8069
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01008070class HMonitorOperation final : public HExpression<1> {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00008071 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00008072 enum class OperationKind {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00008073 kEnter,
8074 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00008075 kLast = kExit
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00008076 };
8077
8078 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01008079 : HExpression(kMonitorOperation,
8080 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
8081 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00008082 SetPackedField<OperationKindField>(kind);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00008083 SetRawInputAt(0, object);
8084 }
8085
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00008086 // Instruction may go into runtime, so we need an environment.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01008087 bool NeedsEnvironment() const override { return true; }
David Brazdilbff75032015-07-08 17:26:51 +00008088
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01008089 bool CanThrow() const override {
David Brazdilbff75032015-07-08 17:26:51 +00008090 // Verifier guarantees that monitor-exit cannot throw.
8091 // This is important because it allows the HGraphBuilder to remove
8092 // a dead throw-catch loop generated for `synchronized` blocks/methods.
8093 return IsEnter();
8094 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00008095
Vladimir Markoa1de9182016-02-25 11:37:38 +00008096 OperationKind GetOperationKind() const { return GetPackedField<OperationKindField>(); }
8097 bool IsEnter() const { return GetOperationKind() == OperationKind::kEnter; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00008098
8099 DECLARE_INSTRUCTION(MonitorOperation);
8100
Artem Serovcced8ba2017-07-19 18:18:09 +01008101 protected:
8102 DEFAULT_COPY_CONSTRUCTOR(MonitorOperation);
8103
Calin Juravle52c48962014-12-16 17:02:57 +00008104 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00008105 static constexpr size_t kFieldOperationKind = HInstruction::kNumberOfGenericPackedBits;
8106 static constexpr size_t kFieldOperationKindSize =
8107 MinimumBitsToStore(static_cast<size_t>(OperationKind::kLast));
8108 static constexpr size_t kNumberOfMonitorOperationPackedBits =
8109 kFieldOperationKind + kFieldOperationKindSize;
8110 static_assert(kNumberOfMonitorOperationPackedBits <= HInstruction::kMaxNumberOfPackedBits,
8111 "Too many packed fields.");
8112 using OperationKindField = BitField<OperationKind, kFieldOperationKind, kFieldOperationKindSize>;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00008113};
8114
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01008115class HSelect final : public HExpression<3> {
David Brazdil74eb1b22015-12-14 11:44:01 +00008116 public:
8117 HSelect(HInstruction* condition,
8118 HInstruction* true_value,
8119 HInstruction* false_value,
8120 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05308121 : HExpression(kSelect, HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
David Brazdil74eb1b22015-12-14 11:44:01 +00008122 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
8123
8124 // First input must be `true_value` or `false_value` to allow codegens to
8125 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
8126 // that architectures which implement HSelect as a conditional move also
8127 // will not need to invert the condition.
8128 SetRawInputAt(0, false_value);
8129 SetRawInputAt(1, true_value);
8130 SetRawInputAt(2, condition);
8131 }
8132
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01008133 bool IsClonable() const override { return true; }
David Brazdil74eb1b22015-12-14 11:44:01 +00008134 HInstruction* GetFalseValue() const { return InputAt(0); }
8135 HInstruction* GetTrueValue() const { return InputAt(1); }
8136 HInstruction* GetCondition() const { return InputAt(2); }
8137
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01008138 bool CanBeMoved() const override { return true; }
8139 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01008140 return true;
8141 }
David Brazdil74eb1b22015-12-14 11:44:01 +00008142
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01008143 bool CanBeNull() const override {
David Brazdil74eb1b22015-12-14 11:44:01 +00008144 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
8145 }
8146
8147 DECLARE_INSTRUCTION(Select);
8148
Artem Serovcced8ba2017-07-19 18:18:09 +01008149 protected:
8150 DEFAULT_COPY_CONSTRUCTOR(Select);
David Brazdil74eb1b22015-12-14 11:44:01 +00008151};
8152
Vladimir Markof9f64412015-09-02 14:05:49 +01008153class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01008154 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01008155 MoveOperands(Location source,
8156 Location destination,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008157 DataType::Type type,
Nicolas Geoffray90218252015-04-15 11:56:51 +01008158 HInstruction* instruction)
8159 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01008160
8161 Location GetSource() const { return source_; }
8162 Location GetDestination() const { return destination_; }
8163
8164 void SetSource(Location value) { source_ = value; }
8165 void SetDestination(Location value) { destination_ = value; }
8166
8167 // The parallel move resolver marks moves as "in-progress" by clearing the
8168 // destination (but not the source).
8169 Location MarkPending() {
8170 DCHECK(!IsPending());
8171 Location dest = destination_;
8172 destination_ = Location::NoLocation();
8173 return dest;
8174 }
8175
8176 void ClearPending(Location dest) {
8177 DCHECK(IsPending());
8178 destination_ = dest;
8179 }
8180
8181 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00008182 DCHECK(source_.IsValid() || destination_.IsInvalid());
8183 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01008184 }
8185
8186 // True if this blocks a move from the given location.
8187 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08008188 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01008189 }
8190
8191 // A move is redundant if it's been eliminated, if its source and
8192 // destination are the same, or if its destination is unneeded.
8193 bool IsRedundant() const {
8194 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
8195 }
8196
8197 // We clear both operands to indicate move that's been eliminated.
8198 void Eliminate() {
8199 source_ = destination_ = Location::NoLocation();
8200 }
8201
8202 bool IsEliminated() const {
8203 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
8204 return source_.IsInvalid();
8205 }
8206
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008207 DataType::Type GetType() const { return type_; }
Alexey Frunze4dda3372015-06-01 18:31:49 -07008208
Nicolas Geoffray90218252015-04-15 11:56:51 +01008209 bool Is64BitMove() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008210 return DataType::Is64BitType(type_);
Nicolas Geoffray90218252015-04-15 11:56:51 +01008211 }
8212
Nicolas Geoffray740475d2014-09-29 10:33:25 +01008213 HInstruction* GetInstruction() const { return instruction_; }
8214
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01008215 private:
8216 Location source_;
8217 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01008218 // The type this move is for.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008219 DataType::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01008220 // The instruction this move is assocatied with. Null when this move is
8221 // for moving an input in the expected locations of user (including a phi user).
8222 // This is only used in debug mode, to ensure we do not connect interval siblings
8223 // in the same parallel move.
8224 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01008225};
8226
Roland Levillainc9285912015-12-18 10:38:42 +00008227std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
8228
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01008229static constexpr size_t kDefaultNumberOfMoves = 4;
8230
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01008231class HParallelMove final : public HExpression<0> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01008232 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01008233 explicit HParallelMove(ArenaAllocator* allocator, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01008234 : HExpression(kParallelMove, SideEffects::None(), dex_pc),
Vladimir Markoe764d2e2017-10-05 14:35:55 +01008235 moves_(allocator->Adapter(kArenaAllocMoveOperands)) {
Vladimir Marko225b6462015-09-28 12:17:40 +01008236 moves_.reserve(kDefaultNumberOfMoves);
8237 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01008238
Nicolas Geoffray90218252015-04-15 11:56:51 +01008239 void AddMove(Location source,
8240 Location destination,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008241 DataType::Type type,
Nicolas Geoffray90218252015-04-15 11:56:51 +01008242 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00008243 DCHECK(source.IsValid());
8244 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00008245 if (kIsDebugBuild) {
8246 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01008247 for (const MoveOperands& move : moves_) {
8248 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00008249 // Special case the situation where the move is for the spill slot
8250 // of the instruction.
8251 if ((GetPrevious() == instruction)
8252 || ((GetPrevious() == nullptr)
8253 && instruction->IsPhi()
8254 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01008255 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00008256 << "Doing parallel moves for the same instruction.";
8257 } else {
8258 DCHECK(false) << "Doing parallel moves for the same instruction.";
8259 }
8260 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00008261 }
8262 }
Vladimir Marko225b6462015-09-28 12:17:40 +01008263 for (const MoveOperands& move : moves_) {
8264 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01008265 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01008266 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Alex Lighta7c66e72021-01-27 11:41:58 -08008267 << source << " ==> " << destination << " for " << SafePrint(instruction);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00008268 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01008269 }
Vladimir Marko225b6462015-09-28 12:17:40 +01008270 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01008271 }
8272
Vladimir Marko225b6462015-09-28 12:17:40 +01008273 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01008274 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01008275 }
8276
Vladimir Marko225b6462015-09-28 12:17:40 +01008277 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01008278
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01008279 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01008280
Artem Serovcced8ba2017-07-19 18:18:09 +01008281 protected:
8282 DEFAULT_COPY_CONSTRUCTOR(ParallelMove);
8283
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01008284 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01008285 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01008286};
8287
xueliang.zhonge0eb4832017-10-30 13:43:14 +00008288// This instruction computes an intermediate address pointing in the 'middle' of an object. The
8289// result pointer cannot be handled by GC, so extra care is taken to make sure that this value is
8290// never used across anything that can trigger GC.
8291// The result of this instruction is not a pointer in the sense of `DataType::Type::kreference`.
8292// So we represent it by the type `DataType::Type::kInt`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01008293class HIntermediateAddress final : public HExpression<2> {
xueliang.zhonge0eb4832017-10-30 13:43:14 +00008294 public:
8295 HIntermediateAddress(HInstruction* base_address, HInstruction* offset, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05308296 : HExpression(kIntermediateAddress,
8297 DataType::Type::kInt32,
8298 SideEffects::DependsOnGC(),
8299 dex_pc) {
xueliang.zhonge0eb4832017-10-30 13:43:14 +00008300 DCHECK_EQ(DataType::Size(DataType::Type::kInt32),
8301 DataType::Size(DataType::Type::kReference))
8302 << "kPrimInt and kPrimNot have different sizes.";
8303 SetRawInputAt(0, base_address);
8304 SetRawInputAt(1, offset);
8305 }
8306
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01008307 bool IsClonable() const override { return true; }
8308 bool CanBeMoved() const override { return true; }
8309 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
xueliang.zhonge0eb4832017-10-30 13:43:14 +00008310 return true;
8311 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01008312 bool IsActualObject() const override { return false; }
xueliang.zhonge0eb4832017-10-30 13:43:14 +00008313
8314 HInstruction* GetBaseAddress() const { return InputAt(0); }
8315 HInstruction* GetOffset() const { return InputAt(1); }
8316
8317 DECLARE_INSTRUCTION(IntermediateAddress);
8318
Artem Serovcced8ba2017-07-19 18:18:09 +01008319 protected:
8320 DEFAULT_COPY_CONSTRUCTOR(IntermediateAddress);
xueliang.zhonge0eb4832017-10-30 13:43:14 +00008321};
8322
8323
Mark Mendell0616ae02015-04-17 12:49:27 -04008324} // namespace art
8325
Aart Bikf8f5a162017-02-06 15:35:29 -08008326#include "nodes_vector.h"
8327
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03008328#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
8329#include "nodes_shared.h"
8330#endif
Shalini Salomi Bodapatidd121f62018-10-26 15:03:53 +05308331#if defined(ART_ENABLE_CODEGEN_x86) || defined(ART_ENABLE_CODEGEN_x86_64)
Mark Mendell0616ae02015-04-17 12:49:27 -04008332#include "nodes_x86.h"
8333#endif
8334
Vladimir Marko0a516052019-10-14 13:00:44 +00008335namespace art {
Mark Mendell0616ae02015-04-17 12:49:27 -04008336
Igor Murashkin6ef45672017-08-08 13:59:55 -07008337class OptimizingCompilerStats;
8338
Nicolas Geoffray818f2102014-02-18 16:43:35 +00008339class HGraphVisitor : public ValueObject {
8340 public:
Igor Murashkin6ef45672017-08-08 13:59:55 -07008341 explicit HGraphVisitor(HGraph* graph, OptimizingCompilerStats* stats = nullptr)
8342 : stats_(stats),
8343 graph_(graph) {}
Dave Allison20dfc792014-06-16 20:44:29 -07008344 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00008345
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01008346 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00008347 virtual void VisitBasicBlock(HBasicBlock* block);
8348
Roland Levillain633021e2014-10-01 14:12:25 +01008349 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00008350 void VisitInsertionOrder();
8351
Roland Levillain633021e2014-10-01 14:12:25 +01008352 // Visit the graph following dominator tree reverse post-order.
8353 void VisitReversePostOrder();
8354
Nicolas Geoffray787c3072014-03-17 10:20:19 +00008355 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00008356
Nicolas Geoffray818f2102014-02-18 16:43:35 +00008357 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01008358#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00008359 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
8360
8361 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
8362
8363#undef DECLARE_VISIT_INSTRUCTION
8364
Igor Murashkin6ef45672017-08-08 13:59:55 -07008365 protected:
8366 OptimizingCompilerStats* stats_;
8367
Nicolas Geoffray818f2102014-02-18 16:43:35 +00008368 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07008369 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00008370
8371 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
8372};
8373
Nicolas Geoffray360231a2014-10-08 21:07:48 +01008374class HGraphDelegateVisitor : public HGraphVisitor {
8375 public:
Igor Murashkin6ef45672017-08-08 13:59:55 -07008376 explicit HGraphDelegateVisitor(HGraph* graph, OptimizingCompilerStats* stats = nullptr)
8377 : HGraphVisitor(graph, stats) {}
Nicolas Geoffray360231a2014-10-08 21:07:48 +01008378 virtual ~HGraphDelegateVisitor() {}
8379
8380 // Visit functions that delegate to to super class.
8381#define DECLARE_VISIT_INSTRUCTION(name, super) \
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01008382 void Visit##name(H##name* instr) override { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01008383
8384 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
8385
8386#undef DECLARE_VISIT_INSTRUCTION
8387
8388 private:
8389 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
8390};
8391
Artem Serovcced8ba2017-07-19 18:18:09 +01008392// Create a clone of the instruction, insert it into the graph; replace the old one with a new
8393// and remove the old instruction.
8394HInstruction* ReplaceInstrOrPhiByClone(HInstruction* instr);
8395
8396// Create a clone for each clonable instructions/phis and replace the original with the clone.
8397//
8398// Used for testing individual instruction cloner.
8399class CloneAndReplaceInstructionVisitor : public HGraphDelegateVisitor {
8400 public:
8401 explicit CloneAndReplaceInstructionVisitor(HGraph* graph)
Artem Serov7f4aff62017-06-21 17:02:18 +01008402 : HGraphDelegateVisitor(graph), instr_replaced_by_clones_count_(0) {}
Artem Serovcced8ba2017-07-19 18:18:09 +01008403
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01008404 void VisitInstruction(HInstruction* instruction) override {
Artem Serovcced8ba2017-07-19 18:18:09 +01008405 if (instruction->IsClonable()) {
8406 ReplaceInstrOrPhiByClone(instruction);
Artem Serov7f4aff62017-06-21 17:02:18 +01008407 instr_replaced_by_clones_count_++;
Artem Serovcced8ba2017-07-19 18:18:09 +01008408 }
8409 }
8410
Artem Serov7f4aff62017-06-21 17:02:18 +01008411 size_t GetInstrReplacedByClonesCount() const { return instr_replaced_by_clones_count_; }
Artem Serovcced8ba2017-07-19 18:18:09 +01008412
8413 private:
Artem Serov7f4aff62017-06-21 17:02:18 +01008414 size_t instr_replaced_by_clones_count_;
Artem Serovcced8ba2017-07-19 18:18:09 +01008415
8416 DISALLOW_COPY_AND_ASSIGN(CloneAndReplaceInstructionVisitor);
8417};
8418
Nicolas Geoffray82091da2015-01-26 10:02:45 +00008419// Iterator over the blocks that art part of the loop. Includes blocks part
8420// of an inner loop. The order in which the blocks are iterated is on their
8421// block id.
8422class HBlocksInLoopIterator : public ValueObject {
8423 public:
8424 explicit HBlocksInLoopIterator(const HLoopInformation& info)
8425 : blocks_in_loop_(info.GetBlocks()),
8426 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
8427 index_(0) {
8428 if (!blocks_in_loop_.IsBitSet(index_)) {
8429 Advance();
8430 }
8431 }
8432
Vladimir Markofa6b93c2015-09-15 10:15:55 +01008433 bool Done() const { return index_ == blocks_.size(); }
8434 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00008435 void Advance() {
8436 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01008437 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00008438 if (blocks_in_loop_.IsBitSet(index_)) {
8439 break;
8440 }
8441 }
8442 }
8443
8444 private:
8445 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01008446 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00008447 size_t index_;
8448
8449 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
8450};
8451
Mingyao Yang3584bce2015-05-19 16:01:59 -07008452// Iterator over the blocks that art part of the loop. Includes blocks part
8453// of an inner loop. The order in which the blocks are iterated is reverse
8454// post order.
8455class HBlocksInLoopReversePostOrderIterator : public ValueObject {
8456 public:
8457 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
8458 : blocks_in_loop_(info.GetBlocks()),
8459 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
8460 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01008461 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07008462 Advance();
8463 }
8464 }
8465
Vladimir Markofa6b93c2015-09-15 10:15:55 +01008466 bool Done() const { return index_ == blocks_.size(); }
8467 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07008468 void Advance() {
8469 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01008470 for (size_t e = blocks_.size(); index_ < e; ++index_) {
8471 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07008472 break;
8473 }
8474 }
8475 }
8476
8477 private:
8478 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01008479 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07008480 size_t index_;
8481
8482 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
8483};
8484
Aart Bikf3e61ee2017-04-12 17:09:20 -07008485// Returns int64_t value of a properly typed constant.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00008486inline int64_t Int64FromConstant(HConstant* constant) {
David Brazdilc0b601b2016-02-08 14:20:45 +00008487 if (constant->IsIntConstant()) {
8488 return constant->AsIntConstant()->GetValue();
8489 } else if (constant->IsLongConstant()) {
8490 return constant->AsLongConstant()->GetValue();
8491 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00008492 DCHECK(constant->IsNullConstant()) << constant->DebugName();
David Brazdilc0b601b2016-02-08 14:20:45 +00008493 return 0;
8494 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00008495}
8496
Aart Bikf3e61ee2017-04-12 17:09:20 -07008497// Returns true iff instruction is an integral constant (and sets value on success).
8498inline bool IsInt64AndGet(HInstruction* instruction, /*out*/ int64_t* value) {
8499 if (instruction->IsIntConstant()) {
8500 *value = instruction->AsIntConstant()->GetValue();
8501 return true;
8502 } else if (instruction->IsLongConstant()) {
8503 *value = instruction->AsLongConstant()->GetValue();
8504 return true;
8505 } else if (instruction->IsNullConstant()) {
8506 *value = 0;
8507 return true;
8508 }
8509 return false;
8510}
8511
Aart Bik0148de42017-09-05 09:25:01 -07008512// Returns true iff instruction is the given integral constant.
8513inline bool IsInt64Value(HInstruction* instruction, int64_t value) {
8514 int64_t val = 0;
8515 return IsInt64AndGet(instruction, &val) && val == value;
8516}
8517
8518// Returns true iff instruction is a zero bit pattern.
8519inline bool IsZeroBitPattern(HInstruction* instruction) {
8520 return instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern();
8521}
8522
Vladimir Marko6d5b7e32018-05-09 16:52:48 +01008523// Implement HInstruction::Is##type() for concrete instructions.
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008524#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Markoa90dd512018-05-04 15:04:45 +01008525 inline bool HInstruction::Is##type() const { return GetKind() == k##type; }
8526 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
8527#undef INSTRUCTION_TYPE_CHECK
8528
Vladimir Marko6d5b7e32018-05-09 16:52:48 +01008529// Implement HInstruction::Is##type() for abstract instructions.
Vladimir Markoa90dd512018-05-04 15:04:45 +01008530#define INSTRUCTION_TYPE_CHECK_RESULT(type, super) \
8531 std::is_base_of<BaseType, H##type>::value,
8532#define INSTRUCTION_TYPE_CHECK(type, super) \
8533 inline bool HInstruction::Is##type() const { \
8534 DCHECK_LT(GetKind(), kLastInstructionKind); \
8535 using BaseType = H##type; \
8536 static constexpr bool results[] = { \
8537 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK_RESULT) \
8538 }; \
8539 return results[static_cast<size_t>(GetKind())]; \
8540 }
8541
8542 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
8543#undef INSTRUCTION_TYPE_CHECK
Vladimir Marko6d5b7e32018-05-09 16:52:48 +01008544#undef INSTRUCTION_TYPE_CHECK_RESULT
Vladimir Markoa90dd512018-05-04 15:04:45 +01008545
8546#define INSTRUCTION_TYPE_CAST(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008547 inline const H##type* HInstruction::As##type() const { \
8548 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
8549 } \
8550 inline H##type* HInstruction::As##type() { \
8551 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
8552 }
8553
Vladimir Markoa90dd512018-05-04 15:04:45 +01008554 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CAST)
8555#undef INSTRUCTION_TYPE_CAST
8556
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008557
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00008558// Create space in `blocks` for adding `number_of_new_blocks` entries
8559// starting at location `at`. Blocks after `at` are moved accordingly.
8560inline void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks,
8561 size_t number_of_new_blocks,
8562 size_t after) {
8563 DCHECK_LT(after, blocks->size());
8564 size_t old_size = blocks->size();
8565 size_t new_size = old_size + number_of_new_blocks;
8566 blocks->resize(new_size);
8567 std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end());
8568}
8569
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00008570/*
8571 * Hunt "under the hood" of array lengths (leading to array references),
8572 * null checks (also leading to array references), and new arrays
8573 * (leading to the actual length). This makes it more likely related
8574 * instructions become actually comparable.
8575 */
8576inline HInstruction* HuntForDeclaration(HInstruction* instruction) {
8577 while (instruction->IsArrayLength() ||
8578 instruction->IsNullCheck() ||
8579 instruction->IsNewArray()) {
8580 instruction = instruction->IsNewArray()
8581 ? instruction->AsNewArray()->GetLength()
8582 : instruction->InputAt(0);
8583 }
8584 return instruction;
8585}
8586
Artem Serovb47b9782019-12-04 21:02:09 +00008587inline bool IsAddOrSub(const HInstruction* instruction) {
8588 return instruction->IsAdd() || instruction->IsSub();
8589}
8590
Artem Serov21c7e6f2017-07-27 16:04:42 +01008591void RemoveEnvironmentUses(HInstruction* instruction);
8592bool HasEnvironmentUsedByOthers(HInstruction* instruction);
8593void ResetEnvironmentInputRecords(HInstruction* instruction);
8594
Evgeny Astigeevichaf92a0f2020-06-26 13:28:33 +01008595// Detects an instruction that is >= 0. As long as the value is carried by
8596// a single instruction, arithmetic wrap-around cannot occur.
8597bool IsGEZero(HInstruction* instruction);
8598
Nicolas Geoffray818f2102014-02-18 16:43:35 +00008599} // namespace art
8600
8601#endif // ART_COMPILER_OPTIMIZING_NODES_H_