blob: 0eece84661be5e3960800f6ada83f62d2a4ea54d [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"
Nicolas Geoffray0d60a2b2020-06-17 14:31:56 +010036#include "compilation_kind.h"
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010037#include "data_type.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070038#include "deoptimization_kind.h"
David Sehr9e734c72018-01-04 17:56:19 -080039#include "dex/dex_file.h"
40#include "dex/dex_file_types.h"
David Sehr8c0961f2018-01-23 16:11:38 -080041#include "dex/invoke_type.h"
David Sehr312f3b22018-03-19 08:39:26 -070042#include "dex/method_reference.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000043#include "entrypoints/quick/quick_entrypoints_enum.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000044#include "handle.h"
45#include "handle_scope.h"
Nicolas Geoffray762869d2016-07-15 15:28:35 +010046#include "intrinsics_enum.h"
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010047#include "locations.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000048#include "mirror/class.h"
Orion Hodson18259d72018-04-12 11:18:23 +010049#include "mirror/method_type.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010050#include "offsets.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000051
Vladimir Marko0a516052019-10-14 13:00:44 +000052namespace art {
Nicolas Geoffray818f2102014-02-18 16:43:35 +000053
Vladimir Markoca6fff82017-10-03 14:49:14 +010054class ArenaStack;
David Brazdil1abb4192015-02-17 18:33:36 +000055class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000056class HBasicBlock;
Igor Murashkind01745e2017-04-05 16:40:31 -070057class HConstructorFence;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010058class HCurrentMethod;
David Brazdil8d5b8b22015-03-24 10:51:52 +000059class HDoubleConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010060class HEnvironment;
David Brazdil8d5b8b22015-03-24 10:51:52 +000061class HFloatConstant;
David Brazdilfc6a86a2015-06-26 10:33:45 +000062class HGraphBuilder;
David Brazdil8d5b8b22015-03-24 10:51:52 +000063class HGraphVisitor;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000064class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000065class HIntConstant;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000066class HInvoke;
David Brazdil8d5b8b22015-03-24 10:51:52 +000067class HLongConstant;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +000068class HNullConstant;
Igor Murashkind01745e2017-04-05 16:40:31 -070069class HParameterValue;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010070class HPhi;
Nicolas Geoffray3c049742014-09-24 18:10:46 +010071class HSuspendCheck;
David Brazdilffee3d32015-07-06 11:48:53 +010072class HTryBoundary;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010073class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000074class LocationSummary;
Nicolas Geoffraydb216f42015-05-05 17:02:20 +010075class SlowPathCode;
David Brazdil8d5b8b22015-03-24 10:51:52 +000076class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000077
Mathieu Chartier736b5602015-09-02 14:54:11 -070078namespace mirror {
79class DexCache;
80} // namespace mirror
81
Nicolas Geoffray818f2102014-02-18 16:43:35 +000082static const int kDefaultNumberOfBlocks = 8;
83static const int kDefaultNumberOfSuccessors = 2;
84static const int kDefaultNumberOfPredecessors = 2;
David Brazdilb618ade2015-07-29 10:31:29 +010085static const int kDefaultNumberOfExceptionalPredecessors = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010086static const int kDefaultNumberOfDominatedBlocks = 1;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000087static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000088
Roland Levillain5b5b9312016-03-22 14:57:31 +000089// The maximum (meaningful) distance (31) that can be used in an integer shift/rotate operation.
90static constexpr int32_t kMaxIntShiftDistance = 0x1f;
91// The maximum (meaningful) distance (63) that can be used in a long shift/rotate operation.
92static constexpr int32_t kMaxLongShiftDistance = 0x3f;
Calin Juravle9aec02f2014-11-18 23:06:35 +000093
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010094static constexpr uint32_t kUnknownFieldIndex = static_cast<uint32_t>(-1);
Mingyao Yang8df69d42015-10-22 15:40:58 -070095static constexpr uint16_t kUnknownClassDefIndex = static_cast<uint16_t>(-1);
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010096
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +010097static constexpr InvokeType kInvalidInvokeType = static_cast<InvokeType>(-1);
98
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +060099static constexpr uint32_t kNoDexPc = -1;
100
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100101inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
102 // For the purposes of the compiler, the dex files must actually be the same object
103 // if we want to safely treat them as the same. This is especially important for JIT
104 // as custom class loaders can open the same underlying file (or memory) multiple
105 // times and provide different class resolution but no two class loaders should ever
106 // use the same DexFile object - doing so is an unsupported hack that can lead to
107 // all sorts of weird failures.
108 return &lhs == &rhs;
109}
110
Dave Allison20dfc792014-06-16 20:44:29 -0700111enum IfCondition {
Aart Bike9f37602015-10-09 11:15:55 -0700112 // All types.
113 kCondEQ, // ==
114 kCondNE, // !=
115 // Signed integers and floating-point numbers.
116 kCondLT, // <
117 kCondLE, // <=
118 kCondGT, // >
119 kCondGE, // >=
120 // Unsigned integers.
121 kCondB, // <
122 kCondBE, // <=
123 kCondA, // >
124 kCondAE, // >=
Scott Wakeling2c76e062016-08-31 09:48:54 +0100125 // First and last aliases.
126 kCondFirst = kCondEQ,
127 kCondLast = kCondAE,
Dave Allison20dfc792014-06-16 20:44:29 -0700128};
129
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000130enum GraphAnalysisResult {
David Brazdil86ea7ee2016-02-16 09:26:07 +0000131 kAnalysisSkipped,
David Brazdilbadd8262016-02-02 16:28:56 +0000132 kAnalysisInvalidBytecode,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000133 kAnalysisFailThrowCatchLoop,
134 kAnalysisFailAmbiguousArrayOp,
Nicolas Geoffray0846a8f2018-09-12 15:21:07 +0100135 kAnalysisFailIrreducibleLoopAndStringInit,
Nicolas Geoffray7cfc8f52019-08-07 10:41:09 +0100136 kAnalysisFailPhiEquivalentInOsr,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000137 kAnalysisSuccess,
David Brazdil4833f5a2015-12-16 10:37:39 +0000138};
139
Andreas Gampe9186ced2016-12-12 14:28:21 -0800140template <typename T>
141static inline typename std::make_unsigned<T>::type MakeUnsigned(T x) {
142 return static_cast<typename std::make_unsigned<T>::type>(x);
143}
144
Vladimir Markof9f64412015-09-02 14:05:49 +0100145class HInstructionList : public ValueObject {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100146 public:
147 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
148
149 void AddInstruction(HInstruction* instruction);
150 void RemoveInstruction(HInstruction* instruction);
151
David Brazdilc3d743f2015-04-22 13:40:50 +0100152 // Insert `instruction` before/after an existing instruction `cursor`.
153 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
154 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
155
Roland Levillain6b469232014-09-25 10:10:38 +0100156 // Return true if this list contains `instruction`.
157 bool Contains(HInstruction* instruction) const;
158
Roland Levillainccc07a92014-09-16 14:48:16 +0100159 // Return true if `instruction1` is found before `instruction2` in
160 // this instruction list and false otherwise. Abort if none
161 // of these instructions is found.
162 bool FoundBefore(const HInstruction* instruction1,
163 const HInstruction* instruction2) const;
164
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000165 bool IsEmpty() const { return first_instruction_ == nullptr; }
166 void Clear() { first_instruction_ = last_instruction_ = nullptr; }
167
168 // Update the block of all instructions to be `block`.
169 void SetBlockOfInstructions(HBasicBlock* block) const;
170
171 void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list);
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000172 void AddBefore(HInstruction* cursor, const HInstructionList& instruction_list);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000173 void Add(const HInstructionList& instruction_list);
174
David Brazdil2d7352b2015-04-20 14:52:42 +0100175 // Return the number of instructions in the list. This is an expensive operation.
176 size_t CountSize() const;
177
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100178 private:
179 HInstruction* first_instruction_;
180 HInstruction* last_instruction_;
181
182 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000183 friend class HGraph;
184 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100185 friend class HInstructionIterator;
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +0000186 friend class HInstructionIteratorHandleChanges;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100187 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100188
189 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
190};
191
David Brazdil4833f5a2015-12-16 10:37:39 +0000192class ReferenceTypeInfo : ValueObject {
193 public:
194 typedef Handle<mirror::Class> TypeHandle;
195
Vladimir Markoa1de9182016-02-25 11:37:38 +0000196 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact);
197
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700198 static ReferenceTypeInfo Create(TypeHandle type_handle) REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil94ab38f2016-06-21 17:48:19 +0100199 return Create(type_handle, type_handle->CannotBeAssignedFromOtherTypes());
200 }
201
Vladimir Markoa1de9182016-02-25 11:37:38 +0000202 static ReferenceTypeInfo CreateUnchecked(TypeHandle type_handle, bool is_exact) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000203 return ReferenceTypeInfo(type_handle, is_exact);
204 }
205
206 static ReferenceTypeInfo CreateInvalid() { return ReferenceTypeInfo(); }
207
Vladimir Markof39745e2016-01-26 12:16:55 +0000208 static bool IsValidHandle(TypeHandle handle) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000209 return handle.GetReference() != nullptr;
210 }
211
Vladimir Marko456307a2016-04-19 14:12:13 +0000212 bool IsValid() const {
David Brazdil4833f5a2015-12-16 10:37:39 +0000213 return IsValidHandle(type_handle_);
214 }
215
216 bool IsExact() const { return is_exact_; }
217
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700218 bool IsObjectClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000219 DCHECK(IsValid());
220 return GetTypeHandle()->IsObjectClass();
221 }
222
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700223 bool IsStringClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000224 DCHECK(IsValid());
225 return GetTypeHandle()->IsStringClass();
226 }
227
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700228 bool IsObjectArray() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000229 DCHECK(IsValid());
230 return IsArrayClass() && GetTypeHandle()->GetComponentType()->IsObjectClass();
231 }
232
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700233 bool IsInterface() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000234 DCHECK(IsValid());
235 return GetTypeHandle()->IsInterface();
236 }
237
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700238 bool IsArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000239 DCHECK(IsValid());
240 return GetTypeHandle()->IsArrayClass();
241 }
242
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700243 bool IsPrimitiveArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000244 DCHECK(IsValid());
245 return GetTypeHandle()->IsPrimitiveArray();
246 }
247
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700248 bool IsNonPrimitiveArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000249 DCHECK(IsValid());
250 return GetTypeHandle()->IsArrayClass() && !GetTypeHandle()->IsPrimitiveArray();
251 }
252
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700253 bool CanArrayHold(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000254 DCHECK(IsValid());
255 if (!IsExact()) return false;
256 if (!IsArrayClass()) return false;
257 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(rti.GetTypeHandle().Get());
258 }
259
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700260 bool CanArrayHoldValuesOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000261 DCHECK(IsValid());
262 if (!IsExact()) return false;
263 if (!IsArrayClass()) return false;
264 if (!rti.IsArrayClass()) return false;
265 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(
266 rti.GetTypeHandle()->GetComponentType());
267 }
268
269 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
270
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700271 bool IsSupertypeOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000272 DCHECK(IsValid());
273 DCHECK(rti.IsValid());
274 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
275 }
276
David Brazdil4833f5a2015-12-16 10:37:39 +0000277 // Returns true if the type information provide the same amount of details.
278 // Note that it does not mean that the instructions have the same actual type
279 // (because the type can be the result of a merge).
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700280 bool IsEqual(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000281 if (!IsValid() && !rti.IsValid()) {
282 // Invalid types are equal.
283 return true;
284 }
285 if (!IsValid() || !rti.IsValid()) {
286 // One is valid, the other not.
287 return false;
288 }
289 return IsExact() == rti.IsExact()
290 && GetTypeHandle().Get() == rti.GetTypeHandle().Get();
291 }
292
293 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +0000294 ReferenceTypeInfo() : type_handle_(TypeHandle()), is_exact_(false) {}
295 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact)
296 : type_handle_(type_handle), is_exact_(is_exact) { }
David Brazdil4833f5a2015-12-16 10:37:39 +0000297
298 // The class of the object.
299 TypeHandle type_handle_;
300 // Whether or not the type is exact or a superclass of the actual type.
301 // Whether or not we have any information about this type.
302 bool is_exact_;
303};
304
305std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
306
Vladimir Marko02ca05a2020-05-12 13:58:51 +0100307class HandleCache {
308 public:
309 explicit HandleCache(VariableSizedHandleScope* handles) : handles_(handles) { }
310
311 VariableSizedHandleScope* GetHandles() { return handles_; }
312
313 template <typename T>
314 MutableHandle<T> NewHandle(T* object) REQUIRES_SHARED(Locks::mutator_lock_) {
315 return handles_->NewHandle(object);
316 }
317
318 template <typename T>
319 MutableHandle<T> NewHandle(ObjPtr<T> object) REQUIRES_SHARED(Locks::mutator_lock_) {
320 return handles_->NewHandle(object);
321 }
322
323 ReferenceTypeInfo::TypeHandle GetObjectClassHandle() {
324 return GetRootHandle(ClassRoot::kJavaLangObject, &object_class_handle_);
325 }
326
327 ReferenceTypeInfo::TypeHandle GetClassClassHandle() {
328 return GetRootHandle(ClassRoot::kJavaLangClass, &class_class_handle_);
329 }
330
331 ReferenceTypeInfo::TypeHandle GetMethodHandleClassHandle() {
332 return GetRootHandle(ClassRoot::kJavaLangInvokeMethodHandleImpl, &method_handle_class_handle_);
333 }
334
335 ReferenceTypeInfo::TypeHandle GetMethodTypeClassHandle() {
336 return GetRootHandle(ClassRoot::kJavaLangInvokeMethodType, &method_type_class_handle_);
337 }
338
339 ReferenceTypeInfo::TypeHandle GetStringClassHandle() {
340 return GetRootHandle(ClassRoot::kJavaLangString, &string_class_handle_);
341 }
342
343 ReferenceTypeInfo::TypeHandle GetThrowableClassHandle() {
344 return GetRootHandle(ClassRoot::kJavaLangThrowable, &throwable_class_handle_);
345 }
346
347
348 private:
349 inline ReferenceTypeInfo::TypeHandle GetRootHandle(ClassRoot class_root,
350 ReferenceTypeInfo::TypeHandle* cache) {
351 if (UNLIKELY(!ReferenceTypeInfo::IsValidHandle(*cache))) {
352 *cache = CreateRootHandle(handles_, class_root);
353 }
354 return *cache;
355 }
356
357 static ReferenceTypeInfo::TypeHandle CreateRootHandle(VariableSizedHandleScope* handles,
358 ClassRoot class_root);
359
360 VariableSizedHandleScope* handles_;
361
362 ReferenceTypeInfo::TypeHandle object_class_handle_;
363 ReferenceTypeInfo::TypeHandle class_class_handle_;
364 ReferenceTypeInfo::TypeHandle method_handle_class_handle_;
365 ReferenceTypeInfo::TypeHandle method_type_class_handle_;
366 ReferenceTypeInfo::TypeHandle string_class_handle_;
367 ReferenceTypeInfo::TypeHandle throwable_class_handle_;
368};
369
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000370// Control-flow graph of a method. Contains a list of basic blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100371class HGraph : public ArenaObject<kArenaAllocGraph> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000372 public:
Vladimir Markoca6fff82017-10-03 14:49:14 +0100373 HGraph(ArenaAllocator* allocator,
374 ArenaStack* arena_stack,
Vladimir Marko02ca05a2020-05-12 13:58:51 +0100375 VariableSizedHandleScope* handles,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100376 const DexFile& dex_file,
377 uint32_t method_idx,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700378 InstructionSet instruction_set,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100379 InvokeType invoke_type = kInvalidInvokeType,
Hans Boehm206348c2018-12-05 11:11:33 -0800380 bool dead_reference_safe = false,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100381 bool debuggable = false,
Nicolas Geoffray0d60a2b2020-06-17 14:31:56 +0100382 CompilationKind compilation_kind = CompilationKind::kOptimized,
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),
Nicolas Geoffray0d60a2b2020-06-17 14:31:56 +0100418 compilation_kind_(compilation_kind),
Vladimir Marko695348f2020-05-19 14:42:02 +0100419 cha_single_implementation_list_(allocator->Adapter(kArenaAllocCHA)) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100420 blocks_.reserve(kDefaultNumberOfBlocks);
421 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000422
Vladimir Markoca6fff82017-10-03 14:49:14 +0100423 ArenaAllocator* GetAllocator() const { return allocator_; }
424 ArenaStack* GetArenaStack() const { return arena_stack_; }
Vladimir Marko02ca05a2020-05-12 13:58:51 +0100425
426 HandleCache* GetHandleCache() { return &handle_cache_; }
427
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100428 const ArenaVector<HBasicBlock*>& GetBlocks() const { return blocks_; }
429
David Brazdil69ba7b72015-06-23 18:27:30 +0100430 bool IsInSsaForm() const { return in_ssa_form_; }
David Brazdilbadd8262016-02-02 16:28:56 +0000431 void SetInSsaForm() { in_ssa_form_ = true; }
David Brazdil69ba7b72015-06-23 18:27:30 +0100432
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000433 HBasicBlock* GetEntryBlock() const { return entry_block_; }
434 HBasicBlock* GetExitBlock() const { return exit_block_; }
David Brazdilc7af85d2015-05-26 12:05:55 +0100435 bool HasExitBlock() const { return exit_block_ != nullptr; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000436
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000437 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
438 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000439
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000440 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100441
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100442 void ComputeDominanceInformation();
443 void ClearDominanceInformation();
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000444 void ClearLoopInformation();
445 void FindBackEdges(ArenaBitVector* visited);
446 GraphAnalysisResult BuildDominatorTree();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100447 void SimplifyCFG();
David Brazdilffee3d32015-07-06 11:48:53 +0100448 void SimplifyCatchBlocks();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000449
David Brazdil4833f5a2015-12-16 10:37:39 +0000450 // Analyze all natural loops in this graph. Returns a code specifying that it
451 // was successful or the reason for failure. The method will fail if a loop
David Brazdil4833f5a2015-12-16 10:37:39 +0000452 // is a throw-catch loop, i.e. the header is a catch block.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000453 GraphAnalysisResult AnalyzeLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100454
David Brazdilffee3d32015-07-06 11:48:53 +0100455 // Iterate over blocks to compute try block membership. Needs reverse post
456 // order and loop information.
457 void ComputeTryBlockInformation();
458
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000459 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
Nicolas Geoffray55bd7492016-02-16 15:37:12 +0000460 // Returns the instruction to replace the invoke expression or null if the
461 // invoke is for a void method. Note that the caller is responsible for replacing
462 // and removing the invoke instruction.
Calin Juravle2e768302015-07-28 14:41:11 +0000463 HInstruction* InlineInto(HGraph* outer_graph, HInvoke* invoke);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000464
Nicolas Geoffraya1d8ddf2016-02-29 11:46:58 +0000465 // Update the loop and try membership of `block`, which was spawned from `reference`.
466 // In case `reference` is a back edge, `replace_if_back_edge` notifies whether `block`
467 // should be the new back edge.
468 void UpdateLoopAndTryInformationOfNewBlock(HBasicBlock* block,
469 HBasicBlock* reference,
470 bool replace_if_back_edge);
471
Mingyao Yang3584bce2015-05-19 16:01:59 -0700472 // Need to add a couple of blocks to test if the loop body is entered and
473 // put deoptimization instructions, etc.
474 void TransformLoopHeaderForBCE(HBasicBlock* header);
475
Aart Bikf8f5a162017-02-06 15:35:29 -0800476 // Adds a new loop directly after the loop with the given header and exit.
477 // Returns the new preheader.
478 HBasicBlock* TransformLoopForVectorization(HBasicBlock* header,
479 HBasicBlock* body,
480 HBasicBlock* exit);
481
David Brazdil8a7c0fe2015-11-02 20:24:55 +0000482 // Removes `block` from the graph. Assumes `block` has been disconnected from
483 // other blocks and has no instructions or phis.
484 void DeleteDeadEmptyBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000485
David Brazdilfc6a86a2015-06-26 10:33:45 +0000486 // Splits the edge between `block` and `successor` while preserving the
487 // indices in the predecessor/successor lists. If there are multiple edges
488 // between the blocks, the lowest indices are used.
489 // Returns the new block which is empty and has the same dex pc as `successor`.
490 HBasicBlock* SplitEdge(HBasicBlock* block, HBasicBlock* successor);
491
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100492 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
Artem Serovc73ee372017-07-31 15:08:40 +0100493 void OrderLoopHeaderPredecessors(HBasicBlock* header);
Artem Serov09faaea2017-12-07 14:36:01 +0000494
495 // Transform a loop into a format with a single preheader.
496 //
497 // Each phi in the header should be split: original one in the header should only hold
498 // inputs reachable from the back edges and a single input from the preheader. The newly created
499 // phi in the preheader should collate the inputs from the original multiple incoming blocks.
500 //
501 // Loops in the graph typically have a single preheader, so this method is used to "repair" loops
502 // that no longer have this property.
503 void TransformLoopToSinglePreheaderFormat(HBasicBlock* header);
504
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100505 void SimplifyLoop(HBasicBlock* header);
506
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000507 int32_t GetNextInstructionId() {
Nicolas Geoffrayc9c31042017-06-29 14:04:16 +0100508 CHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000509 return current_instruction_id_++;
510 }
511
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000512 int32_t GetCurrentInstructionId() const {
513 return current_instruction_id_;
514 }
515
516 void SetCurrentInstructionId(int32_t id) {
Nicolas Geoffrayc9c31042017-06-29 14:04:16 +0100517 CHECK_GE(id, current_instruction_id_);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000518 current_instruction_id_ = id;
519 }
520
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100521 uint16_t GetMaximumNumberOfOutVRegs() const {
522 return maximum_number_of_out_vregs_;
523 }
524
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000525 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
526 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100527 }
528
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100529 void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) {
530 maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value);
531 }
532
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000533 void UpdateTemporariesVRegSlots(size_t slots) {
534 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100535 }
536
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000537 size_t GetTemporariesVRegSlots() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100538 DCHECK(!in_ssa_form_);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000539 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100540 }
541
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100542 void SetNumberOfVRegs(uint16_t number_of_vregs) {
543 number_of_vregs_ = number_of_vregs;
544 }
545
546 uint16_t GetNumberOfVRegs() const {
547 return number_of_vregs_;
548 }
549
550 void SetNumberOfInVRegs(uint16_t value) {
551 number_of_in_vregs_ = value;
552 }
553
David Brazdildee58d62016-04-07 09:54:26 +0000554 uint16_t GetNumberOfInVRegs() const {
555 return number_of_in_vregs_;
556 }
557
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100558 uint16_t GetNumberOfLocalVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100559 DCHECK(!in_ssa_form_);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100560 return number_of_vregs_ - number_of_in_vregs_;
561 }
562
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100563 const ArenaVector<HBasicBlock*>& GetReversePostOrder() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100564 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100565 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100566
Eric Holk1868de92020-02-12 09:10:21 -0800567 ArrayRef<HBasicBlock* const> GetReversePostOrderSkipEntryBlock() const {
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100568 DCHECK(GetReversePostOrder()[0] == entry_block_);
569 return ArrayRef<HBasicBlock* const>(GetReversePostOrder()).SubArray(1);
570 }
571
572 IterationRange<ArenaVector<HBasicBlock*>::const_reverse_iterator> GetPostOrder() const {
573 return ReverseRange(GetReversePostOrder());
574 }
575
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100576 const ArenaVector<HBasicBlock*>& GetLinearOrder() const {
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100577 return linear_order_;
578 }
579
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100580 IterationRange<ArenaVector<HBasicBlock*>::const_reverse_iterator> GetLinearPostOrder() const {
581 return ReverseRange(GetLinearOrder());
582 }
583
Mark Mendell1152c922015-04-24 17:06:35 -0400584 bool HasBoundsChecks() const {
585 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800586 }
587
Mark Mendell1152c922015-04-24 17:06:35 -0400588 void SetHasBoundsChecks(bool value) {
589 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800590 }
591
Hans Boehm206348c2018-12-05 11:11:33 -0800592 // Is the code known to be robust against eliminating dead references
593 // and the effects of early finalization?
594 bool IsDeadReferenceSafe() const { return dead_reference_safe_; }
595
596 void MarkDeadReferenceUnsafe() { dead_reference_safe_ = false; }
597
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000598 bool IsDebuggable() const { return debuggable_; }
599
David Brazdil8d5b8b22015-03-24 10:51:52 +0000600 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000601 // already, it is created and inserted into the graph. This method is only for
602 // integral types.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100603 HConstant* GetConstant(DataType::Type type, int64_t value, uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000604
605 // TODO: This is problematic for the consistency of reference type propagation
606 // because it can be created anytime after the pass and thus it will be left
607 // with an invalid type.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600608 HNullConstant* GetNullConstant(uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000609
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600610 HIntConstant* GetIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc) {
611 return CreateConstant(value, &cached_int_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000612 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600613 HLongConstant* GetLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc) {
614 return CreateConstant(value, &cached_long_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000615 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600616 HFloatConstant* GetFloatConstant(float value, uint32_t dex_pc = kNoDexPc) {
617 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000618 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600619 HDoubleConstant* GetDoubleConstant(double value, uint32_t dex_pc = kNoDexPc) {
620 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000621 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000622
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100623 HCurrentMethod* GetCurrentMethod();
624
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100625 const DexFile& GetDexFile() const {
626 return dex_file_;
627 }
628
629 uint32_t GetMethodIdx() const {
630 return method_idx_;
631 }
632
Igor Murashkind01745e2017-04-05 16:40:31 -0700633 // Get the method name (without the signature), e.g. "<init>"
634 const char* GetMethodName() const;
635
636 // Get the pretty method name (class + name + optionally signature).
637 std::string PrettyMethod(bool with_signature = true) const;
638
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100639 InvokeType GetInvokeType() const {
640 return invoke_type_;
641 }
642
Mark Mendellc4701932015-04-10 13:18:51 -0400643 InstructionSet GetInstructionSet() const {
644 return instruction_set_;
645 }
646
Nicolas Geoffray0d60a2b2020-06-17 14:31:56 +0100647 bool IsCompilingOsr() const { return compilation_kind_ == CompilationKind::kOsr; }
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000648
Nicolas Geoffray0d60a2b2020-06-17 14:31:56 +0100649 bool IsCompilingBaseline() const { return compilation_kind_ == CompilationKind::kBaseline; }
650
651 CompilationKind GetCompilationKind() const { return compilation_kind_; }
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +0000652
Mingyao Yang063fc772016-08-02 11:02:54 -0700653 ArenaSet<ArtMethod*>& GetCHASingleImplementationList() {
654 return cha_single_implementation_list_;
655 }
656
657 void AddCHASingleImplementationDependency(ArtMethod* method) {
658 cha_single_implementation_list_.insert(method);
659 }
660
661 bool HasShouldDeoptimizeFlag() const {
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800662 return number_of_cha_guards_ != 0;
Mingyao Yang063fc772016-08-02 11:02:54 -0700663 }
664
David Brazdil77a48ae2015-09-15 12:34:04 +0000665 bool HasTryCatch() const { return has_try_catch_; }
666 void SetHasTryCatch(bool value) { has_try_catch_ = value; }
David Brazdilbbd733e2015-08-18 17:48:17 +0100667
Artem Serov2808be82018-12-20 19:15:11 +0000668 bool HasMonitorOperations() const { return has_monitor_operations_; }
669 void SetHasMonitorOperations(bool value) { has_monitor_operations_ = value; }
670
Aart Bikb13c65b2017-03-21 20:14:07 -0700671 bool HasSIMD() const { return has_simd_; }
672 void SetHasSIMD(bool value) { has_simd_ = value; }
673
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800674 bool HasLoops() const { return has_loops_; }
675 void SetHasLoops(bool value) { has_loops_ = value; }
676
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000677 bool HasIrreducibleLoops() const { return has_irreducible_loops_; }
678 void SetHasIrreducibleLoops(bool value) { has_irreducible_loops_ = value; }
679
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100680 ArtMethod* GetArtMethod() const { return art_method_; }
681 void SetArtMethod(ArtMethod* method) { art_method_ = method; }
682
Roland Levillain5e8d5f02016-10-18 18:03:43 +0100683 // Returns an instruction with the opposite Boolean value from 'cond'.
Mark Mendellf6529172015-11-17 11:16:56 -0500684 // The instruction has been inserted into the graph, either as a constant, or
685 // before cursor.
686 HInstruction* InsertOppositeCondition(HInstruction* cond, HInstruction* cursor);
687
Vladimir Marko02ca05a2020-05-12 13:58:51 +0100688 ReferenceTypeInfo GetInexactObjectRti() {
689 return ReferenceTypeInfo::Create(handle_cache_.GetObjectClassHandle(), /* is_exact= */ false);
690 }
Nicolas Geoffray18401b72016-03-11 13:35:51 +0000691
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800692 uint32_t GetNumberOfCHAGuards() { return number_of_cha_guards_; }
693 void SetNumberOfCHAGuards(uint32_t num) { number_of_cha_guards_ = num; }
694 void IncrementNumberOfCHAGuards() { number_of_cha_guards_++; }
695
David Brazdil2d7352b2015-04-20 14:52:42 +0100696 private:
Roland Levillainfc600dc2014-12-02 17:16:31 +0000697 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100698 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000699
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000700 template <class InstructionType, typename ValueType>
701 InstructionType* CreateConstant(ValueType value,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600702 ArenaSafeMap<ValueType, InstructionType*>* cache,
703 uint32_t dex_pc = kNoDexPc) {
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000704 // Try to find an existing constant of the given value.
705 InstructionType* constant = nullptr;
706 auto cached_constant = cache->find(value);
707 if (cached_constant != cache->end()) {
708 constant = cached_constant->second;
709 }
710
711 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100712 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000713 if (constant == nullptr || constant->GetBlock() == nullptr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100714 constant = new (allocator_) InstructionType(value, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000715 cache->Overwrite(value, constant);
716 InsertConstant(constant);
717 }
718 return constant;
719 }
720
David Brazdil8d5b8b22015-03-24 10:51:52 +0000721 void InsertConstant(HConstant* instruction);
722
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000723 // Cache a float constant into the graph. This method should only be
724 // called by the SsaBuilder when creating "equivalent" instructions.
725 void CacheFloatConstant(HFloatConstant* constant);
726
727 // See CacheFloatConstant comment.
728 void CacheDoubleConstant(HDoubleConstant* constant);
729
Vladimir Markoca6fff82017-10-03 14:49:14 +0100730 ArenaAllocator* const allocator_;
731 ArenaStack* const arena_stack_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000732
Vladimir Marko02ca05a2020-05-12 13:58:51 +0100733 HandleCache handle_cache_;
734
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000735 // List of blocks in insertion order.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100736 ArenaVector<HBasicBlock*> blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000737
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100738 // List of blocks to perform a reverse post order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100739 ArenaVector<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000740
Aart Bik281c6812016-08-26 11:31:48 -0700741 // List of blocks to perform a linear order tree traversal. Unlike the reverse
742 // post order, this order is not incrementally kept up-to-date.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100743 ArenaVector<HBasicBlock*> linear_order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100744
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000745 HBasicBlock* entry_block_;
746 HBasicBlock* exit_block_;
747
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100748 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100749 uint16_t maximum_number_of_out_vregs_;
750
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100751 // The number of virtual registers in this method. Contains the parameters.
752 uint16_t number_of_vregs_;
753
754 // The number of virtual registers used by parameters of this method.
755 uint16_t number_of_in_vregs_;
756
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000757 // Number of vreg size slots that the temporaries use (used in baseline compiler).
758 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100759
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800760 // Flag whether there are bounds checks in the graph. We can skip
761 // BCE if it's false. It's only best effort to keep it up to date in
762 // the presence of code elimination so there might be false positives.
Mark Mendell1152c922015-04-24 17:06:35 -0400763 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800764
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800765 // Flag whether there are try/catch blocks in the graph. We will skip
766 // try/catch-related passes if it's false. It's only best effort to keep
767 // it up to date in the presence of code elimination so there might be
768 // false positives.
David Brazdil77a48ae2015-09-15 12:34:04 +0000769 bool has_try_catch_;
770
Artem Serov2808be82018-12-20 19:15:11 +0000771 // Flag whether there are any HMonitorOperation in the graph. If yes this will mandate
772 // DexRegisterMap to be present to allow deadlock analysis for non-debuggable code.
773 bool has_monitor_operations_;
774
Aart Bikb13c65b2017-03-21 20:14:07 -0700775 // Flag whether SIMD instructions appear in the graph. If true, the
776 // code generators may have to be more careful spilling the wider
777 // contents of SIMD registers.
778 bool has_simd_;
779
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800780 // Flag whether there are any loops in the graph. We can skip loop
781 // optimization if it's false. It's only best effort to keep it up
782 // to date in the presence of code elimination so there might be false
783 // positives.
784 bool has_loops_;
785
786 // Flag whether there are any irreducible loops in the graph. It's only
787 // best effort to keep it up to date in the presence of code elimination
788 // so there might be false positives.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000789 bool has_irreducible_loops_;
790
Hans Boehm206348c2018-12-05 11:11:33 -0800791 // Is the code known to be robust against eliminating dead references
792 // and the effects of early finalization? If false, dead reference variables
793 // are kept if they might be visible to the garbage collector.
794 // Currently this means that the class was declared to be dead-reference-safe,
795 // the method accesses no reachability-sensitive fields or data, and the same
796 // is true for any methods that were inlined into the current one.
797 bool dead_reference_safe_;
798
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000799 // Indicates whether the graph should be compiled in a way that
800 // ensures full debuggability. If false, we can apply more
801 // aggressive optimizations that may limit the level of debugging.
802 const bool debuggable_;
803
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000804 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000805 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000806
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100807 // The dex file from which the method is from.
808 const DexFile& dex_file_;
809
810 // The method index in the dex file.
811 const uint32_t method_idx_;
812
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100813 // If inlined, this encodes how the callee is being invoked.
814 const InvokeType invoke_type_;
815
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100816 // Whether the graph has been transformed to SSA form. Only used
817 // in debug mode to ensure we are not using properties only valid
818 // for non-SSA form (like the number of temporaries).
819 bool in_ssa_form_;
820
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800821 // Number of CHA guards in the graph. Used to short-circuit the
822 // CHA guard optimization pass when there is no CHA guard left.
823 uint32_t number_of_cha_guards_;
824
Mathieu Chartiere401d142015-04-22 13:56:20 -0700825 const InstructionSet instruction_set_;
826
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000827 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000828 HNullConstant* cached_null_constant_;
829 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000830 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000831 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000832 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000833
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100834 HCurrentMethod* cached_current_method_;
835
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100836 // The ArtMethod this graph is for. Note that for AOT, it may be null,
837 // for example for methods whose declaring class could not be resolved
838 // (such as when the superclass could not be found).
839 ArtMethod* art_method_;
840
Nicolas Geoffray0d60a2b2020-06-17 14:31:56 +0100841 // How we are compiling the graph: either optimized, osr, or baseline.
842 // For osr, we will make all loops seen as irreducible and emit special
843 // stack maps to mark compiled code entries which the interpreter can
844 // directly jump to.
845 const CompilationKind compilation_kind_;
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +0000846
Mingyao Yang063fc772016-08-02 11:02:54 -0700847 // List of methods that are assumed to have single implementation.
848 ArenaSet<ArtMethod*> cha_single_implementation_list_;
849
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000850 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100851 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000852 friend class HInliner; // For the reverse post order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000853 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000854 DISALLOW_COPY_AND_ASSIGN(HGraph);
855};
856
Vladimir Markof9f64412015-09-02 14:05:49 +0100857class HLoopInformation : public ArenaObject<kArenaAllocLoopInfo> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000858 public:
859 HLoopInformation(HBasicBlock* header, HGraph* graph)
860 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100861 suspend_check_(nullptr),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000862 irreducible_(false),
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100863 contains_irreducible_loop_(false),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100864 back_edges_(graph->GetAllocator()->Adapter(kArenaAllocLoopInfoBackEdges)),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100865 // Make bit vector growable, as the number of blocks may change.
Vladimir Markoca6fff82017-10-03 14:49:14 +0100866 blocks_(graph->GetAllocator(),
867 graph->GetBlocks().size(),
868 true,
869 kArenaAllocLoopInfoBackEdges) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100870 back_edges_.reserve(kDefaultNumberOfBackEdges);
871 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100872
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000873 bool IsIrreducible() const { return irreducible_; }
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100874 bool ContainsIrreducibleLoop() const { return contains_irreducible_loop_; }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000875
876 void Dump(std::ostream& os);
877
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100878 HBasicBlock* GetHeader() const {
879 return header_;
880 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000881
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000882 void SetHeader(HBasicBlock* block) {
883 header_ = block;
884 }
885
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100886 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
887 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
888 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
889
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000890 void AddBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100891 back_edges_.push_back(back_edge);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000892 }
893
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100894 void RemoveBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100895 RemoveElement(back_edges_, back_edge);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100896 }
897
David Brazdil46e2a392015-03-16 17:31:52 +0000898 bool IsBackEdge(const HBasicBlock& block) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100899 return ContainsElement(back_edges_, &block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100900 }
901
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000902 size_t NumberOfBackEdges() const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100903 return back_edges_.size();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000904 }
905
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100906 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100907
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100908 const ArenaVector<HBasicBlock*>& GetBackEdges() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100909 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100910 }
911
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100912 // Returns the lifetime position of the back edge that has the
913 // greatest lifetime position.
914 size_t GetLifetimeEnd() const;
915
916 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100917 ReplaceElement(back_edges_, existing, new_back_edge);
Nicolas Geoffray57902602015-04-21 14:28:41 +0100918 }
919
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000920 // Finds blocks that are part of this loop.
921 void Populate();
David Brazdila4b8c212015-05-07 09:59:30 +0100922
Artem Serov7f4aff62017-06-21 17:02:18 +0100923 // Updates blocks population of the loop and all of its outer' ones recursively after the
924 // population of the inner loop is updated.
925 void PopulateInnerLoopUpwards(HLoopInformation* inner_loop);
926
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100927 // Returns whether this loop information contains `block`.
928 // Note that this loop information *must* be populated before entering this function.
929 bool Contains(const HBasicBlock& block) const;
930
931 // Returns whether this loop information is an inner loop of `other`.
932 // Note that `other` *must* be populated before entering this function.
933 bool IsIn(const HLoopInformation& other) const;
934
Mingyao Yang4b467ed2015-11-19 17:04:22 -0800935 // Returns true if instruction is not defined within this loop.
936 bool IsDefinedOutOfTheLoop(HInstruction* instruction) const;
Aart Bik73f1f3b2015-10-28 15:28:08 -0700937
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100938 const ArenaBitVector& GetBlocks() const { return blocks_; }
939
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000940 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000941 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000942
Nicolas Geoffray788f2f02016-01-22 12:41:38 +0000943 void ClearAllBlocks() {
944 blocks_.ClearAllBits();
945 }
946
David Brazdil3f4a5222016-05-06 12:46:21 +0100947 bool HasBackEdgeNotDominatedByHeader() const;
948
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100949 bool IsPopulated() const {
950 return blocks_.GetHighestBitSet() != -1;
951 }
952
Anton Shaminf89381f2016-05-16 16:44:13 +0600953 bool DominatesAllBackEdges(HBasicBlock* block);
954
David Sehrc757dec2016-11-04 15:48:34 -0700955 bool HasExitEdge() const;
956
Artem Serov7f4aff62017-06-21 17:02:18 +0100957 // Resets back edge and blocks-in-loop data.
958 void ResetBasicBlockData() {
959 back_edges_.clear();
960 ClearAllBlocks();
961 }
962
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000963 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100964 // Internal recursive implementation of `Populate`.
965 void PopulateRecursive(HBasicBlock* block);
David Brazdilc2e8af92016-04-05 17:15:19 +0100966 void PopulateIrreducibleRecursive(HBasicBlock* block, ArenaBitVector* finalized);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100967
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000968 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100969 HSuspendCheck* suspend_check_;
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000970 bool irreducible_;
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100971 bool contains_irreducible_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100972 ArenaVector<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100973 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000974
975 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
976};
977
David Brazdilec16f792015-08-19 15:04:01 +0100978// Stores try/catch information for basic blocks.
979// Note that HGraph is constructed so that catch blocks cannot simultaneously
980// be try blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100981class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> {
David Brazdilec16f792015-08-19 15:04:01 +0100982 public:
983 // Try block information constructor.
984 explicit TryCatchInformation(const HTryBoundary& try_entry)
985 : try_entry_(&try_entry),
986 catch_dex_file_(nullptr),
Nicolas Geoffrayebc3b252019-01-09 10:47:02 +0000987 catch_type_index_(dex::TypeIndex::Invalid()) {
David Brazdilec16f792015-08-19 15:04:01 +0100988 DCHECK(try_entry_ != nullptr);
989 }
990
991 // Catch block information constructor.
Andreas Gampea5b09a62016-11-17 15:21:22 -0800992 TryCatchInformation(dex::TypeIndex catch_type_index, const DexFile& dex_file)
David Brazdilec16f792015-08-19 15:04:01 +0100993 : try_entry_(nullptr),
994 catch_dex_file_(&dex_file),
995 catch_type_index_(catch_type_index) {}
996
997 bool IsTryBlock() const { return try_entry_ != nullptr; }
998
999 const HTryBoundary& GetTryEntry() const {
1000 DCHECK(IsTryBlock());
1001 return *try_entry_;
1002 }
1003
1004 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
1005
Nicolas Geoffrayebc3b252019-01-09 10:47:02 +00001006 bool IsValidTypeIndex() const {
David Brazdilec16f792015-08-19 15:04:01 +01001007 DCHECK(IsCatchBlock());
Nicolas Geoffrayebc3b252019-01-09 10:47:02 +00001008 return catch_type_index_.IsValid();
David Brazdilec16f792015-08-19 15:04:01 +01001009 }
1010
Andreas Gampea5b09a62016-11-17 15:21:22 -08001011 dex::TypeIndex GetCatchTypeIndex() const {
David Brazdilec16f792015-08-19 15:04:01 +01001012 DCHECK(IsCatchBlock());
1013 return catch_type_index_;
1014 }
1015
1016 const DexFile& GetCatchDexFile() const {
1017 DCHECK(IsCatchBlock());
1018 return *catch_dex_file_;
1019 }
1020
Nicolas Geoffrayebc3b252019-01-09 10:47:02 +00001021 void SetInvalidTypeIndex() {
1022 catch_type_index_ = dex::TypeIndex::Invalid();
1023 }
1024
David Brazdilec16f792015-08-19 15:04:01 +01001025 private:
1026 // One of possibly several TryBoundary instructions entering the block's try.
1027 // Only set for try blocks.
1028 const HTryBoundary* try_entry_;
1029
1030 // Exception type information. Only set for catch blocks.
1031 const DexFile* catch_dex_file_;
Nicolas Geoffrayebc3b252019-01-09 10:47:02 +00001032 dex::TypeIndex catch_type_index_;
David Brazdilec16f792015-08-19 15:04:01 +01001033};
1034
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001035static constexpr size_t kNoLifetime = -1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001036static constexpr uint32_t kInvalidBlockId = static_cast<uint32_t>(-1);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001037
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001038// A block in a method. Contains the list of instructions represented
1039// as a double linked list. Each block knows its predecessors and
1040// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001041
Vladimir Markof9f64412015-09-02 14:05:49 +01001042class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001043 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -07001044 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001045 : graph_(graph),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001046 predecessors_(graph->GetAllocator()->Adapter(kArenaAllocPredecessors)),
1047 successors_(graph->GetAllocator()->Adapter(kArenaAllocSuccessors)),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001048 loop_information_(nullptr),
1049 dominator_(nullptr),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001050 dominated_blocks_(graph->GetAllocator()->Adapter(kArenaAllocDominated)),
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001051 block_id_(kInvalidBlockId),
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001052 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001053 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001054 lifetime_end_(kNoLifetime),
Vladimir Marko60584552015-09-03 13:35:12 +00001055 try_catch_information_(nullptr) {
1056 predecessors_.reserve(kDefaultNumberOfPredecessors);
1057 successors_.reserve(kDefaultNumberOfSuccessors);
1058 dominated_blocks_.reserve(kDefaultNumberOfDominatedBlocks);
1059 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001060
Vladimir Marko60584552015-09-03 13:35:12 +00001061 const ArenaVector<HBasicBlock*>& GetPredecessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001062 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001063 }
1064
Vladimir Marko60584552015-09-03 13:35:12 +00001065 const ArenaVector<HBasicBlock*>& GetSuccessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001066 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001067 }
1068
David Brazdild26a4112015-11-10 11:07:31 +00001069 ArrayRef<HBasicBlock* const> GetNormalSuccessors() const;
1070 ArrayRef<HBasicBlock* const> GetExceptionalSuccessors() const;
1071
Vladimir Marko60584552015-09-03 13:35:12 +00001072 bool HasSuccessor(const HBasicBlock* block, size_t start_from = 0u) {
1073 return ContainsElement(successors_, block, start_from);
1074 }
1075
1076 const ArenaVector<HBasicBlock*>& GetDominatedBlocks() const {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001077 return dominated_blocks_;
1078 }
1079
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001080 bool IsEntryBlock() const {
1081 return graph_->GetEntryBlock() == this;
1082 }
1083
1084 bool IsExitBlock() const {
1085 return graph_->GetExitBlock() == this;
1086 }
1087
David Brazdil46e2a392015-03-16 17:31:52 +00001088 bool IsSingleGoto() const;
Mads Ager16e52892017-07-14 13:11:37 +02001089 bool IsSingleReturn() const;
Mingyao Yang46721ef2017-10-05 14:45:17 -07001090 bool IsSingleReturnOrReturnVoidAllowingPhis() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +00001091 bool IsSingleTryBoundary() const;
1092
1093 // Returns true if this block emits nothing but a jump.
1094 bool IsSingleJump() const {
1095 HLoopInformation* loop_info = GetLoopInformation();
1096 return (IsSingleGoto() || IsSingleTryBoundary())
1097 // Back edges generate a suspend check.
1098 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
1099 }
David Brazdil46e2a392015-03-16 17:31:52 +00001100
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001101 void AddBackEdge(HBasicBlock* back_edge) {
1102 if (loop_information_ == nullptr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001103 loop_information_ = new (graph_->GetAllocator()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001104 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001105 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001106 loop_information_->AddBackEdge(back_edge);
1107 }
1108
Artem Serov7f4aff62017-06-21 17:02:18 +01001109 // Registers a back edge; if the block was not a loop header before the call associates a newly
1110 // created loop info with it.
1111 //
1112 // Used in SuperblockCloner to preserve LoopInformation object instead of reseting loop
1113 // info for all blocks during back edges recalculation.
1114 void AddBackEdgeWhileUpdating(HBasicBlock* back_edge) {
1115 if (loop_information_ == nullptr || loop_information_->GetHeader() != this) {
1116 loop_information_ = new (graph_->GetAllocator()) HLoopInformation(this, graph_);
1117 }
1118 loop_information_->AddBackEdge(back_edge);
1119 }
1120
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001121 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001122 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001123
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001124 uint32_t GetBlockId() const { return block_id_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001125 void SetBlockId(int id) { block_id_ = id; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001126 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001127
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001128 HBasicBlock* GetDominator() const { return dominator_; }
1129 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Vladimir Marko60584552015-09-03 13:35:12 +00001130 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.push_back(block); }
1131
1132 void RemoveDominatedBlock(HBasicBlock* block) {
1133 RemoveElement(dominated_blocks_, block);
Vladimir Marko91e11c02015-09-02 17:03:22 +01001134 }
Vladimir Marko60584552015-09-03 13:35:12 +00001135
1136 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
1137 ReplaceElement(dominated_blocks_, existing, new_block);
1138 }
1139
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +01001140 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001141
1142 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +01001143 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001144 }
1145
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001146 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
1147 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +01001148 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001149 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +01001150 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
1151 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001152
Nicolas Geoffray09aa1472016-01-19 10:52:54 +00001153 HInstruction* GetFirstInstructionDisregardMoves() const;
1154
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001155 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001156 successors_.push_back(block);
1157 block->predecessors_.push_back(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001158 }
1159
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001160 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
1161 size_t successor_index = GetSuccessorIndexOf(existing);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001162 existing->RemovePredecessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +00001163 new_block->predecessors_.push_back(this);
1164 successors_[successor_index] = new_block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001165 }
1166
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001167 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
1168 size_t predecessor_index = GetPredecessorIndexOf(existing);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001169 existing->RemoveSuccessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +00001170 new_block->successors_.push_back(this);
1171 predecessors_[predecessor_index] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001172 }
1173
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001174 // Insert `this` between `predecessor` and `successor. This method
Alex Light1e52a072019-06-25 09:12:04 -07001175 // preserves the indices, and will update the first edge found between
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001176 // `predecessor` and `successor`.
1177 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
1178 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001179 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko60584552015-09-03 13:35:12 +00001180 successor->predecessors_[predecessor_index] = this;
1181 predecessor->successors_[successor_index] = this;
1182 successors_.push_back(successor);
1183 predecessors_.push_back(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001184 }
1185
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001186 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001187 predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001188 }
1189
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001190 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001191 successors_.erase(successors_.begin() + GetSuccessorIndexOf(block));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001192 }
1193
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001194 void ClearAllPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001195 predecessors_.clear();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001196 }
1197
1198 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001199 predecessors_.push_back(block);
1200 block->successors_.push_back(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001201 }
1202
Nicolas Geoffray604c6e42014-09-17 12:08:44 +01001203 void SwapPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001204 DCHECK_EQ(predecessors_.size(), 2u);
1205 std::swap(predecessors_[0], predecessors_[1]);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +01001206 }
1207
David Brazdil769c9e52015-04-27 13:54:09 +01001208 void SwapSuccessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001209 DCHECK_EQ(successors_.size(), 2u);
1210 std::swap(successors_[0], successors_[1]);
David Brazdil769c9e52015-04-27 13:54:09 +01001211 }
1212
David Brazdilfc6a86a2015-06-26 10:33:45 +00001213 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko60584552015-09-03 13:35:12 +00001214 return IndexOfElement(predecessors_, predecessor);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001215 }
1216
David Brazdilfc6a86a2015-06-26 10:33:45 +00001217 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko60584552015-09-03 13:35:12 +00001218 return IndexOfElement(successors_, successor);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001219 }
1220
David Brazdilfc6a86a2015-06-26 10:33:45 +00001221 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00001222 DCHECK_EQ(GetPredecessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +01001223 return GetPredecessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +00001224 }
1225
1226 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00001227 DCHECK_EQ(GetSuccessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +01001228 return GetSuccessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +00001229 }
1230
1231 // Returns whether the first occurrence of `predecessor` in the list of
1232 // predecessors is at index `idx`.
1233 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01001234 DCHECK_EQ(GetPredecessors()[idx], predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +00001235 return GetPredecessorIndexOf(predecessor) == idx;
1236 }
1237
David Brazdild7558da2015-09-22 13:04:14 +01001238 // Create a new block between this block and its predecessors. The new block
1239 // is added to the graph, all predecessor edges are relinked to it and an edge
1240 // is created to `this`. Returns the new empty block. Reverse post order or
1241 // loop and try/catch information are not updated.
1242 HBasicBlock* CreateImmediateDominator();
1243
David Brazdilfc6a86a2015-06-26 10:33:45 +00001244 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +01001245 // created, latter block. Note that this method will add the block to the
1246 // graph, create a Goto at the end of the former block and will create an edge
1247 // between the blocks. It will not, however, update the reverse post order or
David Brazdild7558da2015-09-22 13:04:14 +01001248 // loop and try/catch information.
David Brazdilfc6a86a2015-06-26 10:33:45 +00001249 HBasicBlock* SplitBefore(HInstruction* cursor);
1250
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001251 // Split the block into two blocks just before `cursor`. Returns the newly
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001252 // created block. Note that this method just updates raw block information,
1253 // like predecessors, successors, dominators, and instruction list. It does not
1254 // update the graph, reverse post order, loop information, nor make sure the
1255 // blocks are consistent (for example ending with a control flow instruction).
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001256 HBasicBlock* SplitBeforeForInlining(HInstruction* cursor);
1257
1258 // Similar to `SplitBeforeForInlining` but does it after `cursor`.
1259 HBasicBlock* SplitAfterForInlining(HInstruction* cursor);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001260
1261 // Merge `other` at the end of `this`. Successors and dominated blocks of
1262 // `other` are changed to be successors and dominated blocks of `this`. Note
1263 // that this method does not update the graph, reverse post order, loop
1264 // information, nor make sure the blocks are consistent (for example ending
1265 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +01001266 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001267
1268 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
1269 // of `this` are moved to `other`.
1270 // Note that this method does not update the graph, reverse post order, loop
1271 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +00001272 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001273 void ReplaceWith(HBasicBlock* other);
1274
Aart Bik6b69e0a2017-01-11 10:20:43 -08001275 // Merges the instructions of `other` at the end of `this`.
1276 void MergeInstructionsWith(HBasicBlock* other);
1277
David Brazdil2d7352b2015-04-20 14:52:42 +01001278 // Merge `other` at the end of `this`. This method updates loops, reverse post
1279 // order, links to predecessors, successors, dominators and deletes the block
1280 // from the graph. The two blocks must be successive, i.e. `this` the only
1281 // predecessor of `other` and vice versa.
1282 void MergeWith(HBasicBlock* other);
1283
1284 // Disconnects `this` from all its predecessors, successors and dominator,
1285 // removes it from all loops it is included in and eventually from the graph.
1286 // The block must not dominate any other block. Predecessors and successors
1287 // are safely updated.
1288 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +00001289
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001290 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001291 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001292 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001293 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Artem Serovcced8ba2017-07-19 18:18:09 +01001294 // Replace phi `initial` with `replacement` within this block.
1295 void ReplaceAndRemovePhiWith(HPhi* initial, HPhi* replacement);
Roland Levillainccc07a92014-09-16 14:48:16 +01001296 // Replace instruction `initial` with `replacement` within this block.
1297 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
1298 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001299 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001300 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +00001301 // RemoveInstruction and RemovePhi delete a given instruction from the respective
1302 // instruction list. With 'ensure_safety' set to true, it verifies that the
1303 // instruction is not in use and removes it from the use lists of its inputs.
1304 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
1305 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +01001306 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001307
1308 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +01001309 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001310 }
1311
Roland Levillain6b879dd2014-09-22 17:13:44 +01001312 bool IsLoopPreHeaderFirstPredecessor() const {
1313 DCHECK(IsLoopHeader());
Vladimir Markoec7802a2015-10-01 20:57:57 +01001314 return GetPredecessors()[0] == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +01001315 }
1316
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001317 bool IsFirstPredecessorBackEdge() const {
1318 DCHECK(IsLoopHeader());
1319 return GetLoopInformation()->IsBackEdge(*GetPredecessors()[0]);
1320 }
1321
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001322 HLoopInformation* GetLoopInformation() const {
1323 return loop_information_;
1324 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001325
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001326 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001327 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001328 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001329 void SetInLoop(HLoopInformation* info) {
1330 if (IsLoopHeader()) {
1331 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +01001332 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001333 loop_information_ = info;
1334 } else if (loop_information_->Contains(*info->GetHeader())) {
1335 // Block is currently part of an outer loop. Make it part of this inner loop.
1336 // Note that a non loop header having a loop information means this loop information
1337 // has already been populated
1338 loop_information_ = info;
1339 } else {
1340 // Block is part of an inner loop. Do not update the loop information.
1341 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
1342 // at this point, because this method is being called while populating `info`.
1343 }
1344 }
1345
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001346 // Raw update of the loop information.
1347 void SetLoopInformation(HLoopInformation* info) {
1348 loop_information_ = info;
1349 }
1350
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001351 bool IsInLoop() const { return loop_information_ != nullptr; }
1352
David Brazdilec16f792015-08-19 15:04:01 +01001353 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
1354
1355 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
1356 try_catch_information_ = try_catch_information;
1357 }
1358
1359 bool IsTryBlock() const {
1360 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
1361 }
1362
1363 bool IsCatchBlock() const {
1364 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
1365 }
David Brazdilffee3d32015-07-06 11:48:53 +01001366
1367 // Returns the try entry that this block's successors should have. They will
1368 // be in the same try, unless the block ends in a try boundary. In that case,
1369 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +01001370 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001371
Aart Bik75ff2c92018-04-21 01:28:11 +00001372 bool HasThrowingInstructions() const;
1373
David Brazdila4b8c212015-05-07 09:59:30 +01001374 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001375 bool Dominates(HBasicBlock* block) const;
1376
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001377 size_t GetLifetimeStart() const { return lifetime_start_; }
1378 size_t GetLifetimeEnd() const { return lifetime_end_; }
1379
1380 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
1381 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
1382
David Brazdil8d5b8b22015-03-24 10:51:52 +00001383 bool EndsWithControlFlowInstruction() const;
Aart Bik4dc09e72018-05-11 14:40:31 -07001384 bool EndsWithReturn() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001385 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001386 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001387 bool HasSinglePhi() const;
1388
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001389 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001390 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +00001391 ArenaVector<HBasicBlock*> predecessors_;
1392 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001393 HInstructionList instructions_;
1394 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001395 HLoopInformation* loop_information_;
1396 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +00001397 ArenaVector<HBasicBlock*> dominated_blocks_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001398 uint32_t block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001399 // The dex program counter of the first instruction of this block.
1400 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001401 size_t lifetime_start_;
1402 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +01001403 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +01001404
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001405 friend class HGraph;
1406 friend class HInstruction;
1407
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001408 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
1409};
1410
David Brazdilb2bd1c52015-03-25 11:17:37 +00001411// Iterates over the LoopInformation of all loops which contain 'block'
1412// from the innermost to the outermost.
1413class HLoopInformationOutwardIterator : public ValueObject {
1414 public:
1415 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
1416 : current_(block.GetLoopInformation()) {}
1417
1418 bool Done() const { return current_ == nullptr; }
1419
1420 void Advance() {
1421 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +01001422 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +00001423 }
1424
1425 HLoopInformation* Current() const {
1426 DCHECK(!Done());
1427 return current_;
1428 }
1429
1430 private:
1431 HLoopInformation* current_;
1432
1433 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
1434};
1435
Artem Serov1a719e42019-07-18 14:24:55 +01001436#define FOR_EACH_CONCRETE_INSTRUCTION_SCALAR_COMMON(M) \
Aart Bike9f37602015-10-09 11:15:55 -07001437 M(Above, Condition) \
1438 M(AboveOrEqual, Condition) \
Aart Bik3dad3412018-02-28 12:01:46 -08001439 M(Abs, UnaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001440 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001441 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001442 M(ArrayGet, Instruction) \
1443 M(ArrayLength, Instruction) \
1444 M(ArraySet, Instruction) \
Aart Bike9f37602015-10-09 11:15:55 -07001445 M(Below, Condition) \
1446 M(BelowOrEqual, Condition) \
David Brazdil66d126e2015-04-03 16:02:44 +01001447 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001448 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001449 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001450 M(CheckCast, Instruction) \
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00001451 M(ClassTableGet, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001452 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001453 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001454 M(Compare, BinaryOperation) \
Igor Murashkind01745e2017-04-05 16:40:31 -07001455 M(ConstructorFence, Instruction) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001456 M(CurrentMethod, Instruction) \
Mingyao Yang063fc772016-08-02 11:02:54 -07001457 M(ShouldDeoptimizeFlag, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001458 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001459 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001460 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001461 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001462 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001463 M(Exit, Instruction) \
1464 M(FloatConstant, Constant) \
1465 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001466 M(GreaterThan, Condition) \
1467 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001468 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001469 M(InstanceFieldGet, Instruction) \
1470 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001471 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001472 M(IntConstant, Constant) \
xueliang.zhonge0eb4832017-10-30 13:43:14 +00001473 M(IntermediateAddress, Instruction) \
Calin Juravle175dc732015-08-25 15:42:32 +01001474 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001475 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001476 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001477 M(InvokeVirtual, Invoke) \
Orion Hodsonac141392017-01-13 11:53:47 +00001478 M(InvokePolymorphic, Invoke) \
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001479 M(InvokeCustom, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001480 M(LessThan, Condition) \
1481 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001482 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001483 M(LoadException, Instruction) \
Orion Hodsondbaa5c72018-05-10 08:22:46 +01001484 M(LoadMethodHandle, Instruction) \
Orion Hodson18259d72018-04-12 11:18:23 +01001485 M(LoadMethodType, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001486 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001487 M(LongConstant, Constant) \
Aart Bik1f8d51b2018-02-15 10:42:37 -08001488 M(Max, Instruction) \
Calin Juravle27df7582015-04-17 19:12:31 +01001489 M(MemoryBarrier, Instruction) \
Aart Bik1f8d51b2018-02-15 10:42:37 -08001490 M(Min, BinaryOperation) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001491 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001492 M(Mul, BinaryOperation) \
David Srbecky0cf44932015-12-09 14:09:59 +00001493 M(NativeDebugInfo, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001494 M(Neg, UnaryOperation) \
1495 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001496 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001497 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001498 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001499 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001500 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001501 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001502 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001503 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001504 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001505 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001506 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001507 M(Return, Instruction) \
1508 M(ReturnVoid, Instruction) \
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001509 M(Ror, BinaryOperation) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001510 M(Shl, BinaryOperation) \
1511 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001512 M(StaticFieldGet, Instruction) \
1513 M(StaticFieldSet, Instruction) \
Vladimir Marko552a1342017-10-31 10:56:47 +00001514 M(StringBuilderAppend, Instruction) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001515 M(UnresolvedInstanceFieldGet, Instruction) \
1516 M(UnresolvedInstanceFieldSet, Instruction) \
1517 M(UnresolvedStaticFieldGet, Instruction) \
1518 M(UnresolvedStaticFieldSet, Instruction) \
David Brazdil74eb1b22015-12-14 11:44:01 +00001519 M(Select, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001520 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001521 M(SuspendCheck, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001522 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001523 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001524 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001525 M(UShr, BinaryOperation) \
Artem Serov1a719e42019-07-18 14:24:55 +01001526 M(Xor, BinaryOperation)
1527
1528#define FOR_EACH_CONCRETE_INSTRUCTION_VECTOR_COMMON(M) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001529 M(VecReplicateScalar, VecUnaryOperation) \
Aart Bik0148de42017-09-05 09:25:01 -07001530 M(VecExtractScalar, VecUnaryOperation) \
1531 M(VecReduce, VecUnaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001532 M(VecCnv, VecUnaryOperation) \
1533 M(VecNeg, VecUnaryOperation) \
Aart Bik6daebeb2017-04-03 14:35:41 -07001534 M(VecAbs, VecUnaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001535 M(VecNot, VecUnaryOperation) \
1536 M(VecAdd, VecBinaryOperation) \
Aart Bikf3e61ee2017-04-12 17:09:20 -07001537 M(VecHalvingAdd, VecBinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001538 M(VecSub, VecBinaryOperation) \
1539 M(VecMul, VecBinaryOperation) \
1540 M(VecDiv, VecBinaryOperation) \
Aart Bikf3e61ee2017-04-12 17:09:20 -07001541 M(VecMin, VecBinaryOperation) \
1542 M(VecMax, VecBinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001543 M(VecAnd, VecBinaryOperation) \
1544 M(VecAndNot, VecBinaryOperation) \
1545 M(VecOr, VecBinaryOperation) \
1546 M(VecXor, VecBinaryOperation) \
Aart Bik29aa0822018-03-08 11:28:00 -08001547 M(VecSaturationAdd, VecBinaryOperation) \
1548 M(VecSaturationSub, VecBinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001549 M(VecShl, VecBinaryOperation) \
1550 M(VecShr, VecBinaryOperation) \
1551 M(VecUShr, VecBinaryOperation) \
Aart Bik8de59162017-04-21 09:42:01 -07001552 M(VecSetScalars, VecOperation) \
Artem Serovf34dd202017-04-10 17:41:46 +01001553 M(VecMultiplyAccumulate, VecOperation) \
Aart Bikdbbac8f2017-09-01 13:06:08 -07001554 M(VecSADAccumulate, VecOperation) \
Artem Serovaaac0e32018-08-07 00:52:22 +01001555 M(VecDotProd, VecOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001556 M(VecLoad, VecMemoryOperation) \
1557 M(VecStore, VecMemoryOperation) \
Artem Serov07718842020-02-24 18:51:42 +00001558 M(VecPredSetAll, VecPredSetOperation) \
1559 M(VecPredWhile, VecPredSetOperation) \
1560 M(VecPredCondition, VecOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001561
Artem Serov1a719e42019-07-18 14:24:55 +01001562#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
1563 FOR_EACH_CONCRETE_INSTRUCTION_SCALAR_COMMON(M) \
1564 FOR_EACH_CONCRETE_INSTRUCTION_VECTOR_COMMON(M)
1565
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001566/*
1567 * Instructions, shared across several (not all) architectures.
1568 */
1569#if !defined(ART_ENABLE_CODEGEN_arm) && !defined(ART_ENABLE_CODEGEN_arm64)
1570#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M)
1571#else
1572#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Artem Serov7fc63502016-02-09 17:15:29 +00001573 M(BitwiseNegatedRight, Instruction) \
Anton Kirilov74234da2017-01-13 14:42:47 +00001574 M(DataProcWithShifterOp, Instruction) \
Artem Serov328429f2016-07-06 16:23:04 +01001575 M(MultiplyAccumulate, Instruction) \
Artem Serove1811ed2017-04-27 16:50:47 +01001576 M(IntermediateAddressIndex, Instruction)
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001577#endif
1578
Alexandre Ramesef20f712015-06-09 10:29:30 +01001579#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
1580
1581#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
1582
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001583#ifndef ART_ENABLE_CODEGEN_x86
1584#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1585#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001586#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1587 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001588 M(X86LoadFromConstantTable, Instruction) \
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001589 M(X86FPNeg, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001590 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001591#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001592
Shalini Salomi Bodapatidd121f62018-10-26 15:03:53 +05301593#if defined(ART_ENABLE_CODEGEN_x86) || defined(ART_ENABLE_CODEGEN_x86_64)
1594#define FOR_EACH_CONCRETE_INSTRUCTION_X86_COMMON(M) \
Shalini Salomi Bodapati81d15be2019-05-30 11:00:42 +05301595 M(X86AndNot, Instruction) \
Shalini Salomi Bodapatib45a4352019-07-10 16:09:41 +05301596 M(X86MaskOrResetLeastSetBit, Instruction)
Shalini Salomi Bodapatidd121f62018-10-26 15:03:53 +05301597#else
1598#define FOR_EACH_CONCRETE_INSTRUCTION_X86_COMMON(M)
1599#endif
1600
Alexandre Ramesef20f712015-06-09 10:29:30 +01001601#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1602
1603#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1604 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001605 FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001606 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1607 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
1608 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
Shalini Salomi Bodapatidd121f62018-10-26 15:03:53 +05301609 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M) \
1610 FOR_EACH_CONCRETE_INSTRUCTION_X86_COMMON(M)
Alexandre Ramesef20f712015-06-09 10:29:30 +01001611
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001612#define FOR_EACH_ABSTRACT_INSTRUCTION(M) \
1613 M(Condition, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001614 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001615 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001616 M(BinaryOperation, Instruction) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001617 M(Invoke, Instruction) \
1618 M(VecOperation, Instruction) \
1619 M(VecUnaryOperation, VecOperation) \
1620 M(VecBinaryOperation, VecOperation) \
Artem Serov07718842020-02-24 18:51:42 +00001621 M(VecMemoryOperation, VecOperation) \
1622 M(VecPredSetOperation, VecOperation)
Dave Allison20dfc792014-06-16 20:44:29 -07001623
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001624#define FOR_EACH_INSTRUCTION(M) \
1625 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1626 FOR_EACH_ABSTRACT_INSTRUCTION(M)
1627
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001628#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001629FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1630#undef FORWARD_DECLARATION
1631
Vladimir Marko372f10e2016-05-17 16:30:10 +01001632#define DECLARE_INSTRUCTION(type) \
Artem Serovcced8ba2017-07-19 18:18:09 +01001633 private: \
1634 H##type& operator=(const H##type&) = delete; \
1635 public: \
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01001636 const char* DebugName() const override { return #type; } \
1637 HInstruction* Clone(ArenaAllocator* arena) const override { \
Artem Serovcced8ba2017-07-19 18:18:09 +01001638 DCHECK(IsClonable()); \
1639 return new (arena) H##type(*this->As##type()); \
1640 } \
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01001641 void Accept(HGraphVisitor* visitor) override
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001642
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001643#define DECLARE_ABSTRACT_INSTRUCTION(type) \
Artem Serovcced8ba2017-07-19 18:18:09 +01001644 private: \
1645 H##type& operator=(const H##type&) = delete; \
Vladimir Markoa90dd512018-05-04 15:04:45 +01001646 public:
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001647
Artem Serovcced8ba2017-07-19 18:18:09 +01001648#define DEFAULT_COPY_CONSTRUCTOR(type) \
1649 explicit H##type(const H##type& other) = default;
1650
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001651template <typename T>
Vladimir Marko82b07402017-03-01 19:02:04 +00001652class HUseListNode : public ArenaObject<kArenaAllocUseListNode>,
1653 public IntrusiveForwardListNode<HUseListNode<T>> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001654 public:
Igor Murashkind01745e2017-04-05 16:40:31 -07001655 // Get the instruction which has this use as one of the inputs.
David Brazdiled596192015-01-23 10:39:45 +00001656 T GetUser() const { return user_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07001657 // Get the position of the input record that this use corresponds to.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001658 size_t GetIndex() const { return index_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07001659 // Set the position of the input record that this use corresponds to.
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001660 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001661
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001662 private:
David Brazdiled596192015-01-23 10:39:45 +00001663 HUseListNode(T user, size_t index)
Vladimir Marko46817b82016-03-29 12:21:58 +01001664 : user_(user), index_(index) {}
David Brazdiled596192015-01-23 10:39:45 +00001665
1666 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001667 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001668
Vladimir Marko46817b82016-03-29 12:21:58 +01001669 friend class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001670
1671 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1672};
1673
David Brazdiled596192015-01-23 10:39:45 +00001674template <typename T>
Vladimir Marko46817b82016-03-29 12:21:58 +01001675using HUseList = IntrusiveForwardList<HUseListNode<T>>;
David Brazdiled596192015-01-23 10:39:45 +00001676
David Brazdil1abb4192015-02-17 18:33:36 +00001677// This class is used by HEnvironment and HInstruction classes to record the
1678// instructions they use and pointers to the corresponding HUseListNodes kept
1679// by the used instructions.
1680template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001681class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001682 public:
Vladimir Marko46817b82016-03-29 12:21:58 +01001683 HUserRecord() : instruction_(nullptr), before_use_node_() {}
1684 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), before_use_node_() {}
David Brazdil1abb4192015-02-17 18:33:36 +00001685
Vladimir Marko46817b82016-03-29 12:21:58 +01001686 HUserRecord(const HUserRecord<T>& old_record, typename HUseList<T>::iterator before_use_node)
1687 : HUserRecord(old_record.instruction_, before_use_node) {}
1688 HUserRecord(HInstruction* instruction, typename HUseList<T>::iterator before_use_node)
1689 : instruction_(instruction), before_use_node_(before_use_node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001690 DCHECK(instruction_ != nullptr);
David Brazdil1abb4192015-02-17 18:33:36 +00001691 }
1692
1693 HInstruction* GetInstruction() const { return instruction_; }
Vladimir Marko46817b82016-03-29 12:21:58 +01001694 typename HUseList<T>::iterator GetBeforeUseNode() const { return before_use_node_; }
1695 typename HUseList<T>::iterator GetUseNode() const { return ++GetBeforeUseNode(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001696
1697 private:
1698 // Instruction used by the user.
1699 HInstruction* instruction_;
1700
Vladimir Marko46817b82016-03-29 12:21:58 +01001701 // Iterator before the corresponding entry in the use list kept by 'instruction_'.
1702 typename HUseList<T>::iterator before_use_node_;
David Brazdil1abb4192015-02-17 18:33:36 +00001703};
1704
Vladimir Markoe9004912016-06-16 16:50:52 +01001705// Helper class that extracts the input instruction from HUserRecord<HInstruction*>.
1706// This is used for HInstruction::GetInputs() to return a container wrapper providing
1707// HInstruction* values even though the underlying container has HUserRecord<>s.
1708struct HInputExtractor {
1709 HInstruction* operator()(HUserRecord<HInstruction*>& record) const {
1710 return record.GetInstruction();
1711 }
1712 const HInstruction* operator()(const HUserRecord<HInstruction*>& record) const {
1713 return record.GetInstruction();
1714 }
1715};
1716
1717using HInputsRef = TransformArrayRef<HUserRecord<HInstruction*>, HInputExtractor>;
1718using HConstInputsRef = TransformArrayRef<const HUserRecord<HInstruction*>, HInputExtractor>;
1719
Aart Bik854a02b2015-07-14 16:07:00 -07001720/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001721 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001722 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001723 * For write/read dependences on fields/arrays, the dependence analysis uses
1724 * type disambiguation (e.g. a float field write cannot modify the value of an
1725 * integer field read) and the access type (e.g. a reference array write cannot
1726 * modify the value of a reference field read [although it may modify the
1727 * reference fetch prior to reading the field, which is represented by its own
1728 * write/read dependence]). The analysis makes conservative points-to
1729 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1730 * the same, and any reference read depends on any reference read without
1731 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001732 *
Artem Serovd1aa7d02018-06-22 11:35:46 +01001733 * kDependsOnGCBit is defined in the following way: instructions with kDependsOnGCBit must not be
1734 * alive across the point where garbage collection might happen.
1735 *
1736 * Note: Instructions with kCanTriggerGCBit do not depend on each other.
1737 *
1738 * kCanTriggerGCBit must be used for instructions for which GC might happen on the path across
1739 * those instructions from the compiler perspective (between this instruction and the next one
1740 * in the IR).
1741 *
1742 * Note: Instructions which can cause GC only on a fatal slow path do not need
1743 * kCanTriggerGCBit as the execution never returns to the instruction next to the exceptional
1744 * one. However the execution may return to compiled code if there is a catch block in the
1745 * current method; for this purpose the TryBoundary exit instruction has kCanTriggerGCBit
1746 * set.
1747 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001748 * The internal representation uses 38-bit and is described in the table below.
1749 * The first line indicates the side effect, and for field/array accesses the
1750 * second line indicates the type of the access (in the order of the
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001751 * DataType::Type enum).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001752 * The two numbered lines below indicate the bit position in the bitfield (read
1753 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001754 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001755 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1756 * +-------------+---------+---------+--------------+---------+---------+
1757 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1758 * | 3 |333333322|222222221| 1 |111111110|000000000|
1759 * | 7 |654321098|765432109| 8 |765432109|876543210|
1760 *
1761 * Note that, to ease the implementation, 'changes' bits are least significant
1762 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001763 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001764class SideEffects : public ValueObject {
1765 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001766 SideEffects() : flags_(0) {}
1767
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001768 static SideEffects None() {
1769 return SideEffects(0);
1770 }
1771
1772 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001773 return SideEffects(kAllChangeBits | kAllDependOnBits);
1774 }
1775
1776 static SideEffects AllChanges() {
1777 return SideEffects(kAllChangeBits);
1778 }
1779
1780 static SideEffects AllDependencies() {
1781 return SideEffects(kAllDependOnBits);
1782 }
1783
1784 static SideEffects AllExceptGCDependency() {
1785 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1786 }
1787
1788 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001789 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001790 }
1791
Aart Bik34c3ba92015-07-20 14:08:59 -07001792 static SideEffects AllWrites() {
1793 return SideEffects(kAllWrites);
1794 }
1795
1796 static SideEffects AllReads() {
1797 return SideEffects(kAllReads);
1798 }
1799
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001800 static SideEffects FieldWriteOfType(DataType::Type type, bool is_volatile) {
Aart Bik34c3ba92015-07-20 14:08:59 -07001801 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001802 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001803 : SideEffects(TypeFlag(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001804 }
1805
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001806 static SideEffects ArrayWriteOfType(DataType::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001807 return SideEffects(TypeFlag(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001808 }
1809
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001810 static SideEffects FieldReadOfType(DataType::Type type, bool is_volatile) {
Aart Bik34c3ba92015-07-20 14:08:59 -07001811 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001812 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001813 : SideEffects(TypeFlag(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001814 }
1815
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001816 static SideEffects ArrayReadOfType(DataType::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001817 return SideEffects(TypeFlag(type, kArrayReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001818 }
1819
Artem Serovd1aa7d02018-06-22 11:35:46 +01001820 // Returns whether GC might happen across this instruction from the compiler perspective so
1821 // the next instruction in the IR would see that.
1822 //
1823 // See the SideEffect class comments.
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001824 static SideEffects CanTriggerGC() {
1825 return SideEffects(1ULL << kCanTriggerGCBit);
1826 }
1827
Artem Serovd1aa7d02018-06-22 11:35:46 +01001828 // Returns whether the instruction must not be alive across a GC point.
1829 //
1830 // See the SideEffect class comments.
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001831 static SideEffects DependsOnGC() {
1832 return SideEffects(1ULL << kDependsOnGCBit);
1833 }
1834
Aart Bik854a02b2015-07-14 16:07:00 -07001835 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001836 SideEffects Union(SideEffects other) const {
1837 return SideEffects(flags_ | other.flags_);
1838 }
1839
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001840 SideEffects Exclusion(SideEffects other) const {
1841 return SideEffects(flags_ & ~other.flags_);
1842 }
1843
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001844 void Add(SideEffects other) {
1845 flags_ |= other.flags_;
1846 }
1847
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001848 bool Includes(SideEffects other) const {
1849 return (other.flags_ & flags_) == other.flags_;
1850 }
1851
1852 bool HasSideEffects() const {
1853 return (flags_ & kAllChangeBits);
1854 }
1855
1856 bool HasDependencies() const {
1857 return (flags_ & kAllDependOnBits);
1858 }
1859
1860 // Returns true if there are no side effects or dependencies.
1861 bool DoesNothing() const {
1862 return flags_ == 0;
1863 }
1864
Aart Bik854a02b2015-07-14 16:07:00 -07001865 // Returns true if something is written.
1866 bool DoesAnyWrite() const {
1867 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001868 }
1869
Aart Bik854a02b2015-07-14 16:07:00 -07001870 // Returns true if something is read.
1871 bool DoesAnyRead() const {
1872 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001873 }
1874
Aart Bik854a02b2015-07-14 16:07:00 -07001875 // Returns true if potentially everything is written and read
1876 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001877 bool DoesAllReadWrite() const {
1878 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1879 }
1880
Aart Bik854a02b2015-07-14 16:07:00 -07001881 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001882 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001883 }
1884
Roland Levillain0d5a2812015-11-13 10:07:31 +00001885 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001886 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001887 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1888 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001889 }
1890
1891 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001892 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001893 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001894 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001895 for (int s = kLastBit; s >= 0; s--) {
1896 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1897 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1898 // This is a bit for the GC side effect.
1899 if (current_bit_is_set) {
1900 flags += "GC";
1901 }
Aart Bik854a02b2015-07-14 16:07:00 -07001902 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001903 } else {
1904 // This is a bit for the array/field analysis.
1905 // The underscore character stands for the 'can trigger GC' bit.
1906 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1907 if (current_bit_is_set) {
1908 flags += kDebug[s];
1909 }
1910 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1911 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1912 flags += "|";
1913 }
1914 }
Aart Bik854a02b2015-07-14 16:07:00 -07001915 }
1916 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001917 }
1918
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001919 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001920
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001921 private:
1922 static constexpr int kFieldArrayAnalysisBits = 9;
1923
1924 static constexpr int kFieldWriteOffset = 0;
1925 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1926 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1927 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1928
1929 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1930
1931 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1932 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1933 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1934 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1935
1936 static constexpr int kLastBit = kDependsOnGCBit;
1937 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1938
1939 // Aliases.
1940
1941 static_assert(kChangeBits == kDependOnBits,
1942 "the 'change' bits should match the 'depend on' bits.");
1943
1944 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1945 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1946 static constexpr uint64_t kAllWrites =
1947 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1948 static constexpr uint64_t kAllReads =
1949 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001950
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001951 // Translates type to bit flag. The type must correspond to a Java type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001952 static uint64_t TypeFlag(DataType::Type type, int offset) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001953 int shift;
1954 switch (type) {
1955 case DataType::Type::kReference: shift = 0; break;
1956 case DataType::Type::kBool: shift = 1; break;
1957 case DataType::Type::kInt8: shift = 2; break;
1958 case DataType::Type::kUint16: shift = 3; break;
1959 case DataType::Type::kInt16: shift = 4; break;
1960 case DataType::Type::kInt32: shift = 5; break;
1961 case DataType::Type::kInt64: shift = 6; break;
1962 case DataType::Type::kFloat32: shift = 7; break;
1963 case DataType::Type::kFloat64: shift = 8; break;
1964 default:
1965 LOG(FATAL) << "Unexpected data type " << type;
1966 UNREACHABLE();
1967 }
Aart Bik854a02b2015-07-14 16:07:00 -07001968 DCHECK_LE(kFieldWriteOffset, shift);
1969 DCHECK_LT(shift, kArrayWriteOffset);
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001970 return UINT64_C(1) << (shift + offset);
Aart Bik854a02b2015-07-14 16:07:00 -07001971 }
1972
1973 // Private constructor on direct flags value.
1974 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1975
1976 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001977};
1978
David Brazdiled596192015-01-23 10:39:45 +00001979// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001980class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001981 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001982 ALWAYS_INLINE HEnvironment(ArenaAllocator* allocator,
Mingyao Yang01b47b02017-02-03 12:09:57 -08001983 size_t number_of_vregs,
1984 ArtMethod* method,
1985 uint32_t dex_pc,
1986 HInstruction* holder)
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001987 : vregs_(number_of_vregs, allocator->Adapter(kArenaAllocEnvironmentVRegs)),
1988 locations_(allocator->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001989 parent_(nullptr),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001990 method_(method),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001991 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001992 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001993 }
1994
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001995 ALWAYS_INLINE HEnvironment(ArenaAllocator* allocator,
1996 const HEnvironment& to_copy,
1997 HInstruction* holder)
1998 : HEnvironment(allocator,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001999 to_copy.Size(),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00002000 to_copy.GetMethod(),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002001 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002002 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002003
Vladimir Markoec32f642017-06-02 10:51:55 +01002004 void AllocateLocations() {
2005 DCHECK(locations_.empty());
2006 locations_.resize(vregs_.size());
2007 }
2008
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002009 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002010 if (parent_ != nullptr) {
2011 parent_->SetAndCopyParentChain(allocator, parent);
2012 } else {
2013 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
2014 parent_->CopyFrom(parent);
2015 if (parent->GetParent() != nullptr) {
2016 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
2017 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002018 }
David Brazdiled596192015-01-23 10:39:45 +00002019 }
2020
Vladimir Marko69d310e2017-10-09 14:12:23 +01002021 void CopyFrom(ArrayRef<HInstruction* const> locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01002022 void CopyFrom(HEnvironment* environment);
2023
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002024 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
2025 // input to the loop phi instead. This is for inserting instructions that
2026 // require an environment (like HDeoptimization) in the loop pre-header.
2027 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00002028
2029 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01002030 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00002031 }
2032
2033 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01002034 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00002035 }
2036
David Brazdil1abb4192015-02-17 18:33:36 +00002037 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00002038
Artem Serovca210e32017-12-15 13:43:20 +00002039 // Replaces the input at the position 'index' with the replacement; the replacement and old
2040 // input instructions' env_uses_ lists are adjusted. The function works similar to
2041 // HInstruction::ReplaceInput.
2042 void ReplaceInput(HInstruction* replacement, size_t index);
2043
Vladimir Markofa6b93c2015-09-15 10:15:55 +01002044 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00002045
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002046 HEnvironment* GetParent() const { return parent_; }
2047
2048 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01002049 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002050 }
2051
2052 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01002053 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002054 }
2055
2056 uint32_t GetDexPc() const {
2057 return dex_pc_;
2058 }
2059
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00002060 ArtMethod* GetMethod() const {
2061 return method_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002062 }
2063
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002064 HInstruction* GetHolder() const {
2065 return holder_;
2066 }
2067
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00002068
2069 bool IsFromInlinedInvoke() const {
2070 return GetParent() != nullptr;
2071 }
2072
David Brazdiled596192015-01-23 10:39:45 +00002073 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01002074 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
2075 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002076 HEnvironment* parent_;
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00002077 ArtMethod* method_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002078 const uint32_t dex_pc_;
David Brazdiled596192015-01-23 10:39:45 +00002079
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01002080 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002081 HInstruction* const holder_;
2082
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01002083 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00002084
2085 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
2086};
2087
Vladimir Markof9f64412015-09-02 14:05:49 +01002088class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002089 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302090#define DECLARE_KIND(type, super) k##type,
Vladimir Marko9974e3c2020-06-10 16:27:06 +01002091 enum InstructionKind { // private marker to avoid generate-operator-out.py from processing.
Vladimir Markoe3946222018-05-04 14:18:47 +01002092 FOR_EACH_CONCRETE_INSTRUCTION(DECLARE_KIND)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302093 kLastInstructionKind
2094 };
2095#undef DECLARE_KIND
2096
2097 HInstruction(InstructionKind kind, SideEffects side_effects, uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01002098 : HInstruction(kind, DataType::Type::kVoid, side_effects, dex_pc) {}
2099
2100 HInstruction(InstructionKind kind, DataType::Type type, SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002101 : previous_(nullptr),
2102 next_(nullptr),
2103 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002104 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002105 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002106 ssa_index_(-1),
Vladimir Markoa1de9182016-02-25 11:37:38 +00002107 packed_fields_(0u),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002108 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002109 locations_(nullptr),
2110 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002111 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00002112 side_effects_(side_effects),
Vladimir Markoa1de9182016-02-25 11:37:38 +00002113 reference_type_handle_(ReferenceTypeInfo::CreateInvalid().GetTypeHandle()) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302114 SetPackedField<InstructionKindField>(kind);
Vladimir Markobd785672018-05-03 17:09:09 +01002115 SetPackedField<TypeField>(type);
Vladimir Markoa1de9182016-02-25 11:37:38 +00002116 SetPackedFlag<kFlagReferenceTypeIsExact>(ReferenceTypeInfo::CreateInvalid().IsExact());
2117 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002118
Dave Allison20dfc792014-06-16 20:44:29 -07002119 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002120
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002121
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002122 HInstruction* GetNext() const { return next_; }
2123 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002124
Calin Juravle77520bc2015-01-12 18:45:46 +00002125 HInstruction* GetNextDisregardingMoves() const;
2126 HInstruction* GetPreviousDisregardingMoves() const;
2127
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002128 HBasicBlock* GetBlock() const { return block_; }
Vladimir Markoca6fff82017-10-03 14:49:14 +01002129 ArenaAllocator* GetAllocator() const { return block_->GetGraph()->GetAllocator(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002130 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002131 bool IsInBlock() const { return block_ != nullptr; }
2132 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00002133 bool IsLoopHeaderPhi() const { return IsPhi() && block_->IsLoopHeader(); }
2134 bool IsIrreducibleLoopHeaderPhi() const {
2135 return IsLoopHeaderPhi() && GetBlock()->GetLoopInformation()->IsIrreducible();
2136 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002137
Vladimir Marko372f10e2016-05-17 16:30:10 +01002138 virtual ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() = 0;
2139
2140 ArrayRef<const HUserRecord<HInstruction*>> GetInputRecords() const {
2141 // One virtual method is enough, just const_cast<> and then re-add the const.
2142 return ArrayRef<const HUserRecord<HInstruction*>>(
2143 const_cast<HInstruction*>(this)->GetInputRecords());
2144 }
2145
Vladimir Markoe9004912016-06-16 16:50:52 +01002146 HInputsRef GetInputs() {
2147 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01002148 }
2149
Vladimir Markoe9004912016-06-16 16:50:52 +01002150 HConstInputsRef GetInputs() const {
2151 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01002152 }
2153
2154 size_t InputCount() const { return GetInputRecords().size(); }
David Brazdil1abb4192015-02-17 18:33:36 +00002155 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002156
Aart Bik2767f4b2016-10-28 15:03:53 -07002157 bool HasInput(HInstruction* input) const {
2158 for (const HInstruction* i : GetInputs()) {
2159 if (i == input) {
2160 return true;
2161 }
2162 }
2163 return false;
2164 }
2165
Vladimir Marko372f10e2016-05-17 16:30:10 +01002166 void SetRawInputAt(size_t index, HInstruction* input) {
2167 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
2168 }
2169
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002170 virtual void Accept(HGraphVisitor* visitor) = 0;
2171 virtual const char* DebugName() const = 0;
2172
Vladimir Markobd785672018-05-03 17:09:09 +01002173 DataType::Type GetType() const {
2174 return TypeField::Decode(GetPackedFields());
2175 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002176
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002177 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002178
2179 uint32_t GetDexPc() const { return dex_pc_; }
2180
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002181 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01002182
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002183 // Can the instruction throw?
2184 // TODO: We should rename to CanVisiblyThrow, as some instructions (like HNewInstance),
2185 // could throw OOME, but it is still OK to remove them if they are unused.
Roland Levillaine161a2a2014-10-03 12:45:18 +01002186 virtual bool CanThrow() const { return false; }
Aart Bika8b8e9b2018-01-09 11:01:02 -08002187
2188 // Does the instruction always throw an exception unconditionally?
2189 virtual bool AlwaysThrows() const { return false; }
2190
David Brazdilec16f792015-08-19 15:04:01 +01002191 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07002192
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002193 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07002194 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002195
Calin Juravle10e244f2015-01-26 18:54:32 +00002196 // Does not apply for all instructions, but having this at top level greatly
2197 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00002198 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002199 virtual bool CanBeNull() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002200 DCHECK_EQ(GetType(), DataType::Type::kReference) << "CanBeNull only applies to reference types";
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002201 return true;
2202 }
Calin Juravle10e244f2015-01-26 18:54:32 +00002203
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002204 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01002205 return false;
2206 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002207
Nicolas Geoffray61ba8d22018-08-07 09:55:57 +01002208 // If this instruction will do an implicit null check, return the `HNullCheck` associated
2209 // with it. Otherwise return null.
2210 HNullCheck* GetImplicitNullCheck() const {
Vladimir Markoee1f1262019-06-26 14:37:17 +01002211 // Go over previous non-move instructions that are emitted at use site.
2212 HInstruction* prev_not_move = GetPreviousDisregardingMoves();
2213 while (prev_not_move != nullptr && prev_not_move->IsEmittedAtUseSite()) {
2214 if (prev_not_move->IsNullCheck()) {
2215 return prev_not_move->AsNullCheck();
2216 }
2217 prev_not_move = prev_not_move->GetPreviousDisregardingMoves();
Nicolas Geoffray61ba8d22018-08-07 09:55:57 +01002218 }
2219 return nullptr;
2220 }
2221
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00002222 virtual bool IsActualObject() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002223 return GetType() == DataType::Type::kReference;
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00002224 }
2225
Calin Juravle2e768302015-07-28 14:41:11 +00002226 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00002227
Calin Juravle61d544b2015-02-23 16:46:57 +00002228 ReferenceTypeInfo GetReferenceTypeInfo() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002229 DCHECK_EQ(GetType(), DataType::Type::kReference);
Vladimir Markoa1de9182016-02-25 11:37:38 +00002230 return ReferenceTypeInfo::CreateUnchecked(reference_type_handle_,
Vladimir Marko456307a2016-04-19 14:12:13 +00002231 GetPackedFlag<kFlagReferenceTypeIsExact>());
Calin Juravle61d544b2015-02-23 16:46:57 +00002232 }
Calin Juravleacf735c2015-02-12 15:25:22 +00002233
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002234 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00002235 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01002236 // Note: fixup_end remains valid across push_front().
2237 auto fixup_end = uses_.empty() ? uses_.begin() : ++uses_.begin();
2238 HUseListNode<HInstruction*>* new_node =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002239 new (GetBlock()->GetGraph()->GetAllocator()) HUseListNode<HInstruction*>(user, index);
Vladimir Marko46817b82016-03-29 12:21:58 +01002240 uses_.push_front(*new_node);
2241 FixUpUserRecordsAfterUseInsertion(fixup_end);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002242 }
2243
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002244 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01002245 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01002246 // Note: env_fixup_end remains valid across push_front().
2247 auto env_fixup_end = env_uses_.empty() ? env_uses_.begin() : ++env_uses_.begin();
2248 HUseListNode<HEnvironment*>* new_node =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002249 new (GetBlock()->GetGraph()->GetAllocator()) HUseListNode<HEnvironment*>(user, index);
Vladimir Marko46817b82016-03-29 12:21:58 +01002250 env_uses_.push_front(*new_node);
2251 FixUpUserRecordsAfterEnvUseInsertion(env_fixup_end);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002252 }
2253
David Brazdil1abb4192015-02-17 18:33:36 +00002254 void RemoveAsUserOfInput(size_t input) {
2255 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
Vladimir Marko46817b82016-03-29 12:21:58 +01002256 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
2257 input_use.GetInstruction()->uses_.erase_after(before_use_node);
2258 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
David Brazdil1abb4192015-02-17 18:33:36 +00002259 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002260
Vladimir Marko372f10e2016-05-17 16:30:10 +01002261 void RemoveAsUserOfAllInputs() {
2262 for (const HUserRecord<HInstruction*>& input_use : GetInputRecords()) {
2263 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
2264 input_use.GetInstruction()->uses_.erase_after(before_use_node);
2265 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
2266 }
2267 }
2268
David Brazdil1abb4192015-02-17 18:33:36 +00002269 const HUseList<HInstruction*>& GetUses() const { return uses_; }
2270 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002271
Vladimir Marko46817b82016-03-29 12:21:58 +01002272 bool HasUses() const { return !uses_.empty() || !env_uses_.empty(); }
2273 bool HasEnvironmentUses() const { return !env_uses_.empty(); }
2274 bool HasNonEnvironmentUses() const { return !uses_.empty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01002275 bool HasOnlyOneNonEnvironmentUse() const {
Vladimir Marko46817b82016-03-29 12:21:58 +01002276 return !HasEnvironmentUses() && GetUses().HasExactlyOneElement();
Alexandre Rames188d4312015-04-09 18:30:21 +01002277 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002278
Aart Bikcc42be02016-10-20 16:14:16 -07002279 bool IsRemovable() const {
Aart Bik482095d2016-10-10 15:39:10 -07002280 return
Aart Bikff7d89c2016-11-07 08:49:28 -08002281 !DoesAnyWrite() &&
Aart Bik482095d2016-10-10 15:39:10 -07002282 !CanThrow() &&
2283 !IsSuspendCheck() &&
2284 !IsControlFlow() &&
2285 !IsNativeDebugInfo() &&
2286 !IsParameterValue() &&
Aart Bik482095d2016-10-10 15:39:10 -07002287 // If we added an explicit barrier then we should keep it.
Igor Murashkind01745e2017-04-05 16:40:31 -07002288 !IsMemoryBarrier() &&
2289 !IsConstructorFence();
Aart Bik482095d2016-10-10 15:39:10 -07002290 }
2291
Aart Bikcc42be02016-10-20 16:14:16 -07002292 bool IsDeadAndRemovable() const {
2293 return IsRemovable() && !HasUses();
2294 }
2295
Roland Levillain6c82d402014-10-13 16:10:27 +01002296 // Does this instruction strictly dominate `other_instruction`?
2297 // Returns false if this instruction and `other_instruction` are the same.
2298 // Aborts if this instruction and `other_instruction` are both phis.
2299 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01002300
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002301 int GetId() const { return id_; }
2302 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002303
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002304 int GetSsaIndex() const { return ssa_index_; }
2305 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
2306 bool HasSsaIndex() const { return ssa_index_ != -1; }
2307
2308 bool HasEnvironment() const { return environment_ != nullptr; }
2309 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002310 // Set the `environment_` field. Raw because this method does not
2311 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002312 void SetRawEnvironment(HEnvironment* environment) {
2313 DCHECK(environment_ == nullptr);
2314 DCHECK_EQ(environment->GetHolder(), this);
2315 environment_ = environment;
2316 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002317
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002318 void InsertRawEnvironment(HEnvironment* environment) {
2319 DCHECK(environment_ != nullptr);
2320 DCHECK_EQ(environment->GetHolder(), this);
2321 DCHECK(environment->GetParent() == nullptr);
2322 environment->parent_ = environment_;
2323 environment_ = environment;
2324 }
2325
Vladimir Markocac5a7e2016-02-22 10:39:50 +00002326 void RemoveEnvironment();
2327
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002328 // Set the environment of this instruction, copying it from `environment`. While
2329 // copying, the uses lists are being updated.
2330 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002331 DCHECK(environment_ == nullptr);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002332 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetAllocator();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002333 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002334 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002335 if (environment->GetParent() != nullptr) {
2336 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2337 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002338 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002339
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002340 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
2341 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002342 DCHECK(environment_ == nullptr);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002343 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetAllocator();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002344 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002345 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002346 if (environment->GetParent() != nullptr) {
2347 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2348 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002349 }
2350
Nicolas Geoffray39468442014-09-02 15:17:15 +01002351 // Returns the number of entries in the environment. Typically, that is the
2352 // number of dex registers in a method. It could be more in case of inlining.
2353 size_t EnvironmentSize() const;
2354
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002355 LocationSummary* GetLocations() const { return locations_; }
2356 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002357
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002358 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00002359 void ReplaceUsesDominatedBy(HInstruction* dominator, HInstruction* replacement);
Nicolas Geoffray8a62a4c2018-07-03 09:39:07 +01002360 void ReplaceEnvUsesDominatedBy(HInstruction* dominator, HInstruction* replacement);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002361 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002362
Alexandre Rames188d4312015-04-09 18:30:21 +01002363 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
2364 // uses of this instruction by `other` are *not* updated.
2365 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
2366 ReplaceWith(other);
2367 other->ReplaceInput(this, use_index);
2368 }
2369
Alexandre Rames22aa54b2016-10-18 09:32:29 +01002370 // Move `this` instruction before `cursor`
2371 void MoveBefore(HInstruction* cursor, bool do_checks = true);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002372
Vladimir Markofb337ea2015-11-25 15:25:10 +00002373 // Move `this` before its first user and out of any loops. If there is no
2374 // out-of-loop user that dominates all other users, move the instruction
2375 // to the end of the out-of-loop common dominator of the user's blocks.
2376 //
2377 // This can be used only on non-throwing instructions with no side effects that
2378 // have at least one use but no environment uses.
2379 void MoveBeforeFirstUserAndOutOfLoops();
2380
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002381#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Markoa90dd512018-05-04 15:04:45 +01002382 bool Is##type() const;
2383
2384 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
2385#undef INSTRUCTION_TYPE_CHECK
2386
2387#define INSTRUCTION_TYPE_CAST(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002388 const H##type* As##type() const; \
2389 H##type* As##type();
2390
Vladimir Markoa90dd512018-05-04 15:04:45 +01002391 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CAST)
2392#undef INSTRUCTION_TYPE_CAST
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002393
Artem Serovcced8ba2017-07-19 18:18:09 +01002394 // Return a clone of the instruction if it is clonable (shallow copy by default, custom copy
2395 // if a custom copy-constructor is provided for a particular type). If IsClonable() is false for
2396 // the instruction then the behaviour of this function is undefined.
2397 //
2398 // Note: It is semantically valid to create a clone of the instruction only until
2399 // prepare_for_register_allocator phase as lifetime, intervals and codegen info are not
2400 // copied.
2401 //
2402 // Note: HEnvironment and some other fields are not copied and are set to default values, see
2403 // 'explicit HInstruction(const HInstruction& other)' for details.
2404 virtual HInstruction* Clone(ArenaAllocator* arena ATTRIBUTE_UNUSED) const {
2405 LOG(FATAL) << "Cloning is not implemented for the instruction " <<
2406 DebugName() << " " << GetId();
2407 UNREACHABLE();
2408 }
2409
2410 // Return whether instruction can be cloned (copied).
2411 virtual bool IsClonable() const { return false; }
2412
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002413 // Returns whether the instruction can be moved within the graph.
Aart Bik71bf7b42016-11-16 10:17:46 -08002414 // TODO: this method is used by LICM and GVN with possibly different
2415 // meanings? split and rename?
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002416 virtual bool CanBeMoved() const { return false; }
2417
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002418 // Returns whether any data encoded in the two instructions is equal.
2419 // This method does not look at the inputs. Both instructions must be
2420 // of the same type, otherwise the method has undefined behavior.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002421 virtual bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002422 return false;
2423 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002424
2425 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00002426 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002427 // 2) Their inputs are identical.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002428 bool Equals(const HInstruction* other) const;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002429
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302430 InstructionKind GetKind() const { return GetPackedField<InstructionKindField>(); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002431
2432 virtual size_t ComputeHashCode() const {
2433 size_t result = GetKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01002434 for (const HInstruction* input : GetInputs()) {
2435 result = (result * 31) + input->GetId();
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002436 }
2437 return result;
2438 }
2439
2440 SideEffects GetSideEffects() const { return side_effects_; }
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00002441 void SetSideEffects(SideEffects other) { side_effects_ = other; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002442 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002443
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002444 size_t GetLifetimePosition() const { return lifetime_position_; }
2445 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2446 LiveInterval* GetLiveInterval() const { return live_interval_; }
2447 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2448 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2449
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002450 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2451
2452 // Returns whether the code generation of the instruction will require to have access
2453 // to the current method. Such instructions are:
2454 // (1): Instructions that require an environment, as calling the runtime requires
2455 // to walk the stack and have the current method stored at a specific stack address.
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002456 // (2): HCurrentMethod, potentially used by HInvokeStaticOrDirect, HLoadString, or HLoadClass
2457 // to access the dex cache.
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002458 bool NeedsCurrentMethod() const {
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002459 return NeedsEnvironment() || IsCurrentMethod();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002460 }
2461
Vladimir Markodc151b22015-10-15 18:02:30 +01002462 // Returns whether the code generation of the instruction will require to have access
2463 // to the dex cache of the current method's declaring class via the current method.
2464 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002465
Mark Mendellc4701932015-04-10 13:18:51 -04002466 // Does this instruction have any use in an environment before
2467 // control flow hits 'other'?
2468 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2469
2470 // Remove all references to environment uses of this instruction.
2471 // The caller must ensure that this is safe to do.
2472 void RemoveEnvironmentUsers();
2473
Vladimir Markoa1de9182016-02-25 11:37:38 +00002474 bool IsEmittedAtUseSite() const { return GetPackedFlag<kFlagEmittedAtUseSite>(); }
2475 void MarkEmittedAtUseSite() { SetPackedFlag<kFlagEmittedAtUseSite>(true); }
David Brazdilb3e773e2016-01-26 11:28:37 +00002476
David Brazdil1abb4192015-02-17 18:33:36 +00002477 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002478 // If set, the machine code for this instruction is assumed to be generated by
2479 // its users. Used by liveness analysis to compute use positions accordingly.
2480 static constexpr size_t kFlagEmittedAtUseSite = 0u;
2481 static constexpr size_t kFlagReferenceTypeIsExact = kFlagEmittedAtUseSite + 1;
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302482 static constexpr size_t kFieldInstructionKind = kFlagReferenceTypeIsExact + 1;
2483 static constexpr size_t kFieldInstructionKindSize =
2484 MinimumBitsToStore(static_cast<size_t>(InstructionKind::kLastInstructionKind - 1));
Vladimir Markobd785672018-05-03 17:09:09 +01002485 static constexpr size_t kFieldType =
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302486 kFieldInstructionKind + kFieldInstructionKindSize;
Vladimir Markobd785672018-05-03 17:09:09 +01002487 static constexpr size_t kFieldTypeSize =
2488 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
2489 static constexpr size_t kNumberOfGenericPackedBits = kFieldType + kFieldTypeSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00002490 static constexpr size_t kMaxNumberOfPackedBits = sizeof(uint32_t) * kBitsPerByte;
2491
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302492 static_assert(kNumberOfGenericPackedBits <= kMaxNumberOfPackedBits,
2493 "Too many generic packed fields");
2494
Vladimir Markobd785672018-05-03 17:09:09 +01002495 using TypeField = BitField<DataType::Type, kFieldType, kFieldTypeSize>;
2496
Vladimir Marko372f10e2016-05-17 16:30:10 +01002497 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const {
2498 return GetInputRecords()[i];
2499 }
2500
2501 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) {
2502 ArrayRef<HUserRecord<HInstruction*>> input_records = GetInputRecords();
2503 input_records[index] = input;
2504 }
David Brazdil1abb4192015-02-17 18:33:36 +00002505
Vladimir Markoa1de9182016-02-25 11:37:38 +00002506 uint32_t GetPackedFields() const {
2507 return packed_fields_;
2508 }
2509
2510 template <size_t flag>
2511 bool GetPackedFlag() const {
2512 return (packed_fields_ & (1u << flag)) != 0u;
2513 }
2514
2515 template <size_t flag>
2516 void SetPackedFlag(bool value = true) {
2517 packed_fields_ = (packed_fields_ & ~(1u << flag)) | ((value ? 1u : 0u) << flag);
2518 }
2519
2520 template <typename BitFieldType>
2521 typename BitFieldType::value_type GetPackedField() const {
2522 return BitFieldType::Decode(packed_fields_);
2523 }
2524
2525 template <typename BitFieldType>
2526 void SetPackedField(typename BitFieldType::value_type value) {
2527 DCHECK(IsUint<BitFieldType::size>(static_cast<uintptr_t>(value)));
2528 packed_fields_ = BitFieldType::Update(value, packed_fields_);
2529 }
2530
Artem Serovcced8ba2017-07-19 18:18:09 +01002531 // Copy construction for the instruction (used for Clone function).
2532 //
2533 // Fields (e.g. lifetime, intervals and codegen info) associated with phases starting from
2534 // prepare_for_register_allocator are not copied (set to default values).
2535 //
2536 // Copy constructors must be provided for every HInstruction type; default copy constructor is
2537 // fine for most of them. However for some of the instructions a custom copy constructor must be
2538 // specified (when instruction has non-trivially copyable fields and must have a special behaviour
2539 // for copying them).
2540 explicit HInstruction(const HInstruction& other)
2541 : previous_(nullptr),
2542 next_(nullptr),
2543 block_(nullptr),
2544 dex_pc_(other.dex_pc_),
2545 id_(-1),
2546 ssa_index_(-1),
2547 packed_fields_(other.packed_fields_),
2548 environment_(nullptr),
2549 locations_(nullptr),
2550 live_interval_(nullptr),
2551 lifetime_position_(kNoLifetime),
2552 side_effects_(other.side_effects_),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302553 reference_type_handle_(other.reference_type_handle_) {
2554 }
Artem Serovcced8ba2017-07-19 18:18:09 +01002555
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002556 private:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302557 using InstructionKindField =
2558 BitField<InstructionKind, kFieldInstructionKind, kFieldInstructionKindSize>;
2559
Vladimir Marko46817b82016-03-29 12:21:58 +01002560 void FixUpUserRecordsAfterUseInsertion(HUseList<HInstruction*>::iterator fixup_end) {
2561 auto before_use_node = uses_.before_begin();
2562 for (auto use_node = uses_.begin(); use_node != fixup_end; ++use_node) {
2563 HInstruction* user = use_node->GetUser();
2564 size_t input_index = use_node->GetIndex();
2565 user->SetRawInputRecordAt(input_index, HUserRecord<HInstruction*>(this, before_use_node));
2566 before_use_node = use_node;
2567 }
2568 }
2569
2570 void FixUpUserRecordsAfterUseRemoval(HUseList<HInstruction*>::iterator before_use_node) {
2571 auto next = ++HUseList<HInstruction*>::iterator(before_use_node);
2572 if (next != uses_.end()) {
2573 HInstruction* next_user = next->GetUser();
2574 size_t next_index = next->GetIndex();
2575 DCHECK(next_user->InputRecordAt(next_index).GetInstruction() == this);
2576 next_user->SetRawInputRecordAt(next_index, HUserRecord<HInstruction*>(this, before_use_node));
2577 }
2578 }
2579
2580 void FixUpUserRecordsAfterEnvUseInsertion(HUseList<HEnvironment*>::iterator env_fixup_end) {
2581 auto before_env_use_node = env_uses_.before_begin();
2582 for (auto env_use_node = env_uses_.begin(); env_use_node != env_fixup_end; ++env_use_node) {
2583 HEnvironment* user = env_use_node->GetUser();
2584 size_t input_index = env_use_node->GetIndex();
2585 user->vregs_[input_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2586 before_env_use_node = env_use_node;
2587 }
2588 }
2589
2590 void FixUpUserRecordsAfterEnvUseRemoval(HUseList<HEnvironment*>::iterator before_env_use_node) {
2591 auto next = ++HUseList<HEnvironment*>::iterator(before_env_use_node);
2592 if (next != env_uses_.end()) {
2593 HEnvironment* next_user = next->GetUser();
2594 size_t next_index = next->GetIndex();
2595 DCHECK(next_user->vregs_[next_index].GetInstruction() == this);
2596 next_user->vregs_[next_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2597 }
2598 }
David Brazdil1abb4192015-02-17 18:33:36 +00002599
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002600 HInstruction* previous_;
2601 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002602 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002603 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002604
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002605 // An instruction gets an id when it is added to the graph.
2606 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002607 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002608 int id_;
2609
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002610 // When doing liveness analysis, instructions that have uses get an SSA index.
2611 int ssa_index_;
2612
Vladimir Markoa1de9182016-02-25 11:37:38 +00002613 // Packed fields.
2614 uint32_t packed_fields_;
David Brazdilb3e773e2016-01-26 11:28:37 +00002615
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002616 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002617 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002618
2619 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002620 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002621
Nicolas Geoffray39468442014-09-02 15:17:15 +01002622 // The environment associated with this instruction. Not null if the instruction
2623 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002624 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002625
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002626 // Set by the code generator.
2627 LocationSummary* locations_;
2628
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002629 // Set by the liveness analysis.
2630 LiveInterval* live_interval_;
2631
2632 // Set by the liveness analysis, this is the position in a linear
2633 // order of blocks where this instruction's live interval start.
2634 size_t lifetime_position_;
2635
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002636 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002637
Vladimir Markoa1de9182016-02-25 11:37:38 +00002638 // The reference handle part of the reference type info.
2639 // The IsExact() flag is stored in packed fields.
Calin Juravleacf735c2015-02-12 15:25:22 +00002640 // TODO: for primitive types this should be marked as invalid.
Vladimir Markoa1de9182016-02-25 11:37:38 +00002641 ReferenceTypeInfo::TypeHandle reference_type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00002642
David Brazdil1abb4192015-02-17 18:33:36 +00002643 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002644 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002645 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002646 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002647 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002648};
Vladimir Marko9974e3c2020-06-10 16:27:06 +01002649std::ostream& operator<<(std::ostream& os, HInstruction::InstructionKind rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002650
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002651// Iterates over the instructions, while preserving the next instruction
2652// in case the current instruction gets removed from the list by the user
2653// of this iterator.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002654class HInstructionIterator : public ValueObject {
2655 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002656 explicit HInstructionIterator(const HInstructionList& instructions)
2657 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002658 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002659 }
2660
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002661 bool Done() const { return instruction_ == nullptr; }
2662 HInstruction* Current() const { return instruction_; }
2663 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002664 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002665 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002666 }
2667
2668 private:
2669 HInstruction* instruction_;
2670 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002671
2672 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002673};
2674
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002675// Iterates over the instructions without saving the next instruction,
2676// therefore handling changes in the graph potentially made by the user
2677// of this iterator.
2678class HInstructionIteratorHandleChanges : public ValueObject {
2679 public:
2680 explicit HInstructionIteratorHandleChanges(const HInstructionList& instructions)
2681 : instruction_(instructions.first_instruction_) {
2682 }
2683
2684 bool Done() const { return instruction_ == nullptr; }
2685 HInstruction* Current() const { return instruction_; }
2686 void Advance() {
2687 instruction_ = instruction_->GetNext();
2688 }
2689
2690 private:
2691 HInstruction* instruction_;
2692
2693 DISALLOW_COPY_AND_ASSIGN(HInstructionIteratorHandleChanges);
2694};
2695
2696
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002697class HBackwardInstructionIterator : public ValueObject {
2698 public:
2699 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2700 : instruction_(instructions.last_instruction_) {
2701 next_ = Done() ? nullptr : instruction_->GetPrevious();
2702 }
2703
2704 bool Done() const { return instruction_ == nullptr; }
2705 HInstruction* Current() const { return instruction_; }
2706 void Advance() {
2707 instruction_ = next_;
2708 next_ = Done() ? nullptr : instruction_->GetPrevious();
2709 }
2710
2711 private:
2712 HInstruction* instruction_;
2713 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002714
2715 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002716};
2717
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002718class HVariableInputSizeInstruction : public HInstruction {
2719 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08002720 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002721 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() override {
Mingyao Yangb0b051a2016-11-17 09:04:53 -08002722 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
2723 }
2724
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002725 void AddInput(HInstruction* input);
2726 void InsertInputAt(size_t index, HInstruction* input);
2727 void RemoveInputAt(size_t index);
2728
Igor Murashkind01745e2017-04-05 16:40:31 -07002729 // Removes all the inputs.
2730 // Also removes this instructions from each input's use list
2731 // (for non-environment uses only).
2732 void RemoveAllInputs();
2733
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002734 protected:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302735 HVariableInputSizeInstruction(InstructionKind inst_kind,
2736 SideEffects side_effects,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002737 uint32_t dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002738 ArenaAllocator* allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002739 size_t number_of_inputs,
2740 ArenaAllocKind kind)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302741 : HInstruction(inst_kind, side_effects, dex_pc),
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002742 inputs_(number_of_inputs, allocator->Adapter(kind)) {}
Vladimir Markobd785672018-05-03 17:09:09 +01002743 HVariableInputSizeInstruction(InstructionKind inst_kind,
2744 DataType::Type type,
2745 SideEffects side_effects,
2746 uint32_t dex_pc,
2747 ArenaAllocator* allocator,
2748 size_t number_of_inputs,
2749 ArenaAllocKind kind)
2750 : HInstruction(inst_kind, type, side_effects, dex_pc),
2751 inputs_(number_of_inputs, allocator->Adapter(kind)) {}
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002752
Artem Serovcced8ba2017-07-19 18:18:09 +01002753 DEFAULT_COPY_CONSTRUCTOR(VariableInputSizeInstruction);
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002754
Artem Serovcced8ba2017-07-19 18:18:09 +01002755 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002756};
2757
Vladimir Markof9f64412015-09-02 14:05:49 +01002758template<size_t N>
Vladimir Markobd785672018-05-03 17:09:09 +01002759class HExpression : public HInstruction {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002760 public:
Vladimir Markobd785672018-05-03 17:09:09 +01002761 HExpression<N>(InstructionKind kind, SideEffects side_effects, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302762 : HInstruction(kind, side_effects, dex_pc), inputs_() {}
Vladimir Markobd785672018-05-03 17:09:09 +01002763 HExpression<N>(InstructionKind kind,
2764 DataType::Type type,
2765 SideEffects side_effects,
2766 uint32_t dex_pc)
2767 : HInstruction(kind, type, side_effects, dex_pc), inputs_() {}
2768 virtual ~HExpression() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002769
Vladimir Marko372f10e2016-05-17 16:30:10 +01002770 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01002771 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Vladimir Marko372f10e2016-05-17 16:30:10 +01002772 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002773 }
2774
Artem Serovcced8ba2017-07-19 18:18:09 +01002775 protected:
Vladimir Markobd785672018-05-03 17:09:09 +01002776 DEFAULT_COPY_CONSTRUCTOR(Expression<N>);
Artem Serovcced8ba2017-07-19 18:18:09 +01002777
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002778 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002779 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002780
2781 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002782};
2783
Vladimir Markobd785672018-05-03 17:09:09 +01002784// HExpression specialization for N=0.
Vladimir Markof9f64412015-09-02 14:05:49 +01002785template<>
Vladimir Markobd785672018-05-03 17:09:09 +01002786class HExpression<0> : public HInstruction {
Vladimir Markof9f64412015-09-02 14:05:49 +01002787 public:
Vladimir Markobd785672018-05-03 17:09:09 +01002788 using HInstruction::HInstruction;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002789
Vladimir Markobd785672018-05-03 17:09:09 +01002790 virtual ~HExpression() {}
Vladimir Markof9f64412015-09-02 14:05:49 +01002791
Vladimir Marko372f10e2016-05-17 16:30:10 +01002792 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01002793 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Vladimir Marko372f10e2016-05-17 16:30:10 +01002794 return ArrayRef<HUserRecord<HInstruction*>>();
Vladimir Markof9f64412015-09-02 14:05:49 +01002795 }
2796
Artem Serovcced8ba2017-07-19 18:18:09 +01002797 protected:
Vladimir Markobd785672018-05-03 17:09:09 +01002798 DEFAULT_COPY_CONSTRUCTOR(Expression<0>);
Artem Serovcced8ba2017-07-19 18:18:09 +01002799
Vladimir Markof9f64412015-09-02 14:05:49 +01002800 private:
2801 friend class SsaBuilder;
2802};
2803
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002804// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2805// instruction that branches to the exit block.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002806class HReturnVoid final : public HExpression<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002807 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002808 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01002809 : HExpression(kReturnVoid, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302810 }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002811
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002812 bool IsControlFlow() const override { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002813
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002814 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002815
Artem Serovcced8ba2017-07-19 18:18:09 +01002816 protected:
2817 DEFAULT_COPY_CONSTRUCTOR(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002818};
2819
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002820// Represents dex's RETURN opcodes. A HReturn is a control flow
2821// instruction that branches to the exit block.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002822class HReturn final : public HExpression<1> {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002823 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002824 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01002825 : HExpression(kReturn, SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002826 SetRawInputAt(0, value);
2827 }
2828
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002829 bool IsControlFlow() const override { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002830
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002831 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002832
Artem Serovcced8ba2017-07-19 18:18:09 +01002833 protected:
2834 DEFAULT_COPY_CONSTRUCTOR(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002835};
2836
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002837class HPhi final : public HVariableInputSizeInstruction {
David Brazdildee58d62016-04-07 09:54:26 +00002838 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002839 HPhi(ArenaAllocator* allocator,
David Brazdildee58d62016-04-07 09:54:26 +00002840 uint32_t reg_number,
2841 size_t number_of_inputs,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002842 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +00002843 uint32_t dex_pc = kNoDexPc)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002844 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302845 kPhi,
Vladimir Markobd785672018-05-03 17:09:09 +01002846 ToPhiType(type),
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002847 SideEffects::None(),
2848 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002849 allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002850 number_of_inputs,
2851 kArenaAllocPhiInputs),
David Brazdildee58d62016-04-07 09:54:26 +00002852 reg_number_(reg_number) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002853 DCHECK_NE(GetType(), DataType::Type::kVoid);
David Brazdildee58d62016-04-07 09:54:26 +00002854 // Phis are constructed live and marked dead if conflicting or unused.
2855 // Individual steps of SsaBuilder should assume that if a phi has been
2856 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
2857 SetPackedFlag<kFlagIsLive>(true);
2858 SetPackedFlag<kFlagCanBeNull>(true);
2859 }
2860
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002861 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01002862
David Brazdildee58d62016-04-07 09:54:26 +00002863 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002864 static DataType::Type ToPhiType(DataType::Type type) {
2865 return DataType::Kind(type);
David Brazdildee58d62016-04-07 09:54:26 +00002866 }
2867
2868 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
2869
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002870 void SetType(DataType::Type new_type) {
David Brazdildee58d62016-04-07 09:54:26 +00002871 // Make sure that only valid type changes occur. The following are allowed:
2872 // (1) int -> float/ref (primitive type propagation),
2873 // (2) long -> double (primitive type propagation).
2874 DCHECK(GetType() == new_type ||
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002875 (GetType() == DataType::Type::kInt32 && new_type == DataType::Type::kFloat32) ||
2876 (GetType() == DataType::Type::kInt32 && new_type == DataType::Type::kReference) ||
2877 (GetType() == DataType::Type::kInt64 && new_type == DataType::Type::kFloat64));
David Brazdildee58d62016-04-07 09:54:26 +00002878 SetPackedField<TypeField>(new_type);
2879 }
2880
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002881 bool CanBeNull() const override { return GetPackedFlag<kFlagCanBeNull>(); }
David Brazdildee58d62016-04-07 09:54:26 +00002882 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
2883
2884 uint32_t GetRegNumber() const { return reg_number_; }
2885
2886 void SetDead() { SetPackedFlag<kFlagIsLive>(false); }
2887 void SetLive() { SetPackedFlag<kFlagIsLive>(true); }
2888 bool IsDead() const { return !IsLive(); }
2889 bool IsLive() const { return GetPackedFlag<kFlagIsLive>(); }
2890
Vladimir Markoe9004912016-06-16 16:50:52 +01002891 bool IsVRegEquivalentOf(const HInstruction* other) const {
David Brazdildee58d62016-04-07 09:54:26 +00002892 return other != nullptr
2893 && other->IsPhi()
2894 && other->AsPhi()->GetBlock() == GetBlock()
2895 && other->AsPhi()->GetRegNumber() == GetRegNumber();
2896 }
2897
Nicolas Geoffrayf57c1ae2017-06-28 17:40:18 +01002898 bool HasEquivalentPhi() const {
2899 if (GetPrevious() != nullptr && GetPrevious()->AsPhi()->GetRegNumber() == GetRegNumber()) {
2900 return true;
2901 }
2902 if (GetNext() != nullptr && GetNext()->AsPhi()->GetRegNumber() == GetRegNumber()) {
2903 return true;
2904 }
2905 return false;
2906 }
2907
David Brazdildee58d62016-04-07 09:54:26 +00002908 // Returns the next equivalent phi (starting from the current one) or null if there is none.
2909 // An equivalent phi is a phi having the same dex register and type.
2910 // It assumes that phis with the same dex register are adjacent.
2911 HPhi* GetNextEquivalentPhiWithSameType() {
2912 HInstruction* next = GetNext();
2913 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
2914 if (next->GetType() == GetType()) {
2915 return next->AsPhi();
2916 }
2917 next = next->GetNext();
2918 }
2919 return nullptr;
2920 }
2921
2922 DECLARE_INSTRUCTION(Phi);
2923
Artem Serovcced8ba2017-07-19 18:18:09 +01002924 protected:
2925 DEFAULT_COPY_CONSTRUCTOR(Phi);
2926
David Brazdildee58d62016-04-07 09:54:26 +00002927 private:
Vladimir Markobd785672018-05-03 17:09:09 +01002928 static constexpr size_t kFlagIsLive = HInstruction::kNumberOfGenericPackedBits;
David Brazdildee58d62016-04-07 09:54:26 +00002929 static constexpr size_t kFlagCanBeNull = kFlagIsLive + 1;
2930 static constexpr size_t kNumberOfPhiPackedBits = kFlagCanBeNull + 1;
2931 static_assert(kNumberOfPhiPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
David Brazdildee58d62016-04-07 09:54:26 +00002932
David Brazdildee58d62016-04-07 09:54:26 +00002933 const uint32_t reg_number_;
David Brazdildee58d62016-04-07 09:54:26 +00002934};
2935
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002936// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002937// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002938// exit block.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002939class HExit final : public HExpression<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002940 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302941 explicit HExit(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01002942 : HExpression(kExit, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302943 }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002944
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002945 bool IsControlFlow() const override { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002946
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002947 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002948
Artem Serovcced8ba2017-07-19 18:18:09 +01002949 protected:
2950 DEFAULT_COPY_CONSTRUCTOR(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002951};
2952
2953// Jumps from one block to another.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002954class HGoto final : public HExpression<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002955 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302956 explicit HGoto(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01002957 : HExpression(kGoto, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302958 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002959
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002960 bool IsClonable() const override { return true; }
2961 bool IsControlFlow() const override { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002962
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002963 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002964 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002965 }
2966
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002967 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002968
Artem Serovcced8ba2017-07-19 18:18:09 +01002969 protected:
2970 DEFAULT_COPY_CONSTRUCTOR(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002971};
2972
Roland Levillain9867bc72015-08-05 10:21:34 +01002973class HConstant : public HExpression<0> {
2974 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302975 explicit HConstant(InstructionKind kind, DataType::Type type, uint32_t dex_pc = kNoDexPc)
2976 : HExpression(kind, type, SideEffects::None(), dex_pc) {
2977 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002978
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002979 bool CanBeMoved() const override { return true; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002980
Roland Levillain1a653882016-03-18 18:05:57 +00002981 // Is this constant -1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002982 virtual bool IsMinusOne() const { return false; }
Roland Levillain1a653882016-03-18 18:05:57 +00002983 // Is this constant 0 in the arithmetic sense?
2984 virtual bool IsArithmeticZero() const { return false; }
2985 // Is this constant a 0-bit pattern?
2986 virtual bool IsZeroBitPattern() const { return false; }
2987 // Is this constant 1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002988 virtual bool IsOne() const { return false; }
2989
David Brazdil77a48ae2015-09-15 12:34:04 +00002990 virtual uint64_t GetValueAsUint64() const = 0;
2991
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002992 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002993
Artem Serovcced8ba2017-07-19 18:18:09 +01002994 protected:
2995 DEFAULT_COPY_CONSTRUCTOR(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002996};
2997
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002998class HNullConstant final : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002999 public:
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003000 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01003001 return true;
3002 }
3003
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003004 uint64_t GetValueAsUint64() const override { return 0; }
David Brazdil77a48ae2015-09-15 12:34:04 +00003005
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003006 size_t ComputeHashCode() const override { return 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01003007
Roland Levillain1a653882016-03-18 18:05:57 +00003008 // The null constant representation is a 0-bit pattern.
Yi Kong39402542019-03-24 02:47:16 -07003009 bool IsZeroBitPattern() const override { return true; }
Roland Levillain1a653882016-03-18 18:05:57 +00003010
Roland Levillain9867bc72015-08-05 10:21:34 +01003011 DECLARE_INSTRUCTION(NullConstant);
3012
Artem Serovcced8ba2017-07-19 18:18:09 +01003013 protected:
3014 DEFAULT_COPY_CONSTRUCTOR(NullConstant);
3015
Roland Levillain9867bc72015-08-05 10:21:34 +01003016 private:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003017 explicit HNullConstant(uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303018 : HConstant(kNullConstant, DataType::Type::kReference, dex_pc) {
3019 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003020
3021 friend class HGraph;
Roland Levillain9867bc72015-08-05 10:21:34 +01003022};
3023
3024// Constants of the type int. Those can be from Dex instructions, or
3025// synthesized (for example with the if-eqz instruction).
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003026class HIntConstant final : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01003027 public:
3028 int32_t GetValue() const { return value_; }
3029
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003030 uint64_t GetValueAsUint64() const override {
David Brazdil9f389d42015-10-01 14:32:56 +01003031 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
3032 }
David Brazdil77a48ae2015-09-15 12:34:04 +00003033
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003034 bool InstructionDataEquals(const HInstruction* other) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003035 DCHECK(other->IsIntConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01003036 return other->AsIntConstant()->value_ == value_;
3037 }
3038
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003039 size_t ComputeHashCode() const override { return GetValue(); }
Roland Levillain9867bc72015-08-05 10:21:34 +01003040
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003041 bool IsMinusOne() const override { return GetValue() == -1; }
3042 bool IsArithmeticZero() const override { return GetValue() == 0; }
3043 bool IsZeroBitPattern() const override { return GetValue() == 0; }
3044 bool IsOne() const override { return GetValue() == 1; }
Roland Levillain9867bc72015-08-05 10:21:34 +01003045
Roland Levillain1a653882016-03-18 18:05:57 +00003046 // Integer constants are used to encode Boolean values as well,
3047 // where 1 means true and 0 means false.
3048 bool IsTrue() const { return GetValue() == 1; }
3049 bool IsFalse() const { return GetValue() == 0; }
3050
Roland Levillain9867bc72015-08-05 10:21:34 +01003051 DECLARE_INSTRUCTION(IntConstant);
3052
Artem Serovcced8ba2017-07-19 18:18:09 +01003053 protected:
3054 DEFAULT_COPY_CONSTRUCTOR(IntConstant);
3055
Roland Levillain9867bc72015-08-05 10:21:34 +01003056 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003057 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303058 : HConstant(kIntConstant, DataType::Type::kInt32, dex_pc), value_(value) {
3059 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003060 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303061 : HConstant(kIntConstant, DataType::Type::kInt32, dex_pc),
3062 value_(value ? 1 : 0) {
3063 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003064
3065 const int32_t value_;
3066
3067 friend class HGraph;
3068 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
3069 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
Roland Levillain9867bc72015-08-05 10:21:34 +01003070};
3071
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003072class HLongConstant final : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01003073 public:
3074 int64_t GetValue() const { return value_; }
3075
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003076 uint64_t GetValueAsUint64() const override { return value_; }
David Brazdil77a48ae2015-09-15 12:34:04 +00003077
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003078 bool InstructionDataEquals(const HInstruction* other) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003079 DCHECK(other->IsLongConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01003080 return other->AsLongConstant()->value_ == value_;
3081 }
3082
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003083 size_t ComputeHashCode() const override { return static_cast<size_t>(GetValue()); }
Roland Levillain9867bc72015-08-05 10:21:34 +01003084
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003085 bool IsMinusOne() const override { return GetValue() == -1; }
3086 bool IsArithmeticZero() const override { return GetValue() == 0; }
3087 bool IsZeroBitPattern() const override { return GetValue() == 0; }
3088 bool IsOne() const override { return GetValue() == 1; }
Roland Levillain9867bc72015-08-05 10:21:34 +01003089
3090 DECLARE_INSTRUCTION(LongConstant);
3091
Artem Serovcced8ba2017-07-19 18:18:09 +01003092 protected:
3093 DEFAULT_COPY_CONSTRUCTOR(LongConstant);
3094
Roland Levillain9867bc72015-08-05 10:21:34 +01003095 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003096 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303097 : HConstant(kLongConstant, DataType::Type::kInt64, dex_pc),
3098 value_(value) {
3099 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003100
3101 const int64_t value_;
3102
3103 friend class HGraph;
Roland Levillain9867bc72015-08-05 10:21:34 +01003104};
Dave Allison20dfc792014-06-16 20:44:29 -07003105
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003106class HFloatConstant final : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003107 public:
3108 float GetValue() const { return value_; }
3109
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003110 uint64_t GetValueAsUint64() const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003111 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
3112 }
3113
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003114 bool InstructionDataEquals(const HInstruction* other) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003115 DCHECK(other->IsFloatConstant()) << other->DebugName();
3116 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
3117 }
3118
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003119 size_t ComputeHashCode() const override { return static_cast<size_t>(GetValue()); }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003120
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003121 bool IsMinusOne() const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003122 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
3123 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003124 bool IsArithmeticZero() const override {
Roland Levillain1a653882016-03-18 18:05:57 +00003125 return std::fpclassify(value_) == FP_ZERO;
3126 }
3127 bool IsArithmeticPositiveZero() const {
3128 return IsArithmeticZero() && !std::signbit(value_);
3129 }
3130 bool IsArithmeticNegativeZero() const {
3131 return IsArithmeticZero() && std::signbit(value_);
3132 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003133 bool IsZeroBitPattern() const override {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00003134 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(0.0f);
Roland Levillain31dd3d62016-02-16 12:21:02 +00003135 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003136 bool IsOne() const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003137 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
3138 }
3139 bool IsNaN() const {
3140 return std::isnan(value_);
3141 }
3142
3143 DECLARE_INSTRUCTION(FloatConstant);
3144
Artem Serovcced8ba2017-07-19 18:18:09 +01003145 protected:
3146 DEFAULT_COPY_CONSTRUCTOR(FloatConstant);
3147
Roland Levillain31dd3d62016-02-16 12:21:02 +00003148 private:
3149 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303150 : HConstant(kFloatConstant, DataType::Type::kFloat32, dex_pc),
3151 value_(value) {
3152 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003153 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303154 : HConstant(kFloatConstant, DataType::Type::kFloat32, dex_pc),
3155 value_(bit_cast<float, int32_t>(value)) {
3156 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003157
3158 const float value_;
3159
3160 // Only the SsaBuilder and HGraph can create floating-point constants.
3161 friend class SsaBuilder;
3162 friend class HGraph;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003163};
3164
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003165class HDoubleConstant final : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003166 public:
3167 double GetValue() const { return value_; }
3168
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003169 uint64_t GetValueAsUint64() const override { return bit_cast<uint64_t, double>(value_); }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003170
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003171 bool InstructionDataEquals(const HInstruction* other) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003172 DCHECK(other->IsDoubleConstant()) << other->DebugName();
3173 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
3174 }
3175
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003176 size_t ComputeHashCode() const override { return static_cast<size_t>(GetValue()); }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003177
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003178 bool IsMinusOne() const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003179 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
3180 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003181 bool IsArithmeticZero() const override {
Roland Levillain1a653882016-03-18 18:05:57 +00003182 return std::fpclassify(value_) == FP_ZERO;
3183 }
3184 bool IsArithmeticPositiveZero() const {
3185 return IsArithmeticZero() && !std::signbit(value_);
3186 }
3187 bool IsArithmeticNegativeZero() const {
3188 return IsArithmeticZero() && std::signbit(value_);
3189 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003190 bool IsZeroBitPattern() const override {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00003191 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((0.0));
Roland Levillain31dd3d62016-02-16 12:21:02 +00003192 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003193 bool IsOne() const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003194 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
3195 }
3196 bool IsNaN() const {
3197 return std::isnan(value_);
3198 }
3199
3200 DECLARE_INSTRUCTION(DoubleConstant);
3201
Artem Serovcced8ba2017-07-19 18:18:09 +01003202 protected:
3203 DEFAULT_COPY_CONSTRUCTOR(DoubleConstant);
3204
Roland Levillain31dd3d62016-02-16 12:21:02 +00003205 private:
3206 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303207 : HConstant(kDoubleConstant, DataType::Type::kFloat64, dex_pc),
3208 value_(value) {
3209 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003210 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303211 : HConstant(kDoubleConstant, DataType::Type::kFloat64, dex_pc),
3212 value_(bit_cast<double, int64_t>(value)) {
3213 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003214
3215 const double value_;
3216
3217 // Only the SsaBuilder and HGraph can create floating-point constants.
3218 friend class SsaBuilder;
3219 friend class HGraph;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003220};
3221
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003222// Conditional branch. A block ending with an HIf instruction must have
3223// two successors.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003224class HIf final : public HExpression<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003225 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003226 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01003227 : HExpression(kIf, SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003228 SetRawInputAt(0, input);
3229 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003230
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003231 bool IsClonable() const override { return true; }
3232 bool IsControlFlow() const override { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003233
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003234 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01003235 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003236 }
3237
3238 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01003239 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003240 }
3241
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003242 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003243
Artem Serovcced8ba2017-07-19 18:18:09 +01003244 protected:
3245 DEFAULT_COPY_CONSTRUCTOR(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003246};
3247
David Brazdilfc6a86a2015-06-26 10:33:45 +00003248
3249// Abstract instruction which marks the beginning and/or end of a try block and
3250// links it to the respective exception handlers. Behaves the same as a Goto in
3251// non-exceptional control flow.
3252// Normal-flow successor is stored at index zero, exception handlers under
3253// higher indices in no particular order.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003254class HTryBoundary final : public HExpression<0> {
David Brazdilfc6a86a2015-06-26 10:33:45 +00003255 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003256 enum class BoundaryKind {
David Brazdil56e1acc2015-06-30 15:41:36 +01003257 kEntry,
3258 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00003259 kLast = kExit
David Brazdil56e1acc2015-06-30 15:41:36 +01003260 };
3261
Artem Serovd1aa7d02018-06-22 11:35:46 +01003262 // SideEffects::CanTriggerGC prevents instructions with SideEffects::DependOnGC to be alive
3263 // across the catch block entering edges as GC might happen during throwing an exception.
3264 // TryBoundary with BoundaryKind::kExit is conservatively used for that as there is no
3265 // HInstruction which a catch block must start from.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003266 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
Artem Serovd1aa7d02018-06-22 11:35:46 +01003267 : HExpression(kTryBoundary,
3268 (kind == BoundaryKind::kExit) ? SideEffects::CanTriggerGC()
3269 : SideEffects::None(),
3270 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003271 SetPackedField<BoundaryKindField>(kind);
3272 }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003273
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003274 bool IsControlFlow() const override { return true; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003275
3276 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01003277 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003278
David Brazdild26a4112015-11-10 11:07:31 +00003279 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
3280 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
3281 }
3282
David Brazdilfc6a86a2015-06-26 10:33:45 +00003283 // Returns whether `handler` is among its exception handlers (non-zero index
3284 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01003285 bool HasExceptionHandler(const HBasicBlock& handler) const {
3286 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00003287 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00003288 }
3289
3290 // If not present already, adds `handler` to its block's list of exception
3291 // handlers.
3292 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01003293 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00003294 GetBlock()->AddSuccessor(handler);
3295 }
3296 }
3297
Vladimir Markoa1de9182016-02-25 11:37:38 +00003298 BoundaryKind GetBoundaryKind() const { return GetPackedField<BoundaryKindField>(); }
3299 bool IsEntry() const { return GetBoundaryKind() == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003300
David Brazdilffee3d32015-07-06 11:48:53 +01003301 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
3302
David Brazdilfc6a86a2015-06-26 10:33:45 +00003303 DECLARE_INSTRUCTION(TryBoundary);
3304
Artem Serovcced8ba2017-07-19 18:18:09 +01003305 protected:
3306 DEFAULT_COPY_CONSTRUCTOR(TryBoundary);
3307
David Brazdilfc6a86a2015-06-26 10:33:45 +00003308 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003309 static constexpr size_t kFieldBoundaryKind = kNumberOfGenericPackedBits;
3310 static constexpr size_t kFieldBoundaryKindSize =
3311 MinimumBitsToStore(static_cast<size_t>(BoundaryKind::kLast));
3312 static constexpr size_t kNumberOfTryBoundaryPackedBits =
3313 kFieldBoundaryKind + kFieldBoundaryKindSize;
3314 static_assert(kNumberOfTryBoundaryPackedBits <= kMaxNumberOfPackedBits,
3315 "Too many packed fields.");
3316 using BoundaryKindField = BitField<BoundaryKind, kFieldBoundaryKind, kFieldBoundaryKindSize>;
David Brazdilfc6a86a2015-06-26 10:33:45 +00003317};
3318
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003319// Deoptimize to interpreter, upon checking a condition.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003320class HDeoptimize final : public HVariableInputSizeInstruction {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003321 public:
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003322 // Use this constructor when the `HDeoptimize` acts as a barrier, where no code can move
3323 // across.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003324 HDeoptimize(ArenaAllocator* allocator,
3325 HInstruction* cond,
3326 DeoptimizationKind kind,
3327 uint32_t dex_pc)
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003328 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303329 kDeoptimize,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003330 SideEffects::All(),
3331 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003332 allocator,
Andreas Gampe3db70682018-12-26 15:12:03 -08003333 /* number_of_inputs= */ 1,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003334 kArenaAllocMisc) {
3335 SetPackedFlag<kFieldCanBeMoved>(false);
3336 SetPackedField<DeoptimizeKindField>(kind);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003337 SetRawInputAt(0, cond);
3338 }
3339
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003340 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01003341
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003342 // Use this constructor when the `HDeoptimize` guards an instruction, and any user
3343 // that relies on the deoptimization to pass should have its input be the `HDeoptimize`
3344 // instead of `guard`.
3345 // We set CanTriggerGC to prevent any intermediate address to be live
3346 // at the point of the `HDeoptimize`.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003347 HDeoptimize(ArenaAllocator* allocator,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003348 HInstruction* cond,
3349 HInstruction* guard,
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003350 DeoptimizationKind kind,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003351 uint32_t dex_pc)
3352 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303353 kDeoptimize,
Vladimir Markobd785672018-05-03 17:09:09 +01003354 guard->GetType(),
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003355 SideEffects::CanTriggerGC(),
3356 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003357 allocator,
Andreas Gampe3db70682018-12-26 15:12:03 -08003358 /* number_of_inputs= */ 2,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003359 kArenaAllocMisc) {
3360 SetPackedFlag<kFieldCanBeMoved>(true);
3361 SetPackedField<DeoptimizeKindField>(kind);
3362 SetRawInputAt(0, cond);
3363 SetRawInputAt(1, guard);
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01003364 }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003365
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003366 bool CanBeMoved() const override { return GetPackedFlag<kFieldCanBeMoved>(); }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003367
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003368 bool InstructionDataEquals(const HInstruction* other) const override {
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003369 return (other->CanBeMoved() == CanBeMoved()) && (other->AsDeoptimize()->GetKind() == GetKind());
3370 }
3371
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003372 bool NeedsEnvironment() const override { return true; }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003373
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003374 bool CanThrow() const override { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003375
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003376 DeoptimizationKind GetDeoptimizationKind() const { return GetPackedField<DeoptimizeKindField>(); }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003377
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003378 bool GuardsAnInput() const {
3379 return InputCount() == 2;
3380 }
3381
3382 HInstruction* GuardedInput() const {
3383 DCHECK(GuardsAnInput());
3384 return InputAt(1);
3385 }
3386
3387 void RemoveGuard() {
3388 RemoveInputAt(1);
3389 }
3390
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003391 DECLARE_INSTRUCTION(Deoptimize);
3392
Artem Serovcced8ba2017-07-19 18:18:09 +01003393 protected:
3394 DEFAULT_COPY_CONSTRUCTOR(Deoptimize);
3395
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003396 private:
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003397 static constexpr size_t kFieldCanBeMoved = kNumberOfGenericPackedBits;
3398 static constexpr size_t kFieldDeoptimizeKind = kNumberOfGenericPackedBits + 1;
3399 static constexpr size_t kFieldDeoptimizeKindSize =
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003400 MinimumBitsToStore(static_cast<size_t>(DeoptimizationKind::kLast));
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003401 static constexpr size_t kNumberOfDeoptimizePackedBits =
3402 kFieldDeoptimizeKind + kFieldDeoptimizeKindSize;
3403 static_assert(kNumberOfDeoptimizePackedBits <= kMaxNumberOfPackedBits,
3404 "Too many packed fields.");
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003405 using DeoptimizeKindField =
3406 BitField<DeoptimizationKind, kFieldDeoptimizeKind, kFieldDeoptimizeKindSize>;
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003407};
3408
Mingyao Yang063fc772016-08-02 11:02:54 -07003409// Represents a should_deoptimize flag. Currently used for CHA-based devirtualization.
3410// The compiled code checks this flag value in a guard before devirtualized call and
3411// if it's true, starts to do deoptimization.
3412// It has a 4-byte slot on stack.
3413// TODO: allocate a register for this flag.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003414class HShouldDeoptimizeFlag final : public HVariableInputSizeInstruction {
Mingyao Yang063fc772016-08-02 11:02:54 -07003415 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003416 // CHA guards are only optimized in a separate pass and it has no side effects
3417 // with regard to other passes.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003418 HShouldDeoptimizeFlag(ArenaAllocator* allocator, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303419 : HVariableInputSizeInstruction(kShouldDeoptimizeFlag,
Vladimir Markobd785672018-05-03 17:09:09 +01003420 DataType::Type::kInt32,
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303421 SideEffects::None(),
3422 dex_pc,
3423 allocator,
3424 0,
3425 kArenaAllocCHA) {
Mingyao Yang063fc772016-08-02 11:02:54 -07003426 }
3427
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003428 // We do all CHA guard elimination/motion in a single pass, after which there is no
3429 // further guard elimination/motion since a guard might have been used for justification
3430 // of the elimination of another guard. Therefore, we pretend this guard cannot be moved
3431 // to avoid other optimizations trying to move it.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003432 bool CanBeMoved() const override { return false; }
Mingyao Yang063fc772016-08-02 11:02:54 -07003433
3434 DECLARE_INSTRUCTION(ShouldDeoptimizeFlag);
3435
Artem Serovcced8ba2017-07-19 18:18:09 +01003436 protected:
3437 DEFAULT_COPY_CONSTRUCTOR(ShouldDeoptimizeFlag);
Mingyao Yang063fc772016-08-02 11:02:54 -07003438};
3439
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003440// Represents the ArtMethod that was passed as a first argument to
3441// the method. It is used by instructions that depend on it, like
3442// instructions that work with the dex cache.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003443class HCurrentMethod final : public HExpression<0> {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003444 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003445 explicit HCurrentMethod(DataType::Type type, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303446 : HExpression(kCurrentMethod, type, SideEffects::None(), dex_pc) {
3447 }
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003448
3449 DECLARE_INSTRUCTION(CurrentMethod);
3450
Artem Serovcced8ba2017-07-19 18:18:09 +01003451 protected:
3452 DEFAULT_COPY_CONSTRUCTOR(CurrentMethod);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003453};
3454
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003455// Fetches an ArtMethod from the virtual table or the interface method table
3456// of a class.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003457class HClassTableGet final : public HExpression<1> {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003458 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003459 enum class TableKind {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003460 kVTable,
3461 kIMTable,
Vladimir Markoa1de9182016-02-25 11:37:38 +00003462 kLast = kIMTable
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003463 };
3464 HClassTableGet(HInstruction* cls,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003465 DataType::Type type,
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003466 TableKind kind,
3467 size_t index,
3468 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303469 : HExpression(kClassTableGet, type, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003470 index_(index) {
3471 SetPackedField<TableKindField>(kind);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003472 SetRawInputAt(0, cls);
3473 }
3474
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003475 bool IsClonable() const override { return true; }
3476 bool CanBeMoved() const override { return true; }
3477 bool InstructionDataEquals(const HInstruction* other) const override {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003478 return other->AsClassTableGet()->GetIndex() == index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00003479 other->AsClassTableGet()->GetPackedFields() == GetPackedFields();
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003480 }
3481
Vladimir Markoa1de9182016-02-25 11:37:38 +00003482 TableKind GetTableKind() const { return GetPackedField<TableKindField>(); }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003483 size_t GetIndex() const { return index_; }
3484
3485 DECLARE_INSTRUCTION(ClassTableGet);
3486
Artem Serovcced8ba2017-07-19 18:18:09 +01003487 protected:
3488 DEFAULT_COPY_CONSTRUCTOR(ClassTableGet);
3489
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003490 private:
Vladimir Markobd785672018-05-03 17:09:09 +01003491 static constexpr size_t kFieldTableKind = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003492 static constexpr size_t kFieldTableKindSize =
3493 MinimumBitsToStore(static_cast<size_t>(TableKind::kLast));
3494 static constexpr size_t kNumberOfClassTableGetPackedBits = kFieldTableKind + kFieldTableKindSize;
3495 static_assert(kNumberOfClassTableGetPackedBits <= kMaxNumberOfPackedBits,
3496 "Too many packed fields.");
3497 using TableKindField = BitField<TableKind, kFieldTableKind, kFieldTableKind>;
3498
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003499 // The index of the ArtMethod in the table.
3500 const size_t index_;
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003501};
3502
Mark Mendellfe57faa2015-09-18 09:26:15 -04003503// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
3504// have one successor for each entry in the switch table, and the final successor
3505// will be the block containing the next Dex opcode.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003506class HPackedSwitch final : public HExpression<1> {
Mark Mendellfe57faa2015-09-18 09:26:15 -04003507 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003508 HPackedSwitch(int32_t start_value,
3509 uint32_t num_entries,
3510 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04003511 uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01003512 : HExpression(kPackedSwitch, SideEffects::None(), dex_pc),
Mark Mendellfe57faa2015-09-18 09:26:15 -04003513 start_value_(start_value),
3514 num_entries_(num_entries) {
3515 SetRawInputAt(0, input);
3516 }
3517
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003518 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01003519
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003520 bool IsControlFlow() const override { return true; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04003521
3522 int32_t GetStartValue() const { return start_value_; }
3523
Vladimir Marko211c2112015-09-24 16:52:33 +01003524 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04003525
3526 HBasicBlock* GetDefaultBlock() const {
3527 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01003528 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04003529 }
3530 DECLARE_INSTRUCTION(PackedSwitch);
3531
Artem Serovcced8ba2017-07-19 18:18:09 +01003532 protected:
3533 DEFAULT_COPY_CONSTRUCTOR(PackedSwitch);
3534
Mark Mendellfe57faa2015-09-18 09:26:15 -04003535 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003536 const int32_t start_value_;
3537 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04003538};
3539
Roland Levillain88cb1752014-10-20 16:36:47 +01003540class HUnaryOperation : public HExpression<1> {
3541 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303542 HUnaryOperation(InstructionKind kind,
3543 DataType::Type result_type,
3544 HInstruction* input,
3545 uint32_t dex_pc = kNoDexPc)
3546 : HExpression(kind, result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01003547 SetRawInputAt(0, input);
3548 }
3549
Artem Serovcced8ba2017-07-19 18:18:09 +01003550 // All of the UnaryOperation instructions are clonable.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003551 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01003552
Roland Levillain88cb1752014-10-20 16:36:47 +01003553 HInstruction* GetInput() const { return InputAt(0); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003554 DataType::Type GetResultType() const { return GetType(); }
Roland Levillain88cb1752014-10-20 16:36:47 +01003555
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003556 bool CanBeMoved() const override { return true; }
3557 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003558 return true;
3559 }
Roland Levillain88cb1752014-10-20 16:36:47 +01003560
Roland Levillain31dd3d62016-02-16 12:21:02 +00003561 // Try to statically evaluate `this` and return a HConstant
3562 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003563 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003564 HConstant* TryStaticEvaluation() const;
3565
3566 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003567 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
3568 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003569 virtual HConstant* Evaluate(HFloatConstant* x) const = 0;
3570 virtual HConstant* Evaluate(HDoubleConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01003571
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003572 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01003573
Artem Serovcced8ba2017-07-19 18:18:09 +01003574 protected:
3575 DEFAULT_COPY_CONSTRUCTOR(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01003576};
3577
Dave Allison20dfc792014-06-16 20:44:29 -07003578class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003579 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303580 HBinaryOperation(InstructionKind kind,
3581 DataType::Type result_type,
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003582 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003583 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003584 SideEffects side_effects = SideEffects::None(),
3585 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303586 : HExpression(kind, result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003587 SetRawInputAt(0, left);
3588 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003589 }
3590
Artem Serovcced8ba2017-07-19 18:18:09 +01003591 // All of the BinaryOperation instructions are clonable.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003592 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01003593
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003594 HInstruction* GetLeft() const { return InputAt(0); }
3595 HInstruction* GetRight() const { return InputAt(1); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003596 DataType::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003597
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003598 virtual bool IsCommutative() const { return false; }
3599
3600 // Put constant on the right.
3601 // Returns whether order is changed.
3602 bool OrderInputsWithConstantOnTheRight() {
3603 HInstruction* left = InputAt(0);
3604 HInstruction* right = InputAt(1);
3605 if (left->IsConstant() && !right->IsConstant()) {
3606 ReplaceInput(right, 0);
3607 ReplaceInput(left, 1);
3608 return true;
3609 }
3610 return false;
3611 }
3612
3613 // Order inputs by instruction id, but favor constant on the right side.
3614 // This helps GVN for commutative ops.
3615 void OrderInputs() {
3616 DCHECK(IsCommutative());
3617 HInstruction* left = InputAt(0);
3618 HInstruction* right = InputAt(1);
3619 if (left == right || (!left->IsConstant() && right->IsConstant())) {
3620 return;
3621 }
3622 if (OrderInputsWithConstantOnTheRight()) {
3623 return;
3624 }
3625 // Order according to instruction id.
3626 if (left->GetId() > right->GetId()) {
3627 ReplaceInput(right, 0);
3628 ReplaceInput(left, 1);
3629 }
3630 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003631
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003632 bool CanBeMoved() const override { return true; }
3633 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003634 return true;
3635 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003636
Roland Levillain31dd3d62016-02-16 12:21:02 +00003637 // Try to statically evaluate `this` and return a HConstant
3638 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003639 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003640 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01003641
3642 // Apply this operation to `x` and `y`.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003643 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3644 HNullConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003645 LOG(FATAL) << DebugName() << " is not defined for the (null, null) case.";
3646 UNREACHABLE();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003647 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003648 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
3649 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
Roland Levillain9867bc72015-08-05 10:21:34 +01003650 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
3651 HIntConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003652 LOG(FATAL) << DebugName() << " is not defined for the (long, int) case.";
3653 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01003654 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003655 virtual HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const = 0;
3656 virtual HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const = 0;
Roland Levillain556c3d12014-09-18 15:25:07 +01003657
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003658 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003659 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003660 HConstant* GetConstantRight() const;
3661
3662 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003663 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003664 HInstruction* GetLeastConstantLeft() const;
3665
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003666 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01003667
Artem Serovcced8ba2017-07-19 18:18:09 +01003668 protected:
3669 DEFAULT_COPY_CONSTRUCTOR(BinaryOperation);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003670};
3671
Mark Mendellc4701932015-04-10 13:18:51 -04003672// The comparison bias applies for floating point operations and indicates how NaN
3673// comparisons are treated:
Vladimir Marko9974e3c2020-06-10 16:27:06 +01003674enum class ComparisonBias { // private marker to avoid generate-operator-out.py from processing.
Mark Mendellc4701932015-04-10 13:18:51 -04003675 kNoBias, // bias is not applicable (i.e. for long operation)
3676 kGtBias, // return 1 for NaN comparisons
3677 kLtBias, // return -1 for NaN comparisons
Vladimir Markoa1de9182016-02-25 11:37:38 +00003678 kLast = kLtBias
Mark Mendellc4701932015-04-10 13:18:51 -04003679};
3680
Vladimir Marko9974e3c2020-06-10 16:27:06 +01003681std::ostream& operator<<(std::ostream& os, ComparisonBias rhs);
Roland Levillain31dd3d62016-02-16 12:21:02 +00003682
Dave Allison20dfc792014-06-16 20:44:29 -07003683class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003684 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303685 HCondition(InstructionKind kind,
3686 HInstruction* first,
3687 HInstruction* second,
3688 uint32_t dex_pc = kNoDexPc)
3689 : HBinaryOperation(kind,
3690 DataType::Type::kBool,
3691 first,
3692 second,
3693 SideEffects::None(),
3694 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003695 SetPackedField<ComparisonBiasField>(ComparisonBias::kNoBias);
3696 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003697
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003698 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003699 // `instruction`, and disregard moves in between.
3700 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003701
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003702 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003703
3704 virtual IfCondition GetCondition() const = 0;
3705
Mark Mendellc4701932015-04-10 13:18:51 -04003706 virtual IfCondition GetOppositeCondition() const = 0;
3707
Vladimir Markoa1de9182016-02-25 11:37:38 +00003708 bool IsGtBias() const { return GetBias() == ComparisonBias::kGtBias; }
Anton Shaminbdd79352016-02-15 12:48:36 +06003709 bool IsLtBias() const { return GetBias() == ComparisonBias::kLtBias; }
3710
Vladimir Markoa1de9182016-02-25 11:37:38 +00003711 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
3712 void SetBias(ComparisonBias bias) { SetPackedField<ComparisonBiasField>(bias); }
Mark Mendellc4701932015-04-10 13:18:51 -04003713
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003714 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003715 return GetPackedFields() == other->AsCondition()->GetPackedFields();
Mark Mendellc4701932015-04-10 13:18:51 -04003716 }
3717
Roland Levillain4fa13f62015-07-06 18:11:54 +01003718 bool IsFPConditionTrueIfNaN() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003719 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003720 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003721 if (if_cond == kCondNE) {
3722 return true;
3723 } else if (if_cond == kCondEQ) {
3724 return false;
3725 }
3726 return ((if_cond == kCondGT) || (if_cond == kCondGE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003727 }
3728
3729 bool IsFPConditionFalseIfNaN() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003730 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003731 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003732 if (if_cond == kCondEQ) {
3733 return true;
3734 } else if (if_cond == kCondNE) {
3735 return false;
3736 }
3737 return ((if_cond == kCondLT) || (if_cond == kCondLE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003738 }
3739
Roland Levillain31dd3d62016-02-16 12:21:02 +00003740 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003741 // Needed if we merge a HCompare into a HCondition.
Vladimir Markobd785672018-05-03 17:09:09 +01003742 static constexpr size_t kFieldComparisonBias = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003743 static constexpr size_t kFieldComparisonBiasSize =
3744 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3745 static constexpr size_t kNumberOfConditionPackedBits =
3746 kFieldComparisonBias + kFieldComparisonBiasSize;
3747 static_assert(kNumberOfConditionPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3748 using ComparisonBiasField =
3749 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3750
Roland Levillain31dd3d62016-02-16 12:21:02 +00003751 template <typename T>
3752 int32_t Compare(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3753
3754 template <typename T>
3755 int32_t CompareFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003756 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003757 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3758 // Handle the bias.
3759 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compare(x, y);
3760 }
3761
3762 // Return an integer constant containing the result of a condition evaluated at compile time.
3763 HIntConstant* MakeConstantCondition(bool value, uint32_t dex_pc) const {
3764 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3765 }
3766
Artem Serovcced8ba2017-07-19 18:18:09 +01003767 DEFAULT_COPY_CONSTRUCTOR(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003768};
3769
3770// Instruction to check if two inputs are equal to each other.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003771class HEqual final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003772 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003773 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303774 : HCondition(kEqual, first, second, dex_pc) {
3775 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003776
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003777 bool IsCommutative() const override { return true; }
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003778
Vladimir Marko9e23df52015-11-10 17:14:35 +00003779 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003780 HNullConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003781 return MakeConstantCondition(true, GetDexPc());
3782 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003783 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003784 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3785 }
3786 // In the following Evaluate methods, a HCompare instruction has
3787 // been merged into this HEqual instruction; evaluate it as
3788 // `Compare(x, y) == 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003789 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003790 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0),
3791 GetDexPc());
3792 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003793 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003794 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3795 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003796 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003797 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003798 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003799
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003800 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003801
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003802 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07003803 return kCondEQ;
3804 }
3805
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003806 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04003807 return kCondNE;
3808 }
3809
Artem Serovcced8ba2017-07-19 18:18:09 +01003810 protected:
3811 DEFAULT_COPY_CONSTRUCTOR(Equal);
3812
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003813 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003814 template <typename T> static bool Compute(T x, T y) { return x == y; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003815};
3816
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003817class HNotEqual final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003818 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303819 HNotEqual(HInstruction* first, HInstruction* second,
3820 uint32_t dex_pc = kNoDexPc)
3821 : HCondition(kNotEqual, first, second, dex_pc) {
3822 }
Dave Allison20dfc792014-06-16 20:44:29 -07003823
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003824 bool IsCommutative() const override { return true; }
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003825
Vladimir Marko9e23df52015-11-10 17:14:35 +00003826 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003827 HNullConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003828 return MakeConstantCondition(false, GetDexPc());
3829 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003830 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003831 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3832 }
3833 // In the following Evaluate methods, a HCompare instruction has
3834 // been merged into this HNotEqual instruction; evaluate it as
3835 // `Compare(x, y) != 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003836 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003837 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3838 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003839 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003840 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3841 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003842 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003843 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003844 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003845
Dave Allison20dfc792014-06-16 20:44:29 -07003846 DECLARE_INSTRUCTION(NotEqual);
3847
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003848 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07003849 return kCondNE;
3850 }
3851
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003852 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04003853 return kCondEQ;
3854 }
3855
Artem Serovcced8ba2017-07-19 18:18:09 +01003856 protected:
3857 DEFAULT_COPY_CONSTRUCTOR(NotEqual);
3858
Dave Allison20dfc792014-06-16 20:44:29 -07003859 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003860 template <typename T> static bool Compute(T x, T y) { return x != y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003861};
3862
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003863class HLessThan final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003864 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303865 HLessThan(HInstruction* first, HInstruction* second,
3866 uint32_t dex_pc = kNoDexPc)
3867 : HCondition(kLessThan, first, second, dex_pc) {
3868 }
Dave Allison20dfc792014-06-16 20:44:29 -07003869
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003870 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003871 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003872 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003873 // In the following Evaluate methods, a HCompare instruction has
3874 // been merged into this HLessThan instruction; evaluate it as
3875 // `Compare(x, y) < 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003876 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003877 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3878 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003879 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003880 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3881 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003882 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003883 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003884 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003885
Dave Allison20dfc792014-06-16 20:44:29 -07003886 DECLARE_INSTRUCTION(LessThan);
3887
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003888 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07003889 return kCondLT;
3890 }
3891
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003892 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04003893 return kCondGE;
3894 }
3895
Artem Serovcced8ba2017-07-19 18:18:09 +01003896 protected:
3897 DEFAULT_COPY_CONSTRUCTOR(LessThan);
3898
Dave Allison20dfc792014-06-16 20:44:29 -07003899 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003900 template <typename T> static bool Compute(T x, T y) { return x < y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003901};
3902
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003903class HLessThanOrEqual final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003904 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303905 HLessThanOrEqual(HInstruction* first, HInstruction* second,
3906 uint32_t dex_pc = kNoDexPc)
3907 : HCondition(kLessThanOrEqual, first, second, dex_pc) {
3908 }
Dave Allison20dfc792014-06-16 20:44:29 -07003909
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003910 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003911 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003912 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003913 // In the following Evaluate methods, a HCompare instruction has
3914 // been merged into this HLessThanOrEqual instruction; evaluate it as
3915 // `Compare(x, y) <= 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003916 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003917 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3918 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003919 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003920 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3921 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003922 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003923 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003924 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003925
Dave Allison20dfc792014-06-16 20:44:29 -07003926 DECLARE_INSTRUCTION(LessThanOrEqual);
3927
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003928 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07003929 return kCondLE;
3930 }
3931
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003932 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04003933 return kCondGT;
3934 }
3935
Artem Serovcced8ba2017-07-19 18:18:09 +01003936 protected:
3937 DEFAULT_COPY_CONSTRUCTOR(LessThanOrEqual);
3938
Dave Allison20dfc792014-06-16 20:44:29 -07003939 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003940 template <typename T> static bool Compute(T x, T y) { return x <= y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003941};
3942
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003943class HGreaterThan final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003944 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003945 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303946 : HCondition(kGreaterThan, first, second, dex_pc) {
3947 }
Dave Allison20dfc792014-06-16 20:44:29 -07003948
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003949 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003950 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003951 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003952 // In the following Evaluate methods, a HCompare instruction has
3953 // been merged into this HGreaterThan instruction; evaluate it as
3954 // `Compare(x, y) > 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003955 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003956 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3957 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003958 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003959 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3960 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003961 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003962 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003963 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003964
Dave Allison20dfc792014-06-16 20:44:29 -07003965 DECLARE_INSTRUCTION(GreaterThan);
3966
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003967 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07003968 return kCondGT;
3969 }
3970
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003971 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04003972 return kCondLE;
3973 }
3974
Artem Serovcced8ba2017-07-19 18:18:09 +01003975 protected:
3976 DEFAULT_COPY_CONSTRUCTOR(GreaterThan);
3977
Dave Allison20dfc792014-06-16 20:44:29 -07003978 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003979 template <typename T> static bool Compute(T x, T y) { return x > y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003980};
3981
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003982class HGreaterThanOrEqual final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003983 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003984 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303985 : HCondition(kGreaterThanOrEqual, first, second, dex_pc) {
3986 }
Dave Allison20dfc792014-06-16 20:44:29 -07003987
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003988 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003989 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003990 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003991 // In the following Evaluate methods, a HCompare instruction has
3992 // been merged into this HGreaterThanOrEqual instruction; evaluate it as
3993 // `Compare(x, y) >= 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003994 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003995 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3996 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003997 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003998 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3999 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004000 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004001 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004002 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004003
Dave Allison20dfc792014-06-16 20:44:29 -07004004 DECLARE_INSTRUCTION(GreaterThanOrEqual);
4005
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004006 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07004007 return kCondGE;
4008 }
4009
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004010 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04004011 return kCondLT;
4012 }
4013
Artem Serovcced8ba2017-07-19 18:18:09 +01004014 protected:
4015 DEFAULT_COPY_CONSTRUCTOR(GreaterThanOrEqual);
4016
Dave Allison20dfc792014-06-16 20:44:29 -07004017 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004018 template <typename T> static bool Compute(T x, T y) { return x >= y; }
Dave Allison20dfc792014-06-16 20:44:29 -07004019};
4020
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004021class HBelow final : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07004022 public:
4023 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304024 : HCondition(kBelow, first, second, dex_pc) {
4025 }
Aart Bike9f37602015-10-09 11:15:55 -07004026
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004027 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004028 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07004029 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004030 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004031 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4032 }
4033 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004034 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004035 LOG(FATAL) << DebugName() << " is not defined for float values";
4036 UNREACHABLE();
4037 }
4038 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004039 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004040 LOG(FATAL) << DebugName() << " is not defined for double values";
4041 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07004042 }
4043
4044 DECLARE_INSTRUCTION(Below);
4045
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004046 IfCondition GetCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004047 return kCondB;
4048 }
4049
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004050 IfCondition GetOppositeCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004051 return kCondAE;
4052 }
4053
Artem Serovcced8ba2017-07-19 18:18:09 +01004054 protected:
4055 DEFAULT_COPY_CONSTRUCTOR(Below);
4056
Aart Bike9f37602015-10-09 11:15:55 -07004057 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004058 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004059 return MakeUnsigned(x) < MakeUnsigned(y);
4060 }
Aart Bike9f37602015-10-09 11:15:55 -07004061};
4062
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004063class HBelowOrEqual final : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07004064 public:
4065 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304066 : HCondition(kBelowOrEqual, first, second, dex_pc) {
4067 }
Aart Bike9f37602015-10-09 11:15:55 -07004068
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004069 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004070 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07004071 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004072 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004073 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4074 }
4075 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004076 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004077 LOG(FATAL) << DebugName() << " is not defined for float values";
4078 UNREACHABLE();
4079 }
4080 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004081 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004082 LOG(FATAL) << DebugName() << " is not defined for double values";
4083 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07004084 }
4085
4086 DECLARE_INSTRUCTION(BelowOrEqual);
4087
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004088 IfCondition GetCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004089 return kCondBE;
4090 }
4091
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004092 IfCondition GetOppositeCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004093 return kCondA;
4094 }
4095
Artem Serovcced8ba2017-07-19 18:18:09 +01004096 protected:
4097 DEFAULT_COPY_CONSTRUCTOR(BelowOrEqual);
4098
Aart Bike9f37602015-10-09 11:15:55 -07004099 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004100 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004101 return MakeUnsigned(x) <= MakeUnsigned(y);
4102 }
Aart Bike9f37602015-10-09 11:15:55 -07004103};
4104
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004105class HAbove final : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07004106 public:
4107 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304108 : HCondition(kAbove, first, second, dex_pc) {
4109 }
Aart Bike9f37602015-10-09 11:15:55 -07004110
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004111 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004112 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07004113 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004114 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004115 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4116 }
4117 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004118 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004119 LOG(FATAL) << DebugName() << " is not defined for float values";
4120 UNREACHABLE();
4121 }
4122 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004123 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004124 LOG(FATAL) << DebugName() << " is not defined for double values";
4125 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07004126 }
4127
4128 DECLARE_INSTRUCTION(Above);
4129
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004130 IfCondition GetCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004131 return kCondA;
4132 }
4133
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004134 IfCondition GetOppositeCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004135 return kCondBE;
4136 }
4137
Artem Serovcced8ba2017-07-19 18:18:09 +01004138 protected:
4139 DEFAULT_COPY_CONSTRUCTOR(Above);
4140
Aart Bike9f37602015-10-09 11:15:55 -07004141 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004142 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004143 return MakeUnsigned(x) > MakeUnsigned(y);
4144 }
Aart Bike9f37602015-10-09 11:15:55 -07004145};
4146
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004147class HAboveOrEqual final : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07004148 public:
4149 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304150 : HCondition(kAboveOrEqual, first, second, dex_pc) {
4151 }
Aart Bike9f37602015-10-09 11:15:55 -07004152
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004153 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004154 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07004155 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004156 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004157 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4158 }
4159 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004160 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004161 LOG(FATAL) << DebugName() << " is not defined for float values";
4162 UNREACHABLE();
4163 }
4164 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004165 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004166 LOG(FATAL) << DebugName() << " is not defined for double values";
4167 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07004168 }
4169
4170 DECLARE_INSTRUCTION(AboveOrEqual);
4171
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004172 IfCondition GetCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004173 return kCondAE;
4174 }
4175
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004176 IfCondition GetOppositeCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004177 return kCondB;
4178 }
4179
Artem Serovcced8ba2017-07-19 18:18:09 +01004180 protected:
4181 DEFAULT_COPY_CONSTRUCTOR(AboveOrEqual);
4182
Aart Bike9f37602015-10-09 11:15:55 -07004183 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004184 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004185 return MakeUnsigned(x) >= MakeUnsigned(y);
4186 }
Aart Bike9f37602015-10-09 11:15:55 -07004187};
Dave Allison20dfc792014-06-16 20:44:29 -07004188
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004189// Instruction to check how two inputs compare to each other.
4190// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004191class HCompare final : public HBinaryOperation {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004192 public:
Roland Levillaina5c4a402016-03-15 15:02:50 +00004193 // Note that `comparison_type` is the type of comparison performed
4194 // between the comparison's inputs, not the type of the instantiated
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004195 // HCompare instruction (which is always DataType::Type::kInt).
4196 HCompare(DataType::Type comparison_type,
Alexey Frunze4dda3372015-06-01 18:31:49 -07004197 HInstruction* first,
4198 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04004199 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07004200 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304201 : HBinaryOperation(kCompare,
4202 DataType::Type::kInt32,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004203 first,
4204 second,
Roland Levillaina5c4a402016-03-15 15:02:50 +00004205 SideEffectsForArchRuntimeCalls(comparison_type),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004206 dex_pc) {
4207 SetPackedField<ComparisonBiasField>(bias);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004208 }
4209
Roland Levillain9867bc72015-08-05 10:21:34 +01004210 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004211 int32_t Compute(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
4212
4213 template <typename T>
4214 int32_t ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004215 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00004216 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
4217 // Handle the bias.
4218 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compute(x, y);
4219 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004220
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004221 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004222 // Note that there is no "cmp-int" Dex instruction so we shouldn't
4223 // reach this code path when processing a freshly built HIR
4224 // graph. However HCompare integer instructions can be synthesized
4225 // by the instruction simplifier to implement IntegerCompare and
4226 // IntegerSignum intrinsics, so we have to handle this case.
4227 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004228 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004229 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004230 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4231 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004232 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004233 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4234 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004235 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004236 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01004237 }
4238
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004239 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004240 return GetPackedFields() == other->AsCompare()->GetPackedFields();
Calin Juravleddb7df22014-11-25 20:56:51 +00004241 }
4242
Vladimir Markoa1de9182016-02-25 11:37:38 +00004243 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
Mark Mendellc4701932015-04-10 13:18:51 -04004244
Roland Levillain31dd3d62016-02-16 12:21:02 +00004245 // Does this compare instruction have a "gt bias" (vs an "lt bias")?
Vladimir Markoa1de9182016-02-25 11:37:38 +00004246 // Only meaningful for floating-point comparisons.
Roland Levillain31dd3d62016-02-16 12:21:02 +00004247 bool IsGtBias() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004248 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004249 return GetBias() == ComparisonBias::kGtBias;
Roland Levillain31dd3d62016-02-16 12:21:02 +00004250 }
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004251
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004252 static SideEffects SideEffectsForArchRuntimeCalls(DataType::Type type ATTRIBUTE_UNUSED) {
Roland Levillain1693a1f2016-03-15 14:57:31 +00004253 // Comparisons do not require a runtime call in any back end.
4254 return SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004255 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004256
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004257 DECLARE_INSTRUCTION(Compare);
4258
Roland Levillain31dd3d62016-02-16 12:21:02 +00004259 protected:
Vladimir Markobd785672018-05-03 17:09:09 +01004260 static constexpr size_t kFieldComparisonBias = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004261 static constexpr size_t kFieldComparisonBiasSize =
4262 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
4263 static constexpr size_t kNumberOfComparePackedBits =
4264 kFieldComparisonBias + kFieldComparisonBiasSize;
4265 static_assert(kNumberOfComparePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
4266 using ComparisonBiasField =
4267 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
4268
Roland Levillain31dd3d62016-02-16 12:21:02 +00004269 // Return an integer constant containing the result of a comparison evaluated at compile time.
4270 HIntConstant* MakeConstantComparison(int32_t value, uint32_t dex_pc) const {
4271 DCHECK(value == -1 || value == 0 || value == 1) << value;
4272 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
4273 }
4274
Artem Serovcced8ba2017-07-19 18:18:09 +01004275 DEFAULT_COPY_CONSTRUCTOR(Compare);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004276};
4277
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004278class HNewInstance final : public HExpression<1> {
David Brazdil6de19382016-01-08 17:37:10 +00004279 public:
4280 HNewInstance(HInstruction* cls,
David Brazdil6de19382016-01-08 17:37:10 +00004281 uint32_t dex_pc,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004282 dex::TypeIndex type_index,
David Brazdil6de19382016-01-08 17:37:10 +00004283 const DexFile& dex_file,
David Brazdil6de19382016-01-08 17:37:10 +00004284 bool finalizable,
4285 QuickEntrypointEnum entrypoint)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304286 : HExpression(kNewInstance,
4287 DataType::Type::kReference,
4288 SideEffects::CanTriggerGC(),
4289 dex_pc),
David Brazdil6de19382016-01-08 17:37:10 +00004290 type_index_(type_index),
4291 dex_file_(dex_file),
David Brazdil6de19382016-01-08 17:37:10 +00004292 entrypoint_(entrypoint) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004293 SetPackedFlag<kFlagFinalizable>(finalizable);
David Brazdil6de19382016-01-08 17:37:10 +00004294 SetRawInputAt(0, cls);
David Brazdil6de19382016-01-08 17:37:10 +00004295 }
4296
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004297 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004298
Andreas Gampea5b09a62016-11-17 15:21:22 -08004299 dex::TypeIndex GetTypeIndex() const { return type_index_; }
David Brazdil6de19382016-01-08 17:37:10 +00004300 const DexFile& GetDexFile() const { return dex_file_; }
4301
4302 // Calls runtime so needs an environment.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004303 bool NeedsEnvironment() const override { return true; }
David Brazdil6de19382016-01-08 17:37:10 +00004304
Mingyao Yang062157f2016-03-02 10:15:36 -08004305 // Can throw errors when out-of-memory or if it's not instantiable/accessible.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004306 bool CanThrow() const override { return true; }
Mingyao Yang062157f2016-03-02 10:15:36 -08004307
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004308 bool NeedsChecks() const {
4309 return entrypoint_ == kQuickAllocObjectWithChecks;
4310 }
David Brazdil6de19382016-01-08 17:37:10 +00004311
Vladimir Markoa1de9182016-02-25 11:37:38 +00004312 bool IsFinalizable() const { return GetPackedFlag<kFlagFinalizable>(); }
David Brazdil6de19382016-01-08 17:37:10 +00004313
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004314 bool CanBeNull() const override { return false; }
David Brazdil6de19382016-01-08 17:37:10 +00004315
4316 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
4317
4318 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
4319 entrypoint_ = entrypoint;
4320 }
4321
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004322 HLoadClass* GetLoadClass() const {
4323 HInstruction* input = InputAt(0);
4324 if (input->IsClinitCheck()) {
4325 input = input->InputAt(0);
4326 }
4327 DCHECK(input->IsLoadClass());
4328 return input->AsLoadClass();
4329 }
4330
David Brazdil6de19382016-01-08 17:37:10 +00004331 bool IsStringAlloc() const;
4332
4333 DECLARE_INSTRUCTION(NewInstance);
4334
Artem Serovcced8ba2017-07-19 18:18:09 +01004335 protected:
4336 DEFAULT_COPY_CONSTRUCTOR(NewInstance);
4337
David Brazdil6de19382016-01-08 17:37:10 +00004338 private:
Vladimir Markobd785672018-05-03 17:09:09 +01004339 static constexpr size_t kFlagFinalizable = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004340 static constexpr size_t kNumberOfNewInstancePackedBits = kFlagFinalizable + 1;
4341 static_assert(kNumberOfNewInstancePackedBits <= kMaxNumberOfPackedBits,
4342 "Too many packed fields.");
4343
Andreas Gampea5b09a62016-11-17 15:21:22 -08004344 const dex::TypeIndex type_index_;
David Brazdil6de19382016-01-08 17:37:10 +00004345 const DexFile& dex_file_;
David Brazdil6de19382016-01-08 17:37:10 +00004346 QuickEntrypointEnum entrypoint_;
David Brazdil6de19382016-01-08 17:37:10 +00004347};
4348
Agi Csaki05f20562015-08-19 14:58:14 -07004349enum IntrinsicNeedsEnvironmentOrCache {
4350 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
4351 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07004352};
4353
Aart Bik5d75afe2015-12-14 11:57:01 -08004354enum IntrinsicSideEffects {
4355 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
4356 kReadSideEffects, // Intrinsic may read heap memory.
4357 kWriteSideEffects, // Intrinsic may write heap memory.
4358 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
4359};
4360
4361enum IntrinsicExceptions {
4362 kNoThrow, // Intrinsic does not throw any exceptions.
4363 kCanThrow // Intrinsic may throw exceptions.
4364};
4365
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004366class HInvoke : public HVariableInputSizeInstruction {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004367 public:
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004368 bool NeedsEnvironment() const override;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004369
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01004370 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004371 SetRawInputAt(index, argument);
4372 }
4373
Roland Levillain3e3d7332015-04-28 11:00:54 +01004374 // Return the number of arguments. This number can be lower than
4375 // the number of inputs returned by InputCount(), as some invoke
4376 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
4377 // inputs at the end of their list of inputs.
4378 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
4379
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004380 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
4381
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004382 InvokeType GetInvokeType() const {
4383 return GetPackedField<InvokeTypeField>();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004384 }
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004385
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01004386 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004387 return intrinsic_;
4388 }
4389
Aart Bik5d75afe2015-12-14 11:57:01 -08004390 void SetIntrinsic(Intrinsics intrinsic,
4391 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
4392 IntrinsicSideEffects side_effects,
4393 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004394
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01004395 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00004396 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01004397 }
4398
Aart Bikff7d89c2016-11-07 08:49:28 -08004399 void SetCanThrow(bool can_throw) { SetPackedFlag<kFlagCanThrow>(can_throw); }
4400
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004401 bool CanThrow() const override { return GetPackedFlag<kFlagCanThrow>(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08004402
Aart Bika8b8e9b2018-01-09 11:01:02 -08004403 void SetAlwaysThrows(bool always_throws) { SetPackedFlag<kFlagAlwaysThrows>(always_throws); }
4404
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004405 bool AlwaysThrows() const override { return GetPackedFlag<kFlagAlwaysThrows>(); }
Aart Bika8b8e9b2018-01-09 11:01:02 -08004406
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004407 bool CanBeMoved() const override { return IsIntrinsic() && !DoesAnyWrite(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08004408
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004409 bool InstructionDataEquals(const HInstruction* other) const override {
Aart Bik5d75afe2015-12-14 11:57:01 -08004410 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
4411 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01004412
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004413 uint32_t* GetIntrinsicOptimizations() {
4414 return &intrinsic_optimizations_;
4415 }
4416
4417 const uint32_t* GetIntrinsicOptimizations() const {
4418 return &intrinsic_optimizations_;
4419 }
4420
4421 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
4422
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004423 ArtMethod* GetResolvedMethod() const { return resolved_method_; }
Nicolas Geoffray76d4bb0f32018-09-21 12:58:45 +01004424 void SetResolvedMethod(ArtMethod* method) REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004425
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004426 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004427
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004428 protected:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004429 static constexpr size_t kFieldInvokeType = kNumberOfGenericPackedBits;
4430 static constexpr size_t kFieldInvokeTypeSize =
Vladimir Markoa1de9182016-02-25 11:37:38 +00004431 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
Vladimir Markobd785672018-05-03 17:09:09 +01004432 static constexpr size_t kFlagCanThrow = kFieldInvokeType + kFieldInvokeTypeSize;
Aart Bika8b8e9b2018-01-09 11:01:02 -08004433 static constexpr size_t kFlagAlwaysThrows = kFlagCanThrow + 1;
4434 static constexpr size_t kNumberOfInvokePackedBits = kFlagAlwaysThrows + 1;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004435 static_assert(kNumberOfInvokePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004436 using InvokeTypeField = BitField<InvokeType, kFieldInvokeType, kFieldInvokeTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004437
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304438 HInvoke(InstructionKind kind,
4439 ArenaAllocator* allocator,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004440 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01004441 uint32_t number_of_other_inputs,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004442 DataType::Type return_type,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004443 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004444 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004445 ArtMethod* resolved_method,
4446 InvokeType invoke_type)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004447 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304448 kind,
Vladimir Markobd785672018-05-03 17:09:09 +01004449 return_type,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004450 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
4451 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004452 allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004453 number_of_arguments + number_of_other_inputs,
4454 kArenaAllocInvokeInputs),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004455 number_of_arguments_(number_of_arguments),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004456 dex_method_index_(dex_method_index),
agicsaki57b81ec2015-08-11 17:39:37 -07004457 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004458 intrinsic_optimizations_(0) {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004459 SetPackedField<InvokeTypeField>(invoke_type);
Vladimir Markoa1de9182016-02-25 11:37:38 +00004460 SetPackedFlag<kFlagCanThrow>(true);
Nicolas Geoffray76d4bb0f32018-09-21 12:58:45 +01004461 // Check mutator lock, constructors lack annotalysis support.
4462 Locks::mutator_lock_->AssertNotExclusiveHeld(Thread::Current());
4463 SetResolvedMethod(resolved_method);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004464 }
4465
Artem Serovcced8ba2017-07-19 18:18:09 +01004466 DEFAULT_COPY_CONSTRUCTOR(Invoke);
4467
Roland Levillain3e3d7332015-04-28 11:00:54 +01004468 uint32_t number_of_arguments_;
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00004469 ArtMethod* resolved_method_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004470 const uint32_t dex_method_index_;
4471 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004472
4473 // A magic word holding optimizations for intrinsics. See intrinsics.h.
4474 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004475};
4476
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004477class HInvokeUnresolved final : public HInvoke {
Calin Juravle175dc732015-08-25 15:42:32 +01004478 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004479 HInvokeUnresolved(ArenaAllocator* allocator,
Calin Juravle175dc732015-08-25 15:42:32 +01004480 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004481 DataType::Type return_type,
Calin Juravle175dc732015-08-25 15:42:32 +01004482 uint32_t dex_pc,
4483 uint32_t dex_method_index,
4484 InvokeType invoke_type)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304485 : HInvoke(kInvokeUnresolved,
4486 allocator,
Calin Juravle175dc732015-08-25 15:42:32 +01004487 number_of_arguments,
Andreas Gampe3db70682018-12-26 15:12:03 -08004488 /* number_of_other_inputs= */ 0u,
Calin Juravle175dc732015-08-25 15:42:32 +01004489 return_type,
4490 dex_pc,
4491 dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004492 nullptr,
Calin Juravle175dc732015-08-25 15:42:32 +01004493 invoke_type) {
4494 }
4495
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004496 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004497
Calin Juravle175dc732015-08-25 15:42:32 +01004498 DECLARE_INSTRUCTION(InvokeUnresolved);
4499
Artem Serovcced8ba2017-07-19 18:18:09 +01004500 protected:
4501 DEFAULT_COPY_CONSTRUCTOR(InvokeUnresolved);
Calin Juravle175dc732015-08-25 15:42:32 +01004502};
4503
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004504class HInvokePolymorphic final : public HInvoke {
Orion Hodsonac141392017-01-13 11:53:47 +00004505 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004506 HInvokePolymorphic(ArenaAllocator* allocator,
Orion Hodsonac141392017-01-13 11:53:47 +00004507 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004508 DataType::Type return_type,
Orion Hodsonac141392017-01-13 11:53:47 +00004509 uint32_t dex_pc,
4510 uint32_t dex_method_index)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304511 : HInvoke(kInvokePolymorphic,
4512 allocator,
Orion Hodsonac141392017-01-13 11:53:47 +00004513 number_of_arguments,
Andreas Gampe3db70682018-12-26 15:12:03 -08004514 /* number_of_other_inputs= */ 0u,
Orion Hodsonac141392017-01-13 11:53:47 +00004515 return_type,
4516 dex_pc,
4517 dex_method_index,
4518 nullptr,
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304519 kVirtual) {
4520 }
Orion Hodsonac141392017-01-13 11:53:47 +00004521
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004522 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004523
Orion Hodsonac141392017-01-13 11:53:47 +00004524 DECLARE_INSTRUCTION(InvokePolymorphic);
4525
Artem Serovcced8ba2017-07-19 18:18:09 +01004526 protected:
4527 DEFAULT_COPY_CONSTRUCTOR(InvokePolymorphic);
Orion Hodsonac141392017-01-13 11:53:47 +00004528};
4529
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004530class HInvokeCustom final : public HInvoke {
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004531 public:
4532 HInvokeCustom(ArenaAllocator* allocator,
4533 uint32_t number_of_arguments,
4534 uint32_t call_site_index,
4535 DataType::Type return_type,
4536 uint32_t dex_pc)
4537 : HInvoke(kInvokeCustom,
4538 allocator,
4539 number_of_arguments,
Andreas Gampe3db70682018-12-26 15:12:03 -08004540 /* number_of_other_inputs= */ 0u,
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004541 return_type,
4542 dex_pc,
Andreas Gampe3db70682018-12-26 15:12:03 -08004543 /* dex_method_index= */ dex::kDexNoIndex,
4544 /* resolved_method= */ nullptr,
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004545 kStatic),
4546 call_site_index_(call_site_index) {
4547 }
4548
4549 uint32_t GetCallSiteIndex() const { return call_site_index_; }
4550
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004551 bool IsClonable() const override { return true; }
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004552
4553 DECLARE_INSTRUCTION(InvokeCustom);
4554
4555 protected:
4556 DEFAULT_COPY_CONSTRUCTOR(InvokeCustom);
4557
4558 private:
4559 uint32_t call_site_index_;
4560};
4561
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004562class HInvokeStaticOrDirect final : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004563 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01004564 // Requirements of this method call regarding the class
4565 // initialization (clinit) check of its declaring class.
Vladimir Marko9974e3c2020-06-10 16:27:06 +01004566 enum class ClinitCheckRequirement { // private marker to avoid generate-operator-out.py from processing.
Roland Levillain4c0eb422015-04-24 16:43:49 +01004567 kNone, // Class already initialized.
4568 kExplicit, // Static call having explicit clinit check as last input.
4569 kImplicit, // Static call implicitly requiring a clinit check.
Vladimir Markoa1de9182016-02-25 11:37:38 +00004570 kLast = kImplicit
Roland Levillain4c0eb422015-04-24 16:43:49 +01004571 };
4572
Vladimir Marko58155012015-08-19 12:49:41 +00004573 // Determines how to load the target ArtMethod*.
4574 enum class MethodLoadKind {
4575 // Use a String init ArtMethod* loaded from Thread entrypoints.
4576 kStringInit,
4577
4578 // Use the method's own ArtMethod* loaded by the register allocator.
4579 kRecursive,
4580
Vladimir Marko65979462017-05-19 17:25:12 +01004581 // Use PC-relative boot image ArtMethod* address that will be known at link time.
4582 // Used for boot image methods referenced by boot image code.
4583 kBootImageLinkTimePcRelative,
4584
Vladimir Markob066d432018-01-03 13:14:37 +00004585 // Load from an entry in the .data.bimg.rel.ro using a PC-relative load.
4586 // Used for app->boot calls with relocatable image.
4587 kBootImageRelRo,
4588
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004589 // Load from an entry in the .bss section using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004590 // Used for methods outside boot image referenced by AOT-compiled app and boot image code.
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004591 kBssEntry,
Vladimir Marko58155012015-08-19 12:49:41 +00004592
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004593 // Use ArtMethod* at a known address, embed the direct address in the code.
4594 // Used for for JIT-compiled calls.
4595 kJitDirectAddress,
4596
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004597 // Make a runtime call to resolve and call the method. This is the last-resort-kind
4598 // used when other kinds are unimplemented on a particular architecture.
4599 kRuntimeCall,
Vladimir Marko58155012015-08-19 12:49:41 +00004600 };
4601
4602 // Determines the location of the code pointer.
4603 enum class CodePtrLocation {
4604 // Recursive call, use local PC-relative call instruction.
4605 kCallSelf,
4606
Vladimir Marko86c87522020-05-11 16:55:55 +01004607 // Use native pointer from the Artmethod*.
4608 // Used for @CriticalNative to avoid going through the compiled stub. This call goes through
4609 // a special resolution stub if the class is not initialized or no native code is registered.
4610 kCallCriticalNative,
4611
Vladimir Marko58155012015-08-19 12:49:41 +00004612 // Use code pointer from the ArtMethod*.
4613 // Used when we don't know the target code. This is also the last-resort-kind used when
4614 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
4615 kCallArtMethod,
4616 };
4617
4618 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01004619 MethodLoadKind method_load_kind;
4620 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00004621 // The method load data holds
4622 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
4623 // Note that there are multiple string init methods, each having its own offset.
4624 // - the method address for kDirectAddress
Vladimir Markodc151b22015-10-15 18:02:30 +01004625 uint64_t method_load_data;
Vladimir Marko58155012015-08-19 12:49:41 +00004626 };
4627
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004628 HInvokeStaticOrDirect(ArenaAllocator* allocator,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004629 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004630 DataType::Type return_type,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004631 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004632 uint32_t method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004633 ArtMethod* resolved_method,
Vladimir Marko58155012015-08-19 12:49:41 +00004634 DispatchInfo dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004635 InvokeType invoke_type,
4636 MethodReference target_method,
Roland Levillain4c0eb422015-04-24 16:43:49 +01004637 ClinitCheckRequirement clinit_check_requirement)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304638 : HInvoke(kInvokeStaticOrDirect,
4639 allocator,
Roland Levillain3e3d7332015-04-28 11:00:54 +01004640 number_of_arguments,
Vladimir Marko86c87522020-05-11 16:55:55 +01004641 // There is potentially one extra argument for the HCurrentMethod input,
4642 // and one other if the clinit check is explicit. These can be removed later.
4643 (NeedsCurrentMethodInput(dispatch_info) ? 1u : 0u) +
David Brazdildee58d62016-04-07 09:54:26 +00004644 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004645 return_type,
4646 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004647 method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004648 resolved_method,
4649 invoke_type),
Vladimir Marko58155012015-08-19 12:49:41 +00004650 target_method_(target_method),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004651 dispatch_info_(dispatch_info) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004652 SetPackedField<ClinitCheckRequirementField>(clinit_check_requirement);
4653 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004654
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004655 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004656
Vladimir Marko86c87522020-05-11 16:55:55 +01004657 void SetDispatchInfo(DispatchInfo dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00004658 bool had_current_method_input = HasCurrentMethodInput();
Vladimir Marko86c87522020-05-11 16:55:55 +01004659 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info);
Vladimir Markob554b5a2015-11-06 12:57:55 +00004660
4661 // Using the current method is the default and once we find a better
4662 // method load kind, we should not go back to using the current method.
4663 DCHECK(had_current_method_input || !needs_current_method_input);
4664
4665 if (had_current_method_input && !needs_current_method_input) {
Vladimir Marko86c87522020-05-11 16:55:55 +01004666 DCHECK_EQ(InputAt(GetCurrentMethodIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
4667 RemoveInputAt(GetCurrentMethodIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004668 }
Vladimir Markodc151b22015-10-15 18:02:30 +01004669 dispatch_info_ = dispatch_info;
4670 }
4671
Aart Bik6daebeb2017-04-03 14:35:41 -07004672 DispatchInfo GetDispatchInfo() const {
4673 return dispatch_info_;
4674 }
4675
Vladimir Marko372f10e2016-05-17 16:30:10 +01004676 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004677 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01004678 ArrayRef<HUserRecord<HInstruction*>> input_records = HInvoke::GetInputRecords();
4679 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck()) {
4680 DCHECK(!input_records.empty());
4681 DCHECK_GT(input_records.size(), GetNumberOfArguments());
4682 HInstruction* last_input = input_records.back().GetInstruction();
4683 // Note: `last_input` may be null during arguments setup.
4684 if (last_input != nullptr) {
4685 // `last_input` is the last input of a static invoke marked as having
4686 // an explicit clinit check. It must either be:
4687 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
4688 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
4689 DCHECK(last_input->IsClinitCheck() || last_input->IsLoadClass()) << last_input->DebugName();
4690 }
4691 }
4692 return input_records;
4693 }
4694
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004695 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const override {
Vladimir Marko86c87522020-05-11 16:55:55 +01004696 // We do not access the method via object reference, so we cannot do an implicit null check.
Calin Juravle77520bc2015-01-12 18:45:46 +00004697 // TODO: for intrinsics we can generate implicit null checks.
4698 return false;
4699 }
4700
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004701 bool CanBeNull() const override {
Vladimir Markobd785672018-05-03 17:09:09 +01004702 return GetType() == DataType::Type::kReference && !IsStringInit();
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004703 }
4704
Vladimir Marko58155012015-08-19 12:49:41 +00004705 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
4706 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
4707 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004708 bool NeedsDexCacheOfDeclaringClass() const override;
Vladimir Marko58155012015-08-19 12:49:41 +00004709 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004710 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kJitDirectAddress; }
Vladimir Marko65979462017-05-19 17:25:12 +01004711 bool HasPcRelativeMethodLoadKind() const {
4712 return GetMethodLoadKind() == MethodLoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markob066d432018-01-03 13:14:37 +00004713 GetMethodLoadKind() == MethodLoadKind::kBootImageRelRo ||
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004714 GetMethodLoadKind() == MethodLoadKind::kBssEntry;
Vladimir Marko65979462017-05-19 17:25:12 +01004715 }
Vladimir Marko58155012015-08-19 12:49:41 +00004716
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004717 QuickEntrypointEnum GetStringInitEntryPoint() const {
Vladimir Marko58155012015-08-19 12:49:41 +00004718 DCHECK(IsStringInit());
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004719 return static_cast<QuickEntrypointEnum>(dispatch_info_.method_load_data);
Vladimir Marko58155012015-08-19 12:49:41 +00004720 }
4721
4722 uint64_t GetMethodAddress() const {
4723 DCHECK(HasMethodAddress());
4724 return dispatch_info_.method_load_data;
4725 }
4726
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00004727 const DexFile& GetDexFileForPcRelativeDexCache() const;
4728
Vladimir Markoa1de9182016-02-25 11:37:38 +00004729 ClinitCheckRequirement GetClinitCheckRequirement() const {
4730 return GetPackedField<ClinitCheckRequirementField>();
4731 }
Calin Juravle68ad6492015-08-18 17:08:12 +01004732
Roland Levillain4c0eb422015-04-24 16:43:49 +01004733 // Is this instruction a call to a static method?
4734 bool IsStatic() const {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004735 return GetInvokeType() == kStatic;
4736 }
4737
4738 MethodReference GetTargetMethod() const {
4739 return target_method_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01004740 }
4741
Vladimir Marko86c87522020-05-11 16:55:55 +01004742 // Does this method load kind need the current method as an input?
4743 static bool NeedsCurrentMethodInput(DispatchInfo dispatch_info) {
4744 return dispatch_info.method_load_kind == MethodLoadKind::kRecursive ||
4745 dispatch_info.method_load_kind == MethodLoadKind::kRuntimeCall ||
4746 dispatch_info.code_ptr_location == CodePtrLocation::kCallCriticalNative;
4747 }
4748
4749 // Get the index of the current method input.
4750 size_t GetCurrentMethodIndex() const {
4751 DCHECK(HasCurrentMethodInput());
4752 return GetCurrentMethodIndexUnchecked();
4753 }
4754 size_t GetCurrentMethodIndexUnchecked() const {
4755 return GetNumberOfArguments();
4756 }
4757
4758 // Check if the method has a current method input.
4759 bool HasCurrentMethodInput() const {
4760 if (NeedsCurrentMethodInput(GetDispatchInfo())) {
4761 DCHECK(InputAt(GetCurrentMethodIndexUnchecked()) == nullptr || // During argument setup.
4762 InputAt(GetCurrentMethodIndexUnchecked())->IsCurrentMethod());
4763 return true;
4764 } else {
4765 DCHECK(InputCount() == GetCurrentMethodIndexUnchecked() ||
4766 InputAt(GetCurrentMethodIndexUnchecked()) == nullptr || // During argument setup.
4767 !InputAt(GetCurrentMethodIndexUnchecked())->IsCurrentMethod());
4768 return false;
4769 }
4770 }
4771
4772 // Get the index of the special input.
4773 size_t GetSpecialInputIndex() const {
4774 DCHECK(HasSpecialInput());
4775 return GetSpecialInputIndexUnchecked();
4776 }
4777 size_t GetSpecialInputIndexUnchecked() const {
4778 return GetNumberOfArguments() + (HasCurrentMethodInput() ? 1u : 0u);
4779 }
4780
4781 // Check if the method has a special input.
4782 bool HasSpecialInput() const {
4783 size_t other_inputs =
4784 GetSpecialInputIndexUnchecked() + (IsStaticWithExplicitClinitCheck() ? 1u : 0u);
4785 size_t input_count = InputCount();
4786 DCHECK_LE(input_count - other_inputs, 1u) << other_inputs << " " << input_count;
4787 return other_inputs != input_count;
4788 }
4789
4790 void AddSpecialInput(HInstruction* input) {
4791 // We allow only one special input.
4792 DCHECK(!HasSpecialInput());
4793 InsertInputAt(GetSpecialInputIndexUnchecked(), input);
4794 }
4795
Vladimir Markofbb184a2015-11-13 14:47:00 +00004796 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
4797 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
4798 // instruction; only relevant for static calls with explicit clinit check.
4799 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01004800 DCHECK(IsStaticWithExplicitClinitCheck());
Vladimir Marko372f10e2016-05-17 16:30:10 +01004801 size_t last_input_index = inputs_.size() - 1u;
4802 HInstruction* last_input = inputs_.back().GetInstruction();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004803 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004804 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004805 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004806 inputs_.pop_back();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004807 SetPackedField<ClinitCheckRequirementField>(new_requirement);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004808 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004809 }
4810
4811 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00004812 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01004813 bool IsStaticWithExplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004814 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kExplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004815 }
4816
4817 // Is this a call to a static method whose declaring class has an
4818 // implicit intialization check requirement?
4819 bool IsStaticWithImplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004820 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kImplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004821 }
4822
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004823 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004824
Artem Serovcced8ba2017-07-19 18:18:09 +01004825 protected:
4826 DEFAULT_COPY_CONSTRUCTOR(InvokeStaticOrDirect);
4827
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004828 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004829 static constexpr size_t kFieldClinitCheckRequirement = kNumberOfInvokePackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004830 static constexpr size_t kFieldClinitCheckRequirementSize =
4831 MinimumBitsToStore(static_cast<size_t>(ClinitCheckRequirement::kLast));
4832 static constexpr size_t kNumberOfInvokeStaticOrDirectPackedBits =
4833 kFieldClinitCheckRequirement + kFieldClinitCheckRequirementSize;
4834 static_assert(kNumberOfInvokeStaticOrDirectPackedBits <= kMaxNumberOfPackedBits,
4835 "Too many packed fields.");
Vladimir Markoa1de9182016-02-25 11:37:38 +00004836 using ClinitCheckRequirementField = BitField<ClinitCheckRequirement,
4837 kFieldClinitCheckRequirement,
4838 kFieldClinitCheckRequirementSize>;
4839
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004840 // Cached values of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffrayea179f42018-02-08 22:30:18 +00004841 const MethodReference target_method_;
Vladimir Marko58155012015-08-19 12:49:41 +00004842 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004843};
Vladimir Markof64242a2015-12-01 14:58:23 +00004844std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Marko86c87522020-05-11 16:55:55 +01004845std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::CodePtrLocation rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004846std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004847
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004848class HInvokeVirtual final : public HInvoke {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004849 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004850 HInvokeVirtual(ArenaAllocator* allocator,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004851 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004852 DataType::Type return_type,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004853 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004854 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004855 ArtMethod* resolved_method,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004856 uint32_t vtable_index)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304857 : HInvoke(kInvokeVirtual,
4858 allocator,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004859 number_of_arguments,
4860 0u,
4861 return_type,
4862 dex_pc,
4863 dex_method_index,
4864 resolved_method,
4865 kVirtual),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304866 vtable_index_(vtable_index) {
4867 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004868
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004869 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004870
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004871 bool CanBeNull() const override {
Aart Bik71bf7b42016-11-16 10:17:46 -08004872 switch (GetIntrinsic()) {
4873 case Intrinsics::kThreadCurrentThread:
4874 case Intrinsics::kStringBufferAppend:
4875 case Intrinsics::kStringBufferToString:
Vladimir Markod4561172017-10-30 17:48:25 +00004876 case Intrinsics::kStringBuilderAppendObject:
4877 case Intrinsics::kStringBuilderAppendString:
4878 case Intrinsics::kStringBuilderAppendCharSequence:
4879 case Intrinsics::kStringBuilderAppendCharArray:
4880 case Intrinsics::kStringBuilderAppendBoolean:
4881 case Intrinsics::kStringBuilderAppendChar:
4882 case Intrinsics::kStringBuilderAppendInt:
4883 case Intrinsics::kStringBuilderAppendLong:
4884 case Intrinsics::kStringBuilderAppendFloat:
4885 case Intrinsics::kStringBuilderAppendDouble:
Aart Bik71bf7b42016-11-16 10:17:46 -08004886 case Intrinsics::kStringBuilderToString:
4887 return false;
4888 default:
4889 return HInvoke::CanBeNull();
4890 }
4891 }
4892
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004893 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Calin Juravle77520bc2015-01-12 18:45:46 +00004894 // TODO: Add implicit null checks in intrinsics.
Nicolas Geoffray61ba8d22018-08-07 09:55:57 +01004895 return (obj == InputAt(0)) && !IsIntrinsic();
Calin Juravle77520bc2015-01-12 18:45:46 +00004896 }
4897
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004898 uint32_t GetVTableIndex() const { return vtable_index_; }
4899
4900 DECLARE_INSTRUCTION(InvokeVirtual);
4901
Artem Serovcced8ba2017-07-19 18:18:09 +01004902 protected:
4903 DEFAULT_COPY_CONSTRUCTOR(InvokeVirtual);
4904
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004905 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004906 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004907 const uint32_t vtable_index_;
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004908};
4909
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004910class HInvokeInterface final : public HInvoke {
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004911 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004912 HInvokeInterface(ArenaAllocator* allocator,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004913 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004914 DataType::Type return_type,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004915 uint32_t dex_pc,
4916 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004917 ArtMethod* resolved_method,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004918 uint32_t imt_index)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304919 : HInvoke(kInvokeInterface,
4920 allocator,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004921 number_of_arguments,
4922 0u,
4923 return_type,
4924 dex_pc,
4925 dex_method_index,
4926 resolved_method,
4927 kInterface),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304928 imt_index_(imt_index) {
4929 }
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004930
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004931 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004932
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004933 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Calin Juravle77520bc2015-01-12 18:45:46 +00004934 // TODO: Add implicit null checks in intrinsics.
Nicolas Geoffray61ba8d22018-08-07 09:55:57 +01004935 return (obj == InputAt(0)) && !IsIntrinsic();
Calin Juravle77520bc2015-01-12 18:45:46 +00004936 }
4937
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004938 bool NeedsDexCacheOfDeclaringClass() const override {
Nicolas Geoffrayfbdfa6d2017-02-03 10:43:13 +00004939 // The assembly stub currently needs it.
4940 return true;
4941 }
4942
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004943 uint32_t GetImtIndex() const { return imt_index_; }
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004944
4945 DECLARE_INSTRUCTION(InvokeInterface);
4946
Artem Serovcced8ba2017-07-19 18:18:09 +01004947 protected:
4948 DEFAULT_COPY_CONSTRUCTOR(InvokeInterface);
4949
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004950 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004951 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004952 const uint32_t imt_index_;
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004953};
4954
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004955class HNeg final : public HUnaryOperation {
Roland Levillain88cb1752014-10-20 16:36:47 +01004956 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004957 HNeg(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304958 : HUnaryOperation(kNeg, result_type, input, dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004959 DCHECK_EQ(result_type, DataType::Kind(input->GetType()));
Roland Levillain937e6cd2016-03-22 11:54:37 +00004960 }
Roland Levillain88cb1752014-10-20 16:36:47 +01004961
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004962 template <typename T> static T Compute(T x) { return -x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004963
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004964 HConstant* Evaluate(HIntConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004965 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004966 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004967 HConstant* Evaluate(HLongConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004968 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004969 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004970 HConstant* Evaluate(HFloatConstant* x) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004971 return GetBlock()->GetGraph()->GetFloatConstant(Compute(x->GetValue()), GetDexPc());
4972 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004973 HConstant* Evaluate(HDoubleConstant* x) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004974 return GetBlock()->GetGraph()->GetDoubleConstant(Compute(x->GetValue()), GetDexPc());
4975 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01004976
Roland Levillain88cb1752014-10-20 16:36:47 +01004977 DECLARE_INSTRUCTION(Neg);
4978
Artem Serovcced8ba2017-07-19 18:18:09 +01004979 protected:
4980 DEFAULT_COPY_CONSTRUCTOR(Neg);
Roland Levillain88cb1752014-10-20 16:36:47 +01004981};
4982
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004983class HNewArray final : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004984 public:
Vladimir Markob5461632018-10-15 14:24:21 +01004985 HNewArray(HInstruction* cls, HInstruction* length, uint32_t dex_pc, size_t component_size_shift)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304986 : HExpression(kNewArray, DataType::Type::kReference, SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004987 SetRawInputAt(0, cls);
4988 SetRawInputAt(1, length);
Vladimir Markob5461632018-10-15 14:24:21 +01004989 SetPackedField<ComponentSizeShiftField>(component_size_shift);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004990 }
4991
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004992 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004993
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004994 // Calls runtime so needs an environment.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004995 bool NeedsEnvironment() const override { return true; }
Calin Juravle10e244f2015-01-26 18:54:32 +00004996
Mingyao Yang0c365e62015-03-31 15:09:29 -07004997 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004998 bool CanThrow() const override { return true; }
Mingyao Yang0c365e62015-03-31 15:09:29 -07004999
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005000 bool CanBeNull() const override { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01005001
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005002 HLoadClass* GetLoadClass() const {
5003 DCHECK(InputAt(0)->IsLoadClass());
5004 return InputAt(0)->AsLoadClass();
5005 }
5006
5007 HInstruction* GetLength() const {
5008 return InputAt(1);
5009 }
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00005010
Vladimir Markob5461632018-10-15 14:24:21 +01005011 size_t GetComponentSizeShift() {
5012 return GetPackedField<ComponentSizeShiftField>();
5013 }
5014
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01005015 DECLARE_INSTRUCTION(NewArray);
5016
Artem Serovcced8ba2017-07-19 18:18:09 +01005017 protected:
5018 DEFAULT_COPY_CONSTRUCTOR(NewArray);
Vladimir Markob5461632018-10-15 14:24:21 +01005019
5020 private:
5021 static constexpr size_t kFieldComponentSizeShift = kNumberOfGenericPackedBits;
5022 static constexpr size_t kFieldComponentSizeShiftSize = MinimumBitsToStore(3u);
5023 static constexpr size_t kNumberOfNewArrayPackedBits =
5024 kFieldComponentSizeShift + kFieldComponentSizeShiftSize;
5025 static_assert(kNumberOfNewArrayPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5026 using ComponentSizeShiftField =
5027 BitField<size_t, kFieldComponentSizeShift, kFieldComponentSizeShift>;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01005028};
5029
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005030class HAdd final : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00005031 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005032 HAdd(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005033 HInstruction* left,
5034 HInstruction* right,
5035 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305036 : HBinaryOperation(kAdd, result_type, left, right, SideEffects::None(), dex_pc) {
5037 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00005038
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005039 bool IsCommutative() const override { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00005040
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005041 template <typename T> static T Compute(T x, T y) { return x + y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005042
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005043 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005044 return GetBlock()->GetGraph()->GetIntConstant(
5045 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01005046 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005047 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005048 return GetBlock()->GetGraph()->GetLongConstant(
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(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005052 return GetBlock()->GetGraph()->GetFloatConstant(
5053 Compute(x->GetValue(), y->GetValue()), GetDexPc());
5054 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005055 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005056 return GetBlock()->GetGraph()->GetDoubleConstant(
5057 Compute(x->GetValue(), y->GetValue()), GetDexPc());
5058 }
Roland Levillain556c3d12014-09-18 15:25:07 +01005059
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00005060 DECLARE_INSTRUCTION(Add);
5061
Artem Serovcced8ba2017-07-19 18:18:09 +01005062 protected:
5063 DEFAULT_COPY_CONSTRUCTOR(Add);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00005064};
5065
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005066class HSub final : public HBinaryOperation {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005067 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005068 HSub(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005069 HInstruction* left,
5070 HInstruction* right,
5071 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305072 : HBinaryOperation(kSub, result_type, left, right, SideEffects::None(), dex_pc) {
5073 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005074
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005075 template <typename T> static T Compute(T x, T y) { return x - y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005076
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005077 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005078 return GetBlock()->GetGraph()->GetIntConstant(
5079 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01005080 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005081 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005082 return GetBlock()->GetGraph()->GetLongConstant(
5083 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01005084 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005085 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005086 return GetBlock()->GetGraph()->GetFloatConstant(
5087 Compute(x->GetValue(), y->GetValue()), GetDexPc());
5088 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005089 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005090 return GetBlock()->GetGraph()->GetDoubleConstant(
5091 Compute(x->GetValue(), y->GetValue()), GetDexPc());
5092 }
Roland Levillain556c3d12014-09-18 15:25:07 +01005093
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005094 DECLARE_INSTRUCTION(Sub);
5095
Artem Serovcced8ba2017-07-19 18:18:09 +01005096 protected:
5097 DEFAULT_COPY_CONSTRUCTOR(Sub);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005098};
5099
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005100class HMul final : public HBinaryOperation {
Calin Juravle34bacdf2014-10-07 20:23:36 +01005101 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005102 HMul(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005103 HInstruction* left,
5104 HInstruction* right,
5105 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305106 : HBinaryOperation(kMul, result_type, left, right, SideEffects::None(), dex_pc) {
5107 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01005108
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005109 bool IsCommutative() const override { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01005110
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005111 template <typename T> static T Compute(T x, T y) { return x * y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005112
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005113 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005114 return GetBlock()->GetGraph()->GetIntConstant(
5115 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005116 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005117 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005118 return GetBlock()->GetGraph()->GetLongConstant(
5119 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005120 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005121 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005122 return GetBlock()->GetGraph()->GetFloatConstant(
5123 Compute(x->GetValue(), y->GetValue()), GetDexPc());
5124 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005125 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005126 return GetBlock()->GetGraph()->GetDoubleConstant(
5127 Compute(x->GetValue(), y->GetValue()), GetDexPc());
5128 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01005129
5130 DECLARE_INSTRUCTION(Mul);
5131
Artem Serovcced8ba2017-07-19 18:18:09 +01005132 protected:
5133 DEFAULT_COPY_CONSTRUCTOR(Mul);
Calin Juravle34bacdf2014-10-07 20:23:36 +01005134};
5135
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005136class HDiv final : public HBinaryOperation {
Calin Juravle7c4954d2014-10-28 16:57:40 +00005137 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005138 HDiv(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005139 HInstruction* left,
5140 HInstruction* right,
5141 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305142 : HBinaryOperation(kDiv, result_type, left, right, SideEffects::None(), dex_pc) {
5143 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00005144
Roland Levillain9867bc72015-08-05 10:21:34 +01005145 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00005146 T ComputeIntegral(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005147 DCHECK(!DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01005148 // Our graph structure ensures we never have 0 for `y` during
5149 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00005150 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00005151 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00005152 return (y == -1) ? -x : x / y;
5153 }
Calin Juravlebacfec32014-11-14 15:54:36 +00005154
Roland Levillain31dd3d62016-02-16 12:21:02 +00005155 template <typename T>
5156 T ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005157 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00005158 return x / y;
5159 }
5160
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005161 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005162 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00005163 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005164 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005165 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005166 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00005167 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5168 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005169 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005170 return GetBlock()->GetGraph()->GetFloatConstant(
5171 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
5172 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005173 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005174 return GetBlock()->GetGraph()->GetDoubleConstant(
5175 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00005176 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00005177
5178 DECLARE_INSTRUCTION(Div);
5179
Artem Serovcced8ba2017-07-19 18:18:09 +01005180 protected:
5181 DEFAULT_COPY_CONSTRUCTOR(Div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00005182};
5183
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005184class HRem final : public HBinaryOperation {
Calin Juravlebacfec32014-11-14 15:54:36 +00005185 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005186 HRem(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005187 HInstruction* left,
5188 HInstruction* right,
5189 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305190 : HBinaryOperation(kRem, result_type, left, right, SideEffects::None(), dex_pc) {
5191 }
Calin Juravlebacfec32014-11-14 15:54:36 +00005192
Roland Levillain9867bc72015-08-05 10:21:34 +01005193 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00005194 T ComputeIntegral(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005195 DCHECK(!DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01005196 // Our graph structure ensures we never have 0 for `y` during
5197 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00005198 DCHECK_NE(y, 0);
5199 // Special case -1 to avoid getting a SIGFPE on x86(_64).
5200 return (y == -1) ? 0 : x % y;
5201 }
5202
Roland Levillain31dd3d62016-02-16 12:21:02 +00005203 template <typename T>
5204 T ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005205 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00005206 return std::fmod(x, y);
5207 }
5208
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005209 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005210 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00005211 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005212 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005213 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005214 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00005215 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00005216 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005217 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005218 return GetBlock()->GetGraph()->GetFloatConstant(
5219 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
5220 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005221 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005222 return GetBlock()->GetGraph()->GetDoubleConstant(
5223 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
5224 }
Calin Juravlebacfec32014-11-14 15:54:36 +00005225
5226 DECLARE_INSTRUCTION(Rem);
5227
Artem Serovcced8ba2017-07-19 18:18:09 +01005228 protected:
5229 DEFAULT_COPY_CONSTRUCTOR(Rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00005230};
5231
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005232class HMin final : public HBinaryOperation {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005233 public:
5234 HMin(DataType::Type result_type,
5235 HInstruction* left,
5236 HInstruction* right,
5237 uint32_t dex_pc)
5238 : HBinaryOperation(kMin, result_type, left, right, SideEffects::None(), dex_pc) {}
5239
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005240 bool IsCommutative() const override { return true; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005241
5242 // Evaluation for integral values.
5243 template <typename T> static T ComputeIntegral(T x, T y) {
5244 return (x <= y) ? x : y;
5245 }
5246
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005247 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005248 return GetBlock()->GetGraph()->GetIntConstant(
5249 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5250 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005251 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005252 return GetBlock()->GetGraph()->GetLongConstant(
5253 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5254 }
5255 // TODO: Evaluation for floating-point values.
5256 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005257 HFloatConstant* y ATTRIBUTE_UNUSED) const override { return nullptr; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005258 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005259 HDoubleConstant* y ATTRIBUTE_UNUSED) const override { return nullptr; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005260
5261 DECLARE_INSTRUCTION(Min);
5262
5263 protected:
5264 DEFAULT_COPY_CONSTRUCTOR(Min);
5265};
5266
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005267class HMax final : public HBinaryOperation {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005268 public:
5269 HMax(DataType::Type result_type,
5270 HInstruction* left,
5271 HInstruction* right,
5272 uint32_t dex_pc)
5273 : HBinaryOperation(kMax, result_type, left, right, SideEffects::None(), dex_pc) {}
5274
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005275 bool IsCommutative() const override { return true; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005276
5277 // Evaluation for integral values.
5278 template <typename T> static T ComputeIntegral(T x, T y) {
5279 return (x >= y) ? x : y;
5280 }
5281
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005282 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005283 return GetBlock()->GetGraph()->GetIntConstant(
5284 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5285 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005286 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005287 return GetBlock()->GetGraph()->GetLongConstant(
5288 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5289 }
5290 // TODO: Evaluation for floating-point values.
5291 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005292 HFloatConstant* y ATTRIBUTE_UNUSED) const override { return nullptr; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005293 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005294 HDoubleConstant* y ATTRIBUTE_UNUSED) const override { return nullptr; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005295
5296 DECLARE_INSTRUCTION(Max);
5297
5298 protected:
5299 DEFAULT_COPY_CONSTRUCTOR(Max);
5300};
5301
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005302class HAbs final : public HUnaryOperation {
Aart Bik3dad3412018-02-28 12:01:46 -08005303 public:
5304 HAbs(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
5305 : HUnaryOperation(kAbs, result_type, input, dex_pc) {}
5306
5307 // Evaluation for integral values.
5308 template <typename T> static T ComputeIntegral(T x) {
5309 return x < 0 ? -x : x;
5310 }
5311
5312 // Evaluation for floating-point values.
5313 // Note, as a "quality of implementation", rather than pure "spec compliance",
5314 // we require that Math.abs() clears the sign bit (but changes nothing else)
5315 // for all floating-point numbers, including NaN (signaling NaN may become quiet though).
5316 // http://b/30758343
5317 template <typename T, typename S> static T ComputeFP(T x) {
5318 S bits = bit_cast<S, T>(x);
5319 return bit_cast<T, S>(bits & std::numeric_limits<S>::max());
5320 }
5321
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005322 HConstant* Evaluate(HIntConstant* x) const override {
Aart Bik3dad3412018-02-28 12:01:46 -08005323 return GetBlock()->GetGraph()->GetIntConstant(ComputeIntegral(x->GetValue()), GetDexPc());
5324 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005325 HConstant* Evaluate(HLongConstant* x) const override {
Aart Bik3dad3412018-02-28 12:01:46 -08005326 return GetBlock()->GetGraph()->GetLongConstant(ComputeIntegral(x->GetValue()), GetDexPc());
5327 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005328 HConstant* Evaluate(HFloatConstant* x) const override {
Aart Bik3dad3412018-02-28 12:01:46 -08005329 return GetBlock()->GetGraph()->GetFloatConstant(
5330 ComputeFP<float, int32_t>(x->GetValue()), GetDexPc());
5331 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005332 HConstant* Evaluate(HDoubleConstant* x) const override {
Aart Bik3dad3412018-02-28 12:01:46 -08005333 return GetBlock()->GetGraph()->GetDoubleConstant(
5334 ComputeFP<double, int64_t>(x->GetValue()), GetDexPc());
5335 }
5336
5337 DECLARE_INSTRUCTION(Abs);
5338
5339 protected:
5340 DEFAULT_COPY_CONSTRUCTOR(Abs);
5341};
5342
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005343class HDivZeroCheck final : public HExpression<1> {
Calin Juravled0d48522014-11-04 16:40:20 +00005344 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00005345 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
Artem Serovd1aa7d02018-06-22 11:35:46 +01005346 // constructor. However it can only do it on a fatal slow path so execution never returns to the
5347 // instruction following the current one; thus 'SideEffects::None()' is used.
Calin Juravled0d48522014-11-04 16:40:20 +00005348 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Artem Serovd1aa7d02018-06-22 11:35:46 +01005349 : HExpression(kDivZeroCheck, value->GetType(), SideEffects::None(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00005350 SetRawInputAt(0, value);
5351 }
5352
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005353 bool IsClonable() const override { return true; }
5354 bool CanBeMoved() const override { return true; }
Calin Juravled0d48522014-11-04 16:40:20 +00005355
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005356 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Calin Juravled0d48522014-11-04 16:40:20 +00005357 return true;
5358 }
5359
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005360 bool NeedsEnvironment() const override { return true; }
5361 bool CanThrow() const override { return true; }
Calin Juravled0d48522014-11-04 16:40:20 +00005362
Calin Juravled0d48522014-11-04 16:40:20 +00005363 DECLARE_INSTRUCTION(DivZeroCheck);
5364
Artem Serovcced8ba2017-07-19 18:18:09 +01005365 protected:
5366 DEFAULT_COPY_CONSTRUCTOR(DivZeroCheck);
Calin Juravled0d48522014-11-04 16:40:20 +00005367};
5368
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005369class HShl final : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005370 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005371 HShl(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005372 HInstruction* value,
5373 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005374 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305375 : HBinaryOperation(kShl, result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005376 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5377 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01005378 }
5379
Roland Levillain5b5b9312016-03-22 14:57:31 +00005380 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005381 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005382 return value << (distance & max_shift_distance);
5383 }
5384
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005385 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005386 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005387 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005388 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005389 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005390 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005391 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005392 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005393 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005394 HLongConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005395 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5396 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01005397 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005398 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005399 HFloatConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005400 LOG(FATAL) << DebugName() << " is not defined for float values";
5401 UNREACHABLE();
5402 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005403 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005404 HDoubleConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005405 LOG(FATAL) << DebugName() << " is not defined for double values";
5406 UNREACHABLE();
5407 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005408
5409 DECLARE_INSTRUCTION(Shl);
5410
Artem Serovcced8ba2017-07-19 18:18:09 +01005411 protected:
5412 DEFAULT_COPY_CONSTRUCTOR(Shl);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005413};
5414
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005415class HShr final : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005416 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005417 HShr(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005418 HInstruction* value,
5419 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005420 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305421 : HBinaryOperation(kShr, result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005422 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5423 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01005424 }
5425
Roland Levillain5b5b9312016-03-22 14:57:31 +00005426 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005427 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005428 return value >> (distance & max_shift_distance);
5429 }
5430
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005431 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005432 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005433 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005434 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005435 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005436 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005437 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005438 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005439 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005440 HLongConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005441 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5442 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01005443 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005444 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005445 HFloatConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005446 LOG(FATAL) << DebugName() << " is not defined for float values";
5447 UNREACHABLE();
5448 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005449 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005450 HDoubleConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005451 LOG(FATAL) << DebugName() << " is not defined for double values";
5452 UNREACHABLE();
5453 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005454
5455 DECLARE_INSTRUCTION(Shr);
5456
Artem Serovcced8ba2017-07-19 18:18:09 +01005457 protected:
5458 DEFAULT_COPY_CONSTRUCTOR(Shr);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005459};
5460
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005461class HUShr final : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005462 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005463 HUShr(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005464 HInstruction* value,
5465 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005466 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305467 : HBinaryOperation(kUShr, result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005468 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5469 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Calin Juravle9aec02f2014-11-18 23:06:35 +00005470 }
5471
Roland Levillain5b5b9312016-03-22 14:57:31 +00005472 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005473 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005474 typedef typename std::make_unsigned<T>::type V;
5475 V ux = static_cast<V>(value);
5476 return static_cast<T>(ux >> (distance & max_shift_distance));
5477 }
5478
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005479 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005480 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005481 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005482 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005483 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005484 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005485 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005486 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005487 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005488 HLongConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005489 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5490 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00005491 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005492 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005493 HFloatConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005494 LOG(FATAL) << DebugName() << " is not defined for float values";
5495 UNREACHABLE();
5496 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005497 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005498 HDoubleConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005499 LOG(FATAL) << DebugName() << " is not defined for double values";
5500 UNREACHABLE();
5501 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005502
5503 DECLARE_INSTRUCTION(UShr);
5504
Artem Serovcced8ba2017-07-19 18:18:09 +01005505 protected:
5506 DEFAULT_COPY_CONSTRUCTOR(UShr);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005507};
5508
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005509class HAnd final : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005510 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005511 HAnd(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005512 HInstruction* left,
5513 HInstruction* right,
5514 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305515 : HBinaryOperation(kAnd, result_type, left, right, SideEffects::None(), dex_pc) {
5516 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005517
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005518 bool IsCommutative() const override { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005519
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005520 template <typename T> static T Compute(T x, T y) { return x & y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005521
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005522 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005523 return GetBlock()->GetGraph()->GetIntConstant(
5524 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005525 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005526 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005527 return GetBlock()->GetGraph()->GetLongConstant(
5528 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005529 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005530 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005531 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005532 LOG(FATAL) << DebugName() << " is not defined for float values";
5533 UNREACHABLE();
5534 }
5535 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005536 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005537 LOG(FATAL) << DebugName() << " is not defined for double values";
5538 UNREACHABLE();
5539 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005540
5541 DECLARE_INSTRUCTION(And);
5542
Artem Serovcced8ba2017-07-19 18:18:09 +01005543 protected:
5544 DEFAULT_COPY_CONSTRUCTOR(And);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005545};
5546
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005547class HOr final : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005548 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005549 HOr(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005550 HInstruction* left,
5551 HInstruction* right,
5552 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305553 : HBinaryOperation(kOr, result_type, left, right, SideEffects::None(), dex_pc) {
5554 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005555
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005556 bool IsCommutative() const override { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005557
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005558 template <typename T> static T Compute(T x, T y) { return x | y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005559
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005560 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005561 return GetBlock()->GetGraph()->GetIntConstant(
5562 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005563 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005564 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005565 return GetBlock()->GetGraph()->GetLongConstant(
5566 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005567 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005568 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005569 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005570 LOG(FATAL) << DebugName() << " is not defined for float values";
5571 UNREACHABLE();
5572 }
5573 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005574 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005575 LOG(FATAL) << DebugName() << " is not defined for double values";
5576 UNREACHABLE();
5577 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005578
5579 DECLARE_INSTRUCTION(Or);
5580
Artem Serovcced8ba2017-07-19 18:18:09 +01005581 protected:
5582 DEFAULT_COPY_CONSTRUCTOR(Or);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005583};
5584
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005585class HXor final : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005586 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005587 HXor(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005588 HInstruction* left,
5589 HInstruction* right,
5590 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305591 : HBinaryOperation(kXor, result_type, left, right, SideEffects::None(), dex_pc) {
5592 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005593
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005594 bool IsCommutative() const override { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005595
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005596 template <typename T> static T Compute(T x, T y) { return x ^ y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005597
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005598 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005599 return GetBlock()->GetGraph()->GetIntConstant(
5600 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005601 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005602 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005603 return GetBlock()->GetGraph()->GetLongConstant(
5604 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005605 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005606 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005607 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005608 LOG(FATAL) << DebugName() << " is not defined for float values";
5609 UNREACHABLE();
5610 }
5611 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005612 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005613 LOG(FATAL) << DebugName() << " is not defined for double values";
5614 UNREACHABLE();
5615 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005616
5617 DECLARE_INSTRUCTION(Xor);
5618
Artem Serovcced8ba2017-07-19 18:18:09 +01005619 protected:
5620 DEFAULT_COPY_CONSTRUCTOR(Xor);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005621};
5622
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005623class HRor final : public HBinaryOperation {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005624 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005625 HRor(DataType::Type result_type, HInstruction* value, HInstruction* distance)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305626 : HBinaryOperation(kRor, result_type, value, distance) {
Roland Levillain22c49222016-03-18 14:04:28 +00005627 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005628
Roland Levillain5b5b9312016-03-22 14:57:31 +00005629 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005630 static T Compute(T value, int32_t distance, int32_t max_shift_value) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005631 typedef typename std::make_unsigned<T>::type V;
5632 V ux = static_cast<V>(value);
5633 if ((distance & max_shift_value) == 0) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005634 return static_cast<T>(ux);
5635 } else {
5636 const V reg_bits = sizeof(T) * 8;
Roland Levillain5b5b9312016-03-22 14:57:31 +00005637 return static_cast<T>(ux >> (distance & max_shift_value)) |
5638 (value << (reg_bits - (distance & max_shift_value)));
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005639 }
5640 }
5641
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005642 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const override {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005643 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005644 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005645 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005646 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const override {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005647 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005648 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005649 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005650 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005651 HLongConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005652 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5653 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005654 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005655 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005656 HFloatConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005657 LOG(FATAL) << DebugName() << " is not defined for float values";
5658 UNREACHABLE();
5659 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005660 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005661 HDoubleConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005662 LOG(FATAL) << DebugName() << " is not defined for double values";
5663 UNREACHABLE();
5664 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005665
5666 DECLARE_INSTRUCTION(Ror);
5667
Artem Serovcced8ba2017-07-19 18:18:09 +01005668 protected:
5669 DEFAULT_COPY_CONSTRUCTOR(Ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005670};
5671
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005672// The value of a parameter in this method. Its location depends on
5673// the calling convention.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005674class HParameterValue final : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005675 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005676 HParameterValue(const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08005677 dex::TypeIndex type_index,
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005678 uint8_t index,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005679 DataType::Type parameter_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005680 bool is_this = false)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305681 : HExpression(kParameterValue, parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005682 dex_file_(dex_file),
5683 type_index_(type_index),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005684 index_(index) {
5685 SetPackedFlag<kFlagIsThis>(is_this);
5686 SetPackedFlag<kFlagCanBeNull>(!is_this);
5687 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005688
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005689 const DexFile& GetDexFile() const { return dex_file_; }
Andreas Gampea5b09a62016-11-17 15:21:22 -08005690 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005691 uint8_t GetIndex() const { return index_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07005692 bool IsThis() const { return GetPackedFlag<kFlagIsThis>(); }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005693
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005694 bool CanBeNull() const override { return GetPackedFlag<kFlagCanBeNull>(); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005695 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00005696
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005697 DECLARE_INSTRUCTION(ParameterValue);
5698
Artem Serovcced8ba2017-07-19 18:18:09 +01005699 protected:
5700 DEFAULT_COPY_CONSTRUCTOR(ParameterValue);
5701
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005702 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005703 // Whether or not the parameter value corresponds to 'this' argument.
Vladimir Markobd785672018-05-03 17:09:09 +01005704 static constexpr size_t kFlagIsThis = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005705 static constexpr size_t kFlagCanBeNull = kFlagIsThis + 1;
5706 static constexpr size_t kNumberOfParameterValuePackedBits = kFlagCanBeNull + 1;
5707 static_assert(kNumberOfParameterValuePackedBits <= kMaxNumberOfPackedBits,
5708 "Too many packed fields.");
5709
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005710 const DexFile& dex_file_;
Andreas Gampea5b09a62016-11-17 15:21:22 -08005711 const dex::TypeIndex type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005712 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00005713 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005714 const uint8_t index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005715};
5716
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005717class HNot final : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005718 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005719 HNot(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305720 : HUnaryOperation(kNot, result_type, input, dex_pc) {
5721 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005722
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005723 bool CanBeMoved() const override { return true; }
5724 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005725 return true;
5726 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005727
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005728 template <typename T> static T Compute(T x) { return ~x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005729
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005730 HConstant* Evaluate(HIntConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005731 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005732 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005733 HConstant* Evaluate(HLongConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005734 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005735 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005736 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005737 LOG(FATAL) << DebugName() << " is not defined for float values";
5738 UNREACHABLE();
5739 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005740 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005741 LOG(FATAL) << DebugName() << " is not defined for double values";
5742 UNREACHABLE();
5743 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01005744
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005745 DECLARE_INSTRUCTION(Not);
5746
Artem Serovcced8ba2017-07-19 18:18:09 +01005747 protected:
5748 DEFAULT_COPY_CONSTRUCTOR(Not);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005749};
5750
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005751class HBooleanNot final : public HUnaryOperation {
David Brazdil66d126e2015-04-03 16:02:44 +01005752 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005753 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305754 : HUnaryOperation(kBooleanNot, DataType::Type::kBool, input, dex_pc) {
5755 }
David Brazdil66d126e2015-04-03 16:02:44 +01005756
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005757 bool CanBeMoved() const override { return true; }
5758 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
David Brazdil66d126e2015-04-03 16:02:44 +01005759 return true;
5760 }
5761
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005762 template <typename T> static bool Compute(T x) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005763 DCHECK(IsUint<1>(x)) << x;
David Brazdil66d126e2015-04-03 16:02:44 +01005764 return !x;
5765 }
5766
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005767 HConstant* Evaluate(HIntConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005768 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005769 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005770 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005771 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01005772 UNREACHABLE();
5773 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005774 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005775 LOG(FATAL) << DebugName() << " is not defined for float values";
5776 UNREACHABLE();
5777 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005778 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005779 LOG(FATAL) << DebugName() << " is not defined for double values";
5780 UNREACHABLE();
5781 }
David Brazdil66d126e2015-04-03 16:02:44 +01005782
5783 DECLARE_INSTRUCTION(BooleanNot);
5784
Artem Serovcced8ba2017-07-19 18:18:09 +01005785 protected:
5786 DEFAULT_COPY_CONSTRUCTOR(BooleanNot);
David Brazdil66d126e2015-04-03 16:02:44 +01005787};
5788
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005789class HTypeConversion final : public HExpression<1> {
Roland Levillaindff1f282014-11-05 14:15:05 +00005790 public:
5791 // Instantiate a type conversion of `input` to `result_type`.
Vladimir Markoc8fb2112017-10-03 11:37:52 +01005792 HTypeConversion(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305793 : HExpression(kTypeConversion, result_type, SideEffects::None(), dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00005794 SetRawInputAt(0, input);
Roland Levillainf355c3f2016-03-30 19:09:03 +01005795 // Invariant: We should never generate a conversion to a Boolean value.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005796 DCHECK_NE(DataType::Type::kBool, result_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00005797 }
5798
5799 HInstruction* GetInput() const { return InputAt(0); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005800 DataType::Type GetInputType() const { return GetInput()->GetType(); }
5801 DataType::Type GetResultType() const { return GetType(); }
Roland Levillaindff1f282014-11-05 14:15:05 +00005802
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005803 bool IsClonable() const override { return true; }
5804 bool CanBeMoved() const override { return true; }
5805 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01005806 return true;
5807 }
Nicolas Geoffrayacc56ac2018-10-09 08:45:24 +01005808 // Return whether the conversion is implicit. This includes conversion to the same type.
5809 bool IsImplicitConversion() const {
5810 return DataType::IsTypeConversionImplicit(GetInputType(), GetResultType());
5811 }
Roland Levillaindff1f282014-11-05 14:15:05 +00005812
Mark Mendelle82549b2015-05-06 10:55:34 -04005813 // Try to statically evaluate the conversion and return a HConstant
5814 // containing the result. If the input cannot be converted, return nullptr.
5815 HConstant* TryStaticEvaluation() const;
5816
Roland Levillaindff1f282014-11-05 14:15:05 +00005817 DECLARE_INSTRUCTION(TypeConversion);
5818
Artem Serovcced8ba2017-07-19 18:18:09 +01005819 protected:
5820 DEFAULT_COPY_CONSTRUCTOR(TypeConversion);
Roland Levillaindff1f282014-11-05 14:15:05 +00005821};
5822
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00005823static constexpr uint32_t kNoRegNumber = -1;
5824
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005825class HNullCheck final : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005826 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005827 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
Artem Serovd1aa7d02018-06-22 11:35:46 +01005828 // constructor. However it can only do it on a fatal slow path so execution never returns to the
5829 // instruction following the current one; thus 'SideEffects::None()' is used.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005830 HNullCheck(HInstruction* value, uint32_t dex_pc)
Artem Serovd1aa7d02018-06-22 11:35:46 +01005831 : HExpression(kNullCheck, value->GetType(), SideEffects::None(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005832 SetRawInputAt(0, value);
5833 }
5834
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005835 bool IsClonable() const override { return true; }
5836 bool CanBeMoved() const override { return true; }
5837 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005838 return true;
5839 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005840
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005841 bool NeedsEnvironment() const override { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005842
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005843 bool CanThrow() const override { return true; }
Calin Juravle10e244f2015-01-26 18:54:32 +00005844
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005845 bool CanBeNull() const override { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005846
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005847 DECLARE_INSTRUCTION(NullCheck);
5848
Artem Serovcced8ba2017-07-19 18:18:09 +01005849 protected:
5850 DEFAULT_COPY_CONSTRUCTOR(NullCheck);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005851};
5852
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005853// Embeds an ArtField and all the information required by the compiler. We cache
5854// that information to avoid requiring the mutator lock every time we need it.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005855class FieldInfo : public ValueObject {
5856 public:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005857 FieldInfo(ArtField* field,
5858 MemberOffset field_offset,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005859 DataType::Type field_type,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005860 bool is_volatile,
5861 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005862 uint16_t declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005863 const DexFile& dex_file)
5864 : field_(field),
5865 field_offset_(field_offset),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005866 field_type_(field_type),
5867 is_volatile_(is_volatile),
5868 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005869 declaring_class_def_index_(declaring_class_def_index),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005870 dex_file_(dex_file) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005871
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005872 ArtField* GetField() const { return field_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005873 MemberOffset GetFieldOffset() const { return field_offset_; }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005874 DataType::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005875 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07005876 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005877 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00005878 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005879
5880 private:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005881 ArtField* const field_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005882 const MemberOffset field_offset_;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005883 const DataType::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00005884 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005885 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07005886 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005887 const DexFile& dex_file_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005888};
5889
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005890class HInstanceFieldGet final : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005891 public:
5892 HInstanceFieldGet(HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005893 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005894 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005895 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005896 bool is_volatile,
5897 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005898 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005899 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005900 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305901 : HExpression(kInstanceFieldGet,
5902 field_type,
5903 SideEffects::FieldReadOfType(field_type, is_volatile),
5904 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005905 field_info_(field,
5906 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005907 field_type,
5908 is_volatile,
5909 field_idx,
5910 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005911 dex_file) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005912 SetRawInputAt(0, value);
5913 }
5914
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005915 bool IsClonable() const override { return true; }
5916 bool CanBeMoved() const override { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005917
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005918 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01005919 const HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005920 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005921 }
5922
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005923 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005924 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005925 }
5926
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005927 size_t ComputeHashCode() const override {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01005928 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5929 }
5930
Calin Juravle52c48962014-12-16 17:02:57 +00005931 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005932 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005933 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005934 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005935
Vladimir Marko61b92282017-10-11 13:23:17 +01005936 void SetType(DataType::Type new_type) {
5937 DCHECK(DataType::IsIntegralType(GetType()));
5938 DCHECK(DataType::IsIntegralType(new_type));
5939 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
5940 SetPackedField<TypeField>(new_type);
5941 }
5942
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005943 DECLARE_INSTRUCTION(InstanceFieldGet);
5944
Artem Serovcced8ba2017-07-19 18:18:09 +01005945 protected:
5946 DEFAULT_COPY_CONSTRUCTOR(InstanceFieldGet);
5947
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005948 private:
5949 const FieldInfo field_info_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005950};
5951
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005952class HInstanceFieldSet final : public HExpression<2> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005953 public:
5954 HInstanceFieldSet(HInstruction* object,
5955 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005956 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005957 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005958 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005959 bool is_volatile,
5960 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005961 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005962 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005963 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01005964 : HExpression(kInstanceFieldSet,
5965 SideEffects::FieldWriteOfType(field_type, is_volatile),
5966 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005967 field_info_(field,
5968 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005969 field_type,
5970 is_volatile,
5971 field_idx,
5972 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005973 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005974 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005975 SetRawInputAt(0, object);
5976 SetRawInputAt(1, value);
5977 }
5978
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005979 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01005980
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005981 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005982 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005983 }
5984
Calin Juravle52c48962014-12-16 17:02:57 +00005985 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005986 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005987 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005988 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005989 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005990 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5991 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005992
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005993 DECLARE_INSTRUCTION(InstanceFieldSet);
5994
Artem Serovcced8ba2017-07-19 18:18:09 +01005995 protected:
5996 DEFAULT_COPY_CONSTRUCTOR(InstanceFieldSet);
5997
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005998 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005999 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
6000 static constexpr size_t kNumberOfInstanceFieldSetPackedBits = kFlagValueCanBeNull + 1;
6001 static_assert(kNumberOfInstanceFieldSetPackedBits <= kMaxNumberOfPackedBits,
6002 "Too many packed fields.");
6003
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006004 const FieldInfo field_info_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006005};
6006
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006007class HArrayGet final : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006008 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006009 HArrayGet(HInstruction* array,
6010 HInstruction* index,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006011 DataType::Type type,
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006012 uint32_t dex_pc)
6013 : HArrayGet(array,
6014 index,
6015 type,
6016 SideEffects::ArrayReadOfType(type),
6017 dex_pc,
Andreas Gampe3db70682018-12-26 15:12:03 -08006018 /* is_string_char_at= */ false) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306019 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006020
6021 HArrayGet(HInstruction* array,
6022 HInstruction* index,
6023 DataType::Type type,
6024 SideEffects side_effects,
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006025 uint32_t dex_pc,
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006026 bool is_string_char_at)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306027 : HExpression(kArrayGet, type, side_effects, dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006028 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006029 SetRawInputAt(0, array);
6030 SetRawInputAt(1, index);
6031 }
6032
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006033 bool IsClonable() const override { return true; }
6034 bool CanBeMoved() const override { return true; }
6035 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07006036 return true;
6037 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006038 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const override {
Calin Juravle77520bc2015-01-12 18:45:46 +00006039 // TODO: We can be smarter here.
Vladimir Marko66d691d2017-04-07 17:53:39 +01006040 // Currently, unless the array is the result of NewArray, the array access is always
6041 // preceded by some form of null NullCheck necessary for the bounds check, usually
6042 // implicit null check on the ArrayLength input to BoundsCheck or Deoptimize for
6043 // dynamic BCE. There are cases when these could be removed to produce better code.
6044 // If we ever add optimizations to do so we should allow an implicit check here
6045 // (as long as the address falls in the first page).
6046 //
6047 // As an example of such fancy optimization, we could eliminate BoundsCheck for
6048 // a = cond ? new int[1] : null;
6049 // a[0]; // The Phi does not need bounds check for either input.
Calin Juravle77520bc2015-01-12 18:45:46 +00006050 return false;
6051 }
6052
David Brazdil4833f5a2015-12-16 10:37:39 +00006053 bool IsEquivalentOf(HArrayGet* other) const {
6054 bool result = (GetDexPc() == other->GetDexPc());
6055 if (kIsDebugBuild && result) {
6056 DCHECK_EQ(GetBlock(), other->GetBlock());
6057 DCHECK_EQ(GetArray(), other->GetArray());
6058 DCHECK_EQ(GetIndex(), other->GetIndex());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006059 if (DataType::IsIntOrLongType(GetType())) {
6060 DCHECK(DataType::IsFloatingPointType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00006061 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006062 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
6063 DCHECK(DataType::IsIntOrLongType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00006064 }
6065 }
6066 return result;
6067 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01006068
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006069 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
6070
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006071 HInstruction* GetArray() const { return InputAt(0); }
6072 HInstruction* GetIndex() const { return InputAt(1); }
6073
Vladimir Marko61b92282017-10-11 13:23:17 +01006074 void SetType(DataType::Type new_type) {
6075 DCHECK(DataType::IsIntegralType(GetType()));
6076 DCHECK(DataType::IsIntegralType(new_type));
6077 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
6078 SetPackedField<TypeField>(new_type);
6079 }
6080
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006081 DECLARE_INSTRUCTION(ArrayGet);
6082
Artem Serovcced8ba2017-07-19 18:18:09 +01006083 protected:
6084 DEFAULT_COPY_CONSTRUCTOR(ArrayGet);
6085
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006086 private:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006087 // We treat a String as an array, creating the HArrayGet from String.charAt()
6088 // intrinsic in the instruction simplifier. We can always determine whether
6089 // a particular HArrayGet is actually a String.charAt() by looking at the type
6090 // of the input but that requires holding the mutator lock, so we prefer to use
6091 // a flag, so that code generators don't need to do the locking.
Vladimir Markobd785672018-05-03 17:09:09 +01006092 static constexpr size_t kFlagIsStringCharAt = kNumberOfGenericPackedBits;
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006093 static constexpr size_t kNumberOfArrayGetPackedBits = kFlagIsStringCharAt + 1;
6094 static_assert(kNumberOfArrayGetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6095 "Too many packed fields.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006096};
6097
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006098class HArraySet final : public HExpression<3> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006099 public:
6100 HArraySet(HInstruction* array,
6101 HInstruction* index,
6102 HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006103 DataType::Type expected_component_type,
Aart Bik18b36ab2016-04-13 16:41:35 -07006104 uint32_t dex_pc)
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006105 : HArraySet(array,
6106 index,
6107 value,
6108 expected_component_type,
6109 // Make a best guess for side effects now, may be refined during SSA building.
6110 ComputeSideEffects(GetComponentType(value->GetType(), expected_component_type)),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306111 dex_pc) {
6112 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006113
6114 HArraySet(HInstruction* array,
6115 HInstruction* index,
6116 HInstruction* value,
6117 DataType::Type expected_component_type,
6118 SideEffects side_effects,
6119 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006120 : HExpression(kArraySet, side_effects, dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006121 SetPackedField<ExpectedComponentTypeField>(expected_component_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006122 SetPackedFlag<kFlagNeedsTypeCheck>(value->GetType() == DataType::Type::kReference);
Vladimir Markoa1de9182016-02-25 11:37:38 +00006123 SetPackedFlag<kFlagValueCanBeNull>(true);
6124 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(false);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006125 SetRawInputAt(0, array);
6126 SetRawInputAt(1, index);
6127 SetRawInputAt(2, value);
6128 }
6129
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006130 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01006131
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006132 bool NeedsEnvironment() const override {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006133 // We call a runtime method to throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006134 return NeedsTypeCheck();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006135 }
6136
Mingyao Yang81014cb2015-06-02 03:16:27 -07006137 // Can throw ArrayStoreException.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006138 bool CanThrow() const override { return NeedsTypeCheck(); }
Mingyao Yang81014cb2015-06-02 03:16:27 -07006139
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006140 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const override {
Calin Juravle77520bc2015-01-12 18:45:46 +00006141 // TODO: Same as for ArrayGet.
6142 return false;
6143 }
6144
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006145 void ClearNeedsTypeCheck() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006146 SetPackedFlag<kFlagNeedsTypeCheck>(false);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006147 }
6148
Nicolas Geoffray07276db2015-05-18 14:22:09 +01006149 void ClearValueCanBeNull() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006150 SetPackedFlag<kFlagValueCanBeNull>(false);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01006151 }
6152
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006153 void SetStaticTypeOfArrayIsObjectArray() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006154 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(true);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006155 }
6156
Vladimir Markoa1de9182016-02-25 11:37:38 +00006157 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
6158 bool NeedsTypeCheck() const { return GetPackedFlag<kFlagNeedsTypeCheck>(); }
6159 bool StaticTypeOfArrayIsObjectArray() const {
6160 return GetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>();
6161 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006162
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006163 HInstruction* GetArray() const { return InputAt(0); }
6164 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006165 HInstruction* GetValue() const { return InputAt(2); }
6166
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006167 DataType::Type GetComponentType() const {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006168 return GetComponentType(GetValue()->GetType(), GetRawExpectedComponentType());
6169 }
6170
6171 static DataType::Type GetComponentType(DataType::Type value_type,
6172 DataType::Type expected_component_type) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006173 // The Dex format does not type floating point index operations. Since the
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006174 // `expected_component_type` comes from SSA building and can therefore not
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006175 // be correct, we also check what is the value type. If it is a floating
6176 // point type, we must use that type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006177 return ((value_type == DataType::Type::kFloat32) || (value_type == DataType::Type::kFloat64))
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006178 ? value_type
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006179 : expected_component_type;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006180 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01006181
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006182 DataType::Type GetRawExpectedComponentType() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006183 return GetPackedField<ExpectedComponentTypeField>();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01006184 }
6185
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006186 static SideEffects ComputeSideEffects(DataType::Type type) {
6187 return SideEffects::ArrayWriteOfType(type).Union(SideEffectsForArchRuntimeCalls(type));
Aart Bik18b36ab2016-04-13 16:41:35 -07006188 }
6189
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006190 static SideEffects SideEffectsForArchRuntimeCalls(DataType::Type value_type) {
6191 return (value_type == DataType::Type::kReference) ? SideEffects::CanTriggerGC()
6192 : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006193 }
6194
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006195 DECLARE_INSTRUCTION(ArraySet);
6196
Artem Serovcced8ba2017-07-19 18:18:09 +01006197 protected:
6198 DEFAULT_COPY_CONSTRUCTOR(ArraySet);
6199
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006200 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006201 static constexpr size_t kFieldExpectedComponentType = kNumberOfGenericPackedBits;
6202 static constexpr size_t kFieldExpectedComponentTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006203 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00006204 static constexpr size_t kFlagNeedsTypeCheck =
6205 kFieldExpectedComponentType + kFieldExpectedComponentTypeSize;
6206 static constexpr size_t kFlagValueCanBeNull = kFlagNeedsTypeCheck + 1;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006207 // Cached information for the reference_type_info_ so that codegen
6208 // does not need to inspect the static type.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006209 static constexpr size_t kFlagStaticTypeOfArrayIsObjectArray = kFlagValueCanBeNull + 1;
6210 static constexpr size_t kNumberOfArraySetPackedBits =
6211 kFlagStaticTypeOfArrayIsObjectArray + 1;
6212 static_assert(kNumberOfArraySetPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6213 using ExpectedComponentTypeField =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006214 BitField<DataType::Type, kFieldExpectedComponentType, kFieldExpectedComponentTypeSize>;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006215};
6216
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006217class HArrayLength final : public HExpression<1> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006218 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006219 HArrayLength(HInstruction* array, uint32_t dex_pc, bool is_string_length = false)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306220 : HExpression(kArrayLength, DataType::Type::kInt32, SideEffects::None(), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006221 SetPackedFlag<kFlagIsStringLength>(is_string_length);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01006222 // Note that arrays do not change length, so the instruction does not
6223 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006224 SetRawInputAt(0, array);
6225 }
6226
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006227 bool IsClonable() const override { return true; }
6228 bool CanBeMoved() const override { return true; }
6229 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07006230 return true;
6231 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006232 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Calin Juravle641547a2015-04-21 22:08:51 +01006233 return obj == InputAt(0);
6234 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01006235
Vladimir Markodce016e2016-04-28 13:10:02 +01006236 bool IsStringLength() const { return GetPackedFlag<kFlagIsStringLength>(); }
6237
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006238 DECLARE_INSTRUCTION(ArrayLength);
6239
Artem Serovcced8ba2017-07-19 18:18:09 +01006240 protected:
6241 DEFAULT_COPY_CONSTRUCTOR(ArrayLength);
6242
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006243 private:
Vladimir Markodce016e2016-04-28 13:10:02 +01006244 // We treat a String as an array, creating the HArrayLength from String.length()
6245 // or String.isEmpty() intrinsic in the instruction simplifier. We can always
6246 // determine whether a particular HArrayLength is actually a String.length() by
6247 // looking at the type of the input but that requires holding the mutator lock, so
6248 // we prefer to use a flag, so that code generators don't need to do the locking.
Vladimir Markobd785672018-05-03 17:09:09 +01006249 static constexpr size_t kFlagIsStringLength = kNumberOfGenericPackedBits;
Vladimir Markodce016e2016-04-28 13:10:02 +01006250 static constexpr size_t kNumberOfArrayLengthPackedBits = kFlagIsStringLength + 1;
6251 static_assert(kNumberOfArrayLengthPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6252 "Too many packed fields.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006253};
6254
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006255class HBoundsCheck final : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006256 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00006257 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
Artem Serovd1aa7d02018-06-22 11:35:46 +01006258 // constructor. However it can only do it on a fatal slow path so execution never returns to the
6259 // instruction following the current one; thus 'SideEffects::None()' is used.
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006260 HBoundsCheck(HInstruction* index,
6261 HInstruction* length,
6262 uint32_t dex_pc,
Vladimir Marko0259c242017-12-04 11:27:47 +00006263 bool is_string_char_at = false)
Artem Serovd1aa7d02018-06-22 11:35:46 +01006264 : HExpression(kBoundsCheck, index->GetType(), SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006265 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(index->GetType()));
Vladimir Marko0259c242017-12-04 11:27:47 +00006266 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006267 SetRawInputAt(0, index);
6268 SetRawInputAt(1, length);
6269 }
6270
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006271 bool IsClonable() const override { return true; }
6272 bool CanBeMoved() const override { return true; }
6273 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07006274 return true;
6275 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01006276
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006277 bool NeedsEnvironment() const override { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006278
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006279 bool CanThrow() const override { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01006280
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00006281 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006282
Alexandre Ramese6dbf482015-10-19 10:10:41 +01006283 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006284
6285 DECLARE_INSTRUCTION(BoundsCheck);
6286
Artem Serovcced8ba2017-07-19 18:18:09 +01006287 protected:
6288 DEFAULT_COPY_CONSTRUCTOR(BoundsCheck);
6289
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006290 private:
Vladimir Markobd785672018-05-03 17:09:09 +01006291 static constexpr size_t kFlagIsStringCharAt = kNumberOfGenericPackedBits;
Vladimir Markof02046e2018-10-02 15:31:32 +01006292 static constexpr size_t kNumberOfBoundsCheckPackedBits = kFlagIsStringCharAt + 1;
6293 static_assert(kNumberOfBoundsCheckPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6294 "Too many packed fields.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006295};
6296
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006297class HSuspendCheck final : public HExpression<0> {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006298 public:
David Brazdil86ea7ee2016-02-16 09:26:07 +00006299 explicit HSuspendCheck(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01006300 : HExpression(kSuspendCheck, SideEffects::CanTriggerGC(), dex_pc),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306301 slow_path_(nullptr) {
6302 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006303
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006304 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01006305
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006306 bool NeedsEnvironment() const override {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006307 return true;
6308 }
6309
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006310 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
6311 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006312
6313 DECLARE_INSTRUCTION(SuspendCheck);
6314
Artem Serovcced8ba2017-07-19 18:18:09 +01006315 protected:
6316 DEFAULT_COPY_CONSTRUCTOR(SuspendCheck);
6317
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006318 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006319 // Only used for code generation, in order to share the same slow path between back edges
6320 // of a same loop.
6321 SlowPathCode* slow_path_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006322};
6323
David Srbecky0cf44932015-12-09 14:09:59 +00006324// Pseudo-instruction which provides the native debugger with mapping information.
6325// It ensures that we can generate line number and local variables at this point.
Vladimir Markobd785672018-05-03 17:09:09 +01006326class HNativeDebugInfo : public HExpression<0> {
David Srbecky0cf44932015-12-09 14:09:59 +00006327 public:
6328 explicit HNativeDebugInfo(uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006329 : HExpression<0>(kNativeDebugInfo, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306330 }
David Srbecky0cf44932015-12-09 14:09:59 +00006331
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006332 bool NeedsEnvironment() const override {
David Srbecky0cf44932015-12-09 14:09:59 +00006333 return true;
6334 }
6335
6336 DECLARE_INSTRUCTION(NativeDebugInfo);
6337
Artem Serovcced8ba2017-07-19 18:18:09 +01006338 protected:
6339 DEFAULT_COPY_CONSTRUCTOR(NativeDebugInfo);
David Srbecky0cf44932015-12-09 14:09:59 +00006340};
6341
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006342/**
6343 * Instruction to load a Class object.
6344 */
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006345class HLoadClass final : public HInstruction {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006346 public:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006347 // Determines how to load the Class.
6348 enum class LoadKind {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006349 // We cannot load this class. See HSharpening::SharpenLoadClass.
6350 kInvalid = -1,
6351
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006352 // Use the Class* from the method's own ArtMethod*.
6353 kReferrersClass,
6354
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006355 // Use PC-relative boot image Class* address that will be known at link time.
Vladimir Marko764d4542017-05-16 10:31:41 +01006356 // Used for boot image classes referenced by boot image code.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006357 kBootImageLinkTimePcRelative,
6358
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006359 // Load from an entry in the .data.bimg.rel.ro using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006360 // Used for boot image classes referenced by apps in AOT-compiled code.
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006361 kBootImageRelRo,
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006362
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006363 // Load from an entry in the .bss section using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006364 // Used for classes outside boot image referenced by AOT-compiled app and boot image code.
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006365 kBssEntry,
6366
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006367 // Use a known boot image Class* address, embedded in the code by the codegen.
6368 // Used for boot image classes referenced by apps in JIT-compiled code.
6369 kJitBootImageAddress,
6370
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006371 // Load from the root table associated with the JIT compiled method.
6372 kJitTableAddress,
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006373
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006374 // Load using a simple runtime call. This is the fall-back load kind when
6375 // the codegen is unable to use another appropriate kind.
6376 kRuntimeCall,
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006377
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006378 kLast = kRuntimeCall
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006379 };
6380
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01006381 HLoadClass(HCurrentMethod* current_method,
Andreas Gampea5b09a62016-11-17 15:21:22 -08006382 dex::TypeIndex type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01006383 const DexFile& dex_file,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006384 Handle<mirror::Class> klass,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006385 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01006386 uint32_t dex_pc,
Nicolas Geoffray56876342016-12-16 16:09:08 +00006387 bool needs_access_check)
Vladimir Markobd785672018-05-03 17:09:09 +01006388 : HInstruction(kLoadClass,
6389 DataType::Type::kReference,
6390 SideEffectsForArchRuntimeCalls(),
6391 dex_pc),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006392 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006393 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01006394 dex_file_(dex_file),
Vladimir Marko175e7862018-03-27 09:03:13 +00006395 klass_(klass) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01006396 // Referrers class should not need access check. We never inline unverified
6397 // methods so we can't possibly end up in this situation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006398 DCHECK(!is_referrers_class || !needs_access_check);
6399
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006400 SetPackedField<LoadKindField>(
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006401 is_referrers_class ? LoadKind::kReferrersClass : LoadKind::kRuntimeCall);
Vladimir Markoa1de9182016-02-25 11:37:38 +00006402 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
Nicolas Geoffray56876342016-12-16 16:09:08 +00006403 SetPackedFlag<kFlagIsInBootImage>(false);
Vladimir Markoa1de9182016-02-25 11:37:38 +00006404 SetPackedFlag<kFlagGenerateClInitCheck>(false);
Vladimir Marko175e7862018-03-27 09:03:13 +00006405 SetPackedFlag<kFlagValidLoadedClassRTI>(false);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006406 }
6407
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006408 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01006409
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006410 void SetLoadKind(LoadKind load_kind);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006411
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006412 LoadKind GetLoadKind() const {
6413 return GetPackedField<LoadKindField>();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01006414 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006415
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006416 bool HasPcRelativeLoadKind() const {
6417 return GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
6418 GetLoadKind() == LoadKind::kBootImageRelRo ||
6419 GetLoadKind() == LoadKind::kBssEntry;
6420 }
6421
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006422 bool CanBeMoved() const override { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006423
Yi Kong39402542019-03-24 02:47:16 -07006424 bool InstructionDataEquals(const HInstruction* other) const override;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006425
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006426 size_t ComputeHashCode() const override { return type_index_.index_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006427
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006428 bool CanBeNull() const override { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006429
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006430 bool NeedsEnvironment() const override {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006431 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006432 }
6433
Calin Juravle0ba218d2015-05-19 18:46:01 +01006434 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00006435 // The entrypoint the code generator is going to call does not do
6436 // clinit of the class.
6437 DCHECK(!NeedsAccessCheck());
Vladimir Markoa1de9182016-02-25 11:37:38 +00006438 SetPackedFlag<kFlagGenerateClInitCheck>(generate_clinit_check);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006439 }
6440
6441 bool CanCallRuntime() const {
Vladimir Marko41559982017-01-06 14:04:23 +00006442 return NeedsAccessCheck() ||
6443 MustGenerateClinitCheck() ||
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006444 GetLoadKind() == LoadKind::kRuntimeCall ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006445 GetLoadKind() == LoadKind::kBssEntry;
Calin Juravle98893e12015-10-02 21:05:03 +01006446 }
6447
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006448 bool CanThrow() const override {
Vladimir Marko41559982017-01-06 14:04:23 +00006449 return NeedsAccessCheck() ||
6450 MustGenerateClinitCheck() ||
6451 // If the class is in the boot image, the lookup in the runtime call cannot throw.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006452 ((GetLoadKind() == LoadKind::kRuntimeCall ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006453 GetLoadKind() == LoadKind::kBssEntry) &&
6454 !IsInBootImage());
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006455 }
6456
Calin Juravleacf735c2015-02-12 15:25:22 +00006457 ReferenceTypeInfo GetLoadedClassRTI() {
Vladimir Marko175e7862018-03-27 09:03:13 +00006458 if (GetPackedFlag<kFlagValidLoadedClassRTI>()) {
6459 // Note: The is_exact flag from the return value should not be used.
Andreas Gampe3db70682018-12-26 15:12:03 -08006460 return ReferenceTypeInfo::CreateUnchecked(klass_, /* is_exact= */ true);
Vladimir Marko175e7862018-03-27 09:03:13 +00006461 } else {
6462 return ReferenceTypeInfo::CreateInvalid();
6463 }
Calin Juravleacf735c2015-02-12 15:25:22 +00006464 }
6465
Vladimir Marko175e7862018-03-27 09:03:13 +00006466 // Loaded class RTI is marked as valid by RTP if the klass_ is admissible.
6467 void SetValidLoadedClassRTI() REQUIRES_SHARED(Locks::mutator_lock_) {
6468 DCHECK(klass_ != nullptr);
6469 SetPackedFlag<kFlagValidLoadedClassRTI>(true);
Calin Juravleacf735c2015-02-12 15:25:22 +00006470 }
6471
Andreas Gampea5b09a62016-11-17 15:21:22 -08006472 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006473 const DexFile& GetDexFile() const { return dex_file_; }
6474
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006475 bool NeedsDexCacheOfDeclaringClass() const override {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006476 return GetLoadKind() == LoadKind::kRuntimeCall;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006477 }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00006478
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006479 static SideEffects SideEffectsForArchRuntimeCalls() {
6480 return SideEffects::CanTriggerGC();
6481 }
6482
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006483 bool IsReferrersClass() const { return GetLoadKind() == LoadKind::kReferrersClass; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006484 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006485 bool IsInBootImage() const { return GetPackedFlag<kFlagIsInBootImage>(); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006486 bool MustGenerateClinitCheck() const { return GetPackedFlag<kFlagGenerateClInitCheck>(); }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006487
Vladimir Markoa9f303c2018-07-20 16:43:56 +01006488 bool MustResolveTypeOnSlowPath() const {
6489 // Check that this instruction has a slow path.
6490 DCHECK(GetLoadKind() != LoadKind::kRuntimeCall); // kRuntimeCall calls on main path.
6491 DCHECK(GetLoadKind() == LoadKind::kBssEntry || MustGenerateClinitCheck());
6492 return GetLoadKind() == LoadKind::kBssEntry;
6493 }
6494
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006495 void MarkInBootImage() {
6496 SetPackedFlag<kFlagIsInBootImage>(true);
6497 }
6498
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006499 void AddSpecialInput(HInstruction* special_input);
6500
6501 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01006502 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006503 return ArrayRef<HUserRecord<HInstruction*>>(
6504 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
6505 }
6506
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006507 Handle<mirror::Class> GetClass() const {
6508 return klass_;
6509 }
6510
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006511 DECLARE_INSTRUCTION(LoadClass);
6512
Artem Serovcced8ba2017-07-19 18:18:09 +01006513 protected:
6514 DEFAULT_COPY_CONSTRUCTOR(LoadClass);
6515
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006516 private:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006517 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfGenericPackedBits;
Vladimir Marko41559982017-01-06 14:04:23 +00006518 static constexpr size_t kFlagIsInBootImage = kFlagNeedsAccessCheck + 1;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006519 // Whether this instruction must generate the initialization check.
6520 // Used for code generation.
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006521 static constexpr size_t kFlagGenerateClInitCheck = kFlagIsInBootImage + 1;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006522 static constexpr size_t kFieldLoadKind = kFlagGenerateClInitCheck + 1;
6523 static constexpr size_t kFieldLoadKindSize =
6524 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
Vladimir Marko175e7862018-03-27 09:03:13 +00006525 static constexpr size_t kFlagValidLoadedClassRTI = kFieldLoadKind + kFieldLoadKindSize;
6526 static constexpr size_t kNumberOfLoadClassPackedBits = kFlagValidLoadedClassRTI + 1;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006527 static_assert(kNumberOfLoadClassPackedBits < kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006528 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
6529
6530 static bool HasTypeReference(LoadKind load_kind) {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006531 return load_kind == LoadKind::kReferrersClass ||
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006532 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006533 load_kind == LoadKind::kBssEntry ||
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006534 load_kind == LoadKind::kRuntimeCall;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006535 }
6536
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006537 void SetLoadKindInternal(LoadKind load_kind);
6538
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006539 // The special input is the HCurrentMethod for kRuntimeCall or kReferrersClass.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006540 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006541 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006542 HUserRecord<HInstruction*> special_input_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006543
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006544 // A type index and dex file where the class can be accessed. The dex file can be:
6545 // - The compiling method's dex file if the class is defined there too.
6546 // - The compiling method's dex file if the class is referenced there.
6547 // - The dex file where the class is defined. When the load kind can only be
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006548 // kBssEntry or kRuntimeCall, we cannot emit code for this `HLoadClass`.
Andreas Gampea5b09a62016-11-17 15:21:22 -08006549 const dex::TypeIndex type_index_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006550 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006551
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006552 Handle<mirror::Class> klass_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006553};
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006554std::ostream& operator<<(std::ostream& os, HLoadClass::LoadKind rhs);
6555
6556// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
Vladimir Marko28e012a2017-12-07 11:22:59 +00006557inline void HLoadClass::SetLoadKind(LoadKind load_kind) {
6558 // The load kind should be determined before inserting the instruction to the graph.
6559 DCHECK(GetBlock() == nullptr);
6560 DCHECK(GetEnvironment() == nullptr);
6561 SetPackedField<LoadKindField>(load_kind);
6562 if (load_kind != LoadKind::kRuntimeCall && load_kind != LoadKind::kReferrersClass) {
6563 special_input_ = HUserRecord<HInstruction*>(nullptr);
6564 }
6565 if (!NeedsEnvironment()) {
6566 SetSideEffects(SideEffects::None());
6567 }
6568}
6569
6570// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006571inline void HLoadClass::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07006572 // The special input is used for PC-relative loads on some architectures,
6573 // including literal pool loads, which are PC-relative too.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006574 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006575 GetLoadKind() == LoadKind::kBootImageRelRo ||
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006576 GetLoadKind() == LoadKind::kBssEntry ||
6577 GetLoadKind() == LoadKind::kJitBootImageAddress) << GetLoadKind();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006578 DCHECK(special_input_.GetInstruction() == nullptr);
6579 special_input_ = HUserRecord<HInstruction*>(special_input);
6580 special_input->AddUseAt(this, 0);
6581}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006582
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006583class HLoadString final : public HInstruction {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006584 public:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006585 // Determines how to load the String.
6586 enum class LoadKind {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006587 // Use PC-relative boot image String* address that will be known at link time.
Vladimir Marko764d4542017-05-16 10:31:41 +01006588 // Used for boot image strings referenced by boot image code.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006589 kBootImageLinkTimePcRelative,
6590
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006591 // Load from an entry in the .data.bimg.rel.ro using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006592 // Used for boot image strings referenced by apps in AOT-compiled code.
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006593 kBootImageRelRo,
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006594
Vladimir Markoaad75c62016-10-03 08:46:48 +00006595 // Load from an entry in the .bss section using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006596 // Used for strings outside boot image referenced by AOT-compiled app and boot image code.
Vladimir Markoaad75c62016-10-03 08:46:48 +00006597 kBssEntry,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006598
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006599 // Use a known boot image String* address, embedded in the code by the codegen.
6600 // Used for boot image strings referenced by apps in JIT-compiled code.
6601 kJitBootImageAddress,
6602
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006603 // Load from the root table associated with the JIT compiled method.
6604 kJitTableAddress,
6605
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006606 // Load using a simple runtime call. This is the fall-back load kind when
6607 // the codegen is unable to use another appropriate kind.
6608 kRuntimeCall,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006609
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006610 kLast = kRuntimeCall,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006611 };
6612
Nicolas Geoffray917d0162015-11-24 18:25:35 +00006613 HLoadString(HCurrentMethod* current_method,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006614 dex::StringIndex string_index,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006615 const DexFile& dex_file,
6616 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006617 : HInstruction(kLoadString,
6618 DataType::Type::kReference,
6619 SideEffectsForArchRuntimeCalls(),
6620 dex_pc),
Vladimir Marko372f10e2016-05-17 16:30:10 +01006621 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006622 string_index_(string_index),
6623 dex_file_(dex_file) {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006624 SetPackedField<LoadKindField>(LoadKind::kRuntimeCall);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01006625 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006626
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006627 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01006628
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006629 void SetLoadKind(LoadKind load_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006630
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006631 LoadKind GetLoadKind() const {
6632 return GetPackedField<LoadKindField>();
6633 }
6634
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006635 bool HasPcRelativeLoadKind() const {
6636 return GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
6637 GetLoadKind() == LoadKind::kBootImageRelRo ||
6638 GetLoadKind() == LoadKind::kBssEntry;
6639 }
6640
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006641 const DexFile& GetDexFile() const {
6642 return dex_file_;
6643 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006644
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006645 dex::StringIndex GetStringIndex() const {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006646 return string_index_;
6647 }
6648
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006649 Handle<mirror::String> GetString() const {
6650 return string_;
6651 }
6652
6653 void SetString(Handle<mirror::String> str) {
6654 string_ = str;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006655 }
6656
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006657 bool CanBeMoved() const override { return true; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006658
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006659 bool InstructionDataEquals(const HInstruction* other) const override;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006660
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006661 size_t ComputeHashCode() const override { return string_index_.index_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006662
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006663 // Will call the runtime if we need to load the string through
6664 // the dex cache and the string is not guaranteed to be there yet.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006665 bool NeedsEnvironment() const override {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006666 LoadKind load_kind = GetLoadKind();
Vladimir Marko764d4542017-05-16 10:31:41 +01006667 if (load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006668 load_kind == LoadKind::kBootImageRelRo ||
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006669 load_kind == LoadKind::kJitBootImageAddress ||
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006670 load_kind == LoadKind::kJitTableAddress) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006671 return false;
6672 }
Vladimir Marko4d1be4922017-01-06 14:43:11 +00006673 return true;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006674 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006675
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006676 bool NeedsDexCacheOfDeclaringClass() const override {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006677 return GetLoadKind() == LoadKind::kRuntimeCall;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006678 }
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006679
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006680 bool CanBeNull() const override { return false; }
6681 bool CanThrow() const override { return NeedsEnvironment(); }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006682
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006683 static SideEffects SideEffectsForArchRuntimeCalls() {
6684 return SideEffects::CanTriggerGC();
6685 }
6686
Vladimir Marko372f10e2016-05-17 16:30:10 +01006687 void AddSpecialInput(HInstruction* special_input);
6688
6689 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01006690 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Vladimir Marko372f10e2016-05-17 16:30:10 +01006691 return ArrayRef<HUserRecord<HInstruction*>>(
6692 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006693 }
6694
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006695 DECLARE_INSTRUCTION(LoadString);
6696
Artem Serovcced8ba2017-07-19 18:18:09 +01006697 protected:
6698 DEFAULT_COPY_CONSTRUCTOR(LoadString);
6699
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006700 private:
Vladimir Marko4d1be4922017-01-06 14:43:11 +00006701 static constexpr size_t kFieldLoadKind = kNumberOfGenericPackedBits;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006702 static constexpr size_t kFieldLoadKindSize =
6703 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
6704 static constexpr size_t kNumberOfLoadStringPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006705 static_assert(kNumberOfLoadStringPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006706 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006707
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006708 void SetLoadKindInternal(LoadKind load_kind);
6709
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006710 // The special input is the HCurrentMethod for kRuntimeCall.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006711 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko48886c22017-01-06 11:45:47 +00006712 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Marko372f10e2016-05-17 16:30:10 +01006713 HUserRecord<HInstruction*> special_input_;
6714
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006715 dex::StringIndex string_index_;
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006716 const DexFile& dex_file_;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006717
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006718 Handle<mirror::String> string_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006719};
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006720std::ostream& operator<<(std::ostream& os, HLoadString::LoadKind rhs);
6721
6722// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
Vladimir Marko28e012a2017-12-07 11:22:59 +00006723inline void HLoadString::SetLoadKind(LoadKind load_kind) {
6724 // The load kind should be determined before inserting the instruction to the graph.
6725 DCHECK(GetBlock() == nullptr);
6726 DCHECK(GetEnvironment() == nullptr);
6727 DCHECK_EQ(GetLoadKind(), LoadKind::kRuntimeCall);
6728 SetPackedField<LoadKindField>(load_kind);
6729 if (load_kind != LoadKind::kRuntimeCall) {
6730 special_input_ = HUserRecord<HInstruction*>(nullptr);
6731 }
6732 if (!NeedsEnvironment()) {
6733 SetSideEffects(SideEffects::None());
6734 }
6735}
6736
6737// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006738inline void HLoadString::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07006739 // The special input is used for PC-relative loads on some architectures,
6740 // including literal pool loads, which are PC-relative too.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006741 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006742 GetLoadKind() == LoadKind::kBootImageRelRo ||
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006743 GetLoadKind() == LoadKind::kBssEntry ||
6744 GetLoadKind() == LoadKind::kJitBootImageAddress) << GetLoadKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01006745 // HLoadString::GetInputRecords() returns an empty array at this point,
6746 // so use the GetInputRecords() from the base class to set the input record.
6747 DCHECK(special_input_.GetInstruction() == nullptr);
6748 special_input_ = HUserRecord<HInstruction*>(special_input);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006749 special_input->AddUseAt(this, 0);
6750}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006751
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006752class HLoadMethodHandle final : public HInstruction {
Orion Hodsondbaa5c72018-05-10 08:22:46 +01006753 public:
6754 HLoadMethodHandle(HCurrentMethod* current_method,
Orion Hodson4c8e12e2018-05-18 08:33:20 +01006755 uint16_t method_handle_idx,
6756 const DexFile& dex_file,
6757 uint32_t dex_pc)
Orion Hodsondbaa5c72018-05-10 08:22:46 +01006758 : HInstruction(kLoadMethodHandle,
6759 DataType::Type::kReference,
6760 SideEffectsForArchRuntimeCalls(),
6761 dex_pc),
6762 special_input_(HUserRecord<HInstruction*>(current_method)),
6763 method_handle_idx_(method_handle_idx),
6764 dex_file_(dex_file) {
6765 }
6766
6767 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01006768 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Orion Hodsondbaa5c72018-05-10 08:22:46 +01006769 return ArrayRef<HUserRecord<HInstruction*>>(
6770 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
6771 }
6772
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006773 bool IsClonable() const override { return true; }
Orion Hodsondbaa5c72018-05-10 08:22:46 +01006774
6775 uint16_t GetMethodHandleIndex() const { return method_handle_idx_; }
6776
6777 const DexFile& GetDexFile() const { return dex_file_; }
6778
6779 static SideEffects SideEffectsForArchRuntimeCalls() {
6780 return SideEffects::CanTriggerGC();
6781 }
6782
6783 DECLARE_INSTRUCTION(LoadMethodHandle);
6784
6785 protected:
6786 DEFAULT_COPY_CONSTRUCTOR(LoadMethodHandle);
6787
6788 private:
6789 // The special input is the HCurrentMethod for kRuntimeCall.
6790 HUserRecord<HInstruction*> special_input_;
6791
6792 const uint16_t method_handle_idx_;
6793 const DexFile& dex_file_;
6794};
6795
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006796class HLoadMethodType final : public HInstruction {
Orion Hodson18259d72018-04-12 11:18:23 +01006797 public:
6798 HLoadMethodType(HCurrentMethod* current_method,
Orion Hodson06d10a72018-05-14 08:53:38 +01006799 dex::ProtoIndex proto_index,
Orion Hodson18259d72018-04-12 11:18:23 +01006800 const DexFile& dex_file,
6801 uint32_t dex_pc)
6802 : HInstruction(kLoadMethodType,
6803 DataType::Type::kReference,
6804 SideEffectsForArchRuntimeCalls(),
6805 dex_pc),
6806 special_input_(HUserRecord<HInstruction*>(current_method)),
Orion Hodson06d10a72018-05-14 08:53:38 +01006807 proto_index_(proto_index),
Orion Hodson18259d72018-04-12 11:18:23 +01006808 dex_file_(dex_file) {
6809 }
6810
6811 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01006812 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Orion Hodson18259d72018-04-12 11:18:23 +01006813 return ArrayRef<HUserRecord<HInstruction*>>(
6814 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
6815 }
6816
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006817 bool IsClonable() const override { return true; }
Orion Hodson18259d72018-04-12 11:18:23 +01006818
Orion Hodson06d10a72018-05-14 08:53:38 +01006819 dex::ProtoIndex GetProtoIndex() const { return proto_index_; }
Orion Hodson18259d72018-04-12 11:18:23 +01006820
6821 const DexFile& GetDexFile() const { return dex_file_; }
6822
6823 static SideEffects SideEffectsForArchRuntimeCalls() {
6824 return SideEffects::CanTriggerGC();
6825 }
6826
6827 DECLARE_INSTRUCTION(LoadMethodType);
6828
6829 protected:
6830 DEFAULT_COPY_CONSTRUCTOR(LoadMethodType);
6831
6832 private:
6833 // The special input is the HCurrentMethod for kRuntimeCall.
6834 HUserRecord<HInstruction*> special_input_;
6835
Orion Hodson06d10a72018-05-14 08:53:38 +01006836 const dex::ProtoIndex proto_index_;
Orion Hodson18259d72018-04-12 11:18:23 +01006837 const DexFile& dex_file_;
6838};
6839
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006840/**
6841 * Performs an initialization check on its Class object input.
6842 */
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006843class HClinitCheck final : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006844 public:
Roland Levillain3887c462015-08-12 18:15:42 +01006845 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07006846 : HExpression(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306847 kClinitCheck,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006848 DataType::Type::kReference,
Mingyao Yang217eb062017-12-11 15:20:07 -08006849 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006850 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006851 SetRawInputAt(0, constant);
6852 }
Artem Serova6e26142018-06-19 14:55:17 +01006853 // TODO: Make ClinitCheck clonable.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006854 bool CanBeMoved() const override { return true; }
6855 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006856 return true;
6857 }
6858
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006859 bool NeedsEnvironment() const override {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006860 // May call runtime to initialize the class.
6861 return true;
6862 }
6863
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006864 bool CanThrow() const override { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006865
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00006866 HLoadClass* GetLoadClass() const {
6867 DCHECK(InputAt(0)->IsLoadClass());
6868 return InputAt(0)->AsLoadClass();
6869 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006870
6871 DECLARE_INSTRUCTION(ClinitCheck);
6872
Artem Serovcced8ba2017-07-19 18:18:09 +01006873
6874 protected:
6875 DEFAULT_COPY_CONSTRUCTOR(ClinitCheck);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006876};
6877
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006878class HStaticFieldGet final : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006879 public:
6880 HStaticFieldGet(HInstruction* cls,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006881 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006882 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006883 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006884 bool is_volatile,
6885 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006886 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006887 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01006888 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306889 : HExpression(kStaticFieldGet,
6890 field_type,
6891 SideEffects::FieldReadOfType(field_type, is_volatile),
6892 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006893 field_info_(field,
6894 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006895 field_type,
6896 is_volatile,
6897 field_idx,
6898 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006899 dex_file) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006900 SetRawInputAt(0, cls);
6901 }
6902
Calin Juravle52c48962014-12-16 17:02:57 +00006903
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006904 bool IsClonable() const override { return true; }
6905 bool CanBeMoved() const override { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006906
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006907 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01006908 const HStaticFieldGet* other_get = other->AsStaticFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00006909 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006910 }
6911
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006912 size_t ComputeHashCode() const override {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006913 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
6914 }
6915
Calin Juravle52c48962014-12-16 17:02:57 +00006916 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006917 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006918 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006919 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006920
Vladimir Marko61b92282017-10-11 13:23:17 +01006921 void SetType(DataType::Type new_type) {
6922 DCHECK(DataType::IsIntegralType(GetType()));
6923 DCHECK(DataType::IsIntegralType(new_type));
6924 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
6925 SetPackedField<TypeField>(new_type);
6926 }
6927
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006928 DECLARE_INSTRUCTION(StaticFieldGet);
6929
Artem Serovcced8ba2017-07-19 18:18:09 +01006930 protected:
6931 DEFAULT_COPY_CONSTRUCTOR(StaticFieldGet);
6932
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006933 private:
6934 const FieldInfo field_info_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006935};
6936
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006937class HStaticFieldSet final : public HExpression<2> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006938 public:
6939 HStaticFieldSet(HInstruction* cls,
6940 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006941 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006942 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006943 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006944 bool is_volatile,
6945 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006946 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006947 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01006948 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006949 : HExpression(kStaticFieldSet,
6950 SideEffects::FieldWriteOfType(field_type, is_volatile),
6951 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006952 field_info_(field,
6953 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006954 field_type,
6955 is_volatile,
6956 field_idx,
6957 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006958 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006959 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006960 SetRawInputAt(0, cls);
6961 SetRawInputAt(1, value);
6962 }
6963
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006964 bool IsClonable() const override { return true; }
Calin Juravle52c48962014-12-16 17:02:57 +00006965 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006966 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006967 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006968 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006969
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006970 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006971 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
6972 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006973
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006974 DECLARE_INSTRUCTION(StaticFieldSet);
6975
Artem Serovcced8ba2017-07-19 18:18:09 +01006976 protected:
6977 DEFAULT_COPY_CONSTRUCTOR(StaticFieldSet);
6978
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006979 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006980 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
6981 static constexpr size_t kNumberOfStaticFieldSetPackedBits = kFlagValueCanBeNull + 1;
6982 static_assert(kNumberOfStaticFieldSetPackedBits <= kMaxNumberOfPackedBits,
6983 "Too many packed fields.");
6984
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006985 const FieldInfo field_info_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006986};
6987
Vladimir Marko552a1342017-10-31 10:56:47 +00006988class HStringBuilderAppend final : public HVariableInputSizeInstruction {
6989 public:
6990 HStringBuilderAppend(HIntConstant* format,
6991 uint32_t number_of_arguments,
6992 ArenaAllocator* allocator,
6993 uint32_t dex_pc)
6994 : HVariableInputSizeInstruction(
6995 kStringBuilderAppend,
6996 DataType::Type::kReference,
6997 // The runtime call may read memory from inputs. It never writes outside
6998 // of the newly allocated result object (or newly allocated helper objects).
6999 SideEffects::AllReads().Union(SideEffects::CanTriggerGC()),
7000 dex_pc,
7001 allocator,
7002 number_of_arguments + /* format */ 1u,
7003 kArenaAllocInvokeInputs) {
7004 DCHECK_GE(number_of_arguments, 1u); // There must be something to append.
7005 SetRawInputAt(FormatIndex(), format);
7006 }
7007
7008 void SetArgumentAt(size_t index, HInstruction* argument) {
7009 DCHECK_LE(index, GetNumberOfArguments());
7010 SetRawInputAt(index, argument);
7011 }
7012
7013 // Return the number of arguments, excluding the format.
7014 size_t GetNumberOfArguments() const {
7015 DCHECK_GE(InputCount(), 1u);
7016 return InputCount() - 1u;
7017 }
7018
7019 size_t FormatIndex() const {
7020 return GetNumberOfArguments();
7021 }
7022
7023 HIntConstant* GetFormat() {
7024 return InputAt(FormatIndex())->AsIntConstant();
7025 }
7026
7027 bool NeedsEnvironment() const override { return true; }
7028
7029 bool CanThrow() const override { return true; }
7030
Vladimir Markob1fe5e12020-03-10 14:30:49 +00007031 bool CanBeNull() const override { return false; }
7032
Vladimir Marko552a1342017-10-31 10:56:47 +00007033 DECLARE_INSTRUCTION(StringBuilderAppend);
7034
7035 protected:
7036 DEFAULT_COPY_CONSTRUCTOR(StringBuilderAppend);
7037};
7038
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007039class HUnresolvedInstanceFieldGet final : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01007040 public:
7041 HUnresolvedInstanceFieldGet(HInstruction* obj,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007042 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01007043 uint32_t field_index,
7044 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307045 : HExpression(kUnresolvedInstanceFieldGet,
7046 field_type,
7047 SideEffects::AllExceptGCDependency(),
7048 dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01007049 field_index_(field_index) {
7050 SetRawInputAt(0, obj);
7051 }
7052
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007053 bool IsClonable() const override { return true; }
7054 bool NeedsEnvironment() const override { return true; }
7055 bool CanThrow() const override { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01007056
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007057 DataType::Type GetFieldType() const { return GetType(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01007058 uint32_t GetFieldIndex() const { return field_index_; }
7059
7060 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
7061
Artem Serovcced8ba2017-07-19 18:18:09 +01007062 protected:
7063 DEFAULT_COPY_CONSTRUCTOR(UnresolvedInstanceFieldGet);
7064
Calin Juravlee460d1d2015-09-29 04:52:17 +01007065 private:
7066 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01007067};
7068
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007069class HUnresolvedInstanceFieldSet final : public HExpression<2> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01007070 public:
7071 HUnresolvedInstanceFieldSet(HInstruction* obj,
7072 HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007073 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01007074 uint32_t field_index,
7075 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01007076 : HExpression(kUnresolvedInstanceFieldSet, SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01007077 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007078 SetPackedField<FieldTypeField>(field_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007079 DCHECK_EQ(DataType::Kind(field_type), DataType::Kind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01007080 SetRawInputAt(0, obj);
7081 SetRawInputAt(1, value);
7082 }
7083
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007084 bool IsClonable() const override { return true; }
7085 bool NeedsEnvironment() const override { return true; }
7086 bool CanThrow() const override { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01007087
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007088 DataType::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01007089 uint32_t GetFieldIndex() const { return field_index_; }
7090
7091 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
7092
Artem Serovcced8ba2017-07-19 18:18:09 +01007093 protected:
7094 DEFAULT_COPY_CONSTRUCTOR(UnresolvedInstanceFieldSet);
7095
Calin Juravlee460d1d2015-09-29 04:52:17 +01007096 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007097 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
7098 static constexpr size_t kFieldFieldTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007099 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00007100 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
7101 kFieldFieldType + kFieldFieldTypeSize;
7102 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
7103 "Too many packed fields.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007104 using FieldTypeField = BitField<DataType::Type, kFieldFieldType, kFieldFieldTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00007105
Calin Juravlee460d1d2015-09-29 04:52:17 +01007106 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01007107};
7108
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007109class HUnresolvedStaticFieldGet final : public HExpression<0> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01007110 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007111 HUnresolvedStaticFieldGet(DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01007112 uint32_t field_index,
7113 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307114 : HExpression(kUnresolvedStaticFieldGet,
7115 field_type,
7116 SideEffects::AllExceptGCDependency(),
7117 dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01007118 field_index_(field_index) {
7119 }
7120
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007121 bool IsClonable() const override { return true; }
7122 bool NeedsEnvironment() const override { return true; }
7123 bool CanThrow() const override { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01007124
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007125 DataType::Type GetFieldType() const { return GetType(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01007126 uint32_t GetFieldIndex() const { return field_index_; }
7127
7128 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
7129
Artem Serovcced8ba2017-07-19 18:18:09 +01007130 protected:
7131 DEFAULT_COPY_CONSTRUCTOR(UnresolvedStaticFieldGet);
7132
Calin Juravlee460d1d2015-09-29 04:52:17 +01007133 private:
7134 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01007135};
7136
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007137class HUnresolvedStaticFieldSet final : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01007138 public:
7139 HUnresolvedStaticFieldSet(HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007140 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01007141 uint32_t field_index,
7142 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01007143 : HExpression(kUnresolvedStaticFieldSet, SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01007144 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007145 SetPackedField<FieldTypeField>(field_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007146 DCHECK_EQ(DataType::Kind(field_type), DataType::Kind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01007147 SetRawInputAt(0, value);
7148 }
7149
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007150 bool IsClonable() const override { return true; }
7151 bool NeedsEnvironment() const override { return true; }
7152 bool CanThrow() const override { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01007153
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007154 DataType::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01007155 uint32_t GetFieldIndex() const { return field_index_; }
7156
7157 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
7158
Artem Serovcced8ba2017-07-19 18:18:09 +01007159 protected:
7160 DEFAULT_COPY_CONSTRUCTOR(UnresolvedStaticFieldSet);
7161
Calin Juravlee460d1d2015-09-29 04:52:17 +01007162 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007163 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
7164 static constexpr size_t kFieldFieldTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007165 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00007166 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
7167 kFieldFieldType + kFieldFieldTypeSize;
7168 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
7169 "Too many packed fields.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007170 using FieldTypeField = BitField<DataType::Type, kFieldFieldType, kFieldFieldTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00007171
Calin Juravlee460d1d2015-09-29 04:52:17 +01007172 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01007173};
7174
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007175// Implement the move-exception DEX instruction.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007176class HLoadException final : public HExpression<0> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007177 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06007178 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307179 : HExpression(kLoadException, DataType::Type::kReference, SideEffects::None(), dex_pc) {
7180 }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007181
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007182 bool CanBeNull() const override { return false; }
David Brazdilbbd733e2015-08-18 17:48:17 +01007183
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007184 DECLARE_INSTRUCTION(LoadException);
7185
Artem Serovcced8ba2017-07-19 18:18:09 +01007186 protected:
7187 DEFAULT_COPY_CONSTRUCTOR(LoadException);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007188};
7189
David Brazdilcb1c0552015-08-04 16:22:25 +01007190// Implicit part of move-exception which clears thread-local exception storage.
7191// Must not be removed because the runtime expects the TLS to get cleared.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007192class HClearException final : public HExpression<0> {
David Brazdilcb1c0552015-08-04 16:22:25 +01007193 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06007194 explicit HClearException(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01007195 : HExpression(kClearException, SideEffects::AllWrites(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307196 }
David Brazdilcb1c0552015-08-04 16:22:25 +01007197
7198 DECLARE_INSTRUCTION(ClearException);
7199
Artem Serovcced8ba2017-07-19 18:18:09 +01007200 protected:
7201 DEFAULT_COPY_CONSTRUCTOR(ClearException);
David Brazdilcb1c0552015-08-04 16:22:25 +01007202};
7203
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007204class HThrow final : public HExpression<1> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007205 public:
7206 HThrow(HInstruction* exception, uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01007207 : HExpression(kThrow, SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007208 SetRawInputAt(0, exception);
7209 }
7210
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007211 bool IsControlFlow() const override { return true; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007212
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007213 bool NeedsEnvironment() const override { return true; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007214
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007215 bool CanThrow() const override { return true; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007216
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007217 bool AlwaysThrows() const override { return true; }
Aart Bika8b8e9b2018-01-09 11:01:02 -08007218
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007219 DECLARE_INSTRUCTION(Throw);
7220
Artem Serovcced8ba2017-07-19 18:18:09 +01007221 protected:
7222 DEFAULT_COPY_CONSTRUCTOR(Throw);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007223};
7224
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007225/**
7226 * Implementation strategies for the code generator of a HInstanceOf
7227 * or `HCheckCast`.
7228 */
Vladimir Marko9974e3c2020-06-10 16:27:06 +01007229enum class TypeCheckKind { // private marker to avoid generate-operator-out.py from processing.
Calin Juravle98893e12015-10-02 21:05:03 +01007230 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007231 kExactCheck, // Can do a single class compare.
7232 kClassHierarchyCheck, // Can just walk the super class chain.
7233 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
7234 kInterfaceCheck, // No optimization yet when checking against an interface.
7235 kArrayObjectCheck, // Can just check if the array is not primitive.
Vladimir Markoa1de9182016-02-25 11:37:38 +00007236 kArrayCheck, // No optimization yet when checking against a generic array.
Vladimir Marko175e7862018-03-27 09:03:13 +00007237 kBitstringCheck, // Compare the type check bitstring.
Vladimir Markoa1de9182016-02-25 11:37:38 +00007238 kLast = kArrayCheck
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007239};
7240
Roland Levillain86503782016-02-11 19:07:30 +00007241std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs);
7242
Vladimir Marko175e7862018-03-27 09:03:13 +00007243// Note: HTypeCheckInstruction is just a helper class, not an abstract instruction with an
7244// `IsTypeCheckInstruction()`. (New virtual methods in the HInstruction class have a high cost.)
7245class HTypeCheckInstruction : public HVariableInputSizeInstruction {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007246 public:
Vladimir Marko175e7862018-03-27 09:03:13 +00007247 HTypeCheckInstruction(InstructionKind kind,
Vladimir Markobd785672018-05-03 17:09:09 +01007248 DataType::Type type,
Vladimir Marko175e7862018-03-27 09:03:13 +00007249 HInstruction* object,
7250 HInstruction* target_class_or_null,
7251 TypeCheckKind check_kind,
7252 Handle<mirror::Class> klass,
7253 uint32_t dex_pc,
7254 ArenaAllocator* allocator,
7255 HIntConstant* bitstring_path_to_root,
7256 HIntConstant* bitstring_mask,
7257 SideEffects side_effects)
7258 : HVariableInputSizeInstruction(
7259 kind,
Vladimir Markobd785672018-05-03 17:09:09 +01007260 type,
Vladimir Marko175e7862018-03-27 09:03:13 +00007261 side_effects,
7262 dex_pc,
7263 allocator,
Andreas Gampe3db70682018-12-26 15:12:03 -08007264 /* number_of_inputs= */ check_kind == TypeCheckKind::kBitstringCheck ? 4u : 2u,
Vladimir Marko175e7862018-03-27 09:03:13 +00007265 kArenaAllocTypeCheckInputs),
7266 klass_(klass) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007267 SetPackedField<TypeCheckKindField>(check_kind);
7268 SetPackedFlag<kFlagMustDoNullCheck>(true);
Vladimir Marko175e7862018-03-27 09:03:13 +00007269 SetPackedFlag<kFlagValidTargetClassRTI>(false);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007270 SetRawInputAt(0, object);
Vladimir Marko175e7862018-03-27 09:03:13 +00007271 SetRawInputAt(1, target_class_or_null);
7272 DCHECK_EQ(check_kind == TypeCheckKind::kBitstringCheck, bitstring_path_to_root != nullptr);
7273 DCHECK_EQ(check_kind == TypeCheckKind::kBitstringCheck, bitstring_mask != nullptr);
7274 if (check_kind == TypeCheckKind::kBitstringCheck) {
7275 DCHECK(target_class_or_null->IsNullConstant());
7276 SetRawInputAt(2, bitstring_path_to_root);
7277 SetRawInputAt(3, bitstring_mask);
7278 } else {
7279 DCHECK(target_class_or_null->IsLoadClass());
7280 }
Vladimir Marko87584542017-12-12 17:47:52 +00007281 }
7282
7283 HLoadClass* GetTargetClass() const {
Vladimir Marko175e7862018-03-27 09:03:13 +00007284 DCHECK_NE(GetTypeCheckKind(), TypeCheckKind::kBitstringCheck);
Vladimir Marko87584542017-12-12 17:47:52 +00007285 HInstruction* load_class = InputAt(1);
7286 DCHECK(load_class->IsLoadClass());
7287 return load_class->AsLoadClass();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007288 }
7289
Vladimir Marko175e7862018-03-27 09:03:13 +00007290 uint32_t GetBitstringPathToRoot() const {
7291 DCHECK_EQ(GetTypeCheckKind(), TypeCheckKind::kBitstringCheck);
7292 HInstruction* path_to_root = InputAt(2);
7293 DCHECK(path_to_root->IsIntConstant());
7294 return static_cast<uint32_t>(path_to_root->AsIntConstant()->GetValue());
7295 }
7296
7297 uint32_t GetBitstringMask() const {
7298 DCHECK_EQ(GetTypeCheckKind(), TypeCheckKind::kBitstringCheck);
7299 HInstruction* mask = InputAt(3);
7300 DCHECK(mask->IsIntConstant());
7301 return static_cast<uint32_t>(mask->AsIntConstant()->GetValue());
7302 }
7303
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007304 bool IsClonable() const override { return true; }
7305 bool CanBeMoved() const override { return true; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007306
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007307 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Marko175e7862018-03-27 09:03:13 +00007308 DCHECK(other->IsInstanceOf() || other->IsCheckCast()) << other->DebugName();
7309 return GetPackedFields() == down_cast<const HTypeCheckInstruction*>(other)->GetPackedFields();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007310 }
7311
Andreas Gampe3fbd3ad2018-03-26 21:14:46 +00007312 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
7313 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
7314 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
7315 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
7316
Vladimir Marko175e7862018-03-27 09:03:13 +00007317 ReferenceTypeInfo GetTargetClassRTI() {
7318 if (GetPackedFlag<kFlagValidTargetClassRTI>()) {
7319 // Note: The is_exact flag from the return value should not be used.
Andreas Gampe3db70682018-12-26 15:12:03 -08007320 return ReferenceTypeInfo::CreateUnchecked(klass_, /* is_exact= */ true);
Vladimir Marko175e7862018-03-27 09:03:13 +00007321 } else {
7322 return ReferenceTypeInfo::CreateInvalid();
7323 }
7324 }
7325
7326 // Target class RTI is marked as valid by RTP if the klass_ is admissible.
7327 void SetValidTargetClassRTI() REQUIRES_SHARED(Locks::mutator_lock_) {
7328 DCHECK(klass_ != nullptr);
7329 SetPackedFlag<kFlagValidTargetClassRTI>(true);
7330 }
7331
7332 Handle<mirror::Class> GetClass() const {
7333 return klass_;
7334 }
7335
7336 protected:
7337 DEFAULT_COPY_CONSTRUCTOR(TypeCheckInstruction);
7338
7339 private:
7340 static constexpr size_t kFieldTypeCheckKind = kNumberOfGenericPackedBits;
7341 static constexpr size_t kFieldTypeCheckKindSize =
7342 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
7343 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
7344 static constexpr size_t kFlagValidTargetClassRTI = kFlagMustDoNullCheck + 1;
7345 static constexpr size_t kNumberOfInstanceOfPackedBits = kFlagValidTargetClassRTI + 1;
7346 static_assert(kNumberOfInstanceOfPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
7347 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
7348
7349 Handle<mirror::Class> klass_;
7350};
7351
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007352class HInstanceOf final : public HTypeCheckInstruction {
Vladimir Marko175e7862018-03-27 09:03:13 +00007353 public:
7354 HInstanceOf(HInstruction* object,
7355 HInstruction* target_class_or_null,
7356 TypeCheckKind check_kind,
7357 Handle<mirror::Class> klass,
7358 uint32_t dex_pc,
7359 ArenaAllocator* allocator,
7360 HIntConstant* bitstring_path_to_root,
7361 HIntConstant* bitstring_mask)
7362 : HTypeCheckInstruction(kInstanceOf,
Vladimir Markobd785672018-05-03 17:09:09 +01007363 DataType::Type::kBool,
Vladimir Marko175e7862018-03-27 09:03:13 +00007364 object,
7365 target_class_or_null,
7366 check_kind,
7367 klass,
7368 dex_pc,
7369 allocator,
7370 bitstring_path_to_root,
7371 bitstring_mask,
7372 SideEffectsForArchRuntimeCalls(check_kind)) {}
7373
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007374 bool IsClonable() const override { return true; }
Artem Serova6e26142018-06-19 14:55:17 +01007375
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007376 bool NeedsEnvironment() const override {
Vladimir Marko175e7862018-03-27 09:03:13 +00007377 return CanCallRuntime(GetTypeCheckKind());
7378 }
7379
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00007380 static bool CanCallRuntime(TypeCheckKind check_kind) {
Vladimir Marko54f4fbd2020-02-12 10:52:22 +00007381 // TODO: Re-evaluate now that mips codegen has been removed.
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00007382 return check_kind != TypeCheckKind::kExactCheck;
7383 }
7384
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007385 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00007386 return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01007387 }
7388
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007389 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007390
Artem Serovcced8ba2017-07-19 18:18:09 +01007391 protected:
7392 DEFAULT_COPY_CONSTRUCTOR(InstanceOf);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007393};
7394
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007395class HBoundType final : public HExpression<1> {
Calin Juravleb1498f62015-02-16 13:13:29 +00007396 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -07007397 explicit HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307398 : HExpression(kBoundType, DataType::Type::kReference, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00007399 upper_bound_(ReferenceTypeInfo::CreateInvalid()) {
7400 SetPackedFlag<kFlagUpperCanBeNull>(true);
7401 SetPackedFlag<kFlagCanBeNull>(true);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007402 DCHECK_EQ(input->GetType(), DataType::Type::kReference);
Calin Juravleb1498f62015-02-16 13:13:29 +00007403 SetRawInputAt(0, input);
7404 }
7405
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007406 bool InstructionDataEquals(const HInstruction* other) const override;
7407 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01007408
David Brazdilf5552582015-12-27 13:36:12 +00007409 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007410 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00007411 bool GetUpperCanBeNull() const { return GetPackedFlag<kFlagUpperCanBeNull>(); }
David Brazdilf5552582015-12-27 13:36:12 +00007412 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00007413
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007414 void SetCanBeNull(bool can_be_null) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007415 DCHECK(GetUpperCanBeNull() || !can_be_null);
7416 SetPackedFlag<kFlagCanBeNull>(can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00007417 }
7418
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007419 bool CanBeNull() const override { return GetPackedFlag<kFlagCanBeNull>(); }
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007420
Calin Juravleb1498f62015-02-16 13:13:29 +00007421 DECLARE_INSTRUCTION(BoundType);
7422
Artem Serovcced8ba2017-07-19 18:18:09 +01007423 protected:
7424 DEFAULT_COPY_CONSTRUCTOR(BoundType);
7425
Calin Juravleb1498f62015-02-16 13:13:29 +00007426 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007427 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
7428 // is false then CanBeNull() cannot be true).
Vladimir Markobd785672018-05-03 17:09:09 +01007429 static constexpr size_t kFlagUpperCanBeNull = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00007430 static constexpr size_t kFlagCanBeNull = kFlagUpperCanBeNull + 1;
7431 static constexpr size_t kNumberOfBoundTypePackedBits = kFlagCanBeNull + 1;
7432 static_assert(kNumberOfBoundTypePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
7433
Calin Juravleb1498f62015-02-16 13:13:29 +00007434 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007435 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
7436 // It is used to bound the type in cases like:
7437 // if (x instanceof ClassX) {
7438 // // uper_bound_ will be ClassX
7439 // }
David Brazdilf5552582015-12-27 13:36:12 +00007440 ReferenceTypeInfo upper_bound_;
Calin Juravleb1498f62015-02-16 13:13:29 +00007441};
7442
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007443class HCheckCast final : public HTypeCheckInstruction {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007444 public:
7445 HCheckCast(HInstruction* object,
Vladimir Marko175e7862018-03-27 09:03:13 +00007446 HInstruction* target_class_or_null,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007447 TypeCheckKind check_kind,
Vladimir Marko175e7862018-03-27 09:03:13 +00007448 Handle<mirror::Class> klass,
7449 uint32_t dex_pc,
7450 ArenaAllocator* allocator,
7451 HIntConstant* bitstring_path_to_root,
7452 HIntConstant* bitstring_mask)
7453 : HTypeCheckInstruction(kCheckCast,
Vladimir Markobd785672018-05-03 17:09:09 +01007454 DataType::Type::kVoid,
Vladimir Marko175e7862018-03-27 09:03:13 +00007455 object,
7456 target_class_or_null,
7457 check_kind,
7458 klass,
7459 dex_pc,
7460 allocator,
7461 bitstring_path_to_root,
7462 bitstring_mask,
7463 SideEffects::CanTriggerGC()) {}
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007464
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007465 bool IsClonable() const override { return true; }
7466 bool NeedsEnvironment() const override {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007467 // Instruction may throw a CheckCastError.
7468 return true;
7469 }
7470
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007471 bool CanThrow() const override { return true; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007472
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007473 DECLARE_INSTRUCTION(CheckCast);
7474
Artem Serovcced8ba2017-07-19 18:18:09 +01007475 protected:
7476 DEFAULT_COPY_CONSTRUCTOR(CheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007477};
7478
Andreas Gampe26de38b2016-07-27 17:53:11 -07007479/**
7480 * @brief Memory barrier types (see "The JSR-133 Cookbook for Compiler Writers").
7481 * @details We define the combined barrier types that are actually required
7482 * by the Java Memory Model, rather than using exactly the terminology from
7483 * the JSR-133 cookbook. These should, in many cases, be replaced by acquire/release
7484 * primitives. Note that the JSR-133 cookbook generally does not deal with
7485 * store atomicity issues, and the recipes there are not always entirely sufficient.
7486 * The current recipe is as follows:
7487 * -# Use AnyStore ~= (LoadStore | StoreStore) ~= release barrier before volatile store.
7488 * -# Use AnyAny barrier after volatile store. (StoreLoad is as expensive.)
7489 * -# Use LoadAny barrier ~= (LoadLoad | LoadStore) ~= acquire barrier after each volatile load.
7490 * -# Use StoreStore barrier after all stores but before return from any constructor whose
7491 * class has final fields.
7492 * -# Use NTStoreStore to order non-temporal stores with respect to all later
7493 * store-to-memory instructions. Only generated together with non-temporal stores.
7494 */
7495enum MemBarrierKind {
7496 kAnyStore,
7497 kLoadAny,
7498 kStoreStore,
7499 kAnyAny,
7500 kNTStoreStore,
7501 kLastBarrierKind = kNTStoreStore
7502};
Vladimir Marko9974e3c2020-06-10 16:27:06 +01007503std::ostream& operator<<(std::ostream& os, MemBarrierKind kind);
Andreas Gampe26de38b2016-07-27 17:53:11 -07007504
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007505class HMemoryBarrier final : public HExpression<0> {
Calin Juravle27df7582015-04-17 19:12:31 +01007506 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06007507 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01007508 : HExpression(kMemoryBarrier,
7509 SideEffects::AllWritesAndReads(), // Assume write/read on all fields/arrays.
7510 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007511 SetPackedField<BarrierKindField>(barrier_kind);
7512 }
Calin Juravle27df7582015-04-17 19:12:31 +01007513
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007514 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01007515
Vladimir Markoa1de9182016-02-25 11:37:38 +00007516 MemBarrierKind GetBarrierKind() { return GetPackedField<BarrierKindField>(); }
Calin Juravle27df7582015-04-17 19:12:31 +01007517
7518 DECLARE_INSTRUCTION(MemoryBarrier);
7519
Artem Serovcced8ba2017-07-19 18:18:09 +01007520 protected:
7521 DEFAULT_COPY_CONSTRUCTOR(MemoryBarrier);
7522
Calin Juravle27df7582015-04-17 19:12:31 +01007523 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007524 static constexpr size_t kFieldBarrierKind = HInstruction::kNumberOfGenericPackedBits;
7525 static constexpr size_t kFieldBarrierKindSize =
7526 MinimumBitsToStore(static_cast<size_t>(kLastBarrierKind));
7527 static constexpr size_t kNumberOfMemoryBarrierPackedBits =
7528 kFieldBarrierKind + kFieldBarrierKindSize;
7529 static_assert(kNumberOfMemoryBarrierPackedBits <= kMaxNumberOfPackedBits,
7530 "Too many packed fields.");
7531 using BarrierKindField = BitField<MemBarrierKind, kFieldBarrierKind, kFieldBarrierKindSize>;
Calin Juravle27df7582015-04-17 19:12:31 +01007532};
7533
Igor Murashkind01745e2017-04-05 16:40:31 -07007534// A constructor fence orders all prior stores to fields that could be accessed via a final field of
7535// the specified object(s), with respect to any subsequent store that might "publish"
7536// (i.e. make visible) the specified object to another thread.
7537//
7538// JLS 17.5.1 "Semantics of final fields" states that a freeze action happens
7539// for all final fields (that were set) at the end of the invoked constructor.
7540//
7541// The constructor fence models the freeze actions for the final fields of an object
7542// being constructed (semantically at the end of the constructor). Constructor fences
7543// have a per-object affinity; two separate objects being constructed get two separate
7544// constructor fences.
7545//
7546// (Note: that if calling a super-constructor or forwarding to another constructor,
7547// the freezes would happen at the end of *that* constructor being invoked).
7548//
7549// The memory model guarantees that when the object being constructed is "published" after
7550// constructor completion (i.e. escapes the current thread via a store), then any final field
7551// writes must be observable on other threads (once they observe that publication).
7552//
7553// Further, anything written before the freeze, and read by dereferencing through the final field,
7554// must also be visible (so final object field could itself have an object with non-final fields;
7555// yet the freeze must also extend to them).
7556//
7557// Constructor example:
7558//
7559// class HasFinal {
7560// final int field; Optimizing IR for <init>()V:
7561// HasFinal() {
7562// field = 123; HInstanceFieldSet(this, HasFinal.field, 123)
7563// // freeze(this.field); HConstructorFence(this)
7564// } HReturn
7565// }
7566//
7567// HConstructorFence can serve double duty as a fence for new-instance/new-array allocations of
7568// already-initialized classes; in that case the allocation must act as a "default-initializer"
7569// of the object which effectively writes the class pointer "final field".
7570//
7571// For example, we can model default-initialiation as roughly the equivalent of the following:
7572//
7573// class Object {
7574// private final Class header;
7575// }
7576//
7577// Java code: Optimizing IR:
7578//
7579// T new_instance<T>() {
7580// Object obj = allocate_memory(T.class.size); obj = HInvoke(art_quick_alloc_object, T)
7581// obj.header = T.class; // header write is done by above call.
7582// // freeze(obj.header) HConstructorFence(obj)
7583// return (T)obj;
7584// }
7585//
7586// See also:
Vladimir Markoc1c34522018-10-31 13:56:49 +00007587// * DexCompilationUnit::RequiresConstructorBarrier
Igor Murashkind01745e2017-04-05 16:40:31 -07007588// * QuasiAtomic::ThreadFenceForConstructor
7589//
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007590class HConstructorFence final : public HVariableInputSizeInstruction {
Igor Murashkind01745e2017-04-05 16:40:31 -07007591 // A fence has variable inputs because the inputs can be removed
7592 // after prepare_for_register_allocation phase.
7593 // (TODO: In the future a fence could freeze multiple objects
7594 // after merging two fences together.)
7595 public:
7596 // `fence_object` is the reference that needs to be protected for correct publication.
7597 //
7598 // It makes sense in the following situations:
7599 // * <init> constructors, it's the "this" parameter (i.e. HParameterValue, s.t. IsThis() == true).
7600 // * new-instance-like instructions, it's the return value (i.e. HNewInstance).
7601 //
7602 // After construction the `fence_object` becomes the 0th input.
7603 // This is not an input in a real sense, but just a convenient place to stash the information
7604 // about the associated object.
7605 HConstructorFence(HInstruction* fence_object,
7606 uint32_t dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007607 ArenaAllocator* allocator)
Igor Murashkind01745e2017-04-05 16:40:31 -07007608 // We strongly suspect there is not a more accurate way to describe the fine-grained reordering
7609 // constraints described in the class header. We claim that these SideEffects constraints
7610 // enforce a superset of the real constraints.
7611 //
7612 // The ordering described above is conservatively modeled with SideEffects as follows:
7613 //
7614 // * To prevent reordering of the publication stores:
7615 // ----> "Reads of objects" is the initial SideEffect.
7616 // * For every primitive final field store in the constructor:
7617 // ----> Union that field's type as a read (e.g. "Read of T") into the SideEffect.
7618 // * If there are any stores to reference final fields in the constructor:
7619 // ----> Use a more conservative "AllReads" SideEffect because any stores to any references
7620 // that are reachable from `fence_object` also need to be prevented for reordering
7621 // (and we do not want to do alias analysis to figure out what those stores are).
7622 //
7623 // In the implementation, this initially starts out as an "all reads" side effect; this is an
7624 // even more conservative approach than the one described above, and prevents all of the
7625 // above reordering without analyzing any of the instructions in the constructor.
7626 //
7627 // If in a later phase we discover that there are no writes to reference final fields,
7628 // we can refine the side effect to a smaller set of type reads (see above constraints).
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307629 : HVariableInputSizeInstruction(kConstructorFence,
7630 SideEffects::AllReads(),
Igor Murashkind01745e2017-04-05 16:40:31 -07007631 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007632 allocator,
Andreas Gampe3db70682018-12-26 15:12:03 -08007633 /* number_of_inputs= */ 1,
Igor Murashkind01745e2017-04-05 16:40:31 -07007634 kArenaAllocConstructorFenceInputs) {
7635 DCHECK(fence_object != nullptr);
7636 SetRawInputAt(0, fence_object);
7637 }
7638
7639 // The object associated with this constructor fence.
7640 //
7641 // (Note: This will be null after the prepare_for_register_allocation phase,
7642 // as all constructor fence inputs are removed there).
7643 HInstruction* GetFenceObject() const {
7644 return InputAt(0);
7645 }
7646
7647 // Find all the HConstructorFence uses (`fence_use`) for `this` and:
7648 // - Delete `fence_use` from `this`'s use list.
7649 // - Delete `this` from `fence_use`'s inputs list.
7650 // - If the `fence_use` is dead, remove it from the graph.
7651 //
7652 // A fence is considered dead once it no longer has any uses
7653 // and all of the inputs are dead.
7654 //
7655 // This must *not* be called during/after prepare_for_register_allocation,
7656 // because that removes all the inputs to the fences but the fence is actually
7657 // still considered live.
Igor Murashkin6ef45672017-08-08 13:59:55 -07007658 //
7659 // Returns how many HConstructorFence instructions were removed from graph.
7660 static size_t RemoveConstructorFences(HInstruction* instruction);
Igor Murashkind01745e2017-04-05 16:40:31 -07007661
Igor Murashkindd018df2017-08-09 10:38:31 -07007662 // Combine all inputs of `this` and `other` instruction and remove
7663 // `other` from the graph.
7664 //
7665 // Inputs are unique after the merge.
7666 //
7667 // Requirement: `this` must not be the same as `other.
7668 void Merge(HConstructorFence* other);
7669
Igor Murashkin79d8fa72017-04-18 09:37:23 -07007670 // Check if this constructor fence is protecting
7671 // an HNewInstance or HNewArray that is also the immediate
7672 // predecessor of `this`.
7673 //
Igor Murashkindd018df2017-08-09 10:38:31 -07007674 // If `ignore_inputs` is true, then the immediate predecessor doesn't need
7675 // to be one of the inputs of `this`.
7676 //
Igor Murashkin79d8fa72017-04-18 09:37:23 -07007677 // Returns the associated HNewArray or HNewInstance,
7678 // or null otherwise.
Igor Murashkindd018df2017-08-09 10:38:31 -07007679 HInstruction* GetAssociatedAllocation(bool ignore_inputs = false);
Igor Murashkin79d8fa72017-04-18 09:37:23 -07007680
Igor Murashkind01745e2017-04-05 16:40:31 -07007681 DECLARE_INSTRUCTION(ConstructorFence);
7682
Artem Serovcced8ba2017-07-19 18:18:09 +01007683 protected:
7684 DEFAULT_COPY_CONSTRUCTOR(ConstructorFence);
Igor Murashkind01745e2017-04-05 16:40:31 -07007685};
7686
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007687class HMonitorOperation final : public HExpression<1> {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007688 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007689 enum class OperationKind {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007690 kEnter,
7691 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00007692 kLast = kExit
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007693 };
7694
7695 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01007696 : HExpression(kMonitorOperation,
7697 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
7698 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007699 SetPackedField<OperationKindField>(kind);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007700 SetRawInputAt(0, object);
7701 }
7702
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00007703 // Instruction may go into runtime, so we need an environment.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007704 bool NeedsEnvironment() const override { return true; }
David Brazdilbff75032015-07-08 17:26:51 +00007705
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007706 bool CanThrow() const override {
David Brazdilbff75032015-07-08 17:26:51 +00007707 // Verifier guarantees that monitor-exit cannot throw.
7708 // This is important because it allows the HGraphBuilder to remove
7709 // a dead throw-catch loop generated for `synchronized` blocks/methods.
7710 return IsEnter();
7711 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007712
Vladimir Markoa1de9182016-02-25 11:37:38 +00007713 OperationKind GetOperationKind() const { return GetPackedField<OperationKindField>(); }
7714 bool IsEnter() const { return GetOperationKind() == OperationKind::kEnter; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007715
7716 DECLARE_INSTRUCTION(MonitorOperation);
7717
Artem Serovcced8ba2017-07-19 18:18:09 +01007718 protected:
7719 DEFAULT_COPY_CONSTRUCTOR(MonitorOperation);
7720
Calin Juravle52c48962014-12-16 17:02:57 +00007721 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007722 static constexpr size_t kFieldOperationKind = HInstruction::kNumberOfGenericPackedBits;
7723 static constexpr size_t kFieldOperationKindSize =
7724 MinimumBitsToStore(static_cast<size_t>(OperationKind::kLast));
7725 static constexpr size_t kNumberOfMonitorOperationPackedBits =
7726 kFieldOperationKind + kFieldOperationKindSize;
7727 static_assert(kNumberOfMonitorOperationPackedBits <= HInstruction::kMaxNumberOfPackedBits,
7728 "Too many packed fields.");
7729 using OperationKindField = BitField<OperationKind, kFieldOperationKind, kFieldOperationKindSize>;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007730};
7731
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007732class HSelect final : public HExpression<3> {
David Brazdil74eb1b22015-12-14 11:44:01 +00007733 public:
7734 HSelect(HInstruction* condition,
7735 HInstruction* true_value,
7736 HInstruction* false_value,
7737 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307738 : HExpression(kSelect, HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
David Brazdil74eb1b22015-12-14 11:44:01 +00007739 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
7740
7741 // First input must be `true_value` or `false_value` to allow codegens to
7742 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
7743 // that architectures which implement HSelect as a conditional move also
7744 // will not need to invert the condition.
7745 SetRawInputAt(0, false_value);
7746 SetRawInputAt(1, true_value);
7747 SetRawInputAt(2, condition);
7748 }
7749
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007750 bool IsClonable() const override { return true; }
David Brazdil74eb1b22015-12-14 11:44:01 +00007751 HInstruction* GetFalseValue() const { return InputAt(0); }
7752 HInstruction* GetTrueValue() const { return InputAt(1); }
7753 HInstruction* GetCondition() const { return InputAt(2); }
7754
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007755 bool CanBeMoved() const override { return true; }
7756 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01007757 return true;
7758 }
David Brazdil74eb1b22015-12-14 11:44:01 +00007759
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007760 bool CanBeNull() const override {
David Brazdil74eb1b22015-12-14 11:44:01 +00007761 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
7762 }
7763
7764 DECLARE_INSTRUCTION(Select);
7765
Artem Serovcced8ba2017-07-19 18:18:09 +01007766 protected:
7767 DEFAULT_COPY_CONSTRUCTOR(Select);
David Brazdil74eb1b22015-12-14 11:44:01 +00007768};
7769
Vladimir Markof9f64412015-09-02 14:05:49 +01007770class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007771 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01007772 MoveOperands(Location source,
7773 Location destination,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007774 DataType::Type type,
Nicolas Geoffray90218252015-04-15 11:56:51 +01007775 HInstruction* instruction)
7776 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007777
7778 Location GetSource() const { return source_; }
7779 Location GetDestination() const { return destination_; }
7780
7781 void SetSource(Location value) { source_ = value; }
7782 void SetDestination(Location value) { destination_ = value; }
7783
7784 // The parallel move resolver marks moves as "in-progress" by clearing the
7785 // destination (but not the source).
7786 Location MarkPending() {
7787 DCHECK(!IsPending());
7788 Location dest = destination_;
7789 destination_ = Location::NoLocation();
7790 return dest;
7791 }
7792
7793 void ClearPending(Location dest) {
7794 DCHECK(IsPending());
7795 destination_ = dest;
7796 }
7797
7798 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00007799 DCHECK(source_.IsValid() || destination_.IsInvalid());
7800 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007801 }
7802
7803 // True if this blocks a move from the given location.
7804 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08007805 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007806 }
7807
7808 // A move is redundant if it's been eliminated, if its source and
7809 // destination are the same, or if its destination is unneeded.
7810 bool IsRedundant() const {
7811 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
7812 }
7813
7814 // We clear both operands to indicate move that's been eliminated.
7815 void Eliminate() {
7816 source_ = destination_ = Location::NoLocation();
7817 }
7818
7819 bool IsEliminated() const {
7820 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
7821 return source_.IsInvalid();
7822 }
7823
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007824 DataType::Type GetType() const { return type_; }
Alexey Frunze4dda3372015-06-01 18:31:49 -07007825
Nicolas Geoffray90218252015-04-15 11:56:51 +01007826 bool Is64BitMove() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007827 return DataType::Is64BitType(type_);
Nicolas Geoffray90218252015-04-15 11:56:51 +01007828 }
7829
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007830 HInstruction* GetInstruction() const { return instruction_; }
7831
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007832 private:
7833 Location source_;
7834 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01007835 // The type this move is for.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007836 DataType::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007837 // The instruction this move is assocatied with. Null when this move is
7838 // for moving an input in the expected locations of user (including a phi user).
7839 // This is only used in debug mode, to ensure we do not connect interval siblings
7840 // in the same parallel move.
7841 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007842};
7843
Roland Levillainc9285912015-12-18 10:38:42 +00007844std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
7845
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007846static constexpr size_t kDefaultNumberOfMoves = 4;
7847
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007848class HParallelMove final : public HExpression<0> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007849 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007850 explicit HParallelMove(ArenaAllocator* allocator, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01007851 : HExpression(kParallelMove, SideEffects::None(), dex_pc),
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007852 moves_(allocator->Adapter(kArenaAllocMoveOperands)) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007853 moves_.reserve(kDefaultNumberOfMoves);
7854 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007855
Nicolas Geoffray90218252015-04-15 11:56:51 +01007856 void AddMove(Location source,
7857 Location destination,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007858 DataType::Type type,
Nicolas Geoffray90218252015-04-15 11:56:51 +01007859 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00007860 DCHECK(source.IsValid());
7861 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00007862 if (kIsDebugBuild) {
7863 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007864 for (const MoveOperands& move : moves_) {
7865 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007866 // Special case the situation where the move is for the spill slot
7867 // of the instruction.
7868 if ((GetPrevious() == instruction)
7869 || ((GetPrevious() == nullptr)
7870 && instruction->IsPhi()
7871 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007872 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007873 << "Doing parallel moves for the same instruction.";
7874 } else {
7875 DCHECK(false) << "Doing parallel moves for the same instruction.";
7876 }
7877 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00007878 }
7879 }
Vladimir Marko225b6462015-09-28 12:17:40 +01007880 for (const MoveOperands& move : moves_) {
7881 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01007882 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01007883 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01007884 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00007885 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007886 }
Vladimir Marko225b6462015-09-28 12:17:40 +01007887 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007888 }
7889
Vladimir Marko225b6462015-09-28 12:17:40 +01007890 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007891 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007892 }
7893
Vladimir Marko225b6462015-09-28 12:17:40 +01007894 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007895
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01007896 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007897
Artem Serovcced8ba2017-07-19 18:18:09 +01007898 protected:
7899 DEFAULT_COPY_CONSTRUCTOR(ParallelMove);
7900
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007901 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01007902 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007903};
7904
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007905// This instruction computes an intermediate address pointing in the 'middle' of an object. The
7906// result pointer cannot be handled by GC, so extra care is taken to make sure that this value is
7907// never used across anything that can trigger GC.
7908// The result of this instruction is not a pointer in the sense of `DataType::Type::kreference`.
7909// So we represent it by the type `DataType::Type::kInt`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007910class HIntermediateAddress final : public HExpression<2> {
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007911 public:
7912 HIntermediateAddress(HInstruction* base_address, HInstruction* offset, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307913 : HExpression(kIntermediateAddress,
7914 DataType::Type::kInt32,
7915 SideEffects::DependsOnGC(),
7916 dex_pc) {
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007917 DCHECK_EQ(DataType::Size(DataType::Type::kInt32),
7918 DataType::Size(DataType::Type::kReference))
7919 << "kPrimInt and kPrimNot have different sizes.";
7920 SetRawInputAt(0, base_address);
7921 SetRawInputAt(1, offset);
7922 }
7923
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007924 bool IsClonable() const override { return true; }
7925 bool CanBeMoved() const override { return true; }
7926 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007927 return true;
7928 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007929 bool IsActualObject() const override { return false; }
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007930
7931 HInstruction* GetBaseAddress() const { return InputAt(0); }
7932 HInstruction* GetOffset() const { return InputAt(1); }
7933
7934 DECLARE_INSTRUCTION(IntermediateAddress);
7935
Artem Serovcced8ba2017-07-19 18:18:09 +01007936 protected:
7937 DEFAULT_COPY_CONSTRUCTOR(IntermediateAddress);
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007938};
7939
7940
Mark Mendell0616ae02015-04-17 12:49:27 -04007941} // namespace art
7942
Aart Bikf8f5a162017-02-06 15:35:29 -08007943#include "nodes_vector.h"
7944
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03007945#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
7946#include "nodes_shared.h"
7947#endif
Shalini Salomi Bodapatidd121f62018-10-26 15:03:53 +05307948#if defined(ART_ENABLE_CODEGEN_x86) || defined(ART_ENABLE_CODEGEN_x86_64)
Mark Mendell0616ae02015-04-17 12:49:27 -04007949#include "nodes_x86.h"
7950#endif
7951
Vladimir Marko0a516052019-10-14 13:00:44 +00007952namespace art {
Mark Mendell0616ae02015-04-17 12:49:27 -04007953
Igor Murashkin6ef45672017-08-08 13:59:55 -07007954class OptimizingCompilerStats;
7955
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007956class HGraphVisitor : public ValueObject {
7957 public:
Igor Murashkin6ef45672017-08-08 13:59:55 -07007958 explicit HGraphVisitor(HGraph* graph, OptimizingCompilerStats* stats = nullptr)
7959 : stats_(stats),
7960 graph_(graph) {}
Dave Allison20dfc792014-06-16 20:44:29 -07007961 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007962
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007963 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007964 virtual void VisitBasicBlock(HBasicBlock* block);
7965
Roland Levillain633021e2014-10-01 14:12:25 +01007966 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007967 void VisitInsertionOrder();
7968
Roland Levillain633021e2014-10-01 14:12:25 +01007969 // Visit the graph following dominator tree reverse post-order.
7970 void VisitReversePostOrder();
7971
Nicolas Geoffray787c3072014-03-17 10:20:19 +00007972 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007973
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007974 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007975#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007976 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
7977
7978 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
7979
7980#undef DECLARE_VISIT_INSTRUCTION
7981
Igor Murashkin6ef45672017-08-08 13:59:55 -07007982 protected:
7983 OptimizingCompilerStats* stats_;
7984
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007985 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07007986 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007987
7988 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
7989};
7990
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007991class HGraphDelegateVisitor : public HGraphVisitor {
7992 public:
Igor Murashkin6ef45672017-08-08 13:59:55 -07007993 explicit HGraphDelegateVisitor(HGraph* graph, OptimizingCompilerStats* stats = nullptr)
7994 : HGraphVisitor(graph, stats) {}
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007995 virtual ~HGraphDelegateVisitor() {}
7996
7997 // Visit functions that delegate to to super class.
7998#define DECLARE_VISIT_INSTRUCTION(name, super) \
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007999 void Visit##name(H##name* instr) override { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01008000
8001 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
8002
8003#undef DECLARE_VISIT_INSTRUCTION
8004
8005 private:
8006 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
8007};
8008
Artem Serovcced8ba2017-07-19 18:18:09 +01008009// Create a clone of the instruction, insert it into the graph; replace the old one with a new
8010// and remove the old instruction.
8011HInstruction* ReplaceInstrOrPhiByClone(HInstruction* instr);
8012
8013// Create a clone for each clonable instructions/phis and replace the original with the clone.
8014//
8015// Used for testing individual instruction cloner.
8016class CloneAndReplaceInstructionVisitor : public HGraphDelegateVisitor {
8017 public:
8018 explicit CloneAndReplaceInstructionVisitor(HGraph* graph)
Artem Serov7f4aff62017-06-21 17:02:18 +01008019 : HGraphDelegateVisitor(graph), instr_replaced_by_clones_count_(0) {}
Artem Serovcced8ba2017-07-19 18:18:09 +01008020
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01008021 void VisitInstruction(HInstruction* instruction) override {
Artem Serovcced8ba2017-07-19 18:18:09 +01008022 if (instruction->IsClonable()) {
8023 ReplaceInstrOrPhiByClone(instruction);
Artem Serov7f4aff62017-06-21 17:02:18 +01008024 instr_replaced_by_clones_count_++;
Artem Serovcced8ba2017-07-19 18:18:09 +01008025 }
8026 }
8027
Artem Serov7f4aff62017-06-21 17:02:18 +01008028 size_t GetInstrReplacedByClonesCount() const { return instr_replaced_by_clones_count_; }
Artem Serovcced8ba2017-07-19 18:18:09 +01008029
8030 private:
Artem Serov7f4aff62017-06-21 17:02:18 +01008031 size_t instr_replaced_by_clones_count_;
Artem Serovcced8ba2017-07-19 18:18:09 +01008032
8033 DISALLOW_COPY_AND_ASSIGN(CloneAndReplaceInstructionVisitor);
8034};
8035
Nicolas Geoffray82091da2015-01-26 10:02:45 +00008036// Iterator over the blocks that art part of the loop. Includes blocks part
8037// of an inner loop. The order in which the blocks are iterated is on their
8038// block id.
8039class HBlocksInLoopIterator : public ValueObject {
8040 public:
8041 explicit HBlocksInLoopIterator(const HLoopInformation& info)
8042 : blocks_in_loop_(info.GetBlocks()),
8043 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
8044 index_(0) {
8045 if (!blocks_in_loop_.IsBitSet(index_)) {
8046 Advance();
8047 }
8048 }
8049
Vladimir Markofa6b93c2015-09-15 10:15:55 +01008050 bool Done() const { return index_ == blocks_.size(); }
8051 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00008052 void Advance() {
8053 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01008054 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00008055 if (blocks_in_loop_.IsBitSet(index_)) {
8056 break;
8057 }
8058 }
8059 }
8060
8061 private:
8062 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01008063 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00008064 size_t index_;
8065
8066 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
8067};
8068
Mingyao Yang3584bce2015-05-19 16:01:59 -07008069// Iterator over the blocks that art part of the loop. Includes blocks part
8070// of an inner loop. The order in which the blocks are iterated is reverse
8071// post order.
8072class HBlocksInLoopReversePostOrderIterator : public ValueObject {
8073 public:
8074 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
8075 : blocks_in_loop_(info.GetBlocks()),
8076 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
8077 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01008078 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07008079 Advance();
8080 }
8081 }
8082
Vladimir Markofa6b93c2015-09-15 10:15:55 +01008083 bool Done() const { return index_ == blocks_.size(); }
8084 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07008085 void Advance() {
8086 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01008087 for (size_t e = blocks_.size(); index_ < e; ++index_) {
8088 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07008089 break;
8090 }
8091 }
8092 }
8093
8094 private:
8095 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01008096 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07008097 size_t index_;
8098
8099 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
8100};
8101
Aart Bikf3e61ee2017-04-12 17:09:20 -07008102// Returns int64_t value of a properly typed constant.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00008103inline int64_t Int64FromConstant(HConstant* constant) {
David Brazdilc0b601b2016-02-08 14:20:45 +00008104 if (constant->IsIntConstant()) {
8105 return constant->AsIntConstant()->GetValue();
8106 } else if (constant->IsLongConstant()) {
8107 return constant->AsLongConstant()->GetValue();
8108 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00008109 DCHECK(constant->IsNullConstant()) << constant->DebugName();
David Brazdilc0b601b2016-02-08 14:20:45 +00008110 return 0;
8111 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00008112}
8113
Aart Bikf3e61ee2017-04-12 17:09:20 -07008114// Returns true iff instruction is an integral constant (and sets value on success).
8115inline bool IsInt64AndGet(HInstruction* instruction, /*out*/ int64_t* value) {
8116 if (instruction->IsIntConstant()) {
8117 *value = instruction->AsIntConstant()->GetValue();
8118 return true;
8119 } else if (instruction->IsLongConstant()) {
8120 *value = instruction->AsLongConstant()->GetValue();
8121 return true;
8122 } else if (instruction->IsNullConstant()) {
8123 *value = 0;
8124 return true;
8125 }
8126 return false;
8127}
8128
Aart Bik0148de42017-09-05 09:25:01 -07008129// Returns true iff instruction is the given integral constant.
8130inline bool IsInt64Value(HInstruction* instruction, int64_t value) {
8131 int64_t val = 0;
8132 return IsInt64AndGet(instruction, &val) && val == value;
8133}
8134
8135// Returns true iff instruction is a zero bit pattern.
8136inline bool IsZeroBitPattern(HInstruction* instruction) {
8137 return instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern();
8138}
8139
Vladimir Marko6d5b7e32018-05-09 16:52:48 +01008140// Implement HInstruction::Is##type() for concrete instructions.
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008141#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Markoa90dd512018-05-04 15:04:45 +01008142 inline bool HInstruction::Is##type() const { return GetKind() == k##type; }
8143 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
8144#undef INSTRUCTION_TYPE_CHECK
8145
Vladimir Marko6d5b7e32018-05-09 16:52:48 +01008146// Implement HInstruction::Is##type() for abstract instructions.
Vladimir Markoa90dd512018-05-04 15:04:45 +01008147#define INSTRUCTION_TYPE_CHECK_RESULT(type, super) \
8148 std::is_base_of<BaseType, H##type>::value,
8149#define INSTRUCTION_TYPE_CHECK(type, super) \
8150 inline bool HInstruction::Is##type() const { \
8151 DCHECK_LT(GetKind(), kLastInstructionKind); \
8152 using BaseType = H##type; \
8153 static constexpr bool results[] = { \
8154 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK_RESULT) \
8155 }; \
8156 return results[static_cast<size_t>(GetKind())]; \
8157 }
8158
8159 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
8160#undef INSTRUCTION_TYPE_CHECK
Vladimir Marko6d5b7e32018-05-09 16:52:48 +01008161#undef INSTRUCTION_TYPE_CHECK_RESULT
Vladimir Markoa90dd512018-05-04 15:04:45 +01008162
8163#define INSTRUCTION_TYPE_CAST(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008164 inline const H##type* HInstruction::As##type() const { \
8165 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
8166 } \
8167 inline H##type* HInstruction::As##type() { \
8168 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
8169 }
8170
Vladimir Markoa90dd512018-05-04 15:04:45 +01008171 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CAST)
8172#undef INSTRUCTION_TYPE_CAST
8173
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008174
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00008175// Create space in `blocks` for adding `number_of_new_blocks` entries
8176// starting at location `at`. Blocks after `at` are moved accordingly.
8177inline void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks,
8178 size_t number_of_new_blocks,
8179 size_t after) {
8180 DCHECK_LT(after, blocks->size());
8181 size_t old_size = blocks->size();
8182 size_t new_size = old_size + number_of_new_blocks;
8183 blocks->resize(new_size);
8184 std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end());
8185}
8186
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00008187/*
8188 * Hunt "under the hood" of array lengths (leading to array references),
8189 * null checks (also leading to array references), and new arrays
8190 * (leading to the actual length). This makes it more likely related
8191 * instructions become actually comparable.
8192 */
8193inline HInstruction* HuntForDeclaration(HInstruction* instruction) {
8194 while (instruction->IsArrayLength() ||
8195 instruction->IsNullCheck() ||
8196 instruction->IsNewArray()) {
8197 instruction = instruction->IsNewArray()
8198 ? instruction->AsNewArray()->GetLength()
8199 : instruction->InputAt(0);
8200 }
8201 return instruction;
8202}
8203
Artem Serovb47b9782019-12-04 21:02:09 +00008204inline bool IsAddOrSub(const HInstruction* instruction) {
8205 return instruction->IsAdd() || instruction->IsSub();
8206}
8207
Artem Serov21c7e6f2017-07-27 16:04:42 +01008208void RemoveEnvironmentUses(HInstruction* instruction);
8209bool HasEnvironmentUsedByOthers(HInstruction* instruction);
8210void ResetEnvironmentInputRecords(HInstruction* instruction);
8211
Nicolas Geoffray818f2102014-02-18 16:43:35 +00008212} // namespace art
8213
8214#endif // ART_COMPILER_OPTIMIZING_NODES_H_