blob: c47ca3bbb5ac8e46a471120c85e68aa1acb84501 [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:
197 typedef Handle<mirror::Class> TypeHandle;
198
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; }
4935 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
4936 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Marko58155012015-08-19 12:49:41 +00004937 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004938 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kJitDirectAddress; }
Vladimir Marko65979462017-05-19 17:25:12 +01004939 bool HasPcRelativeMethodLoadKind() const {
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01004940 return IsPcRelativeMethodLoadKind(GetMethodLoadKind());
Vladimir Marko65979462017-05-19 17:25:12 +01004941 }
Vladimir Marko58155012015-08-19 12:49:41 +00004942
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004943 QuickEntrypointEnum GetStringInitEntryPoint() const {
Vladimir Marko58155012015-08-19 12:49:41 +00004944 DCHECK(IsStringInit());
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004945 return static_cast<QuickEntrypointEnum>(dispatch_info_.method_load_data);
Vladimir Marko58155012015-08-19 12:49:41 +00004946 }
4947
4948 uint64_t GetMethodAddress() const {
4949 DCHECK(HasMethodAddress());
4950 return dispatch_info_.method_load_data;
4951 }
4952
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00004953 const DexFile& GetDexFileForPcRelativeDexCache() const;
4954
Vladimir Markoa1de9182016-02-25 11:37:38 +00004955 ClinitCheckRequirement GetClinitCheckRequirement() const {
4956 return GetPackedField<ClinitCheckRequirementField>();
4957 }
Calin Juravle68ad6492015-08-18 17:08:12 +01004958
Roland Levillain4c0eb422015-04-24 16:43:49 +01004959 // Is this instruction a call to a static method?
4960 bool IsStatic() const {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004961 return GetInvokeType() == kStatic;
4962 }
4963
Vladimir Marko86c87522020-05-11 16:55:55 +01004964 // Does this method load kind need the current method as an input?
4965 static bool NeedsCurrentMethodInput(DispatchInfo dispatch_info) {
4966 return dispatch_info.method_load_kind == MethodLoadKind::kRecursive ||
4967 dispatch_info.method_load_kind == MethodLoadKind::kRuntimeCall ||
4968 dispatch_info.code_ptr_location == CodePtrLocation::kCallCriticalNative;
4969 }
4970
4971 // Get the index of the current method input.
4972 size_t GetCurrentMethodIndex() const {
4973 DCHECK(HasCurrentMethodInput());
4974 return GetCurrentMethodIndexUnchecked();
4975 }
4976 size_t GetCurrentMethodIndexUnchecked() const {
4977 return GetNumberOfArguments();
4978 }
4979
4980 // Check if the method has a current method input.
4981 bool HasCurrentMethodInput() const {
4982 if (NeedsCurrentMethodInput(GetDispatchInfo())) {
4983 DCHECK(InputAt(GetCurrentMethodIndexUnchecked()) == nullptr || // During argument setup.
4984 InputAt(GetCurrentMethodIndexUnchecked())->IsCurrentMethod());
4985 return true;
4986 } else {
4987 DCHECK(InputCount() == GetCurrentMethodIndexUnchecked() ||
4988 InputAt(GetCurrentMethodIndexUnchecked()) == nullptr || // During argument setup.
4989 !InputAt(GetCurrentMethodIndexUnchecked())->IsCurrentMethod());
4990 return false;
4991 }
4992 }
4993
4994 // Get the index of the special input.
4995 size_t GetSpecialInputIndex() const {
4996 DCHECK(HasSpecialInput());
4997 return GetSpecialInputIndexUnchecked();
4998 }
4999 size_t GetSpecialInputIndexUnchecked() const {
5000 return GetNumberOfArguments() + (HasCurrentMethodInput() ? 1u : 0u);
5001 }
5002
5003 // Check if the method has a special input.
5004 bool HasSpecialInput() const {
5005 size_t other_inputs =
5006 GetSpecialInputIndexUnchecked() + (IsStaticWithExplicitClinitCheck() ? 1u : 0u);
5007 size_t input_count = InputCount();
5008 DCHECK_LE(input_count - other_inputs, 1u) << other_inputs << " " << input_count;
5009 return other_inputs != input_count;
5010 }
5011
5012 void AddSpecialInput(HInstruction* input) {
5013 // We allow only one special input.
5014 DCHECK(!HasSpecialInput());
5015 InsertInputAt(GetSpecialInputIndexUnchecked(), input);
5016 }
5017
Vladimir Markofbb184a2015-11-13 14:47:00 +00005018 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
5019 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
5020 // instruction; only relevant for static calls with explicit clinit check.
5021 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01005022 DCHECK(IsStaticWithExplicitClinitCheck());
Vladimir Marko372f10e2016-05-17 16:30:10 +01005023 size_t last_input_index = inputs_.size() - 1u;
5024 HInstruction* last_input = inputs_.back().GetInstruction();
Roland Levillain4c0eb422015-04-24 16:43:49 +01005025 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00005026 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01005027 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01005028 inputs_.pop_back();
Vladimir Markoa1de9182016-02-25 11:37:38 +00005029 SetPackedField<ClinitCheckRequirementField>(new_requirement);
Vladimir Markofbb184a2015-11-13 14:47:00 +00005030 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01005031 }
5032
5033 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00005034 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01005035 bool IsStaticWithExplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005036 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kExplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01005037 }
5038
5039 // Is this a call to a static method whose declaring class has an
5040 // implicit intialization check requirement?
5041 bool IsStaticWithImplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005042 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kImplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01005043 }
5044
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00005045 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00005046
Artem Serovcced8ba2017-07-19 18:18:09 +01005047 protected:
5048 DEFAULT_COPY_CONSTRUCTOR(InvokeStaticOrDirect);
5049
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00005050 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01005051 static constexpr size_t kFieldClinitCheckRequirement = kNumberOfInvokePackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005052 static constexpr size_t kFieldClinitCheckRequirementSize =
5053 MinimumBitsToStore(static_cast<size_t>(ClinitCheckRequirement::kLast));
5054 static constexpr size_t kNumberOfInvokeStaticOrDirectPackedBits =
5055 kFieldClinitCheckRequirement + kFieldClinitCheckRequirementSize;
5056 static_assert(kNumberOfInvokeStaticOrDirectPackedBits <= kMaxNumberOfPackedBits,
5057 "Too many packed fields.");
Vladimir Markoa1de9182016-02-25 11:37:38 +00005058 using ClinitCheckRequirementField = BitField<ClinitCheckRequirement,
5059 kFieldClinitCheckRequirement,
5060 kFieldClinitCheckRequirementSize>;
5061
Vladimir Marko58155012015-08-19 12:49:41 +00005062 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00005063};
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01005064std::ostream& operator<<(std::ostream& os, MethodLoadKind rhs);
5065std::ostream& operator<<(std::ostream& os, CodePtrLocation rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00005066std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00005067
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005068class HInvokeVirtual final : public HInvoke {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01005069 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01005070 HInvokeVirtual(ArenaAllocator* allocator,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01005071 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005072 DataType::Type return_type,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01005073 uint32_t dex_pc,
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01005074 MethodReference method_reference,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01005075 ArtMethod* resolved_method,
Nicolas Geoffrayb0f405e2020-09-24 15:10:35 +01005076 MethodReference resolved_method_reference,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01005077 uint32_t vtable_index)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305078 : HInvoke(kInvokeVirtual,
5079 allocator,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01005080 number_of_arguments,
5081 0u,
5082 return_type,
5083 dex_pc,
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01005084 method_reference,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01005085 resolved_method,
Nicolas Geoffrayb0f405e2020-09-24 15:10:35 +01005086 resolved_method_reference,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01005087 kVirtual),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305088 vtable_index_(vtable_index) {
5089 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01005090
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005091 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01005092
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005093 bool CanBeNull() const override {
Aart Bik71bf7b42016-11-16 10:17:46 -08005094 switch (GetIntrinsic()) {
5095 case Intrinsics::kThreadCurrentThread:
5096 case Intrinsics::kStringBufferAppend:
5097 case Intrinsics::kStringBufferToString:
Vladimir Markod4561172017-10-30 17:48:25 +00005098 case Intrinsics::kStringBuilderAppendObject:
5099 case Intrinsics::kStringBuilderAppendString:
5100 case Intrinsics::kStringBuilderAppendCharSequence:
5101 case Intrinsics::kStringBuilderAppendCharArray:
5102 case Intrinsics::kStringBuilderAppendBoolean:
5103 case Intrinsics::kStringBuilderAppendChar:
5104 case Intrinsics::kStringBuilderAppendInt:
5105 case Intrinsics::kStringBuilderAppendLong:
5106 case Intrinsics::kStringBuilderAppendFloat:
5107 case Intrinsics::kStringBuilderAppendDouble:
Aart Bik71bf7b42016-11-16 10:17:46 -08005108 case Intrinsics::kStringBuilderToString:
5109 return false;
5110 default:
5111 return HInvoke::CanBeNull();
5112 }
5113 }
5114
Vladimir Markoac27ac02021-02-01 09:31:02 +00005115 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override;
Calin Juravle77520bc2015-01-12 18:45:46 +00005116
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01005117 uint32_t GetVTableIndex() const { return vtable_index_; }
5118
5119 DECLARE_INSTRUCTION(InvokeVirtual);
5120
Artem Serovcced8ba2017-07-19 18:18:09 +01005121 protected:
5122 DEFAULT_COPY_CONSTRUCTOR(InvokeVirtual);
5123
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01005124 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01005125 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01005126 const uint32_t vtable_index_;
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01005127};
5128
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005129class HInvokeInterface final : public HInvoke {
Nicolas Geoffray52839d12014-11-07 17:47:25 +00005130 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01005131 HInvokeInterface(ArenaAllocator* allocator,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00005132 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005133 DataType::Type return_type,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00005134 uint32_t dex_pc,
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01005135 MethodReference method_reference,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01005136 ArtMethod* resolved_method,
Nicolas Geoffrayb0f405e2020-09-24 15:10:35 +01005137 MethodReference resolved_method_reference,
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01005138 uint32_t imt_index,
5139 MethodLoadKind load_kind)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305140 : HInvoke(kInvokeInterface,
5141 allocator,
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01005142 number_of_arguments + (NeedsCurrentMethod(load_kind) ? 1 : 0),
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01005143 0u,
5144 return_type,
5145 dex_pc,
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01005146 method_reference,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01005147 resolved_method,
Nicolas Geoffrayb0f405e2020-09-24 15:10:35 +01005148 resolved_method_reference,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01005149 kInterface),
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01005150 imt_index_(imt_index),
5151 hidden_argument_load_kind_(load_kind) {
5152 }
5153
5154 static bool NeedsCurrentMethod(MethodLoadKind load_kind) {
5155 return load_kind == MethodLoadKind::kRecursive;
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305156 }
Nicolas Geoffray52839d12014-11-07 17:47:25 +00005157
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005158 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01005159
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005160 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Calin Juravle77520bc2015-01-12 18:45:46 +00005161 // TODO: Add implicit null checks in intrinsics.
Nicolas Geoffray61ba8d22018-08-07 09:55:57 +01005162 return (obj == InputAt(0)) && !IsIntrinsic();
Calin Juravle77520bc2015-01-12 18:45:46 +00005163 }
5164
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01005165 size_t GetSpecialInputIndex() const {
5166 return GetNumberOfArguments();
5167 }
5168
5169 void AddSpecialInput(HInstruction* input) {
5170 InsertInputAt(GetSpecialInputIndex(), input);
5171 }
5172
Nicolas Geoffray52839d12014-11-07 17:47:25 +00005173 uint32_t GetImtIndex() const { return imt_index_; }
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01005174 MethodLoadKind GetHiddenArgumentLoadKind() const { return hidden_argument_load_kind_; }
Nicolas Geoffray52839d12014-11-07 17:47:25 +00005175
5176 DECLARE_INSTRUCTION(InvokeInterface);
5177
Artem Serovcced8ba2017-07-19 18:18:09 +01005178 protected:
5179 DEFAULT_COPY_CONSTRUCTOR(InvokeInterface);
5180
Nicolas Geoffray52839d12014-11-07 17:47:25 +00005181 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01005182 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00005183 const uint32_t imt_index_;
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01005184
5185 // How the hidden argument (the interface method) is being loaded.
5186 const MethodLoadKind hidden_argument_load_kind_;
Nicolas Geoffray52839d12014-11-07 17:47:25 +00005187};
5188
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005189class HNeg final : public HUnaryOperation {
Roland Levillain88cb1752014-10-20 16:36:47 +01005190 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005191 HNeg(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305192 : HUnaryOperation(kNeg, result_type, input, dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005193 DCHECK_EQ(result_type, DataType::Kind(input->GetType()));
Roland Levillain937e6cd2016-03-22 11:54:37 +00005194 }
Roland Levillain88cb1752014-10-20 16:36:47 +01005195
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005196 template <typename T> static T Compute(T x) { return -x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005197
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005198 HConstant* Evaluate(HIntConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005199 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005200 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005201 HConstant* Evaluate(HLongConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005202 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005203 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005204 HConstant* Evaluate(HFloatConstant* x) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005205 return GetBlock()->GetGraph()->GetFloatConstant(Compute(x->GetValue()), GetDexPc());
5206 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005207 HConstant* Evaluate(HDoubleConstant* x) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005208 return GetBlock()->GetGraph()->GetDoubleConstant(Compute(x->GetValue()), GetDexPc());
5209 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01005210
Roland Levillain88cb1752014-10-20 16:36:47 +01005211 DECLARE_INSTRUCTION(Neg);
5212
Artem Serovcced8ba2017-07-19 18:18:09 +01005213 protected:
5214 DEFAULT_COPY_CONSTRUCTOR(Neg);
Roland Levillain88cb1752014-10-20 16:36:47 +01005215};
5216
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005217class HNewArray final : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01005218 public:
Vladimir Markob5461632018-10-15 14:24:21 +01005219 HNewArray(HInstruction* cls, HInstruction* length, uint32_t dex_pc, size_t component_size_shift)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305220 : HExpression(kNewArray, DataType::Type::kReference, SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005221 SetRawInputAt(0, cls);
5222 SetRawInputAt(1, length);
Vladimir Markob5461632018-10-15 14:24:21 +01005223 SetPackedField<ComponentSizeShiftField>(component_size_shift);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01005224 }
5225
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005226 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01005227
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01005228 // Calls runtime so needs an environment.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005229 bool NeedsEnvironment() const override { return true; }
Calin Juravle10e244f2015-01-26 18:54:32 +00005230
Mingyao Yang0c365e62015-03-31 15:09:29 -07005231 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005232 bool CanThrow() const override { return true; }
Mingyao Yang0c365e62015-03-31 15:09:29 -07005233
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005234 bool CanBeNull() const override { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01005235
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005236 HLoadClass* GetLoadClass() const {
5237 DCHECK(InputAt(0)->IsLoadClass());
5238 return InputAt(0)->AsLoadClass();
5239 }
5240
5241 HInstruction* GetLength() const {
5242 return InputAt(1);
5243 }
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00005244
Vladimir Markob5461632018-10-15 14:24:21 +01005245 size_t GetComponentSizeShift() {
5246 return GetPackedField<ComponentSizeShiftField>();
5247 }
5248
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01005249 DECLARE_INSTRUCTION(NewArray);
5250
Artem Serovcced8ba2017-07-19 18:18:09 +01005251 protected:
5252 DEFAULT_COPY_CONSTRUCTOR(NewArray);
Vladimir Markob5461632018-10-15 14:24:21 +01005253
5254 private:
5255 static constexpr size_t kFieldComponentSizeShift = kNumberOfGenericPackedBits;
5256 static constexpr size_t kFieldComponentSizeShiftSize = MinimumBitsToStore(3u);
5257 static constexpr size_t kNumberOfNewArrayPackedBits =
5258 kFieldComponentSizeShift + kFieldComponentSizeShiftSize;
5259 static_assert(kNumberOfNewArrayPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5260 using ComponentSizeShiftField =
5261 BitField<size_t, kFieldComponentSizeShift, kFieldComponentSizeShift>;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01005262};
5263
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005264class HAdd final : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00005265 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005266 HAdd(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005267 HInstruction* left,
5268 HInstruction* right,
5269 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305270 : HBinaryOperation(kAdd, result_type, left, right, SideEffects::None(), dex_pc) {
5271 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00005272
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005273 bool IsCommutative() const override { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00005274
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005275 template <typename T> static T Compute(T x, T y) { return x + y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005276
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005277 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005278 return GetBlock()->GetGraph()->GetIntConstant(
5279 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01005280 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005281 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005282 return GetBlock()->GetGraph()->GetLongConstant(
5283 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01005284 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005285 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005286 return GetBlock()->GetGraph()->GetFloatConstant(
5287 Compute(x->GetValue(), y->GetValue()), GetDexPc());
5288 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005289 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005290 return GetBlock()->GetGraph()->GetDoubleConstant(
5291 Compute(x->GetValue(), y->GetValue()), GetDexPc());
5292 }
Roland Levillain556c3d12014-09-18 15:25:07 +01005293
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00005294 DECLARE_INSTRUCTION(Add);
5295
Artem Serovcced8ba2017-07-19 18:18:09 +01005296 protected:
5297 DEFAULT_COPY_CONSTRUCTOR(Add);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00005298};
5299
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005300class HSub final : public HBinaryOperation {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005301 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005302 HSub(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005303 HInstruction* left,
5304 HInstruction* right,
5305 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305306 : HBinaryOperation(kSub, result_type, left, right, SideEffects::None(), dex_pc) {
5307 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005308
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005309 template <typename T> static T Compute(T x, T y) { return x - y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005310
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005311 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005312 return GetBlock()->GetGraph()->GetIntConstant(
5313 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01005314 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005315 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005316 return GetBlock()->GetGraph()->GetLongConstant(
5317 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01005318 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005319 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005320 return GetBlock()->GetGraph()->GetFloatConstant(
5321 Compute(x->GetValue(), y->GetValue()), GetDexPc());
5322 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005323 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005324 return GetBlock()->GetGraph()->GetDoubleConstant(
5325 Compute(x->GetValue(), y->GetValue()), GetDexPc());
5326 }
Roland Levillain556c3d12014-09-18 15:25:07 +01005327
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005328 DECLARE_INSTRUCTION(Sub);
5329
Artem Serovcced8ba2017-07-19 18:18:09 +01005330 protected:
5331 DEFAULT_COPY_CONSTRUCTOR(Sub);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005332};
5333
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005334class HMul final : public HBinaryOperation {
Calin Juravle34bacdf2014-10-07 20:23:36 +01005335 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005336 HMul(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005337 HInstruction* left,
5338 HInstruction* right,
5339 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305340 : HBinaryOperation(kMul, result_type, left, right, SideEffects::None(), dex_pc) {
5341 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01005342
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005343 bool IsCommutative() const override { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01005344
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005345 template <typename T> static T Compute(T x, T y) { return x * y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005346
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005347 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005348 return GetBlock()->GetGraph()->GetIntConstant(
5349 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005350 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005351 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005352 return GetBlock()->GetGraph()->GetLongConstant(
5353 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005354 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005355 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005356 return GetBlock()->GetGraph()->GetFloatConstant(
5357 Compute(x->GetValue(), y->GetValue()), GetDexPc());
5358 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005359 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005360 return GetBlock()->GetGraph()->GetDoubleConstant(
5361 Compute(x->GetValue(), y->GetValue()), GetDexPc());
5362 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01005363
5364 DECLARE_INSTRUCTION(Mul);
5365
Artem Serovcced8ba2017-07-19 18:18:09 +01005366 protected:
5367 DEFAULT_COPY_CONSTRUCTOR(Mul);
Calin Juravle34bacdf2014-10-07 20:23:36 +01005368};
5369
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005370class HDiv final : public HBinaryOperation {
Calin Juravle7c4954d2014-10-28 16:57:40 +00005371 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005372 HDiv(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005373 HInstruction* left,
5374 HInstruction* right,
5375 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305376 : HBinaryOperation(kDiv, result_type, left, right, SideEffects::None(), dex_pc) {
5377 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00005378
Roland Levillain9867bc72015-08-05 10:21:34 +01005379 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00005380 T ComputeIntegral(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005381 DCHECK(!DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01005382 // Our graph structure ensures we never have 0 for `y` during
5383 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00005384 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00005385 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00005386 return (y == -1) ? -x : x / y;
5387 }
Calin Juravlebacfec32014-11-14 15:54:36 +00005388
Roland Levillain31dd3d62016-02-16 12:21:02 +00005389 template <typename T>
5390 T ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005391 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00005392 return x / y;
5393 }
5394
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005395 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005396 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00005397 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005398 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005399 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005400 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00005401 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5402 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005403 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005404 return GetBlock()->GetGraph()->GetFloatConstant(
5405 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
5406 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005407 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005408 return GetBlock()->GetGraph()->GetDoubleConstant(
5409 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00005410 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00005411
5412 DECLARE_INSTRUCTION(Div);
5413
Artem Serovcced8ba2017-07-19 18:18:09 +01005414 protected:
5415 DEFAULT_COPY_CONSTRUCTOR(Div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00005416};
5417
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005418class HRem final : public HBinaryOperation {
Calin Juravlebacfec32014-11-14 15:54:36 +00005419 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005420 HRem(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005421 HInstruction* left,
5422 HInstruction* right,
5423 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305424 : HBinaryOperation(kRem, result_type, left, right, SideEffects::None(), dex_pc) {
5425 }
Calin Juravlebacfec32014-11-14 15:54:36 +00005426
Roland Levillain9867bc72015-08-05 10:21:34 +01005427 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00005428 T ComputeIntegral(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005429 DCHECK(!DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01005430 // Our graph structure ensures we never have 0 for `y` during
5431 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00005432 DCHECK_NE(y, 0);
5433 // Special case -1 to avoid getting a SIGFPE on x86(_64).
5434 return (y == -1) ? 0 : x % y;
5435 }
5436
Roland Levillain31dd3d62016-02-16 12:21:02 +00005437 template <typename T>
5438 T ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005439 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00005440 return std::fmod(x, y);
5441 }
5442
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005443 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005444 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00005445 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005446 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005447 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005448 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00005449 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00005450 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005451 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005452 return GetBlock()->GetGraph()->GetFloatConstant(
5453 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
5454 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005455 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005456 return GetBlock()->GetGraph()->GetDoubleConstant(
5457 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
5458 }
Calin Juravlebacfec32014-11-14 15:54:36 +00005459
5460 DECLARE_INSTRUCTION(Rem);
5461
Artem Serovcced8ba2017-07-19 18:18:09 +01005462 protected:
5463 DEFAULT_COPY_CONSTRUCTOR(Rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00005464};
5465
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005466class HMin final : public HBinaryOperation {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005467 public:
5468 HMin(DataType::Type result_type,
5469 HInstruction* left,
5470 HInstruction* right,
5471 uint32_t dex_pc)
5472 : HBinaryOperation(kMin, result_type, left, right, SideEffects::None(), dex_pc) {}
5473
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005474 bool IsCommutative() const override { return true; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005475
5476 // Evaluation for integral values.
5477 template <typename T> static T ComputeIntegral(T x, T y) {
5478 return (x <= y) ? x : y;
5479 }
5480
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005481 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005482 return GetBlock()->GetGraph()->GetIntConstant(
5483 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5484 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005485 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005486 return GetBlock()->GetGraph()->GetLongConstant(
5487 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5488 }
5489 // TODO: Evaluation for floating-point values.
5490 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005491 HFloatConstant* y ATTRIBUTE_UNUSED) const override { return nullptr; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005492 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005493 HDoubleConstant* y ATTRIBUTE_UNUSED) const override { return nullptr; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005494
5495 DECLARE_INSTRUCTION(Min);
5496
5497 protected:
5498 DEFAULT_COPY_CONSTRUCTOR(Min);
5499};
5500
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005501class HMax final : public HBinaryOperation {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005502 public:
5503 HMax(DataType::Type result_type,
5504 HInstruction* left,
5505 HInstruction* right,
5506 uint32_t dex_pc)
5507 : HBinaryOperation(kMax, result_type, left, right, SideEffects::None(), dex_pc) {}
5508
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005509 bool IsCommutative() const override { return true; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005510
5511 // Evaluation for integral values.
5512 template <typename T> static T ComputeIntegral(T x, T y) {
5513 return (x >= y) ? x : y;
5514 }
5515
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005516 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005517 return GetBlock()->GetGraph()->GetIntConstant(
5518 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5519 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005520 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005521 return GetBlock()->GetGraph()->GetLongConstant(
5522 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5523 }
5524 // TODO: Evaluation for floating-point values.
5525 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005526 HFloatConstant* y ATTRIBUTE_UNUSED) const override { return nullptr; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005527 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005528 HDoubleConstant* y ATTRIBUTE_UNUSED) const override { return nullptr; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005529
5530 DECLARE_INSTRUCTION(Max);
5531
5532 protected:
5533 DEFAULT_COPY_CONSTRUCTOR(Max);
5534};
5535
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005536class HAbs final : public HUnaryOperation {
Aart Bik3dad3412018-02-28 12:01:46 -08005537 public:
5538 HAbs(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
5539 : HUnaryOperation(kAbs, result_type, input, dex_pc) {}
5540
5541 // Evaluation for integral values.
5542 template <typename T> static T ComputeIntegral(T x) {
5543 return x < 0 ? -x : x;
5544 }
5545
5546 // Evaluation for floating-point values.
5547 // Note, as a "quality of implementation", rather than pure "spec compliance",
5548 // we require that Math.abs() clears the sign bit (but changes nothing else)
5549 // for all floating-point numbers, including NaN (signaling NaN may become quiet though).
5550 // http://b/30758343
5551 template <typename T, typename S> static T ComputeFP(T x) {
5552 S bits = bit_cast<S, T>(x);
5553 return bit_cast<T, S>(bits & std::numeric_limits<S>::max());
5554 }
5555
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005556 HConstant* Evaluate(HIntConstant* x) const override {
Aart Bik3dad3412018-02-28 12:01:46 -08005557 return GetBlock()->GetGraph()->GetIntConstant(ComputeIntegral(x->GetValue()), GetDexPc());
5558 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005559 HConstant* Evaluate(HLongConstant* x) const override {
Aart Bik3dad3412018-02-28 12:01:46 -08005560 return GetBlock()->GetGraph()->GetLongConstant(ComputeIntegral(x->GetValue()), GetDexPc());
5561 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005562 HConstant* Evaluate(HFloatConstant* x) const override {
Aart Bik3dad3412018-02-28 12:01:46 -08005563 return GetBlock()->GetGraph()->GetFloatConstant(
5564 ComputeFP<float, int32_t>(x->GetValue()), GetDexPc());
5565 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005566 HConstant* Evaluate(HDoubleConstant* x) const override {
Aart Bik3dad3412018-02-28 12:01:46 -08005567 return GetBlock()->GetGraph()->GetDoubleConstant(
5568 ComputeFP<double, int64_t>(x->GetValue()), GetDexPc());
5569 }
5570
5571 DECLARE_INSTRUCTION(Abs);
5572
5573 protected:
5574 DEFAULT_COPY_CONSTRUCTOR(Abs);
5575};
5576
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005577class HDivZeroCheck final : public HExpression<1> {
Calin Juravled0d48522014-11-04 16:40:20 +00005578 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00005579 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
Artem Serovd1aa7d02018-06-22 11:35:46 +01005580 // constructor. However it can only do it on a fatal slow path so execution never returns to the
5581 // instruction following the current one; thus 'SideEffects::None()' is used.
Calin Juravled0d48522014-11-04 16:40:20 +00005582 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Artem Serovd1aa7d02018-06-22 11:35:46 +01005583 : HExpression(kDivZeroCheck, value->GetType(), SideEffects::None(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00005584 SetRawInputAt(0, value);
5585 }
5586
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005587 bool IsClonable() const override { return true; }
5588 bool CanBeMoved() const override { return true; }
Calin Juravled0d48522014-11-04 16:40:20 +00005589
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005590 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Calin Juravled0d48522014-11-04 16:40:20 +00005591 return true;
5592 }
5593
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005594 bool NeedsEnvironment() const override { return true; }
5595 bool CanThrow() const override { return true; }
Calin Juravled0d48522014-11-04 16:40:20 +00005596
Calin Juravled0d48522014-11-04 16:40:20 +00005597 DECLARE_INSTRUCTION(DivZeroCheck);
5598
Artem Serovcced8ba2017-07-19 18:18:09 +01005599 protected:
5600 DEFAULT_COPY_CONSTRUCTOR(DivZeroCheck);
Calin Juravled0d48522014-11-04 16:40:20 +00005601};
5602
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005603class HShl final : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005604 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005605 HShl(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005606 HInstruction* value,
5607 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005608 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305609 : HBinaryOperation(kShl, result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005610 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5611 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01005612 }
5613
Roland Levillain5b5b9312016-03-22 14:57:31 +00005614 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005615 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005616 return value << (distance & max_shift_distance);
5617 }
5618
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005619 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005620 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005621 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005622 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005623 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005624 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005625 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005626 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005627 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005628 HLongConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005629 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5630 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01005631 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005632 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005633 HFloatConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005634 LOG(FATAL) << DebugName() << " is not defined for float values";
5635 UNREACHABLE();
5636 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005637 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005638 HDoubleConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005639 LOG(FATAL) << DebugName() << " is not defined for double values";
5640 UNREACHABLE();
5641 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005642
5643 DECLARE_INSTRUCTION(Shl);
5644
Artem Serovcced8ba2017-07-19 18:18:09 +01005645 protected:
5646 DEFAULT_COPY_CONSTRUCTOR(Shl);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005647};
5648
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005649class HShr final : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005650 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005651 HShr(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005652 HInstruction* value,
5653 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005654 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305655 : HBinaryOperation(kShr, result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005656 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5657 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01005658 }
5659
Roland Levillain5b5b9312016-03-22 14:57:31 +00005660 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005661 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005662 return value >> (distance & max_shift_distance);
5663 }
5664
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005665 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005666 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005667 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005668 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005669 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005670 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005671 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005672 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005673 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005674 HLongConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005675 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5676 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01005677 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005678 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005679 HFloatConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005680 LOG(FATAL) << DebugName() << " is not defined for float values";
5681 UNREACHABLE();
5682 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005683 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005684 HDoubleConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005685 LOG(FATAL) << DebugName() << " is not defined for double values";
5686 UNREACHABLE();
5687 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005688
5689 DECLARE_INSTRUCTION(Shr);
5690
Artem Serovcced8ba2017-07-19 18:18:09 +01005691 protected:
5692 DEFAULT_COPY_CONSTRUCTOR(Shr);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005693};
5694
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005695class HUShr final : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005696 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005697 HUShr(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005698 HInstruction* value,
5699 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005700 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305701 : HBinaryOperation(kUShr, result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005702 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5703 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Calin Juravle9aec02f2014-11-18 23:06:35 +00005704 }
5705
Roland Levillain5b5b9312016-03-22 14:57:31 +00005706 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005707 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005708 typedef typename std::make_unsigned<T>::type V;
5709 V ux = static_cast<V>(value);
5710 return static_cast<T>(ux >> (distance & max_shift_distance));
5711 }
5712
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005713 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005714 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005715 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005716 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005717 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005718 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005719 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005720 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005721 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005722 HLongConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005723 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5724 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00005725 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005726 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005727 HFloatConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005728 LOG(FATAL) << DebugName() << " is not defined for float values";
5729 UNREACHABLE();
5730 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005731 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005732 HDoubleConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005733 LOG(FATAL) << DebugName() << " is not defined for double values";
5734 UNREACHABLE();
5735 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005736
5737 DECLARE_INSTRUCTION(UShr);
5738
Artem Serovcced8ba2017-07-19 18:18:09 +01005739 protected:
5740 DEFAULT_COPY_CONSTRUCTOR(UShr);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005741};
5742
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005743class HAnd final : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005744 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005745 HAnd(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005746 HInstruction* left,
5747 HInstruction* right,
5748 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305749 : HBinaryOperation(kAnd, result_type, left, right, SideEffects::None(), dex_pc) {
5750 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005751
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005752 bool IsCommutative() const override { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005753
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005754 template <typename T> static T Compute(T x, T y) { return x & y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005755
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005756 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005757 return GetBlock()->GetGraph()->GetIntConstant(
5758 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005759 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005760 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005761 return GetBlock()->GetGraph()->GetLongConstant(
5762 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005763 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005764 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005765 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005766 LOG(FATAL) << DebugName() << " is not defined for float values";
5767 UNREACHABLE();
5768 }
5769 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005770 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005771 LOG(FATAL) << DebugName() << " is not defined for double values";
5772 UNREACHABLE();
5773 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005774
5775 DECLARE_INSTRUCTION(And);
5776
Artem Serovcced8ba2017-07-19 18:18:09 +01005777 protected:
5778 DEFAULT_COPY_CONSTRUCTOR(And);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005779};
5780
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005781class HOr final : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005782 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005783 HOr(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005784 HInstruction* left,
5785 HInstruction* right,
5786 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305787 : HBinaryOperation(kOr, result_type, left, right, SideEffects::None(), dex_pc) {
5788 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005789
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005790 bool IsCommutative() const override { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005791
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005792 template <typename T> static T Compute(T x, T y) { return x | y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005793
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005794 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005795 return GetBlock()->GetGraph()->GetIntConstant(
5796 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005797 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005798 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005799 return GetBlock()->GetGraph()->GetLongConstant(
5800 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005801 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005802 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005803 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005804 LOG(FATAL) << DebugName() << " is not defined for float values";
5805 UNREACHABLE();
5806 }
5807 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005808 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005809 LOG(FATAL) << DebugName() << " is not defined for double values";
5810 UNREACHABLE();
5811 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005812
5813 DECLARE_INSTRUCTION(Or);
5814
Artem Serovcced8ba2017-07-19 18:18:09 +01005815 protected:
5816 DEFAULT_COPY_CONSTRUCTOR(Or);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005817};
5818
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005819class HXor final : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005820 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005821 HXor(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005822 HInstruction* left,
5823 HInstruction* right,
5824 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305825 : HBinaryOperation(kXor, result_type, left, right, SideEffects::None(), dex_pc) {
5826 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005827
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005828 bool IsCommutative() const override { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005829
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005830 template <typename T> static T Compute(T x, T y) { return x ^ y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005831
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005832 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005833 return GetBlock()->GetGraph()->GetIntConstant(
5834 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005835 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005836 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005837 return GetBlock()->GetGraph()->GetLongConstant(
5838 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005839 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005840 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005841 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005842 LOG(FATAL) << DebugName() << " is not defined for float values";
5843 UNREACHABLE();
5844 }
5845 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005846 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005847 LOG(FATAL) << DebugName() << " is not defined for double values";
5848 UNREACHABLE();
5849 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005850
5851 DECLARE_INSTRUCTION(Xor);
5852
Artem Serovcced8ba2017-07-19 18:18:09 +01005853 protected:
5854 DEFAULT_COPY_CONSTRUCTOR(Xor);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005855};
5856
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005857class HRor final : public HBinaryOperation {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005858 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005859 HRor(DataType::Type result_type, HInstruction* value, HInstruction* distance)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305860 : HBinaryOperation(kRor, result_type, value, distance) {
Roland Levillain22c49222016-03-18 14:04:28 +00005861 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005862
Roland Levillain5b5b9312016-03-22 14:57:31 +00005863 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005864 static T Compute(T value, int32_t distance, int32_t max_shift_value) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005865 typedef typename std::make_unsigned<T>::type V;
5866 V ux = static_cast<V>(value);
5867 if ((distance & max_shift_value) == 0) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005868 return static_cast<T>(ux);
5869 } else {
5870 const V reg_bits = sizeof(T) * 8;
Roland Levillain5b5b9312016-03-22 14:57:31 +00005871 return static_cast<T>(ux >> (distance & max_shift_value)) |
5872 (value << (reg_bits - (distance & max_shift_value)));
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005873 }
5874 }
5875
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005876 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const override {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005877 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005878 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005879 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005880 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const override {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005881 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005882 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005883 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005884 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005885 HLongConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005886 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5887 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005888 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005889 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005890 HFloatConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005891 LOG(FATAL) << DebugName() << " is not defined for float values";
5892 UNREACHABLE();
5893 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005894 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005895 HDoubleConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005896 LOG(FATAL) << DebugName() << " is not defined for double values";
5897 UNREACHABLE();
5898 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005899
5900 DECLARE_INSTRUCTION(Ror);
5901
Artem Serovcced8ba2017-07-19 18:18:09 +01005902 protected:
5903 DEFAULT_COPY_CONSTRUCTOR(Ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005904};
5905
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005906// The value of a parameter in this method. Its location depends on
5907// the calling convention.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005908class HParameterValue final : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005909 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005910 HParameterValue(const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08005911 dex::TypeIndex type_index,
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005912 uint8_t index,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005913 DataType::Type parameter_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005914 bool is_this = false)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305915 : HExpression(kParameterValue, parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005916 dex_file_(dex_file),
5917 type_index_(type_index),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005918 index_(index) {
5919 SetPackedFlag<kFlagIsThis>(is_this);
5920 SetPackedFlag<kFlagCanBeNull>(!is_this);
5921 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005922
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005923 const DexFile& GetDexFile() const { return dex_file_; }
Andreas Gampea5b09a62016-11-17 15:21:22 -08005924 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005925 uint8_t GetIndex() const { return index_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07005926 bool IsThis() const { return GetPackedFlag<kFlagIsThis>(); }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005927
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005928 bool CanBeNull() const override { return GetPackedFlag<kFlagCanBeNull>(); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005929 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00005930
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005931 DECLARE_INSTRUCTION(ParameterValue);
5932
Artem Serovcced8ba2017-07-19 18:18:09 +01005933 protected:
5934 DEFAULT_COPY_CONSTRUCTOR(ParameterValue);
5935
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005936 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005937 // Whether or not the parameter value corresponds to 'this' argument.
Vladimir Markobd785672018-05-03 17:09:09 +01005938 static constexpr size_t kFlagIsThis = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005939 static constexpr size_t kFlagCanBeNull = kFlagIsThis + 1;
5940 static constexpr size_t kNumberOfParameterValuePackedBits = kFlagCanBeNull + 1;
5941 static_assert(kNumberOfParameterValuePackedBits <= kMaxNumberOfPackedBits,
5942 "Too many packed fields.");
5943
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005944 const DexFile& dex_file_;
Andreas Gampea5b09a62016-11-17 15:21:22 -08005945 const dex::TypeIndex type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005946 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00005947 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005948 const uint8_t index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005949};
5950
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005951class HNot final : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005952 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005953 HNot(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305954 : HUnaryOperation(kNot, result_type, input, dex_pc) {
5955 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005956
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005957 bool CanBeMoved() const override { return true; }
5958 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005959 return true;
5960 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005961
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005962 template <typename T> static T Compute(T x) { return ~x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005963
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005964 HConstant* Evaluate(HIntConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005965 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005966 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005967 HConstant* Evaluate(HLongConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005968 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005969 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005970 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005971 LOG(FATAL) << DebugName() << " is not defined for float values";
5972 UNREACHABLE();
5973 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005974 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005975 LOG(FATAL) << DebugName() << " is not defined for double values";
5976 UNREACHABLE();
5977 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01005978
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005979 DECLARE_INSTRUCTION(Not);
5980
Artem Serovcced8ba2017-07-19 18:18:09 +01005981 protected:
5982 DEFAULT_COPY_CONSTRUCTOR(Not);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005983};
5984
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005985class HBooleanNot final : public HUnaryOperation {
David Brazdil66d126e2015-04-03 16:02:44 +01005986 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005987 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305988 : HUnaryOperation(kBooleanNot, DataType::Type::kBool, input, dex_pc) {
5989 }
David Brazdil66d126e2015-04-03 16:02:44 +01005990
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005991 bool CanBeMoved() const override { return true; }
5992 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
David Brazdil66d126e2015-04-03 16:02:44 +01005993 return true;
5994 }
5995
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005996 template <typename T> static bool Compute(T x) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005997 DCHECK(IsUint<1>(x)) << x;
David Brazdil66d126e2015-04-03 16:02:44 +01005998 return !x;
5999 }
6000
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006001 HConstant* Evaluate(HIntConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006002 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01006003 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006004 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01006005 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01006006 UNREACHABLE();
6007 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006008 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00006009 LOG(FATAL) << DebugName() << " is not defined for float values";
6010 UNREACHABLE();
6011 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006012 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00006013 LOG(FATAL) << DebugName() << " is not defined for double values";
6014 UNREACHABLE();
6015 }
David Brazdil66d126e2015-04-03 16:02:44 +01006016
6017 DECLARE_INSTRUCTION(BooleanNot);
6018
Artem Serovcced8ba2017-07-19 18:18:09 +01006019 protected:
6020 DEFAULT_COPY_CONSTRUCTOR(BooleanNot);
David Brazdil66d126e2015-04-03 16:02:44 +01006021};
6022
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006023class HTypeConversion final : public HExpression<1> {
Roland Levillaindff1f282014-11-05 14:15:05 +00006024 public:
6025 // Instantiate a type conversion of `input` to `result_type`.
Vladimir Markoc8fb2112017-10-03 11:37:52 +01006026 HTypeConversion(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306027 : HExpression(kTypeConversion, result_type, SideEffects::None(), dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00006028 SetRawInputAt(0, input);
Roland Levillainf355c3f2016-03-30 19:09:03 +01006029 // Invariant: We should never generate a conversion to a Boolean value.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006030 DCHECK_NE(DataType::Type::kBool, result_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00006031 }
6032
6033 HInstruction* GetInput() const { return InputAt(0); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006034 DataType::Type GetInputType() const { return GetInput()->GetType(); }
6035 DataType::Type GetResultType() const { return GetType(); }
Roland Levillaindff1f282014-11-05 14:15:05 +00006036
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006037 bool IsClonable() const override { return true; }
6038 bool CanBeMoved() const override { return true; }
6039 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01006040 return true;
6041 }
Nicolas Geoffrayacc56ac2018-10-09 08:45:24 +01006042 // Return whether the conversion is implicit. This includes conversion to the same type.
6043 bool IsImplicitConversion() const {
6044 return DataType::IsTypeConversionImplicit(GetInputType(), GetResultType());
6045 }
Roland Levillaindff1f282014-11-05 14:15:05 +00006046
Mark Mendelle82549b2015-05-06 10:55:34 -04006047 // Try to statically evaluate the conversion and return a HConstant
6048 // containing the result. If the input cannot be converted, return nullptr.
6049 HConstant* TryStaticEvaluation() const;
6050
Roland Levillaindff1f282014-11-05 14:15:05 +00006051 DECLARE_INSTRUCTION(TypeConversion);
6052
Artem Serovcced8ba2017-07-19 18:18:09 +01006053 protected:
6054 DEFAULT_COPY_CONSTRUCTOR(TypeConversion);
Roland Levillaindff1f282014-11-05 14:15:05 +00006055};
6056
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00006057static constexpr uint32_t kNoRegNumber = -1;
6058
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006059class HNullCheck final : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006060 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00006061 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
Artem Serovd1aa7d02018-06-22 11:35:46 +01006062 // constructor. However it can only do it on a fatal slow path so execution never returns to the
6063 // instruction following the current one; thus 'SideEffects::None()' is used.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006064 HNullCheck(HInstruction* value, uint32_t dex_pc)
Artem Serovd1aa7d02018-06-22 11:35:46 +01006065 : HExpression(kNullCheck, value->GetType(), SideEffects::None(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006066 SetRawInputAt(0, value);
6067 }
6068
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006069 bool IsClonable() const override { return true; }
6070 bool CanBeMoved() const override { return true; }
6071 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07006072 return true;
6073 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01006074
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006075 bool NeedsEnvironment() const override { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006076
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006077 bool CanThrow() const override { return true; }
Calin Juravle10e244f2015-01-26 18:54:32 +00006078
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006079 bool CanBeNull() const override { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01006080
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006081 DECLARE_INSTRUCTION(NullCheck);
6082
Artem Serovcced8ba2017-07-19 18:18:09 +01006083 protected:
6084 DEFAULT_COPY_CONSTRUCTOR(NullCheck);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006085};
6086
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006087// Embeds an ArtField and all the information required by the compiler. We cache
6088// that information to avoid requiring the mutator lock every time we need it.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006089class FieldInfo : public ValueObject {
6090 public:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006091 FieldInfo(ArtField* field,
6092 MemberOffset field_offset,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006093 DataType::Type field_type,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006094 bool is_volatile,
6095 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006096 uint16_t declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006097 const DexFile& dex_file)
6098 : field_(field),
6099 field_offset_(field_offset),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006100 field_type_(field_type),
6101 is_volatile_(is_volatile),
6102 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07006103 declaring_class_def_index_(declaring_class_def_index),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006104 dex_file_(dex_file) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006105
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006106 ArtField* GetField() const { return field_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006107 MemberOffset GetFieldOffset() const { return field_offset_; }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006108 DataType::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006109 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07006110 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006111 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00006112 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006113
Alex Light3a73ffb2021-01-25 14:11:05 +00006114 bool Equals(const FieldInfo& other) const {
6115 return field_ == other.field_ &&
6116 field_offset_ == other.field_offset_ &&
6117 field_type_ == other.field_type_ &&
6118 is_volatile_ == other.is_volatile_ &&
6119 index_ == other.index_ &&
6120 declaring_class_def_index_ == other.declaring_class_def_index_ &&
6121 &dex_file_ == &other.dex_file_;
6122 }
6123
6124 std::ostream& Dump(std::ostream& os) const {
6125 os << field_ << ", off: " << field_offset_ << ", type: " << field_type_
6126 << ", volatile: " << std::boolalpha << is_volatile_ << ", index_: " << std::dec << index_
6127 << ", declaring_class: " << declaring_class_def_index_ << ", dex: " << dex_file_;
6128 return os;
6129 }
6130
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006131 private:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006132 ArtField* const field_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006133 const MemberOffset field_offset_;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006134 const DataType::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00006135 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07006136 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07006137 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006138 const DexFile& dex_file_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006139};
6140
Alex Light3a73ffb2021-01-25 14:11:05 +00006141inline bool operator==(const FieldInfo& a, const FieldInfo& b) {
6142 return a.Equals(b);
6143}
6144
6145inline std::ostream& operator<<(std::ostream& os, const FieldInfo& a) {
6146 return a.Dump(os);
6147}
6148
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006149class HInstanceFieldGet final : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006150 public:
6151 HInstanceFieldGet(HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006152 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006153 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006154 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006155 bool is_volatile,
6156 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006157 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006158 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01006159 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306160 : HExpression(kInstanceFieldGet,
6161 field_type,
6162 SideEffects::FieldReadOfType(field_type, is_volatile),
6163 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006164 field_info_(field,
6165 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006166 field_type,
6167 is_volatile,
6168 field_idx,
6169 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006170 dex_file) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006171 SetRawInputAt(0, value);
6172 }
6173
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006174 bool IsClonable() const override { return true; }
6175 bool CanBeMoved() const override { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006176
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006177 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01006178 const HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00006179 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01006180 }
6181
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006182 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01006183 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00006184 }
6185
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006186 size_t ComputeHashCode() const override {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01006187 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
6188 }
6189
Alex Light3a73ffb2021-01-25 14:11:05 +00006190 bool IsFieldAccess() const override { return true; }
6191 const FieldInfo& GetFieldInfo() const override { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006192 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006193 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006194 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006195
Vladimir Marko61b92282017-10-11 13:23:17 +01006196 void SetType(DataType::Type new_type) {
6197 DCHECK(DataType::IsIntegralType(GetType()));
6198 DCHECK(DataType::IsIntegralType(new_type));
6199 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
6200 SetPackedField<TypeField>(new_type);
6201 }
6202
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006203 DECLARE_INSTRUCTION(InstanceFieldGet);
6204
Artem Serovcced8ba2017-07-19 18:18:09 +01006205 protected:
6206 DEFAULT_COPY_CONSTRUCTOR(InstanceFieldGet);
6207
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006208 private:
6209 const FieldInfo field_info_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006210};
6211
Alex Light3a73ffb2021-01-25 14:11:05 +00006212class HPredicatedInstanceFieldGet final : public HExpression<2> {
6213 public:
6214 HPredicatedInstanceFieldGet(HInstanceFieldGet* orig,
6215 HInstruction* target,
6216 HInstruction* default_val)
6217 : HExpression(kPredicatedInstanceFieldGet,
6218 orig->GetFieldType(),
6219 orig->GetSideEffects(),
6220 orig->GetDexPc()),
6221 field_info_(orig->GetFieldInfo()) {
6222 // NB Default-val is at 0 so we can avoid doing a move.
6223 SetRawInputAt(1, target);
6224 SetRawInputAt(0, default_val);
6225 }
6226
6227 HPredicatedInstanceFieldGet(HInstruction* value,
6228 ArtField* field,
6229 HInstruction* default_value,
6230 DataType::Type field_type,
6231 MemberOffset field_offset,
6232 bool is_volatile,
6233 uint32_t field_idx,
6234 uint16_t declaring_class_def_index,
6235 const DexFile& dex_file,
6236 uint32_t dex_pc)
6237 : HExpression(kPredicatedInstanceFieldGet,
6238 field_type,
6239 SideEffects::FieldReadOfType(field_type, is_volatile),
6240 dex_pc),
6241 field_info_(field,
6242 field_offset,
6243 field_type,
6244 is_volatile,
6245 field_idx,
6246 declaring_class_def_index,
6247 dex_file) {
6248 SetRawInputAt(0, value);
6249 SetRawInputAt(1, default_value);
6250 }
6251
6252 bool IsClonable() const override {
6253 return true;
6254 }
6255 bool CanBeMoved() const override {
6256 return !IsVolatile();
6257 }
6258
6259 HInstruction* GetDefaultValue() const {
6260 return InputAt(0);
6261 }
6262 HInstruction* GetTarget() const {
6263 return InputAt(1);
6264 }
6265
6266 bool InstructionDataEquals(const HInstruction* other) const override {
6267 const HPredicatedInstanceFieldGet* other_get = other->AsPredicatedInstanceFieldGet();
6268 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue() &&
6269 GetDefaultValue() == other_get->GetDefaultValue();
6270 }
6271
6272 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
6273 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
6274 }
6275
6276 size_t ComputeHashCode() const override {
6277 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
6278 }
6279
6280 bool IsFieldAccess() const override { return true; }
6281 const FieldInfo& GetFieldInfo() const override { return field_info_; }
6282 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
6283 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
6284 bool IsVolatile() const { return field_info_.IsVolatile(); }
6285
6286 void SetType(DataType::Type new_type) {
6287 DCHECK(DataType::IsIntegralType(GetType()));
6288 DCHECK(DataType::IsIntegralType(new_type));
6289 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
6290 SetPackedField<TypeField>(new_type);
6291 }
6292
6293 DECLARE_INSTRUCTION(PredicatedInstanceFieldGet);
6294
6295 protected:
6296 DEFAULT_COPY_CONSTRUCTOR(PredicatedInstanceFieldGet);
6297
6298 private:
6299 const FieldInfo field_info_;
6300};
6301
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006302class HInstanceFieldSet final : public HExpression<2> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006303 public:
6304 HInstanceFieldSet(HInstruction* object,
6305 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006306 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006307 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006308 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006309 bool is_volatile,
6310 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006311 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006312 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01006313 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006314 : HExpression(kInstanceFieldSet,
6315 SideEffects::FieldWriteOfType(field_type, is_volatile),
6316 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006317 field_info_(field,
6318 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006319 field_type,
6320 is_volatile,
6321 field_idx,
6322 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006323 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006324 SetPackedFlag<kFlagValueCanBeNull>(true);
Alex Light3a73ffb2021-01-25 14:11:05 +00006325 SetPackedFlag<kFlagIsPredicatedSet>(false);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006326 SetRawInputAt(0, object);
6327 SetRawInputAt(1, value);
6328 }
6329
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006330 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01006331
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006332 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01006333 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00006334 }
6335
Alex Light3a73ffb2021-01-25 14:11:05 +00006336 bool IsFieldAccess() const override { return true; }
6337 const FieldInfo& GetFieldInfo() const override { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006338 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006339 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006340 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006341 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006342 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
6343 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Alex Light3a73ffb2021-01-25 14:11:05 +00006344 bool GetIsPredicatedSet() const { return GetPackedFlag<kFlagIsPredicatedSet>(); }
6345 void SetIsPredicatedSet(bool value = true) { SetPackedFlag<kFlagIsPredicatedSet>(value); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006346
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006347 DECLARE_INSTRUCTION(InstanceFieldSet);
6348
Artem Serovcced8ba2017-07-19 18:18:09 +01006349 protected:
6350 DEFAULT_COPY_CONSTRUCTOR(InstanceFieldSet);
6351
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006352 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006353 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
Alex Light3a73ffb2021-01-25 14:11:05 +00006354 static constexpr size_t kFlagIsPredicatedSet = kFlagValueCanBeNull + 1;
6355 static constexpr size_t kNumberOfInstanceFieldSetPackedBits = kFlagIsPredicatedSet + 1;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006356 static_assert(kNumberOfInstanceFieldSetPackedBits <= kMaxNumberOfPackedBits,
6357 "Too many packed fields.");
6358
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006359 const FieldInfo field_info_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006360};
6361
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006362class HArrayGet final : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006363 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006364 HArrayGet(HInstruction* array,
6365 HInstruction* index,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006366 DataType::Type type,
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006367 uint32_t dex_pc)
6368 : HArrayGet(array,
6369 index,
6370 type,
6371 SideEffects::ArrayReadOfType(type),
6372 dex_pc,
Andreas Gampe3db70682018-12-26 15:12:03 -08006373 /* is_string_char_at= */ false) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306374 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006375
6376 HArrayGet(HInstruction* array,
6377 HInstruction* index,
6378 DataType::Type type,
6379 SideEffects side_effects,
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006380 uint32_t dex_pc,
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006381 bool is_string_char_at)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306382 : HExpression(kArrayGet, type, side_effects, dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006383 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006384 SetRawInputAt(0, array);
6385 SetRawInputAt(1, index);
6386 }
6387
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006388 bool IsClonable() const override { return true; }
6389 bool CanBeMoved() const override { return true; }
6390 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07006391 return true;
6392 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006393 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const override {
Calin Juravle77520bc2015-01-12 18:45:46 +00006394 // TODO: We can be smarter here.
Vladimir Marko66d691d2017-04-07 17:53:39 +01006395 // Currently, unless the array is the result of NewArray, the array access is always
6396 // preceded by some form of null NullCheck necessary for the bounds check, usually
6397 // implicit null check on the ArrayLength input to BoundsCheck or Deoptimize for
6398 // dynamic BCE. There are cases when these could be removed to produce better code.
6399 // If we ever add optimizations to do so we should allow an implicit check here
6400 // (as long as the address falls in the first page).
6401 //
6402 // As an example of such fancy optimization, we could eliminate BoundsCheck for
6403 // a = cond ? new int[1] : null;
6404 // a[0]; // The Phi does not need bounds check for either input.
Calin Juravle77520bc2015-01-12 18:45:46 +00006405 return false;
6406 }
6407
David Brazdil4833f5a2015-12-16 10:37:39 +00006408 bool IsEquivalentOf(HArrayGet* other) const {
6409 bool result = (GetDexPc() == other->GetDexPc());
6410 if (kIsDebugBuild && result) {
6411 DCHECK_EQ(GetBlock(), other->GetBlock());
6412 DCHECK_EQ(GetArray(), other->GetArray());
6413 DCHECK_EQ(GetIndex(), other->GetIndex());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006414 if (DataType::IsIntOrLongType(GetType())) {
6415 DCHECK(DataType::IsFloatingPointType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00006416 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006417 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
6418 DCHECK(DataType::IsIntOrLongType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00006419 }
6420 }
6421 return result;
6422 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01006423
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006424 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
6425
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006426 HInstruction* GetArray() const { return InputAt(0); }
6427 HInstruction* GetIndex() const { return InputAt(1); }
6428
Vladimir Marko61b92282017-10-11 13:23:17 +01006429 void SetType(DataType::Type new_type) {
6430 DCHECK(DataType::IsIntegralType(GetType()));
6431 DCHECK(DataType::IsIntegralType(new_type));
6432 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
6433 SetPackedField<TypeField>(new_type);
6434 }
6435
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006436 DECLARE_INSTRUCTION(ArrayGet);
6437
Artem Serovcced8ba2017-07-19 18:18:09 +01006438 protected:
6439 DEFAULT_COPY_CONSTRUCTOR(ArrayGet);
6440
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006441 private:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006442 // We treat a String as an array, creating the HArrayGet from String.charAt()
6443 // intrinsic in the instruction simplifier. We can always determine whether
6444 // a particular HArrayGet is actually a String.charAt() by looking at the type
6445 // of the input but that requires holding the mutator lock, so we prefer to use
6446 // a flag, so that code generators don't need to do the locking.
Vladimir Markobd785672018-05-03 17:09:09 +01006447 static constexpr size_t kFlagIsStringCharAt = kNumberOfGenericPackedBits;
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006448 static constexpr size_t kNumberOfArrayGetPackedBits = kFlagIsStringCharAt + 1;
6449 static_assert(kNumberOfArrayGetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6450 "Too many packed fields.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006451};
6452
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006453class HArraySet final : public HExpression<3> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006454 public:
6455 HArraySet(HInstruction* array,
6456 HInstruction* index,
6457 HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006458 DataType::Type expected_component_type,
Aart Bik18b36ab2016-04-13 16:41:35 -07006459 uint32_t dex_pc)
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006460 : HArraySet(array,
6461 index,
6462 value,
6463 expected_component_type,
6464 // Make a best guess for side effects now, may be refined during SSA building.
6465 ComputeSideEffects(GetComponentType(value->GetType(), expected_component_type)),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306466 dex_pc) {
6467 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006468
6469 HArraySet(HInstruction* array,
6470 HInstruction* index,
6471 HInstruction* value,
6472 DataType::Type expected_component_type,
6473 SideEffects side_effects,
6474 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006475 : HExpression(kArraySet, side_effects, dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006476 SetPackedField<ExpectedComponentTypeField>(expected_component_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006477 SetPackedFlag<kFlagNeedsTypeCheck>(value->GetType() == DataType::Type::kReference);
Vladimir Markoa1de9182016-02-25 11:37:38 +00006478 SetPackedFlag<kFlagValueCanBeNull>(true);
6479 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(false);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006480 SetRawInputAt(0, array);
6481 SetRawInputAt(1, index);
6482 SetRawInputAt(2, value);
6483 }
6484
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006485 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01006486
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006487 bool NeedsEnvironment() const override {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006488 // We call a runtime method to throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006489 return NeedsTypeCheck();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006490 }
6491
Mingyao Yang81014cb2015-06-02 03:16:27 -07006492 // Can throw ArrayStoreException.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006493 bool CanThrow() const override { return NeedsTypeCheck(); }
Mingyao Yang81014cb2015-06-02 03:16:27 -07006494
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006495 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const override {
Calin Juravle77520bc2015-01-12 18:45:46 +00006496 // TODO: Same as for ArrayGet.
6497 return false;
6498 }
6499
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006500 void ClearNeedsTypeCheck() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006501 SetPackedFlag<kFlagNeedsTypeCheck>(false);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006502 }
6503
Nicolas Geoffray07276db2015-05-18 14:22:09 +01006504 void ClearValueCanBeNull() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006505 SetPackedFlag<kFlagValueCanBeNull>(false);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01006506 }
6507
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006508 void SetStaticTypeOfArrayIsObjectArray() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006509 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(true);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006510 }
6511
Vladimir Markoa1de9182016-02-25 11:37:38 +00006512 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
6513 bool NeedsTypeCheck() const { return GetPackedFlag<kFlagNeedsTypeCheck>(); }
6514 bool StaticTypeOfArrayIsObjectArray() const {
6515 return GetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>();
6516 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006517
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006518 HInstruction* GetArray() const { return InputAt(0); }
6519 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006520 HInstruction* GetValue() const { return InputAt(2); }
6521
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006522 DataType::Type GetComponentType() const {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006523 return GetComponentType(GetValue()->GetType(), GetRawExpectedComponentType());
6524 }
6525
6526 static DataType::Type GetComponentType(DataType::Type value_type,
6527 DataType::Type expected_component_type) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006528 // The Dex format does not type floating point index operations. Since the
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006529 // `expected_component_type` comes from SSA building and can therefore not
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006530 // be correct, we also check what is the value type. If it is a floating
6531 // point type, we must use that type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006532 return ((value_type == DataType::Type::kFloat32) || (value_type == DataType::Type::kFloat64))
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006533 ? value_type
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006534 : expected_component_type;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006535 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01006536
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006537 DataType::Type GetRawExpectedComponentType() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006538 return GetPackedField<ExpectedComponentTypeField>();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01006539 }
6540
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006541 static SideEffects ComputeSideEffects(DataType::Type type) {
6542 return SideEffects::ArrayWriteOfType(type).Union(SideEffectsForArchRuntimeCalls(type));
Aart Bik18b36ab2016-04-13 16:41:35 -07006543 }
6544
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006545 static SideEffects SideEffectsForArchRuntimeCalls(DataType::Type value_type) {
6546 return (value_type == DataType::Type::kReference) ? SideEffects::CanTriggerGC()
6547 : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006548 }
6549
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006550 DECLARE_INSTRUCTION(ArraySet);
6551
Artem Serovcced8ba2017-07-19 18:18:09 +01006552 protected:
6553 DEFAULT_COPY_CONSTRUCTOR(ArraySet);
6554
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006555 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006556 static constexpr size_t kFieldExpectedComponentType = kNumberOfGenericPackedBits;
6557 static constexpr size_t kFieldExpectedComponentTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006558 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00006559 static constexpr size_t kFlagNeedsTypeCheck =
6560 kFieldExpectedComponentType + kFieldExpectedComponentTypeSize;
6561 static constexpr size_t kFlagValueCanBeNull = kFlagNeedsTypeCheck + 1;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006562 // Cached information for the reference_type_info_ so that codegen
6563 // does not need to inspect the static type.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006564 static constexpr size_t kFlagStaticTypeOfArrayIsObjectArray = kFlagValueCanBeNull + 1;
6565 static constexpr size_t kNumberOfArraySetPackedBits =
6566 kFlagStaticTypeOfArrayIsObjectArray + 1;
6567 static_assert(kNumberOfArraySetPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6568 using ExpectedComponentTypeField =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006569 BitField<DataType::Type, kFieldExpectedComponentType, kFieldExpectedComponentTypeSize>;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006570};
6571
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006572class HArrayLength final : public HExpression<1> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006573 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006574 HArrayLength(HInstruction* array, uint32_t dex_pc, bool is_string_length = false)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306575 : HExpression(kArrayLength, DataType::Type::kInt32, SideEffects::None(), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006576 SetPackedFlag<kFlagIsStringLength>(is_string_length);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01006577 // Note that arrays do not change length, so the instruction does not
6578 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006579 SetRawInputAt(0, array);
6580 }
6581
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006582 bool IsClonable() const override { return true; }
6583 bool CanBeMoved() const override { return true; }
6584 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07006585 return true;
6586 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006587 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Calin Juravle641547a2015-04-21 22:08:51 +01006588 return obj == InputAt(0);
6589 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01006590
Vladimir Markodce016e2016-04-28 13:10:02 +01006591 bool IsStringLength() const { return GetPackedFlag<kFlagIsStringLength>(); }
6592
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006593 DECLARE_INSTRUCTION(ArrayLength);
6594
Artem Serovcced8ba2017-07-19 18:18:09 +01006595 protected:
6596 DEFAULT_COPY_CONSTRUCTOR(ArrayLength);
6597
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006598 private:
Vladimir Markodce016e2016-04-28 13:10:02 +01006599 // We treat a String as an array, creating the HArrayLength from String.length()
6600 // or String.isEmpty() intrinsic in the instruction simplifier. We can always
6601 // determine whether a particular HArrayLength is actually a String.length() by
6602 // looking at the type of the input but that requires holding the mutator lock, so
6603 // we prefer to use a flag, so that code generators don't need to do the locking.
Vladimir Markobd785672018-05-03 17:09:09 +01006604 static constexpr size_t kFlagIsStringLength = kNumberOfGenericPackedBits;
Vladimir Markodce016e2016-04-28 13:10:02 +01006605 static constexpr size_t kNumberOfArrayLengthPackedBits = kFlagIsStringLength + 1;
6606 static_assert(kNumberOfArrayLengthPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6607 "Too many packed fields.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006608};
6609
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006610class HBoundsCheck final : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006611 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00006612 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
Artem Serovd1aa7d02018-06-22 11:35:46 +01006613 // constructor. However it can only do it on a fatal slow path so execution never returns to the
6614 // instruction following the current one; thus 'SideEffects::None()' is used.
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006615 HBoundsCheck(HInstruction* index,
6616 HInstruction* length,
6617 uint32_t dex_pc,
Vladimir Marko0259c242017-12-04 11:27:47 +00006618 bool is_string_char_at = false)
Artem Serovd1aa7d02018-06-22 11:35:46 +01006619 : HExpression(kBoundsCheck, index->GetType(), SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006620 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(index->GetType()));
Vladimir Marko0259c242017-12-04 11:27:47 +00006621 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006622 SetRawInputAt(0, index);
6623 SetRawInputAt(1, length);
6624 }
6625
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006626 bool IsClonable() const override { return true; }
6627 bool CanBeMoved() const override { return true; }
6628 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07006629 return true;
6630 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01006631
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006632 bool NeedsEnvironment() const override { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006633
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006634 bool CanThrow() const override { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01006635
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00006636 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006637
Alexandre Ramese6dbf482015-10-19 10:10:41 +01006638 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006639
6640 DECLARE_INSTRUCTION(BoundsCheck);
6641
Artem Serovcced8ba2017-07-19 18:18:09 +01006642 protected:
6643 DEFAULT_COPY_CONSTRUCTOR(BoundsCheck);
6644
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006645 private:
Vladimir Markobd785672018-05-03 17:09:09 +01006646 static constexpr size_t kFlagIsStringCharAt = kNumberOfGenericPackedBits;
Vladimir Markof02046e2018-10-02 15:31:32 +01006647 static constexpr size_t kNumberOfBoundsCheckPackedBits = kFlagIsStringCharAt + 1;
6648 static_assert(kNumberOfBoundsCheckPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6649 "Too many packed fields.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006650};
6651
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006652class HSuspendCheck final : public HExpression<0> {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006653 public:
David Brazdil86ea7ee2016-02-16 09:26:07 +00006654 explicit HSuspendCheck(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01006655 : HExpression(kSuspendCheck, SideEffects::CanTriggerGC(), dex_pc),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306656 slow_path_(nullptr) {
6657 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006658
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006659 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01006660
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006661 bool NeedsEnvironment() const override {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006662 return true;
6663 }
6664
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006665 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
6666 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006667
6668 DECLARE_INSTRUCTION(SuspendCheck);
6669
Artem Serovcced8ba2017-07-19 18:18:09 +01006670 protected:
6671 DEFAULT_COPY_CONSTRUCTOR(SuspendCheck);
6672
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006673 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006674 // Only used for code generation, in order to share the same slow path between back edges
6675 // of a same loop.
6676 SlowPathCode* slow_path_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006677};
6678
David Srbecky0cf44932015-12-09 14:09:59 +00006679// Pseudo-instruction which provides the native debugger with mapping information.
6680// It ensures that we can generate line number and local variables at this point.
Vladimir Markobd785672018-05-03 17:09:09 +01006681class HNativeDebugInfo : public HExpression<0> {
David Srbecky0cf44932015-12-09 14:09:59 +00006682 public:
6683 explicit HNativeDebugInfo(uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006684 : HExpression<0>(kNativeDebugInfo, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306685 }
David Srbecky0cf44932015-12-09 14:09:59 +00006686
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006687 bool NeedsEnvironment() const override {
David Srbecky0cf44932015-12-09 14:09:59 +00006688 return true;
6689 }
6690
6691 DECLARE_INSTRUCTION(NativeDebugInfo);
6692
Artem Serovcced8ba2017-07-19 18:18:09 +01006693 protected:
6694 DEFAULT_COPY_CONSTRUCTOR(NativeDebugInfo);
David Srbecky0cf44932015-12-09 14:09:59 +00006695};
6696
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006697/**
6698 * Instruction to load a Class object.
6699 */
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006700class HLoadClass final : public HInstruction {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006701 public:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006702 // Determines how to load the Class.
6703 enum class LoadKind {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006704 // We cannot load this class. See HSharpening::SharpenLoadClass.
6705 kInvalid = -1,
6706
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006707 // Use the Class* from the method's own ArtMethod*.
6708 kReferrersClass,
6709
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006710 // Use PC-relative boot image Class* address that will be known at link time.
Vladimir Marko764d4542017-05-16 10:31:41 +01006711 // Used for boot image classes referenced by boot image code.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006712 kBootImageLinkTimePcRelative,
6713
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006714 // Load from an entry in the .data.bimg.rel.ro using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006715 // Used for boot image classes referenced by apps in AOT-compiled code.
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006716 kBootImageRelRo,
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006717
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006718 // Load from an entry in the .bss section using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006719 // Used for classes outside boot image referenced by AOT-compiled app and boot image code.
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006720 kBssEntry,
6721
Vladimir Marko8f63f102020-09-28 12:10:28 +01006722 // Load from an entry for public class in the .bss section using a PC-relative load.
6723 // Used for classes that were unresolved during AOT-compilation outside the literal
6724 // package of the compiling class. Such classes are accessible only if they are public
6725 // and the .bss entry shall therefore be filled only if the resolved class is public.
6726 kBssEntryPublic,
6727
6728 // Load from an entry for package class in the .bss section using a PC-relative load.
6729 // Used for classes that were unresolved during AOT-compilation but within the literal
6730 // package of the compiling class. Such classes are accessible if they are public or
6731 // in the same package which, given the literal package match, requires only matching
6732 // defining class loader and the .bss entry shall therefore be filled only if at least
6733 // one of those conditions holds. Note that all code in an oat file belongs to classes
6734 // with the same defining class loader.
6735 kBssEntryPackage,
6736
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006737 // Use a known boot image Class* address, embedded in the code by the codegen.
6738 // Used for boot image classes referenced by apps in JIT-compiled code.
6739 kJitBootImageAddress,
6740
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006741 // Load from the root table associated with the JIT compiled method.
6742 kJitTableAddress,
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006743
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006744 // Load using a simple runtime call. This is the fall-back load kind when
6745 // the codegen is unable to use another appropriate kind.
6746 kRuntimeCall,
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006747
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006748 kLast = kRuntimeCall
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006749 };
6750
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01006751 HLoadClass(HCurrentMethod* current_method,
Andreas Gampea5b09a62016-11-17 15:21:22 -08006752 dex::TypeIndex type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01006753 const DexFile& dex_file,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006754 Handle<mirror::Class> klass,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006755 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01006756 uint32_t dex_pc,
Nicolas Geoffray56876342016-12-16 16:09:08 +00006757 bool needs_access_check)
Vladimir Markobd785672018-05-03 17:09:09 +01006758 : HInstruction(kLoadClass,
6759 DataType::Type::kReference,
6760 SideEffectsForArchRuntimeCalls(),
6761 dex_pc),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006762 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006763 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01006764 dex_file_(dex_file),
Vladimir Marko175e7862018-03-27 09:03:13 +00006765 klass_(klass) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01006766 // Referrers class should not need access check. We never inline unverified
6767 // methods so we can't possibly end up in this situation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006768 DCHECK(!is_referrers_class || !needs_access_check);
6769
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006770 SetPackedField<LoadKindField>(
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006771 is_referrers_class ? LoadKind::kReferrersClass : LoadKind::kRuntimeCall);
Vladimir Markoa1de9182016-02-25 11:37:38 +00006772 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
Nicolas Geoffray56876342016-12-16 16:09:08 +00006773 SetPackedFlag<kFlagIsInBootImage>(false);
Vladimir Markoa1de9182016-02-25 11:37:38 +00006774 SetPackedFlag<kFlagGenerateClInitCheck>(false);
Vladimir Marko175e7862018-03-27 09:03:13 +00006775 SetPackedFlag<kFlagValidLoadedClassRTI>(false);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006776 }
6777
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006778 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01006779
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006780 void SetLoadKind(LoadKind load_kind);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006781
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006782 LoadKind GetLoadKind() const {
6783 return GetPackedField<LoadKindField>();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01006784 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006785
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006786 bool HasPcRelativeLoadKind() const {
6787 return GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
6788 GetLoadKind() == LoadKind::kBootImageRelRo ||
Vladimir Marko8f63f102020-09-28 12:10:28 +01006789 GetLoadKind() == LoadKind::kBssEntry ||
6790 GetLoadKind() == LoadKind::kBssEntryPublic ||
6791 GetLoadKind() == LoadKind::kBssEntryPackage;
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006792 }
6793
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006794 bool CanBeMoved() const override { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006795
Yi Kong39402542019-03-24 02:47:16 -07006796 bool InstructionDataEquals(const HInstruction* other) const override;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006797
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006798 size_t ComputeHashCode() const override { return type_index_.index_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006799
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006800 bool CanBeNull() const override { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006801
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006802 bool NeedsEnvironment() const override {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006803 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006804 }
6805
Calin Juravle0ba218d2015-05-19 18:46:01 +01006806 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006807 SetPackedFlag<kFlagGenerateClInitCheck>(generate_clinit_check);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006808 }
6809
6810 bool CanCallRuntime() const {
Vladimir Marko41559982017-01-06 14:04:23 +00006811 return NeedsAccessCheck() ||
6812 MustGenerateClinitCheck() ||
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006813 GetLoadKind() == LoadKind::kRuntimeCall ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006814 GetLoadKind() == LoadKind::kBssEntry;
Calin Juravle98893e12015-10-02 21:05:03 +01006815 }
6816
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006817 bool CanThrow() const override {
Vladimir Marko41559982017-01-06 14:04:23 +00006818 return NeedsAccessCheck() ||
6819 MustGenerateClinitCheck() ||
6820 // If the class is in the boot image, the lookup in the runtime call cannot throw.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006821 ((GetLoadKind() == LoadKind::kRuntimeCall ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006822 GetLoadKind() == LoadKind::kBssEntry) &&
6823 !IsInBootImage());
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006824 }
6825
Calin Juravleacf735c2015-02-12 15:25:22 +00006826 ReferenceTypeInfo GetLoadedClassRTI() {
Vladimir Marko175e7862018-03-27 09:03:13 +00006827 if (GetPackedFlag<kFlagValidLoadedClassRTI>()) {
6828 // Note: The is_exact flag from the return value should not be used.
Andreas Gampe3db70682018-12-26 15:12:03 -08006829 return ReferenceTypeInfo::CreateUnchecked(klass_, /* is_exact= */ true);
Vladimir Marko175e7862018-03-27 09:03:13 +00006830 } else {
6831 return ReferenceTypeInfo::CreateInvalid();
6832 }
Calin Juravleacf735c2015-02-12 15:25:22 +00006833 }
6834
Vladimir Marko175e7862018-03-27 09:03:13 +00006835 // Loaded class RTI is marked as valid by RTP if the klass_ is admissible.
6836 void SetValidLoadedClassRTI() REQUIRES_SHARED(Locks::mutator_lock_) {
6837 DCHECK(klass_ != nullptr);
6838 SetPackedFlag<kFlagValidLoadedClassRTI>(true);
Calin Juravleacf735c2015-02-12 15:25:22 +00006839 }
6840
Andreas Gampea5b09a62016-11-17 15:21:22 -08006841 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006842 const DexFile& GetDexFile() const { return dex_file_; }
6843
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006844 static SideEffects SideEffectsForArchRuntimeCalls() {
6845 return SideEffects::CanTriggerGC();
6846 }
6847
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006848 bool IsReferrersClass() const { return GetLoadKind() == LoadKind::kReferrersClass; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006849 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006850 bool IsInBootImage() const { return GetPackedFlag<kFlagIsInBootImage>(); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006851 bool MustGenerateClinitCheck() const { return GetPackedFlag<kFlagGenerateClInitCheck>(); }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006852
Vladimir Markoa9f303c2018-07-20 16:43:56 +01006853 bool MustResolveTypeOnSlowPath() const {
6854 // Check that this instruction has a slow path.
Vladimir Marko8f63f102020-09-28 12:10:28 +01006855 LoadKind load_kind = GetLoadKind();
6856 DCHECK(load_kind != LoadKind::kRuntimeCall); // kRuntimeCall calls on main path.
6857 bool must_resolve_type_on_slow_path =
6858 load_kind == LoadKind::kBssEntry ||
6859 load_kind == LoadKind::kBssEntryPublic ||
6860 load_kind == LoadKind::kBssEntryPackage;
6861 DCHECK(must_resolve_type_on_slow_path || MustGenerateClinitCheck());
6862 return must_resolve_type_on_slow_path;
Vladimir Markoa9f303c2018-07-20 16:43:56 +01006863 }
6864
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006865 void MarkInBootImage() {
6866 SetPackedFlag<kFlagIsInBootImage>(true);
6867 }
6868
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006869 void AddSpecialInput(HInstruction* special_input);
6870
6871 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01006872 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006873 return ArrayRef<HUserRecord<HInstruction*>>(
6874 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
6875 }
6876
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006877 Handle<mirror::Class> GetClass() const {
6878 return klass_;
6879 }
6880
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006881 DECLARE_INSTRUCTION(LoadClass);
6882
Artem Serovcced8ba2017-07-19 18:18:09 +01006883 protected:
6884 DEFAULT_COPY_CONSTRUCTOR(LoadClass);
6885
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006886 private:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006887 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfGenericPackedBits;
Vladimir Marko41559982017-01-06 14:04:23 +00006888 static constexpr size_t kFlagIsInBootImage = kFlagNeedsAccessCheck + 1;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006889 // Whether this instruction must generate the initialization check.
6890 // Used for code generation.
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006891 static constexpr size_t kFlagGenerateClInitCheck = kFlagIsInBootImage + 1;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006892 static constexpr size_t kFieldLoadKind = kFlagGenerateClInitCheck + 1;
6893 static constexpr size_t kFieldLoadKindSize =
6894 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
Vladimir Marko175e7862018-03-27 09:03:13 +00006895 static constexpr size_t kFlagValidLoadedClassRTI = kFieldLoadKind + kFieldLoadKindSize;
6896 static constexpr size_t kNumberOfLoadClassPackedBits = kFlagValidLoadedClassRTI + 1;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006897 static_assert(kNumberOfLoadClassPackedBits < kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006898 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
6899
6900 static bool HasTypeReference(LoadKind load_kind) {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006901 return load_kind == LoadKind::kReferrersClass ||
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006902 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006903 load_kind == LoadKind::kBssEntry ||
Vladimir Marko8f63f102020-09-28 12:10:28 +01006904 load_kind == LoadKind::kBssEntryPublic ||
6905 load_kind == LoadKind::kBssEntryPackage ||
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006906 load_kind == LoadKind::kRuntimeCall;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006907 }
6908
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006909 void SetLoadKindInternal(LoadKind load_kind);
6910
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006911 // The special input is the HCurrentMethod for kRuntimeCall or kReferrersClass.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006912 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko8f63f102020-09-28 12:10:28 +01006913 // for PC-relative loads, i.e. kBssEntry* or kBootImageLinkTimePcRelative.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006914 HUserRecord<HInstruction*> special_input_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006915
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006916 // A type index and dex file where the class can be accessed. The dex file can be:
6917 // - The compiling method's dex file if the class is defined there too.
6918 // - The compiling method's dex file if the class is referenced there.
6919 // - The dex file where the class is defined. When the load kind can only be
Vladimir Marko8f63f102020-09-28 12:10:28 +01006920 // kBssEntry* or kRuntimeCall, we cannot emit code for this `HLoadClass`.
Andreas Gampea5b09a62016-11-17 15:21:22 -08006921 const dex::TypeIndex type_index_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006922 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006923
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006924 Handle<mirror::Class> klass_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006925};
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006926std::ostream& operator<<(std::ostream& os, HLoadClass::LoadKind rhs);
6927
6928// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
Vladimir Marko28e012a2017-12-07 11:22:59 +00006929inline void HLoadClass::SetLoadKind(LoadKind load_kind) {
6930 // The load kind should be determined before inserting the instruction to the graph.
6931 DCHECK(GetBlock() == nullptr);
6932 DCHECK(GetEnvironment() == nullptr);
6933 SetPackedField<LoadKindField>(load_kind);
6934 if (load_kind != LoadKind::kRuntimeCall && load_kind != LoadKind::kReferrersClass) {
6935 special_input_ = HUserRecord<HInstruction*>(nullptr);
6936 }
6937 if (!NeedsEnvironment()) {
6938 SetSideEffects(SideEffects::None());
6939 }
6940}
6941
6942// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006943inline void HLoadClass::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07006944 // The special input is used for PC-relative loads on some architectures,
6945 // including literal pool loads, which are PC-relative too.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006946 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006947 GetLoadKind() == LoadKind::kBootImageRelRo ||
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006948 GetLoadKind() == LoadKind::kBssEntry ||
Vladimir Marko8f63f102020-09-28 12:10:28 +01006949 GetLoadKind() == LoadKind::kBssEntryPublic ||
6950 GetLoadKind() == LoadKind::kBssEntryPackage ||
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006951 GetLoadKind() == LoadKind::kJitBootImageAddress) << GetLoadKind();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006952 DCHECK(special_input_.GetInstruction() == nullptr);
6953 special_input_ = HUserRecord<HInstruction*>(special_input);
6954 special_input->AddUseAt(this, 0);
6955}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006956
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006957class HLoadString final : public HInstruction {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006958 public:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006959 // Determines how to load the String.
6960 enum class LoadKind {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006961 // Use PC-relative boot image String* address that will be known at link time.
Vladimir Marko764d4542017-05-16 10:31:41 +01006962 // Used for boot image strings referenced by boot image code.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006963 kBootImageLinkTimePcRelative,
6964
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006965 // Load from an entry in the .data.bimg.rel.ro using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006966 // Used for boot image strings referenced by apps in AOT-compiled code.
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006967 kBootImageRelRo,
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006968
Vladimir Markoaad75c62016-10-03 08:46:48 +00006969 // Load from an entry in the .bss section using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006970 // Used for strings outside boot image referenced by AOT-compiled app and boot image code.
Vladimir Markoaad75c62016-10-03 08:46:48 +00006971 kBssEntry,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006972
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006973 // Use a known boot image String* address, embedded in the code by the codegen.
6974 // Used for boot image strings referenced by apps in JIT-compiled code.
6975 kJitBootImageAddress,
6976
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006977 // Load from the root table associated with the JIT compiled method.
6978 kJitTableAddress,
6979
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006980 // Load using a simple runtime call. This is the fall-back load kind when
6981 // the codegen is unable to use another appropriate kind.
6982 kRuntimeCall,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006983
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006984 kLast = kRuntimeCall,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006985 };
6986
Nicolas Geoffray917d0162015-11-24 18:25:35 +00006987 HLoadString(HCurrentMethod* current_method,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006988 dex::StringIndex string_index,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006989 const DexFile& dex_file,
6990 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006991 : HInstruction(kLoadString,
6992 DataType::Type::kReference,
6993 SideEffectsForArchRuntimeCalls(),
6994 dex_pc),
Vladimir Marko372f10e2016-05-17 16:30:10 +01006995 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006996 string_index_(string_index),
6997 dex_file_(dex_file) {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006998 SetPackedField<LoadKindField>(LoadKind::kRuntimeCall);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01006999 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00007000
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007001 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01007002
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007003 void SetLoadKind(LoadKind load_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007004
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007005 LoadKind GetLoadKind() const {
7006 return GetPackedField<LoadKindField>();
7007 }
7008
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007009 bool HasPcRelativeLoadKind() const {
7010 return GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
7011 GetLoadKind() == LoadKind::kBootImageRelRo ||
7012 GetLoadKind() == LoadKind::kBssEntry;
7013 }
7014
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007015 const DexFile& GetDexFile() const {
7016 return dex_file_;
7017 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007018
Andreas Gampe8a0128a2016-11-28 07:38:35 -08007019 dex::StringIndex GetStringIndex() const {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007020 return string_index_;
7021 }
7022
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007023 Handle<mirror::String> GetString() const {
7024 return string_;
7025 }
7026
7027 void SetString(Handle<mirror::String> str) {
7028 string_ = str;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007029 }
7030
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007031 bool CanBeMoved() const override { return true; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00007032
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007033 bool InstructionDataEquals(const HInstruction* other) const override;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00007034
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007035 size_t ComputeHashCode() const override { return string_index_.index_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00007036
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007037 // Will call the runtime if we need to load the string through
7038 // the dex cache and the string is not guaranteed to be there yet.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007039 bool NeedsEnvironment() const override {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007040 LoadKind load_kind = GetLoadKind();
Vladimir Marko764d4542017-05-16 10:31:41 +01007041 if (load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007042 load_kind == LoadKind::kBootImageRelRo ||
Vladimir Marko8e524ad2018-07-13 10:27:43 +01007043 load_kind == LoadKind::kJitBootImageAddress ||
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007044 load_kind == LoadKind::kJitTableAddress) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007045 return false;
7046 }
Vladimir Marko4d1be4922017-01-06 14:43:11 +00007047 return true;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007048 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00007049
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007050 bool CanBeNull() const override { return false; }
7051 bool CanThrow() const override { return NeedsEnvironment(); }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00007052
Alexandre Rames78e3ef62015-08-12 13:43:29 +01007053 static SideEffects SideEffectsForArchRuntimeCalls() {
7054 return SideEffects::CanTriggerGC();
7055 }
7056
Vladimir Marko372f10e2016-05-17 16:30:10 +01007057 void AddSpecialInput(HInstruction* special_input);
7058
7059 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01007060 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Vladimir Marko372f10e2016-05-17 16:30:10 +01007061 return ArrayRef<HUserRecord<HInstruction*>>(
7062 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007063 }
7064
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00007065 DECLARE_INSTRUCTION(LoadString);
7066
Artem Serovcced8ba2017-07-19 18:18:09 +01007067 protected:
7068 DEFAULT_COPY_CONSTRUCTOR(LoadString);
7069
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00007070 private:
Vladimir Marko4d1be4922017-01-06 14:43:11 +00007071 static constexpr size_t kFieldLoadKind = kNumberOfGenericPackedBits;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007072 static constexpr size_t kFieldLoadKindSize =
7073 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
7074 static constexpr size_t kNumberOfLoadStringPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00007075 static_assert(kNumberOfLoadStringPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007076 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00007077
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007078 void SetLoadKindInternal(LoadKind load_kind);
7079
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007080 // The special input is the HCurrentMethod for kRuntimeCall.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007081 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko48886c22017-01-06 11:45:47 +00007082 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Marko372f10e2016-05-17 16:30:10 +01007083 HUserRecord<HInstruction*> special_input_;
7084
Andreas Gampe8a0128a2016-11-28 07:38:35 -08007085 dex::StringIndex string_index_;
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007086 const DexFile& dex_file_;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007087
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007088 Handle<mirror::String> string_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00007089};
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007090std::ostream& operator<<(std::ostream& os, HLoadString::LoadKind rhs);
7091
7092// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
Vladimir Marko28e012a2017-12-07 11:22:59 +00007093inline void HLoadString::SetLoadKind(LoadKind load_kind) {
7094 // The load kind should be determined before inserting the instruction to the graph.
7095 DCHECK(GetBlock() == nullptr);
7096 DCHECK(GetEnvironment() == nullptr);
7097 DCHECK_EQ(GetLoadKind(), LoadKind::kRuntimeCall);
7098 SetPackedField<LoadKindField>(load_kind);
7099 if (load_kind != LoadKind::kRuntimeCall) {
7100 special_input_ = HUserRecord<HInstruction*>(nullptr);
7101 }
7102 if (!NeedsEnvironment()) {
7103 SetSideEffects(SideEffects::None());
7104 }
7105}
7106
7107// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007108inline void HLoadString::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007109 // The special input is used for PC-relative loads on some architectures,
7110 // including literal pool loads, which are PC-relative too.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007111 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007112 GetLoadKind() == LoadKind::kBootImageRelRo ||
Vladimir Marko8e524ad2018-07-13 10:27:43 +01007113 GetLoadKind() == LoadKind::kBssEntry ||
7114 GetLoadKind() == LoadKind::kJitBootImageAddress) << GetLoadKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01007115 // HLoadString::GetInputRecords() returns an empty array at this point,
7116 // so use the GetInputRecords() from the base class to set the input record.
7117 DCHECK(special_input_.GetInstruction() == nullptr);
7118 special_input_ = HUserRecord<HInstruction*>(special_input);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007119 special_input->AddUseAt(this, 0);
7120}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00007121
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007122class HLoadMethodHandle final : public HInstruction {
Orion Hodsondbaa5c72018-05-10 08:22:46 +01007123 public:
7124 HLoadMethodHandle(HCurrentMethod* current_method,
Orion Hodson4c8e12e2018-05-18 08:33:20 +01007125 uint16_t method_handle_idx,
7126 const DexFile& dex_file,
7127 uint32_t dex_pc)
Orion Hodsondbaa5c72018-05-10 08:22:46 +01007128 : HInstruction(kLoadMethodHandle,
7129 DataType::Type::kReference,
7130 SideEffectsForArchRuntimeCalls(),
7131 dex_pc),
7132 special_input_(HUserRecord<HInstruction*>(current_method)),
7133 method_handle_idx_(method_handle_idx),
7134 dex_file_(dex_file) {
7135 }
7136
7137 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01007138 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Orion Hodsondbaa5c72018-05-10 08:22:46 +01007139 return ArrayRef<HUserRecord<HInstruction*>>(
7140 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
7141 }
7142
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007143 bool IsClonable() const override { return true; }
Orion Hodsondbaa5c72018-05-10 08:22:46 +01007144
7145 uint16_t GetMethodHandleIndex() const { return method_handle_idx_; }
7146
7147 const DexFile& GetDexFile() const { return dex_file_; }
7148
7149 static SideEffects SideEffectsForArchRuntimeCalls() {
7150 return SideEffects::CanTriggerGC();
7151 }
7152
7153 DECLARE_INSTRUCTION(LoadMethodHandle);
7154
7155 protected:
7156 DEFAULT_COPY_CONSTRUCTOR(LoadMethodHandle);
7157
7158 private:
7159 // The special input is the HCurrentMethod for kRuntimeCall.
7160 HUserRecord<HInstruction*> special_input_;
7161
7162 const uint16_t method_handle_idx_;
7163 const DexFile& dex_file_;
7164};
7165
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007166class HLoadMethodType final : public HInstruction {
Orion Hodson18259d72018-04-12 11:18:23 +01007167 public:
7168 HLoadMethodType(HCurrentMethod* current_method,
Orion Hodson06d10a72018-05-14 08:53:38 +01007169 dex::ProtoIndex proto_index,
Orion Hodson18259d72018-04-12 11:18:23 +01007170 const DexFile& dex_file,
7171 uint32_t dex_pc)
7172 : HInstruction(kLoadMethodType,
7173 DataType::Type::kReference,
7174 SideEffectsForArchRuntimeCalls(),
7175 dex_pc),
7176 special_input_(HUserRecord<HInstruction*>(current_method)),
Orion Hodson06d10a72018-05-14 08:53:38 +01007177 proto_index_(proto_index),
Orion Hodson18259d72018-04-12 11:18:23 +01007178 dex_file_(dex_file) {
7179 }
7180
7181 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01007182 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Orion Hodson18259d72018-04-12 11:18:23 +01007183 return ArrayRef<HUserRecord<HInstruction*>>(
7184 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
7185 }
7186
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007187 bool IsClonable() const override { return true; }
Orion Hodson18259d72018-04-12 11:18:23 +01007188
Orion Hodson06d10a72018-05-14 08:53:38 +01007189 dex::ProtoIndex GetProtoIndex() const { return proto_index_; }
Orion Hodson18259d72018-04-12 11:18:23 +01007190
7191 const DexFile& GetDexFile() const { return dex_file_; }
7192
7193 static SideEffects SideEffectsForArchRuntimeCalls() {
7194 return SideEffects::CanTriggerGC();
7195 }
7196
7197 DECLARE_INSTRUCTION(LoadMethodType);
7198
7199 protected:
7200 DEFAULT_COPY_CONSTRUCTOR(LoadMethodType);
7201
7202 private:
7203 // The special input is the HCurrentMethod for kRuntimeCall.
7204 HUserRecord<HInstruction*> special_input_;
7205
Orion Hodson06d10a72018-05-14 08:53:38 +01007206 const dex::ProtoIndex proto_index_;
Orion Hodson18259d72018-04-12 11:18:23 +01007207 const DexFile& dex_file_;
7208};
7209
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007210/**
7211 * Performs an initialization check on its Class object input.
7212 */
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007213class HClinitCheck final : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007214 public:
Roland Levillain3887c462015-08-12 18:15:42 +01007215 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07007216 : HExpression(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307217 kClinitCheck,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007218 DataType::Type::kReference,
Mingyao Yang217eb062017-12-11 15:20:07 -08007219 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06007220 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007221 SetRawInputAt(0, constant);
7222 }
Artem Serova6e26142018-06-19 14:55:17 +01007223 // TODO: Make ClinitCheck clonable.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007224 bool CanBeMoved() const override { return true; }
7225 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00007226 return true;
7227 }
7228
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007229 bool NeedsEnvironment() const override {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007230 // May call runtime to initialize the class.
7231 return true;
7232 }
7233
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007234 bool CanThrow() const override { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007235
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00007236 HLoadClass* GetLoadClass() const {
7237 DCHECK(InputAt(0)->IsLoadClass());
7238 return InputAt(0)->AsLoadClass();
7239 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007240
7241 DECLARE_INSTRUCTION(ClinitCheck);
7242
Artem Serovcced8ba2017-07-19 18:18:09 +01007243
7244 protected:
7245 DEFAULT_COPY_CONSTRUCTOR(ClinitCheck);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007246};
7247
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007248class HStaticFieldGet final : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007249 public:
7250 HStaticFieldGet(HInstruction* cls,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00007251 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007252 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00007253 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01007254 bool is_volatile,
7255 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07007256 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07007257 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01007258 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307259 : HExpression(kStaticFieldGet,
7260 field_type,
7261 SideEffects::FieldReadOfType(field_type, is_volatile),
7262 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00007263 field_info_(field,
7264 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07007265 field_type,
7266 is_volatile,
7267 field_idx,
7268 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00007269 dex_file) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007270 SetRawInputAt(0, cls);
7271 }
7272
Calin Juravle52c48962014-12-16 17:02:57 +00007273
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007274 bool IsClonable() const override { return true; }
7275 bool CanBeMoved() const override { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00007276
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007277 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01007278 const HStaticFieldGet* other_get = other->AsStaticFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00007279 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007280 }
7281
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007282 size_t ComputeHashCode() const override {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007283 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
7284 }
7285
Alex Light3a73ffb2021-01-25 14:11:05 +00007286 bool IsFieldAccess() const override { return true; }
7287 const FieldInfo& GetFieldInfo() const override { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007288 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007289 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00007290 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007291
Vladimir Marko61b92282017-10-11 13:23:17 +01007292 void SetType(DataType::Type new_type) {
7293 DCHECK(DataType::IsIntegralType(GetType()));
7294 DCHECK(DataType::IsIntegralType(new_type));
7295 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
7296 SetPackedField<TypeField>(new_type);
7297 }
7298
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007299 DECLARE_INSTRUCTION(StaticFieldGet);
7300
Artem Serovcced8ba2017-07-19 18:18:09 +01007301 protected:
7302 DEFAULT_COPY_CONSTRUCTOR(StaticFieldGet);
7303
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007304 private:
7305 const FieldInfo field_info_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007306};
7307
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007308class HStaticFieldSet final : public HExpression<2> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007309 public:
7310 HStaticFieldSet(HInstruction* cls,
7311 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00007312 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007313 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00007314 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01007315 bool is_volatile,
7316 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07007317 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07007318 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01007319 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01007320 : HExpression(kStaticFieldSet,
7321 SideEffects::FieldWriteOfType(field_type, is_volatile),
7322 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00007323 field_info_(field,
7324 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07007325 field_type,
7326 is_volatile,
7327 field_idx,
7328 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00007329 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007330 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007331 SetRawInputAt(0, cls);
7332 SetRawInputAt(1, value);
7333 }
7334
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007335 bool IsClonable() const override { return true; }
Alex Light3a73ffb2021-01-25 14:11:05 +00007336 bool IsFieldAccess() const override { return true; }
7337 const FieldInfo& GetFieldInfo() const override { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007338 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007339 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00007340 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007341
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00007342 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00007343 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
7344 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00007345
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007346 DECLARE_INSTRUCTION(StaticFieldSet);
7347
Artem Serovcced8ba2017-07-19 18:18:09 +01007348 protected:
7349 DEFAULT_COPY_CONSTRUCTOR(StaticFieldSet);
7350
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007351 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007352 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
7353 static constexpr size_t kNumberOfStaticFieldSetPackedBits = kFlagValueCanBeNull + 1;
7354 static_assert(kNumberOfStaticFieldSetPackedBits <= kMaxNumberOfPackedBits,
7355 "Too many packed fields.");
7356
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007357 const FieldInfo field_info_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007358};
7359
Vladimir Marko552a1342017-10-31 10:56:47 +00007360class HStringBuilderAppend final : public HVariableInputSizeInstruction {
7361 public:
7362 HStringBuilderAppend(HIntConstant* format,
7363 uint32_t number_of_arguments,
7364 ArenaAllocator* allocator,
7365 uint32_t dex_pc)
7366 : HVariableInputSizeInstruction(
7367 kStringBuilderAppend,
7368 DataType::Type::kReference,
7369 // The runtime call may read memory from inputs. It never writes outside
7370 // of the newly allocated result object (or newly allocated helper objects).
7371 SideEffects::AllReads().Union(SideEffects::CanTriggerGC()),
7372 dex_pc,
7373 allocator,
7374 number_of_arguments + /* format */ 1u,
7375 kArenaAllocInvokeInputs) {
7376 DCHECK_GE(number_of_arguments, 1u); // There must be something to append.
7377 SetRawInputAt(FormatIndex(), format);
7378 }
7379
7380 void SetArgumentAt(size_t index, HInstruction* argument) {
7381 DCHECK_LE(index, GetNumberOfArguments());
7382 SetRawInputAt(index, argument);
7383 }
7384
7385 // Return the number of arguments, excluding the format.
7386 size_t GetNumberOfArguments() const {
7387 DCHECK_GE(InputCount(), 1u);
7388 return InputCount() - 1u;
7389 }
7390
7391 size_t FormatIndex() const {
7392 return GetNumberOfArguments();
7393 }
7394
7395 HIntConstant* GetFormat() {
7396 return InputAt(FormatIndex())->AsIntConstant();
7397 }
7398
7399 bool NeedsEnvironment() const override { return true; }
7400
7401 bool CanThrow() const override { return true; }
7402
Vladimir Markob1fe5e12020-03-10 14:30:49 +00007403 bool CanBeNull() const override { return false; }
7404
Vladimir Marko552a1342017-10-31 10:56:47 +00007405 DECLARE_INSTRUCTION(StringBuilderAppend);
7406
7407 protected:
7408 DEFAULT_COPY_CONSTRUCTOR(StringBuilderAppend);
7409};
7410
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007411class HUnresolvedInstanceFieldGet final : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01007412 public:
7413 HUnresolvedInstanceFieldGet(HInstruction* obj,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007414 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01007415 uint32_t field_index,
7416 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307417 : HExpression(kUnresolvedInstanceFieldGet,
7418 field_type,
7419 SideEffects::AllExceptGCDependency(),
7420 dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01007421 field_index_(field_index) {
7422 SetRawInputAt(0, obj);
7423 }
7424
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007425 bool IsClonable() const override { return true; }
7426 bool NeedsEnvironment() const override { return true; }
7427 bool CanThrow() const override { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01007428
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007429 DataType::Type GetFieldType() const { return GetType(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01007430 uint32_t GetFieldIndex() const { return field_index_; }
7431
7432 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
7433
Artem Serovcced8ba2017-07-19 18:18:09 +01007434 protected:
7435 DEFAULT_COPY_CONSTRUCTOR(UnresolvedInstanceFieldGet);
7436
Calin Juravlee460d1d2015-09-29 04:52:17 +01007437 private:
7438 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01007439};
7440
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007441class HUnresolvedInstanceFieldSet final : public HExpression<2> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01007442 public:
7443 HUnresolvedInstanceFieldSet(HInstruction* obj,
7444 HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007445 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01007446 uint32_t field_index,
7447 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01007448 : HExpression(kUnresolvedInstanceFieldSet, SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01007449 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007450 SetPackedField<FieldTypeField>(field_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007451 DCHECK_EQ(DataType::Kind(field_type), DataType::Kind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01007452 SetRawInputAt(0, obj);
7453 SetRawInputAt(1, value);
7454 }
7455
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007456 bool IsClonable() const override { return true; }
7457 bool NeedsEnvironment() const override { return true; }
7458 bool CanThrow() const override { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01007459
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007460 DataType::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01007461 uint32_t GetFieldIndex() const { return field_index_; }
7462
7463 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
7464
Artem Serovcced8ba2017-07-19 18:18:09 +01007465 protected:
7466 DEFAULT_COPY_CONSTRUCTOR(UnresolvedInstanceFieldSet);
7467
Calin Juravlee460d1d2015-09-29 04:52:17 +01007468 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007469 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
7470 static constexpr size_t kFieldFieldTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007471 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00007472 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
7473 kFieldFieldType + kFieldFieldTypeSize;
7474 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
7475 "Too many packed fields.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007476 using FieldTypeField = BitField<DataType::Type, kFieldFieldType, kFieldFieldTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00007477
Calin Juravlee460d1d2015-09-29 04:52:17 +01007478 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01007479};
7480
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007481class HUnresolvedStaticFieldGet final : public HExpression<0> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01007482 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007483 HUnresolvedStaticFieldGet(DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01007484 uint32_t field_index,
7485 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307486 : HExpression(kUnresolvedStaticFieldGet,
7487 field_type,
7488 SideEffects::AllExceptGCDependency(),
7489 dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01007490 field_index_(field_index) {
7491 }
7492
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007493 bool IsClonable() const override { return true; }
7494 bool NeedsEnvironment() const override { return true; }
7495 bool CanThrow() const override { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01007496
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007497 DataType::Type GetFieldType() const { return GetType(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01007498 uint32_t GetFieldIndex() const { return field_index_; }
7499
7500 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
7501
Artem Serovcced8ba2017-07-19 18:18:09 +01007502 protected:
7503 DEFAULT_COPY_CONSTRUCTOR(UnresolvedStaticFieldGet);
7504
Calin Juravlee460d1d2015-09-29 04:52:17 +01007505 private:
7506 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01007507};
7508
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007509class HUnresolvedStaticFieldSet final : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01007510 public:
7511 HUnresolvedStaticFieldSet(HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007512 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01007513 uint32_t field_index,
7514 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01007515 : HExpression(kUnresolvedStaticFieldSet, SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01007516 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007517 SetPackedField<FieldTypeField>(field_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007518 DCHECK_EQ(DataType::Kind(field_type), DataType::Kind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01007519 SetRawInputAt(0, value);
7520 }
7521
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007522 bool IsClonable() const override { return true; }
7523 bool NeedsEnvironment() const override { return true; }
7524 bool CanThrow() const override { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01007525
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007526 DataType::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01007527 uint32_t GetFieldIndex() const { return field_index_; }
7528
7529 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
7530
Artem Serovcced8ba2017-07-19 18:18:09 +01007531 protected:
7532 DEFAULT_COPY_CONSTRUCTOR(UnresolvedStaticFieldSet);
7533
Calin Juravlee460d1d2015-09-29 04:52:17 +01007534 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007535 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
7536 static constexpr size_t kFieldFieldTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007537 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00007538 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
7539 kFieldFieldType + kFieldFieldTypeSize;
7540 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
7541 "Too many packed fields.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007542 using FieldTypeField = BitField<DataType::Type, kFieldFieldType, kFieldFieldTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00007543
Calin Juravlee460d1d2015-09-29 04:52:17 +01007544 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01007545};
7546
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007547// Implement the move-exception DEX instruction.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007548class HLoadException final : public HExpression<0> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007549 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06007550 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307551 : HExpression(kLoadException, DataType::Type::kReference, SideEffects::None(), dex_pc) {
7552 }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007553
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007554 bool CanBeNull() const override { return false; }
David Brazdilbbd733e2015-08-18 17:48:17 +01007555
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007556 DECLARE_INSTRUCTION(LoadException);
7557
Artem Serovcced8ba2017-07-19 18:18:09 +01007558 protected:
7559 DEFAULT_COPY_CONSTRUCTOR(LoadException);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007560};
7561
David Brazdilcb1c0552015-08-04 16:22:25 +01007562// Implicit part of move-exception which clears thread-local exception storage.
7563// Must not be removed because the runtime expects the TLS to get cleared.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007564class HClearException final : public HExpression<0> {
David Brazdilcb1c0552015-08-04 16:22:25 +01007565 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06007566 explicit HClearException(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01007567 : HExpression(kClearException, SideEffects::AllWrites(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307568 }
David Brazdilcb1c0552015-08-04 16:22:25 +01007569
7570 DECLARE_INSTRUCTION(ClearException);
7571
Artem Serovcced8ba2017-07-19 18:18:09 +01007572 protected:
7573 DEFAULT_COPY_CONSTRUCTOR(ClearException);
David Brazdilcb1c0552015-08-04 16:22:25 +01007574};
7575
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007576class HThrow final : public HExpression<1> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007577 public:
7578 HThrow(HInstruction* exception, uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01007579 : HExpression(kThrow, SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007580 SetRawInputAt(0, exception);
7581 }
7582
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007583 bool IsControlFlow() const override { return true; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007584
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007585 bool NeedsEnvironment() const override { return true; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007586
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007587 bool CanThrow() const override { return true; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007588
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007589 bool AlwaysThrows() const override { return true; }
Aart Bika8b8e9b2018-01-09 11:01:02 -08007590
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007591 DECLARE_INSTRUCTION(Throw);
7592
Artem Serovcced8ba2017-07-19 18:18:09 +01007593 protected:
7594 DEFAULT_COPY_CONSTRUCTOR(Throw);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007595};
7596
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007597/**
7598 * Implementation strategies for the code generator of a HInstanceOf
7599 * or `HCheckCast`.
7600 */
Vladimir Marko9974e3c2020-06-10 16:27:06 +01007601enum class TypeCheckKind { // private marker to avoid generate-operator-out.py from processing.
Calin Juravle98893e12015-10-02 21:05:03 +01007602 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007603 kExactCheck, // Can do a single class compare.
7604 kClassHierarchyCheck, // Can just walk the super class chain.
7605 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
7606 kInterfaceCheck, // No optimization yet when checking against an interface.
7607 kArrayObjectCheck, // Can just check if the array is not primitive.
Vladimir Markoa1de9182016-02-25 11:37:38 +00007608 kArrayCheck, // No optimization yet when checking against a generic array.
Vladimir Marko175e7862018-03-27 09:03:13 +00007609 kBitstringCheck, // Compare the type check bitstring.
Vladimir Markoa1de9182016-02-25 11:37:38 +00007610 kLast = kArrayCheck
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007611};
7612
Roland Levillain86503782016-02-11 19:07:30 +00007613std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs);
7614
Vladimir Marko175e7862018-03-27 09:03:13 +00007615// Note: HTypeCheckInstruction is just a helper class, not an abstract instruction with an
7616// `IsTypeCheckInstruction()`. (New virtual methods in the HInstruction class have a high cost.)
7617class HTypeCheckInstruction : public HVariableInputSizeInstruction {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007618 public:
Vladimir Marko175e7862018-03-27 09:03:13 +00007619 HTypeCheckInstruction(InstructionKind kind,
Vladimir Markobd785672018-05-03 17:09:09 +01007620 DataType::Type type,
Vladimir Marko175e7862018-03-27 09:03:13 +00007621 HInstruction* object,
7622 HInstruction* target_class_or_null,
7623 TypeCheckKind check_kind,
7624 Handle<mirror::Class> klass,
7625 uint32_t dex_pc,
7626 ArenaAllocator* allocator,
7627 HIntConstant* bitstring_path_to_root,
7628 HIntConstant* bitstring_mask,
7629 SideEffects side_effects)
7630 : HVariableInputSizeInstruction(
7631 kind,
Vladimir Markobd785672018-05-03 17:09:09 +01007632 type,
Vladimir Marko175e7862018-03-27 09:03:13 +00007633 side_effects,
7634 dex_pc,
7635 allocator,
Andreas Gampe3db70682018-12-26 15:12:03 -08007636 /* number_of_inputs= */ check_kind == TypeCheckKind::kBitstringCheck ? 4u : 2u,
Vladimir Marko175e7862018-03-27 09:03:13 +00007637 kArenaAllocTypeCheckInputs),
7638 klass_(klass) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007639 SetPackedField<TypeCheckKindField>(check_kind);
7640 SetPackedFlag<kFlagMustDoNullCheck>(true);
Vladimir Marko175e7862018-03-27 09:03:13 +00007641 SetPackedFlag<kFlagValidTargetClassRTI>(false);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007642 SetRawInputAt(0, object);
Vladimir Marko175e7862018-03-27 09:03:13 +00007643 SetRawInputAt(1, target_class_or_null);
7644 DCHECK_EQ(check_kind == TypeCheckKind::kBitstringCheck, bitstring_path_to_root != nullptr);
7645 DCHECK_EQ(check_kind == TypeCheckKind::kBitstringCheck, bitstring_mask != nullptr);
7646 if (check_kind == TypeCheckKind::kBitstringCheck) {
7647 DCHECK(target_class_or_null->IsNullConstant());
7648 SetRawInputAt(2, bitstring_path_to_root);
7649 SetRawInputAt(3, bitstring_mask);
7650 } else {
7651 DCHECK(target_class_or_null->IsLoadClass());
7652 }
Vladimir Marko87584542017-12-12 17:47:52 +00007653 }
7654
7655 HLoadClass* GetTargetClass() const {
Vladimir Marko175e7862018-03-27 09:03:13 +00007656 DCHECK_NE(GetTypeCheckKind(), TypeCheckKind::kBitstringCheck);
Vladimir Marko87584542017-12-12 17:47:52 +00007657 HInstruction* load_class = InputAt(1);
7658 DCHECK(load_class->IsLoadClass());
7659 return load_class->AsLoadClass();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007660 }
7661
Vladimir Marko175e7862018-03-27 09:03:13 +00007662 uint32_t GetBitstringPathToRoot() const {
7663 DCHECK_EQ(GetTypeCheckKind(), TypeCheckKind::kBitstringCheck);
7664 HInstruction* path_to_root = InputAt(2);
7665 DCHECK(path_to_root->IsIntConstant());
7666 return static_cast<uint32_t>(path_to_root->AsIntConstant()->GetValue());
7667 }
7668
7669 uint32_t GetBitstringMask() const {
7670 DCHECK_EQ(GetTypeCheckKind(), TypeCheckKind::kBitstringCheck);
7671 HInstruction* mask = InputAt(3);
7672 DCHECK(mask->IsIntConstant());
7673 return static_cast<uint32_t>(mask->AsIntConstant()->GetValue());
7674 }
7675
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007676 bool IsClonable() const override { return true; }
7677 bool CanBeMoved() const override { return true; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007678
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007679 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Marko175e7862018-03-27 09:03:13 +00007680 DCHECK(other->IsInstanceOf() || other->IsCheckCast()) << other->DebugName();
7681 return GetPackedFields() == down_cast<const HTypeCheckInstruction*>(other)->GetPackedFields();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007682 }
7683
Andreas Gampe3fbd3ad2018-03-26 21:14:46 +00007684 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
7685 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
7686 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
7687 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
7688
Vladimir Marko175e7862018-03-27 09:03:13 +00007689 ReferenceTypeInfo GetTargetClassRTI() {
7690 if (GetPackedFlag<kFlagValidTargetClassRTI>()) {
7691 // Note: The is_exact flag from the return value should not be used.
Andreas Gampe3db70682018-12-26 15:12:03 -08007692 return ReferenceTypeInfo::CreateUnchecked(klass_, /* is_exact= */ true);
Vladimir Marko175e7862018-03-27 09:03:13 +00007693 } else {
7694 return ReferenceTypeInfo::CreateInvalid();
7695 }
7696 }
7697
7698 // Target class RTI is marked as valid by RTP if the klass_ is admissible.
7699 void SetValidTargetClassRTI() REQUIRES_SHARED(Locks::mutator_lock_) {
7700 DCHECK(klass_ != nullptr);
7701 SetPackedFlag<kFlagValidTargetClassRTI>(true);
7702 }
7703
7704 Handle<mirror::Class> GetClass() const {
7705 return klass_;
7706 }
7707
7708 protected:
7709 DEFAULT_COPY_CONSTRUCTOR(TypeCheckInstruction);
7710
7711 private:
7712 static constexpr size_t kFieldTypeCheckKind = kNumberOfGenericPackedBits;
7713 static constexpr size_t kFieldTypeCheckKindSize =
7714 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
7715 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
7716 static constexpr size_t kFlagValidTargetClassRTI = kFlagMustDoNullCheck + 1;
7717 static constexpr size_t kNumberOfInstanceOfPackedBits = kFlagValidTargetClassRTI + 1;
7718 static_assert(kNumberOfInstanceOfPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
7719 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
7720
7721 Handle<mirror::Class> klass_;
7722};
7723
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007724class HInstanceOf final : public HTypeCheckInstruction {
Vladimir Marko175e7862018-03-27 09:03:13 +00007725 public:
7726 HInstanceOf(HInstruction* object,
7727 HInstruction* target_class_or_null,
7728 TypeCheckKind check_kind,
7729 Handle<mirror::Class> klass,
7730 uint32_t dex_pc,
7731 ArenaAllocator* allocator,
7732 HIntConstant* bitstring_path_to_root,
7733 HIntConstant* bitstring_mask)
7734 : HTypeCheckInstruction(kInstanceOf,
Vladimir Markobd785672018-05-03 17:09:09 +01007735 DataType::Type::kBool,
Vladimir Marko175e7862018-03-27 09:03:13 +00007736 object,
7737 target_class_or_null,
7738 check_kind,
7739 klass,
7740 dex_pc,
7741 allocator,
7742 bitstring_path_to_root,
7743 bitstring_mask,
7744 SideEffectsForArchRuntimeCalls(check_kind)) {}
7745
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007746 bool IsClonable() const override { return true; }
Artem Serova6e26142018-06-19 14:55:17 +01007747
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007748 bool NeedsEnvironment() const override {
Vladimir Marko175e7862018-03-27 09:03:13 +00007749 return CanCallRuntime(GetTypeCheckKind());
7750 }
7751
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00007752 static bool CanCallRuntime(TypeCheckKind check_kind) {
Vladimir Marko54f4fbd2020-02-12 10:52:22 +00007753 // TODO: Re-evaluate now that mips codegen has been removed.
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00007754 return check_kind != TypeCheckKind::kExactCheck;
7755 }
7756
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007757 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00007758 return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01007759 }
7760
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007761 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007762
Artem Serovcced8ba2017-07-19 18:18:09 +01007763 protected:
7764 DEFAULT_COPY_CONSTRUCTOR(InstanceOf);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007765};
7766
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007767class HBoundType final : public HExpression<1> {
Calin Juravleb1498f62015-02-16 13:13:29 +00007768 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -07007769 explicit HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307770 : HExpression(kBoundType, DataType::Type::kReference, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00007771 upper_bound_(ReferenceTypeInfo::CreateInvalid()) {
7772 SetPackedFlag<kFlagUpperCanBeNull>(true);
7773 SetPackedFlag<kFlagCanBeNull>(true);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007774 DCHECK_EQ(input->GetType(), DataType::Type::kReference);
Calin Juravleb1498f62015-02-16 13:13:29 +00007775 SetRawInputAt(0, input);
7776 }
7777
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007778 bool InstructionDataEquals(const HInstruction* other) const override;
7779 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01007780
David Brazdilf5552582015-12-27 13:36:12 +00007781 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007782 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00007783 bool GetUpperCanBeNull() const { return GetPackedFlag<kFlagUpperCanBeNull>(); }
David Brazdilf5552582015-12-27 13:36:12 +00007784 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00007785
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007786 void SetCanBeNull(bool can_be_null) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007787 DCHECK(GetUpperCanBeNull() || !can_be_null);
7788 SetPackedFlag<kFlagCanBeNull>(can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00007789 }
7790
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007791 bool CanBeNull() const override { return GetPackedFlag<kFlagCanBeNull>(); }
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007792
Calin Juravleb1498f62015-02-16 13:13:29 +00007793 DECLARE_INSTRUCTION(BoundType);
7794
Artem Serovcced8ba2017-07-19 18:18:09 +01007795 protected:
7796 DEFAULT_COPY_CONSTRUCTOR(BoundType);
7797
Calin Juravleb1498f62015-02-16 13:13:29 +00007798 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007799 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
7800 // is false then CanBeNull() cannot be true).
Vladimir Markobd785672018-05-03 17:09:09 +01007801 static constexpr size_t kFlagUpperCanBeNull = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00007802 static constexpr size_t kFlagCanBeNull = kFlagUpperCanBeNull + 1;
7803 static constexpr size_t kNumberOfBoundTypePackedBits = kFlagCanBeNull + 1;
7804 static_assert(kNumberOfBoundTypePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
7805
Calin Juravleb1498f62015-02-16 13:13:29 +00007806 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007807 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
7808 // It is used to bound the type in cases like:
7809 // if (x instanceof ClassX) {
7810 // // uper_bound_ will be ClassX
7811 // }
David Brazdilf5552582015-12-27 13:36:12 +00007812 ReferenceTypeInfo upper_bound_;
Calin Juravleb1498f62015-02-16 13:13:29 +00007813};
7814
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007815class HCheckCast final : public HTypeCheckInstruction {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007816 public:
7817 HCheckCast(HInstruction* object,
Vladimir Marko175e7862018-03-27 09:03:13 +00007818 HInstruction* target_class_or_null,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007819 TypeCheckKind check_kind,
Vladimir Marko175e7862018-03-27 09:03:13 +00007820 Handle<mirror::Class> klass,
7821 uint32_t dex_pc,
7822 ArenaAllocator* allocator,
7823 HIntConstant* bitstring_path_to_root,
7824 HIntConstant* bitstring_mask)
7825 : HTypeCheckInstruction(kCheckCast,
Vladimir Markobd785672018-05-03 17:09:09 +01007826 DataType::Type::kVoid,
Vladimir Marko175e7862018-03-27 09:03:13 +00007827 object,
7828 target_class_or_null,
7829 check_kind,
7830 klass,
7831 dex_pc,
7832 allocator,
7833 bitstring_path_to_root,
7834 bitstring_mask,
7835 SideEffects::CanTriggerGC()) {}
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007836
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007837 bool IsClonable() const override { return true; }
7838 bool NeedsEnvironment() const override {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007839 // Instruction may throw a CheckCastError.
7840 return true;
7841 }
7842
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007843 bool CanThrow() const override { return true; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007844
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007845 DECLARE_INSTRUCTION(CheckCast);
7846
Artem Serovcced8ba2017-07-19 18:18:09 +01007847 protected:
7848 DEFAULT_COPY_CONSTRUCTOR(CheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007849};
7850
Andreas Gampe26de38b2016-07-27 17:53:11 -07007851/**
7852 * @brief Memory barrier types (see "The JSR-133 Cookbook for Compiler Writers").
7853 * @details We define the combined barrier types that are actually required
7854 * by the Java Memory Model, rather than using exactly the terminology from
7855 * the JSR-133 cookbook. These should, in many cases, be replaced by acquire/release
7856 * primitives. Note that the JSR-133 cookbook generally does not deal with
7857 * store atomicity issues, and the recipes there are not always entirely sufficient.
7858 * The current recipe is as follows:
7859 * -# Use AnyStore ~= (LoadStore | StoreStore) ~= release barrier before volatile store.
7860 * -# Use AnyAny barrier after volatile store. (StoreLoad is as expensive.)
7861 * -# Use LoadAny barrier ~= (LoadLoad | LoadStore) ~= acquire barrier after each volatile load.
7862 * -# Use StoreStore barrier after all stores but before return from any constructor whose
7863 * class has final fields.
7864 * -# Use NTStoreStore to order non-temporal stores with respect to all later
7865 * store-to-memory instructions. Only generated together with non-temporal stores.
7866 */
7867enum MemBarrierKind {
7868 kAnyStore,
7869 kLoadAny,
7870 kStoreStore,
7871 kAnyAny,
7872 kNTStoreStore,
7873 kLastBarrierKind = kNTStoreStore
7874};
Vladimir Marko9974e3c2020-06-10 16:27:06 +01007875std::ostream& operator<<(std::ostream& os, MemBarrierKind kind);
Andreas Gampe26de38b2016-07-27 17:53:11 -07007876
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007877class HMemoryBarrier final : public HExpression<0> {
Calin Juravle27df7582015-04-17 19:12:31 +01007878 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06007879 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01007880 : HExpression(kMemoryBarrier,
7881 SideEffects::AllWritesAndReads(), // Assume write/read on all fields/arrays.
7882 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007883 SetPackedField<BarrierKindField>(barrier_kind);
7884 }
Calin Juravle27df7582015-04-17 19:12:31 +01007885
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007886 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01007887
Vladimir Markoa1de9182016-02-25 11:37:38 +00007888 MemBarrierKind GetBarrierKind() { return GetPackedField<BarrierKindField>(); }
Calin Juravle27df7582015-04-17 19:12:31 +01007889
7890 DECLARE_INSTRUCTION(MemoryBarrier);
7891
Artem Serovcced8ba2017-07-19 18:18:09 +01007892 protected:
7893 DEFAULT_COPY_CONSTRUCTOR(MemoryBarrier);
7894
Calin Juravle27df7582015-04-17 19:12:31 +01007895 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007896 static constexpr size_t kFieldBarrierKind = HInstruction::kNumberOfGenericPackedBits;
7897 static constexpr size_t kFieldBarrierKindSize =
7898 MinimumBitsToStore(static_cast<size_t>(kLastBarrierKind));
7899 static constexpr size_t kNumberOfMemoryBarrierPackedBits =
7900 kFieldBarrierKind + kFieldBarrierKindSize;
7901 static_assert(kNumberOfMemoryBarrierPackedBits <= kMaxNumberOfPackedBits,
7902 "Too many packed fields.");
7903 using BarrierKindField = BitField<MemBarrierKind, kFieldBarrierKind, kFieldBarrierKindSize>;
Calin Juravle27df7582015-04-17 19:12:31 +01007904};
7905
Igor Murashkind01745e2017-04-05 16:40:31 -07007906// A constructor fence orders all prior stores to fields that could be accessed via a final field of
7907// the specified object(s), with respect to any subsequent store that might "publish"
7908// (i.e. make visible) the specified object to another thread.
7909//
7910// JLS 17.5.1 "Semantics of final fields" states that a freeze action happens
7911// for all final fields (that were set) at the end of the invoked constructor.
7912//
7913// The constructor fence models the freeze actions for the final fields of an object
7914// being constructed (semantically at the end of the constructor). Constructor fences
7915// have a per-object affinity; two separate objects being constructed get two separate
7916// constructor fences.
7917//
7918// (Note: that if calling a super-constructor or forwarding to another constructor,
7919// the freezes would happen at the end of *that* constructor being invoked).
7920//
7921// The memory model guarantees that when the object being constructed is "published" after
7922// constructor completion (i.e. escapes the current thread via a store), then any final field
7923// writes must be observable on other threads (once they observe that publication).
7924//
7925// Further, anything written before the freeze, and read by dereferencing through the final field,
7926// must also be visible (so final object field could itself have an object with non-final fields;
7927// yet the freeze must also extend to them).
7928//
7929// Constructor example:
7930//
7931// class HasFinal {
7932// final int field; Optimizing IR for <init>()V:
7933// HasFinal() {
7934// field = 123; HInstanceFieldSet(this, HasFinal.field, 123)
7935// // freeze(this.field); HConstructorFence(this)
7936// } HReturn
7937// }
7938//
7939// HConstructorFence can serve double duty as a fence for new-instance/new-array allocations of
7940// already-initialized classes; in that case the allocation must act as a "default-initializer"
7941// of the object which effectively writes the class pointer "final field".
7942//
7943// For example, we can model default-initialiation as roughly the equivalent of the following:
7944//
7945// class Object {
7946// private final Class header;
7947// }
7948//
7949// Java code: Optimizing IR:
7950//
7951// T new_instance<T>() {
7952// Object obj = allocate_memory(T.class.size); obj = HInvoke(art_quick_alloc_object, T)
7953// obj.header = T.class; // header write is done by above call.
7954// // freeze(obj.header) HConstructorFence(obj)
7955// return (T)obj;
7956// }
7957//
7958// See also:
Vladimir Markoc1c34522018-10-31 13:56:49 +00007959// * DexCompilationUnit::RequiresConstructorBarrier
Igor Murashkind01745e2017-04-05 16:40:31 -07007960// * QuasiAtomic::ThreadFenceForConstructor
7961//
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007962class HConstructorFence final : public HVariableInputSizeInstruction {
Igor Murashkind01745e2017-04-05 16:40:31 -07007963 // A fence has variable inputs because the inputs can be removed
7964 // after prepare_for_register_allocation phase.
7965 // (TODO: In the future a fence could freeze multiple objects
7966 // after merging two fences together.)
7967 public:
7968 // `fence_object` is the reference that needs to be protected for correct publication.
7969 //
7970 // It makes sense in the following situations:
7971 // * <init> constructors, it's the "this" parameter (i.e. HParameterValue, s.t. IsThis() == true).
7972 // * new-instance-like instructions, it's the return value (i.e. HNewInstance).
7973 //
7974 // After construction the `fence_object` becomes the 0th input.
7975 // This is not an input in a real sense, but just a convenient place to stash the information
7976 // about the associated object.
7977 HConstructorFence(HInstruction* fence_object,
7978 uint32_t dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007979 ArenaAllocator* allocator)
Igor Murashkind01745e2017-04-05 16:40:31 -07007980 // We strongly suspect there is not a more accurate way to describe the fine-grained reordering
7981 // constraints described in the class header. We claim that these SideEffects constraints
7982 // enforce a superset of the real constraints.
7983 //
7984 // The ordering described above is conservatively modeled with SideEffects as follows:
7985 //
7986 // * To prevent reordering of the publication stores:
7987 // ----> "Reads of objects" is the initial SideEffect.
7988 // * For every primitive final field store in the constructor:
7989 // ----> Union that field's type as a read (e.g. "Read of T") into the SideEffect.
7990 // * If there are any stores to reference final fields in the constructor:
7991 // ----> Use a more conservative "AllReads" SideEffect because any stores to any references
7992 // that are reachable from `fence_object` also need to be prevented for reordering
7993 // (and we do not want to do alias analysis to figure out what those stores are).
7994 //
7995 // In the implementation, this initially starts out as an "all reads" side effect; this is an
7996 // even more conservative approach than the one described above, and prevents all of the
7997 // above reordering without analyzing any of the instructions in the constructor.
7998 //
7999 // If in a later phase we discover that there are no writes to reference final fields,
8000 // we can refine the side effect to a smaller set of type reads (see above constraints).
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05308001 : HVariableInputSizeInstruction(kConstructorFence,
8002 SideEffects::AllReads(),
Igor Murashkind01745e2017-04-05 16:40:31 -07008003 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01008004 allocator,
Andreas Gampe3db70682018-12-26 15:12:03 -08008005 /* number_of_inputs= */ 1,
Igor Murashkind01745e2017-04-05 16:40:31 -07008006 kArenaAllocConstructorFenceInputs) {
8007 DCHECK(fence_object != nullptr);
8008 SetRawInputAt(0, fence_object);
8009 }
8010
8011 // The object associated with this constructor fence.
8012 //
8013 // (Note: This will be null after the prepare_for_register_allocation phase,
8014 // as all constructor fence inputs are removed there).
8015 HInstruction* GetFenceObject() const {
8016 return InputAt(0);
8017 }
8018
8019 // Find all the HConstructorFence uses (`fence_use`) for `this` and:
8020 // - Delete `fence_use` from `this`'s use list.
8021 // - Delete `this` from `fence_use`'s inputs list.
8022 // - If the `fence_use` is dead, remove it from the graph.
8023 //
8024 // A fence is considered dead once it no longer has any uses
8025 // and all of the inputs are dead.
8026 //
8027 // This must *not* be called during/after prepare_for_register_allocation,
8028 // because that removes all the inputs to the fences but the fence is actually
8029 // still considered live.
Igor Murashkin6ef45672017-08-08 13:59:55 -07008030 //
8031 // Returns how many HConstructorFence instructions were removed from graph.
8032 static size_t RemoveConstructorFences(HInstruction* instruction);
Igor Murashkind01745e2017-04-05 16:40:31 -07008033
Igor Murashkindd018df2017-08-09 10:38:31 -07008034 // Combine all inputs of `this` and `other` instruction and remove
8035 // `other` from the graph.
8036 //
8037 // Inputs are unique after the merge.
8038 //
8039 // Requirement: `this` must not be the same as `other.
8040 void Merge(HConstructorFence* other);
8041
Igor Murashkin79d8fa72017-04-18 09:37:23 -07008042 // Check if this constructor fence is protecting
8043 // an HNewInstance or HNewArray that is also the immediate
8044 // predecessor of `this`.
8045 //
Igor Murashkindd018df2017-08-09 10:38:31 -07008046 // If `ignore_inputs` is true, then the immediate predecessor doesn't need
8047 // to be one of the inputs of `this`.
8048 //
Igor Murashkin79d8fa72017-04-18 09:37:23 -07008049 // Returns the associated HNewArray or HNewInstance,
8050 // or null otherwise.
Igor Murashkindd018df2017-08-09 10:38:31 -07008051 HInstruction* GetAssociatedAllocation(bool ignore_inputs = false);
Igor Murashkin79d8fa72017-04-18 09:37:23 -07008052
Igor Murashkind01745e2017-04-05 16:40:31 -07008053 DECLARE_INSTRUCTION(ConstructorFence);
8054
Artem Serovcced8ba2017-07-19 18:18:09 +01008055 protected:
8056 DEFAULT_COPY_CONSTRUCTOR(ConstructorFence);
Igor Murashkind01745e2017-04-05 16:40:31 -07008057};
8058
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01008059class HMonitorOperation final : public HExpression<1> {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00008060 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00008061 enum class OperationKind {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00008062 kEnter,
8063 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00008064 kLast = kExit
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00008065 };
8066
8067 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01008068 : HExpression(kMonitorOperation,
8069 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
8070 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00008071 SetPackedField<OperationKindField>(kind);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00008072 SetRawInputAt(0, object);
8073 }
8074
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00008075 // Instruction may go into runtime, so we need an environment.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01008076 bool NeedsEnvironment() const override { return true; }
David Brazdilbff75032015-07-08 17:26:51 +00008077
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01008078 bool CanThrow() const override {
David Brazdilbff75032015-07-08 17:26:51 +00008079 // Verifier guarantees that monitor-exit cannot throw.
8080 // This is important because it allows the HGraphBuilder to remove
8081 // a dead throw-catch loop generated for `synchronized` blocks/methods.
8082 return IsEnter();
8083 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00008084
Vladimir Markoa1de9182016-02-25 11:37:38 +00008085 OperationKind GetOperationKind() const { return GetPackedField<OperationKindField>(); }
8086 bool IsEnter() const { return GetOperationKind() == OperationKind::kEnter; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00008087
8088 DECLARE_INSTRUCTION(MonitorOperation);
8089
Artem Serovcced8ba2017-07-19 18:18:09 +01008090 protected:
8091 DEFAULT_COPY_CONSTRUCTOR(MonitorOperation);
8092
Calin Juravle52c48962014-12-16 17:02:57 +00008093 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00008094 static constexpr size_t kFieldOperationKind = HInstruction::kNumberOfGenericPackedBits;
8095 static constexpr size_t kFieldOperationKindSize =
8096 MinimumBitsToStore(static_cast<size_t>(OperationKind::kLast));
8097 static constexpr size_t kNumberOfMonitorOperationPackedBits =
8098 kFieldOperationKind + kFieldOperationKindSize;
8099 static_assert(kNumberOfMonitorOperationPackedBits <= HInstruction::kMaxNumberOfPackedBits,
8100 "Too many packed fields.");
8101 using OperationKindField = BitField<OperationKind, kFieldOperationKind, kFieldOperationKindSize>;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00008102};
8103
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01008104class HSelect final : public HExpression<3> {
David Brazdil74eb1b22015-12-14 11:44:01 +00008105 public:
8106 HSelect(HInstruction* condition,
8107 HInstruction* true_value,
8108 HInstruction* false_value,
8109 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05308110 : HExpression(kSelect, HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
David Brazdil74eb1b22015-12-14 11:44:01 +00008111 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
8112
8113 // First input must be `true_value` or `false_value` to allow codegens to
8114 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
8115 // that architectures which implement HSelect as a conditional move also
8116 // will not need to invert the condition.
8117 SetRawInputAt(0, false_value);
8118 SetRawInputAt(1, true_value);
8119 SetRawInputAt(2, condition);
8120 }
8121
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01008122 bool IsClonable() const override { return true; }
David Brazdil74eb1b22015-12-14 11:44:01 +00008123 HInstruction* GetFalseValue() const { return InputAt(0); }
8124 HInstruction* GetTrueValue() const { return InputAt(1); }
8125 HInstruction* GetCondition() const { return InputAt(2); }
8126
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01008127 bool CanBeMoved() const override { return true; }
8128 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01008129 return true;
8130 }
David Brazdil74eb1b22015-12-14 11:44:01 +00008131
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01008132 bool CanBeNull() const override {
David Brazdil74eb1b22015-12-14 11:44:01 +00008133 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
8134 }
8135
8136 DECLARE_INSTRUCTION(Select);
8137
Artem Serovcced8ba2017-07-19 18:18:09 +01008138 protected:
8139 DEFAULT_COPY_CONSTRUCTOR(Select);
David Brazdil74eb1b22015-12-14 11:44:01 +00008140};
8141
Vladimir Markof9f64412015-09-02 14:05:49 +01008142class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01008143 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01008144 MoveOperands(Location source,
8145 Location destination,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008146 DataType::Type type,
Nicolas Geoffray90218252015-04-15 11:56:51 +01008147 HInstruction* instruction)
8148 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01008149
8150 Location GetSource() const { return source_; }
8151 Location GetDestination() const { return destination_; }
8152
8153 void SetSource(Location value) { source_ = value; }
8154 void SetDestination(Location value) { destination_ = value; }
8155
8156 // The parallel move resolver marks moves as "in-progress" by clearing the
8157 // destination (but not the source).
8158 Location MarkPending() {
8159 DCHECK(!IsPending());
8160 Location dest = destination_;
8161 destination_ = Location::NoLocation();
8162 return dest;
8163 }
8164
8165 void ClearPending(Location dest) {
8166 DCHECK(IsPending());
8167 destination_ = dest;
8168 }
8169
8170 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00008171 DCHECK(source_.IsValid() || destination_.IsInvalid());
8172 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01008173 }
8174
8175 // True if this blocks a move from the given location.
8176 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08008177 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01008178 }
8179
8180 // A move is redundant if it's been eliminated, if its source and
8181 // destination are the same, or if its destination is unneeded.
8182 bool IsRedundant() const {
8183 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
8184 }
8185
8186 // We clear both operands to indicate move that's been eliminated.
8187 void Eliminate() {
8188 source_ = destination_ = Location::NoLocation();
8189 }
8190
8191 bool IsEliminated() const {
8192 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
8193 return source_.IsInvalid();
8194 }
8195
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008196 DataType::Type GetType() const { return type_; }
Alexey Frunze4dda3372015-06-01 18:31:49 -07008197
Nicolas Geoffray90218252015-04-15 11:56:51 +01008198 bool Is64BitMove() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008199 return DataType::Is64BitType(type_);
Nicolas Geoffray90218252015-04-15 11:56:51 +01008200 }
8201
Nicolas Geoffray740475d2014-09-29 10:33:25 +01008202 HInstruction* GetInstruction() const { return instruction_; }
8203
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01008204 private:
8205 Location source_;
8206 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01008207 // The type this move is for.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008208 DataType::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01008209 // The instruction this move is assocatied with. Null when this move is
8210 // for moving an input in the expected locations of user (including a phi user).
8211 // This is only used in debug mode, to ensure we do not connect interval siblings
8212 // in the same parallel move.
8213 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01008214};
8215
Roland Levillainc9285912015-12-18 10:38:42 +00008216std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
8217
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01008218static constexpr size_t kDefaultNumberOfMoves = 4;
8219
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01008220class HParallelMove final : public HExpression<0> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01008221 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01008222 explicit HParallelMove(ArenaAllocator* allocator, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01008223 : HExpression(kParallelMove, SideEffects::None(), dex_pc),
Vladimir Markoe764d2e2017-10-05 14:35:55 +01008224 moves_(allocator->Adapter(kArenaAllocMoveOperands)) {
Vladimir Marko225b6462015-09-28 12:17:40 +01008225 moves_.reserve(kDefaultNumberOfMoves);
8226 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01008227
Nicolas Geoffray90218252015-04-15 11:56:51 +01008228 void AddMove(Location source,
8229 Location destination,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008230 DataType::Type type,
Nicolas Geoffray90218252015-04-15 11:56:51 +01008231 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00008232 DCHECK(source.IsValid());
8233 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00008234 if (kIsDebugBuild) {
8235 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01008236 for (const MoveOperands& move : moves_) {
8237 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00008238 // Special case the situation where the move is for the spill slot
8239 // of the instruction.
8240 if ((GetPrevious() == instruction)
8241 || ((GetPrevious() == nullptr)
8242 && instruction->IsPhi()
8243 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01008244 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00008245 << "Doing parallel moves for the same instruction.";
8246 } else {
8247 DCHECK(false) << "Doing parallel moves for the same instruction.";
8248 }
8249 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00008250 }
8251 }
Vladimir Marko225b6462015-09-28 12:17:40 +01008252 for (const MoveOperands& move : moves_) {
8253 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01008254 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01008255 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Alex Lighta7c66e72021-01-27 11:41:58 -08008256 << source << " ==> " << destination << " for " << SafePrint(instruction);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00008257 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01008258 }
Vladimir Marko225b6462015-09-28 12:17:40 +01008259 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01008260 }
8261
Vladimir Marko225b6462015-09-28 12:17:40 +01008262 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01008263 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01008264 }
8265
Vladimir Marko225b6462015-09-28 12:17:40 +01008266 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01008267
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01008268 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01008269
Artem Serovcced8ba2017-07-19 18:18:09 +01008270 protected:
8271 DEFAULT_COPY_CONSTRUCTOR(ParallelMove);
8272
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01008273 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01008274 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01008275};
8276
xueliang.zhonge0eb4832017-10-30 13:43:14 +00008277// This instruction computes an intermediate address pointing in the 'middle' of an object. The
8278// result pointer cannot be handled by GC, so extra care is taken to make sure that this value is
8279// never used across anything that can trigger GC.
8280// The result of this instruction is not a pointer in the sense of `DataType::Type::kreference`.
8281// So we represent it by the type `DataType::Type::kInt`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01008282class HIntermediateAddress final : public HExpression<2> {
xueliang.zhonge0eb4832017-10-30 13:43:14 +00008283 public:
8284 HIntermediateAddress(HInstruction* base_address, HInstruction* offset, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05308285 : HExpression(kIntermediateAddress,
8286 DataType::Type::kInt32,
8287 SideEffects::DependsOnGC(),
8288 dex_pc) {
xueliang.zhonge0eb4832017-10-30 13:43:14 +00008289 DCHECK_EQ(DataType::Size(DataType::Type::kInt32),
8290 DataType::Size(DataType::Type::kReference))
8291 << "kPrimInt and kPrimNot have different sizes.";
8292 SetRawInputAt(0, base_address);
8293 SetRawInputAt(1, offset);
8294 }
8295
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01008296 bool IsClonable() const override { return true; }
8297 bool CanBeMoved() const override { return true; }
8298 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
xueliang.zhonge0eb4832017-10-30 13:43:14 +00008299 return true;
8300 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01008301 bool IsActualObject() const override { return false; }
xueliang.zhonge0eb4832017-10-30 13:43:14 +00008302
8303 HInstruction* GetBaseAddress() const { return InputAt(0); }
8304 HInstruction* GetOffset() const { return InputAt(1); }
8305
8306 DECLARE_INSTRUCTION(IntermediateAddress);
8307
Artem Serovcced8ba2017-07-19 18:18:09 +01008308 protected:
8309 DEFAULT_COPY_CONSTRUCTOR(IntermediateAddress);
xueliang.zhonge0eb4832017-10-30 13:43:14 +00008310};
8311
8312
Mark Mendell0616ae02015-04-17 12:49:27 -04008313} // namespace art
8314
Aart Bikf8f5a162017-02-06 15:35:29 -08008315#include "nodes_vector.h"
8316
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03008317#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
8318#include "nodes_shared.h"
8319#endif
Shalini Salomi Bodapatidd121f62018-10-26 15:03:53 +05308320#if defined(ART_ENABLE_CODEGEN_x86) || defined(ART_ENABLE_CODEGEN_x86_64)
Mark Mendell0616ae02015-04-17 12:49:27 -04008321#include "nodes_x86.h"
8322#endif
8323
Vladimir Marko0a516052019-10-14 13:00:44 +00008324namespace art {
Mark Mendell0616ae02015-04-17 12:49:27 -04008325
Igor Murashkin6ef45672017-08-08 13:59:55 -07008326class OptimizingCompilerStats;
8327
Nicolas Geoffray818f2102014-02-18 16:43:35 +00008328class HGraphVisitor : public ValueObject {
8329 public:
Igor Murashkin6ef45672017-08-08 13:59:55 -07008330 explicit HGraphVisitor(HGraph* graph, OptimizingCompilerStats* stats = nullptr)
8331 : stats_(stats),
8332 graph_(graph) {}
Dave Allison20dfc792014-06-16 20:44:29 -07008333 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00008334
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01008335 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00008336 virtual void VisitBasicBlock(HBasicBlock* block);
8337
Roland Levillain633021e2014-10-01 14:12:25 +01008338 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00008339 void VisitInsertionOrder();
8340
Roland Levillain633021e2014-10-01 14:12:25 +01008341 // Visit the graph following dominator tree reverse post-order.
8342 void VisitReversePostOrder();
8343
Nicolas Geoffray787c3072014-03-17 10:20:19 +00008344 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00008345
Nicolas Geoffray818f2102014-02-18 16:43:35 +00008346 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01008347#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00008348 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
8349
8350 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
8351
8352#undef DECLARE_VISIT_INSTRUCTION
8353
Igor Murashkin6ef45672017-08-08 13:59:55 -07008354 protected:
8355 OptimizingCompilerStats* stats_;
8356
Nicolas Geoffray818f2102014-02-18 16:43:35 +00008357 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07008358 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00008359
8360 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
8361};
8362
Nicolas Geoffray360231a2014-10-08 21:07:48 +01008363class HGraphDelegateVisitor : public HGraphVisitor {
8364 public:
Igor Murashkin6ef45672017-08-08 13:59:55 -07008365 explicit HGraphDelegateVisitor(HGraph* graph, OptimizingCompilerStats* stats = nullptr)
8366 : HGraphVisitor(graph, stats) {}
Nicolas Geoffray360231a2014-10-08 21:07:48 +01008367 virtual ~HGraphDelegateVisitor() {}
8368
8369 // Visit functions that delegate to to super class.
8370#define DECLARE_VISIT_INSTRUCTION(name, super) \
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01008371 void Visit##name(H##name* instr) override { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01008372
8373 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
8374
8375#undef DECLARE_VISIT_INSTRUCTION
8376
8377 private:
8378 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
8379};
8380
Artem Serovcced8ba2017-07-19 18:18:09 +01008381// Create a clone of the instruction, insert it into the graph; replace the old one with a new
8382// and remove the old instruction.
8383HInstruction* ReplaceInstrOrPhiByClone(HInstruction* instr);
8384
8385// Create a clone for each clonable instructions/phis and replace the original with the clone.
8386//
8387// Used for testing individual instruction cloner.
8388class CloneAndReplaceInstructionVisitor : public HGraphDelegateVisitor {
8389 public:
8390 explicit CloneAndReplaceInstructionVisitor(HGraph* graph)
Artem Serov7f4aff62017-06-21 17:02:18 +01008391 : HGraphDelegateVisitor(graph), instr_replaced_by_clones_count_(0) {}
Artem Serovcced8ba2017-07-19 18:18:09 +01008392
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01008393 void VisitInstruction(HInstruction* instruction) override {
Artem Serovcced8ba2017-07-19 18:18:09 +01008394 if (instruction->IsClonable()) {
8395 ReplaceInstrOrPhiByClone(instruction);
Artem Serov7f4aff62017-06-21 17:02:18 +01008396 instr_replaced_by_clones_count_++;
Artem Serovcced8ba2017-07-19 18:18:09 +01008397 }
8398 }
8399
Artem Serov7f4aff62017-06-21 17:02:18 +01008400 size_t GetInstrReplacedByClonesCount() const { return instr_replaced_by_clones_count_; }
Artem Serovcced8ba2017-07-19 18:18:09 +01008401
8402 private:
Artem Serov7f4aff62017-06-21 17:02:18 +01008403 size_t instr_replaced_by_clones_count_;
Artem Serovcced8ba2017-07-19 18:18:09 +01008404
8405 DISALLOW_COPY_AND_ASSIGN(CloneAndReplaceInstructionVisitor);
8406};
8407
Nicolas Geoffray82091da2015-01-26 10:02:45 +00008408// Iterator over the blocks that art part of the loop. Includes blocks part
8409// of an inner loop. The order in which the blocks are iterated is on their
8410// block id.
8411class HBlocksInLoopIterator : public ValueObject {
8412 public:
8413 explicit HBlocksInLoopIterator(const HLoopInformation& info)
8414 : blocks_in_loop_(info.GetBlocks()),
8415 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
8416 index_(0) {
8417 if (!blocks_in_loop_.IsBitSet(index_)) {
8418 Advance();
8419 }
8420 }
8421
Vladimir Markofa6b93c2015-09-15 10:15:55 +01008422 bool Done() const { return index_ == blocks_.size(); }
8423 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00008424 void Advance() {
8425 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01008426 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00008427 if (blocks_in_loop_.IsBitSet(index_)) {
8428 break;
8429 }
8430 }
8431 }
8432
8433 private:
8434 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01008435 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00008436 size_t index_;
8437
8438 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
8439};
8440
Mingyao Yang3584bce2015-05-19 16:01:59 -07008441// Iterator over the blocks that art part of the loop. Includes blocks part
8442// of an inner loop. The order in which the blocks are iterated is reverse
8443// post order.
8444class HBlocksInLoopReversePostOrderIterator : public ValueObject {
8445 public:
8446 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
8447 : blocks_in_loop_(info.GetBlocks()),
8448 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
8449 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01008450 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07008451 Advance();
8452 }
8453 }
8454
Vladimir Markofa6b93c2015-09-15 10:15:55 +01008455 bool Done() const { return index_ == blocks_.size(); }
8456 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07008457 void Advance() {
8458 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01008459 for (size_t e = blocks_.size(); index_ < e; ++index_) {
8460 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07008461 break;
8462 }
8463 }
8464 }
8465
8466 private:
8467 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01008468 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07008469 size_t index_;
8470
8471 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
8472};
8473
Aart Bikf3e61ee2017-04-12 17:09:20 -07008474// Returns int64_t value of a properly typed constant.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00008475inline int64_t Int64FromConstant(HConstant* constant) {
David Brazdilc0b601b2016-02-08 14:20:45 +00008476 if (constant->IsIntConstant()) {
8477 return constant->AsIntConstant()->GetValue();
8478 } else if (constant->IsLongConstant()) {
8479 return constant->AsLongConstant()->GetValue();
8480 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00008481 DCHECK(constant->IsNullConstant()) << constant->DebugName();
David Brazdilc0b601b2016-02-08 14:20:45 +00008482 return 0;
8483 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00008484}
8485
Aart Bikf3e61ee2017-04-12 17:09:20 -07008486// Returns true iff instruction is an integral constant (and sets value on success).
8487inline bool IsInt64AndGet(HInstruction* instruction, /*out*/ int64_t* value) {
8488 if (instruction->IsIntConstant()) {
8489 *value = instruction->AsIntConstant()->GetValue();
8490 return true;
8491 } else if (instruction->IsLongConstant()) {
8492 *value = instruction->AsLongConstant()->GetValue();
8493 return true;
8494 } else if (instruction->IsNullConstant()) {
8495 *value = 0;
8496 return true;
8497 }
8498 return false;
8499}
8500
Aart Bik0148de42017-09-05 09:25:01 -07008501// Returns true iff instruction is the given integral constant.
8502inline bool IsInt64Value(HInstruction* instruction, int64_t value) {
8503 int64_t val = 0;
8504 return IsInt64AndGet(instruction, &val) && val == value;
8505}
8506
8507// Returns true iff instruction is a zero bit pattern.
8508inline bool IsZeroBitPattern(HInstruction* instruction) {
8509 return instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern();
8510}
8511
Vladimir Marko6d5b7e32018-05-09 16:52:48 +01008512// Implement HInstruction::Is##type() for concrete instructions.
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008513#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Markoa90dd512018-05-04 15:04:45 +01008514 inline bool HInstruction::Is##type() const { return GetKind() == k##type; }
8515 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
8516#undef INSTRUCTION_TYPE_CHECK
8517
Vladimir Marko6d5b7e32018-05-09 16:52:48 +01008518// Implement HInstruction::Is##type() for abstract instructions.
Vladimir Markoa90dd512018-05-04 15:04:45 +01008519#define INSTRUCTION_TYPE_CHECK_RESULT(type, super) \
8520 std::is_base_of<BaseType, H##type>::value,
8521#define INSTRUCTION_TYPE_CHECK(type, super) \
8522 inline bool HInstruction::Is##type() const { \
8523 DCHECK_LT(GetKind(), kLastInstructionKind); \
8524 using BaseType = H##type; \
8525 static constexpr bool results[] = { \
8526 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK_RESULT) \
8527 }; \
8528 return results[static_cast<size_t>(GetKind())]; \
8529 }
8530
8531 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
8532#undef INSTRUCTION_TYPE_CHECK
Vladimir Marko6d5b7e32018-05-09 16:52:48 +01008533#undef INSTRUCTION_TYPE_CHECK_RESULT
Vladimir Markoa90dd512018-05-04 15:04:45 +01008534
8535#define INSTRUCTION_TYPE_CAST(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008536 inline const H##type* HInstruction::As##type() const { \
8537 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
8538 } \
8539 inline H##type* HInstruction::As##type() { \
8540 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
8541 }
8542
Vladimir Markoa90dd512018-05-04 15:04:45 +01008543 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CAST)
8544#undef INSTRUCTION_TYPE_CAST
8545
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008546
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00008547// Create space in `blocks` for adding `number_of_new_blocks` entries
8548// starting at location `at`. Blocks after `at` are moved accordingly.
8549inline void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks,
8550 size_t number_of_new_blocks,
8551 size_t after) {
8552 DCHECK_LT(after, blocks->size());
8553 size_t old_size = blocks->size();
8554 size_t new_size = old_size + number_of_new_blocks;
8555 blocks->resize(new_size);
8556 std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end());
8557}
8558
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00008559/*
8560 * Hunt "under the hood" of array lengths (leading to array references),
8561 * null checks (also leading to array references), and new arrays
8562 * (leading to the actual length). This makes it more likely related
8563 * instructions become actually comparable.
8564 */
8565inline HInstruction* HuntForDeclaration(HInstruction* instruction) {
8566 while (instruction->IsArrayLength() ||
8567 instruction->IsNullCheck() ||
8568 instruction->IsNewArray()) {
8569 instruction = instruction->IsNewArray()
8570 ? instruction->AsNewArray()->GetLength()
8571 : instruction->InputAt(0);
8572 }
8573 return instruction;
8574}
8575
Artem Serovb47b9782019-12-04 21:02:09 +00008576inline bool IsAddOrSub(const HInstruction* instruction) {
8577 return instruction->IsAdd() || instruction->IsSub();
8578}
8579
Artem Serov21c7e6f2017-07-27 16:04:42 +01008580void RemoveEnvironmentUses(HInstruction* instruction);
8581bool HasEnvironmentUsedByOthers(HInstruction* instruction);
8582void ResetEnvironmentInputRecords(HInstruction* instruction);
8583
Evgeny Astigeevichaf92a0f2020-06-26 13:28:33 +01008584// Detects an instruction that is >= 0. As long as the value is carried by
8585// a single instruction, arithmetic wrap-around cannot occur.
8586bool IsGEZero(HInstruction* instruction);
8587
Nicolas Geoffray818f2102014-02-18 16:43:35 +00008588} // namespace art
8589
8590#endif // ART_COMPILER_OPTIMIZING_NODES_H_