blob: 7c0e973bce3323bda62f616c84822b023a39afed [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) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001560
Artem Serov1a719e42019-07-18 14:24:55 +01001561#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
1562 FOR_EACH_CONCRETE_INSTRUCTION_SCALAR_COMMON(M) \
1563 FOR_EACH_CONCRETE_INSTRUCTION_VECTOR_COMMON(M)
1564
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001565/*
1566 * Instructions, shared across several (not all) architectures.
1567 */
1568#if !defined(ART_ENABLE_CODEGEN_arm) && !defined(ART_ENABLE_CODEGEN_arm64)
1569#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M)
1570#else
1571#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Artem Serov7fc63502016-02-09 17:15:29 +00001572 M(BitwiseNegatedRight, Instruction) \
Anton Kirilov74234da2017-01-13 14:42:47 +00001573 M(DataProcWithShifterOp, Instruction) \
Artem Serov328429f2016-07-06 16:23:04 +01001574 M(MultiplyAccumulate, Instruction) \
Artem Serove1811ed2017-04-27 16:50:47 +01001575 M(IntermediateAddressIndex, Instruction)
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001576#endif
1577
Alexandre Ramesef20f712015-06-09 10:29:30 +01001578#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
1579
1580#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
1581
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001582#ifndef ART_ENABLE_CODEGEN_x86
1583#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1584#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001585#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1586 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001587 M(X86LoadFromConstantTable, Instruction) \
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001588 M(X86FPNeg, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001589 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001590#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001591
Shalini Salomi Bodapatidd121f62018-10-26 15:03:53 +05301592#if defined(ART_ENABLE_CODEGEN_x86) || defined(ART_ENABLE_CODEGEN_x86_64)
1593#define FOR_EACH_CONCRETE_INSTRUCTION_X86_COMMON(M) \
Shalini Salomi Bodapati81d15be2019-05-30 11:00:42 +05301594 M(X86AndNot, Instruction) \
Shalini Salomi Bodapatib45a4352019-07-10 16:09:41 +05301595 M(X86MaskOrResetLeastSetBit, Instruction)
Shalini Salomi Bodapatidd121f62018-10-26 15:03:53 +05301596#else
1597#define FOR_EACH_CONCRETE_INSTRUCTION_X86_COMMON(M)
1598#endif
1599
Alexandre Ramesef20f712015-06-09 10:29:30 +01001600#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1601
1602#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1603 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001604 FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001605 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1606 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
1607 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
Shalini Salomi Bodapatidd121f62018-10-26 15:03:53 +05301608 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M) \
1609 FOR_EACH_CONCRETE_INSTRUCTION_X86_COMMON(M)
Alexandre Ramesef20f712015-06-09 10:29:30 +01001610
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001611#define FOR_EACH_ABSTRACT_INSTRUCTION(M) \
1612 M(Condition, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001613 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001614 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001615 M(BinaryOperation, Instruction) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001616 M(Invoke, Instruction) \
1617 M(VecOperation, Instruction) \
1618 M(VecUnaryOperation, VecOperation) \
1619 M(VecBinaryOperation, VecOperation) \
1620 M(VecMemoryOperation, VecOperation)
Dave Allison20dfc792014-06-16 20:44:29 -07001621
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001622#define FOR_EACH_INSTRUCTION(M) \
1623 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1624 FOR_EACH_ABSTRACT_INSTRUCTION(M)
1625
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001626#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001627FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1628#undef FORWARD_DECLARATION
1629
Vladimir Marko372f10e2016-05-17 16:30:10 +01001630#define DECLARE_INSTRUCTION(type) \
Artem Serovcced8ba2017-07-19 18:18:09 +01001631 private: \
1632 H##type& operator=(const H##type&) = delete; \
1633 public: \
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01001634 const char* DebugName() const override { return #type; } \
1635 HInstruction* Clone(ArenaAllocator* arena) const override { \
Artem Serovcced8ba2017-07-19 18:18:09 +01001636 DCHECK(IsClonable()); \
1637 return new (arena) H##type(*this->As##type()); \
1638 } \
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01001639 void Accept(HGraphVisitor* visitor) override
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001640
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001641#define DECLARE_ABSTRACT_INSTRUCTION(type) \
Artem Serovcced8ba2017-07-19 18:18:09 +01001642 private: \
1643 H##type& operator=(const H##type&) = delete; \
Vladimir Markoa90dd512018-05-04 15:04:45 +01001644 public:
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001645
Artem Serovcced8ba2017-07-19 18:18:09 +01001646#define DEFAULT_COPY_CONSTRUCTOR(type) \
1647 explicit H##type(const H##type& other) = default;
1648
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001649template <typename T>
Vladimir Marko82b07402017-03-01 19:02:04 +00001650class HUseListNode : public ArenaObject<kArenaAllocUseListNode>,
1651 public IntrusiveForwardListNode<HUseListNode<T>> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001652 public:
Igor Murashkind01745e2017-04-05 16:40:31 -07001653 // Get the instruction which has this use as one of the inputs.
David Brazdiled596192015-01-23 10:39:45 +00001654 T GetUser() const { return user_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07001655 // Get the position of the input record that this use corresponds to.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001656 size_t GetIndex() const { return index_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07001657 // Set the position of the input record that this use corresponds to.
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001658 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001659
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001660 private:
David Brazdiled596192015-01-23 10:39:45 +00001661 HUseListNode(T user, size_t index)
Vladimir Marko46817b82016-03-29 12:21:58 +01001662 : user_(user), index_(index) {}
David Brazdiled596192015-01-23 10:39:45 +00001663
1664 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001665 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001666
Vladimir Marko46817b82016-03-29 12:21:58 +01001667 friend class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001668
1669 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1670};
1671
David Brazdiled596192015-01-23 10:39:45 +00001672template <typename T>
Vladimir Marko46817b82016-03-29 12:21:58 +01001673using HUseList = IntrusiveForwardList<HUseListNode<T>>;
David Brazdiled596192015-01-23 10:39:45 +00001674
David Brazdil1abb4192015-02-17 18:33:36 +00001675// This class is used by HEnvironment and HInstruction classes to record the
1676// instructions they use and pointers to the corresponding HUseListNodes kept
1677// by the used instructions.
1678template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001679class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001680 public:
Vladimir Marko46817b82016-03-29 12:21:58 +01001681 HUserRecord() : instruction_(nullptr), before_use_node_() {}
1682 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), before_use_node_() {}
David Brazdil1abb4192015-02-17 18:33:36 +00001683
Vladimir Marko46817b82016-03-29 12:21:58 +01001684 HUserRecord(const HUserRecord<T>& old_record, typename HUseList<T>::iterator before_use_node)
1685 : HUserRecord(old_record.instruction_, before_use_node) {}
1686 HUserRecord(HInstruction* instruction, typename HUseList<T>::iterator before_use_node)
1687 : instruction_(instruction), before_use_node_(before_use_node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001688 DCHECK(instruction_ != nullptr);
David Brazdil1abb4192015-02-17 18:33:36 +00001689 }
1690
1691 HInstruction* GetInstruction() const { return instruction_; }
Vladimir Marko46817b82016-03-29 12:21:58 +01001692 typename HUseList<T>::iterator GetBeforeUseNode() const { return before_use_node_; }
1693 typename HUseList<T>::iterator GetUseNode() const { return ++GetBeforeUseNode(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001694
1695 private:
1696 // Instruction used by the user.
1697 HInstruction* instruction_;
1698
Vladimir Marko46817b82016-03-29 12:21:58 +01001699 // Iterator before the corresponding entry in the use list kept by 'instruction_'.
1700 typename HUseList<T>::iterator before_use_node_;
David Brazdil1abb4192015-02-17 18:33:36 +00001701};
1702
Vladimir Markoe9004912016-06-16 16:50:52 +01001703// Helper class that extracts the input instruction from HUserRecord<HInstruction*>.
1704// This is used for HInstruction::GetInputs() to return a container wrapper providing
1705// HInstruction* values even though the underlying container has HUserRecord<>s.
1706struct HInputExtractor {
1707 HInstruction* operator()(HUserRecord<HInstruction*>& record) const {
1708 return record.GetInstruction();
1709 }
1710 const HInstruction* operator()(const HUserRecord<HInstruction*>& record) const {
1711 return record.GetInstruction();
1712 }
1713};
1714
1715using HInputsRef = TransformArrayRef<HUserRecord<HInstruction*>, HInputExtractor>;
1716using HConstInputsRef = TransformArrayRef<const HUserRecord<HInstruction*>, HInputExtractor>;
1717
Aart Bik854a02b2015-07-14 16:07:00 -07001718/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001719 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001720 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001721 * For write/read dependences on fields/arrays, the dependence analysis uses
1722 * type disambiguation (e.g. a float field write cannot modify the value of an
1723 * integer field read) and the access type (e.g. a reference array write cannot
1724 * modify the value of a reference field read [although it may modify the
1725 * reference fetch prior to reading the field, which is represented by its own
1726 * write/read dependence]). The analysis makes conservative points-to
1727 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1728 * the same, and any reference read depends on any reference read without
1729 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001730 *
Artem Serovd1aa7d02018-06-22 11:35:46 +01001731 * kDependsOnGCBit is defined in the following way: instructions with kDependsOnGCBit must not be
1732 * alive across the point where garbage collection might happen.
1733 *
1734 * Note: Instructions with kCanTriggerGCBit do not depend on each other.
1735 *
1736 * kCanTriggerGCBit must be used for instructions for which GC might happen on the path across
1737 * those instructions from the compiler perspective (between this instruction and the next one
1738 * in the IR).
1739 *
1740 * Note: Instructions which can cause GC only on a fatal slow path do not need
1741 * kCanTriggerGCBit as the execution never returns to the instruction next to the exceptional
1742 * one. However the execution may return to compiled code if there is a catch block in the
1743 * current method; for this purpose the TryBoundary exit instruction has kCanTriggerGCBit
1744 * set.
1745 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001746 * The internal representation uses 38-bit and is described in the table below.
1747 * The first line indicates the side effect, and for field/array accesses the
1748 * second line indicates the type of the access (in the order of the
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001749 * DataType::Type enum).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001750 * The two numbered lines below indicate the bit position in the bitfield (read
1751 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001752 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001753 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1754 * +-------------+---------+---------+--------------+---------+---------+
1755 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1756 * | 3 |333333322|222222221| 1 |111111110|000000000|
1757 * | 7 |654321098|765432109| 8 |765432109|876543210|
1758 *
1759 * Note that, to ease the implementation, 'changes' bits are least significant
1760 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001761 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001762class SideEffects : public ValueObject {
1763 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001764 SideEffects() : flags_(0) {}
1765
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001766 static SideEffects None() {
1767 return SideEffects(0);
1768 }
1769
1770 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001771 return SideEffects(kAllChangeBits | kAllDependOnBits);
1772 }
1773
1774 static SideEffects AllChanges() {
1775 return SideEffects(kAllChangeBits);
1776 }
1777
1778 static SideEffects AllDependencies() {
1779 return SideEffects(kAllDependOnBits);
1780 }
1781
1782 static SideEffects AllExceptGCDependency() {
1783 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1784 }
1785
1786 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001787 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001788 }
1789
Aart Bik34c3ba92015-07-20 14:08:59 -07001790 static SideEffects AllWrites() {
1791 return SideEffects(kAllWrites);
1792 }
1793
1794 static SideEffects AllReads() {
1795 return SideEffects(kAllReads);
1796 }
1797
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001798 static SideEffects FieldWriteOfType(DataType::Type type, bool is_volatile) {
Aart Bik34c3ba92015-07-20 14:08:59 -07001799 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001800 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001801 : SideEffects(TypeFlag(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001802 }
1803
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001804 static SideEffects ArrayWriteOfType(DataType::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001805 return SideEffects(TypeFlag(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001806 }
1807
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001808 static SideEffects FieldReadOfType(DataType::Type type, bool is_volatile) {
Aart Bik34c3ba92015-07-20 14:08:59 -07001809 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001810 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001811 : SideEffects(TypeFlag(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001812 }
1813
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001814 static SideEffects ArrayReadOfType(DataType::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001815 return SideEffects(TypeFlag(type, kArrayReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001816 }
1817
Artem Serovd1aa7d02018-06-22 11:35:46 +01001818 // Returns whether GC might happen across this instruction from the compiler perspective so
1819 // the next instruction in the IR would see that.
1820 //
1821 // See the SideEffect class comments.
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001822 static SideEffects CanTriggerGC() {
1823 return SideEffects(1ULL << kCanTriggerGCBit);
1824 }
1825
Artem Serovd1aa7d02018-06-22 11:35:46 +01001826 // Returns whether the instruction must not be alive across a GC point.
1827 //
1828 // See the SideEffect class comments.
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001829 static SideEffects DependsOnGC() {
1830 return SideEffects(1ULL << kDependsOnGCBit);
1831 }
1832
Aart Bik854a02b2015-07-14 16:07:00 -07001833 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001834 SideEffects Union(SideEffects other) const {
1835 return SideEffects(flags_ | other.flags_);
1836 }
1837
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001838 SideEffects Exclusion(SideEffects other) const {
1839 return SideEffects(flags_ & ~other.flags_);
1840 }
1841
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001842 void Add(SideEffects other) {
1843 flags_ |= other.flags_;
1844 }
1845
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001846 bool Includes(SideEffects other) const {
1847 return (other.flags_ & flags_) == other.flags_;
1848 }
1849
1850 bool HasSideEffects() const {
1851 return (flags_ & kAllChangeBits);
1852 }
1853
1854 bool HasDependencies() const {
1855 return (flags_ & kAllDependOnBits);
1856 }
1857
1858 // Returns true if there are no side effects or dependencies.
1859 bool DoesNothing() const {
1860 return flags_ == 0;
1861 }
1862
Aart Bik854a02b2015-07-14 16:07:00 -07001863 // Returns true if something is written.
1864 bool DoesAnyWrite() const {
1865 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001866 }
1867
Aart Bik854a02b2015-07-14 16:07:00 -07001868 // Returns true if something is read.
1869 bool DoesAnyRead() const {
1870 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001871 }
1872
Aart Bik854a02b2015-07-14 16:07:00 -07001873 // Returns true if potentially everything is written and read
1874 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001875 bool DoesAllReadWrite() const {
1876 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1877 }
1878
Aart Bik854a02b2015-07-14 16:07:00 -07001879 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001880 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001881 }
1882
Roland Levillain0d5a2812015-11-13 10:07:31 +00001883 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001884 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001885 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1886 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001887 }
1888
1889 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001890 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001891 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001892 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001893 for (int s = kLastBit; s >= 0; s--) {
1894 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1895 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1896 // This is a bit for the GC side effect.
1897 if (current_bit_is_set) {
1898 flags += "GC";
1899 }
Aart Bik854a02b2015-07-14 16:07:00 -07001900 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001901 } else {
1902 // This is a bit for the array/field analysis.
1903 // The underscore character stands for the 'can trigger GC' bit.
1904 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1905 if (current_bit_is_set) {
1906 flags += kDebug[s];
1907 }
1908 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1909 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1910 flags += "|";
1911 }
1912 }
Aart Bik854a02b2015-07-14 16:07:00 -07001913 }
1914 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001915 }
1916
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001917 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001918
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001919 private:
1920 static constexpr int kFieldArrayAnalysisBits = 9;
1921
1922 static constexpr int kFieldWriteOffset = 0;
1923 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1924 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1925 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1926
1927 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1928
1929 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1930 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1931 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1932 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1933
1934 static constexpr int kLastBit = kDependsOnGCBit;
1935 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1936
1937 // Aliases.
1938
1939 static_assert(kChangeBits == kDependOnBits,
1940 "the 'change' bits should match the 'depend on' bits.");
1941
1942 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1943 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1944 static constexpr uint64_t kAllWrites =
1945 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1946 static constexpr uint64_t kAllReads =
1947 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001948
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001949 // Translates type to bit flag. The type must correspond to a Java type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001950 static uint64_t TypeFlag(DataType::Type type, int offset) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001951 int shift;
1952 switch (type) {
1953 case DataType::Type::kReference: shift = 0; break;
1954 case DataType::Type::kBool: shift = 1; break;
1955 case DataType::Type::kInt8: shift = 2; break;
1956 case DataType::Type::kUint16: shift = 3; break;
1957 case DataType::Type::kInt16: shift = 4; break;
1958 case DataType::Type::kInt32: shift = 5; break;
1959 case DataType::Type::kInt64: shift = 6; break;
1960 case DataType::Type::kFloat32: shift = 7; break;
1961 case DataType::Type::kFloat64: shift = 8; break;
1962 default:
1963 LOG(FATAL) << "Unexpected data type " << type;
1964 UNREACHABLE();
1965 }
Aart Bik854a02b2015-07-14 16:07:00 -07001966 DCHECK_LE(kFieldWriteOffset, shift);
1967 DCHECK_LT(shift, kArrayWriteOffset);
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001968 return UINT64_C(1) << (shift + offset);
Aart Bik854a02b2015-07-14 16:07:00 -07001969 }
1970
1971 // Private constructor on direct flags value.
1972 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1973
1974 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001975};
1976
David Brazdiled596192015-01-23 10:39:45 +00001977// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001978class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001979 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001980 ALWAYS_INLINE HEnvironment(ArenaAllocator* allocator,
Mingyao Yang01b47b02017-02-03 12:09:57 -08001981 size_t number_of_vregs,
1982 ArtMethod* method,
1983 uint32_t dex_pc,
1984 HInstruction* holder)
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001985 : vregs_(number_of_vregs, allocator->Adapter(kArenaAllocEnvironmentVRegs)),
1986 locations_(allocator->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001987 parent_(nullptr),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001988 method_(method),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001989 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001990 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001991 }
1992
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001993 ALWAYS_INLINE HEnvironment(ArenaAllocator* allocator,
1994 const HEnvironment& to_copy,
1995 HInstruction* holder)
1996 : HEnvironment(allocator,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001997 to_copy.Size(),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001998 to_copy.GetMethod(),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001999 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002000 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002001
Vladimir Markoec32f642017-06-02 10:51:55 +01002002 void AllocateLocations() {
2003 DCHECK(locations_.empty());
2004 locations_.resize(vregs_.size());
2005 }
2006
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002007 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002008 if (parent_ != nullptr) {
2009 parent_->SetAndCopyParentChain(allocator, parent);
2010 } else {
2011 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
2012 parent_->CopyFrom(parent);
2013 if (parent->GetParent() != nullptr) {
2014 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
2015 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002016 }
David Brazdiled596192015-01-23 10:39:45 +00002017 }
2018
Vladimir Marko69d310e2017-10-09 14:12:23 +01002019 void CopyFrom(ArrayRef<HInstruction* const> locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01002020 void CopyFrom(HEnvironment* environment);
2021
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002022 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
2023 // input to the loop phi instead. This is for inserting instructions that
2024 // require an environment (like HDeoptimization) in the loop pre-header.
2025 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00002026
2027 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01002028 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00002029 }
2030
2031 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01002032 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00002033 }
2034
David Brazdil1abb4192015-02-17 18:33:36 +00002035 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00002036
Artem Serovca210e32017-12-15 13:43:20 +00002037 // Replaces the input at the position 'index' with the replacement; the replacement and old
2038 // input instructions' env_uses_ lists are adjusted. The function works similar to
2039 // HInstruction::ReplaceInput.
2040 void ReplaceInput(HInstruction* replacement, size_t index);
2041
Vladimir Markofa6b93c2015-09-15 10:15:55 +01002042 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00002043
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002044 HEnvironment* GetParent() const { return parent_; }
2045
2046 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01002047 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002048 }
2049
2050 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01002051 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002052 }
2053
2054 uint32_t GetDexPc() const {
2055 return dex_pc_;
2056 }
2057
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00002058 ArtMethod* GetMethod() const {
2059 return method_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002060 }
2061
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002062 HInstruction* GetHolder() const {
2063 return holder_;
2064 }
2065
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00002066
2067 bool IsFromInlinedInvoke() const {
2068 return GetParent() != nullptr;
2069 }
2070
David Brazdiled596192015-01-23 10:39:45 +00002071 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01002072 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
2073 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002074 HEnvironment* parent_;
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00002075 ArtMethod* method_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002076 const uint32_t dex_pc_;
David Brazdiled596192015-01-23 10:39:45 +00002077
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01002078 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002079 HInstruction* const holder_;
2080
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01002081 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00002082
2083 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
2084};
2085
Vladimir Markof9f64412015-09-02 14:05:49 +01002086class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002087 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302088#define DECLARE_KIND(type, super) k##type,
2089 enum InstructionKind {
Vladimir Markoe3946222018-05-04 14:18:47 +01002090 FOR_EACH_CONCRETE_INSTRUCTION(DECLARE_KIND)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302091 kLastInstructionKind
2092 };
2093#undef DECLARE_KIND
2094
2095 HInstruction(InstructionKind kind, SideEffects side_effects, uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01002096 : HInstruction(kind, DataType::Type::kVoid, side_effects, dex_pc) {}
2097
2098 HInstruction(InstructionKind kind, DataType::Type type, SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002099 : previous_(nullptr),
2100 next_(nullptr),
2101 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002102 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002103 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002104 ssa_index_(-1),
Vladimir Markoa1de9182016-02-25 11:37:38 +00002105 packed_fields_(0u),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002106 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002107 locations_(nullptr),
2108 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002109 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00002110 side_effects_(side_effects),
Vladimir Markoa1de9182016-02-25 11:37:38 +00002111 reference_type_handle_(ReferenceTypeInfo::CreateInvalid().GetTypeHandle()) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302112 SetPackedField<InstructionKindField>(kind);
Vladimir Markobd785672018-05-03 17:09:09 +01002113 SetPackedField<TypeField>(type);
Vladimir Markoa1de9182016-02-25 11:37:38 +00002114 SetPackedFlag<kFlagReferenceTypeIsExact>(ReferenceTypeInfo::CreateInvalid().IsExact());
2115 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002116
Dave Allison20dfc792014-06-16 20:44:29 -07002117 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002118
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002119
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002120 HInstruction* GetNext() const { return next_; }
2121 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002122
Calin Juravle77520bc2015-01-12 18:45:46 +00002123 HInstruction* GetNextDisregardingMoves() const;
2124 HInstruction* GetPreviousDisregardingMoves() const;
2125
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002126 HBasicBlock* GetBlock() const { return block_; }
Vladimir Markoca6fff82017-10-03 14:49:14 +01002127 ArenaAllocator* GetAllocator() const { return block_->GetGraph()->GetAllocator(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002128 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002129 bool IsInBlock() const { return block_ != nullptr; }
2130 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00002131 bool IsLoopHeaderPhi() const { return IsPhi() && block_->IsLoopHeader(); }
2132 bool IsIrreducibleLoopHeaderPhi() const {
2133 return IsLoopHeaderPhi() && GetBlock()->GetLoopInformation()->IsIrreducible();
2134 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002135
Vladimir Marko372f10e2016-05-17 16:30:10 +01002136 virtual ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() = 0;
2137
2138 ArrayRef<const HUserRecord<HInstruction*>> GetInputRecords() const {
2139 // One virtual method is enough, just const_cast<> and then re-add the const.
2140 return ArrayRef<const HUserRecord<HInstruction*>>(
2141 const_cast<HInstruction*>(this)->GetInputRecords());
2142 }
2143
Vladimir Markoe9004912016-06-16 16:50:52 +01002144 HInputsRef GetInputs() {
2145 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01002146 }
2147
Vladimir Markoe9004912016-06-16 16:50:52 +01002148 HConstInputsRef GetInputs() const {
2149 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01002150 }
2151
2152 size_t InputCount() const { return GetInputRecords().size(); }
David Brazdil1abb4192015-02-17 18:33:36 +00002153 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002154
Aart Bik2767f4b2016-10-28 15:03:53 -07002155 bool HasInput(HInstruction* input) const {
2156 for (const HInstruction* i : GetInputs()) {
2157 if (i == input) {
2158 return true;
2159 }
2160 }
2161 return false;
2162 }
2163
Vladimir Marko372f10e2016-05-17 16:30:10 +01002164 void SetRawInputAt(size_t index, HInstruction* input) {
2165 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
2166 }
2167
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002168 virtual void Accept(HGraphVisitor* visitor) = 0;
2169 virtual const char* DebugName() const = 0;
2170
Vladimir Markobd785672018-05-03 17:09:09 +01002171 DataType::Type GetType() const {
2172 return TypeField::Decode(GetPackedFields());
2173 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002174
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002175 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002176
2177 uint32_t GetDexPc() const { return dex_pc_; }
2178
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002179 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01002180
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002181 // Can the instruction throw?
2182 // TODO: We should rename to CanVisiblyThrow, as some instructions (like HNewInstance),
2183 // could throw OOME, but it is still OK to remove them if they are unused.
Roland Levillaine161a2a2014-10-03 12:45:18 +01002184 virtual bool CanThrow() const { return false; }
Aart Bika8b8e9b2018-01-09 11:01:02 -08002185
2186 // Does the instruction always throw an exception unconditionally?
2187 virtual bool AlwaysThrows() const { return false; }
2188
David Brazdilec16f792015-08-19 15:04:01 +01002189 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07002190
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002191 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07002192 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002193
Calin Juravle10e244f2015-01-26 18:54:32 +00002194 // Does not apply for all instructions, but having this at top level greatly
2195 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00002196 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002197 virtual bool CanBeNull() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002198 DCHECK_EQ(GetType(), DataType::Type::kReference) << "CanBeNull only applies to reference types";
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002199 return true;
2200 }
Calin Juravle10e244f2015-01-26 18:54:32 +00002201
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002202 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01002203 return false;
2204 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002205
Nicolas Geoffray61ba8d22018-08-07 09:55:57 +01002206 // If this instruction will do an implicit null check, return the `HNullCheck` associated
2207 // with it. Otherwise return null.
2208 HNullCheck* GetImplicitNullCheck() const {
Vladimir Markoee1f1262019-06-26 14:37:17 +01002209 // Go over previous non-move instructions that are emitted at use site.
2210 HInstruction* prev_not_move = GetPreviousDisregardingMoves();
2211 while (prev_not_move != nullptr && prev_not_move->IsEmittedAtUseSite()) {
2212 if (prev_not_move->IsNullCheck()) {
2213 return prev_not_move->AsNullCheck();
2214 }
2215 prev_not_move = prev_not_move->GetPreviousDisregardingMoves();
Nicolas Geoffray61ba8d22018-08-07 09:55:57 +01002216 }
2217 return nullptr;
2218 }
2219
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00002220 virtual bool IsActualObject() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002221 return GetType() == DataType::Type::kReference;
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00002222 }
2223
Calin Juravle2e768302015-07-28 14:41:11 +00002224 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00002225
Calin Juravle61d544b2015-02-23 16:46:57 +00002226 ReferenceTypeInfo GetReferenceTypeInfo() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002227 DCHECK_EQ(GetType(), DataType::Type::kReference);
Vladimir Markoa1de9182016-02-25 11:37:38 +00002228 return ReferenceTypeInfo::CreateUnchecked(reference_type_handle_,
Vladimir Marko456307a2016-04-19 14:12:13 +00002229 GetPackedFlag<kFlagReferenceTypeIsExact>());
Calin Juravle61d544b2015-02-23 16:46:57 +00002230 }
Calin Juravleacf735c2015-02-12 15:25:22 +00002231
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002232 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00002233 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01002234 // Note: fixup_end remains valid across push_front().
2235 auto fixup_end = uses_.empty() ? uses_.begin() : ++uses_.begin();
2236 HUseListNode<HInstruction*>* new_node =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002237 new (GetBlock()->GetGraph()->GetAllocator()) HUseListNode<HInstruction*>(user, index);
Vladimir Marko46817b82016-03-29 12:21:58 +01002238 uses_.push_front(*new_node);
2239 FixUpUserRecordsAfterUseInsertion(fixup_end);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002240 }
2241
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002242 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01002243 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01002244 // Note: env_fixup_end remains valid across push_front().
2245 auto env_fixup_end = env_uses_.empty() ? env_uses_.begin() : ++env_uses_.begin();
2246 HUseListNode<HEnvironment*>* new_node =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002247 new (GetBlock()->GetGraph()->GetAllocator()) HUseListNode<HEnvironment*>(user, index);
Vladimir Marko46817b82016-03-29 12:21:58 +01002248 env_uses_.push_front(*new_node);
2249 FixUpUserRecordsAfterEnvUseInsertion(env_fixup_end);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002250 }
2251
David Brazdil1abb4192015-02-17 18:33:36 +00002252 void RemoveAsUserOfInput(size_t input) {
2253 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
Vladimir Marko46817b82016-03-29 12:21:58 +01002254 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
2255 input_use.GetInstruction()->uses_.erase_after(before_use_node);
2256 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
David Brazdil1abb4192015-02-17 18:33:36 +00002257 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002258
Vladimir Marko372f10e2016-05-17 16:30:10 +01002259 void RemoveAsUserOfAllInputs() {
2260 for (const HUserRecord<HInstruction*>& input_use : GetInputRecords()) {
2261 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
2262 input_use.GetInstruction()->uses_.erase_after(before_use_node);
2263 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
2264 }
2265 }
2266
David Brazdil1abb4192015-02-17 18:33:36 +00002267 const HUseList<HInstruction*>& GetUses() const { return uses_; }
2268 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002269
Vladimir Marko46817b82016-03-29 12:21:58 +01002270 bool HasUses() const { return !uses_.empty() || !env_uses_.empty(); }
2271 bool HasEnvironmentUses() const { return !env_uses_.empty(); }
2272 bool HasNonEnvironmentUses() const { return !uses_.empty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01002273 bool HasOnlyOneNonEnvironmentUse() const {
Vladimir Marko46817b82016-03-29 12:21:58 +01002274 return !HasEnvironmentUses() && GetUses().HasExactlyOneElement();
Alexandre Rames188d4312015-04-09 18:30:21 +01002275 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002276
Aart Bikcc42be02016-10-20 16:14:16 -07002277 bool IsRemovable() const {
Aart Bik482095d2016-10-10 15:39:10 -07002278 return
Aart Bikff7d89c2016-11-07 08:49:28 -08002279 !DoesAnyWrite() &&
Aart Bik482095d2016-10-10 15:39:10 -07002280 !CanThrow() &&
2281 !IsSuspendCheck() &&
2282 !IsControlFlow() &&
2283 !IsNativeDebugInfo() &&
2284 !IsParameterValue() &&
Aart Bik482095d2016-10-10 15:39:10 -07002285 // If we added an explicit barrier then we should keep it.
Igor Murashkind01745e2017-04-05 16:40:31 -07002286 !IsMemoryBarrier() &&
2287 !IsConstructorFence();
Aart Bik482095d2016-10-10 15:39:10 -07002288 }
2289
Aart Bikcc42be02016-10-20 16:14:16 -07002290 bool IsDeadAndRemovable() const {
2291 return IsRemovable() && !HasUses();
2292 }
2293
Roland Levillain6c82d402014-10-13 16:10:27 +01002294 // Does this instruction strictly dominate `other_instruction`?
2295 // Returns false if this instruction and `other_instruction` are the same.
2296 // Aborts if this instruction and `other_instruction` are both phis.
2297 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01002298
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002299 int GetId() const { return id_; }
2300 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002301
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002302 int GetSsaIndex() const { return ssa_index_; }
2303 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
2304 bool HasSsaIndex() const { return ssa_index_ != -1; }
2305
2306 bool HasEnvironment() const { return environment_ != nullptr; }
2307 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002308 // Set the `environment_` field. Raw because this method does not
2309 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002310 void SetRawEnvironment(HEnvironment* environment) {
2311 DCHECK(environment_ == nullptr);
2312 DCHECK_EQ(environment->GetHolder(), this);
2313 environment_ = environment;
2314 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002315
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002316 void InsertRawEnvironment(HEnvironment* environment) {
2317 DCHECK(environment_ != nullptr);
2318 DCHECK_EQ(environment->GetHolder(), this);
2319 DCHECK(environment->GetParent() == nullptr);
2320 environment->parent_ = environment_;
2321 environment_ = environment;
2322 }
2323
Vladimir Markocac5a7e2016-02-22 10:39:50 +00002324 void RemoveEnvironment();
2325
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002326 // Set the environment of this instruction, copying it from `environment`. While
2327 // copying, the uses lists are being updated.
2328 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002329 DCHECK(environment_ == nullptr);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002330 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetAllocator();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002331 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002332 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002333 if (environment->GetParent() != nullptr) {
2334 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2335 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002336 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002337
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002338 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
2339 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002340 DCHECK(environment_ == nullptr);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002341 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetAllocator();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002342 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002343 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002344 if (environment->GetParent() != nullptr) {
2345 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2346 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002347 }
2348
Nicolas Geoffray39468442014-09-02 15:17:15 +01002349 // Returns the number of entries in the environment. Typically, that is the
2350 // number of dex registers in a method. It could be more in case of inlining.
2351 size_t EnvironmentSize() const;
2352
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002353 LocationSummary* GetLocations() const { return locations_; }
2354 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002355
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002356 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00002357 void ReplaceUsesDominatedBy(HInstruction* dominator, HInstruction* replacement);
Nicolas Geoffray8a62a4c2018-07-03 09:39:07 +01002358 void ReplaceEnvUsesDominatedBy(HInstruction* dominator, HInstruction* replacement);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002359 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002360
Alexandre Rames188d4312015-04-09 18:30:21 +01002361 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
2362 // uses of this instruction by `other` are *not* updated.
2363 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
2364 ReplaceWith(other);
2365 other->ReplaceInput(this, use_index);
2366 }
2367
Alexandre Rames22aa54b2016-10-18 09:32:29 +01002368 // Move `this` instruction before `cursor`
2369 void MoveBefore(HInstruction* cursor, bool do_checks = true);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002370
Vladimir Markofb337ea2015-11-25 15:25:10 +00002371 // Move `this` before its first user and out of any loops. If there is no
2372 // out-of-loop user that dominates all other users, move the instruction
2373 // to the end of the out-of-loop common dominator of the user's blocks.
2374 //
2375 // This can be used only on non-throwing instructions with no side effects that
2376 // have at least one use but no environment uses.
2377 void MoveBeforeFirstUserAndOutOfLoops();
2378
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002379#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Markoa90dd512018-05-04 15:04:45 +01002380 bool Is##type() const;
2381
2382 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
2383#undef INSTRUCTION_TYPE_CHECK
2384
2385#define INSTRUCTION_TYPE_CAST(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002386 const H##type* As##type() const; \
2387 H##type* As##type();
2388
Vladimir Markoa90dd512018-05-04 15:04:45 +01002389 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CAST)
2390#undef INSTRUCTION_TYPE_CAST
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002391
Artem Serovcced8ba2017-07-19 18:18:09 +01002392 // Return a clone of the instruction if it is clonable (shallow copy by default, custom copy
2393 // if a custom copy-constructor is provided for a particular type). If IsClonable() is false for
2394 // the instruction then the behaviour of this function is undefined.
2395 //
2396 // Note: It is semantically valid to create a clone of the instruction only until
2397 // prepare_for_register_allocator phase as lifetime, intervals and codegen info are not
2398 // copied.
2399 //
2400 // Note: HEnvironment and some other fields are not copied and are set to default values, see
2401 // 'explicit HInstruction(const HInstruction& other)' for details.
2402 virtual HInstruction* Clone(ArenaAllocator* arena ATTRIBUTE_UNUSED) const {
2403 LOG(FATAL) << "Cloning is not implemented for the instruction " <<
2404 DebugName() << " " << GetId();
2405 UNREACHABLE();
2406 }
2407
2408 // Return whether instruction can be cloned (copied).
2409 virtual bool IsClonable() const { return false; }
2410
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002411 // Returns whether the instruction can be moved within the graph.
Aart Bik71bf7b42016-11-16 10:17:46 -08002412 // TODO: this method is used by LICM and GVN with possibly different
2413 // meanings? split and rename?
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002414 virtual bool CanBeMoved() const { return false; }
2415
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002416 // Returns whether any data encoded in the two instructions is equal.
2417 // This method does not look at the inputs. Both instructions must be
2418 // of the same type, otherwise the method has undefined behavior.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002419 virtual bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002420 return false;
2421 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002422
2423 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00002424 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002425 // 2) Their inputs are identical.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002426 bool Equals(const HInstruction* other) const;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002427
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302428 InstructionKind GetKind() const { return GetPackedField<InstructionKindField>(); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002429
2430 virtual size_t ComputeHashCode() const {
2431 size_t result = GetKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01002432 for (const HInstruction* input : GetInputs()) {
2433 result = (result * 31) + input->GetId();
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002434 }
2435 return result;
2436 }
2437
2438 SideEffects GetSideEffects() const { return side_effects_; }
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00002439 void SetSideEffects(SideEffects other) { side_effects_ = other; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002440 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002441
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002442 size_t GetLifetimePosition() const { return lifetime_position_; }
2443 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2444 LiveInterval* GetLiveInterval() const { return live_interval_; }
2445 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2446 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2447
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002448 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2449
2450 // Returns whether the code generation of the instruction will require to have access
2451 // to the current method. Such instructions are:
2452 // (1): Instructions that require an environment, as calling the runtime requires
2453 // to walk the stack and have the current method stored at a specific stack address.
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002454 // (2): HCurrentMethod, potentially used by HInvokeStaticOrDirect, HLoadString, or HLoadClass
2455 // to access the dex cache.
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002456 bool NeedsCurrentMethod() const {
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002457 return NeedsEnvironment() || IsCurrentMethod();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002458 }
2459
Vladimir Markodc151b22015-10-15 18:02:30 +01002460 // Returns whether the code generation of the instruction will require to have access
2461 // to the dex cache of the current method's declaring class via the current method.
2462 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002463
Mark Mendellc4701932015-04-10 13:18:51 -04002464 // Does this instruction have any use in an environment before
2465 // control flow hits 'other'?
2466 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2467
2468 // Remove all references to environment uses of this instruction.
2469 // The caller must ensure that this is safe to do.
2470 void RemoveEnvironmentUsers();
2471
Vladimir Markoa1de9182016-02-25 11:37:38 +00002472 bool IsEmittedAtUseSite() const { return GetPackedFlag<kFlagEmittedAtUseSite>(); }
2473 void MarkEmittedAtUseSite() { SetPackedFlag<kFlagEmittedAtUseSite>(true); }
David Brazdilb3e773e2016-01-26 11:28:37 +00002474
David Brazdil1abb4192015-02-17 18:33:36 +00002475 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002476 // If set, the machine code for this instruction is assumed to be generated by
2477 // its users. Used by liveness analysis to compute use positions accordingly.
2478 static constexpr size_t kFlagEmittedAtUseSite = 0u;
2479 static constexpr size_t kFlagReferenceTypeIsExact = kFlagEmittedAtUseSite + 1;
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302480 static constexpr size_t kFieldInstructionKind = kFlagReferenceTypeIsExact + 1;
2481 static constexpr size_t kFieldInstructionKindSize =
2482 MinimumBitsToStore(static_cast<size_t>(InstructionKind::kLastInstructionKind - 1));
Vladimir Markobd785672018-05-03 17:09:09 +01002483 static constexpr size_t kFieldType =
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302484 kFieldInstructionKind + kFieldInstructionKindSize;
Vladimir Markobd785672018-05-03 17:09:09 +01002485 static constexpr size_t kFieldTypeSize =
2486 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
2487 static constexpr size_t kNumberOfGenericPackedBits = kFieldType + kFieldTypeSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00002488 static constexpr size_t kMaxNumberOfPackedBits = sizeof(uint32_t) * kBitsPerByte;
2489
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302490 static_assert(kNumberOfGenericPackedBits <= kMaxNumberOfPackedBits,
2491 "Too many generic packed fields");
2492
Vladimir Markobd785672018-05-03 17:09:09 +01002493 using TypeField = BitField<DataType::Type, kFieldType, kFieldTypeSize>;
2494
Vladimir Marko372f10e2016-05-17 16:30:10 +01002495 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const {
2496 return GetInputRecords()[i];
2497 }
2498
2499 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) {
2500 ArrayRef<HUserRecord<HInstruction*>> input_records = GetInputRecords();
2501 input_records[index] = input;
2502 }
David Brazdil1abb4192015-02-17 18:33:36 +00002503
Vladimir Markoa1de9182016-02-25 11:37:38 +00002504 uint32_t GetPackedFields() const {
2505 return packed_fields_;
2506 }
2507
2508 template <size_t flag>
2509 bool GetPackedFlag() const {
2510 return (packed_fields_ & (1u << flag)) != 0u;
2511 }
2512
2513 template <size_t flag>
2514 void SetPackedFlag(bool value = true) {
2515 packed_fields_ = (packed_fields_ & ~(1u << flag)) | ((value ? 1u : 0u) << flag);
2516 }
2517
2518 template <typename BitFieldType>
2519 typename BitFieldType::value_type GetPackedField() const {
2520 return BitFieldType::Decode(packed_fields_);
2521 }
2522
2523 template <typename BitFieldType>
2524 void SetPackedField(typename BitFieldType::value_type value) {
2525 DCHECK(IsUint<BitFieldType::size>(static_cast<uintptr_t>(value)));
2526 packed_fields_ = BitFieldType::Update(value, packed_fields_);
2527 }
2528
Artem Serovcced8ba2017-07-19 18:18:09 +01002529 // Copy construction for the instruction (used for Clone function).
2530 //
2531 // Fields (e.g. lifetime, intervals and codegen info) associated with phases starting from
2532 // prepare_for_register_allocator are not copied (set to default values).
2533 //
2534 // Copy constructors must be provided for every HInstruction type; default copy constructor is
2535 // fine for most of them. However for some of the instructions a custom copy constructor must be
2536 // specified (when instruction has non-trivially copyable fields and must have a special behaviour
2537 // for copying them).
2538 explicit HInstruction(const HInstruction& other)
2539 : previous_(nullptr),
2540 next_(nullptr),
2541 block_(nullptr),
2542 dex_pc_(other.dex_pc_),
2543 id_(-1),
2544 ssa_index_(-1),
2545 packed_fields_(other.packed_fields_),
2546 environment_(nullptr),
2547 locations_(nullptr),
2548 live_interval_(nullptr),
2549 lifetime_position_(kNoLifetime),
2550 side_effects_(other.side_effects_),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302551 reference_type_handle_(other.reference_type_handle_) {
2552 }
Artem Serovcced8ba2017-07-19 18:18:09 +01002553
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002554 private:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302555 using InstructionKindField =
2556 BitField<InstructionKind, kFieldInstructionKind, kFieldInstructionKindSize>;
2557
Vladimir Marko46817b82016-03-29 12:21:58 +01002558 void FixUpUserRecordsAfterUseInsertion(HUseList<HInstruction*>::iterator fixup_end) {
2559 auto before_use_node = uses_.before_begin();
2560 for (auto use_node = uses_.begin(); use_node != fixup_end; ++use_node) {
2561 HInstruction* user = use_node->GetUser();
2562 size_t input_index = use_node->GetIndex();
2563 user->SetRawInputRecordAt(input_index, HUserRecord<HInstruction*>(this, before_use_node));
2564 before_use_node = use_node;
2565 }
2566 }
2567
2568 void FixUpUserRecordsAfterUseRemoval(HUseList<HInstruction*>::iterator before_use_node) {
2569 auto next = ++HUseList<HInstruction*>::iterator(before_use_node);
2570 if (next != uses_.end()) {
2571 HInstruction* next_user = next->GetUser();
2572 size_t next_index = next->GetIndex();
2573 DCHECK(next_user->InputRecordAt(next_index).GetInstruction() == this);
2574 next_user->SetRawInputRecordAt(next_index, HUserRecord<HInstruction*>(this, before_use_node));
2575 }
2576 }
2577
2578 void FixUpUserRecordsAfterEnvUseInsertion(HUseList<HEnvironment*>::iterator env_fixup_end) {
2579 auto before_env_use_node = env_uses_.before_begin();
2580 for (auto env_use_node = env_uses_.begin(); env_use_node != env_fixup_end; ++env_use_node) {
2581 HEnvironment* user = env_use_node->GetUser();
2582 size_t input_index = env_use_node->GetIndex();
2583 user->vregs_[input_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2584 before_env_use_node = env_use_node;
2585 }
2586 }
2587
2588 void FixUpUserRecordsAfterEnvUseRemoval(HUseList<HEnvironment*>::iterator before_env_use_node) {
2589 auto next = ++HUseList<HEnvironment*>::iterator(before_env_use_node);
2590 if (next != env_uses_.end()) {
2591 HEnvironment* next_user = next->GetUser();
2592 size_t next_index = next->GetIndex();
2593 DCHECK(next_user->vregs_[next_index].GetInstruction() == this);
2594 next_user->vregs_[next_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2595 }
2596 }
David Brazdil1abb4192015-02-17 18:33:36 +00002597
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002598 HInstruction* previous_;
2599 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002600 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002601 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002602
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002603 // An instruction gets an id when it is added to the graph.
2604 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002605 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002606 int id_;
2607
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002608 // When doing liveness analysis, instructions that have uses get an SSA index.
2609 int ssa_index_;
2610
Vladimir Markoa1de9182016-02-25 11:37:38 +00002611 // Packed fields.
2612 uint32_t packed_fields_;
David Brazdilb3e773e2016-01-26 11:28:37 +00002613
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002614 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002615 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002616
2617 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002618 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002619
Nicolas Geoffray39468442014-09-02 15:17:15 +01002620 // The environment associated with this instruction. Not null if the instruction
2621 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002622 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002623
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002624 // Set by the code generator.
2625 LocationSummary* locations_;
2626
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002627 // Set by the liveness analysis.
2628 LiveInterval* live_interval_;
2629
2630 // Set by the liveness analysis, this is the position in a linear
2631 // order of blocks where this instruction's live interval start.
2632 size_t lifetime_position_;
2633
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002634 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002635
Vladimir Markoa1de9182016-02-25 11:37:38 +00002636 // The reference handle part of the reference type info.
2637 // The IsExact() flag is stored in packed fields.
Calin Juravleacf735c2015-02-12 15:25:22 +00002638 // TODO: for primitive types this should be marked as invalid.
Vladimir Markoa1de9182016-02-25 11:37:38 +00002639 ReferenceTypeInfo::TypeHandle reference_type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00002640
David Brazdil1abb4192015-02-17 18:33:36 +00002641 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002642 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002643 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002644 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002645 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002646};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002647std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002648
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002649// Iterates over the instructions, while preserving the next instruction
2650// in case the current instruction gets removed from the list by the user
2651// of this iterator.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002652class HInstructionIterator : public ValueObject {
2653 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002654 explicit HInstructionIterator(const HInstructionList& instructions)
2655 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002656 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002657 }
2658
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002659 bool Done() const { return instruction_ == nullptr; }
2660 HInstruction* Current() const { return instruction_; }
2661 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002662 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002663 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002664 }
2665
2666 private:
2667 HInstruction* instruction_;
2668 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002669
2670 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002671};
2672
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002673// Iterates over the instructions without saving the next instruction,
2674// therefore handling changes in the graph potentially made by the user
2675// of this iterator.
2676class HInstructionIteratorHandleChanges : public ValueObject {
2677 public:
2678 explicit HInstructionIteratorHandleChanges(const HInstructionList& instructions)
2679 : instruction_(instructions.first_instruction_) {
2680 }
2681
2682 bool Done() const { return instruction_ == nullptr; }
2683 HInstruction* Current() const { return instruction_; }
2684 void Advance() {
2685 instruction_ = instruction_->GetNext();
2686 }
2687
2688 private:
2689 HInstruction* instruction_;
2690
2691 DISALLOW_COPY_AND_ASSIGN(HInstructionIteratorHandleChanges);
2692};
2693
2694
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002695class HBackwardInstructionIterator : public ValueObject {
2696 public:
2697 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2698 : instruction_(instructions.last_instruction_) {
2699 next_ = Done() ? nullptr : instruction_->GetPrevious();
2700 }
2701
2702 bool Done() const { return instruction_ == nullptr; }
2703 HInstruction* Current() const { return instruction_; }
2704 void Advance() {
2705 instruction_ = next_;
2706 next_ = Done() ? nullptr : instruction_->GetPrevious();
2707 }
2708
2709 private:
2710 HInstruction* instruction_;
2711 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002712
2713 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002714};
2715
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002716class HVariableInputSizeInstruction : public HInstruction {
2717 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08002718 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002719 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() override {
Mingyao Yangb0b051a2016-11-17 09:04:53 -08002720 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
2721 }
2722
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002723 void AddInput(HInstruction* input);
2724 void InsertInputAt(size_t index, HInstruction* input);
2725 void RemoveInputAt(size_t index);
2726
Igor Murashkind01745e2017-04-05 16:40:31 -07002727 // Removes all the inputs.
2728 // Also removes this instructions from each input's use list
2729 // (for non-environment uses only).
2730 void RemoveAllInputs();
2731
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002732 protected:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302733 HVariableInputSizeInstruction(InstructionKind inst_kind,
2734 SideEffects side_effects,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002735 uint32_t dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002736 ArenaAllocator* allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002737 size_t number_of_inputs,
2738 ArenaAllocKind kind)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302739 : HInstruction(inst_kind, side_effects, dex_pc),
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002740 inputs_(number_of_inputs, allocator->Adapter(kind)) {}
Vladimir Markobd785672018-05-03 17:09:09 +01002741 HVariableInputSizeInstruction(InstructionKind inst_kind,
2742 DataType::Type type,
2743 SideEffects side_effects,
2744 uint32_t dex_pc,
2745 ArenaAllocator* allocator,
2746 size_t number_of_inputs,
2747 ArenaAllocKind kind)
2748 : HInstruction(inst_kind, type, side_effects, dex_pc),
2749 inputs_(number_of_inputs, allocator->Adapter(kind)) {}
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002750
Artem Serovcced8ba2017-07-19 18:18:09 +01002751 DEFAULT_COPY_CONSTRUCTOR(VariableInputSizeInstruction);
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002752
Artem Serovcced8ba2017-07-19 18:18:09 +01002753 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002754};
2755
Vladimir Markof9f64412015-09-02 14:05:49 +01002756template<size_t N>
Vladimir Markobd785672018-05-03 17:09:09 +01002757class HExpression : public HInstruction {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002758 public:
Vladimir Markobd785672018-05-03 17:09:09 +01002759 HExpression<N>(InstructionKind kind, SideEffects side_effects, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302760 : HInstruction(kind, side_effects, dex_pc), inputs_() {}
Vladimir Markobd785672018-05-03 17:09:09 +01002761 HExpression<N>(InstructionKind kind,
2762 DataType::Type type,
2763 SideEffects side_effects,
2764 uint32_t dex_pc)
2765 : HInstruction(kind, type, side_effects, dex_pc), inputs_() {}
2766 virtual ~HExpression() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002767
Vladimir Marko372f10e2016-05-17 16:30:10 +01002768 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01002769 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Vladimir Marko372f10e2016-05-17 16:30:10 +01002770 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002771 }
2772
Artem Serovcced8ba2017-07-19 18:18:09 +01002773 protected:
Vladimir Markobd785672018-05-03 17:09:09 +01002774 DEFAULT_COPY_CONSTRUCTOR(Expression<N>);
Artem Serovcced8ba2017-07-19 18:18:09 +01002775
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002776 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002777 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002778
2779 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002780};
2781
Vladimir Markobd785672018-05-03 17:09:09 +01002782// HExpression specialization for N=0.
Vladimir Markof9f64412015-09-02 14:05:49 +01002783template<>
Vladimir Markobd785672018-05-03 17:09:09 +01002784class HExpression<0> : public HInstruction {
Vladimir Markof9f64412015-09-02 14:05:49 +01002785 public:
Vladimir Markobd785672018-05-03 17:09:09 +01002786 using HInstruction::HInstruction;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002787
Vladimir Markobd785672018-05-03 17:09:09 +01002788 virtual ~HExpression() {}
Vladimir Markof9f64412015-09-02 14:05:49 +01002789
Vladimir Marko372f10e2016-05-17 16:30:10 +01002790 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01002791 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Vladimir Marko372f10e2016-05-17 16:30:10 +01002792 return ArrayRef<HUserRecord<HInstruction*>>();
Vladimir Markof9f64412015-09-02 14:05:49 +01002793 }
2794
Artem Serovcced8ba2017-07-19 18:18:09 +01002795 protected:
Vladimir Markobd785672018-05-03 17:09:09 +01002796 DEFAULT_COPY_CONSTRUCTOR(Expression<0>);
Artem Serovcced8ba2017-07-19 18:18:09 +01002797
Vladimir Markof9f64412015-09-02 14:05:49 +01002798 private:
2799 friend class SsaBuilder;
2800};
2801
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002802// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2803// instruction that branches to the exit block.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002804class HReturnVoid final : public HExpression<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002805 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002806 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01002807 : HExpression(kReturnVoid, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302808 }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002809
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002810 bool IsControlFlow() const override { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002811
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002812 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002813
Artem Serovcced8ba2017-07-19 18:18:09 +01002814 protected:
2815 DEFAULT_COPY_CONSTRUCTOR(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002816};
2817
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002818// Represents dex's RETURN opcodes. A HReturn is a control flow
2819// instruction that branches to the exit block.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002820class HReturn final : public HExpression<1> {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002821 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002822 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01002823 : HExpression(kReturn, SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002824 SetRawInputAt(0, value);
2825 }
2826
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002827 bool IsControlFlow() const override { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002828
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002829 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002830
Artem Serovcced8ba2017-07-19 18:18:09 +01002831 protected:
2832 DEFAULT_COPY_CONSTRUCTOR(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002833};
2834
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002835class HPhi final : public HVariableInputSizeInstruction {
David Brazdildee58d62016-04-07 09:54:26 +00002836 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002837 HPhi(ArenaAllocator* allocator,
David Brazdildee58d62016-04-07 09:54:26 +00002838 uint32_t reg_number,
2839 size_t number_of_inputs,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002840 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +00002841 uint32_t dex_pc = kNoDexPc)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002842 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302843 kPhi,
Vladimir Markobd785672018-05-03 17:09:09 +01002844 ToPhiType(type),
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002845 SideEffects::None(),
2846 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002847 allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002848 number_of_inputs,
2849 kArenaAllocPhiInputs),
David Brazdildee58d62016-04-07 09:54:26 +00002850 reg_number_(reg_number) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002851 DCHECK_NE(GetType(), DataType::Type::kVoid);
David Brazdildee58d62016-04-07 09:54:26 +00002852 // Phis are constructed live and marked dead if conflicting or unused.
2853 // Individual steps of SsaBuilder should assume that if a phi has been
2854 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
2855 SetPackedFlag<kFlagIsLive>(true);
2856 SetPackedFlag<kFlagCanBeNull>(true);
2857 }
2858
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002859 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01002860
David Brazdildee58d62016-04-07 09:54:26 +00002861 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002862 static DataType::Type ToPhiType(DataType::Type type) {
2863 return DataType::Kind(type);
David Brazdildee58d62016-04-07 09:54:26 +00002864 }
2865
2866 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
2867
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002868 void SetType(DataType::Type new_type) {
David Brazdildee58d62016-04-07 09:54:26 +00002869 // Make sure that only valid type changes occur. The following are allowed:
2870 // (1) int -> float/ref (primitive type propagation),
2871 // (2) long -> double (primitive type propagation).
2872 DCHECK(GetType() == new_type ||
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002873 (GetType() == DataType::Type::kInt32 && new_type == DataType::Type::kFloat32) ||
2874 (GetType() == DataType::Type::kInt32 && new_type == DataType::Type::kReference) ||
2875 (GetType() == DataType::Type::kInt64 && new_type == DataType::Type::kFloat64));
David Brazdildee58d62016-04-07 09:54:26 +00002876 SetPackedField<TypeField>(new_type);
2877 }
2878
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002879 bool CanBeNull() const override { return GetPackedFlag<kFlagCanBeNull>(); }
David Brazdildee58d62016-04-07 09:54:26 +00002880 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
2881
2882 uint32_t GetRegNumber() const { return reg_number_; }
2883
2884 void SetDead() { SetPackedFlag<kFlagIsLive>(false); }
2885 void SetLive() { SetPackedFlag<kFlagIsLive>(true); }
2886 bool IsDead() const { return !IsLive(); }
2887 bool IsLive() const { return GetPackedFlag<kFlagIsLive>(); }
2888
Vladimir Markoe9004912016-06-16 16:50:52 +01002889 bool IsVRegEquivalentOf(const HInstruction* other) const {
David Brazdildee58d62016-04-07 09:54:26 +00002890 return other != nullptr
2891 && other->IsPhi()
2892 && other->AsPhi()->GetBlock() == GetBlock()
2893 && other->AsPhi()->GetRegNumber() == GetRegNumber();
2894 }
2895
Nicolas Geoffrayf57c1ae2017-06-28 17:40:18 +01002896 bool HasEquivalentPhi() const {
2897 if (GetPrevious() != nullptr && GetPrevious()->AsPhi()->GetRegNumber() == GetRegNumber()) {
2898 return true;
2899 }
2900 if (GetNext() != nullptr && GetNext()->AsPhi()->GetRegNumber() == GetRegNumber()) {
2901 return true;
2902 }
2903 return false;
2904 }
2905
David Brazdildee58d62016-04-07 09:54:26 +00002906 // Returns the next equivalent phi (starting from the current one) or null if there is none.
2907 // An equivalent phi is a phi having the same dex register and type.
2908 // It assumes that phis with the same dex register are adjacent.
2909 HPhi* GetNextEquivalentPhiWithSameType() {
2910 HInstruction* next = GetNext();
2911 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
2912 if (next->GetType() == GetType()) {
2913 return next->AsPhi();
2914 }
2915 next = next->GetNext();
2916 }
2917 return nullptr;
2918 }
2919
2920 DECLARE_INSTRUCTION(Phi);
2921
Artem Serovcced8ba2017-07-19 18:18:09 +01002922 protected:
2923 DEFAULT_COPY_CONSTRUCTOR(Phi);
2924
David Brazdildee58d62016-04-07 09:54:26 +00002925 private:
Vladimir Markobd785672018-05-03 17:09:09 +01002926 static constexpr size_t kFlagIsLive = HInstruction::kNumberOfGenericPackedBits;
David Brazdildee58d62016-04-07 09:54:26 +00002927 static constexpr size_t kFlagCanBeNull = kFlagIsLive + 1;
2928 static constexpr size_t kNumberOfPhiPackedBits = kFlagCanBeNull + 1;
2929 static_assert(kNumberOfPhiPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
David Brazdildee58d62016-04-07 09:54:26 +00002930
David Brazdildee58d62016-04-07 09:54:26 +00002931 const uint32_t reg_number_;
David Brazdildee58d62016-04-07 09:54:26 +00002932};
2933
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002934// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002935// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002936// exit block.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002937class HExit final : public HExpression<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002938 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302939 explicit HExit(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01002940 : HExpression(kExit, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302941 }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002942
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002943 bool IsControlFlow() const override { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002944
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002945 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002946
Artem Serovcced8ba2017-07-19 18:18:09 +01002947 protected:
2948 DEFAULT_COPY_CONSTRUCTOR(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002949};
2950
2951// Jumps from one block to another.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002952class HGoto final : public HExpression<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002953 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302954 explicit HGoto(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01002955 : HExpression(kGoto, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302956 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002957
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002958 bool IsClonable() const override { return true; }
2959 bool IsControlFlow() const override { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002960
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002961 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002962 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002963 }
2964
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002965 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002966
Artem Serovcced8ba2017-07-19 18:18:09 +01002967 protected:
2968 DEFAULT_COPY_CONSTRUCTOR(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002969};
2970
Roland Levillain9867bc72015-08-05 10:21:34 +01002971class HConstant : public HExpression<0> {
2972 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302973 explicit HConstant(InstructionKind kind, DataType::Type type, uint32_t dex_pc = kNoDexPc)
2974 : HExpression(kind, type, SideEffects::None(), dex_pc) {
2975 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002976
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002977 bool CanBeMoved() const override { return true; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002978
Roland Levillain1a653882016-03-18 18:05:57 +00002979 // Is this constant -1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002980 virtual bool IsMinusOne() const { return false; }
Roland Levillain1a653882016-03-18 18:05:57 +00002981 // Is this constant 0 in the arithmetic sense?
2982 virtual bool IsArithmeticZero() const { return false; }
2983 // Is this constant a 0-bit pattern?
2984 virtual bool IsZeroBitPattern() const { return false; }
2985 // Is this constant 1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002986 virtual bool IsOne() const { return false; }
2987
David Brazdil77a48ae2015-09-15 12:34:04 +00002988 virtual uint64_t GetValueAsUint64() const = 0;
2989
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002990 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002991
Artem Serovcced8ba2017-07-19 18:18:09 +01002992 protected:
2993 DEFAULT_COPY_CONSTRUCTOR(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002994};
2995
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002996class HNullConstant final : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002997 public:
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002998 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01002999 return true;
3000 }
3001
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003002 uint64_t GetValueAsUint64() const override { return 0; }
David Brazdil77a48ae2015-09-15 12:34:04 +00003003
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003004 size_t ComputeHashCode() const override { return 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01003005
Roland Levillain1a653882016-03-18 18:05:57 +00003006 // The null constant representation is a 0-bit pattern.
Yi Kong39402542019-03-24 02:47:16 -07003007 bool IsZeroBitPattern() const override { return true; }
Roland Levillain1a653882016-03-18 18:05:57 +00003008
Roland Levillain9867bc72015-08-05 10:21:34 +01003009 DECLARE_INSTRUCTION(NullConstant);
3010
Artem Serovcced8ba2017-07-19 18:18:09 +01003011 protected:
3012 DEFAULT_COPY_CONSTRUCTOR(NullConstant);
3013
Roland Levillain9867bc72015-08-05 10:21:34 +01003014 private:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003015 explicit HNullConstant(uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303016 : HConstant(kNullConstant, DataType::Type::kReference, dex_pc) {
3017 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003018
3019 friend class HGraph;
Roland Levillain9867bc72015-08-05 10:21:34 +01003020};
3021
3022// Constants of the type int. Those can be from Dex instructions, or
3023// synthesized (for example with the if-eqz instruction).
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003024class HIntConstant final : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01003025 public:
3026 int32_t GetValue() const { return value_; }
3027
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003028 uint64_t GetValueAsUint64() const override {
David Brazdil9f389d42015-10-01 14:32:56 +01003029 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
3030 }
David Brazdil77a48ae2015-09-15 12:34:04 +00003031
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003032 bool InstructionDataEquals(const HInstruction* other) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003033 DCHECK(other->IsIntConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01003034 return other->AsIntConstant()->value_ == value_;
3035 }
3036
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003037 size_t ComputeHashCode() const override { return GetValue(); }
Roland Levillain9867bc72015-08-05 10:21:34 +01003038
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003039 bool IsMinusOne() const override { return GetValue() == -1; }
3040 bool IsArithmeticZero() const override { return GetValue() == 0; }
3041 bool IsZeroBitPattern() const override { return GetValue() == 0; }
3042 bool IsOne() const override { return GetValue() == 1; }
Roland Levillain9867bc72015-08-05 10:21:34 +01003043
Roland Levillain1a653882016-03-18 18:05:57 +00003044 // Integer constants are used to encode Boolean values as well,
3045 // where 1 means true and 0 means false.
3046 bool IsTrue() const { return GetValue() == 1; }
3047 bool IsFalse() const { return GetValue() == 0; }
3048
Roland Levillain9867bc72015-08-05 10:21:34 +01003049 DECLARE_INSTRUCTION(IntConstant);
3050
Artem Serovcced8ba2017-07-19 18:18:09 +01003051 protected:
3052 DEFAULT_COPY_CONSTRUCTOR(IntConstant);
3053
Roland Levillain9867bc72015-08-05 10:21:34 +01003054 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003055 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303056 : HConstant(kIntConstant, DataType::Type::kInt32, dex_pc), value_(value) {
3057 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003058 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303059 : HConstant(kIntConstant, DataType::Type::kInt32, dex_pc),
3060 value_(value ? 1 : 0) {
3061 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003062
3063 const int32_t value_;
3064
3065 friend class HGraph;
3066 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
3067 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
Roland Levillain9867bc72015-08-05 10:21:34 +01003068};
3069
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003070class HLongConstant final : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01003071 public:
3072 int64_t GetValue() const { return value_; }
3073
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003074 uint64_t GetValueAsUint64() const override { return value_; }
David Brazdil77a48ae2015-09-15 12:34:04 +00003075
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003076 bool InstructionDataEquals(const HInstruction* other) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003077 DCHECK(other->IsLongConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01003078 return other->AsLongConstant()->value_ == value_;
3079 }
3080
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003081 size_t ComputeHashCode() const override { return static_cast<size_t>(GetValue()); }
Roland Levillain9867bc72015-08-05 10:21:34 +01003082
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003083 bool IsMinusOne() const override { return GetValue() == -1; }
3084 bool IsArithmeticZero() const override { return GetValue() == 0; }
3085 bool IsZeroBitPattern() const override { return GetValue() == 0; }
3086 bool IsOne() const override { return GetValue() == 1; }
Roland Levillain9867bc72015-08-05 10:21:34 +01003087
3088 DECLARE_INSTRUCTION(LongConstant);
3089
Artem Serovcced8ba2017-07-19 18:18:09 +01003090 protected:
3091 DEFAULT_COPY_CONSTRUCTOR(LongConstant);
3092
Roland Levillain9867bc72015-08-05 10:21:34 +01003093 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003094 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303095 : HConstant(kLongConstant, DataType::Type::kInt64, dex_pc),
3096 value_(value) {
3097 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003098
3099 const int64_t value_;
3100
3101 friend class HGraph;
Roland Levillain9867bc72015-08-05 10:21:34 +01003102};
Dave Allison20dfc792014-06-16 20:44:29 -07003103
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003104class HFloatConstant final : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003105 public:
3106 float GetValue() const { return value_; }
3107
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003108 uint64_t GetValueAsUint64() const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003109 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
3110 }
3111
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003112 bool InstructionDataEquals(const HInstruction* other) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003113 DCHECK(other->IsFloatConstant()) << other->DebugName();
3114 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
3115 }
3116
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003117 size_t ComputeHashCode() const override { return static_cast<size_t>(GetValue()); }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003118
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003119 bool IsMinusOne() const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003120 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
3121 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003122 bool IsArithmeticZero() const override {
Roland Levillain1a653882016-03-18 18:05:57 +00003123 return std::fpclassify(value_) == FP_ZERO;
3124 }
3125 bool IsArithmeticPositiveZero() const {
3126 return IsArithmeticZero() && !std::signbit(value_);
3127 }
3128 bool IsArithmeticNegativeZero() const {
3129 return IsArithmeticZero() && std::signbit(value_);
3130 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003131 bool IsZeroBitPattern() const override {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00003132 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(0.0f);
Roland Levillain31dd3d62016-02-16 12:21:02 +00003133 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003134 bool IsOne() const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003135 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
3136 }
3137 bool IsNaN() const {
3138 return std::isnan(value_);
3139 }
3140
3141 DECLARE_INSTRUCTION(FloatConstant);
3142
Artem Serovcced8ba2017-07-19 18:18:09 +01003143 protected:
3144 DEFAULT_COPY_CONSTRUCTOR(FloatConstant);
3145
Roland Levillain31dd3d62016-02-16 12:21:02 +00003146 private:
3147 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303148 : HConstant(kFloatConstant, DataType::Type::kFloat32, dex_pc),
3149 value_(value) {
3150 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003151 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303152 : HConstant(kFloatConstant, DataType::Type::kFloat32, dex_pc),
3153 value_(bit_cast<float, int32_t>(value)) {
3154 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003155
3156 const float value_;
3157
3158 // Only the SsaBuilder and HGraph can create floating-point constants.
3159 friend class SsaBuilder;
3160 friend class HGraph;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003161};
3162
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003163class HDoubleConstant final : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003164 public:
3165 double GetValue() const { return value_; }
3166
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003167 uint64_t GetValueAsUint64() const override { return bit_cast<uint64_t, double>(value_); }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003168
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003169 bool InstructionDataEquals(const HInstruction* other) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003170 DCHECK(other->IsDoubleConstant()) << other->DebugName();
3171 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
3172 }
3173
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003174 size_t ComputeHashCode() const override { return static_cast<size_t>(GetValue()); }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003175
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003176 bool IsMinusOne() const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003177 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
3178 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003179 bool IsArithmeticZero() const override {
Roland Levillain1a653882016-03-18 18:05:57 +00003180 return std::fpclassify(value_) == FP_ZERO;
3181 }
3182 bool IsArithmeticPositiveZero() const {
3183 return IsArithmeticZero() && !std::signbit(value_);
3184 }
3185 bool IsArithmeticNegativeZero() const {
3186 return IsArithmeticZero() && std::signbit(value_);
3187 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003188 bool IsZeroBitPattern() const override {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00003189 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((0.0));
Roland Levillain31dd3d62016-02-16 12:21:02 +00003190 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003191 bool IsOne() const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003192 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
3193 }
3194 bool IsNaN() const {
3195 return std::isnan(value_);
3196 }
3197
3198 DECLARE_INSTRUCTION(DoubleConstant);
3199
Artem Serovcced8ba2017-07-19 18:18:09 +01003200 protected:
3201 DEFAULT_COPY_CONSTRUCTOR(DoubleConstant);
3202
Roland Levillain31dd3d62016-02-16 12:21:02 +00003203 private:
3204 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303205 : HConstant(kDoubleConstant, DataType::Type::kFloat64, dex_pc),
3206 value_(value) {
3207 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003208 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303209 : HConstant(kDoubleConstant, DataType::Type::kFloat64, dex_pc),
3210 value_(bit_cast<double, int64_t>(value)) {
3211 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003212
3213 const double value_;
3214
3215 // Only the SsaBuilder and HGraph can create floating-point constants.
3216 friend class SsaBuilder;
3217 friend class HGraph;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003218};
3219
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003220// Conditional branch. A block ending with an HIf instruction must have
3221// two successors.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003222class HIf final : public HExpression<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003223 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003224 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01003225 : HExpression(kIf, SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003226 SetRawInputAt(0, input);
3227 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003228
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003229 bool IsClonable() const override { return true; }
3230 bool IsControlFlow() const override { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003231
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003232 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01003233 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003234 }
3235
3236 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01003237 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003238 }
3239
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003240 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003241
Artem Serovcced8ba2017-07-19 18:18:09 +01003242 protected:
3243 DEFAULT_COPY_CONSTRUCTOR(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003244};
3245
David Brazdilfc6a86a2015-06-26 10:33:45 +00003246
3247// Abstract instruction which marks the beginning and/or end of a try block and
3248// links it to the respective exception handlers. Behaves the same as a Goto in
3249// non-exceptional control flow.
3250// Normal-flow successor is stored at index zero, exception handlers under
3251// higher indices in no particular order.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003252class HTryBoundary final : public HExpression<0> {
David Brazdilfc6a86a2015-06-26 10:33:45 +00003253 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003254 enum class BoundaryKind {
David Brazdil56e1acc2015-06-30 15:41:36 +01003255 kEntry,
3256 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00003257 kLast = kExit
David Brazdil56e1acc2015-06-30 15:41:36 +01003258 };
3259
Artem Serovd1aa7d02018-06-22 11:35:46 +01003260 // SideEffects::CanTriggerGC prevents instructions with SideEffects::DependOnGC to be alive
3261 // across the catch block entering edges as GC might happen during throwing an exception.
3262 // TryBoundary with BoundaryKind::kExit is conservatively used for that as there is no
3263 // HInstruction which a catch block must start from.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003264 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
Artem Serovd1aa7d02018-06-22 11:35:46 +01003265 : HExpression(kTryBoundary,
3266 (kind == BoundaryKind::kExit) ? SideEffects::CanTriggerGC()
3267 : SideEffects::None(),
3268 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003269 SetPackedField<BoundaryKindField>(kind);
3270 }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003271
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003272 bool IsControlFlow() const override { return true; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003273
3274 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01003275 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003276
David Brazdild26a4112015-11-10 11:07:31 +00003277 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
3278 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
3279 }
3280
David Brazdilfc6a86a2015-06-26 10:33:45 +00003281 // Returns whether `handler` is among its exception handlers (non-zero index
3282 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01003283 bool HasExceptionHandler(const HBasicBlock& handler) const {
3284 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00003285 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00003286 }
3287
3288 // If not present already, adds `handler` to its block's list of exception
3289 // handlers.
3290 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01003291 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00003292 GetBlock()->AddSuccessor(handler);
3293 }
3294 }
3295
Vladimir Markoa1de9182016-02-25 11:37:38 +00003296 BoundaryKind GetBoundaryKind() const { return GetPackedField<BoundaryKindField>(); }
3297 bool IsEntry() const { return GetBoundaryKind() == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003298
David Brazdilffee3d32015-07-06 11:48:53 +01003299 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
3300
David Brazdilfc6a86a2015-06-26 10:33:45 +00003301 DECLARE_INSTRUCTION(TryBoundary);
3302
Artem Serovcced8ba2017-07-19 18:18:09 +01003303 protected:
3304 DEFAULT_COPY_CONSTRUCTOR(TryBoundary);
3305
David Brazdilfc6a86a2015-06-26 10:33:45 +00003306 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003307 static constexpr size_t kFieldBoundaryKind = kNumberOfGenericPackedBits;
3308 static constexpr size_t kFieldBoundaryKindSize =
3309 MinimumBitsToStore(static_cast<size_t>(BoundaryKind::kLast));
3310 static constexpr size_t kNumberOfTryBoundaryPackedBits =
3311 kFieldBoundaryKind + kFieldBoundaryKindSize;
3312 static_assert(kNumberOfTryBoundaryPackedBits <= kMaxNumberOfPackedBits,
3313 "Too many packed fields.");
3314 using BoundaryKindField = BitField<BoundaryKind, kFieldBoundaryKind, kFieldBoundaryKindSize>;
David Brazdilfc6a86a2015-06-26 10:33:45 +00003315};
3316
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003317// Deoptimize to interpreter, upon checking a condition.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003318class HDeoptimize final : public HVariableInputSizeInstruction {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003319 public:
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003320 // Use this constructor when the `HDeoptimize` acts as a barrier, where no code can move
3321 // across.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003322 HDeoptimize(ArenaAllocator* allocator,
3323 HInstruction* cond,
3324 DeoptimizationKind kind,
3325 uint32_t dex_pc)
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003326 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303327 kDeoptimize,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003328 SideEffects::All(),
3329 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003330 allocator,
Andreas Gampe3db70682018-12-26 15:12:03 -08003331 /* number_of_inputs= */ 1,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003332 kArenaAllocMisc) {
3333 SetPackedFlag<kFieldCanBeMoved>(false);
3334 SetPackedField<DeoptimizeKindField>(kind);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003335 SetRawInputAt(0, cond);
3336 }
3337
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003338 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01003339
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003340 // Use this constructor when the `HDeoptimize` guards an instruction, and any user
3341 // that relies on the deoptimization to pass should have its input be the `HDeoptimize`
3342 // instead of `guard`.
3343 // We set CanTriggerGC to prevent any intermediate address to be live
3344 // at the point of the `HDeoptimize`.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003345 HDeoptimize(ArenaAllocator* allocator,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003346 HInstruction* cond,
3347 HInstruction* guard,
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003348 DeoptimizationKind kind,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003349 uint32_t dex_pc)
3350 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303351 kDeoptimize,
Vladimir Markobd785672018-05-03 17:09:09 +01003352 guard->GetType(),
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003353 SideEffects::CanTriggerGC(),
3354 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003355 allocator,
Andreas Gampe3db70682018-12-26 15:12:03 -08003356 /* number_of_inputs= */ 2,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003357 kArenaAllocMisc) {
3358 SetPackedFlag<kFieldCanBeMoved>(true);
3359 SetPackedField<DeoptimizeKindField>(kind);
3360 SetRawInputAt(0, cond);
3361 SetRawInputAt(1, guard);
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01003362 }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003363
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003364 bool CanBeMoved() const override { return GetPackedFlag<kFieldCanBeMoved>(); }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003365
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003366 bool InstructionDataEquals(const HInstruction* other) const override {
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003367 return (other->CanBeMoved() == CanBeMoved()) && (other->AsDeoptimize()->GetKind() == GetKind());
3368 }
3369
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003370 bool NeedsEnvironment() const override { return true; }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003371
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003372 bool CanThrow() const override { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003373
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003374 DeoptimizationKind GetDeoptimizationKind() const { return GetPackedField<DeoptimizeKindField>(); }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003375
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003376 bool GuardsAnInput() const {
3377 return InputCount() == 2;
3378 }
3379
3380 HInstruction* GuardedInput() const {
3381 DCHECK(GuardsAnInput());
3382 return InputAt(1);
3383 }
3384
3385 void RemoveGuard() {
3386 RemoveInputAt(1);
3387 }
3388
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003389 DECLARE_INSTRUCTION(Deoptimize);
3390
Artem Serovcced8ba2017-07-19 18:18:09 +01003391 protected:
3392 DEFAULT_COPY_CONSTRUCTOR(Deoptimize);
3393
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003394 private:
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003395 static constexpr size_t kFieldCanBeMoved = kNumberOfGenericPackedBits;
3396 static constexpr size_t kFieldDeoptimizeKind = kNumberOfGenericPackedBits + 1;
3397 static constexpr size_t kFieldDeoptimizeKindSize =
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003398 MinimumBitsToStore(static_cast<size_t>(DeoptimizationKind::kLast));
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003399 static constexpr size_t kNumberOfDeoptimizePackedBits =
3400 kFieldDeoptimizeKind + kFieldDeoptimizeKindSize;
3401 static_assert(kNumberOfDeoptimizePackedBits <= kMaxNumberOfPackedBits,
3402 "Too many packed fields.");
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003403 using DeoptimizeKindField =
3404 BitField<DeoptimizationKind, kFieldDeoptimizeKind, kFieldDeoptimizeKindSize>;
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003405};
3406
Mingyao Yang063fc772016-08-02 11:02:54 -07003407// Represents a should_deoptimize flag. Currently used for CHA-based devirtualization.
3408// The compiled code checks this flag value in a guard before devirtualized call and
3409// if it's true, starts to do deoptimization.
3410// It has a 4-byte slot on stack.
3411// TODO: allocate a register for this flag.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003412class HShouldDeoptimizeFlag final : public HVariableInputSizeInstruction {
Mingyao Yang063fc772016-08-02 11:02:54 -07003413 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003414 // CHA guards are only optimized in a separate pass and it has no side effects
3415 // with regard to other passes.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003416 HShouldDeoptimizeFlag(ArenaAllocator* allocator, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303417 : HVariableInputSizeInstruction(kShouldDeoptimizeFlag,
Vladimir Markobd785672018-05-03 17:09:09 +01003418 DataType::Type::kInt32,
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303419 SideEffects::None(),
3420 dex_pc,
3421 allocator,
3422 0,
3423 kArenaAllocCHA) {
Mingyao Yang063fc772016-08-02 11:02:54 -07003424 }
3425
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003426 // We do all CHA guard elimination/motion in a single pass, after which there is no
3427 // further guard elimination/motion since a guard might have been used for justification
3428 // of the elimination of another guard. Therefore, we pretend this guard cannot be moved
3429 // to avoid other optimizations trying to move it.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003430 bool CanBeMoved() const override { return false; }
Mingyao Yang063fc772016-08-02 11:02:54 -07003431
3432 DECLARE_INSTRUCTION(ShouldDeoptimizeFlag);
3433
Artem Serovcced8ba2017-07-19 18:18:09 +01003434 protected:
3435 DEFAULT_COPY_CONSTRUCTOR(ShouldDeoptimizeFlag);
Mingyao Yang063fc772016-08-02 11:02:54 -07003436};
3437
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003438// Represents the ArtMethod that was passed as a first argument to
3439// the method. It is used by instructions that depend on it, like
3440// instructions that work with the dex cache.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003441class HCurrentMethod final : public HExpression<0> {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003442 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003443 explicit HCurrentMethod(DataType::Type type, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303444 : HExpression(kCurrentMethod, type, SideEffects::None(), dex_pc) {
3445 }
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003446
3447 DECLARE_INSTRUCTION(CurrentMethod);
3448
Artem Serovcced8ba2017-07-19 18:18:09 +01003449 protected:
3450 DEFAULT_COPY_CONSTRUCTOR(CurrentMethod);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003451};
3452
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003453// Fetches an ArtMethod from the virtual table or the interface method table
3454// of a class.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003455class HClassTableGet final : public HExpression<1> {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003456 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003457 enum class TableKind {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003458 kVTable,
3459 kIMTable,
Vladimir Markoa1de9182016-02-25 11:37:38 +00003460 kLast = kIMTable
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003461 };
3462 HClassTableGet(HInstruction* cls,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003463 DataType::Type type,
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003464 TableKind kind,
3465 size_t index,
3466 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303467 : HExpression(kClassTableGet, type, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003468 index_(index) {
3469 SetPackedField<TableKindField>(kind);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003470 SetRawInputAt(0, cls);
3471 }
3472
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003473 bool IsClonable() const override { return true; }
3474 bool CanBeMoved() const override { return true; }
3475 bool InstructionDataEquals(const HInstruction* other) const override {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003476 return other->AsClassTableGet()->GetIndex() == index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00003477 other->AsClassTableGet()->GetPackedFields() == GetPackedFields();
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003478 }
3479
Vladimir Markoa1de9182016-02-25 11:37:38 +00003480 TableKind GetTableKind() const { return GetPackedField<TableKindField>(); }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003481 size_t GetIndex() const { return index_; }
3482
3483 DECLARE_INSTRUCTION(ClassTableGet);
3484
Artem Serovcced8ba2017-07-19 18:18:09 +01003485 protected:
3486 DEFAULT_COPY_CONSTRUCTOR(ClassTableGet);
3487
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003488 private:
Vladimir Markobd785672018-05-03 17:09:09 +01003489 static constexpr size_t kFieldTableKind = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003490 static constexpr size_t kFieldTableKindSize =
3491 MinimumBitsToStore(static_cast<size_t>(TableKind::kLast));
3492 static constexpr size_t kNumberOfClassTableGetPackedBits = kFieldTableKind + kFieldTableKindSize;
3493 static_assert(kNumberOfClassTableGetPackedBits <= kMaxNumberOfPackedBits,
3494 "Too many packed fields.");
3495 using TableKindField = BitField<TableKind, kFieldTableKind, kFieldTableKind>;
3496
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003497 // The index of the ArtMethod in the table.
3498 const size_t index_;
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003499};
3500
Mark Mendellfe57faa2015-09-18 09:26:15 -04003501// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
3502// have one successor for each entry in the switch table, and the final successor
3503// will be the block containing the next Dex opcode.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003504class HPackedSwitch final : public HExpression<1> {
Mark Mendellfe57faa2015-09-18 09:26:15 -04003505 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003506 HPackedSwitch(int32_t start_value,
3507 uint32_t num_entries,
3508 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04003509 uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01003510 : HExpression(kPackedSwitch, SideEffects::None(), dex_pc),
Mark Mendellfe57faa2015-09-18 09:26:15 -04003511 start_value_(start_value),
3512 num_entries_(num_entries) {
3513 SetRawInputAt(0, input);
3514 }
3515
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003516 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01003517
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003518 bool IsControlFlow() const override { return true; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04003519
3520 int32_t GetStartValue() const { return start_value_; }
3521
Vladimir Marko211c2112015-09-24 16:52:33 +01003522 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04003523
3524 HBasicBlock* GetDefaultBlock() const {
3525 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01003526 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04003527 }
3528 DECLARE_INSTRUCTION(PackedSwitch);
3529
Artem Serovcced8ba2017-07-19 18:18:09 +01003530 protected:
3531 DEFAULT_COPY_CONSTRUCTOR(PackedSwitch);
3532
Mark Mendellfe57faa2015-09-18 09:26:15 -04003533 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003534 const int32_t start_value_;
3535 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04003536};
3537
Roland Levillain88cb1752014-10-20 16:36:47 +01003538class HUnaryOperation : public HExpression<1> {
3539 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303540 HUnaryOperation(InstructionKind kind,
3541 DataType::Type result_type,
3542 HInstruction* input,
3543 uint32_t dex_pc = kNoDexPc)
3544 : HExpression(kind, result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01003545 SetRawInputAt(0, input);
3546 }
3547
Artem Serovcced8ba2017-07-19 18:18:09 +01003548 // All of the UnaryOperation instructions are clonable.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003549 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01003550
Roland Levillain88cb1752014-10-20 16:36:47 +01003551 HInstruction* GetInput() const { return InputAt(0); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003552 DataType::Type GetResultType() const { return GetType(); }
Roland Levillain88cb1752014-10-20 16:36:47 +01003553
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003554 bool CanBeMoved() const override { return true; }
3555 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003556 return true;
3557 }
Roland Levillain88cb1752014-10-20 16:36:47 +01003558
Roland Levillain31dd3d62016-02-16 12:21:02 +00003559 // Try to statically evaluate `this` and return a HConstant
3560 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003561 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003562 HConstant* TryStaticEvaluation() const;
3563
3564 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003565 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
3566 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003567 virtual HConstant* Evaluate(HFloatConstant* x) const = 0;
3568 virtual HConstant* Evaluate(HDoubleConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01003569
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003570 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01003571
Artem Serovcced8ba2017-07-19 18:18:09 +01003572 protected:
3573 DEFAULT_COPY_CONSTRUCTOR(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01003574};
3575
Dave Allison20dfc792014-06-16 20:44:29 -07003576class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003577 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303578 HBinaryOperation(InstructionKind kind,
3579 DataType::Type result_type,
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003580 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003581 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003582 SideEffects side_effects = SideEffects::None(),
3583 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303584 : HExpression(kind, result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003585 SetRawInputAt(0, left);
3586 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003587 }
3588
Artem Serovcced8ba2017-07-19 18:18:09 +01003589 // All of the BinaryOperation instructions are clonable.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003590 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01003591
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003592 HInstruction* GetLeft() const { return InputAt(0); }
3593 HInstruction* GetRight() const { return InputAt(1); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003594 DataType::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003595
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003596 virtual bool IsCommutative() const { return false; }
3597
3598 // Put constant on the right.
3599 // Returns whether order is changed.
3600 bool OrderInputsWithConstantOnTheRight() {
3601 HInstruction* left = InputAt(0);
3602 HInstruction* right = InputAt(1);
3603 if (left->IsConstant() && !right->IsConstant()) {
3604 ReplaceInput(right, 0);
3605 ReplaceInput(left, 1);
3606 return true;
3607 }
3608 return false;
3609 }
3610
3611 // Order inputs by instruction id, but favor constant on the right side.
3612 // This helps GVN for commutative ops.
3613 void OrderInputs() {
3614 DCHECK(IsCommutative());
3615 HInstruction* left = InputAt(0);
3616 HInstruction* right = InputAt(1);
3617 if (left == right || (!left->IsConstant() && right->IsConstant())) {
3618 return;
3619 }
3620 if (OrderInputsWithConstantOnTheRight()) {
3621 return;
3622 }
3623 // Order according to instruction id.
3624 if (left->GetId() > right->GetId()) {
3625 ReplaceInput(right, 0);
3626 ReplaceInput(left, 1);
3627 }
3628 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003629
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003630 bool CanBeMoved() const override { return true; }
3631 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003632 return true;
3633 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003634
Roland Levillain31dd3d62016-02-16 12:21:02 +00003635 // Try to statically evaluate `this` and return a HConstant
3636 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003637 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003638 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01003639
3640 // Apply this operation to `x` and `y`.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003641 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3642 HNullConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003643 LOG(FATAL) << DebugName() << " is not defined for the (null, null) case.";
3644 UNREACHABLE();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003645 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003646 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
3647 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
Roland Levillain9867bc72015-08-05 10:21:34 +01003648 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
3649 HIntConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003650 LOG(FATAL) << DebugName() << " is not defined for the (long, int) case.";
3651 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01003652 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003653 virtual HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const = 0;
3654 virtual HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const = 0;
Roland Levillain556c3d12014-09-18 15:25:07 +01003655
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003656 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003657 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003658 HConstant* GetConstantRight() const;
3659
3660 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003661 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003662 HInstruction* GetLeastConstantLeft() const;
3663
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003664 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01003665
Artem Serovcced8ba2017-07-19 18:18:09 +01003666 protected:
3667 DEFAULT_COPY_CONSTRUCTOR(BinaryOperation);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003668};
3669
Mark Mendellc4701932015-04-10 13:18:51 -04003670// The comparison bias applies for floating point operations and indicates how NaN
3671// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01003672enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04003673 kNoBias, // bias is not applicable (i.e. for long operation)
3674 kGtBias, // return 1 for NaN comparisons
3675 kLtBias, // return -1 for NaN comparisons
Vladimir Markoa1de9182016-02-25 11:37:38 +00003676 kLast = kLtBias
Mark Mendellc4701932015-04-10 13:18:51 -04003677};
3678
Roland Levillain31dd3d62016-02-16 12:21:02 +00003679std::ostream& operator<<(std::ostream& os, const ComparisonBias& rhs);
3680
Dave Allison20dfc792014-06-16 20:44:29 -07003681class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003682 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303683 HCondition(InstructionKind kind,
3684 HInstruction* first,
3685 HInstruction* second,
3686 uint32_t dex_pc = kNoDexPc)
3687 : HBinaryOperation(kind,
3688 DataType::Type::kBool,
3689 first,
3690 second,
3691 SideEffects::None(),
3692 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003693 SetPackedField<ComparisonBiasField>(ComparisonBias::kNoBias);
3694 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003695
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003696 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003697 // `instruction`, and disregard moves in between.
3698 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003699
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003700 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003701
3702 virtual IfCondition GetCondition() const = 0;
3703
Mark Mendellc4701932015-04-10 13:18:51 -04003704 virtual IfCondition GetOppositeCondition() const = 0;
3705
Vladimir Markoa1de9182016-02-25 11:37:38 +00003706 bool IsGtBias() const { return GetBias() == ComparisonBias::kGtBias; }
Anton Shaminbdd79352016-02-15 12:48:36 +06003707 bool IsLtBias() const { return GetBias() == ComparisonBias::kLtBias; }
3708
Vladimir Markoa1de9182016-02-25 11:37:38 +00003709 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
3710 void SetBias(ComparisonBias bias) { SetPackedField<ComparisonBiasField>(bias); }
Mark Mendellc4701932015-04-10 13:18:51 -04003711
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003712 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003713 return GetPackedFields() == other->AsCondition()->GetPackedFields();
Mark Mendellc4701932015-04-10 13:18:51 -04003714 }
3715
Roland Levillain4fa13f62015-07-06 18:11:54 +01003716 bool IsFPConditionTrueIfNaN() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003717 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003718 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003719 if (if_cond == kCondNE) {
3720 return true;
3721 } else if (if_cond == kCondEQ) {
3722 return false;
3723 }
3724 return ((if_cond == kCondGT) || (if_cond == kCondGE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003725 }
3726
3727 bool IsFPConditionFalseIfNaN() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003728 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003729 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003730 if (if_cond == kCondEQ) {
3731 return true;
3732 } else if (if_cond == kCondNE) {
3733 return false;
3734 }
3735 return ((if_cond == kCondLT) || (if_cond == kCondLE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003736 }
3737
Roland Levillain31dd3d62016-02-16 12:21:02 +00003738 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003739 // Needed if we merge a HCompare into a HCondition.
Vladimir Markobd785672018-05-03 17:09:09 +01003740 static constexpr size_t kFieldComparisonBias = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003741 static constexpr size_t kFieldComparisonBiasSize =
3742 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3743 static constexpr size_t kNumberOfConditionPackedBits =
3744 kFieldComparisonBias + kFieldComparisonBiasSize;
3745 static_assert(kNumberOfConditionPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3746 using ComparisonBiasField =
3747 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3748
Roland Levillain31dd3d62016-02-16 12:21:02 +00003749 template <typename T>
3750 int32_t Compare(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3751
3752 template <typename T>
3753 int32_t CompareFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003754 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003755 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3756 // Handle the bias.
3757 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compare(x, y);
3758 }
3759
3760 // Return an integer constant containing the result of a condition evaluated at compile time.
3761 HIntConstant* MakeConstantCondition(bool value, uint32_t dex_pc) const {
3762 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3763 }
3764
Artem Serovcced8ba2017-07-19 18:18:09 +01003765 DEFAULT_COPY_CONSTRUCTOR(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003766};
3767
3768// Instruction to check if two inputs are equal to each other.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003769class HEqual final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003770 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003771 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303772 : HCondition(kEqual, first, second, dex_pc) {
3773 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003774
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003775 bool IsCommutative() const override { return true; }
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003776
Vladimir Marko9e23df52015-11-10 17:14:35 +00003777 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003778 HNullConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003779 return MakeConstantCondition(true, GetDexPc());
3780 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003781 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003782 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3783 }
3784 // In the following Evaluate methods, a HCompare instruction has
3785 // been merged into this HEqual instruction; evaluate it as
3786 // `Compare(x, y) == 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003787 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003788 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0),
3789 GetDexPc());
3790 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003791 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003792 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3793 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003794 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003795 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003796 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003797
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003798 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003799
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003800 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07003801 return kCondEQ;
3802 }
3803
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003804 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04003805 return kCondNE;
3806 }
3807
Artem Serovcced8ba2017-07-19 18:18:09 +01003808 protected:
3809 DEFAULT_COPY_CONSTRUCTOR(Equal);
3810
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003811 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003812 template <typename T> static bool Compute(T x, T y) { return x == y; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003813};
3814
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003815class HNotEqual final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003816 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303817 HNotEqual(HInstruction* first, HInstruction* second,
3818 uint32_t dex_pc = kNoDexPc)
3819 : HCondition(kNotEqual, first, second, dex_pc) {
3820 }
Dave Allison20dfc792014-06-16 20:44:29 -07003821
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003822 bool IsCommutative() const override { return true; }
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003823
Vladimir Marko9e23df52015-11-10 17:14:35 +00003824 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003825 HNullConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003826 return MakeConstantCondition(false, GetDexPc());
3827 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003828 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003829 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3830 }
3831 // In the following Evaluate methods, a HCompare instruction has
3832 // been merged into this HNotEqual instruction; evaluate it as
3833 // `Compare(x, y) != 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003834 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003835 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3836 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003837 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003838 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3839 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003840 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003841 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003842 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003843
Dave Allison20dfc792014-06-16 20:44:29 -07003844 DECLARE_INSTRUCTION(NotEqual);
3845
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003846 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07003847 return kCondNE;
3848 }
3849
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003850 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04003851 return kCondEQ;
3852 }
3853
Artem Serovcced8ba2017-07-19 18:18:09 +01003854 protected:
3855 DEFAULT_COPY_CONSTRUCTOR(NotEqual);
3856
Dave Allison20dfc792014-06-16 20:44:29 -07003857 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003858 template <typename T> static bool Compute(T x, T y) { return x != y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003859};
3860
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003861class HLessThan final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003862 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303863 HLessThan(HInstruction* first, HInstruction* second,
3864 uint32_t dex_pc = kNoDexPc)
3865 : HCondition(kLessThan, first, second, dex_pc) {
3866 }
Dave Allison20dfc792014-06-16 20:44:29 -07003867
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003868 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003869 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003870 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003871 // In the following Evaluate methods, a HCompare instruction has
3872 // been merged into this HLessThan instruction; evaluate it as
3873 // `Compare(x, y) < 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003874 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003875 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3876 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003877 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003878 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3879 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003880 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003881 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003882 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003883
Dave Allison20dfc792014-06-16 20:44:29 -07003884 DECLARE_INSTRUCTION(LessThan);
3885
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003886 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07003887 return kCondLT;
3888 }
3889
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003890 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04003891 return kCondGE;
3892 }
3893
Artem Serovcced8ba2017-07-19 18:18:09 +01003894 protected:
3895 DEFAULT_COPY_CONSTRUCTOR(LessThan);
3896
Dave Allison20dfc792014-06-16 20:44:29 -07003897 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003898 template <typename T> static bool Compute(T x, T y) { return x < y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003899};
3900
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003901class HLessThanOrEqual final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003902 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303903 HLessThanOrEqual(HInstruction* first, HInstruction* second,
3904 uint32_t dex_pc = kNoDexPc)
3905 : HCondition(kLessThanOrEqual, first, second, dex_pc) {
3906 }
Dave Allison20dfc792014-06-16 20:44:29 -07003907
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003908 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003909 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003910 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003911 // In the following Evaluate methods, a HCompare instruction has
3912 // been merged into this HLessThanOrEqual instruction; evaluate it as
3913 // `Compare(x, y) <= 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003914 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003915 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3916 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003917 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003918 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3919 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003920 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003921 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003922 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003923
Dave Allison20dfc792014-06-16 20:44:29 -07003924 DECLARE_INSTRUCTION(LessThanOrEqual);
3925
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003926 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07003927 return kCondLE;
3928 }
3929
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003930 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04003931 return kCondGT;
3932 }
3933
Artem Serovcced8ba2017-07-19 18:18:09 +01003934 protected:
3935 DEFAULT_COPY_CONSTRUCTOR(LessThanOrEqual);
3936
Dave Allison20dfc792014-06-16 20:44:29 -07003937 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003938 template <typename T> static bool Compute(T x, T y) { return x <= y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003939};
3940
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003941class HGreaterThan final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003942 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003943 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303944 : HCondition(kGreaterThan, first, second, dex_pc) {
3945 }
Dave Allison20dfc792014-06-16 20:44:29 -07003946
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003947 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003948 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003949 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003950 // In the following Evaluate methods, a HCompare instruction has
3951 // been merged into this HGreaterThan instruction; evaluate it as
3952 // `Compare(x, y) > 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003953 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003954 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3955 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003956 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003957 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3958 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003959 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003960 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003961 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003962
Dave Allison20dfc792014-06-16 20:44:29 -07003963 DECLARE_INSTRUCTION(GreaterThan);
3964
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003965 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07003966 return kCondGT;
3967 }
3968
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003969 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04003970 return kCondLE;
3971 }
3972
Artem Serovcced8ba2017-07-19 18:18:09 +01003973 protected:
3974 DEFAULT_COPY_CONSTRUCTOR(GreaterThan);
3975
Dave Allison20dfc792014-06-16 20:44:29 -07003976 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003977 template <typename T> static bool Compute(T x, T y) { return x > y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003978};
3979
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003980class HGreaterThanOrEqual final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003981 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003982 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303983 : HCondition(kGreaterThanOrEqual, first, second, dex_pc) {
3984 }
Dave Allison20dfc792014-06-16 20:44:29 -07003985
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003986 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003987 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003988 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003989 // In the following Evaluate methods, a HCompare instruction has
3990 // been merged into this HGreaterThanOrEqual instruction; evaluate it as
3991 // `Compare(x, y) >= 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003992 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003993 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3994 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003995 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003996 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3997 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003998 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003999 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004000 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004001
Dave Allison20dfc792014-06-16 20:44:29 -07004002 DECLARE_INSTRUCTION(GreaterThanOrEqual);
4003
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004004 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07004005 return kCondGE;
4006 }
4007
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004008 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04004009 return kCondLT;
4010 }
4011
Artem Serovcced8ba2017-07-19 18:18:09 +01004012 protected:
4013 DEFAULT_COPY_CONSTRUCTOR(GreaterThanOrEqual);
4014
Dave Allison20dfc792014-06-16 20:44:29 -07004015 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004016 template <typename T> static bool Compute(T x, T y) { return x >= y; }
Dave Allison20dfc792014-06-16 20:44:29 -07004017};
4018
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004019class HBelow final : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07004020 public:
4021 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304022 : HCondition(kBelow, first, second, dex_pc) {
4023 }
Aart Bike9f37602015-10-09 11:15:55 -07004024
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004025 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004026 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07004027 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004028 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004029 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4030 }
4031 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004032 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004033 LOG(FATAL) << DebugName() << " is not defined for float values";
4034 UNREACHABLE();
4035 }
4036 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004037 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004038 LOG(FATAL) << DebugName() << " is not defined for double values";
4039 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07004040 }
4041
4042 DECLARE_INSTRUCTION(Below);
4043
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004044 IfCondition GetCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004045 return kCondB;
4046 }
4047
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004048 IfCondition GetOppositeCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004049 return kCondAE;
4050 }
4051
Artem Serovcced8ba2017-07-19 18:18:09 +01004052 protected:
4053 DEFAULT_COPY_CONSTRUCTOR(Below);
4054
Aart Bike9f37602015-10-09 11:15:55 -07004055 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004056 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004057 return MakeUnsigned(x) < MakeUnsigned(y);
4058 }
Aart Bike9f37602015-10-09 11:15:55 -07004059};
4060
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004061class HBelowOrEqual final : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07004062 public:
4063 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304064 : HCondition(kBelowOrEqual, first, second, dex_pc) {
4065 }
Aart Bike9f37602015-10-09 11:15:55 -07004066
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004067 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004068 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07004069 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004070 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004071 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4072 }
4073 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004074 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004075 LOG(FATAL) << DebugName() << " is not defined for float values";
4076 UNREACHABLE();
4077 }
4078 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004079 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004080 LOG(FATAL) << DebugName() << " is not defined for double values";
4081 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07004082 }
4083
4084 DECLARE_INSTRUCTION(BelowOrEqual);
4085
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004086 IfCondition GetCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004087 return kCondBE;
4088 }
4089
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004090 IfCondition GetOppositeCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004091 return kCondA;
4092 }
4093
Artem Serovcced8ba2017-07-19 18:18:09 +01004094 protected:
4095 DEFAULT_COPY_CONSTRUCTOR(BelowOrEqual);
4096
Aart Bike9f37602015-10-09 11:15:55 -07004097 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004098 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004099 return MakeUnsigned(x) <= MakeUnsigned(y);
4100 }
Aart Bike9f37602015-10-09 11:15:55 -07004101};
4102
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004103class HAbove final : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07004104 public:
4105 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304106 : HCondition(kAbove, first, second, dex_pc) {
4107 }
Aart Bike9f37602015-10-09 11:15:55 -07004108
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004109 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004110 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07004111 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004112 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004113 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4114 }
4115 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004116 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004117 LOG(FATAL) << DebugName() << " is not defined for float values";
4118 UNREACHABLE();
4119 }
4120 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004121 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004122 LOG(FATAL) << DebugName() << " is not defined for double values";
4123 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07004124 }
4125
4126 DECLARE_INSTRUCTION(Above);
4127
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004128 IfCondition GetCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004129 return kCondA;
4130 }
4131
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004132 IfCondition GetOppositeCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004133 return kCondBE;
4134 }
4135
Artem Serovcced8ba2017-07-19 18:18:09 +01004136 protected:
4137 DEFAULT_COPY_CONSTRUCTOR(Above);
4138
Aart Bike9f37602015-10-09 11:15:55 -07004139 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004140 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004141 return MakeUnsigned(x) > MakeUnsigned(y);
4142 }
Aart Bike9f37602015-10-09 11:15:55 -07004143};
4144
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004145class HAboveOrEqual final : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07004146 public:
4147 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304148 : HCondition(kAboveOrEqual, first, second, dex_pc) {
4149 }
Aart Bike9f37602015-10-09 11:15:55 -07004150
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004151 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004152 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07004153 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004154 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004155 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4156 }
4157 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004158 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004159 LOG(FATAL) << DebugName() << " is not defined for float values";
4160 UNREACHABLE();
4161 }
4162 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004163 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004164 LOG(FATAL) << DebugName() << " is not defined for double values";
4165 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07004166 }
4167
4168 DECLARE_INSTRUCTION(AboveOrEqual);
4169
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004170 IfCondition GetCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004171 return kCondAE;
4172 }
4173
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004174 IfCondition GetOppositeCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004175 return kCondB;
4176 }
4177
Artem Serovcced8ba2017-07-19 18:18:09 +01004178 protected:
4179 DEFAULT_COPY_CONSTRUCTOR(AboveOrEqual);
4180
Aart Bike9f37602015-10-09 11:15:55 -07004181 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004182 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004183 return MakeUnsigned(x) >= MakeUnsigned(y);
4184 }
Aart Bike9f37602015-10-09 11:15:55 -07004185};
Dave Allison20dfc792014-06-16 20:44:29 -07004186
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004187// Instruction to check how two inputs compare to each other.
4188// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004189class HCompare final : public HBinaryOperation {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004190 public:
Roland Levillaina5c4a402016-03-15 15:02:50 +00004191 // Note that `comparison_type` is the type of comparison performed
4192 // between the comparison's inputs, not the type of the instantiated
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004193 // HCompare instruction (which is always DataType::Type::kInt).
4194 HCompare(DataType::Type comparison_type,
Alexey Frunze4dda3372015-06-01 18:31:49 -07004195 HInstruction* first,
4196 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04004197 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07004198 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304199 : HBinaryOperation(kCompare,
4200 DataType::Type::kInt32,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004201 first,
4202 second,
Roland Levillaina5c4a402016-03-15 15:02:50 +00004203 SideEffectsForArchRuntimeCalls(comparison_type),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004204 dex_pc) {
4205 SetPackedField<ComparisonBiasField>(bias);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004206 }
4207
Roland Levillain9867bc72015-08-05 10:21:34 +01004208 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004209 int32_t Compute(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
4210
4211 template <typename T>
4212 int32_t ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004213 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00004214 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
4215 // Handle the bias.
4216 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compute(x, y);
4217 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004218
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004219 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004220 // Note that there is no "cmp-int" Dex instruction so we shouldn't
4221 // reach this code path when processing a freshly built HIR
4222 // graph. However HCompare integer instructions can be synthesized
4223 // by the instruction simplifier to implement IntegerCompare and
4224 // IntegerSignum intrinsics, so we have to handle this case.
4225 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004226 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004227 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004228 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4229 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004230 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004231 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4232 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004233 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004234 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01004235 }
4236
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004237 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004238 return GetPackedFields() == other->AsCompare()->GetPackedFields();
Calin Juravleddb7df22014-11-25 20:56:51 +00004239 }
4240
Vladimir Markoa1de9182016-02-25 11:37:38 +00004241 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
Mark Mendellc4701932015-04-10 13:18:51 -04004242
Roland Levillain31dd3d62016-02-16 12:21:02 +00004243 // Does this compare instruction have a "gt bias" (vs an "lt bias")?
Vladimir Markoa1de9182016-02-25 11:37:38 +00004244 // Only meaningful for floating-point comparisons.
Roland Levillain31dd3d62016-02-16 12:21:02 +00004245 bool IsGtBias() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004246 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004247 return GetBias() == ComparisonBias::kGtBias;
Roland Levillain31dd3d62016-02-16 12:21:02 +00004248 }
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004249
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004250 static SideEffects SideEffectsForArchRuntimeCalls(DataType::Type type ATTRIBUTE_UNUSED) {
Roland Levillain1693a1f2016-03-15 14:57:31 +00004251 // Comparisons do not require a runtime call in any back end.
4252 return SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004253 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004254
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004255 DECLARE_INSTRUCTION(Compare);
4256
Roland Levillain31dd3d62016-02-16 12:21:02 +00004257 protected:
Vladimir Markobd785672018-05-03 17:09:09 +01004258 static constexpr size_t kFieldComparisonBias = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004259 static constexpr size_t kFieldComparisonBiasSize =
4260 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
4261 static constexpr size_t kNumberOfComparePackedBits =
4262 kFieldComparisonBias + kFieldComparisonBiasSize;
4263 static_assert(kNumberOfComparePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
4264 using ComparisonBiasField =
4265 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
4266
Roland Levillain31dd3d62016-02-16 12:21:02 +00004267 // Return an integer constant containing the result of a comparison evaluated at compile time.
4268 HIntConstant* MakeConstantComparison(int32_t value, uint32_t dex_pc) const {
4269 DCHECK(value == -1 || value == 0 || value == 1) << value;
4270 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
4271 }
4272
Artem Serovcced8ba2017-07-19 18:18:09 +01004273 DEFAULT_COPY_CONSTRUCTOR(Compare);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004274};
4275
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004276class HNewInstance final : public HExpression<1> {
David Brazdil6de19382016-01-08 17:37:10 +00004277 public:
4278 HNewInstance(HInstruction* cls,
David Brazdil6de19382016-01-08 17:37:10 +00004279 uint32_t dex_pc,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004280 dex::TypeIndex type_index,
David Brazdil6de19382016-01-08 17:37:10 +00004281 const DexFile& dex_file,
David Brazdil6de19382016-01-08 17:37:10 +00004282 bool finalizable,
4283 QuickEntrypointEnum entrypoint)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304284 : HExpression(kNewInstance,
4285 DataType::Type::kReference,
4286 SideEffects::CanTriggerGC(),
4287 dex_pc),
David Brazdil6de19382016-01-08 17:37:10 +00004288 type_index_(type_index),
4289 dex_file_(dex_file),
David Brazdil6de19382016-01-08 17:37:10 +00004290 entrypoint_(entrypoint) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004291 SetPackedFlag<kFlagFinalizable>(finalizable);
David Brazdil6de19382016-01-08 17:37:10 +00004292 SetRawInputAt(0, cls);
David Brazdil6de19382016-01-08 17:37:10 +00004293 }
4294
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004295 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004296
Andreas Gampea5b09a62016-11-17 15:21:22 -08004297 dex::TypeIndex GetTypeIndex() const { return type_index_; }
David Brazdil6de19382016-01-08 17:37:10 +00004298 const DexFile& GetDexFile() const { return dex_file_; }
4299
4300 // Calls runtime so needs an environment.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004301 bool NeedsEnvironment() const override { return true; }
David Brazdil6de19382016-01-08 17:37:10 +00004302
Mingyao Yang062157f2016-03-02 10:15:36 -08004303 // Can throw errors when out-of-memory or if it's not instantiable/accessible.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004304 bool CanThrow() const override { return true; }
Mingyao Yang062157f2016-03-02 10:15:36 -08004305
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004306 bool NeedsChecks() const {
4307 return entrypoint_ == kQuickAllocObjectWithChecks;
4308 }
David Brazdil6de19382016-01-08 17:37:10 +00004309
Vladimir Markoa1de9182016-02-25 11:37:38 +00004310 bool IsFinalizable() const { return GetPackedFlag<kFlagFinalizable>(); }
David Brazdil6de19382016-01-08 17:37:10 +00004311
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004312 bool CanBeNull() const override { return false; }
David Brazdil6de19382016-01-08 17:37:10 +00004313
4314 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
4315
4316 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
4317 entrypoint_ = entrypoint;
4318 }
4319
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004320 HLoadClass* GetLoadClass() const {
4321 HInstruction* input = InputAt(0);
4322 if (input->IsClinitCheck()) {
4323 input = input->InputAt(0);
4324 }
4325 DCHECK(input->IsLoadClass());
4326 return input->AsLoadClass();
4327 }
4328
David Brazdil6de19382016-01-08 17:37:10 +00004329 bool IsStringAlloc() const;
4330
4331 DECLARE_INSTRUCTION(NewInstance);
4332
Artem Serovcced8ba2017-07-19 18:18:09 +01004333 protected:
4334 DEFAULT_COPY_CONSTRUCTOR(NewInstance);
4335
David Brazdil6de19382016-01-08 17:37:10 +00004336 private:
Vladimir Markobd785672018-05-03 17:09:09 +01004337 static constexpr size_t kFlagFinalizable = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004338 static constexpr size_t kNumberOfNewInstancePackedBits = kFlagFinalizable + 1;
4339 static_assert(kNumberOfNewInstancePackedBits <= kMaxNumberOfPackedBits,
4340 "Too many packed fields.");
4341
Andreas Gampea5b09a62016-11-17 15:21:22 -08004342 const dex::TypeIndex type_index_;
David Brazdil6de19382016-01-08 17:37:10 +00004343 const DexFile& dex_file_;
David Brazdil6de19382016-01-08 17:37:10 +00004344 QuickEntrypointEnum entrypoint_;
David Brazdil6de19382016-01-08 17:37:10 +00004345};
4346
Agi Csaki05f20562015-08-19 14:58:14 -07004347enum IntrinsicNeedsEnvironmentOrCache {
4348 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
4349 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07004350};
4351
Aart Bik5d75afe2015-12-14 11:57:01 -08004352enum IntrinsicSideEffects {
4353 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
4354 kReadSideEffects, // Intrinsic may read heap memory.
4355 kWriteSideEffects, // Intrinsic may write heap memory.
4356 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
4357};
4358
4359enum IntrinsicExceptions {
4360 kNoThrow, // Intrinsic does not throw any exceptions.
4361 kCanThrow // Intrinsic may throw exceptions.
4362};
4363
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004364class HInvoke : public HVariableInputSizeInstruction {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004365 public:
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004366 bool NeedsEnvironment() const override;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004367
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01004368 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004369 SetRawInputAt(index, argument);
4370 }
4371
Roland Levillain3e3d7332015-04-28 11:00:54 +01004372 // Return the number of arguments. This number can be lower than
4373 // the number of inputs returned by InputCount(), as some invoke
4374 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
4375 // inputs at the end of their list of inputs.
4376 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
4377
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004378 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
4379
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004380 InvokeType GetInvokeType() const {
4381 return GetPackedField<InvokeTypeField>();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004382 }
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004383
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01004384 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004385 return intrinsic_;
4386 }
4387
Aart Bik5d75afe2015-12-14 11:57:01 -08004388 void SetIntrinsic(Intrinsics intrinsic,
4389 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
4390 IntrinsicSideEffects side_effects,
4391 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004392
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01004393 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00004394 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01004395 }
4396
Aart Bikff7d89c2016-11-07 08:49:28 -08004397 void SetCanThrow(bool can_throw) { SetPackedFlag<kFlagCanThrow>(can_throw); }
4398
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004399 bool CanThrow() const override { return GetPackedFlag<kFlagCanThrow>(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08004400
Aart Bika8b8e9b2018-01-09 11:01:02 -08004401 void SetAlwaysThrows(bool always_throws) { SetPackedFlag<kFlagAlwaysThrows>(always_throws); }
4402
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004403 bool AlwaysThrows() const override { return GetPackedFlag<kFlagAlwaysThrows>(); }
Aart Bika8b8e9b2018-01-09 11:01:02 -08004404
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004405 bool CanBeMoved() const override { return IsIntrinsic() && !DoesAnyWrite(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08004406
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004407 bool InstructionDataEquals(const HInstruction* other) const override {
Aart Bik5d75afe2015-12-14 11:57:01 -08004408 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
4409 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01004410
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004411 uint32_t* GetIntrinsicOptimizations() {
4412 return &intrinsic_optimizations_;
4413 }
4414
4415 const uint32_t* GetIntrinsicOptimizations() const {
4416 return &intrinsic_optimizations_;
4417 }
4418
4419 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
4420
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004421 ArtMethod* GetResolvedMethod() const { return resolved_method_; }
Nicolas Geoffray76d4bb0f32018-09-21 12:58:45 +01004422 void SetResolvedMethod(ArtMethod* method) REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004423
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004424 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004425
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004426 protected:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004427 static constexpr size_t kFieldInvokeType = kNumberOfGenericPackedBits;
4428 static constexpr size_t kFieldInvokeTypeSize =
Vladimir Markoa1de9182016-02-25 11:37:38 +00004429 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
Vladimir Markobd785672018-05-03 17:09:09 +01004430 static constexpr size_t kFlagCanThrow = kFieldInvokeType + kFieldInvokeTypeSize;
Aart Bika8b8e9b2018-01-09 11:01:02 -08004431 static constexpr size_t kFlagAlwaysThrows = kFlagCanThrow + 1;
4432 static constexpr size_t kNumberOfInvokePackedBits = kFlagAlwaysThrows + 1;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004433 static_assert(kNumberOfInvokePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004434 using InvokeTypeField = BitField<InvokeType, kFieldInvokeType, kFieldInvokeTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004435
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304436 HInvoke(InstructionKind kind,
4437 ArenaAllocator* allocator,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004438 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01004439 uint32_t number_of_other_inputs,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004440 DataType::Type return_type,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004441 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004442 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004443 ArtMethod* resolved_method,
4444 InvokeType invoke_type)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004445 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304446 kind,
Vladimir Markobd785672018-05-03 17:09:09 +01004447 return_type,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004448 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
4449 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004450 allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004451 number_of_arguments + number_of_other_inputs,
4452 kArenaAllocInvokeInputs),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004453 number_of_arguments_(number_of_arguments),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004454 dex_method_index_(dex_method_index),
agicsaki57b81ec2015-08-11 17:39:37 -07004455 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004456 intrinsic_optimizations_(0) {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004457 SetPackedField<InvokeTypeField>(invoke_type);
Vladimir Markoa1de9182016-02-25 11:37:38 +00004458 SetPackedFlag<kFlagCanThrow>(true);
Nicolas Geoffray76d4bb0f32018-09-21 12:58:45 +01004459 // Check mutator lock, constructors lack annotalysis support.
4460 Locks::mutator_lock_->AssertNotExclusiveHeld(Thread::Current());
4461 SetResolvedMethod(resolved_method);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004462 }
4463
Artem Serovcced8ba2017-07-19 18:18:09 +01004464 DEFAULT_COPY_CONSTRUCTOR(Invoke);
4465
Roland Levillain3e3d7332015-04-28 11:00:54 +01004466 uint32_t number_of_arguments_;
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00004467 ArtMethod* resolved_method_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004468 const uint32_t dex_method_index_;
4469 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004470
4471 // A magic word holding optimizations for intrinsics. See intrinsics.h.
4472 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004473};
4474
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004475class HInvokeUnresolved final : public HInvoke {
Calin Juravle175dc732015-08-25 15:42:32 +01004476 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004477 HInvokeUnresolved(ArenaAllocator* allocator,
Calin Juravle175dc732015-08-25 15:42:32 +01004478 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004479 DataType::Type return_type,
Calin Juravle175dc732015-08-25 15:42:32 +01004480 uint32_t dex_pc,
4481 uint32_t dex_method_index,
4482 InvokeType invoke_type)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304483 : HInvoke(kInvokeUnresolved,
4484 allocator,
Calin Juravle175dc732015-08-25 15:42:32 +01004485 number_of_arguments,
Andreas Gampe3db70682018-12-26 15:12:03 -08004486 /* number_of_other_inputs= */ 0u,
Calin Juravle175dc732015-08-25 15:42:32 +01004487 return_type,
4488 dex_pc,
4489 dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004490 nullptr,
Calin Juravle175dc732015-08-25 15:42:32 +01004491 invoke_type) {
4492 }
4493
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004494 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004495
Calin Juravle175dc732015-08-25 15:42:32 +01004496 DECLARE_INSTRUCTION(InvokeUnresolved);
4497
Artem Serovcced8ba2017-07-19 18:18:09 +01004498 protected:
4499 DEFAULT_COPY_CONSTRUCTOR(InvokeUnresolved);
Calin Juravle175dc732015-08-25 15:42:32 +01004500};
4501
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004502class HInvokePolymorphic final : public HInvoke {
Orion Hodsonac141392017-01-13 11:53:47 +00004503 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004504 HInvokePolymorphic(ArenaAllocator* allocator,
Orion Hodsonac141392017-01-13 11:53:47 +00004505 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004506 DataType::Type return_type,
Orion Hodsonac141392017-01-13 11:53:47 +00004507 uint32_t dex_pc,
4508 uint32_t dex_method_index)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304509 : HInvoke(kInvokePolymorphic,
4510 allocator,
Orion Hodsonac141392017-01-13 11:53:47 +00004511 number_of_arguments,
Andreas Gampe3db70682018-12-26 15:12:03 -08004512 /* number_of_other_inputs= */ 0u,
Orion Hodsonac141392017-01-13 11:53:47 +00004513 return_type,
4514 dex_pc,
4515 dex_method_index,
4516 nullptr,
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304517 kVirtual) {
4518 }
Orion Hodsonac141392017-01-13 11:53:47 +00004519
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004520 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004521
Orion Hodsonac141392017-01-13 11:53:47 +00004522 DECLARE_INSTRUCTION(InvokePolymorphic);
4523
Artem Serovcced8ba2017-07-19 18:18:09 +01004524 protected:
4525 DEFAULT_COPY_CONSTRUCTOR(InvokePolymorphic);
Orion Hodsonac141392017-01-13 11:53:47 +00004526};
4527
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004528class HInvokeCustom final : public HInvoke {
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004529 public:
4530 HInvokeCustom(ArenaAllocator* allocator,
4531 uint32_t number_of_arguments,
4532 uint32_t call_site_index,
4533 DataType::Type return_type,
4534 uint32_t dex_pc)
4535 : HInvoke(kInvokeCustom,
4536 allocator,
4537 number_of_arguments,
Andreas Gampe3db70682018-12-26 15:12:03 -08004538 /* number_of_other_inputs= */ 0u,
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004539 return_type,
4540 dex_pc,
Andreas Gampe3db70682018-12-26 15:12:03 -08004541 /* dex_method_index= */ dex::kDexNoIndex,
4542 /* resolved_method= */ nullptr,
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004543 kStatic),
4544 call_site_index_(call_site_index) {
4545 }
4546
4547 uint32_t GetCallSiteIndex() const { return call_site_index_; }
4548
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004549 bool IsClonable() const override { return true; }
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004550
4551 DECLARE_INSTRUCTION(InvokeCustom);
4552
4553 protected:
4554 DEFAULT_COPY_CONSTRUCTOR(InvokeCustom);
4555
4556 private:
4557 uint32_t call_site_index_;
4558};
4559
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004560class HInvokeStaticOrDirect final : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004561 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01004562 // Requirements of this method call regarding the class
4563 // initialization (clinit) check of its declaring class.
4564 enum class ClinitCheckRequirement {
4565 kNone, // Class already initialized.
4566 kExplicit, // Static call having explicit clinit check as last input.
4567 kImplicit, // Static call implicitly requiring a clinit check.
Vladimir Markoa1de9182016-02-25 11:37:38 +00004568 kLast = kImplicit
Roland Levillain4c0eb422015-04-24 16:43:49 +01004569 };
4570
Vladimir Marko58155012015-08-19 12:49:41 +00004571 // Determines how to load the target ArtMethod*.
4572 enum class MethodLoadKind {
4573 // Use a String init ArtMethod* loaded from Thread entrypoints.
4574 kStringInit,
4575
4576 // Use the method's own ArtMethod* loaded by the register allocator.
4577 kRecursive,
4578
Vladimir Marko65979462017-05-19 17:25:12 +01004579 // Use PC-relative boot image ArtMethod* address that will be known at link time.
4580 // Used for boot image methods referenced by boot image code.
4581 kBootImageLinkTimePcRelative,
4582
Vladimir Markob066d432018-01-03 13:14:37 +00004583 // Load from an entry in the .data.bimg.rel.ro using a PC-relative load.
4584 // Used for app->boot calls with relocatable image.
4585 kBootImageRelRo,
4586
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004587 // Load from an entry in the .bss section using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004588 // Used for methods outside boot image referenced by AOT-compiled app and boot image code.
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004589 kBssEntry,
Vladimir Marko58155012015-08-19 12:49:41 +00004590
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004591 // Use ArtMethod* at a known address, embed the direct address in the code.
4592 // Used for for JIT-compiled calls.
4593 kJitDirectAddress,
4594
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004595 // Make a runtime call to resolve and call the method. This is the last-resort-kind
4596 // used when other kinds are unimplemented on a particular architecture.
4597 kRuntimeCall,
Vladimir Marko58155012015-08-19 12:49:41 +00004598 };
4599
4600 // Determines the location of the code pointer.
4601 enum class CodePtrLocation {
4602 // Recursive call, use local PC-relative call instruction.
4603 kCallSelf,
4604
Vladimir Marko58155012015-08-19 12:49:41 +00004605 // Use code pointer from the ArtMethod*.
4606 // Used when we don't know the target code. This is also the last-resort-kind used when
4607 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
4608 kCallArtMethod,
4609 };
4610
4611 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01004612 MethodLoadKind method_load_kind;
4613 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00004614 // The method load data holds
4615 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
4616 // Note that there are multiple string init methods, each having its own offset.
4617 // - the method address for kDirectAddress
Vladimir Markodc151b22015-10-15 18:02:30 +01004618 uint64_t method_load_data;
Vladimir Marko58155012015-08-19 12:49:41 +00004619 };
4620
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004621 HInvokeStaticOrDirect(ArenaAllocator* allocator,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004622 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004623 DataType::Type return_type,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004624 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004625 uint32_t method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004626 ArtMethod* resolved_method,
Vladimir Marko58155012015-08-19 12:49:41 +00004627 DispatchInfo dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004628 InvokeType invoke_type,
4629 MethodReference target_method,
Roland Levillain4c0eb422015-04-24 16:43:49 +01004630 ClinitCheckRequirement clinit_check_requirement)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304631 : HInvoke(kInvokeStaticOrDirect,
4632 allocator,
Roland Levillain3e3d7332015-04-28 11:00:54 +01004633 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00004634 // There is potentially one extra argument for the HCurrentMethod node, and
Nicolas Geoffray0846a8f2018-09-12 15:21:07 +01004635 // potentially one other if the clinit check is explicit.
Vladimir Markob554b5a2015-11-06 12:57:55 +00004636 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
David Brazdildee58d62016-04-07 09:54:26 +00004637 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004638 return_type,
4639 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004640 method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004641 resolved_method,
4642 invoke_type),
Vladimir Marko58155012015-08-19 12:49:41 +00004643 target_method_(target_method),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004644 dispatch_info_(dispatch_info) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004645 SetPackedField<ClinitCheckRequirementField>(clinit_check_requirement);
4646 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004647
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004648 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004649
Vladimir Markodc151b22015-10-15 18:02:30 +01004650 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00004651 bool had_current_method_input = HasCurrentMethodInput();
4652 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
4653
4654 // Using the current method is the default and once we find a better
4655 // method load kind, we should not go back to using the current method.
4656 DCHECK(had_current_method_input || !needs_current_method_input);
4657
4658 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004659 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
4660 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004661 }
Vladimir Markodc151b22015-10-15 18:02:30 +01004662 dispatch_info_ = dispatch_info;
4663 }
4664
Aart Bik6daebeb2017-04-03 14:35:41 -07004665 DispatchInfo GetDispatchInfo() const {
4666 return dispatch_info_;
4667 }
4668
Vladimir Markoc53c0792015-11-19 15:48:33 +00004669 void AddSpecialInput(HInstruction* input) {
4670 // We allow only one special input.
4671 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
4672 DCHECK(InputCount() == GetSpecialInputIndex() ||
4673 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
4674 InsertInputAt(GetSpecialInputIndex(), input);
4675 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004676
Vladimir Marko372f10e2016-05-17 16:30:10 +01004677 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004678 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01004679 ArrayRef<HUserRecord<HInstruction*>> input_records = HInvoke::GetInputRecords();
4680 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck()) {
4681 DCHECK(!input_records.empty());
4682 DCHECK_GT(input_records.size(), GetNumberOfArguments());
4683 HInstruction* last_input = input_records.back().GetInstruction();
4684 // Note: `last_input` may be null during arguments setup.
4685 if (last_input != nullptr) {
4686 // `last_input` is the last input of a static invoke marked as having
4687 // an explicit clinit check. It must either be:
4688 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
4689 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
4690 DCHECK(last_input->IsClinitCheck() || last_input->IsLoadClass()) << last_input->DebugName();
4691 }
4692 }
4693 return input_records;
4694 }
4695
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004696 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const override {
Calin Juravle77520bc2015-01-12 18:45:46 +00004697 // We access the method via the dex cache so we can't do an implicit null check.
4698 // TODO: for intrinsics we can generate implicit null checks.
4699 return false;
4700 }
4701
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004702 bool CanBeNull() const override {
Vladimir Markobd785672018-05-03 17:09:09 +01004703 return GetType() == DataType::Type::kReference && !IsStringInit();
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004704 }
4705
Vladimir Markoc53c0792015-11-19 15:48:33 +00004706 // Get the index of the special input, if any.
4707 //
David Brazdil6de19382016-01-08 17:37:10 +00004708 // If the invoke HasCurrentMethodInput(), the "special input" is the current
4709 // method pointer; otherwise there may be one platform-specific special input,
4710 // such as PC-relative addressing base.
Vladimir Markoc53c0792015-11-19 15:48:33 +00004711 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray97793072016-02-16 15:33:54 +00004712 bool HasSpecialInput() const { return GetNumberOfArguments() != InputCount(); }
Vladimir Markoc53c0792015-11-19 15:48:33 +00004713
Vladimir Marko58155012015-08-19 12:49:41 +00004714 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
4715 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
4716 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004717 bool NeedsDexCacheOfDeclaringClass() const override;
Vladimir Marko58155012015-08-19 12:49:41 +00004718 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004719 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kJitDirectAddress; }
Vladimir Marko65979462017-05-19 17:25:12 +01004720 bool HasPcRelativeMethodLoadKind() const {
4721 return GetMethodLoadKind() == MethodLoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markob066d432018-01-03 13:14:37 +00004722 GetMethodLoadKind() == MethodLoadKind::kBootImageRelRo ||
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004723 GetMethodLoadKind() == MethodLoadKind::kBssEntry;
Vladimir Marko65979462017-05-19 17:25:12 +01004724 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004725 bool HasCurrentMethodInput() const {
4726 // This function can be called only after the invoke has been fully initialized by the builder.
4727 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004728 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004729 return true;
4730 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004731 DCHECK(InputCount() == GetSpecialInputIndex() ||
4732 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004733 return false;
4734 }
4735 }
Vladimir Marko58155012015-08-19 12:49:41 +00004736
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004737 QuickEntrypointEnum GetStringInitEntryPoint() const {
Vladimir Marko58155012015-08-19 12:49:41 +00004738 DCHECK(IsStringInit());
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004739 return static_cast<QuickEntrypointEnum>(dispatch_info_.method_load_data);
Vladimir Marko58155012015-08-19 12:49:41 +00004740 }
4741
4742 uint64_t GetMethodAddress() const {
4743 DCHECK(HasMethodAddress());
4744 return dispatch_info_.method_load_data;
4745 }
4746
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00004747 const DexFile& GetDexFileForPcRelativeDexCache() const;
4748
Vladimir Markoa1de9182016-02-25 11:37:38 +00004749 ClinitCheckRequirement GetClinitCheckRequirement() const {
4750 return GetPackedField<ClinitCheckRequirementField>();
4751 }
Calin Juravle68ad6492015-08-18 17:08:12 +01004752
Roland Levillain4c0eb422015-04-24 16:43:49 +01004753 // Is this instruction a call to a static method?
4754 bool IsStatic() const {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004755 return GetInvokeType() == kStatic;
4756 }
4757
4758 MethodReference GetTargetMethod() const {
4759 return target_method_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01004760 }
4761
Vladimir Markofbb184a2015-11-13 14:47:00 +00004762 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
4763 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
4764 // instruction; only relevant for static calls with explicit clinit check.
4765 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01004766 DCHECK(IsStaticWithExplicitClinitCheck());
Vladimir Marko372f10e2016-05-17 16:30:10 +01004767 size_t last_input_index = inputs_.size() - 1u;
4768 HInstruction* last_input = inputs_.back().GetInstruction();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004769 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004770 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004771 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004772 inputs_.pop_back();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004773 SetPackedField<ClinitCheckRequirementField>(new_requirement);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004774 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004775 }
4776
4777 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00004778 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01004779 bool IsStaticWithExplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004780 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kExplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004781 }
4782
4783 // Is this a call to a static method whose declaring class has an
4784 // implicit intialization check requirement?
4785 bool IsStaticWithImplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004786 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kImplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004787 }
4788
Vladimir Markob554b5a2015-11-06 12:57:55 +00004789 // Does this method load kind need the current method as an input?
4790 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004791 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kRuntimeCall;
Vladimir Markob554b5a2015-11-06 12:57:55 +00004792 }
4793
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004794 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004795
Artem Serovcced8ba2017-07-19 18:18:09 +01004796 protected:
4797 DEFAULT_COPY_CONSTRUCTOR(InvokeStaticOrDirect);
4798
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004799 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004800 static constexpr size_t kFieldClinitCheckRequirement = kNumberOfInvokePackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004801 static constexpr size_t kFieldClinitCheckRequirementSize =
4802 MinimumBitsToStore(static_cast<size_t>(ClinitCheckRequirement::kLast));
4803 static constexpr size_t kNumberOfInvokeStaticOrDirectPackedBits =
4804 kFieldClinitCheckRequirement + kFieldClinitCheckRequirementSize;
4805 static_assert(kNumberOfInvokeStaticOrDirectPackedBits <= kMaxNumberOfPackedBits,
4806 "Too many packed fields.");
Vladimir Markoa1de9182016-02-25 11:37:38 +00004807 using ClinitCheckRequirementField = BitField<ClinitCheckRequirement,
4808 kFieldClinitCheckRequirement,
4809 kFieldClinitCheckRequirementSize>;
4810
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004811 // Cached values of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffrayea179f42018-02-08 22:30:18 +00004812 const MethodReference target_method_;
Vladimir Marko58155012015-08-19 12:49:41 +00004813 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004814};
Vladimir Markof64242a2015-12-01 14:58:23 +00004815std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004816std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004817
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004818class HInvokeVirtual final : public HInvoke {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004819 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004820 HInvokeVirtual(ArenaAllocator* allocator,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004821 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004822 DataType::Type return_type,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004823 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004824 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004825 ArtMethod* resolved_method,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004826 uint32_t vtable_index)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304827 : HInvoke(kInvokeVirtual,
4828 allocator,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004829 number_of_arguments,
4830 0u,
4831 return_type,
4832 dex_pc,
4833 dex_method_index,
4834 resolved_method,
4835 kVirtual),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304836 vtable_index_(vtable_index) {
4837 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004838
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004839 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004840
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004841 bool CanBeNull() const override {
Aart Bik71bf7b42016-11-16 10:17:46 -08004842 switch (GetIntrinsic()) {
4843 case Intrinsics::kThreadCurrentThread:
4844 case Intrinsics::kStringBufferAppend:
4845 case Intrinsics::kStringBufferToString:
Vladimir Markod4561172017-10-30 17:48:25 +00004846 case Intrinsics::kStringBuilderAppendObject:
4847 case Intrinsics::kStringBuilderAppendString:
4848 case Intrinsics::kStringBuilderAppendCharSequence:
4849 case Intrinsics::kStringBuilderAppendCharArray:
4850 case Intrinsics::kStringBuilderAppendBoolean:
4851 case Intrinsics::kStringBuilderAppendChar:
4852 case Intrinsics::kStringBuilderAppendInt:
4853 case Intrinsics::kStringBuilderAppendLong:
4854 case Intrinsics::kStringBuilderAppendFloat:
4855 case Intrinsics::kStringBuilderAppendDouble:
Aart Bik71bf7b42016-11-16 10:17:46 -08004856 case Intrinsics::kStringBuilderToString:
4857 return false;
4858 default:
4859 return HInvoke::CanBeNull();
4860 }
4861 }
4862
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004863 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Calin Juravle77520bc2015-01-12 18:45:46 +00004864 // TODO: Add implicit null checks in intrinsics.
Nicolas Geoffray61ba8d22018-08-07 09:55:57 +01004865 return (obj == InputAt(0)) && !IsIntrinsic();
Calin Juravle77520bc2015-01-12 18:45:46 +00004866 }
4867
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004868 uint32_t GetVTableIndex() const { return vtable_index_; }
4869
4870 DECLARE_INSTRUCTION(InvokeVirtual);
4871
Artem Serovcced8ba2017-07-19 18:18:09 +01004872 protected:
4873 DEFAULT_COPY_CONSTRUCTOR(InvokeVirtual);
4874
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004875 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004876 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004877 const uint32_t vtable_index_;
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004878};
4879
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004880class HInvokeInterface final : public HInvoke {
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004881 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004882 HInvokeInterface(ArenaAllocator* allocator,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004883 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004884 DataType::Type return_type,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004885 uint32_t dex_pc,
4886 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004887 ArtMethod* resolved_method,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004888 uint32_t imt_index)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304889 : HInvoke(kInvokeInterface,
4890 allocator,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004891 number_of_arguments,
4892 0u,
4893 return_type,
4894 dex_pc,
4895 dex_method_index,
4896 resolved_method,
4897 kInterface),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304898 imt_index_(imt_index) {
4899 }
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004900
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004901 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004902
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004903 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Calin Juravle77520bc2015-01-12 18:45:46 +00004904 // TODO: Add implicit null checks in intrinsics.
Nicolas Geoffray61ba8d22018-08-07 09:55:57 +01004905 return (obj == InputAt(0)) && !IsIntrinsic();
Calin Juravle77520bc2015-01-12 18:45:46 +00004906 }
4907
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004908 bool NeedsDexCacheOfDeclaringClass() const override {
Nicolas Geoffrayfbdfa6d2017-02-03 10:43:13 +00004909 // The assembly stub currently needs it.
4910 return true;
4911 }
4912
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004913 uint32_t GetImtIndex() const { return imt_index_; }
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004914
4915 DECLARE_INSTRUCTION(InvokeInterface);
4916
Artem Serovcced8ba2017-07-19 18:18:09 +01004917 protected:
4918 DEFAULT_COPY_CONSTRUCTOR(InvokeInterface);
4919
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004920 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004921 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004922 const uint32_t imt_index_;
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004923};
4924
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004925class HNeg final : public HUnaryOperation {
Roland Levillain88cb1752014-10-20 16:36:47 +01004926 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004927 HNeg(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304928 : HUnaryOperation(kNeg, result_type, input, dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004929 DCHECK_EQ(result_type, DataType::Kind(input->GetType()));
Roland Levillain937e6cd2016-03-22 11:54:37 +00004930 }
Roland Levillain88cb1752014-10-20 16:36:47 +01004931
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004932 template <typename T> static T Compute(T x) { return -x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004933
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004934 HConstant* Evaluate(HIntConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004935 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004936 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004937 HConstant* Evaluate(HLongConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004938 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004939 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004940 HConstant* Evaluate(HFloatConstant* x) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004941 return GetBlock()->GetGraph()->GetFloatConstant(Compute(x->GetValue()), GetDexPc());
4942 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004943 HConstant* Evaluate(HDoubleConstant* x) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004944 return GetBlock()->GetGraph()->GetDoubleConstant(Compute(x->GetValue()), GetDexPc());
4945 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01004946
Roland Levillain88cb1752014-10-20 16:36:47 +01004947 DECLARE_INSTRUCTION(Neg);
4948
Artem Serovcced8ba2017-07-19 18:18:09 +01004949 protected:
4950 DEFAULT_COPY_CONSTRUCTOR(Neg);
Roland Levillain88cb1752014-10-20 16:36:47 +01004951};
4952
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004953class HNewArray final : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004954 public:
Vladimir Markob5461632018-10-15 14:24:21 +01004955 HNewArray(HInstruction* cls, HInstruction* length, uint32_t dex_pc, size_t component_size_shift)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304956 : HExpression(kNewArray, DataType::Type::kReference, SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004957 SetRawInputAt(0, cls);
4958 SetRawInputAt(1, length);
Vladimir Markob5461632018-10-15 14:24:21 +01004959 SetPackedField<ComponentSizeShiftField>(component_size_shift);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004960 }
4961
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004962 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004963
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004964 // Calls runtime so needs an environment.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004965 bool NeedsEnvironment() const override { return true; }
Calin Juravle10e244f2015-01-26 18:54:32 +00004966
Mingyao Yang0c365e62015-03-31 15:09:29 -07004967 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004968 bool CanThrow() const override { return true; }
Mingyao Yang0c365e62015-03-31 15:09:29 -07004969
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004970 bool CanBeNull() const override { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004971
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004972 HLoadClass* GetLoadClass() const {
4973 DCHECK(InputAt(0)->IsLoadClass());
4974 return InputAt(0)->AsLoadClass();
4975 }
4976
4977 HInstruction* GetLength() const {
4978 return InputAt(1);
4979 }
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004980
Vladimir Markob5461632018-10-15 14:24:21 +01004981 size_t GetComponentSizeShift() {
4982 return GetPackedField<ComponentSizeShiftField>();
4983 }
4984
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004985 DECLARE_INSTRUCTION(NewArray);
4986
Artem Serovcced8ba2017-07-19 18:18:09 +01004987 protected:
4988 DEFAULT_COPY_CONSTRUCTOR(NewArray);
Vladimir Markob5461632018-10-15 14:24:21 +01004989
4990 private:
4991 static constexpr size_t kFieldComponentSizeShift = kNumberOfGenericPackedBits;
4992 static constexpr size_t kFieldComponentSizeShiftSize = MinimumBitsToStore(3u);
4993 static constexpr size_t kNumberOfNewArrayPackedBits =
4994 kFieldComponentSizeShift + kFieldComponentSizeShiftSize;
4995 static_assert(kNumberOfNewArrayPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
4996 using ComponentSizeShiftField =
4997 BitField<size_t, kFieldComponentSizeShift, kFieldComponentSizeShift>;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004998};
4999
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005000class HAdd final : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00005001 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005002 HAdd(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005003 HInstruction* left,
5004 HInstruction* right,
5005 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305006 : HBinaryOperation(kAdd, result_type, left, right, SideEffects::None(), dex_pc) {
5007 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00005008
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005009 bool IsCommutative() const override { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00005010
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005011 template <typename T> static T Compute(T x, T y) { return x + y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005012
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005013 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005014 return GetBlock()->GetGraph()->GetIntConstant(
5015 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01005016 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005017 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005018 return GetBlock()->GetGraph()->GetLongConstant(
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(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005022 return GetBlock()->GetGraph()->GetFloatConstant(
5023 Compute(x->GetValue(), y->GetValue()), GetDexPc());
5024 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005025 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005026 return GetBlock()->GetGraph()->GetDoubleConstant(
5027 Compute(x->GetValue(), y->GetValue()), GetDexPc());
5028 }
Roland Levillain556c3d12014-09-18 15:25:07 +01005029
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00005030 DECLARE_INSTRUCTION(Add);
5031
Artem Serovcced8ba2017-07-19 18:18:09 +01005032 protected:
5033 DEFAULT_COPY_CONSTRUCTOR(Add);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00005034};
5035
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005036class HSub final : public HBinaryOperation {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005037 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005038 HSub(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005039 HInstruction* left,
5040 HInstruction* right,
5041 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305042 : HBinaryOperation(kSub, result_type, left, right, SideEffects::None(), dex_pc) {
5043 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005044
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005045 template <typename T> static T Compute(T x, T y) { return x - y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005046
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005047 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005048 return GetBlock()->GetGraph()->GetIntConstant(
5049 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01005050 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005051 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005052 return GetBlock()->GetGraph()->GetLongConstant(
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(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005056 return GetBlock()->GetGraph()->GetFloatConstant(
5057 Compute(x->GetValue(), y->GetValue()), GetDexPc());
5058 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005059 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005060 return GetBlock()->GetGraph()->GetDoubleConstant(
5061 Compute(x->GetValue(), y->GetValue()), GetDexPc());
5062 }
Roland Levillain556c3d12014-09-18 15:25:07 +01005063
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005064 DECLARE_INSTRUCTION(Sub);
5065
Artem Serovcced8ba2017-07-19 18:18:09 +01005066 protected:
5067 DEFAULT_COPY_CONSTRUCTOR(Sub);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005068};
5069
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005070class HMul final : public HBinaryOperation {
Calin Juravle34bacdf2014-10-07 20:23:36 +01005071 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005072 HMul(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005073 HInstruction* left,
5074 HInstruction* right,
5075 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305076 : HBinaryOperation(kMul, result_type, left, right, SideEffects::None(), dex_pc) {
5077 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01005078
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005079 bool IsCommutative() const override { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01005080
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005081 template <typename T> static T Compute(T x, T y) { return x * y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005082
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005083 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005084 return GetBlock()->GetGraph()->GetIntConstant(
5085 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005086 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005087 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005088 return GetBlock()->GetGraph()->GetLongConstant(
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(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005092 return GetBlock()->GetGraph()->GetFloatConstant(
5093 Compute(x->GetValue(), y->GetValue()), GetDexPc());
5094 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005095 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005096 return GetBlock()->GetGraph()->GetDoubleConstant(
5097 Compute(x->GetValue(), y->GetValue()), GetDexPc());
5098 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01005099
5100 DECLARE_INSTRUCTION(Mul);
5101
Artem Serovcced8ba2017-07-19 18:18:09 +01005102 protected:
5103 DEFAULT_COPY_CONSTRUCTOR(Mul);
Calin Juravle34bacdf2014-10-07 20:23:36 +01005104};
5105
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005106class HDiv final : public HBinaryOperation {
Calin Juravle7c4954d2014-10-28 16:57:40 +00005107 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005108 HDiv(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005109 HInstruction* left,
5110 HInstruction* right,
5111 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305112 : HBinaryOperation(kDiv, result_type, left, right, SideEffects::None(), dex_pc) {
5113 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00005114
Roland Levillain9867bc72015-08-05 10:21:34 +01005115 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00005116 T ComputeIntegral(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005117 DCHECK(!DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01005118 // Our graph structure ensures we never have 0 for `y` during
5119 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00005120 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00005121 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00005122 return (y == -1) ? -x : x / y;
5123 }
Calin Juravlebacfec32014-11-14 15:54:36 +00005124
Roland Levillain31dd3d62016-02-16 12:21:02 +00005125 template <typename T>
5126 T ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005127 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00005128 return x / y;
5129 }
5130
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005131 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005132 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00005133 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005134 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005135 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005136 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00005137 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5138 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005139 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005140 return GetBlock()->GetGraph()->GetFloatConstant(
5141 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
5142 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005143 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005144 return GetBlock()->GetGraph()->GetDoubleConstant(
5145 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00005146 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00005147
5148 DECLARE_INSTRUCTION(Div);
5149
Artem Serovcced8ba2017-07-19 18:18:09 +01005150 protected:
5151 DEFAULT_COPY_CONSTRUCTOR(Div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00005152};
5153
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005154class HRem final : public HBinaryOperation {
Calin Juravlebacfec32014-11-14 15:54:36 +00005155 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005156 HRem(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005157 HInstruction* left,
5158 HInstruction* right,
5159 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305160 : HBinaryOperation(kRem, result_type, left, right, SideEffects::None(), dex_pc) {
5161 }
Calin Juravlebacfec32014-11-14 15:54:36 +00005162
Roland Levillain9867bc72015-08-05 10:21:34 +01005163 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00005164 T ComputeIntegral(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005165 DCHECK(!DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01005166 // Our graph structure ensures we never have 0 for `y` during
5167 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00005168 DCHECK_NE(y, 0);
5169 // Special case -1 to avoid getting a SIGFPE on x86(_64).
5170 return (y == -1) ? 0 : x % y;
5171 }
5172
Roland Levillain31dd3d62016-02-16 12:21:02 +00005173 template <typename T>
5174 T ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005175 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00005176 return std::fmod(x, y);
5177 }
5178
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005179 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005180 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00005181 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005182 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005183 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005184 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00005185 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00005186 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005187 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005188 return GetBlock()->GetGraph()->GetFloatConstant(
5189 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
5190 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005191 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005192 return GetBlock()->GetGraph()->GetDoubleConstant(
5193 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
5194 }
Calin Juravlebacfec32014-11-14 15:54:36 +00005195
5196 DECLARE_INSTRUCTION(Rem);
5197
Artem Serovcced8ba2017-07-19 18:18:09 +01005198 protected:
5199 DEFAULT_COPY_CONSTRUCTOR(Rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00005200};
5201
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005202class HMin final : public HBinaryOperation {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005203 public:
5204 HMin(DataType::Type result_type,
5205 HInstruction* left,
5206 HInstruction* right,
5207 uint32_t dex_pc)
5208 : HBinaryOperation(kMin, result_type, left, right, SideEffects::None(), dex_pc) {}
5209
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005210 bool IsCommutative() const override { return true; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005211
5212 // Evaluation for integral values.
5213 template <typename T> static T ComputeIntegral(T x, T y) {
5214 return (x <= y) ? x : y;
5215 }
5216
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005217 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005218 return GetBlock()->GetGraph()->GetIntConstant(
5219 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5220 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005221 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005222 return GetBlock()->GetGraph()->GetLongConstant(
5223 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5224 }
5225 // TODO: Evaluation for floating-point values.
5226 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005227 HFloatConstant* y ATTRIBUTE_UNUSED) const override { return nullptr; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005228 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005229 HDoubleConstant* y ATTRIBUTE_UNUSED) const override { return nullptr; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005230
5231 DECLARE_INSTRUCTION(Min);
5232
5233 protected:
5234 DEFAULT_COPY_CONSTRUCTOR(Min);
5235};
5236
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005237class HMax final : public HBinaryOperation {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005238 public:
5239 HMax(DataType::Type result_type,
5240 HInstruction* left,
5241 HInstruction* right,
5242 uint32_t dex_pc)
5243 : HBinaryOperation(kMax, result_type, left, right, SideEffects::None(), dex_pc) {}
5244
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005245 bool IsCommutative() const override { return true; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005246
5247 // Evaluation for integral values.
5248 template <typename T> static T ComputeIntegral(T x, T y) {
5249 return (x >= y) ? x : y;
5250 }
5251
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005252 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005253 return GetBlock()->GetGraph()->GetIntConstant(
5254 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5255 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005256 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005257 return GetBlock()->GetGraph()->GetLongConstant(
5258 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5259 }
5260 // TODO: Evaluation for floating-point values.
5261 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005262 HFloatConstant* y ATTRIBUTE_UNUSED) const override { return nullptr; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005263 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005264 HDoubleConstant* y ATTRIBUTE_UNUSED) const override { return nullptr; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005265
5266 DECLARE_INSTRUCTION(Max);
5267
5268 protected:
5269 DEFAULT_COPY_CONSTRUCTOR(Max);
5270};
5271
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005272class HAbs final : public HUnaryOperation {
Aart Bik3dad3412018-02-28 12:01:46 -08005273 public:
5274 HAbs(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
5275 : HUnaryOperation(kAbs, result_type, input, dex_pc) {}
5276
5277 // Evaluation for integral values.
5278 template <typename T> static T ComputeIntegral(T x) {
5279 return x < 0 ? -x : x;
5280 }
5281
5282 // Evaluation for floating-point values.
5283 // Note, as a "quality of implementation", rather than pure "spec compliance",
5284 // we require that Math.abs() clears the sign bit (but changes nothing else)
5285 // for all floating-point numbers, including NaN (signaling NaN may become quiet though).
5286 // http://b/30758343
5287 template <typename T, typename S> static T ComputeFP(T x) {
5288 S bits = bit_cast<S, T>(x);
5289 return bit_cast<T, S>(bits & std::numeric_limits<S>::max());
5290 }
5291
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005292 HConstant* Evaluate(HIntConstant* x) const override {
Aart Bik3dad3412018-02-28 12:01:46 -08005293 return GetBlock()->GetGraph()->GetIntConstant(ComputeIntegral(x->GetValue()), GetDexPc());
5294 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005295 HConstant* Evaluate(HLongConstant* x) const override {
Aart Bik3dad3412018-02-28 12:01:46 -08005296 return GetBlock()->GetGraph()->GetLongConstant(ComputeIntegral(x->GetValue()), GetDexPc());
5297 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005298 HConstant* Evaluate(HFloatConstant* x) const override {
Aart Bik3dad3412018-02-28 12:01:46 -08005299 return GetBlock()->GetGraph()->GetFloatConstant(
5300 ComputeFP<float, int32_t>(x->GetValue()), GetDexPc());
5301 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005302 HConstant* Evaluate(HDoubleConstant* x) const override {
Aart Bik3dad3412018-02-28 12:01:46 -08005303 return GetBlock()->GetGraph()->GetDoubleConstant(
5304 ComputeFP<double, int64_t>(x->GetValue()), GetDexPc());
5305 }
5306
5307 DECLARE_INSTRUCTION(Abs);
5308
5309 protected:
5310 DEFAULT_COPY_CONSTRUCTOR(Abs);
5311};
5312
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005313class HDivZeroCheck final : public HExpression<1> {
Calin Juravled0d48522014-11-04 16:40:20 +00005314 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00005315 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
Artem Serovd1aa7d02018-06-22 11:35:46 +01005316 // constructor. However it can only do it on a fatal slow path so execution never returns to the
5317 // instruction following the current one; thus 'SideEffects::None()' is used.
Calin Juravled0d48522014-11-04 16:40:20 +00005318 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Artem Serovd1aa7d02018-06-22 11:35:46 +01005319 : HExpression(kDivZeroCheck, value->GetType(), SideEffects::None(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00005320 SetRawInputAt(0, value);
5321 }
5322
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005323 bool IsClonable() const override { return true; }
5324 bool CanBeMoved() const override { return true; }
Calin Juravled0d48522014-11-04 16:40:20 +00005325
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005326 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Calin Juravled0d48522014-11-04 16:40:20 +00005327 return true;
5328 }
5329
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005330 bool NeedsEnvironment() const override { return true; }
5331 bool CanThrow() const override { return true; }
Calin Juravled0d48522014-11-04 16:40:20 +00005332
Calin Juravled0d48522014-11-04 16:40:20 +00005333 DECLARE_INSTRUCTION(DivZeroCheck);
5334
Artem Serovcced8ba2017-07-19 18:18:09 +01005335 protected:
5336 DEFAULT_COPY_CONSTRUCTOR(DivZeroCheck);
Calin Juravled0d48522014-11-04 16:40:20 +00005337};
5338
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005339class HShl final : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005340 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005341 HShl(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005342 HInstruction* value,
5343 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005344 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305345 : HBinaryOperation(kShl, result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005346 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5347 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01005348 }
5349
Roland Levillain5b5b9312016-03-22 14:57:31 +00005350 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005351 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005352 return value << (distance & max_shift_distance);
5353 }
5354
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005355 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005356 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005357 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005358 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005359 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005360 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005361 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005362 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005363 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005364 HLongConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005365 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5366 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01005367 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005368 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005369 HFloatConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005370 LOG(FATAL) << DebugName() << " is not defined for float values";
5371 UNREACHABLE();
5372 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005373 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005374 HDoubleConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005375 LOG(FATAL) << DebugName() << " is not defined for double values";
5376 UNREACHABLE();
5377 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005378
5379 DECLARE_INSTRUCTION(Shl);
5380
Artem Serovcced8ba2017-07-19 18:18:09 +01005381 protected:
5382 DEFAULT_COPY_CONSTRUCTOR(Shl);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005383};
5384
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005385class HShr final : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005386 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005387 HShr(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005388 HInstruction* value,
5389 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005390 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305391 : HBinaryOperation(kShr, result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005392 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5393 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01005394 }
5395
Roland Levillain5b5b9312016-03-22 14:57:31 +00005396 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005397 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005398 return value >> (distance & max_shift_distance);
5399 }
5400
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005401 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005402 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005403 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005404 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005405 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005406 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005407 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005408 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005409 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005410 HLongConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005411 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5412 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01005413 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005414 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005415 HFloatConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005416 LOG(FATAL) << DebugName() << " is not defined for float values";
5417 UNREACHABLE();
5418 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005419 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005420 HDoubleConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005421 LOG(FATAL) << DebugName() << " is not defined for double values";
5422 UNREACHABLE();
5423 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005424
5425 DECLARE_INSTRUCTION(Shr);
5426
Artem Serovcced8ba2017-07-19 18:18:09 +01005427 protected:
5428 DEFAULT_COPY_CONSTRUCTOR(Shr);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005429};
5430
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005431class HUShr final : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005432 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005433 HUShr(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005434 HInstruction* value,
5435 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005436 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305437 : HBinaryOperation(kUShr, result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005438 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5439 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Calin Juravle9aec02f2014-11-18 23:06:35 +00005440 }
5441
Roland Levillain5b5b9312016-03-22 14:57:31 +00005442 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005443 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005444 typedef typename std::make_unsigned<T>::type V;
5445 V ux = static_cast<V>(value);
5446 return static_cast<T>(ux >> (distance & max_shift_distance));
5447 }
5448
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005449 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005450 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005451 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005452 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005453 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005454 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005455 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005456 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005457 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005458 HLongConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005459 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5460 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00005461 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005462 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005463 HFloatConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005464 LOG(FATAL) << DebugName() << " is not defined for float values";
5465 UNREACHABLE();
5466 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005467 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005468 HDoubleConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005469 LOG(FATAL) << DebugName() << " is not defined for double values";
5470 UNREACHABLE();
5471 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005472
5473 DECLARE_INSTRUCTION(UShr);
5474
Artem Serovcced8ba2017-07-19 18:18:09 +01005475 protected:
5476 DEFAULT_COPY_CONSTRUCTOR(UShr);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005477};
5478
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005479class HAnd final : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005480 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005481 HAnd(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005482 HInstruction* left,
5483 HInstruction* right,
5484 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305485 : HBinaryOperation(kAnd, result_type, left, right, SideEffects::None(), dex_pc) {
5486 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005487
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005488 bool IsCommutative() const override { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005489
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005490 template <typename T> static T Compute(T x, T y) { return x & y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005491
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005492 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005493 return GetBlock()->GetGraph()->GetIntConstant(
5494 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005495 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005496 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005497 return GetBlock()->GetGraph()->GetLongConstant(
5498 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005499 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005500 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005501 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005502 LOG(FATAL) << DebugName() << " is not defined for float values";
5503 UNREACHABLE();
5504 }
5505 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005506 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005507 LOG(FATAL) << DebugName() << " is not defined for double values";
5508 UNREACHABLE();
5509 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005510
5511 DECLARE_INSTRUCTION(And);
5512
Artem Serovcced8ba2017-07-19 18:18:09 +01005513 protected:
5514 DEFAULT_COPY_CONSTRUCTOR(And);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005515};
5516
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005517class HOr final : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005518 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005519 HOr(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005520 HInstruction* left,
5521 HInstruction* right,
5522 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305523 : HBinaryOperation(kOr, result_type, left, right, SideEffects::None(), dex_pc) {
5524 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005525
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005526 bool IsCommutative() const override { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005527
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005528 template <typename T> static T Compute(T x, T y) { return x | y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005529
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005530 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005531 return GetBlock()->GetGraph()->GetIntConstant(
5532 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005533 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005534 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005535 return GetBlock()->GetGraph()->GetLongConstant(
5536 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005537 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005538 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005539 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005540 LOG(FATAL) << DebugName() << " is not defined for float values";
5541 UNREACHABLE();
5542 }
5543 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005544 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005545 LOG(FATAL) << DebugName() << " is not defined for double values";
5546 UNREACHABLE();
5547 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005548
5549 DECLARE_INSTRUCTION(Or);
5550
Artem Serovcced8ba2017-07-19 18:18:09 +01005551 protected:
5552 DEFAULT_COPY_CONSTRUCTOR(Or);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005553};
5554
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005555class HXor final : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005556 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005557 HXor(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005558 HInstruction* left,
5559 HInstruction* right,
5560 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305561 : HBinaryOperation(kXor, result_type, left, right, SideEffects::None(), dex_pc) {
5562 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005563
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005564 bool IsCommutative() const override { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005565
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005566 template <typename T> static T Compute(T x, T y) { return x ^ y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005567
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005568 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005569 return GetBlock()->GetGraph()->GetIntConstant(
5570 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005571 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005572 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005573 return GetBlock()->GetGraph()->GetLongConstant(
5574 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005575 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005576 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005577 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005578 LOG(FATAL) << DebugName() << " is not defined for float values";
5579 UNREACHABLE();
5580 }
5581 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005582 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005583 LOG(FATAL) << DebugName() << " is not defined for double values";
5584 UNREACHABLE();
5585 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005586
5587 DECLARE_INSTRUCTION(Xor);
5588
Artem Serovcced8ba2017-07-19 18:18:09 +01005589 protected:
5590 DEFAULT_COPY_CONSTRUCTOR(Xor);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005591};
5592
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005593class HRor final : public HBinaryOperation {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005594 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005595 HRor(DataType::Type result_type, HInstruction* value, HInstruction* distance)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305596 : HBinaryOperation(kRor, result_type, value, distance) {
Roland Levillain22c49222016-03-18 14:04:28 +00005597 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005598
Roland Levillain5b5b9312016-03-22 14:57:31 +00005599 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005600 static T Compute(T value, int32_t distance, int32_t max_shift_value) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005601 typedef typename std::make_unsigned<T>::type V;
5602 V ux = static_cast<V>(value);
5603 if ((distance & max_shift_value) == 0) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005604 return static_cast<T>(ux);
5605 } else {
5606 const V reg_bits = sizeof(T) * 8;
Roland Levillain5b5b9312016-03-22 14:57:31 +00005607 return static_cast<T>(ux >> (distance & max_shift_value)) |
5608 (value << (reg_bits - (distance & max_shift_value)));
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005609 }
5610 }
5611
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005612 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const override {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005613 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005614 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005615 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005616 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const override {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005617 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005618 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005619 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005620 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005621 HLongConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005622 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5623 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005624 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005625 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005626 HFloatConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005627 LOG(FATAL) << DebugName() << " is not defined for float values";
5628 UNREACHABLE();
5629 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005630 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005631 HDoubleConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005632 LOG(FATAL) << DebugName() << " is not defined for double values";
5633 UNREACHABLE();
5634 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005635
5636 DECLARE_INSTRUCTION(Ror);
5637
Artem Serovcced8ba2017-07-19 18:18:09 +01005638 protected:
5639 DEFAULT_COPY_CONSTRUCTOR(Ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005640};
5641
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005642// The value of a parameter in this method. Its location depends on
5643// the calling convention.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005644class HParameterValue final : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005645 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005646 HParameterValue(const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08005647 dex::TypeIndex type_index,
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005648 uint8_t index,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005649 DataType::Type parameter_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005650 bool is_this = false)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305651 : HExpression(kParameterValue, parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005652 dex_file_(dex_file),
5653 type_index_(type_index),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005654 index_(index) {
5655 SetPackedFlag<kFlagIsThis>(is_this);
5656 SetPackedFlag<kFlagCanBeNull>(!is_this);
5657 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005658
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005659 const DexFile& GetDexFile() const { return dex_file_; }
Andreas Gampea5b09a62016-11-17 15:21:22 -08005660 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005661 uint8_t GetIndex() const { return index_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07005662 bool IsThis() const { return GetPackedFlag<kFlagIsThis>(); }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005663
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005664 bool CanBeNull() const override { return GetPackedFlag<kFlagCanBeNull>(); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005665 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00005666
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005667 DECLARE_INSTRUCTION(ParameterValue);
5668
Artem Serovcced8ba2017-07-19 18:18:09 +01005669 protected:
5670 DEFAULT_COPY_CONSTRUCTOR(ParameterValue);
5671
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005672 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005673 // Whether or not the parameter value corresponds to 'this' argument.
Vladimir Markobd785672018-05-03 17:09:09 +01005674 static constexpr size_t kFlagIsThis = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005675 static constexpr size_t kFlagCanBeNull = kFlagIsThis + 1;
5676 static constexpr size_t kNumberOfParameterValuePackedBits = kFlagCanBeNull + 1;
5677 static_assert(kNumberOfParameterValuePackedBits <= kMaxNumberOfPackedBits,
5678 "Too many packed fields.");
5679
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005680 const DexFile& dex_file_;
Andreas Gampea5b09a62016-11-17 15:21:22 -08005681 const dex::TypeIndex type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005682 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00005683 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005684 const uint8_t index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005685};
5686
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005687class HNot final : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005688 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005689 HNot(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305690 : HUnaryOperation(kNot, result_type, input, dex_pc) {
5691 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005692
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005693 bool CanBeMoved() const override { return true; }
5694 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005695 return true;
5696 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005697
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005698 template <typename T> static T Compute(T x) { return ~x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005699
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005700 HConstant* Evaluate(HIntConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005701 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005702 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005703 HConstant* Evaluate(HLongConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005704 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005705 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005706 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005707 LOG(FATAL) << DebugName() << " is not defined for float values";
5708 UNREACHABLE();
5709 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005710 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005711 LOG(FATAL) << DebugName() << " is not defined for double values";
5712 UNREACHABLE();
5713 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01005714
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005715 DECLARE_INSTRUCTION(Not);
5716
Artem Serovcced8ba2017-07-19 18:18:09 +01005717 protected:
5718 DEFAULT_COPY_CONSTRUCTOR(Not);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005719};
5720
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005721class HBooleanNot final : public HUnaryOperation {
David Brazdil66d126e2015-04-03 16:02:44 +01005722 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005723 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305724 : HUnaryOperation(kBooleanNot, DataType::Type::kBool, input, dex_pc) {
5725 }
David Brazdil66d126e2015-04-03 16:02:44 +01005726
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005727 bool CanBeMoved() const override { return true; }
5728 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
David Brazdil66d126e2015-04-03 16:02:44 +01005729 return true;
5730 }
5731
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005732 template <typename T> static bool Compute(T x) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005733 DCHECK(IsUint<1>(x)) << x;
David Brazdil66d126e2015-04-03 16:02:44 +01005734 return !x;
5735 }
5736
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005737 HConstant* Evaluate(HIntConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005738 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005739 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005740 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005741 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01005742 UNREACHABLE();
5743 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005744 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005745 LOG(FATAL) << DebugName() << " is not defined for float values";
5746 UNREACHABLE();
5747 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005748 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005749 LOG(FATAL) << DebugName() << " is not defined for double values";
5750 UNREACHABLE();
5751 }
David Brazdil66d126e2015-04-03 16:02:44 +01005752
5753 DECLARE_INSTRUCTION(BooleanNot);
5754
Artem Serovcced8ba2017-07-19 18:18:09 +01005755 protected:
5756 DEFAULT_COPY_CONSTRUCTOR(BooleanNot);
David Brazdil66d126e2015-04-03 16:02:44 +01005757};
5758
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005759class HTypeConversion final : public HExpression<1> {
Roland Levillaindff1f282014-11-05 14:15:05 +00005760 public:
5761 // Instantiate a type conversion of `input` to `result_type`.
Vladimir Markoc8fb2112017-10-03 11:37:52 +01005762 HTypeConversion(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305763 : HExpression(kTypeConversion, result_type, SideEffects::None(), dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00005764 SetRawInputAt(0, input);
Roland Levillainf355c3f2016-03-30 19:09:03 +01005765 // Invariant: We should never generate a conversion to a Boolean value.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005766 DCHECK_NE(DataType::Type::kBool, result_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00005767 }
5768
5769 HInstruction* GetInput() const { return InputAt(0); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005770 DataType::Type GetInputType() const { return GetInput()->GetType(); }
5771 DataType::Type GetResultType() const { return GetType(); }
Roland Levillaindff1f282014-11-05 14:15:05 +00005772
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005773 bool IsClonable() const override { return true; }
5774 bool CanBeMoved() const override { return true; }
5775 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01005776 return true;
5777 }
Nicolas Geoffrayacc56ac2018-10-09 08:45:24 +01005778 // Return whether the conversion is implicit. This includes conversion to the same type.
5779 bool IsImplicitConversion() const {
5780 return DataType::IsTypeConversionImplicit(GetInputType(), GetResultType());
5781 }
Roland Levillaindff1f282014-11-05 14:15:05 +00005782
Mark Mendelle82549b2015-05-06 10:55:34 -04005783 // Try to statically evaluate the conversion and return a HConstant
5784 // containing the result. If the input cannot be converted, return nullptr.
5785 HConstant* TryStaticEvaluation() const;
5786
Roland Levillaindff1f282014-11-05 14:15:05 +00005787 DECLARE_INSTRUCTION(TypeConversion);
5788
Artem Serovcced8ba2017-07-19 18:18:09 +01005789 protected:
5790 DEFAULT_COPY_CONSTRUCTOR(TypeConversion);
Roland Levillaindff1f282014-11-05 14:15:05 +00005791};
5792
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00005793static constexpr uint32_t kNoRegNumber = -1;
5794
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005795class HNullCheck final : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005796 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005797 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
Artem Serovd1aa7d02018-06-22 11:35:46 +01005798 // constructor. However it can only do it on a fatal slow path so execution never returns to the
5799 // instruction following the current one; thus 'SideEffects::None()' is used.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005800 HNullCheck(HInstruction* value, uint32_t dex_pc)
Artem Serovd1aa7d02018-06-22 11:35:46 +01005801 : HExpression(kNullCheck, value->GetType(), SideEffects::None(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005802 SetRawInputAt(0, value);
5803 }
5804
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005805 bool IsClonable() const override { return true; }
5806 bool CanBeMoved() const override { return true; }
5807 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005808 return true;
5809 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005810
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005811 bool NeedsEnvironment() const override { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005812
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005813 bool CanThrow() const override { return true; }
Calin Juravle10e244f2015-01-26 18:54:32 +00005814
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005815 bool CanBeNull() const override { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005816
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005817 DECLARE_INSTRUCTION(NullCheck);
5818
Artem Serovcced8ba2017-07-19 18:18:09 +01005819 protected:
5820 DEFAULT_COPY_CONSTRUCTOR(NullCheck);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005821};
5822
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005823// Embeds an ArtField and all the information required by the compiler. We cache
5824// that information to avoid requiring the mutator lock every time we need it.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005825class FieldInfo : public ValueObject {
5826 public:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005827 FieldInfo(ArtField* field,
5828 MemberOffset field_offset,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005829 DataType::Type field_type,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005830 bool is_volatile,
5831 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005832 uint16_t declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005833 const DexFile& dex_file)
5834 : field_(field),
5835 field_offset_(field_offset),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005836 field_type_(field_type),
5837 is_volatile_(is_volatile),
5838 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005839 declaring_class_def_index_(declaring_class_def_index),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005840 dex_file_(dex_file) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005841
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005842 ArtField* GetField() const { return field_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005843 MemberOffset GetFieldOffset() const { return field_offset_; }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005844 DataType::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005845 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07005846 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005847 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00005848 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005849
5850 private:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005851 ArtField* const field_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005852 const MemberOffset field_offset_;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005853 const DataType::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00005854 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005855 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07005856 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005857 const DexFile& dex_file_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005858};
5859
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005860class HInstanceFieldGet final : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005861 public:
5862 HInstanceFieldGet(HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005863 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005864 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005865 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005866 bool is_volatile,
5867 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005868 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005869 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005870 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305871 : HExpression(kInstanceFieldGet,
5872 field_type,
5873 SideEffects::FieldReadOfType(field_type, is_volatile),
5874 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005875 field_info_(field,
5876 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005877 field_type,
5878 is_volatile,
5879 field_idx,
5880 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005881 dex_file) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005882 SetRawInputAt(0, value);
5883 }
5884
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005885 bool IsClonable() const override { return true; }
5886 bool CanBeMoved() const override { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005887
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005888 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01005889 const HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005890 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005891 }
5892
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005893 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005894 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005895 }
5896
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005897 size_t ComputeHashCode() const override {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01005898 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5899 }
5900
Calin Juravle52c48962014-12-16 17:02:57 +00005901 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005902 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005903 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005904 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005905
Vladimir Marko61b92282017-10-11 13:23:17 +01005906 void SetType(DataType::Type new_type) {
5907 DCHECK(DataType::IsIntegralType(GetType()));
5908 DCHECK(DataType::IsIntegralType(new_type));
5909 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
5910 SetPackedField<TypeField>(new_type);
5911 }
5912
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005913 DECLARE_INSTRUCTION(InstanceFieldGet);
5914
Artem Serovcced8ba2017-07-19 18:18:09 +01005915 protected:
5916 DEFAULT_COPY_CONSTRUCTOR(InstanceFieldGet);
5917
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005918 private:
5919 const FieldInfo field_info_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005920};
5921
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005922class HInstanceFieldSet final : public HExpression<2> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005923 public:
5924 HInstanceFieldSet(HInstruction* object,
5925 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005926 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005927 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005928 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005929 bool is_volatile,
5930 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005931 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005932 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005933 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01005934 : HExpression(kInstanceFieldSet,
5935 SideEffects::FieldWriteOfType(field_type, is_volatile),
5936 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005937 field_info_(field,
5938 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005939 field_type,
5940 is_volatile,
5941 field_idx,
5942 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005943 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005944 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005945 SetRawInputAt(0, object);
5946 SetRawInputAt(1, value);
5947 }
5948
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005949 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01005950
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005951 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005952 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005953 }
5954
Calin Juravle52c48962014-12-16 17:02:57 +00005955 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005956 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005957 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005958 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005959 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005960 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5961 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005962
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005963 DECLARE_INSTRUCTION(InstanceFieldSet);
5964
Artem Serovcced8ba2017-07-19 18:18:09 +01005965 protected:
5966 DEFAULT_COPY_CONSTRUCTOR(InstanceFieldSet);
5967
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005968 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005969 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5970 static constexpr size_t kNumberOfInstanceFieldSetPackedBits = kFlagValueCanBeNull + 1;
5971 static_assert(kNumberOfInstanceFieldSetPackedBits <= kMaxNumberOfPackedBits,
5972 "Too many packed fields.");
5973
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005974 const FieldInfo field_info_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005975};
5976
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005977class HArrayGet final : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005978 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005979 HArrayGet(HInstruction* array,
5980 HInstruction* index,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005981 DataType::Type type,
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005982 uint32_t dex_pc)
5983 : HArrayGet(array,
5984 index,
5985 type,
5986 SideEffects::ArrayReadOfType(type),
5987 dex_pc,
Andreas Gampe3db70682018-12-26 15:12:03 -08005988 /* is_string_char_at= */ false) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305989 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005990
5991 HArrayGet(HInstruction* array,
5992 HInstruction* index,
5993 DataType::Type type,
5994 SideEffects side_effects,
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005995 uint32_t dex_pc,
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005996 bool is_string_char_at)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305997 : HExpression(kArrayGet, type, side_effects, dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005998 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005999 SetRawInputAt(0, array);
6000 SetRawInputAt(1, index);
6001 }
6002
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006003 bool IsClonable() const override { return true; }
6004 bool CanBeMoved() const override { return true; }
6005 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07006006 return true;
6007 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006008 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const override {
Calin Juravle77520bc2015-01-12 18:45:46 +00006009 // TODO: We can be smarter here.
Vladimir Marko66d691d2017-04-07 17:53:39 +01006010 // Currently, unless the array is the result of NewArray, the array access is always
6011 // preceded by some form of null NullCheck necessary for the bounds check, usually
6012 // implicit null check on the ArrayLength input to BoundsCheck or Deoptimize for
6013 // dynamic BCE. There are cases when these could be removed to produce better code.
6014 // If we ever add optimizations to do so we should allow an implicit check here
6015 // (as long as the address falls in the first page).
6016 //
6017 // As an example of such fancy optimization, we could eliminate BoundsCheck for
6018 // a = cond ? new int[1] : null;
6019 // a[0]; // The Phi does not need bounds check for either input.
Calin Juravle77520bc2015-01-12 18:45:46 +00006020 return false;
6021 }
6022
David Brazdil4833f5a2015-12-16 10:37:39 +00006023 bool IsEquivalentOf(HArrayGet* other) const {
6024 bool result = (GetDexPc() == other->GetDexPc());
6025 if (kIsDebugBuild && result) {
6026 DCHECK_EQ(GetBlock(), other->GetBlock());
6027 DCHECK_EQ(GetArray(), other->GetArray());
6028 DCHECK_EQ(GetIndex(), other->GetIndex());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006029 if (DataType::IsIntOrLongType(GetType())) {
6030 DCHECK(DataType::IsFloatingPointType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00006031 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006032 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
6033 DCHECK(DataType::IsIntOrLongType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00006034 }
6035 }
6036 return result;
6037 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01006038
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006039 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
6040
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006041 HInstruction* GetArray() const { return InputAt(0); }
6042 HInstruction* GetIndex() const { return InputAt(1); }
6043
Vladimir Marko61b92282017-10-11 13:23:17 +01006044 void SetType(DataType::Type new_type) {
6045 DCHECK(DataType::IsIntegralType(GetType()));
6046 DCHECK(DataType::IsIntegralType(new_type));
6047 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
6048 SetPackedField<TypeField>(new_type);
6049 }
6050
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006051 DECLARE_INSTRUCTION(ArrayGet);
6052
Artem Serovcced8ba2017-07-19 18:18:09 +01006053 protected:
6054 DEFAULT_COPY_CONSTRUCTOR(ArrayGet);
6055
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006056 private:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006057 // We treat a String as an array, creating the HArrayGet from String.charAt()
6058 // intrinsic in the instruction simplifier. We can always determine whether
6059 // a particular HArrayGet is actually a String.charAt() by looking at the type
6060 // of the input but that requires holding the mutator lock, so we prefer to use
6061 // a flag, so that code generators don't need to do the locking.
Vladimir Markobd785672018-05-03 17:09:09 +01006062 static constexpr size_t kFlagIsStringCharAt = kNumberOfGenericPackedBits;
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006063 static constexpr size_t kNumberOfArrayGetPackedBits = kFlagIsStringCharAt + 1;
6064 static_assert(kNumberOfArrayGetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6065 "Too many packed fields.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006066};
6067
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006068class HArraySet final : public HExpression<3> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006069 public:
6070 HArraySet(HInstruction* array,
6071 HInstruction* index,
6072 HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006073 DataType::Type expected_component_type,
Aart Bik18b36ab2016-04-13 16:41:35 -07006074 uint32_t dex_pc)
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006075 : HArraySet(array,
6076 index,
6077 value,
6078 expected_component_type,
6079 // Make a best guess for side effects now, may be refined during SSA building.
6080 ComputeSideEffects(GetComponentType(value->GetType(), expected_component_type)),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306081 dex_pc) {
6082 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006083
6084 HArraySet(HInstruction* array,
6085 HInstruction* index,
6086 HInstruction* value,
6087 DataType::Type expected_component_type,
6088 SideEffects side_effects,
6089 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006090 : HExpression(kArraySet, side_effects, dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006091 SetPackedField<ExpectedComponentTypeField>(expected_component_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006092 SetPackedFlag<kFlagNeedsTypeCheck>(value->GetType() == DataType::Type::kReference);
Vladimir Markoa1de9182016-02-25 11:37:38 +00006093 SetPackedFlag<kFlagValueCanBeNull>(true);
6094 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(false);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006095 SetRawInputAt(0, array);
6096 SetRawInputAt(1, index);
6097 SetRawInputAt(2, value);
6098 }
6099
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006100 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01006101
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006102 bool NeedsEnvironment() const override {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006103 // We call a runtime method to throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006104 return NeedsTypeCheck();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006105 }
6106
Mingyao Yang81014cb2015-06-02 03:16:27 -07006107 // Can throw ArrayStoreException.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006108 bool CanThrow() const override { return NeedsTypeCheck(); }
Mingyao Yang81014cb2015-06-02 03:16:27 -07006109
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006110 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const override {
Calin Juravle77520bc2015-01-12 18:45:46 +00006111 // TODO: Same as for ArrayGet.
6112 return false;
6113 }
6114
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006115 void ClearNeedsTypeCheck() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006116 SetPackedFlag<kFlagNeedsTypeCheck>(false);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006117 }
6118
Nicolas Geoffray07276db2015-05-18 14:22:09 +01006119 void ClearValueCanBeNull() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006120 SetPackedFlag<kFlagValueCanBeNull>(false);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01006121 }
6122
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006123 void SetStaticTypeOfArrayIsObjectArray() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006124 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(true);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006125 }
6126
Vladimir Markoa1de9182016-02-25 11:37:38 +00006127 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
6128 bool NeedsTypeCheck() const { return GetPackedFlag<kFlagNeedsTypeCheck>(); }
6129 bool StaticTypeOfArrayIsObjectArray() const {
6130 return GetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>();
6131 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006132
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006133 HInstruction* GetArray() const { return InputAt(0); }
6134 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006135 HInstruction* GetValue() const { return InputAt(2); }
6136
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006137 DataType::Type GetComponentType() const {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006138 return GetComponentType(GetValue()->GetType(), GetRawExpectedComponentType());
6139 }
6140
6141 static DataType::Type GetComponentType(DataType::Type value_type,
6142 DataType::Type expected_component_type) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006143 // The Dex format does not type floating point index operations. Since the
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006144 // `expected_component_type` comes from SSA building and can therefore not
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006145 // be correct, we also check what is the value type. If it is a floating
6146 // point type, we must use that type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006147 return ((value_type == DataType::Type::kFloat32) || (value_type == DataType::Type::kFloat64))
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006148 ? value_type
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006149 : expected_component_type;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006150 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01006151
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006152 DataType::Type GetRawExpectedComponentType() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006153 return GetPackedField<ExpectedComponentTypeField>();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01006154 }
6155
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006156 static SideEffects ComputeSideEffects(DataType::Type type) {
6157 return SideEffects::ArrayWriteOfType(type).Union(SideEffectsForArchRuntimeCalls(type));
Aart Bik18b36ab2016-04-13 16:41:35 -07006158 }
6159
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006160 static SideEffects SideEffectsForArchRuntimeCalls(DataType::Type value_type) {
6161 return (value_type == DataType::Type::kReference) ? SideEffects::CanTriggerGC()
6162 : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006163 }
6164
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006165 DECLARE_INSTRUCTION(ArraySet);
6166
Artem Serovcced8ba2017-07-19 18:18:09 +01006167 protected:
6168 DEFAULT_COPY_CONSTRUCTOR(ArraySet);
6169
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006170 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006171 static constexpr size_t kFieldExpectedComponentType = kNumberOfGenericPackedBits;
6172 static constexpr size_t kFieldExpectedComponentTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006173 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00006174 static constexpr size_t kFlagNeedsTypeCheck =
6175 kFieldExpectedComponentType + kFieldExpectedComponentTypeSize;
6176 static constexpr size_t kFlagValueCanBeNull = kFlagNeedsTypeCheck + 1;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006177 // Cached information for the reference_type_info_ so that codegen
6178 // does not need to inspect the static type.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006179 static constexpr size_t kFlagStaticTypeOfArrayIsObjectArray = kFlagValueCanBeNull + 1;
6180 static constexpr size_t kNumberOfArraySetPackedBits =
6181 kFlagStaticTypeOfArrayIsObjectArray + 1;
6182 static_assert(kNumberOfArraySetPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6183 using ExpectedComponentTypeField =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006184 BitField<DataType::Type, kFieldExpectedComponentType, kFieldExpectedComponentTypeSize>;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006185};
6186
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006187class HArrayLength final : public HExpression<1> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006188 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006189 HArrayLength(HInstruction* array, uint32_t dex_pc, bool is_string_length = false)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306190 : HExpression(kArrayLength, DataType::Type::kInt32, SideEffects::None(), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006191 SetPackedFlag<kFlagIsStringLength>(is_string_length);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01006192 // Note that arrays do not change length, so the instruction does not
6193 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006194 SetRawInputAt(0, array);
6195 }
6196
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006197 bool IsClonable() const override { return true; }
6198 bool CanBeMoved() const override { return true; }
6199 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07006200 return true;
6201 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006202 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Calin Juravle641547a2015-04-21 22:08:51 +01006203 return obj == InputAt(0);
6204 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01006205
Vladimir Markodce016e2016-04-28 13:10:02 +01006206 bool IsStringLength() const { return GetPackedFlag<kFlagIsStringLength>(); }
6207
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006208 DECLARE_INSTRUCTION(ArrayLength);
6209
Artem Serovcced8ba2017-07-19 18:18:09 +01006210 protected:
6211 DEFAULT_COPY_CONSTRUCTOR(ArrayLength);
6212
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006213 private:
Vladimir Markodce016e2016-04-28 13:10:02 +01006214 // We treat a String as an array, creating the HArrayLength from String.length()
6215 // or String.isEmpty() intrinsic in the instruction simplifier. We can always
6216 // determine whether a particular HArrayLength is actually a String.length() by
6217 // looking at the type of the input but that requires holding the mutator lock, so
6218 // we prefer to use a flag, so that code generators don't need to do the locking.
Vladimir Markobd785672018-05-03 17:09:09 +01006219 static constexpr size_t kFlagIsStringLength = kNumberOfGenericPackedBits;
Vladimir Markodce016e2016-04-28 13:10:02 +01006220 static constexpr size_t kNumberOfArrayLengthPackedBits = kFlagIsStringLength + 1;
6221 static_assert(kNumberOfArrayLengthPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6222 "Too many packed fields.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006223};
6224
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006225class HBoundsCheck final : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006226 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00006227 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
Artem Serovd1aa7d02018-06-22 11:35:46 +01006228 // constructor. However it can only do it on a fatal slow path so execution never returns to the
6229 // instruction following the current one; thus 'SideEffects::None()' is used.
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006230 HBoundsCheck(HInstruction* index,
6231 HInstruction* length,
6232 uint32_t dex_pc,
Vladimir Marko0259c242017-12-04 11:27:47 +00006233 bool is_string_char_at = false)
Artem Serovd1aa7d02018-06-22 11:35:46 +01006234 : HExpression(kBoundsCheck, index->GetType(), SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006235 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(index->GetType()));
Vladimir Marko0259c242017-12-04 11:27:47 +00006236 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006237 SetRawInputAt(0, index);
6238 SetRawInputAt(1, length);
6239 }
6240
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006241 bool IsClonable() const override { return true; }
6242 bool CanBeMoved() const override { return true; }
6243 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07006244 return true;
6245 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01006246
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006247 bool NeedsEnvironment() const override { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006248
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006249 bool CanThrow() const override { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01006250
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00006251 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006252
Alexandre Ramese6dbf482015-10-19 10:10:41 +01006253 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006254
6255 DECLARE_INSTRUCTION(BoundsCheck);
6256
Artem Serovcced8ba2017-07-19 18:18:09 +01006257 protected:
6258 DEFAULT_COPY_CONSTRUCTOR(BoundsCheck);
6259
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006260 private:
Vladimir Markobd785672018-05-03 17:09:09 +01006261 static constexpr size_t kFlagIsStringCharAt = kNumberOfGenericPackedBits;
Vladimir Markof02046e2018-10-02 15:31:32 +01006262 static constexpr size_t kNumberOfBoundsCheckPackedBits = kFlagIsStringCharAt + 1;
6263 static_assert(kNumberOfBoundsCheckPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6264 "Too many packed fields.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006265};
6266
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006267class HSuspendCheck final : public HExpression<0> {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006268 public:
David Brazdil86ea7ee2016-02-16 09:26:07 +00006269 explicit HSuspendCheck(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01006270 : HExpression(kSuspendCheck, SideEffects::CanTriggerGC(), dex_pc),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306271 slow_path_(nullptr) {
6272 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006273
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006274 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01006275
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006276 bool NeedsEnvironment() const override {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006277 return true;
6278 }
6279
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006280 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
6281 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006282
6283 DECLARE_INSTRUCTION(SuspendCheck);
6284
Artem Serovcced8ba2017-07-19 18:18:09 +01006285 protected:
6286 DEFAULT_COPY_CONSTRUCTOR(SuspendCheck);
6287
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006288 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006289 // Only used for code generation, in order to share the same slow path between back edges
6290 // of a same loop.
6291 SlowPathCode* slow_path_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006292};
6293
David Srbecky0cf44932015-12-09 14:09:59 +00006294// Pseudo-instruction which provides the native debugger with mapping information.
6295// It ensures that we can generate line number and local variables at this point.
Vladimir Markobd785672018-05-03 17:09:09 +01006296class HNativeDebugInfo : public HExpression<0> {
David Srbecky0cf44932015-12-09 14:09:59 +00006297 public:
6298 explicit HNativeDebugInfo(uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006299 : HExpression<0>(kNativeDebugInfo, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306300 }
David Srbecky0cf44932015-12-09 14:09:59 +00006301
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006302 bool NeedsEnvironment() const override {
David Srbecky0cf44932015-12-09 14:09:59 +00006303 return true;
6304 }
6305
6306 DECLARE_INSTRUCTION(NativeDebugInfo);
6307
Artem Serovcced8ba2017-07-19 18:18:09 +01006308 protected:
6309 DEFAULT_COPY_CONSTRUCTOR(NativeDebugInfo);
David Srbecky0cf44932015-12-09 14:09:59 +00006310};
6311
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006312/**
6313 * Instruction to load a Class object.
6314 */
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006315class HLoadClass final : public HInstruction {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006316 public:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006317 // Determines how to load the Class.
6318 enum class LoadKind {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006319 // We cannot load this class. See HSharpening::SharpenLoadClass.
6320 kInvalid = -1,
6321
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006322 // Use the Class* from the method's own ArtMethod*.
6323 kReferrersClass,
6324
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006325 // Use PC-relative boot image Class* address that will be known at link time.
Vladimir Marko764d4542017-05-16 10:31:41 +01006326 // Used for boot image classes referenced by boot image code.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006327 kBootImageLinkTimePcRelative,
6328
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006329 // Load from an entry in the .data.bimg.rel.ro using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006330 // Used for boot image classes referenced by apps in AOT-compiled code.
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006331 kBootImageRelRo,
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006332
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006333 // Load from an entry in the .bss section using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006334 // Used for classes outside boot image referenced by AOT-compiled app and boot image code.
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006335 kBssEntry,
6336
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006337 // Use a known boot image Class* address, embedded in the code by the codegen.
6338 // Used for boot image classes referenced by apps in JIT-compiled code.
6339 kJitBootImageAddress,
6340
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006341 // Load from the root table associated with the JIT compiled method.
6342 kJitTableAddress,
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006343
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006344 // Load using a simple runtime call. This is the fall-back load kind when
6345 // the codegen is unable to use another appropriate kind.
6346 kRuntimeCall,
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006347
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006348 kLast = kRuntimeCall
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006349 };
6350
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01006351 HLoadClass(HCurrentMethod* current_method,
Andreas Gampea5b09a62016-11-17 15:21:22 -08006352 dex::TypeIndex type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01006353 const DexFile& dex_file,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006354 Handle<mirror::Class> klass,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006355 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01006356 uint32_t dex_pc,
Nicolas Geoffray56876342016-12-16 16:09:08 +00006357 bool needs_access_check)
Vladimir Markobd785672018-05-03 17:09:09 +01006358 : HInstruction(kLoadClass,
6359 DataType::Type::kReference,
6360 SideEffectsForArchRuntimeCalls(),
6361 dex_pc),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006362 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006363 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01006364 dex_file_(dex_file),
Vladimir Marko175e7862018-03-27 09:03:13 +00006365 klass_(klass) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01006366 // Referrers class should not need access check. We never inline unverified
6367 // methods so we can't possibly end up in this situation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006368 DCHECK(!is_referrers_class || !needs_access_check);
6369
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006370 SetPackedField<LoadKindField>(
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006371 is_referrers_class ? LoadKind::kReferrersClass : LoadKind::kRuntimeCall);
Vladimir Markoa1de9182016-02-25 11:37:38 +00006372 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
Nicolas Geoffray56876342016-12-16 16:09:08 +00006373 SetPackedFlag<kFlagIsInBootImage>(false);
Vladimir Markoa1de9182016-02-25 11:37:38 +00006374 SetPackedFlag<kFlagGenerateClInitCheck>(false);
Vladimir Marko175e7862018-03-27 09:03:13 +00006375 SetPackedFlag<kFlagValidLoadedClassRTI>(false);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006376 }
6377
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006378 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01006379
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006380 void SetLoadKind(LoadKind load_kind);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006381
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006382 LoadKind GetLoadKind() const {
6383 return GetPackedField<LoadKindField>();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01006384 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006385
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006386 bool HasPcRelativeLoadKind() const {
6387 return GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
6388 GetLoadKind() == LoadKind::kBootImageRelRo ||
6389 GetLoadKind() == LoadKind::kBssEntry;
6390 }
6391
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006392 bool CanBeMoved() const override { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006393
Yi Kong39402542019-03-24 02:47:16 -07006394 bool InstructionDataEquals(const HInstruction* other) const override;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006395
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006396 size_t ComputeHashCode() const override { return type_index_.index_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006397
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006398 bool CanBeNull() const override { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006399
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006400 bool NeedsEnvironment() const override {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006401 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006402 }
6403
Calin Juravle0ba218d2015-05-19 18:46:01 +01006404 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00006405 // The entrypoint the code generator is going to call does not do
6406 // clinit of the class.
6407 DCHECK(!NeedsAccessCheck());
Vladimir Markoa1de9182016-02-25 11:37:38 +00006408 SetPackedFlag<kFlagGenerateClInitCheck>(generate_clinit_check);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006409 }
6410
6411 bool CanCallRuntime() const {
Vladimir Marko41559982017-01-06 14:04:23 +00006412 return NeedsAccessCheck() ||
6413 MustGenerateClinitCheck() ||
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006414 GetLoadKind() == LoadKind::kRuntimeCall ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006415 GetLoadKind() == LoadKind::kBssEntry;
Calin Juravle98893e12015-10-02 21:05:03 +01006416 }
6417
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006418 bool CanThrow() const override {
Vladimir Marko41559982017-01-06 14:04:23 +00006419 return NeedsAccessCheck() ||
6420 MustGenerateClinitCheck() ||
6421 // If the class is in the boot image, the lookup in the runtime call cannot throw.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006422 ((GetLoadKind() == LoadKind::kRuntimeCall ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006423 GetLoadKind() == LoadKind::kBssEntry) &&
6424 !IsInBootImage());
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006425 }
6426
Calin Juravleacf735c2015-02-12 15:25:22 +00006427 ReferenceTypeInfo GetLoadedClassRTI() {
Vladimir Marko175e7862018-03-27 09:03:13 +00006428 if (GetPackedFlag<kFlagValidLoadedClassRTI>()) {
6429 // Note: The is_exact flag from the return value should not be used.
Andreas Gampe3db70682018-12-26 15:12:03 -08006430 return ReferenceTypeInfo::CreateUnchecked(klass_, /* is_exact= */ true);
Vladimir Marko175e7862018-03-27 09:03:13 +00006431 } else {
6432 return ReferenceTypeInfo::CreateInvalid();
6433 }
Calin Juravleacf735c2015-02-12 15:25:22 +00006434 }
6435
Vladimir Marko175e7862018-03-27 09:03:13 +00006436 // Loaded class RTI is marked as valid by RTP if the klass_ is admissible.
6437 void SetValidLoadedClassRTI() REQUIRES_SHARED(Locks::mutator_lock_) {
6438 DCHECK(klass_ != nullptr);
6439 SetPackedFlag<kFlagValidLoadedClassRTI>(true);
Calin Juravleacf735c2015-02-12 15:25:22 +00006440 }
6441
Andreas Gampea5b09a62016-11-17 15:21:22 -08006442 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006443 const DexFile& GetDexFile() const { return dex_file_; }
6444
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006445 bool NeedsDexCacheOfDeclaringClass() const override {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006446 return GetLoadKind() == LoadKind::kRuntimeCall;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006447 }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00006448
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006449 static SideEffects SideEffectsForArchRuntimeCalls() {
6450 return SideEffects::CanTriggerGC();
6451 }
6452
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006453 bool IsReferrersClass() const { return GetLoadKind() == LoadKind::kReferrersClass; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006454 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006455 bool IsInBootImage() const { return GetPackedFlag<kFlagIsInBootImage>(); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006456 bool MustGenerateClinitCheck() const { return GetPackedFlag<kFlagGenerateClInitCheck>(); }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006457
Vladimir Markoa9f303c2018-07-20 16:43:56 +01006458 bool MustResolveTypeOnSlowPath() const {
6459 // Check that this instruction has a slow path.
6460 DCHECK(GetLoadKind() != LoadKind::kRuntimeCall); // kRuntimeCall calls on main path.
6461 DCHECK(GetLoadKind() == LoadKind::kBssEntry || MustGenerateClinitCheck());
6462 return GetLoadKind() == LoadKind::kBssEntry;
6463 }
6464
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006465 void MarkInBootImage() {
6466 SetPackedFlag<kFlagIsInBootImage>(true);
6467 }
6468
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006469 void AddSpecialInput(HInstruction* special_input);
6470
6471 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01006472 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006473 return ArrayRef<HUserRecord<HInstruction*>>(
6474 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
6475 }
6476
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006477 Handle<mirror::Class> GetClass() const {
6478 return klass_;
6479 }
6480
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006481 DECLARE_INSTRUCTION(LoadClass);
6482
Artem Serovcced8ba2017-07-19 18:18:09 +01006483 protected:
6484 DEFAULT_COPY_CONSTRUCTOR(LoadClass);
6485
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006486 private:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006487 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfGenericPackedBits;
Vladimir Marko41559982017-01-06 14:04:23 +00006488 static constexpr size_t kFlagIsInBootImage = kFlagNeedsAccessCheck + 1;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006489 // Whether this instruction must generate the initialization check.
6490 // Used for code generation.
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006491 static constexpr size_t kFlagGenerateClInitCheck = kFlagIsInBootImage + 1;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006492 static constexpr size_t kFieldLoadKind = kFlagGenerateClInitCheck + 1;
6493 static constexpr size_t kFieldLoadKindSize =
6494 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
Vladimir Marko175e7862018-03-27 09:03:13 +00006495 static constexpr size_t kFlagValidLoadedClassRTI = kFieldLoadKind + kFieldLoadKindSize;
6496 static constexpr size_t kNumberOfLoadClassPackedBits = kFlagValidLoadedClassRTI + 1;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006497 static_assert(kNumberOfLoadClassPackedBits < kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006498 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
6499
6500 static bool HasTypeReference(LoadKind load_kind) {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006501 return load_kind == LoadKind::kReferrersClass ||
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006502 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006503 load_kind == LoadKind::kBssEntry ||
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006504 load_kind == LoadKind::kRuntimeCall;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006505 }
6506
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006507 void SetLoadKindInternal(LoadKind load_kind);
6508
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006509 // The special input is the HCurrentMethod for kRuntimeCall or kReferrersClass.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006510 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006511 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006512 HUserRecord<HInstruction*> special_input_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006513
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006514 // A type index and dex file where the class can be accessed. The dex file can be:
6515 // - The compiling method's dex file if the class is defined there too.
6516 // - The compiling method's dex file if the class is referenced there.
6517 // - The dex file where the class is defined. When the load kind can only be
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006518 // kBssEntry or kRuntimeCall, we cannot emit code for this `HLoadClass`.
Andreas Gampea5b09a62016-11-17 15:21:22 -08006519 const dex::TypeIndex type_index_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006520 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006521
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006522 Handle<mirror::Class> klass_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006523};
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006524std::ostream& operator<<(std::ostream& os, HLoadClass::LoadKind rhs);
6525
6526// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
Vladimir Marko28e012a2017-12-07 11:22:59 +00006527inline void HLoadClass::SetLoadKind(LoadKind load_kind) {
6528 // The load kind should be determined before inserting the instruction to the graph.
6529 DCHECK(GetBlock() == nullptr);
6530 DCHECK(GetEnvironment() == nullptr);
6531 SetPackedField<LoadKindField>(load_kind);
6532 if (load_kind != LoadKind::kRuntimeCall && load_kind != LoadKind::kReferrersClass) {
6533 special_input_ = HUserRecord<HInstruction*>(nullptr);
6534 }
6535 if (!NeedsEnvironment()) {
6536 SetSideEffects(SideEffects::None());
6537 }
6538}
6539
6540// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006541inline void HLoadClass::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07006542 // The special input is used for PC-relative loads on some architectures,
6543 // including literal pool loads, which are PC-relative too.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006544 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006545 GetLoadKind() == LoadKind::kBootImageRelRo ||
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006546 GetLoadKind() == LoadKind::kBssEntry ||
6547 GetLoadKind() == LoadKind::kJitBootImageAddress) << GetLoadKind();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006548 DCHECK(special_input_.GetInstruction() == nullptr);
6549 special_input_ = HUserRecord<HInstruction*>(special_input);
6550 special_input->AddUseAt(this, 0);
6551}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006552
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006553class HLoadString final : public HInstruction {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006554 public:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006555 // Determines how to load the String.
6556 enum class LoadKind {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006557 // Use PC-relative boot image String* address that will be known at link time.
Vladimir Marko764d4542017-05-16 10:31:41 +01006558 // Used for boot image strings referenced by boot image code.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006559 kBootImageLinkTimePcRelative,
6560
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006561 // Load from an entry in the .data.bimg.rel.ro using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006562 // Used for boot image strings referenced by apps in AOT-compiled code.
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006563 kBootImageRelRo,
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006564
Vladimir Markoaad75c62016-10-03 08:46:48 +00006565 // Load from an entry in the .bss section using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006566 // Used for strings outside boot image referenced by AOT-compiled app and boot image code.
Vladimir Markoaad75c62016-10-03 08:46:48 +00006567 kBssEntry,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006568
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006569 // Use a known boot image String* address, embedded in the code by the codegen.
6570 // Used for boot image strings referenced by apps in JIT-compiled code.
6571 kJitBootImageAddress,
6572
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006573 // Load from the root table associated with the JIT compiled method.
6574 kJitTableAddress,
6575
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006576 // Load using a simple runtime call. This is the fall-back load kind when
6577 // the codegen is unable to use another appropriate kind.
6578 kRuntimeCall,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006579
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006580 kLast = kRuntimeCall,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006581 };
6582
Nicolas Geoffray917d0162015-11-24 18:25:35 +00006583 HLoadString(HCurrentMethod* current_method,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006584 dex::StringIndex string_index,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006585 const DexFile& dex_file,
6586 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006587 : HInstruction(kLoadString,
6588 DataType::Type::kReference,
6589 SideEffectsForArchRuntimeCalls(),
6590 dex_pc),
Vladimir Marko372f10e2016-05-17 16:30:10 +01006591 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006592 string_index_(string_index),
6593 dex_file_(dex_file) {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006594 SetPackedField<LoadKindField>(LoadKind::kRuntimeCall);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01006595 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006596
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006597 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01006598
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006599 void SetLoadKind(LoadKind load_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006600
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006601 LoadKind GetLoadKind() const {
6602 return GetPackedField<LoadKindField>();
6603 }
6604
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006605 bool HasPcRelativeLoadKind() const {
6606 return GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
6607 GetLoadKind() == LoadKind::kBootImageRelRo ||
6608 GetLoadKind() == LoadKind::kBssEntry;
6609 }
6610
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006611 const DexFile& GetDexFile() const {
6612 return dex_file_;
6613 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006614
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006615 dex::StringIndex GetStringIndex() const {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006616 return string_index_;
6617 }
6618
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006619 Handle<mirror::String> GetString() const {
6620 return string_;
6621 }
6622
6623 void SetString(Handle<mirror::String> str) {
6624 string_ = str;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006625 }
6626
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006627 bool CanBeMoved() const override { return true; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006628
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006629 bool InstructionDataEquals(const HInstruction* other) const override;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006630
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006631 size_t ComputeHashCode() const override { return string_index_.index_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006632
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006633 // Will call the runtime if we need to load the string through
6634 // the dex cache and the string is not guaranteed to be there yet.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006635 bool NeedsEnvironment() const override {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006636 LoadKind load_kind = GetLoadKind();
Vladimir Marko764d4542017-05-16 10:31:41 +01006637 if (load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006638 load_kind == LoadKind::kBootImageRelRo ||
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006639 load_kind == LoadKind::kJitBootImageAddress ||
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006640 load_kind == LoadKind::kJitTableAddress) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006641 return false;
6642 }
Vladimir Marko4d1be4922017-01-06 14:43:11 +00006643 return true;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006644 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006645
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006646 bool NeedsDexCacheOfDeclaringClass() const override {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006647 return GetLoadKind() == LoadKind::kRuntimeCall;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006648 }
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006649
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006650 bool CanBeNull() const override { return false; }
6651 bool CanThrow() const override { return NeedsEnvironment(); }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006652
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006653 static SideEffects SideEffectsForArchRuntimeCalls() {
6654 return SideEffects::CanTriggerGC();
6655 }
6656
Vladimir Marko372f10e2016-05-17 16:30:10 +01006657 void AddSpecialInput(HInstruction* special_input);
6658
6659 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01006660 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Vladimir Marko372f10e2016-05-17 16:30:10 +01006661 return ArrayRef<HUserRecord<HInstruction*>>(
6662 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006663 }
6664
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006665 DECLARE_INSTRUCTION(LoadString);
6666
Artem Serovcced8ba2017-07-19 18:18:09 +01006667 protected:
6668 DEFAULT_COPY_CONSTRUCTOR(LoadString);
6669
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006670 private:
Vladimir Marko4d1be4922017-01-06 14:43:11 +00006671 static constexpr size_t kFieldLoadKind = kNumberOfGenericPackedBits;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006672 static constexpr size_t kFieldLoadKindSize =
6673 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
6674 static constexpr size_t kNumberOfLoadStringPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006675 static_assert(kNumberOfLoadStringPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006676 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006677
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006678 void SetLoadKindInternal(LoadKind load_kind);
6679
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006680 // The special input is the HCurrentMethod for kRuntimeCall.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006681 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko48886c22017-01-06 11:45:47 +00006682 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Marko372f10e2016-05-17 16:30:10 +01006683 HUserRecord<HInstruction*> special_input_;
6684
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006685 dex::StringIndex string_index_;
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006686 const DexFile& dex_file_;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006687
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006688 Handle<mirror::String> string_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006689};
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006690std::ostream& operator<<(std::ostream& os, HLoadString::LoadKind rhs);
6691
6692// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
Vladimir Marko28e012a2017-12-07 11:22:59 +00006693inline void HLoadString::SetLoadKind(LoadKind load_kind) {
6694 // The load kind should be determined before inserting the instruction to the graph.
6695 DCHECK(GetBlock() == nullptr);
6696 DCHECK(GetEnvironment() == nullptr);
6697 DCHECK_EQ(GetLoadKind(), LoadKind::kRuntimeCall);
6698 SetPackedField<LoadKindField>(load_kind);
6699 if (load_kind != LoadKind::kRuntimeCall) {
6700 special_input_ = HUserRecord<HInstruction*>(nullptr);
6701 }
6702 if (!NeedsEnvironment()) {
6703 SetSideEffects(SideEffects::None());
6704 }
6705}
6706
6707// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006708inline void HLoadString::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07006709 // The special input is used for PC-relative loads on some architectures,
6710 // including literal pool loads, which are PC-relative too.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006711 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006712 GetLoadKind() == LoadKind::kBootImageRelRo ||
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006713 GetLoadKind() == LoadKind::kBssEntry ||
6714 GetLoadKind() == LoadKind::kJitBootImageAddress) << GetLoadKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01006715 // HLoadString::GetInputRecords() returns an empty array at this point,
6716 // so use the GetInputRecords() from the base class to set the input record.
6717 DCHECK(special_input_.GetInstruction() == nullptr);
6718 special_input_ = HUserRecord<HInstruction*>(special_input);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006719 special_input->AddUseAt(this, 0);
6720}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006721
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006722class HLoadMethodHandle final : public HInstruction {
Orion Hodsondbaa5c72018-05-10 08:22:46 +01006723 public:
6724 HLoadMethodHandle(HCurrentMethod* current_method,
Orion Hodson4c8e12e2018-05-18 08:33:20 +01006725 uint16_t method_handle_idx,
6726 const DexFile& dex_file,
6727 uint32_t dex_pc)
Orion Hodsondbaa5c72018-05-10 08:22:46 +01006728 : HInstruction(kLoadMethodHandle,
6729 DataType::Type::kReference,
6730 SideEffectsForArchRuntimeCalls(),
6731 dex_pc),
6732 special_input_(HUserRecord<HInstruction*>(current_method)),
6733 method_handle_idx_(method_handle_idx),
6734 dex_file_(dex_file) {
6735 }
6736
6737 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01006738 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Orion Hodsondbaa5c72018-05-10 08:22:46 +01006739 return ArrayRef<HUserRecord<HInstruction*>>(
6740 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
6741 }
6742
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006743 bool IsClonable() const override { return true; }
Orion Hodsondbaa5c72018-05-10 08:22:46 +01006744
6745 uint16_t GetMethodHandleIndex() const { return method_handle_idx_; }
6746
6747 const DexFile& GetDexFile() const { return dex_file_; }
6748
6749 static SideEffects SideEffectsForArchRuntimeCalls() {
6750 return SideEffects::CanTriggerGC();
6751 }
6752
6753 DECLARE_INSTRUCTION(LoadMethodHandle);
6754
6755 protected:
6756 DEFAULT_COPY_CONSTRUCTOR(LoadMethodHandle);
6757
6758 private:
6759 // The special input is the HCurrentMethod for kRuntimeCall.
6760 HUserRecord<HInstruction*> special_input_;
6761
6762 const uint16_t method_handle_idx_;
6763 const DexFile& dex_file_;
6764};
6765
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006766class HLoadMethodType final : public HInstruction {
Orion Hodson18259d72018-04-12 11:18:23 +01006767 public:
6768 HLoadMethodType(HCurrentMethod* current_method,
Orion Hodson06d10a72018-05-14 08:53:38 +01006769 dex::ProtoIndex proto_index,
Orion Hodson18259d72018-04-12 11:18:23 +01006770 const DexFile& dex_file,
6771 uint32_t dex_pc)
6772 : HInstruction(kLoadMethodType,
6773 DataType::Type::kReference,
6774 SideEffectsForArchRuntimeCalls(),
6775 dex_pc),
6776 special_input_(HUserRecord<HInstruction*>(current_method)),
Orion Hodson06d10a72018-05-14 08:53:38 +01006777 proto_index_(proto_index),
Orion Hodson18259d72018-04-12 11:18:23 +01006778 dex_file_(dex_file) {
6779 }
6780
6781 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01006782 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Orion Hodson18259d72018-04-12 11:18:23 +01006783 return ArrayRef<HUserRecord<HInstruction*>>(
6784 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
6785 }
6786
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006787 bool IsClonable() const override { return true; }
Orion Hodson18259d72018-04-12 11:18:23 +01006788
Orion Hodson06d10a72018-05-14 08:53:38 +01006789 dex::ProtoIndex GetProtoIndex() const { return proto_index_; }
Orion Hodson18259d72018-04-12 11:18:23 +01006790
6791 const DexFile& GetDexFile() const { return dex_file_; }
6792
6793 static SideEffects SideEffectsForArchRuntimeCalls() {
6794 return SideEffects::CanTriggerGC();
6795 }
6796
6797 DECLARE_INSTRUCTION(LoadMethodType);
6798
6799 protected:
6800 DEFAULT_COPY_CONSTRUCTOR(LoadMethodType);
6801
6802 private:
6803 // The special input is the HCurrentMethod for kRuntimeCall.
6804 HUserRecord<HInstruction*> special_input_;
6805
Orion Hodson06d10a72018-05-14 08:53:38 +01006806 const dex::ProtoIndex proto_index_;
Orion Hodson18259d72018-04-12 11:18:23 +01006807 const DexFile& dex_file_;
6808};
6809
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006810/**
6811 * Performs an initialization check on its Class object input.
6812 */
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006813class HClinitCheck final : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006814 public:
Roland Levillain3887c462015-08-12 18:15:42 +01006815 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07006816 : HExpression(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306817 kClinitCheck,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006818 DataType::Type::kReference,
Mingyao Yang217eb062017-12-11 15:20:07 -08006819 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006820 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006821 SetRawInputAt(0, constant);
6822 }
Artem Serova6e26142018-06-19 14:55:17 +01006823 // TODO: Make ClinitCheck clonable.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006824 bool CanBeMoved() const override { return true; }
6825 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006826 return true;
6827 }
6828
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006829 bool NeedsEnvironment() const override {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006830 // May call runtime to initialize the class.
6831 return true;
6832 }
6833
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006834 bool CanThrow() const override { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006835
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00006836 HLoadClass* GetLoadClass() const {
6837 DCHECK(InputAt(0)->IsLoadClass());
6838 return InputAt(0)->AsLoadClass();
6839 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006840
6841 DECLARE_INSTRUCTION(ClinitCheck);
6842
Artem Serovcced8ba2017-07-19 18:18:09 +01006843
6844 protected:
6845 DEFAULT_COPY_CONSTRUCTOR(ClinitCheck);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006846};
6847
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006848class HStaticFieldGet final : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006849 public:
6850 HStaticFieldGet(HInstruction* cls,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006851 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006852 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006853 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006854 bool is_volatile,
6855 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006856 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006857 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01006858 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306859 : HExpression(kStaticFieldGet,
6860 field_type,
6861 SideEffects::FieldReadOfType(field_type, is_volatile),
6862 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006863 field_info_(field,
6864 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006865 field_type,
6866 is_volatile,
6867 field_idx,
6868 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006869 dex_file) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006870 SetRawInputAt(0, cls);
6871 }
6872
Calin Juravle52c48962014-12-16 17:02:57 +00006873
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006874 bool IsClonable() const override { return true; }
6875 bool CanBeMoved() const override { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006876
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006877 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01006878 const HStaticFieldGet* other_get = other->AsStaticFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00006879 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006880 }
6881
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006882 size_t ComputeHashCode() const override {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006883 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
6884 }
6885
Calin Juravle52c48962014-12-16 17:02:57 +00006886 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006887 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006888 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006889 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006890
Vladimir Marko61b92282017-10-11 13:23:17 +01006891 void SetType(DataType::Type new_type) {
6892 DCHECK(DataType::IsIntegralType(GetType()));
6893 DCHECK(DataType::IsIntegralType(new_type));
6894 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
6895 SetPackedField<TypeField>(new_type);
6896 }
6897
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006898 DECLARE_INSTRUCTION(StaticFieldGet);
6899
Artem Serovcced8ba2017-07-19 18:18:09 +01006900 protected:
6901 DEFAULT_COPY_CONSTRUCTOR(StaticFieldGet);
6902
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006903 private:
6904 const FieldInfo field_info_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006905};
6906
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006907class HStaticFieldSet final : public HExpression<2> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006908 public:
6909 HStaticFieldSet(HInstruction* cls,
6910 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006911 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006912 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006913 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006914 bool is_volatile,
6915 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006916 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006917 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01006918 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006919 : HExpression(kStaticFieldSet,
6920 SideEffects::FieldWriteOfType(field_type, is_volatile),
6921 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006922 field_info_(field,
6923 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006924 field_type,
6925 is_volatile,
6926 field_idx,
6927 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006928 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006929 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006930 SetRawInputAt(0, cls);
6931 SetRawInputAt(1, value);
6932 }
6933
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006934 bool IsClonable() const override { return true; }
Calin Juravle52c48962014-12-16 17:02:57 +00006935 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006936 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006937 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006938 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006939
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006940 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006941 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
6942 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006943
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006944 DECLARE_INSTRUCTION(StaticFieldSet);
6945
Artem Serovcced8ba2017-07-19 18:18:09 +01006946 protected:
6947 DEFAULT_COPY_CONSTRUCTOR(StaticFieldSet);
6948
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006949 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006950 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
6951 static constexpr size_t kNumberOfStaticFieldSetPackedBits = kFlagValueCanBeNull + 1;
6952 static_assert(kNumberOfStaticFieldSetPackedBits <= kMaxNumberOfPackedBits,
6953 "Too many packed fields.");
6954
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006955 const FieldInfo field_info_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006956};
6957
Vladimir Marko552a1342017-10-31 10:56:47 +00006958class HStringBuilderAppend final : public HVariableInputSizeInstruction {
6959 public:
6960 HStringBuilderAppend(HIntConstant* format,
6961 uint32_t number_of_arguments,
6962 ArenaAllocator* allocator,
6963 uint32_t dex_pc)
6964 : HVariableInputSizeInstruction(
6965 kStringBuilderAppend,
6966 DataType::Type::kReference,
6967 // The runtime call may read memory from inputs. It never writes outside
6968 // of the newly allocated result object (or newly allocated helper objects).
6969 SideEffects::AllReads().Union(SideEffects::CanTriggerGC()),
6970 dex_pc,
6971 allocator,
6972 number_of_arguments + /* format */ 1u,
6973 kArenaAllocInvokeInputs) {
6974 DCHECK_GE(number_of_arguments, 1u); // There must be something to append.
6975 SetRawInputAt(FormatIndex(), format);
6976 }
6977
6978 void SetArgumentAt(size_t index, HInstruction* argument) {
6979 DCHECK_LE(index, GetNumberOfArguments());
6980 SetRawInputAt(index, argument);
6981 }
6982
6983 // Return the number of arguments, excluding the format.
6984 size_t GetNumberOfArguments() const {
6985 DCHECK_GE(InputCount(), 1u);
6986 return InputCount() - 1u;
6987 }
6988
6989 size_t FormatIndex() const {
6990 return GetNumberOfArguments();
6991 }
6992
6993 HIntConstant* GetFormat() {
6994 return InputAt(FormatIndex())->AsIntConstant();
6995 }
6996
6997 bool NeedsEnvironment() const override { return true; }
6998
6999 bool CanThrow() const override { return true; }
7000
Vladimir Markob1fe5e12020-03-10 14:30:49 +00007001 bool CanBeNull() const override { return false; }
7002
Vladimir Marko552a1342017-10-31 10:56:47 +00007003 DECLARE_INSTRUCTION(StringBuilderAppend);
7004
7005 protected:
7006 DEFAULT_COPY_CONSTRUCTOR(StringBuilderAppend);
7007};
7008
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007009class HUnresolvedInstanceFieldGet final : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01007010 public:
7011 HUnresolvedInstanceFieldGet(HInstruction* obj,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007012 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01007013 uint32_t field_index,
7014 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307015 : HExpression(kUnresolvedInstanceFieldGet,
7016 field_type,
7017 SideEffects::AllExceptGCDependency(),
7018 dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01007019 field_index_(field_index) {
7020 SetRawInputAt(0, obj);
7021 }
7022
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007023 bool IsClonable() const override { return true; }
7024 bool NeedsEnvironment() const override { return true; }
7025 bool CanThrow() const override { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01007026
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007027 DataType::Type GetFieldType() const { return GetType(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01007028 uint32_t GetFieldIndex() const { return field_index_; }
7029
7030 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
7031
Artem Serovcced8ba2017-07-19 18:18:09 +01007032 protected:
7033 DEFAULT_COPY_CONSTRUCTOR(UnresolvedInstanceFieldGet);
7034
Calin Juravlee460d1d2015-09-29 04:52:17 +01007035 private:
7036 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01007037};
7038
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007039class HUnresolvedInstanceFieldSet final : public HExpression<2> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01007040 public:
7041 HUnresolvedInstanceFieldSet(HInstruction* obj,
7042 HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007043 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01007044 uint32_t field_index,
7045 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01007046 : HExpression(kUnresolvedInstanceFieldSet, SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01007047 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007048 SetPackedField<FieldTypeField>(field_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007049 DCHECK_EQ(DataType::Kind(field_type), DataType::Kind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01007050 SetRawInputAt(0, obj);
7051 SetRawInputAt(1, value);
7052 }
7053
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007054 bool IsClonable() const override { return true; }
7055 bool NeedsEnvironment() const override { return true; }
7056 bool CanThrow() const override { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01007057
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007058 DataType::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01007059 uint32_t GetFieldIndex() const { return field_index_; }
7060
7061 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
7062
Artem Serovcced8ba2017-07-19 18:18:09 +01007063 protected:
7064 DEFAULT_COPY_CONSTRUCTOR(UnresolvedInstanceFieldSet);
7065
Calin Juravlee460d1d2015-09-29 04:52:17 +01007066 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007067 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
7068 static constexpr size_t kFieldFieldTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007069 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00007070 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
7071 kFieldFieldType + kFieldFieldTypeSize;
7072 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
7073 "Too many packed fields.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007074 using FieldTypeField = BitField<DataType::Type, kFieldFieldType, kFieldFieldTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00007075
Calin Juravlee460d1d2015-09-29 04:52:17 +01007076 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01007077};
7078
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007079class HUnresolvedStaticFieldGet final : public HExpression<0> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01007080 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007081 HUnresolvedStaticFieldGet(DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01007082 uint32_t field_index,
7083 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307084 : HExpression(kUnresolvedStaticFieldGet,
7085 field_type,
7086 SideEffects::AllExceptGCDependency(),
7087 dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01007088 field_index_(field_index) {
7089 }
7090
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007091 bool IsClonable() const override { return true; }
7092 bool NeedsEnvironment() const override { return true; }
7093 bool CanThrow() const override { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01007094
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007095 DataType::Type GetFieldType() const { return GetType(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01007096 uint32_t GetFieldIndex() const { return field_index_; }
7097
7098 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
7099
Artem Serovcced8ba2017-07-19 18:18:09 +01007100 protected:
7101 DEFAULT_COPY_CONSTRUCTOR(UnresolvedStaticFieldGet);
7102
Calin Juravlee460d1d2015-09-29 04:52:17 +01007103 private:
7104 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01007105};
7106
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007107class HUnresolvedStaticFieldSet final : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01007108 public:
7109 HUnresolvedStaticFieldSet(HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007110 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01007111 uint32_t field_index,
7112 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01007113 : HExpression(kUnresolvedStaticFieldSet, SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01007114 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007115 SetPackedField<FieldTypeField>(field_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007116 DCHECK_EQ(DataType::Kind(field_type), DataType::Kind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01007117 SetRawInputAt(0, value);
7118 }
7119
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007120 bool IsClonable() const override { return true; }
7121 bool NeedsEnvironment() const override { return true; }
7122 bool CanThrow() const override { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01007123
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007124 DataType::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01007125 uint32_t GetFieldIndex() const { return field_index_; }
7126
7127 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
7128
Artem Serovcced8ba2017-07-19 18:18:09 +01007129 protected:
7130 DEFAULT_COPY_CONSTRUCTOR(UnresolvedStaticFieldSet);
7131
Calin Juravlee460d1d2015-09-29 04:52:17 +01007132 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007133 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
7134 static constexpr size_t kFieldFieldTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007135 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00007136 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
7137 kFieldFieldType + kFieldFieldTypeSize;
7138 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
7139 "Too many packed fields.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007140 using FieldTypeField = BitField<DataType::Type, kFieldFieldType, kFieldFieldTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00007141
Calin Juravlee460d1d2015-09-29 04:52:17 +01007142 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01007143};
7144
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007145// Implement the move-exception DEX instruction.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007146class HLoadException final : public HExpression<0> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007147 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06007148 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307149 : HExpression(kLoadException, DataType::Type::kReference, SideEffects::None(), dex_pc) {
7150 }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007151
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007152 bool CanBeNull() const override { return false; }
David Brazdilbbd733e2015-08-18 17:48:17 +01007153
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007154 DECLARE_INSTRUCTION(LoadException);
7155
Artem Serovcced8ba2017-07-19 18:18:09 +01007156 protected:
7157 DEFAULT_COPY_CONSTRUCTOR(LoadException);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007158};
7159
David Brazdilcb1c0552015-08-04 16:22:25 +01007160// Implicit part of move-exception which clears thread-local exception storage.
7161// Must not be removed because the runtime expects the TLS to get cleared.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007162class HClearException final : public HExpression<0> {
David Brazdilcb1c0552015-08-04 16:22:25 +01007163 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06007164 explicit HClearException(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01007165 : HExpression(kClearException, SideEffects::AllWrites(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307166 }
David Brazdilcb1c0552015-08-04 16:22:25 +01007167
7168 DECLARE_INSTRUCTION(ClearException);
7169
Artem Serovcced8ba2017-07-19 18:18:09 +01007170 protected:
7171 DEFAULT_COPY_CONSTRUCTOR(ClearException);
David Brazdilcb1c0552015-08-04 16:22:25 +01007172};
7173
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007174class HThrow final : public HExpression<1> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007175 public:
7176 HThrow(HInstruction* exception, uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01007177 : HExpression(kThrow, SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007178 SetRawInputAt(0, exception);
7179 }
7180
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007181 bool IsControlFlow() const override { return true; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007182
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007183 bool NeedsEnvironment() const override { return true; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007184
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007185 bool CanThrow() const override { return true; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007186
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007187 bool AlwaysThrows() const override { return true; }
Aart Bika8b8e9b2018-01-09 11:01:02 -08007188
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007189 DECLARE_INSTRUCTION(Throw);
7190
Artem Serovcced8ba2017-07-19 18:18:09 +01007191 protected:
7192 DEFAULT_COPY_CONSTRUCTOR(Throw);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007193};
7194
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007195/**
7196 * Implementation strategies for the code generator of a HInstanceOf
7197 * or `HCheckCast`.
7198 */
7199enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01007200 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007201 kExactCheck, // Can do a single class compare.
7202 kClassHierarchyCheck, // Can just walk the super class chain.
7203 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
7204 kInterfaceCheck, // No optimization yet when checking against an interface.
7205 kArrayObjectCheck, // Can just check if the array is not primitive.
Vladimir Markoa1de9182016-02-25 11:37:38 +00007206 kArrayCheck, // No optimization yet when checking against a generic array.
Vladimir Marko175e7862018-03-27 09:03:13 +00007207 kBitstringCheck, // Compare the type check bitstring.
Vladimir Markoa1de9182016-02-25 11:37:38 +00007208 kLast = kArrayCheck
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007209};
7210
Roland Levillain86503782016-02-11 19:07:30 +00007211std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs);
7212
Vladimir Marko175e7862018-03-27 09:03:13 +00007213// Note: HTypeCheckInstruction is just a helper class, not an abstract instruction with an
7214// `IsTypeCheckInstruction()`. (New virtual methods in the HInstruction class have a high cost.)
7215class HTypeCheckInstruction : public HVariableInputSizeInstruction {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007216 public:
Vladimir Marko175e7862018-03-27 09:03:13 +00007217 HTypeCheckInstruction(InstructionKind kind,
Vladimir Markobd785672018-05-03 17:09:09 +01007218 DataType::Type type,
Vladimir Marko175e7862018-03-27 09:03:13 +00007219 HInstruction* object,
7220 HInstruction* target_class_or_null,
7221 TypeCheckKind check_kind,
7222 Handle<mirror::Class> klass,
7223 uint32_t dex_pc,
7224 ArenaAllocator* allocator,
7225 HIntConstant* bitstring_path_to_root,
7226 HIntConstant* bitstring_mask,
7227 SideEffects side_effects)
7228 : HVariableInputSizeInstruction(
7229 kind,
Vladimir Markobd785672018-05-03 17:09:09 +01007230 type,
Vladimir Marko175e7862018-03-27 09:03:13 +00007231 side_effects,
7232 dex_pc,
7233 allocator,
Andreas Gampe3db70682018-12-26 15:12:03 -08007234 /* number_of_inputs= */ check_kind == TypeCheckKind::kBitstringCheck ? 4u : 2u,
Vladimir Marko175e7862018-03-27 09:03:13 +00007235 kArenaAllocTypeCheckInputs),
7236 klass_(klass) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007237 SetPackedField<TypeCheckKindField>(check_kind);
7238 SetPackedFlag<kFlagMustDoNullCheck>(true);
Vladimir Marko175e7862018-03-27 09:03:13 +00007239 SetPackedFlag<kFlagValidTargetClassRTI>(false);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007240 SetRawInputAt(0, object);
Vladimir Marko175e7862018-03-27 09:03:13 +00007241 SetRawInputAt(1, target_class_or_null);
7242 DCHECK_EQ(check_kind == TypeCheckKind::kBitstringCheck, bitstring_path_to_root != nullptr);
7243 DCHECK_EQ(check_kind == TypeCheckKind::kBitstringCheck, bitstring_mask != nullptr);
7244 if (check_kind == TypeCheckKind::kBitstringCheck) {
7245 DCHECK(target_class_or_null->IsNullConstant());
7246 SetRawInputAt(2, bitstring_path_to_root);
7247 SetRawInputAt(3, bitstring_mask);
7248 } else {
7249 DCHECK(target_class_or_null->IsLoadClass());
7250 }
Vladimir Marko87584542017-12-12 17:47:52 +00007251 }
7252
7253 HLoadClass* GetTargetClass() const {
Vladimir Marko175e7862018-03-27 09:03:13 +00007254 DCHECK_NE(GetTypeCheckKind(), TypeCheckKind::kBitstringCheck);
Vladimir Marko87584542017-12-12 17:47:52 +00007255 HInstruction* load_class = InputAt(1);
7256 DCHECK(load_class->IsLoadClass());
7257 return load_class->AsLoadClass();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007258 }
7259
Vladimir Marko175e7862018-03-27 09:03:13 +00007260 uint32_t GetBitstringPathToRoot() const {
7261 DCHECK_EQ(GetTypeCheckKind(), TypeCheckKind::kBitstringCheck);
7262 HInstruction* path_to_root = InputAt(2);
7263 DCHECK(path_to_root->IsIntConstant());
7264 return static_cast<uint32_t>(path_to_root->AsIntConstant()->GetValue());
7265 }
7266
7267 uint32_t GetBitstringMask() const {
7268 DCHECK_EQ(GetTypeCheckKind(), TypeCheckKind::kBitstringCheck);
7269 HInstruction* mask = InputAt(3);
7270 DCHECK(mask->IsIntConstant());
7271 return static_cast<uint32_t>(mask->AsIntConstant()->GetValue());
7272 }
7273
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007274 bool IsClonable() const override { return true; }
7275 bool CanBeMoved() const override { return true; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007276
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007277 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Marko175e7862018-03-27 09:03:13 +00007278 DCHECK(other->IsInstanceOf() || other->IsCheckCast()) << other->DebugName();
7279 return GetPackedFields() == down_cast<const HTypeCheckInstruction*>(other)->GetPackedFields();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007280 }
7281
Andreas Gampe3fbd3ad2018-03-26 21:14:46 +00007282 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
7283 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
7284 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
7285 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
7286
Vladimir Marko175e7862018-03-27 09:03:13 +00007287 ReferenceTypeInfo GetTargetClassRTI() {
7288 if (GetPackedFlag<kFlagValidTargetClassRTI>()) {
7289 // Note: The is_exact flag from the return value should not be used.
Andreas Gampe3db70682018-12-26 15:12:03 -08007290 return ReferenceTypeInfo::CreateUnchecked(klass_, /* is_exact= */ true);
Vladimir Marko175e7862018-03-27 09:03:13 +00007291 } else {
7292 return ReferenceTypeInfo::CreateInvalid();
7293 }
7294 }
7295
7296 // Target class RTI is marked as valid by RTP if the klass_ is admissible.
7297 void SetValidTargetClassRTI() REQUIRES_SHARED(Locks::mutator_lock_) {
7298 DCHECK(klass_ != nullptr);
7299 SetPackedFlag<kFlagValidTargetClassRTI>(true);
7300 }
7301
7302 Handle<mirror::Class> GetClass() const {
7303 return klass_;
7304 }
7305
7306 protected:
7307 DEFAULT_COPY_CONSTRUCTOR(TypeCheckInstruction);
7308
7309 private:
7310 static constexpr size_t kFieldTypeCheckKind = kNumberOfGenericPackedBits;
7311 static constexpr size_t kFieldTypeCheckKindSize =
7312 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
7313 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
7314 static constexpr size_t kFlagValidTargetClassRTI = kFlagMustDoNullCheck + 1;
7315 static constexpr size_t kNumberOfInstanceOfPackedBits = kFlagValidTargetClassRTI + 1;
7316 static_assert(kNumberOfInstanceOfPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
7317 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
7318
7319 Handle<mirror::Class> klass_;
7320};
7321
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007322class HInstanceOf final : public HTypeCheckInstruction {
Vladimir Marko175e7862018-03-27 09:03:13 +00007323 public:
7324 HInstanceOf(HInstruction* object,
7325 HInstruction* target_class_or_null,
7326 TypeCheckKind check_kind,
7327 Handle<mirror::Class> klass,
7328 uint32_t dex_pc,
7329 ArenaAllocator* allocator,
7330 HIntConstant* bitstring_path_to_root,
7331 HIntConstant* bitstring_mask)
7332 : HTypeCheckInstruction(kInstanceOf,
Vladimir Markobd785672018-05-03 17:09:09 +01007333 DataType::Type::kBool,
Vladimir Marko175e7862018-03-27 09:03:13 +00007334 object,
7335 target_class_or_null,
7336 check_kind,
7337 klass,
7338 dex_pc,
7339 allocator,
7340 bitstring_path_to_root,
7341 bitstring_mask,
7342 SideEffectsForArchRuntimeCalls(check_kind)) {}
7343
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007344 bool IsClonable() const override { return true; }
Artem Serova6e26142018-06-19 14:55:17 +01007345
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007346 bool NeedsEnvironment() const override {
Vladimir Marko175e7862018-03-27 09:03:13 +00007347 return CanCallRuntime(GetTypeCheckKind());
7348 }
7349
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00007350 static bool CanCallRuntime(TypeCheckKind check_kind) {
Vladimir Marko54f4fbd2020-02-12 10:52:22 +00007351 // TODO: Re-evaluate now that mips codegen has been removed.
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00007352 return check_kind != TypeCheckKind::kExactCheck;
7353 }
7354
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007355 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00007356 return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01007357 }
7358
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007359 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007360
Artem Serovcced8ba2017-07-19 18:18:09 +01007361 protected:
7362 DEFAULT_COPY_CONSTRUCTOR(InstanceOf);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007363};
7364
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007365class HBoundType final : public HExpression<1> {
Calin Juravleb1498f62015-02-16 13:13:29 +00007366 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -07007367 explicit HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307368 : HExpression(kBoundType, DataType::Type::kReference, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00007369 upper_bound_(ReferenceTypeInfo::CreateInvalid()) {
7370 SetPackedFlag<kFlagUpperCanBeNull>(true);
7371 SetPackedFlag<kFlagCanBeNull>(true);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007372 DCHECK_EQ(input->GetType(), DataType::Type::kReference);
Calin Juravleb1498f62015-02-16 13:13:29 +00007373 SetRawInputAt(0, input);
7374 }
7375
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007376 bool InstructionDataEquals(const HInstruction* other) const override;
7377 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01007378
David Brazdilf5552582015-12-27 13:36:12 +00007379 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007380 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00007381 bool GetUpperCanBeNull() const { return GetPackedFlag<kFlagUpperCanBeNull>(); }
David Brazdilf5552582015-12-27 13:36:12 +00007382 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00007383
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007384 void SetCanBeNull(bool can_be_null) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007385 DCHECK(GetUpperCanBeNull() || !can_be_null);
7386 SetPackedFlag<kFlagCanBeNull>(can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00007387 }
7388
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007389 bool CanBeNull() const override { return GetPackedFlag<kFlagCanBeNull>(); }
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007390
Calin Juravleb1498f62015-02-16 13:13:29 +00007391 DECLARE_INSTRUCTION(BoundType);
7392
Artem Serovcced8ba2017-07-19 18:18:09 +01007393 protected:
7394 DEFAULT_COPY_CONSTRUCTOR(BoundType);
7395
Calin Juravleb1498f62015-02-16 13:13:29 +00007396 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007397 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
7398 // is false then CanBeNull() cannot be true).
Vladimir Markobd785672018-05-03 17:09:09 +01007399 static constexpr size_t kFlagUpperCanBeNull = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00007400 static constexpr size_t kFlagCanBeNull = kFlagUpperCanBeNull + 1;
7401 static constexpr size_t kNumberOfBoundTypePackedBits = kFlagCanBeNull + 1;
7402 static_assert(kNumberOfBoundTypePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
7403
Calin Juravleb1498f62015-02-16 13:13:29 +00007404 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007405 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
7406 // It is used to bound the type in cases like:
7407 // if (x instanceof ClassX) {
7408 // // uper_bound_ will be ClassX
7409 // }
David Brazdilf5552582015-12-27 13:36:12 +00007410 ReferenceTypeInfo upper_bound_;
Calin Juravleb1498f62015-02-16 13:13:29 +00007411};
7412
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007413class HCheckCast final : public HTypeCheckInstruction {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007414 public:
7415 HCheckCast(HInstruction* object,
Vladimir Marko175e7862018-03-27 09:03:13 +00007416 HInstruction* target_class_or_null,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007417 TypeCheckKind check_kind,
Vladimir Marko175e7862018-03-27 09:03:13 +00007418 Handle<mirror::Class> klass,
7419 uint32_t dex_pc,
7420 ArenaAllocator* allocator,
7421 HIntConstant* bitstring_path_to_root,
7422 HIntConstant* bitstring_mask)
7423 : HTypeCheckInstruction(kCheckCast,
Vladimir Markobd785672018-05-03 17:09:09 +01007424 DataType::Type::kVoid,
Vladimir Marko175e7862018-03-27 09:03:13 +00007425 object,
7426 target_class_or_null,
7427 check_kind,
7428 klass,
7429 dex_pc,
7430 allocator,
7431 bitstring_path_to_root,
7432 bitstring_mask,
7433 SideEffects::CanTriggerGC()) {}
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007434
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007435 bool IsClonable() const override { return true; }
7436 bool NeedsEnvironment() const override {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007437 // Instruction may throw a CheckCastError.
7438 return true;
7439 }
7440
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007441 bool CanThrow() const override { return true; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007442
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007443 DECLARE_INSTRUCTION(CheckCast);
7444
Artem Serovcced8ba2017-07-19 18:18:09 +01007445 protected:
7446 DEFAULT_COPY_CONSTRUCTOR(CheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007447};
7448
Andreas Gampe26de38b2016-07-27 17:53:11 -07007449/**
7450 * @brief Memory barrier types (see "The JSR-133 Cookbook for Compiler Writers").
7451 * @details We define the combined barrier types that are actually required
7452 * by the Java Memory Model, rather than using exactly the terminology from
7453 * the JSR-133 cookbook. These should, in many cases, be replaced by acquire/release
7454 * primitives. Note that the JSR-133 cookbook generally does not deal with
7455 * store atomicity issues, and the recipes there are not always entirely sufficient.
7456 * The current recipe is as follows:
7457 * -# Use AnyStore ~= (LoadStore | StoreStore) ~= release barrier before volatile store.
7458 * -# Use AnyAny barrier after volatile store. (StoreLoad is as expensive.)
7459 * -# Use LoadAny barrier ~= (LoadLoad | LoadStore) ~= acquire barrier after each volatile load.
7460 * -# Use StoreStore barrier after all stores but before return from any constructor whose
7461 * class has final fields.
7462 * -# Use NTStoreStore to order non-temporal stores with respect to all later
7463 * store-to-memory instructions. Only generated together with non-temporal stores.
7464 */
7465enum MemBarrierKind {
7466 kAnyStore,
7467 kLoadAny,
7468 kStoreStore,
7469 kAnyAny,
7470 kNTStoreStore,
7471 kLastBarrierKind = kNTStoreStore
7472};
7473std::ostream& operator<<(std::ostream& os, const MemBarrierKind& kind);
7474
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007475class HMemoryBarrier final : public HExpression<0> {
Calin Juravle27df7582015-04-17 19:12:31 +01007476 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06007477 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01007478 : HExpression(kMemoryBarrier,
7479 SideEffects::AllWritesAndReads(), // Assume write/read on all fields/arrays.
7480 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007481 SetPackedField<BarrierKindField>(barrier_kind);
7482 }
Calin Juravle27df7582015-04-17 19:12:31 +01007483
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007484 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01007485
Vladimir Markoa1de9182016-02-25 11:37:38 +00007486 MemBarrierKind GetBarrierKind() { return GetPackedField<BarrierKindField>(); }
Calin Juravle27df7582015-04-17 19:12:31 +01007487
7488 DECLARE_INSTRUCTION(MemoryBarrier);
7489
Artem Serovcced8ba2017-07-19 18:18:09 +01007490 protected:
7491 DEFAULT_COPY_CONSTRUCTOR(MemoryBarrier);
7492
Calin Juravle27df7582015-04-17 19:12:31 +01007493 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007494 static constexpr size_t kFieldBarrierKind = HInstruction::kNumberOfGenericPackedBits;
7495 static constexpr size_t kFieldBarrierKindSize =
7496 MinimumBitsToStore(static_cast<size_t>(kLastBarrierKind));
7497 static constexpr size_t kNumberOfMemoryBarrierPackedBits =
7498 kFieldBarrierKind + kFieldBarrierKindSize;
7499 static_assert(kNumberOfMemoryBarrierPackedBits <= kMaxNumberOfPackedBits,
7500 "Too many packed fields.");
7501 using BarrierKindField = BitField<MemBarrierKind, kFieldBarrierKind, kFieldBarrierKindSize>;
Calin Juravle27df7582015-04-17 19:12:31 +01007502};
7503
Igor Murashkind01745e2017-04-05 16:40:31 -07007504// A constructor fence orders all prior stores to fields that could be accessed via a final field of
7505// the specified object(s), with respect to any subsequent store that might "publish"
7506// (i.e. make visible) the specified object to another thread.
7507//
7508// JLS 17.5.1 "Semantics of final fields" states that a freeze action happens
7509// for all final fields (that were set) at the end of the invoked constructor.
7510//
7511// The constructor fence models the freeze actions for the final fields of an object
7512// being constructed (semantically at the end of the constructor). Constructor fences
7513// have a per-object affinity; two separate objects being constructed get two separate
7514// constructor fences.
7515//
7516// (Note: that if calling a super-constructor or forwarding to another constructor,
7517// the freezes would happen at the end of *that* constructor being invoked).
7518//
7519// The memory model guarantees that when the object being constructed is "published" after
7520// constructor completion (i.e. escapes the current thread via a store), then any final field
7521// writes must be observable on other threads (once they observe that publication).
7522//
7523// Further, anything written before the freeze, and read by dereferencing through the final field,
7524// must also be visible (so final object field could itself have an object with non-final fields;
7525// yet the freeze must also extend to them).
7526//
7527// Constructor example:
7528//
7529// class HasFinal {
7530// final int field; Optimizing IR for <init>()V:
7531// HasFinal() {
7532// field = 123; HInstanceFieldSet(this, HasFinal.field, 123)
7533// // freeze(this.field); HConstructorFence(this)
7534// } HReturn
7535// }
7536//
7537// HConstructorFence can serve double duty as a fence for new-instance/new-array allocations of
7538// already-initialized classes; in that case the allocation must act as a "default-initializer"
7539// of the object which effectively writes the class pointer "final field".
7540//
7541// For example, we can model default-initialiation as roughly the equivalent of the following:
7542//
7543// class Object {
7544// private final Class header;
7545// }
7546//
7547// Java code: Optimizing IR:
7548//
7549// T new_instance<T>() {
7550// Object obj = allocate_memory(T.class.size); obj = HInvoke(art_quick_alloc_object, T)
7551// obj.header = T.class; // header write is done by above call.
7552// // freeze(obj.header) HConstructorFence(obj)
7553// return (T)obj;
7554// }
7555//
7556// See also:
Vladimir Markoc1c34522018-10-31 13:56:49 +00007557// * DexCompilationUnit::RequiresConstructorBarrier
Igor Murashkind01745e2017-04-05 16:40:31 -07007558// * QuasiAtomic::ThreadFenceForConstructor
7559//
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007560class HConstructorFence final : public HVariableInputSizeInstruction {
Igor Murashkind01745e2017-04-05 16:40:31 -07007561 // A fence has variable inputs because the inputs can be removed
7562 // after prepare_for_register_allocation phase.
7563 // (TODO: In the future a fence could freeze multiple objects
7564 // after merging two fences together.)
7565 public:
7566 // `fence_object` is the reference that needs to be protected for correct publication.
7567 //
7568 // It makes sense in the following situations:
7569 // * <init> constructors, it's the "this" parameter (i.e. HParameterValue, s.t. IsThis() == true).
7570 // * new-instance-like instructions, it's the return value (i.e. HNewInstance).
7571 //
7572 // After construction the `fence_object` becomes the 0th input.
7573 // This is not an input in a real sense, but just a convenient place to stash the information
7574 // about the associated object.
7575 HConstructorFence(HInstruction* fence_object,
7576 uint32_t dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007577 ArenaAllocator* allocator)
Igor Murashkind01745e2017-04-05 16:40:31 -07007578 // We strongly suspect there is not a more accurate way to describe the fine-grained reordering
7579 // constraints described in the class header. We claim that these SideEffects constraints
7580 // enforce a superset of the real constraints.
7581 //
7582 // The ordering described above is conservatively modeled with SideEffects as follows:
7583 //
7584 // * To prevent reordering of the publication stores:
7585 // ----> "Reads of objects" is the initial SideEffect.
7586 // * For every primitive final field store in the constructor:
7587 // ----> Union that field's type as a read (e.g. "Read of T") into the SideEffect.
7588 // * If there are any stores to reference final fields in the constructor:
7589 // ----> Use a more conservative "AllReads" SideEffect because any stores to any references
7590 // that are reachable from `fence_object` also need to be prevented for reordering
7591 // (and we do not want to do alias analysis to figure out what those stores are).
7592 //
7593 // In the implementation, this initially starts out as an "all reads" side effect; this is an
7594 // even more conservative approach than the one described above, and prevents all of the
7595 // above reordering without analyzing any of the instructions in the constructor.
7596 //
7597 // If in a later phase we discover that there are no writes to reference final fields,
7598 // we can refine the side effect to a smaller set of type reads (see above constraints).
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307599 : HVariableInputSizeInstruction(kConstructorFence,
7600 SideEffects::AllReads(),
Igor Murashkind01745e2017-04-05 16:40:31 -07007601 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007602 allocator,
Andreas Gampe3db70682018-12-26 15:12:03 -08007603 /* number_of_inputs= */ 1,
Igor Murashkind01745e2017-04-05 16:40:31 -07007604 kArenaAllocConstructorFenceInputs) {
7605 DCHECK(fence_object != nullptr);
7606 SetRawInputAt(0, fence_object);
7607 }
7608
7609 // The object associated with this constructor fence.
7610 //
7611 // (Note: This will be null after the prepare_for_register_allocation phase,
7612 // as all constructor fence inputs are removed there).
7613 HInstruction* GetFenceObject() const {
7614 return InputAt(0);
7615 }
7616
7617 // Find all the HConstructorFence uses (`fence_use`) for `this` and:
7618 // - Delete `fence_use` from `this`'s use list.
7619 // - Delete `this` from `fence_use`'s inputs list.
7620 // - If the `fence_use` is dead, remove it from the graph.
7621 //
7622 // A fence is considered dead once it no longer has any uses
7623 // and all of the inputs are dead.
7624 //
7625 // This must *not* be called during/after prepare_for_register_allocation,
7626 // because that removes all the inputs to the fences but the fence is actually
7627 // still considered live.
Igor Murashkin6ef45672017-08-08 13:59:55 -07007628 //
7629 // Returns how many HConstructorFence instructions were removed from graph.
7630 static size_t RemoveConstructorFences(HInstruction* instruction);
Igor Murashkind01745e2017-04-05 16:40:31 -07007631
Igor Murashkindd018df2017-08-09 10:38:31 -07007632 // Combine all inputs of `this` and `other` instruction and remove
7633 // `other` from the graph.
7634 //
7635 // Inputs are unique after the merge.
7636 //
7637 // Requirement: `this` must not be the same as `other.
7638 void Merge(HConstructorFence* other);
7639
Igor Murashkin79d8fa72017-04-18 09:37:23 -07007640 // Check if this constructor fence is protecting
7641 // an HNewInstance or HNewArray that is also the immediate
7642 // predecessor of `this`.
7643 //
Igor Murashkindd018df2017-08-09 10:38:31 -07007644 // If `ignore_inputs` is true, then the immediate predecessor doesn't need
7645 // to be one of the inputs of `this`.
7646 //
Igor Murashkin79d8fa72017-04-18 09:37:23 -07007647 // Returns the associated HNewArray or HNewInstance,
7648 // or null otherwise.
Igor Murashkindd018df2017-08-09 10:38:31 -07007649 HInstruction* GetAssociatedAllocation(bool ignore_inputs = false);
Igor Murashkin79d8fa72017-04-18 09:37:23 -07007650
Igor Murashkind01745e2017-04-05 16:40:31 -07007651 DECLARE_INSTRUCTION(ConstructorFence);
7652
Artem Serovcced8ba2017-07-19 18:18:09 +01007653 protected:
7654 DEFAULT_COPY_CONSTRUCTOR(ConstructorFence);
Igor Murashkind01745e2017-04-05 16:40:31 -07007655};
7656
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007657class HMonitorOperation final : public HExpression<1> {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007658 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007659 enum class OperationKind {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007660 kEnter,
7661 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00007662 kLast = kExit
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007663 };
7664
7665 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01007666 : HExpression(kMonitorOperation,
7667 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
7668 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007669 SetPackedField<OperationKindField>(kind);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007670 SetRawInputAt(0, object);
7671 }
7672
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00007673 // Instruction may go into runtime, so we need an environment.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007674 bool NeedsEnvironment() const override { return true; }
David Brazdilbff75032015-07-08 17:26:51 +00007675
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007676 bool CanThrow() const override {
David Brazdilbff75032015-07-08 17:26:51 +00007677 // Verifier guarantees that monitor-exit cannot throw.
7678 // This is important because it allows the HGraphBuilder to remove
7679 // a dead throw-catch loop generated for `synchronized` blocks/methods.
7680 return IsEnter();
7681 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007682
Vladimir Markoa1de9182016-02-25 11:37:38 +00007683 OperationKind GetOperationKind() const { return GetPackedField<OperationKindField>(); }
7684 bool IsEnter() const { return GetOperationKind() == OperationKind::kEnter; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007685
7686 DECLARE_INSTRUCTION(MonitorOperation);
7687
Artem Serovcced8ba2017-07-19 18:18:09 +01007688 protected:
7689 DEFAULT_COPY_CONSTRUCTOR(MonitorOperation);
7690
Calin Juravle52c48962014-12-16 17:02:57 +00007691 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007692 static constexpr size_t kFieldOperationKind = HInstruction::kNumberOfGenericPackedBits;
7693 static constexpr size_t kFieldOperationKindSize =
7694 MinimumBitsToStore(static_cast<size_t>(OperationKind::kLast));
7695 static constexpr size_t kNumberOfMonitorOperationPackedBits =
7696 kFieldOperationKind + kFieldOperationKindSize;
7697 static_assert(kNumberOfMonitorOperationPackedBits <= HInstruction::kMaxNumberOfPackedBits,
7698 "Too many packed fields.");
7699 using OperationKindField = BitField<OperationKind, kFieldOperationKind, kFieldOperationKindSize>;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007700};
7701
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007702class HSelect final : public HExpression<3> {
David Brazdil74eb1b22015-12-14 11:44:01 +00007703 public:
7704 HSelect(HInstruction* condition,
7705 HInstruction* true_value,
7706 HInstruction* false_value,
7707 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307708 : HExpression(kSelect, HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
David Brazdil74eb1b22015-12-14 11:44:01 +00007709 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
7710
7711 // First input must be `true_value` or `false_value` to allow codegens to
7712 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
7713 // that architectures which implement HSelect as a conditional move also
7714 // will not need to invert the condition.
7715 SetRawInputAt(0, false_value);
7716 SetRawInputAt(1, true_value);
7717 SetRawInputAt(2, condition);
7718 }
7719
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007720 bool IsClonable() const override { return true; }
David Brazdil74eb1b22015-12-14 11:44:01 +00007721 HInstruction* GetFalseValue() const { return InputAt(0); }
7722 HInstruction* GetTrueValue() const { return InputAt(1); }
7723 HInstruction* GetCondition() const { return InputAt(2); }
7724
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007725 bool CanBeMoved() const override { return true; }
7726 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01007727 return true;
7728 }
David Brazdil74eb1b22015-12-14 11:44:01 +00007729
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007730 bool CanBeNull() const override {
David Brazdil74eb1b22015-12-14 11:44:01 +00007731 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
7732 }
7733
7734 DECLARE_INSTRUCTION(Select);
7735
Artem Serovcced8ba2017-07-19 18:18:09 +01007736 protected:
7737 DEFAULT_COPY_CONSTRUCTOR(Select);
David Brazdil74eb1b22015-12-14 11:44:01 +00007738};
7739
Vladimir Markof9f64412015-09-02 14:05:49 +01007740class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007741 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01007742 MoveOperands(Location source,
7743 Location destination,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007744 DataType::Type type,
Nicolas Geoffray90218252015-04-15 11:56:51 +01007745 HInstruction* instruction)
7746 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007747
7748 Location GetSource() const { return source_; }
7749 Location GetDestination() const { return destination_; }
7750
7751 void SetSource(Location value) { source_ = value; }
7752 void SetDestination(Location value) { destination_ = value; }
7753
7754 // The parallel move resolver marks moves as "in-progress" by clearing the
7755 // destination (but not the source).
7756 Location MarkPending() {
7757 DCHECK(!IsPending());
7758 Location dest = destination_;
7759 destination_ = Location::NoLocation();
7760 return dest;
7761 }
7762
7763 void ClearPending(Location dest) {
7764 DCHECK(IsPending());
7765 destination_ = dest;
7766 }
7767
7768 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00007769 DCHECK(source_.IsValid() || destination_.IsInvalid());
7770 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007771 }
7772
7773 // True if this blocks a move from the given location.
7774 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08007775 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007776 }
7777
7778 // A move is redundant if it's been eliminated, if its source and
7779 // destination are the same, or if its destination is unneeded.
7780 bool IsRedundant() const {
7781 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
7782 }
7783
7784 // We clear both operands to indicate move that's been eliminated.
7785 void Eliminate() {
7786 source_ = destination_ = Location::NoLocation();
7787 }
7788
7789 bool IsEliminated() const {
7790 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
7791 return source_.IsInvalid();
7792 }
7793
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007794 DataType::Type GetType() const { return type_; }
Alexey Frunze4dda3372015-06-01 18:31:49 -07007795
Nicolas Geoffray90218252015-04-15 11:56:51 +01007796 bool Is64BitMove() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007797 return DataType::Is64BitType(type_);
Nicolas Geoffray90218252015-04-15 11:56:51 +01007798 }
7799
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007800 HInstruction* GetInstruction() const { return instruction_; }
7801
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007802 private:
7803 Location source_;
7804 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01007805 // The type this move is for.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007806 DataType::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007807 // The instruction this move is assocatied with. Null when this move is
7808 // for moving an input in the expected locations of user (including a phi user).
7809 // This is only used in debug mode, to ensure we do not connect interval siblings
7810 // in the same parallel move.
7811 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007812};
7813
Roland Levillainc9285912015-12-18 10:38:42 +00007814std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
7815
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007816static constexpr size_t kDefaultNumberOfMoves = 4;
7817
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007818class HParallelMove final : public HExpression<0> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007819 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007820 explicit HParallelMove(ArenaAllocator* allocator, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01007821 : HExpression(kParallelMove, SideEffects::None(), dex_pc),
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007822 moves_(allocator->Adapter(kArenaAllocMoveOperands)) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007823 moves_.reserve(kDefaultNumberOfMoves);
7824 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007825
Nicolas Geoffray90218252015-04-15 11:56:51 +01007826 void AddMove(Location source,
7827 Location destination,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007828 DataType::Type type,
Nicolas Geoffray90218252015-04-15 11:56:51 +01007829 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00007830 DCHECK(source.IsValid());
7831 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00007832 if (kIsDebugBuild) {
7833 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007834 for (const MoveOperands& move : moves_) {
7835 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007836 // Special case the situation where the move is for the spill slot
7837 // of the instruction.
7838 if ((GetPrevious() == instruction)
7839 || ((GetPrevious() == nullptr)
7840 && instruction->IsPhi()
7841 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007842 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007843 << "Doing parallel moves for the same instruction.";
7844 } else {
7845 DCHECK(false) << "Doing parallel moves for the same instruction.";
7846 }
7847 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00007848 }
7849 }
Vladimir Marko225b6462015-09-28 12:17:40 +01007850 for (const MoveOperands& move : moves_) {
7851 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01007852 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01007853 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01007854 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00007855 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007856 }
Vladimir Marko225b6462015-09-28 12:17:40 +01007857 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007858 }
7859
Vladimir Marko225b6462015-09-28 12:17:40 +01007860 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007861 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007862 }
7863
Vladimir Marko225b6462015-09-28 12:17:40 +01007864 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007865
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01007866 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007867
Artem Serovcced8ba2017-07-19 18:18:09 +01007868 protected:
7869 DEFAULT_COPY_CONSTRUCTOR(ParallelMove);
7870
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007871 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01007872 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007873};
7874
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007875// This instruction computes an intermediate address pointing in the 'middle' of an object. The
7876// result pointer cannot be handled by GC, so extra care is taken to make sure that this value is
7877// never used across anything that can trigger GC.
7878// The result of this instruction is not a pointer in the sense of `DataType::Type::kreference`.
7879// So we represent it by the type `DataType::Type::kInt`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007880class HIntermediateAddress final : public HExpression<2> {
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007881 public:
7882 HIntermediateAddress(HInstruction* base_address, HInstruction* offset, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307883 : HExpression(kIntermediateAddress,
7884 DataType::Type::kInt32,
7885 SideEffects::DependsOnGC(),
7886 dex_pc) {
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007887 DCHECK_EQ(DataType::Size(DataType::Type::kInt32),
7888 DataType::Size(DataType::Type::kReference))
7889 << "kPrimInt and kPrimNot have different sizes.";
7890 SetRawInputAt(0, base_address);
7891 SetRawInputAt(1, offset);
7892 }
7893
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007894 bool IsClonable() const override { return true; }
7895 bool CanBeMoved() const override { return true; }
7896 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007897 return true;
7898 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007899 bool IsActualObject() const override { return false; }
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007900
7901 HInstruction* GetBaseAddress() const { return InputAt(0); }
7902 HInstruction* GetOffset() const { return InputAt(1); }
7903
7904 DECLARE_INSTRUCTION(IntermediateAddress);
7905
Artem Serovcced8ba2017-07-19 18:18:09 +01007906 protected:
7907 DEFAULT_COPY_CONSTRUCTOR(IntermediateAddress);
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007908};
7909
7910
Mark Mendell0616ae02015-04-17 12:49:27 -04007911} // namespace art
7912
Aart Bikf8f5a162017-02-06 15:35:29 -08007913#include "nodes_vector.h"
7914
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03007915#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
7916#include "nodes_shared.h"
7917#endif
Shalini Salomi Bodapatidd121f62018-10-26 15:03:53 +05307918#if defined(ART_ENABLE_CODEGEN_x86) || defined(ART_ENABLE_CODEGEN_x86_64)
Mark Mendell0616ae02015-04-17 12:49:27 -04007919#include "nodes_x86.h"
7920#endif
7921
Vladimir Marko0a516052019-10-14 13:00:44 +00007922namespace art {
Mark Mendell0616ae02015-04-17 12:49:27 -04007923
Igor Murashkin6ef45672017-08-08 13:59:55 -07007924class OptimizingCompilerStats;
7925
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007926class HGraphVisitor : public ValueObject {
7927 public:
Igor Murashkin6ef45672017-08-08 13:59:55 -07007928 explicit HGraphVisitor(HGraph* graph, OptimizingCompilerStats* stats = nullptr)
7929 : stats_(stats),
7930 graph_(graph) {}
Dave Allison20dfc792014-06-16 20:44:29 -07007931 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007932
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007933 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007934 virtual void VisitBasicBlock(HBasicBlock* block);
7935
Roland Levillain633021e2014-10-01 14:12:25 +01007936 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007937 void VisitInsertionOrder();
7938
Roland Levillain633021e2014-10-01 14:12:25 +01007939 // Visit the graph following dominator tree reverse post-order.
7940 void VisitReversePostOrder();
7941
Nicolas Geoffray787c3072014-03-17 10:20:19 +00007942 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007943
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007944 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007945#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007946 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
7947
7948 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
7949
7950#undef DECLARE_VISIT_INSTRUCTION
7951
Igor Murashkin6ef45672017-08-08 13:59:55 -07007952 protected:
7953 OptimizingCompilerStats* stats_;
7954
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007955 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07007956 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007957
7958 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
7959};
7960
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007961class HGraphDelegateVisitor : public HGraphVisitor {
7962 public:
Igor Murashkin6ef45672017-08-08 13:59:55 -07007963 explicit HGraphDelegateVisitor(HGraph* graph, OptimizingCompilerStats* stats = nullptr)
7964 : HGraphVisitor(graph, stats) {}
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007965 virtual ~HGraphDelegateVisitor() {}
7966
7967 // Visit functions that delegate to to super class.
7968#define DECLARE_VISIT_INSTRUCTION(name, super) \
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007969 void Visit##name(H##name* instr) override { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007970
7971 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
7972
7973#undef DECLARE_VISIT_INSTRUCTION
7974
7975 private:
7976 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
7977};
7978
Artem Serovcced8ba2017-07-19 18:18:09 +01007979// Create a clone of the instruction, insert it into the graph; replace the old one with a new
7980// and remove the old instruction.
7981HInstruction* ReplaceInstrOrPhiByClone(HInstruction* instr);
7982
7983// Create a clone for each clonable instructions/phis and replace the original with the clone.
7984//
7985// Used for testing individual instruction cloner.
7986class CloneAndReplaceInstructionVisitor : public HGraphDelegateVisitor {
7987 public:
7988 explicit CloneAndReplaceInstructionVisitor(HGraph* graph)
Artem Serov7f4aff62017-06-21 17:02:18 +01007989 : HGraphDelegateVisitor(graph), instr_replaced_by_clones_count_(0) {}
Artem Serovcced8ba2017-07-19 18:18:09 +01007990
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007991 void VisitInstruction(HInstruction* instruction) override {
Artem Serovcced8ba2017-07-19 18:18:09 +01007992 if (instruction->IsClonable()) {
7993 ReplaceInstrOrPhiByClone(instruction);
Artem Serov7f4aff62017-06-21 17:02:18 +01007994 instr_replaced_by_clones_count_++;
Artem Serovcced8ba2017-07-19 18:18:09 +01007995 }
7996 }
7997
Artem Serov7f4aff62017-06-21 17:02:18 +01007998 size_t GetInstrReplacedByClonesCount() const { return instr_replaced_by_clones_count_; }
Artem Serovcced8ba2017-07-19 18:18:09 +01007999
8000 private:
Artem Serov7f4aff62017-06-21 17:02:18 +01008001 size_t instr_replaced_by_clones_count_;
Artem Serovcced8ba2017-07-19 18:18:09 +01008002
8003 DISALLOW_COPY_AND_ASSIGN(CloneAndReplaceInstructionVisitor);
8004};
8005
Nicolas Geoffray82091da2015-01-26 10:02:45 +00008006// Iterator over the blocks that art part of the loop. Includes blocks part
8007// of an inner loop. The order in which the blocks are iterated is on their
8008// block id.
8009class HBlocksInLoopIterator : public ValueObject {
8010 public:
8011 explicit HBlocksInLoopIterator(const HLoopInformation& info)
8012 : blocks_in_loop_(info.GetBlocks()),
8013 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
8014 index_(0) {
8015 if (!blocks_in_loop_.IsBitSet(index_)) {
8016 Advance();
8017 }
8018 }
8019
Vladimir Markofa6b93c2015-09-15 10:15:55 +01008020 bool Done() const { return index_ == blocks_.size(); }
8021 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00008022 void Advance() {
8023 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01008024 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00008025 if (blocks_in_loop_.IsBitSet(index_)) {
8026 break;
8027 }
8028 }
8029 }
8030
8031 private:
8032 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01008033 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00008034 size_t index_;
8035
8036 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
8037};
8038
Mingyao Yang3584bce2015-05-19 16:01:59 -07008039// Iterator over the blocks that art part of the loop. Includes blocks part
8040// of an inner loop. The order in which the blocks are iterated is reverse
8041// post order.
8042class HBlocksInLoopReversePostOrderIterator : public ValueObject {
8043 public:
8044 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
8045 : blocks_in_loop_(info.GetBlocks()),
8046 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
8047 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01008048 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07008049 Advance();
8050 }
8051 }
8052
Vladimir Markofa6b93c2015-09-15 10:15:55 +01008053 bool Done() const { return index_ == blocks_.size(); }
8054 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07008055 void Advance() {
8056 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01008057 for (size_t e = blocks_.size(); index_ < e; ++index_) {
8058 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07008059 break;
8060 }
8061 }
8062 }
8063
8064 private:
8065 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01008066 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07008067 size_t index_;
8068
8069 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
8070};
8071
Aart Bikf3e61ee2017-04-12 17:09:20 -07008072// Returns int64_t value of a properly typed constant.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00008073inline int64_t Int64FromConstant(HConstant* constant) {
David Brazdilc0b601b2016-02-08 14:20:45 +00008074 if (constant->IsIntConstant()) {
8075 return constant->AsIntConstant()->GetValue();
8076 } else if (constant->IsLongConstant()) {
8077 return constant->AsLongConstant()->GetValue();
8078 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00008079 DCHECK(constant->IsNullConstant()) << constant->DebugName();
David Brazdilc0b601b2016-02-08 14:20:45 +00008080 return 0;
8081 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00008082}
8083
Aart Bikf3e61ee2017-04-12 17:09:20 -07008084// Returns true iff instruction is an integral constant (and sets value on success).
8085inline bool IsInt64AndGet(HInstruction* instruction, /*out*/ int64_t* value) {
8086 if (instruction->IsIntConstant()) {
8087 *value = instruction->AsIntConstant()->GetValue();
8088 return true;
8089 } else if (instruction->IsLongConstant()) {
8090 *value = instruction->AsLongConstant()->GetValue();
8091 return true;
8092 } else if (instruction->IsNullConstant()) {
8093 *value = 0;
8094 return true;
8095 }
8096 return false;
8097}
8098
Aart Bik0148de42017-09-05 09:25:01 -07008099// Returns true iff instruction is the given integral constant.
8100inline bool IsInt64Value(HInstruction* instruction, int64_t value) {
8101 int64_t val = 0;
8102 return IsInt64AndGet(instruction, &val) && val == value;
8103}
8104
8105// Returns true iff instruction is a zero bit pattern.
8106inline bool IsZeroBitPattern(HInstruction* instruction) {
8107 return instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern();
8108}
8109
Vladimir Marko6d5b7e32018-05-09 16:52:48 +01008110// Implement HInstruction::Is##type() for concrete instructions.
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008111#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Markoa90dd512018-05-04 15:04:45 +01008112 inline bool HInstruction::Is##type() const { return GetKind() == k##type; }
8113 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
8114#undef INSTRUCTION_TYPE_CHECK
8115
Vladimir Marko6d5b7e32018-05-09 16:52:48 +01008116// Implement HInstruction::Is##type() for abstract instructions.
Vladimir Markoa90dd512018-05-04 15:04:45 +01008117#define INSTRUCTION_TYPE_CHECK_RESULT(type, super) \
8118 std::is_base_of<BaseType, H##type>::value,
8119#define INSTRUCTION_TYPE_CHECK(type, super) \
8120 inline bool HInstruction::Is##type() const { \
8121 DCHECK_LT(GetKind(), kLastInstructionKind); \
8122 using BaseType = H##type; \
8123 static constexpr bool results[] = { \
8124 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK_RESULT) \
8125 }; \
8126 return results[static_cast<size_t>(GetKind())]; \
8127 }
8128
8129 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
8130#undef INSTRUCTION_TYPE_CHECK
Vladimir Marko6d5b7e32018-05-09 16:52:48 +01008131#undef INSTRUCTION_TYPE_CHECK_RESULT
Vladimir Markoa90dd512018-05-04 15:04:45 +01008132
8133#define INSTRUCTION_TYPE_CAST(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008134 inline const H##type* HInstruction::As##type() const { \
8135 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
8136 } \
8137 inline H##type* HInstruction::As##type() { \
8138 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
8139 }
8140
Vladimir Markoa90dd512018-05-04 15:04:45 +01008141 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CAST)
8142#undef INSTRUCTION_TYPE_CAST
8143
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008144
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00008145// Create space in `blocks` for adding `number_of_new_blocks` entries
8146// starting at location `at`. Blocks after `at` are moved accordingly.
8147inline void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks,
8148 size_t number_of_new_blocks,
8149 size_t after) {
8150 DCHECK_LT(after, blocks->size());
8151 size_t old_size = blocks->size();
8152 size_t new_size = old_size + number_of_new_blocks;
8153 blocks->resize(new_size);
8154 std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end());
8155}
8156
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00008157/*
8158 * Hunt "under the hood" of array lengths (leading to array references),
8159 * null checks (also leading to array references), and new arrays
8160 * (leading to the actual length). This makes it more likely related
8161 * instructions become actually comparable.
8162 */
8163inline HInstruction* HuntForDeclaration(HInstruction* instruction) {
8164 while (instruction->IsArrayLength() ||
8165 instruction->IsNullCheck() ||
8166 instruction->IsNewArray()) {
8167 instruction = instruction->IsNewArray()
8168 ? instruction->AsNewArray()->GetLength()
8169 : instruction->InputAt(0);
8170 }
8171 return instruction;
8172}
8173
Artem Serovb47b9782019-12-04 21:02:09 +00008174inline bool IsAddOrSub(const HInstruction* instruction) {
8175 return instruction->IsAdd() || instruction->IsSub();
8176}
8177
Artem Serov21c7e6f2017-07-27 16:04:42 +01008178void RemoveEnvironmentUses(HInstruction* instruction);
8179bool HasEnvironmentUsedByOthers(HInstruction* instruction);
8180void ResetEnvironmentInputRecords(HInstruction* instruction);
8181
Nicolas Geoffray818f2102014-02-18 16:43:35 +00008182} // namespace art
8183
8184#endif // ART_COMPILER_OPTIMIZING_NODES_H_