blob: a7ea371aceaffbc7b5e031bffa76f3a0988b9798 [file] [log] [blame]
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ART_COMPILER_OPTIMIZING_NODES_H_
18#define ART_COMPILER_OPTIMIZING_NODES_H_
19
Vladimir Marko60584552015-09-03 13:35:12 +000020#include <algorithm>
Vladimir Markof9f64412015-09-02 14:05:49 +010021#include <array>
Roland Levillain9867bc72015-08-05 10:21:34 +010022#include <type_traits>
23
Mathieu Chartiere5d80f82015-10-15 17:47:48 -070024#include "base/arena_bit_vector.h"
David Brazdil8d5b8b22015-03-24 10:51:52 +000025#include "base/arena_containers.h"
Mathieu Chartierb666f482015-02-18 14:33:14 -080026#include "base/arena_object.h"
David Brazdild9c90372016-09-14 16:53:55 +010027#include "base/array_ref.h"
Vladimir Marko3fae1292019-06-07 11:26:25 +010028#include "base/intrusive_forward_list.h"
Vladimir Marko2c45bc92016-10-25 16:54:12 +010029#include "base/iteration_range.h"
Andreas Gampe7fbc4a52018-11-28 08:26:47 -080030#include "base/mutex.h"
David Sehrc431b9d2018-03-02 12:01:51 -080031#include "base/quasi_atomic.h"
Vladimir Marko60584552015-09-03 13:35:12 +000032#include "base/stl_util.h"
David Brazdild9c90372016-09-14 16:53:55 +010033#include "base/transform_array_ref.h"
Nicolas Geoffray76d4bb0f32018-09-21 12:58:45 +010034#include "art_method.h"
Vladimir Marko02ca05a2020-05-12 13:58:51 +010035#include "class_root.h"
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010036#include "data_type.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070037#include "deoptimization_kind.h"
David Sehr9e734c72018-01-04 17:56:19 -080038#include "dex/dex_file.h"
39#include "dex/dex_file_types.h"
David Sehr8c0961f2018-01-23 16:11:38 -080040#include "dex/invoke_type.h"
David Sehr312f3b22018-03-19 08:39:26 -070041#include "dex/method_reference.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000042#include "entrypoints/quick/quick_entrypoints_enum.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000043#include "handle.h"
44#include "handle_scope.h"
Nicolas Geoffray762869d2016-07-15 15:28:35 +010045#include "intrinsics_enum.h"
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010046#include "locations.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000047#include "mirror/class.h"
Orion Hodson18259d72018-04-12 11:18:23 +010048#include "mirror/method_type.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010049#include "offsets.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000050
Vladimir Marko0a516052019-10-14 13:00:44 +000051namespace art {
Nicolas Geoffray818f2102014-02-18 16:43:35 +000052
Vladimir Markoca6fff82017-10-03 14:49:14 +010053class ArenaStack;
David Brazdil1abb4192015-02-17 18:33:36 +000054class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000055class HBasicBlock;
Igor Murashkind01745e2017-04-05 16:40:31 -070056class HConstructorFence;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010057class HCurrentMethod;
David Brazdil8d5b8b22015-03-24 10:51:52 +000058class HDoubleConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010059class HEnvironment;
David Brazdil8d5b8b22015-03-24 10:51:52 +000060class HFloatConstant;
David Brazdilfc6a86a2015-06-26 10:33:45 +000061class HGraphBuilder;
David Brazdil8d5b8b22015-03-24 10:51:52 +000062class HGraphVisitor;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000063class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000064class HIntConstant;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000065class HInvoke;
David Brazdil8d5b8b22015-03-24 10:51:52 +000066class HLongConstant;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +000067class HNullConstant;
Igor Murashkind01745e2017-04-05 16:40:31 -070068class HParameterValue;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010069class HPhi;
Nicolas Geoffray3c049742014-09-24 18:10:46 +010070class HSuspendCheck;
David Brazdilffee3d32015-07-06 11:48:53 +010071class HTryBoundary;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010072class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000073class LocationSummary;
Nicolas Geoffraydb216f42015-05-05 17:02:20 +010074class SlowPathCode;
David Brazdil8d5b8b22015-03-24 10:51:52 +000075class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000076
Mathieu Chartier736b5602015-09-02 14:54:11 -070077namespace mirror {
78class DexCache;
79} // namespace mirror
80
Nicolas Geoffray818f2102014-02-18 16:43:35 +000081static const int kDefaultNumberOfBlocks = 8;
82static const int kDefaultNumberOfSuccessors = 2;
83static const int kDefaultNumberOfPredecessors = 2;
David Brazdilb618ade2015-07-29 10:31:29 +010084static const int kDefaultNumberOfExceptionalPredecessors = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010085static const int kDefaultNumberOfDominatedBlocks = 1;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000086static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000087
Roland Levillain5b5b9312016-03-22 14:57:31 +000088// The maximum (meaningful) distance (31) that can be used in an integer shift/rotate operation.
89static constexpr int32_t kMaxIntShiftDistance = 0x1f;
90// The maximum (meaningful) distance (63) that can be used in a long shift/rotate operation.
91static constexpr int32_t kMaxLongShiftDistance = 0x3f;
Calin Juravle9aec02f2014-11-18 23:06:35 +000092
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010093static constexpr uint32_t kUnknownFieldIndex = static_cast<uint32_t>(-1);
Mingyao Yang8df69d42015-10-22 15:40:58 -070094static constexpr uint16_t kUnknownClassDefIndex = static_cast<uint16_t>(-1);
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010095
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +010096static constexpr InvokeType kInvalidInvokeType = static_cast<InvokeType>(-1);
97
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +060098static constexpr uint32_t kNoDexPc = -1;
99
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100100inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
101 // For the purposes of the compiler, the dex files must actually be the same object
102 // if we want to safely treat them as the same. This is especially important for JIT
103 // as custom class loaders can open the same underlying file (or memory) multiple
104 // times and provide different class resolution but no two class loaders should ever
105 // use the same DexFile object - doing so is an unsupported hack that can lead to
106 // all sorts of weird failures.
107 return &lhs == &rhs;
108}
109
Dave Allison20dfc792014-06-16 20:44:29 -0700110enum IfCondition {
Aart Bike9f37602015-10-09 11:15:55 -0700111 // All types.
112 kCondEQ, // ==
113 kCondNE, // !=
114 // Signed integers and floating-point numbers.
115 kCondLT, // <
116 kCondLE, // <=
117 kCondGT, // >
118 kCondGE, // >=
119 // Unsigned integers.
120 kCondB, // <
121 kCondBE, // <=
122 kCondA, // >
123 kCondAE, // >=
Scott Wakeling2c76e062016-08-31 09:48:54 +0100124 // First and last aliases.
125 kCondFirst = kCondEQ,
126 kCondLast = kCondAE,
Dave Allison20dfc792014-06-16 20:44:29 -0700127};
128
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000129enum GraphAnalysisResult {
David Brazdil86ea7ee2016-02-16 09:26:07 +0000130 kAnalysisSkipped,
David Brazdilbadd8262016-02-02 16:28:56 +0000131 kAnalysisInvalidBytecode,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000132 kAnalysisFailThrowCatchLoop,
133 kAnalysisFailAmbiguousArrayOp,
Nicolas Geoffray0846a8f2018-09-12 15:21:07 +0100134 kAnalysisFailIrreducibleLoopAndStringInit,
Nicolas Geoffray7cfc8f52019-08-07 10:41:09 +0100135 kAnalysisFailPhiEquivalentInOsr,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000136 kAnalysisSuccess,
David Brazdil4833f5a2015-12-16 10:37:39 +0000137};
138
Andreas Gampe9186ced2016-12-12 14:28:21 -0800139template <typename T>
140static inline typename std::make_unsigned<T>::type MakeUnsigned(T x) {
141 return static_cast<typename std::make_unsigned<T>::type>(x);
142}
143
Vladimir Markof9f64412015-09-02 14:05:49 +0100144class HInstructionList : public ValueObject {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100145 public:
146 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
147
148 void AddInstruction(HInstruction* instruction);
149 void RemoveInstruction(HInstruction* instruction);
150
David Brazdilc3d743f2015-04-22 13:40:50 +0100151 // Insert `instruction` before/after an existing instruction `cursor`.
152 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
153 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
154
Roland Levillain6b469232014-09-25 10:10:38 +0100155 // Return true if this list contains `instruction`.
156 bool Contains(HInstruction* instruction) const;
157
Roland Levillainccc07a92014-09-16 14:48:16 +0100158 // Return true if `instruction1` is found before `instruction2` in
159 // this instruction list and false otherwise. Abort if none
160 // of these instructions is found.
161 bool FoundBefore(const HInstruction* instruction1,
162 const HInstruction* instruction2) const;
163
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000164 bool IsEmpty() const { return first_instruction_ == nullptr; }
165 void Clear() { first_instruction_ = last_instruction_ = nullptr; }
166
167 // Update the block of all instructions to be `block`.
168 void SetBlockOfInstructions(HBasicBlock* block) const;
169
170 void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list);
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000171 void AddBefore(HInstruction* cursor, const HInstructionList& instruction_list);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000172 void Add(const HInstructionList& instruction_list);
173
David Brazdil2d7352b2015-04-20 14:52:42 +0100174 // Return the number of instructions in the list. This is an expensive operation.
175 size_t CountSize() const;
176
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100177 private:
178 HInstruction* first_instruction_;
179 HInstruction* last_instruction_;
180
181 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000182 friend class HGraph;
183 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100184 friend class HInstructionIterator;
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +0000185 friend class HInstructionIteratorHandleChanges;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100186 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100187
188 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
189};
190
David Brazdil4833f5a2015-12-16 10:37:39 +0000191class ReferenceTypeInfo : ValueObject {
192 public:
193 typedef Handle<mirror::Class> TypeHandle;
194
Vladimir Markoa1de9182016-02-25 11:37:38 +0000195 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact);
196
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700197 static ReferenceTypeInfo Create(TypeHandle type_handle) REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil94ab38f2016-06-21 17:48:19 +0100198 return Create(type_handle, type_handle->CannotBeAssignedFromOtherTypes());
199 }
200
Vladimir Markoa1de9182016-02-25 11:37:38 +0000201 static ReferenceTypeInfo CreateUnchecked(TypeHandle type_handle, bool is_exact) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000202 return ReferenceTypeInfo(type_handle, is_exact);
203 }
204
205 static ReferenceTypeInfo CreateInvalid() { return ReferenceTypeInfo(); }
206
Vladimir Markof39745e2016-01-26 12:16:55 +0000207 static bool IsValidHandle(TypeHandle handle) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000208 return handle.GetReference() != nullptr;
209 }
210
Vladimir Marko456307a2016-04-19 14:12:13 +0000211 bool IsValid() const {
David Brazdil4833f5a2015-12-16 10:37:39 +0000212 return IsValidHandle(type_handle_);
213 }
214
215 bool IsExact() const { return is_exact_; }
216
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700217 bool IsObjectClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000218 DCHECK(IsValid());
219 return GetTypeHandle()->IsObjectClass();
220 }
221
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700222 bool IsStringClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000223 DCHECK(IsValid());
224 return GetTypeHandle()->IsStringClass();
225 }
226
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700227 bool IsObjectArray() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000228 DCHECK(IsValid());
229 return IsArrayClass() && GetTypeHandle()->GetComponentType()->IsObjectClass();
230 }
231
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700232 bool IsInterface() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000233 DCHECK(IsValid());
234 return GetTypeHandle()->IsInterface();
235 }
236
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700237 bool IsArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000238 DCHECK(IsValid());
239 return GetTypeHandle()->IsArrayClass();
240 }
241
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700242 bool IsPrimitiveArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000243 DCHECK(IsValid());
244 return GetTypeHandle()->IsPrimitiveArray();
245 }
246
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700247 bool IsNonPrimitiveArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000248 DCHECK(IsValid());
249 return GetTypeHandle()->IsArrayClass() && !GetTypeHandle()->IsPrimitiveArray();
250 }
251
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700252 bool CanArrayHold(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000253 DCHECK(IsValid());
254 if (!IsExact()) return false;
255 if (!IsArrayClass()) return false;
256 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(rti.GetTypeHandle().Get());
257 }
258
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700259 bool CanArrayHoldValuesOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000260 DCHECK(IsValid());
261 if (!IsExact()) return false;
262 if (!IsArrayClass()) return false;
263 if (!rti.IsArrayClass()) return false;
264 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(
265 rti.GetTypeHandle()->GetComponentType());
266 }
267
268 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
269
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700270 bool IsSupertypeOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000271 DCHECK(IsValid());
272 DCHECK(rti.IsValid());
273 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
274 }
275
David Brazdil4833f5a2015-12-16 10:37:39 +0000276 // Returns true if the type information provide the same amount of details.
277 // Note that it does not mean that the instructions have the same actual type
278 // (because the type can be the result of a merge).
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700279 bool IsEqual(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000280 if (!IsValid() && !rti.IsValid()) {
281 // Invalid types are equal.
282 return true;
283 }
284 if (!IsValid() || !rti.IsValid()) {
285 // One is valid, the other not.
286 return false;
287 }
288 return IsExact() == rti.IsExact()
289 && GetTypeHandle().Get() == rti.GetTypeHandle().Get();
290 }
291
292 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +0000293 ReferenceTypeInfo() : type_handle_(TypeHandle()), is_exact_(false) {}
294 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact)
295 : type_handle_(type_handle), is_exact_(is_exact) { }
David Brazdil4833f5a2015-12-16 10:37:39 +0000296
297 // The class of the object.
298 TypeHandle type_handle_;
299 // Whether or not the type is exact or a superclass of the actual type.
300 // Whether or not we have any information about this type.
301 bool is_exact_;
302};
303
304std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
305
Vladimir Marko02ca05a2020-05-12 13:58:51 +0100306class HandleCache {
307 public:
308 explicit HandleCache(VariableSizedHandleScope* handles) : handles_(handles) { }
309
310 VariableSizedHandleScope* GetHandles() { return handles_; }
311
312 template <typename T>
313 MutableHandle<T> NewHandle(T* object) REQUIRES_SHARED(Locks::mutator_lock_) {
314 return handles_->NewHandle(object);
315 }
316
317 template <typename T>
318 MutableHandle<T> NewHandle(ObjPtr<T> object) REQUIRES_SHARED(Locks::mutator_lock_) {
319 return handles_->NewHandle(object);
320 }
321
322 ReferenceTypeInfo::TypeHandle GetObjectClassHandle() {
323 return GetRootHandle(ClassRoot::kJavaLangObject, &object_class_handle_);
324 }
325
326 ReferenceTypeInfo::TypeHandle GetClassClassHandle() {
327 return GetRootHandle(ClassRoot::kJavaLangClass, &class_class_handle_);
328 }
329
330 ReferenceTypeInfo::TypeHandle GetMethodHandleClassHandle() {
331 return GetRootHandle(ClassRoot::kJavaLangInvokeMethodHandleImpl, &method_handle_class_handle_);
332 }
333
334 ReferenceTypeInfo::TypeHandle GetMethodTypeClassHandle() {
335 return GetRootHandle(ClassRoot::kJavaLangInvokeMethodType, &method_type_class_handle_);
336 }
337
338 ReferenceTypeInfo::TypeHandle GetStringClassHandle() {
339 return GetRootHandle(ClassRoot::kJavaLangString, &string_class_handle_);
340 }
341
342 ReferenceTypeInfo::TypeHandle GetThrowableClassHandle() {
343 return GetRootHandle(ClassRoot::kJavaLangThrowable, &throwable_class_handle_);
344 }
345
346
347 private:
348 inline ReferenceTypeInfo::TypeHandle GetRootHandle(ClassRoot class_root,
349 ReferenceTypeInfo::TypeHandle* cache) {
350 if (UNLIKELY(!ReferenceTypeInfo::IsValidHandle(*cache))) {
351 *cache = CreateRootHandle(handles_, class_root);
352 }
353 return *cache;
354 }
355
356 static ReferenceTypeInfo::TypeHandle CreateRootHandle(VariableSizedHandleScope* handles,
357 ClassRoot class_root);
358
359 VariableSizedHandleScope* handles_;
360
361 ReferenceTypeInfo::TypeHandle object_class_handle_;
362 ReferenceTypeInfo::TypeHandle class_class_handle_;
363 ReferenceTypeInfo::TypeHandle method_handle_class_handle_;
364 ReferenceTypeInfo::TypeHandle method_type_class_handle_;
365 ReferenceTypeInfo::TypeHandle string_class_handle_;
366 ReferenceTypeInfo::TypeHandle throwable_class_handle_;
367};
368
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000369// Control-flow graph of a method. Contains a list of basic blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100370class HGraph : public ArenaObject<kArenaAllocGraph> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000371 public:
Vladimir Markoca6fff82017-10-03 14:49:14 +0100372 HGraph(ArenaAllocator* allocator,
373 ArenaStack* arena_stack,
Vladimir Marko02ca05a2020-05-12 13:58:51 +0100374 VariableSizedHandleScope* handles,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100375 const DexFile& dex_file,
376 uint32_t method_idx,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700377 InstructionSet instruction_set,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100378 InvokeType invoke_type = kInvalidInvokeType,
Hans Boehm206348c2018-12-05 11:11:33 -0800379 bool dead_reference_safe = false,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100380 bool debuggable = false,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000381 bool osr = false,
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +0000382 bool baseline = false,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100383 int start_instruction_id = 0)
Vladimir Markoca6fff82017-10-03 14:49:14 +0100384 : allocator_(allocator),
385 arena_stack_(arena_stack),
Vladimir Marko02ca05a2020-05-12 13:58:51 +0100386 handle_cache_(handles),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100387 blocks_(allocator->Adapter(kArenaAllocBlockList)),
388 reverse_post_order_(allocator->Adapter(kArenaAllocReversePostOrder)),
389 linear_order_(allocator->Adapter(kArenaAllocLinearOrder)),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700390 entry_block_(nullptr),
391 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100392 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100393 number_of_vregs_(0),
394 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000395 temporaries_vreg_slots_(0),
Mark Mendell1152c922015-04-24 17:06:35 -0400396 has_bounds_checks_(false),
David Brazdil77a48ae2015-09-15 12:34:04 +0000397 has_try_catch_(false),
Artem Serov2808be82018-12-20 19:15:11 +0000398 has_monitor_operations_(false),
Aart Bikb13c65b2017-03-21 20:14:07 -0700399 has_simd_(false),
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800400 has_loops_(false),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000401 has_irreducible_loops_(false),
Hans Boehm206348c2018-12-05 11:11:33 -0800402 dead_reference_safe_(dead_reference_safe),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000403 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000404 current_instruction_id_(start_instruction_id),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100405 dex_file_(dex_file),
406 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100407 invoke_type_(invoke_type),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100408 in_ssa_form_(false),
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800409 number_of_cha_guards_(0),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700410 instruction_set_(instruction_set),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000411 cached_null_constant_(nullptr),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100412 cached_int_constants_(std::less<int32_t>(), allocator->Adapter(kArenaAllocConstantsMap)),
413 cached_float_constants_(std::less<int32_t>(), allocator->Adapter(kArenaAllocConstantsMap)),
414 cached_long_constants_(std::less<int64_t>(), allocator->Adapter(kArenaAllocConstantsMap)),
415 cached_double_constants_(std::less<int64_t>(), allocator->Adapter(kArenaAllocConstantsMap)),
David Brazdil4833f5a2015-12-16 10:37:39 +0000416 cached_current_method_(nullptr),
Nicolas Geoffray53fec082017-03-27 12:56:16 +0100417 art_method_(nullptr),
Mingyao Yang063fc772016-08-02 11:02:54 -0700418 osr_(osr),
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +0000419 baseline_(baseline),
Vladimir Marko695348f2020-05-19 14:42:02 +0100420 cha_single_implementation_list_(allocator->Adapter(kArenaAllocCHA)) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100421 blocks_.reserve(kDefaultNumberOfBlocks);
422 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000423
Vladimir Markoca6fff82017-10-03 14:49:14 +0100424 ArenaAllocator* GetAllocator() const { return allocator_; }
425 ArenaStack* GetArenaStack() const { return arena_stack_; }
Vladimir Marko02ca05a2020-05-12 13:58:51 +0100426
427 HandleCache* GetHandleCache() { return &handle_cache_; }
428
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100429 const ArenaVector<HBasicBlock*>& GetBlocks() const { return blocks_; }
430
David Brazdil69ba7b72015-06-23 18:27:30 +0100431 bool IsInSsaForm() const { return in_ssa_form_; }
David Brazdilbadd8262016-02-02 16:28:56 +0000432 void SetInSsaForm() { in_ssa_form_ = true; }
David Brazdil69ba7b72015-06-23 18:27:30 +0100433
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000434 HBasicBlock* GetEntryBlock() const { return entry_block_; }
435 HBasicBlock* GetExitBlock() const { return exit_block_; }
David Brazdilc7af85d2015-05-26 12:05:55 +0100436 bool HasExitBlock() const { return exit_block_ != nullptr; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000437
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000438 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
439 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000440
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000441 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100442
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100443 void ComputeDominanceInformation();
444 void ClearDominanceInformation();
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000445 void ClearLoopInformation();
446 void FindBackEdges(ArenaBitVector* visited);
447 GraphAnalysisResult BuildDominatorTree();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100448 void SimplifyCFG();
David Brazdilffee3d32015-07-06 11:48:53 +0100449 void SimplifyCatchBlocks();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000450
David Brazdil4833f5a2015-12-16 10:37:39 +0000451 // Analyze all natural loops in this graph. Returns a code specifying that it
452 // was successful or the reason for failure. The method will fail if a loop
David Brazdil4833f5a2015-12-16 10:37:39 +0000453 // is a throw-catch loop, i.e. the header is a catch block.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000454 GraphAnalysisResult AnalyzeLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100455
David Brazdilffee3d32015-07-06 11:48:53 +0100456 // Iterate over blocks to compute try block membership. Needs reverse post
457 // order and loop information.
458 void ComputeTryBlockInformation();
459
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000460 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
Nicolas Geoffray55bd7492016-02-16 15:37:12 +0000461 // Returns the instruction to replace the invoke expression or null if the
462 // invoke is for a void method. Note that the caller is responsible for replacing
463 // and removing the invoke instruction.
Calin Juravle2e768302015-07-28 14:41:11 +0000464 HInstruction* InlineInto(HGraph* outer_graph, HInvoke* invoke);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000465
Nicolas Geoffraya1d8ddf2016-02-29 11:46:58 +0000466 // Update the loop and try membership of `block`, which was spawned from `reference`.
467 // In case `reference` is a back edge, `replace_if_back_edge` notifies whether `block`
468 // should be the new back edge.
469 void UpdateLoopAndTryInformationOfNewBlock(HBasicBlock* block,
470 HBasicBlock* reference,
471 bool replace_if_back_edge);
472
Mingyao Yang3584bce2015-05-19 16:01:59 -0700473 // Need to add a couple of blocks to test if the loop body is entered and
474 // put deoptimization instructions, etc.
475 void TransformLoopHeaderForBCE(HBasicBlock* header);
476
Aart Bikf8f5a162017-02-06 15:35:29 -0800477 // Adds a new loop directly after the loop with the given header and exit.
478 // Returns the new preheader.
479 HBasicBlock* TransformLoopForVectorization(HBasicBlock* header,
480 HBasicBlock* body,
481 HBasicBlock* exit);
482
David Brazdil8a7c0fe2015-11-02 20:24:55 +0000483 // Removes `block` from the graph. Assumes `block` has been disconnected from
484 // other blocks and has no instructions or phis.
485 void DeleteDeadEmptyBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000486
David Brazdilfc6a86a2015-06-26 10:33:45 +0000487 // Splits the edge between `block` and `successor` while preserving the
488 // indices in the predecessor/successor lists. If there are multiple edges
489 // between the blocks, the lowest indices are used.
490 // Returns the new block which is empty and has the same dex pc as `successor`.
491 HBasicBlock* SplitEdge(HBasicBlock* block, HBasicBlock* successor);
492
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100493 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
Artem Serovc73ee372017-07-31 15:08:40 +0100494 void OrderLoopHeaderPredecessors(HBasicBlock* header);
Artem Serov09faaea2017-12-07 14:36:01 +0000495
496 // Transform a loop into a format with a single preheader.
497 //
498 // Each phi in the header should be split: original one in the header should only hold
499 // inputs reachable from the back edges and a single input from the preheader. The newly created
500 // phi in the preheader should collate the inputs from the original multiple incoming blocks.
501 //
502 // Loops in the graph typically have a single preheader, so this method is used to "repair" loops
503 // that no longer have this property.
504 void TransformLoopToSinglePreheaderFormat(HBasicBlock* header);
505
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100506 void SimplifyLoop(HBasicBlock* header);
507
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000508 int32_t GetNextInstructionId() {
Nicolas Geoffrayc9c31042017-06-29 14:04:16 +0100509 CHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000510 return current_instruction_id_++;
511 }
512
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000513 int32_t GetCurrentInstructionId() const {
514 return current_instruction_id_;
515 }
516
517 void SetCurrentInstructionId(int32_t id) {
Nicolas Geoffrayc9c31042017-06-29 14:04:16 +0100518 CHECK_GE(id, current_instruction_id_);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000519 current_instruction_id_ = id;
520 }
521
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100522 uint16_t GetMaximumNumberOfOutVRegs() const {
523 return maximum_number_of_out_vregs_;
524 }
525
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000526 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
527 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100528 }
529
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100530 void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) {
531 maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value);
532 }
533
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000534 void UpdateTemporariesVRegSlots(size_t slots) {
535 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100536 }
537
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000538 size_t GetTemporariesVRegSlots() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100539 DCHECK(!in_ssa_form_);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000540 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100541 }
542
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100543 void SetNumberOfVRegs(uint16_t number_of_vregs) {
544 number_of_vregs_ = number_of_vregs;
545 }
546
547 uint16_t GetNumberOfVRegs() const {
548 return number_of_vregs_;
549 }
550
551 void SetNumberOfInVRegs(uint16_t value) {
552 number_of_in_vregs_ = value;
553 }
554
David Brazdildee58d62016-04-07 09:54:26 +0000555 uint16_t GetNumberOfInVRegs() const {
556 return number_of_in_vregs_;
557 }
558
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100559 uint16_t GetNumberOfLocalVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100560 DCHECK(!in_ssa_form_);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100561 return number_of_vregs_ - number_of_in_vregs_;
562 }
563
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100564 const ArenaVector<HBasicBlock*>& GetReversePostOrder() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100565 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100566 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100567
Eric Holk1868de92020-02-12 09:10:21 -0800568 ArrayRef<HBasicBlock* const> GetReversePostOrderSkipEntryBlock() const {
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100569 DCHECK(GetReversePostOrder()[0] == entry_block_);
570 return ArrayRef<HBasicBlock* const>(GetReversePostOrder()).SubArray(1);
571 }
572
573 IterationRange<ArenaVector<HBasicBlock*>::const_reverse_iterator> GetPostOrder() const {
574 return ReverseRange(GetReversePostOrder());
575 }
576
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100577 const ArenaVector<HBasicBlock*>& GetLinearOrder() const {
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100578 return linear_order_;
579 }
580
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100581 IterationRange<ArenaVector<HBasicBlock*>::const_reverse_iterator> GetLinearPostOrder() const {
582 return ReverseRange(GetLinearOrder());
583 }
584
Mark Mendell1152c922015-04-24 17:06:35 -0400585 bool HasBoundsChecks() const {
586 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800587 }
588
Mark Mendell1152c922015-04-24 17:06:35 -0400589 void SetHasBoundsChecks(bool value) {
590 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800591 }
592
Hans Boehm206348c2018-12-05 11:11:33 -0800593 // Is the code known to be robust against eliminating dead references
594 // and the effects of early finalization?
595 bool IsDeadReferenceSafe() const { return dead_reference_safe_; }
596
597 void MarkDeadReferenceUnsafe() { dead_reference_safe_ = false; }
598
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000599 bool IsDebuggable() const { return debuggable_; }
600
David Brazdil8d5b8b22015-03-24 10:51:52 +0000601 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000602 // already, it is created and inserted into the graph. This method is only for
603 // integral types.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100604 HConstant* GetConstant(DataType::Type type, int64_t value, uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000605
606 // TODO: This is problematic for the consistency of reference type propagation
607 // because it can be created anytime after the pass and thus it will be left
608 // with an invalid type.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600609 HNullConstant* GetNullConstant(uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000610
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600611 HIntConstant* GetIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc) {
612 return CreateConstant(value, &cached_int_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000613 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600614 HLongConstant* GetLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc) {
615 return CreateConstant(value, &cached_long_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000616 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600617 HFloatConstant* GetFloatConstant(float value, uint32_t dex_pc = kNoDexPc) {
618 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000619 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600620 HDoubleConstant* GetDoubleConstant(double value, uint32_t dex_pc = kNoDexPc) {
621 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000622 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000623
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100624 HCurrentMethod* GetCurrentMethod();
625
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100626 const DexFile& GetDexFile() const {
627 return dex_file_;
628 }
629
630 uint32_t GetMethodIdx() const {
631 return method_idx_;
632 }
633
Igor Murashkind01745e2017-04-05 16:40:31 -0700634 // Get the method name (without the signature), e.g. "<init>"
635 const char* GetMethodName() const;
636
637 // Get the pretty method name (class + name + optionally signature).
638 std::string PrettyMethod(bool with_signature = true) const;
639
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100640 InvokeType GetInvokeType() const {
641 return invoke_type_;
642 }
643
Mark Mendellc4701932015-04-10 13:18:51 -0400644 InstructionSet GetInstructionSet() const {
645 return instruction_set_;
646 }
647
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000648 bool IsCompilingOsr() const { return osr_; }
649
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +0000650 bool IsCompilingBaseline() const { return baseline_; }
651
Mingyao Yang063fc772016-08-02 11:02:54 -0700652 ArenaSet<ArtMethod*>& GetCHASingleImplementationList() {
653 return cha_single_implementation_list_;
654 }
655
656 void AddCHASingleImplementationDependency(ArtMethod* method) {
657 cha_single_implementation_list_.insert(method);
658 }
659
660 bool HasShouldDeoptimizeFlag() const {
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800661 return number_of_cha_guards_ != 0;
Mingyao Yang063fc772016-08-02 11:02:54 -0700662 }
663
David Brazdil77a48ae2015-09-15 12:34:04 +0000664 bool HasTryCatch() const { return has_try_catch_; }
665 void SetHasTryCatch(bool value) { has_try_catch_ = value; }
David Brazdilbbd733e2015-08-18 17:48:17 +0100666
Artem Serov2808be82018-12-20 19:15:11 +0000667 bool HasMonitorOperations() const { return has_monitor_operations_; }
668 void SetHasMonitorOperations(bool value) { has_monitor_operations_ = value; }
669
Aart Bikb13c65b2017-03-21 20:14:07 -0700670 bool HasSIMD() const { return has_simd_; }
671 void SetHasSIMD(bool value) { has_simd_ = value; }
672
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800673 bool HasLoops() const { return has_loops_; }
674 void SetHasLoops(bool value) { has_loops_ = value; }
675
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000676 bool HasIrreducibleLoops() const { return has_irreducible_loops_; }
677 void SetHasIrreducibleLoops(bool value) { has_irreducible_loops_ = value; }
678
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100679 ArtMethod* GetArtMethod() const { return art_method_; }
680 void SetArtMethod(ArtMethod* method) { art_method_ = method; }
681
Roland Levillain5e8d5f02016-10-18 18:03:43 +0100682 // Returns an instruction with the opposite Boolean value from 'cond'.
Mark Mendellf6529172015-11-17 11:16:56 -0500683 // The instruction has been inserted into the graph, either as a constant, or
684 // before cursor.
685 HInstruction* InsertOppositeCondition(HInstruction* cond, HInstruction* cursor);
686
Vladimir Marko02ca05a2020-05-12 13:58:51 +0100687 ReferenceTypeInfo GetInexactObjectRti() {
688 return ReferenceTypeInfo::Create(handle_cache_.GetObjectClassHandle(), /* is_exact= */ false);
689 }
Nicolas Geoffray18401b72016-03-11 13:35:51 +0000690
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800691 uint32_t GetNumberOfCHAGuards() { return number_of_cha_guards_; }
692 void SetNumberOfCHAGuards(uint32_t num) { number_of_cha_guards_ = num; }
693 void IncrementNumberOfCHAGuards() { number_of_cha_guards_++; }
694
David Brazdil2d7352b2015-04-20 14:52:42 +0100695 private:
Roland Levillainfc600dc2014-12-02 17:16:31 +0000696 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100697 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000698
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000699 template <class InstructionType, typename ValueType>
700 InstructionType* CreateConstant(ValueType value,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600701 ArenaSafeMap<ValueType, InstructionType*>* cache,
702 uint32_t dex_pc = kNoDexPc) {
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000703 // Try to find an existing constant of the given value.
704 InstructionType* constant = nullptr;
705 auto cached_constant = cache->find(value);
706 if (cached_constant != cache->end()) {
707 constant = cached_constant->second;
708 }
709
710 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100711 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000712 if (constant == nullptr || constant->GetBlock() == nullptr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100713 constant = new (allocator_) InstructionType(value, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000714 cache->Overwrite(value, constant);
715 InsertConstant(constant);
716 }
717 return constant;
718 }
719
David Brazdil8d5b8b22015-03-24 10:51:52 +0000720 void InsertConstant(HConstant* instruction);
721
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000722 // Cache a float constant into the graph. This method should only be
723 // called by the SsaBuilder when creating "equivalent" instructions.
724 void CacheFloatConstant(HFloatConstant* constant);
725
726 // See CacheFloatConstant comment.
727 void CacheDoubleConstant(HDoubleConstant* constant);
728
Vladimir Markoca6fff82017-10-03 14:49:14 +0100729 ArenaAllocator* const allocator_;
730 ArenaStack* const arena_stack_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000731
Vladimir Marko02ca05a2020-05-12 13:58:51 +0100732 HandleCache handle_cache_;
733
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000734 // List of blocks in insertion order.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100735 ArenaVector<HBasicBlock*> blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000736
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100737 // List of blocks to perform a reverse post order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100738 ArenaVector<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000739
Aart Bik281c6812016-08-26 11:31:48 -0700740 // List of blocks to perform a linear order tree traversal. Unlike the reverse
741 // post order, this order is not incrementally kept up-to-date.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100742 ArenaVector<HBasicBlock*> linear_order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100743
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000744 HBasicBlock* entry_block_;
745 HBasicBlock* exit_block_;
746
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100747 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100748 uint16_t maximum_number_of_out_vregs_;
749
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100750 // The number of virtual registers in this method. Contains the parameters.
751 uint16_t number_of_vregs_;
752
753 // The number of virtual registers used by parameters of this method.
754 uint16_t number_of_in_vregs_;
755
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000756 // Number of vreg size slots that the temporaries use (used in baseline compiler).
757 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100758
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800759 // Flag whether there are bounds checks in the graph. We can skip
760 // BCE if it's false. It's only best effort to keep it up to date in
761 // the presence of code elimination so there might be false positives.
Mark Mendell1152c922015-04-24 17:06:35 -0400762 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800763
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800764 // Flag whether there are try/catch blocks in the graph. We will skip
765 // try/catch-related passes if it's false. It's only best effort to keep
766 // it up to date in the presence of code elimination so there might be
767 // false positives.
David Brazdil77a48ae2015-09-15 12:34:04 +0000768 bool has_try_catch_;
769
Artem Serov2808be82018-12-20 19:15:11 +0000770 // Flag whether there are any HMonitorOperation in the graph. If yes this will mandate
771 // DexRegisterMap to be present to allow deadlock analysis for non-debuggable code.
772 bool has_monitor_operations_;
773
Aart Bikb13c65b2017-03-21 20:14:07 -0700774 // Flag whether SIMD instructions appear in the graph. If true, the
775 // code generators may have to be more careful spilling the wider
776 // contents of SIMD registers.
777 bool has_simd_;
778
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800779 // Flag whether there are any loops in the graph. We can skip loop
780 // optimization if it's false. It's only best effort to keep it up
781 // to date in the presence of code elimination so there might be false
782 // positives.
783 bool has_loops_;
784
785 // Flag whether there are any irreducible loops in the graph. It's only
786 // best effort to keep it up to date in the presence of code elimination
787 // so there might be false positives.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000788 bool has_irreducible_loops_;
789
Hans Boehm206348c2018-12-05 11:11:33 -0800790 // Is the code known to be robust against eliminating dead references
791 // and the effects of early finalization? If false, dead reference variables
792 // are kept if they might be visible to the garbage collector.
793 // Currently this means that the class was declared to be dead-reference-safe,
794 // the method accesses no reachability-sensitive fields or data, and the same
795 // is true for any methods that were inlined into the current one.
796 bool dead_reference_safe_;
797
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000798 // Indicates whether the graph should be compiled in a way that
799 // ensures full debuggability. If false, we can apply more
800 // aggressive optimizations that may limit the level of debugging.
801 const bool debuggable_;
802
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000803 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000804 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000805
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100806 // The dex file from which the method is from.
807 const DexFile& dex_file_;
808
809 // The method index in the dex file.
810 const uint32_t method_idx_;
811
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100812 // If inlined, this encodes how the callee is being invoked.
813 const InvokeType invoke_type_;
814
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100815 // Whether the graph has been transformed to SSA form. Only used
816 // in debug mode to ensure we are not using properties only valid
817 // for non-SSA form (like the number of temporaries).
818 bool in_ssa_form_;
819
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800820 // Number of CHA guards in the graph. Used to short-circuit the
821 // CHA guard optimization pass when there is no CHA guard left.
822 uint32_t number_of_cha_guards_;
823
Mathieu Chartiere401d142015-04-22 13:56:20 -0700824 const InstructionSet instruction_set_;
825
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000826 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000827 HNullConstant* cached_null_constant_;
828 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000829 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000830 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000831 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000832
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100833 HCurrentMethod* cached_current_method_;
834
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100835 // The ArtMethod this graph is for. Note that for AOT, it may be null,
836 // for example for methods whose declaring class could not be resolved
837 // (such as when the superclass could not be found).
838 ArtMethod* art_method_;
839
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000840 // Whether we are compiling this graph for on stack replacement: this will
841 // make all loops seen as irreducible and emit special stack maps to mark
842 // compiled code entries which the interpreter can directly jump to.
843 const bool osr_;
844
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +0000845 // Whether we are compiling baseline (not running optimizations). This affects
846 // the code being generated.
847 const bool baseline_;
848
Mingyao Yang063fc772016-08-02 11:02:54 -0700849 // List of methods that are assumed to have single implementation.
850 ArenaSet<ArtMethod*> cha_single_implementation_list_;
851
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000852 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100853 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000854 friend class HInliner; // For the reverse post order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000855 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000856 DISALLOW_COPY_AND_ASSIGN(HGraph);
857};
858
Vladimir Markof9f64412015-09-02 14:05:49 +0100859class HLoopInformation : public ArenaObject<kArenaAllocLoopInfo> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000860 public:
861 HLoopInformation(HBasicBlock* header, HGraph* graph)
862 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100863 suspend_check_(nullptr),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000864 irreducible_(false),
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100865 contains_irreducible_loop_(false),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100866 back_edges_(graph->GetAllocator()->Adapter(kArenaAllocLoopInfoBackEdges)),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100867 // Make bit vector growable, as the number of blocks may change.
Vladimir Markoca6fff82017-10-03 14:49:14 +0100868 blocks_(graph->GetAllocator(),
869 graph->GetBlocks().size(),
870 true,
871 kArenaAllocLoopInfoBackEdges) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100872 back_edges_.reserve(kDefaultNumberOfBackEdges);
873 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100874
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000875 bool IsIrreducible() const { return irreducible_; }
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100876 bool ContainsIrreducibleLoop() const { return contains_irreducible_loop_; }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000877
878 void Dump(std::ostream& os);
879
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100880 HBasicBlock* GetHeader() const {
881 return header_;
882 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000883
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000884 void SetHeader(HBasicBlock* block) {
885 header_ = block;
886 }
887
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100888 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
889 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
890 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
891
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000892 void AddBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100893 back_edges_.push_back(back_edge);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000894 }
895
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100896 void RemoveBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100897 RemoveElement(back_edges_, back_edge);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100898 }
899
David Brazdil46e2a392015-03-16 17:31:52 +0000900 bool IsBackEdge(const HBasicBlock& block) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100901 return ContainsElement(back_edges_, &block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100902 }
903
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000904 size_t NumberOfBackEdges() const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100905 return back_edges_.size();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000906 }
907
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100908 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100909
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100910 const ArenaVector<HBasicBlock*>& GetBackEdges() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100911 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100912 }
913
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100914 // Returns the lifetime position of the back edge that has the
915 // greatest lifetime position.
916 size_t GetLifetimeEnd() const;
917
918 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100919 ReplaceElement(back_edges_, existing, new_back_edge);
Nicolas Geoffray57902602015-04-21 14:28:41 +0100920 }
921
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000922 // Finds blocks that are part of this loop.
923 void Populate();
David Brazdila4b8c212015-05-07 09:59:30 +0100924
Artem Serov7f4aff62017-06-21 17:02:18 +0100925 // Updates blocks population of the loop and all of its outer' ones recursively after the
926 // population of the inner loop is updated.
927 void PopulateInnerLoopUpwards(HLoopInformation* inner_loop);
928
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100929 // Returns whether this loop information contains `block`.
930 // Note that this loop information *must* be populated before entering this function.
931 bool Contains(const HBasicBlock& block) const;
932
933 // Returns whether this loop information is an inner loop of `other`.
934 // Note that `other` *must* be populated before entering this function.
935 bool IsIn(const HLoopInformation& other) const;
936
Mingyao Yang4b467ed2015-11-19 17:04:22 -0800937 // Returns true if instruction is not defined within this loop.
938 bool IsDefinedOutOfTheLoop(HInstruction* instruction) const;
Aart Bik73f1f3b2015-10-28 15:28:08 -0700939
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100940 const ArenaBitVector& GetBlocks() const { return blocks_; }
941
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000942 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000943 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000944
Nicolas Geoffray788f2f02016-01-22 12:41:38 +0000945 void ClearAllBlocks() {
946 blocks_.ClearAllBits();
947 }
948
David Brazdil3f4a5222016-05-06 12:46:21 +0100949 bool HasBackEdgeNotDominatedByHeader() const;
950
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100951 bool IsPopulated() const {
952 return blocks_.GetHighestBitSet() != -1;
953 }
954
Anton Shaminf89381f2016-05-16 16:44:13 +0600955 bool DominatesAllBackEdges(HBasicBlock* block);
956
David Sehrc757dec2016-11-04 15:48:34 -0700957 bool HasExitEdge() const;
958
Artem Serov7f4aff62017-06-21 17:02:18 +0100959 // Resets back edge and blocks-in-loop data.
960 void ResetBasicBlockData() {
961 back_edges_.clear();
962 ClearAllBlocks();
963 }
964
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000965 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100966 // Internal recursive implementation of `Populate`.
967 void PopulateRecursive(HBasicBlock* block);
David Brazdilc2e8af92016-04-05 17:15:19 +0100968 void PopulateIrreducibleRecursive(HBasicBlock* block, ArenaBitVector* finalized);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100969
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000970 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100971 HSuspendCheck* suspend_check_;
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000972 bool irreducible_;
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100973 bool contains_irreducible_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100974 ArenaVector<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100975 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000976
977 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
978};
979
David Brazdilec16f792015-08-19 15:04:01 +0100980// Stores try/catch information for basic blocks.
981// Note that HGraph is constructed so that catch blocks cannot simultaneously
982// be try blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100983class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> {
David Brazdilec16f792015-08-19 15:04:01 +0100984 public:
985 // Try block information constructor.
986 explicit TryCatchInformation(const HTryBoundary& try_entry)
987 : try_entry_(&try_entry),
988 catch_dex_file_(nullptr),
Nicolas Geoffrayebc3b252019-01-09 10:47:02 +0000989 catch_type_index_(dex::TypeIndex::Invalid()) {
David Brazdilec16f792015-08-19 15:04:01 +0100990 DCHECK(try_entry_ != nullptr);
991 }
992
993 // Catch block information constructor.
Andreas Gampea5b09a62016-11-17 15:21:22 -0800994 TryCatchInformation(dex::TypeIndex catch_type_index, const DexFile& dex_file)
David Brazdilec16f792015-08-19 15:04:01 +0100995 : try_entry_(nullptr),
996 catch_dex_file_(&dex_file),
997 catch_type_index_(catch_type_index) {}
998
999 bool IsTryBlock() const { return try_entry_ != nullptr; }
1000
1001 const HTryBoundary& GetTryEntry() const {
1002 DCHECK(IsTryBlock());
1003 return *try_entry_;
1004 }
1005
1006 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
1007
Nicolas Geoffrayebc3b252019-01-09 10:47:02 +00001008 bool IsValidTypeIndex() const {
David Brazdilec16f792015-08-19 15:04:01 +01001009 DCHECK(IsCatchBlock());
Nicolas Geoffrayebc3b252019-01-09 10:47:02 +00001010 return catch_type_index_.IsValid();
David Brazdilec16f792015-08-19 15:04:01 +01001011 }
1012
Andreas Gampea5b09a62016-11-17 15:21:22 -08001013 dex::TypeIndex GetCatchTypeIndex() const {
David Brazdilec16f792015-08-19 15:04:01 +01001014 DCHECK(IsCatchBlock());
1015 return catch_type_index_;
1016 }
1017
1018 const DexFile& GetCatchDexFile() const {
1019 DCHECK(IsCatchBlock());
1020 return *catch_dex_file_;
1021 }
1022
Nicolas Geoffrayebc3b252019-01-09 10:47:02 +00001023 void SetInvalidTypeIndex() {
1024 catch_type_index_ = dex::TypeIndex::Invalid();
1025 }
1026
David Brazdilec16f792015-08-19 15:04:01 +01001027 private:
1028 // One of possibly several TryBoundary instructions entering the block's try.
1029 // Only set for try blocks.
1030 const HTryBoundary* try_entry_;
1031
1032 // Exception type information. Only set for catch blocks.
1033 const DexFile* catch_dex_file_;
Nicolas Geoffrayebc3b252019-01-09 10:47:02 +00001034 dex::TypeIndex catch_type_index_;
David Brazdilec16f792015-08-19 15:04:01 +01001035};
1036
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001037static constexpr size_t kNoLifetime = -1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001038static constexpr uint32_t kInvalidBlockId = static_cast<uint32_t>(-1);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001039
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001040// A block in a method. Contains the list of instructions represented
1041// as a double linked list. Each block knows its predecessors and
1042// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001043
Vladimir Markof9f64412015-09-02 14:05:49 +01001044class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001045 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -07001046 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001047 : graph_(graph),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001048 predecessors_(graph->GetAllocator()->Adapter(kArenaAllocPredecessors)),
1049 successors_(graph->GetAllocator()->Adapter(kArenaAllocSuccessors)),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001050 loop_information_(nullptr),
1051 dominator_(nullptr),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001052 dominated_blocks_(graph->GetAllocator()->Adapter(kArenaAllocDominated)),
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001053 block_id_(kInvalidBlockId),
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001054 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001055 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001056 lifetime_end_(kNoLifetime),
Vladimir Marko60584552015-09-03 13:35:12 +00001057 try_catch_information_(nullptr) {
1058 predecessors_.reserve(kDefaultNumberOfPredecessors);
1059 successors_.reserve(kDefaultNumberOfSuccessors);
1060 dominated_blocks_.reserve(kDefaultNumberOfDominatedBlocks);
1061 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001062
Vladimir Marko60584552015-09-03 13:35:12 +00001063 const ArenaVector<HBasicBlock*>& GetPredecessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001064 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001065 }
1066
Vladimir Marko60584552015-09-03 13:35:12 +00001067 const ArenaVector<HBasicBlock*>& GetSuccessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001068 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001069 }
1070
David Brazdild26a4112015-11-10 11:07:31 +00001071 ArrayRef<HBasicBlock* const> GetNormalSuccessors() const;
1072 ArrayRef<HBasicBlock* const> GetExceptionalSuccessors() const;
1073
Vladimir Marko60584552015-09-03 13:35:12 +00001074 bool HasSuccessor(const HBasicBlock* block, size_t start_from = 0u) {
1075 return ContainsElement(successors_, block, start_from);
1076 }
1077
1078 const ArenaVector<HBasicBlock*>& GetDominatedBlocks() const {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001079 return dominated_blocks_;
1080 }
1081
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001082 bool IsEntryBlock() const {
1083 return graph_->GetEntryBlock() == this;
1084 }
1085
1086 bool IsExitBlock() const {
1087 return graph_->GetExitBlock() == this;
1088 }
1089
David Brazdil46e2a392015-03-16 17:31:52 +00001090 bool IsSingleGoto() const;
Mads Ager16e52892017-07-14 13:11:37 +02001091 bool IsSingleReturn() const;
Mingyao Yang46721ef2017-10-05 14:45:17 -07001092 bool IsSingleReturnOrReturnVoidAllowingPhis() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +00001093 bool IsSingleTryBoundary() const;
1094
1095 // Returns true if this block emits nothing but a jump.
1096 bool IsSingleJump() const {
1097 HLoopInformation* loop_info = GetLoopInformation();
1098 return (IsSingleGoto() || IsSingleTryBoundary())
1099 // Back edges generate a suspend check.
1100 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
1101 }
David Brazdil46e2a392015-03-16 17:31:52 +00001102
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001103 void AddBackEdge(HBasicBlock* back_edge) {
1104 if (loop_information_ == nullptr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001105 loop_information_ = new (graph_->GetAllocator()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001106 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001107 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001108 loop_information_->AddBackEdge(back_edge);
1109 }
1110
Artem Serov7f4aff62017-06-21 17:02:18 +01001111 // Registers a back edge; if the block was not a loop header before the call associates a newly
1112 // created loop info with it.
1113 //
1114 // Used in SuperblockCloner to preserve LoopInformation object instead of reseting loop
1115 // info for all blocks during back edges recalculation.
1116 void AddBackEdgeWhileUpdating(HBasicBlock* back_edge) {
1117 if (loop_information_ == nullptr || loop_information_->GetHeader() != this) {
1118 loop_information_ = new (graph_->GetAllocator()) HLoopInformation(this, graph_);
1119 }
1120 loop_information_->AddBackEdge(back_edge);
1121 }
1122
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001123 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001124 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001125
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001126 uint32_t GetBlockId() const { return block_id_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001127 void SetBlockId(int id) { block_id_ = id; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001128 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001129
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001130 HBasicBlock* GetDominator() const { return dominator_; }
1131 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Vladimir Marko60584552015-09-03 13:35:12 +00001132 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.push_back(block); }
1133
1134 void RemoveDominatedBlock(HBasicBlock* block) {
1135 RemoveElement(dominated_blocks_, block);
Vladimir Marko91e11c02015-09-02 17:03:22 +01001136 }
Vladimir Marko60584552015-09-03 13:35:12 +00001137
1138 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
1139 ReplaceElement(dominated_blocks_, existing, new_block);
1140 }
1141
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +01001142 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001143
1144 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +01001145 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001146 }
1147
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001148 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
1149 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +01001150 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001151 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +01001152 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
1153 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001154
Nicolas Geoffray09aa1472016-01-19 10:52:54 +00001155 HInstruction* GetFirstInstructionDisregardMoves() const;
1156
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001157 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001158 successors_.push_back(block);
1159 block->predecessors_.push_back(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001160 }
1161
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001162 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
1163 size_t successor_index = GetSuccessorIndexOf(existing);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001164 existing->RemovePredecessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +00001165 new_block->predecessors_.push_back(this);
1166 successors_[successor_index] = new_block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001167 }
1168
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001169 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
1170 size_t predecessor_index = GetPredecessorIndexOf(existing);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001171 existing->RemoveSuccessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +00001172 new_block->successors_.push_back(this);
1173 predecessors_[predecessor_index] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001174 }
1175
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001176 // Insert `this` between `predecessor` and `successor. This method
Alex Light1e52a072019-06-25 09:12:04 -07001177 // preserves the indices, and will update the first edge found between
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001178 // `predecessor` and `successor`.
1179 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
1180 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001181 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko60584552015-09-03 13:35:12 +00001182 successor->predecessors_[predecessor_index] = this;
1183 predecessor->successors_[successor_index] = this;
1184 successors_.push_back(successor);
1185 predecessors_.push_back(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001186 }
1187
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001188 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001189 predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001190 }
1191
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001192 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001193 successors_.erase(successors_.begin() + GetSuccessorIndexOf(block));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001194 }
1195
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001196 void ClearAllPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001197 predecessors_.clear();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001198 }
1199
1200 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001201 predecessors_.push_back(block);
1202 block->successors_.push_back(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001203 }
1204
Nicolas Geoffray604c6e42014-09-17 12:08:44 +01001205 void SwapPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001206 DCHECK_EQ(predecessors_.size(), 2u);
1207 std::swap(predecessors_[0], predecessors_[1]);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +01001208 }
1209
David Brazdil769c9e52015-04-27 13:54:09 +01001210 void SwapSuccessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001211 DCHECK_EQ(successors_.size(), 2u);
1212 std::swap(successors_[0], successors_[1]);
David Brazdil769c9e52015-04-27 13:54:09 +01001213 }
1214
David Brazdilfc6a86a2015-06-26 10:33:45 +00001215 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko60584552015-09-03 13:35:12 +00001216 return IndexOfElement(predecessors_, predecessor);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001217 }
1218
David Brazdilfc6a86a2015-06-26 10:33:45 +00001219 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko60584552015-09-03 13:35:12 +00001220 return IndexOfElement(successors_, successor);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001221 }
1222
David Brazdilfc6a86a2015-06-26 10:33:45 +00001223 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00001224 DCHECK_EQ(GetPredecessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +01001225 return GetPredecessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +00001226 }
1227
1228 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00001229 DCHECK_EQ(GetSuccessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +01001230 return GetSuccessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +00001231 }
1232
1233 // Returns whether the first occurrence of `predecessor` in the list of
1234 // predecessors is at index `idx`.
1235 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01001236 DCHECK_EQ(GetPredecessors()[idx], predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +00001237 return GetPredecessorIndexOf(predecessor) == idx;
1238 }
1239
David Brazdild7558da2015-09-22 13:04:14 +01001240 // Create a new block between this block and its predecessors. The new block
1241 // is added to the graph, all predecessor edges are relinked to it and an edge
1242 // is created to `this`. Returns the new empty block. Reverse post order or
1243 // loop and try/catch information are not updated.
1244 HBasicBlock* CreateImmediateDominator();
1245
David Brazdilfc6a86a2015-06-26 10:33:45 +00001246 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +01001247 // created, latter block. Note that this method will add the block to the
1248 // graph, create a Goto at the end of the former block and will create an edge
1249 // between the blocks. It will not, however, update the reverse post order or
David Brazdild7558da2015-09-22 13:04:14 +01001250 // loop and try/catch information.
David Brazdilfc6a86a2015-06-26 10:33:45 +00001251 HBasicBlock* SplitBefore(HInstruction* cursor);
1252
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001253 // Split the block into two blocks just before `cursor`. Returns the newly
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001254 // created block. Note that this method just updates raw block information,
1255 // like predecessors, successors, dominators, and instruction list. It does not
1256 // update the graph, reverse post order, loop information, nor make sure the
1257 // blocks are consistent (for example ending with a control flow instruction).
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001258 HBasicBlock* SplitBeforeForInlining(HInstruction* cursor);
1259
1260 // Similar to `SplitBeforeForInlining` but does it after `cursor`.
1261 HBasicBlock* SplitAfterForInlining(HInstruction* cursor);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001262
1263 // Merge `other` at the end of `this`. Successors and dominated blocks of
1264 // `other` are changed to be successors and dominated blocks of `this`. Note
1265 // that this method does not update the graph, reverse post order, loop
1266 // information, nor make sure the blocks are consistent (for example ending
1267 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +01001268 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001269
1270 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
1271 // of `this` are moved to `other`.
1272 // Note that this method does not update the graph, reverse post order, loop
1273 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +00001274 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001275 void ReplaceWith(HBasicBlock* other);
1276
Aart Bik6b69e0a2017-01-11 10:20:43 -08001277 // Merges the instructions of `other` at the end of `this`.
1278 void MergeInstructionsWith(HBasicBlock* other);
1279
David Brazdil2d7352b2015-04-20 14:52:42 +01001280 // Merge `other` at the end of `this`. This method updates loops, reverse post
1281 // order, links to predecessors, successors, dominators and deletes the block
1282 // from the graph. The two blocks must be successive, i.e. `this` the only
1283 // predecessor of `other` and vice versa.
1284 void MergeWith(HBasicBlock* other);
1285
1286 // Disconnects `this` from all its predecessors, successors and dominator,
1287 // removes it from all loops it is included in and eventually from the graph.
1288 // The block must not dominate any other block. Predecessors and successors
1289 // are safely updated.
1290 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +00001291
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001292 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001293 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001294 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001295 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Artem Serovcced8ba2017-07-19 18:18:09 +01001296 // Replace phi `initial` with `replacement` within this block.
1297 void ReplaceAndRemovePhiWith(HPhi* initial, HPhi* replacement);
Roland Levillainccc07a92014-09-16 14:48:16 +01001298 // Replace instruction `initial` with `replacement` within this block.
1299 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
1300 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001301 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001302 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +00001303 // RemoveInstruction and RemovePhi delete a given instruction from the respective
1304 // instruction list. With 'ensure_safety' set to true, it verifies that the
1305 // instruction is not in use and removes it from the use lists of its inputs.
1306 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
1307 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +01001308 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001309
1310 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +01001311 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001312 }
1313
Roland Levillain6b879dd2014-09-22 17:13:44 +01001314 bool IsLoopPreHeaderFirstPredecessor() const {
1315 DCHECK(IsLoopHeader());
Vladimir Markoec7802a2015-10-01 20:57:57 +01001316 return GetPredecessors()[0] == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +01001317 }
1318
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001319 bool IsFirstPredecessorBackEdge() const {
1320 DCHECK(IsLoopHeader());
1321 return GetLoopInformation()->IsBackEdge(*GetPredecessors()[0]);
1322 }
1323
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001324 HLoopInformation* GetLoopInformation() const {
1325 return loop_information_;
1326 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001327
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001328 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001329 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001330 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001331 void SetInLoop(HLoopInformation* info) {
1332 if (IsLoopHeader()) {
1333 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +01001334 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001335 loop_information_ = info;
1336 } else if (loop_information_->Contains(*info->GetHeader())) {
1337 // Block is currently part of an outer loop. Make it part of this inner loop.
1338 // Note that a non loop header having a loop information means this loop information
1339 // has already been populated
1340 loop_information_ = info;
1341 } else {
1342 // Block is part of an inner loop. Do not update the loop information.
1343 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
1344 // at this point, because this method is being called while populating `info`.
1345 }
1346 }
1347
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001348 // Raw update of the loop information.
1349 void SetLoopInformation(HLoopInformation* info) {
1350 loop_information_ = info;
1351 }
1352
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001353 bool IsInLoop() const { return loop_information_ != nullptr; }
1354
David Brazdilec16f792015-08-19 15:04:01 +01001355 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
1356
1357 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
1358 try_catch_information_ = try_catch_information;
1359 }
1360
1361 bool IsTryBlock() const {
1362 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
1363 }
1364
1365 bool IsCatchBlock() const {
1366 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
1367 }
David Brazdilffee3d32015-07-06 11:48:53 +01001368
1369 // Returns the try entry that this block's successors should have. They will
1370 // be in the same try, unless the block ends in a try boundary. In that case,
1371 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +01001372 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001373
Aart Bik75ff2c92018-04-21 01:28:11 +00001374 bool HasThrowingInstructions() const;
1375
David Brazdila4b8c212015-05-07 09:59:30 +01001376 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001377 bool Dominates(HBasicBlock* block) const;
1378
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001379 size_t GetLifetimeStart() const { return lifetime_start_; }
1380 size_t GetLifetimeEnd() const { return lifetime_end_; }
1381
1382 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
1383 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
1384
David Brazdil8d5b8b22015-03-24 10:51:52 +00001385 bool EndsWithControlFlowInstruction() const;
Aart Bik4dc09e72018-05-11 14:40:31 -07001386 bool EndsWithReturn() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001387 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001388 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001389 bool HasSinglePhi() const;
1390
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001391 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001392 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +00001393 ArenaVector<HBasicBlock*> predecessors_;
1394 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001395 HInstructionList instructions_;
1396 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001397 HLoopInformation* loop_information_;
1398 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +00001399 ArenaVector<HBasicBlock*> dominated_blocks_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001400 uint32_t block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001401 // The dex program counter of the first instruction of this block.
1402 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001403 size_t lifetime_start_;
1404 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +01001405 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +01001406
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001407 friend class HGraph;
1408 friend class HInstruction;
1409
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001410 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
1411};
1412
David Brazdilb2bd1c52015-03-25 11:17:37 +00001413// Iterates over the LoopInformation of all loops which contain 'block'
1414// from the innermost to the outermost.
1415class HLoopInformationOutwardIterator : public ValueObject {
1416 public:
1417 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
1418 : current_(block.GetLoopInformation()) {}
1419
1420 bool Done() const { return current_ == nullptr; }
1421
1422 void Advance() {
1423 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +01001424 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +00001425 }
1426
1427 HLoopInformation* Current() const {
1428 DCHECK(!Done());
1429 return current_;
1430 }
1431
1432 private:
1433 HLoopInformation* current_;
1434
1435 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
1436};
1437
Artem Serov1a719e42019-07-18 14:24:55 +01001438#define FOR_EACH_CONCRETE_INSTRUCTION_SCALAR_COMMON(M) \
Aart Bike9f37602015-10-09 11:15:55 -07001439 M(Above, Condition) \
1440 M(AboveOrEqual, Condition) \
Aart Bik3dad3412018-02-28 12:01:46 -08001441 M(Abs, UnaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001442 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001443 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001444 M(ArrayGet, Instruction) \
1445 M(ArrayLength, Instruction) \
1446 M(ArraySet, Instruction) \
Aart Bike9f37602015-10-09 11:15:55 -07001447 M(Below, Condition) \
1448 M(BelowOrEqual, Condition) \
David Brazdil66d126e2015-04-03 16:02:44 +01001449 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001450 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001451 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001452 M(CheckCast, Instruction) \
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00001453 M(ClassTableGet, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001454 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001455 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001456 M(Compare, BinaryOperation) \
Igor Murashkind01745e2017-04-05 16:40:31 -07001457 M(ConstructorFence, Instruction) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001458 M(CurrentMethod, Instruction) \
Mingyao Yang063fc772016-08-02 11:02:54 -07001459 M(ShouldDeoptimizeFlag, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001460 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001461 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001462 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001463 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001464 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001465 M(Exit, Instruction) \
1466 M(FloatConstant, Constant) \
1467 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001468 M(GreaterThan, Condition) \
1469 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001470 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001471 M(InstanceFieldGet, Instruction) \
1472 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001473 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001474 M(IntConstant, Constant) \
xueliang.zhonge0eb4832017-10-30 13:43:14 +00001475 M(IntermediateAddress, Instruction) \
Calin Juravle175dc732015-08-25 15:42:32 +01001476 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001477 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001478 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001479 M(InvokeVirtual, Invoke) \
Orion Hodsonac141392017-01-13 11:53:47 +00001480 M(InvokePolymorphic, Invoke) \
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001481 M(InvokeCustom, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001482 M(LessThan, Condition) \
1483 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001484 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001485 M(LoadException, Instruction) \
Orion Hodsondbaa5c72018-05-10 08:22:46 +01001486 M(LoadMethodHandle, Instruction) \
Orion Hodson18259d72018-04-12 11:18:23 +01001487 M(LoadMethodType, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001488 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001489 M(LongConstant, Constant) \
Aart Bik1f8d51b2018-02-15 10:42:37 -08001490 M(Max, Instruction) \
Calin Juravle27df7582015-04-17 19:12:31 +01001491 M(MemoryBarrier, Instruction) \
Aart Bik1f8d51b2018-02-15 10:42:37 -08001492 M(Min, BinaryOperation) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001493 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001494 M(Mul, BinaryOperation) \
David Srbecky0cf44932015-12-09 14:09:59 +00001495 M(NativeDebugInfo, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001496 M(Neg, UnaryOperation) \
1497 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001498 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001499 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001500 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001501 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001502 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001503 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001504 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001505 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001506 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001507 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001508 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001509 M(Return, Instruction) \
1510 M(ReturnVoid, Instruction) \
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001511 M(Ror, BinaryOperation) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001512 M(Shl, BinaryOperation) \
1513 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001514 M(StaticFieldGet, Instruction) \
1515 M(StaticFieldSet, Instruction) \
Vladimir Marko552a1342017-10-31 10:56:47 +00001516 M(StringBuilderAppend, Instruction) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001517 M(UnresolvedInstanceFieldGet, Instruction) \
1518 M(UnresolvedInstanceFieldSet, Instruction) \
1519 M(UnresolvedStaticFieldGet, Instruction) \
1520 M(UnresolvedStaticFieldSet, Instruction) \
David Brazdil74eb1b22015-12-14 11:44:01 +00001521 M(Select, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001522 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001523 M(SuspendCheck, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001524 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001525 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001526 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001527 M(UShr, BinaryOperation) \
Artem Serov1a719e42019-07-18 14:24:55 +01001528 M(Xor, BinaryOperation)
1529
1530#define FOR_EACH_CONCRETE_INSTRUCTION_VECTOR_COMMON(M) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001531 M(VecReplicateScalar, VecUnaryOperation) \
Aart Bik0148de42017-09-05 09:25:01 -07001532 M(VecExtractScalar, VecUnaryOperation) \
1533 M(VecReduce, VecUnaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001534 M(VecCnv, VecUnaryOperation) \
1535 M(VecNeg, VecUnaryOperation) \
Aart Bik6daebeb2017-04-03 14:35:41 -07001536 M(VecAbs, VecUnaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001537 M(VecNot, VecUnaryOperation) \
1538 M(VecAdd, VecBinaryOperation) \
Aart Bikf3e61ee2017-04-12 17:09:20 -07001539 M(VecHalvingAdd, VecBinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001540 M(VecSub, VecBinaryOperation) \
1541 M(VecMul, VecBinaryOperation) \
1542 M(VecDiv, VecBinaryOperation) \
Aart Bikf3e61ee2017-04-12 17:09:20 -07001543 M(VecMin, VecBinaryOperation) \
1544 M(VecMax, VecBinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001545 M(VecAnd, VecBinaryOperation) \
1546 M(VecAndNot, VecBinaryOperation) \
1547 M(VecOr, VecBinaryOperation) \
1548 M(VecXor, VecBinaryOperation) \
Aart Bik29aa0822018-03-08 11:28:00 -08001549 M(VecSaturationAdd, VecBinaryOperation) \
1550 M(VecSaturationSub, VecBinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001551 M(VecShl, VecBinaryOperation) \
1552 M(VecShr, VecBinaryOperation) \
1553 M(VecUShr, VecBinaryOperation) \
Aart Bik8de59162017-04-21 09:42:01 -07001554 M(VecSetScalars, VecOperation) \
Artem Serovf34dd202017-04-10 17:41:46 +01001555 M(VecMultiplyAccumulate, VecOperation) \
Aart Bikdbbac8f2017-09-01 13:06:08 -07001556 M(VecSADAccumulate, VecOperation) \
Artem Serovaaac0e32018-08-07 00:52:22 +01001557 M(VecDotProd, VecOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001558 M(VecLoad, VecMemoryOperation) \
1559 M(VecStore, VecMemoryOperation) \
Artem Serov07718842020-02-24 18:51:42 +00001560 M(VecPredSetAll, VecPredSetOperation) \
1561 M(VecPredWhile, VecPredSetOperation) \
1562 M(VecPredCondition, VecOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001563
Artem Serov1a719e42019-07-18 14:24:55 +01001564#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
1565 FOR_EACH_CONCRETE_INSTRUCTION_SCALAR_COMMON(M) \
1566 FOR_EACH_CONCRETE_INSTRUCTION_VECTOR_COMMON(M)
1567
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001568/*
1569 * Instructions, shared across several (not all) architectures.
1570 */
1571#if !defined(ART_ENABLE_CODEGEN_arm) && !defined(ART_ENABLE_CODEGEN_arm64)
1572#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M)
1573#else
1574#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Artem Serov7fc63502016-02-09 17:15:29 +00001575 M(BitwiseNegatedRight, Instruction) \
Anton Kirilov74234da2017-01-13 14:42:47 +00001576 M(DataProcWithShifterOp, Instruction) \
Artem Serov328429f2016-07-06 16:23:04 +01001577 M(MultiplyAccumulate, Instruction) \
Artem Serove1811ed2017-04-27 16:50:47 +01001578 M(IntermediateAddressIndex, Instruction)
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001579#endif
1580
Alexandre Ramesef20f712015-06-09 10:29:30 +01001581#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
1582
1583#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
1584
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001585#ifndef ART_ENABLE_CODEGEN_x86
1586#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1587#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001588#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1589 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001590 M(X86LoadFromConstantTable, Instruction) \
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001591 M(X86FPNeg, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001592 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001593#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001594
Shalini Salomi Bodapatidd121f62018-10-26 15:03:53 +05301595#if defined(ART_ENABLE_CODEGEN_x86) || defined(ART_ENABLE_CODEGEN_x86_64)
1596#define FOR_EACH_CONCRETE_INSTRUCTION_X86_COMMON(M) \
Shalini Salomi Bodapati81d15be2019-05-30 11:00:42 +05301597 M(X86AndNot, Instruction) \
Shalini Salomi Bodapatib45a4352019-07-10 16:09:41 +05301598 M(X86MaskOrResetLeastSetBit, Instruction)
Shalini Salomi Bodapatidd121f62018-10-26 15:03:53 +05301599#else
1600#define FOR_EACH_CONCRETE_INSTRUCTION_X86_COMMON(M)
1601#endif
1602
Alexandre Ramesef20f712015-06-09 10:29:30 +01001603#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1604
1605#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1606 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001607 FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001608 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1609 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
1610 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
Shalini Salomi Bodapatidd121f62018-10-26 15:03:53 +05301611 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M) \
1612 FOR_EACH_CONCRETE_INSTRUCTION_X86_COMMON(M)
Alexandre Ramesef20f712015-06-09 10:29:30 +01001613
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001614#define FOR_EACH_ABSTRACT_INSTRUCTION(M) \
1615 M(Condition, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001616 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001617 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001618 M(BinaryOperation, Instruction) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001619 M(Invoke, Instruction) \
1620 M(VecOperation, Instruction) \
1621 M(VecUnaryOperation, VecOperation) \
1622 M(VecBinaryOperation, VecOperation) \
Artem Serov07718842020-02-24 18:51:42 +00001623 M(VecMemoryOperation, VecOperation) \
1624 M(VecPredSetOperation, VecOperation)
Dave Allison20dfc792014-06-16 20:44:29 -07001625
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001626#define FOR_EACH_INSTRUCTION(M) \
1627 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1628 FOR_EACH_ABSTRACT_INSTRUCTION(M)
1629
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001630#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001631FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1632#undef FORWARD_DECLARATION
1633
Vladimir Marko372f10e2016-05-17 16:30:10 +01001634#define DECLARE_INSTRUCTION(type) \
Artem Serovcced8ba2017-07-19 18:18:09 +01001635 private: \
1636 H##type& operator=(const H##type&) = delete; \
1637 public: \
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01001638 const char* DebugName() const override { return #type; } \
1639 HInstruction* Clone(ArenaAllocator* arena) const override { \
Artem Serovcced8ba2017-07-19 18:18:09 +01001640 DCHECK(IsClonable()); \
1641 return new (arena) H##type(*this->As##type()); \
1642 } \
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01001643 void Accept(HGraphVisitor* visitor) override
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001644
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001645#define DECLARE_ABSTRACT_INSTRUCTION(type) \
Artem Serovcced8ba2017-07-19 18:18:09 +01001646 private: \
1647 H##type& operator=(const H##type&) = delete; \
Vladimir Markoa90dd512018-05-04 15:04:45 +01001648 public:
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001649
Artem Serovcced8ba2017-07-19 18:18:09 +01001650#define DEFAULT_COPY_CONSTRUCTOR(type) \
1651 explicit H##type(const H##type& other) = default;
1652
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001653template <typename T>
Vladimir Marko82b07402017-03-01 19:02:04 +00001654class HUseListNode : public ArenaObject<kArenaAllocUseListNode>,
1655 public IntrusiveForwardListNode<HUseListNode<T>> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001656 public:
Igor Murashkind01745e2017-04-05 16:40:31 -07001657 // Get the instruction which has this use as one of the inputs.
David Brazdiled596192015-01-23 10:39:45 +00001658 T GetUser() const { return user_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07001659 // Get the position of the input record that this use corresponds to.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001660 size_t GetIndex() const { return index_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07001661 // Set the position of the input record that this use corresponds to.
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001662 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001663
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001664 private:
David Brazdiled596192015-01-23 10:39:45 +00001665 HUseListNode(T user, size_t index)
Vladimir Marko46817b82016-03-29 12:21:58 +01001666 : user_(user), index_(index) {}
David Brazdiled596192015-01-23 10:39:45 +00001667
1668 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001669 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001670
Vladimir Marko46817b82016-03-29 12:21:58 +01001671 friend class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001672
1673 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1674};
1675
David Brazdiled596192015-01-23 10:39:45 +00001676template <typename T>
Vladimir Marko46817b82016-03-29 12:21:58 +01001677using HUseList = IntrusiveForwardList<HUseListNode<T>>;
David Brazdiled596192015-01-23 10:39:45 +00001678
David Brazdil1abb4192015-02-17 18:33:36 +00001679// This class is used by HEnvironment and HInstruction classes to record the
1680// instructions they use and pointers to the corresponding HUseListNodes kept
1681// by the used instructions.
1682template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001683class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001684 public:
Vladimir Marko46817b82016-03-29 12:21:58 +01001685 HUserRecord() : instruction_(nullptr), before_use_node_() {}
1686 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), before_use_node_() {}
David Brazdil1abb4192015-02-17 18:33:36 +00001687
Vladimir Marko46817b82016-03-29 12:21:58 +01001688 HUserRecord(const HUserRecord<T>& old_record, typename HUseList<T>::iterator before_use_node)
1689 : HUserRecord(old_record.instruction_, before_use_node) {}
1690 HUserRecord(HInstruction* instruction, typename HUseList<T>::iterator before_use_node)
1691 : instruction_(instruction), before_use_node_(before_use_node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001692 DCHECK(instruction_ != nullptr);
David Brazdil1abb4192015-02-17 18:33:36 +00001693 }
1694
1695 HInstruction* GetInstruction() const { return instruction_; }
Vladimir Marko46817b82016-03-29 12:21:58 +01001696 typename HUseList<T>::iterator GetBeforeUseNode() const { return before_use_node_; }
1697 typename HUseList<T>::iterator GetUseNode() const { return ++GetBeforeUseNode(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001698
1699 private:
1700 // Instruction used by the user.
1701 HInstruction* instruction_;
1702
Vladimir Marko46817b82016-03-29 12:21:58 +01001703 // Iterator before the corresponding entry in the use list kept by 'instruction_'.
1704 typename HUseList<T>::iterator before_use_node_;
David Brazdil1abb4192015-02-17 18:33:36 +00001705};
1706
Vladimir Markoe9004912016-06-16 16:50:52 +01001707// Helper class that extracts the input instruction from HUserRecord<HInstruction*>.
1708// This is used for HInstruction::GetInputs() to return a container wrapper providing
1709// HInstruction* values even though the underlying container has HUserRecord<>s.
1710struct HInputExtractor {
1711 HInstruction* operator()(HUserRecord<HInstruction*>& record) const {
1712 return record.GetInstruction();
1713 }
1714 const HInstruction* operator()(const HUserRecord<HInstruction*>& record) const {
1715 return record.GetInstruction();
1716 }
1717};
1718
1719using HInputsRef = TransformArrayRef<HUserRecord<HInstruction*>, HInputExtractor>;
1720using HConstInputsRef = TransformArrayRef<const HUserRecord<HInstruction*>, HInputExtractor>;
1721
Aart Bik854a02b2015-07-14 16:07:00 -07001722/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001723 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001724 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001725 * For write/read dependences on fields/arrays, the dependence analysis uses
1726 * type disambiguation (e.g. a float field write cannot modify the value of an
1727 * integer field read) and the access type (e.g. a reference array write cannot
1728 * modify the value of a reference field read [although it may modify the
1729 * reference fetch prior to reading the field, which is represented by its own
1730 * write/read dependence]). The analysis makes conservative points-to
1731 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1732 * the same, and any reference read depends on any reference read without
1733 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001734 *
Artem Serovd1aa7d02018-06-22 11:35:46 +01001735 * kDependsOnGCBit is defined in the following way: instructions with kDependsOnGCBit must not be
1736 * alive across the point where garbage collection might happen.
1737 *
1738 * Note: Instructions with kCanTriggerGCBit do not depend on each other.
1739 *
1740 * kCanTriggerGCBit must be used for instructions for which GC might happen on the path across
1741 * those instructions from the compiler perspective (between this instruction and the next one
1742 * in the IR).
1743 *
1744 * Note: Instructions which can cause GC only on a fatal slow path do not need
1745 * kCanTriggerGCBit as the execution never returns to the instruction next to the exceptional
1746 * one. However the execution may return to compiled code if there is a catch block in the
1747 * current method; for this purpose the TryBoundary exit instruction has kCanTriggerGCBit
1748 * set.
1749 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001750 * The internal representation uses 38-bit and is described in the table below.
1751 * The first line indicates the side effect, and for field/array accesses the
1752 * second line indicates the type of the access (in the order of the
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001753 * DataType::Type enum).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001754 * The two numbered lines below indicate the bit position in the bitfield (read
1755 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001756 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001757 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1758 * +-------------+---------+---------+--------------+---------+---------+
1759 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1760 * | 3 |333333322|222222221| 1 |111111110|000000000|
1761 * | 7 |654321098|765432109| 8 |765432109|876543210|
1762 *
1763 * Note that, to ease the implementation, 'changes' bits are least significant
1764 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001765 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001766class SideEffects : public ValueObject {
1767 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001768 SideEffects() : flags_(0) {}
1769
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001770 static SideEffects None() {
1771 return SideEffects(0);
1772 }
1773
1774 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001775 return SideEffects(kAllChangeBits | kAllDependOnBits);
1776 }
1777
1778 static SideEffects AllChanges() {
1779 return SideEffects(kAllChangeBits);
1780 }
1781
1782 static SideEffects AllDependencies() {
1783 return SideEffects(kAllDependOnBits);
1784 }
1785
1786 static SideEffects AllExceptGCDependency() {
1787 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1788 }
1789
1790 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001791 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001792 }
1793
Aart Bik34c3ba92015-07-20 14:08:59 -07001794 static SideEffects AllWrites() {
1795 return SideEffects(kAllWrites);
1796 }
1797
1798 static SideEffects AllReads() {
1799 return SideEffects(kAllReads);
1800 }
1801
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001802 static SideEffects FieldWriteOfType(DataType::Type type, bool is_volatile) {
Aart Bik34c3ba92015-07-20 14:08:59 -07001803 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001804 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001805 : SideEffects(TypeFlag(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001806 }
1807
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001808 static SideEffects ArrayWriteOfType(DataType::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001809 return SideEffects(TypeFlag(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001810 }
1811
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001812 static SideEffects FieldReadOfType(DataType::Type type, bool is_volatile) {
Aart Bik34c3ba92015-07-20 14:08:59 -07001813 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001814 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001815 : SideEffects(TypeFlag(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001816 }
1817
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001818 static SideEffects ArrayReadOfType(DataType::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001819 return SideEffects(TypeFlag(type, kArrayReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001820 }
1821
Artem Serovd1aa7d02018-06-22 11:35:46 +01001822 // Returns whether GC might happen across this instruction from the compiler perspective so
1823 // the next instruction in the IR would see that.
1824 //
1825 // See the SideEffect class comments.
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001826 static SideEffects CanTriggerGC() {
1827 return SideEffects(1ULL << kCanTriggerGCBit);
1828 }
1829
Artem Serovd1aa7d02018-06-22 11:35:46 +01001830 // Returns whether the instruction must not be alive across a GC point.
1831 //
1832 // See the SideEffect class comments.
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001833 static SideEffects DependsOnGC() {
1834 return SideEffects(1ULL << kDependsOnGCBit);
1835 }
1836
Aart Bik854a02b2015-07-14 16:07:00 -07001837 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001838 SideEffects Union(SideEffects other) const {
1839 return SideEffects(flags_ | other.flags_);
1840 }
1841
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001842 SideEffects Exclusion(SideEffects other) const {
1843 return SideEffects(flags_ & ~other.flags_);
1844 }
1845
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001846 void Add(SideEffects other) {
1847 flags_ |= other.flags_;
1848 }
1849
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001850 bool Includes(SideEffects other) const {
1851 return (other.flags_ & flags_) == other.flags_;
1852 }
1853
1854 bool HasSideEffects() const {
1855 return (flags_ & kAllChangeBits);
1856 }
1857
1858 bool HasDependencies() const {
1859 return (flags_ & kAllDependOnBits);
1860 }
1861
1862 // Returns true if there are no side effects or dependencies.
1863 bool DoesNothing() const {
1864 return flags_ == 0;
1865 }
1866
Aart Bik854a02b2015-07-14 16:07:00 -07001867 // Returns true if something is written.
1868 bool DoesAnyWrite() const {
1869 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001870 }
1871
Aart Bik854a02b2015-07-14 16:07:00 -07001872 // Returns true if something is read.
1873 bool DoesAnyRead() const {
1874 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001875 }
1876
Aart Bik854a02b2015-07-14 16:07:00 -07001877 // Returns true if potentially everything is written and read
1878 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001879 bool DoesAllReadWrite() const {
1880 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1881 }
1882
Aart Bik854a02b2015-07-14 16:07:00 -07001883 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001884 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001885 }
1886
Roland Levillain0d5a2812015-11-13 10:07:31 +00001887 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001888 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001889 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1890 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001891 }
1892
1893 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001894 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001895 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001896 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001897 for (int s = kLastBit; s >= 0; s--) {
1898 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1899 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1900 // This is a bit for the GC side effect.
1901 if (current_bit_is_set) {
1902 flags += "GC";
1903 }
Aart Bik854a02b2015-07-14 16:07:00 -07001904 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001905 } else {
1906 // This is a bit for the array/field analysis.
1907 // The underscore character stands for the 'can trigger GC' bit.
1908 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1909 if (current_bit_is_set) {
1910 flags += kDebug[s];
1911 }
1912 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1913 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1914 flags += "|";
1915 }
1916 }
Aart Bik854a02b2015-07-14 16:07:00 -07001917 }
1918 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001919 }
1920
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001921 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001922
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001923 private:
1924 static constexpr int kFieldArrayAnalysisBits = 9;
1925
1926 static constexpr int kFieldWriteOffset = 0;
1927 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1928 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1929 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1930
1931 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1932
1933 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1934 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1935 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1936 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1937
1938 static constexpr int kLastBit = kDependsOnGCBit;
1939 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1940
1941 // Aliases.
1942
1943 static_assert(kChangeBits == kDependOnBits,
1944 "the 'change' bits should match the 'depend on' bits.");
1945
1946 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1947 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1948 static constexpr uint64_t kAllWrites =
1949 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1950 static constexpr uint64_t kAllReads =
1951 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001952
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001953 // Translates type to bit flag. The type must correspond to a Java type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001954 static uint64_t TypeFlag(DataType::Type type, int offset) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001955 int shift;
1956 switch (type) {
1957 case DataType::Type::kReference: shift = 0; break;
1958 case DataType::Type::kBool: shift = 1; break;
1959 case DataType::Type::kInt8: shift = 2; break;
1960 case DataType::Type::kUint16: shift = 3; break;
1961 case DataType::Type::kInt16: shift = 4; break;
1962 case DataType::Type::kInt32: shift = 5; break;
1963 case DataType::Type::kInt64: shift = 6; break;
1964 case DataType::Type::kFloat32: shift = 7; break;
1965 case DataType::Type::kFloat64: shift = 8; break;
1966 default:
1967 LOG(FATAL) << "Unexpected data type " << type;
1968 UNREACHABLE();
1969 }
Aart Bik854a02b2015-07-14 16:07:00 -07001970 DCHECK_LE(kFieldWriteOffset, shift);
1971 DCHECK_LT(shift, kArrayWriteOffset);
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001972 return UINT64_C(1) << (shift + offset);
Aart Bik854a02b2015-07-14 16:07:00 -07001973 }
1974
1975 // Private constructor on direct flags value.
1976 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1977
1978 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001979};
1980
David Brazdiled596192015-01-23 10:39:45 +00001981// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001982class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001983 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001984 ALWAYS_INLINE HEnvironment(ArenaAllocator* allocator,
Mingyao Yang01b47b02017-02-03 12:09:57 -08001985 size_t number_of_vregs,
1986 ArtMethod* method,
1987 uint32_t dex_pc,
1988 HInstruction* holder)
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001989 : vregs_(number_of_vregs, allocator->Adapter(kArenaAllocEnvironmentVRegs)),
1990 locations_(allocator->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001991 parent_(nullptr),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001992 method_(method),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001993 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001994 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001995 }
1996
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001997 ALWAYS_INLINE HEnvironment(ArenaAllocator* allocator,
1998 const HEnvironment& to_copy,
1999 HInstruction* holder)
2000 : HEnvironment(allocator,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002001 to_copy.Size(),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00002002 to_copy.GetMethod(),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002003 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002004 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002005
Vladimir Markoec32f642017-06-02 10:51:55 +01002006 void AllocateLocations() {
2007 DCHECK(locations_.empty());
2008 locations_.resize(vregs_.size());
2009 }
2010
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002011 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002012 if (parent_ != nullptr) {
2013 parent_->SetAndCopyParentChain(allocator, parent);
2014 } else {
2015 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
2016 parent_->CopyFrom(parent);
2017 if (parent->GetParent() != nullptr) {
2018 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
2019 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002020 }
David Brazdiled596192015-01-23 10:39:45 +00002021 }
2022
Vladimir Marko69d310e2017-10-09 14:12:23 +01002023 void CopyFrom(ArrayRef<HInstruction* const> locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01002024 void CopyFrom(HEnvironment* environment);
2025
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002026 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
2027 // input to the loop phi instead. This is for inserting instructions that
2028 // require an environment (like HDeoptimization) in the loop pre-header.
2029 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00002030
2031 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01002032 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00002033 }
2034
2035 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01002036 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00002037 }
2038
David Brazdil1abb4192015-02-17 18:33:36 +00002039 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00002040
Artem Serovca210e32017-12-15 13:43:20 +00002041 // Replaces the input at the position 'index' with the replacement; the replacement and old
2042 // input instructions' env_uses_ lists are adjusted. The function works similar to
2043 // HInstruction::ReplaceInput.
2044 void ReplaceInput(HInstruction* replacement, size_t index);
2045
Vladimir Markofa6b93c2015-09-15 10:15:55 +01002046 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00002047
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002048 HEnvironment* GetParent() const { return parent_; }
2049
2050 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01002051 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002052 }
2053
2054 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01002055 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002056 }
2057
2058 uint32_t GetDexPc() const {
2059 return dex_pc_;
2060 }
2061
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00002062 ArtMethod* GetMethod() const {
2063 return method_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002064 }
2065
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002066 HInstruction* GetHolder() const {
2067 return holder_;
2068 }
2069
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00002070
2071 bool IsFromInlinedInvoke() const {
2072 return GetParent() != nullptr;
2073 }
2074
David Brazdiled596192015-01-23 10:39:45 +00002075 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01002076 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
2077 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002078 HEnvironment* parent_;
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00002079 ArtMethod* method_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002080 const uint32_t dex_pc_;
David Brazdiled596192015-01-23 10:39:45 +00002081
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01002082 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002083 HInstruction* const holder_;
2084
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01002085 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00002086
2087 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
2088};
2089
Vladimir Markof9f64412015-09-02 14:05:49 +01002090class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002091 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302092#define DECLARE_KIND(type, super) k##type,
Vladimir Marko9974e3c2020-06-10 16:27:06 +01002093 enum InstructionKind { // private marker to avoid generate-operator-out.py from processing.
Vladimir Markoe3946222018-05-04 14:18:47 +01002094 FOR_EACH_CONCRETE_INSTRUCTION(DECLARE_KIND)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302095 kLastInstructionKind
2096 };
2097#undef DECLARE_KIND
2098
2099 HInstruction(InstructionKind kind, SideEffects side_effects, uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01002100 : HInstruction(kind, DataType::Type::kVoid, side_effects, dex_pc) {}
2101
2102 HInstruction(InstructionKind kind, DataType::Type type, SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002103 : previous_(nullptr),
2104 next_(nullptr),
2105 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002106 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002107 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002108 ssa_index_(-1),
Vladimir Markoa1de9182016-02-25 11:37:38 +00002109 packed_fields_(0u),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002110 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002111 locations_(nullptr),
2112 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002113 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00002114 side_effects_(side_effects),
Vladimir Markoa1de9182016-02-25 11:37:38 +00002115 reference_type_handle_(ReferenceTypeInfo::CreateInvalid().GetTypeHandle()) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302116 SetPackedField<InstructionKindField>(kind);
Vladimir Markobd785672018-05-03 17:09:09 +01002117 SetPackedField<TypeField>(type);
Vladimir Markoa1de9182016-02-25 11:37:38 +00002118 SetPackedFlag<kFlagReferenceTypeIsExact>(ReferenceTypeInfo::CreateInvalid().IsExact());
2119 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002120
Dave Allison20dfc792014-06-16 20:44:29 -07002121 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002122
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002123
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002124 HInstruction* GetNext() const { return next_; }
2125 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002126
Calin Juravle77520bc2015-01-12 18:45:46 +00002127 HInstruction* GetNextDisregardingMoves() const;
2128 HInstruction* GetPreviousDisregardingMoves() const;
2129
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002130 HBasicBlock* GetBlock() const { return block_; }
Vladimir Markoca6fff82017-10-03 14:49:14 +01002131 ArenaAllocator* GetAllocator() const { return block_->GetGraph()->GetAllocator(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002132 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002133 bool IsInBlock() const { return block_ != nullptr; }
2134 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00002135 bool IsLoopHeaderPhi() const { return IsPhi() && block_->IsLoopHeader(); }
2136 bool IsIrreducibleLoopHeaderPhi() const {
2137 return IsLoopHeaderPhi() && GetBlock()->GetLoopInformation()->IsIrreducible();
2138 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002139
Vladimir Marko372f10e2016-05-17 16:30:10 +01002140 virtual ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() = 0;
2141
2142 ArrayRef<const HUserRecord<HInstruction*>> GetInputRecords() const {
2143 // One virtual method is enough, just const_cast<> and then re-add the const.
2144 return ArrayRef<const HUserRecord<HInstruction*>>(
2145 const_cast<HInstruction*>(this)->GetInputRecords());
2146 }
2147
Vladimir Markoe9004912016-06-16 16:50:52 +01002148 HInputsRef GetInputs() {
2149 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01002150 }
2151
Vladimir Markoe9004912016-06-16 16:50:52 +01002152 HConstInputsRef GetInputs() const {
2153 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01002154 }
2155
2156 size_t InputCount() const { return GetInputRecords().size(); }
David Brazdil1abb4192015-02-17 18:33:36 +00002157 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002158
Aart Bik2767f4b2016-10-28 15:03:53 -07002159 bool HasInput(HInstruction* input) const {
2160 for (const HInstruction* i : GetInputs()) {
2161 if (i == input) {
2162 return true;
2163 }
2164 }
2165 return false;
2166 }
2167
Vladimir Marko372f10e2016-05-17 16:30:10 +01002168 void SetRawInputAt(size_t index, HInstruction* input) {
2169 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
2170 }
2171
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002172 virtual void Accept(HGraphVisitor* visitor) = 0;
2173 virtual const char* DebugName() const = 0;
2174
Vladimir Markobd785672018-05-03 17:09:09 +01002175 DataType::Type GetType() const {
2176 return TypeField::Decode(GetPackedFields());
2177 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002178
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002179 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002180
2181 uint32_t GetDexPc() const { return dex_pc_; }
2182
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002183 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01002184
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002185 // Can the instruction throw?
2186 // TODO: We should rename to CanVisiblyThrow, as some instructions (like HNewInstance),
2187 // could throw OOME, but it is still OK to remove them if they are unused.
Roland Levillaine161a2a2014-10-03 12:45:18 +01002188 virtual bool CanThrow() const { return false; }
Aart Bika8b8e9b2018-01-09 11:01:02 -08002189
2190 // Does the instruction always throw an exception unconditionally?
2191 virtual bool AlwaysThrows() const { return false; }
2192
David Brazdilec16f792015-08-19 15:04:01 +01002193 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07002194
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002195 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07002196 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002197
Calin Juravle10e244f2015-01-26 18:54:32 +00002198 // Does not apply for all instructions, but having this at top level greatly
2199 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00002200 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002201 virtual bool CanBeNull() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002202 DCHECK_EQ(GetType(), DataType::Type::kReference) << "CanBeNull only applies to reference types";
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002203 return true;
2204 }
Calin Juravle10e244f2015-01-26 18:54:32 +00002205
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002206 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01002207 return false;
2208 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002209
Nicolas Geoffray61ba8d22018-08-07 09:55:57 +01002210 // If this instruction will do an implicit null check, return the `HNullCheck` associated
2211 // with it. Otherwise return null.
2212 HNullCheck* GetImplicitNullCheck() const {
Vladimir Markoee1f1262019-06-26 14:37:17 +01002213 // Go over previous non-move instructions that are emitted at use site.
2214 HInstruction* prev_not_move = GetPreviousDisregardingMoves();
2215 while (prev_not_move != nullptr && prev_not_move->IsEmittedAtUseSite()) {
2216 if (prev_not_move->IsNullCheck()) {
2217 return prev_not_move->AsNullCheck();
2218 }
2219 prev_not_move = prev_not_move->GetPreviousDisregardingMoves();
Nicolas Geoffray61ba8d22018-08-07 09:55:57 +01002220 }
2221 return nullptr;
2222 }
2223
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00002224 virtual bool IsActualObject() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002225 return GetType() == DataType::Type::kReference;
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00002226 }
2227
Calin Juravle2e768302015-07-28 14:41:11 +00002228 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00002229
Calin Juravle61d544b2015-02-23 16:46:57 +00002230 ReferenceTypeInfo GetReferenceTypeInfo() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002231 DCHECK_EQ(GetType(), DataType::Type::kReference);
Vladimir Markoa1de9182016-02-25 11:37:38 +00002232 return ReferenceTypeInfo::CreateUnchecked(reference_type_handle_,
Vladimir Marko456307a2016-04-19 14:12:13 +00002233 GetPackedFlag<kFlagReferenceTypeIsExact>());
Calin Juravle61d544b2015-02-23 16:46:57 +00002234 }
Calin Juravleacf735c2015-02-12 15:25:22 +00002235
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002236 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00002237 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01002238 // Note: fixup_end remains valid across push_front().
2239 auto fixup_end = uses_.empty() ? uses_.begin() : ++uses_.begin();
2240 HUseListNode<HInstruction*>* new_node =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002241 new (GetBlock()->GetGraph()->GetAllocator()) HUseListNode<HInstruction*>(user, index);
Vladimir Marko46817b82016-03-29 12:21:58 +01002242 uses_.push_front(*new_node);
2243 FixUpUserRecordsAfterUseInsertion(fixup_end);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002244 }
2245
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002246 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01002247 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01002248 // Note: env_fixup_end remains valid across push_front().
2249 auto env_fixup_end = env_uses_.empty() ? env_uses_.begin() : ++env_uses_.begin();
2250 HUseListNode<HEnvironment*>* new_node =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002251 new (GetBlock()->GetGraph()->GetAllocator()) HUseListNode<HEnvironment*>(user, index);
Vladimir Marko46817b82016-03-29 12:21:58 +01002252 env_uses_.push_front(*new_node);
2253 FixUpUserRecordsAfterEnvUseInsertion(env_fixup_end);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002254 }
2255
David Brazdil1abb4192015-02-17 18:33:36 +00002256 void RemoveAsUserOfInput(size_t input) {
2257 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
Vladimir Marko46817b82016-03-29 12:21:58 +01002258 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
2259 input_use.GetInstruction()->uses_.erase_after(before_use_node);
2260 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
David Brazdil1abb4192015-02-17 18:33:36 +00002261 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002262
Vladimir Marko372f10e2016-05-17 16:30:10 +01002263 void RemoveAsUserOfAllInputs() {
2264 for (const HUserRecord<HInstruction*>& input_use : GetInputRecords()) {
2265 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
2266 input_use.GetInstruction()->uses_.erase_after(before_use_node);
2267 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
2268 }
2269 }
2270
David Brazdil1abb4192015-02-17 18:33:36 +00002271 const HUseList<HInstruction*>& GetUses() const { return uses_; }
2272 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002273
Vladimir Marko46817b82016-03-29 12:21:58 +01002274 bool HasUses() const { return !uses_.empty() || !env_uses_.empty(); }
2275 bool HasEnvironmentUses() const { return !env_uses_.empty(); }
2276 bool HasNonEnvironmentUses() const { return !uses_.empty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01002277 bool HasOnlyOneNonEnvironmentUse() const {
Vladimir Marko46817b82016-03-29 12:21:58 +01002278 return !HasEnvironmentUses() && GetUses().HasExactlyOneElement();
Alexandre Rames188d4312015-04-09 18:30:21 +01002279 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002280
Aart Bikcc42be02016-10-20 16:14:16 -07002281 bool IsRemovable() const {
Aart Bik482095d2016-10-10 15:39:10 -07002282 return
Aart Bikff7d89c2016-11-07 08:49:28 -08002283 !DoesAnyWrite() &&
Aart Bik482095d2016-10-10 15:39:10 -07002284 !CanThrow() &&
2285 !IsSuspendCheck() &&
2286 !IsControlFlow() &&
2287 !IsNativeDebugInfo() &&
2288 !IsParameterValue() &&
Aart Bik482095d2016-10-10 15:39:10 -07002289 // If we added an explicit barrier then we should keep it.
Igor Murashkind01745e2017-04-05 16:40:31 -07002290 !IsMemoryBarrier() &&
2291 !IsConstructorFence();
Aart Bik482095d2016-10-10 15:39:10 -07002292 }
2293
Aart Bikcc42be02016-10-20 16:14:16 -07002294 bool IsDeadAndRemovable() const {
2295 return IsRemovable() && !HasUses();
2296 }
2297
Roland Levillain6c82d402014-10-13 16:10:27 +01002298 // Does this instruction strictly dominate `other_instruction`?
2299 // Returns false if this instruction and `other_instruction` are the same.
2300 // Aborts if this instruction and `other_instruction` are both phis.
2301 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01002302
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002303 int GetId() const { return id_; }
2304 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002305
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002306 int GetSsaIndex() const { return ssa_index_; }
2307 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
2308 bool HasSsaIndex() const { return ssa_index_ != -1; }
2309
2310 bool HasEnvironment() const { return environment_ != nullptr; }
2311 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002312 // Set the `environment_` field. Raw because this method does not
2313 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002314 void SetRawEnvironment(HEnvironment* environment) {
2315 DCHECK(environment_ == nullptr);
2316 DCHECK_EQ(environment->GetHolder(), this);
2317 environment_ = environment;
2318 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002319
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002320 void InsertRawEnvironment(HEnvironment* environment) {
2321 DCHECK(environment_ != nullptr);
2322 DCHECK_EQ(environment->GetHolder(), this);
2323 DCHECK(environment->GetParent() == nullptr);
2324 environment->parent_ = environment_;
2325 environment_ = environment;
2326 }
2327
Vladimir Markocac5a7e2016-02-22 10:39:50 +00002328 void RemoveEnvironment();
2329
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002330 // Set the environment of this instruction, copying it from `environment`. While
2331 // copying, the uses lists are being updated.
2332 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002333 DCHECK(environment_ == nullptr);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002334 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetAllocator();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002335 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002336 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002337 if (environment->GetParent() != nullptr) {
2338 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2339 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002340 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002341
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002342 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
2343 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002344 DCHECK(environment_ == nullptr);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002345 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetAllocator();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002346 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002347 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002348 if (environment->GetParent() != nullptr) {
2349 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2350 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002351 }
2352
Nicolas Geoffray39468442014-09-02 15:17:15 +01002353 // Returns the number of entries in the environment. Typically, that is the
2354 // number of dex registers in a method. It could be more in case of inlining.
2355 size_t EnvironmentSize() const;
2356
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002357 LocationSummary* GetLocations() const { return locations_; }
2358 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002359
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002360 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00002361 void ReplaceUsesDominatedBy(HInstruction* dominator, HInstruction* replacement);
Nicolas Geoffray8a62a4c2018-07-03 09:39:07 +01002362 void ReplaceEnvUsesDominatedBy(HInstruction* dominator, HInstruction* replacement);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002363 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002364
Alexandre Rames188d4312015-04-09 18:30:21 +01002365 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
2366 // uses of this instruction by `other` are *not* updated.
2367 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
2368 ReplaceWith(other);
2369 other->ReplaceInput(this, use_index);
2370 }
2371
Alexandre Rames22aa54b2016-10-18 09:32:29 +01002372 // Move `this` instruction before `cursor`
2373 void MoveBefore(HInstruction* cursor, bool do_checks = true);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002374
Vladimir Markofb337ea2015-11-25 15:25:10 +00002375 // Move `this` before its first user and out of any loops. If there is no
2376 // out-of-loop user that dominates all other users, move the instruction
2377 // to the end of the out-of-loop common dominator of the user's blocks.
2378 //
2379 // This can be used only on non-throwing instructions with no side effects that
2380 // have at least one use but no environment uses.
2381 void MoveBeforeFirstUserAndOutOfLoops();
2382
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002383#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Markoa90dd512018-05-04 15:04:45 +01002384 bool Is##type() const;
2385
2386 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
2387#undef INSTRUCTION_TYPE_CHECK
2388
2389#define INSTRUCTION_TYPE_CAST(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002390 const H##type* As##type() const; \
2391 H##type* As##type();
2392
Vladimir Markoa90dd512018-05-04 15:04:45 +01002393 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CAST)
2394#undef INSTRUCTION_TYPE_CAST
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002395
Artem Serovcced8ba2017-07-19 18:18:09 +01002396 // Return a clone of the instruction if it is clonable (shallow copy by default, custom copy
2397 // if a custom copy-constructor is provided for a particular type). If IsClonable() is false for
2398 // the instruction then the behaviour of this function is undefined.
2399 //
2400 // Note: It is semantically valid to create a clone of the instruction only until
2401 // prepare_for_register_allocator phase as lifetime, intervals and codegen info are not
2402 // copied.
2403 //
2404 // Note: HEnvironment and some other fields are not copied and are set to default values, see
2405 // 'explicit HInstruction(const HInstruction& other)' for details.
2406 virtual HInstruction* Clone(ArenaAllocator* arena ATTRIBUTE_UNUSED) const {
2407 LOG(FATAL) << "Cloning is not implemented for the instruction " <<
2408 DebugName() << " " << GetId();
2409 UNREACHABLE();
2410 }
2411
2412 // Return whether instruction can be cloned (copied).
2413 virtual bool IsClonable() const { return false; }
2414
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002415 // Returns whether the instruction can be moved within the graph.
Aart Bik71bf7b42016-11-16 10:17:46 -08002416 // TODO: this method is used by LICM and GVN with possibly different
2417 // meanings? split and rename?
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002418 virtual bool CanBeMoved() const { return false; }
2419
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002420 // Returns whether any data encoded in the two instructions is equal.
2421 // This method does not look at the inputs. Both instructions must be
2422 // of the same type, otherwise the method has undefined behavior.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002423 virtual bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002424 return false;
2425 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002426
2427 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00002428 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002429 // 2) Their inputs are identical.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002430 bool Equals(const HInstruction* other) const;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002431
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302432 InstructionKind GetKind() const { return GetPackedField<InstructionKindField>(); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002433
2434 virtual size_t ComputeHashCode() const {
2435 size_t result = GetKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01002436 for (const HInstruction* input : GetInputs()) {
2437 result = (result * 31) + input->GetId();
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002438 }
2439 return result;
2440 }
2441
2442 SideEffects GetSideEffects() const { return side_effects_; }
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00002443 void SetSideEffects(SideEffects other) { side_effects_ = other; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002444 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002445
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002446 size_t GetLifetimePosition() const { return lifetime_position_; }
2447 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2448 LiveInterval* GetLiveInterval() const { return live_interval_; }
2449 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2450 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2451
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002452 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2453
2454 // Returns whether the code generation of the instruction will require to have access
2455 // to the current method. Such instructions are:
2456 // (1): Instructions that require an environment, as calling the runtime requires
2457 // to walk the stack and have the current method stored at a specific stack address.
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002458 // (2): HCurrentMethod, potentially used by HInvokeStaticOrDirect, HLoadString, or HLoadClass
2459 // to access the dex cache.
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002460 bool NeedsCurrentMethod() const {
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002461 return NeedsEnvironment() || IsCurrentMethod();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002462 }
2463
Vladimir Markodc151b22015-10-15 18:02:30 +01002464 // Returns whether the code generation of the instruction will require to have access
2465 // to the dex cache of the current method's declaring class via the current method.
2466 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002467
Mark Mendellc4701932015-04-10 13:18:51 -04002468 // Does this instruction have any use in an environment before
2469 // control flow hits 'other'?
2470 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2471
2472 // Remove all references to environment uses of this instruction.
2473 // The caller must ensure that this is safe to do.
2474 void RemoveEnvironmentUsers();
2475
Vladimir Markoa1de9182016-02-25 11:37:38 +00002476 bool IsEmittedAtUseSite() const { return GetPackedFlag<kFlagEmittedAtUseSite>(); }
2477 void MarkEmittedAtUseSite() { SetPackedFlag<kFlagEmittedAtUseSite>(true); }
David Brazdilb3e773e2016-01-26 11:28:37 +00002478
David Brazdil1abb4192015-02-17 18:33:36 +00002479 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002480 // If set, the machine code for this instruction is assumed to be generated by
2481 // its users. Used by liveness analysis to compute use positions accordingly.
2482 static constexpr size_t kFlagEmittedAtUseSite = 0u;
2483 static constexpr size_t kFlagReferenceTypeIsExact = kFlagEmittedAtUseSite + 1;
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302484 static constexpr size_t kFieldInstructionKind = kFlagReferenceTypeIsExact + 1;
2485 static constexpr size_t kFieldInstructionKindSize =
2486 MinimumBitsToStore(static_cast<size_t>(InstructionKind::kLastInstructionKind - 1));
Vladimir Markobd785672018-05-03 17:09:09 +01002487 static constexpr size_t kFieldType =
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302488 kFieldInstructionKind + kFieldInstructionKindSize;
Vladimir Markobd785672018-05-03 17:09:09 +01002489 static constexpr size_t kFieldTypeSize =
2490 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
2491 static constexpr size_t kNumberOfGenericPackedBits = kFieldType + kFieldTypeSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00002492 static constexpr size_t kMaxNumberOfPackedBits = sizeof(uint32_t) * kBitsPerByte;
2493
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302494 static_assert(kNumberOfGenericPackedBits <= kMaxNumberOfPackedBits,
2495 "Too many generic packed fields");
2496
Vladimir Markobd785672018-05-03 17:09:09 +01002497 using TypeField = BitField<DataType::Type, kFieldType, kFieldTypeSize>;
2498
Vladimir Marko372f10e2016-05-17 16:30:10 +01002499 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const {
2500 return GetInputRecords()[i];
2501 }
2502
2503 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) {
2504 ArrayRef<HUserRecord<HInstruction*>> input_records = GetInputRecords();
2505 input_records[index] = input;
2506 }
David Brazdil1abb4192015-02-17 18:33:36 +00002507
Vladimir Markoa1de9182016-02-25 11:37:38 +00002508 uint32_t GetPackedFields() const {
2509 return packed_fields_;
2510 }
2511
2512 template <size_t flag>
2513 bool GetPackedFlag() const {
2514 return (packed_fields_ & (1u << flag)) != 0u;
2515 }
2516
2517 template <size_t flag>
2518 void SetPackedFlag(bool value = true) {
2519 packed_fields_ = (packed_fields_ & ~(1u << flag)) | ((value ? 1u : 0u) << flag);
2520 }
2521
2522 template <typename BitFieldType>
2523 typename BitFieldType::value_type GetPackedField() const {
2524 return BitFieldType::Decode(packed_fields_);
2525 }
2526
2527 template <typename BitFieldType>
2528 void SetPackedField(typename BitFieldType::value_type value) {
2529 DCHECK(IsUint<BitFieldType::size>(static_cast<uintptr_t>(value)));
2530 packed_fields_ = BitFieldType::Update(value, packed_fields_);
2531 }
2532
Artem Serovcced8ba2017-07-19 18:18:09 +01002533 // Copy construction for the instruction (used for Clone function).
2534 //
2535 // Fields (e.g. lifetime, intervals and codegen info) associated with phases starting from
2536 // prepare_for_register_allocator are not copied (set to default values).
2537 //
2538 // Copy constructors must be provided for every HInstruction type; default copy constructor is
2539 // fine for most of them. However for some of the instructions a custom copy constructor must be
2540 // specified (when instruction has non-trivially copyable fields and must have a special behaviour
2541 // for copying them).
2542 explicit HInstruction(const HInstruction& other)
2543 : previous_(nullptr),
2544 next_(nullptr),
2545 block_(nullptr),
2546 dex_pc_(other.dex_pc_),
2547 id_(-1),
2548 ssa_index_(-1),
2549 packed_fields_(other.packed_fields_),
2550 environment_(nullptr),
2551 locations_(nullptr),
2552 live_interval_(nullptr),
2553 lifetime_position_(kNoLifetime),
2554 side_effects_(other.side_effects_),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302555 reference_type_handle_(other.reference_type_handle_) {
2556 }
Artem Serovcced8ba2017-07-19 18:18:09 +01002557
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002558 private:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302559 using InstructionKindField =
2560 BitField<InstructionKind, kFieldInstructionKind, kFieldInstructionKindSize>;
2561
Vladimir Marko46817b82016-03-29 12:21:58 +01002562 void FixUpUserRecordsAfterUseInsertion(HUseList<HInstruction*>::iterator fixup_end) {
2563 auto before_use_node = uses_.before_begin();
2564 for (auto use_node = uses_.begin(); use_node != fixup_end; ++use_node) {
2565 HInstruction* user = use_node->GetUser();
2566 size_t input_index = use_node->GetIndex();
2567 user->SetRawInputRecordAt(input_index, HUserRecord<HInstruction*>(this, before_use_node));
2568 before_use_node = use_node;
2569 }
2570 }
2571
2572 void FixUpUserRecordsAfterUseRemoval(HUseList<HInstruction*>::iterator before_use_node) {
2573 auto next = ++HUseList<HInstruction*>::iterator(before_use_node);
2574 if (next != uses_.end()) {
2575 HInstruction* next_user = next->GetUser();
2576 size_t next_index = next->GetIndex();
2577 DCHECK(next_user->InputRecordAt(next_index).GetInstruction() == this);
2578 next_user->SetRawInputRecordAt(next_index, HUserRecord<HInstruction*>(this, before_use_node));
2579 }
2580 }
2581
2582 void FixUpUserRecordsAfterEnvUseInsertion(HUseList<HEnvironment*>::iterator env_fixup_end) {
2583 auto before_env_use_node = env_uses_.before_begin();
2584 for (auto env_use_node = env_uses_.begin(); env_use_node != env_fixup_end; ++env_use_node) {
2585 HEnvironment* user = env_use_node->GetUser();
2586 size_t input_index = env_use_node->GetIndex();
2587 user->vregs_[input_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2588 before_env_use_node = env_use_node;
2589 }
2590 }
2591
2592 void FixUpUserRecordsAfterEnvUseRemoval(HUseList<HEnvironment*>::iterator before_env_use_node) {
2593 auto next = ++HUseList<HEnvironment*>::iterator(before_env_use_node);
2594 if (next != env_uses_.end()) {
2595 HEnvironment* next_user = next->GetUser();
2596 size_t next_index = next->GetIndex();
2597 DCHECK(next_user->vregs_[next_index].GetInstruction() == this);
2598 next_user->vregs_[next_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2599 }
2600 }
David Brazdil1abb4192015-02-17 18:33:36 +00002601
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002602 HInstruction* previous_;
2603 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002604 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002605 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002606
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002607 // An instruction gets an id when it is added to the graph.
2608 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002609 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002610 int id_;
2611
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002612 // When doing liveness analysis, instructions that have uses get an SSA index.
2613 int ssa_index_;
2614
Vladimir Markoa1de9182016-02-25 11:37:38 +00002615 // Packed fields.
2616 uint32_t packed_fields_;
David Brazdilb3e773e2016-01-26 11:28:37 +00002617
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002618 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002619 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002620
2621 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002622 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002623
Nicolas Geoffray39468442014-09-02 15:17:15 +01002624 // The environment associated with this instruction. Not null if the instruction
2625 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002626 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002627
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002628 // Set by the code generator.
2629 LocationSummary* locations_;
2630
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002631 // Set by the liveness analysis.
2632 LiveInterval* live_interval_;
2633
2634 // Set by the liveness analysis, this is the position in a linear
2635 // order of blocks where this instruction's live interval start.
2636 size_t lifetime_position_;
2637
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002638 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002639
Vladimir Markoa1de9182016-02-25 11:37:38 +00002640 // The reference handle part of the reference type info.
2641 // The IsExact() flag is stored in packed fields.
Calin Juravleacf735c2015-02-12 15:25:22 +00002642 // TODO: for primitive types this should be marked as invalid.
Vladimir Markoa1de9182016-02-25 11:37:38 +00002643 ReferenceTypeInfo::TypeHandle reference_type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00002644
David Brazdil1abb4192015-02-17 18:33:36 +00002645 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002646 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002647 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002648 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002649 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002650};
Vladimir Marko9974e3c2020-06-10 16:27:06 +01002651std::ostream& operator<<(std::ostream& os, HInstruction::InstructionKind rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002652
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002653// Iterates over the instructions, while preserving the next instruction
2654// in case the current instruction gets removed from the list by the user
2655// of this iterator.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002656class HInstructionIterator : public ValueObject {
2657 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002658 explicit HInstructionIterator(const HInstructionList& instructions)
2659 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002660 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002661 }
2662
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002663 bool Done() const { return instruction_ == nullptr; }
2664 HInstruction* Current() const { return instruction_; }
2665 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002666 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002667 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002668 }
2669
2670 private:
2671 HInstruction* instruction_;
2672 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002673
2674 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002675};
2676
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002677// Iterates over the instructions without saving the next instruction,
2678// therefore handling changes in the graph potentially made by the user
2679// of this iterator.
2680class HInstructionIteratorHandleChanges : public ValueObject {
2681 public:
2682 explicit HInstructionIteratorHandleChanges(const HInstructionList& instructions)
2683 : instruction_(instructions.first_instruction_) {
2684 }
2685
2686 bool Done() const { return instruction_ == nullptr; }
2687 HInstruction* Current() const { return instruction_; }
2688 void Advance() {
2689 instruction_ = instruction_->GetNext();
2690 }
2691
2692 private:
2693 HInstruction* instruction_;
2694
2695 DISALLOW_COPY_AND_ASSIGN(HInstructionIteratorHandleChanges);
2696};
2697
2698
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002699class HBackwardInstructionIterator : public ValueObject {
2700 public:
2701 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2702 : instruction_(instructions.last_instruction_) {
2703 next_ = Done() ? nullptr : instruction_->GetPrevious();
2704 }
2705
2706 bool Done() const { return instruction_ == nullptr; }
2707 HInstruction* Current() const { return instruction_; }
2708 void Advance() {
2709 instruction_ = next_;
2710 next_ = Done() ? nullptr : instruction_->GetPrevious();
2711 }
2712
2713 private:
2714 HInstruction* instruction_;
2715 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002716
2717 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002718};
2719
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002720class HVariableInputSizeInstruction : public HInstruction {
2721 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08002722 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002723 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() override {
Mingyao Yangb0b051a2016-11-17 09:04:53 -08002724 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
2725 }
2726
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002727 void AddInput(HInstruction* input);
2728 void InsertInputAt(size_t index, HInstruction* input);
2729 void RemoveInputAt(size_t index);
2730
Igor Murashkind01745e2017-04-05 16:40:31 -07002731 // Removes all the inputs.
2732 // Also removes this instructions from each input's use list
2733 // (for non-environment uses only).
2734 void RemoveAllInputs();
2735
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002736 protected:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302737 HVariableInputSizeInstruction(InstructionKind inst_kind,
2738 SideEffects side_effects,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002739 uint32_t dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002740 ArenaAllocator* allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002741 size_t number_of_inputs,
2742 ArenaAllocKind kind)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302743 : HInstruction(inst_kind, side_effects, dex_pc),
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002744 inputs_(number_of_inputs, allocator->Adapter(kind)) {}
Vladimir Markobd785672018-05-03 17:09:09 +01002745 HVariableInputSizeInstruction(InstructionKind inst_kind,
2746 DataType::Type type,
2747 SideEffects side_effects,
2748 uint32_t dex_pc,
2749 ArenaAllocator* allocator,
2750 size_t number_of_inputs,
2751 ArenaAllocKind kind)
2752 : HInstruction(inst_kind, type, side_effects, dex_pc),
2753 inputs_(number_of_inputs, allocator->Adapter(kind)) {}
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002754
Artem Serovcced8ba2017-07-19 18:18:09 +01002755 DEFAULT_COPY_CONSTRUCTOR(VariableInputSizeInstruction);
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002756
Artem Serovcced8ba2017-07-19 18:18:09 +01002757 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002758};
2759
Vladimir Markof9f64412015-09-02 14:05:49 +01002760template<size_t N>
Vladimir Markobd785672018-05-03 17:09:09 +01002761class HExpression : public HInstruction {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002762 public:
Vladimir Markobd785672018-05-03 17:09:09 +01002763 HExpression<N>(InstructionKind kind, SideEffects side_effects, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302764 : HInstruction(kind, side_effects, dex_pc), inputs_() {}
Vladimir Markobd785672018-05-03 17:09:09 +01002765 HExpression<N>(InstructionKind kind,
2766 DataType::Type type,
2767 SideEffects side_effects,
2768 uint32_t dex_pc)
2769 : HInstruction(kind, type, side_effects, dex_pc), inputs_() {}
2770 virtual ~HExpression() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002771
Vladimir Marko372f10e2016-05-17 16:30:10 +01002772 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01002773 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Vladimir Marko372f10e2016-05-17 16:30:10 +01002774 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002775 }
2776
Artem Serovcced8ba2017-07-19 18:18:09 +01002777 protected:
Vladimir Markobd785672018-05-03 17:09:09 +01002778 DEFAULT_COPY_CONSTRUCTOR(Expression<N>);
Artem Serovcced8ba2017-07-19 18:18:09 +01002779
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002780 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002781 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002782
2783 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002784};
2785
Vladimir Markobd785672018-05-03 17:09:09 +01002786// HExpression specialization for N=0.
Vladimir Markof9f64412015-09-02 14:05:49 +01002787template<>
Vladimir Markobd785672018-05-03 17:09:09 +01002788class HExpression<0> : public HInstruction {
Vladimir Markof9f64412015-09-02 14:05:49 +01002789 public:
Vladimir Markobd785672018-05-03 17:09:09 +01002790 using HInstruction::HInstruction;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002791
Vladimir Markobd785672018-05-03 17:09:09 +01002792 virtual ~HExpression() {}
Vladimir Markof9f64412015-09-02 14:05:49 +01002793
Vladimir Marko372f10e2016-05-17 16:30:10 +01002794 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01002795 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Vladimir Marko372f10e2016-05-17 16:30:10 +01002796 return ArrayRef<HUserRecord<HInstruction*>>();
Vladimir Markof9f64412015-09-02 14:05:49 +01002797 }
2798
Artem Serovcced8ba2017-07-19 18:18:09 +01002799 protected:
Vladimir Markobd785672018-05-03 17:09:09 +01002800 DEFAULT_COPY_CONSTRUCTOR(Expression<0>);
Artem Serovcced8ba2017-07-19 18:18:09 +01002801
Vladimir Markof9f64412015-09-02 14:05:49 +01002802 private:
2803 friend class SsaBuilder;
2804};
2805
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002806// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2807// instruction that branches to the exit block.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002808class HReturnVoid final : public HExpression<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002809 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002810 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01002811 : HExpression(kReturnVoid, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302812 }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002813
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002814 bool IsControlFlow() const override { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002815
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002816 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002817
Artem Serovcced8ba2017-07-19 18:18:09 +01002818 protected:
2819 DEFAULT_COPY_CONSTRUCTOR(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002820};
2821
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002822// Represents dex's RETURN opcodes. A HReturn is a control flow
2823// instruction that branches to the exit block.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002824class HReturn final : public HExpression<1> {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002825 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002826 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01002827 : HExpression(kReturn, SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002828 SetRawInputAt(0, value);
2829 }
2830
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002831 bool IsControlFlow() const override { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002832
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002833 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002834
Artem Serovcced8ba2017-07-19 18:18:09 +01002835 protected:
2836 DEFAULT_COPY_CONSTRUCTOR(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002837};
2838
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002839class HPhi final : public HVariableInputSizeInstruction {
David Brazdildee58d62016-04-07 09:54:26 +00002840 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002841 HPhi(ArenaAllocator* allocator,
David Brazdildee58d62016-04-07 09:54:26 +00002842 uint32_t reg_number,
2843 size_t number_of_inputs,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002844 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +00002845 uint32_t dex_pc = kNoDexPc)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002846 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302847 kPhi,
Vladimir Markobd785672018-05-03 17:09:09 +01002848 ToPhiType(type),
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002849 SideEffects::None(),
2850 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002851 allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002852 number_of_inputs,
2853 kArenaAllocPhiInputs),
David Brazdildee58d62016-04-07 09:54:26 +00002854 reg_number_(reg_number) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002855 DCHECK_NE(GetType(), DataType::Type::kVoid);
David Brazdildee58d62016-04-07 09:54:26 +00002856 // Phis are constructed live and marked dead if conflicting or unused.
2857 // Individual steps of SsaBuilder should assume that if a phi has been
2858 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
2859 SetPackedFlag<kFlagIsLive>(true);
2860 SetPackedFlag<kFlagCanBeNull>(true);
2861 }
2862
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002863 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01002864
David Brazdildee58d62016-04-07 09:54:26 +00002865 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002866 static DataType::Type ToPhiType(DataType::Type type) {
2867 return DataType::Kind(type);
David Brazdildee58d62016-04-07 09:54:26 +00002868 }
2869
2870 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
2871
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002872 void SetType(DataType::Type new_type) {
David Brazdildee58d62016-04-07 09:54:26 +00002873 // Make sure that only valid type changes occur. The following are allowed:
2874 // (1) int -> float/ref (primitive type propagation),
2875 // (2) long -> double (primitive type propagation).
2876 DCHECK(GetType() == new_type ||
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002877 (GetType() == DataType::Type::kInt32 && new_type == DataType::Type::kFloat32) ||
2878 (GetType() == DataType::Type::kInt32 && new_type == DataType::Type::kReference) ||
2879 (GetType() == DataType::Type::kInt64 && new_type == DataType::Type::kFloat64));
David Brazdildee58d62016-04-07 09:54:26 +00002880 SetPackedField<TypeField>(new_type);
2881 }
2882
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002883 bool CanBeNull() const override { return GetPackedFlag<kFlagCanBeNull>(); }
David Brazdildee58d62016-04-07 09:54:26 +00002884 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
2885
2886 uint32_t GetRegNumber() const { return reg_number_; }
2887
2888 void SetDead() { SetPackedFlag<kFlagIsLive>(false); }
2889 void SetLive() { SetPackedFlag<kFlagIsLive>(true); }
2890 bool IsDead() const { return !IsLive(); }
2891 bool IsLive() const { return GetPackedFlag<kFlagIsLive>(); }
2892
Vladimir Markoe9004912016-06-16 16:50:52 +01002893 bool IsVRegEquivalentOf(const HInstruction* other) const {
David Brazdildee58d62016-04-07 09:54:26 +00002894 return other != nullptr
2895 && other->IsPhi()
2896 && other->AsPhi()->GetBlock() == GetBlock()
2897 && other->AsPhi()->GetRegNumber() == GetRegNumber();
2898 }
2899
Nicolas Geoffrayf57c1ae2017-06-28 17:40:18 +01002900 bool HasEquivalentPhi() const {
2901 if (GetPrevious() != nullptr && GetPrevious()->AsPhi()->GetRegNumber() == GetRegNumber()) {
2902 return true;
2903 }
2904 if (GetNext() != nullptr && GetNext()->AsPhi()->GetRegNumber() == GetRegNumber()) {
2905 return true;
2906 }
2907 return false;
2908 }
2909
David Brazdildee58d62016-04-07 09:54:26 +00002910 // Returns the next equivalent phi (starting from the current one) or null if there is none.
2911 // An equivalent phi is a phi having the same dex register and type.
2912 // It assumes that phis with the same dex register are adjacent.
2913 HPhi* GetNextEquivalentPhiWithSameType() {
2914 HInstruction* next = GetNext();
2915 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
2916 if (next->GetType() == GetType()) {
2917 return next->AsPhi();
2918 }
2919 next = next->GetNext();
2920 }
2921 return nullptr;
2922 }
2923
2924 DECLARE_INSTRUCTION(Phi);
2925
Artem Serovcced8ba2017-07-19 18:18:09 +01002926 protected:
2927 DEFAULT_COPY_CONSTRUCTOR(Phi);
2928
David Brazdildee58d62016-04-07 09:54:26 +00002929 private:
Vladimir Markobd785672018-05-03 17:09:09 +01002930 static constexpr size_t kFlagIsLive = HInstruction::kNumberOfGenericPackedBits;
David Brazdildee58d62016-04-07 09:54:26 +00002931 static constexpr size_t kFlagCanBeNull = kFlagIsLive + 1;
2932 static constexpr size_t kNumberOfPhiPackedBits = kFlagCanBeNull + 1;
2933 static_assert(kNumberOfPhiPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
David Brazdildee58d62016-04-07 09:54:26 +00002934
David Brazdildee58d62016-04-07 09:54:26 +00002935 const uint32_t reg_number_;
David Brazdildee58d62016-04-07 09:54:26 +00002936};
2937
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002938// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002939// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002940// exit block.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002941class HExit final : public HExpression<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002942 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302943 explicit HExit(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01002944 : HExpression(kExit, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302945 }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002946
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002947 bool IsControlFlow() const override { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002948
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002949 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002950
Artem Serovcced8ba2017-07-19 18:18:09 +01002951 protected:
2952 DEFAULT_COPY_CONSTRUCTOR(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002953};
2954
2955// Jumps from one block to another.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002956class HGoto final : public HExpression<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002957 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302958 explicit HGoto(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01002959 : HExpression(kGoto, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302960 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002961
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002962 bool IsClonable() const override { return true; }
2963 bool IsControlFlow() const override { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002964
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002965 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002966 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002967 }
2968
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002969 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002970
Artem Serovcced8ba2017-07-19 18:18:09 +01002971 protected:
2972 DEFAULT_COPY_CONSTRUCTOR(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002973};
2974
Roland Levillain9867bc72015-08-05 10:21:34 +01002975class HConstant : public HExpression<0> {
2976 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302977 explicit HConstant(InstructionKind kind, DataType::Type type, uint32_t dex_pc = kNoDexPc)
2978 : HExpression(kind, type, SideEffects::None(), dex_pc) {
2979 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002980
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002981 bool CanBeMoved() const override { return true; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002982
Roland Levillain1a653882016-03-18 18:05:57 +00002983 // Is this constant -1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002984 virtual bool IsMinusOne() const { return false; }
Roland Levillain1a653882016-03-18 18:05:57 +00002985 // Is this constant 0 in the arithmetic sense?
2986 virtual bool IsArithmeticZero() const { return false; }
2987 // Is this constant a 0-bit pattern?
2988 virtual bool IsZeroBitPattern() const { return false; }
2989 // Is this constant 1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002990 virtual bool IsOne() const { return false; }
2991
David Brazdil77a48ae2015-09-15 12:34:04 +00002992 virtual uint64_t GetValueAsUint64() const = 0;
2993
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002994 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002995
Artem Serovcced8ba2017-07-19 18:18:09 +01002996 protected:
2997 DEFAULT_COPY_CONSTRUCTOR(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002998};
2999
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003000class HNullConstant final : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01003001 public:
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003002 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01003003 return true;
3004 }
3005
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003006 uint64_t GetValueAsUint64() const override { return 0; }
David Brazdil77a48ae2015-09-15 12:34:04 +00003007
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003008 size_t ComputeHashCode() const override { return 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01003009
Roland Levillain1a653882016-03-18 18:05:57 +00003010 // The null constant representation is a 0-bit pattern.
Yi Kong39402542019-03-24 02:47:16 -07003011 bool IsZeroBitPattern() const override { return true; }
Roland Levillain1a653882016-03-18 18:05:57 +00003012
Roland Levillain9867bc72015-08-05 10:21:34 +01003013 DECLARE_INSTRUCTION(NullConstant);
3014
Artem Serovcced8ba2017-07-19 18:18:09 +01003015 protected:
3016 DEFAULT_COPY_CONSTRUCTOR(NullConstant);
3017
Roland Levillain9867bc72015-08-05 10:21:34 +01003018 private:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003019 explicit HNullConstant(uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303020 : HConstant(kNullConstant, DataType::Type::kReference, dex_pc) {
3021 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003022
3023 friend class HGraph;
Roland Levillain9867bc72015-08-05 10:21:34 +01003024};
3025
3026// Constants of the type int. Those can be from Dex instructions, or
3027// synthesized (for example with the if-eqz instruction).
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003028class HIntConstant final : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01003029 public:
3030 int32_t GetValue() const { return value_; }
3031
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003032 uint64_t GetValueAsUint64() const override {
David Brazdil9f389d42015-10-01 14:32:56 +01003033 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
3034 }
David Brazdil77a48ae2015-09-15 12:34:04 +00003035
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003036 bool InstructionDataEquals(const HInstruction* other) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003037 DCHECK(other->IsIntConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01003038 return other->AsIntConstant()->value_ == value_;
3039 }
3040
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003041 size_t ComputeHashCode() const override { return GetValue(); }
Roland Levillain9867bc72015-08-05 10:21:34 +01003042
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003043 bool IsMinusOne() const override { return GetValue() == -1; }
3044 bool IsArithmeticZero() const override { return GetValue() == 0; }
3045 bool IsZeroBitPattern() const override { return GetValue() == 0; }
3046 bool IsOne() const override { return GetValue() == 1; }
Roland Levillain9867bc72015-08-05 10:21:34 +01003047
Roland Levillain1a653882016-03-18 18:05:57 +00003048 // Integer constants are used to encode Boolean values as well,
3049 // where 1 means true and 0 means false.
3050 bool IsTrue() const { return GetValue() == 1; }
3051 bool IsFalse() const { return GetValue() == 0; }
3052
Roland Levillain9867bc72015-08-05 10:21:34 +01003053 DECLARE_INSTRUCTION(IntConstant);
3054
Artem Serovcced8ba2017-07-19 18:18:09 +01003055 protected:
3056 DEFAULT_COPY_CONSTRUCTOR(IntConstant);
3057
Roland Levillain9867bc72015-08-05 10:21:34 +01003058 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003059 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303060 : HConstant(kIntConstant, DataType::Type::kInt32, dex_pc), value_(value) {
3061 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003062 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303063 : HConstant(kIntConstant, DataType::Type::kInt32, dex_pc),
3064 value_(value ? 1 : 0) {
3065 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003066
3067 const int32_t value_;
3068
3069 friend class HGraph;
3070 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
3071 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
Roland Levillain9867bc72015-08-05 10:21:34 +01003072};
3073
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003074class HLongConstant final : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01003075 public:
3076 int64_t GetValue() const { return value_; }
3077
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003078 uint64_t GetValueAsUint64() const override { return value_; }
David Brazdil77a48ae2015-09-15 12:34:04 +00003079
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003080 bool InstructionDataEquals(const HInstruction* other) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003081 DCHECK(other->IsLongConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01003082 return other->AsLongConstant()->value_ == value_;
3083 }
3084
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003085 size_t ComputeHashCode() const override { return static_cast<size_t>(GetValue()); }
Roland Levillain9867bc72015-08-05 10:21:34 +01003086
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003087 bool IsMinusOne() const override { return GetValue() == -1; }
3088 bool IsArithmeticZero() const override { return GetValue() == 0; }
3089 bool IsZeroBitPattern() const override { return GetValue() == 0; }
3090 bool IsOne() const override { return GetValue() == 1; }
Roland Levillain9867bc72015-08-05 10:21:34 +01003091
3092 DECLARE_INSTRUCTION(LongConstant);
3093
Artem Serovcced8ba2017-07-19 18:18:09 +01003094 protected:
3095 DEFAULT_COPY_CONSTRUCTOR(LongConstant);
3096
Roland Levillain9867bc72015-08-05 10:21:34 +01003097 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003098 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303099 : HConstant(kLongConstant, DataType::Type::kInt64, dex_pc),
3100 value_(value) {
3101 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003102
3103 const int64_t value_;
3104
3105 friend class HGraph;
Roland Levillain9867bc72015-08-05 10:21:34 +01003106};
Dave Allison20dfc792014-06-16 20:44:29 -07003107
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003108class HFloatConstant final : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003109 public:
3110 float GetValue() const { return value_; }
3111
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003112 uint64_t GetValueAsUint64() const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003113 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
3114 }
3115
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003116 bool InstructionDataEquals(const HInstruction* other) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003117 DCHECK(other->IsFloatConstant()) << other->DebugName();
3118 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
3119 }
3120
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003121 size_t ComputeHashCode() const override { return static_cast<size_t>(GetValue()); }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003122
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003123 bool IsMinusOne() const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003124 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
3125 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003126 bool IsArithmeticZero() const override {
Roland Levillain1a653882016-03-18 18:05:57 +00003127 return std::fpclassify(value_) == FP_ZERO;
3128 }
3129 bool IsArithmeticPositiveZero() const {
3130 return IsArithmeticZero() && !std::signbit(value_);
3131 }
3132 bool IsArithmeticNegativeZero() const {
3133 return IsArithmeticZero() && std::signbit(value_);
3134 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003135 bool IsZeroBitPattern() const override {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00003136 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(0.0f);
Roland Levillain31dd3d62016-02-16 12:21:02 +00003137 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003138 bool IsOne() const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003139 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
3140 }
3141 bool IsNaN() const {
3142 return std::isnan(value_);
3143 }
3144
3145 DECLARE_INSTRUCTION(FloatConstant);
3146
Artem Serovcced8ba2017-07-19 18:18:09 +01003147 protected:
3148 DEFAULT_COPY_CONSTRUCTOR(FloatConstant);
3149
Roland Levillain31dd3d62016-02-16 12:21:02 +00003150 private:
3151 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303152 : HConstant(kFloatConstant, DataType::Type::kFloat32, dex_pc),
3153 value_(value) {
3154 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003155 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303156 : HConstant(kFloatConstant, DataType::Type::kFloat32, dex_pc),
3157 value_(bit_cast<float, int32_t>(value)) {
3158 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003159
3160 const float value_;
3161
3162 // Only the SsaBuilder and HGraph can create floating-point constants.
3163 friend class SsaBuilder;
3164 friend class HGraph;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003165};
3166
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003167class HDoubleConstant final : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003168 public:
3169 double GetValue() const { return value_; }
3170
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003171 uint64_t GetValueAsUint64() const override { return bit_cast<uint64_t, double>(value_); }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003172
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003173 bool InstructionDataEquals(const HInstruction* other) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003174 DCHECK(other->IsDoubleConstant()) << other->DebugName();
3175 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
3176 }
3177
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003178 size_t ComputeHashCode() const override { return static_cast<size_t>(GetValue()); }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003179
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003180 bool IsMinusOne() const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003181 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
3182 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003183 bool IsArithmeticZero() const override {
Roland Levillain1a653882016-03-18 18:05:57 +00003184 return std::fpclassify(value_) == FP_ZERO;
3185 }
3186 bool IsArithmeticPositiveZero() const {
3187 return IsArithmeticZero() && !std::signbit(value_);
3188 }
3189 bool IsArithmeticNegativeZero() const {
3190 return IsArithmeticZero() && std::signbit(value_);
3191 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003192 bool IsZeroBitPattern() const override {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00003193 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((0.0));
Roland Levillain31dd3d62016-02-16 12:21:02 +00003194 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003195 bool IsOne() const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003196 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
3197 }
3198 bool IsNaN() const {
3199 return std::isnan(value_);
3200 }
3201
3202 DECLARE_INSTRUCTION(DoubleConstant);
3203
Artem Serovcced8ba2017-07-19 18:18:09 +01003204 protected:
3205 DEFAULT_COPY_CONSTRUCTOR(DoubleConstant);
3206
Roland Levillain31dd3d62016-02-16 12:21:02 +00003207 private:
3208 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303209 : HConstant(kDoubleConstant, DataType::Type::kFloat64, dex_pc),
3210 value_(value) {
3211 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003212 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303213 : HConstant(kDoubleConstant, DataType::Type::kFloat64, dex_pc),
3214 value_(bit_cast<double, int64_t>(value)) {
3215 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003216
3217 const double value_;
3218
3219 // Only the SsaBuilder and HGraph can create floating-point constants.
3220 friend class SsaBuilder;
3221 friend class HGraph;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003222};
3223
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003224// Conditional branch. A block ending with an HIf instruction must have
3225// two successors.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003226class HIf final : public HExpression<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003227 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003228 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01003229 : HExpression(kIf, SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003230 SetRawInputAt(0, input);
3231 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003232
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003233 bool IsClonable() const override { return true; }
3234 bool IsControlFlow() const override { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003235
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003236 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01003237 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003238 }
3239
3240 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01003241 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003242 }
3243
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003244 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003245
Artem Serovcced8ba2017-07-19 18:18:09 +01003246 protected:
3247 DEFAULT_COPY_CONSTRUCTOR(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003248};
3249
David Brazdilfc6a86a2015-06-26 10:33:45 +00003250
3251// Abstract instruction which marks the beginning and/or end of a try block and
3252// links it to the respective exception handlers. Behaves the same as a Goto in
3253// non-exceptional control flow.
3254// Normal-flow successor is stored at index zero, exception handlers under
3255// higher indices in no particular order.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003256class HTryBoundary final : public HExpression<0> {
David Brazdilfc6a86a2015-06-26 10:33:45 +00003257 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003258 enum class BoundaryKind {
David Brazdil56e1acc2015-06-30 15:41:36 +01003259 kEntry,
3260 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00003261 kLast = kExit
David Brazdil56e1acc2015-06-30 15:41:36 +01003262 };
3263
Artem Serovd1aa7d02018-06-22 11:35:46 +01003264 // SideEffects::CanTriggerGC prevents instructions with SideEffects::DependOnGC to be alive
3265 // across the catch block entering edges as GC might happen during throwing an exception.
3266 // TryBoundary with BoundaryKind::kExit is conservatively used for that as there is no
3267 // HInstruction which a catch block must start from.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003268 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
Artem Serovd1aa7d02018-06-22 11:35:46 +01003269 : HExpression(kTryBoundary,
3270 (kind == BoundaryKind::kExit) ? SideEffects::CanTriggerGC()
3271 : SideEffects::None(),
3272 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003273 SetPackedField<BoundaryKindField>(kind);
3274 }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003275
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003276 bool IsControlFlow() const override { return true; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003277
3278 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01003279 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003280
David Brazdild26a4112015-11-10 11:07:31 +00003281 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
3282 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
3283 }
3284
David Brazdilfc6a86a2015-06-26 10:33:45 +00003285 // Returns whether `handler` is among its exception handlers (non-zero index
3286 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01003287 bool HasExceptionHandler(const HBasicBlock& handler) const {
3288 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00003289 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00003290 }
3291
3292 // If not present already, adds `handler` to its block's list of exception
3293 // handlers.
3294 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01003295 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00003296 GetBlock()->AddSuccessor(handler);
3297 }
3298 }
3299
Vladimir Markoa1de9182016-02-25 11:37:38 +00003300 BoundaryKind GetBoundaryKind() const { return GetPackedField<BoundaryKindField>(); }
3301 bool IsEntry() const { return GetBoundaryKind() == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003302
David Brazdilffee3d32015-07-06 11:48:53 +01003303 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
3304
David Brazdilfc6a86a2015-06-26 10:33:45 +00003305 DECLARE_INSTRUCTION(TryBoundary);
3306
Artem Serovcced8ba2017-07-19 18:18:09 +01003307 protected:
3308 DEFAULT_COPY_CONSTRUCTOR(TryBoundary);
3309
David Brazdilfc6a86a2015-06-26 10:33:45 +00003310 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003311 static constexpr size_t kFieldBoundaryKind = kNumberOfGenericPackedBits;
3312 static constexpr size_t kFieldBoundaryKindSize =
3313 MinimumBitsToStore(static_cast<size_t>(BoundaryKind::kLast));
3314 static constexpr size_t kNumberOfTryBoundaryPackedBits =
3315 kFieldBoundaryKind + kFieldBoundaryKindSize;
3316 static_assert(kNumberOfTryBoundaryPackedBits <= kMaxNumberOfPackedBits,
3317 "Too many packed fields.");
3318 using BoundaryKindField = BitField<BoundaryKind, kFieldBoundaryKind, kFieldBoundaryKindSize>;
David Brazdilfc6a86a2015-06-26 10:33:45 +00003319};
3320
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003321// Deoptimize to interpreter, upon checking a condition.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003322class HDeoptimize final : public HVariableInputSizeInstruction {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003323 public:
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003324 // Use this constructor when the `HDeoptimize` acts as a barrier, where no code can move
3325 // across.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003326 HDeoptimize(ArenaAllocator* allocator,
3327 HInstruction* cond,
3328 DeoptimizationKind kind,
3329 uint32_t dex_pc)
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003330 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303331 kDeoptimize,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003332 SideEffects::All(),
3333 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003334 allocator,
Andreas Gampe3db70682018-12-26 15:12:03 -08003335 /* number_of_inputs= */ 1,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003336 kArenaAllocMisc) {
3337 SetPackedFlag<kFieldCanBeMoved>(false);
3338 SetPackedField<DeoptimizeKindField>(kind);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003339 SetRawInputAt(0, cond);
3340 }
3341
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003342 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01003343
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003344 // Use this constructor when the `HDeoptimize` guards an instruction, and any user
3345 // that relies on the deoptimization to pass should have its input be the `HDeoptimize`
3346 // instead of `guard`.
3347 // We set CanTriggerGC to prevent any intermediate address to be live
3348 // at the point of the `HDeoptimize`.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003349 HDeoptimize(ArenaAllocator* allocator,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003350 HInstruction* cond,
3351 HInstruction* guard,
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003352 DeoptimizationKind kind,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003353 uint32_t dex_pc)
3354 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303355 kDeoptimize,
Vladimir Markobd785672018-05-03 17:09:09 +01003356 guard->GetType(),
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003357 SideEffects::CanTriggerGC(),
3358 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003359 allocator,
Andreas Gampe3db70682018-12-26 15:12:03 -08003360 /* number_of_inputs= */ 2,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003361 kArenaAllocMisc) {
3362 SetPackedFlag<kFieldCanBeMoved>(true);
3363 SetPackedField<DeoptimizeKindField>(kind);
3364 SetRawInputAt(0, cond);
3365 SetRawInputAt(1, guard);
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01003366 }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003367
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003368 bool CanBeMoved() const override { return GetPackedFlag<kFieldCanBeMoved>(); }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003369
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003370 bool InstructionDataEquals(const HInstruction* other) const override {
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003371 return (other->CanBeMoved() == CanBeMoved()) && (other->AsDeoptimize()->GetKind() == GetKind());
3372 }
3373
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003374 bool NeedsEnvironment() const override { return true; }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003375
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003376 bool CanThrow() const override { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003377
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003378 DeoptimizationKind GetDeoptimizationKind() const { return GetPackedField<DeoptimizeKindField>(); }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003379
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003380 bool GuardsAnInput() const {
3381 return InputCount() == 2;
3382 }
3383
3384 HInstruction* GuardedInput() const {
3385 DCHECK(GuardsAnInput());
3386 return InputAt(1);
3387 }
3388
3389 void RemoveGuard() {
3390 RemoveInputAt(1);
3391 }
3392
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003393 DECLARE_INSTRUCTION(Deoptimize);
3394
Artem Serovcced8ba2017-07-19 18:18:09 +01003395 protected:
3396 DEFAULT_COPY_CONSTRUCTOR(Deoptimize);
3397
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003398 private:
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003399 static constexpr size_t kFieldCanBeMoved = kNumberOfGenericPackedBits;
3400 static constexpr size_t kFieldDeoptimizeKind = kNumberOfGenericPackedBits + 1;
3401 static constexpr size_t kFieldDeoptimizeKindSize =
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003402 MinimumBitsToStore(static_cast<size_t>(DeoptimizationKind::kLast));
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003403 static constexpr size_t kNumberOfDeoptimizePackedBits =
3404 kFieldDeoptimizeKind + kFieldDeoptimizeKindSize;
3405 static_assert(kNumberOfDeoptimizePackedBits <= kMaxNumberOfPackedBits,
3406 "Too many packed fields.");
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003407 using DeoptimizeKindField =
3408 BitField<DeoptimizationKind, kFieldDeoptimizeKind, kFieldDeoptimizeKindSize>;
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003409};
3410
Mingyao Yang063fc772016-08-02 11:02:54 -07003411// Represents a should_deoptimize flag. Currently used for CHA-based devirtualization.
3412// The compiled code checks this flag value in a guard before devirtualized call and
3413// if it's true, starts to do deoptimization.
3414// It has a 4-byte slot on stack.
3415// TODO: allocate a register for this flag.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003416class HShouldDeoptimizeFlag final : public HVariableInputSizeInstruction {
Mingyao Yang063fc772016-08-02 11:02:54 -07003417 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003418 // CHA guards are only optimized in a separate pass and it has no side effects
3419 // with regard to other passes.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003420 HShouldDeoptimizeFlag(ArenaAllocator* allocator, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303421 : HVariableInputSizeInstruction(kShouldDeoptimizeFlag,
Vladimir Markobd785672018-05-03 17:09:09 +01003422 DataType::Type::kInt32,
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303423 SideEffects::None(),
3424 dex_pc,
3425 allocator,
3426 0,
3427 kArenaAllocCHA) {
Mingyao Yang063fc772016-08-02 11:02:54 -07003428 }
3429
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003430 // We do all CHA guard elimination/motion in a single pass, after which there is no
3431 // further guard elimination/motion since a guard might have been used for justification
3432 // of the elimination of another guard. Therefore, we pretend this guard cannot be moved
3433 // to avoid other optimizations trying to move it.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003434 bool CanBeMoved() const override { return false; }
Mingyao Yang063fc772016-08-02 11:02:54 -07003435
3436 DECLARE_INSTRUCTION(ShouldDeoptimizeFlag);
3437
Artem Serovcced8ba2017-07-19 18:18:09 +01003438 protected:
3439 DEFAULT_COPY_CONSTRUCTOR(ShouldDeoptimizeFlag);
Mingyao Yang063fc772016-08-02 11:02:54 -07003440};
3441
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003442// Represents the ArtMethod that was passed as a first argument to
3443// the method. It is used by instructions that depend on it, like
3444// instructions that work with the dex cache.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003445class HCurrentMethod final : public HExpression<0> {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003446 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003447 explicit HCurrentMethod(DataType::Type type, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303448 : HExpression(kCurrentMethod, type, SideEffects::None(), dex_pc) {
3449 }
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003450
3451 DECLARE_INSTRUCTION(CurrentMethod);
3452
Artem Serovcced8ba2017-07-19 18:18:09 +01003453 protected:
3454 DEFAULT_COPY_CONSTRUCTOR(CurrentMethod);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003455};
3456
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003457// Fetches an ArtMethod from the virtual table or the interface method table
3458// of a class.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003459class HClassTableGet final : public HExpression<1> {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003460 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003461 enum class TableKind {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003462 kVTable,
3463 kIMTable,
Vladimir Markoa1de9182016-02-25 11:37:38 +00003464 kLast = kIMTable
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003465 };
3466 HClassTableGet(HInstruction* cls,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003467 DataType::Type type,
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003468 TableKind kind,
3469 size_t index,
3470 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303471 : HExpression(kClassTableGet, type, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003472 index_(index) {
3473 SetPackedField<TableKindField>(kind);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003474 SetRawInputAt(0, cls);
3475 }
3476
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003477 bool IsClonable() const override { return true; }
3478 bool CanBeMoved() const override { return true; }
3479 bool InstructionDataEquals(const HInstruction* other) const override {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003480 return other->AsClassTableGet()->GetIndex() == index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00003481 other->AsClassTableGet()->GetPackedFields() == GetPackedFields();
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003482 }
3483
Vladimir Markoa1de9182016-02-25 11:37:38 +00003484 TableKind GetTableKind() const { return GetPackedField<TableKindField>(); }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003485 size_t GetIndex() const { return index_; }
3486
3487 DECLARE_INSTRUCTION(ClassTableGet);
3488
Artem Serovcced8ba2017-07-19 18:18:09 +01003489 protected:
3490 DEFAULT_COPY_CONSTRUCTOR(ClassTableGet);
3491
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003492 private:
Vladimir Markobd785672018-05-03 17:09:09 +01003493 static constexpr size_t kFieldTableKind = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003494 static constexpr size_t kFieldTableKindSize =
3495 MinimumBitsToStore(static_cast<size_t>(TableKind::kLast));
3496 static constexpr size_t kNumberOfClassTableGetPackedBits = kFieldTableKind + kFieldTableKindSize;
3497 static_assert(kNumberOfClassTableGetPackedBits <= kMaxNumberOfPackedBits,
3498 "Too many packed fields.");
3499 using TableKindField = BitField<TableKind, kFieldTableKind, kFieldTableKind>;
3500
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003501 // The index of the ArtMethod in the table.
3502 const size_t index_;
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003503};
3504
Mark Mendellfe57faa2015-09-18 09:26:15 -04003505// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
3506// have one successor for each entry in the switch table, and the final successor
3507// will be the block containing the next Dex opcode.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003508class HPackedSwitch final : public HExpression<1> {
Mark Mendellfe57faa2015-09-18 09:26:15 -04003509 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003510 HPackedSwitch(int32_t start_value,
3511 uint32_t num_entries,
3512 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04003513 uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01003514 : HExpression(kPackedSwitch, SideEffects::None(), dex_pc),
Mark Mendellfe57faa2015-09-18 09:26:15 -04003515 start_value_(start_value),
3516 num_entries_(num_entries) {
3517 SetRawInputAt(0, input);
3518 }
3519
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003520 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01003521
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003522 bool IsControlFlow() const override { return true; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04003523
3524 int32_t GetStartValue() const { return start_value_; }
3525
Vladimir Marko211c2112015-09-24 16:52:33 +01003526 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04003527
3528 HBasicBlock* GetDefaultBlock() const {
3529 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01003530 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04003531 }
3532 DECLARE_INSTRUCTION(PackedSwitch);
3533
Artem Serovcced8ba2017-07-19 18:18:09 +01003534 protected:
3535 DEFAULT_COPY_CONSTRUCTOR(PackedSwitch);
3536
Mark Mendellfe57faa2015-09-18 09:26:15 -04003537 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003538 const int32_t start_value_;
3539 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04003540};
3541
Roland Levillain88cb1752014-10-20 16:36:47 +01003542class HUnaryOperation : public HExpression<1> {
3543 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303544 HUnaryOperation(InstructionKind kind,
3545 DataType::Type result_type,
3546 HInstruction* input,
3547 uint32_t dex_pc = kNoDexPc)
3548 : HExpression(kind, result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01003549 SetRawInputAt(0, input);
3550 }
3551
Artem Serovcced8ba2017-07-19 18:18:09 +01003552 // All of the UnaryOperation instructions are clonable.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003553 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01003554
Roland Levillain88cb1752014-10-20 16:36:47 +01003555 HInstruction* GetInput() const { return InputAt(0); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003556 DataType::Type GetResultType() const { return GetType(); }
Roland Levillain88cb1752014-10-20 16:36:47 +01003557
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003558 bool CanBeMoved() const override { return true; }
3559 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003560 return true;
3561 }
Roland Levillain88cb1752014-10-20 16:36:47 +01003562
Roland Levillain31dd3d62016-02-16 12:21:02 +00003563 // Try to statically evaluate `this` and return a HConstant
3564 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003565 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003566 HConstant* TryStaticEvaluation() const;
3567
3568 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003569 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
3570 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003571 virtual HConstant* Evaluate(HFloatConstant* x) const = 0;
3572 virtual HConstant* Evaluate(HDoubleConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01003573
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003574 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01003575
Artem Serovcced8ba2017-07-19 18:18:09 +01003576 protected:
3577 DEFAULT_COPY_CONSTRUCTOR(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01003578};
3579
Dave Allison20dfc792014-06-16 20:44:29 -07003580class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003581 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303582 HBinaryOperation(InstructionKind kind,
3583 DataType::Type result_type,
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003584 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003585 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003586 SideEffects side_effects = SideEffects::None(),
3587 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303588 : HExpression(kind, result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003589 SetRawInputAt(0, left);
3590 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003591 }
3592
Artem Serovcced8ba2017-07-19 18:18:09 +01003593 // All of the BinaryOperation instructions are clonable.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003594 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01003595
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003596 HInstruction* GetLeft() const { return InputAt(0); }
3597 HInstruction* GetRight() const { return InputAt(1); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003598 DataType::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003599
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003600 virtual bool IsCommutative() const { return false; }
3601
3602 // Put constant on the right.
3603 // Returns whether order is changed.
3604 bool OrderInputsWithConstantOnTheRight() {
3605 HInstruction* left = InputAt(0);
3606 HInstruction* right = InputAt(1);
3607 if (left->IsConstant() && !right->IsConstant()) {
3608 ReplaceInput(right, 0);
3609 ReplaceInput(left, 1);
3610 return true;
3611 }
3612 return false;
3613 }
3614
3615 // Order inputs by instruction id, but favor constant on the right side.
3616 // This helps GVN for commutative ops.
3617 void OrderInputs() {
3618 DCHECK(IsCommutative());
3619 HInstruction* left = InputAt(0);
3620 HInstruction* right = InputAt(1);
3621 if (left == right || (!left->IsConstant() && right->IsConstant())) {
3622 return;
3623 }
3624 if (OrderInputsWithConstantOnTheRight()) {
3625 return;
3626 }
3627 // Order according to instruction id.
3628 if (left->GetId() > right->GetId()) {
3629 ReplaceInput(right, 0);
3630 ReplaceInput(left, 1);
3631 }
3632 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003633
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003634 bool CanBeMoved() const override { return true; }
3635 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003636 return true;
3637 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003638
Roland Levillain31dd3d62016-02-16 12:21:02 +00003639 // Try to statically evaluate `this` and return a HConstant
3640 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003641 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003642 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01003643
3644 // Apply this operation to `x` and `y`.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003645 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3646 HNullConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003647 LOG(FATAL) << DebugName() << " is not defined for the (null, null) case.";
3648 UNREACHABLE();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003649 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003650 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
3651 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
Roland Levillain9867bc72015-08-05 10:21:34 +01003652 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
3653 HIntConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003654 LOG(FATAL) << DebugName() << " is not defined for the (long, int) case.";
3655 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01003656 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003657 virtual HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const = 0;
3658 virtual HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const = 0;
Roland Levillain556c3d12014-09-18 15:25:07 +01003659
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003660 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003661 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003662 HConstant* GetConstantRight() const;
3663
3664 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003665 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003666 HInstruction* GetLeastConstantLeft() const;
3667
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003668 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01003669
Artem Serovcced8ba2017-07-19 18:18:09 +01003670 protected:
3671 DEFAULT_COPY_CONSTRUCTOR(BinaryOperation);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003672};
3673
Mark Mendellc4701932015-04-10 13:18:51 -04003674// The comparison bias applies for floating point operations and indicates how NaN
3675// comparisons are treated:
Vladimir Marko9974e3c2020-06-10 16:27:06 +01003676enum class ComparisonBias { // private marker to avoid generate-operator-out.py from processing.
Mark Mendellc4701932015-04-10 13:18:51 -04003677 kNoBias, // bias is not applicable (i.e. for long operation)
3678 kGtBias, // return 1 for NaN comparisons
3679 kLtBias, // return -1 for NaN comparisons
Vladimir Markoa1de9182016-02-25 11:37:38 +00003680 kLast = kLtBias
Mark Mendellc4701932015-04-10 13:18:51 -04003681};
3682
Vladimir Marko9974e3c2020-06-10 16:27:06 +01003683std::ostream& operator<<(std::ostream& os, ComparisonBias rhs);
Roland Levillain31dd3d62016-02-16 12:21:02 +00003684
Dave Allison20dfc792014-06-16 20:44:29 -07003685class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003686 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303687 HCondition(InstructionKind kind,
3688 HInstruction* first,
3689 HInstruction* second,
3690 uint32_t dex_pc = kNoDexPc)
3691 : HBinaryOperation(kind,
3692 DataType::Type::kBool,
3693 first,
3694 second,
3695 SideEffects::None(),
3696 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003697 SetPackedField<ComparisonBiasField>(ComparisonBias::kNoBias);
3698 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003699
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003700 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003701 // `instruction`, and disregard moves in between.
3702 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003703
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003704 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003705
3706 virtual IfCondition GetCondition() const = 0;
3707
Mark Mendellc4701932015-04-10 13:18:51 -04003708 virtual IfCondition GetOppositeCondition() const = 0;
3709
Vladimir Markoa1de9182016-02-25 11:37:38 +00003710 bool IsGtBias() const { return GetBias() == ComparisonBias::kGtBias; }
Anton Shaminbdd79352016-02-15 12:48:36 +06003711 bool IsLtBias() const { return GetBias() == ComparisonBias::kLtBias; }
3712
Vladimir Markoa1de9182016-02-25 11:37:38 +00003713 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
3714 void SetBias(ComparisonBias bias) { SetPackedField<ComparisonBiasField>(bias); }
Mark Mendellc4701932015-04-10 13:18:51 -04003715
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003716 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003717 return GetPackedFields() == other->AsCondition()->GetPackedFields();
Mark Mendellc4701932015-04-10 13:18:51 -04003718 }
3719
Roland Levillain4fa13f62015-07-06 18:11:54 +01003720 bool IsFPConditionTrueIfNaN() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003721 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003722 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003723 if (if_cond == kCondNE) {
3724 return true;
3725 } else if (if_cond == kCondEQ) {
3726 return false;
3727 }
3728 return ((if_cond == kCondGT) || (if_cond == kCondGE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003729 }
3730
3731 bool IsFPConditionFalseIfNaN() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003732 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003733 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003734 if (if_cond == kCondEQ) {
3735 return true;
3736 } else if (if_cond == kCondNE) {
3737 return false;
3738 }
3739 return ((if_cond == kCondLT) || (if_cond == kCondLE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003740 }
3741
Roland Levillain31dd3d62016-02-16 12:21:02 +00003742 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003743 // Needed if we merge a HCompare into a HCondition.
Vladimir Markobd785672018-05-03 17:09:09 +01003744 static constexpr size_t kFieldComparisonBias = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003745 static constexpr size_t kFieldComparisonBiasSize =
3746 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3747 static constexpr size_t kNumberOfConditionPackedBits =
3748 kFieldComparisonBias + kFieldComparisonBiasSize;
3749 static_assert(kNumberOfConditionPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3750 using ComparisonBiasField =
3751 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3752
Roland Levillain31dd3d62016-02-16 12:21:02 +00003753 template <typename T>
3754 int32_t Compare(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3755
3756 template <typename T>
3757 int32_t CompareFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003758 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003759 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3760 // Handle the bias.
3761 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compare(x, y);
3762 }
3763
3764 // Return an integer constant containing the result of a condition evaluated at compile time.
3765 HIntConstant* MakeConstantCondition(bool value, uint32_t dex_pc) const {
3766 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3767 }
3768
Artem Serovcced8ba2017-07-19 18:18:09 +01003769 DEFAULT_COPY_CONSTRUCTOR(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003770};
3771
3772// Instruction to check if two inputs are equal to each other.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003773class HEqual final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003774 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003775 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303776 : HCondition(kEqual, first, second, dex_pc) {
3777 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003778
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003779 bool IsCommutative() const override { return true; }
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003780
Vladimir Marko9e23df52015-11-10 17:14:35 +00003781 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003782 HNullConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003783 return MakeConstantCondition(true, GetDexPc());
3784 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003785 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003786 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3787 }
3788 // In the following Evaluate methods, a HCompare instruction has
3789 // been merged into this HEqual instruction; evaluate it as
3790 // `Compare(x, y) == 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003791 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003792 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0),
3793 GetDexPc());
3794 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003795 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003796 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3797 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003798 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003799 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003800 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003801
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003802 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003803
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003804 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07003805 return kCondEQ;
3806 }
3807
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003808 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04003809 return kCondNE;
3810 }
3811
Artem Serovcced8ba2017-07-19 18:18:09 +01003812 protected:
3813 DEFAULT_COPY_CONSTRUCTOR(Equal);
3814
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003815 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003816 template <typename T> static bool Compute(T x, T y) { return x == y; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003817};
3818
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003819class HNotEqual final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003820 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303821 HNotEqual(HInstruction* first, HInstruction* second,
3822 uint32_t dex_pc = kNoDexPc)
3823 : HCondition(kNotEqual, first, second, dex_pc) {
3824 }
Dave Allison20dfc792014-06-16 20:44:29 -07003825
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003826 bool IsCommutative() const override { return true; }
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003827
Vladimir Marko9e23df52015-11-10 17:14:35 +00003828 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003829 HNullConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003830 return MakeConstantCondition(false, GetDexPc());
3831 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003832 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003833 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3834 }
3835 // In the following Evaluate methods, a HCompare instruction has
3836 // been merged into this HNotEqual instruction; evaluate it as
3837 // `Compare(x, y) != 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003838 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003839 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3840 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003841 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003842 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3843 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003844 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003845 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003846 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003847
Dave Allison20dfc792014-06-16 20:44:29 -07003848 DECLARE_INSTRUCTION(NotEqual);
3849
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003850 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07003851 return kCondNE;
3852 }
3853
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003854 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04003855 return kCondEQ;
3856 }
3857
Artem Serovcced8ba2017-07-19 18:18:09 +01003858 protected:
3859 DEFAULT_COPY_CONSTRUCTOR(NotEqual);
3860
Dave Allison20dfc792014-06-16 20:44:29 -07003861 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003862 template <typename T> static bool Compute(T x, T y) { return x != y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003863};
3864
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003865class HLessThan final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003866 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303867 HLessThan(HInstruction* first, HInstruction* second,
3868 uint32_t dex_pc = kNoDexPc)
3869 : HCondition(kLessThan, first, second, dex_pc) {
3870 }
Dave Allison20dfc792014-06-16 20:44:29 -07003871
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003872 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003873 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003874 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003875 // In the following Evaluate methods, a HCompare instruction has
3876 // been merged into this HLessThan instruction; evaluate it as
3877 // `Compare(x, y) < 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003878 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003879 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3880 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003881 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003882 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3883 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003884 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003885 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003886 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003887
Dave Allison20dfc792014-06-16 20:44:29 -07003888 DECLARE_INSTRUCTION(LessThan);
3889
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003890 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07003891 return kCondLT;
3892 }
3893
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003894 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04003895 return kCondGE;
3896 }
3897
Artem Serovcced8ba2017-07-19 18:18:09 +01003898 protected:
3899 DEFAULT_COPY_CONSTRUCTOR(LessThan);
3900
Dave Allison20dfc792014-06-16 20:44:29 -07003901 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003902 template <typename T> static bool Compute(T x, T y) { return x < y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003903};
3904
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003905class HLessThanOrEqual final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003906 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303907 HLessThanOrEqual(HInstruction* first, HInstruction* second,
3908 uint32_t dex_pc = kNoDexPc)
3909 : HCondition(kLessThanOrEqual, first, second, dex_pc) {
3910 }
Dave Allison20dfc792014-06-16 20:44:29 -07003911
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003912 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003913 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003914 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003915 // In the following Evaluate methods, a HCompare instruction has
3916 // been merged into this HLessThanOrEqual instruction; evaluate it as
3917 // `Compare(x, y) <= 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003918 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003919 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3920 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003921 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003922 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3923 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003924 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003925 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003926 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003927
Dave Allison20dfc792014-06-16 20:44:29 -07003928 DECLARE_INSTRUCTION(LessThanOrEqual);
3929
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003930 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07003931 return kCondLE;
3932 }
3933
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003934 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04003935 return kCondGT;
3936 }
3937
Artem Serovcced8ba2017-07-19 18:18:09 +01003938 protected:
3939 DEFAULT_COPY_CONSTRUCTOR(LessThanOrEqual);
3940
Dave Allison20dfc792014-06-16 20:44:29 -07003941 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003942 template <typename T> static bool Compute(T x, T y) { return x <= y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003943};
3944
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003945class HGreaterThan final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003946 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003947 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303948 : HCondition(kGreaterThan, first, second, dex_pc) {
3949 }
Dave Allison20dfc792014-06-16 20:44:29 -07003950
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003951 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003952 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003953 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003954 // In the following Evaluate methods, a HCompare instruction has
3955 // been merged into this HGreaterThan instruction; evaluate it as
3956 // `Compare(x, y) > 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003957 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003958 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3959 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003960 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003961 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3962 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003963 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003964 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003965 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003966
Dave Allison20dfc792014-06-16 20:44:29 -07003967 DECLARE_INSTRUCTION(GreaterThan);
3968
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003969 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07003970 return kCondGT;
3971 }
3972
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003973 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04003974 return kCondLE;
3975 }
3976
Artem Serovcced8ba2017-07-19 18:18:09 +01003977 protected:
3978 DEFAULT_COPY_CONSTRUCTOR(GreaterThan);
3979
Dave Allison20dfc792014-06-16 20:44:29 -07003980 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003981 template <typename T> static bool Compute(T x, T y) { return x > y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003982};
3983
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003984class HGreaterThanOrEqual final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003985 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003986 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303987 : HCondition(kGreaterThanOrEqual, first, second, dex_pc) {
3988 }
Dave Allison20dfc792014-06-16 20:44:29 -07003989
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003990 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003991 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003992 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003993 // In the following Evaluate methods, a HCompare instruction has
3994 // been merged into this HGreaterThanOrEqual instruction; evaluate it as
3995 // `Compare(x, y) >= 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003996 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003997 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3998 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003999 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004000 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
4001 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004002 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004003 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004004 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004005
Dave Allison20dfc792014-06-16 20:44:29 -07004006 DECLARE_INSTRUCTION(GreaterThanOrEqual);
4007
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004008 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07004009 return kCondGE;
4010 }
4011
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004012 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04004013 return kCondLT;
4014 }
4015
Artem Serovcced8ba2017-07-19 18:18:09 +01004016 protected:
4017 DEFAULT_COPY_CONSTRUCTOR(GreaterThanOrEqual);
4018
Dave Allison20dfc792014-06-16 20:44:29 -07004019 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004020 template <typename T> static bool Compute(T x, T y) { return x >= y; }
Dave Allison20dfc792014-06-16 20:44:29 -07004021};
4022
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004023class HBelow final : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07004024 public:
4025 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304026 : HCondition(kBelow, first, second, dex_pc) {
4027 }
Aart Bike9f37602015-10-09 11:15:55 -07004028
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004029 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004030 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07004031 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004032 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004033 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4034 }
4035 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004036 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004037 LOG(FATAL) << DebugName() << " is not defined for float values";
4038 UNREACHABLE();
4039 }
4040 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004041 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004042 LOG(FATAL) << DebugName() << " is not defined for double values";
4043 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07004044 }
4045
4046 DECLARE_INSTRUCTION(Below);
4047
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004048 IfCondition GetCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004049 return kCondB;
4050 }
4051
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004052 IfCondition GetOppositeCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004053 return kCondAE;
4054 }
4055
Artem Serovcced8ba2017-07-19 18:18:09 +01004056 protected:
4057 DEFAULT_COPY_CONSTRUCTOR(Below);
4058
Aart Bike9f37602015-10-09 11:15:55 -07004059 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004060 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004061 return MakeUnsigned(x) < MakeUnsigned(y);
4062 }
Aart Bike9f37602015-10-09 11:15:55 -07004063};
4064
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004065class HBelowOrEqual final : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07004066 public:
4067 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304068 : HCondition(kBelowOrEqual, first, second, dex_pc) {
4069 }
Aart Bike9f37602015-10-09 11:15:55 -07004070
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004071 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004072 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07004073 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004074 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004075 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4076 }
4077 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004078 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004079 LOG(FATAL) << DebugName() << " is not defined for float values";
4080 UNREACHABLE();
4081 }
4082 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004083 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004084 LOG(FATAL) << DebugName() << " is not defined for double values";
4085 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07004086 }
4087
4088 DECLARE_INSTRUCTION(BelowOrEqual);
4089
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004090 IfCondition GetCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004091 return kCondBE;
4092 }
4093
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004094 IfCondition GetOppositeCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004095 return kCondA;
4096 }
4097
Artem Serovcced8ba2017-07-19 18:18:09 +01004098 protected:
4099 DEFAULT_COPY_CONSTRUCTOR(BelowOrEqual);
4100
Aart Bike9f37602015-10-09 11:15:55 -07004101 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004102 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004103 return MakeUnsigned(x) <= MakeUnsigned(y);
4104 }
Aart Bike9f37602015-10-09 11:15:55 -07004105};
4106
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004107class HAbove final : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07004108 public:
4109 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304110 : HCondition(kAbove, first, second, dex_pc) {
4111 }
Aart Bike9f37602015-10-09 11:15:55 -07004112
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004113 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004114 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07004115 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004116 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004117 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4118 }
4119 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004120 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004121 LOG(FATAL) << DebugName() << " is not defined for float values";
4122 UNREACHABLE();
4123 }
4124 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004125 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004126 LOG(FATAL) << DebugName() << " is not defined for double values";
4127 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07004128 }
4129
4130 DECLARE_INSTRUCTION(Above);
4131
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004132 IfCondition GetCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004133 return kCondA;
4134 }
4135
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004136 IfCondition GetOppositeCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004137 return kCondBE;
4138 }
4139
Artem Serovcced8ba2017-07-19 18:18:09 +01004140 protected:
4141 DEFAULT_COPY_CONSTRUCTOR(Above);
4142
Aart Bike9f37602015-10-09 11:15:55 -07004143 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004144 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004145 return MakeUnsigned(x) > MakeUnsigned(y);
4146 }
Aart Bike9f37602015-10-09 11:15:55 -07004147};
4148
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004149class HAboveOrEqual final : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07004150 public:
4151 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304152 : HCondition(kAboveOrEqual, first, second, dex_pc) {
4153 }
Aart Bike9f37602015-10-09 11:15:55 -07004154
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004155 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004156 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07004157 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004158 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004159 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4160 }
4161 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004162 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004163 LOG(FATAL) << DebugName() << " is not defined for float values";
4164 UNREACHABLE();
4165 }
4166 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004167 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004168 LOG(FATAL) << DebugName() << " is not defined for double values";
4169 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07004170 }
4171
4172 DECLARE_INSTRUCTION(AboveOrEqual);
4173
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004174 IfCondition GetCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004175 return kCondAE;
4176 }
4177
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004178 IfCondition GetOppositeCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004179 return kCondB;
4180 }
4181
Artem Serovcced8ba2017-07-19 18:18:09 +01004182 protected:
4183 DEFAULT_COPY_CONSTRUCTOR(AboveOrEqual);
4184
Aart Bike9f37602015-10-09 11:15:55 -07004185 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004186 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004187 return MakeUnsigned(x) >= MakeUnsigned(y);
4188 }
Aart Bike9f37602015-10-09 11:15:55 -07004189};
Dave Allison20dfc792014-06-16 20:44:29 -07004190
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004191// Instruction to check how two inputs compare to each other.
4192// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004193class HCompare final : public HBinaryOperation {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004194 public:
Roland Levillaina5c4a402016-03-15 15:02:50 +00004195 // Note that `comparison_type` is the type of comparison performed
4196 // between the comparison's inputs, not the type of the instantiated
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004197 // HCompare instruction (which is always DataType::Type::kInt).
4198 HCompare(DataType::Type comparison_type,
Alexey Frunze4dda3372015-06-01 18:31:49 -07004199 HInstruction* first,
4200 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04004201 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07004202 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304203 : HBinaryOperation(kCompare,
4204 DataType::Type::kInt32,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004205 first,
4206 second,
Roland Levillaina5c4a402016-03-15 15:02:50 +00004207 SideEffectsForArchRuntimeCalls(comparison_type),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004208 dex_pc) {
4209 SetPackedField<ComparisonBiasField>(bias);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004210 }
4211
Roland Levillain9867bc72015-08-05 10:21:34 +01004212 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004213 int32_t Compute(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
4214
4215 template <typename T>
4216 int32_t ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004217 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00004218 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
4219 // Handle the bias.
4220 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compute(x, y);
4221 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004222
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004223 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004224 // Note that there is no "cmp-int" Dex instruction so we shouldn't
4225 // reach this code path when processing a freshly built HIR
4226 // graph. However HCompare integer instructions can be synthesized
4227 // by the instruction simplifier to implement IntegerCompare and
4228 // IntegerSignum intrinsics, so we have to handle this case.
4229 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004230 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004231 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004232 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4233 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004234 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004235 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4236 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004237 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004238 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01004239 }
4240
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004241 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004242 return GetPackedFields() == other->AsCompare()->GetPackedFields();
Calin Juravleddb7df22014-11-25 20:56:51 +00004243 }
4244
Vladimir Markoa1de9182016-02-25 11:37:38 +00004245 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
Mark Mendellc4701932015-04-10 13:18:51 -04004246
Roland Levillain31dd3d62016-02-16 12:21:02 +00004247 // Does this compare instruction have a "gt bias" (vs an "lt bias")?
Vladimir Markoa1de9182016-02-25 11:37:38 +00004248 // Only meaningful for floating-point comparisons.
Roland Levillain31dd3d62016-02-16 12:21:02 +00004249 bool IsGtBias() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004250 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004251 return GetBias() == ComparisonBias::kGtBias;
Roland Levillain31dd3d62016-02-16 12:21:02 +00004252 }
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004253
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004254 static SideEffects SideEffectsForArchRuntimeCalls(DataType::Type type ATTRIBUTE_UNUSED) {
Roland Levillain1693a1f2016-03-15 14:57:31 +00004255 // Comparisons do not require a runtime call in any back end.
4256 return SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004257 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004258
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004259 DECLARE_INSTRUCTION(Compare);
4260
Roland Levillain31dd3d62016-02-16 12:21:02 +00004261 protected:
Vladimir Markobd785672018-05-03 17:09:09 +01004262 static constexpr size_t kFieldComparisonBias = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004263 static constexpr size_t kFieldComparisonBiasSize =
4264 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
4265 static constexpr size_t kNumberOfComparePackedBits =
4266 kFieldComparisonBias + kFieldComparisonBiasSize;
4267 static_assert(kNumberOfComparePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
4268 using ComparisonBiasField =
4269 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
4270
Roland Levillain31dd3d62016-02-16 12:21:02 +00004271 // Return an integer constant containing the result of a comparison evaluated at compile time.
4272 HIntConstant* MakeConstantComparison(int32_t value, uint32_t dex_pc) const {
4273 DCHECK(value == -1 || value == 0 || value == 1) << value;
4274 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
4275 }
4276
Artem Serovcced8ba2017-07-19 18:18:09 +01004277 DEFAULT_COPY_CONSTRUCTOR(Compare);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004278};
4279
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004280class HNewInstance final : public HExpression<1> {
David Brazdil6de19382016-01-08 17:37:10 +00004281 public:
4282 HNewInstance(HInstruction* cls,
David Brazdil6de19382016-01-08 17:37:10 +00004283 uint32_t dex_pc,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004284 dex::TypeIndex type_index,
David Brazdil6de19382016-01-08 17:37:10 +00004285 const DexFile& dex_file,
David Brazdil6de19382016-01-08 17:37:10 +00004286 bool finalizable,
4287 QuickEntrypointEnum entrypoint)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304288 : HExpression(kNewInstance,
4289 DataType::Type::kReference,
4290 SideEffects::CanTriggerGC(),
4291 dex_pc),
David Brazdil6de19382016-01-08 17:37:10 +00004292 type_index_(type_index),
4293 dex_file_(dex_file),
David Brazdil6de19382016-01-08 17:37:10 +00004294 entrypoint_(entrypoint) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004295 SetPackedFlag<kFlagFinalizable>(finalizable);
David Brazdil6de19382016-01-08 17:37:10 +00004296 SetRawInputAt(0, cls);
David Brazdil6de19382016-01-08 17:37:10 +00004297 }
4298
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004299 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004300
Andreas Gampea5b09a62016-11-17 15:21:22 -08004301 dex::TypeIndex GetTypeIndex() const { return type_index_; }
David Brazdil6de19382016-01-08 17:37:10 +00004302 const DexFile& GetDexFile() const { return dex_file_; }
4303
4304 // Calls runtime so needs an environment.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004305 bool NeedsEnvironment() const override { return true; }
David Brazdil6de19382016-01-08 17:37:10 +00004306
Mingyao Yang062157f2016-03-02 10:15:36 -08004307 // Can throw errors when out-of-memory or if it's not instantiable/accessible.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004308 bool CanThrow() const override { return true; }
Mingyao Yang062157f2016-03-02 10:15:36 -08004309
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004310 bool NeedsChecks() const {
4311 return entrypoint_ == kQuickAllocObjectWithChecks;
4312 }
David Brazdil6de19382016-01-08 17:37:10 +00004313
Vladimir Markoa1de9182016-02-25 11:37:38 +00004314 bool IsFinalizable() const { return GetPackedFlag<kFlagFinalizable>(); }
David Brazdil6de19382016-01-08 17:37:10 +00004315
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004316 bool CanBeNull() const override { return false; }
David Brazdil6de19382016-01-08 17:37:10 +00004317
4318 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
4319
4320 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
4321 entrypoint_ = entrypoint;
4322 }
4323
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004324 HLoadClass* GetLoadClass() const {
4325 HInstruction* input = InputAt(0);
4326 if (input->IsClinitCheck()) {
4327 input = input->InputAt(0);
4328 }
4329 DCHECK(input->IsLoadClass());
4330 return input->AsLoadClass();
4331 }
4332
David Brazdil6de19382016-01-08 17:37:10 +00004333 bool IsStringAlloc() const;
4334
4335 DECLARE_INSTRUCTION(NewInstance);
4336
Artem Serovcced8ba2017-07-19 18:18:09 +01004337 protected:
4338 DEFAULT_COPY_CONSTRUCTOR(NewInstance);
4339
David Brazdil6de19382016-01-08 17:37:10 +00004340 private:
Vladimir Markobd785672018-05-03 17:09:09 +01004341 static constexpr size_t kFlagFinalizable = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004342 static constexpr size_t kNumberOfNewInstancePackedBits = kFlagFinalizable + 1;
4343 static_assert(kNumberOfNewInstancePackedBits <= kMaxNumberOfPackedBits,
4344 "Too many packed fields.");
4345
Andreas Gampea5b09a62016-11-17 15:21:22 -08004346 const dex::TypeIndex type_index_;
David Brazdil6de19382016-01-08 17:37:10 +00004347 const DexFile& dex_file_;
David Brazdil6de19382016-01-08 17:37:10 +00004348 QuickEntrypointEnum entrypoint_;
David Brazdil6de19382016-01-08 17:37:10 +00004349};
4350
Agi Csaki05f20562015-08-19 14:58:14 -07004351enum IntrinsicNeedsEnvironmentOrCache {
4352 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
4353 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07004354};
4355
Aart Bik5d75afe2015-12-14 11:57:01 -08004356enum IntrinsicSideEffects {
4357 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
4358 kReadSideEffects, // Intrinsic may read heap memory.
4359 kWriteSideEffects, // Intrinsic may write heap memory.
4360 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
4361};
4362
4363enum IntrinsicExceptions {
4364 kNoThrow, // Intrinsic does not throw any exceptions.
4365 kCanThrow // Intrinsic may throw exceptions.
4366};
4367
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004368class HInvoke : public HVariableInputSizeInstruction {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004369 public:
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004370 bool NeedsEnvironment() const override;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004371
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01004372 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004373 SetRawInputAt(index, argument);
4374 }
4375
Roland Levillain3e3d7332015-04-28 11:00:54 +01004376 // Return the number of arguments. This number can be lower than
4377 // the number of inputs returned by InputCount(), as some invoke
4378 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
4379 // inputs at the end of their list of inputs.
4380 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
4381
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004382 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
4383
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004384 InvokeType GetInvokeType() const {
4385 return GetPackedField<InvokeTypeField>();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004386 }
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004387
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01004388 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004389 return intrinsic_;
4390 }
4391
Aart Bik5d75afe2015-12-14 11:57:01 -08004392 void SetIntrinsic(Intrinsics intrinsic,
4393 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
4394 IntrinsicSideEffects side_effects,
4395 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004396
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01004397 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00004398 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01004399 }
4400
Aart Bikff7d89c2016-11-07 08:49:28 -08004401 void SetCanThrow(bool can_throw) { SetPackedFlag<kFlagCanThrow>(can_throw); }
4402
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004403 bool CanThrow() const override { return GetPackedFlag<kFlagCanThrow>(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08004404
Aart Bika8b8e9b2018-01-09 11:01:02 -08004405 void SetAlwaysThrows(bool always_throws) { SetPackedFlag<kFlagAlwaysThrows>(always_throws); }
4406
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004407 bool AlwaysThrows() const override { return GetPackedFlag<kFlagAlwaysThrows>(); }
Aart Bika8b8e9b2018-01-09 11:01:02 -08004408
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004409 bool CanBeMoved() const override { return IsIntrinsic() && !DoesAnyWrite(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08004410
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004411 bool InstructionDataEquals(const HInstruction* other) const override {
Aart Bik5d75afe2015-12-14 11:57:01 -08004412 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
4413 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01004414
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004415 uint32_t* GetIntrinsicOptimizations() {
4416 return &intrinsic_optimizations_;
4417 }
4418
4419 const uint32_t* GetIntrinsicOptimizations() const {
4420 return &intrinsic_optimizations_;
4421 }
4422
4423 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
4424
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004425 ArtMethod* GetResolvedMethod() const { return resolved_method_; }
Nicolas Geoffray76d4bb0f32018-09-21 12:58:45 +01004426 void SetResolvedMethod(ArtMethod* method) REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004427
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004428 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004429
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004430 protected:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004431 static constexpr size_t kFieldInvokeType = kNumberOfGenericPackedBits;
4432 static constexpr size_t kFieldInvokeTypeSize =
Vladimir Markoa1de9182016-02-25 11:37:38 +00004433 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
Vladimir Markobd785672018-05-03 17:09:09 +01004434 static constexpr size_t kFlagCanThrow = kFieldInvokeType + kFieldInvokeTypeSize;
Aart Bika8b8e9b2018-01-09 11:01:02 -08004435 static constexpr size_t kFlagAlwaysThrows = kFlagCanThrow + 1;
4436 static constexpr size_t kNumberOfInvokePackedBits = kFlagAlwaysThrows + 1;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004437 static_assert(kNumberOfInvokePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004438 using InvokeTypeField = BitField<InvokeType, kFieldInvokeType, kFieldInvokeTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004439
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304440 HInvoke(InstructionKind kind,
4441 ArenaAllocator* allocator,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004442 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01004443 uint32_t number_of_other_inputs,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004444 DataType::Type return_type,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004445 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004446 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004447 ArtMethod* resolved_method,
4448 InvokeType invoke_type)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004449 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304450 kind,
Vladimir Markobd785672018-05-03 17:09:09 +01004451 return_type,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004452 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
4453 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004454 allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004455 number_of_arguments + number_of_other_inputs,
4456 kArenaAllocInvokeInputs),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004457 number_of_arguments_(number_of_arguments),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004458 dex_method_index_(dex_method_index),
agicsaki57b81ec2015-08-11 17:39:37 -07004459 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004460 intrinsic_optimizations_(0) {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004461 SetPackedField<InvokeTypeField>(invoke_type);
Vladimir Markoa1de9182016-02-25 11:37:38 +00004462 SetPackedFlag<kFlagCanThrow>(true);
Nicolas Geoffray76d4bb0f32018-09-21 12:58:45 +01004463 // Check mutator lock, constructors lack annotalysis support.
4464 Locks::mutator_lock_->AssertNotExclusiveHeld(Thread::Current());
4465 SetResolvedMethod(resolved_method);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004466 }
4467
Artem Serovcced8ba2017-07-19 18:18:09 +01004468 DEFAULT_COPY_CONSTRUCTOR(Invoke);
4469
Roland Levillain3e3d7332015-04-28 11:00:54 +01004470 uint32_t number_of_arguments_;
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00004471 ArtMethod* resolved_method_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004472 const uint32_t dex_method_index_;
4473 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004474
4475 // A magic word holding optimizations for intrinsics. See intrinsics.h.
4476 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004477};
4478
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004479class HInvokeUnresolved final : public HInvoke {
Calin Juravle175dc732015-08-25 15:42:32 +01004480 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004481 HInvokeUnresolved(ArenaAllocator* allocator,
Calin Juravle175dc732015-08-25 15:42:32 +01004482 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004483 DataType::Type return_type,
Calin Juravle175dc732015-08-25 15:42:32 +01004484 uint32_t dex_pc,
4485 uint32_t dex_method_index,
4486 InvokeType invoke_type)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304487 : HInvoke(kInvokeUnresolved,
4488 allocator,
Calin Juravle175dc732015-08-25 15:42:32 +01004489 number_of_arguments,
Andreas Gampe3db70682018-12-26 15:12:03 -08004490 /* number_of_other_inputs= */ 0u,
Calin Juravle175dc732015-08-25 15:42:32 +01004491 return_type,
4492 dex_pc,
4493 dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004494 nullptr,
Calin Juravle175dc732015-08-25 15:42:32 +01004495 invoke_type) {
4496 }
4497
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004498 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004499
Calin Juravle175dc732015-08-25 15:42:32 +01004500 DECLARE_INSTRUCTION(InvokeUnresolved);
4501
Artem Serovcced8ba2017-07-19 18:18:09 +01004502 protected:
4503 DEFAULT_COPY_CONSTRUCTOR(InvokeUnresolved);
Calin Juravle175dc732015-08-25 15:42:32 +01004504};
4505
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004506class HInvokePolymorphic final : public HInvoke {
Orion Hodsonac141392017-01-13 11:53:47 +00004507 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004508 HInvokePolymorphic(ArenaAllocator* allocator,
Orion Hodsonac141392017-01-13 11:53:47 +00004509 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004510 DataType::Type return_type,
Orion Hodsonac141392017-01-13 11:53:47 +00004511 uint32_t dex_pc,
4512 uint32_t dex_method_index)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304513 : HInvoke(kInvokePolymorphic,
4514 allocator,
Orion Hodsonac141392017-01-13 11:53:47 +00004515 number_of_arguments,
Andreas Gampe3db70682018-12-26 15:12:03 -08004516 /* number_of_other_inputs= */ 0u,
Orion Hodsonac141392017-01-13 11:53:47 +00004517 return_type,
4518 dex_pc,
4519 dex_method_index,
4520 nullptr,
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304521 kVirtual) {
4522 }
Orion Hodsonac141392017-01-13 11:53:47 +00004523
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004524 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004525
Orion Hodsonac141392017-01-13 11:53:47 +00004526 DECLARE_INSTRUCTION(InvokePolymorphic);
4527
Artem Serovcced8ba2017-07-19 18:18:09 +01004528 protected:
4529 DEFAULT_COPY_CONSTRUCTOR(InvokePolymorphic);
Orion Hodsonac141392017-01-13 11:53:47 +00004530};
4531
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004532class HInvokeCustom final : public HInvoke {
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004533 public:
4534 HInvokeCustom(ArenaAllocator* allocator,
4535 uint32_t number_of_arguments,
4536 uint32_t call_site_index,
4537 DataType::Type return_type,
4538 uint32_t dex_pc)
4539 : HInvoke(kInvokeCustom,
4540 allocator,
4541 number_of_arguments,
Andreas Gampe3db70682018-12-26 15:12:03 -08004542 /* number_of_other_inputs= */ 0u,
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004543 return_type,
4544 dex_pc,
Andreas Gampe3db70682018-12-26 15:12:03 -08004545 /* dex_method_index= */ dex::kDexNoIndex,
4546 /* resolved_method= */ nullptr,
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004547 kStatic),
4548 call_site_index_(call_site_index) {
4549 }
4550
4551 uint32_t GetCallSiteIndex() const { return call_site_index_; }
4552
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004553 bool IsClonable() const override { return true; }
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004554
4555 DECLARE_INSTRUCTION(InvokeCustom);
4556
4557 protected:
4558 DEFAULT_COPY_CONSTRUCTOR(InvokeCustom);
4559
4560 private:
4561 uint32_t call_site_index_;
4562};
4563
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004564class HInvokeStaticOrDirect final : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004565 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01004566 // Requirements of this method call regarding the class
4567 // initialization (clinit) check of its declaring class.
Vladimir Marko9974e3c2020-06-10 16:27:06 +01004568 enum class ClinitCheckRequirement { // private marker to avoid generate-operator-out.py from processing.
Roland Levillain4c0eb422015-04-24 16:43:49 +01004569 kNone, // Class already initialized.
4570 kExplicit, // Static call having explicit clinit check as last input.
4571 kImplicit, // Static call implicitly requiring a clinit check.
Vladimir Markoa1de9182016-02-25 11:37:38 +00004572 kLast = kImplicit
Roland Levillain4c0eb422015-04-24 16:43:49 +01004573 };
4574
Vladimir Marko58155012015-08-19 12:49:41 +00004575 // Determines how to load the target ArtMethod*.
4576 enum class MethodLoadKind {
4577 // Use a String init ArtMethod* loaded from Thread entrypoints.
4578 kStringInit,
4579
4580 // Use the method's own ArtMethod* loaded by the register allocator.
4581 kRecursive,
4582
Vladimir Marko65979462017-05-19 17:25:12 +01004583 // Use PC-relative boot image ArtMethod* address that will be known at link time.
4584 // Used for boot image methods referenced by boot image code.
4585 kBootImageLinkTimePcRelative,
4586
Vladimir Markob066d432018-01-03 13:14:37 +00004587 // Load from an entry in the .data.bimg.rel.ro using a PC-relative load.
4588 // Used for app->boot calls with relocatable image.
4589 kBootImageRelRo,
4590
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004591 // Load from an entry in the .bss section using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004592 // Used for methods outside boot image referenced by AOT-compiled app and boot image code.
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004593 kBssEntry,
Vladimir Marko58155012015-08-19 12:49:41 +00004594
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004595 // Use ArtMethod* at a known address, embed the direct address in the code.
4596 // Used for for JIT-compiled calls.
4597 kJitDirectAddress,
4598
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004599 // Make a runtime call to resolve and call the method. This is the last-resort-kind
4600 // used when other kinds are unimplemented on a particular architecture.
4601 kRuntimeCall,
Vladimir Marko58155012015-08-19 12:49:41 +00004602 };
4603
4604 // Determines the location of the code pointer.
4605 enum class CodePtrLocation {
4606 // Recursive call, use local PC-relative call instruction.
4607 kCallSelf,
4608
Vladimir Marko58155012015-08-19 12:49:41 +00004609 // Use code pointer from the ArtMethod*.
4610 // Used when we don't know the target code. This is also the last-resort-kind used when
4611 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
4612 kCallArtMethod,
4613 };
4614
4615 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01004616 MethodLoadKind method_load_kind;
4617 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00004618 // The method load data holds
4619 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
4620 // Note that there are multiple string init methods, each having its own offset.
4621 // - the method address for kDirectAddress
Vladimir Markodc151b22015-10-15 18:02:30 +01004622 uint64_t method_load_data;
Vladimir Marko58155012015-08-19 12:49:41 +00004623 };
4624
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004625 HInvokeStaticOrDirect(ArenaAllocator* allocator,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004626 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004627 DataType::Type return_type,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004628 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004629 uint32_t method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004630 ArtMethod* resolved_method,
Vladimir Marko58155012015-08-19 12:49:41 +00004631 DispatchInfo dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004632 InvokeType invoke_type,
4633 MethodReference target_method,
Roland Levillain4c0eb422015-04-24 16:43:49 +01004634 ClinitCheckRequirement clinit_check_requirement)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304635 : HInvoke(kInvokeStaticOrDirect,
4636 allocator,
Roland Levillain3e3d7332015-04-28 11:00:54 +01004637 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00004638 // There is potentially one extra argument for the HCurrentMethod node, and
Nicolas Geoffray0846a8f2018-09-12 15:21:07 +01004639 // potentially one other if the clinit check is explicit.
Vladimir Markob554b5a2015-11-06 12:57:55 +00004640 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
David Brazdildee58d62016-04-07 09:54:26 +00004641 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004642 return_type,
4643 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004644 method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004645 resolved_method,
4646 invoke_type),
Vladimir Marko58155012015-08-19 12:49:41 +00004647 target_method_(target_method),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004648 dispatch_info_(dispatch_info) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004649 SetPackedField<ClinitCheckRequirementField>(clinit_check_requirement);
4650 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004651
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004652 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004653
Vladimir Markodc151b22015-10-15 18:02:30 +01004654 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00004655 bool had_current_method_input = HasCurrentMethodInput();
4656 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
4657
4658 // Using the current method is the default and once we find a better
4659 // method load kind, we should not go back to using the current method.
4660 DCHECK(had_current_method_input || !needs_current_method_input);
4661
4662 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004663 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
4664 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004665 }
Vladimir Markodc151b22015-10-15 18:02:30 +01004666 dispatch_info_ = dispatch_info;
4667 }
4668
Aart Bik6daebeb2017-04-03 14:35:41 -07004669 DispatchInfo GetDispatchInfo() const {
4670 return dispatch_info_;
4671 }
4672
Vladimir Markoc53c0792015-11-19 15:48:33 +00004673 void AddSpecialInput(HInstruction* input) {
4674 // We allow only one special input.
4675 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
4676 DCHECK(InputCount() == GetSpecialInputIndex() ||
4677 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
4678 InsertInputAt(GetSpecialInputIndex(), input);
4679 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004680
Vladimir Marko372f10e2016-05-17 16:30:10 +01004681 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004682 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01004683 ArrayRef<HUserRecord<HInstruction*>> input_records = HInvoke::GetInputRecords();
4684 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck()) {
4685 DCHECK(!input_records.empty());
4686 DCHECK_GT(input_records.size(), GetNumberOfArguments());
4687 HInstruction* last_input = input_records.back().GetInstruction();
4688 // Note: `last_input` may be null during arguments setup.
4689 if (last_input != nullptr) {
4690 // `last_input` is the last input of a static invoke marked as having
4691 // an explicit clinit check. It must either be:
4692 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
4693 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
4694 DCHECK(last_input->IsClinitCheck() || last_input->IsLoadClass()) << last_input->DebugName();
4695 }
4696 }
4697 return input_records;
4698 }
4699
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004700 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const override {
Calin Juravle77520bc2015-01-12 18:45:46 +00004701 // We access the method via the dex cache so we can't do an implicit null check.
4702 // TODO: for intrinsics we can generate implicit null checks.
4703 return false;
4704 }
4705
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004706 bool CanBeNull() const override {
Vladimir Markobd785672018-05-03 17:09:09 +01004707 return GetType() == DataType::Type::kReference && !IsStringInit();
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004708 }
4709
Vladimir Markoc53c0792015-11-19 15:48:33 +00004710 // Get the index of the special input, if any.
4711 //
David Brazdil6de19382016-01-08 17:37:10 +00004712 // If the invoke HasCurrentMethodInput(), the "special input" is the current
4713 // method pointer; otherwise there may be one platform-specific special input,
4714 // such as PC-relative addressing base.
Vladimir Markoc53c0792015-11-19 15:48:33 +00004715 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray97793072016-02-16 15:33:54 +00004716 bool HasSpecialInput() const { return GetNumberOfArguments() != InputCount(); }
Vladimir Markoc53c0792015-11-19 15:48:33 +00004717
Vladimir Marko58155012015-08-19 12:49:41 +00004718 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
4719 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
4720 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004721 bool NeedsDexCacheOfDeclaringClass() const override;
Vladimir Marko58155012015-08-19 12:49:41 +00004722 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004723 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kJitDirectAddress; }
Vladimir Marko65979462017-05-19 17:25:12 +01004724 bool HasPcRelativeMethodLoadKind() const {
4725 return GetMethodLoadKind() == MethodLoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markob066d432018-01-03 13:14:37 +00004726 GetMethodLoadKind() == MethodLoadKind::kBootImageRelRo ||
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004727 GetMethodLoadKind() == MethodLoadKind::kBssEntry;
Vladimir Marko65979462017-05-19 17:25:12 +01004728 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004729 bool HasCurrentMethodInput() const {
4730 // This function can be called only after the invoke has been fully initialized by the builder.
4731 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004732 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004733 return true;
4734 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004735 DCHECK(InputCount() == GetSpecialInputIndex() ||
4736 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004737 return false;
4738 }
4739 }
Vladimir Marko58155012015-08-19 12:49:41 +00004740
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004741 QuickEntrypointEnum GetStringInitEntryPoint() const {
Vladimir Marko58155012015-08-19 12:49:41 +00004742 DCHECK(IsStringInit());
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004743 return static_cast<QuickEntrypointEnum>(dispatch_info_.method_load_data);
Vladimir Marko58155012015-08-19 12:49:41 +00004744 }
4745
4746 uint64_t GetMethodAddress() const {
4747 DCHECK(HasMethodAddress());
4748 return dispatch_info_.method_load_data;
4749 }
4750
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00004751 const DexFile& GetDexFileForPcRelativeDexCache() const;
4752
Vladimir Markoa1de9182016-02-25 11:37:38 +00004753 ClinitCheckRequirement GetClinitCheckRequirement() const {
4754 return GetPackedField<ClinitCheckRequirementField>();
4755 }
Calin Juravle68ad6492015-08-18 17:08:12 +01004756
Roland Levillain4c0eb422015-04-24 16:43:49 +01004757 // Is this instruction a call to a static method?
4758 bool IsStatic() const {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004759 return GetInvokeType() == kStatic;
4760 }
4761
4762 MethodReference GetTargetMethod() const {
4763 return target_method_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01004764 }
4765
Vladimir Markofbb184a2015-11-13 14:47:00 +00004766 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
4767 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
4768 // instruction; only relevant for static calls with explicit clinit check.
4769 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01004770 DCHECK(IsStaticWithExplicitClinitCheck());
Vladimir Marko372f10e2016-05-17 16:30:10 +01004771 size_t last_input_index = inputs_.size() - 1u;
4772 HInstruction* last_input = inputs_.back().GetInstruction();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004773 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004774 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004775 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004776 inputs_.pop_back();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004777 SetPackedField<ClinitCheckRequirementField>(new_requirement);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004778 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004779 }
4780
4781 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00004782 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01004783 bool IsStaticWithExplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004784 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kExplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004785 }
4786
4787 // Is this a call to a static method whose declaring class has an
4788 // implicit intialization check requirement?
4789 bool IsStaticWithImplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004790 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kImplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004791 }
4792
Vladimir Markob554b5a2015-11-06 12:57:55 +00004793 // Does this method load kind need the current method as an input?
4794 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004795 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kRuntimeCall;
Vladimir Markob554b5a2015-11-06 12:57:55 +00004796 }
4797
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004798 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004799
Artem Serovcced8ba2017-07-19 18:18:09 +01004800 protected:
4801 DEFAULT_COPY_CONSTRUCTOR(InvokeStaticOrDirect);
4802
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004803 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004804 static constexpr size_t kFieldClinitCheckRequirement = kNumberOfInvokePackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004805 static constexpr size_t kFieldClinitCheckRequirementSize =
4806 MinimumBitsToStore(static_cast<size_t>(ClinitCheckRequirement::kLast));
4807 static constexpr size_t kNumberOfInvokeStaticOrDirectPackedBits =
4808 kFieldClinitCheckRequirement + kFieldClinitCheckRequirementSize;
4809 static_assert(kNumberOfInvokeStaticOrDirectPackedBits <= kMaxNumberOfPackedBits,
4810 "Too many packed fields.");
Vladimir Markoa1de9182016-02-25 11:37:38 +00004811 using ClinitCheckRequirementField = BitField<ClinitCheckRequirement,
4812 kFieldClinitCheckRequirement,
4813 kFieldClinitCheckRequirementSize>;
4814
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004815 // Cached values of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffrayea179f42018-02-08 22:30:18 +00004816 const MethodReference target_method_;
Vladimir Marko58155012015-08-19 12:49:41 +00004817 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004818};
Vladimir Markof64242a2015-12-01 14:58:23 +00004819std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004820std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004821
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004822class HInvokeVirtual final : public HInvoke {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004823 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004824 HInvokeVirtual(ArenaAllocator* allocator,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004825 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004826 DataType::Type return_type,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004827 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004828 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004829 ArtMethod* resolved_method,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004830 uint32_t vtable_index)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304831 : HInvoke(kInvokeVirtual,
4832 allocator,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004833 number_of_arguments,
4834 0u,
4835 return_type,
4836 dex_pc,
4837 dex_method_index,
4838 resolved_method,
4839 kVirtual),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304840 vtable_index_(vtable_index) {
4841 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004842
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004843 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004844
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004845 bool CanBeNull() const override {
Aart Bik71bf7b42016-11-16 10:17:46 -08004846 switch (GetIntrinsic()) {
4847 case Intrinsics::kThreadCurrentThread:
4848 case Intrinsics::kStringBufferAppend:
4849 case Intrinsics::kStringBufferToString:
Vladimir Markod4561172017-10-30 17:48:25 +00004850 case Intrinsics::kStringBuilderAppendObject:
4851 case Intrinsics::kStringBuilderAppendString:
4852 case Intrinsics::kStringBuilderAppendCharSequence:
4853 case Intrinsics::kStringBuilderAppendCharArray:
4854 case Intrinsics::kStringBuilderAppendBoolean:
4855 case Intrinsics::kStringBuilderAppendChar:
4856 case Intrinsics::kStringBuilderAppendInt:
4857 case Intrinsics::kStringBuilderAppendLong:
4858 case Intrinsics::kStringBuilderAppendFloat:
4859 case Intrinsics::kStringBuilderAppendDouble:
Aart Bik71bf7b42016-11-16 10:17:46 -08004860 case Intrinsics::kStringBuilderToString:
4861 return false;
4862 default:
4863 return HInvoke::CanBeNull();
4864 }
4865 }
4866
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004867 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Calin Juravle77520bc2015-01-12 18:45:46 +00004868 // TODO: Add implicit null checks in intrinsics.
Nicolas Geoffray61ba8d22018-08-07 09:55:57 +01004869 return (obj == InputAt(0)) && !IsIntrinsic();
Calin Juravle77520bc2015-01-12 18:45:46 +00004870 }
4871
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004872 uint32_t GetVTableIndex() const { return vtable_index_; }
4873
4874 DECLARE_INSTRUCTION(InvokeVirtual);
4875
Artem Serovcced8ba2017-07-19 18:18:09 +01004876 protected:
4877 DEFAULT_COPY_CONSTRUCTOR(InvokeVirtual);
4878
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004879 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004880 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004881 const uint32_t vtable_index_;
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004882};
4883
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004884class HInvokeInterface final : public HInvoke {
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004885 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004886 HInvokeInterface(ArenaAllocator* allocator,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004887 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004888 DataType::Type return_type,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004889 uint32_t dex_pc,
4890 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004891 ArtMethod* resolved_method,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004892 uint32_t imt_index)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304893 : HInvoke(kInvokeInterface,
4894 allocator,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004895 number_of_arguments,
4896 0u,
4897 return_type,
4898 dex_pc,
4899 dex_method_index,
4900 resolved_method,
4901 kInterface),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304902 imt_index_(imt_index) {
4903 }
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004904
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004905 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004906
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004907 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Calin Juravle77520bc2015-01-12 18:45:46 +00004908 // TODO: Add implicit null checks in intrinsics.
Nicolas Geoffray61ba8d22018-08-07 09:55:57 +01004909 return (obj == InputAt(0)) && !IsIntrinsic();
Calin Juravle77520bc2015-01-12 18:45:46 +00004910 }
4911
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004912 bool NeedsDexCacheOfDeclaringClass() const override {
Nicolas Geoffrayfbdfa6d2017-02-03 10:43:13 +00004913 // The assembly stub currently needs it.
4914 return true;
4915 }
4916
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004917 uint32_t GetImtIndex() const { return imt_index_; }
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004918
4919 DECLARE_INSTRUCTION(InvokeInterface);
4920
Artem Serovcced8ba2017-07-19 18:18:09 +01004921 protected:
4922 DEFAULT_COPY_CONSTRUCTOR(InvokeInterface);
4923
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004924 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004925 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004926 const uint32_t imt_index_;
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004927};
4928
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004929class HNeg final : public HUnaryOperation {
Roland Levillain88cb1752014-10-20 16:36:47 +01004930 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004931 HNeg(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304932 : HUnaryOperation(kNeg, result_type, input, dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004933 DCHECK_EQ(result_type, DataType::Kind(input->GetType()));
Roland Levillain937e6cd2016-03-22 11:54:37 +00004934 }
Roland Levillain88cb1752014-10-20 16:36:47 +01004935
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004936 template <typename T> static T Compute(T x) { return -x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004937
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004938 HConstant* Evaluate(HIntConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004939 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004940 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004941 HConstant* Evaluate(HLongConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004942 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004943 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004944 HConstant* Evaluate(HFloatConstant* x) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004945 return GetBlock()->GetGraph()->GetFloatConstant(Compute(x->GetValue()), GetDexPc());
4946 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004947 HConstant* Evaluate(HDoubleConstant* x) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004948 return GetBlock()->GetGraph()->GetDoubleConstant(Compute(x->GetValue()), GetDexPc());
4949 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01004950
Roland Levillain88cb1752014-10-20 16:36:47 +01004951 DECLARE_INSTRUCTION(Neg);
4952
Artem Serovcced8ba2017-07-19 18:18:09 +01004953 protected:
4954 DEFAULT_COPY_CONSTRUCTOR(Neg);
Roland Levillain88cb1752014-10-20 16:36:47 +01004955};
4956
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004957class HNewArray final : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004958 public:
Vladimir Markob5461632018-10-15 14:24:21 +01004959 HNewArray(HInstruction* cls, HInstruction* length, uint32_t dex_pc, size_t component_size_shift)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304960 : HExpression(kNewArray, DataType::Type::kReference, SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004961 SetRawInputAt(0, cls);
4962 SetRawInputAt(1, length);
Vladimir Markob5461632018-10-15 14:24:21 +01004963 SetPackedField<ComponentSizeShiftField>(component_size_shift);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004964 }
4965
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004966 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004967
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004968 // Calls runtime so needs an environment.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004969 bool NeedsEnvironment() const override { return true; }
Calin Juravle10e244f2015-01-26 18:54:32 +00004970
Mingyao Yang0c365e62015-03-31 15:09:29 -07004971 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004972 bool CanThrow() const override { return true; }
Mingyao Yang0c365e62015-03-31 15:09:29 -07004973
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004974 bool CanBeNull() const override { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004975
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004976 HLoadClass* GetLoadClass() const {
4977 DCHECK(InputAt(0)->IsLoadClass());
4978 return InputAt(0)->AsLoadClass();
4979 }
4980
4981 HInstruction* GetLength() const {
4982 return InputAt(1);
4983 }
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004984
Vladimir Markob5461632018-10-15 14:24:21 +01004985 size_t GetComponentSizeShift() {
4986 return GetPackedField<ComponentSizeShiftField>();
4987 }
4988
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004989 DECLARE_INSTRUCTION(NewArray);
4990
Artem Serovcced8ba2017-07-19 18:18:09 +01004991 protected:
4992 DEFAULT_COPY_CONSTRUCTOR(NewArray);
Vladimir Markob5461632018-10-15 14:24:21 +01004993
4994 private:
4995 static constexpr size_t kFieldComponentSizeShift = kNumberOfGenericPackedBits;
4996 static constexpr size_t kFieldComponentSizeShiftSize = MinimumBitsToStore(3u);
4997 static constexpr size_t kNumberOfNewArrayPackedBits =
4998 kFieldComponentSizeShift + kFieldComponentSizeShiftSize;
4999 static_assert(kNumberOfNewArrayPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5000 using ComponentSizeShiftField =
5001 BitField<size_t, kFieldComponentSizeShift, kFieldComponentSizeShift>;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01005002};
5003
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005004class HAdd final : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00005005 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005006 HAdd(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005007 HInstruction* left,
5008 HInstruction* right,
5009 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305010 : HBinaryOperation(kAdd, result_type, left, right, SideEffects::None(), dex_pc) {
5011 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00005012
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005013 bool IsCommutative() const override { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00005014
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005015 template <typename T> static T Compute(T x, T y) { return x + y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005016
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005017 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005018 return GetBlock()->GetGraph()->GetIntConstant(
5019 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01005020 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005021 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005022 return GetBlock()->GetGraph()->GetLongConstant(
5023 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01005024 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005025 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005026 return GetBlock()->GetGraph()->GetFloatConstant(
5027 Compute(x->GetValue(), y->GetValue()), GetDexPc());
5028 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005029 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005030 return GetBlock()->GetGraph()->GetDoubleConstant(
5031 Compute(x->GetValue(), y->GetValue()), GetDexPc());
5032 }
Roland Levillain556c3d12014-09-18 15:25:07 +01005033
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00005034 DECLARE_INSTRUCTION(Add);
5035
Artem Serovcced8ba2017-07-19 18:18:09 +01005036 protected:
5037 DEFAULT_COPY_CONSTRUCTOR(Add);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00005038};
5039
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005040class HSub final : public HBinaryOperation {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005041 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005042 HSub(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005043 HInstruction* left,
5044 HInstruction* right,
5045 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305046 : HBinaryOperation(kSub, result_type, left, right, SideEffects::None(), dex_pc) {
5047 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005048
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005049 template <typename T> static T Compute(T x, T y) { return x - y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005050
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005051 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005052 return GetBlock()->GetGraph()->GetIntConstant(
5053 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01005054 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005055 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005056 return GetBlock()->GetGraph()->GetLongConstant(
5057 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01005058 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005059 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005060 return GetBlock()->GetGraph()->GetFloatConstant(
5061 Compute(x->GetValue(), y->GetValue()), GetDexPc());
5062 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005063 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005064 return GetBlock()->GetGraph()->GetDoubleConstant(
5065 Compute(x->GetValue(), y->GetValue()), GetDexPc());
5066 }
Roland Levillain556c3d12014-09-18 15:25:07 +01005067
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005068 DECLARE_INSTRUCTION(Sub);
5069
Artem Serovcced8ba2017-07-19 18:18:09 +01005070 protected:
5071 DEFAULT_COPY_CONSTRUCTOR(Sub);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005072};
5073
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005074class HMul final : public HBinaryOperation {
Calin Juravle34bacdf2014-10-07 20:23:36 +01005075 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005076 HMul(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005077 HInstruction* left,
5078 HInstruction* right,
5079 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305080 : HBinaryOperation(kMul, result_type, left, right, SideEffects::None(), dex_pc) {
5081 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01005082
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005083 bool IsCommutative() const override { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01005084
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005085 template <typename T> static T Compute(T x, T y) { return x * y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005086
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005087 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005088 return GetBlock()->GetGraph()->GetIntConstant(
5089 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005090 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005091 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005092 return GetBlock()->GetGraph()->GetLongConstant(
5093 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005094 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005095 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005096 return GetBlock()->GetGraph()->GetFloatConstant(
5097 Compute(x->GetValue(), y->GetValue()), GetDexPc());
5098 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005099 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005100 return GetBlock()->GetGraph()->GetDoubleConstant(
5101 Compute(x->GetValue(), y->GetValue()), GetDexPc());
5102 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01005103
5104 DECLARE_INSTRUCTION(Mul);
5105
Artem Serovcced8ba2017-07-19 18:18:09 +01005106 protected:
5107 DEFAULT_COPY_CONSTRUCTOR(Mul);
Calin Juravle34bacdf2014-10-07 20:23:36 +01005108};
5109
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005110class HDiv final : public HBinaryOperation {
Calin Juravle7c4954d2014-10-28 16:57:40 +00005111 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005112 HDiv(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005113 HInstruction* left,
5114 HInstruction* right,
5115 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305116 : HBinaryOperation(kDiv, result_type, left, right, SideEffects::None(), dex_pc) {
5117 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00005118
Roland Levillain9867bc72015-08-05 10:21:34 +01005119 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00005120 T ComputeIntegral(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005121 DCHECK(!DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01005122 // Our graph structure ensures we never have 0 for `y` during
5123 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00005124 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00005125 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00005126 return (y == -1) ? -x : x / y;
5127 }
Calin Juravlebacfec32014-11-14 15:54:36 +00005128
Roland Levillain31dd3d62016-02-16 12:21:02 +00005129 template <typename T>
5130 T ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005131 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00005132 return x / y;
5133 }
5134
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005135 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005136 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00005137 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005138 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005139 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005140 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00005141 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5142 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005143 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005144 return GetBlock()->GetGraph()->GetFloatConstant(
5145 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
5146 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005147 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005148 return GetBlock()->GetGraph()->GetDoubleConstant(
5149 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00005150 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00005151
5152 DECLARE_INSTRUCTION(Div);
5153
Artem Serovcced8ba2017-07-19 18:18:09 +01005154 protected:
5155 DEFAULT_COPY_CONSTRUCTOR(Div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00005156};
5157
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005158class HRem final : public HBinaryOperation {
Calin Juravlebacfec32014-11-14 15:54:36 +00005159 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005160 HRem(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005161 HInstruction* left,
5162 HInstruction* right,
5163 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305164 : HBinaryOperation(kRem, result_type, left, right, SideEffects::None(), dex_pc) {
5165 }
Calin Juravlebacfec32014-11-14 15:54:36 +00005166
Roland Levillain9867bc72015-08-05 10:21:34 +01005167 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00005168 T ComputeIntegral(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005169 DCHECK(!DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01005170 // Our graph structure ensures we never have 0 for `y` during
5171 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00005172 DCHECK_NE(y, 0);
5173 // Special case -1 to avoid getting a SIGFPE on x86(_64).
5174 return (y == -1) ? 0 : x % y;
5175 }
5176
Roland Levillain31dd3d62016-02-16 12:21:02 +00005177 template <typename T>
5178 T ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005179 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00005180 return std::fmod(x, y);
5181 }
5182
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005183 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005184 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00005185 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005186 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005187 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005188 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00005189 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00005190 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005191 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005192 return GetBlock()->GetGraph()->GetFloatConstant(
5193 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
5194 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005195 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005196 return GetBlock()->GetGraph()->GetDoubleConstant(
5197 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
5198 }
Calin Juravlebacfec32014-11-14 15:54:36 +00005199
5200 DECLARE_INSTRUCTION(Rem);
5201
Artem Serovcced8ba2017-07-19 18:18:09 +01005202 protected:
5203 DEFAULT_COPY_CONSTRUCTOR(Rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00005204};
5205
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005206class HMin final : public HBinaryOperation {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005207 public:
5208 HMin(DataType::Type result_type,
5209 HInstruction* left,
5210 HInstruction* right,
5211 uint32_t dex_pc)
5212 : HBinaryOperation(kMin, result_type, left, right, SideEffects::None(), dex_pc) {}
5213
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005214 bool IsCommutative() const override { return true; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005215
5216 // Evaluation for integral values.
5217 template <typename T> static T ComputeIntegral(T x, T y) {
5218 return (x <= y) ? x : y;
5219 }
5220
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005221 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005222 return GetBlock()->GetGraph()->GetIntConstant(
5223 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5224 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005225 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005226 return GetBlock()->GetGraph()->GetLongConstant(
5227 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5228 }
5229 // TODO: Evaluation for floating-point values.
5230 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005231 HFloatConstant* y ATTRIBUTE_UNUSED) const override { return nullptr; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005232 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005233 HDoubleConstant* y ATTRIBUTE_UNUSED) const override { return nullptr; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005234
5235 DECLARE_INSTRUCTION(Min);
5236
5237 protected:
5238 DEFAULT_COPY_CONSTRUCTOR(Min);
5239};
5240
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005241class HMax final : public HBinaryOperation {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005242 public:
5243 HMax(DataType::Type result_type,
5244 HInstruction* left,
5245 HInstruction* right,
5246 uint32_t dex_pc)
5247 : HBinaryOperation(kMax, result_type, left, right, SideEffects::None(), dex_pc) {}
5248
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005249 bool IsCommutative() const override { return true; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005250
5251 // Evaluation for integral values.
5252 template <typename T> static T ComputeIntegral(T x, T y) {
5253 return (x >= y) ? x : y;
5254 }
5255
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005256 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005257 return GetBlock()->GetGraph()->GetIntConstant(
5258 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5259 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005260 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005261 return GetBlock()->GetGraph()->GetLongConstant(
5262 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5263 }
5264 // TODO: Evaluation for floating-point values.
5265 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005266 HFloatConstant* y ATTRIBUTE_UNUSED) const override { return nullptr; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005267 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005268 HDoubleConstant* y ATTRIBUTE_UNUSED) const override { return nullptr; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005269
5270 DECLARE_INSTRUCTION(Max);
5271
5272 protected:
5273 DEFAULT_COPY_CONSTRUCTOR(Max);
5274};
5275
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005276class HAbs final : public HUnaryOperation {
Aart Bik3dad3412018-02-28 12:01:46 -08005277 public:
5278 HAbs(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
5279 : HUnaryOperation(kAbs, result_type, input, dex_pc) {}
5280
5281 // Evaluation for integral values.
5282 template <typename T> static T ComputeIntegral(T x) {
5283 return x < 0 ? -x : x;
5284 }
5285
5286 // Evaluation for floating-point values.
5287 // Note, as a "quality of implementation", rather than pure "spec compliance",
5288 // we require that Math.abs() clears the sign bit (but changes nothing else)
5289 // for all floating-point numbers, including NaN (signaling NaN may become quiet though).
5290 // http://b/30758343
5291 template <typename T, typename S> static T ComputeFP(T x) {
5292 S bits = bit_cast<S, T>(x);
5293 return bit_cast<T, S>(bits & std::numeric_limits<S>::max());
5294 }
5295
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005296 HConstant* Evaluate(HIntConstant* x) const override {
Aart Bik3dad3412018-02-28 12:01:46 -08005297 return GetBlock()->GetGraph()->GetIntConstant(ComputeIntegral(x->GetValue()), GetDexPc());
5298 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005299 HConstant* Evaluate(HLongConstant* x) const override {
Aart Bik3dad3412018-02-28 12:01:46 -08005300 return GetBlock()->GetGraph()->GetLongConstant(ComputeIntegral(x->GetValue()), GetDexPc());
5301 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005302 HConstant* Evaluate(HFloatConstant* x) const override {
Aart Bik3dad3412018-02-28 12:01:46 -08005303 return GetBlock()->GetGraph()->GetFloatConstant(
5304 ComputeFP<float, int32_t>(x->GetValue()), GetDexPc());
5305 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005306 HConstant* Evaluate(HDoubleConstant* x) const override {
Aart Bik3dad3412018-02-28 12:01:46 -08005307 return GetBlock()->GetGraph()->GetDoubleConstant(
5308 ComputeFP<double, int64_t>(x->GetValue()), GetDexPc());
5309 }
5310
5311 DECLARE_INSTRUCTION(Abs);
5312
5313 protected:
5314 DEFAULT_COPY_CONSTRUCTOR(Abs);
5315};
5316
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005317class HDivZeroCheck final : public HExpression<1> {
Calin Juravled0d48522014-11-04 16:40:20 +00005318 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00005319 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
Artem Serovd1aa7d02018-06-22 11:35:46 +01005320 // constructor. However it can only do it on a fatal slow path so execution never returns to the
5321 // instruction following the current one; thus 'SideEffects::None()' is used.
Calin Juravled0d48522014-11-04 16:40:20 +00005322 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Artem Serovd1aa7d02018-06-22 11:35:46 +01005323 : HExpression(kDivZeroCheck, value->GetType(), SideEffects::None(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00005324 SetRawInputAt(0, value);
5325 }
5326
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005327 bool IsClonable() const override { return true; }
5328 bool CanBeMoved() const override { return true; }
Calin Juravled0d48522014-11-04 16:40:20 +00005329
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005330 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Calin Juravled0d48522014-11-04 16:40:20 +00005331 return true;
5332 }
5333
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005334 bool NeedsEnvironment() const override { return true; }
5335 bool CanThrow() const override { return true; }
Calin Juravled0d48522014-11-04 16:40:20 +00005336
Calin Juravled0d48522014-11-04 16:40:20 +00005337 DECLARE_INSTRUCTION(DivZeroCheck);
5338
Artem Serovcced8ba2017-07-19 18:18:09 +01005339 protected:
5340 DEFAULT_COPY_CONSTRUCTOR(DivZeroCheck);
Calin Juravled0d48522014-11-04 16:40:20 +00005341};
5342
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005343class HShl final : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005344 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005345 HShl(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005346 HInstruction* value,
5347 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005348 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305349 : HBinaryOperation(kShl, result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005350 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5351 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01005352 }
5353
Roland Levillain5b5b9312016-03-22 14:57:31 +00005354 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005355 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005356 return value << (distance & max_shift_distance);
5357 }
5358
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005359 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005360 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005361 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005362 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005363 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005364 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005365 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005366 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005367 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005368 HLongConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005369 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5370 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01005371 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005372 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005373 HFloatConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005374 LOG(FATAL) << DebugName() << " is not defined for float values";
5375 UNREACHABLE();
5376 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005377 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005378 HDoubleConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005379 LOG(FATAL) << DebugName() << " is not defined for double values";
5380 UNREACHABLE();
5381 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005382
5383 DECLARE_INSTRUCTION(Shl);
5384
Artem Serovcced8ba2017-07-19 18:18:09 +01005385 protected:
5386 DEFAULT_COPY_CONSTRUCTOR(Shl);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005387};
5388
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005389class HShr final : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005390 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005391 HShr(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005392 HInstruction* value,
5393 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005394 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305395 : HBinaryOperation(kShr, result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005396 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5397 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01005398 }
5399
Roland Levillain5b5b9312016-03-22 14:57:31 +00005400 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005401 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005402 return value >> (distance & max_shift_distance);
5403 }
5404
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005405 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005406 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005407 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005408 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005409 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005410 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005411 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005412 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005413 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005414 HLongConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005415 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5416 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01005417 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005418 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005419 HFloatConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005420 LOG(FATAL) << DebugName() << " is not defined for float values";
5421 UNREACHABLE();
5422 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005423 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005424 HDoubleConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005425 LOG(FATAL) << DebugName() << " is not defined for double values";
5426 UNREACHABLE();
5427 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005428
5429 DECLARE_INSTRUCTION(Shr);
5430
Artem Serovcced8ba2017-07-19 18:18:09 +01005431 protected:
5432 DEFAULT_COPY_CONSTRUCTOR(Shr);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005433};
5434
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005435class HUShr final : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005436 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005437 HUShr(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005438 HInstruction* value,
5439 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005440 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305441 : HBinaryOperation(kUShr, result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005442 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5443 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Calin Juravle9aec02f2014-11-18 23:06:35 +00005444 }
5445
Roland Levillain5b5b9312016-03-22 14:57:31 +00005446 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005447 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005448 typedef typename std::make_unsigned<T>::type V;
5449 V ux = static_cast<V>(value);
5450 return static_cast<T>(ux >> (distance & max_shift_distance));
5451 }
5452
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005453 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005454 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005455 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005456 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005457 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005458 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005459 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005460 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005461 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005462 HLongConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005463 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5464 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00005465 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005466 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005467 HFloatConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005468 LOG(FATAL) << DebugName() << " is not defined for float values";
5469 UNREACHABLE();
5470 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005471 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005472 HDoubleConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005473 LOG(FATAL) << DebugName() << " is not defined for double values";
5474 UNREACHABLE();
5475 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005476
5477 DECLARE_INSTRUCTION(UShr);
5478
Artem Serovcced8ba2017-07-19 18:18:09 +01005479 protected:
5480 DEFAULT_COPY_CONSTRUCTOR(UShr);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005481};
5482
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005483class HAnd final : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005484 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005485 HAnd(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005486 HInstruction* left,
5487 HInstruction* right,
5488 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305489 : HBinaryOperation(kAnd, result_type, left, right, SideEffects::None(), dex_pc) {
5490 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005491
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005492 bool IsCommutative() const override { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005493
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005494 template <typename T> static T Compute(T x, T y) { return x & y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005495
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005496 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005497 return GetBlock()->GetGraph()->GetIntConstant(
5498 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005499 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005500 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005501 return GetBlock()->GetGraph()->GetLongConstant(
5502 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005503 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005504 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005505 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005506 LOG(FATAL) << DebugName() << " is not defined for float values";
5507 UNREACHABLE();
5508 }
5509 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005510 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005511 LOG(FATAL) << DebugName() << " is not defined for double values";
5512 UNREACHABLE();
5513 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005514
5515 DECLARE_INSTRUCTION(And);
5516
Artem Serovcced8ba2017-07-19 18:18:09 +01005517 protected:
5518 DEFAULT_COPY_CONSTRUCTOR(And);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005519};
5520
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005521class HOr final : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005522 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005523 HOr(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005524 HInstruction* left,
5525 HInstruction* right,
5526 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305527 : HBinaryOperation(kOr, result_type, left, right, SideEffects::None(), dex_pc) {
5528 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005529
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005530 bool IsCommutative() const override { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005531
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005532 template <typename T> static T Compute(T x, T y) { return x | y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005533
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005534 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005535 return GetBlock()->GetGraph()->GetIntConstant(
5536 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005537 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005538 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005539 return GetBlock()->GetGraph()->GetLongConstant(
5540 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005541 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005542 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005543 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005544 LOG(FATAL) << DebugName() << " is not defined for float values";
5545 UNREACHABLE();
5546 }
5547 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005548 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005549 LOG(FATAL) << DebugName() << " is not defined for double values";
5550 UNREACHABLE();
5551 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005552
5553 DECLARE_INSTRUCTION(Or);
5554
Artem Serovcced8ba2017-07-19 18:18:09 +01005555 protected:
5556 DEFAULT_COPY_CONSTRUCTOR(Or);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005557};
5558
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005559class HXor final : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005560 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005561 HXor(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005562 HInstruction* left,
5563 HInstruction* right,
5564 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305565 : HBinaryOperation(kXor, result_type, left, right, SideEffects::None(), dex_pc) {
5566 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005567
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005568 bool IsCommutative() const override { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005569
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005570 template <typename T> static T Compute(T x, T y) { return x ^ y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005571
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005572 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005573 return GetBlock()->GetGraph()->GetIntConstant(
5574 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005575 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005576 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005577 return GetBlock()->GetGraph()->GetLongConstant(
5578 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005579 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005580 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005581 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005582 LOG(FATAL) << DebugName() << " is not defined for float values";
5583 UNREACHABLE();
5584 }
5585 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005586 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005587 LOG(FATAL) << DebugName() << " is not defined for double values";
5588 UNREACHABLE();
5589 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005590
5591 DECLARE_INSTRUCTION(Xor);
5592
Artem Serovcced8ba2017-07-19 18:18:09 +01005593 protected:
5594 DEFAULT_COPY_CONSTRUCTOR(Xor);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005595};
5596
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005597class HRor final : public HBinaryOperation {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005598 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005599 HRor(DataType::Type result_type, HInstruction* value, HInstruction* distance)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305600 : HBinaryOperation(kRor, result_type, value, distance) {
Roland Levillain22c49222016-03-18 14:04:28 +00005601 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005602
Roland Levillain5b5b9312016-03-22 14:57:31 +00005603 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005604 static T Compute(T value, int32_t distance, int32_t max_shift_value) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005605 typedef typename std::make_unsigned<T>::type V;
5606 V ux = static_cast<V>(value);
5607 if ((distance & max_shift_value) == 0) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005608 return static_cast<T>(ux);
5609 } else {
5610 const V reg_bits = sizeof(T) * 8;
Roland Levillain5b5b9312016-03-22 14:57:31 +00005611 return static_cast<T>(ux >> (distance & max_shift_value)) |
5612 (value << (reg_bits - (distance & max_shift_value)));
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005613 }
5614 }
5615
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005616 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const override {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005617 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005618 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005619 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005620 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const override {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005621 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005622 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005623 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005624 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005625 HLongConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005626 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5627 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005628 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005629 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005630 HFloatConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005631 LOG(FATAL) << DebugName() << " is not defined for float values";
5632 UNREACHABLE();
5633 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005634 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005635 HDoubleConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005636 LOG(FATAL) << DebugName() << " is not defined for double values";
5637 UNREACHABLE();
5638 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005639
5640 DECLARE_INSTRUCTION(Ror);
5641
Artem Serovcced8ba2017-07-19 18:18:09 +01005642 protected:
5643 DEFAULT_COPY_CONSTRUCTOR(Ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005644};
5645
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005646// The value of a parameter in this method. Its location depends on
5647// the calling convention.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005648class HParameterValue final : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005649 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005650 HParameterValue(const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08005651 dex::TypeIndex type_index,
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005652 uint8_t index,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005653 DataType::Type parameter_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005654 bool is_this = false)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305655 : HExpression(kParameterValue, parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005656 dex_file_(dex_file),
5657 type_index_(type_index),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005658 index_(index) {
5659 SetPackedFlag<kFlagIsThis>(is_this);
5660 SetPackedFlag<kFlagCanBeNull>(!is_this);
5661 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005662
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005663 const DexFile& GetDexFile() const { return dex_file_; }
Andreas Gampea5b09a62016-11-17 15:21:22 -08005664 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005665 uint8_t GetIndex() const { return index_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07005666 bool IsThis() const { return GetPackedFlag<kFlagIsThis>(); }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005667
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005668 bool CanBeNull() const override { return GetPackedFlag<kFlagCanBeNull>(); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005669 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00005670
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005671 DECLARE_INSTRUCTION(ParameterValue);
5672
Artem Serovcced8ba2017-07-19 18:18:09 +01005673 protected:
5674 DEFAULT_COPY_CONSTRUCTOR(ParameterValue);
5675
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005676 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005677 // Whether or not the parameter value corresponds to 'this' argument.
Vladimir Markobd785672018-05-03 17:09:09 +01005678 static constexpr size_t kFlagIsThis = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005679 static constexpr size_t kFlagCanBeNull = kFlagIsThis + 1;
5680 static constexpr size_t kNumberOfParameterValuePackedBits = kFlagCanBeNull + 1;
5681 static_assert(kNumberOfParameterValuePackedBits <= kMaxNumberOfPackedBits,
5682 "Too many packed fields.");
5683
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005684 const DexFile& dex_file_;
Andreas Gampea5b09a62016-11-17 15:21:22 -08005685 const dex::TypeIndex type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005686 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00005687 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005688 const uint8_t index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005689};
5690
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005691class HNot final : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005692 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005693 HNot(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305694 : HUnaryOperation(kNot, result_type, input, dex_pc) {
5695 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005696
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005697 bool CanBeMoved() const override { return true; }
5698 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005699 return true;
5700 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005701
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005702 template <typename T> static T Compute(T x) { return ~x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005703
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005704 HConstant* Evaluate(HIntConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005705 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005706 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005707 HConstant* Evaluate(HLongConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005708 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005709 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005710 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005711 LOG(FATAL) << DebugName() << " is not defined for float values";
5712 UNREACHABLE();
5713 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005714 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005715 LOG(FATAL) << DebugName() << " is not defined for double values";
5716 UNREACHABLE();
5717 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01005718
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005719 DECLARE_INSTRUCTION(Not);
5720
Artem Serovcced8ba2017-07-19 18:18:09 +01005721 protected:
5722 DEFAULT_COPY_CONSTRUCTOR(Not);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005723};
5724
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005725class HBooleanNot final : public HUnaryOperation {
David Brazdil66d126e2015-04-03 16:02:44 +01005726 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005727 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305728 : HUnaryOperation(kBooleanNot, DataType::Type::kBool, input, dex_pc) {
5729 }
David Brazdil66d126e2015-04-03 16:02:44 +01005730
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005731 bool CanBeMoved() const override { return true; }
5732 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
David Brazdil66d126e2015-04-03 16:02:44 +01005733 return true;
5734 }
5735
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005736 template <typename T> static bool Compute(T x) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005737 DCHECK(IsUint<1>(x)) << x;
David Brazdil66d126e2015-04-03 16:02:44 +01005738 return !x;
5739 }
5740
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005741 HConstant* Evaluate(HIntConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005742 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005743 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005744 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005745 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01005746 UNREACHABLE();
5747 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005748 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005749 LOG(FATAL) << DebugName() << " is not defined for float values";
5750 UNREACHABLE();
5751 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005752 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005753 LOG(FATAL) << DebugName() << " is not defined for double values";
5754 UNREACHABLE();
5755 }
David Brazdil66d126e2015-04-03 16:02:44 +01005756
5757 DECLARE_INSTRUCTION(BooleanNot);
5758
Artem Serovcced8ba2017-07-19 18:18:09 +01005759 protected:
5760 DEFAULT_COPY_CONSTRUCTOR(BooleanNot);
David Brazdil66d126e2015-04-03 16:02:44 +01005761};
5762
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005763class HTypeConversion final : public HExpression<1> {
Roland Levillaindff1f282014-11-05 14:15:05 +00005764 public:
5765 // Instantiate a type conversion of `input` to `result_type`.
Vladimir Markoc8fb2112017-10-03 11:37:52 +01005766 HTypeConversion(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305767 : HExpression(kTypeConversion, result_type, SideEffects::None(), dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00005768 SetRawInputAt(0, input);
Roland Levillainf355c3f2016-03-30 19:09:03 +01005769 // Invariant: We should never generate a conversion to a Boolean value.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005770 DCHECK_NE(DataType::Type::kBool, result_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00005771 }
5772
5773 HInstruction* GetInput() const { return InputAt(0); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005774 DataType::Type GetInputType() const { return GetInput()->GetType(); }
5775 DataType::Type GetResultType() const { return GetType(); }
Roland Levillaindff1f282014-11-05 14:15:05 +00005776
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005777 bool IsClonable() const override { return true; }
5778 bool CanBeMoved() const override { return true; }
5779 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01005780 return true;
5781 }
Nicolas Geoffrayacc56ac2018-10-09 08:45:24 +01005782 // Return whether the conversion is implicit. This includes conversion to the same type.
5783 bool IsImplicitConversion() const {
5784 return DataType::IsTypeConversionImplicit(GetInputType(), GetResultType());
5785 }
Roland Levillaindff1f282014-11-05 14:15:05 +00005786
Mark Mendelle82549b2015-05-06 10:55:34 -04005787 // Try to statically evaluate the conversion and return a HConstant
5788 // containing the result. If the input cannot be converted, return nullptr.
5789 HConstant* TryStaticEvaluation() const;
5790
Roland Levillaindff1f282014-11-05 14:15:05 +00005791 DECLARE_INSTRUCTION(TypeConversion);
5792
Artem Serovcced8ba2017-07-19 18:18:09 +01005793 protected:
5794 DEFAULT_COPY_CONSTRUCTOR(TypeConversion);
Roland Levillaindff1f282014-11-05 14:15:05 +00005795};
5796
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00005797static constexpr uint32_t kNoRegNumber = -1;
5798
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005799class HNullCheck final : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005800 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005801 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
Artem Serovd1aa7d02018-06-22 11:35:46 +01005802 // constructor. However it can only do it on a fatal slow path so execution never returns to the
5803 // instruction following the current one; thus 'SideEffects::None()' is used.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005804 HNullCheck(HInstruction* value, uint32_t dex_pc)
Artem Serovd1aa7d02018-06-22 11:35:46 +01005805 : HExpression(kNullCheck, value->GetType(), SideEffects::None(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005806 SetRawInputAt(0, value);
5807 }
5808
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005809 bool IsClonable() const override { return true; }
5810 bool CanBeMoved() const override { return true; }
5811 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005812 return true;
5813 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005814
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005815 bool NeedsEnvironment() const override { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005816
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005817 bool CanThrow() const override { return true; }
Calin Juravle10e244f2015-01-26 18:54:32 +00005818
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005819 bool CanBeNull() const override { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005820
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005821 DECLARE_INSTRUCTION(NullCheck);
5822
Artem Serovcced8ba2017-07-19 18:18:09 +01005823 protected:
5824 DEFAULT_COPY_CONSTRUCTOR(NullCheck);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005825};
5826
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005827// Embeds an ArtField and all the information required by the compiler. We cache
5828// that information to avoid requiring the mutator lock every time we need it.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005829class FieldInfo : public ValueObject {
5830 public:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005831 FieldInfo(ArtField* field,
5832 MemberOffset field_offset,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005833 DataType::Type field_type,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005834 bool is_volatile,
5835 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005836 uint16_t declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005837 const DexFile& dex_file)
5838 : field_(field),
5839 field_offset_(field_offset),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005840 field_type_(field_type),
5841 is_volatile_(is_volatile),
5842 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005843 declaring_class_def_index_(declaring_class_def_index),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005844 dex_file_(dex_file) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005845
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005846 ArtField* GetField() const { return field_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005847 MemberOffset GetFieldOffset() const { return field_offset_; }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005848 DataType::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005849 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07005850 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005851 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00005852 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005853
5854 private:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005855 ArtField* const field_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005856 const MemberOffset field_offset_;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005857 const DataType::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00005858 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005859 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07005860 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005861 const DexFile& dex_file_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005862};
5863
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005864class HInstanceFieldGet final : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005865 public:
5866 HInstanceFieldGet(HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005867 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005868 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005869 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005870 bool is_volatile,
5871 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005872 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005873 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005874 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305875 : HExpression(kInstanceFieldGet,
5876 field_type,
5877 SideEffects::FieldReadOfType(field_type, is_volatile),
5878 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005879 field_info_(field,
5880 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005881 field_type,
5882 is_volatile,
5883 field_idx,
5884 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005885 dex_file) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005886 SetRawInputAt(0, value);
5887 }
5888
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005889 bool IsClonable() const override { return true; }
5890 bool CanBeMoved() const override { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005891
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005892 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01005893 const HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005894 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005895 }
5896
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005897 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005898 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005899 }
5900
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005901 size_t ComputeHashCode() const override {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01005902 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5903 }
5904
Calin Juravle52c48962014-12-16 17:02:57 +00005905 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005906 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005907 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005908 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005909
Vladimir Marko61b92282017-10-11 13:23:17 +01005910 void SetType(DataType::Type new_type) {
5911 DCHECK(DataType::IsIntegralType(GetType()));
5912 DCHECK(DataType::IsIntegralType(new_type));
5913 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
5914 SetPackedField<TypeField>(new_type);
5915 }
5916
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005917 DECLARE_INSTRUCTION(InstanceFieldGet);
5918
Artem Serovcced8ba2017-07-19 18:18:09 +01005919 protected:
5920 DEFAULT_COPY_CONSTRUCTOR(InstanceFieldGet);
5921
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005922 private:
5923 const FieldInfo field_info_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005924};
5925
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005926class HInstanceFieldSet final : public HExpression<2> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005927 public:
5928 HInstanceFieldSet(HInstruction* object,
5929 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005930 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005931 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005932 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005933 bool is_volatile,
5934 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005935 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005936 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005937 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01005938 : HExpression(kInstanceFieldSet,
5939 SideEffects::FieldWriteOfType(field_type, is_volatile),
5940 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005941 field_info_(field,
5942 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005943 field_type,
5944 is_volatile,
5945 field_idx,
5946 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005947 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005948 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005949 SetRawInputAt(0, object);
5950 SetRawInputAt(1, value);
5951 }
5952
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005953 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01005954
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005955 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005956 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005957 }
5958
Calin Juravle52c48962014-12-16 17:02:57 +00005959 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005960 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005961 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005962 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005963 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005964 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5965 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005966
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005967 DECLARE_INSTRUCTION(InstanceFieldSet);
5968
Artem Serovcced8ba2017-07-19 18:18:09 +01005969 protected:
5970 DEFAULT_COPY_CONSTRUCTOR(InstanceFieldSet);
5971
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005972 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005973 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5974 static constexpr size_t kNumberOfInstanceFieldSetPackedBits = kFlagValueCanBeNull + 1;
5975 static_assert(kNumberOfInstanceFieldSetPackedBits <= kMaxNumberOfPackedBits,
5976 "Too many packed fields.");
5977
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005978 const FieldInfo field_info_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005979};
5980
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005981class HArrayGet final : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005982 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005983 HArrayGet(HInstruction* array,
5984 HInstruction* index,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005985 DataType::Type type,
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005986 uint32_t dex_pc)
5987 : HArrayGet(array,
5988 index,
5989 type,
5990 SideEffects::ArrayReadOfType(type),
5991 dex_pc,
Andreas Gampe3db70682018-12-26 15:12:03 -08005992 /* is_string_char_at= */ false) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305993 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005994
5995 HArrayGet(HInstruction* array,
5996 HInstruction* index,
5997 DataType::Type type,
5998 SideEffects side_effects,
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005999 uint32_t dex_pc,
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006000 bool is_string_char_at)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306001 : HExpression(kArrayGet, type, side_effects, dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006002 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006003 SetRawInputAt(0, array);
6004 SetRawInputAt(1, index);
6005 }
6006
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006007 bool IsClonable() const override { return true; }
6008 bool CanBeMoved() const override { return true; }
6009 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07006010 return true;
6011 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006012 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const override {
Calin Juravle77520bc2015-01-12 18:45:46 +00006013 // TODO: We can be smarter here.
Vladimir Marko66d691d2017-04-07 17:53:39 +01006014 // Currently, unless the array is the result of NewArray, the array access is always
6015 // preceded by some form of null NullCheck necessary for the bounds check, usually
6016 // implicit null check on the ArrayLength input to BoundsCheck or Deoptimize for
6017 // dynamic BCE. There are cases when these could be removed to produce better code.
6018 // If we ever add optimizations to do so we should allow an implicit check here
6019 // (as long as the address falls in the first page).
6020 //
6021 // As an example of such fancy optimization, we could eliminate BoundsCheck for
6022 // a = cond ? new int[1] : null;
6023 // a[0]; // The Phi does not need bounds check for either input.
Calin Juravle77520bc2015-01-12 18:45:46 +00006024 return false;
6025 }
6026
David Brazdil4833f5a2015-12-16 10:37:39 +00006027 bool IsEquivalentOf(HArrayGet* other) const {
6028 bool result = (GetDexPc() == other->GetDexPc());
6029 if (kIsDebugBuild && result) {
6030 DCHECK_EQ(GetBlock(), other->GetBlock());
6031 DCHECK_EQ(GetArray(), other->GetArray());
6032 DCHECK_EQ(GetIndex(), other->GetIndex());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006033 if (DataType::IsIntOrLongType(GetType())) {
6034 DCHECK(DataType::IsFloatingPointType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00006035 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006036 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
6037 DCHECK(DataType::IsIntOrLongType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00006038 }
6039 }
6040 return result;
6041 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01006042
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006043 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
6044
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006045 HInstruction* GetArray() const { return InputAt(0); }
6046 HInstruction* GetIndex() const { return InputAt(1); }
6047
Vladimir Marko61b92282017-10-11 13:23:17 +01006048 void SetType(DataType::Type new_type) {
6049 DCHECK(DataType::IsIntegralType(GetType()));
6050 DCHECK(DataType::IsIntegralType(new_type));
6051 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
6052 SetPackedField<TypeField>(new_type);
6053 }
6054
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006055 DECLARE_INSTRUCTION(ArrayGet);
6056
Artem Serovcced8ba2017-07-19 18:18:09 +01006057 protected:
6058 DEFAULT_COPY_CONSTRUCTOR(ArrayGet);
6059
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006060 private:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006061 // We treat a String as an array, creating the HArrayGet from String.charAt()
6062 // intrinsic in the instruction simplifier. We can always determine whether
6063 // a particular HArrayGet is actually a String.charAt() by looking at the type
6064 // of the input but that requires holding the mutator lock, so we prefer to use
6065 // a flag, so that code generators don't need to do the locking.
Vladimir Markobd785672018-05-03 17:09:09 +01006066 static constexpr size_t kFlagIsStringCharAt = kNumberOfGenericPackedBits;
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006067 static constexpr size_t kNumberOfArrayGetPackedBits = kFlagIsStringCharAt + 1;
6068 static_assert(kNumberOfArrayGetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6069 "Too many packed fields.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006070};
6071
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006072class HArraySet final : public HExpression<3> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006073 public:
6074 HArraySet(HInstruction* array,
6075 HInstruction* index,
6076 HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006077 DataType::Type expected_component_type,
Aart Bik18b36ab2016-04-13 16:41:35 -07006078 uint32_t dex_pc)
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006079 : HArraySet(array,
6080 index,
6081 value,
6082 expected_component_type,
6083 // Make a best guess for side effects now, may be refined during SSA building.
6084 ComputeSideEffects(GetComponentType(value->GetType(), expected_component_type)),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306085 dex_pc) {
6086 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006087
6088 HArraySet(HInstruction* array,
6089 HInstruction* index,
6090 HInstruction* value,
6091 DataType::Type expected_component_type,
6092 SideEffects side_effects,
6093 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006094 : HExpression(kArraySet, side_effects, dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006095 SetPackedField<ExpectedComponentTypeField>(expected_component_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006096 SetPackedFlag<kFlagNeedsTypeCheck>(value->GetType() == DataType::Type::kReference);
Vladimir Markoa1de9182016-02-25 11:37:38 +00006097 SetPackedFlag<kFlagValueCanBeNull>(true);
6098 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(false);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006099 SetRawInputAt(0, array);
6100 SetRawInputAt(1, index);
6101 SetRawInputAt(2, value);
6102 }
6103
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006104 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01006105
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006106 bool NeedsEnvironment() const override {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006107 // We call a runtime method to throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006108 return NeedsTypeCheck();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006109 }
6110
Mingyao Yang81014cb2015-06-02 03:16:27 -07006111 // Can throw ArrayStoreException.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006112 bool CanThrow() const override { return NeedsTypeCheck(); }
Mingyao Yang81014cb2015-06-02 03:16:27 -07006113
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006114 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const override {
Calin Juravle77520bc2015-01-12 18:45:46 +00006115 // TODO: Same as for ArrayGet.
6116 return false;
6117 }
6118
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006119 void ClearNeedsTypeCheck() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006120 SetPackedFlag<kFlagNeedsTypeCheck>(false);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006121 }
6122
Nicolas Geoffray07276db2015-05-18 14:22:09 +01006123 void ClearValueCanBeNull() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006124 SetPackedFlag<kFlagValueCanBeNull>(false);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01006125 }
6126
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006127 void SetStaticTypeOfArrayIsObjectArray() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006128 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(true);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006129 }
6130
Vladimir Markoa1de9182016-02-25 11:37:38 +00006131 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
6132 bool NeedsTypeCheck() const { return GetPackedFlag<kFlagNeedsTypeCheck>(); }
6133 bool StaticTypeOfArrayIsObjectArray() const {
6134 return GetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>();
6135 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006136
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006137 HInstruction* GetArray() const { return InputAt(0); }
6138 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006139 HInstruction* GetValue() const { return InputAt(2); }
6140
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006141 DataType::Type GetComponentType() const {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006142 return GetComponentType(GetValue()->GetType(), GetRawExpectedComponentType());
6143 }
6144
6145 static DataType::Type GetComponentType(DataType::Type value_type,
6146 DataType::Type expected_component_type) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006147 // The Dex format does not type floating point index operations. Since the
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006148 // `expected_component_type` comes from SSA building and can therefore not
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006149 // be correct, we also check what is the value type. If it is a floating
6150 // point type, we must use that type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006151 return ((value_type == DataType::Type::kFloat32) || (value_type == DataType::Type::kFloat64))
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006152 ? value_type
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006153 : expected_component_type;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006154 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01006155
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006156 DataType::Type GetRawExpectedComponentType() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006157 return GetPackedField<ExpectedComponentTypeField>();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01006158 }
6159
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006160 static SideEffects ComputeSideEffects(DataType::Type type) {
6161 return SideEffects::ArrayWriteOfType(type).Union(SideEffectsForArchRuntimeCalls(type));
Aart Bik18b36ab2016-04-13 16:41:35 -07006162 }
6163
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006164 static SideEffects SideEffectsForArchRuntimeCalls(DataType::Type value_type) {
6165 return (value_type == DataType::Type::kReference) ? SideEffects::CanTriggerGC()
6166 : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006167 }
6168
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006169 DECLARE_INSTRUCTION(ArraySet);
6170
Artem Serovcced8ba2017-07-19 18:18:09 +01006171 protected:
6172 DEFAULT_COPY_CONSTRUCTOR(ArraySet);
6173
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006174 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006175 static constexpr size_t kFieldExpectedComponentType = kNumberOfGenericPackedBits;
6176 static constexpr size_t kFieldExpectedComponentTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006177 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00006178 static constexpr size_t kFlagNeedsTypeCheck =
6179 kFieldExpectedComponentType + kFieldExpectedComponentTypeSize;
6180 static constexpr size_t kFlagValueCanBeNull = kFlagNeedsTypeCheck + 1;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006181 // Cached information for the reference_type_info_ so that codegen
6182 // does not need to inspect the static type.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006183 static constexpr size_t kFlagStaticTypeOfArrayIsObjectArray = kFlagValueCanBeNull + 1;
6184 static constexpr size_t kNumberOfArraySetPackedBits =
6185 kFlagStaticTypeOfArrayIsObjectArray + 1;
6186 static_assert(kNumberOfArraySetPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6187 using ExpectedComponentTypeField =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006188 BitField<DataType::Type, kFieldExpectedComponentType, kFieldExpectedComponentTypeSize>;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006189};
6190
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006191class HArrayLength final : public HExpression<1> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006192 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006193 HArrayLength(HInstruction* array, uint32_t dex_pc, bool is_string_length = false)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306194 : HExpression(kArrayLength, DataType::Type::kInt32, SideEffects::None(), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006195 SetPackedFlag<kFlagIsStringLength>(is_string_length);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01006196 // Note that arrays do not change length, so the instruction does not
6197 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006198 SetRawInputAt(0, array);
6199 }
6200
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006201 bool IsClonable() const override { return true; }
6202 bool CanBeMoved() const override { return true; }
6203 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07006204 return true;
6205 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006206 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Calin Juravle641547a2015-04-21 22:08:51 +01006207 return obj == InputAt(0);
6208 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01006209
Vladimir Markodce016e2016-04-28 13:10:02 +01006210 bool IsStringLength() const { return GetPackedFlag<kFlagIsStringLength>(); }
6211
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006212 DECLARE_INSTRUCTION(ArrayLength);
6213
Artem Serovcced8ba2017-07-19 18:18:09 +01006214 protected:
6215 DEFAULT_COPY_CONSTRUCTOR(ArrayLength);
6216
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006217 private:
Vladimir Markodce016e2016-04-28 13:10:02 +01006218 // We treat a String as an array, creating the HArrayLength from String.length()
6219 // or String.isEmpty() intrinsic in the instruction simplifier. We can always
6220 // determine whether a particular HArrayLength is actually a String.length() by
6221 // looking at the type of the input but that requires holding the mutator lock, so
6222 // we prefer to use a flag, so that code generators don't need to do the locking.
Vladimir Markobd785672018-05-03 17:09:09 +01006223 static constexpr size_t kFlagIsStringLength = kNumberOfGenericPackedBits;
Vladimir Markodce016e2016-04-28 13:10:02 +01006224 static constexpr size_t kNumberOfArrayLengthPackedBits = kFlagIsStringLength + 1;
6225 static_assert(kNumberOfArrayLengthPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6226 "Too many packed fields.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006227};
6228
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006229class HBoundsCheck final : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006230 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00006231 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
Artem Serovd1aa7d02018-06-22 11:35:46 +01006232 // constructor. However it can only do it on a fatal slow path so execution never returns to the
6233 // instruction following the current one; thus 'SideEffects::None()' is used.
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006234 HBoundsCheck(HInstruction* index,
6235 HInstruction* length,
6236 uint32_t dex_pc,
Vladimir Marko0259c242017-12-04 11:27:47 +00006237 bool is_string_char_at = false)
Artem Serovd1aa7d02018-06-22 11:35:46 +01006238 : HExpression(kBoundsCheck, index->GetType(), SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006239 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(index->GetType()));
Vladimir Marko0259c242017-12-04 11:27:47 +00006240 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006241 SetRawInputAt(0, index);
6242 SetRawInputAt(1, length);
6243 }
6244
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006245 bool IsClonable() const override { return true; }
6246 bool CanBeMoved() const override { return true; }
6247 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07006248 return true;
6249 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01006250
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006251 bool NeedsEnvironment() const override { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006252
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006253 bool CanThrow() const override { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01006254
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00006255 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006256
Alexandre Ramese6dbf482015-10-19 10:10:41 +01006257 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006258
6259 DECLARE_INSTRUCTION(BoundsCheck);
6260
Artem Serovcced8ba2017-07-19 18:18:09 +01006261 protected:
6262 DEFAULT_COPY_CONSTRUCTOR(BoundsCheck);
6263
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006264 private:
Vladimir Markobd785672018-05-03 17:09:09 +01006265 static constexpr size_t kFlagIsStringCharAt = kNumberOfGenericPackedBits;
Vladimir Markof02046e2018-10-02 15:31:32 +01006266 static constexpr size_t kNumberOfBoundsCheckPackedBits = kFlagIsStringCharAt + 1;
6267 static_assert(kNumberOfBoundsCheckPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6268 "Too many packed fields.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006269};
6270
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006271class HSuspendCheck final : public HExpression<0> {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006272 public:
David Brazdil86ea7ee2016-02-16 09:26:07 +00006273 explicit HSuspendCheck(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01006274 : HExpression(kSuspendCheck, SideEffects::CanTriggerGC(), dex_pc),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306275 slow_path_(nullptr) {
6276 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006277
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006278 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01006279
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006280 bool NeedsEnvironment() const override {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006281 return true;
6282 }
6283
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006284 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
6285 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006286
6287 DECLARE_INSTRUCTION(SuspendCheck);
6288
Artem Serovcced8ba2017-07-19 18:18:09 +01006289 protected:
6290 DEFAULT_COPY_CONSTRUCTOR(SuspendCheck);
6291
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006292 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006293 // Only used for code generation, in order to share the same slow path between back edges
6294 // of a same loop.
6295 SlowPathCode* slow_path_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006296};
6297
David Srbecky0cf44932015-12-09 14:09:59 +00006298// Pseudo-instruction which provides the native debugger with mapping information.
6299// It ensures that we can generate line number and local variables at this point.
Vladimir Markobd785672018-05-03 17:09:09 +01006300class HNativeDebugInfo : public HExpression<0> {
David Srbecky0cf44932015-12-09 14:09:59 +00006301 public:
6302 explicit HNativeDebugInfo(uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006303 : HExpression<0>(kNativeDebugInfo, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306304 }
David Srbecky0cf44932015-12-09 14:09:59 +00006305
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006306 bool NeedsEnvironment() const override {
David Srbecky0cf44932015-12-09 14:09:59 +00006307 return true;
6308 }
6309
6310 DECLARE_INSTRUCTION(NativeDebugInfo);
6311
Artem Serovcced8ba2017-07-19 18:18:09 +01006312 protected:
6313 DEFAULT_COPY_CONSTRUCTOR(NativeDebugInfo);
David Srbecky0cf44932015-12-09 14:09:59 +00006314};
6315
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006316/**
6317 * Instruction to load a Class object.
6318 */
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006319class HLoadClass final : public HInstruction {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006320 public:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006321 // Determines how to load the Class.
6322 enum class LoadKind {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006323 // We cannot load this class. See HSharpening::SharpenLoadClass.
6324 kInvalid = -1,
6325
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006326 // Use the Class* from the method's own ArtMethod*.
6327 kReferrersClass,
6328
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006329 // Use PC-relative boot image Class* address that will be known at link time.
Vladimir Marko764d4542017-05-16 10:31:41 +01006330 // Used for boot image classes referenced by boot image code.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006331 kBootImageLinkTimePcRelative,
6332
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006333 // Load from an entry in the .data.bimg.rel.ro using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006334 // Used for boot image classes referenced by apps in AOT-compiled code.
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006335 kBootImageRelRo,
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006336
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006337 // Load from an entry in the .bss section using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006338 // Used for classes outside boot image referenced by AOT-compiled app and boot image code.
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006339 kBssEntry,
6340
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006341 // Use a known boot image Class* address, embedded in the code by the codegen.
6342 // Used for boot image classes referenced by apps in JIT-compiled code.
6343 kJitBootImageAddress,
6344
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006345 // Load from the root table associated with the JIT compiled method.
6346 kJitTableAddress,
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006347
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006348 // Load using a simple runtime call. This is the fall-back load kind when
6349 // the codegen is unable to use another appropriate kind.
6350 kRuntimeCall,
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006351
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006352 kLast = kRuntimeCall
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006353 };
6354
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01006355 HLoadClass(HCurrentMethod* current_method,
Andreas Gampea5b09a62016-11-17 15:21:22 -08006356 dex::TypeIndex type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01006357 const DexFile& dex_file,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006358 Handle<mirror::Class> klass,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006359 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01006360 uint32_t dex_pc,
Nicolas Geoffray56876342016-12-16 16:09:08 +00006361 bool needs_access_check)
Vladimir Markobd785672018-05-03 17:09:09 +01006362 : HInstruction(kLoadClass,
6363 DataType::Type::kReference,
6364 SideEffectsForArchRuntimeCalls(),
6365 dex_pc),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006366 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006367 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01006368 dex_file_(dex_file),
Vladimir Marko175e7862018-03-27 09:03:13 +00006369 klass_(klass) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01006370 // Referrers class should not need access check. We never inline unverified
6371 // methods so we can't possibly end up in this situation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006372 DCHECK(!is_referrers_class || !needs_access_check);
6373
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006374 SetPackedField<LoadKindField>(
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006375 is_referrers_class ? LoadKind::kReferrersClass : LoadKind::kRuntimeCall);
Vladimir Markoa1de9182016-02-25 11:37:38 +00006376 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
Nicolas Geoffray56876342016-12-16 16:09:08 +00006377 SetPackedFlag<kFlagIsInBootImage>(false);
Vladimir Markoa1de9182016-02-25 11:37:38 +00006378 SetPackedFlag<kFlagGenerateClInitCheck>(false);
Vladimir Marko175e7862018-03-27 09:03:13 +00006379 SetPackedFlag<kFlagValidLoadedClassRTI>(false);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006380 }
6381
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006382 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01006383
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006384 void SetLoadKind(LoadKind load_kind);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006385
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006386 LoadKind GetLoadKind() const {
6387 return GetPackedField<LoadKindField>();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01006388 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006389
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006390 bool HasPcRelativeLoadKind() const {
6391 return GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
6392 GetLoadKind() == LoadKind::kBootImageRelRo ||
6393 GetLoadKind() == LoadKind::kBssEntry;
6394 }
6395
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006396 bool CanBeMoved() const override { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006397
Yi Kong39402542019-03-24 02:47:16 -07006398 bool InstructionDataEquals(const HInstruction* other) const override;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006399
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006400 size_t ComputeHashCode() const override { return type_index_.index_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006401
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006402 bool CanBeNull() const override { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006403
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006404 bool NeedsEnvironment() const override {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006405 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006406 }
6407
Calin Juravle0ba218d2015-05-19 18:46:01 +01006408 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00006409 // The entrypoint the code generator is going to call does not do
6410 // clinit of the class.
6411 DCHECK(!NeedsAccessCheck());
Vladimir Markoa1de9182016-02-25 11:37:38 +00006412 SetPackedFlag<kFlagGenerateClInitCheck>(generate_clinit_check);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006413 }
6414
6415 bool CanCallRuntime() const {
Vladimir Marko41559982017-01-06 14:04:23 +00006416 return NeedsAccessCheck() ||
6417 MustGenerateClinitCheck() ||
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006418 GetLoadKind() == LoadKind::kRuntimeCall ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006419 GetLoadKind() == LoadKind::kBssEntry;
Calin Juravle98893e12015-10-02 21:05:03 +01006420 }
6421
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006422 bool CanThrow() const override {
Vladimir Marko41559982017-01-06 14:04:23 +00006423 return NeedsAccessCheck() ||
6424 MustGenerateClinitCheck() ||
6425 // If the class is in the boot image, the lookup in the runtime call cannot throw.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006426 ((GetLoadKind() == LoadKind::kRuntimeCall ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006427 GetLoadKind() == LoadKind::kBssEntry) &&
6428 !IsInBootImage());
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006429 }
6430
Calin Juravleacf735c2015-02-12 15:25:22 +00006431 ReferenceTypeInfo GetLoadedClassRTI() {
Vladimir Marko175e7862018-03-27 09:03:13 +00006432 if (GetPackedFlag<kFlagValidLoadedClassRTI>()) {
6433 // Note: The is_exact flag from the return value should not be used.
Andreas Gampe3db70682018-12-26 15:12:03 -08006434 return ReferenceTypeInfo::CreateUnchecked(klass_, /* is_exact= */ true);
Vladimir Marko175e7862018-03-27 09:03:13 +00006435 } else {
6436 return ReferenceTypeInfo::CreateInvalid();
6437 }
Calin Juravleacf735c2015-02-12 15:25:22 +00006438 }
6439
Vladimir Marko175e7862018-03-27 09:03:13 +00006440 // Loaded class RTI is marked as valid by RTP if the klass_ is admissible.
6441 void SetValidLoadedClassRTI() REQUIRES_SHARED(Locks::mutator_lock_) {
6442 DCHECK(klass_ != nullptr);
6443 SetPackedFlag<kFlagValidLoadedClassRTI>(true);
Calin Juravleacf735c2015-02-12 15:25:22 +00006444 }
6445
Andreas Gampea5b09a62016-11-17 15:21:22 -08006446 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006447 const DexFile& GetDexFile() const { return dex_file_; }
6448
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006449 bool NeedsDexCacheOfDeclaringClass() const override {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006450 return GetLoadKind() == LoadKind::kRuntimeCall;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006451 }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00006452
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006453 static SideEffects SideEffectsForArchRuntimeCalls() {
6454 return SideEffects::CanTriggerGC();
6455 }
6456
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006457 bool IsReferrersClass() const { return GetLoadKind() == LoadKind::kReferrersClass; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006458 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006459 bool IsInBootImage() const { return GetPackedFlag<kFlagIsInBootImage>(); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006460 bool MustGenerateClinitCheck() const { return GetPackedFlag<kFlagGenerateClInitCheck>(); }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006461
Vladimir Markoa9f303c2018-07-20 16:43:56 +01006462 bool MustResolveTypeOnSlowPath() const {
6463 // Check that this instruction has a slow path.
6464 DCHECK(GetLoadKind() != LoadKind::kRuntimeCall); // kRuntimeCall calls on main path.
6465 DCHECK(GetLoadKind() == LoadKind::kBssEntry || MustGenerateClinitCheck());
6466 return GetLoadKind() == LoadKind::kBssEntry;
6467 }
6468
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006469 void MarkInBootImage() {
6470 SetPackedFlag<kFlagIsInBootImage>(true);
6471 }
6472
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006473 void AddSpecialInput(HInstruction* special_input);
6474
6475 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01006476 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006477 return ArrayRef<HUserRecord<HInstruction*>>(
6478 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
6479 }
6480
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006481 Handle<mirror::Class> GetClass() const {
6482 return klass_;
6483 }
6484
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006485 DECLARE_INSTRUCTION(LoadClass);
6486
Artem Serovcced8ba2017-07-19 18:18:09 +01006487 protected:
6488 DEFAULT_COPY_CONSTRUCTOR(LoadClass);
6489
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006490 private:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006491 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfGenericPackedBits;
Vladimir Marko41559982017-01-06 14:04:23 +00006492 static constexpr size_t kFlagIsInBootImage = kFlagNeedsAccessCheck + 1;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006493 // Whether this instruction must generate the initialization check.
6494 // Used for code generation.
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006495 static constexpr size_t kFlagGenerateClInitCheck = kFlagIsInBootImage + 1;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006496 static constexpr size_t kFieldLoadKind = kFlagGenerateClInitCheck + 1;
6497 static constexpr size_t kFieldLoadKindSize =
6498 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
Vladimir Marko175e7862018-03-27 09:03:13 +00006499 static constexpr size_t kFlagValidLoadedClassRTI = kFieldLoadKind + kFieldLoadKindSize;
6500 static constexpr size_t kNumberOfLoadClassPackedBits = kFlagValidLoadedClassRTI + 1;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006501 static_assert(kNumberOfLoadClassPackedBits < kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006502 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
6503
6504 static bool HasTypeReference(LoadKind load_kind) {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006505 return load_kind == LoadKind::kReferrersClass ||
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006506 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006507 load_kind == LoadKind::kBssEntry ||
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006508 load_kind == LoadKind::kRuntimeCall;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006509 }
6510
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006511 void SetLoadKindInternal(LoadKind load_kind);
6512
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006513 // The special input is the HCurrentMethod for kRuntimeCall or kReferrersClass.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006514 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006515 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006516 HUserRecord<HInstruction*> special_input_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006517
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006518 // A type index and dex file where the class can be accessed. The dex file can be:
6519 // - The compiling method's dex file if the class is defined there too.
6520 // - The compiling method's dex file if the class is referenced there.
6521 // - The dex file where the class is defined. When the load kind can only be
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006522 // kBssEntry or kRuntimeCall, we cannot emit code for this `HLoadClass`.
Andreas Gampea5b09a62016-11-17 15:21:22 -08006523 const dex::TypeIndex type_index_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006524 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006525
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006526 Handle<mirror::Class> klass_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006527};
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006528std::ostream& operator<<(std::ostream& os, HLoadClass::LoadKind rhs);
6529
6530// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
Vladimir Marko28e012a2017-12-07 11:22:59 +00006531inline void HLoadClass::SetLoadKind(LoadKind load_kind) {
6532 // The load kind should be determined before inserting the instruction to the graph.
6533 DCHECK(GetBlock() == nullptr);
6534 DCHECK(GetEnvironment() == nullptr);
6535 SetPackedField<LoadKindField>(load_kind);
6536 if (load_kind != LoadKind::kRuntimeCall && load_kind != LoadKind::kReferrersClass) {
6537 special_input_ = HUserRecord<HInstruction*>(nullptr);
6538 }
6539 if (!NeedsEnvironment()) {
6540 SetSideEffects(SideEffects::None());
6541 }
6542}
6543
6544// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006545inline void HLoadClass::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07006546 // The special input is used for PC-relative loads on some architectures,
6547 // including literal pool loads, which are PC-relative too.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006548 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006549 GetLoadKind() == LoadKind::kBootImageRelRo ||
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006550 GetLoadKind() == LoadKind::kBssEntry ||
6551 GetLoadKind() == LoadKind::kJitBootImageAddress) << GetLoadKind();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006552 DCHECK(special_input_.GetInstruction() == nullptr);
6553 special_input_ = HUserRecord<HInstruction*>(special_input);
6554 special_input->AddUseAt(this, 0);
6555}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006556
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006557class HLoadString final : public HInstruction {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006558 public:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006559 // Determines how to load the String.
6560 enum class LoadKind {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006561 // Use PC-relative boot image String* address that will be known at link time.
Vladimir Marko764d4542017-05-16 10:31:41 +01006562 // Used for boot image strings referenced by boot image code.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006563 kBootImageLinkTimePcRelative,
6564
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006565 // Load from an entry in the .data.bimg.rel.ro using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006566 // Used for boot image strings referenced by apps in AOT-compiled code.
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006567 kBootImageRelRo,
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006568
Vladimir Markoaad75c62016-10-03 08:46:48 +00006569 // Load from an entry in the .bss section using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006570 // Used for strings outside boot image referenced by AOT-compiled app and boot image code.
Vladimir Markoaad75c62016-10-03 08:46:48 +00006571 kBssEntry,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006572
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006573 // Use a known boot image String* address, embedded in the code by the codegen.
6574 // Used for boot image strings referenced by apps in JIT-compiled code.
6575 kJitBootImageAddress,
6576
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006577 // Load from the root table associated with the JIT compiled method.
6578 kJitTableAddress,
6579
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006580 // Load using a simple runtime call. This is the fall-back load kind when
6581 // the codegen is unable to use another appropriate kind.
6582 kRuntimeCall,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006583
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006584 kLast = kRuntimeCall,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006585 };
6586
Nicolas Geoffray917d0162015-11-24 18:25:35 +00006587 HLoadString(HCurrentMethod* current_method,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006588 dex::StringIndex string_index,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006589 const DexFile& dex_file,
6590 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006591 : HInstruction(kLoadString,
6592 DataType::Type::kReference,
6593 SideEffectsForArchRuntimeCalls(),
6594 dex_pc),
Vladimir Marko372f10e2016-05-17 16:30:10 +01006595 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006596 string_index_(string_index),
6597 dex_file_(dex_file) {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006598 SetPackedField<LoadKindField>(LoadKind::kRuntimeCall);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01006599 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006600
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006601 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01006602
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006603 void SetLoadKind(LoadKind load_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006604
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006605 LoadKind GetLoadKind() const {
6606 return GetPackedField<LoadKindField>();
6607 }
6608
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006609 bool HasPcRelativeLoadKind() const {
6610 return GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
6611 GetLoadKind() == LoadKind::kBootImageRelRo ||
6612 GetLoadKind() == LoadKind::kBssEntry;
6613 }
6614
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006615 const DexFile& GetDexFile() const {
6616 return dex_file_;
6617 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006618
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006619 dex::StringIndex GetStringIndex() const {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006620 return string_index_;
6621 }
6622
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006623 Handle<mirror::String> GetString() const {
6624 return string_;
6625 }
6626
6627 void SetString(Handle<mirror::String> str) {
6628 string_ = str;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006629 }
6630
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006631 bool CanBeMoved() const override { return true; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006632
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006633 bool InstructionDataEquals(const HInstruction* other) const override;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006634
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006635 size_t ComputeHashCode() const override { return string_index_.index_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006636
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006637 // Will call the runtime if we need to load the string through
6638 // the dex cache and the string is not guaranteed to be there yet.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006639 bool NeedsEnvironment() const override {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006640 LoadKind load_kind = GetLoadKind();
Vladimir Marko764d4542017-05-16 10:31:41 +01006641 if (load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006642 load_kind == LoadKind::kBootImageRelRo ||
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006643 load_kind == LoadKind::kJitBootImageAddress ||
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006644 load_kind == LoadKind::kJitTableAddress) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006645 return false;
6646 }
Vladimir Marko4d1be4922017-01-06 14:43:11 +00006647 return true;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006648 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006649
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006650 bool NeedsDexCacheOfDeclaringClass() const override {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006651 return GetLoadKind() == LoadKind::kRuntimeCall;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006652 }
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006653
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006654 bool CanBeNull() const override { return false; }
6655 bool CanThrow() const override { return NeedsEnvironment(); }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006656
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006657 static SideEffects SideEffectsForArchRuntimeCalls() {
6658 return SideEffects::CanTriggerGC();
6659 }
6660
Vladimir Marko372f10e2016-05-17 16:30:10 +01006661 void AddSpecialInput(HInstruction* special_input);
6662
6663 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01006664 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Vladimir Marko372f10e2016-05-17 16:30:10 +01006665 return ArrayRef<HUserRecord<HInstruction*>>(
6666 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006667 }
6668
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006669 DECLARE_INSTRUCTION(LoadString);
6670
Artem Serovcced8ba2017-07-19 18:18:09 +01006671 protected:
6672 DEFAULT_COPY_CONSTRUCTOR(LoadString);
6673
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006674 private:
Vladimir Marko4d1be4922017-01-06 14:43:11 +00006675 static constexpr size_t kFieldLoadKind = kNumberOfGenericPackedBits;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006676 static constexpr size_t kFieldLoadKindSize =
6677 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
6678 static constexpr size_t kNumberOfLoadStringPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006679 static_assert(kNumberOfLoadStringPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006680 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006681
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006682 void SetLoadKindInternal(LoadKind load_kind);
6683
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006684 // The special input is the HCurrentMethod for kRuntimeCall.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006685 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko48886c22017-01-06 11:45:47 +00006686 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Marko372f10e2016-05-17 16:30:10 +01006687 HUserRecord<HInstruction*> special_input_;
6688
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006689 dex::StringIndex string_index_;
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006690 const DexFile& dex_file_;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006691
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006692 Handle<mirror::String> string_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006693};
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006694std::ostream& operator<<(std::ostream& os, HLoadString::LoadKind rhs);
6695
6696// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
Vladimir Marko28e012a2017-12-07 11:22:59 +00006697inline void HLoadString::SetLoadKind(LoadKind load_kind) {
6698 // The load kind should be determined before inserting the instruction to the graph.
6699 DCHECK(GetBlock() == nullptr);
6700 DCHECK(GetEnvironment() == nullptr);
6701 DCHECK_EQ(GetLoadKind(), LoadKind::kRuntimeCall);
6702 SetPackedField<LoadKindField>(load_kind);
6703 if (load_kind != LoadKind::kRuntimeCall) {
6704 special_input_ = HUserRecord<HInstruction*>(nullptr);
6705 }
6706 if (!NeedsEnvironment()) {
6707 SetSideEffects(SideEffects::None());
6708 }
6709}
6710
6711// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006712inline void HLoadString::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07006713 // The special input is used for PC-relative loads on some architectures,
6714 // including literal pool loads, which are PC-relative too.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006715 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006716 GetLoadKind() == LoadKind::kBootImageRelRo ||
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006717 GetLoadKind() == LoadKind::kBssEntry ||
6718 GetLoadKind() == LoadKind::kJitBootImageAddress) << GetLoadKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01006719 // HLoadString::GetInputRecords() returns an empty array at this point,
6720 // so use the GetInputRecords() from the base class to set the input record.
6721 DCHECK(special_input_.GetInstruction() == nullptr);
6722 special_input_ = HUserRecord<HInstruction*>(special_input);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006723 special_input->AddUseAt(this, 0);
6724}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006725
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006726class HLoadMethodHandle final : public HInstruction {
Orion Hodsondbaa5c72018-05-10 08:22:46 +01006727 public:
6728 HLoadMethodHandle(HCurrentMethod* current_method,
Orion Hodson4c8e12e2018-05-18 08:33:20 +01006729 uint16_t method_handle_idx,
6730 const DexFile& dex_file,
6731 uint32_t dex_pc)
Orion Hodsondbaa5c72018-05-10 08:22:46 +01006732 : HInstruction(kLoadMethodHandle,
6733 DataType::Type::kReference,
6734 SideEffectsForArchRuntimeCalls(),
6735 dex_pc),
6736 special_input_(HUserRecord<HInstruction*>(current_method)),
6737 method_handle_idx_(method_handle_idx),
6738 dex_file_(dex_file) {
6739 }
6740
6741 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01006742 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Orion Hodsondbaa5c72018-05-10 08:22:46 +01006743 return ArrayRef<HUserRecord<HInstruction*>>(
6744 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
6745 }
6746
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006747 bool IsClonable() const override { return true; }
Orion Hodsondbaa5c72018-05-10 08:22:46 +01006748
6749 uint16_t GetMethodHandleIndex() const { return method_handle_idx_; }
6750
6751 const DexFile& GetDexFile() const { return dex_file_; }
6752
6753 static SideEffects SideEffectsForArchRuntimeCalls() {
6754 return SideEffects::CanTriggerGC();
6755 }
6756
6757 DECLARE_INSTRUCTION(LoadMethodHandle);
6758
6759 protected:
6760 DEFAULT_COPY_CONSTRUCTOR(LoadMethodHandle);
6761
6762 private:
6763 // The special input is the HCurrentMethod for kRuntimeCall.
6764 HUserRecord<HInstruction*> special_input_;
6765
6766 const uint16_t method_handle_idx_;
6767 const DexFile& dex_file_;
6768};
6769
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006770class HLoadMethodType final : public HInstruction {
Orion Hodson18259d72018-04-12 11:18:23 +01006771 public:
6772 HLoadMethodType(HCurrentMethod* current_method,
Orion Hodson06d10a72018-05-14 08:53:38 +01006773 dex::ProtoIndex proto_index,
Orion Hodson18259d72018-04-12 11:18:23 +01006774 const DexFile& dex_file,
6775 uint32_t dex_pc)
6776 : HInstruction(kLoadMethodType,
6777 DataType::Type::kReference,
6778 SideEffectsForArchRuntimeCalls(),
6779 dex_pc),
6780 special_input_(HUserRecord<HInstruction*>(current_method)),
Orion Hodson06d10a72018-05-14 08:53:38 +01006781 proto_index_(proto_index),
Orion Hodson18259d72018-04-12 11:18:23 +01006782 dex_file_(dex_file) {
6783 }
6784
6785 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01006786 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Orion Hodson18259d72018-04-12 11:18:23 +01006787 return ArrayRef<HUserRecord<HInstruction*>>(
6788 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
6789 }
6790
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006791 bool IsClonable() const override { return true; }
Orion Hodson18259d72018-04-12 11:18:23 +01006792
Orion Hodson06d10a72018-05-14 08:53:38 +01006793 dex::ProtoIndex GetProtoIndex() const { return proto_index_; }
Orion Hodson18259d72018-04-12 11:18:23 +01006794
6795 const DexFile& GetDexFile() const { return dex_file_; }
6796
6797 static SideEffects SideEffectsForArchRuntimeCalls() {
6798 return SideEffects::CanTriggerGC();
6799 }
6800
6801 DECLARE_INSTRUCTION(LoadMethodType);
6802
6803 protected:
6804 DEFAULT_COPY_CONSTRUCTOR(LoadMethodType);
6805
6806 private:
6807 // The special input is the HCurrentMethod for kRuntimeCall.
6808 HUserRecord<HInstruction*> special_input_;
6809
Orion Hodson06d10a72018-05-14 08:53:38 +01006810 const dex::ProtoIndex proto_index_;
Orion Hodson18259d72018-04-12 11:18:23 +01006811 const DexFile& dex_file_;
6812};
6813
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006814/**
6815 * Performs an initialization check on its Class object input.
6816 */
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006817class HClinitCheck final : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006818 public:
Roland Levillain3887c462015-08-12 18:15:42 +01006819 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07006820 : HExpression(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306821 kClinitCheck,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006822 DataType::Type::kReference,
Mingyao Yang217eb062017-12-11 15:20:07 -08006823 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006824 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006825 SetRawInputAt(0, constant);
6826 }
Artem Serova6e26142018-06-19 14:55:17 +01006827 // TODO: Make ClinitCheck clonable.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006828 bool CanBeMoved() const override { return true; }
6829 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006830 return true;
6831 }
6832
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006833 bool NeedsEnvironment() const override {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006834 // May call runtime to initialize the class.
6835 return true;
6836 }
6837
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006838 bool CanThrow() const override { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006839
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00006840 HLoadClass* GetLoadClass() const {
6841 DCHECK(InputAt(0)->IsLoadClass());
6842 return InputAt(0)->AsLoadClass();
6843 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006844
6845 DECLARE_INSTRUCTION(ClinitCheck);
6846
Artem Serovcced8ba2017-07-19 18:18:09 +01006847
6848 protected:
6849 DEFAULT_COPY_CONSTRUCTOR(ClinitCheck);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006850};
6851
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006852class HStaticFieldGet final : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006853 public:
6854 HStaticFieldGet(HInstruction* cls,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006855 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006856 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006857 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006858 bool is_volatile,
6859 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006860 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006861 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01006862 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306863 : HExpression(kStaticFieldGet,
6864 field_type,
6865 SideEffects::FieldReadOfType(field_type, is_volatile),
6866 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006867 field_info_(field,
6868 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006869 field_type,
6870 is_volatile,
6871 field_idx,
6872 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006873 dex_file) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006874 SetRawInputAt(0, cls);
6875 }
6876
Calin Juravle52c48962014-12-16 17:02:57 +00006877
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006878 bool IsClonable() const override { return true; }
6879 bool CanBeMoved() const override { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006880
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006881 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01006882 const HStaticFieldGet* other_get = other->AsStaticFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00006883 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006884 }
6885
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006886 size_t ComputeHashCode() const override {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006887 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
6888 }
6889
Calin Juravle52c48962014-12-16 17:02:57 +00006890 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006891 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006892 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006893 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006894
Vladimir Marko61b92282017-10-11 13:23:17 +01006895 void SetType(DataType::Type new_type) {
6896 DCHECK(DataType::IsIntegralType(GetType()));
6897 DCHECK(DataType::IsIntegralType(new_type));
6898 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
6899 SetPackedField<TypeField>(new_type);
6900 }
6901
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006902 DECLARE_INSTRUCTION(StaticFieldGet);
6903
Artem Serovcced8ba2017-07-19 18:18:09 +01006904 protected:
6905 DEFAULT_COPY_CONSTRUCTOR(StaticFieldGet);
6906
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006907 private:
6908 const FieldInfo field_info_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006909};
6910
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006911class HStaticFieldSet final : public HExpression<2> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006912 public:
6913 HStaticFieldSet(HInstruction* cls,
6914 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006915 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006916 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006917 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006918 bool is_volatile,
6919 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006920 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006921 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01006922 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006923 : HExpression(kStaticFieldSet,
6924 SideEffects::FieldWriteOfType(field_type, is_volatile),
6925 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006926 field_info_(field,
6927 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006928 field_type,
6929 is_volatile,
6930 field_idx,
6931 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006932 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006933 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006934 SetRawInputAt(0, cls);
6935 SetRawInputAt(1, value);
6936 }
6937
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006938 bool IsClonable() const override { return true; }
Calin Juravle52c48962014-12-16 17:02:57 +00006939 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006940 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006941 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006942 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006943
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006944 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006945 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
6946 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006947
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006948 DECLARE_INSTRUCTION(StaticFieldSet);
6949
Artem Serovcced8ba2017-07-19 18:18:09 +01006950 protected:
6951 DEFAULT_COPY_CONSTRUCTOR(StaticFieldSet);
6952
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006953 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006954 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
6955 static constexpr size_t kNumberOfStaticFieldSetPackedBits = kFlagValueCanBeNull + 1;
6956 static_assert(kNumberOfStaticFieldSetPackedBits <= kMaxNumberOfPackedBits,
6957 "Too many packed fields.");
6958
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006959 const FieldInfo field_info_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006960};
6961
Vladimir Marko552a1342017-10-31 10:56:47 +00006962class HStringBuilderAppend final : public HVariableInputSizeInstruction {
6963 public:
6964 HStringBuilderAppend(HIntConstant* format,
6965 uint32_t number_of_arguments,
6966 ArenaAllocator* allocator,
6967 uint32_t dex_pc)
6968 : HVariableInputSizeInstruction(
6969 kStringBuilderAppend,
6970 DataType::Type::kReference,
6971 // The runtime call may read memory from inputs. It never writes outside
6972 // of the newly allocated result object (or newly allocated helper objects).
6973 SideEffects::AllReads().Union(SideEffects::CanTriggerGC()),
6974 dex_pc,
6975 allocator,
6976 number_of_arguments + /* format */ 1u,
6977 kArenaAllocInvokeInputs) {
6978 DCHECK_GE(number_of_arguments, 1u); // There must be something to append.
6979 SetRawInputAt(FormatIndex(), format);
6980 }
6981
6982 void SetArgumentAt(size_t index, HInstruction* argument) {
6983 DCHECK_LE(index, GetNumberOfArguments());
6984 SetRawInputAt(index, argument);
6985 }
6986
6987 // Return the number of arguments, excluding the format.
6988 size_t GetNumberOfArguments() const {
6989 DCHECK_GE(InputCount(), 1u);
6990 return InputCount() - 1u;
6991 }
6992
6993 size_t FormatIndex() const {
6994 return GetNumberOfArguments();
6995 }
6996
6997 HIntConstant* GetFormat() {
6998 return InputAt(FormatIndex())->AsIntConstant();
6999 }
7000
7001 bool NeedsEnvironment() const override { return true; }
7002
7003 bool CanThrow() const override { return true; }
7004
Vladimir Markob1fe5e12020-03-10 14:30:49 +00007005 bool CanBeNull() const override { return false; }
7006
Vladimir Marko552a1342017-10-31 10:56:47 +00007007 DECLARE_INSTRUCTION(StringBuilderAppend);
7008
7009 protected:
7010 DEFAULT_COPY_CONSTRUCTOR(StringBuilderAppend);
7011};
7012
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007013class HUnresolvedInstanceFieldGet final : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01007014 public:
7015 HUnresolvedInstanceFieldGet(HInstruction* obj,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007016 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01007017 uint32_t field_index,
7018 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307019 : HExpression(kUnresolvedInstanceFieldGet,
7020 field_type,
7021 SideEffects::AllExceptGCDependency(),
7022 dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01007023 field_index_(field_index) {
7024 SetRawInputAt(0, obj);
7025 }
7026
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007027 bool IsClonable() const override { return true; }
7028 bool NeedsEnvironment() const override { return true; }
7029 bool CanThrow() const override { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01007030
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007031 DataType::Type GetFieldType() const { return GetType(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01007032 uint32_t GetFieldIndex() const { return field_index_; }
7033
7034 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
7035
Artem Serovcced8ba2017-07-19 18:18:09 +01007036 protected:
7037 DEFAULT_COPY_CONSTRUCTOR(UnresolvedInstanceFieldGet);
7038
Calin Juravlee460d1d2015-09-29 04:52:17 +01007039 private:
7040 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01007041};
7042
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007043class HUnresolvedInstanceFieldSet final : public HExpression<2> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01007044 public:
7045 HUnresolvedInstanceFieldSet(HInstruction* obj,
7046 HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007047 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01007048 uint32_t field_index,
7049 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01007050 : HExpression(kUnresolvedInstanceFieldSet, SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01007051 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007052 SetPackedField<FieldTypeField>(field_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007053 DCHECK_EQ(DataType::Kind(field_type), DataType::Kind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01007054 SetRawInputAt(0, obj);
7055 SetRawInputAt(1, value);
7056 }
7057
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007058 bool IsClonable() const override { return true; }
7059 bool NeedsEnvironment() const override { return true; }
7060 bool CanThrow() const override { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01007061
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007062 DataType::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01007063 uint32_t GetFieldIndex() const { return field_index_; }
7064
7065 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
7066
Artem Serovcced8ba2017-07-19 18:18:09 +01007067 protected:
7068 DEFAULT_COPY_CONSTRUCTOR(UnresolvedInstanceFieldSet);
7069
Calin Juravlee460d1d2015-09-29 04:52:17 +01007070 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007071 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
7072 static constexpr size_t kFieldFieldTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007073 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00007074 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
7075 kFieldFieldType + kFieldFieldTypeSize;
7076 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
7077 "Too many packed fields.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007078 using FieldTypeField = BitField<DataType::Type, kFieldFieldType, kFieldFieldTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00007079
Calin Juravlee460d1d2015-09-29 04:52:17 +01007080 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01007081};
7082
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007083class HUnresolvedStaticFieldGet final : public HExpression<0> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01007084 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007085 HUnresolvedStaticFieldGet(DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01007086 uint32_t field_index,
7087 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307088 : HExpression(kUnresolvedStaticFieldGet,
7089 field_type,
7090 SideEffects::AllExceptGCDependency(),
7091 dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01007092 field_index_(field_index) {
7093 }
7094
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007095 bool IsClonable() const override { return true; }
7096 bool NeedsEnvironment() const override { return true; }
7097 bool CanThrow() const override { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01007098
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007099 DataType::Type GetFieldType() const { return GetType(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01007100 uint32_t GetFieldIndex() const { return field_index_; }
7101
7102 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
7103
Artem Serovcced8ba2017-07-19 18:18:09 +01007104 protected:
7105 DEFAULT_COPY_CONSTRUCTOR(UnresolvedStaticFieldGet);
7106
Calin Juravlee460d1d2015-09-29 04:52:17 +01007107 private:
7108 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01007109};
7110
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007111class HUnresolvedStaticFieldSet final : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01007112 public:
7113 HUnresolvedStaticFieldSet(HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007114 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01007115 uint32_t field_index,
7116 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01007117 : HExpression(kUnresolvedStaticFieldSet, SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01007118 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007119 SetPackedField<FieldTypeField>(field_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007120 DCHECK_EQ(DataType::Kind(field_type), DataType::Kind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01007121 SetRawInputAt(0, value);
7122 }
7123
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007124 bool IsClonable() const override { return true; }
7125 bool NeedsEnvironment() const override { return true; }
7126 bool CanThrow() const override { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01007127
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007128 DataType::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01007129 uint32_t GetFieldIndex() const { return field_index_; }
7130
7131 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
7132
Artem Serovcced8ba2017-07-19 18:18:09 +01007133 protected:
7134 DEFAULT_COPY_CONSTRUCTOR(UnresolvedStaticFieldSet);
7135
Calin Juravlee460d1d2015-09-29 04:52:17 +01007136 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007137 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
7138 static constexpr size_t kFieldFieldTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007139 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00007140 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
7141 kFieldFieldType + kFieldFieldTypeSize;
7142 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
7143 "Too many packed fields.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007144 using FieldTypeField = BitField<DataType::Type, kFieldFieldType, kFieldFieldTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00007145
Calin Juravlee460d1d2015-09-29 04:52:17 +01007146 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01007147};
7148
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007149// Implement the move-exception DEX instruction.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007150class HLoadException final : public HExpression<0> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007151 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06007152 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307153 : HExpression(kLoadException, DataType::Type::kReference, SideEffects::None(), dex_pc) {
7154 }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007155
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007156 bool CanBeNull() const override { return false; }
David Brazdilbbd733e2015-08-18 17:48:17 +01007157
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007158 DECLARE_INSTRUCTION(LoadException);
7159
Artem Serovcced8ba2017-07-19 18:18:09 +01007160 protected:
7161 DEFAULT_COPY_CONSTRUCTOR(LoadException);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007162};
7163
David Brazdilcb1c0552015-08-04 16:22:25 +01007164// Implicit part of move-exception which clears thread-local exception storage.
7165// Must not be removed because the runtime expects the TLS to get cleared.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007166class HClearException final : public HExpression<0> {
David Brazdilcb1c0552015-08-04 16:22:25 +01007167 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06007168 explicit HClearException(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01007169 : HExpression(kClearException, SideEffects::AllWrites(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307170 }
David Brazdilcb1c0552015-08-04 16:22:25 +01007171
7172 DECLARE_INSTRUCTION(ClearException);
7173
Artem Serovcced8ba2017-07-19 18:18:09 +01007174 protected:
7175 DEFAULT_COPY_CONSTRUCTOR(ClearException);
David Brazdilcb1c0552015-08-04 16:22:25 +01007176};
7177
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007178class HThrow final : public HExpression<1> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007179 public:
7180 HThrow(HInstruction* exception, uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01007181 : HExpression(kThrow, SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007182 SetRawInputAt(0, exception);
7183 }
7184
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007185 bool IsControlFlow() const override { return true; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007186
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007187 bool NeedsEnvironment() const override { return true; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007188
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007189 bool CanThrow() const override { return true; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007190
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007191 bool AlwaysThrows() const override { return true; }
Aart Bika8b8e9b2018-01-09 11:01:02 -08007192
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007193 DECLARE_INSTRUCTION(Throw);
7194
Artem Serovcced8ba2017-07-19 18:18:09 +01007195 protected:
7196 DEFAULT_COPY_CONSTRUCTOR(Throw);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007197};
7198
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007199/**
7200 * Implementation strategies for the code generator of a HInstanceOf
7201 * or `HCheckCast`.
7202 */
Vladimir Marko9974e3c2020-06-10 16:27:06 +01007203enum class TypeCheckKind { // private marker to avoid generate-operator-out.py from processing.
Calin Juravle98893e12015-10-02 21:05:03 +01007204 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007205 kExactCheck, // Can do a single class compare.
7206 kClassHierarchyCheck, // Can just walk the super class chain.
7207 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
7208 kInterfaceCheck, // No optimization yet when checking against an interface.
7209 kArrayObjectCheck, // Can just check if the array is not primitive.
Vladimir Markoa1de9182016-02-25 11:37:38 +00007210 kArrayCheck, // No optimization yet when checking against a generic array.
Vladimir Marko175e7862018-03-27 09:03:13 +00007211 kBitstringCheck, // Compare the type check bitstring.
Vladimir Markoa1de9182016-02-25 11:37:38 +00007212 kLast = kArrayCheck
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007213};
7214
Roland Levillain86503782016-02-11 19:07:30 +00007215std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs);
7216
Vladimir Marko175e7862018-03-27 09:03:13 +00007217// Note: HTypeCheckInstruction is just a helper class, not an abstract instruction with an
7218// `IsTypeCheckInstruction()`. (New virtual methods in the HInstruction class have a high cost.)
7219class HTypeCheckInstruction : public HVariableInputSizeInstruction {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007220 public:
Vladimir Marko175e7862018-03-27 09:03:13 +00007221 HTypeCheckInstruction(InstructionKind kind,
Vladimir Markobd785672018-05-03 17:09:09 +01007222 DataType::Type type,
Vladimir Marko175e7862018-03-27 09:03:13 +00007223 HInstruction* object,
7224 HInstruction* target_class_or_null,
7225 TypeCheckKind check_kind,
7226 Handle<mirror::Class> klass,
7227 uint32_t dex_pc,
7228 ArenaAllocator* allocator,
7229 HIntConstant* bitstring_path_to_root,
7230 HIntConstant* bitstring_mask,
7231 SideEffects side_effects)
7232 : HVariableInputSizeInstruction(
7233 kind,
Vladimir Markobd785672018-05-03 17:09:09 +01007234 type,
Vladimir Marko175e7862018-03-27 09:03:13 +00007235 side_effects,
7236 dex_pc,
7237 allocator,
Andreas Gampe3db70682018-12-26 15:12:03 -08007238 /* number_of_inputs= */ check_kind == TypeCheckKind::kBitstringCheck ? 4u : 2u,
Vladimir Marko175e7862018-03-27 09:03:13 +00007239 kArenaAllocTypeCheckInputs),
7240 klass_(klass) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007241 SetPackedField<TypeCheckKindField>(check_kind);
7242 SetPackedFlag<kFlagMustDoNullCheck>(true);
Vladimir Marko175e7862018-03-27 09:03:13 +00007243 SetPackedFlag<kFlagValidTargetClassRTI>(false);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007244 SetRawInputAt(0, object);
Vladimir Marko175e7862018-03-27 09:03:13 +00007245 SetRawInputAt(1, target_class_or_null);
7246 DCHECK_EQ(check_kind == TypeCheckKind::kBitstringCheck, bitstring_path_to_root != nullptr);
7247 DCHECK_EQ(check_kind == TypeCheckKind::kBitstringCheck, bitstring_mask != nullptr);
7248 if (check_kind == TypeCheckKind::kBitstringCheck) {
7249 DCHECK(target_class_or_null->IsNullConstant());
7250 SetRawInputAt(2, bitstring_path_to_root);
7251 SetRawInputAt(3, bitstring_mask);
7252 } else {
7253 DCHECK(target_class_or_null->IsLoadClass());
7254 }
Vladimir Marko87584542017-12-12 17:47:52 +00007255 }
7256
7257 HLoadClass* GetTargetClass() const {
Vladimir Marko175e7862018-03-27 09:03:13 +00007258 DCHECK_NE(GetTypeCheckKind(), TypeCheckKind::kBitstringCheck);
Vladimir Marko87584542017-12-12 17:47:52 +00007259 HInstruction* load_class = InputAt(1);
7260 DCHECK(load_class->IsLoadClass());
7261 return load_class->AsLoadClass();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007262 }
7263
Vladimir Marko175e7862018-03-27 09:03:13 +00007264 uint32_t GetBitstringPathToRoot() const {
7265 DCHECK_EQ(GetTypeCheckKind(), TypeCheckKind::kBitstringCheck);
7266 HInstruction* path_to_root = InputAt(2);
7267 DCHECK(path_to_root->IsIntConstant());
7268 return static_cast<uint32_t>(path_to_root->AsIntConstant()->GetValue());
7269 }
7270
7271 uint32_t GetBitstringMask() const {
7272 DCHECK_EQ(GetTypeCheckKind(), TypeCheckKind::kBitstringCheck);
7273 HInstruction* mask = InputAt(3);
7274 DCHECK(mask->IsIntConstant());
7275 return static_cast<uint32_t>(mask->AsIntConstant()->GetValue());
7276 }
7277
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007278 bool IsClonable() const override { return true; }
7279 bool CanBeMoved() const override { return true; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007280
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007281 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Marko175e7862018-03-27 09:03:13 +00007282 DCHECK(other->IsInstanceOf() || other->IsCheckCast()) << other->DebugName();
7283 return GetPackedFields() == down_cast<const HTypeCheckInstruction*>(other)->GetPackedFields();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007284 }
7285
Andreas Gampe3fbd3ad2018-03-26 21:14:46 +00007286 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
7287 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
7288 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
7289 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
7290
Vladimir Marko175e7862018-03-27 09:03:13 +00007291 ReferenceTypeInfo GetTargetClassRTI() {
7292 if (GetPackedFlag<kFlagValidTargetClassRTI>()) {
7293 // Note: The is_exact flag from the return value should not be used.
Andreas Gampe3db70682018-12-26 15:12:03 -08007294 return ReferenceTypeInfo::CreateUnchecked(klass_, /* is_exact= */ true);
Vladimir Marko175e7862018-03-27 09:03:13 +00007295 } else {
7296 return ReferenceTypeInfo::CreateInvalid();
7297 }
7298 }
7299
7300 // Target class RTI is marked as valid by RTP if the klass_ is admissible.
7301 void SetValidTargetClassRTI() REQUIRES_SHARED(Locks::mutator_lock_) {
7302 DCHECK(klass_ != nullptr);
7303 SetPackedFlag<kFlagValidTargetClassRTI>(true);
7304 }
7305
7306 Handle<mirror::Class> GetClass() const {
7307 return klass_;
7308 }
7309
7310 protected:
7311 DEFAULT_COPY_CONSTRUCTOR(TypeCheckInstruction);
7312
7313 private:
7314 static constexpr size_t kFieldTypeCheckKind = kNumberOfGenericPackedBits;
7315 static constexpr size_t kFieldTypeCheckKindSize =
7316 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
7317 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
7318 static constexpr size_t kFlagValidTargetClassRTI = kFlagMustDoNullCheck + 1;
7319 static constexpr size_t kNumberOfInstanceOfPackedBits = kFlagValidTargetClassRTI + 1;
7320 static_assert(kNumberOfInstanceOfPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
7321 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
7322
7323 Handle<mirror::Class> klass_;
7324};
7325
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007326class HInstanceOf final : public HTypeCheckInstruction {
Vladimir Marko175e7862018-03-27 09:03:13 +00007327 public:
7328 HInstanceOf(HInstruction* object,
7329 HInstruction* target_class_or_null,
7330 TypeCheckKind check_kind,
7331 Handle<mirror::Class> klass,
7332 uint32_t dex_pc,
7333 ArenaAllocator* allocator,
7334 HIntConstant* bitstring_path_to_root,
7335 HIntConstant* bitstring_mask)
7336 : HTypeCheckInstruction(kInstanceOf,
Vladimir Markobd785672018-05-03 17:09:09 +01007337 DataType::Type::kBool,
Vladimir Marko175e7862018-03-27 09:03:13 +00007338 object,
7339 target_class_or_null,
7340 check_kind,
7341 klass,
7342 dex_pc,
7343 allocator,
7344 bitstring_path_to_root,
7345 bitstring_mask,
7346 SideEffectsForArchRuntimeCalls(check_kind)) {}
7347
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007348 bool IsClonable() const override { return true; }
Artem Serova6e26142018-06-19 14:55:17 +01007349
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007350 bool NeedsEnvironment() const override {
Vladimir Marko175e7862018-03-27 09:03:13 +00007351 return CanCallRuntime(GetTypeCheckKind());
7352 }
7353
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00007354 static bool CanCallRuntime(TypeCheckKind check_kind) {
Vladimir Marko54f4fbd2020-02-12 10:52:22 +00007355 // TODO: Re-evaluate now that mips codegen has been removed.
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00007356 return check_kind != TypeCheckKind::kExactCheck;
7357 }
7358
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007359 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00007360 return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01007361 }
7362
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007363 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007364
Artem Serovcced8ba2017-07-19 18:18:09 +01007365 protected:
7366 DEFAULT_COPY_CONSTRUCTOR(InstanceOf);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007367};
7368
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007369class HBoundType final : public HExpression<1> {
Calin Juravleb1498f62015-02-16 13:13:29 +00007370 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -07007371 explicit HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307372 : HExpression(kBoundType, DataType::Type::kReference, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00007373 upper_bound_(ReferenceTypeInfo::CreateInvalid()) {
7374 SetPackedFlag<kFlagUpperCanBeNull>(true);
7375 SetPackedFlag<kFlagCanBeNull>(true);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007376 DCHECK_EQ(input->GetType(), DataType::Type::kReference);
Calin Juravleb1498f62015-02-16 13:13:29 +00007377 SetRawInputAt(0, input);
7378 }
7379
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007380 bool InstructionDataEquals(const HInstruction* other) const override;
7381 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01007382
David Brazdilf5552582015-12-27 13:36:12 +00007383 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007384 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00007385 bool GetUpperCanBeNull() const { return GetPackedFlag<kFlagUpperCanBeNull>(); }
David Brazdilf5552582015-12-27 13:36:12 +00007386 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00007387
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007388 void SetCanBeNull(bool can_be_null) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007389 DCHECK(GetUpperCanBeNull() || !can_be_null);
7390 SetPackedFlag<kFlagCanBeNull>(can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00007391 }
7392
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007393 bool CanBeNull() const override { return GetPackedFlag<kFlagCanBeNull>(); }
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007394
Calin Juravleb1498f62015-02-16 13:13:29 +00007395 DECLARE_INSTRUCTION(BoundType);
7396
Artem Serovcced8ba2017-07-19 18:18:09 +01007397 protected:
7398 DEFAULT_COPY_CONSTRUCTOR(BoundType);
7399
Calin Juravleb1498f62015-02-16 13:13:29 +00007400 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007401 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
7402 // is false then CanBeNull() cannot be true).
Vladimir Markobd785672018-05-03 17:09:09 +01007403 static constexpr size_t kFlagUpperCanBeNull = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00007404 static constexpr size_t kFlagCanBeNull = kFlagUpperCanBeNull + 1;
7405 static constexpr size_t kNumberOfBoundTypePackedBits = kFlagCanBeNull + 1;
7406 static_assert(kNumberOfBoundTypePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
7407
Calin Juravleb1498f62015-02-16 13:13:29 +00007408 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007409 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
7410 // It is used to bound the type in cases like:
7411 // if (x instanceof ClassX) {
7412 // // uper_bound_ will be ClassX
7413 // }
David Brazdilf5552582015-12-27 13:36:12 +00007414 ReferenceTypeInfo upper_bound_;
Calin Juravleb1498f62015-02-16 13:13:29 +00007415};
7416
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007417class HCheckCast final : public HTypeCheckInstruction {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007418 public:
7419 HCheckCast(HInstruction* object,
Vladimir Marko175e7862018-03-27 09:03:13 +00007420 HInstruction* target_class_or_null,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007421 TypeCheckKind check_kind,
Vladimir Marko175e7862018-03-27 09:03:13 +00007422 Handle<mirror::Class> klass,
7423 uint32_t dex_pc,
7424 ArenaAllocator* allocator,
7425 HIntConstant* bitstring_path_to_root,
7426 HIntConstant* bitstring_mask)
7427 : HTypeCheckInstruction(kCheckCast,
Vladimir Markobd785672018-05-03 17:09:09 +01007428 DataType::Type::kVoid,
Vladimir Marko175e7862018-03-27 09:03:13 +00007429 object,
7430 target_class_or_null,
7431 check_kind,
7432 klass,
7433 dex_pc,
7434 allocator,
7435 bitstring_path_to_root,
7436 bitstring_mask,
7437 SideEffects::CanTriggerGC()) {}
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007438
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007439 bool IsClonable() const override { return true; }
7440 bool NeedsEnvironment() const override {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007441 // Instruction may throw a CheckCastError.
7442 return true;
7443 }
7444
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007445 bool CanThrow() const override { return true; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007446
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007447 DECLARE_INSTRUCTION(CheckCast);
7448
Artem Serovcced8ba2017-07-19 18:18:09 +01007449 protected:
7450 DEFAULT_COPY_CONSTRUCTOR(CheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007451};
7452
Andreas Gampe26de38b2016-07-27 17:53:11 -07007453/**
7454 * @brief Memory barrier types (see "The JSR-133 Cookbook for Compiler Writers").
7455 * @details We define the combined barrier types that are actually required
7456 * by the Java Memory Model, rather than using exactly the terminology from
7457 * the JSR-133 cookbook. These should, in many cases, be replaced by acquire/release
7458 * primitives. Note that the JSR-133 cookbook generally does not deal with
7459 * store atomicity issues, and the recipes there are not always entirely sufficient.
7460 * The current recipe is as follows:
7461 * -# Use AnyStore ~= (LoadStore | StoreStore) ~= release barrier before volatile store.
7462 * -# Use AnyAny barrier after volatile store. (StoreLoad is as expensive.)
7463 * -# Use LoadAny barrier ~= (LoadLoad | LoadStore) ~= acquire barrier after each volatile load.
7464 * -# Use StoreStore barrier after all stores but before return from any constructor whose
7465 * class has final fields.
7466 * -# Use NTStoreStore to order non-temporal stores with respect to all later
7467 * store-to-memory instructions. Only generated together with non-temporal stores.
7468 */
7469enum MemBarrierKind {
7470 kAnyStore,
7471 kLoadAny,
7472 kStoreStore,
7473 kAnyAny,
7474 kNTStoreStore,
7475 kLastBarrierKind = kNTStoreStore
7476};
Vladimir Marko9974e3c2020-06-10 16:27:06 +01007477std::ostream& operator<<(std::ostream& os, MemBarrierKind kind);
Andreas Gampe26de38b2016-07-27 17:53:11 -07007478
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007479class HMemoryBarrier final : public HExpression<0> {
Calin Juravle27df7582015-04-17 19:12:31 +01007480 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06007481 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01007482 : HExpression(kMemoryBarrier,
7483 SideEffects::AllWritesAndReads(), // Assume write/read on all fields/arrays.
7484 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007485 SetPackedField<BarrierKindField>(barrier_kind);
7486 }
Calin Juravle27df7582015-04-17 19:12:31 +01007487
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007488 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01007489
Vladimir Markoa1de9182016-02-25 11:37:38 +00007490 MemBarrierKind GetBarrierKind() { return GetPackedField<BarrierKindField>(); }
Calin Juravle27df7582015-04-17 19:12:31 +01007491
7492 DECLARE_INSTRUCTION(MemoryBarrier);
7493
Artem Serovcced8ba2017-07-19 18:18:09 +01007494 protected:
7495 DEFAULT_COPY_CONSTRUCTOR(MemoryBarrier);
7496
Calin Juravle27df7582015-04-17 19:12:31 +01007497 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007498 static constexpr size_t kFieldBarrierKind = HInstruction::kNumberOfGenericPackedBits;
7499 static constexpr size_t kFieldBarrierKindSize =
7500 MinimumBitsToStore(static_cast<size_t>(kLastBarrierKind));
7501 static constexpr size_t kNumberOfMemoryBarrierPackedBits =
7502 kFieldBarrierKind + kFieldBarrierKindSize;
7503 static_assert(kNumberOfMemoryBarrierPackedBits <= kMaxNumberOfPackedBits,
7504 "Too many packed fields.");
7505 using BarrierKindField = BitField<MemBarrierKind, kFieldBarrierKind, kFieldBarrierKindSize>;
Calin Juravle27df7582015-04-17 19:12:31 +01007506};
7507
Igor Murashkind01745e2017-04-05 16:40:31 -07007508// A constructor fence orders all prior stores to fields that could be accessed via a final field of
7509// the specified object(s), with respect to any subsequent store that might "publish"
7510// (i.e. make visible) the specified object to another thread.
7511//
7512// JLS 17.5.1 "Semantics of final fields" states that a freeze action happens
7513// for all final fields (that were set) at the end of the invoked constructor.
7514//
7515// The constructor fence models the freeze actions for the final fields of an object
7516// being constructed (semantically at the end of the constructor). Constructor fences
7517// have a per-object affinity; two separate objects being constructed get two separate
7518// constructor fences.
7519//
7520// (Note: that if calling a super-constructor or forwarding to another constructor,
7521// the freezes would happen at the end of *that* constructor being invoked).
7522//
7523// The memory model guarantees that when the object being constructed is "published" after
7524// constructor completion (i.e. escapes the current thread via a store), then any final field
7525// writes must be observable on other threads (once they observe that publication).
7526//
7527// Further, anything written before the freeze, and read by dereferencing through the final field,
7528// must also be visible (so final object field could itself have an object with non-final fields;
7529// yet the freeze must also extend to them).
7530//
7531// Constructor example:
7532//
7533// class HasFinal {
7534// final int field; Optimizing IR for <init>()V:
7535// HasFinal() {
7536// field = 123; HInstanceFieldSet(this, HasFinal.field, 123)
7537// // freeze(this.field); HConstructorFence(this)
7538// } HReturn
7539// }
7540//
7541// HConstructorFence can serve double duty as a fence for new-instance/new-array allocations of
7542// already-initialized classes; in that case the allocation must act as a "default-initializer"
7543// of the object which effectively writes the class pointer "final field".
7544//
7545// For example, we can model default-initialiation as roughly the equivalent of the following:
7546//
7547// class Object {
7548// private final Class header;
7549// }
7550//
7551// Java code: Optimizing IR:
7552//
7553// T new_instance<T>() {
7554// Object obj = allocate_memory(T.class.size); obj = HInvoke(art_quick_alloc_object, T)
7555// obj.header = T.class; // header write is done by above call.
7556// // freeze(obj.header) HConstructorFence(obj)
7557// return (T)obj;
7558// }
7559//
7560// See also:
Vladimir Markoc1c34522018-10-31 13:56:49 +00007561// * DexCompilationUnit::RequiresConstructorBarrier
Igor Murashkind01745e2017-04-05 16:40:31 -07007562// * QuasiAtomic::ThreadFenceForConstructor
7563//
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007564class HConstructorFence final : public HVariableInputSizeInstruction {
Igor Murashkind01745e2017-04-05 16:40:31 -07007565 // A fence has variable inputs because the inputs can be removed
7566 // after prepare_for_register_allocation phase.
7567 // (TODO: In the future a fence could freeze multiple objects
7568 // after merging two fences together.)
7569 public:
7570 // `fence_object` is the reference that needs to be protected for correct publication.
7571 //
7572 // It makes sense in the following situations:
7573 // * <init> constructors, it's the "this" parameter (i.e. HParameterValue, s.t. IsThis() == true).
7574 // * new-instance-like instructions, it's the return value (i.e. HNewInstance).
7575 //
7576 // After construction the `fence_object` becomes the 0th input.
7577 // This is not an input in a real sense, but just a convenient place to stash the information
7578 // about the associated object.
7579 HConstructorFence(HInstruction* fence_object,
7580 uint32_t dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007581 ArenaAllocator* allocator)
Igor Murashkind01745e2017-04-05 16:40:31 -07007582 // We strongly suspect there is not a more accurate way to describe the fine-grained reordering
7583 // constraints described in the class header. We claim that these SideEffects constraints
7584 // enforce a superset of the real constraints.
7585 //
7586 // The ordering described above is conservatively modeled with SideEffects as follows:
7587 //
7588 // * To prevent reordering of the publication stores:
7589 // ----> "Reads of objects" is the initial SideEffect.
7590 // * For every primitive final field store in the constructor:
7591 // ----> Union that field's type as a read (e.g. "Read of T") into the SideEffect.
7592 // * If there are any stores to reference final fields in the constructor:
7593 // ----> Use a more conservative "AllReads" SideEffect because any stores to any references
7594 // that are reachable from `fence_object` also need to be prevented for reordering
7595 // (and we do not want to do alias analysis to figure out what those stores are).
7596 //
7597 // In the implementation, this initially starts out as an "all reads" side effect; this is an
7598 // even more conservative approach than the one described above, and prevents all of the
7599 // above reordering without analyzing any of the instructions in the constructor.
7600 //
7601 // If in a later phase we discover that there are no writes to reference final fields,
7602 // we can refine the side effect to a smaller set of type reads (see above constraints).
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307603 : HVariableInputSizeInstruction(kConstructorFence,
7604 SideEffects::AllReads(),
Igor Murashkind01745e2017-04-05 16:40:31 -07007605 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007606 allocator,
Andreas Gampe3db70682018-12-26 15:12:03 -08007607 /* number_of_inputs= */ 1,
Igor Murashkind01745e2017-04-05 16:40:31 -07007608 kArenaAllocConstructorFenceInputs) {
7609 DCHECK(fence_object != nullptr);
7610 SetRawInputAt(0, fence_object);
7611 }
7612
7613 // The object associated with this constructor fence.
7614 //
7615 // (Note: This will be null after the prepare_for_register_allocation phase,
7616 // as all constructor fence inputs are removed there).
7617 HInstruction* GetFenceObject() const {
7618 return InputAt(0);
7619 }
7620
7621 // Find all the HConstructorFence uses (`fence_use`) for `this` and:
7622 // - Delete `fence_use` from `this`'s use list.
7623 // - Delete `this` from `fence_use`'s inputs list.
7624 // - If the `fence_use` is dead, remove it from the graph.
7625 //
7626 // A fence is considered dead once it no longer has any uses
7627 // and all of the inputs are dead.
7628 //
7629 // This must *not* be called during/after prepare_for_register_allocation,
7630 // because that removes all the inputs to the fences but the fence is actually
7631 // still considered live.
Igor Murashkin6ef45672017-08-08 13:59:55 -07007632 //
7633 // Returns how many HConstructorFence instructions were removed from graph.
7634 static size_t RemoveConstructorFences(HInstruction* instruction);
Igor Murashkind01745e2017-04-05 16:40:31 -07007635
Igor Murashkindd018df2017-08-09 10:38:31 -07007636 // Combine all inputs of `this` and `other` instruction and remove
7637 // `other` from the graph.
7638 //
7639 // Inputs are unique after the merge.
7640 //
7641 // Requirement: `this` must not be the same as `other.
7642 void Merge(HConstructorFence* other);
7643
Igor Murashkin79d8fa72017-04-18 09:37:23 -07007644 // Check if this constructor fence is protecting
7645 // an HNewInstance or HNewArray that is also the immediate
7646 // predecessor of `this`.
7647 //
Igor Murashkindd018df2017-08-09 10:38:31 -07007648 // If `ignore_inputs` is true, then the immediate predecessor doesn't need
7649 // to be one of the inputs of `this`.
7650 //
Igor Murashkin79d8fa72017-04-18 09:37:23 -07007651 // Returns the associated HNewArray or HNewInstance,
7652 // or null otherwise.
Igor Murashkindd018df2017-08-09 10:38:31 -07007653 HInstruction* GetAssociatedAllocation(bool ignore_inputs = false);
Igor Murashkin79d8fa72017-04-18 09:37:23 -07007654
Igor Murashkind01745e2017-04-05 16:40:31 -07007655 DECLARE_INSTRUCTION(ConstructorFence);
7656
Artem Serovcced8ba2017-07-19 18:18:09 +01007657 protected:
7658 DEFAULT_COPY_CONSTRUCTOR(ConstructorFence);
Igor Murashkind01745e2017-04-05 16:40:31 -07007659};
7660
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007661class HMonitorOperation final : public HExpression<1> {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007662 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007663 enum class OperationKind {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007664 kEnter,
7665 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00007666 kLast = kExit
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007667 };
7668
7669 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01007670 : HExpression(kMonitorOperation,
7671 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
7672 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007673 SetPackedField<OperationKindField>(kind);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007674 SetRawInputAt(0, object);
7675 }
7676
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00007677 // Instruction may go into runtime, so we need an environment.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007678 bool NeedsEnvironment() const override { return true; }
David Brazdilbff75032015-07-08 17:26:51 +00007679
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007680 bool CanThrow() const override {
David Brazdilbff75032015-07-08 17:26:51 +00007681 // Verifier guarantees that monitor-exit cannot throw.
7682 // This is important because it allows the HGraphBuilder to remove
7683 // a dead throw-catch loop generated for `synchronized` blocks/methods.
7684 return IsEnter();
7685 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007686
Vladimir Markoa1de9182016-02-25 11:37:38 +00007687 OperationKind GetOperationKind() const { return GetPackedField<OperationKindField>(); }
7688 bool IsEnter() const { return GetOperationKind() == OperationKind::kEnter; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007689
7690 DECLARE_INSTRUCTION(MonitorOperation);
7691
Artem Serovcced8ba2017-07-19 18:18:09 +01007692 protected:
7693 DEFAULT_COPY_CONSTRUCTOR(MonitorOperation);
7694
Calin Juravle52c48962014-12-16 17:02:57 +00007695 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007696 static constexpr size_t kFieldOperationKind = HInstruction::kNumberOfGenericPackedBits;
7697 static constexpr size_t kFieldOperationKindSize =
7698 MinimumBitsToStore(static_cast<size_t>(OperationKind::kLast));
7699 static constexpr size_t kNumberOfMonitorOperationPackedBits =
7700 kFieldOperationKind + kFieldOperationKindSize;
7701 static_assert(kNumberOfMonitorOperationPackedBits <= HInstruction::kMaxNumberOfPackedBits,
7702 "Too many packed fields.");
7703 using OperationKindField = BitField<OperationKind, kFieldOperationKind, kFieldOperationKindSize>;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007704};
7705
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007706class HSelect final : public HExpression<3> {
David Brazdil74eb1b22015-12-14 11:44:01 +00007707 public:
7708 HSelect(HInstruction* condition,
7709 HInstruction* true_value,
7710 HInstruction* false_value,
7711 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307712 : HExpression(kSelect, HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
David Brazdil74eb1b22015-12-14 11:44:01 +00007713 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
7714
7715 // First input must be `true_value` or `false_value` to allow codegens to
7716 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
7717 // that architectures which implement HSelect as a conditional move also
7718 // will not need to invert the condition.
7719 SetRawInputAt(0, false_value);
7720 SetRawInputAt(1, true_value);
7721 SetRawInputAt(2, condition);
7722 }
7723
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007724 bool IsClonable() const override { return true; }
David Brazdil74eb1b22015-12-14 11:44:01 +00007725 HInstruction* GetFalseValue() const { return InputAt(0); }
7726 HInstruction* GetTrueValue() const { return InputAt(1); }
7727 HInstruction* GetCondition() const { return InputAt(2); }
7728
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007729 bool CanBeMoved() const override { return true; }
7730 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01007731 return true;
7732 }
David Brazdil74eb1b22015-12-14 11:44:01 +00007733
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007734 bool CanBeNull() const override {
David Brazdil74eb1b22015-12-14 11:44:01 +00007735 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
7736 }
7737
7738 DECLARE_INSTRUCTION(Select);
7739
Artem Serovcced8ba2017-07-19 18:18:09 +01007740 protected:
7741 DEFAULT_COPY_CONSTRUCTOR(Select);
David Brazdil74eb1b22015-12-14 11:44:01 +00007742};
7743
Vladimir Markof9f64412015-09-02 14:05:49 +01007744class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007745 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01007746 MoveOperands(Location source,
7747 Location destination,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007748 DataType::Type type,
Nicolas Geoffray90218252015-04-15 11:56:51 +01007749 HInstruction* instruction)
7750 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007751
7752 Location GetSource() const { return source_; }
7753 Location GetDestination() const { return destination_; }
7754
7755 void SetSource(Location value) { source_ = value; }
7756 void SetDestination(Location value) { destination_ = value; }
7757
7758 // The parallel move resolver marks moves as "in-progress" by clearing the
7759 // destination (but not the source).
7760 Location MarkPending() {
7761 DCHECK(!IsPending());
7762 Location dest = destination_;
7763 destination_ = Location::NoLocation();
7764 return dest;
7765 }
7766
7767 void ClearPending(Location dest) {
7768 DCHECK(IsPending());
7769 destination_ = dest;
7770 }
7771
7772 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00007773 DCHECK(source_.IsValid() || destination_.IsInvalid());
7774 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007775 }
7776
7777 // True if this blocks a move from the given location.
7778 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08007779 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007780 }
7781
7782 // A move is redundant if it's been eliminated, if its source and
7783 // destination are the same, or if its destination is unneeded.
7784 bool IsRedundant() const {
7785 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
7786 }
7787
7788 // We clear both operands to indicate move that's been eliminated.
7789 void Eliminate() {
7790 source_ = destination_ = Location::NoLocation();
7791 }
7792
7793 bool IsEliminated() const {
7794 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
7795 return source_.IsInvalid();
7796 }
7797
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007798 DataType::Type GetType() const { return type_; }
Alexey Frunze4dda3372015-06-01 18:31:49 -07007799
Nicolas Geoffray90218252015-04-15 11:56:51 +01007800 bool Is64BitMove() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007801 return DataType::Is64BitType(type_);
Nicolas Geoffray90218252015-04-15 11:56:51 +01007802 }
7803
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007804 HInstruction* GetInstruction() const { return instruction_; }
7805
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007806 private:
7807 Location source_;
7808 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01007809 // The type this move is for.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007810 DataType::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007811 // The instruction this move is assocatied with. Null when this move is
7812 // for moving an input in the expected locations of user (including a phi user).
7813 // This is only used in debug mode, to ensure we do not connect interval siblings
7814 // in the same parallel move.
7815 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007816};
7817
Roland Levillainc9285912015-12-18 10:38:42 +00007818std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
7819
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007820static constexpr size_t kDefaultNumberOfMoves = 4;
7821
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007822class HParallelMove final : public HExpression<0> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007823 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007824 explicit HParallelMove(ArenaAllocator* allocator, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01007825 : HExpression(kParallelMove, SideEffects::None(), dex_pc),
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007826 moves_(allocator->Adapter(kArenaAllocMoveOperands)) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007827 moves_.reserve(kDefaultNumberOfMoves);
7828 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007829
Nicolas Geoffray90218252015-04-15 11:56:51 +01007830 void AddMove(Location source,
7831 Location destination,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007832 DataType::Type type,
Nicolas Geoffray90218252015-04-15 11:56:51 +01007833 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00007834 DCHECK(source.IsValid());
7835 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00007836 if (kIsDebugBuild) {
7837 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007838 for (const MoveOperands& move : moves_) {
7839 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007840 // Special case the situation where the move is for the spill slot
7841 // of the instruction.
7842 if ((GetPrevious() == instruction)
7843 || ((GetPrevious() == nullptr)
7844 && instruction->IsPhi()
7845 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007846 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007847 << "Doing parallel moves for the same instruction.";
7848 } else {
7849 DCHECK(false) << "Doing parallel moves for the same instruction.";
7850 }
7851 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00007852 }
7853 }
Vladimir Marko225b6462015-09-28 12:17:40 +01007854 for (const MoveOperands& move : moves_) {
7855 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01007856 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01007857 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01007858 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00007859 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007860 }
Vladimir Marko225b6462015-09-28 12:17:40 +01007861 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007862 }
7863
Vladimir Marko225b6462015-09-28 12:17:40 +01007864 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007865 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007866 }
7867
Vladimir Marko225b6462015-09-28 12:17:40 +01007868 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007869
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01007870 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007871
Artem Serovcced8ba2017-07-19 18:18:09 +01007872 protected:
7873 DEFAULT_COPY_CONSTRUCTOR(ParallelMove);
7874
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007875 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01007876 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007877};
7878
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007879// This instruction computes an intermediate address pointing in the 'middle' of an object. The
7880// result pointer cannot be handled by GC, so extra care is taken to make sure that this value is
7881// never used across anything that can trigger GC.
7882// The result of this instruction is not a pointer in the sense of `DataType::Type::kreference`.
7883// So we represent it by the type `DataType::Type::kInt`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007884class HIntermediateAddress final : public HExpression<2> {
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007885 public:
7886 HIntermediateAddress(HInstruction* base_address, HInstruction* offset, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307887 : HExpression(kIntermediateAddress,
7888 DataType::Type::kInt32,
7889 SideEffects::DependsOnGC(),
7890 dex_pc) {
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007891 DCHECK_EQ(DataType::Size(DataType::Type::kInt32),
7892 DataType::Size(DataType::Type::kReference))
7893 << "kPrimInt and kPrimNot have different sizes.";
7894 SetRawInputAt(0, base_address);
7895 SetRawInputAt(1, offset);
7896 }
7897
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007898 bool IsClonable() const override { return true; }
7899 bool CanBeMoved() const override { return true; }
7900 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007901 return true;
7902 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007903 bool IsActualObject() const override { return false; }
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007904
7905 HInstruction* GetBaseAddress() const { return InputAt(0); }
7906 HInstruction* GetOffset() const { return InputAt(1); }
7907
7908 DECLARE_INSTRUCTION(IntermediateAddress);
7909
Artem Serovcced8ba2017-07-19 18:18:09 +01007910 protected:
7911 DEFAULT_COPY_CONSTRUCTOR(IntermediateAddress);
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007912};
7913
7914
Mark Mendell0616ae02015-04-17 12:49:27 -04007915} // namespace art
7916
Aart Bikf8f5a162017-02-06 15:35:29 -08007917#include "nodes_vector.h"
7918
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03007919#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
7920#include "nodes_shared.h"
7921#endif
Shalini Salomi Bodapatidd121f62018-10-26 15:03:53 +05307922#if defined(ART_ENABLE_CODEGEN_x86) || defined(ART_ENABLE_CODEGEN_x86_64)
Mark Mendell0616ae02015-04-17 12:49:27 -04007923#include "nodes_x86.h"
7924#endif
7925
Vladimir Marko0a516052019-10-14 13:00:44 +00007926namespace art {
Mark Mendell0616ae02015-04-17 12:49:27 -04007927
Igor Murashkin6ef45672017-08-08 13:59:55 -07007928class OptimizingCompilerStats;
7929
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007930class HGraphVisitor : public ValueObject {
7931 public:
Igor Murashkin6ef45672017-08-08 13:59:55 -07007932 explicit HGraphVisitor(HGraph* graph, OptimizingCompilerStats* stats = nullptr)
7933 : stats_(stats),
7934 graph_(graph) {}
Dave Allison20dfc792014-06-16 20:44:29 -07007935 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007936
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007937 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007938 virtual void VisitBasicBlock(HBasicBlock* block);
7939
Roland Levillain633021e2014-10-01 14:12:25 +01007940 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007941 void VisitInsertionOrder();
7942
Roland Levillain633021e2014-10-01 14:12:25 +01007943 // Visit the graph following dominator tree reverse post-order.
7944 void VisitReversePostOrder();
7945
Nicolas Geoffray787c3072014-03-17 10:20:19 +00007946 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007947
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007948 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007949#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007950 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
7951
7952 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
7953
7954#undef DECLARE_VISIT_INSTRUCTION
7955
Igor Murashkin6ef45672017-08-08 13:59:55 -07007956 protected:
7957 OptimizingCompilerStats* stats_;
7958
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007959 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07007960 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007961
7962 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
7963};
7964
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007965class HGraphDelegateVisitor : public HGraphVisitor {
7966 public:
Igor Murashkin6ef45672017-08-08 13:59:55 -07007967 explicit HGraphDelegateVisitor(HGraph* graph, OptimizingCompilerStats* stats = nullptr)
7968 : HGraphVisitor(graph, stats) {}
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007969 virtual ~HGraphDelegateVisitor() {}
7970
7971 // Visit functions that delegate to to super class.
7972#define DECLARE_VISIT_INSTRUCTION(name, super) \
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007973 void Visit##name(H##name* instr) override { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007974
7975 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
7976
7977#undef DECLARE_VISIT_INSTRUCTION
7978
7979 private:
7980 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
7981};
7982
Artem Serovcced8ba2017-07-19 18:18:09 +01007983// Create a clone of the instruction, insert it into the graph; replace the old one with a new
7984// and remove the old instruction.
7985HInstruction* ReplaceInstrOrPhiByClone(HInstruction* instr);
7986
7987// Create a clone for each clonable instructions/phis and replace the original with the clone.
7988//
7989// Used for testing individual instruction cloner.
7990class CloneAndReplaceInstructionVisitor : public HGraphDelegateVisitor {
7991 public:
7992 explicit CloneAndReplaceInstructionVisitor(HGraph* graph)
Artem Serov7f4aff62017-06-21 17:02:18 +01007993 : HGraphDelegateVisitor(graph), instr_replaced_by_clones_count_(0) {}
Artem Serovcced8ba2017-07-19 18:18:09 +01007994
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007995 void VisitInstruction(HInstruction* instruction) override {
Artem Serovcced8ba2017-07-19 18:18:09 +01007996 if (instruction->IsClonable()) {
7997 ReplaceInstrOrPhiByClone(instruction);
Artem Serov7f4aff62017-06-21 17:02:18 +01007998 instr_replaced_by_clones_count_++;
Artem Serovcced8ba2017-07-19 18:18:09 +01007999 }
8000 }
8001
Artem Serov7f4aff62017-06-21 17:02:18 +01008002 size_t GetInstrReplacedByClonesCount() const { return instr_replaced_by_clones_count_; }
Artem Serovcced8ba2017-07-19 18:18:09 +01008003
8004 private:
Artem Serov7f4aff62017-06-21 17:02:18 +01008005 size_t instr_replaced_by_clones_count_;
Artem Serovcced8ba2017-07-19 18:18:09 +01008006
8007 DISALLOW_COPY_AND_ASSIGN(CloneAndReplaceInstructionVisitor);
8008};
8009
Nicolas Geoffray82091da2015-01-26 10:02:45 +00008010// Iterator over the blocks that art part of the loop. Includes blocks part
8011// of an inner loop. The order in which the blocks are iterated is on their
8012// block id.
8013class HBlocksInLoopIterator : public ValueObject {
8014 public:
8015 explicit HBlocksInLoopIterator(const HLoopInformation& info)
8016 : blocks_in_loop_(info.GetBlocks()),
8017 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
8018 index_(0) {
8019 if (!blocks_in_loop_.IsBitSet(index_)) {
8020 Advance();
8021 }
8022 }
8023
Vladimir Markofa6b93c2015-09-15 10:15:55 +01008024 bool Done() const { return index_ == blocks_.size(); }
8025 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00008026 void Advance() {
8027 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01008028 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00008029 if (blocks_in_loop_.IsBitSet(index_)) {
8030 break;
8031 }
8032 }
8033 }
8034
8035 private:
8036 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01008037 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00008038 size_t index_;
8039
8040 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
8041};
8042
Mingyao Yang3584bce2015-05-19 16:01:59 -07008043// Iterator over the blocks that art part of the loop. Includes blocks part
8044// of an inner loop. The order in which the blocks are iterated is reverse
8045// post order.
8046class HBlocksInLoopReversePostOrderIterator : public ValueObject {
8047 public:
8048 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
8049 : blocks_in_loop_(info.GetBlocks()),
8050 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
8051 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01008052 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07008053 Advance();
8054 }
8055 }
8056
Vladimir Markofa6b93c2015-09-15 10:15:55 +01008057 bool Done() const { return index_ == blocks_.size(); }
8058 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07008059 void Advance() {
8060 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01008061 for (size_t e = blocks_.size(); index_ < e; ++index_) {
8062 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07008063 break;
8064 }
8065 }
8066 }
8067
8068 private:
8069 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01008070 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07008071 size_t index_;
8072
8073 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
8074};
8075
Aart Bikf3e61ee2017-04-12 17:09:20 -07008076// Returns int64_t value of a properly typed constant.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00008077inline int64_t Int64FromConstant(HConstant* constant) {
David Brazdilc0b601b2016-02-08 14:20:45 +00008078 if (constant->IsIntConstant()) {
8079 return constant->AsIntConstant()->GetValue();
8080 } else if (constant->IsLongConstant()) {
8081 return constant->AsLongConstant()->GetValue();
8082 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00008083 DCHECK(constant->IsNullConstant()) << constant->DebugName();
David Brazdilc0b601b2016-02-08 14:20:45 +00008084 return 0;
8085 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00008086}
8087
Aart Bikf3e61ee2017-04-12 17:09:20 -07008088// Returns true iff instruction is an integral constant (and sets value on success).
8089inline bool IsInt64AndGet(HInstruction* instruction, /*out*/ int64_t* value) {
8090 if (instruction->IsIntConstant()) {
8091 *value = instruction->AsIntConstant()->GetValue();
8092 return true;
8093 } else if (instruction->IsLongConstant()) {
8094 *value = instruction->AsLongConstant()->GetValue();
8095 return true;
8096 } else if (instruction->IsNullConstant()) {
8097 *value = 0;
8098 return true;
8099 }
8100 return false;
8101}
8102
Aart Bik0148de42017-09-05 09:25:01 -07008103// Returns true iff instruction is the given integral constant.
8104inline bool IsInt64Value(HInstruction* instruction, int64_t value) {
8105 int64_t val = 0;
8106 return IsInt64AndGet(instruction, &val) && val == value;
8107}
8108
8109// Returns true iff instruction is a zero bit pattern.
8110inline bool IsZeroBitPattern(HInstruction* instruction) {
8111 return instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern();
8112}
8113
Vladimir Marko6d5b7e32018-05-09 16:52:48 +01008114// Implement HInstruction::Is##type() for concrete instructions.
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008115#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Markoa90dd512018-05-04 15:04:45 +01008116 inline bool HInstruction::Is##type() const { return GetKind() == k##type; }
8117 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
8118#undef INSTRUCTION_TYPE_CHECK
8119
Vladimir Marko6d5b7e32018-05-09 16:52:48 +01008120// Implement HInstruction::Is##type() for abstract instructions.
Vladimir Markoa90dd512018-05-04 15:04:45 +01008121#define INSTRUCTION_TYPE_CHECK_RESULT(type, super) \
8122 std::is_base_of<BaseType, H##type>::value,
8123#define INSTRUCTION_TYPE_CHECK(type, super) \
8124 inline bool HInstruction::Is##type() const { \
8125 DCHECK_LT(GetKind(), kLastInstructionKind); \
8126 using BaseType = H##type; \
8127 static constexpr bool results[] = { \
8128 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK_RESULT) \
8129 }; \
8130 return results[static_cast<size_t>(GetKind())]; \
8131 }
8132
8133 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
8134#undef INSTRUCTION_TYPE_CHECK
Vladimir Marko6d5b7e32018-05-09 16:52:48 +01008135#undef INSTRUCTION_TYPE_CHECK_RESULT
Vladimir Markoa90dd512018-05-04 15:04:45 +01008136
8137#define INSTRUCTION_TYPE_CAST(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008138 inline const H##type* HInstruction::As##type() const { \
8139 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
8140 } \
8141 inline H##type* HInstruction::As##type() { \
8142 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
8143 }
8144
Vladimir Markoa90dd512018-05-04 15:04:45 +01008145 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CAST)
8146#undef INSTRUCTION_TYPE_CAST
8147
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008148
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00008149// Create space in `blocks` for adding `number_of_new_blocks` entries
8150// starting at location `at`. Blocks after `at` are moved accordingly.
8151inline void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks,
8152 size_t number_of_new_blocks,
8153 size_t after) {
8154 DCHECK_LT(after, blocks->size());
8155 size_t old_size = blocks->size();
8156 size_t new_size = old_size + number_of_new_blocks;
8157 blocks->resize(new_size);
8158 std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end());
8159}
8160
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00008161/*
8162 * Hunt "under the hood" of array lengths (leading to array references),
8163 * null checks (also leading to array references), and new arrays
8164 * (leading to the actual length). This makes it more likely related
8165 * instructions become actually comparable.
8166 */
8167inline HInstruction* HuntForDeclaration(HInstruction* instruction) {
8168 while (instruction->IsArrayLength() ||
8169 instruction->IsNullCheck() ||
8170 instruction->IsNewArray()) {
8171 instruction = instruction->IsNewArray()
8172 ? instruction->AsNewArray()->GetLength()
8173 : instruction->InputAt(0);
8174 }
8175 return instruction;
8176}
8177
Artem Serovb47b9782019-12-04 21:02:09 +00008178inline bool IsAddOrSub(const HInstruction* instruction) {
8179 return instruction->IsAdd() || instruction->IsSub();
8180}
8181
Artem Serov21c7e6f2017-07-27 16:04:42 +01008182void RemoveEnvironmentUses(HInstruction* instruction);
8183bool HasEnvironmentUsedByOthers(HInstruction* instruction);
8184void ResetEnvironmentInputRecords(HInstruction* instruction);
8185
Nicolas Geoffray818f2102014-02-18 16:43:35 +00008186} // namespace art
8187
8188#endif // ART_COMPILER_OPTIMIZING_NODES_H_